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