1 /*
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>
22 
23 #include <linux/futex.h>
24 #include <sys/syscall.h>
25 
26 namespace android {
27 
28 // used to clamp a value to size_t.  TODO: move to another file.
29 template <typename T>
clampToSize(T x)30 size_t clampToSize(T x) {
31     return sizeof(T) > sizeof(size_t) && x > (T) SIZE_MAX ? SIZE_MAX : x < 0 ? 0 : (size_t) x;
32 }
33 
34 // 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.
39 __attribute__((no_sanitize("integer")))
incrementSequence(uint32_t self,uint32_t other)40 static uint32_t incrementSequence(uint32_t self, uint32_t other) {
41     int32_t diff = (int32_t) self - (int32_t) other;
42     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 
audio_track_cblk_t()48 audio_track_cblk_t::audio_track_cblk_t()
49     : mServer(0), mFutex(0), mMinimum(0)
50     , mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0)
51     , mBufferSizeInFrames(0)
52     , mFlags(0)
53 {
54     memset(&u, 0, sizeof(u));
55 }
56 
57 // ---------------------------------------------------------------------------
58 
Proxy(audio_track_cblk_t * cblk,void * buffers,size_t frameCount,size_t frameSize,bool isOut,bool clientInServer)59 Proxy::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),
63       mIsShutdown(false), mUnreleased(0)
64 {
65 }
66 
67 // ---------------------------------------------------------------------------
68 
ClientProxy(audio_track_cblk_t * cblk,void * buffers,size_t frameCount,size_t frameSize,bool isOut,bool clientInServer)69 ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
70         size_t frameSize, bool isOut, bool clientInServer)
71     : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer)
72     , mEpoch(0)
73     , mTimestampObserver(&cblk->mExtendedTimestampQueue)
74 {
75     setBufferSizeInFrames(frameCount);
76 }
77 
78 const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/};
79 const 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 
setBufferSizeInFrames(uint32_t size)89 uint32_t ClientProxy::setBufferSizeInFrames(uint32_t size)
90 {
91     // The minimum should be  greater than zero and less than the size
92     // at which underruns will occur.
93     const uint32_t minimum = 16; // based on AudioMixer::BLOCKSIZE
94     const uint32_t maximum = frameCount();
95     uint32_t clippedSize = size;
96     if (maximum < minimum) {
97         clippedSize = maximum;
98     } else if (clippedSize < minimum) {
99         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 
110 __attribute__((no_sanitize("integer")))
obtainBuffer(Buffer * buffer,const struct timespec * requested,struct timespec * elapsed)111 status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested,
112         struct timespec *elapsed)
113 {
114     LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0,
115             "%s: null or zero frame buffer, buffer:%p", __func__, buffer);
116     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
120 
121     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;
134     } else {
135         timeout = TIMEOUT_FINITE;
136         if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) {
137             measure = true;
138         }
139     }
140     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 (;;) {
150         int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags);
151         // 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         }
157         if (flags & CBLK_DISABLED) {
158             ALOGV("Track disabled");
159             status = NOT_ENOUGH_DATA;
160             goto end;
161         }
162         // 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;
182         } else {
183             // 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         }
187         // write to rear, read from front
188         ssize_t filled = rear - front;
189         // pipe should not be overfull
190         if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
191             if (mIsOut) {
192                 ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); "
193                         "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);
202         }
203         // 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.
207         ssize_t adjustableSize = (ssize_t) getBufferSizeInFrames();
208         ssize_t avail =  (mIsOut) ? adjustableSize - filled : filled;
209         if (avail < 0) {
210             avail = 0;
211         } else if (avail > 0) {
212             // 'avail' may be non-contiguous, so return only the first contiguous chunk
213             size_t part1;
214             if (mIsOut) {
215                 rear &= mFrameCountP2 - 1;
216                 part1 = mFrameCountP2 - rear;
217             } else {
218                 front &= mFrameCountP2 - 1;
219                 part1 = mFrameCountP2 - front;
220             }
221             if (part1 > (size_t)avail) {
222                 part1 = avail;
223             }
224             if (part1 > buffer->mFrameCount) {
225                 part1 = buffer->mFrameCount;
226             }
227             buffer->mFrameCount = part1;
228             buffer->mRaw = part1 > 0 ?
229                     &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL;
230             buffer->mNonContig = avail - part1;
231             mUnreleased = part1;
232             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:
270             LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout);
271             ts = NULL;
272             break;
273         }
274         int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
275         if (!(old & CBLK_FUTEX_WAKE)) {
276             if (measure && !beforeIsValid) {
277                 clock_gettime(CLOCK_MONOTONIC, &before);
278                 beforeIsValid = true;
279             }
280             errno = 0;
281             (void) syscall(__NR_futex, &cblk->mFutex,
282                     mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
283             status_t error = errno; // clock_gettime can affect errno
284             // 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;
289                 long deltaNs = after.tv_nsec - before.tv_nsec;
290                 if (deltaNs < 0) {
291                     deltaNs += 1000000000;
292                     total.tv_sec--;
293                 }
294                 if ((total.tv_nsec += deltaNs) >= 1000000000) {
295                     total.tv_nsec -= 1000000000;
296                     total.tv_sec++;
297                 }
298                 before = after;
299                 beforeIsValid = true;
300             }
301             switch (error) {
302             case 0:            // normal wakeup by server, or by binderDied()
303             case EWOULDBLOCK:  // benign race condition with server
304             case EINTR:        // wait was interrupted by signal or other spurious wakeup
305             case ETIMEDOUT:    // time-out expired
306                 // FIXME these error/non-0 status are being dropped
307                 break;
308             default:
309                 status = error;
310                 ALOGE("%s unexpected error %s", __func__, strerror(status));
311                 goto end;
312             }
313         }
314     }
315 
316 end:
317     if (status != NO_ERROR) {
318         buffer->mFrameCount = 0;
319         buffer->mRaw = NULL;
320         buffer->mNonContig = 0;
321         mUnreleased = 0;
322     }
323     if (elapsed != NULL) {
324         *elapsed = total;
325     }
326     if (requested == NULL) {
327         requested = &kNonBlocking;
328     }
329     if (measure) {
330         ALOGV("requested %ld.%03ld elapsed %ld.%03ld",
331               requested->tv_sec, requested->tv_nsec / 1000000,
332               total.tv_sec, total.tv_nsec / 1000000);
333     }
334     return status;
335 }
336 
337 __attribute__((no_sanitize("integer")))
releaseBuffer(Buffer * buffer)338 void ClientProxy::releaseBuffer(Buffer* buffer)
339 {
340     LOG_ALWAYS_FATAL_IF(buffer == NULL);
341     size_t stepCount = buffer->mFrameCount;
342     if (stepCount == 0 || mIsShutdown) {
343         // prevent accidental re-use of buffer
344         buffer->mFrameCount = 0;
345         buffer->mRaw = NULL;
346         buffer->mNonContig = 0;
347         return;
348     }
349     LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount),
350             "%s: mUnreleased out of range, "
351             "!(stepCount:%zu <= mUnreleased:%zu <= mFrameCount:%zu), BufferSizeInFrames:%u",
352             __func__, stepCount, mUnreleased, mFrameCount, getBufferSizeInFrames());
353     mUnreleased -= stepCount;
354     audio_track_cblk_t* cblk = mCblk;
355     // Both of these barriers are required
356     if (mIsOut) {
357         int32_t rear = cblk->u.mStreaming.mRear;
358         android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
359     } else {
360         int32_t front = cblk->u.mStreaming.mFront;
361         android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
362     }
363 }
364 
binderDied()365 void ClientProxy::binderDied()
366 {
367     audio_track_cblk_t* cblk = mCblk;
368     if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) {
369         android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
370         // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process
371         (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
372                 1);
373     }
374 }
375 
interrupt()376 void ClientProxy::interrupt()
377 {
378     audio_track_cblk_t* cblk = mCblk;
379     if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) {
380         android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
381         (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
382                 1);
383     }
384 }
385 
386 __attribute__((no_sanitize("integer")))
getMisalignment()387 size_t ClientProxy::getMisalignment()
388 {
389     audio_track_cblk_t* cblk = mCblk;
390     return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) &
391             (mFrameCountP2 - 1);
392 }
393 
394 // ---------------------------------------------------------------------------
395 
flush()396 void AudioTrackClientProxy::flush()
397 {
398     sendStreamingFlushStop(true /* flush */);
399 }
400 
stop()401 void AudioTrackClientProxy::stop()
402 {
403     sendStreamingFlushStop(false /* flush */);
404 }
405 
406 // Sets the client-written mFlush and mStop positions, which control server behavior.
407 //
408 // @param flush indicates whether the operation is a flush or stop.
409 // A client stop sets mStop to the current write position;
410 // the server will not read past this point until start() or subsequent flush().
411 // A client flush sets both mStop and mFlush to the current write position.
412 // This advances the server read limit (if previously set) and on the next
413 // server read advances the server read position to this limit.
414 //
sendStreamingFlushStop(bool flush)415 void AudioTrackClientProxy::sendStreamingFlushStop(bool flush)
416 {
417     // TODO: Replace this by 64 bit counters - avoids wrap complication.
418     // This works for mFrameCountP2 <= 2^30
419     // mFlush is 32 bits concatenated as [ flush_counter ] [ newfront_offset ]
420     // Should newFlush = cblk->u.mStreaming.mRear?  Only problem is
421     // if you want to flush twice to the same rear location after a 32 bit wrap.
422 
423     const size_t increment = mFrameCountP2 << 1;
424     const size_t mask = increment - 1;
425     // No need for client atomic synchronization on mRear, mStop, mFlush
426     // as AudioTrack client only read/writes to them under client lock. Server only reads.
427     const int32_t rearMasked = mCblk->u.mStreaming.mRear & mask;
428 
429     // update stop before flush so that the server front
430     // never advances beyond a (potential) previous stop's rear limit.
431     int32_t stopBits; // the following add can overflow
432     __builtin_add_overflow(mCblk->u.mStreaming.mStop & ~mask, increment, &stopBits);
433     android_atomic_release_store(rearMasked | stopBits, &mCblk->u.mStreaming.mStop);
434 
435     if (flush) {
436         int32_t flushBits; // the following add can overflow
437         __builtin_add_overflow(mCblk->u.mStreaming.mFlush & ~mask, increment, &flushBits);
438         android_atomic_release_store(rearMasked | flushBits, &mCblk->u.mStreaming.mFlush);
439     }
440 }
441 
clearStreamEndDone()442 bool AudioTrackClientProxy::clearStreamEndDone() {
443     return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
444 }
445 
getStreamEndDone() const446 bool AudioTrackClientProxy::getStreamEndDone() const {
447     return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0;
448 }
449 
waitStreamEndDone(const struct timespec * requested)450 status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested)
451 {
452     struct timespec total;          // total elapsed time spent waiting
453     total.tv_sec = 0;
454     total.tv_nsec = 0;
455     audio_track_cblk_t* cblk = mCblk;
456     status_t status;
457     enum {
458         TIMEOUT_ZERO,       // requested == NULL || *requested == 0
459         TIMEOUT_INFINITE,   // *requested == infinity
460         TIMEOUT_FINITE,     // 0 < *requested < infinity
461         TIMEOUT_CONTINUE,   // additional chances after TIMEOUT_FINITE
462     } timeout;
463     if (requested == NULL) {
464         timeout = TIMEOUT_ZERO;
465     } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) {
466         timeout = TIMEOUT_ZERO;
467     } else if (requested->tv_sec == INT_MAX) {
468         timeout = TIMEOUT_INFINITE;
469     } else {
470         timeout = TIMEOUT_FINITE;
471     }
472     for (;;) {
473         int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags);
474         // check for track invalidation by server, or server death detection
475         if (flags & CBLK_INVALID) {
476             ALOGV("Track invalidated");
477             status = DEAD_OBJECT;
478             goto end;
479         }
480         // a track is not supposed to underrun at this stage but consider it done
481         if (flags & (CBLK_STREAM_END_DONE | CBLK_DISABLED)) {
482             ALOGV("stream end received");
483             status = NO_ERROR;
484             goto end;
485         }
486         // check for obtainBuffer interrupted by client
487         if (flags & CBLK_INTERRUPT) {
488             ALOGV("waitStreamEndDone() interrupted by client");
489             status = -EINTR;
490             goto end;
491         }
492         struct timespec remaining;
493         const struct timespec *ts;
494         switch (timeout) {
495         case TIMEOUT_ZERO:
496             status = WOULD_BLOCK;
497             goto end;
498         case TIMEOUT_INFINITE:
499             ts = NULL;
500             break;
501         case TIMEOUT_FINITE:
502             timeout = TIMEOUT_CONTINUE;
503             if (MAX_SEC == 0) {
504                 ts = requested;
505                 break;
506             }
507             // fall through
508         case TIMEOUT_CONTINUE:
509             // FIXME we do not retry if requested < 10ms? needs documentation on this state machine
510             if (requested->tv_sec < total.tv_sec ||
511                     (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) {
512                 status = TIMED_OUT;
513                 goto end;
514             }
515             remaining.tv_sec = requested->tv_sec - total.tv_sec;
516             if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) {
517                 remaining.tv_nsec += 1000000000;
518                 remaining.tv_sec++;
519             }
520             if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) {
521                 remaining.tv_sec = MAX_SEC;
522                 remaining.tv_nsec = 0;
523             }
524             ts = &remaining;
525             break;
526         default:
527             LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout);
528             ts = NULL;
529             break;
530         }
531         int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex);
532         if (!(old & CBLK_FUTEX_WAKE)) {
533             errno = 0;
534             (void) syscall(__NR_futex, &cblk->mFutex,
535                     mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts);
536             switch (errno) {
537             case 0:            // normal wakeup by server, or by binderDied()
538             case EWOULDBLOCK:  // benign race condition with server
539             case EINTR:        // wait was interrupted by signal or other spurious wakeup
540             case ETIMEDOUT:    // time-out expired
541                 break;
542             default:
543                 status = errno;
544                 ALOGE("%s unexpected error %s", __func__, strerror(status));
545                 goto end;
546             }
547         }
548     }
549 
550 end:
551     if (requested == NULL) {
552         requested = &kNonBlocking;
553     }
554     return status;
555 }
556 
557 // ---------------------------------------------------------------------------
558 
StaticAudioTrackClientProxy(audio_track_cblk_t * cblk,void * buffers,size_t frameCount,size_t frameSize)559 StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers,
560         size_t frameCount, size_t frameSize)
561     : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize),
562       mMutator(&cblk->u.mStatic.mSingleStateQueue),
563       mPosLoopObserver(&cblk->u.mStatic.mPosLoopQueue)
564 {
565     memset(&mState, 0, sizeof(mState));
566     memset(&mPosLoop, 0, sizeof(mPosLoop));
567 }
568 
flush()569 void StaticAudioTrackClientProxy::flush()
570 {
571     LOG_ALWAYS_FATAL("static flush");
572 }
573 
stop()574 void StaticAudioTrackClientProxy::stop()
575 {
576     ; // no special handling required for static tracks.
577 }
578 
setLoop(size_t loopStart,size_t loopEnd,int loopCount)579 void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount)
580 {
581     // This can only happen on a 64-bit client
582     if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) {
583         // FIXME Should return an error status
584         return;
585     }
586     mState.mLoopStart = (uint32_t) loopStart;
587     mState.mLoopEnd = (uint32_t) loopEnd;
588     mState.mLoopCount = loopCount;
589     mState.mLoopSequence = incrementSequence(mState.mLoopSequence, mState.mPositionSequence);
590     // set patch-up variables until the mState is acknowledged by the ServerProxy.
591     // observed buffer position and loop count will freeze until then to give the
592     // illusion of a synchronous change.
593     getBufferPositionAndLoopCount(NULL, NULL);
594     // preserve behavior to restart at mState.mLoopStart if position exceeds mState.mLoopEnd.
595     if (mState.mLoopCount != 0 && mPosLoop.mBufferPosition >= mState.mLoopEnd) {
596         mPosLoop.mBufferPosition = mState.mLoopStart;
597     }
598     mPosLoop.mLoopCount = mState.mLoopCount;
599     (void) mMutator.push(mState);
600 }
601 
setBufferPosition(size_t position)602 void StaticAudioTrackClientProxy::setBufferPosition(size_t position)
603 {
604     // This can only happen on a 64-bit client
605     if (position > UINT32_MAX) {
606         // FIXME Should return an error status
607         return;
608     }
609     mState.mPosition = (uint32_t) position;
610     mState.mPositionSequence = incrementSequence(mState.mPositionSequence, mState.mLoopSequence);
611     // set patch-up variables until the mState is acknowledged by the ServerProxy.
612     // observed buffer position and loop count will freeze until then to give the
613     // illusion of a synchronous change.
614     if (mState.mLoopCount > 0) {  // only check if loop count is changing
615         getBufferPositionAndLoopCount(NULL, NULL); // get last position
616     }
617     mPosLoop.mBufferPosition = position;
618     if (position >= mState.mLoopEnd) {
619         // no ongoing loop is possible if position is greater than loopEnd.
620         mPosLoop.mLoopCount = 0;
621     }
622     (void) mMutator.push(mState);
623 }
624 
setBufferPositionAndLoop(size_t position,size_t loopStart,size_t loopEnd,int loopCount)625 void StaticAudioTrackClientProxy::setBufferPositionAndLoop(size_t position, size_t loopStart,
626         size_t loopEnd, int loopCount)
627 {
628     setLoop(loopStart, loopEnd, loopCount);
629     setBufferPosition(position);
630 }
631 
getBufferPosition()632 size_t StaticAudioTrackClientProxy::getBufferPosition()
633 {
634     getBufferPositionAndLoopCount(NULL, NULL);
635     return mPosLoop.mBufferPosition;
636 }
637 
getBufferPositionAndLoopCount(size_t * position,int * loopCount)638 void StaticAudioTrackClientProxy::getBufferPositionAndLoopCount(
639         size_t *position, int *loopCount)
640 {
641     if (mMutator.ack() == StaticAudioTrackSingleStateQueue::SSQ_DONE) {
642          if (mPosLoopObserver.poll(mPosLoop)) {
643              ; // a valid mPosLoop should be available if ackDone is true.
644          }
645     }
646     if (position != NULL) {
647         *position = mPosLoop.mBufferPosition;
648     }
649     if (loopCount != NULL) {
650         *loopCount = mPosLoop.mLoopCount;
651     }
652 }
653 
654 // ---------------------------------------------------------------------------
655 
ServerProxy(audio_track_cblk_t * cblk,void * buffers,size_t frameCount,size_t frameSize,bool isOut,bool clientInServer)656 ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount,
657         size_t frameSize, bool isOut, bool clientInServer)
658     : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer),
659       mAvailToClient(0), mFlush(0), mReleased(0), mFlushed(0)
660     , mTimestampMutator(&cblk->mExtendedTimestampQueue)
661 {
662     cblk->mBufferSizeInFrames = frameCount;
663 }
664 
665 __attribute__((no_sanitize("integer")))
flushBufferIfNeeded()666 void ServerProxy::flushBufferIfNeeded()
667 {
668     audio_track_cblk_t* cblk = mCblk;
669     // The acquire_load is not really required. But since the write is a release_store in the
670     // client, using acquire_load here makes it easier for people to maintain the code,
671     // and the logic for communicating ipc variables seems somewhat standard,
672     // and there really isn't much penalty for 4 or 8 byte atomics.
673     int32_t flush = android_atomic_acquire_load(&cblk->u.mStreaming.mFlush);
674     if (flush != mFlush) {
675         ALOGV("ServerProxy::flushBufferIfNeeded() mStreaming.mFlush = 0x%x, mFlush = 0x%0x",
676                 flush, mFlush);
677         // shouldn't matter, but for range safety use mRear instead of getRear().
678         int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear);
679         int32_t front = cblk->u.mStreaming.mFront;
680 
681         // effectively obtain then release whatever is in the buffer
682         const size_t overflowBit = mFrameCountP2 << 1;
683         const size_t mask = overflowBit - 1;
684         int32_t newFront = (front & ~mask) | (flush & mask);
685         ssize_t filled = rear - newFront;
686         if (filled >= (ssize_t)overflowBit) {
687             // front and rear offsets span the overflow bit of the p2 mask
688             // so rebasing newFront on the front offset is off by the overflow bit.
689             // adjust newFront to match rear offset.
690             ALOGV("flush wrap: filled %zx >= overflowBit %zx", filled, overflowBit);
691             newFront += overflowBit;
692             filled -= overflowBit;
693         }
694         // Rather than shutting down on a corrupt flush, just treat it as a full flush
695         if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
696             ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, "
697                     "filled %zd=%#x",
698                     mFlush, flush, front, rear,
699                     (unsigned)mask, newFront, filled, (unsigned)filled);
700             newFront = rear;
701         }
702         mFlush = flush;
703         android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront);
704         // There is no danger from a false positive, so err on the side of caution
705         if (true /*front != newFront*/) {
706             int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
707             if (!(old & CBLK_FUTEX_WAKE)) {
708                 (void) syscall(__NR_futex, &cblk->mFutex,
709                         mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
710             }
711         }
712         mFlushed += (newFront - front) & mask;
713     }
714 }
715 
716 __attribute__((no_sanitize("integer")))
getRear() const717 int32_t AudioTrackServerProxy::getRear() const
718 {
719     const int32_t stop = android_atomic_acquire_load(&mCblk->u.mStreaming.mStop);
720     const int32_t rear = android_atomic_acquire_load(&mCblk->u.mStreaming.mRear);
721     const int32_t stopLast = mStopLast.load(std::memory_order_acquire);
722     if (stop != stopLast) {
723         const int32_t front = mCblk->u.mStreaming.mFront;
724         const size_t overflowBit = mFrameCountP2 << 1;
725         const size_t mask = overflowBit - 1;
726         int32_t newRear = (rear & ~mask) | (stop & mask);
727         ssize_t filled = newRear - front;
728         // overflowBit is unsigned, so cast to signed for comparison.
729         if (filled >= (ssize_t)overflowBit) {
730             // front and rear offsets span the overflow bit of the p2 mask
731             // so rebasing newRear on the rear offset is off by the overflow bit.
732             ALOGV("stop wrap: filled %zx >= overflowBit %zx", filled, overflowBit);
733             newRear -= overflowBit;
734             filled -= overflowBit;
735         }
736         if (0 <= filled && (size_t) filled <= mFrameCount) {
737             // we're stopped, return the stop level as newRear
738             return newRear;
739         }
740 
741         // A corrupt stop. Log error and ignore.
742         ALOGE("mStopLast %#x -> stop %#x, front %#x, rear %#x, mask %#x, newRear %#x, "
743                 "filled %zd=%#x",
744                 stopLast, stop, front, rear,
745                 (unsigned)mask, newRear, filled, (unsigned)filled);
746         // Don't reset mStopLast as this is const.
747     }
748     return rear;
749 }
750 
start()751 void AudioTrackServerProxy::start()
752 {
753     mStopLast = android_atomic_acquire_load(&mCblk->u.mStreaming.mStop);
754 }
755 
756 __attribute__((no_sanitize("integer")))
obtainBuffer(Buffer * buffer,bool ackFlush)757 status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
758 {
759     LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0,
760             "%s: null or zero frame buffer, buffer:%p", __func__, buffer);
761     if (mIsShutdown) {
762         goto no_init;
763     }
764     {
765     audio_track_cblk_t* cblk = mCblk;
766     // compute number of frames available to write (AudioTrack) or read (AudioRecord),
767     // or use previous cached value from framesReady(), with added barrier if it omits.
768     int32_t front;
769     int32_t rear;
770     // See notes on barriers at ClientProxy::obtainBuffer()
771     if (mIsOut) {
772         flushBufferIfNeeded(); // might modify mFront
773         rear = getRear();
774         front = cblk->u.mStreaming.mFront;
775     } else {
776         front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront);
777         rear = cblk->u.mStreaming.mRear;
778     }
779     ssize_t filled = rear - front;
780     // pipe should not already be overfull
781     if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
782         ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); shutting down",
783                 filled, mFrameCount);
784         mIsShutdown = true;
785     }
786     if (mIsShutdown) {
787         goto no_init;
788     }
789     // don't allow filling pipe beyond the nominal size
790     size_t availToServer;
791     if (mIsOut) {
792         availToServer = filled;
793         mAvailToClient = mFrameCount - filled;
794     } else {
795         availToServer = mFrameCount - filled;
796         mAvailToClient = filled;
797     }
798     // 'availToServer' may be non-contiguous, so return only the first contiguous chunk
799     size_t part1;
800     if (mIsOut) {
801         front &= mFrameCountP2 - 1;
802         part1 = mFrameCountP2 - front;
803     } else {
804         rear &= mFrameCountP2 - 1;
805         part1 = mFrameCountP2 - rear;
806     }
807     if (part1 > availToServer) {
808         part1 = availToServer;
809     }
810     size_t ask = buffer->mFrameCount;
811     if (part1 > ask) {
812         part1 = ask;
813     }
814     // is assignment redundant in some cases?
815     buffer->mFrameCount = part1;
816     buffer->mRaw = part1 > 0 ?
817             &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL;
818     buffer->mNonContig = availToServer - part1;
819     // After flush(), allow releaseBuffer() on a previously obtained buffer;
820     // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp.
821     if (!ackFlush) {
822         mUnreleased = part1;
823     }
824     return part1 > 0 ? NO_ERROR : WOULD_BLOCK;
825     }
826 no_init:
827     buffer->mFrameCount = 0;
828     buffer->mRaw = NULL;
829     buffer->mNonContig = 0;
830     mUnreleased = 0;
831     return NO_INIT;
832 }
833 
834 __attribute__((no_sanitize("integer")))
releaseBuffer(Buffer * buffer)835 void ServerProxy::releaseBuffer(Buffer* buffer)
836 {
837     LOG_ALWAYS_FATAL_IF(buffer == NULL);
838     size_t stepCount = buffer->mFrameCount;
839     if (stepCount == 0 || mIsShutdown) {
840         // prevent accidental re-use of buffer
841         buffer->mFrameCount = 0;
842         buffer->mRaw = NULL;
843         buffer->mNonContig = 0;
844         return;
845     }
846     LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount),
847             "%s: mUnreleased out of range, "
848             "!(stepCount:%zu <= mUnreleased:%zu <= mFrameCount:%zu)",
849             __func__, stepCount, mUnreleased, mFrameCount);
850     mUnreleased -= stepCount;
851     audio_track_cblk_t* cblk = mCblk;
852     if (mIsOut) {
853         int32_t front = cblk->u.mStreaming.mFront;
854         android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront);
855     } else {
856         int32_t rear = cblk->u.mStreaming.mRear;
857         android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear);
858     }
859 
860     cblk->mServer += stepCount;
861     mReleased += stepCount;
862 
863     size_t half = mFrameCount / 2;
864     if (half == 0) {
865         half = 1;
866     }
867     size_t minimum = (size_t) cblk->mMinimum;
868     if (minimum == 0) {
869         minimum = mIsOut ? half : 1;
870     } else if (minimum > half) {
871         minimum = half;
872     }
873     // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time
874     if (!mIsOut || (mAvailToClient + stepCount >= minimum)) {
875         ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum);
876         int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex);
877         if (!(old & CBLK_FUTEX_WAKE)) {
878             (void) syscall(__NR_futex, &cblk->mFutex,
879                     mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1);
880         }
881     }
882 
883     buffer->mFrameCount = 0;
884     buffer->mRaw = NULL;
885     buffer->mNonContig = 0;
886 }
887 
888 // ---------------------------------------------------------------------------
889 
890 __attribute__((no_sanitize("integer")))
framesReady()891 size_t AudioTrackServerProxy::framesReady()
892 {
893     LOG_ALWAYS_FATAL_IF(!mIsOut);
894 
895     if (mIsShutdown) {
896         return 0;
897     }
898     audio_track_cblk_t* cblk = mCblk;
899 
900     int32_t flush = cblk->u.mStreaming.mFlush;
901     if (flush != mFlush) {
902         // FIXME should return an accurate value, but over-estimate is better than under-estimate
903         return mFrameCount;
904     }
905     const int32_t rear = getRear();
906     ssize_t filled = rear - cblk->u.mStreaming.mFront;
907     // pipe should not already be overfull
908     if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
909         ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); shutting down",
910                 filled, mFrameCount);
911         mIsShutdown = true;
912         return 0;
913     }
914     //  cache this value for later use by obtainBuffer(), with added barrier
915     //  and racy if called by normal mixer thread
916     // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer()
917     return filled;
918 }
919 
920 __attribute__((no_sanitize("integer")))
framesReadySafe() const921 size_t AudioTrackServerProxy::framesReadySafe() const
922 {
923     if (mIsShutdown) {
924         return 0;
925     }
926     const audio_track_cblk_t* cblk = mCblk;
927     const int32_t flush = android_atomic_acquire_load(&cblk->u.mStreaming.mFlush);
928     if (flush != mFlush) {
929         return mFrameCount;
930     }
931     const int32_t rear = getRear();
932     const ssize_t filled = rear - cblk->u.mStreaming.mFront;
933     if (!(0 <= filled && (size_t) filled <= mFrameCount)) {
934         return 0; // error condition, silently return 0.
935     }
936     return filled;
937 }
938 
setStreamEndDone()939 bool  AudioTrackServerProxy::setStreamEndDone() {
940     audio_track_cblk_t* cblk = mCblk;
941     bool old =
942             (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0;
943     if (!old) {
944         (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE,
945                 1);
946     }
947     return old;
948 }
949 
950 __attribute__((no_sanitize("integer")))
tallyUnderrunFrames(uint32_t frameCount)951 void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
952 {
953     audio_track_cblk_t* cblk = mCblk;
954     if (frameCount > 0) {
955         cblk->u.mStreaming.mUnderrunFrames += frameCount;
956 
957         if (!mUnderrunning) { // start of underrun?
958             mUnderrunCount++;
959             cblk->u.mStreaming.mUnderrunCount = mUnderrunCount;
960             mUnderrunning = true;
961             ALOGV("tallyUnderrunFrames(%3u) at uf = %u, bump mUnderrunCount = %u",
962                 frameCount, cblk->u.mStreaming.mUnderrunFrames, mUnderrunCount);
963         }
964 
965         // FIXME also wake futex so that underrun is noticed more quickly
966         (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags);
967     } else {
968         ALOGV_IF(mUnderrunning,
969             "tallyUnderrunFrames(%3u) at uf = %u, underrun finished",
970             frameCount, cblk->u.mStreaming.mUnderrunFrames);
971         mUnderrunning = false; // so we can detect the next edge
972     }
973 }
974 
getPlaybackRate()975 AudioPlaybackRate AudioTrackServerProxy::getPlaybackRate()
976 {   // do not call from multiple threads without holding lock
977     mPlaybackRateObserver.poll(mPlaybackRate);
978     return mPlaybackRate;
979 }
980 
981 // ---------------------------------------------------------------------------
982 
StaticAudioTrackServerProxy(audio_track_cblk_t * cblk,void * buffers,size_t frameCount,size_t frameSize)983 StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers,
984         size_t frameCount, size_t frameSize)
985     : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize),
986       mObserver(&cblk->u.mStatic.mSingleStateQueue),
987       mPosLoopMutator(&cblk->u.mStatic.mPosLoopQueue),
988       mFramesReadySafe(frameCount), mFramesReady(frameCount),
989       mFramesReadyIsCalledByMultipleThreads(false)
990 {
991     memset(&mState, 0, sizeof(mState));
992 }
993 
framesReadyIsCalledByMultipleThreads()994 void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads()
995 {
996     mFramesReadyIsCalledByMultipleThreads = true;
997 }
998 
framesReady()999 size_t StaticAudioTrackServerProxy::framesReady()
1000 {
1001     // Can't call pollPosition() from multiple threads.
1002     if (!mFramesReadyIsCalledByMultipleThreads) {
1003         (void) pollPosition();
1004     }
1005     return mFramesReadySafe;
1006 }
1007 
framesReadySafe() const1008 size_t StaticAudioTrackServerProxy::framesReadySafe() const
1009 {
1010     return mFramesReadySafe;
1011 }
1012 
updateStateWithLoop(StaticAudioTrackState * localState,const StaticAudioTrackState & update) const1013 status_t StaticAudioTrackServerProxy::updateStateWithLoop(
1014         StaticAudioTrackState *localState, const StaticAudioTrackState &update) const
1015 {
1016     if (localState->mLoopSequence != update.mLoopSequence) {
1017         bool valid = false;
1018         const size_t loopStart = update.mLoopStart;
1019         const size_t loopEnd = update.mLoopEnd;
1020         size_t position = localState->mPosition;
1021         if (update.mLoopCount == 0) {
1022             valid = true;
1023         } else if (update.mLoopCount >= -1) {
1024             if (loopStart < loopEnd && loopEnd <= mFrameCount &&
1025                     loopEnd - loopStart >= MIN_LOOP) {
1026                 // If the current position is greater than the end of the loop
1027                 // we "wrap" to the loop start. This might cause an audible pop.
1028                 if (position >= loopEnd) {
1029                     position = loopStart;
1030                 }
1031                 valid = true;
1032             }
1033         }
1034         if (!valid || position > mFrameCount) {
1035             return NO_INIT;
1036         }
1037         localState->mPosition = position;
1038         localState->mLoopCount = update.mLoopCount;
1039         localState->mLoopEnd = loopEnd;
1040         localState->mLoopStart = loopStart;
1041         localState->mLoopSequence = update.mLoopSequence;
1042     }
1043     return OK;
1044 }
1045 
updateStateWithPosition(StaticAudioTrackState * localState,const StaticAudioTrackState & update) const1046 status_t StaticAudioTrackServerProxy::updateStateWithPosition(
1047         StaticAudioTrackState *localState, const StaticAudioTrackState &update) const
1048 {
1049     if (localState->mPositionSequence != update.mPositionSequence) {
1050         if (update.mPosition > mFrameCount) {
1051             return NO_INIT;
1052         } else if (localState->mLoopCount != 0 && update.mPosition >= localState->mLoopEnd) {
1053             localState->mLoopCount = 0; // disable loop count if position is beyond loop end.
1054         }
1055         localState->mPosition = update.mPosition;
1056         localState->mPositionSequence = update.mPositionSequence;
1057     }
1058     return OK;
1059 }
1060 
pollPosition()1061 ssize_t StaticAudioTrackServerProxy::pollPosition()
1062 {
1063     StaticAudioTrackState state;
1064     if (mObserver.poll(state)) {
1065         StaticAudioTrackState trystate = mState;
1066         bool result;
1067         const int32_t diffSeq = (int32_t) state.mLoopSequence - (int32_t) state.mPositionSequence;
1068 
1069         if (diffSeq < 0) {
1070             result = updateStateWithLoop(&trystate, state) == OK &&
1071                     updateStateWithPosition(&trystate, state) == OK;
1072         } else {
1073             result = updateStateWithPosition(&trystate, state) == OK &&
1074                     updateStateWithLoop(&trystate, state) == OK;
1075         }
1076         if (!result) {
1077             mObserver.done();
1078             // caution: no update occurs so server state will be inconsistent with client state.
1079             ALOGE("%s client pushed an invalid state, shutting down", __func__);
1080             mIsShutdown = true;
1081             return (ssize_t) NO_INIT;
1082         }
1083         mState = trystate;
1084         if (mState.mLoopCount == -1) {
1085             mFramesReady = INT64_MAX;
1086         } else if (mState.mLoopCount == 0) {
1087             mFramesReady = mFrameCount - mState.mPosition;
1088         } else if (mState.mLoopCount > 0) {
1089             // TODO: Later consider fixing overflow, but does not seem needed now
1090             // as will not overflow if loopStart and loopEnd are Java "ints".
1091             mFramesReady = int64_t(mState.mLoopCount) * (mState.mLoopEnd - mState.mLoopStart)
1092                     + mFrameCount - mState.mPosition;
1093         }
1094         mFramesReadySafe = clampToSize(mFramesReady);
1095         // This may overflow, but client is not supposed to rely on it
1096         StaticAudioTrackPosLoop posLoop;
1097 
1098         posLoop.mLoopCount = (int32_t) mState.mLoopCount;
1099         posLoop.mBufferPosition = (uint32_t) mState.mPosition;
1100         mPosLoopMutator.push(posLoop);
1101         mObserver.done(); // safe to read mStatic variables.
1102     }
1103     return (ssize_t) mState.mPosition;
1104 }
1105 
1106 __attribute__((no_sanitize("integer")))
obtainBuffer(Buffer * buffer,bool ackFlush)1107 status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush)
1108 {
1109     if (mIsShutdown) {
1110         buffer->mFrameCount = 0;
1111         buffer->mRaw = NULL;
1112         buffer->mNonContig = 0;
1113         mUnreleased = 0;
1114         return NO_INIT;
1115     }
1116     ssize_t positionOrStatus = pollPosition();
1117     if (positionOrStatus < 0) {
1118         buffer->mFrameCount = 0;
1119         buffer->mRaw = NULL;
1120         buffer->mNonContig = 0;
1121         mUnreleased = 0;
1122         return (status_t) positionOrStatus;
1123     }
1124     size_t position = (size_t) positionOrStatus;
1125     size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount;
1126     size_t avail;
1127     if (position < end) {
1128         avail = end - position;
1129         size_t wanted = buffer->mFrameCount;
1130         if (avail < wanted) {
1131             buffer->mFrameCount = avail;
1132         } else {
1133             avail = wanted;
1134         }
1135         buffer->mRaw = &((char *) mBuffers)[position * mFrameSize];
1136     } else {
1137         avail = 0;
1138         buffer->mFrameCount = 0;
1139         buffer->mRaw = NULL;
1140     }
1141     // As mFramesReady is the total remaining frames in the static audio track,
1142     // it is always larger or equal to avail.
1143     LOG_ALWAYS_FATAL_IF(mFramesReady < (int64_t) avail,
1144             "%s: mFramesReady out of range, mFramesReady:%lld < avail:%zu",
1145             __func__, (long long)mFramesReady, avail);
1146     buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail);
1147     if (!ackFlush) {
1148         mUnreleased = avail;
1149     }
1150     return NO_ERROR;
1151 }
1152 
1153 __attribute__((no_sanitize("integer")))
releaseBuffer(Buffer * buffer)1154 void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer)
1155 {
1156     size_t stepCount = buffer->mFrameCount;
1157     LOG_ALWAYS_FATAL_IF(!((int64_t) stepCount <= mFramesReady),
1158             "%s: stepCount out of range, "
1159             "!(stepCount:%zu <= mFramesReady:%lld)",
1160             __func__, stepCount, (long long)mFramesReady);
1161     LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased),
1162             "%s: stepCount out of range, "
1163             "!(stepCount:%zu <= mUnreleased:%zu)",
1164             __func__, stepCount, mUnreleased);
1165     if (stepCount == 0) {
1166         // prevent accidental re-use of buffer
1167         buffer->mRaw = NULL;
1168         buffer->mNonContig = 0;
1169         return;
1170     }
1171     mUnreleased -= stepCount;
1172     audio_track_cblk_t* cblk = mCblk;
1173     size_t position = mState.mPosition;
1174     size_t newPosition = position + stepCount;
1175     int32_t setFlags = 0;
1176     if (!(position <= newPosition && newPosition <= mFrameCount)) {
1177         ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position,
1178                 mFrameCount);
1179         newPosition = mFrameCount;
1180     } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) {
1181         newPosition = mState.mLoopStart;
1182         if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) {
1183             setFlags = CBLK_LOOP_CYCLE;
1184         } else {
1185             setFlags = CBLK_LOOP_FINAL;
1186         }
1187     }
1188     if (newPosition == mFrameCount) {
1189         setFlags |= CBLK_BUFFER_END;
1190     }
1191     mState.mPosition = newPosition;
1192     if (mFramesReady != INT64_MAX) {
1193         mFramesReady -= stepCount;
1194     }
1195     mFramesReadySafe = clampToSize(mFramesReady);
1196 
1197     cblk->mServer += stepCount;
1198     mReleased += stepCount;
1199 
1200     // This may overflow, but client is not supposed to rely on it
1201     StaticAudioTrackPosLoop posLoop;
1202     posLoop.mBufferPosition = mState.mPosition;
1203     posLoop.mLoopCount = mState.mLoopCount;
1204     mPosLoopMutator.push(posLoop);
1205     if (setFlags != 0) {
1206         (void) android_atomic_or(setFlags, &cblk->mFlags);
1207         // this would be a good place to wake a futex
1208     }
1209 
1210     buffer->mFrameCount = 0;
1211     buffer->mRaw = NULL;
1212     buffer->mNonContig = 0;
1213 }
1214 
tallyUnderrunFrames(uint32_t frameCount)1215 void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount)
1216 {
1217     // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks,
1218     // we don't have a location to count underrun frames.  The underrun frame counter
1219     // only exists in AudioTrackSharedStreaming.  Fortunately, underruns are not
1220     // possible for static buffer tracks other than at end of buffer, so this is not a loss.
1221 
1222     // FIXME also wake futex so that underrun is noticed more quickly
1223     if (frameCount > 0) {
1224         (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags);
1225     }
1226 }
1227 
getRear() const1228 int32_t StaticAudioTrackServerProxy::getRear() const
1229 {
1230     LOG_ALWAYS_FATAL("getRear() not permitted for static tracks");
1231     return 0;
1232 }
1233 
1234 // ---------------------------------------------------------------------------
1235 
1236 }   // namespace android
1237