1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef AUDIO_INFO_H
16 #define AUDIO_INFO_H
17
18 #ifdef __MUSL__
19 #include <stdint.h>
20 #endif // __MUSL__
21
22 #include <cmath>
23 #include <limits>
24 #include <string>
25 #include <vector>
26 #include <unistd.h>
27
28 namespace OHOS {
29 namespace AudioStandard {
30 constexpr int32_t MAX_NUM_STREAMS = 3;
31 constexpr int32_t RENDERER_STREAM_USAGE_SHIFT = 16;
32 constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5;
33 constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20;
34 constexpr int32_t MIN_SERVICE_COUNT = 2;
35
36
37 enum DeviceFlag {
38 /**
39 * Device flag none.
40 */
41 DEVICE_FLAG_NONE = -1,
42 /**
43 * Indicates all output audio devices.
44 */
45 OUTPUT_DEVICES_FLAG = 1,
46 /**
47 * Indicates all input audio devices.
48 */
49 INPUT_DEVICES_FLAG = 2,
50 /**
51 * Indicates all audio devices.
52 */
53 ALL_DEVICES_FLAG = 3,
54 /**
55 * Device flag max count.
56 */
57 DEVICE_FLAG_MAX
58 };
59
60 enum DeviceRole {
61 /**
62 * Device role none.
63 */
64 DEVICE_ROLE_NONE = -1,
65 /**
66 * Input device role.
67 */
68 INPUT_DEVICE = 1,
69 /**
70 * Output device role.
71 */
72 OUTPUT_DEVICE = 2,
73 /**
74 * Device role max count.
75 */
76 DEVICE_ROLE_MAX
77 };
78
79 enum DeviceType {
80 /**
81 * Indicates device type none.
82 */
83 DEVICE_TYPE_NONE = -1,
84 /**
85 * Indicates invalid device
86 */
87 DEVICE_TYPE_INVALID = 0,
88 /**
89 * Indicates a speaker built in a device.
90 */
91 DEVICE_TYPE_SPEAKER = 2,
92 /**
93 * Indicates a headset, which is the combination of a pair of headphones and a microphone.
94 */
95 DEVICE_TYPE_WIRED_HEADSET = 3,
96 /**
97 * Indicates a pair of wired headphones.
98 */
99 DEVICE_TYPE_WIRED_HEADPHONES = 4,
100 /**
101 * Indicates a Bluetooth device used for telephony.
102 */
103 DEVICE_TYPE_BLUETOOTH_SCO = 7,
104 /**
105 * Indicates a Bluetooth device supporting the Advanced Audio Distribution Profile (A2DP).
106 */
107 DEVICE_TYPE_BLUETOOTH_A2DP = 8,
108 /**
109 * Indicates a microphone built in a device.
110 */
111 DEVICE_TYPE_MIC = 15,
112 /**
113 * Indicates a microphone built in a device.
114 */
115 DEVICE_TYPE_USB_HEADSET = 22,
116 /**
117 * Indicates device type max count.
118 */
119 DEVICE_TYPE_MAX
120 };
121
122 enum ActiveDeviceType {
123 ACTIVE_DEVICE_TYPE_NONE = -1,
124 SPEAKER = 2,
125 BLUETOOTH_SCO = 7,
126 ACTIVE_DEVICE_TYPE_MAX
127 };
128
129 enum AudioStreamType {
130 /**
131 * Indicates audio streams default.
132 */
133 STREAM_DEFAULT = -1,
134 /**
135 * Indicates audio streams of voices in calls.
136 */
137 STREAM_VOICE_CALL = 0,
138 /**
139 * Indicates audio streams for music playback.
140 */
141 STREAM_MUSIC = 1,
142 /**
143 * Indicates audio streams for ringtones.
144 */
145 STREAM_RING = 2,
146 /**
147 * Indicates audio streams media.
148 */
149 STREAM_MEDIA = 3,
150 /**
151 * Indicates Audio streams for voice assistant
152 */
153 STREAM_VOICE_ASSISTANT = 4,
154 /**
155 * Indicates audio streams for system sounds.
156 */
157 STREAM_SYSTEM = 5,
158 /**
159 * Indicates audio streams for alarms.
160 */
161 STREAM_ALARM = 6,
162 /**
163 * Indicates audio streams for notifications.
164 */
165 STREAM_NOTIFICATION = 7,
166 /**
167 * Indicates audio streams for voice calls routed through a connected Bluetooth device.
168 */
169 STREAM_BLUETOOTH_SCO = 8,
170 /**
171 * Indicates audio streams for enforced audible.
172 */
173 STREAM_ENFORCED_AUDIBLE = 9,
174 /**
175 * Indicates audio streams for dual-tone multi-frequency (DTMF) tones.
176 */
177 STREAM_DTMF = 10,
178 /**
179 * Indicates audio streams exclusively transmitted through the speaker (text-to-speech) of a device.
180 */
181 STREAM_TTS = 11,
182 /**
183 * Indicates audio streams used for prompts in terms of accessibility.
184 */
185 STREAM_ACCESSIBILITY = 12
186 };
187
188 enum AudioEncodingType {
189 ENCODING_PCM = 0,
190 ENCODING_INVALID = -1
191 };
192
193 // Ringer Mode
194 enum AudioRingerMode {
195 RINGER_MODE_NORMAL = 0,
196 RINGER_MODE_SILENT = 1,
197 RINGER_MODE_VIBRATE = 2
198 };
199
200 // format
201 enum AudioSampleFormat {
202 SAMPLE_U8 = 8,
203 SAMPLE_S16LE = 16,
204 SAMPLE_S24LE = 24,
205 SAMPLE_S32LE = 32,
206 INVALID_WIDTH = -1
207 };
208
209 // channel
210 enum AudioChannel {
211 MONO = 1,
212 STEREO = 2,
213 CHANNEL_3 = 3,
214 };
215
216 // sampling rate
217 enum AudioSamplingRate {
218 SAMPLE_RATE_8000 = 8000,
219 SAMPLE_RATE_11025 = 11025,
220 SAMPLE_RATE_12000 = 12000,
221 SAMPLE_RATE_16000 = 16000,
222 SAMPLE_RATE_22050 = 22050,
223 SAMPLE_RATE_24000 = 24000,
224 SAMPLE_RATE_32000 = 32000,
225 SAMPLE_RATE_44100 = 44100,
226 SAMPLE_RATE_48000 = 48000,
227 SAMPLE_RATE_64000 = 64000,
228 SAMPLE_RATE_96000 = 96000
229 };
230
231 typedef enum {
232 /** Invalid audio source */
233 AUDIO_SOURCE_INVALID = -1,
234 /** Default audio source */
235 AUDIO_SOURCE_DEFAULT = 0,
236 /** Microphone */
237 AUDIO_MIC = 1,
238 /** Uplink voice */
239 AUDIO_VOICE_UPLINK = 2,
240 /** Downlink voice */
241 AUDIO_VOICE_DOWNLINK = 3,
242 /** Voice call */
243 AUDIO_VOICE_CALL = 4,
244 /** Camcorder */
245 AUDIO_CAMCORDER = 5,
246 /** Voice recognition */
247 AUDIO_VOICE_RECOGNITION = 6,
248 /** Voice communication */
249 AUDIO_VOICE_COMMUNICATION = 7,
250 /** Remote submix */
251 AUDIO_REMOTE_SUBMIX = 8,
252 /** Unprocessed audio */
253 AUDIO_UNPROCESSED = 9,
254 /** Voice performance */
255 AUDIO_VOICE_PERFORMANCE = 10,
256 /** Echo reference */
257 AUDIO_ECHO_REFERENCE = 1997,
258 /** Radio tuner */
259 AUDIO_RADIO_TUNER = 1998,
260 /** Hotword */
261 AUDIO_HOTWORD = 1999,
262 /** Extended remote submix */
263 AUDIO_REMOTE_SUBMIX_EXTEND = 10007,
264 } AudioSourceType;
265
266 struct AudioStreamParams {
267 uint32_t samplingRate;
268 uint8_t encoding;
269 uint8_t format;
270 uint8_t channels;
271 };
272
273 /**
274 * Enumerates the audio content type.
275 */
276 enum ContentType {
277 CONTENT_TYPE_UNKNOWN = 0,
278 CONTENT_TYPE_SPEECH = 1,
279 CONTENT_TYPE_MUSIC = 2,
280 CONTENT_TYPE_MOVIE = 3,
281 CONTENT_TYPE_SONIFICATION = 4,
282 CONTENT_TYPE_RINGTONE = 5
283 };
284
285 /**
286 * Enumerates the stream usage.
287 */
288 enum StreamUsage {
289 STREAM_USAGE_UNKNOWN = 0,
290 STREAM_USAGE_MEDIA = 1,
291 STREAM_USAGE_VOICE_COMMUNICATION = 2,
292 STREAM_USAGE_VOICE_ASSISTANT = 4,
293 STREAM_USAGE_NOTIFICATION_RINGTONE = 6
294 };
295
296 /**
297 * Enumerates the capturer source type
298 */
299 enum SourceType {
300 SOURCE_TYPE_INVALID = -1,
301 SOURCE_TYPE_MIC,
302 SOURCE_TYPE_VOICE_COMMUNICATION = 7
303 };
304
305 /**
306 * Enumerates the renderer playback speed.
307 */
308 enum AudioRendererRate {
309 RENDER_RATE_NORMAL = 0,
310 RENDER_RATE_DOUBLE = 1,
311 RENDER_RATE_HALF = 2,
312 };
313
314 enum InterruptType {
315 INTERRUPT_TYPE_BEGIN = 1,
316 INTERRUPT_TYPE_END = 2,
317 };
318
319 enum InterruptHint {
320 INTERRUPT_HINT_NONE = 0,
321 INTERRUPT_HINT_RESUME,
322 INTERRUPT_HINT_PAUSE,
323 INTERRUPT_HINT_STOP,
324 INTERRUPT_HINT_DUCK,
325 INTERRUPT_HINT_UNDUCK
326 };
327
328 enum InterruptForceType {
329 /**
330 * Force type, system change audio state.
331 */
332 INTERRUPT_FORCE = 0,
333 /**
334 * Share type, application change audio state.
335 */
336 INTERRUPT_SHARE
337 };
338
339 enum ActionTarget {
340 CURRENT = 0,
341 INCOMING,
342 BOTH
343 };
344
345 struct InterruptEvent {
346 /**
347 * Interrupt event type, begin or end
348 */
349 InterruptType eventType;
350 /**
351 * Interrupt force type, force or share
352 */
353 InterruptForceType forceType;
354 /**
355 * Interrupt hint type. In force type, the audio state already changed,
356 * but in share mode, only provide a hint for application to decide.
357 */
358 InterruptHint hintType;
359 };
360
361 // Used internally only by AudioFramework
362 struct InterruptEventInternal {
363 InterruptType eventType;
364 InterruptForceType forceType;
365 InterruptHint hintType;
366 float duckVolume;
367 };
368
369 struct AudioFocusEntry {
370 InterruptForceType forceType;
371 InterruptHint hintType;
372 ActionTarget actionOn;
373 bool isReject;
374 };
375
376 struct AudioInterrupt {
377 StreamUsage streamUsage;
378 ContentType contentType;
379 AudioStreamType streamType;
380 uint32_t sessionID;
381 bool pauseWhenDucked;
382 };
383
384 struct VolumeEvent {
385 AudioStreamType volumeType;
386 int32_t volume;
387 bool updateUi;
388 };
389
390 struct AudioParameters {
391 AudioSampleFormat format;
392 AudioChannel channels;
393 AudioSamplingRate samplingRate;
394 AudioEncodingType encoding;
395 ContentType contentType;
396 StreamUsage usage;
397 DeviceRole deviceRole;
398 DeviceType deviceType;
399 };
400
401 struct AudioStreamInfo {
402 AudioSamplingRate samplingRate;
403 AudioEncodingType encoding;
404 AudioSampleFormat format;
405 AudioChannel channels;
406 };
407
408 struct AudioRendererInfo {
409 ContentType contentType = CONTENT_TYPE_UNKNOWN;
410 StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
411 int32_t rendererFlags = 0;
412 };
413
414 struct AudioCapturerInfo {
415 SourceType sourceType = SOURCE_TYPE_INVALID;
416 int32_t capturerFlags = 0;
417 };
418
419 struct AudioRendererDesc {
420 ContentType contentType = CONTENT_TYPE_UNKNOWN;
421 StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
422 };
423
424 struct AudioRendererOptions {
425 AudioStreamInfo streamInfo;
426 AudioRendererInfo rendererInfo;
427 };
428
429 enum DeviceChangeType {
430 CONNECT = 0,
431 DISCONNECT = 1,
432 };
433
434 enum AudioScene {
435 /**
436 * Default audio scene
437 */
438 AUDIO_SCENE_DEFAULT,
439 /**
440 * Ringing audio scene
441 * Only available for system api.
442 */
443 AUDIO_SCENE_RINGING,
444 /**
445 * Phone call audio scene
446 * Only available for system api.
447 */
448 AUDIO_SCENE_PHONE_CALL,
449 /**
450 * Voice chat audio scene
451 */
452 AUDIO_SCENE_PHONE_CHAT,
453 };
454
455 struct AudioCapturerOptions {
456 AudioStreamInfo streamInfo;
457 AudioCapturerInfo capturerInfo;
458 };
459
460 // Supported audio parameters for both renderer and capturer
461 const std::vector<AudioSampleFormat> AUDIO_SUPPORTED_FORMATS {
462 SAMPLE_U8,
463 SAMPLE_S16LE,
464 SAMPLE_S24LE,
465 SAMPLE_S32LE
466 };
467
468 const std::vector<AudioChannel> AUDIO_SUPPORTED_CHANNELS {
469 MONO,
470 STEREO
471 };
472
473 const std::vector<AudioEncodingType> AUDIO_SUPPORTED_ENCODING_TYPES {
474 ENCODING_PCM
475 };
476
477 const std::vector<AudioSamplingRate> AUDIO_SUPPORTED_SAMPLING_RATES {
478 SAMPLE_RATE_8000,
479 SAMPLE_RATE_11025,
480 SAMPLE_RATE_12000,
481 SAMPLE_RATE_16000,
482 SAMPLE_RATE_22050,
483 SAMPLE_RATE_24000,
484 SAMPLE_RATE_32000,
485 SAMPLE_RATE_44100,
486 SAMPLE_RATE_48000,
487 SAMPLE_RATE_64000,
488 SAMPLE_RATE_96000
489 };
490
491 struct BufferDesc {
492 uint8_t* buffer;
493 size_t bufLength;
494 size_t dataLength;
495 };
496
497 struct BufferQueueState {
498 uint32_t numBuffers;
499 uint32_t currentIndex;
500 };
501
502 enum AudioRenderMode {
503 RENDER_MODE_NORMAL,
504 RENDER_MODE_CALLBACK
505 };
506
507 typedef uint32_t AudioIOHandle;
508
FLOAT_COMPARE_EQ(const float & x,const float & y)509 static inline bool FLOAT_COMPARE_EQ(const float& x, const float& y)
510 {
511 return (std::abs((x) - (y)) <= (std::numeric_limits<float>::epsilon()));
512 }
513
514 // Below APIs are added to handle compilation error in call manager
515 // Once call manager adapt to new interrupt APIs, this will be removed
516 enum InterruptActionType {
517 TYPE_ACTIVATED = 0,
518 TYPE_INTERRUPT = 1
519 };
520
521 struct InterruptAction {
522 InterruptActionType actionType;
523 InterruptType interruptType;
524 InterruptHint interruptHint;
525 bool activated;
526 };
527
528 enum AudioServiceIndex {
529 HDI_SERVICE_INDEX = 0,
530 AUDIO_SERVICE_INDEX
531 };
532 } // namespace AudioStandard
533 } // namespace OHOS
534 #endif // AUDIO_INFO_H
535