Oboe
A library for creating real-time audio apps on Android
Loading...
Searching...
No Matches
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 "oboe/Utilities.h"
23#include "ResultWithValue.h"
24
25namespace oboe {
26
27 // This depends on AudioStream, so we use forward declaration, it will close and delete the stream
28 struct StreamDeleterFunctor;
29 using ManagedStream = std::unique_ptr<AudioStream, StreamDeleterFunctor>;
30
35public:
36
38
39 AudioStreamBuilder(const AudioStreamBase &audioStreamBase): AudioStreamBase(audioStreamBase) {}
40
52 mChannelCount = channelCount;
53 mChannelMask = ChannelMask::Unspecified;
54 return this;
55 }
56
71 mChannelMask = channelMask;
72 mChannelCount = getChannelCountFromChannelMask(channelMask);
73 return this;
74 }
75
82 mDirection = direction;
83 return this;
84 }
85
97 AudioStreamBuilder *setSampleRate(int32_t sampleRate) {
98 mSampleRate = sampleRate;
99 return this;
100 }
101
105 AudioStreamBuilder *setFramesPerCallback(int framesPerCallback) {
106 return setFramesPerDataCallback(framesPerCallback);
107 }
108
132 mFramesPerCallback = framesPerCallback;
133 return this;
134 }
135
143 mFormat = format;
144 return this;
145 }
146
159 AudioStreamBuilder *setBufferCapacityInFrames(int32_t bufferCapacityInFrames) {
160 mBufferCapacityInFrames = bufferCapacityInFrames;
161 return this;
162 }
163
174 AudioApi getAudioApi() const { return mAudioApi; }
175
190 mAudioApi = audioApi;
191 return this;
192 }
193
201 static bool isAAudioSupported();
202
211 static bool isAAudioRecommended();
212
222 mSharingMode = sharingMode;
223 return this;
224 }
225
235 mPerformanceMode = performanceMode;
236 return this;
237 }
238
239
254 mUsage = usage;
255 return this;
256 }
257
272 mContentType = contentType;
273 return this;
274 }
275
292 mInputPreset = inputPreset;
293 return this;
294 }
295
321 mSessionId = sessionId;
322 return this;
323 }
324
346 AudioStreamBuilder *setDeviceId(int32_t deviceId) {
347 mDeviceId = deviceId;
348 return this;
349 }
350
363 AudioStreamBuilder *setDataCallback(std::shared_ptr<AudioStreamDataCallback> sharedDataCallback) {
364 // Use this raw pointer in the rest of the code to retain backwards compatibility.
365 mDataCallback = sharedDataCallback.get();
366 // Hold a shared_ptr to protect the raw pointer for the lifetime of the stream.
367 mSharedDataCallback = sharedDataCallback;
368 return this;
369 }
370
380 mDataCallback = dataCallback;
381 mSharedDataCallback = nullptr;
382 return this;
383 }
384
404 AudioStreamBuilder *setErrorCallback(std::shared_ptr<AudioStreamErrorCallback> sharedErrorCallback) {
405 // Use this raw pointer in the rest of the code to retain backwards compatibility.
406 mErrorCallback = sharedErrorCallback.get();
407 // Hold a shared_ptr to protect the raw pointer for the lifetime of the stream.
408 mSharedErrorCallback = sharedErrorCallback;
409 return this;
410 }
411
421 mErrorCallback = errorCallback;
422 mSharedErrorCallback = nullptr;
423 return this;
424 }
425
440 // Use the same callback object for both, dual inheritance.
441 mDataCallback = streamCallback;
442 mErrorCallback = streamCallback;
443 return this;
444 }
445
456 mChannelConversionAllowed = allowed;
457 return this;
458 }
459
468 mFormatConversionAllowed = allowed;
469 return this;
470 }
471
484 mSampleRateConversionQuality = quality;
485 return this;
486 }
487
504 AudioStreamBuilder *setPackageName(std::string packageName) {
505 mPackageName = packageName;
506 return this;
507 }
508
520 AudioStreamBuilder *setAttributionTag(std::string attributionTag) {
521 mAttributionTag = attributionTag;
522 return this;
523 }
524
528 bool willUseAAudio() const {
529 return (mAudioApi == AudioApi::AAudio && isAAudioSupported())
530 || (mAudioApi == AudioApi::Unspecified && isAAudioRecommended());
531 }
532
544
555 Result openStream(std::shared_ptr<oboe::AudioStream> &stream);
556
568 Result openManagedStream(ManagedStream &stream);
569
570private:
571
576 bool isCompatible(AudioStreamBase &other);
577
585 oboe::AudioStream *build();
586
588};
589
590} // namespace oboe
591
592#endif /* OBOE_STREAM_BUILDER_H_ */
Definition: AudioStreamBase.h:30
InputPreset mInputPreset
Definition: AudioStreamBase.h:228
int32_t mDeviceId
Definition: AudioStreamBase.h:204
Direction mDirection
Definition: AudioStreamBase.h:217
int32_t mFramesPerCallback
Definition: AudioStreamBase.h:198
Usage mUsage
Definition: AudioStreamBase.h:222
ContentType mContentType
Definition: AudioStreamBase.h:224
int32_t mChannelCount
Definition: AudioStreamBase.h:200
AudioStreamDataCallback * mDataCallback
Definition: AudioStreamBase.h:190
AudioFormat mFormat
Definition: AudioStreamBase.h:215
int32_t mSampleRate
Definition: AudioStreamBase.h:202
ChannelMask mChannelMask
Definition: AudioStreamBase.h:210
PerformanceMode mPerformanceMode
Definition: AudioStreamBase.h:219
SessionId mSessionId
Definition: AudioStreamBase.h:230
std::string mPackageName
Definition: AudioStreamBase.h:233
int32_t mBufferCapacityInFrames
Definition: AudioStreamBase.h:206
AudioStreamErrorCallback * mErrorCallback
Definition: AudioStreamBase.h:194
std::string mAttributionTag
Definition: AudioStreamBase.h:235
SharingMode mSharingMode
Definition: AudioStreamBase.h:213
Definition: AudioStreamBuilder.h:34
AudioStreamBuilder * setSampleRateConversionQuality(SampleRateConversionQuality quality)
Definition: AudioStreamBuilder.h:483
AudioStreamBuilder * setChannelMask(ChannelMask channelMask)
Definition: AudioStreamBuilder.h:70
static bool isAAudioSupported()
AudioStreamBuilder * setChannelCount(int channelCount)
Definition: AudioStreamBuilder.h:51
AudioStreamBuilder * setFramesPerCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:105
AudioStreamBuilder * setAudioApi(AudioApi audioApi)
Definition: AudioStreamBuilder.h:189
AudioStreamBuilder * setErrorCallback(AudioStreamErrorCallback *errorCallback)
Definition: AudioStreamBuilder.h:420
AudioStreamBuilder * setInputPreset(InputPreset inputPreset)
Definition: AudioStreamBuilder.h:291
AudioStreamBuilder * setSessionId(SessionId sessionId)
Definition: AudioStreamBuilder.h:320
Result openStream(std::shared_ptr< oboe::AudioStream > &stream)
AudioStreamBuilder * setChannelConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:455
static bool isAAudioRecommended()
AudioStreamBuilder * setDeviceId(int32_t deviceId)
Definition: AudioStreamBuilder.h:346
AudioStreamBuilder * setPackageName(std::string packageName)
Definition: AudioStreamBuilder.h:504
AudioStreamBuilder * setSampleRate(int32_t sampleRate)
Definition: AudioStreamBuilder.h:97
Result openManagedStream(ManagedStream &stream)
AudioStreamBuilder * setDataCallback(std::shared_ptr< AudioStreamDataCallback > sharedDataCallback)
Definition: AudioStreamBuilder.h:363
AudioStreamBuilder * setErrorCallback(std::shared_ptr< AudioStreamErrorCallback > sharedErrorCallback)
Definition: AudioStreamBuilder.h:404
Result openStream(AudioStream **stream)
AudioStreamBuilder * setUsage(Usage usage)
Definition: AudioStreamBuilder.h:253
AudioStreamBuilder * setAttributionTag(std::string attributionTag)
Definition: AudioStreamBuilder.h:520
bool willUseAAudio() const
Definition: AudioStreamBuilder.h:528
AudioStreamBuilder * setFormatConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:467
AudioStreamBuilder * setDirection(Direction direction)
Definition: AudioStreamBuilder.h:81
AudioStreamBuilder * setSharingMode(SharingMode sharingMode)
Definition: AudioStreamBuilder.h:221
AudioStreamBuilder * setFormat(AudioFormat format)
Definition: AudioStreamBuilder.h:142
AudioApi getAudioApi() const
Definition: AudioStreamBuilder.h:174
AudioStreamBuilder * setPerformanceMode(PerformanceMode performanceMode)
Definition: AudioStreamBuilder.h:234
AudioStreamBuilder * setBufferCapacityInFrames(int32_t bufferCapacityInFrames)
Definition: AudioStreamBuilder.h:159
AudioStreamBuilder * setContentType(ContentType contentType)
Definition: AudioStreamBuilder.h:271
AudioStreamBuilder * setDataCallback(AudioStreamDataCallback *dataCallback)
Definition: AudioStreamBuilder.h:379
AudioStreamBuilder * setFramesPerDataCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:131
AudioStreamBuilder * setCallback(AudioStreamCallback *streamCallback)
Definition: AudioStreamBuilder.h:439
Definition: AudioStreamCallback.h:186
Definition: AudioStreamCallback.h:34
Definition: AudioStreamCallback.h:101
Definition: AudioStream.h:44
Definition: AudioStream.h:31
Usage
Definition: Definitions.h:301
PerformanceMode
Definition: Definitions.h:218
ContentType
Definition: Definitions.h:377
InputPreset
Definition: Definitions.h:410
Result
Definition: Definitions.h:157
SessionId
Definition: Definitions.h:452
SampleRateConversionQuality
Definition: Definitions.h:264
SharingMode
Definition: Definitions.h:193
AudioApi
Definition: Definitions.h:239
AudioFormat
Definition: Definitions.h:93
ChannelMask
Definition: Definitions.h:506
Direction
Definition: Definitions.h:77