blob: b5a4c0b8900c97662becd3933ecbce210b411e8c [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
23#include <utils/threads.h>
Glenn Kastene3aa6592012-12-04 12:22:46 -080024#include <utils/Log.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080025#include <utils/RefBase.h>
26#include <media/nbaio/roundup.h>
27#include <media/SingleStateQueue.h>
28#include <private/media/StaticAudioTrackState.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
Glenn Kasten9f80dd22012-12-18 15:57:32 -080063typedef SingleStateQueue<StaticAudioTrackState> StaticAudioTrackSingleStateQueue;
64
Glenn Kastene3aa6592012-12-04 12:22:46 -080065struct AudioTrackSharedStatic {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080066 StaticAudioTrackSingleStateQueue::Shared
67 mSingleStateQueue;
68 size_t mBufferPosition; // updated asynchronously by server,
69 // "for entertainment purposes only"
Glenn Kastene3aa6592012-12-04 12:22:46 -080070};
71
72// ----------------------------------------------------------------------------
73
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -080074// Important: do not add any virtual methods, including ~
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075struct audio_track_cblk_t
76{
Glenn Kasten9f80dd22012-12-18 15:57:32 -080077 // Since the control block is always located in shared memory, this constructor
78 // is only used for placement new(). It is never used for regular new() or stack.
79 audio_track_cblk_t();
80 /*virtual*/ ~audio_track_cblk_t() { }
81
Glenn Kastene3aa6592012-12-04 12:22:46 -080082 friend class Proxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080083 friend class ClientProxy;
Glenn Kastene3aa6592012-12-04 12:22:46 -080084 friend class AudioTrackClientProxy;
85 friend class AudioRecordClientProxy;
86 friend class ServerProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080087 friend class AudioTrackServerProxy;
88 friend class AudioRecordServerProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089
90 // The data members are grouped so that members accessed frequently and in the same context
91 // are in the same line of data cache.
Glenn Kasten99e53b82012-01-19 08:59:58 -080092
Glenn Kastenf20e1d82013-07-12 09:45:18 -070093 uint32_t mServer; // Number of filled frames consumed by server (mIsOut),
94 // or filled frames provided by server (!mIsOut).
95 // It is updated asynchronously by server without a barrier.
96 // The value should be used "for entertainment purposes only",
97 // which means don't make important decisions based on it.
Glenn Kasten22eb4e22012-11-07 14:03:00 -080098
Glenn Kasten74935e42013-12-19 08:56:45 -080099 uint32_t mPad1; // unused
Glenn Kasten99e53b82012-01-19 08:59:58 -0800100
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700101 volatile int32_t mFutex; // event flag: down (P) by client,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 // up (V) by server or binderDied() or interrupt()
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700103#define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending
Glenn Kasten99e53b82012-01-19 08:59:58 -0800104
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800105private:
106
107 size_t mMinimum; // server wakes up client if available >= mMinimum
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800108
109 // Channel volumes are fixed point U4.12, so 0x1000 means 1.0.
110 // Left channel is in [0:15], right channel is in [16:31].
111 // Always read and write the combined pair atomically.
112 // For AudioTrack only, not used by AudioRecord.
Glenn Kasten83d86532012-01-17 14:39:34 -0800113 uint32_t mVolumeLR;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800114
Glenn Kastene3aa6592012-12-04 12:22:46 -0800115 uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz
116 // or 0 == default. Write-only client, read-only server.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800117
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800118 // client write-only, server read-only
119 uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0
120
Glenn Kastend054c322013-07-12 12:59:20 -0700121 uint16_t mPad2; // unused
Eric Laurentd1b449a2010-05-14 03:26:45 -0700122
Glenn Kastene3aa6592012-12-04 12:22:46 -0800123public:
Glenn Kasten99e53b82012-01-19 08:59:58 -0800124
Glenn Kasten96f60d82013-07-12 10:21:18 -0700125 volatile int32_t mFlags; // combinations of CBLK_*
Eric Laurent38ccae22011-03-28 18:37:07 -0700126
Eric Laurentd1b449a2010-05-14 03:26:45 -0700127 // Cache line boundary (32 bytes)
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700128
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800129public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800130 union {
131 AudioTrackSharedStreaming mStreaming;
132 AudioTrackSharedStatic mStatic;
133 int mAlign[8];
134 } u;
135
136 // Cache line boundary (32 bytes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137};
138
Glenn Kastene3aa6592012-12-04 12:22:46 -0800139// ----------------------------------------------------------------------------
140
141// Proxy for shared memory control block, to isolate callers from needing to know the details.
142// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
143// The proxies are located in normal memory, and are not multi-thread safe within a given side.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800144class Proxy : public RefBase {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800145protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800146 Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
147 bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800148 virtual ~Proxy() { }
149
150public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800151 struct Buffer {
152 size_t mFrameCount; // number of frames available in this buffer
153 void* mRaw; // pointer to first frame
154 size_t mNonContig; // number of additional non-contiguous frames available
155 };
Glenn Kastene3aa6592012-12-04 12:22:46 -0800156
157protected:
158 // These refer to shared memory, and are virtual addresses with respect to the current process.
159 // They may have different virtual addresses within the other process.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800160 audio_track_cblk_t* const mCblk; // the control block
161 void* const mBuffers; // starting address of buffers
Glenn Kastene3aa6592012-12-04 12:22:46 -0800162
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800163 const size_t mFrameCount; // not necessarily a power of 2
164 const size_t mFrameSize; // in bytes
165 const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
166 const bool mIsOut; // true for AudioTrack, false for AudioRecord
167 const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
168 bool mIsShutdown; // latch set to true when shared memory corruption detected
Glenn Kasten7db7df02013-06-25 16:13:23 -0700169 size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
Glenn Kastene3aa6592012-12-04 12:22:46 -0800170};
171
172// ----------------------------------------------------------------------------
173
174// Proxy seen by AudioTrack client and AudioRecord client
175class ClientProxy : public Proxy {
176protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800177 ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
178 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800179 virtual ~ClientProxy() { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800180
181public:
182 static const struct timespec kForever;
183 static const struct timespec kNonBlocking;
184
185 // Obtain a buffer with filled frames (reading) or empty frames (writing).
186 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
187 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
188 // sets or extends the unreleased frame count.
189 // On entry:
190 // buffer->mFrameCount should be initialized to maximum number of desired frames,
191 // which must be > 0.
192 // buffer->mNonContig is unused.
193 // buffer->mRaw is unused.
194 // requested is the requested timeout in local monotonic delta time units:
195 // NULL or &kNonBlocking means non-blocking (zero timeout).
196 // &kForever means block forever (infinite timeout).
197 // Other values mean a specific timeout in local monotonic delta time units.
198 // elapsed is a pointer to a location that will hold the total local monotonic time that
199 // elapsed while blocked, or NULL if not needed.
200 // On exit:
201 // buffer->mFrameCount has the actual number of contiguous available frames,
202 // which is always 0 when the return status != NO_ERROR.
203 // buffer->mNonContig is the number of additional non-contiguous available frames.
204 // buffer->mRaw is a pointer to the first available frame,
205 // or NULL when buffer->mFrameCount == 0.
206 // The return status is one of:
207 // NO_ERROR Success, buffer->mFrameCount > 0.
208 // WOULD_BLOCK Non-blocking mode and no frames are available.
209 // TIMED_OUT Timeout occurred before any frames became available.
210 // This can happen even for infinite timeout, due to a spurious wakeup.
211 // In this case, the caller should investigate and then re-try as appropriate.
212 // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
213 // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again.
214 // NO_INIT Shared memory is corrupt.
Glenn Kasten7db7df02013-06-25 16:13:23 -0700215 // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800216 status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
217 struct timespec *elapsed = NULL);
218
219 // Release (some of) the frames last obtained.
220 // On entry, buffer->mFrameCount should have the number of frames to release,
221 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
222 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
223 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
224 // On exit:
225 // buffer->mFrameCount is zero.
226 // buffer->mRaw is NULL.
227 void releaseBuffer(Buffer* buffer);
228
229 // Call after detecting server's death
230 void binderDied();
231
232 // Call to force an obtainBuffer() to return quickly with -EINTR
233 void interrupt();
234
235 size_t getPosition() {
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700236 return mEpoch + mCblk->mServer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800237 }
238
239 void setEpoch(size_t epoch) {
240 mEpoch = epoch;
241 }
242
243 void setMinimum(size_t minimum) {
244 mCblk->mMinimum = minimum;
245 }
246
247 // Return the number of frames that would need to be obtained and released
248 // in order for the client to be aligned at start of buffer
249 virtual size_t getMisalignment();
250
251 size_t getEpoch() const {
252 return mEpoch;
253 }
254
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700255 size_t getFramesFilled();
256
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800257private:
258 size_t mEpoch;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800259};
260
261// ----------------------------------------------------------------------------
262
263// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
264class AudioTrackClientProxy : public ClientProxy {
265public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800266 AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
267 size_t frameSize, bool clientInServer = false)
268 : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
269 clientInServer) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800270 virtual ~AudioTrackClientProxy() { }
271
272 // No barriers on the following operations, so the ordering of loads/stores
273 // with respect to other parameters is UNPREDICTABLE. That's considered safe.
274
275 // caller must limit to 0.0 <= sendLevel <= 1.0
276 void setSendLevel(float sendLevel) {
277 mCblk->mSendLevel = uint16_t(sendLevel * 0x1000);
278 }
279
280 // caller must limit to 0 <= volumeLR <= 0x10001000
281 void setVolumeLR(uint32_t volumeLR) {
282 mCblk->mVolumeLR = volumeLR;
283 }
284
285 void setSampleRate(uint32_t sampleRate) {
286 mCblk->mSampleRate = sampleRate;
287 }
288
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800289 virtual void flush();
290
291 virtual uint32_t getUnderrunFrames() const {
292 return mCblk->u.mStreaming.mUnderrunFrames;
293 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800294
295 bool clearStreamEndDone(); // and return previous value
296
297 bool getStreamEndDone() const;
298
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100299 status_t waitStreamEndDone(const struct timespec *requested);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800300};
301
302class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
303public:
304 StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
305 size_t frameSize);
306 virtual ~StaticAudioTrackClientProxy() { }
307
308 virtual void flush();
309
310#define MIN_LOOP 16 // minimum length of each loop iteration in frames
311 void setLoop(size_t loopStart, size_t loopEnd, int loopCount);
312 size_t getBufferPosition();
313
314 virtual size_t getMisalignment() {
315 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800316 }
317
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800318 virtual uint32_t getUnderrunFrames() const {
319 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800320 }
321
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800322private:
323 StaticAudioTrackSingleStateQueue::Mutator mMutator;
324 size_t mBufferPosition; // so that getBufferPosition() appears to be synchronous
Glenn Kastene3aa6592012-12-04 12:22:46 -0800325};
326
327// ----------------------------------------------------------------------------
328
329// Proxy used by AudioRecord client
330class AudioRecordClientProxy : public ClientProxy {
331public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800332 AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
333 size_t frameSize)
334 : ClientProxy(cblk, buffers, frameCount, frameSize,
335 false /*isOut*/, false /*clientInServer*/) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800336 ~AudioRecordClientProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800337};
338
339// ----------------------------------------------------------------------------
340
341// Proxy used by AudioFlinger server
342class ServerProxy : public Proxy {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800343protected:
344 ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
345 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800346public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800347 virtual ~ServerProxy() { }
348
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800349 // Obtain a buffer with filled frames (writing) or empty frames (reading).
350 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
351 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
352 // sets or extends the unreleased frame count.
353 // Always non-blocking.
354 // On entry:
355 // buffer->mFrameCount should be initialized to maximum number of desired frames,
356 // which must be > 0.
357 // buffer->mNonContig is unused.
358 // buffer->mRaw is unused.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700359 // ackFlush is true iff being called from Track::start to acknowledge a pending flush.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800360 // On exit:
361 // buffer->mFrameCount has the actual number of contiguous available frames,
362 // which is always 0 when the return status != NO_ERROR.
363 // buffer->mNonContig is the number of additional non-contiguous available frames.
364 // buffer->mRaw is a pointer to the first available frame,
365 // or NULL when buffer->mFrameCount == 0.
366 // The return status is one of:
367 // NO_ERROR Success, buffer->mFrameCount > 0.
368 // WOULD_BLOCK No frames are available.
369 // NO_INIT Shared memory is corrupt.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700370 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800371
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800372 // Release (some of) the frames last obtained.
373 // On entry, buffer->mFrameCount should have the number of frames to release,
374 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
375 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
376 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
377 // On exit:
378 // buffer->mFrameCount is zero.
379 // buffer->mRaw is NULL.
380 virtual void releaseBuffer(Buffer* buffer);
381
382protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800384 int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800385};
386
387// Proxy used by AudioFlinger for servicing AudioTrack
388class AudioTrackServerProxy : public ServerProxy {
389public:
390 AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
391 size_t frameSize, bool clientInServer = false)
392 : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) { }
393protected:
394 virtual ~AudioTrackServerProxy() { }
395
396public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800397 // return value of these methods must be validated by the caller
398 uint32_t getSampleRate() const { return mCblk->mSampleRate; }
399 uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; }
400 uint32_t getVolumeLR() const { return mCblk->mVolumeLR; }
401
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800402 // estimated total number of filled frames available to server to read,
403 // which may include non-contiguous frames
404 virtual size_t framesReady();
Glenn Kastene3aa6592012-12-04 12:22:46 -0800405
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800406 // Currently AudioFlinger will call framesReady() for a fast track from two threads:
407 // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended
408 // to be called from at most one thread of server, and one thread of client.
409 // As a temporary workaround, this method informs the proxy implementation that it
410 // should avoid doing a state queue poll from within framesReady().
411 // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
412 virtual void framesReadyIsCalledByMultipleThreads() { }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800413
414 bool setStreamEndDone(); // and return previous value
Glenn Kasten82aaf942013-07-17 16:05:07 -0700415
416 // Add to the tally of underrun frames, and inform client of underrun
417 virtual void tallyUnderrunFrames(uint32_t frameCount);
418
419 // Return the total number of frames which AudioFlinger desired but were unavailable,
420 // and thus which resulted in an underrun.
421 virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; }
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700422
423 // Return the total number of frames that AudioFlinger has obtained and released
424 virtual size_t framesReleased() const { return mCblk->mServer; }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800425};
426
427class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
428public:
429 StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
430 size_t frameSize);
431protected:
432 virtual ~StaticAudioTrackServerProxy() { }
433
434public:
435 virtual size_t framesReady();
436 virtual void framesReadyIsCalledByMultipleThreads();
Glenn Kasten2e422c42013-10-18 13:00:29 -0700437 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800438 virtual void releaseBuffer(Buffer* buffer);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700439 virtual void tallyUnderrunFrames(uint32_t frameCount);
440 virtual uint32_t getUnderrunFrames() const { return 0; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800441
442private:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800443 ssize_t pollPosition(); // poll for state queue update, and return current position
444 StaticAudioTrackSingleStateQueue::Observer mObserver;
445 size_t mPosition; // server's current play position in frames, relative to 0
446 size_t mEnd; // cached value computed from mState, safe for asynchronous read
447 bool mFramesReadyIsCalledByMultipleThreads;
448 StaticAudioTrackState mState;
449};
Glenn Kastene3aa6592012-12-04 12:22:46 -0800450
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451// Proxy used by AudioFlinger for servicing AudioRecord
452class AudioRecordServerProxy : public ServerProxy {
453public:
454 AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
455 size_t frameSize)
456 : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/,
457 false /*clientInServer*/) { }
458protected:
459 virtual ~AudioRecordServerProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800460};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461
462// ----------------------------------------------------------------------------
463
464}; // namespace android
465
466#endif // ANDROID_AUDIO_TRACK_SHARED_H