blob: 4a783b39bb14e158f74ce22f912b706a2e4983d1 [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;
Andy Hung680b7952014-11-12 13:18:52 -0800506 size_t bufferPosition;
507 if (loopCount == 0 || (bufferPosition = getBufferPosition()) >= loopEnd) {
508 bufferPosition = loopStart;
509 }
510 mBufferPosition = bufferPosition; // snapshot buffer position until loop is acknowledged.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511 (void) mMutator.push(newState);
512}
513
514size_t StaticAudioTrackClientProxy::getBufferPosition()
515{
516 size_t bufferPosition;
517 if (mMutator.ack()) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800518 bufferPosition = (size_t) mCblk->u.mStatic.mBufferPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 if (bufferPosition > mFrameCount) {
520 bufferPosition = mFrameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800521 }
522 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800523 bufferPosition = mBufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800524 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800525 return bufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800526}
527
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800528// ---------------------------------------------------------------------------
529
530ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
531 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700532 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Glenn Kastence8828a2013-09-16 18:07:38 -0700533 mAvailToClient(0), mFlush(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800534{
Glenn Kastena8190fc2012-12-03 17:06:56 -0800535}
536
Glenn Kasten2e422c42013-10-18 13:00:29 -0700537status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800538{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700539 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700541 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800542 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700543 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800544 audio_track_cblk_t* cblk = mCblk;
545 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
546 // or use previous cached value from framesReady(), with added barrier if it omits.
547 int32_t front;
548 int32_t rear;
549 // See notes on barriers at ClientProxy::obtainBuffer()
550 if (mIsOut) {
551 int32_t flush = cblk->u.mStreaming.mFlush;
552 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100553 front = cblk->u.mStreaming.mFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800554 if (flush != mFlush) {
Glenn Kasten050501d2013-07-11 10:35:38 -0700555 // effectively obtain then release whatever is in the buffer
Glenn Kasten20f51b12014-10-30 10:43:19 -0700556 size_t mask = (mFrameCountP2 << 1) - 1;
557 int32_t newFront = (front & ~mask) | (flush & mask);
558 ssize_t filled = rear - newFront;
559 // Rather than shutting down on a corrupt flush, just treat it as a full flush
560 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
561 ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, filled %d=%#x",
562 mFlush, flush, front, rear, mask, newFront, filled, filled);
563 newFront = rear;
564 }
565 mFlush = flush;
566 android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront);
567 // There is no danger from a false positive, so err on the side of caution
568 if (true /*front != newFront*/) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100569 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
570 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700571 (void) syscall(__NR_futex, &cblk->mFutex,
572 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100573 }
574 }
Glenn Kasten20f51b12014-10-30 10:43:19 -0700575 front = newFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800576 }
577 } else {
578 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
579 rear = cblk->u.mStreaming.mRear;
580 }
581 ssize_t filled = rear - front;
582 // pipe should not already be overfull
583 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700584 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800585 mIsShutdown = true;
586 }
587 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700588 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800589 }
590 // don't allow filling pipe beyond the nominal size
591 size_t availToServer;
592 if (mIsOut) {
593 availToServer = filled;
594 mAvailToClient = mFrameCount - filled;
595 } else {
596 availToServer = mFrameCount - filled;
597 mAvailToClient = filled;
598 }
599 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
600 size_t part1;
601 if (mIsOut) {
602 front &= mFrameCountP2 - 1;
603 part1 = mFrameCountP2 - front;
604 } else {
605 rear &= mFrameCountP2 - 1;
606 part1 = mFrameCountP2 - rear;
607 }
608 if (part1 > availToServer) {
609 part1 = availToServer;
610 }
611 size_t ask = buffer->mFrameCount;
612 if (part1 > ask) {
613 part1 = ask;
614 }
615 // is assignment redundant in some cases?
616 buffer->mFrameCount = part1;
617 buffer->mRaw = part1 > 0 ?
618 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
619 buffer->mNonContig = availToServer - part1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700620 // After flush(), allow releaseBuffer() on a previously obtained buffer;
621 // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
622 if (!ackFlush) {
623 mUnreleased = part1;
624 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800625 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700626 }
627no_init:
628 buffer->mFrameCount = 0;
629 buffer->mRaw = NULL;
630 buffer->mNonContig = 0;
631 mUnreleased = 0;
632 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800633}
634
635void ServerProxy::releaseBuffer(Buffer* buffer)
636{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700637 LOG_ALWAYS_FATAL_IF(buffer == NULL);
638 size_t stepCount = buffer->mFrameCount;
639 if (stepCount == 0 || mIsShutdown) {
640 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800641 buffer->mFrameCount = 0;
642 buffer->mRaw = NULL;
643 buffer->mNonContig = 0;
644 return;
645 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700646 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800647 mUnreleased -= stepCount;
648 audio_track_cblk_t* cblk = mCblk;
649 if (mIsOut) {
650 int32_t front = cblk->u.mStreaming.mFront;
651 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
652 } else {
653 int32_t rear = cblk->u.mStreaming.mRear;
654 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
655 }
656
Glenn Kasten844f88c2014-05-09 13:38:09 -0700657 cblk->mServer += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800658
659 size_t half = mFrameCount / 2;
660 if (half == 0) {
661 half = 1;
662 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800663 size_t minimum = (size_t) cblk->mMinimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664 if (minimum == 0) {
665 minimum = mIsOut ? half : 1;
666 } else if (minimum > half) {
667 minimum = half;
668 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700669 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
Glenn Kastence8828a2013-09-16 18:07:38 -0700670 if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700671 ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700672 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
673 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700674 (void) syscall(__NR_futex, &cblk->mFutex,
675 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800676 }
677 }
678
679 buffer->mFrameCount = 0;
680 buffer->mRaw = NULL;
681 buffer->mNonContig = 0;
682}
683
684// ---------------------------------------------------------------------------
685
686size_t AudioTrackServerProxy::framesReady()
687{
688 LOG_ALWAYS_FATAL_IF(!mIsOut);
689
690 if (mIsShutdown) {
691 return 0;
692 }
693 audio_track_cblk_t* cblk = mCblk;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100694
695 int32_t flush = cblk->u.mStreaming.mFlush;
696 if (flush != mFlush) {
Glenn Kasten20f51b12014-10-30 10:43:19 -0700697 // FIXME should return an accurate value, but over-estimate is better than under-estimate
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100698 return mFrameCount;
699 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800700 // the acquire might not be necessary since not doing a subsequent read
701 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
702 ssize_t filled = rear - cblk->u.mStreaming.mFront;
703 // pipe should not already be overfull
704 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700705 ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800706 mIsShutdown = true;
707 return 0;
708 }
709 // cache this value for later use by obtainBuffer(), with added barrier
710 // and racy if called by normal mixer thread
711 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
712 return filled;
713}
714
Eric Laurentbfb1b832013-01-07 09:53:42 -0800715bool AudioTrackServerProxy::setStreamEndDone() {
Glenn Kasten844f88c2014-05-09 13:38:09 -0700716 audio_track_cblk_t* cblk = mCblk;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800717 bool old =
Glenn Kasten844f88c2014-05-09 13:38:09 -0700718 (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800719 if (!old) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700720 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
Elliott Hughesee499292014-05-21 17:55:51 -0700721 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800722 }
723 return old;
724}
725
Glenn Kasten82aaf942013-07-17 16:05:07 -0700726void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
727{
Glenn Kasten844f88c2014-05-09 13:38:09 -0700728 audio_track_cblk_t* cblk = mCblk;
729 cblk->u.mStreaming.mUnderrunFrames += frameCount;
Glenn Kasten82aaf942013-07-17 16:05:07 -0700730
731 // FIXME also wake futex so that underrun is noticed more quickly
Glenn Kasten844f88c2014-05-09 13:38:09 -0700732 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700733}
734
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800735// ---------------------------------------------------------------------------
736
737StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
738 size_t frameCount, size_t frameSize)
739 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
740 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0),
Andy Hungcb2129b2014-11-11 12:17:22 -0800741 mFramesReadySafe(frameCount), mFramesReady(frameCount),
742 mFramesReadyIsCalledByMultipleThreads(false)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800743{
744 mState.mLoopStart = 0;
745 mState.mLoopEnd = 0;
746 mState.mLoopCount = 0;
747}
748
749void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
750{
751 mFramesReadyIsCalledByMultipleThreads = true;
752}
753
754size_t StaticAudioTrackServerProxy::framesReady()
755{
Andy Hungcb2129b2014-11-11 12:17:22 -0800756 // Can't call pollPosition() from multiple threads.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800757 if (!mFramesReadyIsCalledByMultipleThreads) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800758 (void) pollPosition();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800760 return mFramesReadySafe;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800761}
762
763ssize_t StaticAudioTrackServerProxy::pollPosition()
764{
765 size_t position = mPosition;
766 StaticAudioTrackState state;
767 if (mObserver.poll(state)) {
768 bool valid = false;
769 size_t loopStart = state.mLoopStart;
770 size_t loopEnd = state.mLoopEnd;
771 if (state.mLoopCount == 0) {
772 if (loopStart > mFrameCount) {
773 loopStart = mFrameCount;
774 }
775 // ignore loopEnd
776 mPosition = position = loopStart;
Andy Hungcb2129b2014-11-11 12:17:22 -0800777 mFramesReady = mFrameCount - mPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800778 mState.mLoopCount = 0;
779 valid = true;
Andy Hungcb2129b2014-11-11 12:17:22 -0800780 } else if (state.mLoopCount >= -1) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800781 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
782 loopEnd - loopStart >= MIN_LOOP) {
Andy Hung680b7952014-11-12 13:18:52 -0800783 // If the current position is greater than the end of the loop
784 // we "wrap" to the loop start. This might cause an audible pop.
785 if (position >= loopEnd) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800786 mPosition = position = loopStart;
787 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800788 if (state.mLoopCount == -1) {
789 mFramesReady = INT64_MAX;
790 } else {
791 // mFramesReady is 64 bits to handle the effective number of frames
792 // that the static audio track contains, including loops.
793 // TODO: Later consider fixing overflow, but does not seem needed now
794 // as will not overflow if loopStart and loopEnd are Java "ints".
795 mFramesReady = int64_t(state.mLoopCount) * (loopEnd - loopStart)
796 + mFrameCount - mPosition;
797 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800798 mState = state;
799 valid = true;
800 }
801 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800802 if (!valid || mPosition > mFrameCount) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800803 ALOGE("%s client pushed an invalid state, shutting down", __func__);
804 mIsShutdown = true;
805 return (ssize_t) NO_INIT;
806 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800807 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800808 // This may overflow, but client is not supposed to rely on it
809 mCblk->u.mStatic.mBufferPosition = (uint32_t) position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800810 }
811 return (ssize_t) position;
812}
813
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800814status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800815{
816 if (mIsShutdown) {
817 buffer->mFrameCount = 0;
818 buffer->mRaw = NULL;
819 buffer->mNonContig = 0;
820 mUnreleased = 0;
821 return NO_INIT;
822 }
823 ssize_t positionOrStatus = pollPosition();
824 if (positionOrStatus < 0) {
825 buffer->mFrameCount = 0;
826 buffer->mRaw = NULL;
827 buffer->mNonContig = 0;
828 mUnreleased = 0;
829 return (status_t) positionOrStatus;
830 }
831 size_t position = (size_t) positionOrStatus;
Andy Hungcb2129b2014-11-11 12:17:22 -0800832 size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800833 size_t avail;
Andy Hungcb2129b2014-11-11 12:17:22 -0800834 if (position < end) {
835 avail = end - position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800836 size_t wanted = buffer->mFrameCount;
837 if (avail < wanted) {
838 buffer->mFrameCount = avail;
839 } else {
840 avail = wanted;
841 }
842 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
843 } else {
844 avail = 0;
845 buffer->mFrameCount = 0;
846 buffer->mRaw = NULL;
847 }
Andy Hungcb2129b2014-11-11 12:17:22 -0800848 // As mFramesReady is the total remaining frames in the static audio track,
849 // it is always larger or equal to avail.
850 LOG_ALWAYS_FATAL_IF(mFramesReady < avail);
851 buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800852 mUnreleased = avail;
853 return NO_ERROR;
854}
855
856void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
857{
858 size_t stepCount = buffer->mFrameCount;
Andy Hungcb2129b2014-11-11 12:17:22 -0800859 LOG_ALWAYS_FATAL_IF(!(stepCount <= mFramesReady));
Glenn Kasten7db7df02013-06-25 16:13:23 -0700860 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800861 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700862 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800863 buffer->mRaw = NULL;
864 buffer->mNonContig = 0;
865 return;
866 }
867 mUnreleased -= stepCount;
868 audio_track_cblk_t* cblk = mCblk;
869 size_t position = mPosition;
870 size_t newPosition = position + stepCount;
871 int32_t setFlags = 0;
872 if (!(position <= newPosition && newPosition <= mFrameCount)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700873 ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800874 newPosition = mFrameCount;
875 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
Andy Hungcb2129b2014-11-11 12:17:22 -0800876 newPosition = mState.mLoopStart;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800877 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800878 setFlags = CBLK_LOOP_CYCLE;
879 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800880 setFlags = CBLK_LOOP_FINAL;
881 }
882 }
883 if (newPosition == mFrameCount) {
884 setFlags |= CBLK_BUFFER_END;
885 }
886 mPosition = newPosition;
Andy Hungcb2129b2014-11-11 12:17:22 -0800887 if (mFramesReady != INT64_MAX) {
888 mFramesReady -= stepCount;
889 }
890 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800891
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700892 cblk->mServer += stepCount;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800893 // This may overflow, but client is not supposed to rely on it
894 cblk->u.mStatic.mBufferPosition = (uint32_t) newPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800895 if (setFlags != 0) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700896 (void) android_atomic_or(setFlags, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800897 // this would be a good place to wake a futex
898 }
899
900 buffer->mFrameCount = 0;
901 buffer->mRaw = NULL;
902 buffer->mNonContig = 0;
903}
904
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800905void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount __unused)
Glenn Kasten82aaf942013-07-17 16:05:07 -0700906{
907 // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
908 // we don't have a location to count underrun frames. The underrun frame counter
909 // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not
910 // possible for static buffer tracks other than at end of buffer, so this is not a loss.
911
912 // FIXME also wake futex so that underrun is noticed more quickly
913 (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
914}
915
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800916// ---------------------------------------------------------------------------
917
Glenn Kastena8190fc2012-12-03 17:06:56 -0800918} // namespace android