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