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 18 #ifndef SAMPLES_LATENCY_TUNING_CALLBACK_H 19 #define SAMPLES_LATENCY_TUNING_CALLBACK_H 20 21 #include <oboe/Oboe.h> 22 #include <oboe/LatencyTuner.h> 23 24 #include <TappableAudioSource.h> 25 #include <DefaultDataCallback.h> 26 #include <trace.h> 27 28 /** 29 * This callback object extends the functionality of `DefaultDataCallback` by automatically 30 * tuning the latency of the audio stream. @see onAudioReady for more details on this. 31 * 32 * It also demonstrates how to use tracing functions for logging inside the audio callback without 33 * blocking. 34 */ 35 class LatencyTuningCallback: public DefaultDataCallback { 36 public: LatencyTuningCallback()37 LatencyTuningCallback() : DefaultDataCallback() { 38 39 // Initialize the trace functions, this enables you to output trace statements without 40 // blocking. See https://developer.android.com/studio/profile/systrace-commandline.html 41 Trace::initialize(); 42 } 43 44 /** 45 * Every time the playback stream requires data this method will be called. 46 * 47 * @param audioStream the audio stream which is requesting data, this is the mPlayStream object 48 * @param audioData an empty buffer into which we can write our audio data 49 * @param numFrames the number of audio frames which are required 50 * @return Either oboe::DataCallbackResult::Continue if the stream should continue requesting data 51 * or oboe::DataCallbackResult::Stop if the stream should stop. 52 */ 53 oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override; 54 setBufferTuneEnabled(bool enabled)55 void setBufferTuneEnabled(bool enabled) {mBufferTuneEnabled = enabled;} 56 57 void useStream(std::shared_ptr<oboe::AudioStream> stream); 58 59 private: 60 bool mBufferTuneEnabled = true; 61 62 // This will be used to automatically tune the buffer size of the stream, obtaining optimal latency 63 std::unique_ptr<oboe::LatencyTuner> mLatencyTuner; 64 oboe::AudioStream *mStream = nullptr; 65 }; 66 67 #endif //SAMPLES_LATENCY_TUNING_CALLBACK_H 68