Oboe  1.2
A library for creating real-time audio apps on Android
LatencyTuner.h
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 #ifndef OBOE_LATENCY_TUNER_
18 #define OBOE_LATENCY_TUNER_
19 
20 #include <atomic>
21 #include <cstdint>
22 #include "oboe/Definitions.h"
23 #include "oboe/AudioStream.h"
24 
25 namespace oboe {
26 
41 class LatencyTuner {
42 public:
43 
49  explicit LatencyTuner(AudioStream &stream);
50 
57  explicit LatencyTuner(AudioStream &stream, int32_t maximumBufferSize);
58 
67  Result tune();
68 
77  void requestReset();
78 
84  bool isAtMaximumBufferSize();
85 
86 
87 private:
88 
95  void reset();
96 
97  enum class State {
98  Idle,
99  Active,
100  AtMax,
101  Unsupported
102  } ;
103 
104  // arbitrary number of calls to wait before bumping up the latency
105  static constexpr int32_t kIdleCount = 8;
106 
107  AudioStream &mStream;
108  State mState = State::Idle;
109  int32_t mMaxBufferSize = 0;
110  int32_t mPreviousXRuns = 0;
111  int32_t mIdleCountDown = 0;
112  std::atomic<int32_t> mLatencyTriggerRequests{0}; // TODO user atomic requester from AAudio
113  std::atomic<int32_t> mLatencyTriggerResponses{0};
114 };
115 
116 } // namespace oboe
117 
118 #endif // OBOE_LATENCY_TUNER_
Definition: AudioStream.h:44
bool isAtMaximumBufferSize()
LatencyTuner(AudioStream &stream)
Result
Definition: Definitions.h:131
Definition: LatencyTuner.h:41
Definition: AudioStream.h:31