OSDN Git Service

c3e7b18c3abcb611ee1be8ea94451b6bcedbb924
[android-x86/system-media.git] / audio / include / system / audio.h
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 <sys/cdefs.h>
25 #include <sys/types.h>
26
27 #include <cutils/bitops.h>
28
29 #include "audio-base.h"
30 #include "audio-base-utils.h"
31
32 /*
33  * Annotation to tell clang that we intend to fall through from one case to
34  * another in a switch (for c++ files). Sourced from android-base/macros.h.
35  */
36 #ifndef FALLTHROUGH_INTENDED
37 #ifdef __cplusplus
38 #define FALLTHROUGH_INTENDED [[fallthrough]]
39 #else
40 #define FALLTHROUGH_INTENDED
41 #endif // __cplusplus
42 #endif // FALLTHROUGH_INTENDED
43
44 __BEGIN_DECLS
45
46 /* The enums were moved here mostly from
47  * frameworks/base/include/media/AudioSystem.h
48  */
49
50 /* represents an invalid uid for tracks; the calling or client uid is often substituted. */
51 #define AUDIO_UID_INVALID ((uid_t)-1)
52
53 /* device address used to refer to the standard remote submix */
54 #define AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS "0"
55
56 /* AudioFlinger and AudioPolicy services use I/O handles to identify audio sources and sinks */
57 typedef int audio_io_handle_t;
58
59 typedef uint32_t audio_flags_mask_t;
60
61 /* Do not change these values without updating their counterparts
62  * in frameworks/base/media/java/android/media/AudioAttributes.java
63  */
64 enum {
65     AUDIO_FLAG_NONE                       = 0x0,
66     AUDIO_FLAG_AUDIBILITY_ENFORCED        = 0x1,
67     AUDIO_FLAG_SECURE                     = 0x2,
68     AUDIO_FLAG_SCO                        = 0x4,
69     AUDIO_FLAG_BEACON                     = 0x8,
70     AUDIO_FLAG_HW_AV_SYNC                 = 0x10,
71     AUDIO_FLAG_HW_HOTWORD                 = 0x20,
72     AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY = 0x40,
73     AUDIO_FLAG_BYPASS_MUTE                = 0x80,
74     AUDIO_FLAG_LOW_LATENCY                = 0x100,
75     AUDIO_FLAG_DEEP_BUFFER                = 0x200,
76     AUDIO_FLAG_NO_MEDIA_PROJECTION        = 0X400,
77     AUDIO_FLAG_MUTE_HAPTIC                = 0x800,
78     AUDIO_FLAG_NO_SYSTEM_CAPTURE          = 0X1000,
79 };
80
81 /* Audio attributes */
82 #define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
83 typedef struct {
84     audio_content_type_t content_type;
85     audio_usage_t        usage;
86     audio_source_t       source;
87     audio_flags_mask_t   flags;
88     char                 tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
89 } __attribute__((packed)) audio_attributes_t; // sent through Binder;
90
91 static const audio_attributes_t AUDIO_ATTRIBUTES_INITIALIZER = {
92     /* .content_type = */ AUDIO_CONTENT_TYPE_UNKNOWN,
93     /* .usage = */ AUDIO_USAGE_UNKNOWN,
94     /* .source = */ AUDIO_SOURCE_DEFAULT,
95     /* .flags = */ AUDIO_INPUT_FLAG_NONE,
96     /* .tags = */ ""
97 };
98
99 static inline audio_attributes_t attributes_initializer(audio_usage_t usage)
100 {
101     audio_attributes_t attributes = AUDIO_ATTRIBUTES_INITIALIZER;
102     attributes.usage = usage;
103     return attributes;
104 }
105
106 static inline void audio_flags_to_audio_output_flags(
107                                            const audio_flags_mask_t audio_flags,
108                                            audio_output_flags_t *flags)
109 {
110     if ((audio_flags & AUDIO_FLAG_HW_AV_SYNC) != 0) {
111         *flags = (audio_output_flags_t)(*flags |
112             AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_DIRECT);
113     }
114     if ((audio_flags & AUDIO_FLAG_LOW_LATENCY) != 0) {
115         *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_FAST);
116     }
117     // check deep buffer after flags have been modified above
118     if (*flags == AUDIO_OUTPUT_FLAG_NONE && (audio_flags & AUDIO_FLAG_DEEP_BUFFER) != 0) {
119         *flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
120     }
121 }
122
123
124 /* a unique ID allocated by AudioFlinger for use as an audio_io_handle_t, audio_session_t,
125  * effect ID (int), audio_module_handle_t, and audio_patch_handle_t.
126  * Audio port IDs (audio_port_handle_t) are allocated by AudioPolicy
127  * in a different namespace than AudioFlinger unique IDs.
128  */
129 typedef int audio_unique_id_t;
130
131 /* Possible uses for an audio_unique_id_t */
132 typedef enum {
133     AUDIO_UNIQUE_ID_USE_UNSPECIFIED = 0,
134     AUDIO_UNIQUE_ID_USE_SESSION = 1,    // for allocated sessions, not special AUDIO_SESSION_*
135     AUDIO_UNIQUE_ID_USE_MODULE = 2,
136     AUDIO_UNIQUE_ID_USE_EFFECT = 3,
137     AUDIO_UNIQUE_ID_USE_PATCH = 4,
138     AUDIO_UNIQUE_ID_USE_OUTPUT = 5,
139     AUDIO_UNIQUE_ID_USE_INPUT = 6,
140     AUDIO_UNIQUE_ID_USE_PLAYER = 7,
141     AUDIO_UNIQUE_ID_USE_MAX = 8,  // must be a power-of-two
142     AUDIO_UNIQUE_ID_USE_MASK = AUDIO_UNIQUE_ID_USE_MAX - 1
143 } audio_unique_id_use_t;
144
145 /* Return the use of an audio_unique_id_t */
146 static inline audio_unique_id_use_t audio_unique_id_get_use(audio_unique_id_t id)
147 {
148     return (audio_unique_id_use_t) (id & AUDIO_UNIQUE_ID_USE_MASK);
149 }
150
151 /* Reserved audio_unique_id_t values.  FIXME: not a complete list. */
152 #define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
153
154 /* A channel mask per se only defines the presence or absence of a channel, not the order.
155  * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
156  *
157  * audio_channel_mask_t is an opaque type and its internal layout should not
158  * be assumed as it may change in the future.
159  * Instead, always use the functions declared in this header to examine.
160  *
161  * These are the current representations:
162  *
163  *   AUDIO_CHANNEL_REPRESENTATION_POSITION
164  *     is a channel mask representation for position assignment.
165  *     Each low-order bit corresponds to the spatial position of a transducer (output),
166  *     or interpretation of channel (input).
167  *     The user of a channel mask needs to know the context of whether it is for output or input.
168  *     The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
169  *     It is not permitted for no bits to be set.
170  *
171  *   AUDIO_CHANNEL_REPRESENTATION_INDEX
172  *     is a channel mask representation for index assignment.
173  *     Each low-order bit corresponds to a selected channel.
174  *     There is no platform interpretation of the various bits.
175  *     There is no concept of output or input.
176  *     It is not permitted for no bits to be set.
177  *
178  * All other representations are reserved for future use.
179  *
180  * Warning: current representation distinguishes between input and output, but this will not the be
181  * case in future revisions of the platform. Wherever there is an ambiguity between input and output
182  * that is currently resolved by checking the channel mask, the implementer should look for ways to
183  * fix it with additional information outside of the mask.
184  */
185 typedef uint32_t audio_channel_mask_t;
186
187 /* log(2) of maximum number of representations, not part of public API */
188 #define AUDIO_CHANNEL_REPRESENTATION_LOG2   2
189
190 /* The return value is undefined if the channel mask is invalid. */
191 static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
192 {
193     return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
194 }
195
196 typedef uint32_t audio_channel_representation_t;
197
198 /* The return value is undefined if the channel mask is invalid. */
199 static inline audio_channel_representation_t audio_channel_mask_get_representation(
200         audio_channel_mask_t channel)
201 {
202     // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
203     return (audio_channel_representation_t)
204             ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
205 }
206
207 /* Returns true if the channel mask is valid,
208  * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
209  * This function is unable to determine whether a channel mask for position assignment
210  * is invalid because an output mask has an invalid output bit set,
211  * or because an input mask has an invalid input bit set.
212  * All other APIs that take a channel mask assume that it is valid.
213  */
214 static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
215 {
216     uint32_t bits = audio_channel_mask_get_bits(channel);
217     audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
218     switch (representation) {
219     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
220     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
221         break;
222     default:
223         bits = 0;
224         break;
225     }
226     return bits != 0;
227 }
228
229 /* Not part of public API */
230 static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
231         audio_channel_representation_t representation, uint32_t bits)
232 {
233     return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
234 }
235
236 /**
237  * Expresses the convention when stereo audio samples are stored interleaved
238  * in an array.  This should improve readability by allowing code to use
239  * symbolic indices instead of hard-coded [0] and [1].
240  *
241  * For multi-channel beyond stereo, the platform convention is that channels
242  * are interleaved in order from least significant channel mask bit to most
243  * significant channel mask bit, with unused bits skipped.  Any exceptions
244  * to this convention will be noted at the appropriate API.
245  */
246 enum {
247     AUDIO_INTERLEAVE_LEFT = 0,
248     AUDIO_INTERLEAVE_RIGHT = 1,
249 };
250
251 /* This enum is deprecated */
252 typedef enum {
253     AUDIO_IN_ACOUSTICS_NONE          = 0,
254     AUDIO_IN_ACOUSTICS_AGC_ENABLE    = 0x0001,
255     AUDIO_IN_ACOUSTICS_AGC_DISABLE   = 0,
256     AUDIO_IN_ACOUSTICS_NS_ENABLE     = 0x0002,
257     AUDIO_IN_ACOUSTICS_NS_DISABLE    = 0,
258     AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004,
259     AUDIO_IN_ACOUSTICS_TX_DISABLE    = 0,
260 } audio_in_acoustics_t;
261
262 typedef uint32_t audio_devices_t;
263 /**
264  * Stub audio output device. Used in policy configuration file on platforms without audio outputs.
265  * This alias value to AUDIO_DEVICE_OUT_DEFAULT is only used in the audio policy context.
266  */
267 #define AUDIO_DEVICE_OUT_STUB AUDIO_DEVICE_OUT_DEFAULT
268 /**
269  * Stub audio input device. Used in policy configuration file on platforms without audio inputs.
270  * This alias value to AUDIO_DEVICE_IN_DEFAULT is only used in the audio policy context.
271  */
272 #define AUDIO_DEVICE_IN_STUB AUDIO_DEVICE_IN_DEFAULT
273
274 /* Additional information about compressed streams offloaded to
275  * hardware playback
276  * The version and size fields must be initialized by the caller by using
277  * one of the constants defined here.
278  * Must be aligned to transmit as raw memory through Binder.
279  */
280 typedef struct {
281     uint16_t version;                   // version of the info structure
282     uint16_t size;                      // total size of the structure including version and size
283     uint32_t sample_rate;               // sample rate in Hz
284     audio_channel_mask_t channel_mask;  // channel mask
285     audio_format_t format;              // audio format
286     audio_stream_type_t stream_type;    // stream type
287     uint32_t bit_rate;                  // bit rate in bits per second
288     int64_t duration_us;                // duration in microseconds, -1 if unknown
289     bool has_video;                     // true if stream is tied to a video stream
290     bool is_streaming;                  // true if streaming, false if local playback
291     uint32_t bit_width;
292     uint32_t offload_buffer_size;       // offload fragment size
293     audio_usage_t usage;
294 } __attribute__((aligned(8))) audio_offload_info_t;
295
296 #define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \
297             ((((maj) & 0xff) << 8) | ((min) & 0xff))
298
299 #define AUDIO_OFFLOAD_INFO_VERSION_0_1 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 1)
300 #define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_1
301
302 static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
303     /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
304     /* .size = */ sizeof(audio_offload_info_t),
305     /* .sample_rate = */ 0,
306     /* .channel_mask = */ 0,
307     /* .format = */ AUDIO_FORMAT_DEFAULT,
308     /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
309     /* .bit_rate = */ 0,
310     /* .duration_us = */ 0,
311     /* .has_video = */ false,
312     /* .is_streaming = */ false,
313     /* .bit_width = */ 16,
314     /* .offload_buffer_size = */ 0,
315     /* .usage = */ AUDIO_USAGE_UNKNOWN
316 };
317
318 /* common audio stream configuration parameters
319  * You should memset() the entire structure to zero before use to
320  * ensure forward compatibility
321  * Must be aligned to transmit as raw memory through Binder.
322  */
323 struct __attribute__((aligned(8))) audio_config {
324     uint32_t sample_rate;
325     audio_channel_mask_t channel_mask;
326     audio_format_t  format;
327     audio_offload_info_t offload_info;
328     uint32_t frame_count;
329 };
330 typedef struct audio_config audio_config_t;
331
332 static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
333     /* .sample_rate = */ 0,
334     /* .channel_mask = */ AUDIO_CHANNEL_NONE,
335     /* .format = */ AUDIO_FORMAT_DEFAULT,
336     /* .offload_info = */ {
337         /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
338         /* .size = */ sizeof(audio_offload_info_t),
339         /* .sample_rate = */ 0,
340         /* .channel_mask = */ 0,
341         /* .format = */ AUDIO_FORMAT_DEFAULT,
342         /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
343         /* .bit_rate = */ 0,
344         /* .duration_us = */ 0,
345         /* .has_video = */ false,
346         /* .is_streaming = */ false,
347         /* .bit_width = */ 16,
348         /* .offload_buffer_size = */ 0,
349         /* .usage = */ AUDIO_USAGE_UNKNOWN
350     },
351     /* .frame_count = */ 0,
352 };
353
354 struct audio_config_base {
355     uint32_t sample_rate;
356     audio_channel_mask_t channel_mask;
357     audio_format_t  format;
358 };
359
360 typedef struct audio_config_base audio_config_base_t;
361
362 static const audio_config_base_t AUDIO_CONFIG_BASE_INITIALIZER = {
363     /* .sample_rate = */ 0,
364     /* .channel_mask = */ AUDIO_CHANNEL_NONE,
365     /* .format = */ AUDIO_FORMAT_DEFAULT
366 };
367
368 /* audio hw module handle functions or structures referencing a module */
369 typedef int audio_module_handle_t;
370
371 /******************************
372  *  Volume control
373  *****************************/
374
375 /** 3 dB headroom are allowed on float samples (3db = 10^(3/20) = 1.412538).
376 * See: https://developer.android.com/reference/android/media/AudioTrack.html#write(float[], int, int, int)
377 */
378 #define FLOAT_NOMINAL_RANGE_HEADROOM 1.412538
379
380 /* If the audio hardware supports gain control on some audio paths,
381  * the platform can expose them in the audio_policy.conf file. The audio HAL
382  * will then implement gain control functions that will use the following data
383  * structures. */
384
385 typedef uint32_t audio_gain_mode_t;
386
387
388 /* An audio_gain struct is a representation of a gain stage.
389  * A gain stage is always attached to an audio port. */
390 struct audio_gain  {
391     audio_gain_mode_t    mode;          /* e.g. AUDIO_GAIN_MODE_JOINT */
392     audio_channel_mask_t channel_mask;  /* channels which gain an be controlled.
393                                            N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
394     int                  min_value;     /* minimum gain value in millibels */
395     int                  max_value;     /* maximum gain value in millibels */
396     int                  default_value; /* default gain value in millibels */
397     unsigned int         step_value;    /* gain step in millibels */
398     unsigned int         min_ramp_ms;   /* minimum ramp duration in ms */
399     unsigned int         max_ramp_ms;   /* maximum ramp duration in ms */
400 };
401
402 /* The gain configuration structure is used to get or set the gain values of a
403  * given port */
404 struct audio_gain_config  {
405     int                  index;             /* index of the corresponding audio_gain in the
406                                                audio_port gains[] table */
407     audio_gain_mode_t    mode;              /* mode requested for this command */
408     audio_channel_mask_t channel_mask;      /* channels which gain value follows.
409                                                N/A in joint mode */
410
411     // note this "8" is not FCC_8, so it won't need to be changed for > 8 channels
412     int                  values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
413                                                for each channel ordered from LSb to MSb in
414                                                channel mask. The number of values is 1 in joint
415                                                mode or popcount(channel_mask) */
416     unsigned int         ramp_duration_ms; /* ramp duration in ms */
417 };
418
419 /******************************
420  *  Routing control
421  *****************************/
422
423 /* Types defined here are used to describe an audio source or sink at internal
424  * framework interfaces (audio policy, patch panel) or at the audio HAL.
425  * Sink and sources are grouped in a concept of “audio port” representing an
426  * audio end point at the edge of the system managed by the module exposing
427  * the interface. */
428
429 /* Each port has a unique ID or handle allocated by policy manager */
430 typedef int audio_port_handle_t;
431
432 /* the maximum length for the human-readable device name */
433 #define AUDIO_PORT_MAX_NAME_LEN 128
434
435 /* a union to store port configuration flags. Declared as a type so can be reused
436    in framework code */
437 union audio_io_flags {
438     audio_input_flags_t  input;
439     audio_output_flags_t output;
440 };
441
442 /* maximum audio device address length */
443 #define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
444
445 /* extension for audio port configuration structure when the audio port is a
446  * hardware device */
447 struct audio_port_config_device_ext {
448     audio_module_handle_t hw_module;                /* module the device is attached to */
449     audio_devices_t       type;                     /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
450     char                  address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
451 };
452
453 /* extension for audio port configuration structure when the audio port is a
454  * sub mix */
455 struct audio_port_config_mix_ext {
456     audio_module_handle_t hw_module;    /* module the stream is attached to */
457     audio_io_handle_t handle;           /* I/O handle of the input/output stream */
458     union {
459         //TODO: change use case for output streams: use strategy and mixer attributes
460         audio_stream_type_t stream;
461         audio_source_t      source;
462     } usecase;
463 };
464
465 /* extension for audio port configuration structure when the audio port is an
466  * audio session */
467 struct audio_port_config_session_ext {
468     audio_session_t   session; /* audio session */
469 };
470
471 /* audio port configuration structure used to specify a particular configuration of
472  * an audio port */
473 struct audio_port_config {
474     audio_port_handle_t      id;           /* port unique ID */
475     audio_port_role_t        role;         /* sink or source */
476     audio_port_type_t        type;         /* device, mix ... */
477     unsigned int             config_mask;  /* e.g AUDIO_PORT_CONFIG_ALL */
478     unsigned int             sample_rate;  /* sampling rate in Hz */
479     audio_channel_mask_t     channel_mask; /* channel mask if applicable */
480     audio_format_t           format;       /* format if applicable */
481     struct audio_gain_config gain;         /* gain to apply if applicable */
482 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
483     union audio_io_flags     flags;        /* framework only: HW_AV_SYNC, DIRECT, ... */
484 #endif
485     union {
486         struct audio_port_config_device_ext  device;  /* device specific info */
487         struct audio_port_config_mix_ext     mix;     /* mix specific info */
488         struct audio_port_config_session_ext session; /* session specific info */
489     } ext;
490 };
491
492
493 /* max number of sampling rates in audio port */
494 #define AUDIO_PORT_MAX_SAMPLING_RATES 32
495 /* max number of channel masks in audio port */
496 #define AUDIO_PORT_MAX_CHANNEL_MASKS 32
497 /* max number of audio formats in audio port */
498 #define AUDIO_PORT_MAX_FORMATS 32
499 /* max number of gain controls in audio port */
500 #define AUDIO_PORT_MAX_GAINS 16
501
502 /* extension for audio port structure when the audio port is a hardware device */
503 struct audio_port_device_ext {
504     audio_module_handle_t hw_module;    /* module the device is attached to */
505     audio_devices_t       type;         /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
506     char                  address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
507 };
508
509 /* extension for audio port structure when the audio port is a sub mix */
510 struct audio_port_mix_ext {
511     audio_module_handle_t     hw_module;     /* module the stream is attached to */
512     audio_io_handle_t         handle;        /* I/O handle of the input.output stream */
513     audio_mix_latency_class_t latency_class; /* latency class */
514     // other attributes: routing strategies
515 };
516
517 /* extension for audio port structure when the audio port is an audio session */
518 struct audio_port_session_ext {
519     audio_session_t   session; /* audio session */
520 };
521
522 struct audio_port {
523     audio_port_handle_t      id;                /* port unique ID */
524     audio_port_role_t        role;              /* sink or source */
525     audio_port_type_t        type;              /* device, mix ... */
526     char                     name[AUDIO_PORT_MAX_NAME_LEN];
527     unsigned int             num_sample_rates;  /* number of sampling rates in following array */
528     unsigned int             sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
529     unsigned int             num_channel_masks; /* number of channel masks in following array */
530     audio_channel_mask_t     channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
531     unsigned int             num_formats;       /* number of formats in following array */
532     audio_format_t           formats[AUDIO_PORT_MAX_FORMATS];
533     unsigned int             num_gains;         /* number of gains in following array */
534     struct audio_gain        gains[AUDIO_PORT_MAX_GAINS];
535     struct audio_port_config active_config;     /* current audio port configuration */
536     union {
537         struct audio_port_device_ext  device;
538         struct audio_port_mix_ext     mix;
539         struct audio_port_session_ext session;
540     } ext;
541 };
542
543 /* An audio patch represents a connection between one or more source ports and
544  * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
545  * applications via framework APIs.
546  * Each patch is identified by a handle at the interface used to create that patch. For instance,
547  * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
548  * This handle is unique to a given audio HAL hardware module.
549  * But the same patch receives another system wide unique handle allocated by the framework.
550  * This unique handle is used for all transactions inside the framework.
551  */
552 typedef int audio_patch_handle_t;
553
554 #define AUDIO_PATCH_PORTS_MAX   16
555
556 struct audio_patch {
557     audio_patch_handle_t id;            /* patch unique ID */
558     unsigned int      num_sources;      /* number of sources in following array */
559     struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
560     unsigned int      num_sinks;        /* number of sinks in following array */
561     struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
562 };
563
564
565
566 /* a HW synchronization source returned by the audio HAL */
567 typedef uint32_t audio_hw_sync_t;
568
569 /* an invalid HW synchronization source indicating an error */
570 #define AUDIO_HW_SYNC_INVALID 0
571
572 /** @TODO export from .hal */
573 typedef enum {
574     NONE    = 0x0,
575     /**
576      * Only set this flag if applications can access the audio buffer memory
577      * shared with the backend (usually DSP) _without_ security issue.
578      *
579      * Setting this flag also implies that Binder will allow passing the shared memory FD
580      * to applications.
581      *
582      * That usually implies that the kernel will prevent any access to the
583      * memory surrounding the audio buffer as it could lead to a security breach.
584      *
585      * For example, a "/dev/snd/" file descriptor generally is not shareable,
586      * but an "anon_inode:dmabuffer" file descriptor is shareable.
587      * See also Linux kernel's dma_buf.
588      *
589      * This flag is required to support AAudio exclusive mode:
590      * See: https://source.android.com/devices/audio/aaudio
591      */
592     AUDIO_MMAP_APPLICATION_SHAREABLE    = 0x1,
593 } audio_mmap_buffer_flag;
594
595 /**
596  * Mmap buffer descriptor returned by audio_stream->create_mmap_buffer().
597  * note\ Used by streams opened in mmap mode.
598  */
599 struct audio_mmap_buffer_info {
600     void*   shared_memory_address;  /**< base address of mmap memory buffer.
601                                          For use by local process only */
602     int32_t shared_memory_fd;       /**< FD for mmap memory buffer */
603     int32_t buffer_size_frames;     /**< total buffer size in frames */
604     int32_t burst_size_frames;      /**< transfer size granularity in frames */
605     audio_mmap_buffer_flag flags;   /**< Attributes describing the buffer. */
606 };
607
608 /**
609  * Mmap buffer read/write position returned by audio_stream->get_mmap_position().
610  * note\ Used by streams opened in mmap mode.
611  */
612 struct audio_mmap_position {
613     int64_t  time_nanoseconds; /**< timestamp in ns, CLOCK_MONOTONIC */
614     int32_t  position_frames;  /**< increasing 32 bit frame count reset when stream->stop()
615                                     is called */
616 };
617
618 /** Metadata of a playback track for an in stream. */
619 typedef struct playback_track_metadata {
620     audio_usage_t usage;
621     audio_content_type_t content_type;
622     float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
623 } playback_track_metadata_t;
624
625 /** Metadata of a record track for an out stream. */
626 typedef struct record_track_metadata {
627     audio_source_t source;
628     float gain; // Normalized linear volume. 0=silence, 1=0dbfs...
629     // For record tracks originating from a software patch, the dest_device
630     // fields provide information about the downstream device.
631     audio_devices_t dest_device;
632     char dest_device_address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
633 } record_track_metadata_t;
634
635
636 /******************************
637  *  Helper functions
638  *****************************/
639
640 static inline bool audio_is_output_device(audio_devices_t device)
641 {
642     if (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
643             (popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL) == 0))
644         return true;
645     else
646         return false;
647 }
648
649 static inline bool audio_is_input_device(audio_devices_t device)
650 {
651     if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
652         device &= ~AUDIO_DEVICE_BIT_IN;
653         if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_ALL) == 0))
654             return true;
655     }
656     return false;
657 }
658
659 static inline bool audio_is_output_devices(audio_devices_t device)
660 {
661     return (device & AUDIO_DEVICE_BIT_IN) == 0;
662 }
663
664 static inline bool audio_is_a2dp_in_device(audio_devices_t device)
665 {
666     if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
667         device &= ~AUDIO_DEVICE_BIT_IN;
668         if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP))
669             return true;
670     }
671     return false;
672 }
673
674 static inline bool audio_is_a2dp_out_device(audio_devices_t device)
675 {
676     if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP))
677         return true;
678     else
679         return false;
680 }
681
682 // Deprecated - use audio_is_a2dp_out_device() instead
683 static inline bool audio_is_a2dp_device(audio_devices_t device)
684 {
685     return audio_is_a2dp_out_device(device);
686 }
687
688 static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
689 {
690     if ((device & AUDIO_DEVICE_BIT_IN) == 0) {
691         if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0))
692             return true;
693     } else {
694         device &= ~AUDIO_DEVICE_BIT_IN;
695         if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0))
696             return true;
697     }
698
699     return false;
700 }
701
702 static inline bool audio_is_hearing_aid_out_device(audio_devices_t device)
703 {
704     return device == AUDIO_DEVICE_OUT_HEARING_AID;
705 }
706
707 static inline bool audio_is_usb_out_device(audio_devices_t device)
708 {
709     return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB));
710 }
711
712 static inline bool audio_is_usb_in_device(audio_devices_t device)
713 {
714     if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
715         device &= ~AUDIO_DEVICE_BIT_IN;
716         if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0)
717             return true;
718     }
719     return false;
720 }
721
722 /* OBSOLETE - use audio_is_usb_out_device() instead. */
723 static inline bool audio_is_usb_device(audio_devices_t device)
724 {
725     return audio_is_usb_out_device(device);
726 }
727
728 static inline bool audio_is_remote_submix_device(audio_devices_t device)
729 {
730     if ((audio_is_output_devices(device) &&
731          (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) == AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
732         || (!audio_is_output_devices(device) &&
733          (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX))
734         return true;
735     else
736         return false;
737 }
738
739 /* Returns true if:
740  *  representation is valid, and
741  *  there is at least one channel bit set which _could_ correspond to an input channel, and
742  *  there are no channel bits set which could _not_ correspond to an input channel.
743  * Otherwise returns false.
744  */
745 static inline bool audio_is_input_channel(audio_channel_mask_t channel)
746 {
747     uint32_t bits = audio_channel_mask_get_bits(channel);
748     switch (audio_channel_mask_get_representation(channel)) {
749     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
750         if (bits & ~AUDIO_CHANNEL_IN_ALL) {
751             bits = 0;
752         }
753         FALLTHROUGH_INTENDED;
754     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
755         return bits != 0;
756     default:
757         return false;
758     }
759 }
760
761 /* Returns true if:
762  *  representation is valid, and
763  *  there is at least one channel bit set which _could_ correspond to an output channel, and
764  *  there are no channel bits set which could _not_ correspond to an output channel.
765  * Otherwise returns false.
766  */
767 static inline bool audio_is_output_channel(audio_channel_mask_t channel)
768 {
769     uint32_t bits = audio_channel_mask_get_bits(channel);
770     switch (audio_channel_mask_get_representation(channel)) {
771     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
772         if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
773             bits = 0;
774         }
775         FALLTHROUGH_INTENDED;
776     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
777         return bits != 0;
778     default:
779         return false;
780     }
781 }
782
783 /* Returns the number of channels from an input channel mask,
784  * used in the context of audio input or recording.
785  * If a channel bit is set which could _not_ correspond to an input channel,
786  * it is excluded from the count.
787  * Returns zero if the representation is invalid.
788  */
789 static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
790 {
791     uint32_t bits = audio_channel_mask_get_bits(channel);
792     switch (audio_channel_mask_get_representation(channel)) {
793     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
794         // TODO: We can now merge with from_out_mask and remove anding
795         bits &= AUDIO_CHANNEL_IN_ALL;
796         FALLTHROUGH_INTENDED;
797     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
798         return popcount(bits);
799     default:
800         return 0;
801     }
802 }
803
804 /* Returns the number of channels from an output channel mask,
805  * used in the context of audio output or playback.
806  * If a channel bit is set which could _not_ correspond to an output channel,
807  * it is excluded from the count.
808  * Returns zero if the representation is invalid.
809  */
810 static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
811 {
812     uint32_t bits = audio_channel_mask_get_bits(channel);
813     switch (audio_channel_mask_get_representation(channel)) {
814     case AUDIO_CHANNEL_REPRESENTATION_POSITION:
815         // TODO: We can now merge with from_in_mask and remove anding
816         bits &= AUDIO_CHANNEL_OUT_ALL;
817         FALLTHROUGH_INTENDED;
818     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
819         return popcount(bits);
820     default:
821         return 0;
822     }
823 }
824
825 /* Derive a channel mask for index assignment from a channel count.
826  * Returns the matching channel mask,
827  * or AUDIO_CHANNEL_NONE if the channel count is zero,
828  * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
829  */
830 static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
831         uint32_t channel_count)
832 {
833     if (channel_count == 0) {
834         return AUDIO_CHANNEL_NONE;
835     }
836     if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
837         return AUDIO_CHANNEL_INVALID;
838     }
839     uint32_t bits = (1 << channel_count) - 1;
840     return audio_channel_mask_from_representation_and_bits(
841             AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
842 }
843
844 /* Derive an output channel mask for position assignment from a channel count.
845  * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
846  * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
847  * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
848  * for continuity with stereo.
849  * Returns the matching channel mask,
850  * or AUDIO_CHANNEL_NONE if the channel count is zero,
851  * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
852  * configurations for which a default output channel mask is defined.
853  */
854 static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
855 {
856     uint32_t bits;
857     switch (channel_count) {
858     case 0:
859         return AUDIO_CHANNEL_NONE;
860     case 1:
861         bits = AUDIO_CHANNEL_OUT_MONO;
862         break;
863     case 2:
864         bits = AUDIO_CHANNEL_OUT_STEREO;
865         break;
866     case 3: // 2.1
867         bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_LOW_FREQUENCY;
868         break;
869     case 4: // 4.0
870         bits = AUDIO_CHANNEL_OUT_QUAD;
871         break;
872     case 5: // 5.0
873         bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
874         break;
875     case 6: // 5.1
876         bits = AUDIO_CHANNEL_OUT_5POINT1;
877         break;
878     case 7: // 6.1
879         bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
880         break;
881     case 8:
882         bits = AUDIO_CHANNEL_OUT_7POINT1;
883         break;
884     // FIXME FCC_8
885     default:
886         return AUDIO_CHANNEL_INVALID;
887     }
888     return audio_channel_mask_from_representation_and_bits(
889             AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
890 }
891
892 /* Derive a default input channel mask from a channel count.
893  * Assumes a position mask for mono and stereo, or an index mask for channel counts > 2.
894  * Returns the matching channel mask,
895  * or AUDIO_CHANNEL_NONE if the channel count is zero,
896  * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
897  * configurations for which a default input channel mask is defined.
898  */
899 static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count)
900 {
901     uint32_t bits;
902     switch (channel_count) {
903     case 0:
904         return AUDIO_CHANNEL_NONE;
905     case 1:
906         bits = AUDIO_CHANNEL_IN_MONO;
907         break;
908     case 2:
909         bits = AUDIO_CHANNEL_IN_STEREO;
910         break;
911     case 3:
912     case 4:
913     case 5:
914     case 6:
915     case 7:
916     case 8:
917         // FIXME FCC_8
918         return audio_channel_mask_for_index_assignment_from_count(channel_count);
919     default:
920         return AUDIO_CHANNEL_INVALID;
921     }
922     return audio_channel_mask_from_representation_and_bits(
923             AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
924 }
925
926 /* Derive a default haptic channel mask from a channel count.
927  */
928 static inline audio_channel_mask_t haptic_channel_mask_from_count(uint32_t channel_count)
929 {
930     switch(channel_count) {
931     case 0:
932         return AUDIO_CHANNEL_NONE;
933     case 1:
934         return AUDIO_CHANNEL_OUT_HAPTIC_A;
935     case 2:
936         return AUDIO_CHANNEL_OUT_HAPTIC_AB;
937     default:
938         return AUDIO_CHANNEL_INVALID;
939     }
940 }
941
942 static inline audio_channel_mask_t audio_channel_mask_in_to_out(audio_channel_mask_t in)
943 {
944     switch (in) {
945     case AUDIO_CHANNEL_IN_MONO:
946         return AUDIO_CHANNEL_OUT_MONO;
947     case AUDIO_CHANNEL_IN_STEREO:
948         return AUDIO_CHANNEL_OUT_STEREO;
949     case AUDIO_CHANNEL_IN_5POINT1:
950         return AUDIO_CHANNEL_OUT_5POINT1;
951     case AUDIO_CHANNEL_IN_3POINT1POINT2:
952         return AUDIO_CHANNEL_OUT_3POINT1POINT2;
953     case AUDIO_CHANNEL_IN_3POINT0POINT2:
954         return AUDIO_CHANNEL_OUT_3POINT0POINT2;
955     case AUDIO_CHANNEL_IN_2POINT1POINT2:
956         return AUDIO_CHANNEL_OUT_2POINT1POINT2;
957     case AUDIO_CHANNEL_IN_2POINT0POINT2:
958         return AUDIO_CHANNEL_OUT_2POINT0POINT2;
959     default:
960         return AUDIO_CHANNEL_INVALID;
961     }
962 }
963
964 static inline audio_channel_mask_t audio_channel_mask_out_to_in(audio_channel_mask_t out)
965 {
966     switch (out) {
967     case AUDIO_CHANNEL_OUT_MONO:
968         return AUDIO_CHANNEL_IN_MONO;
969     case AUDIO_CHANNEL_OUT_STEREO:
970         return AUDIO_CHANNEL_IN_STEREO;
971     case AUDIO_CHANNEL_OUT_5POINT1:
972         return AUDIO_CHANNEL_IN_5POINT1;
973     case AUDIO_CHANNEL_OUT_3POINT1POINT2:
974         return AUDIO_CHANNEL_IN_3POINT1POINT2;
975     case AUDIO_CHANNEL_OUT_3POINT0POINT2:
976         return AUDIO_CHANNEL_IN_3POINT0POINT2;
977     case AUDIO_CHANNEL_OUT_2POINT1POINT2:
978         return AUDIO_CHANNEL_IN_2POINT1POINT2;
979     case AUDIO_CHANNEL_OUT_2POINT0POINT2:
980         return AUDIO_CHANNEL_IN_2POINT0POINT2;
981     default:
982         return AUDIO_CHANNEL_INVALID;
983     }
984 }
985
986 static inline bool audio_channel_position_mask_is_out_canonical(audio_channel_mask_t channelMask)
987 {
988     if (audio_channel_mask_get_representation(channelMask)
989             != AUDIO_CHANNEL_REPRESENTATION_POSITION) {
990         return false;
991     }
992     const uint32_t audioChannelCount = audio_channel_count_from_out_mask(
993             channelMask & ~AUDIO_CHANNEL_HAPTIC_ALL);
994     const uint32_t hapticChannelCount = audio_channel_count_from_out_mask(
995             channelMask & AUDIO_CHANNEL_HAPTIC_ALL);
996     return channelMask == (audio_channel_out_mask_from_count(audioChannelCount) |
997             haptic_channel_mask_from_count(hapticChannelCount));
998 }
999
1000 static inline bool audio_is_valid_format(audio_format_t format)
1001 {
1002     switch (format & AUDIO_FORMAT_MAIN_MASK) {
1003     case AUDIO_FORMAT_PCM:
1004         switch (format) {
1005         case AUDIO_FORMAT_PCM_16_BIT:
1006         case AUDIO_FORMAT_PCM_8_BIT:
1007         case AUDIO_FORMAT_PCM_32_BIT:
1008         case AUDIO_FORMAT_PCM_8_24_BIT:
1009         case AUDIO_FORMAT_PCM_FLOAT:
1010         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1011             return true;
1012         default:
1013             return false;
1014         }
1015         /* not reached */
1016     case AUDIO_FORMAT_MP3:
1017     case AUDIO_FORMAT_AMR_NB:
1018     case AUDIO_FORMAT_AMR_WB:
1019         return true;
1020     case AUDIO_FORMAT_AAC:
1021         switch (format) {
1022         case AUDIO_FORMAT_AAC:
1023         case AUDIO_FORMAT_AAC_MAIN:
1024         case AUDIO_FORMAT_AAC_LC:
1025         case AUDIO_FORMAT_AAC_SSR:
1026         case AUDIO_FORMAT_AAC_LTP:
1027         case AUDIO_FORMAT_AAC_HE_V1:
1028         case AUDIO_FORMAT_AAC_SCALABLE:
1029         case AUDIO_FORMAT_AAC_ERLC:
1030         case AUDIO_FORMAT_AAC_LD:
1031         case AUDIO_FORMAT_AAC_HE_V2:
1032         case AUDIO_FORMAT_AAC_ELD:
1033         case AUDIO_FORMAT_AAC_XHE:
1034             return true;
1035         default:
1036             return false;
1037         }
1038         /* not reached */
1039     case AUDIO_FORMAT_HE_AAC_V1:
1040     case AUDIO_FORMAT_HE_AAC_V2:
1041     case AUDIO_FORMAT_VORBIS:
1042     case AUDIO_FORMAT_OPUS:
1043     case AUDIO_FORMAT_AC3:
1044         return true;
1045     case AUDIO_FORMAT_E_AC3:
1046         switch (format) {
1047         case AUDIO_FORMAT_E_AC3:
1048         case AUDIO_FORMAT_E_AC3_JOC:
1049             return true;
1050         default:
1051             return false;
1052         }
1053         /* not reached */
1054     case AUDIO_FORMAT_DTS:
1055     case AUDIO_FORMAT_DTS_HD:
1056     case AUDIO_FORMAT_IEC61937:
1057     case AUDIO_FORMAT_DOLBY_TRUEHD:
1058     case AUDIO_FORMAT_EVRC:
1059     case AUDIO_FORMAT_EVRCB:
1060     case AUDIO_FORMAT_EVRCWB:
1061     case AUDIO_FORMAT_EVRCNW:
1062     case AUDIO_FORMAT_AAC_ADIF:
1063     case AUDIO_FORMAT_WMA:
1064     case AUDIO_FORMAT_WMA_PRO:
1065     case AUDIO_FORMAT_AMR_WB_PLUS:
1066     case AUDIO_FORMAT_MP2:
1067     case AUDIO_FORMAT_QCELP:
1068     case AUDIO_FORMAT_DSD:
1069     case AUDIO_FORMAT_FLAC:
1070     case AUDIO_FORMAT_ALAC:
1071     case AUDIO_FORMAT_APE:
1072         return true;
1073     case AUDIO_FORMAT_AAC_ADTS:
1074         switch (format) {
1075         case AUDIO_FORMAT_AAC_ADTS:
1076         case AUDIO_FORMAT_AAC_ADTS_MAIN:
1077         case AUDIO_FORMAT_AAC_ADTS_LC:
1078         case AUDIO_FORMAT_AAC_ADTS_SSR:
1079         case AUDIO_FORMAT_AAC_ADTS_LTP:
1080         case AUDIO_FORMAT_AAC_ADTS_HE_V1:
1081         case AUDIO_FORMAT_AAC_ADTS_SCALABLE:
1082         case AUDIO_FORMAT_AAC_ADTS_ERLC:
1083         case AUDIO_FORMAT_AAC_ADTS_LD:
1084         case AUDIO_FORMAT_AAC_ADTS_HE_V2:
1085         case AUDIO_FORMAT_AAC_ADTS_ELD:
1086         case AUDIO_FORMAT_AAC_ADTS_XHE:
1087             return true;
1088         default:
1089             return false;
1090         }
1091         /* not reached */
1092     case AUDIO_FORMAT_SBC:
1093     case AUDIO_FORMAT_APTX:
1094     case AUDIO_FORMAT_APTX_HD:
1095     case AUDIO_FORMAT_AC4:
1096     case AUDIO_FORMAT_LDAC:
1097         return true;
1098     case AUDIO_FORMAT_MAT:
1099         switch (format) {
1100         case AUDIO_FORMAT_MAT:
1101         case AUDIO_FORMAT_MAT_1_0:
1102         case AUDIO_FORMAT_MAT_2_0:
1103         case AUDIO_FORMAT_MAT_2_1:
1104             return true;
1105         default:
1106             return false;
1107         }
1108         /* not reached */
1109     case AUDIO_FORMAT_AAC_LATM:
1110         switch (format) {
1111         case AUDIO_FORMAT_AAC_LATM:
1112         case AUDIO_FORMAT_AAC_LATM_LC:
1113         case AUDIO_FORMAT_AAC_LATM_HE_V1:
1114         case AUDIO_FORMAT_AAC_LATM_HE_V2:
1115             return true;
1116         default:
1117             return false;
1118         }
1119         /* not reached */
1120     case AUDIO_FORMAT_CELT:
1121     case AUDIO_FORMAT_APTX_ADAPTIVE:
1122     case AUDIO_FORMAT_LHDC:
1123     case AUDIO_FORMAT_LHDC_LL:
1124     case AUDIO_FORMAT_APTX_TWSP:
1125         return true;
1126     default:
1127         return false;
1128     }
1129 }
1130
1131 /**
1132  * Extract the primary format, eg. PCM, AC3, etc.
1133  */
1134 static inline audio_format_t audio_get_main_format(audio_format_t format)
1135 {
1136     return (audio_format_t)(format & AUDIO_FORMAT_MAIN_MASK);
1137 }
1138
1139 /**
1140  * Is the data plain PCM samples that can be scaled and mixed?
1141  */
1142 static inline bool audio_is_linear_pcm(audio_format_t format)
1143 {
1144     return (audio_get_main_format(format) == AUDIO_FORMAT_PCM);
1145 }
1146
1147 /**
1148  * For this format, is the number of PCM audio frames directly proportional
1149  * to the number of data bytes?
1150  *
1151  * In other words, is the format transported as PCM audio samples,
1152  * but not necessarily scalable or mixable.
1153  * This returns true for real PCM, but also for AUDIO_FORMAT_IEC61937,
1154  * which is transported as 16 bit PCM audio, but where the encoded data
1155  * cannot be mixed or scaled.
1156  */
1157 static inline bool audio_has_proportional_frames(audio_format_t format)
1158 {
1159     audio_format_t mainFormat = audio_get_main_format(format);
1160     return (mainFormat == AUDIO_FORMAT_PCM
1161             || mainFormat == AUDIO_FORMAT_IEC61937);
1162 }
1163
1164 static inline size_t audio_bytes_per_sample(audio_format_t format)
1165 {
1166     size_t size = 0;
1167
1168     switch (format) {
1169     case AUDIO_FORMAT_PCM_32_BIT:
1170     case AUDIO_FORMAT_PCM_8_24_BIT:
1171         size = sizeof(int32_t);
1172         break;
1173     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1174         size = sizeof(uint8_t) * 3;
1175         break;
1176     case AUDIO_FORMAT_PCM_16_BIT:
1177     case AUDIO_FORMAT_IEC61937:
1178         size = sizeof(int16_t);
1179         break;
1180     case AUDIO_FORMAT_PCM_8_BIT:
1181         size = sizeof(uint8_t);
1182         break;
1183     case AUDIO_FORMAT_PCM_FLOAT:
1184         size = sizeof(float);
1185         break;
1186     default:
1187         break;
1188     }
1189     return size;
1190 }
1191
1192 static inline size_t audio_bytes_per_frame(uint32_t channel_count, audio_format_t format)
1193 {
1194     // cannot overflow for reasonable channel_count
1195     return channel_count * audio_bytes_per_sample(format);
1196 }
1197
1198 /* converts device address to string sent to audio HAL via set_parameters */
1199 static inline char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
1200 {
1201     const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address=");
1202     char param[kSize];
1203
1204     if (device & AUDIO_DEVICE_OUT_ALL_A2DP)
1205         snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
1206     else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
1207         snprintf(param, kSize, "%s=%s", "mix", address);
1208     else
1209         snprintf(param, kSize, "%s", address);
1210
1211     return strdup(param);
1212 }
1213
1214 static inline bool audio_device_is_digital(audio_devices_t device) {
1215     if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1216         // input
1217         return (~AUDIO_DEVICE_BIT_IN & device & (AUDIO_DEVICE_IN_ALL_USB |
1218                           AUDIO_DEVICE_IN_HDMI |
1219                           AUDIO_DEVICE_IN_HDMI_ARC |
1220                           AUDIO_DEVICE_IN_SPDIF |
1221                           AUDIO_DEVICE_IN_IP |
1222                           AUDIO_DEVICE_IN_BUS)) != 0;
1223     } else {
1224         // output
1225         return (device & (AUDIO_DEVICE_OUT_ALL_USB |
1226                           AUDIO_DEVICE_OUT_HDMI |
1227                           AUDIO_DEVICE_OUT_HDMI_ARC |
1228                           AUDIO_DEVICE_OUT_SPDIF |
1229                           AUDIO_DEVICE_OUT_IP |
1230                           AUDIO_DEVICE_OUT_BUS)) != 0;
1231     }
1232 }
1233
1234 #ifndef AUDIO_NO_SYSTEM_DECLARATIONS
1235
1236 static inline bool audio_gain_config_are_equal(
1237         const struct audio_gain_config *lhs, const struct audio_gain_config *rhs) {
1238     if (lhs->mode != rhs->mode) return false;
1239     switch (lhs->mode) {
1240     case AUDIO_GAIN_MODE_JOINT:
1241         if (lhs->values[0] != rhs->values[0]) return false;
1242         break;
1243     case AUDIO_GAIN_MODE_CHANNELS:
1244     case AUDIO_GAIN_MODE_RAMP:
1245         if (lhs->channel_mask != rhs->channel_mask) return false;
1246         for (int i = 0; i < popcount(lhs->channel_mask); ++i) {
1247             if (lhs->values[i] != rhs->values[i]) return false;
1248         }
1249         break;
1250     default: return false;
1251     }
1252     return lhs->ramp_duration_ms == rhs->ramp_duration_ms;
1253 }
1254
1255 static inline bool audio_port_config_has_input_direction(const struct audio_port_config *port_cfg) {
1256     switch (port_cfg->type) {
1257     case AUDIO_PORT_TYPE_DEVICE:
1258         switch (port_cfg->role) {
1259         case AUDIO_PORT_ROLE_SOURCE: return true;
1260         case AUDIO_PORT_ROLE_SINK: return false;
1261         default: return false;
1262         }
1263     case AUDIO_PORT_TYPE_MIX:
1264         switch (port_cfg->role) {
1265         case AUDIO_PORT_ROLE_SOURCE: return false;
1266         case AUDIO_PORT_ROLE_SINK: return true;
1267         default: return false;
1268         }
1269     default: return false;
1270     }
1271 }
1272
1273 static inline bool audio_port_configs_are_equal(
1274         const struct audio_port_config *lhs, const struct audio_port_config *rhs) {
1275     if (lhs->role != rhs->role || lhs->type != rhs->type) return false;
1276     switch (lhs->type) {
1277     case AUDIO_PORT_TYPE_NONE: break;
1278     case AUDIO_PORT_TYPE_DEVICE:
1279         if (lhs->ext.device.hw_module != rhs->ext.device.hw_module ||
1280                 lhs->ext.device.type != rhs->ext.device.type ||
1281                 strncmp(lhs->ext.device.address, rhs->ext.device.address,
1282                         AUDIO_DEVICE_MAX_ADDRESS_LEN) != 0) {
1283             return false;
1284         }
1285         break;
1286     case AUDIO_PORT_TYPE_MIX:
1287         if (lhs->ext.mix.hw_module != rhs->ext.mix.hw_module ||
1288                 lhs->ext.mix.handle != rhs->ext.mix.handle) return false;
1289         if (lhs->role == AUDIO_PORT_ROLE_SOURCE &&
1290                 lhs->ext.mix.usecase.stream != rhs->ext.mix.usecase.stream) return false;
1291         else if (lhs->role == AUDIO_PORT_ROLE_SINK &&
1292                 lhs->ext.mix.usecase.source != rhs->ext.mix.usecase.source) return false;
1293         break;
1294     case AUDIO_PORT_TYPE_SESSION:
1295         if (lhs->ext.session.session != rhs->ext.session.session) return false;
1296         break;
1297     default: return false;
1298     }
1299     return lhs->config_mask == rhs->config_mask &&
1300             ((lhs->config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) == 0 ||
1301                     lhs->sample_rate == rhs->sample_rate) &&
1302             ((lhs->config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) == 0 ||
1303                     lhs->channel_mask == rhs->channel_mask) &&
1304             ((lhs->config_mask & AUDIO_PORT_CONFIG_FORMAT) == 0 ||
1305                     lhs->format == rhs->format) &&
1306             ((lhs->config_mask & AUDIO_PORT_CONFIG_GAIN) == 0 ||
1307                     audio_gain_config_are_equal(&lhs->gain, &rhs->gain)) &&
1308             ((lhs->config_mask & AUDIO_PORT_CONFIG_FLAGS) == 0 ||
1309                     (audio_port_config_has_input_direction(lhs) ?
1310                             lhs->flags.input == rhs->flags.input :
1311                             lhs->flags.output == rhs->flags.output));
1312 }
1313
1314 static inline bool audio_port_config_has_hw_av_sync(const struct audio_port_config *port_cfg) {
1315     if (!(port_cfg->config_mask & AUDIO_PORT_CONFIG_FLAGS)) {
1316         return false;
1317     }
1318     return audio_port_config_has_input_direction(port_cfg) ?
1319             port_cfg->flags.input & AUDIO_INPUT_FLAG_HW_AV_SYNC
1320             : port_cfg->flags.output & AUDIO_OUTPUT_FLAG_HW_AV_SYNC;
1321 }
1322
1323 static inline bool audio_patch_has_hw_av_sync(const struct audio_patch *patch) {
1324     for (unsigned int i = 0; i < patch->num_sources; ++i) {
1325         if (audio_port_config_has_hw_av_sync(&patch->sources[i])) return true;
1326     }
1327     for (unsigned int i = 0; i < patch->num_sinks; ++i) {
1328         if (audio_port_config_has_hw_av_sync(&patch->sinks[i])) return true;
1329     }
1330     return false;
1331 }
1332
1333 static inline bool audio_patch_is_valid(const struct audio_patch *patch) {
1334     // Note that patch can have no sinks.
1335     return patch->num_sources != 0 && patch->num_sources <= AUDIO_PATCH_PORTS_MAX &&
1336             patch->num_sinks <= AUDIO_PATCH_PORTS_MAX;
1337 }
1338
1339 // Note that when checking for equality the order of ports must match.
1340 // Patches will not be equivalent if they contain the same ports but they are permuted differently.
1341 static inline bool audio_patches_are_equal(
1342         const struct audio_patch *lhs, const struct audio_patch *rhs) {
1343     if (!audio_patch_is_valid(lhs) || !audio_patch_is_valid(rhs)) return false;
1344     if (lhs->num_sources != rhs->num_sources || lhs->num_sinks != rhs->num_sinks) return false;
1345     for (unsigned int i = 0; i < lhs->num_sources; ++i) {
1346         if (!audio_port_configs_are_equal(&lhs->sources[i], &rhs->sources[i])) return false;
1347     }
1348     for (unsigned int i = 0; i < lhs->num_sinks; ++i) {
1349         if (!audio_port_configs_are_equal(&lhs->sinks[i], &rhs->sinks[i])) return false;
1350     }
1351     return true;
1352 }
1353
1354 #endif
1355
1356 // Unique effect ID (can be generated from the following site:
1357 //  http://www.itu.int/ITU-T/asn1/uuid.html)
1358 // This struct is used for effects identification and in soundtrigger.
1359 typedef struct audio_uuid_s {
1360     uint32_t timeLow;
1361     uint16_t timeMid;
1362     uint16_t timeHiAndVersion;
1363     uint16_t clockSeq;
1364     uint8_t node[6];
1365 } audio_uuid_t;
1366
1367 //TODO: audio_microphone_location_t need to move to HAL v4.0
1368 typedef enum {
1369     AUDIO_MICROPHONE_LOCATION_UNKNOWN = 0,
1370     AUDIO_MICROPHONE_LOCATION_MAINBODY = 1,
1371     AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE = 2,
1372     AUDIO_MICROPHONE_LOCATION_PERIPHERAL = 3,
1373     AUDIO_MICROPHONE_LOCATION_CNT = 4,
1374 } audio_microphone_location_t;
1375
1376 //TODO: audio_microphone_directionality_t need to move to HAL v4.0
1377 typedef enum {
1378     AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN = 0,
1379     AUDIO_MICROPHONE_DIRECTIONALITY_OMNI = 1,
1380     AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL = 2,
1381     AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID = 3,
1382     AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID = 4,
1383     AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID = 5,
1384     AUDIO_MICROPHONE_DIRECTIONALITY_CNT = 6,
1385 } audio_microphone_directionality_t;
1386
1387 /* A 3D point which could be used to represent geometric location
1388  * or orientation of a microphone.
1389  */
1390 struct audio_microphone_coordinate {
1391     float x;
1392     float y;
1393     float z;
1394 };
1395
1396 /* An number to indicate which group the microphone locate. Main body is
1397  * usually group 0. Developer could use this value to group the microphones
1398  * that locate on the same peripheral or attachments.
1399  */
1400 typedef int audio_microphone_group_t;
1401
1402 typedef enum {
1403     AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED = 0,
1404     AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT = 1,
1405     AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED = 2,
1406     AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT = 3,
1407 } audio_microphone_channel_mapping_t;
1408
1409 /* the maximum length for the microphone id */
1410 #define AUDIO_MICROPHONE_ID_MAX_LEN 32
1411 /* max number of frequency responses in a frequency response table */
1412 #define AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES 256
1413 /* max number of microphone */
1414 #define AUDIO_MICROPHONE_MAX_COUNT 32
1415 /* the value of unknown spl */
1416 #define AUDIO_MICROPHONE_SPL_UNKNOWN -FLT_MAX
1417 /* the value of unknown sensitivity */
1418 #define AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN -FLT_MAX
1419 /* the value of unknown coordinate */
1420 #define AUDIO_MICROPHONE_COORDINATE_UNKNOWN -FLT_MAX
1421 /* the value used as address when the address of bottom microphone is empty */
1422 #define AUDIO_BOTTOM_MICROPHONE_ADDRESS "bottom"
1423 /* the value used as address when the address of back microphone is empty */
1424 #define AUDIO_BACK_MICROPHONE_ADDRESS "back"
1425
1426 struct audio_microphone_characteristic_t {
1427     char                               device_id[AUDIO_MICROPHONE_ID_MAX_LEN];
1428     audio_port_handle_t                id;
1429     audio_devices_t                    device;
1430     char                               address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
1431     audio_microphone_channel_mapping_t channel_mapping[AUDIO_CHANNEL_COUNT_MAX];
1432     audio_microphone_location_t        location;
1433     audio_microphone_group_t           group;
1434     unsigned int                       index_in_the_group;
1435     float                              sensitivity;
1436     float                              max_spl;
1437     float                              min_spl;
1438     audio_microphone_directionality_t  directionality;
1439     unsigned int                       num_frequency_responses;
1440     float frequency_responses[2][AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES];
1441     struct audio_microphone_coordinate geometric_location;
1442     struct audio_microphone_coordinate orientation;
1443 };
1444
1445 __END_DECLS
1446
1447 /**
1448  * List of known audio HAL modules. This is the base name of the audio HAL
1449  * library composed of the "audio." prefix, one of the base names below and
1450  * a suffix specific to the device.
1451  * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
1452  *
1453  * The same module names are used in audio policy configuration files.
1454  */
1455
1456 #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
1457 #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
1458 #define AUDIO_HARDWARE_MODULE_ID_USB "usb"
1459 #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
1460 #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
1461 #define AUDIO_HARDWARE_MODULE_ID_STUB "stub"
1462 #define AUDIO_HARDWARE_MODULE_ID_HEARING_AID "hearing_aid"
1463 #define AUDIO_HARDWARE_MODULE_ID_MSD "msd"
1464
1465 /**
1466  * Multi-Stream Decoder (MSD) HAL service name. MSD HAL is used to mix
1467  * encoded streams together with PCM streams, producing re-encoded
1468  * streams or PCM streams.
1469  *
1470  * The service must register itself using this name, and audioserver
1471  * tries to instantiate a device factory using this name as well.
1472  * Note that the HIDL implementation library file name *must* have the
1473  * suffix "msd" in order to be picked up by HIDL that is:
1474  *
1475  *   android.hardware.audio@x.x-implmsd.so
1476  */
1477 #define AUDIO_HAL_SERVICE_NAME_MSD "msd"
1478
1479 /**
1480  * Parameter definitions.
1481  * Note that in the framework code it's recommended to use AudioParameter.h
1482  * instead of these preprocessor defines, and for sure avoid just copying
1483  * the constant values.
1484  */
1485
1486 #define AUDIO_PARAMETER_VALUE_ON "on"
1487 #define AUDIO_PARAMETER_VALUE_OFF "off"
1488
1489 /**
1490  *  audio device parameters
1491  */
1492
1493 /* BT SCO Noise Reduction + Echo Cancellation parameters */
1494 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
1495
1496 /* Get a new HW synchronization source identifier.
1497  * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
1498  * or no HW sync is available. */
1499 #define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
1500
1501 /* Screen state */
1502 #define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
1503
1504 /* User's preferred audio language setting (in ISO 639-2/T three-letter string code)
1505  * used to select a specific language presentation for next generation audio codecs. */
1506 #define AUDIO_PARAMETER_KEY_AUDIO_LANGUAGE_PREFERRED "audio_language_preferred"
1507
1508 /**
1509  *  audio stream parameters
1510  */
1511
1512 #define AUDIO_PARAMETER_STREAM_ROUTING "routing"             /* audio_devices_t */
1513 #define AUDIO_PARAMETER_STREAM_FORMAT "format"               /* audio_format_t */
1514 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels"           /* audio_channel_mask_t */
1515 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"     /* size_t */
1516 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"   /* audio_source_t */
1517 #define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
1518
1519 /* Request the presentation id to be decoded by a next gen audio decoder */
1520 #define AUDIO_PARAMETER_STREAM_PRESENTATION_ID "presentation_id" /* int32_t */
1521
1522 /* Request the program id to be decoded by a next gen audio decoder */
1523 #define AUDIO_PARAMETER_STREAM_PROGRAM_ID "program_id"           /* int32_t */
1524
1525 #define AUDIO_PARAMETER_DEVICE_CONNECT "connect"            /* audio_devices_t */
1526 #define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect"      /* audio_devices_t */
1527
1528 /* Enable mono audio playback if 1, else should be 0. */
1529 #define AUDIO_PARAMETER_MONO_OUTPUT "mono_output"
1530
1531 /* Set the HW synchronization source for an output stream. */
1532 #define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
1533
1534 /* Query supported formats. The response is a '|' separated list of strings from
1535  * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
1536 #define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
1537 /* Query supported channel masks. The response is a '|' separated list of strings from
1538  * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
1539 #define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
1540 /* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
1541  * "sup_sampling_rates=44100|48000" */
1542 #define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
1543
1544 #define AUDIO_PARAMETER_VALUE_LIST_SEPARATOR "|"
1545
1546 /* Reconfigure offloaded A2DP codec */
1547 #define AUDIO_PARAMETER_RECONFIG_A2DP "reconfigA2dp"
1548 /* Query if HwModule supports reconfiguration of offloaded A2DP codec */
1549 #define AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED "isReconfigA2dpSupported"
1550
1551 /**
1552  * audio codec parameters
1553  */
1554
1555 #define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
1556 #define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
1557 #define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
1558 #define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
1559 #define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
1560 #define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
1561 #define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
1562 #define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
1563 #define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL  "music_offload_num_channels"
1564 #define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING  "music_offload_down_sampling"
1565 #define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES  "delay_samples"
1566 #define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES  "padding_samples"
1567
1568 #endif  // ANDROID_AUDIO_CORE_H