Oboe  1.5
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 
91  void setMinimumBufferSize(int32_t bufferSize) {
92  mMinimumBufferSize = bufferSize;
93  }
94 
95  int32_t getMinimumBufferSize() const {
96  return mMinimumBufferSize;
97  }
98 
108  void setBufferSizeIncrement(int32_t sizeIncrement) {
109  mBufferSizeIncrement = sizeIncrement;
110  }
111 
112  int32_t getBufferSizeIncrement() const {
113  return mBufferSizeIncrement;
114  }
115 
116 private:
117 
124  void reset();
125 
126  enum class State {
127  Idle,
128  Active,
129  AtMax,
130  Unsupported
131  } ;
132 
133  // arbitrary number of calls to wait before bumping up the latency
134  static constexpr int32_t kIdleCount = 8;
135  static constexpr int32_t kDefaultNumBursts = 2;
136 
137  AudioStream &mStream;
138  State mState = State::Idle;
139  int32_t mMaxBufferSize = 0;
140  int32_t mPreviousXRuns = 0;
141  int32_t mIdleCountDown = 0;
142  int32_t mMinimumBufferSize;
143  int32_t mBufferSizeIncrement;
144  std::atomic<int32_t> mLatencyTriggerRequests{0}; // TODO user atomic requester from AAudio
145  std::atomic<int32_t> mLatencyTriggerResponses{0};
146 };
147 
148 } // namespace oboe
149 
150 #endif // OBOE_LATENCY_TUNER_
void setBufferSizeIncrement(int32_t sizeIncrement)
Definition: LatencyTuner.h:108
Definition: AudioStream.h:44
bool isAtMaximumBufferSize()
LatencyTuner(AudioStream &stream)
Result
Definition: Definitions.h:131
Definition: LatencyTuner.h:41
void setMinimumBufferSize(int32_t bufferSize)
Definition: LatencyTuner.h:91
Definition: AudioStream.h:31