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