blob: 3901e79481d60e643bf6896f1d3f77af37e51052 [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;
Glenn Kastenfdac7c02014-01-28 11:03:28 -080068 // This field should be a size_t, but since it is located in shared memory we
69 // force to 32-bit. The client and server may have different typedefs for size_t.
70 uint32_t mBufferPosition; // updated asynchronously by server,
Glenn Kasten9f80dd22012-12-18 15:57:32 -080071 // "for entertainment purposes only"
Glenn Kastene3aa6592012-12-04 12:22:46 -080072};
73
74// ----------------------------------------------------------------------------
75
Glenn Kasten1a0ae5b2012-02-03 10:24:48 -080076// Important: do not add any virtual methods, including ~
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080077struct audio_track_cblk_t
78{
Glenn Kasten9f80dd22012-12-18 15:57:32 -080079 // Since the control block is always located in shared memory, this constructor
80 // is only used for placement new(). It is never used for regular new() or stack.
81 audio_track_cblk_t();
82 /*virtual*/ ~audio_track_cblk_t() { }
83
Glenn Kastene3aa6592012-12-04 12:22:46 -080084 friend class Proxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080085 friend class ClientProxy;
Glenn Kastene3aa6592012-12-04 12:22:46 -080086 friend class AudioTrackClientProxy;
87 friend class AudioRecordClientProxy;
88 friend class ServerProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -080089 friend class AudioTrackServerProxy;
90 friend class AudioRecordServerProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92 // The data members are grouped so that members accessed frequently and in the same context
93 // are in the same line of data cache.
Glenn Kasten99e53b82012-01-19 08:59:58 -080094
Glenn Kastenf20e1d82013-07-12 09:45:18 -070095 uint32_t mServer; // Number of filled frames consumed by server (mIsOut),
96 // or filled frames provided by server (!mIsOut).
97 // It is updated asynchronously by server without a barrier.
98 // The value should be used "for entertainment purposes only",
99 // which means don't make important decisions based on it.
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800100
Glenn Kasten74935e42013-12-19 08:56:45 -0800101 uint32_t mPad1; // unused
Glenn Kasten99e53b82012-01-19 08:59:58 -0800102
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700103 volatile int32_t mFutex; // event flag: down (P) by client,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800104 // up (V) by server or binderDied() or interrupt()
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700105#define CBLK_FUTEX_WAKE 1 // if event flag bit is set, then a deferred wake is pending
Glenn Kasten99e53b82012-01-19 08:59:58 -0800106
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800107private:
108
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800109 // This field should be a size_t, but since it is located in shared memory we
110 // force to 32-bit. The client and server may have different typedefs for size_t.
111 uint32_t mMinimum; // server wakes up client if available >= mMinimum
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800112
113 // Channel volumes are fixed point U4.12, so 0x1000 means 1.0.
114 // Left channel is in [0:15], right channel is in [16:31].
115 // Always read and write the combined pair atomically.
116 // For AudioTrack only, not used by AudioRecord.
Glenn Kasten83d86532012-01-17 14:39:34 -0800117 uint32_t mVolumeLR;
Glenn Kastenb1cf75c2012-01-17 12:20:54 -0800118
Glenn Kastene3aa6592012-12-04 12:22:46 -0800119 uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz
120 // or 0 == default. Write-only client, read-only server.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800121
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800122 // client write-only, server read-only
123 uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0
124
Glenn Kastend054c322013-07-12 12:59:20 -0700125 uint16_t mPad2; // unused
Eric Laurentd1b449a2010-05-14 03:26:45 -0700126
Glenn Kastene3aa6592012-12-04 12:22:46 -0800127public:
Glenn Kasten99e53b82012-01-19 08:59:58 -0800128
Glenn Kasten96f60d82013-07-12 10:21:18 -0700129 volatile int32_t mFlags; // combinations of CBLK_*
Eric Laurent38ccae22011-03-28 18:37:07 -0700130
Eric Laurentd1b449a2010-05-14 03:26:45 -0700131 // Cache line boundary (32 bytes)
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700132
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800133public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800134 union {
135 AudioTrackSharedStreaming mStreaming;
136 AudioTrackSharedStatic mStatic;
137 int mAlign[8];
138 } u;
139
140 // Cache line boundary (32 bytes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141};
142
Glenn Kastene3aa6592012-12-04 12:22:46 -0800143// ----------------------------------------------------------------------------
144
145// Proxy for shared memory control block, to isolate callers from needing to know the details.
146// There is exactly one ClientProxy and one ServerProxy per shared memory control block.
147// The proxies are located in normal memory, and are not multi-thread safe within a given side.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148class Proxy : public RefBase {
Glenn Kastene3aa6592012-12-04 12:22:46 -0800149protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800150 Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, bool isOut,
151 bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800152 virtual ~Proxy() { }
153
154public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800155 struct Buffer {
156 size_t mFrameCount; // number of frames available in this buffer
157 void* mRaw; // pointer to first frame
158 size_t mNonContig; // number of additional non-contiguous frames available
159 };
Glenn Kastene3aa6592012-12-04 12:22:46 -0800160
161protected:
162 // These refer to shared memory, and are virtual addresses with respect to the current process.
163 // They may have different virtual addresses within the other process.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800164 audio_track_cblk_t* const mCblk; // the control block
165 void* const mBuffers; // starting address of buffers
Glenn Kastene3aa6592012-12-04 12:22:46 -0800166
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800167 const size_t mFrameCount; // not necessarily a power of 2
168 const size_t mFrameSize; // in bytes
169 const size_t mFrameCountP2; // mFrameCount rounded to power of 2, streaming mode
170 const bool mIsOut; // true for AudioTrack, false for AudioRecord
171 const bool mClientInServer; // true for OutputTrack, false for AudioTrack & AudioRecord
172 bool mIsShutdown; // latch set to true when shared memory corruption detected
Glenn Kasten7db7df02013-06-25 16:13:23 -0700173 size_t mUnreleased; // unreleased frames remaining from most recent obtainBuffer
Glenn Kastene3aa6592012-12-04 12:22:46 -0800174};
175
176// ----------------------------------------------------------------------------
177
178// Proxy seen by AudioTrack client and AudioRecord client
179class ClientProxy : public Proxy {
180protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
182 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800183 virtual ~ClientProxy() { }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800184
185public:
186 static const struct timespec kForever;
187 static const struct timespec kNonBlocking;
188
189 // Obtain a buffer with filled frames (reading) or empty frames (writing).
190 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
191 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
192 // sets or extends the unreleased frame count.
193 // On entry:
194 // buffer->mFrameCount should be initialized to maximum number of desired frames,
195 // which must be > 0.
196 // buffer->mNonContig is unused.
197 // buffer->mRaw is unused.
198 // requested is the requested timeout in local monotonic delta time units:
199 // NULL or &kNonBlocking means non-blocking (zero timeout).
200 // &kForever means block forever (infinite timeout).
201 // Other values mean a specific timeout in local monotonic delta time units.
202 // elapsed is a pointer to a location that will hold the total local monotonic time that
203 // elapsed while blocked, or NULL if not needed.
204 // On exit:
205 // buffer->mFrameCount has the actual number of contiguous available frames,
206 // which is always 0 when the return status != NO_ERROR.
207 // buffer->mNonContig is the number of additional non-contiguous available frames.
208 // buffer->mRaw is a pointer to the first available frame,
209 // or NULL when buffer->mFrameCount == 0.
210 // The return status is one of:
211 // NO_ERROR Success, buffer->mFrameCount > 0.
212 // WOULD_BLOCK Non-blocking mode and no frames are available.
213 // TIMED_OUT Timeout occurred before any frames became available.
214 // This can happen even for infinite timeout, due to a spurious wakeup.
215 // In this case, the caller should investigate and then re-try as appropriate.
216 // DEAD_OBJECT Server has died or invalidated, caller should destroy this proxy and re-create.
217 // -EINTR Call has been interrupted. Look around to see why, and then perhaps try again.
218 // NO_INIT Shared memory is corrupt.
Glenn Kasten7db7df02013-06-25 16:13:23 -0700219 // Assertion failure on entry, if buffer == NULL or buffer->mFrameCount == 0.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800220 status_t obtainBuffer(Buffer* buffer, const struct timespec *requested = NULL,
221 struct timespec *elapsed = NULL);
222
223 // Release (some of) the frames last obtained.
224 // On entry, buffer->mFrameCount should have the number of frames to release,
225 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
226 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
227 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
228 // On exit:
229 // buffer->mFrameCount is zero.
230 // buffer->mRaw is NULL.
231 void releaseBuffer(Buffer* buffer);
232
233 // Call after detecting server's death
234 void binderDied();
235
236 // Call to force an obtainBuffer() to return quickly with -EINTR
237 void interrupt();
238
239 size_t getPosition() {
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700240 return mEpoch + mCblk->mServer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800241 }
242
243 void setEpoch(size_t epoch) {
244 mEpoch = epoch;
245 }
246
247 void setMinimum(size_t minimum) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800248 // This can only happen on a 64-bit client
249 if (minimum > UINT32_MAX) {
250 minimum = UINT32_MAX;
251 }
252 mCblk->mMinimum = (uint32_t) minimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800253 }
254
255 // Return the number of frames that would need to be obtained and released
256 // in order for the client to be aligned at start of buffer
257 virtual size_t getMisalignment();
258
259 size_t getEpoch() const {
260 return mEpoch;
261 }
262
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700263 size_t getFramesFilled();
264
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800265private:
266 size_t mEpoch;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800267};
268
269// ----------------------------------------------------------------------------
270
271// Proxy used by AudioTrack client, which also includes AudioFlinger::PlaybackThread::OutputTrack
272class AudioTrackClientProxy : public ClientProxy {
273public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800274 AudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
275 size_t frameSize, bool clientInServer = false)
276 : ClientProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/,
277 clientInServer) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800278 virtual ~AudioTrackClientProxy() { }
279
280 // No barriers on the following operations, so the ordering of loads/stores
281 // with respect to other parameters is UNPREDICTABLE. That's considered safe.
282
283 // caller must limit to 0.0 <= sendLevel <= 1.0
284 void setSendLevel(float sendLevel) {
285 mCblk->mSendLevel = uint16_t(sendLevel * 0x1000);
286 }
287
288 // caller must limit to 0 <= volumeLR <= 0x10001000
289 void setVolumeLR(uint32_t volumeLR) {
290 mCblk->mVolumeLR = volumeLR;
291 }
292
293 void setSampleRate(uint32_t sampleRate) {
294 mCblk->mSampleRate = sampleRate;
295 }
296
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800297 virtual void flush();
298
299 virtual uint32_t getUnderrunFrames() const {
300 return mCblk->u.mStreaming.mUnderrunFrames;
301 }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800302
303 bool clearStreamEndDone(); // and return previous value
304
305 bool getStreamEndDone() const;
306
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100307 status_t waitStreamEndDone(const struct timespec *requested);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800308};
309
310class StaticAudioTrackClientProxy : public AudioTrackClientProxy {
311public:
312 StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
313 size_t frameSize);
314 virtual ~StaticAudioTrackClientProxy() { }
315
316 virtual void flush();
317
318#define MIN_LOOP 16 // minimum length of each loop iteration in frames
319 void setLoop(size_t loopStart, size_t loopEnd, int loopCount);
320 size_t getBufferPosition();
321
322 virtual size_t getMisalignment() {
323 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800324 }
325
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800326 virtual uint32_t getUnderrunFrames() const {
327 return 0;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800328 }
329
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800330private:
331 StaticAudioTrackSingleStateQueue::Mutator mMutator;
332 size_t mBufferPosition; // so that getBufferPosition() appears to be synchronous
Glenn Kastene3aa6592012-12-04 12:22:46 -0800333};
334
335// ----------------------------------------------------------------------------
336
337// Proxy used by AudioRecord client
338class AudioRecordClientProxy : public ClientProxy {
339public:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800340 AudioRecordClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
341 size_t frameSize)
342 : ClientProxy(cblk, buffers, frameCount, frameSize,
343 false /*isOut*/, false /*clientInServer*/) { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800344 ~AudioRecordClientProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800345};
346
347// ----------------------------------------------------------------------------
348
349// Proxy used by AudioFlinger server
350class ServerProxy : public Proxy {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800351protected:
352 ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
353 bool isOut, bool clientInServer);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800354public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800355 virtual ~ServerProxy() { }
356
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800357 // Obtain a buffer with filled frames (writing) or empty frames (reading).
358 // It is permitted to call obtainBuffer() multiple times in succession, without any intervening
359 // calls to releaseBuffer(). In that case, the final obtainBuffer() is the one that effectively
360 // sets or extends the unreleased frame count.
361 // Always non-blocking.
362 // On entry:
363 // buffer->mFrameCount should be initialized to maximum number of desired frames,
364 // which must be > 0.
365 // buffer->mNonContig is unused.
366 // buffer->mRaw is unused.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700367 // ackFlush is true iff being called from Track::start to acknowledge a pending flush.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800368 // On exit:
369 // buffer->mFrameCount has the actual number of contiguous available frames,
370 // which is always 0 when the return status != NO_ERROR.
371 // buffer->mNonContig is the number of additional non-contiguous available frames.
372 // buffer->mRaw is a pointer to the first available frame,
373 // or NULL when buffer->mFrameCount == 0.
374 // The return status is one of:
375 // NO_ERROR Success, buffer->mFrameCount > 0.
376 // WOULD_BLOCK No frames are available.
377 // NO_INIT Shared memory is corrupt.
Glenn Kasten2e422c42013-10-18 13:00:29 -0700378 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush = false);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800379
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800380 // Release (some of) the frames last obtained.
381 // On entry, buffer->mFrameCount should have the number of frames to release,
382 // which must (cumulatively) be <= the number of frames last obtained but not yet released.
383 // It is permitted to call releaseBuffer() multiple times to release the frames in chunks.
384 // buffer->mRaw is ignored, but is normally same pointer returned by last obtainBuffer().
385 // On exit:
386 // buffer->mFrameCount is zero.
387 // buffer->mRaw is NULL.
388 virtual void releaseBuffer(Buffer* buffer);
389
390protected:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 size_t mAvailToClient; // estimated frames available to client prior to releaseBuffer()
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800392 int32_t mFlush; // our copy of cblk->u.mStreaming.mFlush, for streaming output only
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800393};
394
395// Proxy used by AudioFlinger for servicing AudioTrack
396class AudioTrackServerProxy : public ServerProxy {
397public:
398 AudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
399 size_t frameSize, bool clientInServer = false)
400 : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer) { }
401protected:
402 virtual ~AudioTrackServerProxy() { }
403
404public:
Glenn Kastene3aa6592012-12-04 12:22:46 -0800405 // return value of these methods must be validated by the caller
406 uint32_t getSampleRate() const { return mCblk->mSampleRate; }
407 uint16_t getSendLevel_U4_12() const { return mCblk->mSendLevel; }
408 uint32_t getVolumeLR() const { return mCblk->mVolumeLR; }
409
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800410 // estimated total number of filled frames available to server to read,
411 // which may include non-contiguous frames
412 virtual size_t framesReady();
Glenn Kastene3aa6592012-12-04 12:22:46 -0800413
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 // Currently AudioFlinger will call framesReady() for a fast track from two threads:
415 // FastMixer thread, and normal mixer thread. This is dangerous, as the proxy is intended
416 // to be called from at most one thread of server, and one thread of client.
417 // As a temporary workaround, this method informs the proxy implementation that it
418 // should avoid doing a state queue poll from within framesReady().
419 // FIXME Change AudioFlinger to not call framesReady() from normal mixer thread.
420 virtual void framesReadyIsCalledByMultipleThreads() { }
Eric Laurentbfb1b832013-01-07 09:53:42 -0800421
422 bool setStreamEndDone(); // and return previous value
Glenn Kasten82aaf942013-07-17 16:05:07 -0700423
424 // Add to the tally of underrun frames, and inform client of underrun
425 virtual void tallyUnderrunFrames(uint32_t frameCount);
426
427 // Return the total number of frames which AudioFlinger desired but were unavailable,
428 // and thus which resulted in an underrun.
429 virtual uint32_t getUnderrunFrames() const { return mCblk->u.mStreaming.mUnderrunFrames; }
Glenn Kastenbd096fd2013-08-23 13:53:56 -0700430
431 // Return the total number of frames that AudioFlinger has obtained and released
432 virtual size_t framesReleased() const { return mCblk->mServer; }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800433};
434
435class StaticAudioTrackServerProxy : public AudioTrackServerProxy {
436public:
437 StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
438 size_t frameSize);
439protected:
440 virtual ~StaticAudioTrackServerProxy() { }
441
442public:
443 virtual size_t framesReady();
444 virtual void framesReadyIsCalledByMultipleThreads();
Glenn Kasten2e422c42013-10-18 13:00:29 -0700445 virtual status_t obtainBuffer(Buffer* buffer, bool ackFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446 virtual void releaseBuffer(Buffer* buffer);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700447 virtual void tallyUnderrunFrames(uint32_t frameCount);
448 virtual uint32_t getUnderrunFrames() const { return 0; }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800449
450private:
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451 ssize_t pollPosition(); // poll for state queue update, and return current position
452 StaticAudioTrackSingleStateQueue::Observer mObserver;
453 size_t mPosition; // server's current play position in frames, relative to 0
454 size_t mEnd; // cached value computed from mState, safe for asynchronous read
455 bool mFramesReadyIsCalledByMultipleThreads;
456 StaticAudioTrackState mState;
457};
Glenn Kastene3aa6592012-12-04 12:22:46 -0800458
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800459// Proxy used by AudioFlinger for servicing AudioRecord
460class AudioRecordServerProxy : public ServerProxy {
461public:
462 AudioRecordServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
463 size_t frameSize)
464 : ServerProxy(cblk, buffers, frameCount, frameSize, false /*isOut*/,
465 false /*clientInServer*/) { }
466protected:
467 virtual ~AudioRecordServerProxy() { }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800468};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469
470// ----------------------------------------------------------------------------
471
472}; // namespace android
473
474#endif // ANDROID_AUDIO_TRACK_SHARED_H