blob: d40f193ccf9d3e5f3faa97aaea251d45195a8b89 [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) {
Andy Hung486a7132014-12-22 16:54:21 -080031 return sizeof(T) > sizeof(size_t) && x > (T) SIZE_MAX ? SIZE_MAX : x < 0 ? 0 : (size_t) x;
Andy Hungcb2129b2014-11-11 12:17:22 -080032}
33
Andy Hung9b461582014-12-01 17:56:29 -080034// incrementSequence is used to determine the next sequence value
35// for the loop and position sequence counters. It should return
36// a value between "other" + 1 and "other" + INT32_MAX, the choice of
37// which needs to be the "least recently used" sequence value for "self".
38// In general, this means (new_self) returned is max(self, other) + 1.
Andy Hungd4ee4db2017-07-12 15:26:04 -070039__attribute__((no_sanitize("integer")))
Andy Hung9b461582014-12-01 17:56:29 -080040static uint32_t incrementSequence(uint32_t self, uint32_t other) {
Chad Brubakercb50c542015-10-07 14:20:10 -070041 int32_t diff = (int32_t) self - (int32_t) other;
Andy Hung9b461582014-12-01 17:56:29 -080042 if (diff >= 0 && diff < INT32_MAX) {
43 return self + 1; // we're already ahead of other.
44 }
45 return other + 1; // we're behind, so move just ahead of other.
46}
47
Glenn Kastena8190fc2012-12-03 17:06:56 -080048audio_track_cblk_t::audio_track_cblk_t()
Phil Burke8972b02016-03-04 11:29:57 -080049 : mServer(0), mFutex(0), mMinimum(0)
50 , mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0)
51 , mBufferSizeInFrames(0)
52 , mFlags(0)
Glenn Kasten9f80dd22012-12-18 15:57:32 -080053{
54 memset(&u, 0, sizeof(u));
55}
56
57// ---------------------------------------------------------------------------
58
59Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize,
60 bool isOut, bool clientInServer)
61 : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize),
62 mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer),
Glenn Kasten7db7df02013-06-25 16:13:23 -070063 mIsShutdown(false), mUnreleased(0)
Glenn Kastena8190fc2012-12-03 17:06:56 -080064{
65}
66
Glenn Kasten9f80dd22012-12-18 15:57:32 -080067// ---------------------------------------------------------------------------
68
69ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
70 size_t frameSize, bool isOut, bool clientInServer)
Phil Burkc0adecb2016-01-08 12:44:11 -080071 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer)
Phil Burkc0adecb2016-01-08 12:44:11 -080072 , mEpoch(0)
Andy Hung6ae58432016-02-16 18:32:24 -080073 , mTimestampObserver(&cblk->mExtendedTimestampQueue)
Glenn Kastena8190fc2012-12-03 17:06:56 -080074{
Phil Burke8972b02016-03-04 11:29:57 -080075 setBufferSizeInFrames(frameCount);
Glenn Kastena8190fc2012-12-03 17:06:56 -080076}
77
Glenn Kasten9f80dd22012-12-18 15:57:32 -080078const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/};
79const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/};
80
81#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS
82
83// To facilitate quicker recovery from server failure, this value limits the timeout per each futex
84// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit.
85// FIXME May not be compatible with audio tunneling requirements where timeout should be in the
86// order of minutes.
87#define MAX_SEC 5
88
Phil Burke8972b02016-03-04 11:29:57 -080089uint32_t ClientProxy::setBufferSizeInFrames(uint32_t size)
90{
Phil Burke8972b02016-03-04 11:29:57 -080091 // The minimum should be greater than zero and less than the size
92 // at which underruns will occur.
Phil Burk26760d12016-03-21 11:53:07 -070093 const uint32_t minimum = 16; // based on AudioMixer::BLOCKSIZE
Phil Burke8972b02016-03-04 11:29:57 -080094 const uint32_t maximum = frameCount();
95 uint32_t clippedSize = size;
Phil Burk26760d12016-03-21 11:53:07 -070096 if (maximum < minimum) {
97 clippedSize = maximum;
98 } else if (clippedSize < minimum) {
Phil Burke8972b02016-03-04 11:29:57 -080099 clippedSize = minimum;
100 } else if (clippedSize > maximum) {
101 clippedSize = maximum;
102 }
103 // for server to read
104 android_atomic_release_store(clippedSize, (int32_t *)&mCblk->mBufferSizeInFrames);
105 // for client to read
106 mBufferSizeInFrames = clippedSize;
107 return clippedSize;
108}
109
ilewis926b82f2016-03-29 14:50:36 -0700110__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800111status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested,
112 struct timespec *elapsed)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800113{
Andy Hung9c64f342017-08-02 18:10:00 -0700114 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0,
115 "%s: null or zero frame buffer, buffer:%p", __func__, buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800116 struct timespec total; // total elapsed time spent waiting
117 total.tv_sec = 0;
118 total.tv_nsec = 0;
119 bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting
Glenn Kastena8190fc2012-12-03 17:06:56 -0800120
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800121 status_t status;
122 enum {
123 TIMEOUT_ZERO, // requested == NULL || *requested == 0
124 TIMEOUT_INFINITE, // *requested == infinity
125 TIMEOUT_FINITE, // 0 < *requested < infinity
126 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
127 } timeout;
128 if (requested == NULL) {
129 timeout = TIMEOUT_ZERO;
130 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
131 timeout = TIMEOUT_ZERO;
132 } else if (requested->tv_sec == INT_MAX) {
133 timeout = TIMEOUT_INFINITE;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800134 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 timeout = TIMEOUT_FINITE;
136 if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) {
137 measure = true;
138 }
Glenn Kastena8190fc2012-12-03 17:06:56 -0800139 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800140 struct timespec before;
141 bool beforeIsValid = false;
142 audio_track_cblk_t* cblk = mCblk;
143 bool ignoreInitialPendingInterrupt = true;
144 // check for shared memory corruption
145 if (mIsShutdown) {
146 status = NO_INIT;
147 goto end;
148 }
149 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700150 int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800151 // check for track invalidation by server, or server death detection
152 if (flags & CBLK_INVALID) {
153 ALOGV("Track invalidated");
154 status = DEAD_OBJECT;
155 goto end;
156 }
Eric Laurent4d231dc2016-03-11 18:38:23 -0800157 if (flags & CBLK_DISABLED) {
158 ALOGV("Track disabled");
159 status = NOT_ENOUGH_DATA;
160 goto end;
161 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800162 // check for obtainBuffer interrupted by client
163 if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) {
164 ALOGV("obtainBuffer() interrupted by client");
165 status = -EINTR;
166 goto end;
167 }
168 ignoreInitialPendingInterrupt = false;
169 // compute number of frames available to write (AudioTrack) or read (AudioRecord)
170 int32_t front;
171 int32_t rear;
172 if (mIsOut) {
173 // The barrier following the read of mFront is probably redundant.
174 // We're about to perform a conditional branch based on 'filled',
175 // which will force the processor to observe the read of mFront
176 // prior to allowing data writes starting at mRaw.
177 // However, the processor may support speculative execution,
178 // and be unable to undo speculative writes into shared memory.
179 // The barrier will prevent such speculative execution.
180 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
181 rear = cblk->u.mStreaming.mRear;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800182 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800183 // On the other hand, this barrier is required.
184 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
185 front = cblk->u.mStreaming.mFront;
186 }
Phil Burkc0adecb2016-01-08 12:44:11 -0800187 // write to rear, read from front
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800188 ssize_t filled = rear - front;
189 // pipe should not be overfull
190 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700191 if (mIsOut) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700192 ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); "
Glenn Kasten6dbb5e32014-05-13 10:38:42 -0700193 "shutting down", filled, mFrameCount);
194 mIsShutdown = true;
195 status = NO_INIT;
196 goto end;
197 }
198 // for input, sync up on overrun
199 filled = 0;
200 cblk->u.mStreaming.mFront = rear;
201 (void) android_atomic_or(CBLK_OVERRUN, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800202 }
Phil Burkc0adecb2016-01-08 12:44:11 -0800203 // Don't allow filling pipe beyond the user settable size.
204 // The calculation for avail can go negative if the buffer size
205 // is suddenly dropped below the amount already in the buffer.
206 // So use a signed calculation to prevent a numeric overflow abort.
Phil Burke8972b02016-03-04 11:29:57 -0800207 ssize_t adjustableSize = (ssize_t) getBufferSizeInFrames();
Phil Burkc0adecb2016-01-08 12:44:11 -0800208 ssize_t avail = (mIsOut) ? adjustableSize - filled : filled;
209 if (avail < 0) {
210 avail = 0;
211 } else if (avail > 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800212 // 'avail' may be non-contiguous, so return only the first contiguous chunk
Eric Laurentbdd81012016-01-29 15:25:06 -0800213 size_t part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800214 if (mIsOut) {
215 rear &= mFrameCountP2 - 1;
216 part1 = mFrameCountP2 - rear;
217 } else {
218 front &= mFrameCountP2 - 1;
219 part1 = mFrameCountP2 - front;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800220 }
Eric Laurentbdd81012016-01-29 15:25:06 -0800221 if (part1 > (size_t)avail) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800222 part1 = avail;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800223 }
Eric Laurentbdd81012016-01-29 15:25:06 -0800224 if (part1 > buffer->mFrameCount) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800225 part1 = buffer->mFrameCount;
226 }
Eric Laurentbdd81012016-01-29 15:25:06 -0800227 buffer->mFrameCount = part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800228 buffer->mRaw = part1 > 0 ?
229 &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL;
230 buffer->mNonContig = avail - part1;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700231 mUnreleased = part1;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800232 status = NO_ERROR;
233 break;
234 }
235 struct timespec remaining;
236 const struct timespec *ts;
237 switch (timeout) {
238 case TIMEOUT_ZERO:
239 status = WOULD_BLOCK;
240 goto end;
241 case TIMEOUT_INFINITE:
242 ts = NULL;
243 break;
244 case TIMEOUT_FINITE:
245 timeout = TIMEOUT_CONTINUE;
246 if (MAX_SEC == 0) {
247 ts = requested;
248 break;
249 }
250 // fall through
251 case TIMEOUT_CONTINUE:
252 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
253 if (!measure || requested->tv_sec < total.tv_sec ||
254 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
255 status = TIMED_OUT;
256 goto end;
257 }
258 remaining.tv_sec = requested->tv_sec - total.tv_sec;
259 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
260 remaining.tv_nsec += 1000000000;
261 remaining.tv_sec++;
262 }
263 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
264 remaining.tv_sec = MAX_SEC;
265 remaining.tv_nsec = 0;
266 }
267 ts = &remaining;
268 break;
269 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800270 LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800271 ts = NULL;
272 break;
273 }
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700274 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
275 if (!(old & CBLK_FUTEX_WAKE)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800276 if (measure && !beforeIsValid) {
277 clock_gettime(CLOCK_MONOTONIC, &before);
278 beforeIsValid = true;
279 }
Elliott Hughesee499292014-05-21 17:55:51 -0700280 errno = 0;
281 (void) syscall(__NR_futex, &cblk->mFutex,
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700282 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Leena Winterrowdb463da82015-12-14 15:58:16 -0800283 status_t error = errno; // clock_gettime can affect errno
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800284 // update total elapsed time spent waiting
285 if (measure) {
286 struct timespec after;
287 clock_gettime(CLOCK_MONOTONIC, &after);
288 total.tv_sec += after.tv_sec - before.tv_sec;
Chih-Hung Hsiehbca74292018-08-10 16:06:07 -0700289 // Use auto instead of long to avoid the google-runtime-int warning.
290 auto deltaNs = after.tv_nsec - before.tv_nsec;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800291 if (deltaNs < 0) {
292 deltaNs += 1000000000;
293 total.tv_sec--;
294 }
295 if ((total.tv_nsec += deltaNs) >= 1000000000) {
296 total.tv_nsec -= 1000000000;
297 total.tv_sec++;
298 }
299 before = after;
300 beforeIsValid = true;
301 }
Leena Winterrowdb463da82015-12-14 15:58:16 -0800302 switch (error) {
Elliott Hughesee499292014-05-21 17:55:51 -0700303 case 0: // normal wakeup by server, or by binderDied()
304 case EWOULDBLOCK: // benign race condition with server
305 case EINTR: // wait was interrupted by signal or other spurious wakeup
306 case ETIMEDOUT: // time-out expired
Glenn Kasten7db7df02013-06-25 16:13:23 -0700307 // FIXME these error/non-0 status are being dropped
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800308 break;
309 default:
Leena Winterrowdb463da82015-12-14 15:58:16 -0800310 status = error;
Elliott Hughesee499292014-05-21 17:55:51 -0700311 ALOGE("%s unexpected error %s", __func__, strerror(status));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800312 goto end;
313 }
314 }
315 }
316
317end:
318 if (status != NO_ERROR) {
319 buffer->mFrameCount = 0;
320 buffer->mRaw = NULL;
321 buffer->mNonContig = 0;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700322 mUnreleased = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800323 }
324 if (elapsed != NULL) {
325 *elapsed = total;
326 }
327 if (requested == NULL) {
328 requested = &kNonBlocking;
329 }
330 if (measure) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100331 ALOGV("requested %ld.%03ld elapsed %ld.%03ld",
332 requested->tv_sec, requested->tv_nsec / 1000000,
333 total.tv_sec, total.tv_nsec / 1000000);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800334 }
335 return status;
336}
337
ilewis926b82f2016-03-29 14:50:36 -0700338__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800339void ClientProxy::releaseBuffer(Buffer* buffer)
340{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700341 LOG_ALWAYS_FATAL_IF(buffer == NULL);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800342 size_t stepCount = buffer->mFrameCount;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700343 if (stepCount == 0 || mIsShutdown) {
344 // prevent accidental re-use of buffer
345 buffer->mFrameCount = 0;
346 buffer->mRaw = NULL;
347 buffer->mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800348 return;
349 }
Andy Hung9c64f342017-08-02 18:10:00 -0700350 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount),
351 "%s: mUnreleased out of range, "
352 "!(stepCount:%zu <= mUnreleased:%zu <= mFrameCount:%zu), BufferSizeInFrames:%u",
353 __func__, stepCount, mUnreleased, mFrameCount, getBufferSizeInFrames());
Glenn Kasten7db7df02013-06-25 16:13:23 -0700354 mUnreleased -= stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800355 audio_track_cblk_t* cblk = mCblk;
356 // Both of these barriers are required
357 if (mIsOut) {
358 int32_t rear = cblk->u.mStreaming.mRear;
359 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
360 } else {
361 int32_t front = cblk->u.mStreaming.mFront;
362 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
363 }
364}
365
366void ClientProxy::binderDied()
367{
368 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700369 if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) {
zunkyu.lee82a69ea2014-11-07 15:47:32 +0900370 android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800371 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
Elliott Hughesee499292014-05-21 17:55:51 -0700372 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
373 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800374 }
375}
376
377void ClientProxy::interrupt()
378{
379 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700380 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) {
zunkyu.lee82a69ea2014-11-07 15:47:32 +0900381 android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
Elliott Hughesee499292014-05-21 17:55:51 -0700382 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
383 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800384 }
385}
386
Chad Brubaker65dda4f2015-09-22 16:13:30 -0700387__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800388size_t ClientProxy::getMisalignment()
389{
390 audio_track_cblk_t* cblk = mCblk;
391 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
392 (mFrameCountP2 - 1);
393}
394
395// ---------------------------------------------------------------------------
396
397void AudioTrackClientProxy::flush()
398{
Andy Hung1d3556d2018-03-29 16:30:14 -0700399 sendStreamingFlushStop(true /* flush */);
400}
401
402void AudioTrackClientProxy::stop()
403{
404 sendStreamingFlushStop(false /* flush */);
405}
406
407// Sets the client-written mFlush and mStop positions, which control server behavior.
408//
409// @param flush indicates whether the operation is a flush or stop.
410// A client stop sets mStop to the current write position;
411// the server will not read past this point until start() or subsequent flush().
412// A client flush sets both mStop and mFlush to the current write position.
413// This advances the server read limit (if previously set) and on the next
414// server read advances the server read position to this limit.
415//
416void AudioTrackClientProxy::sendStreamingFlushStop(bool flush)
417{
418 // TODO: Replace this by 64 bit counters - avoids wrap complication.
Glenn Kasten20f51b12014-10-30 10:43:19 -0700419 // This works for mFrameCountP2 <= 2^30
Andy Hunga2d75cd2015-07-15 17:04:20 -0700420 // mFlush is 32 bits concatenated as [ flush_counter ] [ newfront_offset ]
421 // Should newFlush = cblk->u.mStreaming.mRear? Only problem is
422 // if you want to flush twice to the same rear location after a 32 bit wrap.
Andy Hung1d3556d2018-03-29 16:30:14 -0700423
424 const size_t increment = mFrameCountP2 << 1;
425 const size_t mask = increment - 1;
426 // No need for client atomic synchronization on mRear, mStop, mFlush
427 // as AudioTrack client only read/writes to them under client lock. Server only reads.
428 const int32_t rearMasked = mCblk->u.mStreaming.mRear & mask;
429
430 // update stop before flush so that the server front
431 // never advances beyond a (potential) previous stop's rear limit.
432 int32_t stopBits; // the following add can overflow
433 __builtin_add_overflow(mCblk->u.mStreaming.mStop & ~mask, increment, &stopBits);
434 android_atomic_release_store(rearMasked | stopBits, &mCblk->u.mStreaming.mStop);
435
436 if (flush) {
437 int32_t flushBits; // the following add can overflow
438 __builtin_add_overflow(mCblk->u.mStreaming.mFlush & ~mask, increment, &flushBits);
439 android_atomic_release_store(rearMasked | flushBits, &mCblk->u.mStreaming.mFlush);
440 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441}
442
Eric Laurentbfb1b832013-01-07 09:53:42 -0800443bool AudioTrackClientProxy::clearStreamEndDone() {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700444 return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800445}
446
447bool AudioTrackClientProxy::getStreamEndDone() const {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700448 return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800449}
450
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100451status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested)
452{
453 struct timespec total; // total elapsed time spent waiting
454 total.tv_sec = 0;
455 total.tv_nsec = 0;
456 audio_track_cblk_t* cblk = mCblk;
457 status_t status;
458 enum {
459 TIMEOUT_ZERO, // requested == NULL || *requested == 0
460 TIMEOUT_INFINITE, // *requested == infinity
461 TIMEOUT_FINITE, // 0 < *requested < infinity
462 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE
463 } timeout;
464 if (requested == NULL) {
465 timeout = TIMEOUT_ZERO;
466 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
467 timeout = TIMEOUT_ZERO;
468 } else if (requested->tv_sec == INT_MAX) {
469 timeout = TIMEOUT_INFINITE;
470 } else {
471 timeout = TIMEOUT_FINITE;
472 }
473 for (;;) {
Glenn Kasten96f60d82013-07-12 10:21:18 -0700474 int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475 // check for track invalidation by server, or server death detection
476 if (flags & CBLK_INVALID) {
477 ALOGV("Track invalidated");
478 status = DEAD_OBJECT;
479 goto end;
480 }
Eric Laurent4d231dc2016-03-11 18:38:23 -0800481 // a track is not supposed to underrun at this stage but consider it done
482 if (flags & (CBLK_STREAM_END_DONE | CBLK_DISABLED)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100483 ALOGV("stream end received");
484 status = NO_ERROR;
485 goto end;
486 }
487 // check for obtainBuffer interrupted by client
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100488 if (flags & CBLK_INTERRUPT) {
489 ALOGV("waitStreamEndDone() interrupted by client");
490 status = -EINTR;
491 goto end;
492 }
493 struct timespec remaining;
494 const struct timespec *ts;
495 switch (timeout) {
496 case TIMEOUT_ZERO:
497 status = WOULD_BLOCK;
498 goto end;
499 case TIMEOUT_INFINITE:
500 ts = NULL;
501 break;
502 case TIMEOUT_FINITE:
503 timeout = TIMEOUT_CONTINUE;
504 if (MAX_SEC == 0) {
505 ts = requested;
506 break;
507 }
508 // fall through
509 case TIMEOUT_CONTINUE:
510 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
511 if (requested->tv_sec < total.tv_sec ||
512 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
513 status = TIMED_OUT;
514 goto end;
515 }
516 remaining.tv_sec = requested->tv_sec - total.tv_sec;
517 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
518 remaining.tv_nsec += 1000000000;
519 remaining.tv_sec++;
520 }
521 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
522 remaining.tv_sec = MAX_SEC;
523 remaining.tv_nsec = 0;
524 }
525 ts = &remaining;
526 break;
527 default:
Glenn Kastenadad3d72014-02-21 14:51:43 -0800528 LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100529 ts = NULL;
530 break;
531 }
532 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
533 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700534 errno = 0;
535 (void) syscall(__NR_futex, &cblk->mFutex,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100536 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
Elliott Hughesee499292014-05-21 17:55:51 -0700537 switch (errno) {
538 case 0: // normal wakeup by server, or by binderDied()
539 case EWOULDBLOCK: // benign race condition with server
540 case EINTR: // wait was interrupted by signal or other spurious wakeup
541 case ETIMEDOUT: // time-out expired
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100542 break;
543 default:
Elliott Hughesee499292014-05-21 17:55:51 -0700544 status = errno;
545 ALOGE("%s unexpected error %s", __func__, strerror(status));
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100546 goto end;
547 }
548 }
549 }
550
551end:
552 if (requested == NULL) {
553 requested = &kNonBlocking;
554 }
555 return status;
556}
557
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800558// ---------------------------------------------------------------------------
559
560StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
561 size_t frameCount, size_t frameSize)
562 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
Andy Hung4ede21d2014-12-12 15:37:34 -0800563 mMutator(&cblk->u.mStatic.mSingleStateQueue),
564 mPosLoopObserver(&cblk->u.mStatic.mPosLoopQueue)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800565{
Andy Hung9b461582014-12-01 17:56:29 -0800566 memset(&mState, 0, sizeof(mState));
Andy Hung4ede21d2014-12-12 15:37:34 -0800567 memset(&mPosLoop, 0, sizeof(mPosLoop));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800568}
569
570void StaticAudioTrackClientProxy::flush()
571{
Glenn Kastenadad3d72014-02-21 14:51:43 -0800572 LOG_ALWAYS_FATAL("static flush");
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800573}
574
Andy Hung1d3556d2018-03-29 16:30:14 -0700575void StaticAudioTrackClientProxy::stop()
576{
577 ; // no special handling required for static tracks.
578}
579
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800580void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
581{
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800582 // This can only happen on a 64-bit client
583 if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) {
584 // FIXME Should return an error status
585 return;
586 }
Andy Hung9b461582014-12-01 17:56:29 -0800587 mState.mLoopStart = (uint32_t) loopStart;
588 mState.mLoopEnd = (uint32_t) loopEnd;
589 mState.mLoopCount = loopCount;
590 mState.mLoopSequence = incrementSequence(mState.mLoopSequence, mState.mPositionSequence);
591 // set patch-up variables until the mState is acknowledged by the ServerProxy.
592 // observed buffer position and loop count will freeze until then to give the
593 // illusion of a synchronous change.
Andy Hung4ede21d2014-12-12 15:37:34 -0800594 getBufferPositionAndLoopCount(NULL, NULL);
Andy Hung9b461582014-12-01 17:56:29 -0800595 // preserve behavior to restart at mState.mLoopStart if position exceeds mState.mLoopEnd.
Andy Hung4ede21d2014-12-12 15:37:34 -0800596 if (mState.mLoopCount != 0 && mPosLoop.mBufferPosition >= mState.mLoopEnd) {
597 mPosLoop.mBufferPosition = mState.mLoopStart;
Andy Hung680b7952014-11-12 13:18:52 -0800598 }
Andy Hung4ede21d2014-12-12 15:37:34 -0800599 mPosLoop.mLoopCount = mState.mLoopCount;
Andy Hung9b461582014-12-01 17:56:29 -0800600 (void) mMutator.push(mState);
601}
602
603void StaticAudioTrackClientProxy::setBufferPosition(size_t position)
604{
605 // This can only happen on a 64-bit client
606 if (position > UINT32_MAX) {
607 // FIXME Should return an error status
608 return;
609 }
610 mState.mPosition = (uint32_t) position;
611 mState.mPositionSequence = incrementSequence(mState.mPositionSequence, mState.mLoopSequence);
Andy Hung4ede21d2014-12-12 15:37:34 -0800612 // set patch-up variables until the mState is acknowledged by the ServerProxy.
613 // observed buffer position and loop count will freeze until then to give the
614 // illusion of a synchronous change.
615 if (mState.mLoopCount > 0) { // only check if loop count is changing
616 getBufferPositionAndLoopCount(NULL, NULL); // get last position
617 }
618 mPosLoop.mBufferPosition = position;
619 if (position >= mState.mLoopEnd) {
620 // no ongoing loop is possible if position is greater than loopEnd.
621 mPosLoop.mLoopCount = 0;
622 }
Andy Hung9b461582014-12-01 17:56:29 -0800623 (void) mMutator.push(mState);
624}
625
626void StaticAudioTrackClientProxy::setBufferPositionAndLoop(size_t position, size_t loopStart,
627 size_t loopEnd, int loopCount)
628{
629 setLoop(loopStart, loopEnd, loopCount);
630 setBufferPosition(position);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800631}
632
633size_t StaticAudioTrackClientProxy::getBufferPosition()
634{
Andy Hung4ede21d2014-12-12 15:37:34 -0800635 getBufferPositionAndLoopCount(NULL, NULL);
636 return mPosLoop.mBufferPosition;
637}
638
639void StaticAudioTrackClientProxy::getBufferPositionAndLoopCount(
640 size_t *position, int *loopCount)
641{
642 if (mMutator.ack() == StaticAudioTrackSingleStateQueue::SSQ_DONE) {
643 if (mPosLoopObserver.poll(mPosLoop)) {
644 ; // a valid mPosLoop should be available if ackDone is true.
645 }
Glenn Kastena8190fc2012-12-03 17:06:56 -0800646 }
Andy Hung4ede21d2014-12-12 15:37:34 -0800647 if (position != NULL) {
648 *position = mPosLoop.mBufferPosition;
649 }
650 if (loopCount != NULL) {
651 *loopCount = mPosLoop.mLoopCount;
652 }
Glenn Kastena8190fc2012-12-03 17:06:56 -0800653}
654
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655// ---------------------------------------------------------------------------
656
657ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
658 size_t frameSize, bool isOut, bool clientInServer)
Glenn Kasten7db7df02013-06-25 16:13:23 -0700659 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
Andy Hungea2b9c02016-02-12 17:06:53 -0800660 mAvailToClient(0), mFlush(0), mReleased(0), mFlushed(0)
Andy Hung6ae58432016-02-16 18:32:24 -0800661 , mTimestampMutator(&cblk->mExtendedTimestampQueue)
Glenn Kastena8190fc2012-12-03 17:06:56 -0800662{
Phil Burke8972b02016-03-04 11:29:57 -0800663 cblk->mBufferSizeInFrames = frameCount;
Glenn Kastena8190fc2012-12-03 17:06:56 -0800664}
665
ilewis926b82f2016-03-29 14:50:36 -0700666__attribute__((no_sanitize("integer")))
Phil Burk4bb650b2016-09-09 12:11:17 -0700667void ServerProxy::flushBufferIfNeeded()
668{
669 audio_track_cblk_t* cblk = mCblk;
670 // The acquire_load is not really required. But since the write is a release_store in the
671 // client, using acquire_load here makes it easier for people to maintain the code,
672 // and the logic for communicating ipc variables seems somewhat standard,
673 // and there really isn't much penalty for 4 or 8 byte atomics.
674 int32_t flush = android_atomic_acquire_load(&cblk->u.mStreaming.mFlush);
675 if (flush != mFlush) {
676 ALOGV("ServerProxy::flushBufferIfNeeded() mStreaming.mFlush = 0x%x, mFlush = 0x%0x",
677 flush, mFlush);
Andy Hung1d3556d2018-03-29 16:30:14 -0700678 // shouldn't matter, but for range safety use mRear instead of getRear().
Phil Burk4bb650b2016-09-09 12:11:17 -0700679 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
680 int32_t front = cblk->u.mStreaming.mFront;
681
682 // effectively obtain then release whatever is in the buffer
683 const size_t overflowBit = mFrameCountP2 << 1;
684 const size_t mask = overflowBit - 1;
685 int32_t newFront = (front & ~mask) | (flush & mask);
686 ssize_t filled = rear - newFront;
687 if (filled >= (ssize_t)overflowBit) {
688 // front and rear offsets span the overflow bit of the p2 mask
689 // so rebasing newFront on the front offset is off by the overflow bit.
690 // adjust newFront to match rear offset.
691 ALOGV("flush wrap: filled %zx >= overflowBit %zx", filled, overflowBit);
692 newFront += overflowBit;
693 filled -= overflowBit;
694 }
695 // Rather than shutting down on a corrupt flush, just treat it as a full flush
696 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
697 ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, "
698 "filled %zd=%#x",
699 mFlush, flush, front, rear,
700 (unsigned)mask, newFront, filled, (unsigned)filled);
701 newFront = rear;
702 }
703 mFlush = flush;
704 android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront);
705 // There is no danger from a false positive, so err on the side of caution
706 if (true /*front != newFront*/) {
707 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
708 if (!(old & CBLK_FUTEX_WAKE)) {
709 (void) syscall(__NR_futex, &cblk->mFutex,
710 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
711 }
712 }
713 mFlushed += (newFront - front) & mask;
714 }
715}
716
717__attribute__((no_sanitize("integer")))
Andy Hung1d3556d2018-03-29 16:30:14 -0700718int32_t AudioTrackServerProxy::getRear() const
719{
720 const int32_t stop = android_atomic_acquire_load(&mCblk->u.mStreaming.mStop);
721 const int32_t rear = android_atomic_acquire_load(&mCblk->u.mStreaming.mRear);
722 const int32_t stopLast = mStopLast.load(std::memory_order_acquire);
723 if (stop != stopLast) {
724 const int32_t front = mCblk->u.mStreaming.mFront;
725 const size_t overflowBit = mFrameCountP2 << 1;
726 const size_t mask = overflowBit - 1;
727 int32_t newRear = (rear & ~mask) | (stop & mask);
728 ssize_t filled = newRear - front;
Andy Hung54274032018-04-19 18:16:44 -0700729 // overflowBit is unsigned, so cast to signed for comparison.
730 if (filled >= (ssize_t)overflowBit) {
Andy Hung1d3556d2018-03-29 16:30:14 -0700731 // front and rear offsets span the overflow bit of the p2 mask
Andy Hung54274032018-04-19 18:16:44 -0700732 // so rebasing newRear on the rear offset is off by the overflow bit.
Andy Hung1d3556d2018-03-29 16:30:14 -0700733 ALOGV("stop wrap: filled %zx >= overflowBit %zx", filled, overflowBit);
Andy Hung54274032018-04-19 18:16:44 -0700734 newRear -= overflowBit;
735 filled -= overflowBit;
Andy Hung1d3556d2018-03-29 16:30:14 -0700736 }
737 if (0 <= filled && (size_t) filled <= mFrameCount) {
738 // we're stopped, return the stop level as newRear
739 return newRear;
740 }
741
742 // A corrupt stop. Log error and ignore.
743 ALOGE("mStopLast %#x -> stop %#x, front %#x, rear %#x, mask %#x, newRear %#x, "
744 "filled %zd=%#x",
745 stopLast, stop, front, rear,
746 (unsigned)mask, newRear, filled, (unsigned)filled);
747 // Don't reset mStopLast as this is const.
748 }
749 return rear;
750}
751
752void AudioTrackServerProxy::start()
753{
754 mStopLast = android_atomic_acquire_load(&mCblk->u.mStreaming.mStop);
755}
756
757__attribute__((no_sanitize("integer")))
Glenn Kasten2e422c42013-10-18 13:00:29 -0700758status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759{
Andy Hung9c64f342017-08-02 18:10:00 -0700760 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0,
761 "%s: null or zero frame buffer, buffer:%p", __func__, buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800762 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700763 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800764 }
Glenn Kasten7db7df02013-06-25 16:13:23 -0700765 {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800766 audio_track_cblk_t* cblk = mCblk;
767 // compute number of frames available to write (AudioTrack) or read (AudioRecord),
768 // or use previous cached value from framesReady(), with added barrier if it omits.
769 int32_t front;
770 int32_t rear;
771 // See notes on barriers at ClientProxy::obtainBuffer()
772 if (mIsOut) {
Phil Burk4bb650b2016-09-09 12:11:17 -0700773 flushBufferIfNeeded(); // might modify mFront
Andy Hung1d3556d2018-03-29 16:30:14 -0700774 rear = getRear();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100775 front = cblk->u.mStreaming.mFront;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800776 } else {
777 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
778 rear = cblk->u.mStreaming.mRear;
779 }
780 ssize_t filled = rear - front;
781 // pipe should not already be overfull
782 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800783 ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); shutting down",
784 filled, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800785 mIsShutdown = true;
786 }
787 if (mIsShutdown) {
Glenn Kasten7db7df02013-06-25 16:13:23 -0700788 goto no_init;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800789 }
790 // don't allow filling pipe beyond the nominal size
791 size_t availToServer;
792 if (mIsOut) {
793 availToServer = filled;
794 mAvailToClient = mFrameCount - filled;
795 } else {
796 availToServer = mFrameCount - filled;
797 mAvailToClient = filled;
798 }
799 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
800 size_t part1;
801 if (mIsOut) {
802 front &= mFrameCountP2 - 1;
803 part1 = mFrameCountP2 - front;
804 } else {
805 rear &= mFrameCountP2 - 1;
806 part1 = mFrameCountP2 - rear;
807 }
808 if (part1 > availToServer) {
809 part1 = availToServer;
810 }
811 size_t ask = buffer->mFrameCount;
812 if (part1 > ask) {
813 part1 = ask;
814 }
815 // is assignment redundant in some cases?
816 buffer->mFrameCount = part1;
817 buffer->mRaw = part1 > 0 ?
818 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
819 buffer->mNonContig = availToServer - part1;
Glenn Kasten2e422c42013-10-18 13:00:29 -0700820 // After flush(), allow releaseBuffer() on a previously obtained buffer;
821 // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
822 if (!ackFlush) {
823 mUnreleased = part1;
824 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800825 return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
Glenn Kasten7db7df02013-06-25 16:13:23 -0700826 }
827no_init:
828 buffer->mFrameCount = 0;
829 buffer->mRaw = NULL;
830 buffer->mNonContig = 0;
831 mUnreleased = 0;
832 return NO_INIT;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800833}
834
ilewis926b82f2016-03-29 14:50:36 -0700835__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800836void ServerProxy::releaseBuffer(Buffer* buffer)
837{
Glenn Kasten7db7df02013-06-25 16:13:23 -0700838 LOG_ALWAYS_FATAL_IF(buffer == NULL);
839 size_t stepCount = buffer->mFrameCount;
840 if (stepCount == 0 || mIsShutdown) {
841 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800842 buffer->mFrameCount = 0;
843 buffer->mRaw = NULL;
844 buffer->mNonContig = 0;
845 return;
846 }
Andy Hung9c64f342017-08-02 18:10:00 -0700847 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount),
848 "%s: mUnreleased out of range, "
849 "!(stepCount:%zu <= mUnreleased:%zu <= mFrameCount:%zu)",
850 __func__, stepCount, mUnreleased, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800851 mUnreleased -= stepCount;
852 audio_track_cblk_t* cblk = mCblk;
853 if (mIsOut) {
854 int32_t front = cblk->u.mStreaming.mFront;
855 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
856 } else {
857 int32_t rear = cblk->u.mStreaming.mRear;
858 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
859 }
860
Glenn Kasten844f88c2014-05-09 13:38:09 -0700861 cblk->mServer += stepCount;
Andy Hung3f0c9022016-01-15 17:49:46 -0800862 mReleased += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800863
864 size_t half = mFrameCount / 2;
865 if (half == 0) {
866 half = 1;
867 }
Glenn Kastenfdac7c02014-01-28 11:03:28 -0800868 size_t minimum = (size_t) cblk->mMinimum;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800869 if (minimum == 0) {
870 minimum = mIsOut ? half : 1;
871 } else if (minimum > half) {
872 minimum = half;
873 }
Glenn Kasten93bb77d2013-06-24 12:10:45 -0700874 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
Glenn Kastence8828a2013-09-16 18:07:38 -0700875 if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700876 ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum);
Glenn Kasten0d09a9b2013-06-24 12:06:46 -0700877 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
878 if (!(old & CBLK_FUTEX_WAKE)) {
Elliott Hughesee499292014-05-21 17:55:51 -0700879 (void) syscall(__NR_futex, &cblk->mFutex,
880 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800881 }
882 }
883
884 buffer->mFrameCount = 0;
885 buffer->mRaw = NULL;
886 buffer->mNonContig = 0;
887}
888
889// ---------------------------------------------------------------------------
890
ilewis926b82f2016-03-29 14:50:36 -0700891__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800892size_t AudioTrackServerProxy::framesReady()
893{
894 LOG_ALWAYS_FATAL_IF(!mIsOut);
895
896 if (mIsShutdown) {
897 return 0;
898 }
899 audio_track_cblk_t* cblk = mCblk;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100900
901 int32_t flush = cblk->u.mStreaming.mFlush;
902 if (flush != mFlush) {
Glenn Kasten20f51b12014-10-30 10:43:19 -0700903 // FIXME should return an accurate value, but over-estimate is better than under-estimate
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100904 return mFrameCount;
905 }
Andy Hung1d3556d2018-03-29 16:30:14 -0700906 const int32_t rear = getRear();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800907 ssize_t filled = rear - cblk->u.mStreaming.mFront;
908 // pipe should not already be overfull
909 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
Glenn Kasten1bfe09a2017-02-21 13:05:56 -0800910 ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); shutting down",
911 filled, mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800912 mIsShutdown = true;
913 return 0;
914 }
915 // cache this value for later use by obtainBuffer(), with added barrier
916 // and racy if called by normal mixer thread
917 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
918 return filled;
919}
920
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700921__attribute__((no_sanitize("integer")))
922size_t AudioTrackServerProxy::framesReadySafe() const
923{
924 if (mIsShutdown) {
925 return 0;
926 }
927 const audio_track_cblk_t* cblk = mCblk;
928 const int32_t flush = android_atomic_acquire_load(&cblk->u.mStreaming.mFlush);
929 if (flush != mFlush) {
930 return mFrameCount;
931 }
Andy Hung1d3556d2018-03-29 16:30:14 -0700932 const int32_t rear = getRear();
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700933 const ssize_t filled = rear - cblk->u.mStreaming.mFront;
934 if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
935 return 0; // error condition, silently return 0.
936 }
937 return filled;
938}
939
Eric Laurentbfb1b832013-01-07 09:53:42 -0800940bool AudioTrackServerProxy::setStreamEndDone() {
Glenn Kasten844f88c2014-05-09 13:38:09 -0700941 audio_track_cblk_t* cblk = mCblk;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800942 bool old =
Glenn Kasten844f88c2014-05-09 13:38:09 -0700943 (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800944 if (!old) {
Elliott Hughese348c5b2014-05-21 18:47:50 -0700945 (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
Elliott Hughesee499292014-05-21 17:55:51 -0700946 1);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800947 }
948 return old;
949}
950
Andy Hungd4ee4db2017-07-12 15:26:04 -0700951__attribute__((no_sanitize("integer")))
Glenn Kasten82aaf942013-07-17 16:05:07 -0700952void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
953{
Glenn Kasten844f88c2014-05-09 13:38:09 -0700954 audio_track_cblk_t* cblk = mCblk;
Phil Burk2812d9e2016-01-04 10:34:30 -0800955 if (frameCount > 0) {
956 cblk->u.mStreaming.mUnderrunFrames += frameCount;
Glenn Kasten82aaf942013-07-17 16:05:07 -0700957
Phil Burk2812d9e2016-01-04 10:34:30 -0800958 if (!mUnderrunning) { // start of underrun?
959 mUnderrunCount++;
960 cblk->u.mStreaming.mUnderrunCount = mUnderrunCount;
961 mUnderrunning = true;
962 ALOGV("tallyUnderrunFrames(%3u) at uf = %u, bump mUnderrunCount = %u",
963 frameCount, cblk->u.mStreaming.mUnderrunFrames, mUnderrunCount);
964 }
965
966 // FIXME also wake futex so that underrun is noticed more quickly
967 (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
968 } else {
969 ALOGV_IF(mUnderrunning,
970 "tallyUnderrunFrames(%3u) at uf = %u, underrun finished",
971 frameCount, cblk->u.mStreaming.mUnderrunFrames);
972 mUnderrunning = false; // so we can detect the next edge
973 }
Glenn Kasten82aaf942013-07-17 16:05:07 -0700974}
975
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700976AudioPlaybackRate AudioTrackServerProxy::getPlaybackRate()
Andy Hung8edb8dc2015-03-26 19:13:55 -0700977{ // do not call from multiple threads without holding lock
Ricardo Garcia5a8a95d2015-04-18 14:47:04 -0700978 mPlaybackRateObserver.poll(mPlaybackRate);
979 return mPlaybackRate;
Andy Hung8edb8dc2015-03-26 19:13:55 -0700980}
981
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800982// ---------------------------------------------------------------------------
983
984StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
985 size_t frameCount, size_t frameSize)
986 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
Andy Hung4ede21d2014-12-12 15:37:34 -0800987 mObserver(&cblk->u.mStatic.mSingleStateQueue),
988 mPosLoopMutator(&cblk->u.mStatic.mPosLoopQueue),
Andy Hungcb2129b2014-11-11 12:17:22 -0800989 mFramesReadySafe(frameCount), mFramesReady(frameCount),
990 mFramesReadyIsCalledByMultipleThreads(false)
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800991{
Andy Hung9b461582014-12-01 17:56:29 -0800992 memset(&mState, 0, sizeof(mState));
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800993}
994
995void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
996{
997 mFramesReadyIsCalledByMultipleThreads = true;
998}
999
1000size_t StaticAudioTrackServerProxy::framesReady()
1001{
Andy Hungcb2129b2014-11-11 12:17:22 -08001002 // Can't call pollPosition() from multiple threads.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001003 if (!mFramesReadyIsCalledByMultipleThreads) {
Andy Hungcb2129b2014-11-11 12:17:22 -08001004 (void) pollPosition();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001005 }
Andy Hungcb2129b2014-11-11 12:17:22 -08001006 return mFramesReadySafe;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001007}
1008
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001009size_t StaticAudioTrackServerProxy::framesReadySafe() const
1010{
1011 return mFramesReadySafe;
1012}
1013
Andy Hung9b461582014-12-01 17:56:29 -08001014status_t StaticAudioTrackServerProxy::updateStateWithLoop(
1015 StaticAudioTrackState *localState, const StaticAudioTrackState &update) const
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001016{
Andy Hung9b461582014-12-01 17:56:29 -08001017 if (localState->mLoopSequence != update.mLoopSequence) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001018 bool valid = false;
Andy Hung9b461582014-12-01 17:56:29 -08001019 const size_t loopStart = update.mLoopStart;
1020 const size_t loopEnd = update.mLoopEnd;
1021 size_t position = localState->mPosition;
1022 if (update.mLoopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001023 valid = true;
Andy Hung9b461582014-12-01 17:56:29 -08001024 } else if (update.mLoopCount >= -1) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001025 if (loopStart < loopEnd && loopEnd <= mFrameCount &&
1026 loopEnd - loopStart >= MIN_LOOP) {
Andy Hung680b7952014-11-12 13:18:52 -08001027 // If the current position is greater than the end of the loop
1028 // we "wrap" to the loop start. This might cause an audible pop.
1029 if (position >= loopEnd) {
Andy Hung9b461582014-12-01 17:56:29 -08001030 position = loopStart;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001031 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001032 valid = true;
1033 }
1034 }
Andy Hung9b461582014-12-01 17:56:29 -08001035 if (!valid || position > mFrameCount) {
1036 return NO_INIT;
1037 }
1038 localState->mPosition = position;
1039 localState->mLoopCount = update.mLoopCount;
1040 localState->mLoopEnd = loopEnd;
1041 localState->mLoopStart = loopStart;
1042 localState->mLoopSequence = update.mLoopSequence;
1043 }
1044 return OK;
1045}
1046
1047status_t StaticAudioTrackServerProxy::updateStateWithPosition(
1048 StaticAudioTrackState *localState, const StaticAudioTrackState &update) const
1049{
1050 if (localState->mPositionSequence != update.mPositionSequence) {
1051 if (update.mPosition > mFrameCount) {
1052 return NO_INIT;
1053 } else if (localState->mLoopCount != 0 && update.mPosition >= localState->mLoopEnd) {
1054 localState->mLoopCount = 0; // disable loop count if position is beyond loop end.
1055 }
1056 localState->mPosition = update.mPosition;
1057 localState->mPositionSequence = update.mPositionSequence;
1058 }
1059 return OK;
1060}
1061
1062ssize_t StaticAudioTrackServerProxy::pollPosition()
1063{
1064 StaticAudioTrackState state;
1065 if (mObserver.poll(state)) {
1066 StaticAudioTrackState trystate = mState;
1067 bool result;
Chad Brubakercb50c542015-10-07 14:20:10 -07001068 const int32_t diffSeq = (int32_t) state.mLoopSequence - (int32_t) state.mPositionSequence;
Andy Hung9b461582014-12-01 17:56:29 -08001069
1070 if (diffSeq < 0) {
1071 result = updateStateWithLoop(&trystate, state) == OK &&
1072 updateStateWithPosition(&trystate, state) == OK;
1073 } else {
1074 result = updateStateWithPosition(&trystate, state) == OK &&
1075 updateStateWithLoop(&trystate, state) == OK;
1076 }
1077 if (!result) {
Andy Hung4ede21d2014-12-12 15:37:34 -08001078 mObserver.done();
Andy Hung9b461582014-12-01 17:56:29 -08001079 // caution: no update occurs so server state will be inconsistent with client state.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001080 ALOGE("%s client pushed an invalid state, shutting down", __func__);
1081 mIsShutdown = true;
1082 return (ssize_t) NO_INIT;
1083 }
Andy Hung9b461582014-12-01 17:56:29 -08001084 mState = trystate;
1085 if (mState.mLoopCount == -1) {
1086 mFramesReady = INT64_MAX;
1087 } else if (mState.mLoopCount == 0) {
1088 mFramesReady = mFrameCount - mState.mPosition;
1089 } else if (mState.mLoopCount > 0) {
1090 // TODO: Later consider fixing overflow, but does not seem needed now
1091 // as will not overflow if loopStart and loopEnd are Java "ints".
1092 mFramesReady = int64_t(mState.mLoopCount) * (mState.mLoopEnd - mState.mLoopStart)
1093 + mFrameCount - mState.mPosition;
1094 }
Andy Hungcb2129b2014-11-11 12:17:22 -08001095 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kastenfdac7c02014-01-28 11:03:28 -08001096 // This may overflow, but client is not supposed to rely on it
Andy Hung4ede21d2014-12-12 15:37:34 -08001097 StaticAudioTrackPosLoop posLoop;
1098
1099 posLoop.mLoopCount = (int32_t) mState.mLoopCount;
1100 posLoop.mBufferPosition = (uint32_t) mState.mPosition;
1101 mPosLoopMutator.push(posLoop);
1102 mObserver.done(); // safe to read mStatic variables.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001103 }
Andy Hung9b461582014-12-01 17:56:29 -08001104 return (ssize_t) mState.mPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001105}
1106
Andy Hungd4ee4db2017-07-12 15:26:04 -07001107__attribute__((no_sanitize("integer")))
Andy Hung954ca452015-09-09 14:39:02 -07001108status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001109{
1110 if (mIsShutdown) {
1111 buffer->mFrameCount = 0;
1112 buffer->mRaw = NULL;
1113 buffer->mNonContig = 0;
1114 mUnreleased = 0;
1115 return NO_INIT;
1116 }
1117 ssize_t positionOrStatus = pollPosition();
1118 if (positionOrStatus < 0) {
1119 buffer->mFrameCount = 0;
1120 buffer->mRaw = NULL;
1121 buffer->mNonContig = 0;
1122 mUnreleased = 0;
1123 return (status_t) positionOrStatus;
1124 }
1125 size_t position = (size_t) positionOrStatus;
Andy Hungcb2129b2014-11-11 12:17:22 -08001126 size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001127 size_t avail;
Andy Hungcb2129b2014-11-11 12:17:22 -08001128 if (position < end) {
1129 avail = end - position;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001130 size_t wanted = buffer->mFrameCount;
1131 if (avail < wanted) {
1132 buffer->mFrameCount = avail;
1133 } else {
1134 avail = wanted;
1135 }
1136 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
1137 } else {
1138 avail = 0;
1139 buffer->mFrameCount = 0;
1140 buffer->mRaw = NULL;
1141 }
Andy Hungcb2129b2014-11-11 12:17:22 -08001142 // As mFramesReady is the total remaining frames in the static audio track,
1143 // it is always larger or equal to avail.
Andy Hung9c64f342017-08-02 18:10:00 -07001144 LOG_ALWAYS_FATAL_IF(mFramesReady < (int64_t) avail,
1145 "%s: mFramesReady out of range, mFramesReady:%lld < avail:%zu",
1146 __func__, (long long)mFramesReady, avail);
Andy Hungcb2129b2014-11-11 12:17:22 -08001147 buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail);
Andy Hung954ca452015-09-09 14:39:02 -07001148 if (!ackFlush) {
1149 mUnreleased = avail;
1150 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001151 return NO_ERROR;
1152}
1153
Andy Hungd4ee4db2017-07-12 15:26:04 -07001154__attribute__((no_sanitize("integer")))
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001155void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
1156{
1157 size_t stepCount = buffer->mFrameCount;
Andy Hung9c64f342017-08-02 18:10:00 -07001158 LOG_ALWAYS_FATAL_IF(!((int64_t) stepCount <= mFramesReady),
1159 "%s: stepCount out of range, "
1160 "!(stepCount:%zu <= mFramesReady:%lld)",
1161 __func__, stepCount, (long long)mFramesReady);
1162 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased),
1163 "%s: stepCount out of range, "
1164 "!(stepCount:%zu <= mUnreleased:%zu)",
1165 __func__, stepCount, mUnreleased);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001166 if (stepCount == 0) {
Glenn Kasten7db7df02013-06-25 16:13:23 -07001167 // prevent accidental re-use of buffer
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001168 buffer->mRaw = NULL;
1169 buffer->mNonContig = 0;
1170 return;
1171 }
1172 mUnreleased -= stepCount;
1173 audio_track_cblk_t* cblk = mCblk;
Andy Hung9b461582014-12-01 17:56:29 -08001174 size_t position = mState.mPosition;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001175 size_t newPosition = position + stepCount;
1176 int32_t setFlags = 0;
1177 if (!(position <= newPosition && newPosition <= mFrameCount)) {
Glenn Kastenb187de12014-12-30 08:18:15 -08001178 ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position,
1179 mFrameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001180 newPosition = mFrameCount;
1181 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
Andy Hungcb2129b2014-11-11 12:17:22 -08001182 newPosition = mState.mLoopStart;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001183 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 setFlags = CBLK_LOOP_CYCLE;
1185 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001186 setFlags = CBLK_LOOP_FINAL;
1187 }
1188 }
1189 if (newPosition == mFrameCount) {
1190 setFlags |= CBLK_BUFFER_END;
1191 }
Andy Hung9b461582014-12-01 17:56:29 -08001192 mState.mPosition = newPosition;
Andy Hungcb2129b2014-11-11 12:17:22 -08001193 if (mFramesReady != INT64_MAX) {
1194 mFramesReady -= stepCount;
1195 }
1196 mFramesReadySafe = clampToSize(mFramesReady);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001197
Glenn Kastenf20e1d82013-07-12 09:45:18 -07001198 cblk->mServer += stepCount;
Andy Hung3f0c9022016-01-15 17:49:46 -08001199 mReleased += stepCount;
1200
Glenn Kastenfdac7c02014-01-28 11:03:28 -08001201 // This may overflow, but client is not supposed to rely on it
Andy Hung4ede21d2014-12-12 15:37:34 -08001202 StaticAudioTrackPosLoop posLoop;
1203 posLoop.mBufferPosition = mState.mPosition;
1204 posLoop.mLoopCount = mState.mLoopCount;
1205 mPosLoopMutator.push(posLoop);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001206 if (setFlags != 0) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001207 (void) android_atomic_or(setFlags, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001208 // this would be a good place to wake a futex
1209 }
1210
1211 buffer->mFrameCount = 0;
1212 buffer->mRaw = NULL;
1213 buffer->mNonContig = 0;
1214}
1215
Phil Burk2812d9e2016-01-04 10:34:30 -08001216void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
Glenn Kasten82aaf942013-07-17 16:05:07 -07001217{
1218 // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
1219 // we don't have a location to count underrun frames. The underrun frame counter
1220 // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not
1221 // possible for static buffer tracks other than at end of buffer, so this is not a loss.
1222
1223 // FIXME also wake futex so that underrun is noticed more quickly
Phil Burk2812d9e2016-01-04 10:34:30 -08001224 if (frameCount > 0) {
1225 (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
1226 }
Glenn Kasten82aaf942013-07-17 16:05:07 -07001227}
1228
Andy Hung1d3556d2018-03-29 16:30:14 -07001229int32_t StaticAudioTrackServerProxy::getRear() const
1230{
1231 LOG_ALWAYS_FATAL("getRear() not permitted for static tracks");
1232 return 0;
1233}
1234
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001235// ---------------------------------------------------------------------------
1236
Glenn Kastena8190fc2012-12-03 17:06:56 -08001237} // namespace android