blob: aa9e98c64bfd7b40438baa28f2bb89596d375868 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIO_TRACK_SHARED_H
18#define ANDROID_AUDIO_TRACK_SHARED_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
Glenn Kastenc56f3422014-03-21 17:53:17 -070023#include <audio_utils/minifloat.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080024#include <utils/threads.h>
Glenn Kastene3aa6592012-12-04 12:22:46 -080025#include <utils/Log.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080026#include <utils/RefBase.h>
Glenn Kasten53dbe772015-01-06 10:46:38 -080027#include <audio_utils/roundup.h>
Andy Hung8edb8dc2015-03-26 19:13:55 -070028#include <media/AudioResamplerPublic.h>
Andy Hung3f0c9022016-01-15 17:49:46 -080029#include <media/AudioTimestamp.h>
Andy Hung90e8a972015-11-09 16:42:40 -080030#include <media/Modulo.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080031#include <media/SingleStateQueue.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032
33namespace android {
34
35// ----------------------------------------------------------------------------
36
Glenn Kasten96f60d82013-07-12 10:21:18 -070037// for audio_track_cblk_t::mFlags
Glenn Kasten9f80dd22012-12-18 15:57:32 -080038#define CBLK_UNDERRUN 0x01 // set by server immediately on output underrun, cleared by client
Glenn Kasten864585d2012-11-06 16:15:41 -080039#define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger,
Glenn Kasten9c5fdd82012-11-05 13:38:15 -080040 // clear: track is ready when buffer full
Glenn Kasten864585d2012-11-06 16:15:41 -080041#define CBLK_INVALID 0x04 // track buffer invalidated by AudioFlinger, need to re-create
Glenn Kasten9f80dd22012-12-18 15:57:32 -080042#define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun,
43 // need to re-start. Unlike CBLK_UNDERRUN, this is not set
44 // immediately, but only after a long string of underruns.
45// 0x10 unused
46#define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes
47#define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes
48#define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping
49#define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client
50#define CBLK_INTERRUPT 0x200 // set by client on interrupt(), cleared by client in obtainBuffer()
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000051#define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client
52
53//EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation
Glenn Kastene198c362013-08-13 09:13:36 -070054#define MAX_RUN_OFFLOADED_TIMEOUT_MS 20000 // assuming up to a maximum of 20 seconds of offloaded
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055
Glenn Kastene3aa6592012-12-04 12:22:46 -080056struct AudioTrackSharedStreaming {
57 // similar to NBAIO MonoPipe
Glenn Kasten9f80dd22012-12-18 15:57:32 -080058 // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2
Glenn Kastenf59497b2015-01-26 16:35:47 -080059 volatile int32_t mFront; // read by consumer (output: server, input: client)
60 volatile int32_t mRear; // written by producer (output: client, input: server)
Glenn Kasten9f80dd22012-12-18 15:57:32 -080061 volatile int32_t mFlush; // incremented by client to indicate a request to flush;
62 // server notices and discards all data between mFront and mRear
Phil Burk2812d9e2016-01-04 10:34:30 -080063 volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame
64 volatile uint32_t mUnderrunCount; // server increments for each underrun occurrence
Glenn Kastene3aa6592012-12-04 12:22:46 -080065};
66
Andy Hung9b461582014-12-01 17:56:29 -080067// Represents a single state of an AudioTrack that was created in static mode (shared memory buffer
68// supplied by the client). This state needs to be communicated from the client to server. As this
69// state is too large to be updated atomically without a mutex, and mutexes aren't allowed here, the
70// state is wrapped by a SingleStateQueue.
71struct StaticAudioTrackState {
72 // Do not define constructors, destructors, or virtual methods as this is part of a
73 // union in shared memory and they will not get called properly.
74
75 // These fields should both be size_t, but since they are located in shared memory we
76 // force to 32-bit. The client and server may have different typedefs for size_t.
77
78 // The state has a sequence counter to indicate whether changes are made to loop or position.
79 // The sequence counter also currently indicates whether loop or position is first depending
80 // on which is greater; it jumps by max(mLoopSequence, mPositionSequence) + 1.
81
82 uint32_t mLoopStart;
83 uint32_t mLoopEnd;
84 int32_t mLoopCount;
85 uint32_t mLoopSequence; // a sequence counter to indicate changes to loop
86 uint32_t mPosition;
87 uint32_t mPositionSequence; // a sequence counter to indicate changes to position
88};
89
Glenn Kasten9f80dd22012-12-18 15:57:32 -080090typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue;
91
Andy Hung4ede21d2014-12-12 15:37:34 -080092struct StaticAudioTrackPosLoop {
93 // Do not define constructors, destructors, or virtual methods as this is part of a
94 // union in shared memory and will not get called properly.
95
96 // These fields should both be size_t, but since they are located in shared memory we
97 // force to 32-bit. The client and server may have different typedefs for size_t.
98
99 // This struct information is stored in a single state queue to communicate the
100 // static AudioTrack server state to the client while data is consumed.
101 // It is smaller than StaticAudioTrackState to prevent unnecessary information from
102 // being sent.
103
104 uint32_t mBufferPosition;
105 int32_t mLoopCount;
106};
107
108typedef SingleStateQueue<StaticAudioTrackPosLoop> StaticAudioTrackPosLoopQueue;
109
Glenn Kastene3aa6592012-12-04 12:22:46 -0800110struct AudioTrackSharedStatic {
Andy Hung4ede21d2014-12-12 15:37:34 -0800111 // client requests to the server for loop or position changes.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800112 StaticAudioTrackSingleStateQueue::Shared
113 mSingleStateQueue;
Andy Hung4ede21d2014-12-12 15:37:34 -0800114 // position info updated asynchronously by server and read by client,
115 // "for entertainment purposes only"
116 StaticAudioTrackPosLoopQueue::Shared
117 mPosLoopQueue;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800118};
119
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700120typedef SingleStateQueue<AudioPlaybackRate> PlaybackRateQueue;
Andy Hung8edb8dc2015-03-26 19:13:55 -0700121
Andy Hung3f0c9022016-01-15 17:49:46 -0800122typedef SingleStateQueue<ExtendedTimestamp> ExtendedTimestampQueue;
123
Glenn Kastene3aa6592012-12-04 12:22:46 -0800124// ----------------------------------------------------------------------------
125
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -0800126// Important: do not add any virtual methods, including ~
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127struct audio_track_cblk_t
128{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800129 // Since the control block is always located in shared memory, this constructor
130 // is only used for placement new(). It is never used for regular new() or stack.
131 audio_track_cblk_t();
132 /*virtual*/ ~audio_track_cblk_t() { }
133
Glenn Kastene3aa6592012-12-04 12:22:46 -0800134 friend class Proxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 friend class ClientProxy;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800136 friend class AudioTrackClientProxy;
137 friend class AudioRecordClientProxy;
138 friend class ServerProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800139 friend class AudioTrackServerProxy;
140 friend class AudioRecordServerProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141
142 // The data members are grouped so that members accessed frequently and in the same context
143 // are in the same line of data cache.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800144
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700145 uint32_t mServer; // Number of filled frames consumed by server (mIsOut),
146 // or filled frames provided by server (!mIsOut).
147 // It is updated asynchronously by server without a barrier.
Glenn Kastenb187de12014-12-30 08:18:15 -0800148 // The value should be used
149 // "for entertainment purposes only",
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700150 // which means don't make important decisions based on it.
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800151
Glenn Kasten74935e42013-12-19 08:56:45 -0800152 uint32_t mPad1; // unused
Glenn Kasten99e53b82012-01-19 08:59:58 -0800153
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700154 volatile int32_t mFutex; // event flag: down (P) by client,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800155 // up (V) by server or binderDied() or interrupt()
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700156#define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending
Glenn Kasten99e53b82012-01-19 08:59:58 -0800157
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800158private:
159
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800160 // This field should be a size_t, but since it is located in shared memory we
161 // force to 32-bit. The client and server may have different typedefs for size_t.
162 uint32_t mMinimum; // server wakes up client if available >= mMinimum
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800163
Glenn Kastenc56f3422014-03-21 17:53:17 -0700164 // Stereo gains for AudioTrack only, not used by AudioRecord.
165 gain_minifloat_packed_t mVolumeLR;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800166
Glenn Kastene3aa6592012-12-04 12:22:46 -0800167 uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz
168 // or 0 == default. Write-only client, read-only server.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800169
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700170 PlaybackRateQueue::Shared mPlaybackRateQueue;
Andy Hung8edb8dc2015-03-26 19:13:55 -0700171
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800172 // client write-only, server read-only
173 uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0
174
Glenn Kastend054c322013-07-12 12:59:20 -0700175 uint16_t mPad2; // unused
Eric Laurentd1b449a2010-05-14 03:26:45 -0700176
Andy Hung3f0c9022016-01-15 17:49:46 -0800177 // server write-only, client read
Eric Laurent8ce8e262016-02-16 11:59:23 -0800178 ExtendedTimestampQueue::Shared mExtendedTimestampQueue;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800179public:
Glenn Kasten99e53b82012-01-19 08:59:58 -0800180
Glenn Kasten96f60d82013-07-12 10:21:18 -0700181 volatile int32_t mFlags; // combinations of CBLK_*
Eric Laurent38ccae22011-03-28 18:37:07 -0700182
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800183public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800184 union {
185 AudioTrackSharedStreaming mStreaming;
186 AudioTrackSharedStatic mStatic;
187 int mAlign[8];
188 } u;
189
190 // Cache line boundary (32 bytes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191};
192
Glenn Kastene3aa6592012-12-04 12:22:46 -0800193// ----------------------------------------------------------------------------
194
195// Proxy for shared memory control block, to isolate callers from needing to know the details.
196// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
197// The proxies are located in normal memory, and are not multi-thread safe within a given side.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800198class Proxy : public RefBase {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800199protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800200 Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
201 bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800202 virtual ~Proxy() { }
203
204public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800205 struct Buffer {
206 size_t mFrameCount; // number of frames available in this buffer
207 void* mRaw; // pointer to first frame
208 size_t mNonContig; // number of additional non-contiguous frames available
209 };
Glenn Kastene3aa6592012-12-04 12:22:46 -0800210
Phil Burkc0adecb2016-01-08 12:44:11 -0800211 size_t frameCount() const { return mFrameCount; }
212
Glenn Kastene3aa6592012-12-04 12:22:46 -0800213protected:
214 // These refer to shared memory, and are virtual addresses with respect to the current process.
215 // They may have different virtual addresses within the other process.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800216 audio_track_cblk_t* const mCblk; // the control block
217 void* const mBuffers; // starting address of buffers
Glenn Kastene3aa6592012-12-04 12:22:46 -0800218
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800219 const size_t mFrameCount; // not necessarily a power of 2
220 const size_t mFrameSize; // in bytes
221 const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
222 const bool mIsOut; // true for AudioTrack, false for AudioRecord
223 const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
224 bool mIsShutdown; // latch set to true when shared memory corruption detected
Glenn Kasten7db7df02013-06-25 16:13:23 -0700225 size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
Glenn Kastene3aa6592012-12-04 12:22:46 -0800226};
227
228// ----------------------------------------------------------------------------
229
230// Proxy seen by AudioTrack client and AudioRecord client
231class ClientProxy : public Proxy {
Eric Laurent83b88082014-06-20 18:31:16 -0700232public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800233 ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
234 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800235 virtual ~ClientProxy() { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800236
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800237 static const struct timespec kForever;
238 static const struct timespec kNonBlocking;
239
240 // Obtain a buffer with filled frames (reading) or empty frames (writing).
241 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
242 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
243 // sets or extends the unreleased frame count.
244 // On entry:
245 // buffer->mFrameCount should be initialized to maximum number of desired frames,
246 // which must be > 0.
247 // buffer->mNonContig is unused.
248 // buffer->mRaw is unused.
249 // requested is the requested timeout in local monotonic delta time units:
250 // NULL or &kNonBlocking means non-blocking (zero timeout).
251 // &kForever means block forever (infinite timeout).
252 // Other values mean a specific timeout in local monotonic delta time units.
253 // elapsed is a pointer to a location that will hold the total local monotonic time that
254 // elapsed while blocked, or NULL if not needed.
255 // On exit:
256 // buffer->mFrameCount has the actual number of contiguous available frames,
257 // which is always 0 when the return status != NO_ERROR.
258 // buffer->mNonContig is the number of additional non-contiguous available frames.
259 // buffer->mRaw is a pointer to the first available frame,
260 // or NULL when buffer->mFrameCount == 0.
261 // The return status is one of:
262 // NO_ERROR Success, buffer->mFrameCount > 0.
263 // WOULD_BLOCK Non-blocking mode and no frames are available.
264 // TIMED_OUT Timeout occurred before any frames became available.
265 // This can happen even for infinite timeout, due to a spurious wakeup.
266 // In this case, the caller should investigate and then re-try as appropriate.
267 // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
268 // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again.
269 // NO_INIT Shared memory is corrupt.
Glenn Kasten7db7df02013-06-25 16:13:23 -0700270 // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800271 status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
272 struct timespec *elapsed = NULL);
273
274 // Release (some of) the frames last obtained.
275 // On entry, buffer->mFrameCount should have the number of frames to release,
276 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
277 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
278 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
279 // On exit:
280 // buffer->mFrameCount is zero.
281 // buffer->mRaw is NULL.
282 void releaseBuffer(Buffer* buffer);
283
284 // Call after detecting server's death
285 void binderDied();
286
287 // Call to force an obtainBuffer() to return quickly with -EINTR
288 void interrupt();
289
Andy Hung90e8a972015-11-09 16:42:40 -0800290 Modulo<uint32_t> getPosition() {
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700291 return mEpoch + mCblk->mServer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800292 }
293
Phil Burkc0adecb2016-01-08 12:44:11 -0800294 void setEpoch(const Modulo<uint32_t> &epoch) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800295 mEpoch = epoch;
296 }
297
298 void setMinimum(size_t minimum) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800299 // This can only happen on a 64-bit client
300 if (minimum > UINT32_MAX) {
301 minimum = UINT32_MAX;
302 }
303 mCblk->mMinimum = (uint32_t) minimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800304 }
305
306 // Return the number of frames that would need to be obtained and released
307 // in order for the client to be aligned at start of buffer
308 virtual size_t getMisalignment();
309
Andy Hung90e8a972015-11-09 16:42:40 -0800310 Modulo<uint32_t> getEpoch() const {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800311 return mEpoch;
312 }
313
Phil Burkc0adecb2016-01-08 12:44:11 -0800314 size_t getBufferSizeInFrames() const { return mBufferSizeInFrames; }
315 // See documentation for AudioTrack.setBufferSizeInFrames()
316 size_t setBufferSizeInFrames(size_t requestedSize);
317
Andy Hung6ae58432016-02-16 18:32:24 -0800318 status_t getTimestamp(ExtendedTimestamp *timestamp) {
319 if (timestamp == nullptr) {
320 return BAD_VALUE;
321 }
322 (void) mTimestampObserver.poll(mTimestamp);
323 *timestamp = mTimestamp;
324 return OK;
325 }
326
327 void clearTimestamp() {
328 mTimestamp.clear();
329 }
330
Phil Burkc0adecb2016-01-08 12:44:11 -0800331protected:
332 // This is set by AudioTrack.setBufferSizeInFrames().
333 // A write will not fill the buffer above this limit.
334 size_t mBufferSizeInFrames; // effective size of the buffer
335
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800336private:
Andy Hung90e8a972015-11-09 16:42:40 -0800337 Modulo<uint32_t> mEpoch;
Andy Hung6ae58432016-02-16 18:32:24 -0800338
339 // The shared buffer contents referred to by the timestamp observer
340 // is initialized when the server proxy created. A local zero timestamp
341 // is initialized by the client constructor.
342 ExtendedTimestampQueue::Observer mTimestampObserver;
343 ExtendedTimestamp mTimestamp; // initialized by constructor
Glenn Kastene3aa6592012-12-04 12:22:46 -0800344};
345
346// ----------------------------------------------------------------------------
347
348// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
349class AudioTrackClientProxy : public ClientProxy {
350public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800351 AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
352 size_t frameSize, bool clientInServer = false)
353 : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
Andy Hung8edb8dc2015-03-26 19:13:55 -0700354 clientInServer),
Andy Hung6ae58432016-02-16 18:32:24 -0800355 mPlaybackRateMutator(&cblk->mPlaybackRateQueue) {
356 }
357
Glenn Kastene3aa6592012-12-04 12:22:46 -0800358 virtual ~AudioTrackClientProxy() { }
359
360 // No barriers on the following operations, so the ordering of loads/stores
361 // with respect to other parameters is UNPREDICTABLE. That's considered safe.
362
363 // caller must limit to 0.0 <= sendLevel <= 1.0
364 void setSendLevel(float sendLevel) {
365 mCblk->mSendLevel = uint16_t(sendLevel * 0x1000);
366 }
367
Glenn Kastenc56f3422014-03-21 17:53:17 -0700368 // set stereo gains
369 void setVolumeLR(gain_minifloat_packed_t volumeLR) {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800370 mCblk->mVolumeLR = volumeLR;
371 }
372
373 void setSampleRate(uint32_t sampleRate) {
374 mCblk->mSampleRate = sampleRate;
375 }
376
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700377 void setPlaybackRate(const AudioPlaybackRate& playbackRate) {
Andy Hung8edb8dc2015-03-26 19:13:55 -0700378 mPlaybackRateMutator.push(playbackRate);
379 }
380
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381 virtual void flush();
382
383 virtual uint32_t getUnderrunFrames() const {
384 return mCblk->u.mStreaming.mUnderrunFrames;
385 }
Phil Burk2812d9e2016-01-04 10:34:30 -0800386 virtual uint32_t getUnderrunCount() const {
387 return mCblk->u.mStreaming.mUnderrunCount;
388 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800389
390 bool clearStreamEndDone(); // and return previous value
391
392 bool getStreamEndDone() const;
393
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100394 status_t waitStreamEndDone(const struct timespec *requested);
Andy Hung8edb8dc2015-03-26 19:13:55 -0700395
396private:
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700397 PlaybackRateQueue::Mutator mPlaybackRateMutator;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800398};
399
400class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
401public:
402 StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
403 size_t frameSize);
404 virtual ~StaticAudioTrackClientProxy() { }
405
406 virtual void flush();
407
408#define MIN_LOOP 16 // minimum length of each loop iteration in frames
Andy Hung9b461582014-12-01 17:56:29 -0800409
410 // setLoop(), setBufferPosition(), and setBufferPositionAndLoop() set the
411 // static buffer position and looping parameters. These commands are not
412 // synchronous (they do not wait or block); instead they take effect at the
413 // next buffer data read from the server side. However, the client side
414 // getters will read a cached version of the position and loop variables
415 // until the setting takes effect.
416 //
417 // setBufferPositionAndLoop() is equivalent to calling, in order, setLoop() and
418 // setBufferPosition().
419 //
420 // The functions should not be relied upon to do parameter or state checking.
421 // That is done at the AudioTrack level.
422
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800423 void setLoop(size_t loopStart, size_t loopEnd, int loopCount);
Andy Hung9b461582014-12-01 17:56:29 -0800424 void setBufferPosition(size_t position);
425 void setBufferPositionAndLoop(size_t position, size_t loopStart, size_t loopEnd,
426 int loopCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800427 size_t getBufferPosition();
Andy Hung4ede21d2014-12-12 15:37:34 -0800428 // getBufferPositionAndLoopCount() provides the proper snapshot of
429 // position and loopCount together.
430 void getBufferPositionAndLoopCount(size_t *position, int *loopCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431
432 virtual size_t getMisalignment() {
433 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800434 }
435
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800436 virtual uint32_t getUnderrunFrames() const {
437 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800438 }
439
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800440private:
441 StaticAudioTrackSingleStateQueue::Mutator mMutator;
Andy Hung4ede21d2014-12-12 15:37:34 -0800442 StaticAudioTrackPosLoopQueue::Observer mPosLoopObserver;
Andy Hung9b461582014-12-01 17:56:29 -0800443 StaticAudioTrackState mState; // last communicated state to server
Andy Hung4ede21d2014-12-12 15:37:34 -0800444 StaticAudioTrackPosLoop mPosLoop; // snapshot of position and loop.
Glenn Kastene3aa6592012-12-04 12:22:46 -0800445};
446
447// ----------------------------------------------------------------------------
448
449// Proxy used by AudioRecord client
450class AudioRecordClientProxy : public ClientProxy {
451public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800452 AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
453 size_t frameSize)
454 : ClientProxy(cblk, buffers, frameCount, frameSize,
Andy Hung6ae58432016-02-16 18:32:24 -0800455 false /*isOut*/, false /*clientInServer*/) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800456 ~AudioRecordClientProxy() { }
Andy Hung3f0c9022016-01-15 17:49:46 -0800457
Andy Hung3f0c9022016-01-15 17:49:46 -0800458 // Advances the client read pointer to the server write head pointer
459 // effectively flushing the client read buffer. The effect is
460 // instantaneous. Returns the number of frames flushed.
461 uint32_t flush() {
462 int32_t rear = android_atomic_acquire_load(&mCblk->u.mStreaming.mRear);
463 int32_t front = mCblk->u.mStreaming.mFront;
464 android_atomic_release_store(rear, &mCblk->u.mStreaming.mFront);
465 return (Modulo<int32_t>(rear) - front).unsignedValue();
466 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800467};
468
469// ----------------------------------------------------------------------------
470
471// Proxy used by AudioFlinger server
472class ServerProxy : public Proxy {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473protected:
474 ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
475 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800476public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800477 virtual ~ServerProxy() { }
478
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800479 // Obtain a buffer with filled frames (writing) or empty frames (reading).
480 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
481 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
482 // sets or extends the unreleased frame count.
483 // Always non-blocking.
484 // On entry:
485 // buffer->mFrameCount should be initialized to maximum number of desired frames,
486 // which must be > 0.
487 // buffer->mNonContig is unused.
488 // buffer->mRaw is unused.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700489 // ackFlush is true iff being called from Track::start to acknowledge a pending flush.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800490 // On exit:
491 // buffer->mFrameCount has the actual number of contiguous available frames,
492 // which is always 0 when the return status != NO_ERROR.
493 // buffer->mNonContig is the number of additional non-contiguous available frames.
494 // buffer->mRaw is a pointer to the first available frame,
495 // or NULL when buffer->mFrameCount == 0.
496 // The return status is one of:
497 // NO_ERROR Success, buffer->mFrameCount > 0.
498 // WOULD_BLOCK No frames are available.
499 // NO_INIT Shared memory is corrupt.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700500 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800501
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 // Release (some of) the frames last obtained.
503 // On entry, buffer->mFrameCount should have the number of frames to release,
504 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
505 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
506 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
507 // On exit:
508 // buffer->mFrameCount is zero.
509 // buffer->mRaw is NULL.
510 virtual void releaseBuffer(Buffer* buffer);
511
Andy Hung6ae58432016-02-16 18:32:24 -0800512 // Return the total number of frames that AudioFlinger has obtained and released
513 virtual int64_t framesReleased() const { return mReleased; }
514
515 // Expose timestamp to client proxy. Should only be called by a single thread.
516 virtual void setTimestamp(const ExtendedTimestamp &timestamp) {
517 mTimestampMutator.push(timestamp);
518 }
519
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800520protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522 int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only
Andy Hung3f0c9022016-01-15 17:49:46 -0800523 int64_t mReleased; // our copy of cblk->mServer, at 64 bit resolution
Andy Hung6ae58432016-02-16 18:32:24 -0800524
525 ExtendedTimestampQueue::Mutator mTimestampMutator;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800526};
527
528// Proxy used by AudioFlinger for servicing AudioTrack
529class AudioTrackServerProxy : public ServerProxy {
530public:
531 AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700532 size_t frameSize, bool clientInServer = false, uint32_t sampleRate = 0)
Andy Hung8edb8dc2015-03-26 19:13:55 -0700533 : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer),
Phil Burk2812d9e2016-01-04 10:34:30 -0800534 mPlaybackRateObserver(&cblk->mPlaybackRateQueue),
Eric Laurent8ce8e262016-02-16 11:59:23 -0800535 mUnderrunCount(0), mUnderrunning(false) {
Eric Laurent83b88082014-06-20 18:31:16 -0700536 mCblk->mSampleRate = sampleRate;
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700537 mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT;
Eric Laurent83b88082014-06-20 18:31:16 -0700538 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539protected:
540 virtual ~AudioTrackServerProxy() { }
541
542public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800543 // return value of these methods must be validated by the caller
544 uint32_t getSampleRate() const { return mCblk->mSampleRate; }
545 uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; }
Glenn Kastenc56f3422014-03-21 17:53:17 -0700546 gain_minifloat_packed_t getVolumeLR() const { return mCblk->mVolumeLR; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800547
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800548 // estimated total number of filled frames available to server to read,
549 // which may include non-contiguous frames
550 virtual size_t framesReady();
Glenn Kastene3aa6592012-12-04 12:22:46 -0800551
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800552 // Currently AudioFlinger will call framesReady() for a fast track from two threads:
553 // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended
554 // to be called from at most one thread of server, and one thread of client.
555 // As a temporary workaround, this method informs the proxy implementation that it
556 // should avoid doing a state queue poll from within framesReady().
557 // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
558 virtual void framesReadyIsCalledByMultipleThreads() { }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800559
560 bool setStreamEndDone(); // and return previous value
Glenn Kasten82aaf942013-07-17 16:05:07 -0700561
562 // Add to the tally of underrun frames, and inform client of underrun
563 virtual void tallyUnderrunFrames(uint32_t frameCount);
564
565 // Return the total number of frames which AudioFlinger desired but were unavailable,
566 // and thus which resulted in an underrun.
567 virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; }
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700568
Andy Hung8edb8dc2015-03-26 19:13:55 -0700569 // Return the playback speed and pitch read atomically. Not multi-thread safe on server side.
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700570 AudioPlaybackRate getPlaybackRate();
Andy Hung8edb8dc2015-03-26 19:13:55 -0700571
572private:
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700573 AudioPlaybackRate mPlaybackRate; // last observed playback rate
574 PlaybackRateQueue::Observer mPlaybackRateObserver;
Phil Burk2812d9e2016-01-04 10:34:30 -0800575
576 // The server keeps a copy here where it is safe from the client.
577 uint32_t mUnderrunCount; // echoed to mCblk
578 bool mUnderrunning; // used to detect edge of underrun
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800579};
580
581class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
582public:
583 StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
584 size_t frameSize);
585protected:
586 virtual ~StaticAudioTrackServerProxy() { }
587
588public:
589 virtual size_t framesReady();
590 virtual void framesReadyIsCalledByMultipleThreads();
Glenn Kasten2e422c42013-10-18 13:00:29 -0700591 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800592 virtual void releaseBuffer(Buffer* buffer);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700593 virtual void tallyUnderrunFrames(uint32_t frameCount);
594 virtual uint32_t getUnderrunFrames() const { return 0; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800595
596private:
Andy Hung9b461582014-12-01 17:56:29 -0800597 status_t updateStateWithLoop(StaticAudioTrackState *localState,
598 const StaticAudioTrackState &update) const;
599 status_t updateStateWithPosition(StaticAudioTrackState *localState,
600 const StaticAudioTrackState &update) const;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800601 ssize_t pollPosition(); // poll for state queue update, and return current position
602 StaticAudioTrackSingleStateQueue::Observer mObserver;
Andy Hung4ede21d2014-12-12 15:37:34 -0800603 StaticAudioTrackPosLoopQueue::Mutator mPosLoopMutator;
Andy Hungcb2129b2014-11-11 12:17:22 -0800604 size_t mFramesReadySafe; // Assuming size_t read/writes are atomic on 32 / 64 bit
605 // processors, this is a thread-safe version of
606 // mFramesReady.
607 int64_t mFramesReady; // The number of frames ready in the static buffer
608 // including loops. This is 64 bits since loop mode
609 // can cause a track to appear to have a large number
610 // of frames. INT64_MAX means an infinite loop.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800611 bool mFramesReadyIsCalledByMultipleThreads;
Andy Hung9b461582014-12-01 17:56:29 -0800612 StaticAudioTrackState mState; // Server side state. Any updates from client must be
613 // passed by the mObserver SingleStateQueue.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800614};
Glenn Kastene3aa6592012-12-04 12:22:46 -0800615
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800616// Proxy used by AudioFlinger for servicing AudioRecord
617class AudioRecordServerProxy : public ServerProxy {
618public:
619 AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700620 size_t frameSize, bool clientInServer)
Andy Hung6ae58432016-02-16 18:32:24 -0800621 : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, clientInServer) { }
Andy Hung3f0c9022016-01-15 17:49:46 -0800622
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800623protected:
624 virtual ~AudioRecordServerProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800625};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626
627// ----------------------------------------------------------------------------
628
629}; // namespace android
630
631#endif // ANDROID_AUDIO_TRACK_SHARED_H