blob: 62362dac2f49c7173d4762fdc875f0b17a8a47e6 [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)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800310 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
Elliott Hughesee499292014-05-21 17:55:51 -0700311 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
312 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800313 }
314}
315
316void ClientProxy::interrupt()
317{
318 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700319 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700320 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
321 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800322 }
323}
324
325size_t ClientProxy::getMisalignment()
326{
327 audio_track_cblk_t* cblk = mCblk;
328 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
329 (mFrameCountP2 - 1);
330}
331
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700332size_t ClientProxy::getFramesFilled() {
333 audio_track_cblk_t* cblk = mCblk;
334 int32_t front;
335 int32_t rear;
336
337 if (mIsOut) {
338 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
339 rear = cblk->u.mStreaming.mRear;
340 } else {
341 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
342 front = cblk->u.mStreaming.mFront;
343 }
344 ssize_t filled = rear - front;
345 // pipe should not be overfull
346 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700347 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700348 return 0;
349 }
350 return (size_t)filled;
351}
352
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800353// ---------------------------------------------------------------------------
354
355void AudioTrackClientProxy::flush()
356{
Glenn Kasten20f51b12014-10-30 10:43:19 -0700357 // This works for mFrameCountP2 <= 2^30
358 size_t increment = mFrameCountP2 << 1;
359 size_t mask = increment - 1;
360 audio_track_cblk_t* cblk = mCblk;
361 int32_t newFlush = (cblk->u.mStreaming.mRear & mask) |
362 ((cblk->u.mStreaming.mFlush & ~mask) + increment);
363 android_atomic_release_store(newFlush, &cblk->u.mStreaming.mFlush);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800364}
365
Eric Laurentbfb1b832013-01-07 09:53:42 -0800366bool AudioTrackClientProxy::clearStreamEndDone() {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700367 return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800368}
369
370bool AudioTrackClientProxy::getStreamEndDone() const {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700371 return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800372}
373
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100374status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested)
375{
376 struct timespec total; // total elapsed time spent waiting
377 total.tv_sec = 0;
378 total.tv_nsec = 0;
379 audio_track_cblk_t* cblk = mCblk;
380 status_t status;
381 enum {
382 TIMEOUT_ZERO, // requested == NULL || *requested == 0
383 TIMEOUT_INFINITE, // *requested == infinity
384 TIMEOUT_FINITE, // 0 < *requested < infinity
385 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
386 } timeout;
387 if (requested == NULL) {
388 timeout = TIMEOUT_ZERO;
389 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
390 timeout = TIMEOUT_ZERO;
391 } else if (requested->tv_sec == INT_MAX) {
392 timeout = TIMEOUT_INFINITE;
393 } else {
394 timeout = TIMEOUT_FINITE;
395 }
396 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700397 int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100398 // check for track invalidation by server, or server death detection
399 if (flags & CBLK_INVALID) {
400 ALOGV("Track invalidated");
401 status = DEAD_OBJECT;
402 goto end;
403 }
404 if (flags & CBLK_STREAM_END_DONE) {
405 ALOGV("stream end received");
406 status = NO_ERROR;
407 goto end;
408 }
409 // check for obtainBuffer interrupted by client
410 // check for obtainBuffer interrupted by client
411 if (flags & CBLK_INTERRUPT) {
412 ALOGV("waitStreamEndDone() interrupted by client");
413 status = -EINTR;
414 goto end;
415 }
416 struct timespec remaining;
417 const struct timespec *ts;
418 switch (timeout) {
419 case TIMEOUT_ZERO:
420 status = WOULD_BLOCK;
421 goto end;
422 case TIMEOUT_INFINITE:
423 ts = NULL;
424 break;
425 case TIMEOUT_FINITE:
426 timeout = TIMEOUT_CONTINUE;
427 if (MAX_SEC == 0) {
428 ts = requested;
429 break;
430 }
431 // fall through
432 case TIMEOUT_CONTINUE:
433 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
434 if (requested->tv_sec < total.tv_sec ||
435 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
436 status = TIMED_OUT;
437 goto end;
438 }
439 remaining.tv_sec = requested->tv_sec - total.tv_sec;
440 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
441 remaining.tv_nsec += 1000000000;
442 remaining.tv_sec++;
443 }
444 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
445 remaining.tv_sec = MAX_SEC;
446 remaining.tv_nsec = 0;
447 }
448 ts = &remaining;
449 break;
450 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800451 LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100452 ts = NULL;
453 break;
454 }
455 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
456 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700457 errno = 0;
458 (void) syscall(__NR_futex, &cblk->mFutex,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100459 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Elliott Hughesee499292014-05-21 17:55:51 -0700460 switch (errno) {
461 case 0: // normal wakeup by server, or by binderDied()
462 case EWOULDBLOCK: // benign race condition with server
463 case EINTR: // wait was interrupted by signal or other spurious wakeup
464 case ETIMEDOUT: // time-out expired
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100465 break;
466 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700467 status = errno;
468 ALOGE("%s unexpected error %s", __func__, strerror(status));
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100469 goto end;
470 }
471 }
472 }
473
474end:
475 if (requested == NULL) {
476 requested = &kNonBlocking;
477 }
478 return status;
479}
480
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481// ---------------------------------------------------------------------------
482
483StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
484 size_t frameCount, size_t frameSize)
485 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
486 mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0)
487{
488}
489
490void StaticAudioTrackClientProxy::flush()
491{
Glenn Kastenadad3d72014-02-21 14:51:43 -0800492 LOG_ALWAYS_FATAL("static flush");
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800493}
494
495void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
496{
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800497 // This can only happen on a 64-bit client
498 if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) {
499 // FIXME Should return an error status
500 return;
501 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 StaticAudioTrackState newState;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800503 newState.mLoopStart = (uint32_t) loopStart;
504 newState.mLoopEnd = (uint32_t) loopEnd;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800505 newState.mLoopCount = loopCount;
506 mBufferPosition = loopStart;
507 (void) mMutator.push(newState);
508}
509
510size_t StaticAudioTrackClientProxy::getBufferPosition()
511{
512 size_t bufferPosition;
513 if (mMutator.ack()) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800514 bufferPosition = (size_t) mCblk->u.mStatic.mBufferPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800515 if (bufferPosition > mFrameCount) {
516 bufferPosition = mFrameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800517 }
518 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 bufferPosition = mBufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800520 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 return bufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800522}
523
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800524// ---------------------------------------------------------------------------
525
526ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
527 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700528 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Glenn Kastence8828a2013-09-16 18:07:38 -0700529 mAvailToClient(0), mFlush(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800530{
Glenn Kastena8190fc2012-12-03 17:06:56 -0800531}
532
Glenn Kasten2e422c42013-10-18 13:00:29 -0700533status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800534{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700535 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800536 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700537 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800538 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700539 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540 audio_track_cblk_t* cblk = mCblk;
541 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
542 // or use previous cached value from framesReady(), with added barrier if it omits.
543 int32_t front;
544 int32_t rear;
545 // See notes on barriers at ClientProxy::obtainBuffer()
546 if (mIsOut) {
547 int32_t flush = cblk->u.mStreaming.mFlush;
548 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100549 front = cblk->u.mStreaming.mFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800550 if (flush != mFlush) {
Glenn Kasten050501d2013-07-11 10:35:38 -0700551 // effectively obtain then release whatever is in the buffer
Glenn Kasten20f51b12014-10-30 10:43:19 -0700552 size_t mask = (mFrameCountP2 << 1) - 1;
553 int32_t newFront = (front & ~mask) | (flush & mask);
554 ssize_t filled = rear - newFront;
555 // Rather than shutting down on a corrupt flush, just treat it as a full flush
556 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
557 ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, filled %d=%#x",
558 mFlush, flush, front, rear, mask, newFront, filled, filled);
559 newFront = rear;
560 }
561 mFlush = flush;
562 android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront);
563 // There is no danger from a false positive, so err on the side of caution
564 if (true /*front != newFront*/) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100565 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
566 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700567 (void) syscall(__NR_futex, &cblk->mFutex,
568 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100569 }
570 }
Glenn Kasten20f51b12014-10-30 10:43:19 -0700571 front = newFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800572 }
573 } else {
574 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
575 rear = cblk->u.mStreaming.mRear;
576 }
577 ssize_t filled = rear - front;
578 // pipe should not already be overfull
579 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700580 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800581 mIsShutdown = true;
582 }
583 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700584 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800585 }
586 // don't allow filling pipe beyond the nominal size
587 size_t availToServer;
588 if (mIsOut) {
589 availToServer = filled;
590 mAvailToClient = mFrameCount - filled;
591 } else {
592 availToServer = mFrameCount - filled;
593 mAvailToClient = filled;
594 }
595 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
596 size_t part1;
597 if (mIsOut) {
598 front &= mFrameCountP2 - 1;
599 part1 = mFrameCountP2 - front;
600 } else {
601 rear &= mFrameCountP2 - 1;
602 part1 = mFrameCountP2 - rear;
603 }
604 if (part1 > availToServer) {
605 part1 = availToServer;
606 }
607 size_t ask = buffer->mFrameCount;
608 if (part1 > ask) {
609 part1 = ask;
610 }
611 // is assignment redundant in some cases?
612 buffer->mFrameCount = part1;
613 buffer->mRaw = part1 > 0 ?
614 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
615 buffer->mNonContig = availToServer - part1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700616 // After flush(), allow releaseBuffer() on a previously obtained buffer;
617 // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
618 if (!ackFlush) {
619 mUnreleased = part1;
620 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800621 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700622 }
623no_init:
624 buffer->mFrameCount = 0;
625 buffer->mRaw = NULL;
626 buffer->mNonContig = 0;
627 mUnreleased = 0;
628 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800629}
630
631void ServerProxy::releaseBuffer(Buffer* buffer)
632{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700633 LOG_ALWAYS_FATAL_IF(buffer == NULL);
634 size_t stepCount = buffer->mFrameCount;
635 if (stepCount == 0 || mIsShutdown) {
636 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800637 buffer->mFrameCount = 0;
638 buffer->mRaw = NULL;
639 buffer->mNonContig = 0;
640 return;
641 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700642 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800643 mUnreleased -= stepCount;
644 audio_track_cblk_t* cblk = mCblk;
645 if (mIsOut) {
646 int32_t front = cblk->u.mStreaming.mFront;
647 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
648 } else {
649 int32_t rear = cblk->u.mStreaming.mRear;
650 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
651 }
652
Glenn Kasten844f88c2014-05-09 13:38:09 -0700653 cblk->mServer += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800654
655 size_t half = mFrameCount / 2;
656 if (half == 0) {
657 half = 1;
658 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800659 size_t minimum = (size_t) cblk->mMinimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800660 if (minimum == 0) {
661 minimum = mIsOut ? half : 1;
662 } else if (minimum > half) {
663 minimum = half;
664 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700665 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
Glenn Kastence8828a2013-09-16 18:07:38 -0700666 if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700667 ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700668 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
669 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700670 (void) syscall(__NR_futex, &cblk->mFutex,
671 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800672 }
673 }
674
675 buffer->mFrameCount = 0;
676 buffer->mRaw = NULL;
677 buffer->mNonContig = 0;
678}
679
680// ---------------------------------------------------------------------------
681
682size_t AudioTrackServerProxy::framesReady()
683{
684 LOG_ALWAYS_FATAL_IF(!mIsOut);
685
686 if (mIsShutdown) {
687 return 0;
688 }
689 audio_track_cblk_t* cblk = mCblk;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100690
691 int32_t flush = cblk->u.mStreaming.mFlush;
692 if (flush != mFlush) {
Glenn Kasten20f51b12014-10-30 10:43:19 -0700693 // FIXME should return an accurate value, but over-estimate is better than under-estimate
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100694 return mFrameCount;
695 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800696 // the acquire might not be necessary since not doing a subsequent read
697 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
698 ssize_t filled = rear - cblk->u.mStreaming.mFront;
699 // pipe should not already be overfull
700 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700701 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800702 mIsShutdown = true;
703 return 0;
704 }
705 // cache this value for later use by obtainBuffer(), with added barrier
706 // and racy if called by normal mixer thread
707 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
708 return filled;
709}
710
Eric Laurentbfb1b832013-01-07 09:53:42 -0800711bool AudioTrackServerProxy::setStreamEndDone() {
Glenn Kasten844f88c2014-05-09 13:38:09 -0700712 audio_track_cblk_t* cblk = mCblk;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800713 bool old =
Glenn Kasten844f88c2014-05-09 13:38:09 -0700714 (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800715 if (!old) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700716 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
Elliott Hughesee499292014-05-21 17:55:51 -0700717 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800718 }
719 return old;
720}
721
Glenn Kasten82aaf942013-07-17 16:05:07 -0700722void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
723{
Glenn Kasten844f88c2014-05-09 13:38:09 -0700724 audio_track_cblk_t* cblk = mCblk;
725 cblk->u.mStreaming.mUnderrunFrames += frameCount;
Glenn Kasten82aaf942013-07-17 16:05:07 -0700726
727 // FIXME also wake futex so that underrun is noticed more quickly
Glenn Kasten844f88c2014-05-09 13:38:09 -0700728 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700729}
730
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800731// ---------------------------------------------------------------------------
732
733StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
734 size_t frameCount, size_t frameSize)
735 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
736 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0),
Andy Hungcb2129b2014-11-11 12:17:22 -0800737 mFramesReadySafe(frameCount), mFramesReady(frameCount),
738 mFramesReadyIsCalledByMultipleThreads(false)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800739{
740 mState.mLoopStart = 0;
741 mState.mLoopEnd = 0;
742 mState.mLoopCount = 0;
743}
744
745void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
746{
747 mFramesReadyIsCalledByMultipleThreads = true;
748}
749
750size_t StaticAudioTrackServerProxy::framesReady()
751{
Andy Hungcb2129b2014-11-11 12:17:22 -0800752 // Can't call pollPosition() from multiple threads.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753 if (!mFramesReadyIsCalledByMultipleThreads) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800754 (void) pollPosition();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800755 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800756 return mFramesReadySafe;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800757}
758
759ssize_t StaticAudioTrackServerProxy::pollPosition()
760{
761 size_t position = mPosition;
762 StaticAudioTrackState state;
763 if (mObserver.poll(state)) {
764 bool valid = false;
765 size_t loopStart = state.mLoopStart;
766 size_t loopEnd = state.mLoopEnd;
767 if (state.mLoopCount == 0) {
768 if (loopStart > mFrameCount) {
769 loopStart = mFrameCount;
770 }
771 // ignore loopEnd
772 mPosition = position = loopStart;
Andy Hungcb2129b2014-11-11 12:17:22 -0800773 mFramesReady = mFrameCount - mPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800774 mState.mLoopCount = 0;
775 valid = true;
Andy Hungcb2129b2014-11-11 12:17:22 -0800776 } else if (state.mLoopCount >= -1) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800777 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
778 loopEnd - loopStart >= MIN_LOOP) {
779 if (!(loopStart <= position && position < loopEnd)) {
780 mPosition = position = loopStart;
781 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800782 if (state.mLoopCount == -1) {
783 mFramesReady = INT64_MAX;
784 } else {
785 // mFramesReady is 64 bits to handle the effective number of frames
786 // that the static audio track contains, including loops.
787 // TODO: Later consider fixing overflow, but does not seem needed now
788 // as will not overflow if loopStart and loopEnd are Java "ints".
789 mFramesReady = int64_t(state.mLoopCount) * (loopEnd - loopStart)
790 + mFrameCount - mPosition;
791 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800792 mState = state;
793 valid = true;
794 }
795 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800796 if (!valid || mPosition > mFrameCount) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800797 ALOGE("%s client pushed an invalid state, shutting down", __func__);
798 mIsShutdown = true;
799 return (ssize_t) NO_INIT;
800 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800801 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800802 // This may overflow, but client is not supposed to rely on it
803 mCblk->u.mStatic.mBufferPosition = (uint32_t) position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800804 }
805 return (ssize_t) position;
806}
807
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800808status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800809{
810 if (mIsShutdown) {
811 buffer->mFrameCount = 0;
812 buffer->mRaw = NULL;
813 buffer->mNonContig = 0;
814 mUnreleased = 0;
815 return NO_INIT;
816 }
817 ssize_t positionOrStatus = pollPosition();
818 if (positionOrStatus < 0) {
819 buffer->mFrameCount = 0;
820 buffer->mRaw = NULL;
821 buffer->mNonContig = 0;
822 mUnreleased = 0;
823 return (status_t) positionOrStatus;
824 }
825 size_t position = (size_t) positionOrStatus;
Andy Hungcb2129b2014-11-11 12:17:22 -0800826 size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800827 size_t avail;
Andy Hungcb2129b2014-11-11 12:17:22 -0800828 if (position < end) {
829 avail = end - position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800830 size_t wanted = buffer->mFrameCount;
831 if (avail < wanted) {
832 buffer->mFrameCount = avail;
833 } else {
834 avail = wanted;
835 }
836 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
837 } else {
838 avail = 0;
839 buffer->mFrameCount = 0;
840 buffer->mRaw = NULL;
841 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800842 // As mFramesReady is the total remaining frames in the static audio track,
843 // it is always larger or equal to avail.
844 LOG_ALWAYS_FATAL_IF(mFramesReady < avail);
845 buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800846 mUnreleased = avail;
847 return NO_ERROR;
848}
849
850void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
851{
852 size_t stepCount = buffer->mFrameCount;
Andy Hungcb2129b2014-11-11 12:17:22 -0800853 LOG_ALWAYS_FATAL_IF(!(stepCount <= mFramesReady));
Glenn Kasten7db7df02013-06-25 16:13:23 -0700854 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800855 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700856 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800857 buffer->mRaw = NULL;
858 buffer->mNonContig = 0;
859 return;
860 }
861 mUnreleased -= stepCount;
862 audio_track_cblk_t* cblk = mCblk;
863 size_t position = mPosition;
864 size_t newPosition = position + stepCount;
865 int32_t setFlags = 0;
866 if (!(position <= newPosition && newPosition <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700867 ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800868 newPosition = mFrameCount;
869 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800870 newPosition = mState.mLoopStart;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800871 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800872 setFlags = CBLK_LOOP_CYCLE;
873 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800874 setFlags = CBLK_LOOP_FINAL;
875 }
876 }
877 if (newPosition == mFrameCount) {
878 setFlags |= CBLK_BUFFER_END;
879 }
880 mPosition = newPosition;
Andy Hungcb2129b2014-11-11 12:17:22 -0800881 if (mFramesReady != INT64_MAX) {
882 mFramesReady -= stepCount;
883 }
884 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800885
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700886 cblk->mServer += stepCount;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800887 // This may overflow, but client is not supposed to rely on it
888 cblk->u.mStatic.mBufferPosition = (uint32_t) newPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800889 if (setFlags != 0) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700890 (void) android_atomic_or(setFlags, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800891 // this would be a good place to wake a futex
892 }
893
894 buffer->mFrameCount = 0;
895 buffer->mRaw = NULL;
896 buffer->mNonContig = 0;
897}
898
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800899void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount __unused)
Glenn Kasten82aaf942013-07-17 16:05:07 -0700900{
901 // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
902 // we don't have a location to count underrun frames. The underrun frame counter
903 // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not
904 // possible for static buffer tracks other than at end of buffer, so this is not a loss.
905
906 // FIXME also wake futex so that underrun is noticed more quickly
907 (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
908}
909
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800910// ---------------------------------------------------------------------------
911
Glenn Kastena8190fc2012-12-03 17:06:56 -0800912} // namespace android