1 /*
2 * Copyright (C) 2011 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
18 #ifndef ANDROID_AUDIO_CORE_H
19 #define ANDROID_AUDIO_CORE_H
20
21 #include <float.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/cdefs.h>
27 #include <sys/types.h>
28
29 #include "audio-base-utils.h"
30 #include "audio-base.h"
31 #include "audio-hal-enums.h"
32 #include "audio_common-base.h"
33
34 /*
35 * Annotation to tell clang that we intend to fall through from one case to
36 * another in a switch. Sourced from android-base/macros.h.
37 */
38 #ifndef FALLTHROUGH_INTENDED
39 #ifdef __cplusplus
40 #define FALLTHROUGH_INTENDED [[fallthrough]]
41 #elif __has_attribute(fallthrough)
42 #define FALLTHROUGH_INTENDED __attribute__((__fallthrough__))
43 #else
44 #define FALLTHROUGH_INTENDED
45 #endif // __cplusplus
46 #endif // FALLTHROUGH_INTENDED
47
48 #ifdef __cplusplus
49 #define CONSTEXPR constexpr
50 #else
51 #define CONSTEXPR
52 #endif
53
54 __BEGIN_DECLS
55
56 /* The enums were moved here mostly from
57 * frameworks/base/include/media/AudioSystem.h
58 */
59
60 /* represents an invalid uid for tracks; the calling or client uid is often substituted. */
61 #define AUDIO_UID_INVALID ((uid_t)-1)
62
63 /* device address used to refer to the standard remote submix */
64 #define AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS "0"
65
66 /* AudioFlinger and AudioPolicy services use I/O handles to identify audio sources and sinks */
67 typedef int audio_io_handle_t;
68
69 /* Null values for handles. */
70 enum {
71 AUDIO_IO_HANDLE_NONE = 0,
72 AUDIO_MODULE_HANDLE_NONE = 0,
73 AUDIO_PORT_HANDLE_NONE = 0,
74 AUDIO_PATCH_HANDLE_NONE = 0,
75 };
76
77 typedef enum {
78 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
79 AUDIO_MODE_INVALID = -2, // (-2)
80 AUDIO_MODE_CURRENT = -1, // (-1)
81 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
82 AUDIO_MODE_NORMAL = HAL_AUDIO_MODE_NORMAL,
83 AUDIO_MODE_RINGTONE = HAL_AUDIO_MODE_RINGTONE,
84 AUDIO_MODE_IN_CALL = HAL_AUDIO_MODE_IN_CALL,
85 AUDIO_MODE_IN_COMMUNICATION = HAL_AUDIO_MODE_IN_COMMUNICATION,
86 AUDIO_MODE_CALL_SCREEN = HAL_AUDIO_MODE_CALL_SCREEN,
87 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
88 AUDIO_MODE_CALL_REDIRECT = 5,
89 AUDIO_MODE_COMMUNICATION_REDIRECT = 6,
90 AUDIO_MODE_MAX = AUDIO_MODE_COMMUNICATION_REDIRECT,
91 AUDIO_MODE_CNT = AUDIO_MODE_MAX + 1,
92 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
93 } audio_mode_t;
94
95 /* Do not change these values without updating their counterparts
96 * in frameworks/base/media/java/android/media/AudioAttributes.java
97 */
98 typedef enum {
99 AUDIO_FLAG_NONE = 0x0,
100 AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1,
101 AUDIO_FLAG_SECURE = 0x2,
102 AUDIO_FLAG_SCO = 0x4,
103 AUDIO_FLAG_BEACON = 0x8,
104 AUDIO_FLAG_HW_AV_SYNC = 0x10,
105 AUDIO_FLAG_HW_HOTWORD = 0x20,
106 AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY = 0x40,
107 AUDIO_FLAG_BYPASS_MUTE = 0x80,
108 AUDIO_FLAG_LOW_LATENCY = 0x100,
109 AUDIO_FLAG_DEEP_BUFFER = 0x200,
110 AUDIO_FLAG_NO_MEDIA_PROJECTION = 0X400,
111 AUDIO_FLAG_MUTE_HAPTIC = 0x800,
112 AUDIO_FLAG_NO_SYSTEM_CAPTURE = 0X1000,
113 AUDIO_FLAG_CAPTURE_PRIVATE = 0X2000,
114 AUDIO_FLAG_CONTENT_SPATIALIZED = 0X4000,
115 AUDIO_FLAG_NEVER_SPATIALIZE = 0X8000,
116 AUDIO_FLAG_CALL_REDIRECTION = 0X10000,
117 } audio_flags_mask_t;
118
119 /* Audio attributes */
120 #define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
121 typedef struct {
122 audio_content_type_t content_type;
123 audio_usage_t usage;
124 audio_source_t source;
125 audio_flags_mask_t flags;
126 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
127 } __attribute__((packed)) audio_attributes_t; // sent through Binder;
128 /** The separator for tags. */
129 static const char AUDIO_ATTRIBUTES_TAGS_SEPARATOR = ';';
130
131 static const audio_attributes_t AUDIO_ATTRIBUTES_INITIALIZER = {
132 /* .content_type = */ AUDIO_CONTENT_TYPE_UNKNOWN,
133 /* .usage = */ AUDIO_USAGE_UNKNOWN,
134 /* .source = */ AUDIO_SOURCE_DEFAULT,
135 /* .flags = */ AUDIO_FLAG_NONE,
136 /* .tags = */ ""
137 };
138
attributes_initializer(audio_usage_t usage)139 static inline audio_attributes_t attributes_initializer(audio_usage_t usage)
140 {
141 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
142 attributes.usage = usage;
143 return attributes;
144 }
145
attributes_initializer_flags(audio_flags_mask_t flags)146 static inline audio_attributes_t attributes_initializer_flags(audio_flags_mask_t flags)
147 {
148 audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
149 attributes.flags = flags;
150 return attributes;
151 }
152
audio_flags_to_audio_output_flags(const audio_flags_mask_t audio_flags,audio_output_flags_t * flags)153 static inline void audio_flags_to_audio_output_flags(
154 const audio_flags_mask_t audio_flags,
155 audio_output_flags_t *flags)
156 {
157 if ((audio_flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
158 *flags = (audio_output_flags_t)(*flags |
159 AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_DIRECT);
160 }
161 if ((audio_flags & AUDIO_FLAG_LOW_LATENCY) != 0) {
162 *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_FAST);
163 }
164 // check deep buffer after flags have been modified above
165 if (*flags == AUDIO_OUTPUT_FLAG_NONE && (audio_flags & AUDIO_FLAG_DEEP_BUFFER) != 0) {
166 *flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
167 }
168 }
169
170
171 /* A unique ID allocated by AudioFlinger for use as an audio_io_handle_t, audio_session_t,
172 * audio_effect_handle_t, audio_module_handle_t, and audio_patch_handle_t.
173 * Audio port IDs (audio_port_handle_t) are allocated by AudioPolicy
174 * in a different namespace than AudioFlinger unique IDs.
175 */
176 typedef int audio_unique_id_t;
177
178 /* A unique ID with use AUDIO_UNIQUE_ID_USE_EFFECT */
179 typedef int audio_effect_handle_t;
180
181 /* Possible uses for an audio_unique_id_t */
182 typedef enum {
183 AUDIO_UNIQUE_ID_USE_UNSPECIFIED = 0,
184 AUDIO_UNIQUE_ID_USE_SESSION = 1, // audio_session_t
185 // for allocated sessions, not special AUDIO_SESSION_*
186 AUDIO_UNIQUE_ID_USE_MODULE = 2, // audio_module_handle_t
187 AUDIO_UNIQUE_ID_USE_EFFECT = 3, // audio_effect_handle_t
188 AUDIO_UNIQUE_ID_USE_PATCH = 4, // audio_patch_handle_t
189 AUDIO_UNIQUE_ID_USE_OUTPUT = 5, // audio_io_handle_t
190 AUDIO_UNIQUE_ID_USE_INPUT = 6, // audio_io_handle_t
191 AUDIO_UNIQUE_ID_USE_CLIENT = 7, // client-side players and recorders
192 // FIXME should move to a separate namespace;
193 // these IDs are allocated by AudioFlinger on client request,
194 // but are never used by AudioFlinger
195 AUDIO_UNIQUE_ID_USE_MAX = 8, // must be a power-of-two
196 AUDIO_UNIQUE_ID_USE_MASK = AUDIO_UNIQUE_ID_USE_MAX - 1
197 } audio_unique_id_use_t;
198
199 /* Return the use of an audio_unique_id_t */
audio_unique_id_get_use(audio_unique_id_t id)200 static inline audio_unique_id_use_t audio_unique_id_get_use(audio_unique_id_t id)
201 {
202 return (audio_unique_id_use_t) (id & AUDIO_UNIQUE_ID_USE_MASK);
203 }
204
205 typedef enum : int32_t {
206 AUDIO_SESSION_DEVICE = HAL_AUDIO_SESSION_DEVICE,
207 AUDIO_SESSION_OUTPUT_STAGE = HAL_AUDIO_SESSION_OUTPUT_STAGE,
208 AUDIO_SESSION_OUTPUT_MIX = HAL_AUDIO_SESSION_OUTPUT_MIX,
209 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
210 AUDIO_SESSION_ALLOCATE = 0,
211 AUDIO_SESSION_NONE = 0,
212 #endif
213 } audio_session_t;
214
215 /* Reserved audio_unique_id_t values. FIXME: not a complete list. */
216 #define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
217
218 /* returns true if the audio session ID corresponds to a global
219 * effect sessions (e.g. OUTPUT_MIX, OUTPUT_STAGE, or DEVICE).
220 */
audio_is_global_session(audio_session_t session)221 static inline bool audio_is_global_session(audio_session_t session) {
222 return session <= AUDIO_SESSION_OUTPUT_MIX;
223 }
224
225 /* These constants are used instead of "magic numbers" for
226 * channel counts.
227 */
228 enum {
229 FCC_1 = 1,
230 FCC_2 = 2,
231 FCC_8 = 8,
232 FCC_12 = 12,
233 FCC_24 = 24,
234 FCC_26 = 26,
235 // FCC_LIMIT is the maximum PCM channel count supported through
236 // the mixing pipeline to the audio HAL.
237 //
238 // This can be adjusted onto a value such as FCC_12 or FCC_26
239 // if the device HAL can support it. Do not reduce below FCC_8.
240 FCC_LIMIT = FCC_12,
241 };
242
243 /* A channel mask per se only defines the presence or absence of a channel, not the order.
244 * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
245 *
246 * audio_channel_mask_t is an opaque type and its internal layout should not
247 * be assumed as it may change in the future.
248 * Instead, always use the functions declared in this header to examine.
249 *
250 * These are the current representations:
251 *
252 * AUDIO_CHANNEL_REPRESENTATION_POSITION
253 * is a channel mask representation for position assignment.
254 * Each low-order bit corresponds to the spatial position of a transducer (output),
255 * or interpretation of channel (input).
256 * The user of a channel mask needs to know the context of whether it is for output or input.
257 * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
258 * It is not permitted for no bits to be set.
259 *
260 * AUDIO_CHANNEL_REPRESENTATION_INDEX
261 * is a channel mask representation for index assignment.
262 * Each low-order bit corresponds to a selected channel.
263 * There is no platform interpretation of the various bits.
264 * There is no concept of output or input.
265 * It is not permitted for no bits to be set.
266 *
267 * All other representations are reserved for future use.
268 *
269 * Warning: current representation distinguishes between input and output, but this will not the be
270 * case in future revisions of the platform. Wherever there is an ambiguity between input and output
271 * that is currently resolved by checking the channel mask, the implementer should look for ways to
272 * fix it with additional information outside of the mask.
273 */
274
275 /* log(2) of maximum number of representations, not part of public API */
276 #define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
277
278 /* The return value is undefined if the channel mask is invalid. */
audio_channel_mask_get_bits(audio_channel_mask_t channel)279 static inline CONSTEXPR uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
280 {
281 return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
282 }
283
284 typedef enum {
285 AUDIO_CHANNEL_REPRESENTATION_POSITION = 0x0u,
286 AUDIO_CHANNEL_REPRESENTATION_INDEX = 0x2u,
287 } audio_channel_representation_t;
288
289 /* The return value is undefined if the channel mask is invalid. */
audio_channel_mask_get_representation(audio_channel_mask_t channel)290 static inline CONSTEXPR audio_channel_representation_t audio_channel_mask_get_representation(
291 audio_channel_mask_t channel)
292 {
293 // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
294 return (audio_channel_representation_t)
295 ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
296 }
297
298 #ifdef __cplusplus
299 // Some effects use `int32_t` directly for channel mask.
audio_channel_mask_get_representation(int32_t mask)300 static inline constexpr uint32_t audio_channel_mask_get_representation(int32_t mask) {
301 return audio_channel_mask_get_representation(static_cast<audio_channel_mask_t>(mask));
302 }
303 #endif
304
305 /* Returns true if the channel mask is valid,
306 * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
307 * This function is unable to determine whether a channel mask for position assignment
308 * is invalid because an output mask has an invalid output bit set,
309 * or because an input mask has an invalid input bit set.
310 * All other APIs that take a channel mask assume that it is valid.
311 */
audio_channel_mask_is_valid(audio_channel_mask_t channel)312 static inline CONSTEXPR bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
313 {
314 uint32_t bits = audio_channel_mask_get_bits(channel);
315 audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
316 switch (representation) {
317 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
318 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
319 break;
320 default:
321 bits = 0;
322 break;
323 }
324 return bits != 0;
325 }
326
327 /* Not part of public API */
audio_channel_mask_from_representation_and_bits(audio_channel_representation_t representation,uint32_t bits)328 static inline CONSTEXPR audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
329 audio_channel_representation_t representation, uint32_t bits)
330 {
331 return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
332 }
333
334 /*
335 * Returns true so long as Quadraphonic channels (FL, FR, BL, BR)
336 * or (FL, FR, SL, SR) are completely specified
337 * in the channel mask. We expect these 4 channels to be the minimum for
338 * reasonable spatializer effect quality.
339 *
340 * Note, this covers:
341 * AUDIO_CHANNEL_OUT_5POINT1
342 * AUDIO_CHANNEL_OUT_5POINT1POINT4
343 * AUDIO_CHANNEL_OUT_7POINT1
344 * AUDIO_CHANNEL_OUT_7POINT1POINT2
345 * AUDIO_CHANNEL_OUT_7POINT1POINT4
346 * AUDIO_CHANNEL_OUT_9POINT1POINT4
347 * AUDIO_CHANNEL_OUT_9POINT1POINT6
348 * AUDIO_CHANNEL_OUT_13POINT_360RA
349 * AUDIO_CHANNEL_OUT_22POINT2
350 */
audio_is_channel_mask_spatialized(audio_channel_mask_t channelMask)351 static inline CONSTEXPR bool audio_is_channel_mask_spatialized(audio_channel_mask_t channelMask) {
352 return audio_channel_mask_get_representation(channelMask)
353 == AUDIO_CHANNEL_REPRESENTATION_POSITION
354 && ((channelMask & AUDIO_CHANNEL_OUT_QUAD) == AUDIO_CHANNEL_OUT_QUAD
355 || (channelMask & AUDIO_CHANNEL_OUT_QUAD_SIDE) == AUDIO_CHANNEL_OUT_QUAD_SIDE);
356 }
357
358 /*
359 * MediaFormat channel masks follow the Java channel mask spec
360 * but might be specified as a native channel mask. This method
361 * does a "smart" correction to ensure a native channel mask.
362 */
363 static inline audio_channel_mask_t
audio_channel_mask_from_media_format_mask(int32_t channelMaskFromFormat)364 audio_channel_mask_from_media_format_mask(int32_t channelMaskFromFormat) {
365 // KEY_CHANNEL_MASK follows the android.media.AudioFormat java mask
366 // which is left-bitshifted by 2 relative to the native mask
367 if ((channelMaskFromFormat & 0b11) != 0) {
368 // received an unexpected mask (supposed to follow AudioFormat constants
369 // for output masks with the 2 least-significant bits at 0), but
370 // it may come from an extractor that uses native masks: keeping
371 // the mask as given is ok as it contains at least mono or stereo
372 // and potentially the haptic channels
373 return (audio_channel_mask_t)channelMaskFromFormat;
374 } else {
375 // We exclude bits from the lowest haptic bit all the way to the top of int.
376 // to avoid aliasing. The remainder bits are position bits
377 // which must be shifted by 2 from Java to get native.
378 //
379 // Using the lowest set bit exclusion AND mask (x - 1), we find
380 // all the bits from lowest set bit to the top is m = x | ~(x - 1).
381 // Using the one's complement to two's complement formula ~x = -x - 1,
382 // we can reduce this to m = x | -x.
383 // (Note -x is also the lowest bit extraction AND mask; i.e. lowest_bit = x & -x).
384 const int32_t EXCLUDE_BITS = AUDIO_CHANNEL_HAPTIC_ALL | -AUDIO_CHANNEL_HAPTIC_ALL;
385 const int32_t positionBits = (channelMaskFromFormat & ~EXCLUDE_BITS) >> 2;
386
387 // Haptic bits are identical between Java and native.
388 const int32_t hapticBits = channelMaskFromFormat & AUDIO_CHANNEL_HAPTIC_ALL;
389 return (audio_channel_mask_t)(positionBits | hapticBits);
390 }
391 }
392
393 /**
394 * Expresses the convention when stereo audio samples are stored interleaved
395 * in an array. This should improve readability by allowing code to use
396 * symbolic indices instead of hard-coded [0] and [1].
397 *
398 * For multi-channel beyond stereo, the platform convention is that channels
399 * are interleaved in order from least significant channel mask bit to most
400 * significant channel mask bit, with unused bits skipped. Any exceptions
401 * to this convention will be noted at the appropriate API.
402 */
403 enum {
404 AUDIO_INTERLEAVE_LEFT = 0,
405 AUDIO_INTERLEAVE_RIGHT = 1,
406 };
407
408 /* This enum is deprecated */
409 typedef enum {
410 AUDIO_IN_ACOUSTICS_NONE = 0,
411 AUDIO_IN_ACOUSTICS_AGC_ENABLE = 0x0001,
412 AUDIO_IN_ACOUSTICS_AGC_DISABLE = 0,
413 AUDIO_IN_ACOUSTICS_NS_ENABLE = 0x0002,
414 AUDIO_IN_ACOUSTICS_NS_DISABLE = 0,
415 AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004,
416 AUDIO_IN_ACOUSTICS_TX_DISABLE = 0,
417 } audio_in_acoustics_t;
418
419 /* Additional information about compressed streams offloaded to
420 * hardware playback
421 * The version and size fields must be initialized by the caller by using
422 * one of the constants defined here.
423 * Must be aligned to transmit as raw memory through Binder.
424 */
425 typedef struct {
426 uint16_t version; // version of the info structure
427 uint16_t size; // total size of the structure including version and size
428 uint32_t sample_rate; // sample rate in Hz
429 audio_channel_mask_t channel_mask; // channel mask
430 audio_format_t format; // audio format
431 audio_stream_type_t stream_type; // stream type
432 uint32_t bit_rate; // bit rate in bits per second
433 int64_t duration_us; // duration in microseconds, -1 if unknown
434 bool has_video; // true if stream is tied to a video stream
435 bool is_streaming; // true if streaming, false if local playback
436 uint32_t bit_width;
437 uint32_t offload_buffer_size; // offload fragment size
438 audio_usage_t usage;
439 audio_encapsulation_mode_t encapsulation_mode; // version 0.2:
440 int32_t content_id; // version 0.2: content id from tuner hal (0 if none)
441 int32_t sync_id; // version 0.2: sync id from tuner hal (0 if none)
442 } __attribute__((aligned(8))) audio_offload_info_t;
443
444 #define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \
445 ((((maj) & 0xff) << 8) | ((min) & 0xff))
446
447 #define AUDIO_OFFLOAD_INFO_VERSION_0_2 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 2)
448 #define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_2
449
450 static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
451 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
452 /* .size = */ sizeof(audio_offload_info_t),
453 /* .sample_rate = */ 0,
454 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
455 /* .format = */ AUDIO_FORMAT_DEFAULT,
456 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
457 /* .bit_rate = */ 0,
458 /* .duration_us = */ 0,
459 /* .has_video = */ false,
460 /* .is_streaming = */ false,
461 /* .bit_width = */ 16,
462 /* .offload_buffer_size = */ 0,
463 /* .usage = */ AUDIO_USAGE_UNKNOWN,
464 /* .encapsulation_mode = */ AUDIO_ENCAPSULATION_MODE_NONE,
465 /* .content_id = */ 0,
466 /* .sync_id = */ 0,
467 };
468
469 /* common audio stream configuration parameters
470 * You should memset() the entire structure to zero before use to
471 * ensure forward compatibility
472 * Must be aligned to transmit as raw memory through Binder.
473 */
474 struct __attribute__((aligned(8))) audio_config {
475 uint32_t sample_rate;
476 audio_channel_mask_t channel_mask;
477 audio_format_t format;
478 audio_offload_info_t offload_info;
479 uint32_t frame_count;
480 };
481 typedef struct audio_config audio_config_t;
482
483 static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
484 /* .sample_rate = */ 0,
485 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
486 /* .format = */ AUDIO_FORMAT_DEFAULT,
487 /* .offload_info = */ {
488 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
489 /* .size = */ sizeof(audio_offload_info_t),
490 /* .sample_rate = */ 0,
491 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
492 /* .format = */ AUDIO_FORMAT_DEFAULT,
493 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
494 /* .bit_rate = */ 0,
495 /* .duration_us = */ 0,
496 /* .has_video = */ false,
497 /* .is_streaming = */ false,
498 /* .bit_width = */ 16,
499 /* .offload_buffer_size = */ 0,
500 /* .usage = */ AUDIO_USAGE_UNKNOWN,
501 /* .encapsulation_mode = */ AUDIO_ENCAPSULATION_MODE_NONE,
502 /* .content_id = */ 0,
503 /* .sync_id = */ 0,
504 },
505 /* .frame_count = */ 0,
506 };
507
508 struct audio_config_base {
509 uint32_t sample_rate;
510 audio_channel_mask_t channel_mask;
511 audio_format_t format;
512 };
513
514 typedef struct audio_config_base audio_config_base_t;
515
516 static const audio_config_base_t AUDIO_CONFIG_BASE_INITIALIZER = {
517 /* .sample_rate = */ 0,
518 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
519 /* .format = */ AUDIO_FORMAT_DEFAULT
520 };
521
522
audio_config_initializer(const audio_config_base_t * base)523 static inline audio_config_t audio_config_initializer(const audio_config_base_t *base)
524 {
525 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
526 config.sample_rate = base->sample_rate;
527 config.channel_mask = base->channel_mask;
528 config.format = base->format;
529 return config;
530 }
531
532 /* audio hw module handle functions or structures referencing a module */
533 typedef int audio_module_handle_t;
534
535 /******************************
536 * Volume control
537 *****************************/
538
539 /** 3 dB headroom are allowed on float samples (3db = 10^(3/20) = 1.412538).
540 * See: https://developer.android.com/reference/android/media/AudioTrack.html#write(float[], int, int, int)
541 */
542 #define FLOAT_NOMINAL_RANGE_HEADROOM 1.412538
543
544 /* If the audio hardware supports gain control on some audio paths,
545 * the platform can expose them in the audio_policy_configuration.xml file. The audio HAL
546 * will then implement gain control functions that will use the following data
547 * structures. */
548
549 /* An audio_gain struct is a representation of a gain stage.
550 * A gain stage is always attached to an audio port. */
551 struct audio_gain {
552 audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */
553 audio_channel_mask_t channel_mask; /* channels which gain an be controlled.
554 N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
555 int min_value; /* minimum gain value in millibels */
556 int max_value; /* maximum gain value in millibels */
557 int default_value; /* default gain value in millibels */
558 unsigned int step_value; /* gain step in millibels */
559 unsigned int min_ramp_ms; /* minimum ramp duration in ms */
560 unsigned int max_ramp_ms; /* maximum ramp duration in ms */
561 };
562
563 /* The gain configuration structure is used to get or set the gain values of a
564 * given port */
565 struct audio_gain_config {
566 int index; /* index of the corresponding audio_gain in the
567 audio_port gains[] table */
568 audio_gain_mode_t mode; /* mode requested for this command */
569 audio_channel_mask_t channel_mask; /* channels which gain value follows.
570 N/A in joint mode */
571
572 // note this "8" is not FCC_8, so it won't need to be changed for > 8 channels
573 int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
574 for each channel ordered from LSb to MSb in
575 channel mask. The number of values is 1 in joint
576 mode or __builtin_popcount(channel_mask) */
577 unsigned int ramp_duration_ms; /* ramp duration in ms */
578 };
579
580 /******************************
581 * Routing control
582 *****************************/
583
584 /* Types defined here are used to describe an audio source or sink at internal
585 * framework interfaces (audio policy, patch panel) or at the audio HAL.
586 * Sink and sources are grouped in a concept of “audio port” representing an
587 * audio end point at the edge of the system managed by the module exposing
588 * the interface. */
589
590 /* Each port has a unique ID or handle allocated by policy manager */
591 typedef int audio_port_handle_t;
592
593 /* the maximum length for the human-readable device name */
594 #define AUDIO_PORT_MAX_NAME_LEN 128
595
596 /* a union to store port configuration flags. Declared as a type so can be reused
597 in framework code */
598 union audio_io_flags {
599 audio_input_flags_t input;
600 audio_output_flags_t output;
601 };
602
603 /* maximum audio device address length */
604 #define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
605
606 /* extension for audio port configuration structure when the audio port is a
607 * hardware device */
608 struct audio_port_config_device_ext {
609 audio_module_handle_t hw_module; /* module the device is attached to */
610 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
611 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
612 };
613
614 /* extension for audio port configuration structure when the audio port is a
615 * sub mix */
616 struct audio_port_config_mix_ext {
617 audio_module_handle_t hw_module; /* module the stream is attached to */
618 audio_io_handle_t handle; /* I/O handle of the input/output stream */
619 union {
620 //TODO: change use case for output streams: use strategy and mixer attributes
621 audio_stream_type_t stream;
622 audio_source_t source;
623 } usecase;
624 };
625
626 /* extension for audio port configuration structure when the audio port is an
627 * audio session */
628 struct audio_port_config_session_ext {
629 audio_session_t session; /* audio session */
630 };
631
632 typedef enum {
633 AUDIO_PORT_ROLE_NONE = 0,
634 AUDIO_PORT_ROLE_SOURCE = 1,
635 AUDIO_PORT_ROLE_SINK = 2,
636 } audio_port_role_t;
637
638 typedef enum {
639 AUDIO_PORT_TYPE_NONE = 0,
640 AUDIO_PORT_TYPE_DEVICE = 1,
641 AUDIO_PORT_TYPE_MIX = 2,
642 AUDIO_PORT_TYPE_SESSION = 3,
643 } audio_port_type_t;
644
645 enum {
646 AUDIO_PORT_CONFIG_SAMPLE_RATE = 0x1u,
647 AUDIO_PORT_CONFIG_CHANNEL_MASK = 0x2u,
648 AUDIO_PORT_CONFIG_FORMAT = 0x4u,
649 AUDIO_PORT_CONFIG_GAIN = 0x8u,
650 AUDIO_PORT_CONFIG_FLAGS = 0x10u,
651 AUDIO_PORT_CONFIG_ALL = AUDIO_PORT_CONFIG_SAMPLE_RATE |
652 AUDIO_PORT_CONFIG_CHANNEL_MASK |
653 AUDIO_PORT_CONFIG_FORMAT |
654 AUDIO_PORT_CONFIG_GAIN |
655 AUDIO_PORT_CONFIG_FLAGS
656 };
657
658 typedef enum {
659 AUDIO_LATENCY_LOW = 0,
660 AUDIO_LATENCY_NORMAL = 1,
661 } audio_mix_latency_class_t;
662
663 /* audio port configuration structure used to specify a particular configuration of
664 * an audio port */
665 struct audio_port_config {
666 audio_port_handle_t id; /* port unique ID */
667 audio_port_role_t role; /* sink or source */
668 audio_port_type_t type; /* device, mix ... */
669 unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */
670 unsigned int sample_rate; /* sampling rate in Hz */
671 audio_channel_mask_t channel_mask; /* channel mask if applicable */
672 audio_format_t format; /* format if applicable */
673 struct audio_gain_config gain; /* gain to apply if applicable */
674 union audio_io_flags flags; /* HW_AV_SYNC, DIRECT, ... */
675 union {
676 struct audio_port_config_device_ext device; /* device specific info */
677 struct audio_port_config_mix_ext mix; /* mix specific info */
678 struct audio_port_config_session_ext session; /* session specific info */
679 } ext;
680 };
681
682
683 /* max number of sampling rates in audio port */
684 #define AUDIO_PORT_MAX_SAMPLING_RATES 32
685 /* max number of channel masks in audio port */
686 #define AUDIO_PORT_MAX_CHANNEL_MASKS 32
687 /* max number of audio formats in audio port */
688 #define AUDIO_PORT_MAX_FORMATS 32
689 /* max number of audio profiles in audio port. The audio profiles are used in
690 * `struct audio_port_v7`. When converting between `struct audio_port` and
691 * `struct audio_port_v7`, the number of audio profiles in `struct audio_port_v7`
692 * must be the same as the number of formats in `struct audio_port`. Therefore,
693 * the maximum number of audio profiles must be the same as the maximum number
694 * of formats. */
695 #define AUDIO_PORT_MAX_AUDIO_PROFILES AUDIO_PORT_MAX_FORMATS
696 /* max number of extra audio descriptors in audio port. */
697 #define AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS AUDIO_PORT_MAX_FORMATS
698 /* max number of gain controls in audio port */
699 #define AUDIO_PORT_MAX_GAINS 16
700 /* max bytes of extra audio descriptor */
701 #define EXTRA_AUDIO_DESCRIPTOR_SIZE 32
702
703 /* extension for audio port structure when the audio port is a hardware device */
704 struct audio_port_device_ext {
705 audio_module_handle_t hw_module; /* module the device is attached to */
706 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
707 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
708 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
709 uint32_t encapsulation_modes;
710 uint32_t encapsulation_metadata_types;
711 #endif
712 };
713
714 /* extension for audio port structure when the audio port is a sub mix */
715 struct audio_port_mix_ext {
716 audio_module_handle_t hw_module; /* module the stream is attached to */
717 audio_io_handle_t handle; /* I/O handle of the input.output stream */
718 audio_mix_latency_class_t latency_class; /* latency class */
719 // other attributes: routing strategies
720 };
721
722 /* extension for audio port structure when the audio port is an audio session */
723 struct audio_port_session_ext {
724 audio_session_t session; /* audio session */
725 };
726
727 struct audio_port {
728 audio_port_handle_t id; /* port unique ID */
729 audio_port_role_t role; /* sink or source */
730 audio_port_type_t type; /* device, mix ... */
731 char name[AUDIO_PORT_MAX_NAME_LEN];
732 unsigned int num_sample_rates; /* number of sampling rates in following array */
733 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
734 unsigned int num_channel_masks; /* number of channel masks in following array */
735 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
736 unsigned int num_formats; /* number of formats in following array */
737 audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
738 unsigned int num_gains; /* number of gains in following array */
739 struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
740 struct audio_port_config active_config; /* current audio port configuration */
741 union {
742 struct audio_port_device_ext device;
743 struct audio_port_mix_ext mix;
744 struct audio_port_session_ext session;
745 } ext;
746 };
747
748 typedef enum : int32_t {
749 AUDIO_STANDARD_NONE = 0,
750 AUDIO_STANDARD_EDID = 1,
751 AUDIO_STANDARD_SADB = 2,
752 AUDIO_STANDARD_VSADB = 3,
753 } audio_standard_t;
754
755 /**
756 * Configuration described by hardware descriptor for a format that is unrecognized
757 * by the platform.
758 */
759 struct audio_extra_audio_descriptor {
760 audio_standard_t standard;
761 unsigned int descriptor_length;
762 uint8_t descriptor[EXTRA_AUDIO_DESCRIPTOR_SIZE];
763 audio_encapsulation_type_t encapsulation_type;
764 };
765
766 /* configurations supported for a certain format */
767 struct audio_profile {
768 audio_format_t format;
769 unsigned int num_sample_rates; /* number of sampling rates in following array */
770 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
771 unsigned int num_channel_masks; /* number of channel masks in following array */
772 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
773 audio_encapsulation_type_t encapsulation_type;
774 };
775
776 struct audio_port_v7 {
777 audio_port_handle_t id; /* port unique ID */
778 audio_port_role_t role; /* sink or source */
779 audio_port_type_t type; /* device, mix ... */
780 char name[AUDIO_PORT_MAX_NAME_LEN];
781 unsigned int num_audio_profiles; /* number of audio profiles in the following
782 array */
783 struct audio_profile audio_profiles[AUDIO_PORT_MAX_AUDIO_PROFILES];
784 unsigned int num_extra_audio_descriptors; /* number of extra audio descriptors in
785 the following array */
786 struct audio_extra_audio_descriptor
787 extra_audio_descriptors[AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS];
788 unsigned int num_gains; /* number of gains in following array */
789 struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
790 struct audio_port_config active_config; /* current audio port configuration */
791 union {
792 struct audio_port_device_ext device;
793 struct audio_port_mix_ext mix;
794 struct audio_port_session_ext session;
795 } ext;
796 };
797
798 /* Return true when a given uint8_t array is a valid short audio descriptor. This function just
799 * does basic validation by checking if the first value is not zero.
800 */
audio_is_valid_short_audio_descriptor(const uint8_t * shortAudioDescriptor,size_t length)801 static inline bool audio_is_valid_short_audio_descriptor(const uint8_t *shortAudioDescriptor,
802 size_t length) {
803 return length != 0 && *shortAudioDescriptor != 0;
804 }
805
audio_populate_audio_port_v7(const struct audio_port * port,struct audio_port_v7 * portV7)806 static inline void audio_populate_audio_port_v7(
807 const struct audio_port *port, struct audio_port_v7 *portV7) {
808 portV7->id = port->id;
809 portV7->role = port->role;
810 portV7->type = port->type;
811 strncpy(portV7->name, port->name, AUDIO_PORT_MAX_NAME_LEN);
812 portV7->name[AUDIO_PORT_MAX_NAME_LEN-1] = '\0';
813 portV7->num_audio_profiles =
814 port->num_formats > AUDIO_PORT_MAX_AUDIO_PROFILES ?
815 AUDIO_PORT_MAX_AUDIO_PROFILES : port->num_formats;
816 for (size_t i = 0; i < portV7->num_audio_profiles; ++i) {
817 portV7->audio_profiles[i].format = port->formats[i];
818 portV7->audio_profiles[i].num_sample_rates = port->num_sample_rates;
819 memcpy(portV7->audio_profiles[i].sample_rates, port->sample_rates,
820 port->num_sample_rates * sizeof(unsigned int));
821 portV7->audio_profiles[i].num_channel_masks = port->num_channel_masks;
822 memcpy(portV7->audio_profiles[i].channel_masks, port->channel_masks,
823 port->num_channel_masks * sizeof(audio_channel_mask_t));
824 }
825 portV7->num_gains = port->num_gains;
826 memcpy(portV7->gains, port->gains, port->num_gains * sizeof(struct audio_gain));
827 memcpy(&portV7->active_config, &port->active_config, sizeof(struct audio_port_config));
828 memcpy(&portV7->ext, &port->ext, sizeof(port->ext));
829 }
830
831 /* Populate the data in `struct audio_port` using data from `struct audio_port_v7`. As the
832 * `struct audio_port_v7` use audio profiles to describe its capabilities, it may contain more
833 * data for sample rates or channel masks than the data that can be held by `struct audio_port`.
834 * Return true if all the data from `struct audio_port_v7` are converted to `struct audio_port`.
835 * Otherwise, return false.
836 */
audio_populate_audio_port(const struct audio_port_v7 * portV7,struct audio_port * port)837 static inline bool audio_populate_audio_port(
838 const struct audio_port_v7 *portV7, struct audio_port *port) {
839 bool allDataConverted = true;
840 port->id = portV7->id;
841 port->role = portV7->role;
842 port->type = portV7->type;
843 strncpy(port->name, portV7->name, AUDIO_PORT_MAX_NAME_LEN);
844 port->name[AUDIO_PORT_MAX_NAME_LEN-1] = '\0';
845 port->num_formats =
846 portV7->num_audio_profiles > AUDIO_PORT_MAX_FORMATS ?
847 AUDIO_PORT_MAX_FORMATS : portV7->num_audio_profiles;
848 port->num_sample_rates = 0;
849 port->num_channel_masks = 0;
850 for (size_t i = 0; i < port->num_formats; ++i) {
851 port->formats[i] = portV7->audio_profiles[i].format;
852 for (size_t j = 0; j < portV7->audio_profiles[i].num_sample_rates; ++j) {
853 size_t k = 0;
854 for (; k < port->num_sample_rates; ++k) {
855 if (port->sample_rates[k] == portV7->audio_profiles[i].sample_rates[j]) {
856 break;
857 }
858 }
859 if (k == port->num_sample_rates) {
860 if (port->num_sample_rates >= AUDIO_PORT_MAX_SAMPLING_RATES) {
861 allDataConverted = false;
862 break;
863 }
864 port->sample_rates[port->num_sample_rates++] =
865 portV7->audio_profiles[i].sample_rates[j];
866 }
867 }
868 for (size_t j = 0; j < portV7->audio_profiles[i].num_channel_masks; ++j) {
869 size_t k = 0;
870 for (; k < port->num_channel_masks; ++k) {
871 if (port->channel_masks[k] == portV7->audio_profiles[i].channel_masks[j]) {
872 break;
873 }
874 }
875 if (k == port->num_channel_masks) {
876 if (port->num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
877 allDataConverted = false;
878 break;
879 }
880 port->channel_masks[port->num_channel_masks++] =
881 portV7->audio_profiles[i].channel_masks[j];
882 }
883 }
884 }
885 port->num_gains = portV7->num_gains;
886 memcpy(port->gains, portV7->gains, port->num_gains * sizeof(struct audio_gain));
887 memcpy(&port->active_config, &portV7->active_config, sizeof(struct audio_port_config));
888 memcpy(&port->ext, &portV7->ext, sizeof(port->ext));
889 return allDataConverted && portV7->num_extra_audio_descriptors == 0;
890 }
891
audio_gain_config_are_equal(const struct audio_gain_config * lhs,const struct audio_gain_config * rhs)892 static inline bool audio_gain_config_are_equal(
893 const struct audio_gain_config *lhs, const struct audio_gain_config *rhs) {
894 if (lhs->mode != rhs->mode) return false;
895 if (lhs->mode & AUDIO_GAIN_MODE_JOINT) {
896 if (lhs->values[0] != rhs->values[0]) return false;
897 }
898 if (lhs->mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
899 if (lhs->channel_mask != rhs->channel_mask) return false;
900 for (int i = 0; i < __builtin_popcount(lhs->channel_mask); ++i) {
901 if (lhs->values[i] != rhs->values[i]) return false;
902 }
903 }
904 return lhs->ramp_duration_ms == rhs->ramp_duration_ms;
905 }
906
audio_has_input_direction(audio_port_type_t type,audio_port_role_t role)907 static inline bool audio_has_input_direction(audio_port_type_t type, audio_port_role_t role) {
908 switch (type) {
909 case AUDIO_PORT_TYPE_DEVICE:
910 switch (role) {
911 case AUDIO_PORT_ROLE_SOURCE: return true;
912 case AUDIO_PORT_ROLE_SINK: return false;
913 default: return false;
914 }
915 case AUDIO_PORT_TYPE_MIX:
916 switch (role) {
917 case AUDIO_PORT_ROLE_SOURCE: return false;
918 case AUDIO_PORT_ROLE_SINK: return true;
919 default: return false;
920 }
921 default: return false;
922 }
923 }
924
audio_port_config_has_input_direction(const struct audio_port_config * port_cfg)925 static inline bool audio_port_config_has_input_direction(const struct audio_port_config *port_cfg) {
926 return audio_has_input_direction(port_cfg->type, port_cfg->role);
927 }
928
audio_port_configs_are_equal(const struct audio_port_config * lhs,const struct audio_port_config * rhs)929 static inline bool audio_port_configs_are_equal(
930 const struct audio_port_config *lhs, const struct audio_port_config *rhs) {
931 if (lhs->role != rhs->role || lhs->type != rhs->type) return false;
932 switch (lhs->type) {
933 case AUDIO_PORT_TYPE_NONE: break;
934 case AUDIO_PORT_TYPE_DEVICE:
935 if (lhs->ext.device.hw_module != rhs->ext.device.hw_module ||
936 lhs->ext.device.type != rhs->ext.device.type ||
937 strncmp(lhs->ext.device.address, rhs->ext.device.address,
938 AUDIO_DEVICE_MAX_ADDRESS_LEN) != 0) {
939 return false;
940 }
941 break;
942 case AUDIO_PORT_TYPE_MIX:
943 if (lhs->ext.mix.hw_module != rhs->ext.mix.hw_module ||
944 lhs->ext.mix.handle != rhs->ext.mix.handle) return false;
945 if (lhs->role == AUDIO_PORT_ROLE_SOURCE &&
946 lhs->ext.mix.usecase.stream != rhs->ext.mix.usecase.stream) return false;
947 else if (lhs->role == AUDIO_PORT_ROLE_SINK &&
948 lhs->ext.mix.usecase.source != rhs->ext.mix.usecase.source) return false;
949 break;
950 case AUDIO_PORT_TYPE_SESSION:
951 if (lhs->ext.session.session != rhs->ext.session.session) return false;
952 break;
953 default: return false;
954 }
955 return
956 lhs->config_mask == rhs->config_mask &&
957 ((lhs->config_mask & AUDIO_PORT_CONFIG_FLAGS) == 0 ||
958 (audio_port_config_has_input_direction(lhs) ?
959 lhs->flags.input == rhs->flags.input :
960 lhs->flags.output == rhs->flags.output)) &&
961 ((lhs->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) == 0 ||
962 lhs->sample_rate == rhs->sample_rate) &&
963 ((lhs->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) == 0 ||
964 lhs->channel_mask == rhs->channel_mask) &&
965 ((lhs->config_mask & AUDIO_PORT_CONFIG_FORMAT) == 0 ||
966 lhs->format == rhs->format) &&
967 ((lhs->config_mask & AUDIO_PORT_CONFIG_GAIN) == 0 ||
968 audio_gain_config_are_equal(&lhs->gain, &rhs->gain));
969 }
970
audio_gains_are_equal(const struct audio_gain * lhs,const struct audio_gain * rhs)971 static inline bool audio_gains_are_equal(const struct audio_gain* lhs, const struct audio_gain* rhs) {
972 return lhs->mode == rhs->mode &&
973 ((lhs->mode & AUDIO_GAIN_MODE_CHANNELS) != AUDIO_GAIN_MODE_CHANNELS ||
974 lhs->channel_mask == rhs->channel_mask) &&
975 lhs->min_value == rhs->min_value &&
976 lhs->max_value == rhs->max_value &&
977 lhs->default_value == rhs->default_value &&
978 lhs->step_value == rhs->step_value &&
979 lhs->min_ramp_ms == rhs->min_ramp_ms &&
980 lhs->max_ramp_ms == rhs->max_ramp_ms;
981 }
982
983 // Define the helper functions of compare two audio_port/audio_port_v7 only in
984 // C++ as it is easier to compare the device capabilities.
985 #ifdef __cplusplus
986 extern "C++" {
987 #include <map>
988 #include <set>
989 #include <type_traits>
990 #include <utility>
991 #include <vector>
992
993 namespace {
994
audio_gain_array_contains_all_elements_from(const struct audio_gain gains[],const size_t numGains,const struct audio_gain from[],size_t numFromGains)995 static inline bool audio_gain_array_contains_all_elements_from(
996 const struct audio_gain gains[], const size_t numGains,
997 const struct audio_gain from[], size_t numFromGains) {
998 for (size_t i = 0; i < numFromGains; ++i) {
999 size_t j = 0;
1000 for (;j < numGains; ++j) {
1001 if (audio_gains_are_equal(&from[i], &gains[j])) {
1002 break;
1003 }
1004 }
1005 if (j == numGains) {
1006 return false;
1007 }
1008 }
1009 return true;
1010 }
1011
1012 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
1013 || std::is_same<T, struct audio_port_v7>::value, int> = 0>
audio_ports_base_are_equal(const T * lhs,const T * rhs)1014 static inline bool audio_ports_base_are_equal(const T* lhs, const T* rhs) {
1015 if (lhs->id != rhs->id || lhs->role != rhs->role || lhs->type != rhs->type ||
1016 strncmp(lhs->name, rhs->name, AUDIO_PORT_MAX_NAME_LEN) != 0 ||
1017 lhs->num_gains != rhs->num_gains) {
1018 return false;
1019 }
1020 switch (lhs->type) {
1021 case AUDIO_PORT_TYPE_NONE: break;
1022 case AUDIO_PORT_TYPE_DEVICE:
1023 if (
1024 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
1025 lhs->ext.device.encapsulation_modes != rhs->ext.device.encapsulation_modes ||
1026 lhs->ext.device.encapsulation_metadata_types !=
1027 rhs->ext.device.encapsulation_metadata_types ||
1028 #endif
1029 lhs->ext.device.hw_module != rhs->ext.device.hw_module ||
1030 lhs->ext.device.type != rhs->ext.device.type ||
1031 strncmp(lhs->ext.device.address, rhs->ext.device.address,
1032 AUDIO_DEVICE_MAX_ADDRESS_LEN) != 0) {
1033 return false;
1034 }
1035 break;
1036 case AUDIO_PORT_TYPE_MIX:
1037 if (lhs->ext.mix.hw_module != rhs->ext.mix.hw_module ||
1038 lhs->ext.mix.handle != rhs->ext.mix.handle ||
1039 lhs->ext.mix.latency_class != rhs->ext.mix.latency_class) {
1040 return false;
1041 }
1042 break;
1043 case AUDIO_PORT_TYPE_SESSION:
1044 if (lhs->ext.session.session != rhs->ext.session.session) {
1045 return false;
1046 }
1047 break;
1048 default:
1049 return false;
1050 }
1051 if (!audio_gain_array_contains_all_elements_from(
1052 lhs->gains, lhs->num_gains, rhs->gains, rhs->num_gains) ||
1053 !audio_gain_array_contains_all_elements_from(
1054 rhs->gains, rhs->num_gains, lhs->gains, lhs->num_gains)) {
1055 return false;
1056 }
1057 return audio_port_configs_are_equal(&lhs->active_config, &rhs->active_config);
1058 }
1059
1060 template <typename T, std::enable_if_t<std::is_same<T, audio_format_t>::value
1061 || std::is_same<T, unsigned int>::value
1062 || std::is_same<T, audio_channel_mask_t>::value, int> = 0>
audio_capability_arrays_are_equal(const T lhs[],unsigned int lsize,const T rhs[],unsigned int rsize)1063 static inline bool audio_capability_arrays_are_equal(
1064 const T lhs[], unsigned int lsize, const T rhs[], unsigned int rsize) {
1065 std::set<T> lhsSet(lhs, lhs + lsize);
1066 std::set<T> rhsSet(rhs, rhs + rsize);
1067 return lhsSet == rhsSet;
1068 }
1069
1070 using AudioProfileMap =
1071 std::map<audio_format_t,
1072 std::pair<std::set<unsigned int>, std::set<audio_channel_mask_t>>>;
getAudioProfileMap(const struct audio_profile profiles[],unsigned int size)1073 static inline AudioProfileMap getAudioProfileMap(
1074 const struct audio_profile profiles[], unsigned int size) {
1075 AudioProfileMap audioProfiles;
1076 for (size_t i = 0; i < size; ++i) {
1077 std::set<unsigned int> sampleRates(
1078 profiles[i].sample_rates, profiles[i].sample_rates + profiles[i].num_sample_rates);
1079 std::set<audio_channel_mask_t> channelMasks(
1080 profiles[i].channel_masks,
1081 profiles[i].channel_masks + profiles[i].num_channel_masks);
1082 audioProfiles.emplace(profiles[i].format, std::make_pair(sampleRates, channelMasks));
1083 }
1084 return audioProfiles;
1085 }
1086
audio_profile_arrays_are_equal(const struct audio_profile lhs[],unsigned int lsize,const struct audio_profile rhs[],unsigned int rsize)1087 static inline bool audio_profile_arrays_are_equal(
1088 const struct audio_profile lhs[], unsigned int lsize,
1089 const struct audio_profile rhs[], unsigned int rsize) {
1090 return getAudioProfileMap(lhs, lsize) == getAudioProfileMap(rhs, rsize);
1091 }
1092
1093 using ExtraAudioDescriptorMap =std::map<audio_standard_t,
1094 std::map<audio_encapsulation_type_t,
1095 std::set<std::vector<uint8_t>>>>;
1096
getExtraAudioDescriptorMap(const struct audio_extra_audio_descriptor extraAudioDescriptors[],unsigned int numExtraAudioDescriptors)1097 static inline ExtraAudioDescriptorMap getExtraAudioDescriptorMap(
1098 const struct audio_extra_audio_descriptor extraAudioDescriptors[],
1099 unsigned int numExtraAudioDescriptors) {
1100 ExtraAudioDescriptorMap extraAudioDescriptorMap;
1101 for (unsigned int i = 0; i < numExtraAudioDescriptors; ++i) {
1102 extraAudioDescriptorMap[extraAudioDescriptors[i].standard]
1103 [extraAudioDescriptors[i].encapsulation_type].insert(
1104 std::vector<uint8_t>(
1105 extraAudioDescriptors[i].descriptor,
1106 extraAudioDescriptors[i].descriptor
1107 + extraAudioDescriptors[i].descriptor_length));
1108 }
1109 return extraAudioDescriptorMap;
1110 }
1111
audio_extra_audio_descriptor_are_equal(const struct audio_extra_audio_descriptor lhs[],unsigned int lsize,const struct audio_extra_audio_descriptor rhs[],unsigned int rsize)1112 static inline bool audio_extra_audio_descriptor_are_equal(
1113 const struct audio_extra_audio_descriptor lhs[], unsigned int lsize,
1114 const struct audio_extra_audio_descriptor rhs[], unsigned int rsize) {
1115 return getExtraAudioDescriptorMap(lhs, lsize) == getExtraAudioDescriptorMap(rhs, rsize);
1116 }
1117
1118 } // namespace
1119
audio_ports_are_equal(const struct audio_port * lhs,const struct audio_port * rhs)1120 static inline bool audio_ports_are_equal(
1121 const struct audio_port* lhs, const struct audio_port* rhs) {
1122 if (!audio_ports_base_are_equal(lhs, rhs)) {
1123 return false;
1124 }
1125 return audio_capability_arrays_are_equal(
1126 lhs->formats, lhs->num_formats, rhs->formats, rhs->num_formats) &&
1127 audio_capability_arrays_are_equal(
1128 lhs->sample_rates, lhs->num_sample_rates,
1129 rhs->sample_rates, rhs->num_sample_rates) &&
1130 audio_capability_arrays_are_equal(
1131 lhs->channel_masks, lhs->num_channel_masks,
1132 rhs->channel_masks, rhs->num_channel_masks);
1133 }
1134
audio_ports_v7_are_equal(const struct audio_port_v7 * lhs,const struct audio_port_v7 * rhs)1135 static inline bool audio_ports_v7_are_equal(
1136 const struct audio_port_v7* lhs, const struct audio_port_v7* rhs) {
1137 if (!audio_ports_base_are_equal(lhs, rhs)) {
1138 return false;
1139 }
1140 return audio_profile_arrays_are_equal(
1141 lhs->audio_profiles, lhs->num_audio_profiles,
1142 rhs->audio_profiles, rhs->num_audio_profiles) &&
1143 audio_extra_audio_descriptor_are_equal(
1144 lhs->extra_audio_descriptors, lhs->num_extra_audio_descriptors,
1145 rhs->extra_audio_descriptors, rhs->num_extra_audio_descriptors);
1146 }
1147
1148 } // extern "C++"
1149 #endif // __cplusplus
1150
1151 /* An audio patch represents a connection between one or more source ports and
1152 * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
1153 * applications via framework APIs.
1154 * Each patch is identified by a handle at the interface used to create that patch. For instance,
1155 * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
1156 * This handle is unique to a given audio HAL hardware module.
1157 * But the same patch receives another system wide unique handle allocated by the framework.
1158 * This unique handle is used for all transactions inside the framework.
1159 */
1160 typedef int audio_patch_handle_t;
1161
1162 #define AUDIO_PATCH_PORTS_MAX 16
1163
1164 struct audio_patch {
1165 audio_patch_handle_t id; /* patch unique ID */
1166 unsigned int num_sources; /* number of sources in following array */
1167 struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
1168 unsigned int num_sinks; /* number of sinks in following array */
1169 struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
1170 };
1171
1172
1173
1174 /* a HW synchronization source returned by the audio HAL */
1175 typedef uint32_t audio_hw_sync_t;
1176
1177 /* an invalid HW synchronization source indicating an error */
1178 #define AUDIO_HW_SYNC_INVALID 0
1179
1180 /** @TODO export from .hal */
1181 typedef enum {
1182 NONE = 0x0,
1183 /**
1184 * Only set this flag if applications can access the audio buffer memory
1185 * shared with the backend (usually DSP) _without_ security issue.
1186 *
1187 * Setting this flag also implies that Binder will allow passing the shared memory FD
1188 * to applications.
1189 *
1190 * That usually implies that the kernel will prevent any access to the
1191 * memory surrounding the audio buffer as it could lead to a security breach.
1192 *
1193 * For example, a "/dev/snd/" file descriptor generally is not shareable,
1194 * but an "anon_inode:dmabuffer" file descriptor is shareable.
1195 * See also Linux kernel's dma_buf.
1196 *
1197 * This flag is required to support AAudio exclusive mode:
1198 * See: https://source.android.com/devices/audio/aaudio
1199 */
1200 AUDIO_MMAP_APPLICATION_SHAREABLE = 0x1,
1201 } audio_mmap_buffer_flag;
1202
1203 /**
1204 * Mmap buffer descriptor returned by audio_stream->create_mmap_buffer().
1205 * note\ Used by streams opened in mmap mode.
1206 */
1207 struct audio_mmap_buffer_info {
1208 void* shared_memory_address; /**< base address of mmap memory buffer.
1209 For use by local process only */
1210 int32_t shared_memory_fd; /**< FD for mmap memory buffer */
1211 int32_t buffer_size_frames; /**< total buffer size in frames */
1212 int32_t burst_size_frames; /**< transfer size granularity in frames */
1213 audio_mmap_buffer_flag flags; /**< Attributes describing the buffer. */
1214 };
1215
1216 /**
1217 * Mmap buffer read/write position returned by audio_stream->get_mmap_position().
1218 * note\ Used by streams opened in mmap mode.
1219 */
1220 struct audio_mmap_position {
1221 int64_t time_nanoseconds; /**< timestamp in ns, CLOCK_MONOTONIC */
1222 int32_t position_frames; /**< increasing 32 bit frame count reset when stream->stop()
1223 is called */
1224 };
1225
1226 /** Metadata of a playback track for an in stream. */
1227 typedef struct playback_track_metadata {
1228 audio_usage_t usage;
1229 audio_content_type_t content_type;
1230 float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
1231 } playback_track_metadata_t;
1232
1233 /** Metadata of a record track for an out stream. */
1234 typedef struct record_track_metadata {
1235 audio_source_t source;
1236 float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
1237 // For record tracks originating from a software patch, the dest_device
1238 // fields provide information about the downstream device.
1239 audio_devices_t dest_device;
1240 char dest_device_address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
1241 } record_track_metadata_t;
1242
1243 /** Metadata of a playback track for an in stream. */
1244 typedef struct playback_track_metadata_v7 {
1245 struct playback_track_metadata base;
1246 audio_channel_mask_t channel_mask;
1247 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
1248 } playback_track_metadata_v7_t;
1249
1250 /** Metadata of a record track for an out stream. */
1251 typedef struct record_track_metadata_v7 {
1252 struct record_track_metadata base;
1253 audio_channel_mask_t channel_mask;
1254 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
1255 } record_track_metadata_v7_t;
1256
playback_track_metadata_to_v7(struct playback_track_metadata_v7 * dst,const struct playback_track_metadata * src)1257 static inline void playback_track_metadata_to_v7(struct playback_track_metadata_v7 *dst,
1258 const struct playback_track_metadata *src) {
1259 dst->base = *src;
1260 dst->channel_mask = AUDIO_CHANNEL_NONE;
1261 dst->tags[0] = '\0';
1262 }
1263
playback_track_metadata_from_v7(struct playback_track_metadata * dst,const struct playback_track_metadata_v7 * src)1264 static inline void playback_track_metadata_from_v7(struct playback_track_metadata *dst,
1265 const struct playback_track_metadata_v7 *src) {
1266 *dst = src->base;
1267 }
1268
record_track_metadata_to_v7(struct record_track_metadata_v7 * dst,const struct record_track_metadata * src)1269 static inline void record_track_metadata_to_v7(struct record_track_metadata_v7 *dst,
1270 const struct record_track_metadata *src) {
1271 dst->base = *src;
1272 dst->channel_mask = AUDIO_CHANNEL_NONE;
1273 dst->tags[0] = '\0';
1274 }
1275
record_track_metadata_from_v7(struct record_track_metadata * dst,const struct record_track_metadata_v7 * src)1276 static inline void record_track_metadata_from_v7(struct record_track_metadata *dst,
1277 const struct record_track_metadata_v7 *src) {
1278 *dst = src->base;
1279 }
1280
1281 /******************************
1282 * Helper functions
1283 *****************************/
1284
1285 // see also: std::binary_search
1286 // search range [left, right)
audio_binary_search_device_array(const audio_devices_t audio_array[],size_t left,size_t right,audio_devices_t target)1287 static inline bool audio_binary_search_device_array(const audio_devices_t audio_array[],
1288 size_t left, size_t right,
1289 audio_devices_t target)
1290 {
1291 if (right <= left || target < audio_array[left] || target > audio_array[right - 1]) {
1292 return false;
1293 }
1294
1295 while (left < right) {
1296 const size_t mid = left + (right - left) / 2;
1297 if (audio_array[mid] == target) {
1298 return true;
1299 } else if (audio_array[mid] < target) {
1300 left = mid + 1;
1301 } else {
1302 right = mid;
1303 }
1304 }
1305 return false;
1306 }
1307
audio_is_output_device(audio_devices_t device)1308 static inline bool audio_is_output_device(audio_devices_t device)
1309 {
1310 switch (device) {
1311 case AUDIO_DEVICE_OUT_SPEAKER_SAFE:
1312 case AUDIO_DEVICE_OUT_SPEAKER:
1313 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
1314 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1315 case AUDIO_DEVICE_OUT_USB_HEADSET:
1316 case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
1317 case AUDIO_DEVICE_OUT_EARPIECE:
1318 case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
1319 case AUDIO_DEVICE_OUT_TELEPHONY_TX:
1320 // Search the most common devices first as these devices are most likely
1321 // to be used. Put the most common devices in the order of the likelihood
1322 // of usage to get a quick return.
1323 return true;
1324 default:
1325 // Binary seach all devices if the device is not a most common device.
1326 return audio_binary_search_device_array(
1327 AUDIO_DEVICE_OUT_ALL_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_CNT, device);
1328 }
1329 }
1330
audio_is_input_device(audio_devices_t device)1331 static inline bool audio_is_input_device(audio_devices_t device)
1332 {
1333 switch (device) {
1334 case AUDIO_DEVICE_IN_BUILTIN_MIC:
1335 case AUDIO_DEVICE_IN_BACK_MIC:
1336 case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET:
1337 case AUDIO_DEVICE_IN_WIRED_HEADSET:
1338 case AUDIO_DEVICE_IN_USB_HEADSET:
1339 case AUDIO_DEVICE_IN_REMOTE_SUBMIX:
1340 case AUDIO_DEVICE_IN_TELEPHONY_RX:
1341 // Search the most common devices first as these devices are most likely
1342 // to be used. Put the most common devices in the order of the likelihood
1343 // of usage to get a quick return.
1344 return true;
1345 default:
1346 // Binary seach all devices if the device is not a most common device.
1347 return audio_binary_search_device_array(
1348 AUDIO_DEVICE_IN_ALL_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_CNT, device);
1349 }
1350 }
1351
1352 #ifdef __cplusplus
1353 // Some effects use `uint32_t` directly for device.
audio_is_input_device(uint32_t device)1354 static inline bool audio_is_input_device(uint32_t device) {
1355 return audio_is_input_device(static_cast<audio_devices_t>(device));
1356 }
1357 // This needs to be used when `audio_is_input_device` is passed
1358 // to an STL algorithm, as otherwise the compiler can't resolve
1359 // the overload at that point--the type of the container elements
1360 // doesn't appear in the predicate parameter type definition.
1361 const auto audio_call_is_input_device = [](auto x) { return audio_is_input_device(x); };
1362 #endif
1363
1364
1365 // TODO: this function expects a combination of audio device types as parameter. It should
1366 // be deprecated as audio device types should not be use as bit mask any more since R.
audio_is_output_devices(audio_devices_t device)1367 static inline bool audio_is_output_devices(audio_devices_t device)
1368 {
1369 return (device & AUDIO_DEVICE_BIT_IN) == 0;
1370 }
1371
audio_is_a2dp_in_device(audio_devices_t device)1372 static inline bool audio_is_a2dp_in_device(audio_devices_t device)
1373 {
1374 return device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
1375 }
1376
audio_is_a2dp_out_device(audio_devices_t device)1377 static inline bool audio_is_a2dp_out_device(audio_devices_t device)
1378 {
1379 return audio_binary_search_device_array(
1380 AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_A2DP_CNT, device);
1381 }
1382
1383 // Deprecated - use audio_is_a2dp_out_device() instead
audio_is_a2dp_device(audio_devices_t device)1384 static inline bool audio_is_a2dp_device(audio_devices_t device)
1385 {
1386 return audio_is_a2dp_out_device(device);
1387 }
1388
audio_is_bluetooth_out_sco_device(audio_devices_t device)1389 static inline bool audio_is_bluetooth_out_sco_device(audio_devices_t device)
1390 {
1391 return audio_binary_search_device_array(
1392 AUDIO_DEVICE_OUT_ALL_SCO_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_SCO_CNT, device);
1393 }
1394
audio_is_bluetooth_in_sco_device(audio_devices_t device)1395 static inline bool audio_is_bluetooth_in_sco_device(audio_devices_t device)
1396 {
1397 return audio_binary_search_device_array(
1398 AUDIO_DEVICE_IN_ALL_SCO_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_SCO_CNT, device);
1399 }
1400
audio_is_bluetooth_sco_device(audio_devices_t device)1401 static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
1402 {
1403 return audio_is_bluetooth_out_sco_device(device) ||
1404 audio_is_bluetooth_in_sco_device(device);
1405 }
1406
audio_is_hearing_aid_out_device(audio_devices_t device)1407 static inline bool audio_is_hearing_aid_out_device(audio_devices_t device)
1408 {
1409 return device == AUDIO_DEVICE_OUT_HEARING_AID;
1410 }
1411
audio_is_usb_out_device(audio_devices_t device)1412 static inline bool audio_is_usb_out_device(audio_devices_t device)
1413 {
1414 return audio_binary_search_device_array(
1415 AUDIO_DEVICE_OUT_ALL_USB_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_USB_CNT, device);
1416 }
1417
audio_is_usb_in_device(audio_devices_t device)1418 static inline bool audio_is_usb_in_device(audio_devices_t device)
1419 {
1420 return audio_binary_search_device_array(
1421 AUDIO_DEVICE_IN_ALL_USB_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_USB_CNT, device);
1422 }
1423
1424 /* OBSOLETE - use audio_is_usb_out_device() instead. */
audio_is_usb_device(audio_devices_t device)1425 static inline bool audio_is_usb_device(audio_devices_t device)
1426 {
1427 return audio_is_usb_out_device(device);
1428 }
1429
audio_is_remote_submix_device(audio_devices_t device)1430 static inline bool audio_is_remote_submix_device(audio_devices_t device)
1431 {
1432 return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
1433 device == AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1434 }
1435
audio_is_digital_out_device(audio_devices_t device)1436 static inline bool audio_is_digital_out_device(audio_devices_t device)
1437 {
1438 return audio_binary_search_device_array(
1439 AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_DIGITAL_CNT, device);
1440 }
1441
audio_is_digital_in_device(audio_devices_t device)1442 static inline bool audio_is_digital_in_device(audio_devices_t device)
1443 {
1444 return audio_binary_search_device_array(
1445 AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_DIGITAL_CNT, device);
1446 }
1447
audio_device_is_digital(audio_devices_t device)1448 static inline bool audio_device_is_digital(audio_devices_t device) {
1449 return audio_is_digital_in_device(device) ||
1450 audio_is_digital_out_device(device);
1451 }
1452
audio_is_ble_out_device(audio_devices_t device)1453 static inline bool audio_is_ble_out_device(audio_devices_t device)
1454 {
1455 return audio_binary_search_device_array(
1456 AUDIO_DEVICE_OUT_ALL_BLE_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_BLE_CNT, device);
1457 }
1458
audio_is_ble_unicast_device(audio_devices_t device)1459 static inline bool audio_is_ble_unicast_device(audio_devices_t device)
1460 {
1461 return audio_binary_search_device_array(
1462 AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY, 0 /*left*/,
1463 AUDIO_DEVICE_OUT_BLE_UNICAST_CNT, device);
1464 }
1465
audio_is_ble_broadcast_device(audio_devices_t device)1466 static inline bool audio_is_ble_broadcast_device(audio_devices_t device)
1467 {
1468 return audio_binary_search_device_array(
1469 AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY, 0 /*left*/,
1470 AUDIO_DEVICE_OUT_BLE_BROADCAST_CNT, device);
1471 }
1472
audio_is_ble_in_device(audio_devices_t device)1473 static inline bool audio_is_ble_in_device(audio_devices_t device)
1474 {
1475 return audio_binary_search_device_array(
1476 AUDIO_DEVICE_IN_ALL_BLE_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_BLE_CNT, device);
1477 }
1478
audio_is_ble_device(audio_devices_t device)1479 static inline bool audio_is_ble_device(audio_devices_t device) {
1480 return audio_is_ble_in_device(device) ||
1481 audio_is_ble_out_device(device);
1482 }
1483
1484 /* Returns true if:
1485 * representation is valid, and
1486 * there is at least one channel bit set which _could_ correspond to an input channel, and
1487 * there are no channel bits set which could _not_ correspond to an input channel.
1488 * Otherwise returns false.
1489 */
audio_is_input_channel(audio_channel_mask_t channel)1490 static inline bool audio_is_input_channel(audio_channel_mask_t channel)
1491 {
1492 uint32_t bits = audio_channel_mask_get_bits(channel);
1493 switch (audio_channel_mask_get_representation(channel)) {
1494 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1495 if (bits & ~AUDIO_CHANNEL_IN_ALL) {
1496 bits = 0;
1497 }
1498 FALLTHROUGH_INTENDED;
1499 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1500 return bits != 0;
1501 default:
1502 return false;
1503 }
1504 }
1505
1506 /* Returns true if:
1507 * representation is valid, and
1508 * there is at least one channel bit set which _could_ correspond to an output channel, and
1509 * there are no channel bits set which could _not_ correspond to an output channel.
1510 * Otherwise returns false.
1511 */
audio_is_output_channel(audio_channel_mask_t channel)1512 static inline CONSTEXPR bool audio_is_output_channel(audio_channel_mask_t channel)
1513 {
1514 uint32_t bits = audio_channel_mask_get_bits(channel);
1515 switch (audio_channel_mask_get_representation(channel)) {
1516 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1517 if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
1518 bits = 0;
1519 }
1520 FALLTHROUGH_INTENDED;
1521 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1522 return bits != 0;
1523 default:
1524 return false;
1525 }
1526 }
1527
1528 /* Returns the number of channels from an input channel mask,
1529 * used in the context of audio input or recording.
1530 * If a channel bit is set which could _not_ correspond to an input channel,
1531 * it is excluded from the count.
1532 * Returns zero if the representation is invalid.
1533 */
audio_channel_count_from_in_mask(audio_channel_mask_t channel)1534 static inline CONSTEXPR uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
1535 {
1536 uint32_t bits = audio_channel_mask_get_bits(channel);
1537 switch (audio_channel_mask_get_representation(channel)) {
1538 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1539 // TODO: We can now merge with from_out_mask and remove anding
1540 bits &= AUDIO_CHANNEL_IN_ALL;
1541 FALLTHROUGH_INTENDED;
1542 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1543 return __builtin_popcount(bits);
1544 default:
1545 return 0;
1546 }
1547 }
1548
1549 #ifdef __cplusplus
1550 // FIXME(b/169889714): buffer_config_t uses `uint32_t` for the mask.
1551 // A lot of effects code thus use `uint32_t` directly.
audio_channel_count_from_in_mask(uint32_t mask)1552 static inline CONSTEXPR uint32_t audio_channel_count_from_in_mask(uint32_t mask) {
1553 return audio_channel_count_from_in_mask(static_cast<audio_channel_mask_t>(mask));
1554 }
1555 #endif
1556
1557 /* Returns the number of channels from an output channel mask,
1558 * used in the context of audio output or playback.
1559 * If a channel bit is set which could _not_ correspond to an output channel,
1560 * it is excluded from the count.
1561 * Returns zero if the representation is invalid.
1562 */
audio_channel_count_from_out_mask(audio_channel_mask_t channel)1563 static inline CONSTEXPR uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
1564 {
1565 uint32_t bits = audio_channel_mask_get_bits(channel);
1566 switch (audio_channel_mask_get_representation(channel)) {
1567 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1568 // TODO: We can now merge with from_in_mask and remove anding
1569 bits &= AUDIO_CHANNEL_OUT_ALL;
1570 FALLTHROUGH_INTENDED;
1571 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1572 return __builtin_popcount(bits);
1573 default:
1574 return 0;
1575 }
1576 }
1577
1578 #ifdef __cplusplus
1579 // FIXME(b/169889714): buffer_config_t uses `uint32_t` for the mask.
1580 // A lot of effects code thus use `uint32_t` directly.
audio_channel_count_from_out_mask(uint32_t mask)1581 static inline CONSTEXPR uint32_t audio_channel_count_from_out_mask(uint32_t mask) {
1582 return audio_channel_count_from_out_mask(static_cast<audio_channel_mask_t>(mask));
1583 }
1584 #endif
1585
1586 /* Derive a channel mask for index assignment from a channel count.
1587 * Returns the matching channel mask,
1588 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1589 * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
1590 */
audio_channel_mask_for_index_assignment_from_count(uint32_t channel_count)1591 static inline CONSTEXPR audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
1592 uint32_t channel_count)
1593 {
1594 if (channel_count == 0) {
1595 return AUDIO_CHANNEL_NONE;
1596 }
1597 if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
1598 return AUDIO_CHANNEL_INVALID;
1599 }
1600 uint32_t bits = (1 << channel_count) - 1;
1601 return audio_channel_mask_from_representation_and_bits(
1602 AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
1603 }
1604
1605 /* Derive an output channel mask for position assignment from a channel count.
1606 * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
1607 * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
1608 * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
1609 * for continuity with stereo.
1610 * Returns the matching channel mask,
1611 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1612 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1613 * configurations for which a default output channel mask is defined.
1614 */
audio_channel_out_mask_from_count(uint32_t channel_count)1615 static inline CONSTEXPR audio_channel_mask_t audio_channel_out_mask_from_count(
1616 uint32_t channel_count)
1617 {
1618 uint32_t bits = 0;
1619 switch (channel_count) {
1620 case 0:
1621 return AUDIO_CHANNEL_NONE;
1622 case 1:
1623 bits = AUDIO_CHANNEL_OUT_MONO;
1624 break;
1625 case 2:
1626 bits = AUDIO_CHANNEL_OUT_STEREO;
1627 break;
1628 case 3:
1629 bits = AUDIO_CHANNEL_OUT_2POINT1;
1630 break;
1631 case 4: // 4.0
1632 bits = AUDIO_CHANNEL_OUT_QUAD;
1633 break;
1634 case 5: // 5.0
1635 bits = AUDIO_CHANNEL_OUT_PENTA;
1636 break;
1637 case 6:
1638 bits = AUDIO_CHANNEL_OUT_5POINT1;
1639 break;
1640 case 7:
1641 bits = AUDIO_CHANNEL_OUT_6POINT1;
1642 break;
1643 case FCC_8:
1644 bits = AUDIO_CHANNEL_OUT_7POINT1;
1645 break;
1646 case 10:
1647 bits = AUDIO_CHANNEL_OUT_5POINT1POINT4;
1648 break;
1649 case FCC_12:
1650 bits = AUDIO_CHANNEL_OUT_7POINT1POINT4;
1651 break;
1652 case FCC_24:
1653 bits = AUDIO_CHANNEL_OUT_22POINT2;
1654 break;
1655 default:
1656 return AUDIO_CHANNEL_INVALID;
1657 }
1658 return audio_channel_mask_from_representation_and_bits(
1659 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1660 }
1661
1662 /* Derive a default input channel mask from a channel count.
1663 * Assumes a position mask for mono and stereo, or an index mask for channel counts > 2.
1664 * Returns the matching channel mask,
1665 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1666 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1667 * configurations for which a default input channel mask is defined.
1668 */
audio_channel_in_mask_from_count(uint32_t channel_count)1669 static inline CONSTEXPR audio_channel_mask_t audio_channel_in_mask_from_count(
1670 uint32_t channel_count)
1671 {
1672 uint32_t bits = 0;
1673 switch (channel_count) {
1674 case 0:
1675 return AUDIO_CHANNEL_NONE;
1676 case 1:
1677 bits = AUDIO_CHANNEL_IN_MONO;
1678 break;
1679 case 2:
1680 bits = AUDIO_CHANNEL_IN_STEREO;
1681 break;
1682 default:
1683 if (channel_count <= FCC_LIMIT) {
1684 return audio_channel_mask_for_index_assignment_from_count(channel_count);
1685 }
1686 return AUDIO_CHANNEL_INVALID;
1687 }
1688 return audio_channel_mask_from_representation_and_bits(
1689 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1690 }
1691
1692 /* Derive a default haptic channel mask from a channel count.
1693 */
haptic_channel_mask_from_count(uint32_t channel_count)1694 static inline audio_channel_mask_t haptic_channel_mask_from_count(uint32_t channel_count)
1695 {
1696 switch(channel_count) {
1697 case 0:
1698 return AUDIO_CHANNEL_NONE;
1699 case 1:
1700 return AUDIO_CHANNEL_OUT_HAPTIC_A;
1701 case 2:
1702 return AUDIO_CHANNEL_OUT_HAPTIC_AB;
1703 default:
1704 return AUDIO_CHANNEL_INVALID;
1705 }
1706 }
1707
audio_channel_mask_in_to_out(audio_channel_mask_t in)1708 static inline audio_channel_mask_t audio_channel_mask_in_to_out(audio_channel_mask_t in)
1709 {
1710 switch (in) {
1711 case AUDIO_CHANNEL_IN_MONO:
1712 return AUDIO_CHANNEL_OUT_MONO;
1713 case AUDIO_CHANNEL_IN_STEREO:
1714 return AUDIO_CHANNEL_OUT_STEREO;
1715 case AUDIO_CHANNEL_IN_2POINT1:
1716 return AUDIO_CHANNEL_OUT_2POINT1;
1717 case AUDIO_CHANNEL_IN_QUAD:
1718 return AUDIO_CHANNEL_OUT_QUAD;
1719 case AUDIO_CHANNEL_IN_PENTA:
1720 return AUDIO_CHANNEL_OUT_PENTA;
1721 case AUDIO_CHANNEL_IN_5POINT1:
1722 return AUDIO_CHANNEL_OUT_5POINT1;
1723 case AUDIO_CHANNEL_IN_3POINT1POINT2:
1724 return AUDIO_CHANNEL_OUT_3POINT1POINT2;
1725 case AUDIO_CHANNEL_IN_3POINT0POINT2:
1726 return AUDIO_CHANNEL_OUT_3POINT0POINT2;
1727 case AUDIO_CHANNEL_IN_2POINT1POINT2:
1728 return AUDIO_CHANNEL_OUT_2POINT1POINT2;
1729 case AUDIO_CHANNEL_IN_2POINT0POINT2:
1730 return AUDIO_CHANNEL_OUT_2POINT0POINT2;
1731 default:
1732 return AUDIO_CHANNEL_INVALID;
1733 }
1734 }
1735
audio_channel_mask_out_to_in(audio_channel_mask_t out)1736 static inline audio_channel_mask_t audio_channel_mask_out_to_in(audio_channel_mask_t out)
1737 {
1738 switch (out) {
1739 case AUDIO_CHANNEL_OUT_MONO:
1740 return AUDIO_CHANNEL_IN_MONO;
1741 case AUDIO_CHANNEL_OUT_STEREO:
1742 return AUDIO_CHANNEL_IN_STEREO;
1743 case AUDIO_CHANNEL_OUT_2POINT1:
1744 return AUDIO_CHANNEL_IN_2POINT1;
1745 case AUDIO_CHANNEL_OUT_QUAD:
1746 return AUDIO_CHANNEL_IN_QUAD;
1747 case AUDIO_CHANNEL_OUT_PENTA:
1748 return AUDIO_CHANNEL_IN_PENTA;
1749 case AUDIO_CHANNEL_OUT_5POINT1:
1750 return AUDIO_CHANNEL_IN_5POINT1;
1751 case AUDIO_CHANNEL_OUT_3POINT1POINT2:
1752 return AUDIO_CHANNEL_IN_3POINT1POINT2;
1753 case AUDIO_CHANNEL_OUT_3POINT0POINT2:
1754 return AUDIO_CHANNEL_IN_3POINT0POINT2;
1755 case AUDIO_CHANNEL_OUT_2POINT1POINT2:
1756 return AUDIO_CHANNEL_IN_2POINT1POINT2;
1757 case AUDIO_CHANNEL_OUT_2POINT0POINT2:
1758 return AUDIO_CHANNEL_IN_2POINT0POINT2;
1759 default:
1760 return AUDIO_CHANNEL_INVALID;
1761 }
1762 }
1763
audio_channel_mask_out_to_in_index_mask(audio_channel_mask_t out)1764 static inline audio_channel_mask_t audio_channel_mask_out_to_in_index_mask(audio_channel_mask_t out)
1765 {
1766 return audio_channel_mask_for_index_assignment_from_count(
1767 audio_channel_count_from_out_mask(out));
1768 }
1769
audio_channel_position_mask_is_out_canonical(audio_channel_mask_t channelMask)1770 static inline bool audio_channel_position_mask_is_out_canonical(audio_channel_mask_t channelMask)
1771 {
1772 if (audio_channel_mask_get_representation(channelMask)
1773 != AUDIO_CHANNEL_REPRESENTATION_POSITION) {
1774 return false;
1775 }
1776 const uint32_t audioChannelCount = audio_channel_count_from_out_mask(
1777 (audio_channel_mask_t)(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL));
1778 const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(
1779 (audio_channel_mask_t)(channelMask & AUDIO_CHANNEL_HAPTIC_ALL));
1780 return channelMask == (audio_channel_mask_t)(
1781 audio_channel_out_mask_from_count(audioChannelCount) |
1782 haptic_channel_mask_from_count(hapticChannelCount));
1783 }
1784
audio_is_valid_format(audio_format_t format)1785 static inline bool audio_is_valid_format(audio_format_t format)
1786 {
1787 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1788 case AUDIO_FORMAT_PCM:
1789 switch (format) {
1790 case AUDIO_FORMAT_PCM_16_BIT:
1791 case AUDIO_FORMAT_PCM_8_BIT:
1792 case AUDIO_FORMAT_PCM_32_BIT:
1793 case AUDIO_FORMAT_PCM_8_24_BIT:
1794 case AUDIO_FORMAT_PCM_FLOAT:
1795 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1796 return true;
1797 default:
1798 return false;
1799 }
1800 /* not reached */
1801 case AUDIO_FORMAT_MP3:
1802 case AUDIO_FORMAT_AMR_NB:
1803 case AUDIO_FORMAT_AMR_WB:
1804 return true;
1805 case AUDIO_FORMAT_AAC:
1806 switch (format) {
1807 case AUDIO_FORMAT_AAC:
1808 case AUDIO_FORMAT_AAC_MAIN:
1809 case AUDIO_FORMAT_AAC_LC:
1810 case AUDIO_FORMAT_AAC_SSR:
1811 case AUDIO_FORMAT_AAC_LTP:
1812 case AUDIO_FORMAT_AAC_HE_V1:
1813 case AUDIO_FORMAT_AAC_SCALABLE:
1814 case AUDIO_FORMAT_AAC_ERLC:
1815 case AUDIO_FORMAT_AAC_LD:
1816 case AUDIO_FORMAT_AAC_HE_V2:
1817 case AUDIO_FORMAT_AAC_ELD:
1818 case AUDIO_FORMAT_AAC_XHE:
1819 return true;
1820 default:
1821 return false;
1822 }
1823 /* not reached */
1824 case AUDIO_FORMAT_HE_AAC_V1:
1825 case AUDIO_FORMAT_HE_AAC_V2:
1826 case AUDIO_FORMAT_VORBIS:
1827 case AUDIO_FORMAT_OPUS:
1828 case AUDIO_FORMAT_AC3:
1829 return true;
1830 case AUDIO_FORMAT_E_AC3:
1831 switch (format) {
1832 case AUDIO_FORMAT_E_AC3:
1833 case AUDIO_FORMAT_E_AC3_JOC:
1834 return true;
1835 default:
1836 return false;
1837 }
1838 /* not reached */
1839 case AUDIO_FORMAT_DTS:
1840 case AUDIO_FORMAT_DTS_HD:
1841 case AUDIO_FORMAT_IEC60958:
1842 case AUDIO_FORMAT_IEC61937:
1843 case AUDIO_FORMAT_DOLBY_TRUEHD:
1844 case AUDIO_FORMAT_EVRC:
1845 case AUDIO_FORMAT_EVRCB:
1846 case AUDIO_FORMAT_EVRCWB:
1847 case AUDIO_FORMAT_EVRCNW:
1848 case AUDIO_FORMAT_AAC_ADIF:
1849 case AUDIO_FORMAT_WMA:
1850 case AUDIO_FORMAT_WMA_PRO:
1851 case AUDIO_FORMAT_AMR_WB_PLUS:
1852 case AUDIO_FORMAT_MP2:
1853 case AUDIO_FORMAT_QCELP:
1854 case AUDIO_FORMAT_DSD:
1855 case AUDIO_FORMAT_FLAC:
1856 case AUDIO_FORMAT_ALAC:
1857 case AUDIO_FORMAT_APE:
1858 return true;
1859 case AUDIO_FORMAT_AAC_ADTS:
1860 switch (format) {
1861 case AUDIO_FORMAT_AAC_ADTS:
1862 case AUDIO_FORMAT_AAC_ADTS_MAIN:
1863 case AUDIO_FORMAT_AAC_ADTS_LC:
1864 case AUDIO_FORMAT_AAC_ADTS_SSR:
1865 case AUDIO_FORMAT_AAC_ADTS_LTP:
1866 case AUDIO_FORMAT_AAC_ADTS_HE_V1:
1867 case AUDIO_FORMAT_AAC_ADTS_SCALABLE:
1868 case AUDIO_FORMAT_AAC_ADTS_ERLC:
1869 case AUDIO_FORMAT_AAC_ADTS_LD:
1870 case AUDIO_FORMAT_AAC_ADTS_HE_V2:
1871 case AUDIO_FORMAT_AAC_ADTS_ELD:
1872 case AUDIO_FORMAT_AAC_ADTS_XHE:
1873 return true;
1874 default:
1875 return false;
1876 }
1877 /* not reached */
1878 case AUDIO_FORMAT_SBC:
1879 case AUDIO_FORMAT_APTX:
1880 case AUDIO_FORMAT_APTX_HD:
1881 case AUDIO_FORMAT_AC4:
1882 case AUDIO_FORMAT_LDAC:
1883 return true;
1884 case AUDIO_FORMAT_MAT:
1885 switch (format) {
1886 case AUDIO_FORMAT_MAT:
1887 case AUDIO_FORMAT_MAT_1_0:
1888 case AUDIO_FORMAT_MAT_2_0:
1889 case AUDIO_FORMAT_MAT_2_1:
1890 return true;
1891 default:
1892 return false;
1893 }
1894 /* not reached */
1895 case AUDIO_FORMAT_AAC_LATM:
1896 switch (format) {
1897 case AUDIO_FORMAT_AAC_LATM:
1898 case AUDIO_FORMAT_AAC_LATM_LC:
1899 case AUDIO_FORMAT_AAC_LATM_HE_V1:
1900 case AUDIO_FORMAT_AAC_LATM_HE_V2:
1901 return true;
1902 default:
1903 return false;
1904 }
1905 /* not reached */
1906 case AUDIO_FORMAT_CELT:
1907 case AUDIO_FORMAT_APTX_ADAPTIVE:
1908 case AUDIO_FORMAT_LHDC:
1909 case AUDIO_FORMAT_LHDC_LL:
1910 case AUDIO_FORMAT_APTX_TWSP:
1911 case AUDIO_FORMAT_LC3:
1912 case AUDIO_FORMAT_APTX_ADAPTIVE_QLEA:
1913 case AUDIO_FORMAT_APTX_ADAPTIVE_R4:
1914 return true;
1915 case AUDIO_FORMAT_MPEGH:
1916 switch (format) {
1917 case AUDIO_FORMAT_MPEGH_BL_L3:
1918 case AUDIO_FORMAT_MPEGH_BL_L4:
1919 case AUDIO_FORMAT_MPEGH_LC_L3:
1920 case AUDIO_FORMAT_MPEGH_LC_L4:
1921 return true;
1922 default:
1923 return false;
1924 }
1925 /* not reached */
1926 case AUDIO_FORMAT_DTS_UHD:
1927 case AUDIO_FORMAT_DRA:
1928 case AUDIO_FORMAT_DTS_HD_MA:
1929 case AUDIO_FORMAT_DTS_UHD_P2:
1930 return true;
1931 default:
1932 return false;
1933 }
1934 }
1935
audio_is_iec61937_compatible(audio_format_t format)1936 static inline bool audio_is_iec61937_compatible(audio_format_t format)
1937 {
1938 switch (format) {
1939 case AUDIO_FORMAT_AC3: // IEC 61937-3:2017
1940 case AUDIO_FORMAT_AC4: // IEC 61937-14:2017
1941 case AUDIO_FORMAT_E_AC3: // IEC 61937-3:2017
1942 case AUDIO_FORMAT_E_AC3_JOC: // IEC 61937-3:2017
1943 case AUDIO_FORMAT_MAT: // IEC 61937-9:2017
1944 case AUDIO_FORMAT_MAT_1_0: // IEC 61937-9:2017
1945 case AUDIO_FORMAT_MAT_2_0: // IEC 61937-9:2017
1946 case AUDIO_FORMAT_MAT_2_1: // IEC 61937-9:2017
1947 case AUDIO_FORMAT_MPEGH_BL_L3: // IEC 61937-13:2018
1948 case AUDIO_FORMAT_MPEGH_BL_L4: // IEC 61937-13:2018
1949 case AUDIO_FORMAT_MPEGH_LC_L3: // IEC 61937-13:2018
1950 case AUDIO_FORMAT_MPEGH_LC_L4: // IEC 61937-13:2018
1951 return true;
1952 default:
1953 return false;
1954 }
1955 }
1956
1957 /**
1958 * Extract the primary format, eg. PCM, AC3, etc.
1959 */
audio_get_main_format(audio_format_t format)1960 static inline audio_format_t audio_get_main_format(audio_format_t format)
1961 {
1962 return (audio_format_t)(format & AUDIO_FORMAT_MAIN_MASK);
1963 }
1964
1965 /**
1966 * Is the data plain PCM samples that can be scaled and mixed?
1967 */
audio_is_linear_pcm(audio_format_t format)1968 static inline bool audio_is_linear_pcm(audio_format_t format)
1969 {
1970 return (audio_get_main_format(format) == AUDIO_FORMAT_PCM);
1971 }
1972
1973 /**
1974 * For this format, is the number of PCM audio frames directly proportional
1975 * to the number of data bytes?
1976 *
1977 * In other words, is the format transported as PCM audio samples,
1978 * but not necessarily scalable or mixable.
1979 * This returns true for real PCM, but also for AUDIO_FORMAT_IEC61937,
1980 * which is transported as 16 bit PCM audio, but where the encoded data
1981 * cannot be mixed or scaled.
1982 */
audio_has_proportional_frames(audio_format_t format)1983 static inline bool audio_has_proportional_frames(audio_format_t format)
1984 {
1985 audio_format_t mainFormat = audio_get_main_format(format);
1986 return (mainFormat == AUDIO_FORMAT_PCM
1987 || mainFormat == AUDIO_FORMAT_IEC61937);
1988 }
1989
audio_bytes_per_sample(audio_format_t format)1990 static inline size_t audio_bytes_per_sample(audio_format_t format)
1991 {
1992 size_t size = 0;
1993
1994 switch (format) {
1995 case AUDIO_FORMAT_PCM_32_BIT:
1996 case AUDIO_FORMAT_PCM_8_24_BIT:
1997 size = sizeof(int32_t);
1998 break;
1999 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
2000 size = sizeof(uint8_t) * 3;
2001 break;
2002 case AUDIO_FORMAT_PCM_16_BIT:
2003 case AUDIO_FORMAT_IEC61937:
2004 size = sizeof(int16_t);
2005 break;
2006 case AUDIO_FORMAT_PCM_8_BIT:
2007 size = sizeof(uint8_t);
2008 break;
2009 case AUDIO_FORMAT_PCM_FLOAT:
2010 size = sizeof(float);
2011 break;
2012 default:
2013 break;
2014 }
2015 return size;
2016 }
2017
audio_bytes_per_frame(uint32_t channel_count,audio_format_t format)2018 static inline size_t audio_bytes_per_frame(uint32_t channel_count, audio_format_t format)
2019 {
2020 // cannot overflow for reasonable channel_count
2021 return channel_count * audio_bytes_per_sample(format);
2022 }
2023
2024 /* converts device address to string sent to audio HAL via set_parameters */
audio_device_address_to_parameter(audio_devices_t device,const char * address)2025 static inline char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
2026 {
2027 const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_source_address=");
2028 char param[kSize];
2029
2030 if (device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
2031 snprintf(param, kSize, "%s=%s", "a2dp_source_address", address);
2032 } else if (audio_is_a2dp_out_device(device)) {
2033 snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
2034 } else if (audio_is_remote_submix_device(device)) {
2035 snprintf(param, kSize, "%s=%s", "mix", address);
2036 } else {
2037 snprintf(param, kSize, "%s", address);
2038 }
2039 return strdup(param);
2040 }
2041
audio_is_valid_audio_source(audio_source_t audioSource)2042 static inline bool audio_is_valid_audio_source(audio_source_t audioSource)
2043 {
2044 switch (audioSource) {
2045 case AUDIO_SOURCE_MIC:
2046 case AUDIO_SOURCE_VOICE_UPLINK:
2047 case AUDIO_SOURCE_VOICE_DOWNLINK:
2048 case AUDIO_SOURCE_VOICE_CALL:
2049 case AUDIO_SOURCE_CAMCORDER:
2050 case AUDIO_SOURCE_VOICE_RECOGNITION:
2051 case AUDIO_SOURCE_VOICE_COMMUNICATION:
2052 case AUDIO_SOURCE_REMOTE_SUBMIX:
2053 case AUDIO_SOURCE_UNPROCESSED:
2054 case AUDIO_SOURCE_VOICE_PERFORMANCE:
2055 case AUDIO_SOURCE_ECHO_REFERENCE:
2056 case AUDIO_SOURCE_FM_TUNER:
2057 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2058 case AUDIO_SOURCE_HOTWORD:
2059 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
2060 case AUDIO_SOURCE_ULTRASOUND:
2061 return true;
2062 default:
2063 return false;
2064 }
2065 }
2066
2067 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2068
audio_port_config_has_hw_av_sync(const struct audio_port_config * port_cfg)2069 static inline bool audio_port_config_has_hw_av_sync(const struct audio_port_config *port_cfg) {
2070 if (!(port_cfg->config_mask & AUDIO_PORT_CONFIG_FLAGS)) {
2071 return false;
2072 }
2073 return audio_port_config_has_input_direction(port_cfg) ?
2074 port_cfg->flags.input & AUDIO_INPUT_FLAG_HW_AV_SYNC
2075 : port_cfg->flags.output & AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
2076 }
2077
audio_patch_has_hw_av_sync(const struct audio_patch * patch)2078 static inline bool audio_patch_has_hw_av_sync(const struct audio_patch *patch) {
2079 for (unsigned int i = 0; i < patch->num_sources; ++i) {
2080 if (audio_port_config_has_hw_av_sync(&patch->sources[i])) return true;
2081 }
2082 for (unsigned int i = 0; i < patch->num_sinks; ++i) {
2083 if (audio_port_config_has_hw_av_sync(&patch->sinks[i])) return true;
2084 }
2085 return false;
2086 }
2087
audio_patch_is_valid(const struct audio_patch * patch)2088 static inline bool audio_patch_is_valid(const struct audio_patch *patch) {
2089 // Note that patch can have no sinks.
2090 return patch->num_sources != 0 && patch->num_sources <= AUDIO_PATCH_PORTS_MAX &&
2091 patch->num_sinks <= AUDIO_PATCH_PORTS_MAX;
2092 }
2093
2094 // Note that when checking for equality the order of ports must match.
2095 // Patches will not be equivalent if they contain the same ports but they are permuted differently.
audio_patches_are_equal(const struct audio_patch * lhs,const struct audio_patch * rhs)2096 static inline bool audio_patches_are_equal(
2097 const struct audio_patch *lhs, const struct audio_patch *rhs) {
2098 if (!audio_patch_is_valid(lhs) || !audio_patch_is_valid(rhs)) return false;
2099 if (lhs->num_sources != rhs->num_sources || lhs->num_sinks != rhs->num_sinks) return false;
2100 for (unsigned int i = 0; i < lhs->num_sources; ++i) {
2101 if (!audio_port_configs_are_equal(&lhs->sources[i], &rhs->sources[i])) return false;
2102 }
2103 for (unsigned int i = 0; i < lhs->num_sinks; ++i) {
2104 if (!audio_port_configs_are_equal(&lhs->sinks[i], &rhs->sinks[i])) return false;
2105 }
2106 return true;
2107 }
2108
2109 #endif
2110
2111 // Unique effect ID (can be generated from the following site:
2112 // http://www.itu.int/ITU-T/asn1/uuid.html)
2113 // This struct is used for effects identification and in soundtrigger.
2114 typedef struct audio_uuid_s {
2115 uint32_t timeLow;
2116 uint16_t timeMid;
2117 uint16_t timeHiAndVersion;
2118 uint16_t clockSeq;
2119 uint8_t node[6];
2120 } audio_uuid_t;
2121
2122 /* A 3D point which could be used to represent geometric location
2123 * or orientation of a microphone.
2124 */
2125 struct audio_microphone_coordinate {
2126 float x;
2127 float y;
2128 float z;
2129 };
2130
2131 /* An number to indicate which group the microphone locate. Main body is
2132 * usually group 0. Developer could use this value to group the microphones
2133 * that locate on the same peripheral or attachments.
2134 */
2135 typedef int audio_microphone_group_t;
2136
2137 /* the maximum length for the microphone id */
2138 #define AUDIO_MICROPHONE_ID_MAX_LEN 32
2139 /* max number of frequency responses in a frequency response table */
2140 #define AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES 256
2141 /* max number of microphone */
2142 #define AUDIO_MICROPHONE_MAX_COUNT 32
2143 /* the value of unknown spl */
2144 #define AUDIO_MICROPHONE_SPL_UNKNOWN -FLT_MAX
2145 /* the value of unknown sensitivity */
2146 #define AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN -FLT_MAX
2147 /* the value of unknown coordinate */
2148 #define AUDIO_MICROPHONE_COORDINATE_UNKNOWN -FLT_MAX
2149 /* the value used as address when the address of bottom microphone is empty */
2150 #define AUDIO_BOTTOM_MICROPHONE_ADDRESS "bottom"
2151 /* the value used as address when the address of back microphone is empty */
2152 #define AUDIO_BACK_MICROPHONE_ADDRESS "back"
2153
2154 struct audio_microphone_characteristic_t {
2155 char device_id[AUDIO_MICROPHONE_ID_MAX_LEN];
2156 audio_port_handle_t id;
2157 audio_devices_t device;
2158 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
2159 audio_microphone_channel_mapping_t channel_mapping[AUDIO_CHANNEL_COUNT_MAX];
2160 audio_microphone_location_t location;
2161 audio_microphone_group_t group;
2162 unsigned int index_in_the_group;
2163 float sensitivity;
2164 float max_spl;
2165 float min_spl;
2166 audio_microphone_directionality_t directionality;
2167 unsigned int num_frequency_responses;
2168 float frequency_responses[2][AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES];
2169 struct audio_microphone_coordinate geometric_location;
2170 struct audio_microphone_coordinate orientation;
2171 };
2172
2173 typedef enum {
2174 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2175 AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT = -1, // (framework only) for speed <1.0 will truncate
2176 // frames, for speed > 1.0 will repeat frames
2177 AUDIO_TIMESTRETCH_FALLBACK_DEFAULT = 0, // (framework only) system determines behavior
2178 #endif
2179 /* Set all processed frames to zero. */
2180 AUDIO_TIMESTRETCH_FALLBACK_MUTE = HAL_AUDIO_TIMESTRETCH_FALLBACK_MUTE,
2181 /* Stop processing and indicate an error. */
2182 AUDIO_TIMESTRETCH_FALLBACK_FAIL = HAL_AUDIO_TIMESTRETCH_FALLBACK_FAIL,
2183 } audio_timestretch_fallback_mode_t;
2184
2185 // AUDIO_TIMESTRETCH_SPEED_MIN and AUDIO_TIMESTRETCH_SPEED_MAX define the min and max time stretch
2186 // speeds supported by the system. These are enforced by the system and values outside this range
2187 // will result in a runtime error.
2188 // Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than
2189 // the ones specified here
2190 // AUDIO_TIMESTRETCH_SPEED_MIN_DELTA is the minimum absolute speed difference that might trigger a
2191 // parameter update
2192 #define AUDIO_TIMESTRETCH_SPEED_MIN 0.01f
2193 #define AUDIO_TIMESTRETCH_SPEED_MAX 20.0f
2194 #define AUDIO_TIMESTRETCH_SPEED_NORMAL 1.0f
2195 #define AUDIO_TIMESTRETCH_SPEED_MIN_DELTA 0.0001f
2196
2197 // AUDIO_TIMESTRETCH_PITCH_MIN and AUDIO_TIMESTRETCH_PITCH_MAX define the min and max time stretch
2198 // pitch shifting supported by the system. These are not enforced by the system and values
2199 // outside this range might result in a pitch different than the one requested.
2200 // Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than
2201 // the ones specified here.
2202 // AUDIO_TIMESTRETCH_PITCH_MIN_DELTA is the minimum absolute pitch difference that might trigger a
2203 // parameter update
2204 #define AUDIO_TIMESTRETCH_PITCH_MIN 0.25f
2205 #define AUDIO_TIMESTRETCH_PITCH_MAX 4.0f
2206 #define AUDIO_TIMESTRETCH_PITCH_NORMAL 1.0f
2207 #define AUDIO_TIMESTRETCH_PITCH_MIN_DELTA 0.0001f
2208
2209 //Limits for AUDIO_TIMESTRETCH_STRETCH_VOICE mode
2210 #define TIMESTRETCH_SONIC_SPEED_MIN 0.1f
2211 #define TIMESTRETCH_SONIC_SPEED_MAX 6.0f
2212
2213 struct audio_playback_rate {
2214 float mSpeed;
2215 float mPitch;
2216 audio_timestretch_stretch_mode_t mStretchMode;
2217 audio_timestretch_fallback_mode_t mFallbackMode;
2218 };
2219
2220 typedef struct audio_playback_rate audio_playback_rate_t;
2221
2222 static const audio_playback_rate_t AUDIO_PLAYBACK_RATE_INITIALIZER = {
2223 /* .mSpeed = */ AUDIO_TIMESTRETCH_SPEED_NORMAL,
2224 /* .mPitch = */ AUDIO_TIMESTRETCH_PITCH_NORMAL,
2225 /* .mStretchMode = */ AUDIO_TIMESTRETCH_STRETCH_DEFAULT,
2226 /* .mFallbackMode = */ AUDIO_TIMESTRETCH_FALLBACK_FAIL
2227 };
2228
2229 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2230 typedef enum {
2231 AUDIO_DIRECT_NOT_SUPPORTED = 0x0u,
2232 AUDIO_DIRECT_OFFLOAD_SUPPORTED = 0x1u,
2233 AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED = 0x2u,
2234 // TODO(b/211628732): may need an enum for direct pcm
2235 AUDIO_DIRECT_BITSTREAM_SUPPORTED = 0x4u,
2236 } audio_direct_mode_t;
2237
2238 // TODO: Deprecate audio_offload_mode_t and use audio_direct_mode_t instead.
2239 typedef enum {
2240 AUDIO_OFFLOAD_NOT_SUPPORTED = AUDIO_DIRECT_NOT_SUPPORTED,
2241 AUDIO_OFFLOAD_SUPPORTED = AUDIO_DIRECT_OFFLOAD_SUPPORTED,
2242 AUDIO_OFFLOAD_GAPLESS_SUPPORTED = AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED
2243 } audio_offload_mode_t;
2244 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
2245
2246 typedef enum : int32_t {
2247 AUDIO_MIXER_BEHAVIOR_INVALID = -1,
2248 AUDIO_MIXER_BEHAVIOR_DEFAULT = 0,
2249 AUDIO_MIXER_BEHAVIOR_BIT_PERFECT = 1,
2250 } audio_mixer_behavior_t;
2251
2252 struct audio_mixer_attributes {
2253 audio_config_base_t config;
2254 audio_mixer_behavior_t mixer_behavior;
2255 };
2256
2257 typedef struct audio_mixer_attributes audio_mixer_attributes_t;
2258
2259 static const audio_mixer_attributes_t AUDIO_MIXER_ATTRIBUTES_INITIALIZER = {
2260 /* .config */ {
2261 /* .sample_rate*/ 0,
2262 /* .channel_mask*/ AUDIO_CHANNEL_NONE,
2263 /* .format */ AUDIO_FORMAT_DEFAULT,
2264 },
2265 /* .mixer_behavior */ AUDIO_MIXER_BEHAVIOR_DEFAULT,
2266 };
2267
audio_output_flags_from_mixer_behavior(audio_mixer_behavior_t mixerBehavior)2268 static inline audio_output_flags_t audio_output_flags_from_mixer_behavior(
2269 audio_mixer_behavior_t mixerBehavior) {
2270 switch (mixerBehavior) {
2271 case AUDIO_MIXER_BEHAVIOR_BIT_PERFECT:
2272 return AUDIO_OUTPUT_FLAG_BIT_PERFECT;
2273 case AUDIO_MIXER_BEHAVIOR_DEFAULT:
2274 default:
2275 return AUDIO_OUTPUT_FLAG_NONE;
2276 }
2277 }
2278
audio_channel_mask_to_string(audio_channel_mask_t channel_mask)2279 inline const char* audio_channel_mask_to_string(audio_channel_mask_t channel_mask) {
2280 if (audio_is_input_channel(channel_mask)) {
2281 return audio_channel_in_mask_to_string(channel_mask);
2282 } else if (audio_is_output_channel(channel_mask)) {
2283 return audio_channel_out_mask_to_string(channel_mask);
2284 } else {
2285 return audio_channel_index_mask_to_string(channel_mask);
2286 }
2287 }
2288
2289 __END_DECLS
2290
2291 /**
2292 * List of known audio HAL modules. This is the base name of the audio HAL
2293 * library composed of the "audio." prefix, one of the base names below and
2294 * a suffix specific to the device.
2295 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
2296 *
2297 * The same module names are used in audio policy configuration files.
2298 */
2299
2300 #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
2301 #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
2302 #define AUDIO_HARDWARE_MODULE_ID_USB "usb"
2303 #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
2304 #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
2305 #define AUDIO_HARDWARE_MODULE_ID_STUB "stub"
2306 #define AUDIO_HARDWARE_MODULE_ID_HEARING_AID "hearing_aid"
2307 #define AUDIO_HARDWARE_MODULE_ID_MSD "msd"
2308
2309 /**
2310 * Multi-Stream Decoder (MSD) HAL service name. MSD HAL is used to mix
2311 * encoded streams together with PCM streams, producing re-encoded
2312 * streams or PCM streams.
2313 *
2314 * The service must register itself using this name, and audioserver
2315 * tries to instantiate a device factory using this name as well.
2316 * Note that the HIDL implementation library file name *must* have the
2317 * suffix "msd" in order to be picked up by HIDL that is:
2318 *
2319 * android.hardware.audio@x.x-implmsd.so
2320 */
2321 #define AUDIO_HAL_SERVICE_NAME_MSD "msd"
2322
2323 /**
2324 * Parameter definitions.
2325 * Note that in the framework code it's recommended to use AudioParameter.h
2326 * instead of these preprocessor defines, and for sure avoid just copying
2327 * the constant values.
2328 */
2329
2330 #define AUDIO_PARAMETER_VALUE_ON "on"
2331 #define AUDIO_PARAMETER_VALUE_OFF "off"
2332 #define AUDIO_PARAMETER_VALUE_TRUE "true"
2333 #define AUDIO_PARAMETER_VALUE_FALSE "false"
2334
2335 /**
2336 * audio device parameters
2337 */
2338
2339 /* Used to enable or disable BT SCO */
2340 #define AUDIO_PARAMETER_KEY_BT_SCO "BT_SCO"
2341
2342 /* BT SCO Noise Reduction + Echo Cancellation parameters */
2343 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
2344
2345 /* Used to enable or disable BT A2DP */
2346 #define AUDIO_PARAMETER_KEY_BT_A2DP_SUSPENDED "A2dpSuspended"
2347
2348 /* Used to enable or disable BT LE */
2349 #define AUDIO_PARAMETER_KEY_BT_LE_SUSPENDED "LeAudioSuspended"
2350
2351 /* Get a new HW synchronization source identifier.
2352 * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
2353 * or no HW sync is available. */
2354 #define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
2355
2356 /* Screen state */
2357 #define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
2358
2359 /* User's preferred audio language setting (in ISO 639-2/T three-letter string code)
2360 * used to select a specific language presentation for next generation audio codecs. */
2361 #define AUDIO_PARAMETER_KEY_AUDIO_LANGUAGE_PREFERRED "audio_language_preferred"
2362
2363 /* Set to "true" when the AudioOutputDescriptor is closing.
2364 * This notification is used by A2DP HAL.
2365 * TODO(b/73175392) unify with exiting in the AIDL interface.
2366 */
2367 #define AUDIO_PARAMETER_KEY_CLOSING "closing"
2368
2369 /* Set to "1" on AudioFlinger preExit() for the thread.
2370 * This notification is used by the remote submix and A2DP HAL.
2371 * TODO(b/73175392) unify with closing in the AIDL interface.
2372 */
2373 #define AUDIO_PARAMETER_KEY_EXITING "exiting"
2374
2375 /**
2376 * audio stream parameters
2377 */
2378
2379 #define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */
2380 #define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */
2381 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */
2382 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */
2383 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */
2384 #define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
2385
2386 /* Request the presentation id to be decoded by a next gen audio decoder */
2387 #define AUDIO_PARAMETER_STREAM_PRESENTATION_ID "presentation_id" /* int32_t */
2388
2389 /* Request the program id to be decoded by a next gen audio decoder */
2390 #define AUDIO_PARAMETER_STREAM_PROGRAM_ID "program_id" /* int32_t */
2391
2392 #define AUDIO_PARAMETER_DEVICE_CONNECT "connect" /* audio_devices_t */
2393 #define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect" /* audio_devices_t */
2394
2395 /* Enable mono audio playback if 1, else should be 0. */
2396 #define AUDIO_PARAMETER_MONO_OUTPUT "mono_output"
2397
2398 /* Set the HW synchronization source for an output stream. */
2399 #define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
2400
2401 /* Query supported formats. The response is a '|' separated list of strings from
2402 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
2403 #define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
2404 /* Query supported channel masks. The response is a '|' separated list of strings from
2405 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
2406 #define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
2407 /* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
2408 * "sup_sampling_rates=44100|48000" */
2409 #define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
2410
2411 #define AUDIO_PARAMETER_VALUE_LIST_SEPARATOR "|"
2412
2413 /* Reconfigure offloaded A2DP codec */
2414 #define AUDIO_PARAMETER_RECONFIG_A2DP "reconfigA2dp"
2415 /* Query if HwModule supports reconfiguration of offloaded A2DP codec */
2416 #define AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED "isReconfigA2dpSupported"
2417
2418 /* Query if HwModule supports variable Bluetooth latency control */
2419 #define AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED "isBtVariableLatencySupported"
2420
2421 /**
2422 * For querying device supported encapsulation capabilities. All returned values are integer,
2423 * which are bit fields composed from using encapsulation capability values as position bits.
2424 * Encapsulation capability values are defined in audio_encapsulation_mode_t and
2425 * audio_encapsulation_metadata_type_t. For instance, if the supported encapsulation mode is
2426 * AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM, the returned value is
2427 * "supEncapsulationModes=1 << AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM".
2428 * When querying device supported encapsulation capabilities, the key should use device type
2429 * and address so that it is able to identify the device. The device will be a key. The device
2430 * type will be the value of key AUDIO_PARAMETER_STREAM_ROUTING.
2431 */
2432 #define AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES "supEncapsulationModes"
2433 #define AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES "supEncapsulationMetadataTypes"
2434
2435 /* Query additional delay in millisecond on each output device. */
2436 #define AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY "additional_output_device_delay"
2437 #define AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY "max_additional_output_device_delay"
2438
2439 /**
2440 * audio codec parameters
2441 */
2442
2443 #define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
2444 #define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
2445 #define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
2446 #define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
2447 #define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
2448 #define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
2449 #define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
2450 #define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
2451 #define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels"
2452 #define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling"
2453 #define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples"
2454 #define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples"
2455
2456
2457 #endif // ANDROID_AUDIO_CORE_H
2458