1a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten/* 2a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * Copyright (C) 2007 The Android Open Source Project 3a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * 4a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License"); 5a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * you may not use this file except in compliance with the License. 6a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * You may obtain a copy of the License at 7a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * 8a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * http://www.apache.org/licenses/LICENSE-2.0 9a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * 10a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * Unless required by applicable law or agreed to in writing, software 11a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS, 12a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * See the License for the specific language governing permissions and 14a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten * limitations under the License. 15a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten */ 16a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 17a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten#define LOG_TAG "AudioTrackShared" 18a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten//#define LOG_NDEBUG 0 19a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 20a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten#include <private/media/AudioTrackShared.h> 21a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten#include <utils/Log.h> 22ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes 23ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes#include <linux/futex.h> 24ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes#include <sys/syscall.h> 25a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 26a8190fc518b6769257896605f3aee091aeb60b50Glenn Kastennamespace android { 27a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 28cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung// used to clamp a value to size_t. TODO: move to another file. 29cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hungtemplate <typename T> 30cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hungsize_t clampToSize(T x) { 31486a7135593b4b40c067e9b06f393577abfef482Andy Hung return sizeof(T) > sizeof(size_t) && x > (T) SIZE_MAX ? SIZE_MAX : x < 0 ? 0 : (size_t) x; 32cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung} 33cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung 349b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung// incrementSequence is used to determine the next sequence value 359b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung// for the loop and position sequence counters. It should return 369b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung// a value between "other" + 1 and "other" + INT32_MAX, the choice of 379b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung// which needs to be the "least recently used" sequence value for "self". 389b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung// In general, this means (new_self) returned is max(self, other) + 1. 399b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 409b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungstatic uint32_t incrementSequence(uint32_t self, uint32_t other) { 41cb50c54b31a83ac598530780fa8c145a3cb4a2deChad Brubaker int32_t diff = (int32_t) self - (int32_t) other; 429b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (diff >= 0 && diff < INT32_MAX) { 439b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return self + 1; // we're already ahead of other. 449b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 459b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return other + 1; // we're behind, so move just ahead of other. 469b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung} 479b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 48a8190fc518b6769257896605f3aee091aeb60b50Glenn Kastenaudio_track_cblk_t::audio_track_cblk_t() 49e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk : mServer(0), mFutex(0), mMinimum(0) 50e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk , mVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY), mSampleRate(0), mSendLevel(0) 51e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk , mBufferSizeInFrames(0) 52e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk , mFlags(0) 53a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten memset(&u, 0, sizeof(u)); 55a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 56a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn KastenProxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, 609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten bool isOut, bool clientInServer) 619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize), 629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer), 637db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten mIsShutdown(false), mUnreleased(0) 64a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 66a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn KastenClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, 709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t frameSize, bool isOut, bool clientInServer) 71c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer) 72c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk , mEpoch(0) 736ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung , mTimestampObserver(&cblk->mExtendedTimestampQueue) 749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 75e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk setBufferSizeInFrames(frameCount); 769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenconst struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/}; 799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenconst struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/}; 809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS 829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// To facilitate quicker recovery from server failure, this value limits the timeout per each futex 849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit. 859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// FIXME May not be compatible with audio tunneling requirements where timeout should be in the 869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// order of minutes. 879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten#define MAX_SEC 5 889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 89e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burkuint32_t ClientProxy::setBufferSizeInFrames(uint32_t size) 90e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk{ 91e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk // The minimum should be greater than zero and less than the size 92e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk // at which underruns will occur. 9326760d1a1105b6b318c837a491cdb96f6901804ePhil Burk const uint32_t minimum = 16; // based on AudioMixer::BLOCKSIZE 94e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk const uint32_t maximum = frameCount(); 95e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk uint32_t clippedSize = size; 9626760d1a1105b6b318c837a491cdb96f6901804ePhil Burk if (maximum < minimum) { 9726760d1a1105b6b318c837a491cdb96f6901804ePhil Burk clippedSize = maximum; 9826760d1a1105b6b318c837a491cdb96f6901804ePhil Burk } else if (clippedSize < minimum) { 99e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk clippedSize = minimum; 100e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk } else if (clippedSize > maximum) { 101e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk clippedSize = maximum; 102e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk } 103e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk // for server to read 104e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk android_atomic_release_store(clippedSize, (int32_t *)&mCblk->mBufferSizeInFrames); 105e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk // for client to read 106e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk mBufferSizeInFrames = clippedSize; 107e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk return clippedSize; 108e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk} 109e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk 110926b82fa9e22739686602f7ae8cf24105f14d82eilewis__attribute__((no_sanitize("integer"))) 1119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenstatus_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested, 1129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten struct timespec *elapsed) 1139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 1147db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); 1159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten struct timespec total; // total elapsed time spent waiting 1169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_sec = 0; 1179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_nsec = 0; 1189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting 119a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 1209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status_t status; 1219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten enum { 1229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten TIMEOUT_ZERO, // requested == NULL || *requested == 0 1239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten TIMEOUT_INFINITE, // *requested == infinity 1249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten TIMEOUT_FINITE, // 0 < *requested < infinity 1259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE 1269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } timeout; 1279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (requested == NULL) { 1289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten timeout = TIMEOUT_ZERO; 1299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { 1309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten timeout = TIMEOUT_ZERO; 1319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else if (requested->tv_sec == INT_MAX) { 1329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten timeout = TIMEOUT_INFINITE; 1339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 1349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten timeout = TIMEOUT_FINITE; 1359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) { 1369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten measure = true; 1379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten struct timespec before; 1409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten bool beforeIsValid = false; 1419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 1429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten bool ignoreInitialPendingInterrupt = true; 1439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // check for shared memory corruption 1449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsShutdown) { 1459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = NO_INIT; 1469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 1479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten for (;;) { 14996f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->mFlags); 1509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // check for track invalidation by server, or server death detection 1519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (flags & CBLK_INVALID) { 1529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ALOGV("Track invalidated"); 1539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = DEAD_OBJECT; 1549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 1559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1564d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent if (flags & CBLK_DISABLED) { 1574d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent ALOGV("Track disabled"); 1584d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent status = NOT_ENOUGH_DATA; 1594d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent goto end; 1604d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent } 1619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // check for obtainBuffer interrupted by client 1629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) { 1639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ALOGV("obtainBuffer() interrupted by client"); 1649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = -EINTR; 1659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 1669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ignoreInitialPendingInterrupt = false; 1689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // compute number of frames available to write (AudioTrack) or read (AudioRecord) 1699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t front; 1709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t rear; 1719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 1729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // The barrier following the read of mFront is probably redundant. 1739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // We're about to perform a conditional branch based on 'filled', 1749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // which will force the processor to observe the read of mFront 1759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // prior to allowing data writes starting at mRaw. 1769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // However, the processor may support speculative execution, 1779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // and be unable to undo speculative writes into shared memory. 1789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // The barrier will prevent such speculative execution. 1799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); 1809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear = cblk->u.mStreaming.mRear; 1819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 1829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // On the other hand, this barrier is required. 1839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 1849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten front = cblk->u.mStreaming.mFront; 1859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 186c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk // write to rear, read from front 1879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ssize_t filled = rear - front; 1889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // pipe should not be overfull 1899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 1906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten if (mIsOut) { 19134fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn ALOGE("Shared memory control block is corrupt (filled=%zd, mFrameCount=%zu); " 1926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten "shutting down", filled, mFrameCount); 1936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten mIsShutdown = true; 1946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten status = NO_INIT; 1956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten goto end; 1966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten } 1976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // for input, sync up on overrun 1986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten filled = 0; 1996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten cblk->u.mStreaming.mFront = rear; 2006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten (void) android_atomic_or(CBLK_OVERRUN, &cblk->mFlags); 2019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 202c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk // Don't allow filling pipe beyond the user settable size. 203c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk // The calculation for avail can go negative if the buffer size 204c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk // is suddenly dropped below the amount already in the buffer. 205c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk // So use a signed calculation to prevent a numeric overflow abort. 206e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk ssize_t adjustableSize = (ssize_t) getBufferSizeInFrames(); 207c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk ssize_t avail = (mIsOut) ? adjustableSize - filled : filled; 208c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk if (avail < 0) { 209c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk avail = 0; 210c0adecb800b197cb8c028513130ebabf2d0f37baPhil Burk } else if (avail > 0) { 2119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // 'avail' may be non-contiguous, so return only the first contiguous chunk 212bdd8101cd487cdfe36726aa0d78c24008c35d605Eric Laurent size_t part1; 2139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 2149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear &= mFrameCountP2 - 1; 2159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = mFrameCountP2 - rear; 2169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 2179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten front &= mFrameCountP2 - 1; 2189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = mFrameCountP2 - front; 2199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 220bdd8101cd487cdfe36726aa0d78c24008c35d605Eric Laurent if (part1 > (size_t)avail) { 2219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = avail; 2229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 223bdd8101cd487cdfe36726aa0d78c24008c35d605Eric Laurent if (part1 > buffer->mFrameCount) { 2249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = buffer->mFrameCount; 2259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 226bdd8101cd487cdfe36726aa0d78c24008c35d605Eric Laurent buffer->mFrameCount = part1; 2279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = part1 > 0 ? 2289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL; 2299f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = avail - part1; 2307db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten mUnreleased = part1; 2319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = NO_ERROR; 2329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 2339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten struct timespec remaining; 2359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten const struct timespec *ts; 2369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten switch (timeout) { 2379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten case TIMEOUT_ZERO: 2389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = WOULD_BLOCK; 2399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 2409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten case TIMEOUT_INFINITE: 2419f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ts = NULL; 2429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 2439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten case TIMEOUT_FINITE: 2449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten timeout = TIMEOUT_CONTINUE; 2459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (MAX_SEC == 0) { 2469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ts = requested; 2479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 2489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // fall through 2509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten case TIMEOUT_CONTINUE: 2519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // FIXME we do not retry if requested < 10ms? needs documentation on this state machine 2529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!measure || requested->tv_sec < total.tv_sec || 2539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { 2549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten status = TIMED_OUT; 2559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 2569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten remaining.tv_sec = requested->tv_sec - total.tv_sec; 2589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { 2599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten remaining.tv_nsec += 1000000000; 2609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten remaining.tv_sec++; 2619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { 2639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten remaining.tv_sec = MAX_SEC; 2649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten remaining.tv_nsec = 0; 2659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ts = &remaining; 2679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 2689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten default: 269adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("obtainBuffer() timeout=%d", timeout); 2709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ts = NULL; 2719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 2729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2730d09a9bec07b3bec78bd473ff0bfcf0a261f3f25Glenn Kasten int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); 2740d09a9bec07b3bec78bd473ff0bfcf0a261f3f25Glenn Kasten if (!(old & CBLK_FUTEX_WAKE)) { 2759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (measure && !beforeIsValid) { 2769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten clock_gettime(CLOCK_MONOTONIC, &before); 2779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten beforeIsValid = true; 2789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 279ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes errno = 0; 280ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, 2810d09a9bec07b3bec78bd473ff0bfcf0a261f3f25Glenn Kasten mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); 282b463da8e499149c7230b8dbd839bb7bc8ece602fLeena Winterrowd status_t error = errno; // clock_gettime can affect errno 2839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // update total elapsed time spent waiting 2849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (measure) { 2859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten struct timespec after; 2869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten clock_gettime(CLOCK_MONOTONIC, &after); 2879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_sec += after.tv_sec - before.tv_sec; 2889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten long deltaNs = after.tv_nsec - before.tv_nsec; 2899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (deltaNs < 0) { 2909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten deltaNs += 1000000000; 2919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_sec--; 2929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if ((total.tv_nsec += deltaNs) >= 1000000000) { 2949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_nsec -= 1000000000; 2959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten total.tv_sec++; 2969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 2979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten before = after; 2989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten beforeIsValid = true; 2999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 300b463da8e499149c7230b8dbd839bb7bc8ece602fLeena Winterrowd switch (error) { 301ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case 0: // normal wakeup by server, or by binderDied() 302ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case EWOULDBLOCK: // benign race condition with server 303ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case EINTR: // wait was interrupted by signal or other spurious wakeup 304ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case ETIMEDOUT: // time-out expired 3057db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten // FIXME these error/non-0 status are being dropped 3069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten break; 3079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten default: 308b463da8e499149c7230b8dbd839bb7bc8ece602fLeena Winterrowd status = error; 309ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes ALOGE("%s unexpected error %s", __func__, strerror(status)); 3109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten goto end; 3119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 312a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 313a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 314a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 3159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenend: 3169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (status != NO_ERROR) { 3179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 3189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 3199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 3207db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten mUnreleased = 0; 3219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 3229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (elapsed != NULL) { 3239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten *elapsed = total; 324a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 3259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (requested == NULL) { 3269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten requested = &kNonBlocking; 3279f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 3289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (measure) { 329b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ALOGV("requested %ld.%03ld elapsed %ld.%03ld", 330b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald requested->tv_sec, requested->tv_nsec / 1000000, 331b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald total.tv_sec, total.tv_nsec / 1000000); 3329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 3339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return status; 3349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 335a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 336926b82fa9e22739686602f7ae8cf24105f14d82eilewis__attribute__((no_sanitize("integer"))) 3379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid ClientProxy::releaseBuffer(Buffer* buffer) 3389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 3397db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(buffer == NULL); 3409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t stepCount = buffer->mFrameCount; 3417db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten if (stepCount == 0 || mIsShutdown) { 3427db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten // prevent accidental re-use of buffer 3437db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mFrameCount = 0; 3447db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mRaw = NULL; 3457db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mNonContig = 0; 3469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return; 3479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 3487db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); 3497db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten mUnreleased -= stepCount; 3509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 3519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // Both of these barriers are required 3529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 3539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t rear = cblk->u.mStreaming.mRear; 3549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); 3559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 3569f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t front = cblk->u.mStreaming.mFront; 3579f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); 3589f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 359a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 360a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 3619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid ClientProxy::binderDied() 362a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 3639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 36496f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten if (!(android_atomic_or(CBLK_INVALID, &cblk->mFlags) & CBLK_INVALID)) { 36582a69ea8b090d57bff5e02774688b546a711bbb8zunkyu.lee android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); 3669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process 367ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 368ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes 1); 3699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 3709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 371a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 3729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid ClientProxy::interrupt() 3739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 3749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 37596f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->mFlags) & CBLK_INTERRUPT)) { 37682a69ea8b090d57bff5e02774688b546a711bbb8zunkyu.lee android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); 377ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 378ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes 1); 379a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 3809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 381a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 38265dda4f09425ad027e27a51a3108472448d3ab8bChad Brubaker__attribute__((no_sanitize("integer"))) 3839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastensize_t ClientProxy::getMisalignment() 3849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 3859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 3869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) & 3879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten (mFrameCountP2 - 1); 3889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 389a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 3909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 3919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 3929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid AudioTrackClientProxy::flush() 3939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 39420f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten // This works for mFrameCountP2 <= 2^30 39520f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten size_t increment = mFrameCountP2 << 1; 39620f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten size_t mask = increment - 1; 39720f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten audio_track_cblk_t* cblk = mCblk; 398a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // mFlush is 32 bits concatenated as [ flush_counter ] [ newfront_offset ] 399a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // Should newFlush = cblk->u.mStreaming.mRear? Only problem is 400a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // if you want to flush twice to the same rear location after a 32 bit wrap. 40120f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten int32_t newFlush = (cblk->u.mStreaming.mRear & mask) | 40220f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten ((cblk->u.mStreaming.mFlush & ~mask) + increment); 40320f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten android_atomic_release_store(newFlush, &cblk->u.mStreaming.mFlush); 4049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 4059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioTrackClientProxy::clearStreamEndDone() { 40796f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten return (android_atomic_and(~CBLK_STREAM_END_DONE, &mCblk->mFlags) & CBLK_STREAM_END_DONE) != 0; 408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 409bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 410bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioTrackClientProxy::getStreamEndDone() const { 41196f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten return (mCblk->mFlags & CBLK_STREAM_END_DONE) != 0; 412bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 413bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 414b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgeraldstatus_t AudioTrackClientProxy::waitStreamEndDone(const struct timespec *requested) 415b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald{ 416b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald struct timespec total; // total elapsed time spent waiting 417b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald total.tv_sec = 0; 418b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald total.tv_nsec = 0; 419b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald audio_track_cblk_t* cblk = mCblk; 420b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status_t status; 421b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald enum { 422b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald TIMEOUT_ZERO, // requested == NULL || *requested == 0 423b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald TIMEOUT_INFINITE, // *requested == infinity 424b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald TIMEOUT_FINITE, // 0 < *requested < infinity 425b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE 426b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } timeout; 427b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (requested == NULL) { 428b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald timeout = TIMEOUT_ZERO; 429b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { 430b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald timeout = TIMEOUT_ZERO; 431b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } else if (requested->tv_sec == INT_MAX) { 432b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald timeout = TIMEOUT_INFINITE; 433b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } else { 434b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald timeout = TIMEOUT_FINITE; 435b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 436b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald for (;;) { 43796f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten int32_t flags = android_atomic_and(~(CBLK_INTERRUPT|CBLK_STREAM_END_DONE), &cblk->mFlags); 438b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald // check for track invalidation by server, or server death detection 439b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (flags & CBLK_INVALID) { 440b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ALOGV("Track invalidated"); 441b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status = DEAD_OBJECT; 442b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 443b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 4444d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent // a track is not supposed to underrun at this stage but consider it done 4454d231dc0ee34380956c71cbe18a750e487a69601Eric Laurent if (flags & (CBLK_STREAM_END_DONE | CBLK_DISABLED)) { 446b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ALOGV("stream end received"); 447b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status = NO_ERROR; 448b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 449b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 450b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald // check for obtainBuffer interrupted by client 451b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (flags & CBLK_INTERRUPT) { 452b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ALOGV("waitStreamEndDone() interrupted by client"); 453b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status = -EINTR; 454b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 455b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 456b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald struct timespec remaining; 457b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald const struct timespec *ts; 458b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald switch (timeout) { 459b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald case TIMEOUT_ZERO: 460b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status = WOULD_BLOCK; 461b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 462b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald case TIMEOUT_INFINITE: 463b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ts = NULL; 464b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald break; 465b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald case TIMEOUT_FINITE: 466b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald timeout = TIMEOUT_CONTINUE; 467b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (MAX_SEC == 0) { 468b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ts = requested; 469b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald break; 470b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 471b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald // fall through 472b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald case TIMEOUT_CONTINUE: 473b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald // FIXME we do not retry if requested < 10ms? needs documentation on this state machine 474b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (requested->tv_sec < total.tv_sec || 475b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { 476b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald status = TIMED_OUT; 477b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 478b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 479b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald remaining.tv_sec = requested->tv_sec - total.tv_sec; 480b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { 481b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald remaining.tv_nsec += 1000000000; 482b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald remaining.tv_sec++; 483b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 484b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { 485b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald remaining.tv_sec = MAX_SEC; 486b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald remaining.tv_nsec = 0; 487b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 488b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ts = &remaining; 489b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald break; 490b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald default: 491adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("waitStreamEndDone() timeout=%d", timeout); 492b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald ts = NULL; 493b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald break; 494b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 495b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); 496b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (!(old & CBLK_FUTEX_WAKE)) { 497ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes errno = 0; 498ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, 499b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); 500ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes switch (errno) { 501ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case 0: // normal wakeup by server, or by binderDied() 502ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case EWOULDBLOCK: // benign race condition with server 503ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case EINTR: // wait was interrupted by signal or other spurious wakeup 504ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes case ETIMEDOUT: // time-out expired 505b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald break; 506b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald default: 507ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes status = errno; 508ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes ALOGE("%s unexpected error %s", __func__, strerror(status)); 509b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald goto end; 510b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 511b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 512b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 513b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald 514b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgeraldend: 515b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (requested == NULL) { 516b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald requested = &kNonBlocking; 517b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 518b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald return status; 519b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald} 520b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald 5219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 5229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 5239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn KastenStaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, 5249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t frameCount, size_t frameSize) 5259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize), 5264ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mMutator(&cblk->u.mStatic.mSingleStateQueue), 5274ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoopObserver(&cblk->u.mStatic.mPosLoopQueue) 5289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 5299b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung memset(&mState, 0, sizeof(mState)); 5304ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung memset(&mPosLoop, 0, sizeof(mPosLoop)); 5319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 5329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 5339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid StaticAudioTrackClientProxy::flush() 5349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 535adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten LOG_ALWAYS_FATAL("static flush"); 5369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 5379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 5389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount) 5399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 540fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten // This can only happen on a 64-bit client 541fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten if (loopStart > UINT32_MAX || loopEnd > UINT32_MAX) { 542fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten // FIXME Should return an error status 543fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten return; 544fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten } 5459b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mLoopStart = (uint32_t) loopStart; 5469b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mLoopEnd = (uint32_t) loopEnd; 5479b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mLoopCount = loopCount; 5489b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mLoopSequence = incrementSequence(mState.mLoopSequence, mState.mPositionSequence); 5499b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // set patch-up variables until the mState is acknowledged by the ServerProxy. 5509b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // observed buffer position and loop count will freeze until then to give the 5519b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // illusion of a synchronous change. 5524ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung getBufferPositionAndLoopCount(NULL, NULL); 5539b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // preserve behavior to restart at mState.mLoopStart if position exceeds mState.mLoopEnd. 5544ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (mState.mLoopCount != 0 && mPosLoop.mBufferPosition >= mState.mLoopEnd) { 5554ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoop.mBufferPosition = mState.mLoopStart; 5569b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 5574ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoop.mLoopCount = mState.mLoopCount; 5589b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung (void) mMutator.push(mState); 5599b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung} 5609b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 5619b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungvoid StaticAudioTrackClientProxy::setBufferPosition(size_t position) 5629b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung{ 5639b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // This can only happen on a 64-bit client 5649b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (position > UINT32_MAX) { 5659b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // FIXME Should return an error status 5669b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return; 567680b795435a5a501a56610258dce5eda40a1725dAndy Hung } 5689b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mPosition = (uint32_t) position; 5699b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mPositionSequence = incrementSequence(mState.mPositionSequence, mState.mLoopSequence); 5704ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung // set patch-up variables until the mState is acknowledged by the ServerProxy. 5714ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung // observed buffer position and loop count will freeze until then to give the 5724ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung // illusion of a synchronous change. 5734ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (mState.mLoopCount > 0) { // only check if loop count is changing 5744ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung getBufferPositionAndLoopCount(NULL, NULL); // get last position 5754ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung } 5764ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoop.mBufferPosition = position; 5774ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (position >= mState.mLoopEnd) { 5784ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung // no ongoing loop is possible if position is greater than loopEnd. 5794ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoop.mLoopCount = 0; 5804ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung } 5819b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung (void) mMutator.push(mState); 5829b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung} 5839b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 5849b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungvoid StaticAudioTrackClientProxy::setBufferPositionAndLoop(size_t position, size_t loopStart, 5859b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung size_t loopEnd, int loopCount) 5869b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung{ 5879b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung setLoop(loopStart, loopEnd, loopCount); 5889b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung setBufferPosition(position); 5899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 5909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 5919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastensize_t StaticAudioTrackClientProxy::getBufferPosition() 5929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 5934ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung getBufferPositionAndLoopCount(NULL, NULL); 5944ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung return mPosLoop.mBufferPosition; 5954ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung} 5964ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung 5974ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hungvoid StaticAudioTrackClientProxy::getBufferPositionAndLoopCount( 5984ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung size_t *position, int *loopCount) 5994ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung{ 6004ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (mMutator.ack() == StaticAudioTrackSingleStateQueue::SSQ_DONE) { 6014ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (mPosLoopObserver.poll(mPosLoop)) { 6024ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung ; // a valid mPosLoop should be available if ackDone is true. 6034ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung } 6044ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung } 6054ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (position != NULL) { 6064ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung *position = mPosLoop.mBufferPosition; 6074ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung } 6084ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung if (loopCount != NULL) { 6094ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung *loopCount = mPosLoop.mLoopCount; 610a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 6119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 6129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 6139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 614a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 6159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn KastenServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, 6169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t frameSize, bool isOut, bool clientInServer) 6177db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), 618ea2b9c07b34079f0dbd8610a511e006e69a15adcAndy Hung mAvailToClient(0), mFlush(0), mReleased(0), mFlushed(0) 6196ae5843c281301a9ffd1059d185620a9337e15a2Andy Hung , mTimestampMutator(&cblk->mExtendedTimestampQueue) 6209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 621e8972b0a27fac327c997fceb20d9abae1dc1d899Phil Burk cblk->mBufferSizeInFrames = frameCount; 6229f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 6239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 624926b82fa9e22739686602f7ae8cf24105f14d82eilewis__attribute__((no_sanitize("integer"))) 6252e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kastenstatus_t ServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush) 6269f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 6277db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); 6289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsShutdown) { 6297db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten goto no_init; 6309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 6317db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten { 6329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 6339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // compute number of frames available to write (AudioTrack) or read (AudioRecord), 6349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // or use previous cached value from framesReady(), with added barrier if it omits. 6359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t front; 6369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t rear; 6379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // See notes on barriers at ClientProxy::obtainBuffer() 6389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 6399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t flush = cblk->u.mStreaming.mFlush; 6409f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 641b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald front = cblk->u.mStreaming.mFront; 6429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (flush != mFlush) { 643050501d11d944dcb256d37d3b86bd658d94f6a7fGlenn Kasten // effectively obtain then release whatever is in the buffer 644a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung const size_t overflowBit = mFrameCountP2 << 1; 645a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung const size_t mask = overflowBit - 1; 64620f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten int32_t newFront = (front & ~mask) | (flush & mask); 64720f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten ssize_t filled = rear - newFront; 648a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung if (filled >= (ssize_t)overflowBit) { 649a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // front and rear offsets span the overflow bit of the p2 mask 650a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // so rebasing newFront on the front offset is off by the overflow bit. 651a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung // adjust newFront to match rear offset. 652dbd0f3c5e15e56c2d91ae1405fecf2279785ef00Glenn Kasten ALOGV("flush wrap: filled %zx >= overflowBit %zx", filled, overflowBit); 653a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung newFront += overflowBit; 654a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung filled -= overflowBit; 655a2d75cdb1e389f2b4ce5992fb6652399b4d30966Andy Hung } 65620f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten // Rather than shutting down on a corrupt flush, just treat it as a full flush 65720f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 658b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten ALOGE("mFlush %#x -> %#x, front %#x, rear %#x, mask %#x, newFront %#x, " 659f1063e2659cad01ec334a265c31734c63c319cc4Lajos Molnar "filled %zd=%#x", 660f1063e2659cad01ec334a265c31734c63c319cc4Lajos Molnar mFlush, flush, front, rear, 661f1063e2659cad01ec334a265c31734c63c319cc4Lajos Molnar (unsigned)mask, newFront, filled, (unsigned)filled); 66220f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten newFront = rear; 66320f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten } 66420f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten mFlush = flush; 66520f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten android_atomic_release_store(newFront, &cblk->u.mStreaming.mFront); 66620f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten // There is no danger from a false positive, so err on the side of caution 66720f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten if (true /*front != newFront*/) { 668b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); 669b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (!(old & CBLK_FUTEX_WAKE)) { 670ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, 671ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); 672b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 673b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 674ea2b9c07b34079f0dbd8610a511e006e69a15adcAndy Hung mFlushed += (newFront - front) & mask; 67520f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten front = newFront; 676a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 6779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 6789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); 6799f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear = cblk->u.mStreaming.mRear; 6809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 6819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ssize_t filled = rear - front; 6829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // pipe should not already be overfull 6839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 68434fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled); 6859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mIsShutdown = true; 6869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 6879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsShutdown) { 6887db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten goto no_init; 689a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 6909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // don't allow filling pipe beyond the nominal size 6919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t availToServer; 6929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 6939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten availToServer = filled; 6949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mAvailToClient = mFrameCount - filled; 6959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 6969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten availToServer = mFrameCount - filled; 6979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mAvailToClient = filled; 6989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 6999f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // 'availToServer' may be non-contiguous, so return only the first contiguous chunk 7009f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t part1; 7019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 7029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten front &= mFrameCountP2 - 1; 7039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = mFrameCountP2 - front; 7049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 7059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten rear &= mFrameCountP2 - 1; 7069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = mFrameCountP2 - rear; 7079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 7089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (part1 > availToServer) { 7099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = availToServer; 7109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 7119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t ask = buffer->mFrameCount; 7129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (part1 > ask) { 7139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten part1 = ask; 7149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 7159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // is assignment redundant in some cases? 7169f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = part1; 7179f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = part1 > 0 ? 7189f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL; 7199f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = availToServer - part1; 7202e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kasten // After flush(), allow releaseBuffer() on a previously obtained buffer; 7212e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kasten // see "Acknowledge any pending flush()" in audioflinger/Tracks.cpp. 7222e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kasten if (!ackFlush) { 7232e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kasten mUnreleased = part1; 7242e422c472c91aa7912befd0fc038d1e11f354bc1Glenn Kasten } 7259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return part1 > 0 ? NO_ERROR : WOULD_BLOCK; 7267db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten } 7277db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kastenno_init: 7287db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mFrameCount = 0; 7297db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mRaw = NULL; 7307db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mNonContig = 0; 7317db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten mUnreleased = 0; 7327db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten return NO_INIT; 7339f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 734a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 735926b82fa9e22739686602f7ae8cf24105f14d82eilewis__attribute__((no_sanitize("integer"))) 7369f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid ServerProxy::releaseBuffer(Buffer* buffer) 7379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 7387db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(buffer == NULL); 7399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t stepCount = buffer->mFrameCount; 7407db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten if (stepCount == 0 || mIsShutdown) { 7417db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten // prevent accidental re-use of buffer 7427db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten buffer->mFrameCount = 0; 7439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 7449f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 7459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return; 7469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 7477db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); 7489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mUnreleased -= stepCount; 7499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 7509f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsOut) { 7519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t front = cblk->u.mStreaming.mFront; 7529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); 7539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 7549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t rear = cblk->u.mStreaming.mRear; 7559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); 756a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 757a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 758844f88ccfcca95687e774bccb5e9065da61d33f8Glenn Kasten cblk->mServer += stepCount; 7593f0c902beb53a245c9db35e871607dba05b8d391Andy Hung mReleased += stepCount; 760a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 7619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t half = mFrameCount / 2; 7629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (half == 0) { 7639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten half = 1; 7649f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 765fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten size_t minimum = (size_t) cblk->mMinimum; 7669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (minimum == 0) { 7679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minimum = mIsOut ? half : 1; 7689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else if (minimum > half) { 7699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten minimum = half; 7709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 77193bb77da5481ab75c2cd6e3aa681839273c6e43dGlenn Kasten // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time 772ce8828a016b082f730152af2204b8ea3610dc1ecGlenn Kasten if (!mIsOut || (mAvailToClient + stepCount >= minimum)) { 77334fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn ALOGV("mAvailToClient=%zu stepCount=%zu minimum=%zu", mAvailToClient, stepCount, minimum); 7740d09a9bec07b3bec78bd473ff0bfcf0a261f3f25Glenn Kasten int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); 7750d09a9bec07b3bec78bd473ff0bfcf0a261f3f25Glenn Kasten if (!(old & CBLK_FUTEX_WAKE)) { 776ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, 777ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); 7789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 779a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 7809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 7819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 7829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 7839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 784a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 785a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 7869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 7879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 788926b82fa9e22739686602f7ae8cf24105f14d82eilewis__attribute__((no_sanitize("integer"))) 7899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastensize_t AudioTrackServerProxy::framesReady() 790a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 7919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten LOG_ALWAYS_FATAL_IF(!mIsOut); 7929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 7939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsShutdown) { 7949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return 0; 7959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 7969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 797b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald 798b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald int32_t flush = cblk->u.mStreaming.mFlush; 799b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald if (flush != mFlush) { 80020f51b1ea04c410a25f214e37bcdb586e2a028ccGlenn Kasten // FIXME should return an accurate value, but over-estimate is better than under-estimate 801b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald return mFrameCount; 802b1a270d1e926fb9a01b4265a7675ed0c2c8f4868Richard Fitzgerald } 8039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // the acquire might not be necessary since not doing a subsequent read 8049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 8059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ssize_t filled = rear - cblk->u.mStreaming.mFront; 8069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // pipe should not already be overfull 8079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 80834fb29696b0f3abf61b10f8d053b1f33d501de0aMark Salyzyn ALOGE("Shared memory control block is corrupt (filled=%zd); shutting down", filled); 8099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mIsShutdown = true; 8109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return 0; 8119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 8129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // cache this value for later use by obtainBuffer(), with added barrier 8139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // and racy if called by normal mixer thread 8149f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer() 8159f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return filled; 816a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 817a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 818bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioTrackServerProxy::setStreamEndDone() { 819844f88ccfcca95687e774bccb5e9065da61d33f8Glenn Kasten audio_track_cblk_t* cblk = mCblk; 820bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool old = 821844f88ccfcca95687e774bccb5e9065da61d33f8Glenn Kasten (android_atomic_or(CBLK_STREAM_END_DONE, &cblk->mFlags) & CBLK_STREAM_END_DONE) != 0; 822bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (!old) { 823e348c5b72ad889389c7c1c900c121f0fbee221b5Elliott Hughes (void) syscall(__NR_futex, &cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 824ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes 1); 825bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 826bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent return old; 827bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent} 828bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 82982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kastenvoid AudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) 83082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten{ 831844f88ccfcca95687e774bccb5e9065da61d33f8Glenn Kasten audio_track_cblk_t* cblk = mCblk; 8322812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk if (frameCount > 0) { 8332812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk cblk->u.mStreaming.mUnderrunFrames += frameCount; 8342812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk 8352812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk if (!mUnderrunning) { // start of underrun? 8362812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk mUnderrunCount++; 8372812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk cblk->u.mStreaming.mUnderrunCount = mUnderrunCount; 8382812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk mUnderrunning = true; 8392812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk ALOGV("tallyUnderrunFrames(%3u) at uf = %u, bump mUnderrunCount = %u", 8402812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk frameCount, cblk->u.mStreaming.mUnderrunFrames, mUnderrunCount); 8412812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk } 84282aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten 8432812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk // FIXME also wake futex so that underrun is noticed more quickly 8442812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk (void) android_atomic_or(CBLK_UNDERRUN, &cblk->mFlags); 8452812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk } else { 8462812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk ALOGV_IF(mUnderrunning, 8472812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk "tallyUnderrunFrames(%3u) at uf = %u, underrun finished", 8482812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk frameCount, cblk->u.mStreaming.mUnderrunFrames); 8492812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk mUnderrunning = false; // so we can detect the next edge 8502812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk } 85182aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten} 85282aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten 8535a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo GarciaAudioPlaybackRate AudioTrackServerProxy::getPlaybackRate() 8548edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung{ // do not call from multiple threads without holding lock 8555a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia mPlaybackRateObserver.poll(mPlaybackRate); 8565a8a95de6dad1a3bcf3da5a37b35766e89086e13Ricardo Garcia return mPlaybackRate; 8578edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung} 8588edb8dc44b8a2f81bdb5db645b6b708548771a31Andy Hung 8599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 8609f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 8619f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn KastenStaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, 8629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t frameCount, size_t frameSize) 8639f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize), 8644ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mObserver(&cblk->u.mStatic.mSingleStateQueue), 8654ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoopMutator(&cblk->u.mStatic.mPosLoopQueue), 866cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung mFramesReadySafe(frameCount), mFramesReady(frameCount), 867cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung mFramesReadyIsCalledByMultipleThreads(false) 868a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 8699b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung memset(&mState, 0, sizeof(mState)); 870a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 871a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 8729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads() 873a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 8749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mFramesReadyIsCalledByMultipleThreads = true; 8759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 876a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 8779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastensize_t StaticAudioTrackServerProxy::framesReady() 8789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 879cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung // Can't call pollPosition() from multiple threads. 8809f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!mFramesReadyIsCalledByMultipleThreads) { 881cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung (void) pollPosition(); 882a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 883cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung return mFramesReadySafe; 884a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 885a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 8869b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungstatus_t StaticAudioTrackServerProxy::updateStateWithLoop( 8879b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung StaticAudioTrackState *localState, const StaticAudioTrackState &update) const 888a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 8899b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (localState->mLoopSequence != update.mLoopSequence) { 8909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten bool valid = false; 8919b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung const size_t loopStart = update.mLoopStart; 8929b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung const size_t loopEnd = update.mLoopEnd; 8939b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung size_t position = localState->mPosition; 8949b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (update.mLoopCount == 0) { 8959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten valid = true; 8969b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } else if (update.mLoopCount >= -1) { 8979f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (loopStart < loopEnd && loopEnd <= mFrameCount && 8989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten loopEnd - loopStart >= MIN_LOOP) { 899680b795435a5a501a56610258dce5eda40a1725dAndy Hung // If the current position is greater than the end of the loop 900680b795435a5a501a56610258dce5eda40a1725dAndy Hung // we "wrap" to the loop start. This might cause an audible pop. 901680b795435a5a501a56610258dce5eda40a1725dAndy Hung if (position >= loopEnd) { 9029b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung position = loopStart; 903cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung } 9049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten valid = true; 905a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 906a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 9079b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (!valid || position > mFrameCount) { 9089b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return NO_INIT; 9099b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 9109b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mPosition = position; 9119b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mLoopCount = update.mLoopCount; 9129b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mLoopEnd = loopEnd; 9139b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mLoopStart = loopStart; 9149b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mLoopSequence = update.mLoopSequence; 9159b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 9169b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return OK; 9179b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung} 9189b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 9199b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungstatus_t StaticAudioTrackServerProxy::updateStateWithPosition( 9209b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung StaticAudioTrackState *localState, const StaticAudioTrackState &update) const 9219b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung{ 9229b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (localState->mPositionSequence != update.mPositionSequence) { 9239b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (update.mPosition > mFrameCount) { 9249b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return NO_INIT; 9259b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } else if (localState->mLoopCount != 0 && update.mPosition >= localState->mLoopEnd) { 9269b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mLoopCount = 0; // disable loop count if position is beyond loop end. 9279b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 9289b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mPosition = update.mPosition; 9299b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung localState->mPositionSequence = update.mPositionSequence; 9309b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 9319b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return OK; 9329b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung} 9339b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 9349b4615887c23548438fd0d8e3d8f04ac21912850Andy Hungssize_t StaticAudioTrackServerProxy::pollPosition() 9359b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung{ 9369b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung StaticAudioTrackState state; 9379b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (mObserver.poll(state)) { 9389b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung StaticAudioTrackState trystate = mState; 9399b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung bool result; 940cb50c54b31a83ac598530780fa8c145a3cb4a2deChad Brubaker const int32_t diffSeq = (int32_t) state.mLoopSequence - (int32_t) state.mPositionSequence; 9419b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung 9429b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (diffSeq < 0) { 9439b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung result = updateStateWithLoop(&trystate, state) == OK && 9449b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung updateStateWithPosition(&trystate, state) == OK; 9459b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } else { 9469b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung result = updateStateWithPosition(&trystate, state) == OK && 9479b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung updateStateWithLoop(&trystate, state) == OK; 9489b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 9499b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (!result) { 9504ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mObserver.done(); 9519b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // caution: no update occurs so server state will be inconsistent with client state. 9529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ALOGE("%s client pushed an invalid state, shutting down", __func__); 9539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mIsShutdown = true; 9549f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return (ssize_t) NO_INIT; 9559f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 9569b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState = trystate; 9579b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung if (mState.mLoopCount == -1) { 9589b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mFramesReady = INT64_MAX; 9599b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } else if (mState.mLoopCount == 0) { 9609b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mFramesReady = mFrameCount - mState.mPosition; 9619b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } else if (mState.mLoopCount > 0) { 9629b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // TODO: Later consider fixing overflow, but does not seem needed now 9639b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung // as will not overflow if loopStart and loopEnd are Java "ints". 9649b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mFramesReady = int64_t(mState.mLoopCount) * (mState.mLoopEnd - mState.mLoopStart) 9659b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung + mFrameCount - mState.mPosition; 9669b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung } 967cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung mFramesReadySafe = clampToSize(mFramesReady); 968fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten // This may overflow, but client is not supposed to rely on it 9694ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung StaticAudioTrackPosLoop posLoop; 9704ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung 9714ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung posLoop.mLoopCount = (int32_t) mState.mLoopCount; 9724ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung posLoop.mBufferPosition = (uint32_t) mState.mPosition; 9734ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoopMutator.push(posLoop); 9744ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mObserver.done(); // safe to read mStatic variables. 975a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 9769b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung return (ssize_t) mState.mPosition; 977a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 978a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 979954ca45ac30539a49f179580b667b0ab1056d113Andy Hungstatus_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer, bool ackFlush) 980a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten{ 9819f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mIsShutdown) { 9829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 9839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 9849f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 9859f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mUnreleased = 0; 9869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return NO_INIT; 9879f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 9889f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten ssize_t positionOrStatus = pollPosition(); 9899f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (positionOrStatus < 0) { 9909f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 9919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 9929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 9939f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mUnreleased = 0; 9949f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return (status_t) positionOrStatus; 9959f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 9969f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t position = (size_t) positionOrStatus; 997cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung size_t end = mState.mLoopCount != 0 ? mState.mLoopEnd : mFrameCount; 9989f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t avail; 999cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung if (position < end) { 1000cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung avail = end - position; 10019f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t wanted = buffer->mFrameCount; 10029f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (avail < wanted) { 10039f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = avail; 10049f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 10059f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten avail = wanted; 10069f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 10079f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = &((char *) mBuffers)[position * mFrameSize]; 10089f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 10099f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten avail = 0; 10109f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 10119f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 10129f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1013cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung // As mFramesReady is the total remaining frames in the static audio track, 1014cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung // it is always larger or equal to avail. 1015486a7135593b4b40c067e9b06f393577abfef482Andy Hung LOG_ALWAYS_FATAL_IF(mFramesReady < (int64_t) avail); 1016cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung buffer->mNonContig = mFramesReady == INT64_MAX ? SIZE_MAX : clampToSize(mFramesReady - avail); 1017954ca45ac30539a49f179580b667b0ab1056d113Andy Hung if (!ackFlush) { 1018954ca45ac30539a49f179580b667b0ab1056d113Andy Hung mUnreleased = avail; 1019954ca45ac30539a49f179580b667b0ab1056d113Andy Hung } 10209f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return NO_ERROR; 10219f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten} 1022a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 10239f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kastenvoid StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer) 10249f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten{ 10259f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t stepCount = buffer->mFrameCount; 1026486a7135593b4b40c067e9b06f393577abfef482Andy Hung LOG_ALWAYS_FATAL_IF(!((int64_t) stepCount <= mFramesReady)); 10277db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased)); 10289f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (stepCount == 0) { 10297db7df0e8d9d7cee8ba374468cdbfa0108e3337cGlenn Kasten // prevent accidental re-use of buffer 10309f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 10319f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 10329f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten return; 1033a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 10349f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten mUnreleased -= stepCount; 10359f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten audio_track_cblk_t* cblk = mCblk; 10369b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung size_t position = mState.mPosition; 10379f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten size_t newPosition = position + stepCount; 10389f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten int32_t setFlags = 0; 10399f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (!(position <= newPosition && newPosition <= mFrameCount)) { 1040b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten ALOGW("%s newPosition %zu outside [%zu, %zu]", __func__, newPosition, position, 1041b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten mFrameCount); 10429f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten newPosition = mFrameCount; 10439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) { 1044cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung newPosition = mState.mLoopStart; 10459f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) { 10469f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten setFlags = CBLK_LOOP_CYCLE; 10479f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } else { 10489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten setFlags = CBLK_LOOP_FINAL; 10499f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 1050a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten } 10519f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (newPosition == mFrameCount) { 10529f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten setFlags |= CBLK_BUFFER_END; 10539f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 10549b4615887c23548438fd0d8e3d8f04ac21912850Andy Hung mState.mPosition = newPosition; 1055cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung if (mFramesReady != INT64_MAX) { 1056cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung mFramesReady -= stepCount; 1057cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung } 1058cb2129b3b568a4e31bcbda3545a468024bc972feAndy Hung mFramesReadySafe = clampToSize(mFramesReady); 10599f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 1060f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten cblk->mServer += stepCount; 10613f0c902beb53a245c9db35e871607dba05b8d391Andy Hung mReleased += stepCount; 10623f0c902beb53a245c9db35e871607dba05b8d391Andy Hung 1063fdac7c00f9201bb3a9862069145f01d37e39755bGlenn Kasten // This may overflow, but client is not supposed to rely on it 10644ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung StaticAudioTrackPosLoop posLoop; 10654ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung posLoop.mBufferPosition = mState.mPosition; 10664ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung posLoop.mLoopCount = mState.mLoopCount; 10674ede21d9c1f957baf5e561849ff9bbe4bcbefc20Andy Hung mPosLoopMutator.push(posLoop); 10689f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten if (setFlags != 0) { 106996f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten (void) android_atomic_or(setFlags, &cblk->mFlags); 10709f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten // this would be a good place to wake a futex 10719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten } 10729f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 10739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mFrameCount = 0; 10749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mRaw = NULL; 10759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten buffer->mNonContig = 0; 1076a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} 1077a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten 10782812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burkvoid StaticAudioTrackServerProxy::tallyUnderrunFrames(uint32_t frameCount) 107982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten{ 108082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // Unlike AudioTrackServerProxy::tallyUnderrunFrames() used for streaming tracks, 108182aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // we don't have a location to count underrun frames. The underrun frame counter 108282aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // only exists in AudioTrackSharedStreaming. Fortunately, underruns are not 108382aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // possible for static buffer tracks other than at end of buffer, so this is not a loss. 108482aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten 108582aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten // FIXME also wake futex so that underrun is noticed more quickly 10862812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk if (frameCount > 0) { 10872812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk (void) android_atomic_or(CBLK_UNDERRUN, &mCblk->mFlags); 10882812d9ea3a3a33142dd8e23c9d949c498d6f7a12Phil Burk } 108982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten} 109082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten 10919f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten// --------------------------------------------------------------------------- 10929f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten 1093a8190fc518b6769257896605f3aee091aeb60b50Glenn Kasten} // namespace android 1094