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