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