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