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_AUDIO_SOURCE_CALLER_H 18 #define OBOE_AUDIO_SOURCE_CALLER_H 19 20 #include "OboeDebug.h" 21 #include "oboe/Oboe.h" 22 23 #include "flowgraph/FlowGraphNode.h" 24 #include "FixedBlockReader.h" 25 26 namespace oboe { 27 28 class AudioStreamCallback; 29 class AudioStream; 30 31 /** 32 * For output streams that use a callback, call the application for more data. 33 * For input streams that do not use a callback, read from the stream. 34 */ 35 class AudioSourceCaller : public flowgraph::FlowGraphSource, public FixedBlockProcessor { 36 public: AudioSourceCaller(int32_t channelCount,int32_t framesPerCallback,int32_t bytesPerSample)37 AudioSourceCaller(int32_t channelCount, int32_t framesPerCallback, int32_t bytesPerSample) 38 : FlowGraphSource(channelCount) 39 , mBlockReader(*this) { 40 mBlockReader.open(channelCount * framesPerCallback * bytesPerSample); 41 } 42 43 /** 44 * Set the stream to use as a source of data. 45 * @param stream 46 */ setStream(oboe::AudioStream * stream)47 void setStream(oboe::AudioStream *stream) { 48 mStream = stream; 49 } 50 getStream()51 oboe::AudioStream *getStream() { 52 return mStream; 53 } 54 55 /** 56 * Timeout value to use when calling audioStream->read(). 57 * @param timeoutNanos Zero for no timeout or time in nanoseconds. 58 */ setTimeoutNanos(int64_t timeoutNanos)59 void setTimeoutNanos(int64_t timeoutNanos) { 60 mTimeoutNanos = timeoutNanos; 61 } 62 getTimeoutNanos()63 int64_t getTimeoutNanos() const { 64 return mTimeoutNanos; 65 } 66 67 /** 68 * Called internally for block size adaptation. 69 * @param buffer 70 * @param numBytes 71 * @return 72 */ 73 int32_t onProcessFixedBlock(uint8_t *buffer, int32_t numBytes) override; 74 75 protected: 76 oboe::AudioStream *mStream = nullptr; 77 int64_t mTimeoutNanos = 0; 78 79 FixedBlockReader mBlockReader; 80 }; 81 82 } 83 #endif //OBOE_AUDIO_SOURCE_CALLER_H 84