blob: 0dbfa62afe62c1d3b291aa29a779b1545a6489e7 [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
28audio_track_cblk_t::audio_track_cblk_t()
Glenn Kasten74935e42013-12-19 08:56:45 -080029 : mServer(0), mFutex(0), mMinimum(0),
Glenn Kastenc56f3422014-03-21 17:53:17 -070030 mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0), mFlags(0)
Glenn Kasten9f80dd22012-12-18 15:57:32 -080031{
32 memset(&u, 0, sizeof(u));
33}
34
35// ---------------------------------------------------------------------------
36
37Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
38 bool isOut, bool clientInServer)
39 : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize),
40 mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer),
Glenn Kasten7db7df02013-06-25 16:13:23 -070041 mIsShutdown(false), mUnreleased(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080042{
43}
44
Glenn Kasten9f80dd22012-12-18 15:57:32 -080045// ---------------------------------------------------------------------------
46
47ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
48 size_t frameSize, bool isOut, bool clientInServer)
49 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), mEpoch(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080050{
Glenn Kastena8190fc2012-12-03 17:06:56 -080051}
52
Glenn Kasten9f80dd22012-12-18 15:57:32 -080053const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/};
54const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/};
55
56#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS
57
58// To facilitate quicker recovery from server failure, this value limits the timeout per each futex
59// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit.
60// FIXME May not be compatible with audio tunneling requirements where timeout should be in the
61// order of minutes.
62#define MAX_SEC 5
63
64status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested,
65 struct timespec *elapsed)
Glenn Kastena8190fc2012-12-03 17:06:56 -080066{
Glenn Kasten7db7df02013-06-25 16:13:23 -070067 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080068 struct timespec total; // total elapsed time spent waiting
69 total.tv_sec = 0;
70 total.tv_nsec = 0;
71 bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting
Glenn Kastena8190fc2012-12-03 17:06:56 -080072
Glenn Kasten9f80dd22012-12-18 15:57:32 -080073 status_t status;
74 enum {
75 TIMEOUT_ZERO, // requested == NULL || *requested == 0
76 TIMEOUT_INFINITE, // *requested == infinity
77 TIMEOUT_FINITE, // 0 < *requested < infinity
78 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
79 } timeout;
80 if (requested == NULL) {
81 timeout = TIMEOUT_ZERO;
82 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
83 timeout = TIMEOUT_ZERO;
84 } else if (requested->tv_sec == INT_MAX) {
85 timeout = TIMEOUT_INFINITE;
Glenn Kastena8190fc2012-12-03 17:06:56 -080086 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -080087 timeout = TIMEOUT_FINITE;
88 if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) {
89 measure = true;
90 }
Glenn Kastena8190fc2012-12-03 17:06:56 -080091 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -080092 struct timespec before;
93 bool beforeIsValid = false;
94 audio_track_cblk_t* cblk = mCblk;
95 bool ignoreInitialPendingInterrupt = true;
96 // check for shared memory corruption
97 if (mIsShutdown) {
98 status = NO_INIT;
99 goto end;
100 }
101 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700102 int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800103 // check for track invalidation by server, or server death detection
104 if (flags & CBLK_INVALID) {
105 ALOGV("Track invalidated");
106 status = DEAD_OBJECT;
107 goto end;
108 }
109 // check for obtainBuffer interrupted by client
110 if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) {
111 ALOGV("obtainBuffer() interrupted by client");
112 status = -EINTR;
113 goto end;
114 }
115 ignoreInitialPendingInterrupt = false;
116 // compute number of frames available to write (AudioTrack) or read (AudioRecord)
117 int32_t front;
118 int32_t rear;
119 if (mIsOut) {
120 // The barrier following the read of mFront is probably redundant.
121 // We're about to perform a conditional branch based on 'filled',
122 // which will force the processor to observe the read of mFront
123 // prior to allowing data writes starting at mRaw.
124 // However, the processor may support speculative execution,
125 // and be unable to undo speculative writes into shared memory.
126 // The barrier will prevent such speculative execution.
127 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
128 rear = cblk->u.mStreaming.mRear;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800129 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800130 // On the other hand, this barrier is required.
131 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
132 front = cblk->u.mStreaming.mFront;
133 }
134 ssize_t filled = rear - front;
135 // pipe should not be overfull
136 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Glenn Kastenc263ca02014-06-04 20:31:46 -0700137 if (mIsOut) {
138 ALOGE("Shared memory control block is corrupt (filled=%d, mFrameCount=%u); "
139 "shutting down", filled, mFrameCount);
140 mIsShutdown = true;
141 status = NO_INIT;
142 goto end;
143 }
144 // for input, sync up on overrun
145 filled = 0;
146 cblk->u.mStreaming.mFront = rear;
147 (void) android_atomic_or(CBLK_OVERRUN, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148 }
149 // don't allow filling pipe beyond the nominal size
150 size_t avail = mIsOut ? mFrameCount - filled : filled;
151 if (avail > 0) {
152 // 'avail' may be non-contiguous, so return only the first contiguous chunk
153 size_t part1;
154 if (mIsOut) {
155 rear &= mFrameCountP2 - 1;
156 part1 = mFrameCountP2 - rear;
157 } else {
158 front &= mFrameCountP2 - 1;
159 part1 = mFrameCountP2 - front;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800160 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800161 if (part1 > avail) {
162 part1 = avail;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800163 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800164 if (part1 > buffer->mFrameCount) {
165 part1 = buffer->mFrameCount;
166 }
167 buffer->mFrameCount = part1;
168 buffer->mRaw = part1 > 0 ?
169 &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL;
170 buffer->mNonContig = avail - part1;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700171 mUnreleased = part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800172 status = NO_ERROR;
173 break;
174 }
175 struct timespec remaining;
176 const struct timespec *ts;
177 switch (timeout) {
178 case TIMEOUT_ZERO:
179 status = WOULD_BLOCK;
180 goto end;
181 case TIMEOUT_INFINITE:
182 ts = NULL;
183 break;
184 case TIMEOUT_FINITE:
185 timeout = TIMEOUT_CONTINUE;
186 if (MAX_SEC == 0) {
187 ts = requested;
188 break;
189 }
190 // fall through
191 case TIMEOUT_CONTINUE:
192 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
193 if (!measure || requested->tv_sec < total.tv_sec ||
194 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
195 status = TIMED_OUT;
196 goto end;
197 }
198 remaining.tv_sec = requested->tv_sec - total.tv_sec;
199 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
200 remaining.tv_nsec += 1000000000;
201 remaining.tv_sec++;
202 }
203 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
204 remaining.tv_sec = MAX_SEC;
205 remaining.tv_nsec = 0;
206 }
207 ts = &remaining;
208 break;
209 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800210 LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800211 ts = NULL;
212 break;
213 }
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700214 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
215 if (!(old & CBLK_FUTEX_WAKE)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800216 if (measure && !beforeIsValid) {
217 clock_gettime(CLOCK_MONOTONIC, &before);
218 beforeIsValid = true;
219 }
Elliott Hughesee499292014-05-21 17:55:51 -0700220 errno = 0;
221 (void) syscall(__NR_futex, &cblk->mFutex,
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700222 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800223 // update total elapsed time spent waiting
224 if (measure) {
225 struct timespec after;
226 clock_gettime(CLOCK_MONOTONIC, &after);
227 total.tv_sec += after.tv_sec - before.tv_sec;
228 long deltaNs = after.tv_nsec - before.tv_nsec;
229 if (deltaNs < 0) {
230 deltaNs += 1000000000;
231 total.tv_sec--;
232 }
233 if ((total.tv_nsec += deltaNs) >= 1000000000) {
234 total.tv_nsec -= 1000000000;
235 total.tv_sec++;
236 }
237 before = after;
238 beforeIsValid = true;
239 }
Elliott Hughesee499292014-05-21 17:55:51 -0700240 switch (errno) {
241 case 0: // normal wakeup by server, or by binderDied()
242 case EWOULDBLOCK: // benign race condition with server
243 case EINTR: // wait was interrupted by signal or other spurious wakeup
244 case ETIMEDOUT: // time-out expired
Glenn Kasten7db7df02013-06-25 16:13:23 -0700245 // FIXME these error/non-0 status are being dropped
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800246 break;
247 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700248 status = errno;
249 ALOGE("%s unexpected error %s", __func__, strerror(status));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800250 goto end;
251 }
252 }
253 }
254
255end:
256 if (status != NO_ERROR) {
257 buffer->mFrameCount = 0;
258 buffer->mRaw = NULL;
259 buffer->mNonContig = 0;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700260 mUnreleased = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800261 }
262 if (elapsed != NULL) {
263 *elapsed = total;
264 }
265 if (requested == NULL) {
266 requested = &kNonBlocking;
267 }
268 if (measure) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100269 ALOGV("requested %ld.%03ld elapsed %ld.%03ld",
270 requested->tv_sec, requested->tv_nsec / 1000000,
271 total.tv_sec, total.tv_nsec / 1000000);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800272 }
273 return status;
274}
275
276void ClientProxy::releaseBuffer(Buffer* buffer)
277{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700278 LOG_ALWAYS_FATAL_IF(buffer == NULL);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800279 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700280 if (stepCount == 0 || mIsShutdown) {
281 // prevent accidental re-use of buffer
282 buffer->mFrameCount = 0;
283 buffer->mRaw = NULL;
284 buffer->mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800285 return;
286 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700287 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
288 mUnreleased -= stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800289 audio_track_cblk_t* cblk = mCblk;
290 // Both of these barriers are required
291 if (mIsOut) {
292 int32_t rear = cblk->u.mStreaming.mRear;
293 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
294 } else {
295 int32_t front = cblk->u.mStreaming.mFront;
296 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
297 }
298}
299
300void ClientProxy::binderDied()
301{
302 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700303 if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800304 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
Elliott Hughesee499292014-05-21 17:55:51 -0700305 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
306 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800307 }
308}
309
310void ClientProxy::interrupt()
311{
312 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700313 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700314 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
315 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800316 }
317}
318
319size_t ClientProxy::getMisalignment()
320{
321 audio_track_cblk_t* cblk = mCblk;
322 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
323 (mFrameCountP2 - 1);
324}
325
Eric Laurentcc21e4f2013-10-16 15:12:32 -0700326size_t ClientProxy::getFramesFilled() {
327 audio_track_cblk_t* cblk = mCblk;
328 int32_t front;
329 int32_t rear;
330
331 if (mIsOut) {
332 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
333 rear = cblk->u.mStreaming.mRear;
334 } else {
335 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
336 front = cblk->u.mStreaming.mFront;
337 }
338 ssize_t filled = rear - front;
339 // pipe should not be overfull
340 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
341 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
342 return 0;
343 }
344 return (size_t)filled;
345}
346
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800347// ---------------------------------------------------------------------------
348
349void AudioTrackClientProxy::flush()
350{
351 mCblk->u.mStreaming.mFlush++;
352}
353
Eric Laurentbfb1b832013-01-07 09:53:42 -0800354bool AudioTrackClientProxy::clearStreamEndDone() {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700355 return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800356}
357
358bool AudioTrackClientProxy::getStreamEndDone() const {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700359 return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800360}
361
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100362status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested)
363{
364 struct timespec total; // total elapsed time spent waiting
365 total.tv_sec = 0;
366 total.tv_nsec = 0;
367 audio_track_cblk_t* cblk = mCblk;
368 status_t status;
369 enum {
370 TIMEOUT_ZERO, // requested == NULL || *requested == 0
371 TIMEOUT_INFINITE, // *requested == infinity
372 TIMEOUT_FINITE, // 0 < *requested < infinity
373 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
374 } timeout;
375 if (requested == NULL) {
376 timeout = TIMEOUT_ZERO;
377 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
378 timeout = TIMEOUT_ZERO;
379 } else if (requested->tv_sec == INT_MAX) {
380 timeout = TIMEOUT_INFINITE;
381 } else {
382 timeout = TIMEOUT_FINITE;
383 }
384 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700385 int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100386 // check for track invalidation by server, or server death detection
387 if (flags & CBLK_INVALID) {
388 ALOGV("Track invalidated");
389 status = DEAD_OBJECT;
390 goto end;
391 }
392 if (flags & CBLK_STREAM_END_DONE) {
393 ALOGV("stream end received");
394 status = NO_ERROR;
395 goto end;
396 }
397 // check for obtainBuffer interrupted by client
398 // check for obtainBuffer interrupted by client
399 if (flags & CBLK_INTERRUPT) {
400 ALOGV("waitStreamEndDone() interrupted by client");
401 status = -EINTR;
402 goto end;
403 }
404 struct timespec remaining;
405 const struct timespec *ts;
406 switch (timeout) {
407 case TIMEOUT_ZERO:
408 status = WOULD_BLOCK;
409 goto end;
410 case TIMEOUT_INFINITE:
411 ts = NULL;
412 break;
413 case TIMEOUT_FINITE:
414 timeout = TIMEOUT_CONTINUE;
415 if (MAX_SEC == 0) {
416 ts = requested;
417 break;
418 }
419 // fall through
420 case TIMEOUT_CONTINUE:
421 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
422 if (requested->tv_sec < total.tv_sec ||
423 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
424 status = TIMED_OUT;
425 goto end;
426 }
427 remaining.tv_sec = requested->tv_sec - total.tv_sec;
428 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
429 remaining.tv_nsec += 1000000000;
430 remaining.tv_sec++;
431 }
432 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
433 remaining.tv_sec = MAX_SEC;
434 remaining.tv_nsec = 0;
435 }
436 ts = &remaining;
437 break;
438 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800439 LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100440 ts = NULL;
441 break;
442 }
443 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
444 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700445 errno = 0;
446 (void) syscall(__NR_futex, &cblk->mFutex,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100447 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Elliott Hughesee499292014-05-21 17:55:51 -0700448 switch (errno) {
449 case 0: // normal wakeup by server, or by binderDied()
450 case EWOULDBLOCK: // benign race condition with server
451 case EINTR: // wait was interrupted by signal or other spurious wakeup
452 case ETIMEDOUT: // time-out expired
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100453 break;
454 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700455 status = errno;
456 ALOGE("%s unexpected error %s", __func__, strerror(status));
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100457 goto end;
458 }
459 }
460 }
461
462end:
463 if (requested == NULL) {
464 requested = &kNonBlocking;
465 }
466 return status;
467}
468
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800469// ---------------------------------------------------------------------------
470
471StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
472 size_t frameCount, size_t frameSize)
473 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
474 mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0)
475{
476}
477
478void StaticAudioTrackClientProxy::flush()
479{
Glenn Kastenadad3d72014-02-21 14:51:43 -0800480 LOG_ALWAYS_FATAL("static flush");
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481}
482
483void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
484{
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800485 // This can only happen on a 64-bit client
486 if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) {
487 // FIXME Should return an error status
488 return;
489 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800490 StaticAudioTrackState newState;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800491 newState.mLoopStart = (uint32_t) loopStart;
492 newState.mLoopEnd = (uint32_t) loopEnd;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800493 newState.mLoopCount = loopCount;
494 mBufferPosition = loopStart;
495 (void) mMutator.push(newState);
496}
497
498size_t StaticAudioTrackClientProxy::getBufferPosition()
499{
500 size_t bufferPosition;
501 if (mMutator.ack()) {
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800502 bufferPosition = (size_t) mCblk->u.mStatic.mBufferPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800503 if (bufferPosition > mFrameCount) {
504 bufferPosition = mFrameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800505 }
506 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 bufferPosition = mBufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800508 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800509 return bufferPosition;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800510}
511
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800512// ---------------------------------------------------------------------------
513
514ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
515 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700516 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Glenn Kastence8828a2013-09-16 18:07:38 -0700517 mAvailToClient(0), mFlush(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800518{
Glenn Kastena8190fc2012-12-03 17:06:56 -0800519}
520
Glenn Kasten2e422c42013-10-18 13:00:29 -0700521status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700523 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800524 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700525 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800526 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700527 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800528 audio_track_cblk_t* cblk = mCblk;
529 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
530 // or use previous cached value from framesReady(), with added barrier if it omits.
531 int32_t front;
532 int32_t rear;
533 // See notes on barriers at ClientProxy::obtainBuffer()
534 if (mIsOut) {
535 int32_t flush = cblk->u.mStreaming.mFlush;
536 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100537 front = cblk->u.mStreaming.mFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800538 if (flush != mFlush) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539 mFlush = flush;
Glenn Kasten050501d2013-07-11 10:35:38 -0700540 // effectively obtain then release whatever is in the buffer
541 android_atomic_release_store(rear, &cblk->u.mStreaming.mFront);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100542 if (front != rear) {
543 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
544 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700545 (void) syscall(__NR_futex, &cblk->mFutex,
546 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100547 }
548 }
549 front = rear;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800550 }
551 } else {
552 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
553 rear = cblk->u.mStreaming.mRear;
554 }
555 ssize_t filled = rear - front;
556 // pipe should not already be overfull
557 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
558 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
559 mIsShutdown = true;
560 }
561 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700562 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800563 }
564 // don't allow filling pipe beyond the nominal size
565 size_t availToServer;
566 if (mIsOut) {
567 availToServer = filled;
568 mAvailToClient = mFrameCount - filled;
569 } else {
570 availToServer = mFrameCount - filled;
571 mAvailToClient = filled;
572 }
573 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
574 size_t part1;
575 if (mIsOut) {
576 front &= mFrameCountP2 - 1;
577 part1 = mFrameCountP2 - front;
578 } else {
579 rear &= mFrameCountP2 - 1;
580 part1 = mFrameCountP2 - rear;
581 }
582 if (part1 > availToServer) {
583 part1 = availToServer;
584 }
585 size_t ask = buffer->mFrameCount;
586 if (part1 > ask) {
587 part1 = ask;
588 }
589 // is assignment redundant in some cases?
590 buffer->mFrameCount = part1;
591 buffer->mRaw = part1 > 0 ?
592 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
593 buffer->mNonContig = availToServer - part1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700594 // After flush(), allow releaseBuffer() on a previously obtained buffer;
595 // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
596 if (!ackFlush) {
597 mUnreleased = part1;
598 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800599 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700600 }
601no_init:
602 buffer->mFrameCount = 0;
603 buffer->mRaw = NULL;
604 buffer->mNonContig = 0;
605 mUnreleased = 0;
606 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800607}
608
609void ServerProxy::releaseBuffer(Buffer* buffer)
610{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700611 LOG_ALWAYS_FATAL_IF(buffer == NULL);
612 size_t stepCount = buffer->mFrameCount;
613 if (stepCount == 0 || mIsShutdown) {
614 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800615 buffer->mFrameCount = 0;
616 buffer->mRaw = NULL;
617 buffer->mNonContig = 0;
618 return;
619 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700620 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800621 mUnreleased -= stepCount;
622 audio_track_cblk_t* cblk = mCblk;
623 if (mIsOut) {
624 int32_t front = cblk->u.mStreaming.mFront;
625 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
626 } else {
627 int32_t rear = cblk->u.mStreaming.mRear;
628 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
629 }
630
Glenn Kasten844f88c2014-05-09 13:38:09 -0700631 cblk->mServer += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800632
633 size_t half = mFrameCount / 2;
634 if (half == 0) {
635 half = 1;
636 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800637 size_t minimum = (size_t) cblk->mMinimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800638 if (minimum == 0) {
639 minimum = mIsOut ? half : 1;
640 } else if (minimum > half) {
641 minimum = half;
642 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700643 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
Glenn Kastence8828a2013-09-16 18:07:38 -0700644 if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800645 ALOGV("mAvailToClient=%u stepCount=%u minimum=%u", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700646 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
647 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700648 (void) syscall(__NR_futex, &cblk->mFutex,
649 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 }
651 }
652
653 buffer->mFrameCount = 0;
654 buffer->mRaw = NULL;
655 buffer->mNonContig = 0;
656}
657
658// ---------------------------------------------------------------------------
659
660size_t AudioTrackServerProxy::framesReady()
661{
662 LOG_ALWAYS_FATAL_IF(!mIsOut);
663
664 if (mIsShutdown) {
665 return 0;
666 }
667 audio_track_cblk_t* cblk = mCblk;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100668
669 int32_t flush = cblk->u.mStreaming.mFlush;
670 if (flush != mFlush) {
671 return mFrameCount;
672 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800673 // the acquire might not be necessary since not doing a subsequent read
674 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
675 ssize_t filled = rear - cblk->u.mStreaming.mFront;
676 // pipe should not already be overfull
677 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
678 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled);
679 mIsShutdown = true;
680 return 0;
681 }
682 // cache this value for later use by obtainBuffer(), with added barrier
683 // and racy if called by normal mixer thread
684 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
685 return filled;
686}
687
Eric Laurentbfb1b832013-01-07 09:53:42 -0800688bool AudioTrackServerProxy::setStreamEndDone() {
Glenn Kasten844f88c2014-05-09 13:38:09 -0700689 audio_track_cblk_t* cblk = mCblk;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800690 bool old =
Glenn Kasten844f88c2014-05-09 13:38:09 -0700691 (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800692 if (!old) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700693 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
Elliott Hughesee499292014-05-21 17:55:51 -0700694 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800695 }
696 return old;
697}
698
Glenn Kasten82aaf942013-07-17 16:05:07 -0700699void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
700{
Glenn Kasten844f88c2014-05-09 13:38:09 -0700701 audio_track_cblk_t* cblk = mCblk;
702 cblk->u.mStreaming.mUnderrunFrames += frameCount;
Glenn Kasten82aaf942013-07-17 16:05:07 -0700703
704 // FIXME also wake futex so that underrun is noticed more quickly
Glenn Kasten844f88c2014-05-09 13:38:09 -0700705 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
Glenn Kasten82aaf942013-07-17 16:05:07 -0700706}
707
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800708// ---------------------------------------------------------------------------
709
710StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
711 size_t frameCount, size_t frameSize)
712 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
713 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0),
714 mEnd(frameCount), mFramesReadyIsCalledByMultipleThreads(false)
715{
716 mState.mLoopStart = 0;
717 mState.mLoopEnd = 0;
718 mState.mLoopCount = 0;
719}
720
721void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
722{
723 mFramesReadyIsCalledByMultipleThreads = true;
724}
725
726size_t StaticAudioTrackServerProxy::framesReady()
727{
728 // FIXME
729 // This is racy if called by normal mixer thread,
730 // as we're reading 2 independent variables without a lock.
731 // Can't call mObserver.poll(), as we might be called from wrong thread.
732 // If looping is enabled, should return a higher number (since includes non-contiguous).
733 size_t position = mPosition;
734 if (!mFramesReadyIsCalledByMultipleThreads) {
735 ssize_t positionOrStatus = pollPosition();
736 if (positionOrStatus >= 0) {
737 position = (size_t) positionOrStatus;
738 }
739 }
740 size_t end = mEnd;
741 return position < end ? end - position : 0;
742}
743
744ssize_t StaticAudioTrackServerProxy::pollPosition()
745{
746 size_t position = mPosition;
747 StaticAudioTrackState state;
748 if (mObserver.poll(state)) {
749 bool valid = false;
750 size_t loopStart = state.mLoopStart;
751 size_t loopEnd = state.mLoopEnd;
752 if (state.mLoopCount == 0) {
753 if (loopStart > mFrameCount) {
754 loopStart = mFrameCount;
755 }
756 // ignore loopEnd
757 mPosition = position = loopStart;
758 mEnd = mFrameCount;
759 mState.mLoopCount = 0;
760 valid = true;
761 } else {
762 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
763 loopEnd - loopStart >= MIN_LOOP) {
764 if (!(loopStart <= position && position < loopEnd)) {
765 mPosition = position = loopStart;
766 }
767 mEnd = loopEnd;
768 mState = state;
769 valid = true;
770 }
771 }
772 if (!valid) {
773 ALOGE("%s client pushed an invalid state, shutting down", __func__);
774 mIsShutdown = true;
775 return (ssize_t) NO_INIT;
776 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800777 // This may overflow, but client is not supposed to rely on it
778 mCblk->u.mStatic.mBufferPosition = (uint32_t) position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800779 }
780 return (ssize_t) position;
781}
782
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800783status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800784{
785 if (mIsShutdown) {
786 buffer->mFrameCount = 0;
787 buffer->mRaw = NULL;
788 buffer->mNonContig = 0;
789 mUnreleased = 0;
790 return NO_INIT;
791 }
792 ssize_t positionOrStatus = pollPosition();
793 if (positionOrStatus < 0) {
794 buffer->mFrameCount = 0;
795 buffer->mRaw = NULL;
796 buffer->mNonContig = 0;
797 mUnreleased = 0;
798 return (status_t) positionOrStatus;
799 }
800 size_t position = (size_t) positionOrStatus;
801 size_t avail;
802 if (position < mEnd) {
803 avail = mEnd - position;
804 size_t wanted = buffer->mFrameCount;
805 if (avail < wanted) {
806 buffer->mFrameCount = avail;
807 } else {
808 avail = wanted;
809 }
810 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
811 } else {
812 avail = 0;
813 buffer->mFrameCount = 0;
814 buffer->mRaw = NULL;
815 }
816 buffer->mNonContig = 0; // FIXME should be > 0 for looping
817 mUnreleased = avail;
818 return NO_ERROR;
819}
820
821void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
822{
823 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700824 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800825 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700826 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800827 buffer->mRaw = NULL;
828 buffer->mNonContig = 0;
829 return;
830 }
831 mUnreleased -= stepCount;
832 audio_track_cblk_t* cblk = mCblk;
833 size_t position = mPosition;
834 size_t newPosition = position + stepCount;
835 int32_t setFlags = 0;
836 if (!(position <= newPosition && newPosition <= mFrameCount)) {
837 ALOGW("%s newPosition %u outside [%u, %u]", __func__, newPosition, position, mFrameCount);
838 newPosition = mFrameCount;
839 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
840 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
841 newPosition = mState.mLoopStart;
842 setFlags = CBLK_LOOP_CYCLE;
843 } else {
844 mEnd = mFrameCount; // this is what allows playback to continue after the loop
845 setFlags = CBLK_LOOP_FINAL;
846 }
847 }
848 if (newPosition == mFrameCount) {
849 setFlags |= CBLK_BUFFER_END;
850 }
851 mPosition = newPosition;
852
Glenn Kastenf20e1d82013-07-12 09:45:18 -0700853 cblk->mServer += stepCount;
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800854 // This may overflow, but client is not supposed to rely on it
855 cblk->u.mStatic.mBufferPosition = (uint32_t) newPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800856 if (setFlags != 0) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700857 (void) android_atomic_or(setFlags, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800858 // this would be a good place to wake a futex
859 }
860
861 buffer->mFrameCount = 0;
862 buffer->mRaw = NULL;
863 buffer->mNonContig = 0;
864}
865
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800866void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount __unused)
Glenn Kasten82aaf942013-07-17 16:05:07 -0700867{
868 // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
869 // we don't have a location to count underrun frames. The underrun frame counter
870 // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not
871 // possible for static buffer tracks other than at end of buffer, so this is not a loss.
872
873 // FIXME also wake futex so that underrun is noticed more quickly
874 (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
875}
876
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800877// ---------------------------------------------------------------------------
878
Glenn Kastena8190fc2012-12-03 17:06:56 -0800879} // namespace android