1 /* 2 * Copyright 2019 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_FULL_DUPLEX_STREAM_H 18 #define OBOE_FULL_DUPLEX_STREAM_H 19 20 #include <unistd.h> 21 #include <sys/types.h> 22 23 #include "oboe/Oboe.h" 24 25 class FullDuplexStream : public oboe::AudioStreamCallback { 26 public: FullDuplexStream()27 FullDuplexStream() {} 28 virtual ~FullDuplexStream() = default; 29 setInputStream(std::shared_ptr<oboe::AudioStream> stream)30 void setInputStream(std::shared_ptr<oboe::AudioStream> stream) { 31 mInputStream = stream; 32 } 33 setOutputStream(std::shared_ptr<oboe::AudioStream> stream)34 void setOutputStream(std::shared_ptr<oboe::AudioStream> stream) { 35 mOutputStream = stream; 36 } 37 38 virtual oboe::Result start(); 39 40 virtual oboe::Result stop(); 41 42 /** 43 * Called when data is available on both streams. 44 * App should override this method. 45 */ 46 virtual oboe::DataCallbackResult onBothStreamsReady( 47 std::shared_ptr<oboe::AudioStream> inputStream, 48 const void *inputData, 49 int numInputFrames, 50 std::shared_ptr<oboe::AudioStream> outputStream, 51 void *outputData, 52 int numOutputFrames 53 ) = 0; 54 55 /** 56 * Called by Oboe when the stream is ready to process audio. 57 * This implements the stream synchronization. App should NOT override this method. 58 */ 59 oboe::DataCallbackResult onAudioReady( 60 oboe::AudioStream *audioStream, 61 void *audioData, 62 int numFrames) override; 63 64 int32_t getNumInputBurstsCushion() const; 65 66 /** 67 * Number of bursts to leave in the input buffer as a cushion. 68 * Typically 0 for latency measurements 69 * or 1 for glitch tests. 70 * 71 * @param mNumInputBurstsCushion 72 */ 73 void setNumInputBurstsCushion(int32_t numInputBurstsCushion); 74 75 private: 76 77 // TODO add getters and setters 78 static constexpr int32_t kNumCallbacksToDrain = 20; 79 static constexpr int32_t kNumCallbacksToDiscard = 30; 80 81 // let input fill back up, usually 0 or 1 82 int32_t mNumInputBurstsCushion = 1; 83 84 // We want to reach a state where the input buffer is empty and 85 // the output buffer is full. 86 // These are used in order. 87 // Drain several callback so that input is empty. 88 int32_t mCountCallbacksToDrain = kNumCallbacksToDrain; 89 // Let the input fill back up slightly so we don't run dry. 90 int32_t mCountInputBurstsCushion = mNumInputBurstsCushion; 91 // Discard some callbacks so the input and output reach equilibrium. 92 int32_t mCountCallbacksToDiscard = kNumCallbacksToDiscard; 93 94 std::shared_ptr<oboe::AudioStream> mInputStream; 95 std::shared_ptr<oboe::AudioStream> mOutputStream; 96 97 int32_t mBufferSize = 0; 98 std::unique_ptr<float[]> mInputBuffer; 99 }; 100 101 102 #endif //OBOE_FULL_DUPLEX_STREAM_H 103