1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OBOE_STREAM_BASE_H_
#define OBOE_STREAM_BASE_H_
#include <memory>
#include <string>
#include "oboe/AudioStreamCallback.h"
#include "oboe/Definitions.h"
namespace oboe {
/**
* Base class containing parameters for audio streams and builders.
**/
class AudioStreamBase {
public:
AudioStreamBase() {}
virtual ~AudioStreamBase() = default;
// This class only contains primitives so we can use default constructor and copy methods.
/**
* Default copy constructor
*/
AudioStreamBase(const AudioStreamBase&) = default;
/**
* Default assignment operator
*/
AudioStreamBase& operator=(const AudioStreamBase&) = default;
/**
* @return number of channels, for example 2 for stereo, or kUnspecified
*/
int32_t getChannelCount() const { return mChannelCount; }
/**
* @return Direction::Input or Direction::Output
*/
Direction getDirection() const { return mDirection; }
/**
* @return sample rate for the stream or kUnspecified
*/
int32_t getSampleRate() const { return mSampleRate; }
/**
* @deprecated use `getFramesPerDataCallback` instead.
*/
int32_t getFramesPerCallback() const { return getFramesPerDataCallback(); }
/**
* @return the number of frames in each data callback or kUnspecified.
*/
int32_t getFramesPerDataCallback() const { return mFramesPerCallback; }
/**
* @return the audio sample format (e.g. Float or I16)
*/
AudioFormat getFormat() const { return mFormat; }
/**
* Query the maximum number of frames that can be filled without blocking.
* If the stream has been closed the last known value will be returned.
*
* @return buffer size
*/
virtual int32_t getBufferSizeInFrames() { return mBufferSizeInFrames; }
/**
* @return capacityInFrames or kUnspecified
*/
virtual int32_t getBufferCapacityInFrames() const { return mBufferCapacityInFrames; }
/**
* @return the sharing mode of the stream.
*/
SharingMode getSharingMode() const { return mSharingMode; }
/**
* @return the performance mode of the stream.
*/
PerformanceMode getPerformanceMode() const { return mPerformanceMode; }
/**
* @return the device ID of the stream.
*/
int32_t getDeviceId() const { return mDeviceId; }
/**
* For internal use only.
* @return the data callback object for this stream, if set.
*/
AudioStreamDataCallback *getDataCallback() const {
return mDataCallback;
}
/**
* For internal use only.
* @return the error callback object for this stream, if set.
*/
AudioStreamErrorCallback *getErrorCallback() const {
return mErrorCallback;
}
/**
* @return true if a data callback was set for this stream
*/
bool isDataCallbackSpecified() const {
return mDataCallback != nullptr;
}
/**
* Note that if the app does not set an error callback then a
* default one may be provided.
* @return true if an error callback was set for this stream
*/
bool isErrorCallbackSpecified() const {
return mErrorCallback != nullptr;
}
/**
* @return the usage for this stream.
*/
Usage getUsage() const { return mUsage; }
/**
* @return the stream's content type.
*/
ContentType getContentType() const { return mContentType; }
/**
* @return the stream's input preset.
*/
InputPreset getInputPreset() const { return mInputPreset; }
/**
* @return the stream's session ID allocation strategy (None or Allocate).
*/
SessionId getSessionId() const { return mSessionId; }
/**
* @return whether the content of the stream is spatialized.
*/
bool isContentSpatialized() const { return mIsContentSpatialized; }
/**
* @return the spatialization behavior for the stream.
*/
SpatializationBehavior getSpatializationBehavior() const { return mSpatializationBehavior; }
/**
* Return the policy that determines whether the audio may or may not be captured
* by other apps or the system.
*
* See AudioStreamBuilder_setAllowedCapturePolicy().
*
* Added in API level 29 to AAudio.
*
* @return the allowed capture policy, for example AllowedCapturePolicy::All
*/
AllowedCapturePolicy getAllowedCapturePolicy() const { return mAllowedCapturePolicy; }
/**
* Return whether this input stream is marked as privacy sensitive.
*
* See AudioStreamBuilder_setPrivacySensitiveMode().
*
* Added in API level 30 to AAudio.
*
* @return PrivacySensitiveMode::Enabled if privacy sensitive,
* PrivacySensitiveMode::Disabled if not privacy sensitive, and
* PrivacySensitiveMode::Unspecified if API is not supported.
*/
PrivacySensitiveMode getPrivacySensitiveMode() const { return mPrivacySensitiveMode; }
/**
* @return true if Oboe can convert channel counts to achieve optimal results.
*/
bool isChannelConversionAllowed() const {
return mChannelConversionAllowed;
}
/**
* @return true if Oboe can convert data formats to achieve optimal results.
*/
bool isFormatConversionAllowed() const {
return mFormatConversionAllowed;
}
/**
* @return whether and how Oboe can convert sample rates to achieve optimal results.
*/
SampleRateConversionQuality getSampleRateConversionQuality() const {
return mSampleRateConversionQuality;
}
/**
* @return the stream's channel mask.
*/
ChannelMask getChannelMask() const {
return mChannelMask;
}
/**
* @return number of channels for the hardware, for example 2 for stereo, or kUnspecified.
*/
int32_t getHardwareChannelCount() const { return mHardwareChannelCount; }
/**
* @return hardware sample rate for the stream or kUnspecified
*/
int32_t getHardwareSampleRate() const { return mHardwareSampleRate; }
/**
* @return the audio sample format of the hardware (e.g. Float or I16)
*/
AudioFormat getHardwareFormat() const { return mHardwareFormat; }
protected:
/** The callback which will be fired when new data is ready to be read/written. **/
AudioStreamDataCallback *mDataCallback = nullptr;
std::shared_ptr<AudioStreamDataCallback> mSharedDataCallback;
/** The callback which will be fired when an error or a disconnect occurs. **/
AudioStreamErrorCallback *mErrorCallback = nullptr;
std::shared_ptr<AudioStreamErrorCallback> mSharedErrorCallback;
/** Number of audio frames which will be requested in each callback */
int32_t mFramesPerCallback = kUnspecified;
/** Stream channel count */
int32_t mChannelCount = kUnspecified;
/** Stream sample rate */
int32_t mSampleRate = kUnspecified;
/** Stream audio device ID */
int32_t mDeviceId = kUnspecified;
/** Stream buffer capacity specified as a number of audio frames */
int32_t mBufferCapacityInFrames = kUnspecified;
/** Stream buffer size specified as a number of audio frames */
int32_t mBufferSizeInFrames = kUnspecified;
/** Stream channel mask. Only active on Android 32+ */
ChannelMask mChannelMask = ChannelMask::Unspecified;
/** Stream sharing mode */
SharingMode mSharingMode = SharingMode::Shared;
/** Format of audio frames */
AudioFormat mFormat = AudioFormat::Unspecified;
/** Stream direction */
Direction mDirection = Direction::Output;
/** Stream performance mode */
PerformanceMode mPerformanceMode = PerformanceMode::None;
/** Stream usage. Only active on Android 28+ */
Usage mUsage = Usage::Media;
/** Stream content type. Only active on Android 28+ */
ContentType mContentType = ContentType::Music;
/** Stream input preset. Only active on Android 28+
* TODO InputPreset::Unspecified should be considered as a possible default alternative.
*/
InputPreset mInputPreset = InputPreset::VoiceRecognition;
/** Stream session ID allocation strategy. Only active on Android 28+ */
SessionId mSessionId = SessionId::None;
/** Allowed Capture Policy. Only active on Android 29+ */
AllowedCapturePolicy mAllowedCapturePolicy = AllowedCapturePolicy::Unspecified;
/** Privacy Sensitive Mode. Only active on Android 30+ */
PrivacySensitiveMode mPrivacySensitiveMode = PrivacySensitiveMode::Unspecified;
/** Control the name of the package creating the stream. Only active on Android 31+ */
std::string mPackageName;
/** Control the attribution tag of the context creating the stream. Only active on Android 31+ */
std::string mAttributionTag;
/** Whether the content is already spatialized. Only used on Android 32+ */
bool mIsContentSpatialized = false;
/** Spatialization Behavior. Only active on Android 32+ */
SpatializationBehavior mSpatializationBehavior = SpatializationBehavior::Unspecified;
/** Hardware channel count. Only specified on Android 34+ AAudio streams */
int32_t mHardwareChannelCount = kUnspecified;
/** Hardware sample rate. Only specified on Android 34+ AAudio streams */
int32_t mHardwareSampleRate = kUnspecified;
/** Hardware format. Only specified on Android 34+ AAudio streams */
AudioFormat mHardwareFormat = AudioFormat::Unspecified;
// Control whether Oboe can convert channel counts to achieve optimal results.
bool mChannelConversionAllowed = false;
// Control whether Oboe can convert data formats to achieve optimal results.
bool mFormatConversionAllowed = false;
// Control whether and how Oboe can convert sample rates to achieve optimal results.
SampleRateConversionQuality mSampleRateConversionQuality = SampleRateConversionQuality::None;
/** Validate stream parameters that might not be checked in lower layers */
virtual Result isValidConfig() {
switch (mFormat) {
case AudioFormat::Unspecified:
case AudioFormat::I16:
case AudioFormat::Float:
case AudioFormat::I24:
case AudioFormat::I32:
case AudioFormat::IEC61937:
break;
default:
return Result::ErrorInvalidFormat;
}
switch (mSampleRateConversionQuality) {
case SampleRateConversionQuality::None:
case SampleRateConversionQuality::Fastest:
case SampleRateConversionQuality::Low:
case SampleRateConversionQuality::Medium:
case SampleRateConversionQuality::High:
case SampleRateConversionQuality::Best:
return Result::OK;
default:
return Result::ErrorIllegalArgument;
}
}
};
} // namespace oboe
#endif /* OBOE_STREAM_BASE_H_ */