1 /* 2 * Copyright 2015 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_STREAM_BASE_H_ 18 #define OBOE_STREAM_BASE_H_ 19 20 #include <memory> 21 #include "oboe/AudioStreamCallback.h" 22 #include "oboe/Definitions.h" 23 24 namespace oboe { 25 26 /** 27 * Base class containing parameters for audio streams and builders. 28 **/ 29 class AudioStreamBase { 30 31 public: 32 AudioStreamBase()33 AudioStreamBase() {} 34 35 virtual ~AudioStreamBase() = default; 36 37 // This class only contains primitives so we can use default constructor and copy methods. 38 39 /** 40 * Default copy constructor 41 */ 42 AudioStreamBase(const AudioStreamBase&) = default; 43 44 /** 45 * Default assignment operator 46 */ 47 AudioStreamBase& operator=(const AudioStreamBase&) = default; 48 49 /** 50 * @return number of channels, for example 2 for stereo, or kUnspecified 51 */ getChannelCount()52 int32_t getChannelCount() const { return mChannelCount; } 53 54 /** 55 * @return Direction::Input or Direction::Output 56 */ getDirection()57 Direction getDirection() const { return mDirection; } 58 59 /** 60 * @return sample rate for the stream or kUnspecified 61 */ getSampleRate()62 int32_t getSampleRate() const { return mSampleRate; } 63 64 /** 65 * @return the number of frames in each callback or kUnspecified. 66 */ getFramesPerCallback()67 int32_t getFramesPerCallback() const { return mFramesPerCallback; } 68 69 /** 70 * @return the audio sample format (e.g. Float or I16) 71 */ getFormat()72 AudioFormat getFormat() const { return mFormat; } 73 74 /** 75 * Query the maximum number of frames that can be filled without blocking. 76 * If the stream has been closed the last known value will be returned. 77 * 78 * @return buffer size 79 */ getBufferSizeInFrames()80 virtual int32_t getBufferSizeInFrames() { return mBufferSizeInFrames; } 81 82 /** 83 * @return capacityInFrames or kUnspecified 84 */ getBufferCapacityInFrames()85 virtual int32_t getBufferCapacityInFrames() const { return mBufferCapacityInFrames; } 86 87 /** 88 * @return the sharing mode of the stream. 89 */ getSharingMode()90 SharingMode getSharingMode() const { return mSharingMode; } 91 92 /** 93 * @return the performance mode of the stream. 94 */ getPerformanceMode()95 PerformanceMode getPerformanceMode() const { return mPerformanceMode; } 96 97 /** 98 * @return the device ID of the stream. 99 */ getDeviceId()100 int32_t getDeviceId() const { return mDeviceId; } 101 102 /** 103 * @return the callback object for this stream, if set. 104 */ getCallback()105 AudioStreamCallback* getCallback() const { 106 return mStreamCallback; 107 } 108 109 /** 110 * @return the usage for this stream. 111 */ getUsage()112 Usage getUsage() const { return mUsage; } 113 114 /** 115 * @return the stream's content type. 116 */ getContentType()117 ContentType getContentType() const { return mContentType; } 118 119 /** 120 * @return the stream's input preset. 121 */ getInputPreset()122 InputPreset getInputPreset() const { return mInputPreset; } 123 124 /** 125 * @return the stream's session ID allocation strategy (None or Allocate). 126 */ getSessionId()127 SessionId getSessionId() const { return mSessionId; } 128 129 /** 130 * @return true if Oboe can convert channel counts to achieve optimal results. 131 */ isChannelConversionAllowed()132 bool isChannelConversionAllowed() const { 133 return mChannelConversionAllowed; 134 } 135 136 /** 137 * @return true if Oboe can convert data formats to achieve optimal results. 138 */ isFormatConversionAllowed()139 bool isFormatConversionAllowed() const { 140 return mFormatConversionAllowed; 141 } 142 143 /** 144 * @return whether and how Oboe can convert sample rates to achieve optimal results. 145 */ getSampleRateConversionQuality()146 SampleRateConversionQuality getSampleRateConversionQuality() const { 147 return mSampleRateConversionQuality; 148 } 149 150 protected: 151 152 /** The callback which will be fired when new data is ready to be read/written **/ 153 AudioStreamCallback *mStreamCallback = nullptr; 154 /** Number of audio frames which will be requested in each callback */ 155 int32_t mFramesPerCallback = kUnspecified; 156 /** Stream channel count */ 157 int32_t mChannelCount = kUnspecified; 158 /** Stream sample rate */ 159 int32_t mSampleRate = kUnspecified; 160 /** Stream audio device ID */ 161 int32_t mDeviceId = kUnspecified; 162 /** Stream buffer capacity specified as a number of audio frames */ 163 int32_t mBufferCapacityInFrames = kUnspecified; 164 /** Stream buffer size specified as a number of audio frames */ 165 int32_t mBufferSizeInFrames = kUnspecified; 166 /** 167 * Number of frames which will be copied to/from the audio device in a single read/write 168 * operation 169 */ 170 int32_t mFramesPerBurst = kUnspecified; 171 172 /** Stream sharing mode */ 173 SharingMode mSharingMode = SharingMode::Shared; 174 /** Format of audio frames */ 175 AudioFormat mFormat = AudioFormat::Unspecified; 176 /** Stream direction */ 177 Direction mDirection = Direction::Output; 178 /** Stream performance mode */ 179 PerformanceMode mPerformanceMode = PerformanceMode::None; 180 181 /** Stream usage. Only active on Android 28+ */ 182 Usage mUsage = Usage::Media; 183 /** Stream content type. Only active on Android 28+ */ 184 ContentType mContentType = ContentType::Music; 185 /** Stream input preset. Only active on Android 28+ 186 * TODO InputPreset::Unspecified should be considered as a possible default alternative. 187 */ 188 InputPreset mInputPreset = InputPreset::VoiceRecognition; 189 /** Stream session ID allocation strategy. Only active on Android 28+ */ 190 SessionId mSessionId = SessionId::None; 191 192 // Control whether Oboe can convert channel counts to achieve optimal results. 193 bool mChannelConversionAllowed = false; 194 // Control whether Oboe can convert data formats to achieve optimal results. 195 bool mFormatConversionAllowed = false; 196 // Control whether and how Oboe can convert sample rates to achieve optimal results. 197 SampleRateConversionQuality mSampleRateConversionQuality = SampleRateConversionQuality::None; 198 199 /** Validate stream parameters that might not be checked in lower layers */ isValidConfig()200 virtual Result isValidConfig() { 201 switch (mFormat) { 202 case AudioFormat::Unspecified: 203 case AudioFormat::I16: 204 case AudioFormat::Float: 205 break; 206 207 default: 208 return Result::ErrorInvalidFormat; 209 } 210 211 switch (mSampleRateConversionQuality) { 212 case SampleRateConversionQuality::None: 213 case SampleRateConversionQuality::Fastest: 214 case SampleRateConversionQuality::Low: 215 case SampleRateConversionQuality::Medium: 216 case SampleRateConversionQuality::High: 217 case SampleRateConversionQuality::Best: 218 return Result::OK; 219 default: 220 return Result::ErrorIllegalArgument; 221 } 222 } 223 }; 224 225 } // namespace oboe 226 227 #endif /* OBOE_STREAM_BASE_H_ */ 228