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 // Note that this will not allocate more memory. It simply determines 68 // how much of the existing buffer capacity will be used. The size will be 69 // clipped to the bufferCapacity by AAudio. 70 auto setBufferResult = mStream.setBufferSizeInFrames(requestedBufferSize); 71 if (setBufferResult != Result::OK) { 72 result = setBufferResult; 73 mState = State::Unsupported; 74 } else if (setBufferResult.value() == oldBufferSize) { 75 mState = State::AtMax; 76 } 77 } 78 } else { 79 mState = State::Unsupported; 80 } 81 } 82 83 if (mState == State::Unsupported) { 84 result = Result::ErrorUnimplemented; 85 } 86 87 if (mState == State::AtMax) { 88 result = Result::OK; 89 } 90 return result; 91 } 92 requestReset()93void LatencyTuner::requestReset() { 94 if (mState != State::Unsupported) { 95 mLatencyTriggerRequests++; 96 } 97 } 98 reset()99void LatencyTuner::reset() { 100 mState = State::Idle; 101 mIdleCountDown = kIdleCount; 102 // Set to minimal latency 103 mStream.setBufferSizeInFrames(getMinimumBufferSize()); 104 } 105 isAtMaximumBufferSize()106bool LatencyTuner::isAtMaximumBufferSize() { 107 return mState == State::AtMax; 108 } 109