blob: ff24475ff207c6da63312fe88e0733bbb34c809c [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>
Elliott Hughesee499292014-05-21 17:55:51 -070022
23#include <linux/futex.h>
24#include <sys/syscall.h>
Glenn Kastena8190fc2012-12-03 17:06:56 -080025
26namespace android {
27
Andy Hungcb2129b2014-11-11 12:17:22 -080028// used to clamp a value to size_t. TODO: move to another file.
29template <typename T>
30size_t clampToSize(T x) {
31 return x > SIZE_MAX ? SIZE_MAX : x < 0 ? 0 : (size_t) x;
32}
33
Glenn Kastena8190fc2012-12-03 17:06:56 -080034audio_track_cblk_t::audio_track_cblk_t()
Glenn Kasten74935e42013-12-19 08:56:45 -080035 : mServer(0), mFutex(0), mMinimum(0),
Glenn Kastenc56f3422014-03-21 17:53:17 -070036 mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0), mFlags(0)
Glenn Kasten9f80dd22012-12-18 15:57:32 -080037{
38 memset(&u, 0, sizeof(u));
39}
40
41// ---------------------------------------------------------------------------
42
43Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
44 bool isOut, bool clientInServer)
45 : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize),
46 mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer),
Glenn Kasten7db7df02013-06-25 16:13:23 -070047 mIsShutdown(false), mUnreleased(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080048{
49}
50
Glenn Kasten9f80dd22012-12-18 15:57:32 -080051// ---------------------------------------------------------------------------
52
53ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
54 size_t frameSize, bool isOut, bool clientInServer)
55 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), mEpoch(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080056{
Glenn Kastena8190fc2012-12-03 17:06:56 -080057}
58
Glenn Kasten9f80dd22012-12-18 15:57:32 -080059const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/};
60const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/};
61
62#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS
63
64// To facilitate quicker recovery from server failure, this value limits the timeout per each futex
65// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit.
66// FIXME May not be compatible with audio tunneling requirements where timeout should be in the
67// order of minutes.
68#define MAX_SEC 5
69
70status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested,
71 struct timespec *elapsed)
Glenn Kastena8190fc2012-12-03 17:06:56 -080072{
Glenn Kasten7db7df02013-06-25 16:13:23 -070073 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080074 struct timespec total; // total elapsed time spent waiting
75 total.tv_sec = 0;
76 total.tv_nsec = 0;
77 bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting
Glenn Kastena8190fc2012-12-03 17:06:56 -080078
Glenn Kasten9f80dd22012-12-18 15:57:32 -080079 status_t status;
80 enum {
81 TIMEOUT_ZERO, // requested == NULL || *requested == 0
82 TIMEOUT_INFINITE, // *requested == infinity
83 TIMEOUT_FINITE, // 0 < *requested < infinity
84 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
85 } timeout;
86 if (requested == NULL) {
87 timeout = TIMEOUT_ZERO;
88 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
89 timeout = TIMEOUT_ZERO;
90 } else if (requested->tv_sec == INT_MAX) {
91 timeout = TIMEOUT_INFINITE;
Glenn Kastena8190fc2012-12-03 17:06:56 -080092 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080093 timeout = TIMEOUT_FINITE;
94 if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) {
95 measure = true;
96 }
Glenn Kastena8190fc2012-12-03 17:06:56 -080097 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -080098 struct timespec before;
99 bool beforeIsValid = false;
100 audio_track_cblk_t* cblk = mCblk;
101 bool ignoreInitialPendingInterrupt = true;
102 // check for shared memory corruption
103 if (mIsShutdown) {
104 status = NO_INIT;
105 goto end;
106 }
107 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700108 int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800109 // check for track invalidation by server, or server death detection
110 if (flags & CBLK_INVALID) {
111 ALOGV("Track invalidated");
112 status = DEAD_OBJECT;
113 goto end;
114 }
115 // check for obtainBuffer interrupted by client
116 if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) {
117 ALOGV("obtainBuffer() interrupted by client");
118 status = -EINTR;
119 goto end;
120 }
121 ignoreInitialPendingInterrupt = false;
122 // compute number of frames available to write (AudioTrack) or read (AudioRecord)
123 int32_t front;
124 int32_t rear;
125 if (mIsOut) {
126 // The barrier following the read of mFront is probably redundant.
127 // We're about to perform a conditional branch based on 'filled',
128 // which will force the processor to observe the read of mFront
129 // prior to allowing data writes starting at mRaw.
130 // However, the processor may support speculative execution,
131 // and be unable to undo speculative writes into shared memory.
132 // The barrier will prevent such speculative execution.
133 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
134 rear = cblk->u.mStreaming.mRear;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800135 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800136 // On the other hand, this barrier is required.
137 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
138 front = cblk->u.mStreaming.mFront;
139 }
140 ssize_t filled = rear - front;
141 // pipe should not be overfull
142 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700143 if (mIsOut) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700144 ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); "
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700145 "shutting down", filled, mFrameCount);
146 mIsShutdown = true;
147 status = NO_INIT;
148 goto end;
149 }
150 // for input, sync up on overrun
151 filled = 0;
152 cblk->u.mStreaming.mFront = rear;
153 (void) android_atomic_or(CBLK_OVERRUN, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800154 }
155 // don't allow filling pipe beyond the nominal size
156 size_t avail = mIsOut ? mFrameCount - filled : filled;
157 if (avail > 0) {
158 // 'avail' may be non-contiguous, so return only the first contiguous chunk
159 size_t part1;
160 if (mIsOut) {
161 rear &= mFrameCountP2 - 1;
162 part1 = mFrameCountP2 - rear;
163 } else {
164 front &= mFrameCountP2 - 1;
165 part1 = mFrameCountP2 - front;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800166 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800167 if (part1 > avail) {
168 part1 = avail;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800169 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800170 if (part1 > buffer->mFrameCount) {
171 part1 = buffer->mFrameCount;
172 }
173 buffer->mFrameCount = part1;
174 buffer->mRaw = part1 > 0 ?
175 &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL;
176 buffer->mNonContig = avail - part1;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700177 mUnreleased = part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800178 status = NO_ERROR;
179 break;
180 }
181 struct timespec remaining;
182 const struct timespec *ts;
183 switch (timeout) {
184 case TIMEOUT_ZERO:
185 status = WOULD_BLOCK;
186 goto end;
187 case TIMEOUT_INFINITE:
188 ts = NULL;
189 break;
190 case TIMEOUT_FINITE:
191 timeout = TIMEOUT_CONTINUE;
192 if (MAX_SEC == 0) {
193 ts = requested;
194 break;
195 }
196 // fall through
197 case TIMEOUT_CONTINUE:
198 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
199 if (!measure || requested->tv_sec < total.tv_sec ||
200 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
201 status = TIMED_OUT;
202 goto end;
203 }
204 remaining.tv_sec = requested->tv_sec - total.tv_sec;
205 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
206 remaining.tv_nsec += 1000000000;
207 remaining.tv_sec++;
208 }
209 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
210 remaining.tv_sec = MAX_SEC;
211 remaining.tv_nsec = 0;
212 }
213 ts = &remaining;
214 break;
215 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800216 LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800217 ts = NULL;
218 break;
219 }
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700220 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
221 if (!(old & CBLK_FUTEX_WAKE)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800222 if (measure && !beforeIsValid) {
223 clock_gettime(CLOCK_MONOTONIC, &before);
224 beforeIsValid = true;
225 }
Elliott Hughesee499292014-05-21 17:55:51 -0700226 errno = 0;
227 (void) syscall(__NR_futex, &cblk->mFutex,
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700228 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800229 // update total elapsed time spent waiting
230 if (measure) {
231 struct timespec after;
232 clock_gettime(CLOCK_MONOTONIC, &after);
233 total.tv_sec += after.tv_sec - before.tv_sec;
234 long deltaNs = after.tv_nsec - before.tv_nsec;
235 if (deltaNs < 0) {
236 deltaNs += 1000000000;
237 total.tv_sec--;
238 }
239 if ((total.tv_nsec += deltaNs) >= 1000000000) {
240 total.tv_nsec -= 1000000000;
241 total.tv_sec++;
242 }
243 before = after;
244 beforeIsValid = true;
245 }
Elliott Hughesee499292014-05-21 17:55:51 -0700246 switch (errno) {
247 case 0: // normal wakeup by server, or by binderDied()
248 case EWOULDBLOCK: // benign race condition with server
249 case EINTR: // wait was interrupted by signal or other spurious wakeup
250 case ETIMEDOUT: // time-out expired
Glenn Kasten7db7df02013-06-25 16:13:23 -0700251 // FIXME these error/non-0 status are being dropped
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800252 break;
253 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700254 status = errno;
255 ALOGE("%s unexpected error %s", __func__, strerror(status));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800256 goto end;
257 }
258 }
259 }
260
261end:
262 if (status != NO_ERROR) {
263 buffer->mFrameCount = 0;
264 buffer->mRaw = NULL;
265 buffer->mNonContig = 0;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700266 mUnreleased = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800267 }
268 if (elapsed != NULL) {
269 *elapsed = total;
270 }
271 if (requested == NULL) {
272 requested = &kNonBlocking;
273 }
274 if (measure) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100275 ALOGV("requested %ld.%03ld elapsed %ld.%03ld",
276 requested->tv_sec, requested->tv_nsec / 1000000,
277 total.tv_sec, total.tv_nsec / 1000000);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800278 }
279 return status;
280}
281
282void ClientProxy::releaseBuffer(Buffer* buffer)
283{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700284 LOG_ALWAYS_FATAL_IF(buffer == NULL);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800285 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700286 if (stepCount == 0 || mIsShutdown) {
287 // prevent accidental re-use of buffer
288 buffer->mFrameCount = 0;
289 buffer->mRaw = NULL;
290 buffer->mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800291 return;
292 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700293 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
294 mUnreleased -= stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800295 audio_track_cblk_t* cblk = mCblk;
296 // Both of these barriers are required
297 if (mIsOut) {
298 int32_t rear = cblk->u.mStreaming.mRear;
299 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
300 } else {
301 int32_t front = cblk->u.mStreaming.mFront;
302 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
303 }
304}
305
306void ClientProxy::binderDied()
307{
308 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700309 if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) {
zunkyu.lee82a69ea2014-11-07 15:47:32 +0900310 android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800311 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
Elliott Hughesee499292014-05-21 17:55:51 -0700312 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
313 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800314 }
315}
316
317void ClientProxy::interrupt()
318{
319 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700320 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) {
zunkyu.lee82a69ea2014-11-07 15:47:32 +0900321 android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
Elliott Hughesee499292014-05-21 17:55:51 -0700322 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
323 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800324 }
325}
326
327size_t ClientProxy::getMisalignment()
328{
329 audio_track_cblk_t* cblk = mCblk;
330 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
331 (mFrameCountP2 - 1);
332}
333
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700334size_t ClientProxy::getFramesFilled() {
335 audio_track_cblk_t* cblk = mCblk;
336 int32_t front;
337 int32_t rear;
338
339 if (mIsOut) {
340 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
341 rear = cblk->u.mStreaming.mRear;
342 } else {
343 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
344 front = cblk->u.mStreaming.mFront;
345 }
346 ssize_t filled = rear - front;
347 // pipe should not be overfull
348 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700349 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700350 return 0;
351 }
352 return (size_t)filled;
353}
354
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800355// ---------------------------------------------------------------------------
356
357void AudioTrackClientProxy::flush()
358{
Glenn Kasten20f51b12014-10-30 10:43:19 -0700359 // This works for mFrameCountP2 <= 2^30
360 size_t increment = mFrameCountP2 << 1;
361 size_t mask = increment - 1;
362 audio_track_cblk_t* cblk = mCblk;
363 int32_t newFlush = (cblk->u.mStreaming.mRear & mask) |
364 ((cblk->u.mStreaming.mFlush & ~mask) + increment);
365 android_atomic_release_store(newFlush, &cblk->u.mStreaming.mFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800366}
367
Eric Laurentbfb1b832013-01-07 09:53:42 -0800368bool AudioTrackClientProxy::clearStreamEndDone() {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700369 return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800370}
371
372bool AudioTrackClientProxy::getStreamEndDone() const {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700373 return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800374}
375
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100376status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested)
377{
378 struct timespec total; // total elapsed time spent waiting
379 total.tv_sec = 0;
380 total.tv_nsec = 0;
381 audio_track_cblk_t* cblk = mCblk;
382 status_t status;
383 enum {
384 TIMEOUT_ZERO, // requested == NULL || *requested == 0
385 TIMEOUT_INFINITE, // *requested == infinity
386 TIMEOUT_FINITE, // 0 < *requested < infinity
387 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
388 } timeout;
389 if (requested == NULL) {
390 timeout = TIMEOUT_ZERO;
391 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
392 timeout = TIMEOUT_ZERO;
393 } else if (requested->tv_sec == INT_MAX) {
394 timeout = TIMEOUT_INFINITE;
395 } else {
396 timeout = TIMEOUT_FINITE;
397 }
398 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700399 int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100400 // check for track invalidation by server, or server death detection
401 if (flags & CBLK_INVALID) {
402 ALOGV("Track invalidated");
403 status = DEAD_OBJECT;
404 goto end;
405 }
406 if (flags & CBLK_STREAM_END_DONE) {
407 ALOGV("stream end received");
408 status = NO_ERROR;
409 goto end;
410 }
411 // check for obtainBuffer interrupted by client
412 // check for obtainBuffer interrupted by client
413 if (flags & CBLK_INTERRUPT) {
414 ALOGV("waitStreamEndDone() interrupted by client");
415 status = -EINTR;
416 goto end;
417 }
418 struct timespec remaining;
419 const struct timespec *ts;
420 switch (timeout) {
421 case TIMEOUT_ZERO:
422 status = WOULD_BLOCK;
423 goto end;
424 case TIMEOUT_INFINITE:
425 ts = NULL;
426 break;
427 case TIMEOUT_FINITE:
428 timeout = TIMEOUT_CONTINUE;
429 if (MAX_SEC == 0) {
430 ts = requested;
431 break;
432 }
433 // fall through
434 case TIMEOUT_CONTINUE:
435 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
436 if (requested->tv_sec < total.tv_sec ||
437 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
438 status = TIMED_OUT;
439 goto end;
440 }
441 remaining.tv_sec = requested->tv_sec - total.tv_sec;
442 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
443 remaining.tv_nsec += 1000000000;
444 remaining.tv_sec++;
445 }
446 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
447 remaining.tv_sec = MAX_SEC;
448 remaining.tv_nsec = 0;
449 }
450 ts = &remaining;
451 break;
452 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800453 LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100454 ts = NULL;
455 break;
456 }
457 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
458 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700459 errno = 0;
460 (void) syscall(__NR_futex, &cblk->mFutex,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100461 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Elliott Hughesee499292014-05-21 17:55:51 -0700462 switch (errno) {
463 case 0: // normal wakeup by server, or by binderDied()
464 case EWOULDBLOCK: // benign race condition with server
465 case EINTR: // wait was interrupted by signal or other spurious wakeup
466 case ETIMEDOUT: // time-out expired
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100467 break;
468 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700469 status = errno;
470 ALOGE("%s unexpected error %s", __func__, strerror(status));
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100471 goto end;
472 }
473 }
474 }
475
476end:
477 if (requested == NULL) {
478 requested = &kNonBlocking;
479 }
480 return status;
481}
482
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800483// ---------------------------------------------------------------------------
484
485StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
486 size_t frameCount, size_t frameSize)
487 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
488 mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0)
489{
490}
491
492void StaticAudioTrackClientProxy::flush()
493{
Glenn Kastenadad3d72014-02-21 14:51:43 -0800494 LOG_ALWAYS_FATAL("static flush");
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495}
496
497void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
498{
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800499 // This can only happen on a 64-bit client
500 if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) {
501 // FIXME Should return an error status
502 return;
503 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800504 StaticAudioTrackState newState;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800505 newState.mLoopStart = (uint32_t) loopStart;
506 newState.mLoopEnd = (uint32_t) loopEnd;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 newState.mLoopCount = loopCount;
Andy Hung680b7952014-11-12 13:18:52 -0800508 size_t bufferPosition;
509 if (loopCount == 0 || (bufferPosition = getBufferPosition()) >= loopEnd) {
510 bufferPosition = loopStart;
511 }
512 mBufferPosition = bufferPosition; // snapshot buffer position until loop is acknowledged.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800513 (void) mMutator.push(newState);
514}
515
516size_t StaticAudioTrackClientProxy::getBufferPosition()
517{
518 size_t bufferPosition;
519 if (mMutator.ack()) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800520 bufferPosition = (size_t) mCblk->u.mStatic.mBufferPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 if (bufferPosition > mFrameCount) {
522 bufferPosition = mFrameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800523 }
524 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800525 bufferPosition = mBufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800526 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 return bufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800528}
529
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800530// ---------------------------------------------------------------------------
531
532ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
533 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700534 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Glenn Kastence8828a2013-09-16 18:07:38 -0700535 mAvailToClient(0), mFlush(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800536{
Glenn Kastena8190fc2012-12-03 17:06:56 -0800537}
538
Glenn Kasten2e422c42013-10-18 13:00:29 -0700539status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700541 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800542 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700543 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800544 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700545 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800546 audio_track_cblk_t* cblk = mCblk;
547 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
548 // or use previous cached value from framesReady(), with added barrier if it omits.
549 int32_t front;
550 int32_t rear;
551 // See notes on barriers at ClientProxy::obtainBuffer()
552 if (mIsOut) {
553 int32_t flush = cblk->u.mStreaming.mFlush;
554 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100555 front = cblk->u.mStreaming.mFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800556 if (flush != mFlush) {
Glenn Kasten050501d2013-07-11 10:35:38 -0700557 // effectively obtain then release whatever is in the buffer
Glenn Kasten20f51b12014-10-30 10:43:19 -0700558 size_t mask = (mFrameCountP2 << 1) - 1;
559 int32_t newFront = (front & ~mask) | (flush & mask);
560 ssize_t filled = rear - newFront;
561 // Rather than shutting down on a corrupt flush, just treat it as a full flush
562 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
563 ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, filled %d=%#x",
564 mFlush, flush, front, rear, mask, newFront, filled, filled);
565 newFront = rear;
566 }
567 mFlush = flush;
568 android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront);
569 // There is no danger from a false positive, so err on the side of caution
570 if (true /*front != newFront*/) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100571 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
572 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700573 (void) syscall(__NR_futex, &cblk->mFutex,
574 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100575 }
576 }
Glenn Kasten20f51b12014-10-30 10:43:19 -0700577 front = newFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800578 }
579 } else {
580 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
581 rear = cblk->u.mStreaming.mRear;
582 }
583 ssize_t filled = rear - front;
584 // pipe should not already be overfull
585 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700586 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800587 mIsShutdown = true;
588 }
589 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700590 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800591 }
592 // don't allow filling pipe beyond the nominal size
593 size_t availToServer;
594 if (mIsOut) {
595 availToServer = filled;
596 mAvailToClient = mFrameCount - filled;
597 } else {
598 availToServer = mFrameCount - filled;
599 mAvailToClient = filled;
600 }
601 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
602 size_t part1;
603 if (mIsOut) {
604 front &= mFrameCountP2 - 1;
605 part1 = mFrameCountP2 - front;
606 } else {
607 rear &= mFrameCountP2 - 1;
608 part1 = mFrameCountP2 - rear;
609 }
610 if (part1 > availToServer) {
611 part1 = availToServer;
612 }
613 size_t ask = buffer->mFrameCount;
614 if (part1 > ask) {
615 part1 = ask;
616 }
617 // is assignment redundant in some cases?
618 buffer->mFrameCount = part1;
619 buffer->mRaw = part1 > 0 ?
620 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
621 buffer->mNonContig = availToServer - part1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700622 // After flush(), allow releaseBuffer() on a previously obtained buffer;
623 // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
624 if (!ackFlush) {
625 mUnreleased = part1;
626 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800627 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700628 }
629no_init:
630 buffer->mFrameCount = 0;
631 buffer->mRaw = NULL;
632 buffer->mNonContig = 0;
633 mUnreleased = 0;
634 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800635}
636
637void ServerProxy::releaseBuffer(Buffer* buffer)
638{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700639 LOG_ALWAYS_FATAL_IF(buffer == NULL);
640 size_t stepCount = buffer->mFrameCount;
641 if (stepCount == 0 || mIsShutdown) {
642 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800643 buffer->mFrameCount = 0;
644 buffer->mRaw = NULL;
645 buffer->mNonContig = 0;
646 return;
647 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700648 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800649 mUnreleased -= stepCount;
650 audio_track_cblk_t* cblk = mCblk;
651 if (mIsOut) {
652 int32_t front = cblk->u.mStreaming.mFront;
653 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
654 } else {
655 int32_t rear = cblk->u.mStreaming.mRear;
656 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
657 }
658
Glenn Kasten844f88c2014-05-09 13:38:09 -0700659 cblk->mServer += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800660
661 size_t half = mFrameCount / 2;
662 if (half == 0) {
663 half = 1;
664 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800665 size_t minimum = (size_t) cblk->mMinimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800666 if (minimum == 0) {
667 minimum = mIsOut ? half : 1;
668 } else if (minimum > half) {
669 minimum = half;
670 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700671 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
Glenn Kastence8828a2013-09-16 18:07:38 -0700672 if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700673 ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700674 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
675 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700676 (void) syscall(__NR_futex, &cblk->mFutex,
677 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800678 }
679 }
680
681 buffer->mFrameCount = 0;
682 buffer->mRaw = NULL;
683 buffer->mNonContig = 0;
684}
685
686// ---------------------------------------------------------------------------
687
688size_t AudioTrackServerProxy::framesReady()
689{
690 LOG_ALWAYS_FATAL_IF(!mIsOut);
691
692 if (mIsShutdown) {
693 return 0;
694 }
695 audio_track_cblk_t* cblk = mCblk;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100696
697 int32_t flush = cblk->u.mStreaming.mFlush;
698 if (flush != mFlush) {
Glenn Kasten20f51b12014-10-30 10:43:19 -0700699 // FIXME should return an accurate value, but over-estimate is better than under-estimate
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100700 return mFrameCount;
701 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800702 // the acquire might not be necessary since not doing a subsequent read
703 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
704 ssize_t filled = rear - cblk->u.mStreaming.mFront;
705 // pipe should not already be overfull
706 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700707 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800708 mIsShutdown = true;
709 return 0;
710 }
711 // cache this value for later use by obtainBuffer(), with added barrier
712 // and racy if called by normal mixer thread
713 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
714 return filled;
715}
716
Eric Laurentbfb1b832013-01-07 09:53:42 -0800717bool AudioTrackServerProxy::setStreamEndDone() {
Glenn Kasten844f88c2014-05-09 13:38:09 -0700718 audio_track_cblk_t* cblk = mCblk;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800719 bool old =
Glenn Kasten844f88c2014-05-09 13:38:09 -0700720 (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800721 if (!old) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700722 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
Elliott Hughesee499292014-05-21 17:55:51 -0700723 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800724 }
725 return old;
726}
727
Glenn Kasten82aaf942013-07-17 16:05:07 -0700728void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
729{
Glenn Kasten844f88c2014-05-09 13:38:09 -0700730 audio_track_cblk_t* cblk = mCblk;
731 cblk->u.mStreaming.mUnderrunFrames += frameCount;
Glenn Kasten82aaf942013-07-17 16:05:07 -0700732
733 // FIXME also wake futex so that underrun is noticed more quickly
Glenn Kasten844f88c2014-05-09 13:38:09 -0700734 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700735}
736
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800737// ---------------------------------------------------------------------------
738
739StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
740 size_t frameCount, size_t frameSize)
741 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
742 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0),
Andy Hungcb2129b2014-11-11 12:17:22 -0800743 mFramesReadySafe(frameCount), mFramesReady(frameCount),
744 mFramesReadyIsCalledByMultipleThreads(false)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745{
746 mState.mLoopStart = 0;
747 mState.mLoopEnd = 0;
748 mState.mLoopCount = 0;
749}
750
751void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
752{
753 mFramesReadyIsCalledByMultipleThreads = true;
754}
755
756size_t StaticAudioTrackServerProxy::framesReady()
757{
Andy Hungcb2129b2014-11-11 12:17:22 -0800758 // Can't call pollPosition() from multiple threads.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759 if (!mFramesReadyIsCalledByMultipleThreads) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800760 (void) pollPosition();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800761 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800762 return mFramesReadySafe;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800763}
764
765ssize_t StaticAudioTrackServerProxy::pollPosition()
766{
767 size_t position = mPosition;
768 StaticAudioTrackState state;
769 if (mObserver.poll(state)) {
770 bool valid = false;
771 size_t loopStart = state.mLoopStart;
772 size_t loopEnd = state.mLoopEnd;
773 if (state.mLoopCount == 0) {
774 if (loopStart > mFrameCount) {
775 loopStart = mFrameCount;
776 }
777 // ignore loopEnd
778 mPosition = position = loopStart;
Andy Hungcb2129b2014-11-11 12:17:22 -0800779 mFramesReady = mFrameCount - mPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800780 mState.mLoopCount = 0;
781 valid = true;
Andy Hungcb2129b2014-11-11 12:17:22 -0800782 } else if (state.mLoopCount >= -1) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800783 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
784 loopEnd - loopStart >= MIN_LOOP) {
Andy Hung680b7952014-11-12 13:18:52 -0800785 // If the current position is greater than the end of the loop
786 // we "wrap" to the loop start. This might cause an audible pop.
787 if (position >= loopEnd) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800788 mPosition = position = loopStart;
789 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800790 if (state.mLoopCount == -1) {
791 mFramesReady = INT64_MAX;
792 } else {
793 // mFramesReady is 64 bits to handle the effective number of frames
794 // that the static audio track contains, including loops.
795 // TODO: Later consider fixing overflow, but does not seem needed now
796 // as will not overflow if loopStart and loopEnd are Java "ints".
797 mFramesReady = int64_t(state.mLoopCount) * (loopEnd - loopStart)
798 + mFrameCount - mPosition;
799 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800800 mState = state;
801 valid = true;
802 }
803 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800804 if (!valid || mPosition > mFrameCount) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800805 ALOGE("%s client pushed an invalid state, shutting down", __func__);
806 mIsShutdown = true;
807 return (ssize_t) NO_INIT;
808 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800809 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800810 // This may overflow, but client is not supposed to rely on it
811 mCblk->u.mStatic.mBufferPosition = (uint32_t) position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800812 }
813 return (ssize_t) position;
814}
815
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800816status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800817{
818 if (mIsShutdown) {
819 buffer->mFrameCount = 0;
820 buffer->mRaw = NULL;
821 buffer->mNonContig = 0;
822 mUnreleased = 0;
823 return NO_INIT;
824 }
825 ssize_t positionOrStatus = pollPosition();
826 if (positionOrStatus < 0) {
827 buffer->mFrameCount = 0;
828 buffer->mRaw = NULL;
829 buffer->mNonContig = 0;
830 mUnreleased = 0;
831 return (status_t) positionOrStatus;
832 }
833 size_t position = (size_t) positionOrStatus;
Andy Hungcb2129b2014-11-11 12:17:22 -0800834 size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800835 size_t avail;
Andy Hungcb2129b2014-11-11 12:17:22 -0800836 if (position < end) {
837 avail = end - position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800838 size_t wanted = buffer->mFrameCount;
839 if (avail < wanted) {
840 buffer->mFrameCount = avail;
841 } else {
842 avail = wanted;
843 }
844 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
845 } else {
846 avail = 0;
847 buffer->mFrameCount = 0;
848 buffer->mRaw = NULL;
849 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800850 // As mFramesReady is the total remaining frames in the static audio track,
851 // it is always larger or equal to avail.
852 LOG_ALWAYS_FATAL_IF(mFramesReady < avail);
853 buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800854 mUnreleased = avail;
855 return NO_ERROR;
856}
857
858void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
859{
860 size_t stepCount = buffer->mFrameCount;
Andy Hungcb2129b2014-11-11 12:17:22 -0800861 LOG_ALWAYS_FATAL_IF(!(stepCount <= mFramesReady));
Glenn Kasten7db7df02013-06-25 16:13:23 -0700862 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800863 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700864 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800865 buffer->mRaw = NULL;
866 buffer->mNonContig = 0;
867 return;
868 }
869 mUnreleased -= stepCount;
870 audio_track_cblk_t* cblk = mCblk;
871 size_t position = mPosition;
872 size_t newPosition = position + stepCount;
873 int32_t setFlags = 0;
874 if (!(position <= newPosition && newPosition <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700875 ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800876 newPosition = mFrameCount;
877 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800878 newPosition = mState.mLoopStart;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800879 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800880 setFlags = CBLK_LOOP_CYCLE;
881 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800882 setFlags = CBLK_LOOP_FINAL;
883 }
884 }
885 if (newPosition == mFrameCount) {
886 setFlags |= CBLK_BUFFER_END;
887 }
888 mPosition = newPosition;
Andy Hungcb2129b2014-11-11 12:17:22 -0800889 if (mFramesReady != INT64_MAX) {
890 mFramesReady -= stepCount;
891 }
892 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800893
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700894 cblk->mServer += stepCount;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800895 // This may overflow, but client is not supposed to rely on it
896 cblk->u.mStatic.mBufferPosition = (uint32_t) newPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800897 if (setFlags != 0) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700898 (void) android_atomic_or(setFlags, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800899 // this would be a good place to wake a futex
900 }
901
902 buffer->mFrameCount = 0;
903 buffer->mRaw = NULL;
904 buffer->mNonContig = 0;
905}
906
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800907void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount __unused)
Glenn Kasten82aaf942013-07-17 16:05:07 -0700908{
909 // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
910 // we don't have a location to count underrun frames. The underrun frame counter
911 // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not
912 // possible for static buffer tracks other than at end of buffer, so this is not a loss.
913
914 // FIXME also wake futex so that underrun is noticed more quickly
915 (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
916}
917
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800918// ---------------------------------------------------------------------------
919
Glenn Kastena8190fc2012-12-03 17:06:56 -0800920} // namespace android