| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 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> | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <linux/futex.h> | 
|  | 24 | #include <sys/syscall.h> | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 |  | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 28 | // used to clamp a value to size_t.  TODO: move to another file. | 
|  | 29 | template <typename T> | 
|  | 30 | size_t clampToSize(T x) { | 
| Andy Hung | 486a713 | 2014-12-22 16:54:21 -0800 | [diff] [blame] | 31 | return sizeof(T) > sizeof(size_t) && x > (T) SIZE_MAX ? SIZE_MAX : x < 0 ? 0 : (size_t) x; | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 32 | } | 
|  | 33 |  | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 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 |  | 
|  | 40 | static uint32_t incrementSequence(uint32_t self, uint32_t other) { | 
| Chad Brubaker | cb50c54 | 2015-10-07 14:20:10 -0700 | [diff] [blame] | 41 | int32_t diff = (int32_t) self - (int32_t) other; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 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 |  | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 48 | audio_track_cblk_t::audio_track_cblk_t() | 
| Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 49 | : mServer(0), mFutex(0), mMinimum(0), | 
| Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 50 | mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0), mFlags(0) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 51 | { | 
|  | 52 | memset(&u, 0, sizeof(u)); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | // --------------------------------------------------------------------------- | 
|  | 56 |  | 
|  | 57 | Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, | 
|  | 58 | bool isOut, bool clientInServer) | 
|  | 59 | : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize), | 
|  | 60 | mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer), | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 61 | mIsShutdown(false), mUnreleased(0) | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 62 | { | 
|  | 63 | } | 
|  | 64 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 65 | // --------------------------------------------------------------------------- | 
|  | 66 |  | 
|  | 67 | ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, | 
|  | 68 | size_t frameSize, bool isOut, bool clientInServer) | 
| Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 69 | : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer) | 
|  | 70 | , mBufferSizeInFrames(frameCount) | 
|  | 71 | , mEpoch(0) | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 72 | { | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 73 | } | 
|  | 74 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 75 | const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/}; | 
|  | 76 | const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/}; | 
|  | 77 |  | 
|  | 78 | #define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS | 
|  | 79 |  | 
|  | 80 | // To facilitate quicker recovery from server failure, this value limits the timeout per each futex | 
|  | 81 | // wait.  However it does not protect infinite timeouts.  If defined to be zero, there is no limit. | 
|  | 82 | // FIXME May not be compatible with audio tunneling requirements where timeout should be in the | 
|  | 83 | // order of minutes. | 
|  | 84 | #define MAX_SEC    5 | 
|  | 85 |  | 
|  | 86 | status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested, | 
|  | 87 | struct timespec *elapsed) | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 88 | { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 90 | struct timespec total;          // total elapsed time spent waiting | 
|  | 91 | total.tv_sec = 0; | 
|  | 92 | total.tv_nsec = 0; | 
|  | 93 | bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 94 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 95 | status_t status; | 
|  | 96 | enum { | 
|  | 97 | TIMEOUT_ZERO,       // requested == NULL || *requested == 0 | 
|  | 98 | TIMEOUT_INFINITE,   // *requested == infinity | 
|  | 99 | TIMEOUT_FINITE,     // 0 < *requested < infinity | 
|  | 100 | TIMEOUT_CONTINUE,   // additional chances after TIMEOUT_FINITE | 
|  | 101 | } timeout; | 
|  | 102 | if (requested == NULL) { | 
|  | 103 | timeout = TIMEOUT_ZERO; | 
|  | 104 | } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { | 
|  | 105 | timeout = TIMEOUT_ZERO; | 
|  | 106 | } else if (requested->tv_sec == INT_MAX) { | 
|  | 107 | timeout = TIMEOUT_INFINITE; | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 108 | } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 109 | timeout = TIMEOUT_FINITE; | 
|  | 110 | if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) { | 
|  | 111 | measure = true; | 
|  | 112 | } | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 113 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 114 | struct timespec before; | 
|  | 115 | bool beforeIsValid = false; | 
|  | 116 | audio_track_cblk_t* cblk = mCblk; | 
|  | 117 | bool ignoreInitialPendingInterrupt = true; | 
|  | 118 | // check for shared memory corruption | 
|  | 119 | if (mIsShutdown) { | 
|  | 120 | status = NO_INIT; | 
|  | 121 | goto end; | 
|  | 122 | } | 
|  | 123 | for (;;) { | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 124 | int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 125 | // check for track invalidation by server, or server death detection | 
|  | 126 | if (flags & CBLK_INVALID) { | 
|  | 127 | ALOGV("Track invalidated"); | 
|  | 128 | status = DEAD_OBJECT; | 
|  | 129 | goto end; | 
|  | 130 | } | 
|  | 131 | // check for obtainBuffer interrupted by client | 
|  | 132 | if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) { | 
|  | 133 | ALOGV("obtainBuffer() interrupted by client"); | 
|  | 134 | status = -EINTR; | 
|  | 135 | goto end; | 
|  | 136 | } | 
|  | 137 | ignoreInitialPendingInterrupt = false; | 
|  | 138 | // compute number of frames available to write (AudioTrack) or read (AudioRecord) | 
|  | 139 | int32_t front; | 
|  | 140 | int32_t rear; | 
|  | 141 | if (mIsOut) { | 
|  | 142 | // The barrier following the read of mFront is probably redundant. | 
|  | 143 | // We're about to perform a conditional branch based on 'filled', | 
|  | 144 | // which will force the processor to observe the read of mFront | 
|  | 145 | // prior to allowing data writes starting at mRaw. | 
|  | 146 | // However, the processor may support speculative execution, | 
|  | 147 | // and be unable to undo speculative writes into shared memory. | 
|  | 148 | // The barrier will prevent such speculative execution. | 
|  | 149 | front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); | 
|  | 150 | rear = cblk->u.mStreaming.mRear; | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 151 | } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 152 | // On the other hand, this barrier is required. | 
|  | 153 | rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); | 
|  | 154 | front = cblk->u.mStreaming.mFront; | 
|  | 155 | } | 
| Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 156 | // write to rear, read from front | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 157 | ssize_t filled = rear - front; | 
|  | 158 | // pipe should not be overfull | 
|  | 159 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 160 | if (mIsOut) { | 
| Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 161 | ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); " | 
| Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 162 | "shutting down", filled, mFrameCount); | 
|  | 163 | mIsShutdown = true; | 
|  | 164 | status = NO_INIT; | 
|  | 165 | goto end; | 
|  | 166 | } | 
|  | 167 | // for input, sync up on overrun | 
|  | 168 | filled = 0; | 
|  | 169 | cblk->u.mStreaming.mFront = rear; | 
|  | 170 | (void) android_atomic_or(CBLK_OVERRUN, &cblk->mFlags); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 171 | } | 
| Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 172 | // Don't allow filling pipe beyond the user settable size. | 
|  | 173 | // The calculation for avail can go negative if the buffer size | 
|  | 174 | // is suddenly dropped below the amount already in the buffer. | 
|  | 175 | // So use a signed calculation to prevent a numeric overflow abort. | 
|  | 176 | ssize_t adjustableSize = (ssize_t) mBufferSizeInFrames; | 
|  | 177 | ssize_t avail =  (mIsOut) ? adjustableSize - filled : filled; | 
|  | 178 | if (avail < 0) { | 
|  | 179 | avail = 0; | 
|  | 180 | } else if (avail > 0) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 181 | // 'avail' may be non-contiguous, so return only the first contiguous chunk | 
| Eric Laurent | bdd8101 | 2016-01-29 15:25:06 -0800 | [diff] [blame] | 182 | size_t part1; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 183 | if (mIsOut) { | 
|  | 184 | rear &= mFrameCountP2 - 1; | 
|  | 185 | part1 = mFrameCountP2 - rear; | 
|  | 186 | } else { | 
|  | 187 | front &= mFrameCountP2 - 1; | 
|  | 188 | part1 = mFrameCountP2 - front; | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 189 | } | 
| Eric Laurent | bdd8101 | 2016-01-29 15:25:06 -0800 | [diff] [blame] | 190 | if (part1 > (size_t)avail) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 191 | part1 = avail; | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 192 | } | 
| Eric Laurent | bdd8101 | 2016-01-29 15:25:06 -0800 | [diff] [blame] | 193 | if (part1 > buffer->mFrameCount) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 194 | part1 = buffer->mFrameCount; | 
|  | 195 | } | 
| Eric Laurent | bdd8101 | 2016-01-29 15:25:06 -0800 | [diff] [blame] | 196 | buffer->mFrameCount = part1; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 197 | buffer->mRaw = part1 > 0 ? | 
|  | 198 | &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL; | 
|  | 199 | buffer->mNonContig = avail - part1; | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 200 | mUnreleased = part1; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 201 | status = NO_ERROR; | 
|  | 202 | break; | 
|  | 203 | } | 
|  | 204 | struct timespec remaining; | 
|  | 205 | const struct timespec *ts; | 
|  | 206 | switch (timeout) { | 
|  | 207 | case TIMEOUT_ZERO: | 
|  | 208 | status = WOULD_BLOCK; | 
|  | 209 | goto end; | 
|  | 210 | case TIMEOUT_INFINITE: | 
|  | 211 | ts = NULL; | 
|  | 212 | break; | 
|  | 213 | case TIMEOUT_FINITE: | 
|  | 214 | timeout = TIMEOUT_CONTINUE; | 
|  | 215 | if (MAX_SEC == 0) { | 
|  | 216 | ts = requested; | 
|  | 217 | break; | 
|  | 218 | } | 
|  | 219 | // fall through | 
|  | 220 | case TIMEOUT_CONTINUE: | 
|  | 221 | // FIXME we do not retry if requested < 10ms? needs documentation on this state machine | 
|  | 222 | if (!measure || requested->tv_sec < total.tv_sec || | 
|  | 223 | (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { | 
|  | 224 | status = TIMED_OUT; | 
|  | 225 | goto end; | 
|  | 226 | } | 
|  | 227 | remaining.tv_sec = requested->tv_sec - total.tv_sec; | 
|  | 228 | if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { | 
|  | 229 | remaining.tv_nsec += 1000000000; | 
|  | 230 | remaining.tv_sec++; | 
|  | 231 | } | 
|  | 232 | if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { | 
|  | 233 | remaining.tv_sec = MAX_SEC; | 
|  | 234 | remaining.tv_nsec = 0; | 
|  | 235 | } | 
|  | 236 | ts = &remaining; | 
|  | 237 | break; | 
|  | 238 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 239 | LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 240 | ts = NULL; | 
|  | 241 | break; | 
|  | 242 | } | 
| Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 243 | int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); | 
|  | 244 | if (!(old & CBLK_FUTEX_WAKE)) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 245 | if (measure && !beforeIsValid) { | 
|  | 246 | clock_gettime(CLOCK_MONOTONIC, &before); | 
|  | 247 | beforeIsValid = true; | 
|  | 248 | } | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 249 | errno = 0; | 
|  | 250 | (void) syscall(__NR_futex, &cblk->mFutex, | 
| Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 251 | mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); | 
| Leena Winterrowd | b463da8 | 2015-12-14 15:58:16 -0800 | [diff] [blame] | 252 | status_t error = errno; // clock_gettime can affect errno | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 253 | // update total elapsed time spent waiting | 
|  | 254 | if (measure) { | 
|  | 255 | struct timespec after; | 
|  | 256 | clock_gettime(CLOCK_MONOTONIC, &after); | 
|  | 257 | total.tv_sec += after.tv_sec - before.tv_sec; | 
|  | 258 | long deltaNs = after.tv_nsec - before.tv_nsec; | 
|  | 259 | if (deltaNs < 0) { | 
|  | 260 | deltaNs += 1000000000; | 
|  | 261 | total.tv_sec--; | 
|  | 262 | } | 
|  | 263 | if ((total.tv_nsec += deltaNs) >= 1000000000) { | 
|  | 264 | total.tv_nsec -= 1000000000; | 
|  | 265 | total.tv_sec++; | 
|  | 266 | } | 
|  | 267 | before = after; | 
|  | 268 | beforeIsValid = true; | 
|  | 269 | } | 
| Leena Winterrowd | b463da8 | 2015-12-14 15:58:16 -0800 | [diff] [blame] | 270 | switch (error) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 271 | case 0:            // normal wakeup by server, or by binderDied() | 
|  | 272 | case EWOULDBLOCK:  // benign race condition with server | 
|  | 273 | case EINTR:        // wait was interrupted by signal or other spurious wakeup | 
|  | 274 | case ETIMEDOUT:    // time-out expired | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 275 | // FIXME these error/non-0 status are being dropped | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 276 | break; | 
|  | 277 | default: | 
| Leena Winterrowd | b463da8 | 2015-12-14 15:58:16 -0800 | [diff] [blame] | 278 | status = error; | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 279 | ALOGE("%s unexpected error %s", __func__, strerror(status)); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 280 | goto end; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | end: | 
|  | 286 | if (status != NO_ERROR) { | 
|  | 287 | buffer->mFrameCount = 0; | 
|  | 288 | buffer->mRaw = NULL; | 
|  | 289 | buffer->mNonContig = 0; | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 290 | mUnreleased = 0; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 291 | } | 
|  | 292 | if (elapsed != NULL) { | 
|  | 293 | *elapsed = total; | 
|  | 294 | } | 
|  | 295 | if (requested == NULL) { | 
|  | 296 | requested = &kNonBlocking; | 
|  | 297 | } | 
|  | 298 | if (measure) { | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 299 | ALOGV("requested %ld.%03ld elapsed %ld.%03ld", | 
|  | 300 | requested->tv_sec, requested->tv_nsec / 1000000, | 
|  | 301 | total.tv_sec, total.tv_nsec / 1000000); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 302 | } | 
|  | 303 | return status; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | void ClientProxy::releaseBuffer(Buffer* buffer) | 
|  | 307 | { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 308 | LOG_ALWAYS_FATAL_IF(buffer == NULL); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 309 | size_t stepCount = buffer->mFrameCount; | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 310 | if (stepCount == 0 || mIsShutdown) { | 
|  | 311 | // prevent accidental re-use of buffer | 
|  | 312 | buffer->mFrameCount = 0; | 
|  | 313 | buffer->mRaw = NULL; | 
|  | 314 | buffer->mNonContig = 0; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 315 | return; | 
|  | 316 | } | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 317 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); | 
|  | 318 | mUnreleased -= stepCount; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 319 | audio_track_cblk_t* cblk = mCblk; | 
|  | 320 | // Both of these barriers are required | 
|  | 321 | if (mIsOut) { | 
|  | 322 | int32_t rear = cblk->u.mStreaming.mRear; | 
|  | 323 | android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); | 
|  | 324 | } else { | 
|  | 325 | int32_t front = cblk->u.mStreaming.mFront; | 
|  | 326 | android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); | 
|  | 327 | } | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | void ClientProxy::binderDied() | 
|  | 331 | { | 
|  | 332 | audio_track_cblk_t* cblk = mCblk; | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 333 | if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) { | 
| zunkyu.lee | 82a69ea | 2014-11-07 15:47:32 +0900 | [diff] [blame] | 334 | android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 335 | // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 336 | (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, | 
|  | 337 | 1); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | void ClientProxy::interrupt() | 
|  | 342 | { | 
|  | 343 | audio_track_cblk_t* cblk = mCblk; | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 344 | if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) { | 
| zunkyu.lee | 82a69ea | 2014-11-07 15:47:32 +0900 | [diff] [blame] | 345 | android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 346 | (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, | 
|  | 347 | 1); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Chad Brubaker | 65dda4f | 2015-09-22 16:13:30 -0700 | [diff] [blame] | 351 | __attribute__((no_sanitize("integer"))) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 352 | size_t ClientProxy::getMisalignment() | 
|  | 353 | { | 
|  | 354 | audio_track_cblk_t* cblk = mCblk; | 
|  | 355 | return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) & | 
|  | 356 | (mFrameCountP2 - 1); | 
|  | 357 | } | 
|  | 358 |  | 
| Phil Burk | c0adecb | 2016-01-08 12:44:11 -0800 | [diff] [blame] | 359 | size_t ClientProxy::setBufferSizeInFrames(size_t size) | 
|  | 360 | { | 
|  | 361 | // TODO set minimum to 2X the fast mixer buffer size. | 
|  | 362 | size_t minimum = 128 * 2; // arbitrary | 
|  | 363 | size_t maximum = frameCount(); | 
|  | 364 | if (size < minimum) { | 
|  | 365 | size = minimum; | 
|  | 366 | } else if (size > maximum) { | 
|  | 367 | size = maximum; | 
|  | 368 | } | 
|  | 369 | mBufferSizeInFrames = size; | 
|  | 370 | return size; | 
|  | 371 | } | 
|  | 372 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 373 | // --------------------------------------------------------------------------- | 
|  | 374 |  | 
|  | 375 | void AudioTrackClientProxy::flush() | 
|  | 376 | { | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 377 | // This works for mFrameCountP2 <= 2^30 | 
|  | 378 | size_t increment = mFrameCountP2 << 1; | 
|  | 379 | size_t mask = increment - 1; | 
|  | 380 | audio_track_cblk_t* cblk = mCblk; | 
| Andy Hung | a2d75cd | 2015-07-15 17:04:20 -0700 | [diff] [blame] | 381 | // mFlush is 32 bits concatenated as [ flush_counter ] [ newfront_offset ] | 
|  | 382 | // Should newFlush = cblk->u.mStreaming.mRear?  Only problem is | 
|  | 383 | // if you want to flush twice to the same rear location after a 32 bit wrap. | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 384 | int32_t newFlush = (cblk->u.mStreaming.mRear & mask) | | 
|  | 385 | ((cblk->u.mStreaming.mFlush & ~mask) + increment); | 
|  | 386 | android_atomic_release_store(newFlush, &cblk->u.mStreaming.mFlush); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 389 | bool AudioTrackClientProxy::clearStreamEndDone() { | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 390 | return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 391 | } | 
|  | 392 |  | 
|  | 393 | bool AudioTrackClientProxy::getStreamEndDone() const { | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 394 | return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 395 | } | 
|  | 396 |  | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 397 | status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested) | 
|  | 398 | { | 
|  | 399 | struct timespec total;          // total elapsed time spent waiting | 
|  | 400 | total.tv_sec = 0; | 
|  | 401 | total.tv_nsec = 0; | 
|  | 402 | audio_track_cblk_t* cblk = mCblk; | 
|  | 403 | status_t status; | 
|  | 404 | enum { | 
|  | 405 | TIMEOUT_ZERO,       // requested == NULL || *requested == 0 | 
|  | 406 | TIMEOUT_INFINITE,   // *requested == infinity | 
|  | 407 | TIMEOUT_FINITE,     // 0 < *requested < infinity | 
|  | 408 | TIMEOUT_CONTINUE,   // additional chances after TIMEOUT_FINITE | 
|  | 409 | } timeout; | 
|  | 410 | if (requested == NULL) { | 
|  | 411 | timeout = TIMEOUT_ZERO; | 
|  | 412 | } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { | 
|  | 413 | timeout = TIMEOUT_ZERO; | 
|  | 414 | } else if (requested->tv_sec == INT_MAX) { | 
|  | 415 | timeout = TIMEOUT_INFINITE; | 
|  | 416 | } else { | 
|  | 417 | timeout = TIMEOUT_FINITE; | 
|  | 418 | } | 
|  | 419 | for (;;) { | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 420 | int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags); | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 421 | // check for track invalidation by server, or server death detection | 
|  | 422 | if (flags & CBLK_INVALID) { | 
|  | 423 | ALOGV("Track invalidated"); | 
|  | 424 | status = DEAD_OBJECT; | 
|  | 425 | goto end; | 
|  | 426 | } | 
|  | 427 | if (flags & CBLK_STREAM_END_DONE) { | 
|  | 428 | ALOGV("stream end received"); | 
|  | 429 | status = NO_ERROR; | 
|  | 430 | goto end; | 
|  | 431 | } | 
|  | 432 | // check for obtainBuffer interrupted by client | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 433 | if (flags & CBLK_INTERRUPT) { | 
|  | 434 | ALOGV("waitStreamEndDone() interrupted by client"); | 
|  | 435 | status = -EINTR; | 
|  | 436 | goto end; | 
|  | 437 | } | 
|  | 438 | struct timespec remaining; | 
|  | 439 | const struct timespec *ts; | 
|  | 440 | switch (timeout) { | 
|  | 441 | case TIMEOUT_ZERO: | 
|  | 442 | status = WOULD_BLOCK; | 
|  | 443 | goto end; | 
|  | 444 | case TIMEOUT_INFINITE: | 
|  | 445 | ts = NULL; | 
|  | 446 | break; | 
|  | 447 | case TIMEOUT_FINITE: | 
|  | 448 | timeout = TIMEOUT_CONTINUE; | 
|  | 449 | if (MAX_SEC == 0) { | 
|  | 450 | ts = requested; | 
|  | 451 | break; | 
|  | 452 | } | 
|  | 453 | // fall through | 
|  | 454 | case TIMEOUT_CONTINUE: | 
|  | 455 | // FIXME we do not retry if requested < 10ms? needs documentation on this state machine | 
|  | 456 | if (requested->tv_sec < total.tv_sec || | 
|  | 457 | (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { | 
|  | 458 | status = TIMED_OUT; | 
|  | 459 | goto end; | 
|  | 460 | } | 
|  | 461 | remaining.tv_sec = requested->tv_sec - total.tv_sec; | 
|  | 462 | if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { | 
|  | 463 | remaining.tv_nsec += 1000000000; | 
|  | 464 | remaining.tv_sec++; | 
|  | 465 | } | 
|  | 466 | if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { | 
|  | 467 | remaining.tv_sec = MAX_SEC; | 
|  | 468 | remaining.tv_nsec = 0; | 
|  | 469 | } | 
|  | 470 | ts = &remaining; | 
|  | 471 | break; | 
|  | 472 | default: | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 473 | LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout); | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 474 | ts = NULL; | 
|  | 475 | break; | 
|  | 476 | } | 
|  | 477 | int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); | 
|  | 478 | if (!(old & CBLK_FUTEX_WAKE)) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 479 | errno = 0; | 
|  | 480 | (void) syscall(__NR_futex, &cblk->mFutex, | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 481 | mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 482 | switch (errno) { | 
|  | 483 | case 0:            // normal wakeup by server, or by binderDied() | 
|  | 484 | case EWOULDBLOCK:  // benign race condition with server | 
|  | 485 | case EINTR:        // wait was interrupted by signal or other spurious wakeup | 
|  | 486 | case ETIMEDOUT:    // time-out expired | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 487 | break; | 
|  | 488 | default: | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 489 | status = errno; | 
|  | 490 | ALOGE("%s unexpected error %s", __func__, strerror(status)); | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 491 | goto end; | 
|  | 492 | } | 
|  | 493 | } | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | end: | 
|  | 497 | if (requested == NULL) { | 
|  | 498 | requested = &kNonBlocking; | 
|  | 499 | } | 
|  | 500 | return status; | 
|  | 501 | } | 
|  | 502 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 503 | // --------------------------------------------------------------------------- | 
|  | 504 |  | 
|  | 505 | StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, | 
|  | 506 | size_t frameCount, size_t frameSize) | 
|  | 507 | : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize), | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 508 | mMutator(&cblk->u.mStatic.mSingleStateQueue), | 
|  | 509 | mPosLoopObserver(&cblk->u.mStatic.mPosLoopQueue) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 510 | { | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 511 | memset(&mState, 0, sizeof(mState)); | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 512 | memset(&mPosLoop, 0, sizeof(mPosLoop)); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 513 | } | 
|  | 514 |  | 
|  | 515 | void StaticAudioTrackClientProxy::flush() | 
|  | 516 | { | 
| Glenn Kasten | adad3d7 | 2014-02-21 14:51:43 -0800 | [diff] [blame] | 517 | LOG_ALWAYS_FATAL("static flush"); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 518 | } | 
|  | 519 |  | 
|  | 520 | void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount) | 
|  | 521 | { | 
| Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 522 | // This can only happen on a 64-bit client | 
|  | 523 | if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) { | 
|  | 524 | // FIXME Should return an error status | 
|  | 525 | return; | 
|  | 526 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 527 | mState.mLoopStart = (uint32_t) loopStart; | 
|  | 528 | mState.mLoopEnd = (uint32_t) loopEnd; | 
|  | 529 | mState.mLoopCount = loopCount; | 
|  | 530 | mState.mLoopSequence = incrementSequence(mState.mLoopSequence, mState.mPositionSequence); | 
|  | 531 | // set patch-up variables until the mState is acknowledged by the ServerProxy. | 
|  | 532 | // observed buffer position and loop count will freeze until then to give the | 
|  | 533 | // illusion of a synchronous change. | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 534 | getBufferPositionAndLoopCount(NULL, NULL); | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 535 | // preserve behavior to restart at mState.mLoopStart if position exceeds mState.mLoopEnd. | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 536 | if (mState.mLoopCount != 0 && mPosLoop.mBufferPosition >= mState.mLoopEnd) { | 
|  | 537 | mPosLoop.mBufferPosition = mState.mLoopStart; | 
| Andy Hung | 680b795 | 2014-11-12 13:18:52 -0800 | [diff] [blame] | 538 | } | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 539 | mPosLoop.mLoopCount = mState.mLoopCount; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 540 | (void) mMutator.push(mState); | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | void StaticAudioTrackClientProxy::setBufferPosition(size_t position) | 
|  | 544 | { | 
|  | 545 | // This can only happen on a 64-bit client | 
|  | 546 | if (position > UINT32_MAX) { | 
|  | 547 | // FIXME Should return an error status | 
|  | 548 | return; | 
|  | 549 | } | 
|  | 550 | mState.mPosition = (uint32_t) position; | 
|  | 551 | mState.mPositionSequence = incrementSequence(mState.mPositionSequence, mState.mLoopSequence); | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 552 | // set patch-up variables until the mState is acknowledged by the ServerProxy. | 
|  | 553 | // observed buffer position and loop count will freeze until then to give the | 
|  | 554 | // illusion of a synchronous change. | 
|  | 555 | if (mState.mLoopCount > 0) {  // only check if loop count is changing | 
|  | 556 | getBufferPositionAndLoopCount(NULL, NULL); // get last position | 
|  | 557 | } | 
|  | 558 | mPosLoop.mBufferPosition = position; | 
|  | 559 | if (position >= mState.mLoopEnd) { | 
|  | 560 | // no ongoing loop is possible if position is greater than loopEnd. | 
|  | 561 | mPosLoop.mLoopCount = 0; | 
|  | 562 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 563 | (void) mMutator.push(mState); | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | void StaticAudioTrackClientProxy::setBufferPositionAndLoop(size_t position, size_t loopStart, | 
|  | 567 | size_t loopEnd, int loopCount) | 
|  | 568 | { | 
|  | 569 | setLoop(loopStart, loopEnd, loopCount); | 
|  | 570 | setBufferPosition(position); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 571 | } | 
|  | 572 |  | 
|  | 573 | size_t StaticAudioTrackClientProxy::getBufferPosition() | 
|  | 574 | { | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 575 | getBufferPositionAndLoopCount(NULL, NULL); | 
|  | 576 | return mPosLoop.mBufferPosition; | 
|  | 577 | } | 
|  | 578 |  | 
|  | 579 | void StaticAudioTrackClientProxy::getBufferPositionAndLoopCount( | 
|  | 580 | size_t *position, int *loopCount) | 
|  | 581 | { | 
|  | 582 | if (mMutator.ack() == StaticAudioTrackSingleStateQueue::SSQ_DONE) { | 
|  | 583 | if (mPosLoopObserver.poll(mPosLoop)) { | 
|  | 584 | ; // a valid mPosLoop should be available if ackDone is true. | 
|  | 585 | } | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 586 | } | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 587 | if (position != NULL) { | 
|  | 588 | *position = mPosLoop.mBufferPosition; | 
|  | 589 | } | 
|  | 590 | if (loopCount != NULL) { | 
|  | 591 | *loopCount = mPosLoop.mLoopCount; | 
|  | 592 | } | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 593 | } | 
|  | 594 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 595 | // --------------------------------------------------------------------------- | 
|  | 596 |  | 
|  | 597 | ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, | 
|  | 598 | size_t frameSize, bool isOut, bool clientInServer) | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 599 | : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 600 | mAvailToClient(0), mFlush(0), mReleased(0) | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 601 | { | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 602 | } | 
|  | 603 |  | 
| Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 604 | status_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 605 | { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 606 | LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 607 | if (mIsShutdown) { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 608 | goto no_init; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 609 | } | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 610 | { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 611 | audio_track_cblk_t* cblk = mCblk; | 
|  | 612 | // compute number of frames available to write (AudioTrack) or read (AudioRecord), | 
|  | 613 | // or use previous cached value from framesReady(), with added barrier if it omits. | 
|  | 614 | int32_t front; | 
|  | 615 | int32_t rear; | 
|  | 616 | // See notes on barriers at ClientProxy::obtainBuffer() | 
|  | 617 | if (mIsOut) { | 
|  | 618 | int32_t flush = cblk->u.mStreaming.mFlush; | 
|  | 619 | rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 620 | front = cblk->u.mStreaming.mFront; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 621 | if (flush != mFlush) { | 
| Glenn Kasten | 050501d | 2013-07-11 10:35:38 -0700 | [diff] [blame] | 622 | // effectively obtain then release whatever is in the buffer | 
| Andy Hung | a2d75cd | 2015-07-15 17:04:20 -0700 | [diff] [blame] | 623 | const size_t overflowBit = mFrameCountP2 << 1; | 
|  | 624 | const size_t mask = overflowBit - 1; | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 625 | int32_t newFront = (front & ~mask) | (flush & mask); | 
|  | 626 | ssize_t filled = rear - newFront; | 
| Andy Hung | a2d75cd | 2015-07-15 17:04:20 -0700 | [diff] [blame] | 627 | if (filled >= (ssize_t)overflowBit) { | 
|  | 628 | // front and rear offsets span the overflow bit of the p2 mask | 
|  | 629 | // so rebasing newFront on the front offset is off by the overflow bit. | 
|  | 630 | // adjust newFront to match rear offset. | 
| Glenn Kasten | dbd0f3c | 2015-07-17 11:04:04 -0700 | [diff] [blame] | 631 | ALOGV("flush wrap: filled %zx >= overflowBit %zx", filled, overflowBit); | 
| Andy Hung | a2d75cd | 2015-07-15 17:04:20 -0700 | [diff] [blame] | 632 | newFront += overflowBit; | 
|  | 633 | filled -= overflowBit; | 
|  | 634 | } | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 635 | // Rather than shutting down on a corrupt flush, just treat it as a full flush | 
|  | 636 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { | 
| Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 637 | ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, " | 
| Lajos Molnar | f1063e2 | 2015-04-17 15:19:42 -0700 | [diff] [blame] | 638 | "filled %zd=%#x", | 
|  | 639 | mFlush, flush, front, rear, | 
|  | 640 | (unsigned)mask, newFront, filled, (unsigned)filled); | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 641 | newFront = rear; | 
|  | 642 | } | 
|  | 643 | mFlush = flush; | 
|  | 644 | android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront); | 
|  | 645 | // There is no danger from a false positive, so err on the side of caution | 
|  | 646 | if (true /*front != newFront*/) { | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 647 | int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); | 
|  | 648 | if (!(old & CBLK_FUTEX_WAKE)) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 649 | (void) syscall(__NR_futex, &cblk->mFutex, | 
|  | 650 | mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 651 | } | 
|  | 652 | } | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 653 | front = newFront; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 654 | } | 
|  | 655 | } else { | 
|  | 656 | front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); | 
|  | 657 | rear = cblk->u.mStreaming.mRear; | 
|  | 658 | } | 
|  | 659 | ssize_t filled = rear - front; | 
|  | 660 | // pipe should not already be overfull | 
|  | 661 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { | 
| Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 662 | ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 663 | mIsShutdown = true; | 
|  | 664 | } | 
|  | 665 | if (mIsShutdown) { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 666 | goto no_init; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 667 | } | 
|  | 668 | // don't allow filling pipe beyond the nominal size | 
|  | 669 | size_t availToServer; | 
|  | 670 | if (mIsOut) { | 
|  | 671 | availToServer = filled; | 
|  | 672 | mAvailToClient = mFrameCount - filled; | 
|  | 673 | } else { | 
|  | 674 | availToServer = mFrameCount - filled; | 
|  | 675 | mAvailToClient = filled; | 
|  | 676 | } | 
|  | 677 | // 'availToServer' may be non-contiguous, so return only the first contiguous chunk | 
|  | 678 | size_t part1; | 
|  | 679 | if (mIsOut) { | 
|  | 680 | front &= mFrameCountP2 - 1; | 
|  | 681 | part1 = mFrameCountP2 - front; | 
|  | 682 | } else { | 
|  | 683 | rear &= mFrameCountP2 - 1; | 
|  | 684 | part1 = mFrameCountP2 - rear; | 
|  | 685 | } | 
|  | 686 | if (part1 > availToServer) { | 
|  | 687 | part1 = availToServer; | 
|  | 688 | } | 
|  | 689 | size_t ask = buffer->mFrameCount; | 
|  | 690 | if (part1 > ask) { | 
|  | 691 | part1 = ask; | 
|  | 692 | } | 
|  | 693 | // is assignment redundant in some cases? | 
|  | 694 | buffer->mFrameCount = part1; | 
|  | 695 | buffer->mRaw = part1 > 0 ? | 
|  | 696 | &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL; | 
|  | 697 | buffer->mNonContig = availToServer - part1; | 
| Glenn Kasten | 2e422c4 | 2013-10-18 13:00:29 -0700 | [diff] [blame] | 698 | // After flush(), allow releaseBuffer() on a previously obtained buffer; | 
|  | 699 | // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp. | 
|  | 700 | if (!ackFlush) { | 
|  | 701 | mUnreleased = part1; | 
|  | 702 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 703 | return part1 > 0 ? NO_ERROR : WOULD_BLOCK; | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 704 | } | 
|  | 705 | no_init: | 
|  | 706 | buffer->mFrameCount = 0; | 
|  | 707 | buffer->mRaw = NULL; | 
|  | 708 | buffer->mNonContig = 0; | 
|  | 709 | mUnreleased = 0; | 
|  | 710 | return NO_INIT; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 711 | } | 
|  | 712 |  | 
|  | 713 | void ServerProxy::releaseBuffer(Buffer* buffer) | 
|  | 714 | { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 715 | LOG_ALWAYS_FATAL_IF(buffer == NULL); | 
|  | 716 | size_t stepCount = buffer->mFrameCount; | 
|  | 717 | if (stepCount == 0 || mIsShutdown) { | 
|  | 718 | // prevent accidental re-use of buffer | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 719 | buffer->mFrameCount = 0; | 
|  | 720 | buffer->mRaw = NULL; | 
|  | 721 | buffer->mNonContig = 0; | 
|  | 722 | return; | 
|  | 723 | } | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 724 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 725 | mUnreleased -= stepCount; | 
|  | 726 | audio_track_cblk_t* cblk = mCblk; | 
|  | 727 | if (mIsOut) { | 
|  | 728 | int32_t front = cblk->u.mStreaming.mFront; | 
|  | 729 | android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); | 
|  | 730 | } else { | 
|  | 731 | int32_t rear = cblk->u.mStreaming.mRear; | 
|  | 732 | android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); | 
|  | 733 | } | 
|  | 734 |  | 
| Glenn Kasten | 844f88c | 2014-05-09 13:38:09 -0700 | [diff] [blame] | 735 | cblk->mServer += stepCount; | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 736 | mReleased += stepCount; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 737 |  | 
|  | 738 | size_t half = mFrameCount / 2; | 
|  | 739 | if (half == 0) { | 
|  | 740 | half = 1; | 
|  | 741 | } | 
| Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 742 | size_t minimum = (size_t) cblk->mMinimum; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 743 | if (minimum == 0) { | 
|  | 744 | minimum = mIsOut ? half : 1; | 
|  | 745 | } else if (minimum > half) { | 
|  | 746 | minimum = half; | 
|  | 747 | } | 
| Glenn Kasten | 93bb77d | 2013-06-24 12:10:45 -0700 | [diff] [blame] | 748 | // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time | 
| Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 749 | if (!mIsOut || (mAvailToClient + stepCount >= minimum)) { | 
| Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 750 | ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum); | 
| Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 751 | int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); | 
|  | 752 | if (!(old & CBLK_FUTEX_WAKE)) { | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 753 | (void) syscall(__NR_futex, &cblk->mFutex, | 
|  | 754 | mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 755 | } | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | buffer->mFrameCount = 0; | 
|  | 759 | buffer->mRaw = NULL; | 
|  | 760 | buffer->mNonContig = 0; | 
|  | 761 | } | 
|  | 762 |  | 
|  | 763 | // --------------------------------------------------------------------------- | 
|  | 764 |  | 
|  | 765 | size_t AudioTrackServerProxy::framesReady() | 
|  | 766 | { | 
|  | 767 | LOG_ALWAYS_FATAL_IF(!mIsOut); | 
|  | 768 |  | 
|  | 769 | if (mIsShutdown) { | 
|  | 770 | return 0; | 
|  | 771 | } | 
|  | 772 | audio_track_cblk_t* cblk = mCblk; | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 773 |  | 
|  | 774 | int32_t flush = cblk->u.mStreaming.mFlush; | 
|  | 775 | if (flush != mFlush) { | 
| Glenn Kasten | 20f51b1 | 2014-10-30 10:43:19 -0700 | [diff] [blame] | 776 | // FIXME should return an accurate value, but over-estimate is better than under-estimate | 
| Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 777 | return mFrameCount; | 
|  | 778 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 779 | // the acquire might not be necessary since not doing a subsequent read | 
|  | 780 | int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); | 
|  | 781 | ssize_t filled = rear - cblk->u.mStreaming.mFront; | 
|  | 782 | // pipe should not already be overfull | 
|  | 783 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { | 
| Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 784 | ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 785 | mIsShutdown = true; | 
|  | 786 | return 0; | 
|  | 787 | } | 
|  | 788 | //  cache this value for later use by obtainBuffer(), with added barrier | 
|  | 789 | //  and racy if called by normal mixer thread | 
|  | 790 | // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer() | 
|  | 791 | return filled; | 
|  | 792 | } | 
|  | 793 |  | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 794 | bool  AudioTrackServerProxy::setStreamEndDone() { | 
| Glenn Kasten | 844f88c | 2014-05-09 13:38:09 -0700 | [diff] [blame] | 795 | audio_track_cblk_t* cblk = mCblk; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 796 | bool old = | 
| Glenn Kasten | 844f88c | 2014-05-09 13:38:09 -0700 | [diff] [blame] | 797 | (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0; | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 798 | if (!old) { | 
| Elliott Hughes | e348c5b | 2014-05-21 18:47:50 -0700 | [diff] [blame] | 799 | (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, | 
| Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 800 | 1); | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 801 | } | 
|  | 802 | return old; | 
|  | 803 | } | 
|  | 804 |  | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 805 | void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) | 
|  | 806 | { | 
| Glenn Kasten | 844f88c | 2014-05-09 13:38:09 -0700 | [diff] [blame] | 807 | audio_track_cblk_t* cblk = mCblk; | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 808 | if (frameCount > 0) { | 
|  | 809 | cblk->u.mStreaming.mUnderrunFrames += frameCount; | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 810 |  | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 811 | if (!mUnderrunning) { // start of underrun? | 
|  | 812 | mUnderrunCount++; | 
|  | 813 | cblk->u.mStreaming.mUnderrunCount = mUnderrunCount; | 
|  | 814 | mUnderrunning = true; | 
|  | 815 | ALOGV("tallyUnderrunFrames(%3u) at uf = %u, bump mUnderrunCount = %u", | 
|  | 816 | frameCount, cblk->u.mStreaming.mUnderrunFrames, mUnderrunCount); | 
|  | 817 | } | 
|  | 818 |  | 
|  | 819 | // FIXME also wake futex so that underrun is noticed more quickly | 
|  | 820 | (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags); | 
|  | 821 | } else { | 
|  | 822 | ALOGV_IF(mUnderrunning, | 
|  | 823 | "tallyUnderrunFrames(%3u) at uf = %u, underrun finished", | 
|  | 824 | frameCount, cblk->u.mStreaming.mUnderrunFrames); | 
|  | 825 | mUnderrunning = false; // so we can detect the next edge | 
|  | 826 | } | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 827 | } | 
|  | 828 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 829 | AudioPlaybackRate AudioTrackServerProxy::getPlaybackRate() | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 830 | {   // do not call from multiple threads without holding lock | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 831 | mPlaybackRateObserver.poll(mPlaybackRate); | 
|  | 832 | return mPlaybackRate; | 
| Andy Hung | 8edb8dc | 2015-03-26 19:13:55 -0700 | [diff] [blame] | 833 | } | 
|  | 834 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 835 | // --------------------------------------------------------------------------- | 
|  | 836 |  | 
|  | 837 | StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, | 
|  | 838 | size_t frameCount, size_t frameSize) | 
|  | 839 | : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize), | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 840 | mObserver(&cblk->u.mStatic.mSingleStateQueue), | 
|  | 841 | mPosLoopMutator(&cblk->u.mStatic.mPosLoopQueue), | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 842 | mFramesReadySafe(frameCount), mFramesReady(frameCount), | 
|  | 843 | mFramesReadyIsCalledByMultipleThreads(false) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 844 | { | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 845 | memset(&mState, 0, sizeof(mState)); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 846 | } | 
|  | 847 |  | 
|  | 848 | void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads() | 
|  | 849 | { | 
|  | 850 | mFramesReadyIsCalledByMultipleThreads = true; | 
|  | 851 | } | 
|  | 852 |  | 
|  | 853 | size_t StaticAudioTrackServerProxy::framesReady() | 
|  | 854 | { | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 855 | // Can't call pollPosition() from multiple threads. | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 856 | if (!mFramesReadyIsCalledByMultipleThreads) { | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 857 | (void) pollPosition(); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 858 | } | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 859 | return mFramesReadySafe; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 860 | } | 
|  | 861 |  | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 862 | status_t StaticAudioTrackServerProxy::updateStateWithLoop( | 
|  | 863 | StaticAudioTrackState *localState, const StaticAudioTrackState &update) const | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 864 | { | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 865 | if (localState->mLoopSequence != update.mLoopSequence) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 866 | bool valid = false; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 867 | const size_t loopStart = update.mLoopStart; | 
|  | 868 | const size_t loopEnd = update.mLoopEnd; | 
|  | 869 | size_t position = localState->mPosition; | 
|  | 870 | if (update.mLoopCount == 0) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 871 | valid = true; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 872 | } else if (update.mLoopCount >= -1) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 873 | if (loopStart < loopEnd && loopEnd <= mFrameCount && | 
|  | 874 | loopEnd - loopStart >= MIN_LOOP) { | 
| Andy Hung | 680b795 | 2014-11-12 13:18:52 -0800 | [diff] [blame] | 875 | // If the current position is greater than the end of the loop | 
|  | 876 | // we "wrap" to the loop start. This might cause an audible pop. | 
|  | 877 | if (position >= loopEnd) { | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 878 | position = loopStart; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 879 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 880 | valid = true; | 
|  | 881 | } | 
|  | 882 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 883 | if (!valid || position > mFrameCount) { | 
|  | 884 | return NO_INIT; | 
|  | 885 | } | 
|  | 886 | localState->mPosition = position; | 
|  | 887 | localState->mLoopCount = update.mLoopCount; | 
|  | 888 | localState->mLoopEnd = loopEnd; | 
|  | 889 | localState->mLoopStart = loopStart; | 
|  | 890 | localState->mLoopSequence = update.mLoopSequence; | 
|  | 891 | } | 
|  | 892 | return OK; | 
|  | 893 | } | 
|  | 894 |  | 
|  | 895 | status_t StaticAudioTrackServerProxy::updateStateWithPosition( | 
|  | 896 | StaticAudioTrackState *localState, const StaticAudioTrackState &update) const | 
|  | 897 | { | 
|  | 898 | if (localState->mPositionSequence != update.mPositionSequence) { | 
|  | 899 | if (update.mPosition > mFrameCount) { | 
|  | 900 | return NO_INIT; | 
|  | 901 | } else if (localState->mLoopCount != 0 && update.mPosition >= localState->mLoopEnd) { | 
|  | 902 | localState->mLoopCount = 0; // disable loop count if position is beyond loop end. | 
|  | 903 | } | 
|  | 904 | localState->mPosition = update.mPosition; | 
|  | 905 | localState->mPositionSequence = update.mPositionSequence; | 
|  | 906 | } | 
|  | 907 | return OK; | 
|  | 908 | } | 
|  | 909 |  | 
|  | 910 | ssize_t StaticAudioTrackServerProxy::pollPosition() | 
|  | 911 | { | 
|  | 912 | StaticAudioTrackState state; | 
|  | 913 | if (mObserver.poll(state)) { | 
|  | 914 | StaticAudioTrackState trystate = mState; | 
|  | 915 | bool result; | 
| Chad Brubaker | cb50c54 | 2015-10-07 14:20:10 -0700 | [diff] [blame] | 916 | const int32_t diffSeq = (int32_t) state.mLoopSequence - (int32_t) state.mPositionSequence; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 917 |  | 
|  | 918 | if (diffSeq < 0) { | 
|  | 919 | result = updateStateWithLoop(&trystate, state) == OK && | 
|  | 920 | updateStateWithPosition(&trystate, state) == OK; | 
|  | 921 | } else { | 
|  | 922 | result = updateStateWithPosition(&trystate, state) == OK && | 
|  | 923 | updateStateWithLoop(&trystate, state) == OK; | 
|  | 924 | } | 
|  | 925 | if (!result) { | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 926 | mObserver.done(); | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 927 | // caution: no update occurs so server state will be inconsistent with client state. | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 928 | ALOGE("%s client pushed an invalid state, shutting down", __func__); | 
|  | 929 | mIsShutdown = true; | 
|  | 930 | return (ssize_t) NO_INIT; | 
|  | 931 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 932 | mState = trystate; | 
|  | 933 | if (mState.mLoopCount == -1) { | 
|  | 934 | mFramesReady = INT64_MAX; | 
|  | 935 | } else if (mState.mLoopCount == 0) { | 
|  | 936 | mFramesReady = mFrameCount - mState.mPosition; | 
|  | 937 | } else if (mState.mLoopCount > 0) { | 
|  | 938 | // TODO: Later consider fixing overflow, but does not seem needed now | 
|  | 939 | // as will not overflow if loopStart and loopEnd are Java "ints". | 
|  | 940 | mFramesReady = int64_t(mState.mLoopCount) * (mState.mLoopEnd - mState.mLoopStart) | 
|  | 941 | + mFrameCount - mState.mPosition; | 
|  | 942 | } | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 943 | mFramesReadySafe = clampToSize(mFramesReady); | 
| Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 944 | // This may overflow, but client is not supposed to rely on it | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 945 | StaticAudioTrackPosLoop posLoop; | 
|  | 946 |  | 
|  | 947 | posLoop.mLoopCount = (int32_t) mState.mLoopCount; | 
|  | 948 | posLoop.mBufferPosition = (uint32_t) mState.mPosition; | 
|  | 949 | mPosLoopMutator.push(posLoop); | 
|  | 950 | mObserver.done(); // safe to read mStatic variables. | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 951 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 952 | return (ssize_t) mState.mPosition; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 953 | } | 
|  | 954 |  | 
| Andy Hung | 954ca45 | 2015-09-09 14:39:02 -0700 | [diff] [blame] | 955 | status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush) | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 956 | { | 
|  | 957 | if (mIsShutdown) { | 
|  | 958 | buffer->mFrameCount = 0; | 
|  | 959 | buffer->mRaw = NULL; | 
|  | 960 | buffer->mNonContig = 0; | 
|  | 961 | mUnreleased = 0; | 
|  | 962 | return NO_INIT; | 
|  | 963 | } | 
|  | 964 | ssize_t positionOrStatus = pollPosition(); | 
|  | 965 | if (positionOrStatus < 0) { | 
|  | 966 | buffer->mFrameCount = 0; | 
|  | 967 | buffer->mRaw = NULL; | 
|  | 968 | buffer->mNonContig = 0; | 
|  | 969 | mUnreleased = 0; | 
|  | 970 | return (status_t) positionOrStatus; | 
|  | 971 | } | 
|  | 972 | size_t position = (size_t) positionOrStatus; | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 973 | size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 974 | size_t avail; | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 975 | if (position < end) { | 
|  | 976 | avail = end - position; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 977 | size_t wanted = buffer->mFrameCount; | 
|  | 978 | if (avail < wanted) { | 
|  | 979 | buffer->mFrameCount = avail; | 
|  | 980 | } else { | 
|  | 981 | avail = wanted; | 
|  | 982 | } | 
|  | 983 | buffer->mRaw = &((char *) mBuffers)[position * mFrameSize]; | 
|  | 984 | } else { | 
|  | 985 | avail = 0; | 
|  | 986 | buffer->mFrameCount = 0; | 
|  | 987 | buffer->mRaw = NULL; | 
|  | 988 | } | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 989 | // As mFramesReady is the total remaining frames in the static audio track, | 
|  | 990 | // it is always larger or equal to avail. | 
| Andy Hung | 486a713 | 2014-12-22 16:54:21 -0800 | [diff] [blame] | 991 | LOG_ALWAYS_FATAL_IF(mFramesReady < (int64_t) avail); | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 992 | buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail); | 
| Andy Hung | 954ca45 | 2015-09-09 14:39:02 -0700 | [diff] [blame] | 993 | if (!ackFlush) { | 
|  | 994 | mUnreleased = avail; | 
|  | 995 | } | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 996 | return NO_ERROR; | 
|  | 997 | } | 
|  | 998 |  | 
|  | 999 | void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer) | 
|  | 1000 | { | 
|  | 1001 | size_t stepCount = buffer->mFrameCount; | 
| Andy Hung | 486a713 | 2014-12-22 16:54:21 -0800 | [diff] [blame] | 1002 | LOG_ALWAYS_FATAL_IF(!((int64_t) stepCount <= mFramesReady)); | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 1003 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased)); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1004 | if (stepCount == 0) { | 
| Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 1005 | // prevent accidental re-use of buffer | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1006 | buffer->mRaw = NULL; | 
|  | 1007 | buffer->mNonContig = 0; | 
|  | 1008 | return; | 
|  | 1009 | } | 
|  | 1010 | mUnreleased -= stepCount; | 
|  | 1011 | audio_track_cblk_t* cblk = mCblk; | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 1012 | size_t position = mState.mPosition; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1013 | size_t newPosition = position + stepCount; | 
|  | 1014 | int32_t setFlags = 0; | 
|  | 1015 | if (!(position <= newPosition && newPosition <= mFrameCount)) { | 
| Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 1016 | ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position, | 
|  | 1017 | mFrameCount); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1018 | newPosition = mFrameCount; | 
|  | 1019 | } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) { | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 1020 | newPosition = mState.mLoopStart; | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1021 | if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1022 | setFlags = CBLK_LOOP_CYCLE; | 
|  | 1023 | } else { | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1024 | setFlags = CBLK_LOOP_FINAL; | 
|  | 1025 | } | 
|  | 1026 | } | 
|  | 1027 | if (newPosition == mFrameCount) { | 
|  | 1028 | setFlags |= CBLK_BUFFER_END; | 
|  | 1029 | } | 
| Andy Hung | 9b46158 | 2014-12-01 17:56:29 -0800 | [diff] [blame] | 1030 | mState.mPosition = newPosition; | 
| Andy Hung | cb2129b | 2014-11-11 12:17:22 -0800 | [diff] [blame] | 1031 | if (mFramesReady != INT64_MAX) { | 
|  | 1032 | mFramesReady -= stepCount; | 
|  | 1033 | } | 
|  | 1034 | mFramesReadySafe = clampToSize(mFramesReady); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1035 |  | 
| Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 1036 | cblk->mServer += stepCount; | 
| Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 1037 | mReleased += stepCount; | 
|  | 1038 |  | 
| Glenn Kasten | fdac7c0 | 2014-01-28 11:03:28 -0800 | [diff] [blame] | 1039 | // This may overflow, but client is not supposed to rely on it | 
| Andy Hung | 4ede21d | 2014-12-12 15:37:34 -0800 | [diff] [blame] | 1040 | StaticAudioTrackPosLoop posLoop; | 
|  | 1041 | posLoop.mBufferPosition = mState.mPosition; | 
|  | 1042 | posLoop.mLoopCount = mState.mLoopCount; | 
|  | 1043 | mPosLoopMutator.push(posLoop); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1044 | if (setFlags != 0) { | 
| Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1045 | (void) android_atomic_or(setFlags, &cblk->mFlags); | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1046 | // this would be a good place to wake a futex | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | buffer->mFrameCount = 0; | 
|  | 1050 | buffer->mRaw = NULL; | 
|  | 1051 | buffer->mNonContig = 0; | 
|  | 1052 | } | 
|  | 1053 |  | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 1054 | void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 1055 | { | 
|  | 1056 | // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks, | 
|  | 1057 | // we don't have a location to count underrun frames.  The underrun frame counter | 
|  | 1058 | // only exists in AudioTrackSharedStreaming.  Fortunately, underruns are not | 
|  | 1059 | // possible for static buffer tracks other than at end of buffer, so this is not a loss. | 
|  | 1060 |  | 
|  | 1061 | // FIXME also wake futex so that underrun is noticed more quickly | 
| Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 1062 | if (frameCount > 0) { | 
|  | 1063 | (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags); | 
|  | 1064 | } | 
| Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 1065 | } | 
|  | 1066 |  | 
| Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1067 | // --------------------------------------------------------------------------- | 
|  | 1068 |  | 
| Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 1069 | }   // namespace android |