2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef ANDROID_AUDIO_CORE_H
19 #define ANDROID_AUDIO_CORE_H
24 #include <sys/cdefs.h>
25 #include <sys/types.h>
27 #include <cutils/bitops.h>
29 #include "audio-base.h"
30 #include "audio-base-utils.h"
34 /* The enums were moved here mostly from
35 * frameworks/base/include/media/AudioSystem.h
38 /* represents an invalid uid for tracks; the calling or client uid is often substituted. */
39 #define AUDIO_UID_INVALID ((uid_t)-1)
41 /* device address used to refer to the standard remote submix */
42 #define AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS "0"
44 /* AudioFlinger and AudioPolicy services use I/O handles to identify audio sources and sinks */
45 typedef int audio_io_handle_t;
47 typedef uint32_t audio_flags_mask_t;
49 /* Do not change these values without updating their counterparts
50 * in frameworks/base/media/java/android/media/AudioAttributes.java
53 AUDIO_FLAG_NONE = 0x0,
54 AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1,
55 AUDIO_FLAG_SECURE = 0x2,
57 AUDIO_FLAG_BEACON = 0x8,
58 AUDIO_FLAG_HW_AV_SYNC = 0x10,
59 AUDIO_FLAG_HW_HOTWORD = 0x20,
60 AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY = 0x40,
61 AUDIO_FLAG_BYPASS_MUTE = 0x80,
62 AUDIO_FLAG_LOW_LATENCY = 0x100,
63 AUDIO_FLAG_DEEP_BUFFER = 0x200,
66 /* Audio attributes */
67 #define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
69 audio_content_type_t content_type;
71 audio_source_t source;
72 audio_flags_mask_t flags;
73 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
74 } __attribute__((packed)) audio_attributes_t; // sent through Binder;
76 /* a unique ID allocated by AudioFlinger for use as an audio_io_handle_t, audio_session_t,
77 * effect ID (int), audio_module_handle_t, and audio_patch_handle_t.
78 * Audio port IDs (audio_port_handle_t) are allocated by AudioPolicy
79 * in a different namespace than AudioFlinger unique IDs.
81 typedef int audio_unique_id_t;
83 /* Possible uses for an audio_unique_id_t */
85 AUDIO_UNIQUE_ID_USE_UNSPECIFIED = 0,
86 AUDIO_UNIQUE_ID_USE_SESSION = 1, // for allocated sessions, not special AUDIO_SESSION_*
87 AUDIO_UNIQUE_ID_USE_MODULE = 2,
88 AUDIO_UNIQUE_ID_USE_EFFECT = 3,
89 AUDIO_UNIQUE_ID_USE_PATCH = 4,
90 AUDIO_UNIQUE_ID_USE_OUTPUT = 5,
91 AUDIO_UNIQUE_ID_USE_INPUT = 6,
92 AUDIO_UNIQUE_ID_USE_PLAYER = 7,
93 AUDIO_UNIQUE_ID_USE_MAX = 8, // must be a power-of-two
94 AUDIO_UNIQUE_ID_USE_MASK = AUDIO_UNIQUE_ID_USE_MAX - 1
95 } audio_unique_id_use_t;
97 /* Return the use of an audio_unique_id_t */
98 static inline audio_unique_id_use_t audio_unique_id_get_use(audio_unique_id_t id)
100 return (audio_unique_id_use_t) (id & AUDIO_UNIQUE_ID_USE_MASK);
103 /* Reserved audio_unique_id_t values. FIXME: not a complete list. */
104 #define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
106 /* A channel mask per se only defines the presence or absence of a channel, not the order.
107 * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
109 * audio_channel_mask_t is an opaque type and its internal layout should not
110 * be assumed as it may change in the future.
111 * Instead, always use the functions declared in this header to examine.
113 * These are the current representations:
115 * AUDIO_CHANNEL_REPRESENTATION_POSITION
116 * is a channel mask representation for position assignment.
117 * Each low-order bit corresponds to the spatial position of a transducer (output),
118 * or interpretation of channel (input).
119 * The user of a channel mask needs to know the context of whether it is for output or input.
120 * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
121 * It is not permitted for no bits to be set.
123 * AUDIO_CHANNEL_REPRESENTATION_INDEX
124 * is a channel mask representation for index assignment.
125 * Each low-order bit corresponds to a selected channel.
126 * There is no platform interpretation of the various bits.
127 * There is no concept of output or input.
128 * It is not permitted for no bits to be set.
130 * All other representations are reserved for future use.
132 * Warning: current representation distinguishes between input and output, but this will not the be
133 * case in future revisions of the platform. Wherever there is an ambiguity between input and output
134 * that is currently resolved by checking the channel mask, the implementer should look for ways to
135 * fix it with additional information outside of the mask.
137 typedef uint32_t audio_channel_mask_t;
139 /* log(2) of maximum number of representations, not part of public API */
140 #define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
142 /* The return value is undefined if the channel mask is invalid. */
143 static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
145 return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
148 typedef uint32_t audio_channel_representation_t;
150 /* The return value is undefined if the channel mask is invalid. */
151 static inline audio_channel_representation_t audio_channel_mask_get_representation(
152 audio_channel_mask_t channel)
154 // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
155 return (audio_channel_representation_t)
156 ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
159 /* Returns true if the channel mask is valid,
160 * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
161 * This function is unable to determine whether a channel mask for position assignment
162 * is invalid because an output mask has an invalid output bit set,
163 * or because an input mask has an invalid input bit set.
164 * All other APIs that take a channel mask assume that it is valid.
166 static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
168 uint32_t bits = audio_channel_mask_get_bits(channel);
169 audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
170 switch (representation) {
171 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
172 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
181 /* Not part of public API */
182 static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
183 audio_channel_representation_t representation, uint32_t bits)
185 return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
189 * Expresses the convention when stereo audio samples are stored interleaved
190 * in an array. This should improve readability by allowing code to use
191 * symbolic indices instead of hard-coded [0] and [1].
193 * For multi-channel beyond stereo, the platform convention is that channels
194 * are interleaved in order from least significant channel mask bit to most
195 * significant channel mask bit, with unused bits skipped. Any exceptions
196 * to this convention will be noted at the appropriate API.
199 AUDIO_INTERLEAVE_LEFT = 0,
200 AUDIO_INTERLEAVE_RIGHT = 1,
203 /* This enum is deprecated */
205 AUDIO_IN_ACOUSTICS_NONE = 0,
206 AUDIO_IN_ACOUSTICS_AGC_ENABLE = 0x0001,
207 AUDIO_IN_ACOUSTICS_AGC_DISABLE = 0,
208 AUDIO_IN_ACOUSTICS_NS_ENABLE = 0x0002,
209 AUDIO_IN_ACOUSTICS_NS_DISABLE = 0,
210 AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004,
211 AUDIO_IN_ACOUSTICS_TX_DISABLE = 0,
212 } audio_in_acoustics_t;
214 typedef uint32_t audio_devices_t;
216 * Stub audio output device. Used in policy configuration file on platforms without audio outputs.
217 * This alias value to AUDIO_DEVICE_OUT_DEFAULT is only used in the audio policy context.
219 #define AUDIO_DEVICE_OUT_STUB AUDIO_DEVICE_OUT_DEFAULT
221 * Stub audio input device. Used in policy configuration file on platforms without audio inputs.
222 * This alias value to AUDIO_DEVICE_IN_DEFAULT is only used in the audio policy context.
224 #define AUDIO_DEVICE_IN_STUB AUDIO_DEVICE_IN_DEFAULT
226 /* Additional information about compressed streams offloaded to
228 * The version and size fields must be initialized by the caller by using
229 * one of the constants defined here.
230 * Must be aligned to transmit as raw memory through Binder.
233 uint16_t version; // version of the info structure
234 uint16_t size; // total size of the structure including version and size
235 uint32_t sample_rate; // sample rate in Hz
236 audio_channel_mask_t channel_mask; // channel mask
237 audio_format_t format; // audio format
238 audio_stream_type_t stream_type; // stream type
239 uint32_t bit_rate; // bit rate in bits per second
240 int64_t duration_us; // duration in microseconds, -1 if unknown
241 bool has_video; // true if stream is tied to a video stream
242 bool is_streaming; // true if streaming, false if local playback
244 uint32_t offload_buffer_size; // offload fragment size
246 } __attribute__((aligned(8))) audio_offload_info_t;
248 #define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \
249 ((((maj) & 0xff) << 8) | ((min) & 0xff))
251 #define AUDIO_OFFLOAD_INFO_VERSION_0_1 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 1)
252 #define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_1
254 static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
255 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
256 /* .size = */ sizeof(audio_offload_info_t),
257 /* .sample_rate = */ 0,
258 /* .channel_mask = */ 0,
259 /* .format = */ AUDIO_FORMAT_DEFAULT,
260 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
262 /* .duration_us = */ 0,
263 /* .has_video = */ false,
264 /* .is_streaming = */ false,
265 /* .bit_width = */ 16,
266 /* .offload_buffer_size = */ 0,
267 /* .usage = */ AUDIO_USAGE_UNKNOWN
270 /* common audio stream configuration parameters
271 * You should memset() the entire structure to zero before use to
272 * ensure forward compatibility
273 * Must be aligned to transmit as raw memory through Binder.
275 struct __attribute__((aligned(8))) audio_config {
276 uint32_t sample_rate;
277 audio_channel_mask_t channel_mask;
278 audio_format_t format;
279 audio_offload_info_t offload_info;
280 uint32_t frame_count;
282 typedef struct audio_config audio_config_t;
284 static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
285 /* .sample_rate = */ 0,
286 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
287 /* .format = */ AUDIO_FORMAT_DEFAULT,
288 /* .offload_info = */ {
289 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
290 /* .size = */ sizeof(audio_offload_info_t),
291 /* .sample_rate = */ 0,
292 /* .channel_mask = */ 0,
293 /* .format = */ AUDIO_FORMAT_DEFAULT,
294 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL,
296 /* .duration_us = */ 0,
297 /* .has_video = */ false,
298 /* .is_streaming = */ false,
299 /* .bit_width = */ 16,
300 /* .offload_buffer_size = */ 0,
301 /* .usage = */ AUDIO_USAGE_UNKNOWN
303 /* .frame_count = */ 0,
306 struct audio_config_base {
307 uint32_t sample_rate;
308 audio_channel_mask_t channel_mask;
309 audio_format_t format;
312 typedef struct audio_config_base audio_config_base_t;
314 static const audio_config_base_t AUDIO_CONFIG_BASE_INITIALIZER = {
315 /* .sample_rate = */ 0,
316 /* .channel_mask = */ AUDIO_CHANNEL_NONE,
317 /* .format = */ AUDIO_FORMAT_DEFAULT
320 /* audio hw module handle functions or structures referencing a module */
321 typedef int audio_module_handle_t;
323 /******************************
325 *****************************/
327 /** 3 dB headroom are allowed on float samples (3db = 10^(3/20) = 1.412538).
328 * See: https://developer.android.com/reference/android/media/AudioTrack.html#write(float[], int, int, int)
330 #define FLOAT_NOMINAL_RANGE_HEADROOM 1.412538
332 /* If the audio hardware supports gain control on some audio paths,
333 * the platform can expose them in the audio_policy.conf file. The audio HAL
334 * will then implement gain control functions that will use the following data
337 typedef uint32_t audio_gain_mode_t;
340 /* An audio_gain struct is a representation of a gain stage.
341 * A gain stage is always attached to an audio port. */
343 audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */
344 audio_channel_mask_t channel_mask; /* channels which gain an be controlled.
345 N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
346 int min_value; /* minimum gain value in millibels */
347 int max_value; /* maximum gain value in millibels */
348 int default_value; /* default gain value in millibels */
349 unsigned int step_value; /* gain step in millibels */
350 unsigned int min_ramp_ms; /* minimum ramp duration in ms */
351 unsigned int max_ramp_ms; /* maximum ramp duration in ms */
354 /* The gain configuration structure is used to get or set the gain values of a
356 struct audio_gain_config {
357 int index; /* index of the corresponding audio_gain in the
358 audio_port gains[] table */
359 audio_gain_mode_t mode; /* mode requested for this command */
360 audio_channel_mask_t channel_mask; /* channels which gain value follows.
363 // note this "8" is not FCC_8, so it won't need to be changed for > 8 channels
364 int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
365 for each channel ordered from LSb to MSb in
366 channel mask. The number of values is 1 in joint
367 mode or popcount(channel_mask) */
368 unsigned int ramp_duration_ms; /* ramp duration in ms */
371 /******************************
373 *****************************/
375 /* Types defined here are used to describe an audio source or sink at internal
376 * framework interfaces (audio policy, patch panel) or at the audio HAL.
377 * Sink and sources are grouped in a concept of “audio port” representing an
378 * audio end point at the edge of the system managed by the module exposing
381 /* Each port has a unique ID or handle allocated by policy manager */
382 typedef int audio_port_handle_t;
384 /* the maximum length for the human-readable device name */
385 #define AUDIO_PORT_MAX_NAME_LEN 128
387 /* maximum audio device address length */
388 #define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
390 /* extension for audio port configuration structure when the audio port is a
392 struct audio_port_config_device_ext {
393 audio_module_handle_t hw_module; /* module the device is attached to */
394 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
395 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
398 /* extension for audio port configuration structure when the audio port is a
400 struct audio_port_config_mix_ext {
401 audio_module_handle_t hw_module; /* module the stream is attached to */
402 audio_io_handle_t handle; /* I/O handle of the input/output stream */
404 //TODO: change use case for output streams: use strategy and mixer attributes
405 audio_stream_type_t stream;
406 audio_source_t source;
410 /* extension for audio port configuration structure when the audio port is an
412 struct audio_port_config_session_ext {
413 audio_session_t session; /* audio session */
416 /* audio port configuration structure used to specify a particular configuration of
418 struct audio_port_config {
419 audio_port_handle_t id; /* port unique ID */
420 audio_port_role_t role; /* sink or source */
421 audio_port_type_t type; /* device, mix ... */
422 unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */
423 unsigned int sample_rate; /* sampling rate in Hz */
424 audio_channel_mask_t channel_mask; /* channel mask if applicable */
425 audio_format_t format; /* format if applicable */
426 struct audio_gain_config gain; /* gain to apply if applicable */
428 struct audio_port_config_device_ext device; /* device specific info */
429 struct audio_port_config_mix_ext mix; /* mix specific info */
430 struct audio_port_config_session_ext session; /* session specific info */
435 /* max number of sampling rates in audio port */
436 #define AUDIO_PORT_MAX_SAMPLING_RATES 32
437 /* max number of channel masks in audio port */
438 #define AUDIO_PORT_MAX_CHANNEL_MASKS 32
439 /* max number of audio formats in audio port */
440 #define AUDIO_PORT_MAX_FORMATS 32
441 /* max number of gain controls in audio port */
442 #define AUDIO_PORT_MAX_GAINS 16
444 /* extension for audio port structure when the audio port is a hardware device */
445 struct audio_port_device_ext {
446 audio_module_handle_t hw_module; /* module the device is attached to */
447 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
448 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
451 /* extension for audio port structure when the audio port is a sub mix */
452 struct audio_port_mix_ext {
453 audio_module_handle_t hw_module; /* module the stream is attached to */
454 audio_io_handle_t handle; /* I/O handle of the input.output stream */
455 audio_mix_latency_class_t latency_class; /* latency class */
456 // other attributes: routing strategies
459 /* extension for audio port structure when the audio port is an audio session */
460 struct audio_port_session_ext {
461 audio_session_t session; /* audio session */
465 audio_port_handle_t id; /* port unique ID */
466 audio_port_role_t role; /* sink or source */
467 audio_port_type_t type; /* device, mix ... */
468 char name[AUDIO_PORT_MAX_NAME_LEN];
469 unsigned int num_sample_rates; /* number of sampling rates in following array */
470 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
471 unsigned int num_channel_masks; /* number of channel masks in following array */
472 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
473 unsigned int num_formats; /* number of formats in following array */
474 audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
475 unsigned int num_gains; /* number of gains in following array */
476 struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
477 struct audio_port_config active_config; /* current audio port configuration */
479 struct audio_port_device_ext device;
480 struct audio_port_mix_ext mix;
481 struct audio_port_session_ext session;
485 /* An audio patch represents a connection between one or more source ports and
486 * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
487 * applications via framework APIs.
488 * Each patch is identified by a handle at the interface used to create that patch. For instance,
489 * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
490 * This handle is unique to a given audio HAL hardware module.
491 * But the same patch receives another system wide unique handle allocated by the framework.
492 * This unique handle is used for all transactions inside the framework.
494 typedef int audio_patch_handle_t;
496 #define AUDIO_PATCH_PORTS_MAX 16
499 audio_patch_handle_t id; /* patch unique ID */
500 unsigned int num_sources; /* number of sources in following array */
501 struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
502 unsigned int num_sinks; /* number of sinks in following array */
503 struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
508 /* a HW synchronization source returned by the audio HAL */
509 typedef uint32_t audio_hw_sync_t;
511 /* an invalid HW synchronization source indicating an error */
512 #define AUDIO_HW_SYNC_INVALID 0
515 * Mmap buffer descriptor returned by audio_stream->create_mmap_buffer().
516 * note\ Used by streams opened in mmap mode.
518 struct audio_mmap_buffer_info {
519 void* shared_memory_address; /**< base address of mmap memory buffer.
520 For use by local process only */
521 int32_t shared_memory_fd; /**< FD for mmap memory buffer */
522 int32_t buffer_size_frames; /**< total buffer size in frames */
523 int32_t burst_size_frames; /**< transfer size granularity in frames */
527 * Mmap buffer read/write position returned by audio_stream->get_mmap_position().
528 * note\ Used by streams opened in mmap mode.
530 struct audio_mmap_position {
531 int64_t time_nanoseconds; /**< timestamp in ns, CLOCK_MONOTONIC */
532 int32_t position_frames; /**< increasing 32 bit frame count reset when stream->stop()
536 /******************************
538 *****************************/
540 static inline bool audio_is_output_device(audio_devices_t device)
542 if (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
543 (popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL) == 0))
549 static inline bool audio_is_input_device(audio_devices_t device)
551 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
552 device &= ~AUDIO_DEVICE_BIT_IN;
553 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_ALL) == 0))
559 static inline bool audio_is_output_devices(audio_devices_t device)
561 return (device & AUDIO_DEVICE_BIT_IN) == 0;
564 static inline bool audio_is_a2dp_in_device(audio_devices_t device)
566 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
567 device &= ~AUDIO_DEVICE_BIT_IN;
568 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP))
574 static inline bool audio_is_a2dp_out_device(audio_devices_t device)
576 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP))
582 // Deprecated - use audio_is_a2dp_out_device() instead
583 static inline bool audio_is_a2dp_device(audio_devices_t device)
585 return audio_is_a2dp_out_device(device);
588 static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
590 if ((device & AUDIO_DEVICE_BIT_IN) == 0) {
591 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0))
594 device &= ~AUDIO_DEVICE_BIT_IN;
595 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0))
602 static inline bool audio_is_usb_out_device(audio_devices_t device)
604 return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB));
607 static inline bool audio_is_usb_in_device(audio_devices_t device)
609 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
610 device &= ~AUDIO_DEVICE_BIT_IN;
611 if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0)
617 /* OBSOLETE - use audio_is_usb_out_device() instead. */
618 static inline bool audio_is_usb_device(audio_devices_t device)
620 return audio_is_usb_out_device(device);
623 static inline bool audio_is_remote_submix_device(audio_devices_t device)
625 if ((audio_is_output_devices(device) &&
626 (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) == AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
627 || (!audio_is_output_devices(device) &&
628 (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX))
635 * representation is valid, and
636 * there is at least one channel bit set which _could_ correspond to an input channel, and
637 * there are no channel bits set which could _not_ correspond to an input channel.
638 * Otherwise returns false.
640 static inline bool audio_is_input_channel(audio_channel_mask_t channel)
642 uint32_t bits = audio_channel_mask_get_bits(channel);
643 switch (audio_channel_mask_get_representation(channel)) {
644 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
645 if (bits & ~AUDIO_CHANNEL_IN_ALL) {
649 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
657 * representation is valid, and
658 * there is at least one channel bit set which _could_ correspond to an output channel, and
659 * there are no channel bits set which could _not_ correspond to an output channel.
660 * Otherwise returns false.
662 static inline bool audio_is_output_channel(audio_channel_mask_t channel)
664 uint32_t bits = audio_channel_mask_get_bits(channel);
665 switch (audio_channel_mask_get_representation(channel)) {
666 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
667 if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
671 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
678 /* Returns the number of channels from an input channel mask,
679 * used in the context of audio input or recording.
680 * If a channel bit is set which could _not_ correspond to an input channel,
681 * it is excluded from the count.
682 * Returns zero if the representation is invalid.
684 static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
686 uint32_t bits = audio_channel_mask_get_bits(channel);
687 switch (audio_channel_mask_get_representation(channel)) {
688 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
689 // TODO: We can now merge with from_out_mask and remove anding
690 bits &= AUDIO_CHANNEL_IN_ALL;
692 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
693 return popcount(bits);
699 /* Returns the number of channels from an output channel mask,
700 * used in the context of audio output or playback.
701 * If a channel bit is set which could _not_ correspond to an output channel,
702 * it is excluded from the count.
703 * Returns zero if the representation is invalid.
705 static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
707 uint32_t bits = audio_channel_mask_get_bits(channel);
708 switch (audio_channel_mask_get_representation(channel)) {
709 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
710 // TODO: We can now merge with from_in_mask and remove anding
711 bits &= AUDIO_CHANNEL_OUT_ALL;
713 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
714 return popcount(bits);
720 /* Derive a channel mask for index assignment from a channel count.
721 * Returns the matching channel mask,
722 * or AUDIO_CHANNEL_NONE if the channel count is zero,
723 * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
725 static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
726 uint32_t channel_count)
728 if (channel_count == 0) {
729 return AUDIO_CHANNEL_NONE;
731 if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
732 return AUDIO_CHANNEL_INVALID;
734 uint32_t bits = (1 << channel_count) - 1;
735 return audio_channel_mask_from_representation_and_bits(
736 AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
739 /* Derive an output channel mask for position assignment from a channel count.
740 * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
741 * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
742 * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
743 * for continuity with stereo.
744 * Returns the matching channel mask,
745 * or AUDIO_CHANNEL_NONE if the channel count is zero,
746 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
747 * configurations for which a default output channel mask is defined.
749 static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
752 switch (channel_count) {
754 return AUDIO_CHANNEL_NONE;
756 bits = AUDIO_CHANNEL_OUT_MONO;
759 bits = AUDIO_CHANNEL_OUT_STEREO;
762 bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER;
765 bits = AUDIO_CHANNEL_OUT_QUAD;
768 bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
771 bits = AUDIO_CHANNEL_OUT_5POINT1;
774 bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
777 bits = AUDIO_CHANNEL_OUT_7POINT1;
781 return AUDIO_CHANNEL_INVALID;
783 return audio_channel_mask_from_representation_and_bits(
784 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
787 /* Derive a default input channel mask from a channel count.
788 * Assumes a position mask for mono and stereo, or an index mask for channel counts > 2.
789 * Returns the matching channel mask,
790 * or AUDIO_CHANNEL_NONE if the channel count is zero,
791 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
792 * configurations for which a default input channel mask is defined.
794 static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count)
797 switch (channel_count) {
799 return AUDIO_CHANNEL_NONE;
801 bits = AUDIO_CHANNEL_IN_MONO;
804 bits = AUDIO_CHANNEL_IN_STEREO;
813 return audio_channel_mask_for_index_assignment_from_count(channel_count);
815 return AUDIO_CHANNEL_INVALID;
817 return audio_channel_mask_from_representation_and_bits(
818 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
821 static inline audio_channel_mask_t audio_channel_mask_in_to_out(audio_channel_mask_t in)
824 case AUDIO_CHANNEL_IN_MONO:
825 return AUDIO_CHANNEL_OUT_MONO;
826 case AUDIO_CHANNEL_IN_STEREO:
827 return AUDIO_CHANNEL_OUT_STEREO;
828 case AUDIO_CHANNEL_IN_5POINT1:
829 return AUDIO_CHANNEL_OUT_5POINT1;
830 case AUDIO_CHANNEL_IN_3POINT1POINT2:
831 return AUDIO_CHANNEL_OUT_3POINT1POINT2;
832 case AUDIO_CHANNEL_IN_3POINT0POINT2:
833 return AUDIO_CHANNEL_OUT_3POINT0POINT2;
834 case AUDIO_CHANNEL_IN_2POINT1POINT2:
835 return AUDIO_CHANNEL_OUT_2POINT1POINT2;
836 case AUDIO_CHANNEL_IN_2POINT0POINT2:
837 return AUDIO_CHANNEL_OUT_2POINT0POINT2;
839 return AUDIO_CHANNEL_INVALID;
843 static inline bool audio_is_valid_format(audio_format_t format)
845 switch (format & AUDIO_FORMAT_MAIN_MASK) {
846 case AUDIO_FORMAT_PCM:
848 case AUDIO_FORMAT_PCM_16_BIT:
849 case AUDIO_FORMAT_PCM_8_BIT:
850 case AUDIO_FORMAT_PCM_32_BIT:
851 case AUDIO_FORMAT_PCM_8_24_BIT:
852 case AUDIO_FORMAT_PCM_FLOAT:
853 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
859 case AUDIO_FORMAT_MP3:
860 case AUDIO_FORMAT_AMR_NB:
861 case AUDIO_FORMAT_AMR_WB:
862 case AUDIO_FORMAT_AAC:
863 case AUDIO_FORMAT_AAC_ADTS:
864 case AUDIO_FORMAT_HE_AAC_V1:
865 case AUDIO_FORMAT_HE_AAC_V2:
866 case AUDIO_FORMAT_AAC_ELD:
867 case AUDIO_FORMAT_AAC_XHE:
868 case AUDIO_FORMAT_VORBIS:
869 case AUDIO_FORMAT_OPUS:
870 case AUDIO_FORMAT_AC3:
871 case AUDIO_FORMAT_E_AC3:
872 case AUDIO_FORMAT_DTS:
873 case AUDIO_FORMAT_DTS_HD:
874 case AUDIO_FORMAT_IEC61937:
875 case AUDIO_FORMAT_DOLBY_TRUEHD:
876 case AUDIO_FORMAT_QCELP:
877 case AUDIO_FORMAT_EVRC:
878 case AUDIO_FORMAT_EVRCB:
879 case AUDIO_FORMAT_EVRCWB:
880 case AUDIO_FORMAT_AAC_ADIF:
881 case AUDIO_FORMAT_AMR_WB_PLUS:
882 case AUDIO_FORMAT_MP2:
883 case AUDIO_FORMAT_EVRCNW:
884 case AUDIO_FORMAT_FLAC:
885 case AUDIO_FORMAT_ALAC:
886 case AUDIO_FORMAT_APE:
887 case AUDIO_FORMAT_WMA:
888 case AUDIO_FORMAT_WMA_PRO:
889 case AUDIO_FORMAT_DSD:
890 case AUDIO_FORMAT_AC4:
891 case AUDIO_FORMAT_LDAC:
892 case AUDIO_FORMAT_E_AC3_JOC:
893 case AUDIO_FORMAT_MAT_1_0:
894 case AUDIO_FORMAT_MAT_2_0:
895 case AUDIO_FORMAT_MAT_2_1:
903 * Extract the primary format, eg. PCM, AC3, etc.
905 static inline audio_format_t audio_get_main_format(audio_format_t format)
907 return (audio_format_t)(format & AUDIO_FORMAT_MAIN_MASK);
911 * Is the data plain PCM samples that can be scaled and mixed?
913 static inline bool audio_is_linear_pcm(audio_format_t format)
915 return (audio_get_main_format(format) == AUDIO_FORMAT_PCM);
919 * For this format, is the number of PCM audio frames directly proportional
920 * to the number of data bytes?
922 * In other words, is the format transported as PCM audio samples,
923 * but not necessarily scalable or mixable.
924 * This returns true for real PCM, but also for AUDIO_FORMAT_IEC61937,
925 * which is transported as 16 bit PCM audio, but where the encoded data
926 * cannot be mixed or scaled.
928 static inline bool audio_has_proportional_frames(audio_format_t format)
930 audio_format_t mainFormat = audio_get_main_format(format);
931 return (mainFormat == AUDIO_FORMAT_PCM
932 || mainFormat == AUDIO_FORMAT_IEC61937);
935 static inline size_t audio_bytes_per_sample(audio_format_t format)
940 case AUDIO_FORMAT_PCM_32_BIT:
941 case AUDIO_FORMAT_PCM_8_24_BIT:
942 size = sizeof(int32_t);
944 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
945 size = sizeof(uint8_t) * 3;
947 case AUDIO_FORMAT_PCM_16_BIT:
948 case AUDIO_FORMAT_IEC61937:
949 size = sizeof(int16_t);
951 case AUDIO_FORMAT_PCM_8_BIT:
952 size = sizeof(uint8_t);
954 case AUDIO_FORMAT_PCM_FLOAT:
955 size = sizeof(float);
963 static inline size_t audio_bytes_per_frame(uint32_t channel_count, audio_format_t format)
965 // cannot overflow for reasonable channel_count
966 return channel_count * audio_bytes_per_sample(format);
969 /* converts device address to string sent to audio HAL via set_parameters */
970 static inline char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
972 const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address=");
975 if (device & AUDIO_DEVICE_OUT_ALL_A2DP)
976 snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
977 else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
978 snprintf(param, kSize, "%s=%s", "mix", address);
980 snprintf(param, kSize, "%s", address);
982 return strdup(param);
985 static inline bool audio_device_is_digital(audio_devices_t device) {
986 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
988 return (~AUDIO_DEVICE_BIT_IN & device & (AUDIO_DEVICE_IN_ALL_USB |
989 AUDIO_DEVICE_IN_HDMI |
990 AUDIO_DEVICE_IN_SPDIF |
992 AUDIO_DEVICE_IN_BUS)) != 0;
995 return (device & (AUDIO_DEVICE_OUT_ALL_USB |
996 AUDIO_DEVICE_OUT_HDMI |
997 AUDIO_DEVICE_OUT_HDMI_ARC |
998 AUDIO_DEVICE_OUT_SPDIF |
999 AUDIO_DEVICE_OUT_IP |
1000 AUDIO_DEVICE_OUT_BUS)) != 0;
1004 // Unique effect ID (can be generated from the following site:
1005 // http://www.itu.int/ITU-T/asn1/uuid.html)
1006 // This struct is used for effects identification and in soundtrigger.
1007 typedef struct audio_uuid_s {
1010 uint16_t timeHiAndVersion;
1015 //TODO: audio_microphone_location_t need to move to HAL v4.0
1017 AUDIO_MICROPHONE_LOCATION_UNKNOWN = 0,
1018 AUDIO_MICROPHONE_LOCATION_MAINBODY = 1,
1019 AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE = 2,
1020 AUDIO_MICROPHONE_LOCATION_PERIPHERAL = 3,
1021 AUDIO_MICROPHONE_LOCATION_CNT = 4,
1022 } audio_microphone_location_t;
1024 //TODO: audio_microphone_directionality_t need to move to HAL v4.0
1026 AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN = 0,
1027 AUDIO_MICROPHONE_DIRECTIONALITY_OMNI = 1,
1028 AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL = 2,
1029 AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID = 3,
1030 AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID = 4,
1031 AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID = 5,
1032 AUDIO_MICROPHONE_DIRECTIONALITY_CNT = 6,
1033 } audio_microphone_directionality_t;
1035 /* A 3D point which could be used to represent geometric location
1036 * or orientation of a microphone.
1038 struct audio_microphone_coordinate {
1044 /* An number to indicate which group the microphone locate. Main body is
1045 * usually group 0. Developer could use this value to group the microphones
1046 * that locate on the same peripheral or attachments.
1048 typedef int audio_microphone_group_t;
1051 AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED = 0,
1052 AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT = 1,
1053 AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED = 2,
1054 } audio_microphone_channel_mapping_t;
1056 /* the maximum length for the microphone id */
1057 #define AUDIO_MICROPHONE_ID_MAX_LEN 32
1058 /* max number of frequency responses in a frequency response table */
1059 #define AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES 32
1061 struct audio_microphone_characteristic_t {
1062 char device_id[AUDIO_MICROPHONE_ID_MAX_LEN];
1063 audio_port_handle_t id;
1064 audio_devices_t device;
1065 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
1066 audio_microphone_channel_mapping_t channel_mapping[AUDIO_CHANNEL_COUNT_MAX];
1067 audio_microphone_location_t location;
1068 audio_microphone_group_t group;
1069 unsigned int index_in_the_group;
1073 audio_microphone_directionality_t directionality;
1074 unsigned int num_frequency_responses;
1075 float frequency_responses[2][AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES];
1076 struct audio_microphone_coordinate geometric_location;
1077 struct audio_microphone_coordinate orientation;
1083 * List of known audio HAL modules. This is the base name of the audio HAL
1084 * library composed of the "audio." prefix, one of the base names below and
1085 * a suffix specific to the device.
1086 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
1088 * The same module names are used in audio policy configuration files.
1091 #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
1092 #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
1093 #define AUDIO_HARDWARE_MODULE_ID_USB "usb"
1094 #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix"
1095 #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload"
1096 #define AUDIO_HARDWARE_MODULE_ID_STUB "stub"
1097 #define AUDIO_HARDWARE_MODULE_ID_HEARING_AID "hearing_aid"
1100 * Multi-Stream Decoder (MSD) HAL service name. MSD HAL is used to mix
1101 * encoded streams together with PCM streams, producing re-encoded
1102 * streams or PCM streams.
1104 * The service must register itself using this name, and audioserver
1105 * tries to instantiate a device factory using this name as well.
1106 * Note that the HIDL implementation library file name *must* have the
1107 * suffix "msd" in order to be picked up by HIDL that is:
1109 * android.hardware.audio@x.x-implmsd.so
1111 #define AUDIO_HAL_SERVICE_NAME_MSD "msd"
1114 * Parameter definitions.
1115 * Note that in the framework code it's recommended to use AudioParameter.h
1116 * instead of these preprocessor defines, and for sure avoid just copying
1117 * the constant values.
1120 #define AUDIO_PARAMETER_VALUE_ON "on"
1121 #define AUDIO_PARAMETER_VALUE_OFF "off"
1124 * audio device parameters
1127 /* BT SCO Noise Reduction + Echo Cancellation parameters */
1128 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec"
1130 /* Get a new HW synchronization source identifier.
1131 * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs
1132 * or no HW sync is available. */
1133 #define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync"
1136 #define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
1139 * audio stream parameters
1142 #define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */
1143 #define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */
1144 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */
1145 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */
1146 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */
1147 #define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */
1149 /* Request the presentation id to be decoded by a next gen audio decoder */
1150 #define AUDIO_PARAMETER_STREAM_PRESENTATION_ID "presentation_id" /* int32_t */
1152 /* Request the program id to be decoded by a next gen audio decoder */
1153 #define AUDIO_PARAMETER_STREAM_PROGRAM_ID "program_id" /* int32_t */
1155 #define AUDIO_PARAMETER_DEVICE_CONNECT "connect" /* audio_devices_t */
1156 #define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect" /* audio_devices_t */
1158 /* Enable mono audio playback if 1, else should be 0. */
1159 #define AUDIO_PARAMETER_MONO_OUTPUT "mono_output"
1161 /* Set the HW synchronization source for an output stream. */
1162 #define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync"
1164 /* Query supported formats. The response is a '|' separated list of strings from
1165 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
1166 #define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
1167 /* Query supported channel masks. The response is a '|' separated list of strings from
1168 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
1169 #define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
1170 /* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
1171 * "sup_sampling_rates=44100|48000" */
1172 #define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
1174 #define AUDIO_PARAMETER_VALUE_LIST_SEPARATOR "|"
1177 * audio codec parameters
1180 #define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param"
1181 #define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample"
1182 #define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate"
1183 #define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate"
1184 #define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id"
1185 #define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align"
1186 #define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate"
1187 #define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option"
1188 #define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels"
1189 #define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling"
1190 #define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples"
1191 #define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples"
1193 #endif // ANDROID_AUDIO_CORE_H