blob: 31dff3689afbb924fa3e81498e8165bae9f453d1 [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>
29#include <private/media/StaticAudioTrackState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080030
31namespace android {
32
33// ----------------------------------------------------------------------------
34
Glenn Kasten96f60d82013-07-12 10:21:18 -070035// for audio_track_cblk_t::mFlags
Glenn Kasten9f80dd22012-12-18 15:57:32 -080036#define CBLK_UNDERRUN 0x01 // set by server immediately on output underrun, cleared by client
Glenn Kasten864585d2012-11-06 16:15:41 -080037#define CBLK_FORCEREADY 0x02 // set: track is considered ready immediately by AudioFlinger,
Glenn Kasten9c5fdd82012-11-05 13:38:15 -080038 // clear: track is ready when buffer full
Glenn Kasten864585d2012-11-06 16:15:41 -080039#define CBLK_INVALID 0x04 // track buffer invalidated by AudioFlinger, need to re-create
Glenn Kasten9f80dd22012-12-18 15:57:32 -080040#define CBLK_DISABLED 0x08 // output track disabled by AudioFlinger due to underrun,
41 // need to re-start. Unlike CBLK_UNDERRUN, this is not set
42 // immediately, but only after a long string of underruns.
43// 0x10 unused
44#define CBLK_LOOP_CYCLE 0x20 // set by server each time a loop cycle other than final one completes
45#define CBLK_LOOP_FINAL 0x40 // set by server when the final loop cycle completes
46#define CBLK_BUFFER_END 0x80 // set by server when the position reaches end of buffer if not looping
47#define CBLK_OVERRUN 0x100 // set by server immediately on input overrun, cleared by client
48#define CBLK_INTERRUPT 0x200 // set by client on interrupt(), cleared by client in obtainBuffer()
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000049#define CBLK_STREAM_END_DONE 0x400 // set by server on render completion, cleared by client
50
51//EL_FIXME 20 seconds may not be enough and must be reconciled with new obtainBuffer implementation
Glenn Kastene198c362013-08-13 09:13:36 -070052#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 -080053
Glenn Kastene3aa6592012-12-04 12:22:46 -080054struct AudioTrackSharedStreaming {
55 // similar to NBAIO MonoPipe
Glenn Kasten9f80dd22012-12-18 15:57:32 -080056 // in continuously incrementing frame units, take modulo buffer size, which must be a power of 2
57 volatile int32_t mFront; // read by server
58 volatile int32_t mRear; // write by client
59 volatile int32_t mFlush; // incremented by client to indicate a request to flush;
60 // server notices and discards all data between mFront and mRear
61 volatile uint32_t mUnderrunFrames; // server increments for each unavailable but desired frame
Glenn Kastene3aa6592012-12-04 12:22:46 -080062};
63
Glenn Kasten9f80dd22012-12-18 15:57:32 -080064typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue;
65
Glenn Kastene3aa6592012-12-04 12:22:46 -080066struct AudioTrackSharedStatic {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080067 StaticAudioTrackSingleStateQueue::Shared
68 mSingleStateQueue;
Glenn Kastenfdac7c02014-01-28 11:03:28 -080069 // This field should be a size_t, but since it is located in shared memory we
70 // force to 32-bit. The client and server may have different typedefs for size_t.
71 uint32_t mBufferPosition; // updated asynchronously by server,
Glenn Kasten9f80dd22012-12-18 15:57:32 -080072 // "for entertainment purposes only"
Glenn Kastene3aa6592012-12-04 12:22:46 -080073};
74
75// ----------------------------------------------------------------------------
76
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -080077// Important: do not add any virtual methods, including ~
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078struct audio_track_cblk_t
79{
Glenn Kasten9f80dd22012-12-18 15:57:32 -080080 // Since the control block is always located in shared memory, this constructor
81 // is only used for placement new(). It is never used for regular new() or stack.
82 audio_track_cblk_t();
83 /*virtual*/ ~audio_track_cblk_t() { }
84
Glenn Kastene3aa6592012-12-04 12:22:46 -080085 friend class Proxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080086 friend class ClientProxy;
Glenn Kastene3aa6592012-12-04 12:22:46 -080087 friend class AudioTrackClientProxy;
88 friend class AudioRecordClientProxy;
89 friend class ServerProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080090 friend class AudioTrackServerProxy;
91 friend class AudioRecordServerProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092
93 // The data members are grouped so that members accessed frequently and in the same context
94 // are in the same line of data cache.
Glenn Kasten99e53b82012-01-19 08:59:58 -080095
Glenn Kastenf20e1d82013-07-12 09:45:18 -070096 uint32_t mServer; // Number of filled frames consumed by server (mIsOut),
97 // or filled frames provided by server (!mIsOut).
98 // It is updated asynchronously by server without a barrier.
99 // The value should be used "for entertainment purposes only",
100 // which means don't make important decisions based on it.
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800101
Glenn Kasten74935e42013-12-19 08:56:45 -0800102 uint32_t mPad1; // unused
Glenn Kasten99e53b82012-01-19 08:59:58 -0800103
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700104 volatile int32_t mFutex; // event flag: down (P) by client,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800105 // up (V) by server or binderDied() or interrupt()
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700106#define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending
Glenn Kasten99e53b82012-01-19 08:59:58 -0800107
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800108private:
109
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800110 // This field should be a size_t, but since it is located in shared memory we
111 // force to 32-bit. The client and server may have different typedefs for size_t.
112 uint32_t mMinimum; // server wakes up client if available >= mMinimum
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800113
Glenn Kastenc56f3422014-03-21 17:53:17 -0700114 // Stereo gains for AudioTrack only, not used by AudioRecord.
115 gain_minifloat_packed_t mVolumeLR;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800116
Glenn Kastene3aa6592012-12-04 12:22:46 -0800117 uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz
118 // or 0 == default. Write-only client, read-only server.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800119
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800120 // client write-only, server read-only
121 uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0
122
Glenn Kastend054c322013-07-12 12:59:20 -0700123 uint16_t mPad2; // unused
Eric Laurentd1b449a2010-05-14 03:26:45 -0700124
Glenn Kastene3aa6592012-12-04 12:22:46 -0800125public:
Glenn Kasten99e53b82012-01-19 08:59:58 -0800126
Glenn Kasten96f60d82013-07-12 10:21:18 -0700127 volatile int32_t mFlags; // combinations of CBLK_*
Eric Laurent38ccae22011-03-28 18:37:07 -0700128
Eric Laurentd1b449a2010-05-14 03:26:45 -0700129 // Cache line boundary (32 bytes)
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700130
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800131public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800132 union {
133 AudioTrackSharedStreaming mStreaming;
134 AudioTrackSharedStatic mStatic;
135 int mAlign[8];
136 } u;
137
138 // Cache line boundary (32 bytes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139};
140
Glenn Kastene3aa6592012-12-04 12:22:46 -0800141// ----------------------------------------------------------------------------
142
143// Proxy for shared memory control block, to isolate callers from needing to know the details.
144// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
145// The proxies are located in normal memory, and are not multi-thread safe within a given side.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800146class Proxy : public RefBase {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800147protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148 Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
149 bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800150 virtual ~Proxy() { }
151
152public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800153 struct Buffer {
154 size_t mFrameCount; // number of frames available in this buffer
155 void* mRaw; // pointer to first frame
156 size_t mNonContig; // number of additional non-contiguous frames available
157 };
Glenn Kastene3aa6592012-12-04 12:22:46 -0800158
159protected:
160 // These refer to shared memory, and are virtual addresses with respect to the current process.
161 // They may have different virtual addresses within the other process.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800162 audio_track_cblk_t* const mCblk; // the control block
163 void* const mBuffers; // starting address of buffers
Glenn Kastene3aa6592012-12-04 12:22:46 -0800164
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800165 const size_t mFrameCount; // not necessarily a power of 2
166 const size_t mFrameSize; // in bytes
167 const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
168 const bool mIsOut; // true for AudioTrack, false for AudioRecord
169 const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
170 bool mIsShutdown; // latch set to true when shared memory corruption detected
Glenn Kasten7db7df02013-06-25 16:13:23 -0700171 size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
Glenn Kastene3aa6592012-12-04 12:22:46 -0800172};
173
174// ----------------------------------------------------------------------------
175
176// Proxy seen by AudioTrack client and AudioRecord client
177class ClientProxy : public Proxy {
Eric Laurent83b88082014-06-20 18:31:16 -0700178public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800179 ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
180 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800181 virtual ~ClientProxy() { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800182
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800183 static const struct timespec kForever;
184 static const struct timespec kNonBlocking;
185
186 // Obtain a buffer with filled frames (reading) or empty frames (writing).
187 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
188 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
189 // sets or extends the unreleased frame count.
190 // On entry:
191 // buffer->mFrameCount should be initialized to maximum number of desired frames,
192 // which must be > 0.
193 // buffer->mNonContig is unused.
194 // buffer->mRaw is unused.
195 // requested is the requested timeout in local monotonic delta time units:
196 // NULL or &kNonBlocking means non-blocking (zero timeout).
197 // &kForever means block forever (infinite timeout).
198 // Other values mean a specific timeout in local monotonic delta time units.
199 // elapsed is a pointer to a location that will hold the total local monotonic time that
200 // elapsed while blocked, or NULL if not needed.
201 // On exit:
202 // buffer->mFrameCount has the actual number of contiguous available frames,
203 // which is always 0 when the return status != NO_ERROR.
204 // buffer->mNonContig is the number of additional non-contiguous available frames.
205 // buffer->mRaw is a pointer to the first available frame,
206 // or NULL when buffer->mFrameCount == 0.
207 // The return status is one of:
208 // NO_ERROR Success, buffer->mFrameCount > 0.
209 // WOULD_BLOCK Non-blocking mode and no frames are available.
210 // TIMED_OUT Timeout occurred before any frames became available.
211 // This can happen even for infinite timeout, due to a spurious wakeup.
212 // In this case, the caller should investigate and then re-try as appropriate.
213 // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
214 // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again.
215 // NO_INIT Shared memory is corrupt.
Glenn Kasten7db7df02013-06-25 16:13:23 -0700216 // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800217 status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
218 struct timespec *elapsed = NULL);
219
220 // Release (some of) the frames last obtained.
221 // On entry, buffer->mFrameCount should have the number of frames to release,
222 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
223 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
224 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
225 // On exit:
226 // buffer->mFrameCount is zero.
227 // buffer->mRaw is NULL.
228 void releaseBuffer(Buffer* buffer);
229
230 // Call after detecting server's death
231 void binderDied();
232
233 // Call to force an obtainBuffer() to return quickly with -EINTR
234 void interrupt();
235
236 size_t getPosition() {
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700237 return mEpoch + mCblk->mServer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800238 }
239
240 void setEpoch(size_t epoch) {
241 mEpoch = epoch;
242 }
243
244 void setMinimum(size_t minimum) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800245 // This can only happen on a 64-bit client
246 if (minimum > UINT32_MAX) {
247 minimum = UINT32_MAX;
248 }
249 mCblk->mMinimum = (uint32_t) minimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800250 }
251
252 // Return the number of frames that would need to be obtained and released
253 // in order for the client to be aligned at start of buffer
254 virtual size_t getMisalignment();
255
256 size_t getEpoch() const {
257 return mEpoch;
258 }
259
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700260 size_t getFramesFilled();
261
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800262private:
263 size_t mEpoch;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800264};
265
266// ----------------------------------------------------------------------------
267
268// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
269class AudioTrackClientProxy : public ClientProxy {
270public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800271 AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
272 size_t frameSize, bool clientInServer = false)
273 : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
274 clientInServer) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800275 virtual ~AudioTrackClientProxy() { }
276
277 // No barriers on the following operations, so the ordering of loads/stores
278 // with respect to other parameters is UNPREDICTABLE. That's considered safe.
279
280 // caller must limit to 0.0 <= sendLevel <= 1.0
281 void setSendLevel(float sendLevel) {
282 mCblk->mSendLevel = uint16_t(sendLevel * 0x1000);
283 }
284
Glenn Kastenc56f3422014-03-21 17:53:17 -0700285 // set stereo gains
286 void setVolumeLR(gain_minifloat_packed_t volumeLR) {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800287 mCblk->mVolumeLR = volumeLR;
288 }
289
290 void setSampleRate(uint32_t sampleRate) {
291 mCblk->mSampleRate = sampleRate;
292 }
293
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800294 virtual void flush();
295
296 virtual uint32_t getUnderrunFrames() const {
297 return mCblk->u.mStreaming.mUnderrunFrames;
298 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800299
300 bool clearStreamEndDone(); // and return previous value
301
302 bool getStreamEndDone() const;
303
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100304 status_t waitStreamEndDone(const struct timespec *requested);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800305};
306
307class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
308public:
309 StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
310 size_t frameSize);
311 virtual ~StaticAudioTrackClientProxy() { }
312
313 virtual void flush();
314
315#define MIN_LOOP 16 // minimum length of each loop iteration in frames
316 void setLoop(size_t loopStart, size_t loopEnd, int loopCount);
317 size_t getBufferPosition();
318
319 virtual size_t getMisalignment() {
320 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800321 }
322
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800323 virtual uint32_t getUnderrunFrames() const {
324 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800325 }
326
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800327private:
328 StaticAudioTrackSingleStateQueue::Mutator mMutator;
329 size_t mBufferPosition; // so that getBufferPosition() appears to be synchronous
Glenn Kastene3aa6592012-12-04 12:22:46 -0800330};
331
332// ----------------------------------------------------------------------------
333
334// Proxy used by AudioRecord client
335class AudioRecordClientProxy : public ClientProxy {
336public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800337 AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
338 size_t frameSize)
339 : ClientProxy(cblk, buffers, frameCount, frameSize,
340 false /*isOut*/, false /*clientInServer*/) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800341 ~AudioRecordClientProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800342};
343
344// ----------------------------------------------------------------------------
345
346// Proxy used by AudioFlinger server
347class ServerProxy : public Proxy {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800348protected:
349 ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
350 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800351public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800352 virtual ~ServerProxy() { }
353
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800354 // Obtain a buffer with filled frames (writing) or empty frames (reading).
355 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
356 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
357 // sets or extends the unreleased frame count.
358 // Always non-blocking.
359 // On entry:
360 // buffer->mFrameCount should be initialized to maximum number of desired frames,
361 // which must be > 0.
362 // buffer->mNonContig is unused.
363 // buffer->mRaw is unused.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700364 // ackFlush is true iff being called from Track::start to acknowledge a pending flush.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800365 // On exit:
366 // buffer->mFrameCount has the actual number of contiguous available frames,
367 // which is always 0 when the return status != NO_ERROR.
368 // buffer->mNonContig is the number of additional non-contiguous available frames.
369 // buffer->mRaw is a pointer to the first available frame,
370 // or NULL when buffer->mFrameCount == 0.
371 // The return status is one of:
372 // NO_ERROR Success, buffer->mFrameCount > 0.
373 // WOULD_BLOCK No frames are available.
374 // NO_INIT Shared memory is corrupt.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700375 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800376
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 // Release (some of) the frames last obtained.
378 // On entry, buffer->mFrameCount should have the number of frames to release,
379 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
380 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
381 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
382 // On exit:
383 // buffer->mFrameCount is zero.
384 // buffer->mRaw is NULL.
385 virtual void releaseBuffer(Buffer* buffer);
386
387protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800388 size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800390};
391
392// Proxy used by AudioFlinger for servicing AudioTrack
393class AudioTrackServerProxy : public ServerProxy {
394public:
395 AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700396 size_t frameSize, bool clientInServer = false, uint32_t sampleRate = 0)
397 : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) {
398 mCblk->mSampleRate = sampleRate;
399 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800400protected:
401 virtual ~AudioTrackServerProxy() { }
402
403public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800404 // return value of these methods must be validated by the caller
405 uint32_t getSampleRate() const { return mCblk->mSampleRate; }
406 uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; }
Glenn Kastenc56f3422014-03-21 17:53:17 -0700407 gain_minifloat_packed_t getVolumeLR() const { return mCblk->mVolumeLR; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800408
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 // estimated total number of filled frames available to server to read,
410 // which may include non-contiguous frames
411 virtual size_t framesReady();
Glenn Kastene3aa6592012-12-04 12:22:46 -0800412
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800413 // Currently AudioFlinger will call framesReady() for a fast track from two threads:
414 // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended
415 // to be called from at most one thread of server, and one thread of client.
416 // As a temporary workaround, this method informs the proxy implementation that it
417 // should avoid doing a state queue poll from within framesReady().
418 // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
419 virtual void framesReadyIsCalledByMultipleThreads() { }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800420
421 bool setStreamEndDone(); // and return previous value
Glenn Kasten82aaf942013-07-17 16:05:07 -0700422
423 // Add to the tally of underrun frames, and inform client of underrun
424 virtual void tallyUnderrunFrames(uint32_t frameCount);
425
426 // Return the total number of frames which AudioFlinger desired but were unavailable,
427 // and thus which resulted in an underrun.
428 virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; }
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700429
430 // Return the total number of frames that AudioFlinger has obtained and released
431 virtual size_t framesReleased() const { return mCblk->mServer; }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432};
433
434class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
435public:
436 StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
437 size_t frameSize);
438protected:
439 virtual ~StaticAudioTrackServerProxy() { }
440
441public:
442 virtual size_t framesReady();
443 virtual void framesReadyIsCalledByMultipleThreads();
Glenn Kasten2e422c42013-10-18 13:00:29 -0700444 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800445 virtual void releaseBuffer(Buffer* buffer);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700446 virtual void tallyUnderrunFrames(uint32_t frameCount);
447 virtual uint32_t getUnderrunFrames() const { return 0; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800448
449private:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800450 ssize_t pollPosition(); // poll for state queue update, and return current position
451 StaticAudioTrackSingleStateQueue::Observer mObserver;
452 size_t mPosition; // server's current play position in frames, relative to 0
Andy Hungcb2129b2014-11-11 12:17:22 -0800453
454 size_t mFramesReadySafe; // Assuming size_t read/writes are atomic on 32 / 64 bit
455 // processors, this is a thread-safe version of
456 // mFramesReady.
457 int64_t mFramesReady; // The number of frames ready in the static buffer
458 // including loops. This is 64 bits since loop mode
459 // can cause a track to appear to have a large number
460 // of frames. INT64_MAX means an infinite loop.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800461 bool mFramesReadyIsCalledByMultipleThreads;
462 StaticAudioTrackState mState;
463};
Glenn Kastene3aa6592012-12-04 12:22:46 -0800464
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800465// Proxy used by AudioFlinger for servicing AudioRecord
466class AudioRecordServerProxy : public ServerProxy {
467public:
468 AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700469 size_t frameSize, bool clientInServer)
470 : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/, clientInServer) { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471protected:
472 virtual ~AudioRecordServerProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800473};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474
475// ----------------------------------------------------------------------------
476
477}; // namespace android
478
479#endif // ANDROID_AUDIO_TRACK_SHARED_H