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