Oboe 1.5 A library for creating real-time audio apps on Android

AudioStreamBuilder.h
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_BUILDER_H_
18 #define OBOE_STREAM_BUILDER_H_
19
20 #include "oboe/Definitions.h"
21 #include "oboe/AudioStreamBase.h"
22 #include "ResultWithValue.h"
23
24 namespace oboe {
25
26 // This depends on AudioStream, so we use forward declaration, it will close and delete the stream
27 struct StreamDeleterFunctor;
28 using ManagedStream = std::unique_ptr<AudioStream, StreamDeleterFunctor>;
29
33 class AudioStreamBuilder : public AudioStreamBase {
34 public:
35
36 AudioStreamBuilder() : AudioStreamBase() {}
37
38 AudioStreamBuilder(const AudioStreamBase &audioStreamBase): AudioStreamBase(audioStreamBase) {}
39
46 AudioStreamBuilder *setChannelCount(int channelCount) {
47 mChannelCount = channelCount;
48 return this;
49 }
50
56 AudioStreamBuilder *setDirection(Direction direction) {
57 mDirection = direction;
58 return this;
59 }
60
72 AudioStreamBuilder *setSampleRate(int32_t sampleRate) {
73 mSampleRate = sampleRate;
74 return this;
75 }
76
80 AudioStreamBuilder *setFramesPerCallback(int framesPerCallback) {
81 return setFramesPerDataCallback(framesPerCallback);
82 }
83
98 AudioStreamBuilder *setFramesPerDataCallback(int framesPerCallback) {
99 mFramesPerCallback = framesPerCallback;
100 return this;
101 }
102
109 AudioStreamBuilder *setFormat(AudioFormat format) {
110 mFormat = format;
111 return this;
112 }
113
126 AudioStreamBuilder *setBufferCapacityInFrames(int32_t bufferCapacityInFrames) {
127 mBufferCapacityInFrames = bufferCapacityInFrames;
128 return this;
129 }
130
141 AudioApi getAudioApi() const { return mAudioApi; }
142
156 AudioStreamBuilder *setAudioApi(AudioApi audioApi) {
157 mAudioApi = audioApi;
158 return this;
159 }
160
168 static bool isAAudioSupported();
169
178 static bool isAAudioRecommended();
179
188 AudioStreamBuilder *setSharingMode(SharingMode sharingMode) {
189 mSharingMode = sharingMode;
190 return this;
191 }
192
201 AudioStreamBuilder *setPerformanceMode(PerformanceMode performanceMode) {
202 mPerformanceMode = performanceMode;
203 return this;
204 }
205
206
220 AudioStreamBuilder *setUsage(Usage usage) {
221 mUsage = usage;
222 return this;
223 }
224
238 AudioStreamBuilder *setContentType(ContentType contentType) {
239 mContentType = contentType;
240 return this;
241 }
242
258 AudioStreamBuilder *setInputPreset(InputPreset inputPreset) {
259 mInputPreset = inputPreset;
260 return this;
261 }
262
287 AudioStreamBuilder *setSessionId(SessionId sessionId) {
288 mSessionId = sessionId;
289 return this;
290 }
291
310 AudioStreamBuilder *setDeviceId(int32_t deviceId) {
311 mDeviceId = deviceId;
312 return this;
313 }
314
324 AudioStreamBuilder *setDataCallback(oboe::AudioStreamDataCallback *dataCallback) {
325 mDataCallback = dataCallback;
326 return this;
327 }
328
344 AudioStreamBuilder *setErrorCallback(oboe::AudioStreamErrorCallback *errorCallback) {
345 mErrorCallback = errorCallback;
346 return this;
347 }
348
372 AudioStreamBuilder *setCallback(AudioStreamCallback *streamCallback) {
373 // Use the same callback object for both, dual inheritance.
374 mDataCallback = streamCallback;
375 mErrorCallback = streamCallback;
376 return this;
377 }
378
388 AudioStreamBuilder *setChannelConversionAllowed(bool allowed) {
389 mChannelConversionAllowed = allowed;
390 return this;
391 }
392
400 AudioStreamBuilder *setFormatConversionAllowed(bool allowed) {
401 mFormatConversionAllowed = allowed;
402 return this;
403 }
404
416 AudioStreamBuilder *setSampleRateConversionQuality(SampleRateConversionQuality quality) {
417 mSampleRateConversionQuality = quality;
418 return this;
419 }
420
424 bool willUseAAudio() const {
425 return (mAudioApi == AudioApi::AAudio && isAAudioSupported())
426 || (mAudioApi == AudioApi::Unspecified && isAAudioRecommended());
427 }
428
438 Result openStream(AudioStream **stream);
439
450 Result openStream(std::shared_ptr<oboe::AudioStream> &stream);
451
461 Result openManagedStream(ManagedStream &stream);
462
463 private:
464
469 bool isCompatible(AudioStreamBase &other);
470
478 oboe::AudioStream *build();
479
480 AudioApi mAudioApi = AudioApi::Unspecified;
481 };
482
483 } // namespace oboe
484
485 #endif /* OBOE_STREAM_BUILDER_H_ */
oboe::AudioStreamBuilder::setDeviceId
AudioStreamBuilder * setDeviceId(int32_t deviceId)
Definition: AudioStreamBuilder.h:310
oboe::AudioApi::AAudio
oboe::AudioStreamBuilder
Definition: AudioStreamBuilder.h:33
oboe::AudioStreamBase::mSessionId
SessionId mSessionId
Definition: AudioStreamBase.h:223
oboe::AudioStreamBuilder::setPerformanceMode
AudioStreamBuilder * setPerformanceMode(PerformanceMode performanceMode)
Definition: AudioStreamBuilder.h:201
oboe::AudioStreamBase::mDirection
Direction mDirection
Definition: AudioStreamBase.h:210
oboe::AudioStreamBase::mDataCallback
AudioStreamDataCallback * mDataCallback
Definition: AudioStreamBase.h:182
oboe::AudioStreamBase::mFormat
AudioFormat mFormat
Definition: AudioStreamBase.h:208
oboe::AudioStreamBase::mContentType
ContentType mContentType
Definition: AudioStreamBase.h:217
oboe::AudioStreamBuilder::isAAudioRecommended
static bool isAAudioRecommended()
oboe::AudioStreamBuilder::setSampleRateConversionQuality
AudioStreamBuilder * setSampleRateConversionQuality(SampleRateConversionQuality quality)
Definition: AudioStreamBuilder.h:416
oboe::PerformanceMode
PerformanceMode
Definition: Definitions.h:192
oboe::SessionId
SessionId
Definition: Definitions.h:414
oboe::AudioStreamBuilder::setSessionId
AudioStreamBuilder * setSessionId(SessionId sessionId)
Definition: AudioStreamBuilder.h:287
oboe::AudioStreamBase::mPerformanceMode
PerformanceMode mPerformanceMode
Definition: AudioStreamBase.h:212
oboe::AudioStreamDataCallback
Definition: AudioStreamCallback.h:34
oboe::AudioStreamCallback
Definition: AudioStreamCallback.h:181
oboe::AudioStreamBase::mSharingMode
SharingMode mSharingMode
Definition: AudioStreamBase.h:206
oboe::AudioStreamBuilder::setContentType
AudioStreamBuilder * setContentType(ContentType contentType)
Definition: AudioStreamBuilder.h:238
oboe::AudioStreamBase::mChannelCount
int32_t mChannelCount
Definition: AudioStreamBase.h:190
oboe::AudioApi
AudioApi
Definition: Definitions.h:213
oboe::ContentType
ContentType
Definition: Definitions.h:339
oboe::AudioStreamBuilder::setUsage
AudioStreamBuilder * setUsage(Usage usage)
Definition: AudioStreamBuilder.h:220
oboe::AudioStreamBase::mDeviceId
int32_t mDeviceId
Definition: AudioStreamBase.h:194
oboe::AudioStreamBuilder::isAAudioSupported
static bool isAAudioSupported()
oboe::AudioStreamBuilder::setChannelConversionAllowed
AudioStreamBuilder * setChannelConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:388
oboe::AudioStreamBuilder::setFramesPerCallback
AudioStreamBuilder * setFramesPerCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:80
oboe::AudioStreamBuilder::setDirection
AudioStreamBuilder * setDirection(Direction direction)
Definition: AudioStreamBuilder.h:56
oboe::AudioStreamBuilder::setFormat
AudioStreamBuilder * setFormat(AudioFormat format)
Definition: AudioStreamBuilder.h:109
oboe::AudioStreamBase::mUsage
Usage mUsage
Definition: AudioStreamBase.h:215
oboe::AudioStream
Definition: AudioStream.h:44
oboe::Usage
Usage
Definition: Definitions.h:263
oboe::AudioStreamErrorCallback
Definition: AudioStreamCallback.h:97
oboe::AudioStreamBuilder::setAudioApi
AudioStreamBuilder * setAudioApi(AudioApi audioApi)
Definition: AudioStreamBuilder.h:156
oboe::AudioStreamBuilder::setInputPreset
AudioStreamBuilder * setInputPreset(InputPreset inputPreset)
Definition: AudioStreamBuilder.h:258
oboe::AudioStreamBuilder::openManagedStream
Result openManagedStream(ManagedStream &stream)
oboe::AudioStreamBuilder::setSampleRate
AudioStreamBuilder * setSampleRate(int32_t sampleRate)
Definition: AudioStreamBuilder.h:72
oboe::AudioStreamBase::mSampleRate
int32_t mSampleRate
Definition: AudioStreamBase.h:192
oboe::AudioStreamBase
Definition: AudioStreamBase.h:29
oboe::AudioStreamBase::mBufferCapacityInFrames
int32_t mBufferCapacityInFrames
Definition: AudioStreamBase.h:196
oboe::AudioStreamBuilder::setBufferCapacityInFrames
AudioStreamBuilder * setBufferCapacityInFrames(int32_t bufferCapacityInFrames)
Definition: AudioStreamBuilder.h:126
oboe::AudioStreamBuilder::setFramesPerDataCallback
AudioStreamBuilder * setFramesPerDataCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:98
oboe::AudioStreamBuilder::openStream
Result openStream(AudioStream **stream)
oboe::AudioFormat
AudioFormat
Definition: Definitions.h:94
oboe::AudioStreamBuilder::setErrorCallback
AudioStreamBuilder * setErrorCallback(oboe::AudioStreamErrorCallback *errorCallback)
Definition: AudioStreamBuilder.h:344
oboe::Result
Result
Definition: Definitions.h:131
oboe::AudioStreamBuilder::setSharingMode
AudioStreamBuilder * setSharingMode(SharingMode sharingMode)
Definition: AudioStreamBuilder.h:188
oboe
Definition: AudioStream.h:31
oboe::AudioStreamBuilder::willUseAAudio
bool willUseAAudio() const
Definition: AudioStreamBuilder.h:424
oboe::SampleRateConversionQuality
SampleRateConversionQuality
Definition: Definitions.h:235
oboe::AudioStreamBase::mInputPreset
InputPreset mInputPreset
Definition: AudioStreamBase.h:221
oboe::Direction
Direction
Definition: Definitions.h:78
oboe::SharingMode
SharingMode
Definition: Definitions.h:167
oboe::InputPreset
InputPreset
Definition: Definitions.h:372
oboe::AudioStreamBuilder::setChannelCount
AudioStreamBuilder * setChannelCount(int channelCount)
Definition: AudioStreamBuilder.h:46
oboe::AudioStreamBuilder::setCallback
AudioStreamBuilder * setCallback(AudioStreamCallback *streamCallback)
Definition: AudioStreamBuilder.h:372
oboe::AudioStreamBuilder::setDataCallback
AudioStreamBuilder * setDataCallback(oboe::AudioStreamDataCallback *dataCallback)
Definition: AudioStreamBuilder.h:324
oboe::AudioStreamBase::mFramesPerCallback
int32_t mFramesPerCallback
Definition: AudioStreamBase.h:188
oboe::AudioStreamBuilder::setFormatConversionAllowed
AudioStreamBuilder * setFormatConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:400
oboe::AudioStreamBuilder::getAudioApi
AudioApi getAudioApi() const
Definition: AudioStreamBuilder.h:141
oboe::AudioStreamBase::mErrorCallback
AudioStreamErrorCallback * mErrorCallback
Definition: AudioStreamBase.h:185