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 
34 public:
35 
37 
38  AudioStreamBuilder(const AudioStreamBase &audioStreamBase): AudioStreamBase(audioStreamBase) {}
39 
46  AudioStreamBuilder *setChannelCount(int channelCount) {
47  mChannelCount = channelCount;
48  return this;
49  }
50 
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 
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 
157  mAudioApi = audioApi;
158  return this;
159  }
160 
168  static bool isAAudioSupported();
169 
178  static bool isAAudioRecommended();
179 
189  mSharingMode = sharingMode;
190  return this;
191  }
192 
202  mPerformanceMode = performanceMode;
203  return this;
204  }
205 
206 
221  mUsage = usage;
222  return this;
223  }
224 
239  mContentType = contentType;
240  return this;
241  }
242 
259  mInputPreset = inputPreset;
260  return this;
261  }
262 
288  mSessionId = sessionId;
289  return this;
290  }
291 
310  AudioStreamBuilder *setDeviceId(int32_t deviceId) {
311  mDeviceId = deviceId;
312  return this;
313  }
314 
325  mDataCallback = dataCallback;
326  return this;
327  }
328 
345  mErrorCallback = errorCallback;
346  return this;
347  }
348 
373  // Use the same callback object for both, dual inheritance.
374  mDataCallback = streamCallback;
375  mErrorCallback = streamCallback;
376  return this;
377  }
378 
389  mChannelConversionAllowed = allowed;
390  return this;
391  }
392 
401  mFormatConversionAllowed = allowed;
402  return this;
403  }
404 
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_ */
AudioStreamBuilder * setDeviceId(int32_t deviceId)
Definition: AudioStreamBuilder.h:310
Definition: AudioStreamBuilder.h:33
SessionId mSessionId
Definition: AudioStreamBase.h:223
AudioStreamBuilder * setPerformanceMode(PerformanceMode performanceMode)
Definition: AudioStreamBuilder.h:201
Direction mDirection
Definition: AudioStreamBase.h:210
AudioStreamDataCallback * mDataCallback
Definition: AudioStreamBase.h:182
AudioFormat mFormat
Definition: AudioStreamBase.h:208
ContentType mContentType
Definition: AudioStreamBase.h:217
static bool isAAudioRecommended()
AudioStreamBuilder * setSampleRateConversionQuality(SampleRateConversionQuality quality)
Definition: AudioStreamBuilder.h:416
PerformanceMode
Definition: Definitions.h:192
SessionId
Definition: Definitions.h:414
AudioStreamBuilder * setSessionId(SessionId sessionId)
Definition: AudioStreamBuilder.h:287
PerformanceMode mPerformanceMode
Definition: AudioStreamBase.h:212
Definition: AudioStreamCallback.h:34
Definition: AudioStreamCallback.h:181
SharingMode mSharingMode
Definition: AudioStreamBase.h:206
AudioStreamBuilder * setContentType(ContentType contentType)
Definition: AudioStreamBuilder.h:238
int32_t mChannelCount
Definition: AudioStreamBase.h:190
AudioApi
Definition: Definitions.h:213
ContentType
Definition: Definitions.h:339
AudioStreamBuilder * setUsage(Usage usage)
Definition: AudioStreamBuilder.h:220
int32_t mDeviceId
Definition: AudioStreamBase.h:194
static bool isAAudioSupported()
AudioStreamBuilder * setChannelConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:388
AudioStreamBuilder * setFramesPerCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:80
AudioStreamBuilder * setDirection(Direction direction)
Definition: AudioStreamBuilder.h:56
AudioStreamBuilder * setFormat(AudioFormat format)
Definition: AudioStreamBuilder.h:109
Usage mUsage
Definition: AudioStreamBase.h:215
Definition: AudioStream.h:44
Usage
Definition: Definitions.h:263
Definition: AudioStreamCallback.h:97
AudioStreamBuilder * setAudioApi(AudioApi audioApi)
Definition: AudioStreamBuilder.h:156
AudioStreamBuilder * setInputPreset(InputPreset inputPreset)
Definition: AudioStreamBuilder.h:258
Result openManagedStream(ManagedStream &stream)
AudioStreamBuilder * setSampleRate(int32_t sampleRate)
Definition: AudioStreamBuilder.h:72
int32_t mSampleRate
Definition: AudioStreamBase.h:192
Definition: AudioStreamBase.h:29
int32_t mBufferCapacityInFrames
Definition: AudioStreamBase.h:196
AudioStreamBuilder * setBufferCapacityInFrames(int32_t bufferCapacityInFrames)
Definition: AudioStreamBuilder.h:126
AudioStreamBuilder * setFramesPerDataCallback(int framesPerCallback)
Definition: AudioStreamBuilder.h:98
Result openStream(AudioStream **stream)
AudioFormat
Definition: Definitions.h:94
AudioStreamBuilder * setErrorCallback(oboe::AudioStreamErrorCallback *errorCallback)
Definition: AudioStreamBuilder.h:344
Result
Definition: Definitions.h:131
AudioStreamBuilder * setSharingMode(SharingMode sharingMode)
Definition: AudioStreamBuilder.h:188
Definition: AudioStream.h:31
bool willUseAAudio() const
Definition: AudioStreamBuilder.h:424
SampleRateConversionQuality
Definition: Definitions.h:235
InputPreset mInputPreset
Definition: AudioStreamBase.h:221
Direction
Definition: Definitions.h:78
SharingMode
Definition: Definitions.h:167
InputPreset
Definition: Definitions.h:372
AudioStreamBuilder * setChannelCount(int channelCount)
Definition: AudioStreamBuilder.h:46
AudioStreamBuilder * setCallback(AudioStreamCallback *streamCallback)
Definition: AudioStreamBuilder.h:372
AudioStreamBuilder * setDataCallback(oboe::AudioStreamDataCallback *dataCallback)
Definition: AudioStreamBuilder.h:324
int32_t mFramesPerCallback
Definition: AudioStreamBase.h:188
AudioStreamBuilder * setFormatConversionAllowed(bool allowed)
Definition: AudioStreamBuilder.h:400
AudioApi getAudioApi() const
Definition: AudioStreamBuilder.h:141
AudioStreamErrorCallback * mErrorCallback
Definition: AudioStreamBase.h:185