xref: /aosp_15_r20/system/media/audio/include/system/audio.h (revision b9df5ad1c9ac98a7fefaac271a55f7ae3db05414)
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     audio_channel_mask_t  speaker_layout_channel_mask; /* represents physical speaker layout. */
635 };
636 
637 /* extension for audio port configuration structure when the audio port is a
638  * sub mix */
639 struct audio_port_config_mix_ext {
640     audio_module_handle_t hw_module;    /* module the stream is attached to */
641     audio_io_handle_t handle;           /* I/O handle of the input/output stream */
642     union {
643         //TODO: change use case for output streams: use strategy and mixer attributes
644         audio_stream_type_t stream;
645         audio_source_t      source;
646     } usecase;
647 };
648 
649 /* extension for audio port configuration structure when the audio port is an
650  * audio session */
651 struct audio_port_config_session_ext {
652     audio_session_t   session; /* audio session */
653 };
654 
655 typedef enum {
656     AUDIO_PORT_ROLE_NONE = 0,
657     AUDIO_PORT_ROLE_SOURCE = 1,
658     AUDIO_PORT_ROLE_SINK = 2,
659 } audio_port_role_t;
660 
661 typedef enum {
662     AUDIO_PORT_TYPE_NONE = 0,
663     AUDIO_PORT_TYPE_DEVICE = 1,
664     AUDIO_PORT_TYPE_MIX = 2,
665     AUDIO_PORT_TYPE_SESSION = 3,
666 } audio_port_type_t;
667 
668 enum {
669     AUDIO_PORT_CONFIG_SAMPLE_RATE  = 0x1u,
670     AUDIO_PORT_CONFIG_CHANNEL_MASK = 0x2u,
671     AUDIO_PORT_CONFIG_FORMAT       = 0x4u,
672     AUDIO_PORT_CONFIG_GAIN         = 0x8u,
673     AUDIO_PORT_CONFIG_FLAGS        = 0x10u,
674     AUDIO_PORT_CONFIG_ALL          = AUDIO_PORT_CONFIG_SAMPLE_RATE |
675                                      AUDIO_PORT_CONFIG_CHANNEL_MASK |
676                                      AUDIO_PORT_CONFIG_FORMAT |
677                                      AUDIO_PORT_CONFIG_GAIN |
678                                      AUDIO_PORT_CONFIG_FLAGS
679 };
680 
681 typedef enum {
682     AUDIO_LATENCY_LOW = 0,
683     AUDIO_LATENCY_NORMAL = 1,
684 } audio_mix_latency_class_t;
685 
686 /* audio port configuration structure used to specify a particular configuration of
687  * an audio port */
688 struct audio_port_config {
689     audio_port_handle_t      id;           /* port unique ID */
690     audio_port_role_t        role;         /* sink or source */
691     audio_port_type_t        type;         /* device, mix ... */
692     unsigned int             config_mask;  /* e.g AUDIO_PORT_CONFIG_ALL */
693     unsigned int             sample_rate;  /* sampling rate in Hz */
694     audio_channel_mask_t     channel_mask; /* channel mask if applicable */
695     audio_format_t           format;       /* format if applicable */
696     struct audio_gain_config gain;         /* gain to apply if applicable */
697     union audio_io_flags     flags;        /* HW_AV_SYNC, DIRECT, ... */
698     union {
699         struct audio_port_config_device_ext  device;  /* device specific info */
700         struct audio_port_config_mix_ext     mix;     /* mix specific info */
701         struct audio_port_config_session_ext session; /* session specific info */
702     } ext;
703 };
704 
705 
706 /* max number of sampling rates in audio port */
707 #define AUDIO_PORT_MAX_SAMPLING_RATES 32
708 /* max number of channel masks in audio port */
709 #define AUDIO_PORT_MAX_CHANNEL_MASKS 32
710 /* max number of audio formats in audio port */
711 #define AUDIO_PORT_MAX_FORMATS 32
712 /* max number of audio profiles in audio port. The audio profiles are used in
713  * `struct audio_port_v7`. When converting between `struct audio_port` and
714  * `struct audio_port_v7`, the number of audio profiles in `struct audio_port_v7`
715  * must be the same as the number of formats in `struct audio_port`. Therefore,
716  * the maximum number of audio profiles must be the same as the maximum number
717  * of formats. */
718 #define AUDIO_PORT_MAX_AUDIO_PROFILES AUDIO_PORT_MAX_FORMATS
719 /* max number of extra audio descriptors in audio port. */
720 #define AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS AUDIO_PORT_MAX_FORMATS
721 /* max number of gain controls in audio port */
722 #define AUDIO_PORT_MAX_GAINS 16
723 /* max bytes of extra audio descriptor */
724 #define EXTRA_AUDIO_DESCRIPTOR_SIZE 32
725 
726 /* extension for audio port structure when the audio port is a hardware device */
727 struct audio_port_device_ext {
728     audio_module_handle_t hw_module;    /* module the device is attached to */
729     audio_devices_t       type;         /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
730     char                  address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
731 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
732     uint32_t              encapsulation_modes;
733     uint32_t              encapsulation_metadata_types;
734 #endif
735 };
736 
737 /* extension for audio port structure when the audio port is a sub mix */
738 struct audio_port_mix_ext {
739     audio_module_handle_t     hw_module;     /* module the stream is attached to */
740     audio_io_handle_t         handle;        /* I/O handle of the input.output stream */
741     audio_mix_latency_class_t latency_class; /* latency class */
742     // other attributes: routing strategies
743 };
744 
745 /* extension for audio port structure when the audio port is an audio session */
746 struct audio_port_session_ext {
747     audio_session_t   session; /* audio session */
748 };
749 
750 struct audio_port {
751     audio_port_handle_t      id;                 /* port unique ID */
752     audio_port_role_t        role;               /* sink or source */
753     audio_port_type_t        type;               /* device, mix ... */
754     char                     name[AUDIO_PORT_MAX_NAME_LEN];
755     unsigned int             num_sample_rates;   /* number of sampling rates in following array */
756     unsigned int             sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
757     unsigned int             num_channel_masks;  /* number of channel masks in following array */
758     audio_channel_mask_t     channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
759     unsigned int             num_formats;        /* number of formats in following array */
760     audio_format_t           formats[AUDIO_PORT_MAX_FORMATS];
761     unsigned int             num_gains;          /* number of gains in following array */
762     struct audio_gain        gains[AUDIO_PORT_MAX_GAINS];
763     struct audio_port_config active_config;      /* current audio port configuration */
764     union {
765         struct audio_port_device_ext  device;
766         struct audio_port_mix_ext     mix;
767         struct audio_port_session_ext session;
768     } ext;
769 };
770 
771 typedef enum : int32_t {
772     AUDIO_STANDARD_NONE = 0,
773     AUDIO_STANDARD_EDID = 1,
774     AUDIO_STANDARD_SADB = 2,
775     AUDIO_STANDARD_VSADB = 3,
776 } audio_standard_t;
777 
778 /**
779  * Configuration described by hardware descriptor for a format that is unrecognized
780  * by the platform.
781  */
782 struct audio_extra_audio_descriptor {
783     audio_standard_t standard;
784     unsigned int descriptor_length;
785     uint8_t descriptor[EXTRA_AUDIO_DESCRIPTOR_SIZE];
786     audio_encapsulation_type_t encapsulation_type;
787 };
788 
789 /* configurations supported for a certain format */
790 struct audio_profile {
791     audio_format_t format;
792     unsigned int num_sample_rates;  /* number of sampling rates in following array */
793     unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
794     unsigned int num_channel_masks; /* number of channel masks in following array */
795     audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
796     audio_encapsulation_type_t encapsulation_type;
797 };
798 
799 struct audio_port_v7 {
800     audio_port_handle_t      id;                 /* port unique ID */
801     audio_port_role_t        role;               /* sink or source */
802     audio_port_type_t        type;               /* device, mix ... */
803     char                     name[AUDIO_PORT_MAX_NAME_LEN];
804     unsigned int             num_audio_profiles; /* number of audio profiles in the following
805                                                     array */
806     struct audio_profile     audio_profiles[AUDIO_PORT_MAX_AUDIO_PROFILES];
807     unsigned int             num_extra_audio_descriptors; /* number of extra audio descriptors in
808                                                              the following array */
809     struct audio_extra_audio_descriptor
810             extra_audio_descriptors[AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS];
811     unsigned int             num_gains;          /* number of gains in following array */
812     struct audio_gain        gains[AUDIO_PORT_MAX_GAINS];
813     struct audio_port_config active_config;      /* current audio port configuration */
814     union {
815         struct audio_port_device_ext  device;
816         struct audio_port_mix_ext     mix;
817         struct audio_port_session_ext session;
818     } ext;
819 };
820 
821 /* Return true when a given uint8_t array is a valid short audio descriptor. This function just
822  * does basic validation by checking if the first value is not zero.
823  */
audio_is_valid_short_audio_descriptor(const uint8_t * shortAudioDescriptor,size_t length)824 static inline bool audio_is_valid_short_audio_descriptor(const uint8_t *shortAudioDescriptor,
825                                                          size_t length) {
826     return length != 0 && *shortAudioDescriptor != 0;
827 }
828 
audio_populate_audio_port_v7(const struct audio_port * port,struct audio_port_v7 * portV7)829 static inline void audio_populate_audio_port_v7(
830         const struct audio_port *port, struct audio_port_v7 *portV7) {
831     portV7->id = port->id;
832     portV7->role = port->role;
833     portV7->type = port->type;
834     strncpy(portV7->name, port->name, AUDIO_PORT_MAX_NAME_LEN);
835     portV7->name[AUDIO_PORT_MAX_NAME_LEN-1] = '\0';
836     portV7->num_audio_profiles =
837             port->num_formats > AUDIO_PORT_MAX_AUDIO_PROFILES ?
838             AUDIO_PORT_MAX_AUDIO_PROFILES : port->num_formats;
839     for (size_t i = 0; i < portV7->num_audio_profiles; ++i) {
840         portV7->audio_profiles[i].format = port->formats[i];
841         portV7->audio_profiles[i].num_sample_rates = port->num_sample_rates;
842         memcpy(portV7->audio_profiles[i].sample_rates, port->sample_rates,
843                 port->num_sample_rates * sizeof(unsigned int));
844         portV7->audio_profiles[i].num_channel_masks = port->num_channel_masks;
845         memcpy(portV7->audio_profiles[i].channel_masks, port->channel_masks,
846                 port->num_channel_masks * sizeof(audio_channel_mask_t));
847     }
848     portV7->num_gains = port->num_gains;
849     memcpy(portV7->gains, port->gains, port->num_gains * sizeof(struct audio_gain));
850     memcpy(&portV7->active_config, &port->active_config, sizeof(struct audio_port_config));
851     memcpy(&portV7->ext, &port->ext, sizeof(port->ext));
852 }
853 
854 /* Populate the data in `struct audio_port` using data from `struct audio_port_v7`. As the
855  * `struct audio_port_v7` use audio profiles to describe its capabilities, it may contain more
856  * data for sample rates or channel masks than the data that can be held by `struct audio_port`.
857  * Return true if all the data from `struct audio_port_v7` are converted to `struct audio_port`.
858  * Otherwise, return false.
859  */
audio_populate_audio_port(const struct audio_port_v7 * portV7,struct audio_port * port)860 static inline bool audio_populate_audio_port(
861         const struct audio_port_v7 *portV7, struct audio_port *port) {
862     bool allDataConverted = true;
863     port->id = portV7->id;
864     port->role = portV7->role;
865     port->type = portV7->type;
866     strncpy(port->name, portV7->name, AUDIO_PORT_MAX_NAME_LEN);
867     port->name[AUDIO_PORT_MAX_NAME_LEN-1] = '\0';
868     port->num_formats =
869             portV7->num_audio_profiles > AUDIO_PORT_MAX_FORMATS ?
870             AUDIO_PORT_MAX_FORMATS : portV7->num_audio_profiles;
871     port->num_sample_rates = 0;
872     port->num_channel_masks = 0;
873     for (size_t i = 0; i < port->num_formats; ++i) {
874         port->formats[i] = portV7->audio_profiles[i].format;
875         for (size_t j = 0; j < portV7->audio_profiles[i].num_sample_rates; ++j) {
876             size_t k = 0;
877             for (; k < port->num_sample_rates; ++k) {
878                 if (port->sample_rates[k] == portV7->audio_profiles[i].sample_rates[j]) {
879                     break;
880                 }
881             }
882             if (k == port->num_sample_rates) {
883                 if (port->num_sample_rates >= AUDIO_PORT_MAX_SAMPLING_RATES) {
884                     allDataConverted = false;
885                     break;
886                 }
887                 port->sample_rates[port->num_sample_rates++] =
888                         portV7->audio_profiles[i].sample_rates[j];
889             }
890         }
891         for (size_t j = 0; j < portV7->audio_profiles[i].num_channel_masks; ++j) {
892             size_t k = 0;
893             for (; k < port->num_channel_masks; ++k) {
894                 if (port->channel_masks[k] == portV7->audio_profiles[i].channel_masks[j]) {
895                     break;
896                 }
897             }
898             if (k == port->num_channel_masks) {
899                 if (port->num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
900                     allDataConverted = false;
901                     break;
902                 }
903                 port->channel_masks[port->num_channel_masks++] =
904                         portV7->audio_profiles[i].channel_masks[j];
905             }
906         }
907     }
908     port->num_gains = portV7->num_gains;
909     memcpy(port->gains, portV7->gains, port->num_gains * sizeof(struct audio_gain));
910     memcpy(&port->active_config, &portV7->active_config, sizeof(struct audio_port_config));
911     memcpy(&port->ext, &portV7->ext, sizeof(port->ext));
912     return allDataConverted && portV7->num_extra_audio_descriptors == 0;
913 }
914 
audio_gain_config_are_equal(const struct audio_gain_config * lhs,const struct audio_gain_config * rhs)915 static inline bool audio_gain_config_are_equal(
916         const struct audio_gain_config *lhs, const struct audio_gain_config *rhs) {
917     if (lhs->mode != rhs->mode) return false;
918     if (lhs->mode & AUDIO_GAIN_MODE_JOINT) {
919         if (lhs->values[0] != rhs->values[0]) return false;
920     }
921     if (lhs->mode & (AUDIO_GAIN_MODE_CHANNELS | AUDIO_GAIN_MODE_RAMP)) {
922         if (lhs->channel_mask != rhs->channel_mask) return false;
923         for (int i = 0; i < __builtin_popcount(lhs->channel_mask); ++i) {
924             if (lhs->values[i] != rhs->values[i]) return false;
925         }
926     }
927     return lhs->ramp_duration_ms == rhs->ramp_duration_ms;
928 }
929 
audio_has_input_direction(audio_port_type_t type,audio_port_role_t role)930 static inline bool audio_has_input_direction(audio_port_type_t type, audio_port_role_t role) {
931     switch (type) {
932     case AUDIO_PORT_TYPE_DEVICE:
933         switch (role) {
934         case AUDIO_PORT_ROLE_SOURCE: return true;
935         case AUDIO_PORT_ROLE_SINK: return false;
936         default: return false;
937         }
938     case AUDIO_PORT_TYPE_MIX:
939         switch (role) {
940         case AUDIO_PORT_ROLE_SOURCE: return false;
941         case AUDIO_PORT_ROLE_SINK: return true;
942         default: return false;
943         }
944     default: return false;
945     }
946 }
947 
audio_port_config_has_input_direction(const struct audio_port_config * port_cfg)948 static inline bool audio_port_config_has_input_direction(const struct audio_port_config *port_cfg) {
949     return audio_has_input_direction(port_cfg->type, port_cfg->role);
950 }
951 
audio_port_configs_are_equal(const struct audio_port_config * lhs,const struct audio_port_config * rhs)952 static inline bool audio_port_configs_are_equal(
953         const struct audio_port_config *lhs, const struct audio_port_config *rhs) {
954     if (lhs->role != rhs->role || lhs->type != rhs->type) return false;
955     switch (lhs->type) {
956     case AUDIO_PORT_TYPE_NONE: break;
957     case AUDIO_PORT_TYPE_DEVICE:
958         if (lhs->ext.device.hw_module != rhs->ext.device.hw_module ||
959                 lhs->ext.device.type != rhs->ext.device.type ||
960                 lhs->ext.device.speaker_layout_channel_mask !=
961                         rhs->ext.device.speaker_layout_channel_mask ||
962                 strncmp(lhs->ext.device.address, rhs->ext.device.address,
963                         AUDIO_DEVICE_MAX_ADDRESS_LEN) != 0) {
964             return false;
965         }
966         break;
967     case AUDIO_PORT_TYPE_MIX:
968         if (lhs->ext.mix.hw_module != rhs->ext.mix.hw_module ||
969                 lhs->ext.mix.handle != rhs->ext.mix.handle) return false;
970         if (lhs->role == AUDIO_PORT_ROLE_SOURCE &&
971                 lhs->ext.mix.usecase.stream != rhs->ext.mix.usecase.stream) return false;
972         else if (lhs->role == AUDIO_PORT_ROLE_SINK &&
973                 lhs->ext.mix.usecase.source != rhs->ext.mix.usecase.source) return false;
974         break;
975     case AUDIO_PORT_TYPE_SESSION:
976         if (lhs->ext.session.session != rhs->ext.session.session) return false;
977         break;
978     default: return false;
979     }
980     return
981             lhs->config_mask == rhs->config_mask &&
982             ((lhs->config_mask & AUDIO_PORT_CONFIG_FLAGS) == 0 ||
983                     (audio_port_config_has_input_direction(lhs) ?
984                             lhs->flags.input == rhs->flags.input :
985                             lhs->flags.output == rhs->flags.output)) &&
986             ((lhs->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) == 0 ||
987                     lhs->sample_rate == rhs->sample_rate) &&
988             ((lhs->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) == 0 ||
989                     lhs->channel_mask == rhs->channel_mask) &&
990             ((lhs->config_mask & AUDIO_PORT_CONFIG_FORMAT) == 0 ||
991                     lhs->format == rhs->format) &&
992             ((lhs->config_mask & AUDIO_PORT_CONFIG_GAIN) == 0 ||
993                     audio_gain_config_are_equal(&lhs->gain, &rhs->gain));
994 }
995 
audio_gains_are_equal(const struct audio_gain * lhs,const struct audio_gain * rhs)996 static inline bool audio_gains_are_equal(const struct audio_gain* lhs, const struct audio_gain* rhs) {
997     return lhs->mode == rhs->mode &&
998             ((lhs->mode & AUDIO_GAIN_MODE_CHANNELS) != AUDIO_GAIN_MODE_CHANNELS ||
999                     lhs->channel_mask == rhs->channel_mask) &&
1000             lhs->min_value == rhs->min_value &&
1001             lhs->max_value == rhs->max_value &&
1002             lhs->default_value == rhs->default_value &&
1003             lhs->step_value == rhs->step_value &&
1004             lhs->min_ramp_ms == rhs->min_ramp_ms &&
1005             lhs->max_ramp_ms == rhs->max_ramp_ms;
1006 }
1007 
1008 // Define the helper functions of compare two audio_port/audio_port_v7 only in
1009 // C++ as it is easier to compare the device capabilities.
1010 #ifdef __cplusplus
1011 extern "C++" {
1012 #include <map>
1013 #include <set>
1014 #include <type_traits>
1015 #include <utility>
1016 #include <vector>
1017 
1018 namespace {
1019 
audio_gain_array_contains_all_elements_from(const struct audio_gain gains[],const size_t numGains,const struct audio_gain from[],size_t numFromGains)1020 static inline bool audio_gain_array_contains_all_elements_from(
1021         const struct audio_gain gains[], const size_t numGains,
1022         const struct audio_gain from[], size_t numFromGains) {
1023     for (size_t i = 0; i < numFromGains; ++i) {
1024         size_t j = 0;
1025         for (;j < numGains; ++j) {
1026             if (audio_gains_are_equal(&from[i], &gains[j])) {
1027                 break;
1028             }
1029         }
1030         if (j == numGains) {
1031             return false;
1032         }
1033     }
1034     return true;
1035 }
1036 
1037 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
1038                                     || std::is_same<T, struct audio_port_v7>::value, int> = 0>
audio_ports_base_are_equal(const T * lhs,const T * rhs)1039 static inline bool audio_ports_base_are_equal(const T* lhs, const T* rhs) {
1040     if (lhs->id != rhs->id || lhs->role != rhs->role || lhs->type != rhs->type ||
1041             strncmp(lhs->name, rhs->name, AUDIO_PORT_MAX_NAME_LEN) != 0 ||
1042             lhs->num_gains != rhs->num_gains) {
1043         return false;
1044     }
1045     switch (lhs->type) {
1046     case AUDIO_PORT_TYPE_NONE: break;
1047     case AUDIO_PORT_TYPE_DEVICE:
1048         if (
1049 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
1050                 lhs->ext.device.encapsulation_modes != rhs->ext.device.encapsulation_modes ||
1051                 lhs->ext.device.encapsulation_metadata_types !=
1052                         rhs->ext.device.encapsulation_metadata_types ||
1053 #endif
1054                 lhs->ext.device.hw_module != rhs->ext.device.hw_module ||
1055                 lhs->ext.device.type != rhs->ext.device.type ||
1056                 strncmp(lhs->ext.device.address, rhs->ext.device.address,
1057                         AUDIO_DEVICE_MAX_ADDRESS_LEN) != 0) {
1058             return false;
1059         }
1060         break;
1061     case AUDIO_PORT_TYPE_MIX:
1062         if (lhs->ext.mix.hw_module != rhs->ext.mix.hw_module ||
1063                 lhs->ext.mix.handle != rhs->ext.mix.handle ||
1064                 lhs->ext.mix.latency_class != rhs->ext.mix.latency_class) {
1065             return false;
1066         }
1067         break;
1068     case AUDIO_PORT_TYPE_SESSION:
1069         if (lhs->ext.session.session != rhs->ext.session.session) {
1070             return false;
1071         }
1072         break;
1073     default:
1074         return false;
1075     }
1076     if (!audio_gain_array_contains_all_elements_from(
1077             lhs->gains, lhs->num_gains, rhs->gains, rhs->num_gains) ||
1078             !audio_gain_array_contains_all_elements_from(
1079                     rhs->gains, rhs->num_gains, lhs->gains, lhs->num_gains)) {
1080         return false;
1081     }
1082     return audio_port_configs_are_equal(&lhs->active_config, &rhs->active_config);
1083 }
1084 
1085 template <typename T, std::enable_if_t<std::is_same<T, audio_format_t>::value
1086                                     || std::is_same<T, unsigned int>::value
1087                                     || 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)1088 static inline bool audio_capability_arrays_are_equal(
1089         const T lhs[], unsigned int lsize, const T rhs[], unsigned int rsize) {
1090     std::set<T> lhsSet(lhs, lhs + lsize);
1091     std::set<T> rhsSet(rhs, rhs + rsize);
1092     return lhsSet == rhsSet;
1093 }
1094 
1095 using AudioProfileMap =
1096         std::map<audio_format_t,
1097                  std::pair<std::set<unsigned int>, std::set<audio_channel_mask_t>>>;
getAudioProfileMap(const struct audio_profile profiles[],unsigned int size)1098 static inline AudioProfileMap getAudioProfileMap(
1099         const struct audio_profile profiles[], unsigned int size) {
1100     AudioProfileMap audioProfiles;
1101     for (size_t i = 0; i < size; ++i) {
1102         std::set<unsigned int> sampleRates(
1103                 profiles[i].sample_rates, profiles[i].sample_rates + profiles[i].num_sample_rates);
1104         std::set<audio_channel_mask_t> channelMasks(
1105                 profiles[i].channel_masks,
1106                 profiles[i].channel_masks + profiles[i].num_channel_masks);
1107         audioProfiles.emplace(profiles[i].format, std::make_pair(sampleRates, channelMasks));
1108     }
1109     return audioProfiles;
1110 }
1111 
audio_profile_arrays_are_equal(const struct audio_profile lhs[],unsigned int lsize,const struct audio_profile rhs[],unsigned int rsize)1112 static inline bool audio_profile_arrays_are_equal(
1113         const struct audio_profile lhs[], unsigned int lsize,
1114         const struct audio_profile rhs[], unsigned int rsize) {
1115     return getAudioProfileMap(lhs, lsize) == getAudioProfileMap(rhs, rsize);
1116 }
1117 
1118 using ExtraAudioDescriptorMap =std::map<audio_standard_t,
1119                                         std::map<audio_encapsulation_type_t,
1120                                                  std::set<std::vector<uint8_t>>>>;
1121 
getExtraAudioDescriptorMap(const struct audio_extra_audio_descriptor extraAudioDescriptors[],unsigned int numExtraAudioDescriptors)1122 static inline ExtraAudioDescriptorMap getExtraAudioDescriptorMap(
1123         const struct audio_extra_audio_descriptor extraAudioDescriptors[],
1124         unsigned int numExtraAudioDescriptors) {
1125     ExtraAudioDescriptorMap extraAudioDescriptorMap;
1126     for (unsigned int i = 0; i < numExtraAudioDescriptors; ++i) {
1127         extraAudioDescriptorMap[extraAudioDescriptors[i].standard]
1128                 [extraAudioDescriptors[i].encapsulation_type].insert(
1129                 std::vector<uint8_t>(
1130                         extraAudioDescriptors[i].descriptor,
1131                         extraAudioDescriptors[i].descriptor
1132                                 + extraAudioDescriptors[i].descriptor_length));
1133     }
1134     return extraAudioDescriptorMap;
1135 }
1136 
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)1137 static inline bool audio_extra_audio_descriptor_are_equal(
1138         const struct audio_extra_audio_descriptor lhs[], unsigned int lsize,
1139         const struct audio_extra_audio_descriptor rhs[], unsigned int rsize) {
1140     return getExtraAudioDescriptorMap(lhs, lsize) == getExtraAudioDescriptorMap(rhs, rsize);
1141 }
1142 
1143 } // namespace
1144 
audio_ports_are_equal(const struct audio_port * lhs,const struct audio_port * rhs)1145 static inline bool audio_ports_are_equal(
1146         const struct audio_port* lhs, const struct audio_port* rhs) {
1147     if (!audio_ports_base_are_equal(lhs, rhs)) {
1148         return false;
1149     }
1150     return audio_capability_arrays_are_equal(
1151             lhs->formats, lhs->num_formats, rhs->formats, rhs->num_formats) &&
1152             audio_capability_arrays_are_equal(
1153                     lhs->sample_rates, lhs->num_sample_rates,
1154                     rhs->sample_rates, rhs->num_sample_rates) &&
1155             audio_capability_arrays_are_equal(
1156                     lhs->channel_masks, lhs->num_channel_masks,
1157                     rhs->channel_masks, rhs->num_channel_masks);
1158 }
1159 
audio_ports_v7_are_equal(const struct audio_port_v7 * lhs,const struct audio_port_v7 * rhs)1160 static inline bool audio_ports_v7_are_equal(
1161         const struct audio_port_v7* lhs, const struct audio_port_v7* rhs) {
1162     if (!audio_ports_base_are_equal(lhs, rhs)) {
1163         return false;
1164     }
1165     return audio_profile_arrays_are_equal(
1166             lhs->audio_profiles, lhs->num_audio_profiles,
1167             rhs->audio_profiles, rhs->num_audio_profiles) &&
1168            audio_extra_audio_descriptor_are_equal(
1169                    lhs->extra_audio_descriptors, lhs->num_extra_audio_descriptors,
1170                    rhs->extra_audio_descriptors, rhs->num_extra_audio_descriptors);
1171 }
1172 
1173 } // extern "C++"
1174 #endif // __cplusplus
1175 
1176 /* An audio patch represents a connection between one or more source ports and
1177  * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
1178  * applications via framework APIs.
1179  * Each patch is identified by a handle at the interface used to create that patch. For instance,
1180  * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
1181  * This handle is unique to a given audio HAL hardware module.
1182  * But the same patch receives another system wide unique handle allocated by the framework.
1183  * This unique handle is used for all transactions inside the framework.
1184  */
1185 typedef int audio_patch_handle_t;
1186 
1187 #define AUDIO_PATCH_PORTS_MAX   16
1188 
1189 struct audio_patch {
1190     audio_patch_handle_t id;            /* patch unique ID */
1191     unsigned int      num_sources;      /* number of sources in following array */
1192     struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
1193     unsigned int      num_sinks;        /* number of sinks in following array */
1194     struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
1195 };
1196 
1197 
1198 
1199 /* a HW synchronization source returned by the audio HAL */
1200 typedef uint32_t audio_hw_sync_t;
1201 
1202 /* an invalid HW synchronization source indicating an error */
1203 #define AUDIO_HW_SYNC_INVALID 0
1204 
1205 /** @TODO export from .hal */
1206 typedef enum {
1207     NONE    = 0x0,
1208     /**
1209      * Only set this flag if applications can access the audio buffer memory
1210      * shared with the backend (usually DSP) _without_ security issue.
1211      *
1212      * Setting this flag also implies that Binder will allow passing the shared memory FD
1213      * to applications.
1214      *
1215      * That usually implies that the kernel will prevent any access to the
1216      * memory surrounding the audio buffer as it could lead to a security breach.
1217      *
1218      * For example, a "/dev/snd/" file descriptor generally is not shareable,
1219      * but an "anon_inode:dmabuffer" file descriptor is shareable.
1220      * See also Linux kernel's dma_buf.
1221      *
1222      * This flag is required to support AAudio exclusive mode:
1223      * See: https://source.android.com/devices/audio/aaudio
1224      */
1225     AUDIO_MMAP_APPLICATION_SHAREABLE    = 0x1,
1226 } audio_mmap_buffer_flag;
1227 
1228 /**
1229  * Mmap buffer descriptor returned by audio_stream->create_mmap_buffer().
1230  * note\ Used by streams opened in mmap mode.
1231  */
1232 struct audio_mmap_buffer_info {
1233     void*   shared_memory_address;  /**< base address of mmap memory buffer.
1234                                          For use by local process only */
1235     int32_t shared_memory_fd;       /**< FD for mmap memory buffer */
1236     int32_t buffer_size_frames;     /**< total buffer size in frames */
1237     int32_t burst_size_frames;      /**< transfer size granularity in frames */
1238     audio_mmap_buffer_flag flags;   /**< Attributes describing the buffer. */
1239 };
1240 
1241 /**
1242  * Mmap buffer read/write position returned by audio_stream->get_mmap_position().
1243  * note\ Used by streams opened in mmap mode.
1244  */
1245 struct audio_mmap_position {
1246     int64_t  time_nanoseconds; /**< timestamp in ns, CLOCK_MONOTONIC */
1247     int32_t  position_frames;  /**< increasing 32 bit frame count reset when stream->stop()
1248                                     is called */
1249 };
1250 
1251 /** Metadata of a playback track for an in stream. */
1252 typedef struct playback_track_metadata {
1253     audio_usage_t usage;
1254     audio_content_type_t content_type;
1255     float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
1256 } playback_track_metadata_t;
1257 
1258 /** Metadata of a record track for an out stream. */
1259 typedef struct record_track_metadata {
1260     audio_source_t source;
1261     float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
1262     // For record tracks originating from a software patch, the dest_device
1263     // fields provide information about the downstream device.
1264     audio_devices_t dest_device;
1265     char dest_device_address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
1266 } record_track_metadata_t;
1267 
1268 /** Metadata of a playback track for an in stream. */
1269 typedef struct playback_track_metadata_v7 {
1270     struct playback_track_metadata base;
1271     audio_channel_mask_t channel_mask;
1272     char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
1273 } playback_track_metadata_v7_t;
1274 
1275 /** Metadata of a record track for an out stream. */
1276 typedef struct record_track_metadata_v7 {
1277     struct record_track_metadata base;
1278     audio_channel_mask_t channel_mask;
1279     char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
1280 } record_track_metadata_v7_t;
1281 
playback_track_metadata_to_v7(struct playback_track_metadata_v7 * dst,const struct playback_track_metadata * src)1282 static inline void playback_track_metadata_to_v7(struct playback_track_metadata_v7 *dst,
1283                                                  const struct playback_track_metadata *src) {
1284     dst->base = *src;
1285     dst->channel_mask = AUDIO_CHANNEL_NONE;
1286     dst->tags[0] = '\0';
1287 }
1288 
playback_track_metadata_from_v7(struct playback_track_metadata * dst,const struct playback_track_metadata_v7 * src)1289 static inline void playback_track_metadata_from_v7(struct playback_track_metadata *dst,
1290                                                    const struct playback_track_metadata_v7 *src) {
1291     *dst = src->base;
1292 }
1293 
record_track_metadata_to_v7(struct record_track_metadata_v7 * dst,const struct record_track_metadata * src)1294 static inline void record_track_metadata_to_v7(struct record_track_metadata_v7 *dst,
1295                                                const struct record_track_metadata *src) {
1296     dst->base = *src;
1297     dst->channel_mask = AUDIO_CHANNEL_NONE;
1298     dst->tags[0] = '\0';
1299 }
1300 
record_track_metadata_from_v7(struct record_track_metadata * dst,const struct record_track_metadata_v7 * src)1301 static inline void record_track_metadata_from_v7(struct record_track_metadata *dst,
1302                                                  const struct record_track_metadata_v7 *src) {
1303     *dst = src->base;
1304 }
1305 
1306 /******************************
1307  *  Helper functions
1308  *****************************/
1309 
1310 // see also: std::binary_search
1311 // 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)1312 static inline bool audio_binary_search_device_array(const audio_devices_t audio_array[],
1313                                                     size_t left, size_t right,
1314                                                     audio_devices_t target)
1315 {
1316     if (right <= left || target < audio_array[left] || target > audio_array[right - 1]) {
1317         return false;
1318     }
1319 
1320     while (left < right) {
1321         const size_t mid = left + (right - left) / 2;
1322         if (audio_array[mid] == target) {
1323             return true;
1324         } else if (audio_array[mid] < target) {
1325             left = mid + 1;
1326         } else {
1327             right = mid;
1328         }
1329     }
1330     return false;
1331 }
1332 
audio_is_output_device(audio_devices_t device)1333 static inline bool audio_is_output_device(audio_devices_t device)
1334 {
1335     switch (device) {
1336     case AUDIO_DEVICE_OUT_SPEAKER_SAFE:
1337     case AUDIO_DEVICE_OUT_SPEAKER:
1338     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
1339     case AUDIO_DEVICE_OUT_WIRED_HEADSET:
1340     case AUDIO_DEVICE_OUT_USB_HEADSET:
1341     case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
1342     case AUDIO_DEVICE_OUT_EARPIECE:
1343     case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
1344     case AUDIO_DEVICE_OUT_TELEPHONY_TX:
1345         // Search the most common devices first as these devices are most likely
1346         // to be used. Put the most common devices in the order of the likelihood
1347         // of usage to get a quick return.
1348         return true;
1349     default:
1350         // Binary seach all devices if the device is not a most common device.
1351         return audio_binary_search_device_array(
1352                 AUDIO_DEVICE_OUT_ALL_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_CNT, device);
1353     }
1354 }
1355 
audio_is_input_device(audio_devices_t device)1356 static inline bool audio_is_input_device(audio_devices_t device)
1357 {
1358     switch (device) {
1359     case AUDIO_DEVICE_IN_BUILTIN_MIC:
1360     case AUDIO_DEVICE_IN_BACK_MIC:
1361     case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET:
1362     case AUDIO_DEVICE_IN_WIRED_HEADSET:
1363     case AUDIO_DEVICE_IN_USB_HEADSET:
1364     case AUDIO_DEVICE_IN_REMOTE_SUBMIX:
1365     case AUDIO_DEVICE_IN_TELEPHONY_RX:
1366         // Search the most common devices first as these devices are most likely
1367         // to be used. Put the most common devices in the order of the likelihood
1368         // of usage to get a quick return.
1369         return true;
1370     default:
1371         // Binary seach all devices if the device is not a most common device.
1372         return audio_binary_search_device_array(
1373                 AUDIO_DEVICE_IN_ALL_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_CNT, device);
1374     }
1375 }
1376 
1377 #ifdef __cplusplus
1378 // Some effects use `uint32_t` directly for device.
audio_is_input_device(uint32_t device)1379 static inline bool audio_is_input_device(uint32_t device) {
1380     return audio_is_input_device(static_cast<audio_devices_t>(device));
1381 }
1382 // This needs to be used when `audio_is_input_device` is passed
1383 // to an STL algorithm, as otherwise the compiler can't resolve
1384 // the overload at that point--the type of the container elements
1385 // doesn't appear in the predicate parameter type definition.
1386 const auto audio_call_is_input_device = [](auto x) { return audio_is_input_device(x); };
1387 #endif
1388 
1389 
1390 // TODO: this function expects a combination of audio device types as parameter. It should
1391 // 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)1392 static inline bool audio_is_output_devices(audio_devices_t device)
1393 {
1394     return (device & AUDIO_DEVICE_BIT_IN) == 0;
1395 }
1396 
audio_is_a2dp_in_device(audio_devices_t device)1397 static inline bool audio_is_a2dp_in_device(audio_devices_t device)
1398 {
1399     return device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
1400 }
1401 
audio_is_a2dp_out_device(audio_devices_t device)1402 static inline bool audio_is_a2dp_out_device(audio_devices_t device)
1403 {
1404     return audio_binary_search_device_array(
1405             AUDIO_DEVICE_OUT_ALL_A2DP_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_A2DP_CNT, device);
1406 }
1407 
1408 // Deprecated - use audio_is_a2dp_out_device() instead
audio_is_a2dp_device(audio_devices_t device)1409 static inline bool audio_is_a2dp_device(audio_devices_t device)
1410 {
1411     return audio_is_a2dp_out_device(device);
1412 }
1413 
audio_is_bluetooth_out_sco_device(audio_devices_t device)1414 static inline bool audio_is_bluetooth_out_sco_device(audio_devices_t device)
1415 {
1416     return audio_binary_search_device_array(
1417             AUDIO_DEVICE_OUT_ALL_SCO_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_SCO_CNT, device);
1418 }
1419 
audio_is_bluetooth_in_sco_device(audio_devices_t device)1420 static inline bool audio_is_bluetooth_in_sco_device(audio_devices_t device)
1421 {
1422     return audio_binary_search_device_array(
1423             AUDIO_DEVICE_IN_ALL_SCO_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_SCO_CNT, device);
1424 }
1425 
audio_is_bluetooth_sco_device(audio_devices_t device)1426 static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
1427 {
1428     return audio_is_bluetooth_out_sco_device(device) ||
1429             audio_is_bluetooth_in_sco_device(device);
1430 }
1431 
audio_is_hearing_aid_out_device(audio_devices_t device)1432 static inline bool audio_is_hearing_aid_out_device(audio_devices_t device)
1433 {
1434     return device == AUDIO_DEVICE_OUT_HEARING_AID;
1435 }
1436 
audio_is_usb_out_device(audio_devices_t device)1437 static inline bool audio_is_usb_out_device(audio_devices_t device)
1438 {
1439     return audio_binary_search_device_array(
1440             AUDIO_DEVICE_OUT_ALL_USB_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_USB_CNT, device);
1441 }
1442 
audio_is_usb_in_device(audio_devices_t device)1443 static inline bool audio_is_usb_in_device(audio_devices_t device)
1444 {
1445     return audio_binary_search_device_array(
1446             AUDIO_DEVICE_IN_ALL_USB_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_USB_CNT, device);
1447 }
1448 
1449 /* OBSOLETE - use audio_is_usb_out_device() instead. */
audio_is_usb_device(audio_devices_t device)1450 static inline bool audio_is_usb_device(audio_devices_t device)
1451 {
1452     return audio_is_usb_out_device(device);
1453 }
1454 
audio_is_remote_submix_device(audio_devices_t device)1455 static inline bool audio_is_remote_submix_device(audio_devices_t device)
1456 {
1457     return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
1458            device == AUDIO_DEVICE_IN_REMOTE_SUBMIX;
1459 }
1460 
audio_is_digital_out_device(audio_devices_t device)1461 static inline bool audio_is_digital_out_device(audio_devices_t device)
1462 {
1463     return audio_binary_search_device_array(
1464             AUDIO_DEVICE_OUT_ALL_DIGITAL_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_DIGITAL_CNT, device);
1465 }
1466 
audio_is_digital_in_device(audio_devices_t device)1467 static inline bool audio_is_digital_in_device(audio_devices_t device)
1468 {
1469     return audio_binary_search_device_array(
1470             AUDIO_DEVICE_IN_ALL_DIGITAL_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_DIGITAL_CNT, device);
1471 }
1472 
audio_device_is_digital(audio_devices_t device)1473 static inline bool audio_device_is_digital(audio_devices_t device) {
1474     return audio_is_digital_in_device(device) ||
1475            audio_is_digital_out_device(device);
1476 }
1477 
audio_is_ble_out_device(audio_devices_t device)1478 static inline bool audio_is_ble_out_device(audio_devices_t device)
1479 {
1480     return audio_binary_search_device_array(
1481             AUDIO_DEVICE_OUT_ALL_BLE_ARRAY, 0 /*left*/, AUDIO_DEVICE_OUT_BLE_CNT, device);
1482 }
1483 
audio_is_ble_unicast_device(audio_devices_t device)1484 static inline bool audio_is_ble_unicast_device(audio_devices_t device)
1485 {
1486     return audio_binary_search_device_array(
1487             AUDIO_DEVICE_OUT_BLE_UNICAST_ARRAY, 0 /*left*/,
1488             AUDIO_DEVICE_OUT_BLE_UNICAST_CNT, device);
1489 }
1490 
audio_is_ble_broadcast_device(audio_devices_t device)1491 static inline bool audio_is_ble_broadcast_device(audio_devices_t device)
1492 {
1493     return audio_binary_search_device_array(
1494             AUDIO_DEVICE_OUT_BLE_BROADCAST_ARRAY, 0 /*left*/,
1495             AUDIO_DEVICE_OUT_BLE_BROADCAST_CNT, device);
1496 }
1497 
audio_is_ble_in_device(audio_devices_t device)1498 static inline bool audio_is_ble_in_device(audio_devices_t device)
1499 {
1500     return audio_binary_search_device_array(
1501             AUDIO_DEVICE_IN_ALL_BLE_ARRAY, 0 /*left*/, AUDIO_DEVICE_IN_BLE_CNT, device);
1502 }
1503 
audio_is_ble_device(audio_devices_t device)1504 static inline bool audio_is_ble_device(audio_devices_t device) {
1505     return audio_is_ble_in_device(device) ||
1506            audio_is_ble_out_device(device);
1507 }
1508 
1509 /* Returns true if:
1510  *  representation is valid, and
1511  *  there is at least one channel bit set which _could_ correspond to an input channel, and
1512  *  there are no channel bits set which could _not_ correspond to an input channel.
1513  * Otherwise returns false.
1514  */
audio_is_input_channel(audio_channel_mask_t channel)1515 static inline bool audio_is_input_channel(audio_channel_mask_t channel)
1516 {
1517     uint32_t bits = audio_channel_mask_get_bits(channel);
1518     switch (audio_channel_mask_get_representation(channel)) {
1519     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1520         if (bits & ~AUDIO_CHANNEL_IN_ALL) {
1521             bits = 0;
1522         }
1523         FALLTHROUGH_INTENDED;
1524     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1525         return bits != 0;
1526     default:
1527         return false;
1528     }
1529 }
1530 
1531 /* Returns true if:
1532  *  representation is valid, and
1533  *  there is at least one channel bit set which _could_ correspond to an output channel, and
1534  *  there are no channel bits set which could _not_ correspond to an output channel.
1535  * Otherwise returns false.
1536  */
audio_is_output_channel(audio_channel_mask_t channel)1537 static inline CONSTEXPR bool audio_is_output_channel(audio_channel_mask_t channel)
1538 {
1539     uint32_t bits = audio_channel_mask_get_bits(channel);
1540     switch (audio_channel_mask_get_representation(channel)) {
1541     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1542         if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
1543             bits = 0;
1544         }
1545         FALLTHROUGH_INTENDED;
1546     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1547         return bits != 0;
1548     default:
1549         return false;
1550     }
1551 }
1552 
1553 /* Returns the number of channels from an input channel mask,
1554  * used in the context of audio input or recording.
1555  * If a channel bit is set which could _not_ correspond to an input channel,
1556  * it is excluded from the count.
1557  * Returns zero if the representation is invalid.
1558  */
audio_channel_count_from_in_mask(audio_channel_mask_t channel)1559 static inline CONSTEXPR uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
1560 {
1561     uint32_t bits = audio_channel_mask_get_bits(channel);
1562     switch (audio_channel_mask_get_representation(channel)) {
1563     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1564         // TODO: We can now merge with from_out_mask and remove anding
1565         bits &= AUDIO_CHANNEL_IN_ALL;
1566         FALLTHROUGH_INTENDED;
1567     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1568         return __builtin_popcount(bits);
1569     default:
1570         return 0;
1571     }
1572 }
1573 
1574 #ifdef __cplusplus
1575 // FIXME(b/169889714): buffer_config_t uses `uint32_t` for the mask.
1576 // A lot of effects code thus use `uint32_t` directly.
audio_channel_count_from_in_mask(uint32_t mask)1577 static inline CONSTEXPR uint32_t audio_channel_count_from_in_mask(uint32_t mask) {
1578     return audio_channel_count_from_in_mask(static_cast<audio_channel_mask_t>(mask));
1579 }
1580 #endif
1581 
1582 /* Returns the number of channels from an output channel mask,
1583  * used in the context of audio output or playback.
1584  * If a channel bit is set which could _not_ correspond to an output channel,
1585  * it is excluded from the count.
1586  * Returns zero if the representation is invalid.
1587  */
audio_channel_count_from_out_mask(audio_channel_mask_t channel)1588 static inline CONSTEXPR uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
1589 {
1590     uint32_t bits = audio_channel_mask_get_bits(channel);
1591     switch (audio_channel_mask_get_representation(channel)) {
1592     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1593         // TODO: We can now merge with from_in_mask and remove anding
1594         bits &= AUDIO_CHANNEL_OUT_ALL;
1595         FALLTHROUGH_INTENDED;
1596     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1597         return __builtin_popcount(bits);
1598     default:
1599         return 0;
1600     }
1601 }
1602 
1603 #ifdef __cplusplus
1604 // FIXME(b/169889714): buffer_config_t uses `uint32_t` for the mask.
1605 // A lot of effects code thus use `uint32_t` directly.
audio_channel_count_from_out_mask(uint32_t mask)1606 static inline CONSTEXPR uint32_t audio_channel_count_from_out_mask(uint32_t mask) {
1607     return audio_channel_count_from_out_mask(static_cast<audio_channel_mask_t>(mask));
1608 }
1609 #endif
1610 
1611 /* Derive a channel mask for index assignment from a channel count.
1612  * Returns the matching channel mask,
1613  * or AUDIO_CHANNEL_NONE if the channel count is zero,
1614  * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
1615  */
audio_channel_mask_for_index_assignment_from_count(uint32_t channel_count)1616 static inline CONSTEXPR audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
1617         uint32_t channel_count)
1618 {
1619     if (channel_count == 0) {
1620         return AUDIO_CHANNEL_NONE;
1621     }
1622     if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
1623         return AUDIO_CHANNEL_INVALID;
1624     }
1625     uint32_t bits = (1 << channel_count) - 1;
1626     return audio_channel_mask_from_representation_and_bits(
1627             AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
1628 }
1629 
1630 /* Derive an output channel mask for position assignment from a channel count.
1631  * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
1632  * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
1633  * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
1634  * for continuity with stereo.
1635  * Returns the matching channel mask,
1636  * or AUDIO_CHANNEL_NONE if the channel count is zero,
1637  * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1638  * configurations for which a default output channel mask is defined.
1639  */
audio_channel_out_mask_from_count(uint32_t channel_count)1640 static inline CONSTEXPR audio_channel_mask_t audio_channel_out_mask_from_count(
1641         uint32_t channel_count)
1642 {
1643     uint32_t bits = 0;
1644     switch (channel_count) {
1645     case 0:
1646         return AUDIO_CHANNEL_NONE;
1647     case 1:
1648         bits = AUDIO_CHANNEL_OUT_MONO;
1649         break;
1650     case 2:
1651         bits = AUDIO_CHANNEL_OUT_STEREO;
1652         break;
1653     case 3:
1654         bits = AUDIO_CHANNEL_OUT_2POINT1;
1655         break;
1656     case 4: // 4.0
1657         bits = AUDIO_CHANNEL_OUT_QUAD;
1658         break;
1659     case 5: // 5.0
1660         bits = AUDIO_CHANNEL_OUT_PENTA;
1661         break;
1662     case 6:
1663         bits = AUDIO_CHANNEL_OUT_5POINT1;
1664         break;
1665     case 7:
1666         bits = AUDIO_CHANNEL_OUT_6POINT1;
1667         break;
1668     case FCC_8:
1669         bits = AUDIO_CHANNEL_OUT_7POINT1;
1670         break;
1671     case 10:
1672         bits = AUDIO_CHANNEL_OUT_5POINT1POINT4;
1673         break;
1674     case FCC_12:
1675         bits = AUDIO_CHANNEL_OUT_7POINT1POINT4;
1676         break;
1677     case FCC_24:
1678         bits = AUDIO_CHANNEL_OUT_22POINT2;
1679         break;
1680     default:
1681         return AUDIO_CHANNEL_INVALID;
1682     }
1683     return audio_channel_mask_from_representation_and_bits(
1684             AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1685 }
1686 
1687 /* Derive a default input channel mask from a channel count.
1688  * Assumes a position mask for mono and stereo, or an index mask for channel counts > 2.
1689  * Returns the matching channel mask,
1690  * or AUDIO_CHANNEL_NONE if the channel count is zero,
1691  * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1692  * configurations for which a default input channel mask is defined.
1693  */
audio_channel_in_mask_from_count(uint32_t channel_count)1694 static inline CONSTEXPR audio_channel_mask_t audio_channel_in_mask_from_count(
1695         uint32_t channel_count)
1696 {
1697     uint32_t bits = 0;
1698     switch (channel_count) {
1699     case 0:
1700         return AUDIO_CHANNEL_NONE;
1701     case 1:
1702         bits = AUDIO_CHANNEL_IN_MONO;
1703         break;
1704     case 2:
1705         bits = AUDIO_CHANNEL_IN_STEREO;
1706         break;
1707     default:
1708         if (channel_count <= FCC_LIMIT) {
1709             return audio_channel_mask_for_index_assignment_from_count(channel_count);
1710         }
1711         return AUDIO_CHANNEL_INVALID;
1712     }
1713     return audio_channel_mask_from_representation_and_bits(
1714             AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1715 }
1716 
1717 /* Derive a default haptic channel mask from a channel count.
1718  */
haptic_channel_mask_from_count(uint32_t channel_count)1719 static inline audio_channel_mask_t haptic_channel_mask_from_count(uint32_t channel_count)
1720 {
1721     switch(channel_count) {
1722     case 0:
1723         return AUDIO_CHANNEL_NONE;
1724     case 1:
1725         return AUDIO_CHANNEL_OUT_HAPTIC_A;
1726     case 2:
1727         return AUDIO_CHANNEL_OUT_HAPTIC_AB;
1728     default:
1729         return AUDIO_CHANNEL_INVALID;
1730     }
1731 }
1732 
audio_channel_mask_in_to_out(audio_channel_mask_t in)1733 static inline audio_channel_mask_t audio_channel_mask_in_to_out(audio_channel_mask_t in)
1734 {
1735     switch (in) {
1736     case AUDIO_CHANNEL_IN_MONO:
1737         return AUDIO_CHANNEL_OUT_MONO;
1738     case AUDIO_CHANNEL_IN_STEREO:
1739         return AUDIO_CHANNEL_OUT_STEREO;
1740     case AUDIO_CHANNEL_IN_2POINT1:
1741         return AUDIO_CHANNEL_OUT_2POINT1;
1742     case AUDIO_CHANNEL_IN_QUAD:
1743         return AUDIO_CHANNEL_OUT_QUAD;
1744     case AUDIO_CHANNEL_IN_PENTA:
1745         return AUDIO_CHANNEL_OUT_PENTA;
1746     case AUDIO_CHANNEL_IN_5POINT1:
1747         return AUDIO_CHANNEL_OUT_5POINT1;
1748     case AUDIO_CHANNEL_IN_3POINT1POINT2:
1749         return AUDIO_CHANNEL_OUT_3POINT1POINT2;
1750     case AUDIO_CHANNEL_IN_3POINT0POINT2:
1751         return AUDIO_CHANNEL_OUT_3POINT0POINT2;
1752     case AUDIO_CHANNEL_IN_2POINT1POINT2:
1753         return AUDIO_CHANNEL_OUT_2POINT1POINT2;
1754     case AUDIO_CHANNEL_IN_2POINT0POINT2:
1755         return AUDIO_CHANNEL_OUT_2POINT0POINT2;
1756     default:
1757         return AUDIO_CHANNEL_INVALID;
1758     }
1759 }
1760 
audio_channel_mask_out_to_in(audio_channel_mask_t out)1761 static inline audio_channel_mask_t audio_channel_mask_out_to_in(audio_channel_mask_t out)
1762 {
1763     switch (out) {
1764     case AUDIO_CHANNEL_OUT_MONO:
1765         return AUDIO_CHANNEL_IN_MONO;
1766     case AUDIO_CHANNEL_OUT_STEREO:
1767         return AUDIO_CHANNEL_IN_STEREO;
1768     case AUDIO_CHANNEL_OUT_2POINT1:
1769         return AUDIO_CHANNEL_IN_2POINT1;
1770     case AUDIO_CHANNEL_OUT_QUAD:
1771         return AUDIO_CHANNEL_IN_QUAD;
1772     case AUDIO_CHANNEL_OUT_PENTA:
1773         return AUDIO_CHANNEL_IN_PENTA;
1774     case AUDIO_CHANNEL_OUT_5POINT1:
1775         return AUDIO_CHANNEL_IN_5POINT1;
1776     case AUDIO_CHANNEL_OUT_3POINT1POINT2:
1777         return AUDIO_CHANNEL_IN_3POINT1POINT2;
1778     case AUDIO_CHANNEL_OUT_3POINT0POINT2:
1779         return AUDIO_CHANNEL_IN_3POINT0POINT2;
1780     case AUDIO_CHANNEL_OUT_2POINT1POINT2:
1781         return AUDIO_CHANNEL_IN_2POINT1POINT2;
1782     case AUDIO_CHANNEL_OUT_2POINT0POINT2:
1783         return AUDIO_CHANNEL_IN_2POINT0POINT2;
1784     default:
1785         return AUDIO_CHANNEL_INVALID;
1786     }
1787 }
1788 
audio_channel_mask_out_to_in_index_mask(audio_channel_mask_t out)1789 static inline audio_channel_mask_t audio_channel_mask_out_to_in_index_mask(audio_channel_mask_t out)
1790 {
1791     return audio_channel_mask_for_index_assignment_from_count(
1792             audio_channel_count_from_out_mask(out));
1793 }
1794 
audio_channel_position_mask_is_out_canonical(audio_channel_mask_t channelMask)1795 static inline bool audio_channel_position_mask_is_out_canonical(audio_channel_mask_t channelMask)
1796 {
1797     if (audio_channel_mask_get_representation(channelMask)
1798             != AUDIO_CHANNEL_REPRESENTATION_POSITION) {
1799         return false;
1800     }
1801     const uint32_t audioChannelCount = audio_channel_count_from_out_mask(
1802             (audio_channel_mask_t)(channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL));
1803     const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(
1804             (audio_channel_mask_t)(channelMask & AUDIO_CHANNEL_HAPTIC_ALL));
1805     return channelMask == (audio_channel_mask_t)(
1806             audio_channel_out_mask_from_count(audioChannelCount) |
1807             haptic_channel_mask_from_count(hapticChannelCount));
1808 }
1809 
audio_is_valid_format(audio_format_t format)1810 static inline bool audio_is_valid_format(audio_format_t format)
1811 {
1812     switch (format & AUDIO_FORMAT_MAIN_MASK) {
1813     case AUDIO_FORMAT_PCM:
1814         switch (format) {
1815         case AUDIO_FORMAT_PCM_16_BIT:
1816         case AUDIO_FORMAT_PCM_8_BIT:
1817         case AUDIO_FORMAT_PCM_32_BIT:
1818         case AUDIO_FORMAT_PCM_8_24_BIT:
1819         case AUDIO_FORMAT_PCM_FLOAT:
1820         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1821             return true;
1822         default:
1823             return false;
1824         }
1825         /* not reached */
1826     case AUDIO_FORMAT_MP3:
1827     case AUDIO_FORMAT_AMR_NB:
1828     case AUDIO_FORMAT_AMR_WB:
1829         return true;
1830     case AUDIO_FORMAT_AAC:
1831         switch (format) {
1832         case AUDIO_FORMAT_AAC:
1833         case AUDIO_FORMAT_AAC_MAIN:
1834         case AUDIO_FORMAT_AAC_LC:
1835         case AUDIO_FORMAT_AAC_SSR:
1836         case AUDIO_FORMAT_AAC_LTP:
1837         case AUDIO_FORMAT_AAC_HE_V1:
1838         case AUDIO_FORMAT_AAC_SCALABLE:
1839         case AUDIO_FORMAT_AAC_ERLC:
1840         case AUDIO_FORMAT_AAC_LD:
1841         case AUDIO_FORMAT_AAC_HE_V2:
1842         case AUDIO_FORMAT_AAC_ELD:
1843         case AUDIO_FORMAT_AAC_XHE:
1844             return true;
1845         default:
1846             return false;
1847         }
1848         /* not reached */
1849     case AUDIO_FORMAT_HE_AAC_V1:
1850     case AUDIO_FORMAT_HE_AAC_V2:
1851     case AUDIO_FORMAT_VORBIS:
1852     case AUDIO_FORMAT_OPUS:
1853     case AUDIO_FORMAT_AC3:
1854         return true;
1855     case AUDIO_FORMAT_E_AC3:
1856         switch (format) {
1857         case AUDIO_FORMAT_E_AC3:
1858         case AUDIO_FORMAT_E_AC3_JOC:
1859             return true;
1860         default:
1861             return false;
1862         }
1863         /* not reached */
1864     case AUDIO_FORMAT_DTS:
1865     case AUDIO_FORMAT_DTS_HD:
1866     case AUDIO_FORMAT_IEC60958:
1867     case AUDIO_FORMAT_IEC61937:
1868     case AUDIO_FORMAT_DOLBY_TRUEHD:
1869     case AUDIO_FORMAT_EVRC:
1870     case AUDIO_FORMAT_EVRCB:
1871     case AUDIO_FORMAT_EVRCWB:
1872     case AUDIO_FORMAT_EVRCNW:
1873     case AUDIO_FORMAT_AAC_ADIF:
1874     case AUDIO_FORMAT_WMA:
1875     case AUDIO_FORMAT_WMA_PRO:
1876     case AUDIO_FORMAT_AMR_WB_PLUS:
1877     case AUDIO_FORMAT_MP2:
1878     case AUDIO_FORMAT_QCELP:
1879     case AUDIO_FORMAT_DSD:
1880     case AUDIO_FORMAT_FLAC:
1881     case AUDIO_FORMAT_ALAC:
1882     case AUDIO_FORMAT_APE:
1883         return true;
1884     case AUDIO_FORMAT_AAC_ADTS:
1885         switch (format) {
1886         case AUDIO_FORMAT_AAC_ADTS:
1887         case AUDIO_FORMAT_AAC_ADTS_MAIN:
1888         case AUDIO_FORMAT_AAC_ADTS_LC:
1889         case AUDIO_FORMAT_AAC_ADTS_SSR:
1890         case AUDIO_FORMAT_AAC_ADTS_LTP:
1891         case AUDIO_FORMAT_AAC_ADTS_HE_V1:
1892         case AUDIO_FORMAT_AAC_ADTS_SCALABLE:
1893         case AUDIO_FORMAT_AAC_ADTS_ERLC:
1894         case AUDIO_FORMAT_AAC_ADTS_LD:
1895         case AUDIO_FORMAT_AAC_ADTS_HE_V2:
1896         case AUDIO_FORMAT_AAC_ADTS_ELD:
1897         case AUDIO_FORMAT_AAC_ADTS_XHE:
1898             return true;
1899         default:
1900             return false;
1901         }
1902         /* not reached */
1903     case AUDIO_FORMAT_SBC:
1904     case AUDIO_FORMAT_APTX:
1905     case AUDIO_FORMAT_APTX_HD:
1906         return true;
1907     case AUDIO_FORMAT_AC4:
1908         switch (format) {
1909         case AUDIO_FORMAT_AC4:
1910         case AUDIO_FORMAT_AC4_L4:
1911             return true;
1912         default:
1913             return false;
1914         }
1915         /* not reached */
1916     case AUDIO_FORMAT_LDAC:
1917         return true;
1918     case AUDIO_FORMAT_MAT:
1919         switch (format) {
1920         case AUDIO_FORMAT_MAT:
1921         case AUDIO_FORMAT_MAT_1_0:
1922         case AUDIO_FORMAT_MAT_2_0:
1923         case AUDIO_FORMAT_MAT_2_1:
1924             return true;
1925         default:
1926             return false;
1927         }
1928         /* not reached */
1929     case AUDIO_FORMAT_AAC_LATM:
1930         switch (format) {
1931         case AUDIO_FORMAT_AAC_LATM:
1932         case AUDIO_FORMAT_AAC_LATM_LC:
1933         case AUDIO_FORMAT_AAC_LATM_HE_V1:
1934         case AUDIO_FORMAT_AAC_LATM_HE_V2:
1935             return true;
1936         default:
1937             return false;
1938         }
1939         /* not reached */
1940     case AUDIO_FORMAT_CELT:
1941     case AUDIO_FORMAT_APTX_ADAPTIVE:
1942     case AUDIO_FORMAT_LHDC:
1943     case AUDIO_FORMAT_LHDC_LL:
1944     case AUDIO_FORMAT_APTX_TWSP:
1945     case AUDIO_FORMAT_LC3:
1946     case AUDIO_FORMAT_APTX_ADAPTIVE_QLEA:
1947     case AUDIO_FORMAT_APTX_ADAPTIVE_R4:
1948         return true;
1949     case AUDIO_FORMAT_MPEGH:
1950         switch (format) {
1951         case AUDIO_FORMAT_MPEGH_BL_L3:
1952         case AUDIO_FORMAT_MPEGH_BL_L4:
1953         case AUDIO_FORMAT_MPEGH_LC_L3:
1954         case AUDIO_FORMAT_MPEGH_LC_L4:
1955             return true;
1956         default:
1957             return false;
1958         }
1959         /* not reached */
1960     case AUDIO_FORMAT_DTS_UHD:
1961     case AUDIO_FORMAT_DRA:
1962     case AUDIO_FORMAT_DTS_HD_MA:
1963     case AUDIO_FORMAT_DTS_UHD_P2:
1964         return true;
1965     case AUDIO_FORMAT_IAMF:
1966         switch (format) {
1967         case AUDIO_FORMAT_IAMF_SIMPLE_OPUS:
1968         case AUDIO_FORMAT_IAMF_SIMPLE_AAC:
1969         case AUDIO_FORMAT_IAMF_SIMPLE_PCM:
1970         case AUDIO_FORMAT_IAMF_SIMPLE_FLAC:
1971         case AUDIO_FORMAT_IAMF_BASE_OPUS:
1972         case AUDIO_FORMAT_IAMF_BASE_AAC:
1973         case AUDIO_FORMAT_IAMF_BASE_PCM:
1974         case AUDIO_FORMAT_IAMF_BASE_FLAC:
1975         case AUDIO_FORMAT_IAMF_BASE_ENHANCED_OPUS:
1976         case AUDIO_FORMAT_IAMF_BASE_ENHANCED_AAC:
1977         case AUDIO_FORMAT_IAMF_BASE_ENHANCED_PCM:
1978         case AUDIO_FORMAT_IAMF_BASE_ENHANCED_FLAC:
1979                 return true;
1980         default:
1981                 return false;
1982         }
1983         /* not reached */
1984     default:
1985         return false;
1986     }
1987 }
1988 
audio_is_iec61937_compatible(audio_format_t format)1989 static inline bool audio_is_iec61937_compatible(audio_format_t format)
1990 {
1991     switch (format) {
1992     case AUDIO_FORMAT_AC3:         // IEC 61937-3:2017
1993     case AUDIO_FORMAT_AC4:         // IEC 61937-14:2017
1994     case AUDIO_FORMAT_AC4_L4:      // IEC 61937-14:2017
1995     case AUDIO_FORMAT_E_AC3:       // IEC 61937-3:2017
1996     case AUDIO_FORMAT_E_AC3_JOC:   // IEC 61937-3:2017
1997     case AUDIO_FORMAT_MAT:         // IEC 61937-9:2017
1998     case AUDIO_FORMAT_MAT_1_0:     // IEC 61937-9:2017
1999     case AUDIO_FORMAT_MAT_2_0:     // IEC 61937-9:2017
2000     case AUDIO_FORMAT_MAT_2_1:     // IEC 61937-9:2017
2001     case AUDIO_FORMAT_MPEGH_BL_L3: // IEC 61937-13:2018
2002     case AUDIO_FORMAT_MPEGH_BL_L4: // IEC 61937-13:2018
2003     case AUDIO_FORMAT_MPEGH_LC_L3: // IEC 61937-13:2018
2004     case AUDIO_FORMAT_MPEGH_LC_L4: // IEC 61937-13:2018
2005         return true;
2006     default:
2007         return false;
2008     }
2009 }
2010 
2011 /**
2012  * Extract the primary format, eg. PCM, AC3, etc.
2013  */
audio_get_main_format(audio_format_t format)2014 static inline audio_format_t audio_get_main_format(audio_format_t format)
2015 {
2016     return (audio_format_t)(format & AUDIO_FORMAT_MAIN_MASK);
2017 }
2018 
2019 /**
2020  * Is the data plain PCM samples that can be scaled and mixed?
2021  */
audio_is_linear_pcm(audio_format_t format)2022 static inline bool audio_is_linear_pcm(audio_format_t format)
2023 {
2024     return (audio_get_main_format(format) == AUDIO_FORMAT_PCM);
2025 }
2026 
2027 /**
2028  * For this format, is the number of PCM audio frames directly proportional
2029  * to the number of data bytes?
2030  *
2031  * In other words, is the format transported as PCM audio samples,
2032  * but not necessarily scalable or mixable.
2033  * This returns true for real PCM, but also for AUDIO_FORMAT_IEC61937,
2034  * which is transported as 16 bit PCM audio, but where the encoded data
2035  * cannot be mixed or scaled.
2036  */
audio_has_proportional_frames(audio_format_t format)2037 static inline bool audio_has_proportional_frames(audio_format_t format)
2038 {
2039     audio_format_t mainFormat = audio_get_main_format(format);
2040     return (mainFormat == AUDIO_FORMAT_PCM
2041             || mainFormat == AUDIO_FORMAT_IEC61937);
2042 }
2043 
audio_bytes_per_sample(audio_format_t format)2044 static inline size_t audio_bytes_per_sample(audio_format_t format)
2045 {
2046     size_t size = 0;
2047 
2048     switch (format) {
2049     case AUDIO_FORMAT_PCM_32_BIT:
2050     case AUDIO_FORMAT_PCM_8_24_BIT:
2051         size = sizeof(int32_t);
2052         break;
2053     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
2054         size = sizeof(uint8_t) * 3;
2055         break;
2056     case AUDIO_FORMAT_PCM_16_BIT:
2057     case AUDIO_FORMAT_IEC61937:
2058         size = sizeof(int16_t);
2059         break;
2060     case AUDIO_FORMAT_PCM_8_BIT:
2061         size = sizeof(uint8_t);
2062         break;
2063     case AUDIO_FORMAT_PCM_FLOAT:
2064         size = sizeof(float);
2065         break;
2066     default:
2067         break;
2068     }
2069     return size;
2070 }
2071 
audio_bytes_per_frame(uint32_t channel_count,audio_format_t format)2072 static inline size_t audio_bytes_per_frame(uint32_t channel_count, audio_format_t format)
2073 {
2074     if (audio_has_proportional_frames(format)) {
2075         // cannot overflow for reasonable channel_count
2076         return channel_count * audio_bytes_per_sample(format);
2077     } else {
2078         // compressed formats have a frame size of 1 by convention.
2079         return sizeof(uint8_t);
2080     }
2081 }
2082 
2083 /* converts device address to string sent to audio HAL via set_parameters */
audio_device_address_to_parameter(audio_devices_t device,const char * address)2084 static inline char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
2085 {
2086     const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_source_address=");
2087     char param[kSize];
2088 
2089     if (device == AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
2090         snprintf(param, kSize, "%s=%s", "a2dp_source_address", address);
2091     } else if (audio_is_a2dp_out_device(device)) {
2092         snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
2093     } else if (audio_is_remote_submix_device(device)) {
2094         snprintf(param, kSize, "%s=%s", "mix", address);
2095     } else {
2096         snprintf(param, kSize, "%s", address);
2097     }
2098     return strdup(param);
2099 }
2100 
audio_is_valid_audio_source(audio_source_t audioSource)2101 static inline bool audio_is_valid_audio_source(audio_source_t audioSource)
2102 {
2103     switch (audioSource) {
2104     case AUDIO_SOURCE_MIC:
2105     case AUDIO_SOURCE_VOICE_UPLINK:
2106     case AUDIO_SOURCE_VOICE_DOWNLINK:
2107     case AUDIO_SOURCE_VOICE_CALL:
2108     case AUDIO_SOURCE_CAMCORDER:
2109     case AUDIO_SOURCE_VOICE_RECOGNITION:
2110     case AUDIO_SOURCE_VOICE_COMMUNICATION:
2111     case AUDIO_SOURCE_REMOTE_SUBMIX:
2112     case AUDIO_SOURCE_UNPROCESSED:
2113     case AUDIO_SOURCE_VOICE_PERFORMANCE:
2114     case AUDIO_SOURCE_ECHO_REFERENCE:
2115     case AUDIO_SOURCE_FM_TUNER:
2116 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2117     case AUDIO_SOURCE_HOTWORD:
2118 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
2119     case AUDIO_SOURCE_ULTRASOUND:
2120         return true;
2121     default:
2122         return false;
2123     }
2124 }
2125 
2126 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2127 
audio_port_config_has_hw_av_sync(const struct audio_port_config * port_cfg)2128 static inline bool audio_port_config_has_hw_av_sync(const struct audio_port_config *port_cfg) {
2129     if (!(port_cfg->config_mask & AUDIO_PORT_CONFIG_FLAGS)) {
2130         return false;
2131     }
2132     return audio_port_config_has_input_direction(port_cfg) ?
2133             port_cfg->flags.input & AUDIO_INPUT_FLAG_HW_AV_SYNC
2134             : port_cfg->flags.output & AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
2135 }
2136 
audio_patch_has_hw_av_sync(const struct audio_patch * patch)2137 static inline bool audio_patch_has_hw_av_sync(const struct audio_patch *patch) {
2138     for (unsigned int i = 0; i < patch->num_sources; ++i) {
2139         if (audio_port_config_has_hw_av_sync(&patch->sources[i])) return true;
2140     }
2141     for (unsigned int i = 0; i < patch->num_sinks; ++i) {
2142         if (audio_port_config_has_hw_av_sync(&patch->sinks[i])) return true;
2143     }
2144     return false;
2145 }
2146 
audio_patch_is_valid(const struct audio_patch * patch)2147 static inline bool audio_patch_is_valid(const struct audio_patch *patch) {
2148     // Note that patch can have no sinks.
2149     return patch->num_sources != 0 && patch->num_sources <= AUDIO_PATCH_PORTS_MAX &&
2150             patch->num_sinks <= AUDIO_PATCH_PORTS_MAX;
2151 }
2152 
2153 // Note that when checking for equality the order of ports must match.
2154 // 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)2155 static inline bool audio_patches_are_equal(
2156         const struct audio_patch *lhs, const struct audio_patch *rhs) {
2157     if (!audio_patch_is_valid(lhs) || !audio_patch_is_valid(rhs)) return false;
2158     if (lhs->num_sources != rhs->num_sources || lhs->num_sinks != rhs->num_sinks) return false;
2159     for (unsigned int i = 0; i < lhs->num_sources; ++i) {
2160         if (!audio_port_configs_are_equal(&lhs->sources[i], &rhs->sources[i])) return false;
2161     }
2162     for (unsigned int i = 0; i < lhs->num_sinks; ++i) {
2163         if (!audio_port_configs_are_equal(&lhs->sinks[i], &rhs->sinks[i])) return false;
2164     }
2165     return true;
2166 }
2167 
2168 #endif
2169 
2170 // Unique effect ID (can be generated from the following site:
2171 //  http://www.itu.int/ITU-T/asn1/uuid.html)
2172 // This struct is used for effects identification and in soundtrigger.
2173 typedef struct audio_uuid_s {
2174     uint32_t timeLow;
2175     uint16_t timeMid;
2176     uint16_t timeHiAndVersion;
2177     uint16_t clockSeq;
2178     uint8_t node[6];
2179 } audio_uuid_t;
2180 
2181 /* A 3D point which could be used to represent geometric location
2182  * or orientation of a microphone.
2183  */
2184 struct audio_microphone_coordinate {
2185     float x;
2186     float y;
2187     float z;
2188 };
2189 
2190 /* An number to indicate which group the microphone locate. Main body is
2191  * usually group 0. Developer could use this value to group the microphones
2192  * that locate on the same peripheral or attachments.
2193  */
2194 typedef int audio_microphone_group_t;
2195 
2196 /* the maximum length for the microphone id */
2197 #define AUDIO_MICROPHONE_ID_MAX_LEN 32
2198 /* max number of frequency responses in a frequency response table */
2199 #define AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES 256
2200 /* max number of microphone */
2201 #define AUDIO_MICROPHONE_MAX_COUNT 32
2202 /* the value of unknown spl */
2203 #define AUDIO_MICROPHONE_SPL_UNKNOWN -FLT_MAX
2204 /* the value of unknown sensitivity */
2205 #define AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN -FLT_MAX
2206 /* the value of unknown coordinate */
2207 #define AUDIO_MICROPHONE_COORDINATE_UNKNOWN -FLT_MAX
2208 /* the value used as address when the address of bottom microphone is empty */
2209 #define AUDIO_BOTTOM_MICROPHONE_ADDRESS "bottom"
2210 /* the value used as address when the address of back microphone is empty */
2211 #define AUDIO_BACK_MICROPHONE_ADDRESS "back"
2212 
2213 struct audio_microphone_characteristic_t {
2214     char                               device_id[AUDIO_MICROPHONE_ID_MAX_LEN];
2215     audio_port_handle_t                id;
2216     audio_devices_t                    device;
2217     char                               address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
2218     audio_microphone_channel_mapping_t channel_mapping[AUDIO_CHANNEL_COUNT_MAX];
2219     audio_microphone_location_t        location;
2220     audio_microphone_group_t           group;
2221     unsigned int                       index_in_the_group;
2222     float                              sensitivity;
2223     float                              max_spl;
2224     float                              min_spl;
2225     audio_microphone_directionality_t  directionality;
2226     unsigned int                       num_frequency_responses;
2227     float frequency_responses[2][AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES];
2228     struct audio_microphone_coordinate geometric_location;
2229     struct audio_microphone_coordinate orientation;
2230 };
2231 
2232 typedef enum {
2233 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2234     AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT = -1, // (framework only) for speed <1.0 will truncate
2235                                                 // frames, for speed > 1.0 will repeat frames
2236     AUDIO_TIMESTRETCH_FALLBACK_DEFAULT    = 0,  // (framework only) system determines behavior
2237 #endif
2238     /* Set all processed frames to zero. */
2239     AUDIO_TIMESTRETCH_FALLBACK_MUTE       = HAL_AUDIO_TIMESTRETCH_FALLBACK_MUTE,
2240     /* Stop processing and indicate an error. */
2241     AUDIO_TIMESTRETCH_FALLBACK_FAIL       = HAL_AUDIO_TIMESTRETCH_FALLBACK_FAIL,
2242 } audio_timestretch_fallback_mode_t;
2243 
2244 // AUDIO_TIMESTRETCH_SPEED_MIN and AUDIO_TIMESTRETCH_SPEED_MAX define the min and max time stretch
2245 // speeds supported by the system. These are enforced by the system and values outside this range
2246 // will result in a runtime error.
2247 // Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than
2248 // the ones specified here
2249 // AUDIO_TIMESTRETCH_SPEED_MIN_DELTA is the minimum absolute speed difference that might trigger a
2250 // parameter update
2251 #define AUDIO_TIMESTRETCH_SPEED_MIN    0.01f
2252 #define AUDIO_TIMESTRETCH_SPEED_MAX    20.0f
2253 #define AUDIO_TIMESTRETCH_SPEED_NORMAL 1.0f
2254 #define AUDIO_TIMESTRETCH_SPEED_MIN_DELTA 0.0001f
2255 
2256 // AUDIO_TIMESTRETCH_PITCH_MIN and AUDIO_TIMESTRETCH_PITCH_MAX define the min and max time stretch
2257 // pitch shifting supported by the system. These are not enforced by the system and values
2258 // outside this range might result in a pitch different than the one requested.
2259 // Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than
2260 // the ones specified here.
2261 // AUDIO_TIMESTRETCH_PITCH_MIN_DELTA is the minimum absolute pitch difference that might trigger a
2262 // parameter update
2263 #define AUDIO_TIMESTRETCH_PITCH_MIN    0.25f
2264 #define AUDIO_TIMESTRETCH_PITCH_MAX    4.0f
2265 #define AUDIO_TIMESTRETCH_PITCH_NORMAL 1.0f
2266 #define AUDIO_TIMESTRETCH_PITCH_MIN_DELTA 0.0001f
2267 
2268 //Limits for AUDIO_TIMESTRETCH_STRETCH_VOICE mode
2269 #define TIMESTRETCH_SONIC_SPEED_MIN 0.1f
2270 #define TIMESTRETCH_SONIC_SPEED_MAX 6.0f
2271 
2272 struct audio_playback_rate {
2273     float mSpeed;
2274     float mPitch;
2275     audio_timestretch_stretch_mode_t  mStretchMode;
2276     audio_timestretch_fallback_mode_t mFallbackMode;
2277 };
2278 
2279 typedef struct audio_playback_rate audio_playback_rate_t;
2280 
2281 static const audio_playback_rate_t AUDIO_PLAYBACK_RATE_INITIALIZER = {
2282     /* .mSpeed = */ AUDIO_TIMESTRETCH_SPEED_NORMAL,
2283     /* .mPitch = */ AUDIO_TIMESTRETCH_PITCH_NORMAL,
2284     /* .mStretchMode = */ AUDIO_TIMESTRETCH_STRETCH_DEFAULT,
2285     /* .mFallbackMode = */ AUDIO_TIMESTRETCH_FALLBACK_FAIL
2286 };
2287 
2288 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
2289 typedef enum {
2290     AUDIO_DIRECT_NOT_SUPPORTED = 0x0u,
2291     AUDIO_DIRECT_OFFLOAD_SUPPORTED = 0x1u,
2292     AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED = 0x2u,
2293     // TODO(b/211628732): may need an enum for direct pcm
2294     AUDIO_DIRECT_BITSTREAM_SUPPORTED = 0x4u,
2295 } audio_direct_mode_t;
2296 
2297 // TODO: Deprecate audio_offload_mode_t and use audio_direct_mode_t instead.
2298 typedef enum {
2299     AUDIO_OFFLOAD_NOT_SUPPORTED = AUDIO_DIRECT_NOT_SUPPORTED,
2300     AUDIO_OFFLOAD_SUPPORTED = AUDIO_DIRECT_OFFLOAD_SUPPORTED,
2301     AUDIO_OFFLOAD_GAPLESS_SUPPORTED = AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED
2302 } audio_offload_mode_t;
2303 #endif // AUDIO_NO_SYSTEM_DECLARATIONS
2304 
2305 typedef enum : int32_t {
2306     AUDIO_MIXER_BEHAVIOR_INVALID = -1,
2307     AUDIO_MIXER_BEHAVIOR_DEFAULT = 0,
2308     AUDIO_MIXER_BEHAVIOR_BIT_PERFECT = 1,
2309 } audio_mixer_behavior_t;
2310 
2311 struct audio_mixer_attributes {
2312     audio_config_base_t config;
2313     audio_mixer_behavior_t mixer_behavior;
2314 };
2315 
2316 typedef struct audio_mixer_attributes audio_mixer_attributes_t;
2317 
2318 static const audio_mixer_attributes_t AUDIO_MIXER_ATTRIBUTES_INITIALIZER = {
2319     /* .config */ {
2320         /* .sample_rate*/ 0,
2321         /* .channel_mask*/ AUDIO_CHANNEL_NONE,
2322         /* .format */ AUDIO_FORMAT_DEFAULT,
2323     },
2324     /* .mixer_behavior */ AUDIO_MIXER_BEHAVIOR_DEFAULT,
2325 };
2326 
audio_output_flags_from_mixer_behavior(audio_mixer_behavior_t mixerBehavior)2327 static inline audio_output_flags_t audio_output_flags_from_mixer_behavior(
2328         audio_mixer_behavior_t mixerBehavior) {
2329     switch (mixerBehavior) {
2330         case AUDIO_MIXER_BEHAVIOR_BIT_PERFECT:
2331             return AUDIO_OUTPUT_FLAG_BIT_PERFECT;
2332         case AUDIO_MIXER_BEHAVIOR_DEFAULT:
2333         default:
2334             return AUDIO_OUTPUT_FLAG_NONE;
2335     }
2336 }
2337 
audio_channel_mask_to_string(audio_channel_mask_t channel_mask)2338 inline const char* audio_channel_mask_to_string(audio_channel_mask_t channel_mask) {
2339     if (audio_is_input_channel(channel_mask)) {
2340         return audio_channel_in_mask_to_string(channel_mask);
2341     } else if (audio_is_output_channel(channel_mask)) {
2342         return audio_channel_out_mask_to_string(channel_mask);
2343     } else {
2344         return audio_channel_index_mask_to_string(channel_mask);
2345     }
2346 }
2347 
audio_output_is_mixed_output_flags(audio_output_flags_t flags)2348 inline CONSTEXPR bool audio_output_is_mixed_output_flags(audio_output_flags_t flags) {
2349     return (flags & (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
2350             AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO |
2351             AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD |
2352             AUDIO_OUTPUT_FLAG_BIT_PERFECT)) == 0;
2353 }
2354 
2355 __END_DECLS
2356 
2357 /**
2358  * List of known audio HAL modules. This is the base name of the audio HAL
2359  * library composed of the "audio." prefix, one of the base names below and
2360  * a suffix specific to the device.
2361  * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
2362  *
2363  * "bluetooth" is a newer implementation, combining functionality
2364  * from the legacy "a2dp" and "hearing_aid" modules,
2365  * and adding support for BT LE devices.
2366  *
2367  * The same module names are used in audio policy configuration files.
2368  */
2369 
2370 #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
2371 #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
2372 #define AUDIO_HARDWARE_MODULE_ID_BLUETOOTH "bluetooth"
2373 #define AUDIO_HARDWARE_MODULE_ID_USB "usb"
2374 #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
2375 #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
2376 #define AUDIO_HARDWARE_MODULE_ID_STUB "stub"
2377 #define AUDIO_HARDWARE_MODULE_ID_HEARING_AID "hearing_aid"
2378 #define AUDIO_HARDWARE_MODULE_ID_MSD "msd"
2379 
2380 /**
2381  * Multi-Stream Decoder (MSD) HAL service name. MSD HAL is used to mix
2382  * encoded streams together with PCM streams, producing re-encoded
2383  * streams or PCM streams.
2384  *
2385  * The service must register itself using this name, and audioserver
2386  * tries to instantiate a device factory using this name as well.
2387  * Note that the HIDL implementation library file name *must* have the
2388  * suffix "msd" in order to be picked up by HIDL that is:
2389  *
2390  *   [email protected]
2391  */
2392 #define AUDIO_HAL_SERVICE_NAME_MSD "msd"
2393 
2394 /**
2395  * Parameter definitions.
2396  * Note that in the framework code it's recommended to use AudioParameter.h
2397  * instead of these preprocessor defines, and for sure avoid just copying
2398  * the constant values.
2399  */
2400 
2401 #define AUDIO_PARAMETER_VALUE_ON "on"
2402 #define AUDIO_PARAMETER_VALUE_OFF "off"
2403 #define AUDIO_PARAMETER_VALUE_TRUE "true"
2404 #define AUDIO_PARAMETER_VALUE_FALSE "false"
2405 
2406 /**
2407  *  audio device parameters
2408  */
2409 
2410 /* Used to enable or disable BT SCO */
2411 #define AUDIO_PARAMETER_KEY_BT_SCO "BT_SCO"
2412 
2413 /* BT SCO Noise Reduction + Echo Cancellation parameters */
2414 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
2415 
2416 /* Used to enable or disable BT A2DP */
2417 #define AUDIO_PARAMETER_KEY_BT_A2DP_SUSPENDED "A2dpSuspended"
2418 
2419 /* Used to enable or disable BT LE */
2420 #define AUDIO_PARAMETER_KEY_BT_LE_SUSPENDED "LeAudioSuspended"
2421 
2422 /* Get a new HW synchronization source identifier.
2423  * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
2424  * or no HW sync is available. */
2425 #define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
2426 
2427 /* Screen state */
2428 #define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
2429 
2430 /* User's preferred audio language setting (in ISO 639-2/T three-letter string code)
2431  * used to select a specific language presentation for next generation audio codecs. */
2432 #define AUDIO_PARAMETER_KEY_AUDIO_LANGUAGE_PREFERRED "audio_language_preferred"
2433 
2434 /* Set to "true" when the AudioOutputDescriptor is closing.
2435  * This notification is used by A2DP HAL.
2436  * TODO(b/73175392) unify with exiting in the AIDL interface.
2437  */
2438 #define AUDIO_PARAMETER_KEY_CLOSING "closing"
2439 
2440 /* Set to "1" on AudioFlinger preExit() for the thread.
2441  * This notification is used by the remote submix and A2DP HAL.
2442  * TODO(b/73175392) unify with closing in the AIDL interface.
2443  */
2444 #define AUDIO_PARAMETER_KEY_EXITING "exiting"
2445 
2446 /**
2447  *  audio stream parameters
2448  */
2449 
2450 #define AUDIO_PARAMETER_STREAM_ROUTING "routing"             /* audio_devices_t */
2451 #define AUDIO_PARAMETER_STREAM_FORMAT "format"               /* audio_format_t */
2452 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels"           /* audio_channel_mask_t */
2453 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"     /* size_t */
2454 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"   /* audio_source_t */
2455 #define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
2456 
2457 /* Request the presentation id to be decoded by a next gen audio decoder */
2458 #define AUDIO_PARAMETER_STREAM_PRESENTATION_ID "presentation_id" /* int32_t */
2459 
2460 /* Request the program id to be decoded by a next gen audio decoder */
2461 #define AUDIO_PARAMETER_STREAM_PROGRAM_ID "program_id"           /* int32_t */
2462 
2463 #define AUDIO_PARAMETER_DEVICE_CONNECT "connect"            /* audio_devices_t */
2464 #define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect"      /* audio_devices_t */
2465 
2466 /* Enable mono audio playback if 1, else should be 0. */
2467 #define AUDIO_PARAMETER_MONO_OUTPUT "mono_output"
2468 
2469 /* Set the HW synchronization source for an output stream. */
2470 #define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
2471 
2472 /* Query supported formats. The response is a '|' separated list of strings from
2473  * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
2474 #define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
2475 /* Query supported channel masks. The response is a '|' separated list of strings from
2476  * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
2477 #define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
2478 /* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
2479  * "sup_sampling_rates=44100|48000" */
2480 #define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
2481 
2482 #define AUDIO_PARAMETER_VALUE_LIST_SEPARATOR "|"
2483 
2484 /* Reconfigure offloaded A2DP codec */
2485 #define AUDIO_PARAMETER_RECONFIG_A2DP "reconfigA2dp"
2486 /* Query if HwModule supports reconfiguration of offloaded A2DP codec */
2487 #define AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED "isReconfigA2dpSupported"
2488 
2489 /* Query if HwModule supports variable Bluetooth latency control */
2490 #define AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED "isBtVariableLatencySupported"
2491 
2492 /* Reconfigure offloaded LE codec */
2493 #define AUDIO_PARAMETER_RECONFIG_LE "reconfigLe"
2494 /* Query if HwModule supports reconfiguration of offloaded LE codec */
2495 #define AUDIO_PARAMETER_LE_RECONFIG_SUPPORTED "isReconfigLeSupported"
2496 
2497 /**
2498  * For querying device supported encapsulation capabilities. All returned values are integer,
2499  * which are bit fields composed from using encapsulation capability values as position bits.
2500  * Encapsulation capability values are defined in audio_encapsulation_mode_t and
2501  * audio_encapsulation_metadata_type_t. For instance, if the supported encapsulation mode is
2502  * AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM, the returned value is
2503  * "supEncapsulationModes=1 << AUDIO_ENCAPSULATION_MODE_ELEMENTARY_STREAM".
2504  * When querying device supported encapsulation capabilities, the key should use device type
2505  * and address so that it is able to identify the device. The device will be a key. The device
2506  * type will be the value of key AUDIO_PARAMETER_STREAM_ROUTING.
2507  */
2508 #define AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES "supEncapsulationModes"
2509 #define AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES "supEncapsulationMetadataTypes"
2510 
2511 /* Query additional delay in millisecond on each output device. */
2512 #define AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY "additional_output_device_delay"
2513 #define AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY "max_additional_output_device_delay"
2514 
2515 /**
2516  * audio codec parameters
2517  */
2518 
2519 #define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
2520 #define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
2521 #define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
2522 #define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
2523 #define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
2524 #define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
2525 #define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
2526 #define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
2527 #define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL  "music_offload_num_channels"
2528 #define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING  "music_offload_down_sampling"
2529 #define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES  "delay_samples"
2530 #define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES  "padding_samples"
2531 
2532 /**
2533  * The maximum supported audio sample rate.
2534  *
2535  * note: The audio policy will use it as the max mixer sample rate for mixed
2536  * output and inputs.
2537  */
2538 #define SAMPLE_RATE_HZ_MAX 192000
2539 
2540 /**
2541  * The minimum supported audio sample rate.
2542  */
2543 #define SAMPLE_RATE_HZ_MIN 4000
2544 
2545 /**
2546  * The maximum possible audio sample rate as defined in IEC61937.
2547  * This definition is for a pre-check before asking the lower level service to
2548  * open an AAudio stream.
2549  *
2550  * note: HDMI supports up to 32 channels at 1536000 Hz.
2551  * note: This definition serve the purpose of parameter pre-check, real
2552  * validation happens in the audio policy.
2553  */
2554 #define SAMPLE_RATE_HZ_MAX_IEC610937 1600000
2555 
2556 /**
2557  * The minimum audio sample rate supported by AAudio stream.
2558  * This definition is for a pre-check before asking the lower level service to
2559  * open an AAudio stream.
2560  */
2561 #define SAMPLE_RATE_HZ_MIN_AAUDIO 8000
2562 
2563 /**
2564  * Minimum/maximum channel count supported by AAudio stream.
2565  */
2566 #define CHANNEL_COUNT_MIN_AAUDIO 1
2567 #define CHANNEL_COUNT_MAX_AAUDIO FCC_LIMIT
2568 
2569 #endif  // ANDROID_AUDIO_CORE_H
2570