1010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten/* 2010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * Copyright (C) 2012 The Android Open Source Project 3010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * 4010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * Licensed under the Apache License, Version 2.0 (the "License"); 5010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * you may not use this file except in compliance with the License. 6010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * You may obtain a copy of the License at 7010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * 8010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * http://www.apache.org/licenses/LICENSE-2.0 9010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * 10010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * Unless required by applicable law or agreed to in writing, software 11010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * distributed under the License is distributed on an "AS IS" BASIS, 12010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * See the License for the specific language governing permissions and 14010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten * limitations under the License. 15010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten */ 16010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 170f6a0435713c435e1aaeacbfd9ce7abb6a5b19a9Mark Salyzyn#include <inttypes.h> 180f6a0435713c435e1aaeacbfd9ce7abb6a5b19a9Mark Salyzyn 19010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten#define LOG_TAG "MonoPipe" 20010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten//#define LOG_NDEBUG 0 21010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 22010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten#include <cutils/atomic.h> 23010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten#include <cutils/compiler.h> 24010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten#include <utils/Log.h> 2528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten#include <utils/Trace.h> 262dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/AudioBufferProvider.h> 272dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/nbaio/MonoPipe.h> 2853dbe77b9f50cdd0de88b9d25dd643e90c8ba68bGlenn Kasten#include <audio_utils/roundup.h> 29010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 302c3b2da3049627264b7c6b449a1622f002210f03John Grossman 31010662326b9c43c703725f933e95e0897f8a6bddGlenn Kastennamespace android { 32010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 3372e54af9fcdc4754914fe2bf8de699523538b315Glenn KastenMonoPipe::MonoPipe(size_t reqFrames, const NBAIO_Format& format, bool writeCanBlock) : 34010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten NBAIO_Sink(format), 35820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten mReqFrames(reqFrames), 36820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten mMaxFrames(roundup(reqFrames)), 37010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten mBuffer(malloc(mMaxFrames * Format_frameSize(format))), 38010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten mFront(0), 39010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten mRear(0), 4028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mWriteTsValid(false), 4128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // mWriteTs 4228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mSetpoint((reqFrames * 11) / 16), 43003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten mWriteCanBlock(writeCanBlock), 44a07a1c2c91dc7ee6ded319262499f20cd01edcf7Glenn Kasten mIsShutdown(false), 45a07a1c2c91dc7ee6ded319262499f20cd01edcf7Glenn Kasten // mTimestampShared 46a07a1c2c91dc7ee6ded319262499f20cd01edcf7Glenn Kasten mTimestampMutator(&mTimestampShared), 47a07a1c2c91dc7ee6ded319262499f20cd01edcf7Glenn Kasten mTimestampObserver(&mTimestampShared) 48010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten{ 49010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten} 50010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 51010662326b9c43c703725f933e95e0897f8a6bddGlenn KastenMonoPipe::~MonoPipe() 52010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten{ 53010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten free(mBuffer); 54010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten} 55010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 56010662326b9c43c703725f933e95e0897f8a6bddGlenn Kastenssize_t MonoPipe::availableToWrite() const 57010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten{ 58010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_UNLIKELY(!mNegotiated)) { 59010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten return NEGOTIATE; 60010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 61820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten // uses mMaxFrames not mReqFrames, so allows "over-filling" the pipe beyond requested limit 62010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten ssize_t ret = mMaxFrames - (mRear - android_atomic_acquire_load(&mFront)); 63010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames)); 64010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten return ret; 65010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten} 66010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 67010662326b9c43c703725f933e95e0897f8a6bddGlenn Kastenssize_t MonoPipe::write(const void *buffer, size_t count) 68010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten{ 69010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_UNLIKELY(!mNegotiated)) { 70010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten return NEGOTIATE; 71010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 72010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten size_t totalFramesWritten = 0; 736d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten while (count > 0) { 74820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten // can't return a negative value, as we already checked for !mNegotiated 756d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten size_t avail = availableToWrite(); 766d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten size_t written = avail; 77010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_LIKELY(written > count)) { 78010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten written = count; 79010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 80010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten size_t rear = mRear & (mMaxFrames - 1); 81010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten size_t part1 = mMaxFrames - rear; 82010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (part1 > written) { 83010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten part1 = written; 84010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 85010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_LIKELY(part1 > 0)) { 864d693d6b8cc1283f92f5301daf19a07abc772a2bGlenn Kasten memcpy((char *) mBuffer + (rear * mFrameSize), buffer, part1 * mFrameSize); 87010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_UNLIKELY(rear + part1 == mMaxFrames)) { 88010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten size_t part2 = written - part1; 89010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten if (CC_LIKELY(part2 > 0)) { 904d693d6b8cc1283f92f5301daf19a07abc772a2bGlenn Kasten memcpy(mBuffer, (char *) buffer + (part1 * mFrameSize), part2 * mFrameSize); 91010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 92010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 93010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten android_atomic_release_store(written + mRear, &mRear); 94010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten totalFramesWritten += written; 95010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 96003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten if (!mWriteCanBlock || mIsShutdown) { 97010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten break; 98010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 996d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten count -= written; 1004d693d6b8cc1283f92f5301daf19a07abc772a2bGlenn Kasten buffer = (char *) buffer + (written * mFrameSize); 1016d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten // Simulate blocking I/O by sleeping at different rates, depending on a throttle. 10228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // The throttle tries to keep the mean pipe depth near the setpoint, with a slight jitter. 103820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten uint32_t ns; 1046d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten if (written > 0) { 105820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten size_t filled = (mMaxFrames - avail) + written; 106820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten // FIXME cache these values to avoid re-computation 10728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (filled <= mSetpoint / 2) { 108820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten // pipe is (nearly) empty, fill quickly 1096d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten ns = written * ( 500000000 / Format_sampleRate(mFormat)); 11028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } else if (filled <= (mSetpoint * 3) / 4) { 11128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // pipe is below setpoint, fill at slightly faster rate 1126d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten ns = written * ( 750000000 / Format_sampleRate(mFormat)); 11328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } else if (filled <= (mSetpoint * 5) / 4) { 11428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // pipe is at setpoint, fill at nominal rate 1156d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten ns = written * (1000000000 / Format_sampleRate(mFormat)); 11628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } else if (filled <= (mSetpoint * 3) / 2) { 11728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // pipe is above setpoint, fill at slightly slower rate 11828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns = written * (1150000000 / Format_sampleRate(mFormat)); 11928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } else if (filled <= (mSetpoint * 7) / 4) { 12028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // pipe is overflowing, fill slowly 12128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns = written * (1350000000 / Format_sampleRate(mFormat)); 122820ba70df8ba595ae9055dfd34fdbfa32f70f14dGlenn Kasten } else { 12328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // pipe is severely overflowing 12428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns = written * (1750000000 / Format_sampleRate(mFormat)); 1256d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten } 1266d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten } else { 12728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns = count * (1350000000 / Format_sampleRate(mFormat)); 1286d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten } 1296d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten if (ns > 999999999) { 1306d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten ns = 999999999; 1316d8aabe8a3be1ac0789d00b82c3ca8b81381f5abGlenn Kasten } 13228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten struct timespec nowTs; 13328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten bool nowTsValid = !clock_gettime(CLOCK_MONOTONIC, &nowTs); 13428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // deduct the elapsed time since previous write() completed 13528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (nowTsValid && mWriteTsValid) { 13628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten time_t sec = nowTs.tv_sec - mWriteTs.tv_sec; 13728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten long nsec = nowTs.tv_nsec - mWriteTs.tv_nsec; 13880b3273cea8660fe8a5868d024d2788a1e083ffcGlenn Kasten ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0), 13980b3273cea8660fe8a5868d024d2788a1e083ffcGlenn Kasten "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld", 14080b3273cea8660fe8a5868d024d2788a1e083ffcGlenn Kasten mWriteTs.tv_sec, mWriteTs.tv_nsec, nowTs.tv_sec, nowTs.tv_nsec); 14128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (nsec < 0) { 14228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten --sec; 14328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten nsec += 1000000000; 14428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 14528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (sec == 0) { 14628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if ((long) ns > nsec) { 14728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns -= nsec; 14828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } else { 14928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ns = 0; 15028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 15128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 15228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 15328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (ns > 0) { 1543b546caeb0a576c4d7d7f1e8d2fdfed2bb8bdeedsynergydev const struct timespec req = {0, static_cast<long>(ns)}; 15528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten nanosleep(&req, NULL); 15628ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 15728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten // record the time that this write() completed 15828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (nowTsValid) { 15928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mWriteTs = nowTs; 16028ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if ((mWriteTs.tv_nsec += ns) >= 1000000000) { 16128ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mWriteTs.tv_nsec -= 1000000000; 16228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten ++mWriteTs.tv_sec; 16328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 16428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 16528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mWriteTsValid = nowTsValid; 166010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten } 167010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten mFramesWritten += totalFramesWritten; 168010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten return totalFramesWritten; 169010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten} 170010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten 17128ed2f93324988767b5658eba7c1fa781a275183Glenn Kastenvoid MonoPipe::setAvgFrames(size_t setpoint) 17228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten{ 17328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten mSetpoint = setpoint; 17428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten} 17528ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten 176003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kastenvoid MonoPipe::shutdown(bool newState) 177003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten{ 178003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten mIsShutdown = newState; 179003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten} 180003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten 181003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kastenbool MonoPipe::isShutdown() 182003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten{ 183003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten return mIsShutdown; 184003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten} 185003d9f71937070791418bf7efc1f7fe1e4c6b821Glenn Kasten 186818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hungstatus_t MonoPipe::getTimestamp(ExtendedTimestamp ×tamp) 187767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten{ 188818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung ExtendedTimestamp ets; 189818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung if (mTimestampObserver.poll(ets)) { 190818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = 191818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung ets.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; 192818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = 193818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung ets.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; 1944d0815d694e5a2edb3ce48427de50f55d0f84c0bGlenn Kasten return OK; 1954d0815d694e5a2edb3ce48427de50f55d0f84c0bGlenn Kasten } 196767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten return INVALID_OPERATION; 197767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten} 198767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten 199010662326b9c43c703725f933e95e0897f8a6bddGlenn Kasten} // namespace android 200