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