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