blob: 8389773f2828e46eb45a6b0ba10e96e4417a43f3 [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>
27#include <media/nbaio/roundup.h>
28#include <media/SingleStateQueue.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
30namespace android {
31
32// ----------------------------------------------------------------------------
33
Glenn Kasten96f60d82013-07-12 10:21:18 -070034// for audio_track_cblk_t::mFlags
Glenn Kasten9f80dd22012-12-18 15:57:32 -080035#define CBLK_UNDERRUN 0x01 // set by server immediately on output underrun, cleared by client
Glenn Kasten864585d2012-11-06 16:15:41 -080036#define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger,
Glenn Kasten9c5fdd82012-11-05 13:38:15 -080037 // clear: track is ready when buffer full
Glenn Kasten864585d2012-11-06 16:15:41 -080038#define CBLK_INVALID 0x04 // track buffer invalidated by AudioFlinger, need to re-create
Glenn Kasten9f80dd22012-12-18 15:57:32 -080039#define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun,
40 // need to re-start. Unlike CBLK_UNDERRUN, this is not set
41 // immediately, but only after a long string of underruns.
42// 0x10 unused
43#define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes
44#define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes
45#define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping
46#define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client
47#define CBLK_INTERRUPT 0x200 // set by client on interrupt(), cleared by client in obtainBuffer()
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000048#define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client
49
50//EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation
Glenn Kastene198c362013-08-13 09:13:36 -070051#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 -080052
Glenn Kastene3aa6592012-12-04 12:22:46 -080053struct AudioTrackSharedStreaming {
54 // similar to NBAIO MonoPipe
Glenn Kasten9f80dd22012-12-18 15:57:32 -080055 // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2
56 volatile int32_t mFront; // read by server
57 volatile int32_t mRear; // write by client
58 volatile int32_t mFlush; // incremented by client to indicate a request to flush;
59 // server notices and discards all data between mFront and mRear
60 volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame
Glenn Kastene3aa6592012-12-04 12:22:46 -080061};
62
Andy Hung9b461582014-12-01 17:56:29 -080063// Represents a single state of an AudioTrack that was created in static mode (shared memory buffer
64// supplied by the client). This state needs to be communicated from the client to server. As this
65// state is too large to be updated atomically without a mutex, and mutexes aren't allowed here, the
66// state is wrapped by a SingleStateQueue.
67struct StaticAudioTrackState {
68 // Do not define constructors, destructors, or virtual methods as this is part of a
69 // union in shared memory and they will not get called properly.
70
71 // These fields should both be size_t, but since they are located in shared memory we
72 // force to 32-bit. The client and server may have different typedefs for size_t.
73
74 // The state has a sequence counter to indicate whether changes are made to loop or position.
75 // The sequence counter also currently indicates whether loop or position is first depending
76 // on which is greater; it jumps by max(mLoopSequence, mPositionSequence) + 1.
77
78 uint32_t mLoopStart;
79 uint32_t mLoopEnd;
80 int32_t mLoopCount;
81 uint32_t mLoopSequence; // a sequence counter to indicate changes to loop
82 uint32_t mPosition;
83 uint32_t mPositionSequence; // a sequence counter to indicate changes to position
84};
85
Glenn Kasten9f80dd22012-12-18 15:57:32 -080086typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue;
87
Glenn Kastene3aa6592012-12-04 12:22:46 -080088struct AudioTrackSharedStatic {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080089 StaticAudioTrackSingleStateQueue::Shared
90 mSingleStateQueue;
Glenn Kastenfdac7c02014-01-28 11:03:28 -080091 // This field should be a size_t, but since it is located in shared memory we
92 // force to 32-bit. The client and server may have different typedefs for size_t.
93 uint32_t mBufferPosition; // updated asynchronously by server,
Glenn Kasten9f80dd22012-12-18 15:57:32 -080094 // "for entertainment purposes only"
Glenn Kastene3aa6592012-12-04 12:22:46 -080095};
96
97// ----------------------------------------------------------------------------
98
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -080099// Important: do not add any virtual methods, including ~
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100struct audio_track_cblk_t
101{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 // Since the control block is always located in shared memory, this constructor
103 // is only used for placement new(). It is never used for regular new() or stack.
104 audio_track_cblk_t();
105 /*virtual*/ ~audio_track_cblk_t() { }
106
Glenn Kastene3aa6592012-12-04 12:22:46 -0800107 friend class Proxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800108 friend class ClientProxy;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800109 friend class AudioTrackClientProxy;
110 friend class AudioRecordClientProxy;
111 friend class ServerProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800112 friend class AudioTrackServerProxy;
113 friend class AudioRecordServerProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800114
115 // The data members are grouped so that members accessed frequently and in the same context
116 // are in the same line of data cache.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800117
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700118 uint32_t mServer; // Number of filled frames consumed by server (mIsOut),
119 // or filled frames provided by server (!mIsOut).
120 // It is updated asynchronously by server without a barrier.
Glenn Kastenb187de12014-12-30 08:18:15 -0800121 // The value should be used
122 // "for entertainment purposes only",
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700123 // which means don't make important decisions based on it.
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800124
Glenn Kasten74935e42013-12-19 08:56:45 -0800125 uint32_t mPad1; // unused
Glenn Kasten99e53b82012-01-19 08:59:58 -0800126
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700127 volatile int32_t mFutex; // event flag: down (P) by client,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800128 // up (V) by server or binderDied() or interrupt()
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700129#define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending
Glenn Kasten99e53b82012-01-19 08:59:58 -0800130
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800131private:
132
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800133 // This field should be a size_t, but since it is located in shared memory we
134 // force to 32-bit. The client and server may have different typedefs for size_t.
135 uint32_t mMinimum; // server wakes up client if available >= mMinimum
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800136
Glenn Kastenc56f3422014-03-21 17:53:17 -0700137 // Stereo gains for AudioTrack only, not used by AudioRecord.
138 gain_minifloat_packed_t mVolumeLR;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800139
Glenn Kastene3aa6592012-12-04 12:22:46 -0800140 uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz
141 // or 0 == default. Write-only client, read-only server.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800142
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800143 // client write-only, server read-only
144 uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0
145
Glenn Kastend054c322013-07-12 12:59:20 -0700146 uint16_t mPad2; // unused
Eric Laurentd1b449a2010-05-14 03:26:45 -0700147
Glenn Kastene3aa6592012-12-04 12:22:46 -0800148public:
Glenn Kasten99e53b82012-01-19 08:59:58 -0800149
Glenn Kasten96f60d82013-07-12 10:21:18 -0700150 volatile int32_t mFlags; // combinations of CBLK_*
Eric Laurent38ccae22011-03-28 18:37:07 -0700151
Eric Laurentd1b449a2010-05-14 03:26:45 -0700152 // Cache line boundary (32 bytes)
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700153
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800154public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800155 union {
156 AudioTrackSharedStreaming mStreaming;
157 AudioTrackSharedStatic mStatic;
158 int mAlign[8];
159 } u;
160
161 // Cache line boundary (32 bytes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162};
163
Glenn Kastene3aa6592012-12-04 12:22:46 -0800164// ----------------------------------------------------------------------------
165
166// Proxy for shared memory control block, to isolate callers from needing to know the details.
167// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
168// The proxies are located in normal memory, and are not multi-thread safe within a given side.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800169class Proxy : public RefBase {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800170protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800171 Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
172 bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800173 virtual ~Proxy() { }
174
175public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800176 struct Buffer {
177 size_t mFrameCount; // number of frames available in this buffer
178 void* mRaw; // pointer to first frame
179 size_t mNonContig; // number of additional non-contiguous frames available
180 };
Glenn Kastene3aa6592012-12-04 12:22:46 -0800181
182protected:
183 // These refer to shared memory, and are virtual addresses with respect to the current process.
184 // They may have different virtual addresses within the other process.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800185 audio_track_cblk_t* const mCblk; // the control block
186 void* const mBuffers; // starting address of buffers
Glenn Kastene3aa6592012-12-04 12:22:46 -0800187
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800188 const size_t mFrameCount; // not necessarily a power of 2
189 const size_t mFrameSize; // in bytes
190 const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
191 const bool mIsOut; // true for AudioTrack, false for AudioRecord
192 const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
193 bool mIsShutdown; // latch set to true when shared memory corruption detected
Glenn Kasten7db7df02013-06-25 16:13:23 -0700194 size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
Glenn Kastene3aa6592012-12-04 12:22:46 -0800195};
196
197// ----------------------------------------------------------------------------
198
199// Proxy seen by AudioTrack client and AudioRecord client
200class ClientProxy : public Proxy {
Eric Laurent83b88082014-06-20 18:31:16 -0700201public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800202 ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
203 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800204 virtual ~ClientProxy() { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800205
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800206 static const struct timespec kForever;
207 static const struct timespec kNonBlocking;
208
209 // Obtain a buffer with filled frames (reading) or empty frames (writing).
210 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
211 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
212 // sets or extends the unreleased frame count.
213 // On entry:
214 // buffer->mFrameCount should be initialized to maximum number of desired frames,
215 // which must be > 0.
216 // buffer->mNonContig is unused.
217 // buffer->mRaw is unused.
218 // requested is the requested timeout in local monotonic delta time units:
219 // NULL or &kNonBlocking means non-blocking (zero timeout).
220 // &kForever means block forever (infinite timeout).
221 // Other values mean a specific timeout in local monotonic delta time units.
222 // elapsed is a pointer to a location that will hold the total local monotonic time that
223 // elapsed while blocked, or NULL if not needed.
224 // On exit:
225 // buffer->mFrameCount has the actual number of contiguous available frames,
226 // which is always 0 when the return status != NO_ERROR.
227 // buffer->mNonContig is the number of additional non-contiguous available frames.
228 // buffer->mRaw is a pointer to the first available frame,
229 // or NULL when buffer->mFrameCount == 0.
230 // The return status is one of:
231 // NO_ERROR Success, buffer->mFrameCount > 0.
232 // WOULD_BLOCK Non-blocking mode and no frames are available.
233 // TIMED_OUT Timeout occurred before any frames became available.
234 // This can happen even for infinite timeout, due to a spurious wakeup.
235 // In this case, the caller should investigate and then re-try as appropriate.
236 // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
237 // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again.
238 // NO_INIT Shared memory is corrupt.
Glenn Kasten7db7df02013-06-25 16:13:23 -0700239 // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800240 status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
241 struct timespec *elapsed = NULL);
242
243 // Release (some of) the frames last obtained.
244 // On entry, buffer->mFrameCount should have the number of frames to release,
245 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
246 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
247 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
248 // On exit:
249 // buffer->mFrameCount is zero.
250 // buffer->mRaw is NULL.
251 void releaseBuffer(Buffer* buffer);
252
253 // Call after detecting server's death
254 void binderDied();
255
256 // Call to force an obtainBuffer() to return quickly with -EINTR
257 void interrupt();
258
259 size_t getPosition() {
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700260 return mEpoch + mCblk->mServer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800261 }
262
263 void setEpoch(size_t epoch) {
264 mEpoch = epoch;
265 }
266
267 void setMinimum(size_t minimum) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800268 // This can only happen on a 64-bit client
269 if (minimum > UINT32_MAX) {
270 minimum = UINT32_MAX;
271 }
272 mCblk->mMinimum = (uint32_t) minimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800273 }
274
275 // Return the number of frames that would need to be obtained and released
276 // in order for the client to be aligned at start of buffer
277 virtual size_t getMisalignment();
278
279 size_t getEpoch() const {
280 return mEpoch;
281 }
282
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700283 size_t getFramesFilled();
284
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800285private:
286 size_t mEpoch;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800287};
288
289// ----------------------------------------------------------------------------
290
291// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
292class AudioTrackClientProxy : public ClientProxy {
293public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800294 AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
295 size_t frameSize, bool clientInServer = false)
296 : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
297 clientInServer) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800298 virtual ~AudioTrackClientProxy() { }
299
300 // No barriers on the following operations, so the ordering of loads/stores
301 // with respect to other parameters is UNPREDICTABLE. That's considered safe.
302
303 // caller must limit to 0.0 <= sendLevel <= 1.0
304 void setSendLevel(float sendLevel) {
305 mCblk->mSendLevel = uint16_t(sendLevel * 0x1000);
306 }
307
Glenn Kastenc56f3422014-03-21 17:53:17 -0700308 // set stereo gains
309 void setVolumeLR(gain_minifloat_packed_t volumeLR) {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800310 mCblk->mVolumeLR = volumeLR;
311 }
312
313 void setSampleRate(uint32_t sampleRate) {
314 mCblk->mSampleRate = sampleRate;
315 }
316
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800317 virtual void flush();
318
319 virtual uint32_t getUnderrunFrames() const {
320 return mCblk->u.mStreaming.mUnderrunFrames;
321 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800322
323 bool clearStreamEndDone(); // and return previous value
324
325 bool getStreamEndDone() const;
326
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100327 status_t waitStreamEndDone(const struct timespec *requested);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800328};
329
330class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
331public:
332 StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
333 size_t frameSize);
334 virtual ~StaticAudioTrackClientProxy() { }
335
336 virtual void flush();
337
338#define MIN_LOOP 16 // minimum length of each loop iteration in frames
Andy Hung9b461582014-12-01 17:56:29 -0800339
340 // setLoop(), setBufferPosition(), and setBufferPositionAndLoop() set the
341 // static buffer position and looping parameters. These commands are not
342 // synchronous (they do not wait or block); instead they take effect at the
343 // next buffer data read from the server side. However, the client side
344 // getters will read a cached version of the position and loop variables
345 // until the setting takes effect.
346 //
347 // setBufferPositionAndLoop() is equivalent to calling, in order, setLoop() and
348 // setBufferPosition().
349 //
350 // The functions should not be relied upon to do parameter or state checking.
351 // That is done at the AudioTrack level.
352
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800353 void setLoop(size_t loopStart, size_t loopEnd, int loopCount);
Andy Hung9b461582014-12-01 17:56:29 -0800354 void setBufferPosition(size_t position);
355 void setBufferPositionAndLoop(size_t position, size_t loopStart, size_t loopEnd,
356 int loopCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800357 size_t getBufferPosition();
358
359 virtual size_t getMisalignment() {
360 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800361 }
362
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800363 virtual uint32_t getUnderrunFrames() const {
364 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800365 }
366
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800367private:
368 StaticAudioTrackSingleStateQueue::Mutator mMutator;
Andy Hung9b461582014-12-01 17:56:29 -0800369 StaticAudioTrackState mState; // last communicated state to server
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800370 size_t mBufferPosition; // so that getBufferPosition() appears to be synchronous
Glenn Kastene3aa6592012-12-04 12:22:46 -0800371};
372
373// ----------------------------------------------------------------------------
374
375// Proxy used by AudioRecord client
376class AudioRecordClientProxy : public ClientProxy {
377public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800378 AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
379 size_t frameSize)
380 : ClientProxy(cblk, buffers, frameCount, frameSize,
381 false /*isOut*/, false /*clientInServer*/) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800382 ~AudioRecordClientProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800383};
384
385// ----------------------------------------------------------------------------
386
387// Proxy used by AudioFlinger server
388class ServerProxy : public Proxy {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389protected:
390 ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
391 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800392public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800393 virtual ~ServerProxy() { }
394
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 // Obtain a buffer with filled frames (writing) or empty frames (reading).
396 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
397 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
398 // sets or extends the unreleased frame count.
399 // Always non-blocking.
400 // On entry:
401 // buffer->mFrameCount should be initialized to maximum number of desired frames,
402 // which must be > 0.
403 // buffer->mNonContig is unused.
404 // buffer->mRaw is unused.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700405 // ackFlush is true iff being called from Track::start to acknowledge a pending flush.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800406 // On exit:
407 // buffer->mFrameCount has the actual number of contiguous available frames,
408 // which is always 0 when the return status != NO_ERROR.
409 // buffer->mNonContig is the number of additional non-contiguous available frames.
410 // buffer->mRaw is a pointer to the first available frame,
411 // or NULL when buffer->mFrameCount == 0.
412 // The return status is one of:
413 // NO_ERROR Success, buffer->mFrameCount > 0.
414 // WOULD_BLOCK No frames are available.
415 // NO_INIT Shared memory is corrupt.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700416 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800417
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418 // Release (some of) the frames last obtained.
419 // On entry, buffer->mFrameCount should have the number of frames to release,
420 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
421 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
422 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
423 // On exit:
424 // buffer->mFrameCount is zero.
425 // buffer->mRaw is NULL.
426 virtual void releaseBuffer(Buffer* buffer);
427
428protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800429 size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800430 int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431};
432
433// Proxy used by AudioFlinger for servicing AudioTrack
434class AudioTrackServerProxy : public ServerProxy {
435public:
436 AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700437 size_t frameSize, bool clientInServer = false, uint32_t sampleRate = 0)
438 : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) {
439 mCblk->mSampleRate = sampleRate;
440 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441protected:
442 virtual ~AudioTrackServerProxy() { }
443
444public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800445 // return value of these methods must be validated by the caller
446 uint32_t getSampleRate() const { return mCblk->mSampleRate; }
447 uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; }
Glenn Kastenc56f3422014-03-21 17:53:17 -0700448 gain_minifloat_packed_t getVolumeLR() const { return mCblk->mVolumeLR; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800449
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800450 // estimated total number of filled frames available to server to read,
451 // which may include non-contiguous frames
452 virtual size_t framesReady();
Glenn Kastene3aa6592012-12-04 12:22:46 -0800453
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800454 // Currently AudioFlinger will call framesReady() for a fast track from two threads:
455 // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended
456 // to be called from at most one thread of server, and one thread of client.
457 // As a temporary workaround, this method informs the proxy implementation that it
458 // should avoid doing a state queue poll from within framesReady().
459 // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
460 virtual void framesReadyIsCalledByMultipleThreads() { }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800461
462 bool setStreamEndDone(); // and return previous value
Glenn Kasten82aaf942013-07-17 16:05:07 -0700463
464 // Add to the tally of underrun frames, and inform client of underrun
465 virtual void tallyUnderrunFrames(uint32_t frameCount);
466
467 // Return the total number of frames which AudioFlinger desired but were unavailable,
468 // and thus which resulted in an underrun.
469 virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; }
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700470
471 // Return the total number of frames that AudioFlinger has obtained and released
472 virtual size_t framesReleased() const { return mCblk->mServer; }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473};
474
475class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
476public:
477 StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
478 size_t frameSize);
479protected:
480 virtual ~StaticAudioTrackServerProxy() { }
481
482public:
483 virtual size_t framesReady();
484 virtual void framesReadyIsCalledByMultipleThreads();
Glenn Kasten2e422c42013-10-18 13:00:29 -0700485 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800486 virtual void releaseBuffer(Buffer* buffer);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700487 virtual void tallyUnderrunFrames(uint32_t frameCount);
488 virtual uint32_t getUnderrunFrames() const { return 0; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800489
490private:
Andy Hung9b461582014-12-01 17:56:29 -0800491 status_t updateStateWithLoop(StaticAudioTrackState *localState,
492 const StaticAudioTrackState &update) const;
493 status_t updateStateWithPosition(StaticAudioTrackState *localState,
494 const StaticAudioTrackState &update) const;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495 ssize_t pollPosition(); // poll for state queue update, and return current position
496 StaticAudioTrackSingleStateQueue::Observer mObserver;
497 size_t mPosition; // server's current play position in frames, relative to 0
Andy Hungcb2129b2014-11-11 12:17:22 -0800498
499 size_t mFramesReadySafe; // Assuming size_t read/writes are atomic on 32 / 64 bit
500 // processors, this is a thread-safe version of
501 // mFramesReady.
502 int64_t mFramesReady; // The number of frames ready in the static buffer
503 // including loops. This is 64 bits since loop mode
504 // can cause a track to appear to have a large number
505 // of frames. INT64_MAX means an infinite loop.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800506 bool mFramesReadyIsCalledByMultipleThreads;
Andy Hung9b461582014-12-01 17:56:29 -0800507 StaticAudioTrackState mState; // Server side state. Any updates from client must be
508 // passed by the mObserver SingleStateQueue.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800509};
Glenn Kastene3aa6592012-12-04 12:22:46 -0800510
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511// Proxy used by AudioFlinger for servicing AudioRecord
512class AudioRecordServerProxy : public ServerProxy {
513public:
514 AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700515 size_t frameSize, bool clientInServer)
516 : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, clientInServer) { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517protected:
518 virtual ~AudioRecordServerProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800519};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520
521// ----------------------------------------------------------------------------
522
523}; // namespace android
524
525#endif // ANDROID_AUDIO_TRACK_SHARED_H