blob: 55bf1756b906bb9752274785513f3b67f37878df [file] [log] [blame]
Glenn Kastena8190fc2012-12-03 17:06:56 -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#define LOG_TAG "AudioTrackShared"
18//#define LOG_NDEBUG 0
19
20#include <private/media/AudioTrackShared.h>
21#include <utils/Log.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080022extern "C" {
23#include "../private/bionic_futex.h"
24}
Glenn Kastena8190fc2012-12-03 17:06:56 -080025
26namespace android {
27
28audio_track_cblk_t::audio_track_cblk_t()
Glenn Kasten9f80dd22012-12-18 15:57:32 -080029 : server(0), frameCount_(0), mFutex(0), mMinimum(0),
30 mVolumeLR(0x10001000), mSampleRate(0), mSendLevel(0), mName(0), flags(0)
31{
32 memset(&u, 0, sizeof(u));
33}
34
35// ---------------------------------------------------------------------------
36
37Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
38 bool isOut, bool clientInServer)
39 : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize),
40 mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer),
Glenn Kasten7db7df02013-06-25 16:13:23 -070041 mIsShutdown(false), mUnreleased(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080042{
43}
44
Glenn Kasten9f80dd22012-12-18 15:57:32 -080045// ---------------------------------------------------------------------------
46
47ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
48 size_t frameSize, bool isOut, bool clientInServer)
49 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), mEpoch(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080050{
Glenn Kastena8190fc2012-12-03 17:06:56 -080051}
52
Glenn Kasten9f80dd22012-12-18 15:57:32 -080053const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/};
54const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/};
55
56#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS
57
58// To facilitate quicker recovery from server failure, this value limits the timeout per each futex
59// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit.
60// FIXME May not be compatible with audio tunneling requirements where timeout should be in the
61// order of minutes.
62#define MAX_SEC 5
63
64status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested,
65 struct timespec *elapsed)
Glenn Kastena8190fc2012-12-03 17:06:56 -080066{
Glenn Kasten7db7df02013-06-25 16:13:23 -070067 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080068 struct timespec total; // total elapsed time spent waiting
69 total.tv_sec = 0;
70 total.tv_nsec = 0;
71 bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting
Glenn Kastena8190fc2012-12-03 17:06:56 -080072
Glenn Kasten9f80dd22012-12-18 15:57:32 -080073 status_t status;
74 enum {
75 TIMEOUT_ZERO, // requested == NULL || *requested == 0
76 TIMEOUT_INFINITE, // *requested == infinity
77 TIMEOUT_FINITE, // 0 < *requested < infinity
78 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
79 } timeout;
80 if (requested == NULL) {
81 timeout = TIMEOUT_ZERO;
82 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
83 timeout = TIMEOUT_ZERO;
84 } else if (requested->tv_sec == INT_MAX) {
85 timeout = TIMEOUT_INFINITE;
Glenn Kastena8190fc2012-12-03 17:06:56 -080086 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080087 timeout = TIMEOUT_FINITE;
88 if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) {
89 measure = true;
90 }
Glenn Kastena8190fc2012-12-03 17:06:56 -080091 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -080092 struct timespec before;
93 bool beforeIsValid = false;
94 audio_track_cblk_t* cblk = mCblk;
95 bool ignoreInitialPendingInterrupt = true;
96 // check for shared memory corruption
97 if (mIsShutdown) {
98 status = NO_INIT;
99 goto end;
100 }
101 for (;;) {
102 int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->flags);
103 // check for track invalidation by server, or server death detection
104 if (flags & CBLK_INVALID) {
105 ALOGV("Track invalidated");
106 status = DEAD_OBJECT;
107 goto end;
108 }
109 // check for obtainBuffer interrupted by client
110 if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) {
111 ALOGV("obtainBuffer() interrupted by client");
112 status = -EINTR;
113 goto end;
114 }
115 ignoreInitialPendingInterrupt = false;
116 // compute number of frames available to write (AudioTrack) or read (AudioRecord)
117 int32_t front;
118 int32_t rear;
119 if (mIsOut) {
120 // The barrier following the read of mFront is probably redundant.
121 // We're about to perform a conditional branch based on 'filled',
122 // which will force the processor to observe the read of mFront
123 // prior to allowing data writes starting at mRaw.
124 // However, the processor may support speculative execution,
125 // and be unable to undo speculative writes into shared memory.
126 // The barrier will prevent such speculative execution.
127 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
128 rear = cblk->u.mStreaming.mRear;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800129 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800130 // On the other hand, this barrier is required.
131 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
132 front = cblk->u.mStreaming.mFront;
133 }
134 ssize_t filled = rear - front;
135 // pipe should not be overfull
136 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
137 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
138 mIsShutdown = true;
139 status = NO_INIT;
140 goto end;
141 }
142 // don't allow filling pipe beyond the nominal size
143 size_t avail = mIsOut ? mFrameCount - filled : filled;
144 if (avail > 0) {
145 // 'avail' may be non-contiguous, so return only the first contiguous chunk
146 size_t part1;
147 if (mIsOut) {
148 rear &= mFrameCountP2 - 1;
149 part1 = mFrameCountP2 - rear;
150 } else {
151 front &= mFrameCountP2 - 1;
152 part1 = mFrameCountP2 - front;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800153 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800154 if (part1 > avail) {
155 part1 = avail;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800156 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800157 if (part1 > buffer->mFrameCount) {
158 part1 = buffer->mFrameCount;
159 }
160 buffer->mFrameCount = part1;
161 buffer->mRaw = part1 > 0 ?
162 &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL;
163 buffer->mNonContig = avail - part1;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700164 mUnreleased = part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800165 status = NO_ERROR;
166 break;
167 }
168 struct timespec remaining;
169 const struct timespec *ts;
170 switch (timeout) {
171 case TIMEOUT_ZERO:
172 status = WOULD_BLOCK;
173 goto end;
174 case TIMEOUT_INFINITE:
175 ts = NULL;
176 break;
177 case TIMEOUT_FINITE:
178 timeout = TIMEOUT_CONTINUE;
179 if (MAX_SEC == 0) {
180 ts = requested;
181 break;
182 }
183 // fall through
184 case TIMEOUT_CONTINUE:
185 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
186 if (!measure || requested->tv_sec < total.tv_sec ||
187 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
188 status = TIMED_OUT;
189 goto end;
190 }
191 remaining.tv_sec = requested->tv_sec - total.tv_sec;
192 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
193 remaining.tv_nsec += 1000000000;
194 remaining.tv_sec++;
195 }
196 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
197 remaining.tv_sec = MAX_SEC;
198 remaining.tv_nsec = 0;
199 }
200 ts = &remaining;
201 break;
202 default:
203 LOG_FATAL("%s timeout=%d", timeout);
204 ts = NULL;
205 break;
206 }
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700207 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
208 if (!(old & CBLK_FUTEX_WAKE)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800209 int rc;
210 if (measure && !beforeIsValid) {
211 clock_gettime(CLOCK_MONOTONIC, &before);
212 beforeIsValid = true;
213 }
214 int ret = __futex_syscall4(&cblk->mFutex,
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700215 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800216 // update total elapsed time spent waiting
217 if (measure) {
218 struct timespec after;
219 clock_gettime(CLOCK_MONOTONIC, &after);
220 total.tv_sec += after.tv_sec - before.tv_sec;
221 long deltaNs = after.tv_nsec - before.tv_nsec;
222 if (deltaNs < 0) {
223 deltaNs += 1000000000;
224 total.tv_sec--;
225 }
226 if ((total.tv_nsec += deltaNs) >= 1000000000) {
227 total.tv_nsec -= 1000000000;
228 total.tv_sec++;
229 }
230 before = after;
231 beforeIsValid = true;
232 }
233 switch (ret) {
234 case 0: // normal wakeup by server, or by binderDied()
235 case -EWOULDBLOCK: // benign race condition with server
236 case -EINTR: // wait was interrupted by signal or other spurious wakeup
237 case -ETIMEDOUT: // time-out expired
Glenn Kasten7db7df02013-06-25 16:13:23 -0700238 // FIXME these error/non-0 status are being dropped
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800239 break;
240 default:
241 ALOGE("%s unexpected error %d", __func__, ret);
242 status = -ret;
243 goto end;
244 }
245 }
246 }
247
248end:
249 if (status != NO_ERROR) {
250 buffer->mFrameCount = 0;
251 buffer->mRaw = NULL;
252 buffer->mNonContig = 0;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700253 mUnreleased = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800254 }
255 if (elapsed != NULL) {
256 *elapsed = total;
257 }
258 if (requested == NULL) {
259 requested = &kNonBlocking;
260 }
261 if (measure) {
262 ALOGV("requested %d.%03d elapsed %d.%03d", requested->tv_sec, requested->tv_nsec / 1000000,
263 total.tv_sec, total.tv_nsec / 1000000);
264 }
265 return status;
266}
267
268void ClientProxy::releaseBuffer(Buffer* buffer)
269{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700270 LOG_ALWAYS_FATAL_IF(buffer == NULL);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800271 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700272 if (stepCount == 0 || mIsShutdown) {
273 // prevent accidental re-use of buffer
274 buffer->mFrameCount = 0;
275 buffer->mRaw = NULL;
276 buffer->mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800277 return;
278 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700279 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
280 mUnreleased -= stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800281 audio_track_cblk_t* cblk = mCblk;
282 // Both of these barriers are required
283 if (mIsOut) {
284 int32_t rear = cblk->u.mStreaming.mRear;
285 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
286 } else {
287 int32_t front = cblk->u.mStreaming.mFront;
288 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
289 }
290}
291
292void ClientProxy::binderDied()
293{
294 audio_track_cblk_t* cblk = mCblk;
295 if (!(android_atomic_or(CBLK_INVALID, &cblk->flags) & CBLK_INVALID)) {
296 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
297 (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
298 1);
299 }
300}
301
302void ClientProxy::interrupt()
303{
304 audio_track_cblk_t* cblk = mCblk;
305 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->flags) & CBLK_INTERRUPT)) {
306 (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
307 1);
308 }
309}
310
311size_t ClientProxy::getMisalignment()
312{
313 audio_track_cblk_t* cblk = mCblk;
314 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
315 (mFrameCountP2 - 1);
316}
317
318// ---------------------------------------------------------------------------
319
320void AudioTrackClientProxy::flush()
321{
322 mCblk->u.mStreaming.mFlush++;
323}
324
325// ---------------------------------------------------------------------------
326
327StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
328 size_t frameCount, size_t frameSize)
329 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
330 mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0)
331{
332}
333
334void StaticAudioTrackClientProxy::flush()
335{
336 LOG_FATAL("static flush");
337}
338
339void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
340{
341 StaticAudioTrackState newState;
342 newState.mLoopStart = loopStart;
343 newState.mLoopEnd = loopEnd;
344 newState.mLoopCount = loopCount;
345 mBufferPosition = loopStart;
346 (void) mMutator.push(newState);
347}
348
349size_t StaticAudioTrackClientProxy::getBufferPosition()
350{
351 size_t bufferPosition;
352 if (mMutator.ack()) {
353 bufferPosition = mCblk->u.mStatic.mBufferPosition;
354 if (bufferPosition > mFrameCount) {
355 bufferPosition = mFrameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800356 }
357 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800358 bufferPosition = mBufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800359 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800360 return bufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800361}
362
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800363// ---------------------------------------------------------------------------
364
365ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
366 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700367 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800368 mAvailToClient(0), mFlush(0), mDeferWake(false)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800369{
Glenn Kastena8190fc2012-12-03 17:06:56 -0800370}
371
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800372status_t ServerProxy::obtainBuffer(Buffer* buffer)
373{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700374 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800375 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700376 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700378 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800379 audio_track_cblk_t* cblk = mCblk;
380 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
381 // or use previous cached value from framesReady(), with added barrier if it omits.
382 int32_t front;
383 int32_t rear;
384 // See notes on barriers at ClientProxy::obtainBuffer()
385 if (mIsOut) {
386 int32_t flush = cblk->u.mStreaming.mFlush;
387 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
388 if (flush != mFlush) {
389 front = rear;
390 mFlush = flush;
Glenn Kasten050501d2013-07-11 10:35:38 -0700391 // effectively obtain then release whatever is in the buffer
392 android_atomic_release_store(rear, &cblk->u.mStreaming.mFront);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800393 } else {
394 front = cblk->u.mStreaming.mFront;
395 }
396 } else {
397 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
398 rear = cblk->u.mStreaming.mRear;
399 }
400 ssize_t filled = rear - front;
401 // pipe should not already be overfull
402 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
403 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
404 mIsShutdown = true;
405 }
406 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700407 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800408 }
409 // don't allow filling pipe beyond the nominal size
410 size_t availToServer;
411 if (mIsOut) {
412 availToServer = filled;
413 mAvailToClient = mFrameCount - filled;
414 } else {
415 availToServer = mFrameCount - filled;
416 mAvailToClient = filled;
417 }
418 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
419 size_t part1;
420 if (mIsOut) {
421 front &= mFrameCountP2 - 1;
422 part1 = mFrameCountP2 - front;
423 } else {
424 rear &= mFrameCountP2 - 1;
425 part1 = mFrameCountP2 - rear;
426 }
427 if (part1 > availToServer) {
428 part1 = availToServer;
429 }
430 size_t ask = buffer->mFrameCount;
431 if (part1 > ask) {
432 part1 = ask;
433 }
434 // is assignment redundant in some cases?
435 buffer->mFrameCount = part1;
436 buffer->mRaw = part1 > 0 ?
437 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
438 buffer->mNonContig = availToServer - part1;
439 mUnreleased = part1;
440 // optimization to avoid waking up the client too early
441 // FIXME need to test for recording
442 mDeferWake = part1 < ask && availToServer >= ask;
443 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700444 }
445no_init:
446 buffer->mFrameCount = 0;
447 buffer->mRaw = NULL;
448 buffer->mNonContig = 0;
449 mUnreleased = 0;
450 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451}
452
453void ServerProxy::releaseBuffer(Buffer* buffer)
454{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700455 LOG_ALWAYS_FATAL_IF(buffer == NULL);
456 size_t stepCount = buffer->mFrameCount;
457 if (stepCount == 0 || mIsShutdown) {
458 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800459 buffer->mFrameCount = 0;
460 buffer->mRaw = NULL;
461 buffer->mNonContig = 0;
462 return;
463 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700464 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800465 mUnreleased -= stepCount;
466 audio_track_cblk_t* cblk = mCblk;
467 if (mIsOut) {
468 int32_t front = cblk->u.mStreaming.mFront;
469 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
470 } else {
471 int32_t rear = cblk->u.mStreaming.mRear;
472 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
473 }
474
475 mCblk->server += stepCount;
476
477 size_t half = mFrameCount / 2;
478 if (half == 0) {
479 half = 1;
480 }
481 size_t minimum = cblk->mMinimum;
482 if (minimum == 0) {
483 minimum = mIsOut ? half : 1;
484 } else if (minimum > half) {
485 minimum = half;
486 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700487 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
488 if (!mIsOut || (!mDeferWake && mAvailToClient + stepCount >= minimum)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800489 ALOGV("mAvailToClient=%u stepCount=%u minimum=%u", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700490 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
491 if (!(old & CBLK_FUTEX_WAKE)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 (void) __futex_syscall3(&cblk->mFutex,
493 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
494 }
495 }
496
497 buffer->mFrameCount = 0;
498 buffer->mRaw = NULL;
499 buffer->mNonContig = 0;
500}
501
502// ---------------------------------------------------------------------------
503
504size_t AudioTrackServerProxy::framesReady()
505{
506 LOG_ALWAYS_FATAL_IF(!mIsOut);
507
508 if (mIsShutdown) {
509 return 0;
510 }
511 audio_track_cblk_t* cblk = mCblk;
512 // the acquire might not be necessary since not doing a subsequent read
513 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
514 ssize_t filled = rear - cblk->u.mStreaming.mFront;
515 // pipe should not already be overfull
516 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
517 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
518 mIsShutdown = true;
519 return 0;
520 }
521 // cache this value for later use by obtainBuffer(), with added barrier
522 // and racy if called by normal mixer thread
523 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
524 return filled;
525}
526
527// ---------------------------------------------------------------------------
528
529StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
530 size_t frameCount, size_t frameSize)
531 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
532 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0),
533 mEnd(frameCount), mFramesReadyIsCalledByMultipleThreads(false)
534{
535 mState.mLoopStart = 0;
536 mState.mLoopEnd = 0;
537 mState.mLoopCount = 0;
538}
539
540void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
541{
542 mFramesReadyIsCalledByMultipleThreads = true;
543}
544
545size_t StaticAudioTrackServerProxy::framesReady()
546{
547 // FIXME
548 // This is racy if called by normal mixer thread,
549 // as we're reading 2 independent variables without a lock.
550 // Can't call mObserver.poll(), as we might be called from wrong thread.
551 // If looping is enabled, should return a higher number (since includes non-contiguous).
552 size_t position = mPosition;
553 if (!mFramesReadyIsCalledByMultipleThreads) {
554 ssize_t positionOrStatus = pollPosition();
555 if (positionOrStatus >= 0) {
556 position = (size_t) positionOrStatus;
557 }
558 }
559 size_t end = mEnd;
560 return position < end ? end - position : 0;
561}
562
563ssize_t StaticAudioTrackServerProxy::pollPosition()
564{
565 size_t position = mPosition;
566 StaticAudioTrackState state;
567 if (mObserver.poll(state)) {
568 bool valid = false;
569 size_t loopStart = state.mLoopStart;
570 size_t loopEnd = state.mLoopEnd;
571 if (state.mLoopCount == 0) {
572 if (loopStart > mFrameCount) {
573 loopStart = mFrameCount;
574 }
575 // ignore loopEnd
576 mPosition = position = loopStart;
577 mEnd = mFrameCount;
578 mState.mLoopCount = 0;
579 valid = true;
580 } else {
581 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
582 loopEnd - loopStart >= MIN_LOOP) {
583 if (!(loopStart <= position && position < loopEnd)) {
584 mPosition = position = loopStart;
585 }
586 mEnd = loopEnd;
587 mState = state;
588 valid = true;
589 }
590 }
591 if (!valid) {
592 ALOGE("%s client pushed an invalid state, shutting down", __func__);
593 mIsShutdown = true;
594 return (ssize_t) NO_INIT;
595 }
596 mCblk->u.mStatic.mBufferPosition = position;
597 }
598 return (ssize_t) position;
599}
600
601status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer)
602{
603 if (mIsShutdown) {
604 buffer->mFrameCount = 0;
605 buffer->mRaw = NULL;
606 buffer->mNonContig = 0;
607 mUnreleased = 0;
608 return NO_INIT;
609 }
610 ssize_t positionOrStatus = pollPosition();
611 if (positionOrStatus < 0) {
612 buffer->mFrameCount = 0;
613 buffer->mRaw = NULL;
614 buffer->mNonContig = 0;
615 mUnreleased = 0;
616 return (status_t) positionOrStatus;
617 }
618 size_t position = (size_t) positionOrStatus;
619 size_t avail;
620 if (position < mEnd) {
621 avail = mEnd - position;
622 size_t wanted = buffer->mFrameCount;
623 if (avail < wanted) {
624 buffer->mFrameCount = avail;
625 } else {
626 avail = wanted;
627 }
628 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
629 } else {
630 avail = 0;
631 buffer->mFrameCount = 0;
632 buffer->mRaw = NULL;
633 }
634 buffer->mNonContig = 0; // FIXME should be > 0 for looping
635 mUnreleased = avail;
636 return NO_ERROR;
637}
638
639void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
640{
641 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700642 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800643 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700644 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800645 buffer->mRaw = NULL;
646 buffer->mNonContig = 0;
647 return;
648 }
649 mUnreleased -= stepCount;
650 audio_track_cblk_t* cblk = mCblk;
651 size_t position = mPosition;
652 size_t newPosition = position + stepCount;
653 int32_t setFlags = 0;
654 if (!(position <= newPosition && newPosition <= mFrameCount)) {
655 ALOGW("%s newPosition %u outside [%u, %u]", __func__, newPosition, position, mFrameCount);
656 newPosition = mFrameCount;
657 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
658 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
659 newPosition = mState.mLoopStart;
660 setFlags = CBLK_LOOP_CYCLE;
661 } else {
662 mEnd = mFrameCount; // this is what allows playback to continue after the loop
663 setFlags = CBLK_LOOP_FINAL;
664 }
665 }
666 if (newPosition == mFrameCount) {
667 setFlags |= CBLK_BUFFER_END;
668 }
669 mPosition = newPosition;
670
671 cblk->server += stepCount;
672 cblk->u.mStatic.mBufferPosition = newPosition;
673 if (setFlags != 0) {
674 (void) android_atomic_or(setFlags, &cblk->flags);
675 // this would be a good place to wake a futex
676 }
677
678 buffer->mFrameCount = 0;
679 buffer->mRaw = NULL;
680 buffer->mNonContig = 0;
681}
682
683// ---------------------------------------------------------------------------
684
Glenn Kastena8190fc2012-12-03 17:06:56 -0800685} // namespace android