1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "oboe/LatencyTuner.h" 18 19 using namespace oboe; 20 LatencyTuner(AudioStream & stream)21LatencyTuner::LatencyTuner(AudioStream &stream) 22 : LatencyTuner(stream, stream.getBufferCapacityInFrames()) { 23 } 24 LatencyTuner(oboe::AudioStream & stream,int32_t maximumBufferSize)25LatencyTuner::LatencyTuner(oboe::AudioStream &stream, int32_t maximumBufferSize) 26 : mStream(stream) 27 , mMaxBufferSize(maximumBufferSize) { 28 int32_t burstSize = stream.getFramesPerBurst(); 29 setMinimumBufferSize(kDefaultNumBursts * burstSize); 30 setBufferSizeIncrement(burstSize); 31 reset(); 32 } 33 tune()34Result LatencyTuner::tune() { 35 if (mState == State::Unsupported) { 36 return Result::ErrorUnimplemented; 37 } 38 39 Result result = Result::OK; 40 41 // Process reset requests. 42 int32_t numRequests = mLatencyTriggerRequests.load(); 43 if (numRequests != mLatencyTriggerResponses.load()) { 44 mLatencyTriggerResponses.store(numRequests); 45 reset(); 46 } 47 48 // Set state to Active if the idle countdown has reached zero. 49 if (mState == State::Idle && --mIdleCountDown <= 0) { 50 mState = State::Active; 51 } 52 53 // When state is Active attempt to change the buffer size if the number of xRuns has increased. 54 if (mState == State::Active) { 55 56 auto xRunCountResult = mStream.getXRunCount(); 57 if (xRunCountResult == Result::OK) { 58 if ((xRunCountResult.value() - mPreviousXRuns) > 0) { 59 mPreviousXRuns = xRunCountResult.value(); 60 int32_t oldBufferSize = mStream.getBufferSizeInFrames(); 61 int32_t requestedBufferSize = oldBufferSize + getBufferSizeIncrement(); 62 63 // Do not request more than the maximum buffer size (which was either user-specified 64 // or was from stream->getBufferCapacityInFrames()) 65 if (requestedBufferSize > mMaxBufferSize) requestedBufferSize = mMaxBufferSize; 66 67 auto setBufferResult = mStream.setBufferSizeInFrames(requestedBufferSize); 68 if (setBufferResult != Result::OK) { 69 result = setBufferResult; 70 mState = State::Unsupported; 71 } else if (setBufferResult.value() == oldBufferSize) { 72 mState = State::AtMax; 73 } 74 } 75 } else { 76 mState = State::Unsupported; 77 } 78 } 79 80 if (mState == State::Unsupported) { 81 result = Result::ErrorUnimplemented; 82 } 83 84 if (mState == State::AtMax) { 85 result = Result::OK; 86 } 87 return result; 88 } 89 requestReset()90void LatencyTuner::requestReset() { 91 if (mState != State::Unsupported) { 92 mLatencyTriggerRequests++; 93 } 94 } 95 reset()96void LatencyTuner::reset() { 97 mState = State::Idle; 98 mIdleCountDown = kIdleCount; 99 // Set to minimal latency 100 mStream.setBufferSizeInFrames(getMinimumBufferSize()); 101 } 102 isAtMaximumBufferSize()103bool LatencyTuner::isAtMaximumBufferSize() { 104 return mState == State::AtMax; 105 } 106