aboutsummaryrefslogtreecommitdiff
path: root/src/common/LatencyTuner.cpp
blob: d30fc43f3fe37a0941e3c484250111bd7306cd26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "oboe/LatencyTuner.h"

using namespace oboe;

LatencyTuner::LatencyTuner(AudioStream &stream)
    : mStream(stream) {
    reset();
}

Result LatencyTuner::tune() {
    if (mState == State::Unsupported) {
        return Result::ErrorUnimplemented;
    }

    Result result = Result::OK;

    // Process reset requests.
    int32_t numRequests = mLatencyTriggerRequests.load();
    if (numRequests != mLatencyTriggerResponses.load()) {
        mLatencyTriggerResponses.store(numRequests);
        reset();
    }

    // Set state to Active if the idle countdown has reached zero.
    if (mState == State::Idle && --mIdleCountDown <= 0) {
        mState = State::Active;
    }

    // When state is Active attempt to change the buffer size if the number of xRuns has increased.
    if (mState == State::Active) {

        auto xRunCountResult = mStream.getXRunCount();
        if (xRunCountResult == Result::OK) {
            if ((xRunCountResult.value() - mPreviousXRuns) > 0) {
                mPreviousXRuns = xRunCountResult.value();
                int32_t oldBufferSize = mStream.getBufferSizeInFrames();
                int32_t requestedBufferSize = oldBufferSize + mStream.getFramesPerBurst();
                auto setBufferResult = mStream.setBufferSizeInFrames(requestedBufferSize);
                if (setBufferResult != Result::OK) {
                    result = setBufferResult;
                    mState = State::Unsupported;
                } else if (setBufferResult.value() == oldBufferSize) {
                    mState = State::AtMax;
                }
            }
        } else {
            mState = State::Unsupported;
        }
    }

    if (mState == State::Unsupported) {
        result = Result::ErrorUnimplemented;
    }

    if (mState == State::AtMax) {
        result = Result::OK;
    }
    return result;
}

void LatencyTuner::requestReset() {
    if (mState != State::Unsupported) {
        mLatencyTriggerRequests++;
    }
}

void LatencyTuner::reset() {
    mState = State::Idle;
    mIdleCountDown = kIdleCount;
    // Set to minimal latency
    mStream.setBufferSizeInFrames(mStream.getFramesPerBurst());
}