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> |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 22 | extern "C" { |
| 23 | #include "../private/bionic_futex.h" |
| 24 | } |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | audio_track_cblk_t::audio_track_cblk_t() |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 29 | : mServer(0), frameCount_(0), mFutex(0), mMinimum(0), |
Glenn Kasten | d054c32 | 2013-07-12 12:59:20 -0700 | [diff] [blame] | 30 | mVolumeLR(0x10001000), mSampleRate(0), mSendLevel(0), mFlags(0) |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 31 | { |
| 32 | memset(&u, 0, sizeof(u)); |
| 33 | } |
| 34 | |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
| 37 | Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, |
| 38 | bool isOut, bool clientInServer) |
| 39 | : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize), |
| 40 | mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer), |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 41 | mIsShutdown(false), mUnreleased(0) |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 48 | size_t frameSize, bool isOut, bool clientInServer) |
| 49 | : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), mEpoch(0) |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 50 | { |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 53 | const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/}; |
| 54 | const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/}; |
| 55 | |
| 56 | #define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS |
| 57 | |
| 58 | // To facilitate quicker recovery from server failure, this value limits the timeout per each futex |
| 59 | // wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit. |
| 60 | // FIXME May not be compatible with audio tunneling requirements where timeout should be in the |
| 61 | // order of minutes. |
| 62 | #define MAX_SEC 5 |
| 63 | |
| 64 | status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested, |
| 65 | struct timespec *elapsed) |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 66 | { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 67 | LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 68 | struct timespec total; // total elapsed time spent waiting |
| 69 | total.tv_sec = 0; |
| 70 | total.tv_nsec = 0; |
| 71 | bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 72 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 73 | status_t status; |
| 74 | enum { |
| 75 | TIMEOUT_ZERO, // requested == NULL || *requested == 0 |
| 76 | TIMEOUT_INFINITE, // *requested == infinity |
| 77 | TIMEOUT_FINITE, // 0 < *requested < infinity |
| 78 | TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE |
| 79 | } timeout; |
| 80 | if (requested == NULL) { |
| 81 | timeout = TIMEOUT_ZERO; |
| 82 | } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { |
| 83 | timeout = TIMEOUT_ZERO; |
| 84 | } else if (requested->tv_sec == INT_MAX) { |
| 85 | timeout = TIMEOUT_INFINITE; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 86 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 87 | timeout = TIMEOUT_FINITE; |
| 88 | if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) { |
| 89 | measure = true; |
| 90 | } |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 91 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 92 | struct timespec before; |
| 93 | bool beforeIsValid = false; |
| 94 | audio_track_cblk_t* cblk = mCblk; |
| 95 | bool ignoreInitialPendingInterrupt = true; |
| 96 | // check for shared memory corruption |
| 97 | if (mIsShutdown) { |
| 98 | status = NO_INIT; |
| 99 | goto end; |
| 100 | } |
| 101 | for (;;) { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 102 | int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 103 | // check for track invalidation by server, or server death detection |
| 104 | if (flags & CBLK_INVALID) { |
| 105 | ALOGV("Track invalidated"); |
| 106 | status = DEAD_OBJECT; |
| 107 | goto end; |
| 108 | } |
| 109 | // check for obtainBuffer interrupted by client |
| 110 | if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) { |
| 111 | ALOGV("obtainBuffer() interrupted by client"); |
| 112 | status = -EINTR; |
| 113 | goto end; |
| 114 | } |
| 115 | ignoreInitialPendingInterrupt = false; |
| 116 | // compute number of frames available to write (AudioTrack) or read (AudioRecord) |
| 117 | int32_t front; |
| 118 | int32_t rear; |
| 119 | if (mIsOut) { |
| 120 | // The barrier following the read of mFront is probably redundant. |
| 121 | // We're about to perform a conditional branch based on 'filled', |
| 122 | // which will force the processor to observe the read of mFront |
| 123 | // prior to allowing data writes starting at mRaw. |
| 124 | // However, the processor may support speculative execution, |
| 125 | // and be unable to undo speculative writes into shared memory. |
| 126 | // The barrier will prevent such speculative execution. |
| 127 | front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); |
| 128 | rear = cblk->u.mStreaming.mRear; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 129 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 130 | // On the other hand, this barrier is required. |
| 131 | rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); |
| 132 | front = cblk->u.mStreaming.mFront; |
| 133 | } |
| 134 | ssize_t filled = rear - front; |
| 135 | // pipe should not be overfull |
| 136 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { |
| 137 | ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); |
| 138 | mIsShutdown = true; |
| 139 | status = NO_INIT; |
| 140 | goto end; |
| 141 | } |
| 142 | // don't allow filling pipe beyond the nominal size |
| 143 | size_t avail = mIsOut ? mFrameCount - filled : filled; |
| 144 | if (avail > 0) { |
| 145 | // 'avail' may be non-contiguous, so return only the first contiguous chunk |
| 146 | size_t part1; |
| 147 | if (mIsOut) { |
| 148 | rear &= mFrameCountP2 - 1; |
| 149 | part1 = mFrameCountP2 - rear; |
| 150 | } else { |
| 151 | front &= mFrameCountP2 - 1; |
| 152 | part1 = mFrameCountP2 - front; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 153 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 154 | if (part1 > avail) { |
| 155 | part1 = avail; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 156 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 157 | if (part1 > buffer->mFrameCount) { |
| 158 | part1 = buffer->mFrameCount; |
| 159 | } |
| 160 | buffer->mFrameCount = part1; |
| 161 | buffer->mRaw = part1 > 0 ? |
| 162 | &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL; |
| 163 | buffer->mNonContig = avail - part1; |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 164 | mUnreleased = part1; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 165 | status = NO_ERROR; |
| 166 | break; |
| 167 | } |
| 168 | struct timespec remaining; |
| 169 | const struct timespec *ts; |
| 170 | switch (timeout) { |
| 171 | case TIMEOUT_ZERO: |
| 172 | status = WOULD_BLOCK; |
| 173 | goto end; |
| 174 | case TIMEOUT_INFINITE: |
| 175 | ts = NULL; |
| 176 | break; |
| 177 | case TIMEOUT_FINITE: |
| 178 | timeout = TIMEOUT_CONTINUE; |
| 179 | if (MAX_SEC == 0) { |
| 180 | ts = requested; |
| 181 | break; |
| 182 | } |
| 183 | // fall through |
| 184 | case TIMEOUT_CONTINUE: |
| 185 | // FIXME we do not retry if requested < 10ms? needs documentation on this state machine |
| 186 | if (!measure || requested->tv_sec < total.tv_sec || |
| 187 | (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { |
| 188 | status = TIMED_OUT; |
| 189 | goto end; |
| 190 | } |
| 191 | remaining.tv_sec = requested->tv_sec - total.tv_sec; |
| 192 | if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { |
| 193 | remaining.tv_nsec += 1000000000; |
| 194 | remaining.tv_sec++; |
| 195 | } |
| 196 | if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { |
| 197 | remaining.tv_sec = MAX_SEC; |
| 198 | remaining.tv_nsec = 0; |
| 199 | } |
| 200 | ts = &remaining; |
| 201 | break; |
| 202 | default: |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 203 | LOG_FATAL("obtainBuffer() timeout=%d", timeout); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 204 | ts = NULL; |
| 205 | break; |
| 206 | } |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 207 | int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); |
| 208 | if (!(old & CBLK_FUTEX_WAKE)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 209 | int rc; |
| 210 | if (measure && !beforeIsValid) { |
| 211 | clock_gettime(CLOCK_MONOTONIC, &before); |
| 212 | beforeIsValid = true; |
| 213 | } |
| 214 | int ret = __futex_syscall4(&cblk->mFutex, |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 215 | mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 216 | // update total elapsed time spent waiting |
| 217 | if (measure) { |
| 218 | struct timespec after; |
| 219 | clock_gettime(CLOCK_MONOTONIC, &after); |
| 220 | total.tv_sec += after.tv_sec - before.tv_sec; |
| 221 | long deltaNs = after.tv_nsec - before.tv_nsec; |
| 222 | if (deltaNs < 0) { |
| 223 | deltaNs += 1000000000; |
| 224 | total.tv_sec--; |
| 225 | } |
| 226 | if ((total.tv_nsec += deltaNs) >= 1000000000) { |
| 227 | total.tv_nsec -= 1000000000; |
| 228 | total.tv_sec++; |
| 229 | } |
| 230 | before = after; |
| 231 | beforeIsValid = true; |
| 232 | } |
| 233 | switch (ret) { |
| 234 | case 0: // normal wakeup by server, or by binderDied() |
| 235 | case -EWOULDBLOCK: // benign race condition with server |
| 236 | case -EINTR: // wait was interrupted by signal or other spurious wakeup |
| 237 | case -ETIMEDOUT: // time-out expired |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 238 | // FIXME these error/non-0 status are being dropped |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 239 | break; |
| 240 | default: |
| 241 | ALOGE("%s unexpected error %d", __func__, ret); |
| 242 | status = -ret; |
| 243 | goto end; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | end: |
| 249 | if (status != NO_ERROR) { |
| 250 | buffer->mFrameCount = 0; |
| 251 | buffer->mRaw = NULL; |
| 252 | buffer->mNonContig = 0; |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 253 | mUnreleased = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 254 | } |
| 255 | if (elapsed != NULL) { |
| 256 | *elapsed = total; |
| 257 | } |
| 258 | if (requested == NULL) { |
| 259 | requested = &kNonBlocking; |
| 260 | } |
| 261 | if (measure) { |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 262 | ALOGV("requested %ld.%03ld elapsed %ld.%03ld", |
| 263 | requested->tv_sec, requested->tv_nsec / 1000000, |
| 264 | total.tv_sec, total.tv_nsec / 1000000); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 265 | } |
| 266 | return status; |
| 267 | } |
| 268 | |
| 269 | void ClientProxy::releaseBuffer(Buffer* buffer) |
| 270 | { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 271 | LOG_ALWAYS_FATAL_IF(buffer == NULL); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 272 | size_t stepCount = buffer->mFrameCount; |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 273 | if (stepCount == 0 || mIsShutdown) { |
| 274 | // prevent accidental re-use of buffer |
| 275 | buffer->mFrameCount = 0; |
| 276 | buffer->mRaw = NULL; |
| 277 | buffer->mNonContig = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 278 | return; |
| 279 | } |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 280 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); |
| 281 | mUnreleased -= stepCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 282 | audio_track_cblk_t* cblk = mCblk; |
| 283 | // Both of these barriers are required |
| 284 | if (mIsOut) { |
| 285 | int32_t rear = cblk->u.mStreaming.mRear; |
| 286 | android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); |
| 287 | } else { |
| 288 | int32_t front = cblk->u.mStreaming.mFront; |
| 289 | android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void ClientProxy::binderDied() |
| 294 | { |
| 295 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 296 | if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 297 | // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process |
| 298 | (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, |
| 299 | 1); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void ClientProxy::interrupt() |
| 304 | { |
| 305 | audio_track_cblk_t* cblk = mCblk; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 306 | if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 307 | (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, |
| 308 | 1); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | size_t ClientProxy::getMisalignment() |
| 313 | { |
| 314 | audio_track_cblk_t* cblk = mCblk; |
| 315 | return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) & |
| 316 | (mFrameCountP2 - 1); |
| 317 | } |
| 318 | |
| 319 | // --------------------------------------------------------------------------- |
| 320 | |
| 321 | void AudioTrackClientProxy::flush() |
| 322 | { |
| 323 | mCblk->u.mStreaming.mFlush++; |
| 324 | } |
| 325 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 326 | bool AudioTrackClientProxy::clearStreamEndDone() { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 327 | 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] | 328 | } |
| 329 | |
| 330 | bool AudioTrackClientProxy::getStreamEndDone() const { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 331 | return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 334 | status_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested) |
| 335 | { |
| 336 | struct timespec total; // total elapsed time spent waiting |
| 337 | total.tv_sec = 0; |
| 338 | total.tv_nsec = 0; |
| 339 | audio_track_cblk_t* cblk = mCblk; |
| 340 | status_t status; |
| 341 | enum { |
| 342 | TIMEOUT_ZERO, // requested == NULL || *requested == 0 |
| 343 | TIMEOUT_INFINITE, // *requested == infinity |
| 344 | TIMEOUT_FINITE, // 0 < *requested < infinity |
| 345 | TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE |
| 346 | } timeout; |
| 347 | if (requested == NULL) { |
| 348 | timeout = TIMEOUT_ZERO; |
| 349 | } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { |
| 350 | timeout = TIMEOUT_ZERO; |
| 351 | } else if (requested->tv_sec == INT_MAX) { |
| 352 | timeout = TIMEOUT_INFINITE; |
| 353 | } else { |
| 354 | timeout = TIMEOUT_FINITE; |
| 355 | } |
| 356 | for (;;) { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 357 | 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] | 358 | // check for track invalidation by server, or server death detection |
| 359 | if (flags & CBLK_INVALID) { |
| 360 | ALOGV("Track invalidated"); |
| 361 | status = DEAD_OBJECT; |
| 362 | goto end; |
| 363 | } |
| 364 | if (flags & CBLK_STREAM_END_DONE) { |
| 365 | ALOGV("stream end received"); |
| 366 | status = NO_ERROR; |
| 367 | goto end; |
| 368 | } |
| 369 | // check for obtainBuffer interrupted by client |
| 370 | // check for obtainBuffer interrupted by client |
| 371 | if (flags & CBLK_INTERRUPT) { |
| 372 | ALOGV("waitStreamEndDone() interrupted by client"); |
| 373 | status = -EINTR; |
| 374 | goto end; |
| 375 | } |
| 376 | struct timespec remaining; |
| 377 | const struct timespec *ts; |
| 378 | switch (timeout) { |
| 379 | case TIMEOUT_ZERO: |
| 380 | status = WOULD_BLOCK; |
| 381 | goto end; |
| 382 | case TIMEOUT_INFINITE: |
| 383 | ts = NULL; |
| 384 | break; |
| 385 | case TIMEOUT_FINITE: |
| 386 | timeout = TIMEOUT_CONTINUE; |
| 387 | if (MAX_SEC == 0) { |
| 388 | ts = requested; |
| 389 | break; |
| 390 | } |
| 391 | // fall through |
| 392 | case TIMEOUT_CONTINUE: |
| 393 | // FIXME we do not retry if requested < 10ms? needs documentation on this state machine |
| 394 | if (requested->tv_sec < total.tv_sec || |
| 395 | (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { |
| 396 | status = TIMED_OUT; |
| 397 | goto end; |
| 398 | } |
| 399 | remaining.tv_sec = requested->tv_sec - total.tv_sec; |
| 400 | if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { |
| 401 | remaining.tv_nsec += 1000000000; |
| 402 | remaining.tv_sec++; |
| 403 | } |
| 404 | if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { |
| 405 | remaining.tv_sec = MAX_SEC; |
| 406 | remaining.tv_nsec = 0; |
| 407 | } |
| 408 | ts = &remaining; |
| 409 | break; |
| 410 | default: |
| 411 | LOG_FATAL("waitStreamEndDone() timeout=%d", timeout); |
| 412 | ts = NULL; |
| 413 | break; |
| 414 | } |
| 415 | int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); |
| 416 | if (!(old & CBLK_FUTEX_WAKE)) { |
| 417 | int rc; |
| 418 | int ret = __futex_syscall4(&cblk->mFutex, |
| 419 | mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); |
| 420 | switch (ret) { |
| 421 | case 0: // normal wakeup by server, or by binderDied() |
| 422 | case -EWOULDBLOCK: // benign race condition with server |
| 423 | case -EINTR: // wait was interrupted by signal or other spurious wakeup |
| 424 | case -ETIMEDOUT: // time-out expired |
| 425 | break; |
| 426 | default: |
| 427 | ALOGE("%s unexpected error %d", __func__, ret); |
| 428 | status = -ret; |
| 429 | goto end; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | end: |
| 435 | if (requested == NULL) { |
| 436 | requested = &kNonBlocking; |
| 437 | } |
| 438 | return status; |
| 439 | } |
| 440 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 441 | // --------------------------------------------------------------------------- |
| 442 | |
| 443 | StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, |
| 444 | size_t frameCount, size_t frameSize) |
| 445 | : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize), |
| 446 | mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0) |
| 447 | { |
| 448 | } |
| 449 | |
| 450 | void StaticAudioTrackClientProxy::flush() |
| 451 | { |
| 452 | LOG_FATAL("static flush"); |
| 453 | } |
| 454 | |
| 455 | void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount) |
| 456 | { |
| 457 | StaticAudioTrackState newState; |
| 458 | newState.mLoopStart = loopStart; |
| 459 | newState.mLoopEnd = loopEnd; |
| 460 | newState.mLoopCount = loopCount; |
| 461 | mBufferPosition = loopStart; |
| 462 | (void) mMutator.push(newState); |
| 463 | } |
| 464 | |
| 465 | size_t StaticAudioTrackClientProxy::getBufferPosition() |
| 466 | { |
| 467 | size_t bufferPosition; |
| 468 | if (mMutator.ack()) { |
| 469 | bufferPosition = mCblk->u.mStatic.mBufferPosition; |
| 470 | if (bufferPosition > mFrameCount) { |
| 471 | bufferPosition = mFrameCount; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 472 | } |
| 473 | } else { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 474 | bufferPosition = mBufferPosition; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 475 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 476 | return bufferPosition; |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 479 | // --------------------------------------------------------------------------- |
| 480 | |
| 481 | ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, |
| 482 | size_t frameSize, bool isOut, bool clientInServer) |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 483 | : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), |
Glenn Kasten | ce8828a | 2013-09-16 18:07:38 -0700 | [diff] [blame] | 484 | mAvailToClient(0), mFlush(0) |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 485 | { |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 488 | status_t ServerProxy::obtainBuffer(Buffer* buffer) |
| 489 | { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 490 | LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 491 | if (mIsShutdown) { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 492 | goto no_init; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 493 | } |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 494 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 495 | audio_track_cblk_t* cblk = mCblk; |
| 496 | // compute number of frames available to write (AudioTrack) or read (AudioRecord), |
| 497 | // or use previous cached value from framesReady(), with added barrier if it omits. |
| 498 | int32_t front; |
| 499 | int32_t rear; |
| 500 | // See notes on barriers at ClientProxy::obtainBuffer() |
| 501 | if (mIsOut) { |
| 502 | int32_t flush = cblk->u.mStreaming.mFlush; |
| 503 | rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 504 | front = cblk->u.mStreaming.mFront; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 505 | if (flush != mFlush) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 506 | mFlush = flush; |
Glenn Kasten | 050501d | 2013-07-11 10:35:38 -0700 | [diff] [blame] | 507 | // effectively obtain then release whatever is in the buffer |
| 508 | android_atomic_release_store(rear, &cblk->u.mStreaming.mFront); |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 509 | if (front != rear) { |
| 510 | int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); |
| 511 | if (!(old & CBLK_FUTEX_WAKE)) { |
| 512 | (void) __futex_syscall3(&cblk->mFutex, |
| 513 | mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); |
| 514 | } |
| 515 | } |
| 516 | front = rear; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 517 | } |
| 518 | } else { |
| 519 | front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); |
| 520 | rear = cblk->u.mStreaming.mRear; |
| 521 | } |
| 522 | ssize_t filled = rear - front; |
| 523 | // pipe should not already be overfull |
| 524 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { |
| 525 | ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); |
| 526 | mIsShutdown = true; |
| 527 | } |
| 528 | if (mIsShutdown) { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 529 | goto no_init; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 530 | } |
| 531 | // don't allow filling pipe beyond the nominal size |
| 532 | size_t availToServer; |
| 533 | if (mIsOut) { |
| 534 | availToServer = filled; |
| 535 | mAvailToClient = mFrameCount - filled; |
| 536 | } else { |
| 537 | availToServer = mFrameCount - filled; |
| 538 | mAvailToClient = filled; |
| 539 | } |
| 540 | // 'availToServer' may be non-contiguous, so return only the first contiguous chunk |
| 541 | size_t part1; |
| 542 | if (mIsOut) { |
| 543 | front &= mFrameCountP2 - 1; |
| 544 | part1 = mFrameCountP2 - front; |
| 545 | } else { |
| 546 | rear &= mFrameCountP2 - 1; |
| 547 | part1 = mFrameCountP2 - rear; |
| 548 | } |
| 549 | if (part1 > availToServer) { |
| 550 | part1 = availToServer; |
| 551 | } |
| 552 | size_t ask = buffer->mFrameCount; |
| 553 | if (part1 > ask) { |
| 554 | part1 = ask; |
| 555 | } |
| 556 | // is assignment redundant in some cases? |
| 557 | buffer->mFrameCount = part1; |
| 558 | buffer->mRaw = part1 > 0 ? |
| 559 | &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL; |
| 560 | buffer->mNonContig = availToServer - part1; |
| 561 | mUnreleased = part1; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 562 | return part1 > 0 ? NO_ERROR : WOULD_BLOCK; |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 563 | } |
| 564 | no_init: |
| 565 | buffer->mFrameCount = 0; |
| 566 | buffer->mRaw = NULL; |
| 567 | buffer->mNonContig = 0; |
| 568 | mUnreleased = 0; |
| 569 | return NO_INIT; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void ServerProxy::releaseBuffer(Buffer* buffer) |
| 573 | { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 574 | LOG_ALWAYS_FATAL_IF(buffer == NULL); |
| 575 | size_t stepCount = buffer->mFrameCount; |
| 576 | if (stepCount == 0 || mIsShutdown) { |
| 577 | // prevent accidental re-use of buffer |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 578 | buffer->mFrameCount = 0; |
| 579 | buffer->mRaw = NULL; |
| 580 | buffer->mNonContig = 0; |
| 581 | return; |
| 582 | } |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 583 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 584 | mUnreleased -= stepCount; |
| 585 | audio_track_cblk_t* cblk = mCblk; |
| 586 | if (mIsOut) { |
| 587 | int32_t front = cblk->u.mStreaming.mFront; |
| 588 | android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); |
| 589 | } else { |
| 590 | int32_t rear = cblk->u.mStreaming.mRear; |
| 591 | android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); |
| 592 | } |
| 593 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 594 | mCblk->mServer += stepCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 595 | |
| 596 | size_t half = mFrameCount / 2; |
| 597 | if (half == 0) { |
| 598 | half = 1; |
| 599 | } |
| 600 | size_t minimum = cblk->mMinimum; |
| 601 | if (minimum == 0) { |
| 602 | minimum = mIsOut ? half : 1; |
| 603 | } else if (minimum > half) { |
| 604 | minimum = half; |
| 605 | } |
Glenn Kasten | 93bb77d | 2013-06-24 12:10:45 -0700 | [diff] [blame] | 606 | // 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] | 607 | if (!mIsOut || (mAvailToClient + stepCount >= minimum)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 608 | ALOGV("mAvailToClient=%u stepCount=%u minimum=%u", mAvailToClient, stepCount, minimum); |
Glenn Kasten | 0d09a9b | 2013-06-24 12:06:46 -0700 | [diff] [blame] | 609 | int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); |
| 610 | if (!(old & CBLK_FUTEX_WAKE)) { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 611 | (void) __futex_syscall3(&cblk->mFutex, |
| 612 | mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | buffer->mFrameCount = 0; |
| 617 | buffer->mRaw = NULL; |
| 618 | buffer->mNonContig = 0; |
| 619 | } |
| 620 | |
| 621 | // --------------------------------------------------------------------------- |
| 622 | |
| 623 | size_t AudioTrackServerProxy::framesReady() |
| 624 | { |
| 625 | LOG_ALWAYS_FATAL_IF(!mIsOut); |
| 626 | |
| 627 | if (mIsShutdown) { |
| 628 | return 0; |
| 629 | } |
| 630 | audio_track_cblk_t* cblk = mCblk; |
Richard Fitzgerald | b1a270d | 2013-05-14 12:12:21 +0100 | [diff] [blame] | 631 | |
| 632 | int32_t flush = cblk->u.mStreaming.mFlush; |
| 633 | if (flush != mFlush) { |
| 634 | return mFrameCount; |
| 635 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 636 | // the acquire might not be necessary since not doing a subsequent read |
| 637 | int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); |
| 638 | ssize_t filled = rear - cblk->u.mStreaming.mFront; |
| 639 | // pipe should not already be overfull |
| 640 | if (!(0 <= filled && (size_t) filled <= mFrameCount)) { |
| 641 | ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); |
| 642 | mIsShutdown = true; |
| 643 | return 0; |
| 644 | } |
| 645 | // cache this value for later use by obtainBuffer(), with added barrier |
| 646 | // and racy if called by normal mixer thread |
| 647 | // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer() |
| 648 | return filled; |
| 649 | } |
| 650 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 651 | bool AudioTrackServerProxy::setStreamEndDone() { |
| 652 | bool old = |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 653 | (android_atomic_or(CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 654 | if (!old) { |
| 655 | (void) __futex_syscall3(&mCblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, |
| 656 | 1); |
| 657 | } |
| 658 | return old; |
| 659 | } |
| 660 | |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 661 | void AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) |
| 662 | { |
| 663 | mCblk->u.mStreaming.mUnderrunFrames += frameCount; |
| 664 | |
| 665 | // FIXME also wake futex so that underrun is noticed more quickly |
| 666 | (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags); |
| 667 | } |
| 668 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 669 | // --------------------------------------------------------------------------- |
| 670 | |
| 671 | StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, |
| 672 | size_t frameCount, size_t frameSize) |
| 673 | : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize), |
| 674 | mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0), |
| 675 | mEnd(frameCount), mFramesReadyIsCalledByMultipleThreads(false) |
| 676 | { |
| 677 | mState.mLoopStart = 0; |
| 678 | mState.mLoopEnd = 0; |
| 679 | mState.mLoopCount = 0; |
| 680 | } |
| 681 | |
| 682 | void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads() |
| 683 | { |
| 684 | mFramesReadyIsCalledByMultipleThreads = true; |
| 685 | } |
| 686 | |
| 687 | size_t StaticAudioTrackServerProxy::framesReady() |
| 688 | { |
| 689 | // FIXME |
| 690 | // This is racy if called by normal mixer thread, |
| 691 | // as we're reading 2 independent variables without a lock. |
| 692 | // Can't call mObserver.poll(), as we might be called from wrong thread. |
| 693 | // If looping is enabled, should return a higher number (since includes non-contiguous). |
| 694 | size_t position = mPosition; |
| 695 | if (!mFramesReadyIsCalledByMultipleThreads) { |
| 696 | ssize_t positionOrStatus = pollPosition(); |
| 697 | if (positionOrStatus >= 0) { |
| 698 | position = (size_t) positionOrStatus; |
| 699 | } |
| 700 | } |
| 701 | size_t end = mEnd; |
| 702 | return position < end ? end - position : 0; |
| 703 | } |
| 704 | |
| 705 | ssize_t StaticAudioTrackServerProxy::pollPosition() |
| 706 | { |
| 707 | size_t position = mPosition; |
| 708 | StaticAudioTrackState state; |
| 709 | if (mObserver.poll(state)) { |
| 710 | bool valid = false; |
| 711 | size_t loopStart = state.mLoopStart; |
| 712 | size_t loopEnd = state.mLoopEnd; |
| 713 | if (state.mLoopCount == 0) { |
| 714 | if (loopStart > mFrameCount) { |
| 715 | loopStart = mFrameCount; |
| 716 | } |
| 717 | // ignore loopEnd |
| 718 | mPosition = position = loopStart; |
| 719 | mEnd = mFrameCount; |
| 720 | mState.mLoopCount = 0; |
| 721 | valid = true; |
| 722 | } else { |
| 723 | if (loopStart < loopEnd && loopEnd <= mFrameCount && |
| 724 | loopEnd - loopStart >= MIN_LOOP) { |
| 725 | if (!(loopStart <= position && position < loopEnd)) { |
| 726 | mPosition = position = loopStart; |
| 727 | } |
| 728 | mEnd = loopEnd; |
| 729 | mState = state; |
| 730 | valid = true; |
| 731 | } |
| 732 | } |
| 733 | if (!valid) { |
| 734 | ALOGE("%s client pushed an invalid state, shutting down", __func__); |
| 735 | mIsShutdown = true; |
| 736 | return (ssize_t) NO_INIT; |
| 737 | } |
| 738 | mCblk->u.mStatic.mBufferPosition = position; |
| 739 | } |
| 740 | return (ssize_t) position; |
| 741 | } |
| 742 | |
| 743 | status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer) |
| 744 | { |
| 745 | if (mIsShutdown) { |
| 746 | buffer->mFrameCount = 0; |
| 747 | buffer->mRaw = NULL; |
| 748 | buffer->mNonContig = 0; |
| 749 | mUnreleased = 0; |
| 750 | return NO_INIT; |
| 751 | } |
| 752 | ssize_t positionOrStatus = pollPosition(); |
| 753 | if (positionOrStatus < 0) { |
| 754 | buffer->mFrameCount = 0; |
| 755 | buffer->mRaw = NULL; |
| 756 | buffer->mNonContig = 0; |
| 757 | mUnreleased = 0; |
| 758 | return (status_t) positionOrStatus; |
| 759 | } |
| 760 | size_t position = (size_t) positionOrStatus; |
| 761 | size_t avail; |
| 762 | if (position < mEnd) { |
| 763 | avail = mEnd - position; |
| 764 | size_t wanted = buffer->mFrameCount; |
| 765 | if (avail < wanted) { |
| 766 | buffer->mFrameCount = avail; |
| 767 | } else { |
| 768 | avail = wanted; |
| 769 | } |
| 770 | buffer->mRaw = &((char *) mBuffers)[position * mFrameSize]; |
| 771 | } else { |
| 772 | avail = 0; |
| 773 | buffer->mFrameCount = 0; |
| 774 | buffer->mRaw = NULL; |
| 775 | } |
| 776 | buffer->mNonContig = 0; // FIXME should be > 0 for looping |
| 777 | mUnreleased = avail; |
| 778 | return NO_ERROR; |
| 779 | } |
| 780 | |
| 781 | void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer) |
| 782 | { |
| 783 | size_t stepCount = buffer->mFrameCount; |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 784 | LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased)); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 785 | if (stepCount == 0) { |
Glenn Kasten | 7db7df0 | 2013-06-25 16:13:23 -0700 | [diff] [blame] | 786 | // prevent accidental re-use of buffer |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 787 | buffer->mRaw = NULL; |
| 788 | buffer->mNonContig = 0; |
| 789 | return; |
| 790 | } |
| 791 | mUnreleased -= stepCount; |
| 792 | audio_track_cblk_t* cblk = mCblk; |
| 793 | size_t position = mPosition; |
| 794 | size_t newPosition = position + stepCount; |
| 795 | int32_t setFlags = 0; |
| 796 | if (!(position <= newPosition && newPosition <= mFrameCount)) { |
| 797 | ALOGW("%s newPosition %u outside [%u, %u]", __func__, newPosition, position, mFrameCount); |
| 798 | newPosition = mFrameCount; |
| 799 | } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) { |
| 800 | if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) { |
| 801 | newPosition = mState.mLoopStart; |
| 802 | setFlags = CBLK_LOOP_CYCLE; |
| 803 | } else { |
| 804 | mEnd = mFrameCount; // this is what allows playback to continue after the loop |
| 805 | setFlags = CBLK_LOOP_FINAL; |
| 806 | } |
| 807 | } |
| 808 | if (newPosition == mFrameCount) { |
| 809 | setFlags |= CBLK_BUFFER_END; |
| 810 | } |
| 811 | mPosition = newPosition; |
| 812 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 813 | cblk->mServer += stepCount; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 814 | cblk->u.mStatic.mBufferPosition = newPosition; |
| 815 | if (setFlags != 0) { |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 816 | (void) android_atomic_or(setFlags, &cblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 817 | // this would be a good place to wake a futex |
| 818 | } |
| 819 | |
| 820 | buffer->mFrameCount = 0; |
| 821 | buffer->mRaw = NULL; |
| 822 | buffer->mNonContig = 0; |
| 823 | } |
| 824 | |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 825 | void StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) |
| 826 | { |
| 827 | // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks, |
| 828 | // we don't have a location to count underrun frames. The underrun frame counter |
| 829 | // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not |
| 830 | // possible for static buffer tracks other than at end of buffer, so this is not a loss. |
| 831 | |
| 832 | // FIXME also wake futex so that underrun is noticed more quickly |
| 833 | (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags); |
| 834 | } |
| 835 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 836 | // --------------------------------------------------------------------------- |
| 837 | |
Glenn Kasten | a8190fc | 2012-12-03 17:06:56 -0800 | [diff] [blame] | 838 | } // namespace android |