1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 #ifndef ANDROID_AUDIO_CORE_H 19 #define ANDROID_AUDIO_CORE_H 20 21 #include <stdbool.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <sys/cdefs.h> 25 #include <sys/types.h> 26 27 #include <cutils/bitops.h> 28 29 #include "audio-base.h" 30 31 __BEGIN_DECLS 32 33 /* The enums were moved here mostly from 34 * frameworks/base/include/media/AudioSystem.h 35 */ 36 37 /* represents an invalid uid for tracks; the calling or client uid is often substituted. */ 38 #define AUDIO_UID_INVALID ((uid_t)-1) 39 40 /* device address used to refer to the standard remote submix */ 41 #define AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS "0" 42 43 /* AudioFlinger and AudioPolicy services use I/O handles to identify audio sources and sinks */ 44 typedef int audio_io_handle_t; 45 46 /* Do not change these values without updating their counterparts 47 * in frameworks/base/media/java/android/media/AudioAttributes.java 48 */ 49 typedef enum { 50 AUDIO_CONTENT_TYPE_UNKNOWN = 0, 51 AUDIO_CONTENT_TYPE_SPEECH = 1, 52 AUDIO_CONTENT_TYPE_MUSIC = 2, 53 AUDIO_CONTENT_TYPE_MOVIE = 3, 54 AUDIO_CONTENT_TYPE_SONIFICATION = 4, 55 56 AUDIO_CONTENT_TYPE_CNT, 57 AUDIO_CONTENT_TYPE_MAX = AUDIO_CONTENT_TYPE_CNT - 1, 58 } audio_content_type_t; 59 60 typedef uint32_t audio_flags_mask_t; 61 62 /* Do not change these values without updating their counterparts 63 * in frameworks/base/media/java/android/media/AudioAttributes.java 64 */ 65 enum { 66 AUDIO_FLAG_NONE = 0x0, 67 AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1, 68 AUDIO_FLAG_SECURE = 0x2, 69 AUDIO_FLAG_SCO = 0x4, 70 AUDIO_FLAG_BEACON = 0x8, 71 AUDIO_FLAG_HW_AV_SYNC = 0x10, 72 AUDIO_FLAG_HW_HOTWORD = 0x20, 73 AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY = 0x40, 74 AUDIO_FLAG_BYPASS_MUTE = 0x80, 75 AUDIO_FLAG_LOW_LATENCY = 0x100, 76 AUDIO_FLAG_DEEP_BUFFER = 0x200, 77 }; 78 79 /* Audio attributes */ 80 #define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256 81 typedef struct { 82 audio_content_type_t content_type; 83 audio_usage_t usage; 84 audio_source_t source; 85 audio_flags_mask_t flags; 86 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */ 87 } audio_attributes_t; 88 89 /* a unique ID allocated by AudioFlinger for use as an audio_io_handle_t, audio_session_t, 90 * effect ID (int), audio_module_handle_t, and audio_patch_handle_t. 91 * Audio port IDs (audio_port_handle_t) are allocated by AudioPolicy 92 * in a different namespace than AudioFlinger unique IDs. 93 */ 94 typedef int audio_unique_id_t; 95 96 /* Possible uses for an audio_unique_id_t */ 97 typedef enum { 98 AUDIO_UNIQUE_ID_USE_UNSPECIFIED = 0, 99 AUDIO_UNIQUE_ID_USE_SESSION = 1, // for allocated sessions, not special AUDIO_SESSION_* 100 AUDIO_UNIQUE_ID_USE_MODULE = 2, 101 AUDIO_UNIQUE_ID_USE_EFFECT = 3, 102 AUDIO_UNIQUE_ID_USE_PATCH = 4, 103 AUDIO_UNIQUE_ID_USE_OUTPUT = 5, 104 AUDIO_UNIQUE_ID_USE_INPUT = 6, 105 AUDIO_UNIQUE_ID_USE_PLAYER = 7, 106 AUDIO_UNIQUE_ID_USE_MAX = 8, // must be a power-of-two 107 AUDIO_UNIQUE_ID_USE_MASK = AUDIO_UNIQUE_ID_USE_MAX - 1 108 } audio_unique_id_use_t; 109 110 /* Return the use of an audio_unique_id_t */ 111 static inline audio_unique_id_use_t audio_unique_id_get_use(audio_unique_id_t id) 112 { 113 return (audio_unique_id_use_t) (id & AUDIO_UNIQUE_ID_USE_MASK); 114 } 115 116 /* Reserved audio_unique_id_t values. FIXME: not a complete list. */ 117 #define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE 118 119 /* A channel mask per se only defines the presence or absence of a channel, not the order. 120 * But see AUDIO_INTERLEAVE_* below for the platform convention of order. 121 * 122 * audio_channel_mask_t is an opaque type and its internal layout should not 123 * be assumed as it may change in the future. 124 * Instead, always use the functions declared in this header to examine. 125 * 126 * These are the current representations: 127 * 128 * AUDIO_CHANNEL_REPRESENTATION_POSITION 129 * is a channel mask representation for position assignment. 130 * Each low-order bit corresponds to the spatial position of a transducer (output), 131 * or interpretation of channel (input). 132 * The user of a channel mask needs to know the context of whether it is for output or input. 133 * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion. 134 * It is not permitted for no bits to be set. 135 * 136 * AUDIO_CHANNEL_REPRESENTATION_INDEX 137 * is a channel mask representation for index assignment. 138 * Each low-order bit corresponds to a selected channel. 139 * There is no platform interpretation of the various bits. 140 * There is no concept of output or input. 141 * It is not permitted for no bits to be set. 142 * 143 * All other representations are reserved for future use. 144 * 145 * Warning: current representation distinguishes between input and output, but this will not the be 146 * case in future revisions of the platform. Wherever there is an ambiguity between input and output 147 * that is currently resolved by checking the channel mask, the implementer should look for ways to 148 * fix it with additional information outside of the mask. 149 */ 150 typedef uint32_t audio_channel_mask_t; 151 152 /* log(2) of maximum number of representations, not part of public API */ 153 #define AUDIO_CHANNEL_REPRESENTATION_LOG2 2 154 155 /* The return value is undefined if the channel mask is invalid. */ 156 static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel) 157 { 158 return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1); 159 } 160 161 typedef uint32_t audio_channel_representation_t; 162 163 /* The return value is undefined if the channel mask is invalid. */ 164 static inline audio_channel_representation_t audio_channel_mask_get_representation( 165 audio_channel_mask_t channel) 166 { 167 // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits 168 return (audio_channel_representation_t) 169 ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1)); 170 } 171 172 /* Returns true if the channel mask is valid, 173 * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values. 174 * This function is unable to determine whether a channel mask for position assignment 175 * is invalid because an output mask has an invalid output bit set, 176 * or because an input mask has an invalid input bit set. 177 * All other APIs that take a channel mask assume that it is valid. 178 */ 179 static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel) 180 { 181 uint32_t bits = audio_channel_mask_get_bits(channel); 182 audio_channel_representation_t representation = audio_channel_mask_get_representation(channel); 183 switch (representation) { 184 case AUDIO_CHANNEL_REPRESENTATION_POSITION: 185 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 186 break; 187 default: 188 bits = 0; 189 break; 190 } 191 return bits != 0; 192 } 193 194 /* Not part of public API */ 195 static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits( 196 audio_channel_representation_t representation, uint32_t bits) 197 { 198 return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits); 199 } 200 201 /* This enum is deprecated */ 202 typedef enum { 203 AUDIO_IN_ACOUSTICS_NONE = 0, 204 AUDIO_IN_ACOUSTICS_AGC_ENABLE = 0x0001, 205 AUDIO_IN_ACOUSTICS_AGC_DISABLE = 0, 206 AUDIO_IN_ACOUSTICS_NS_ENABLE = 0x0002, 207 AUDIO_IN_ACOUSTICS_NS_DISABLE = 0, 208 AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004, 209 AUDIO_IN_ACOUSTICS_TX_DISABLE = 0, 210 } audio_in_acoustics_t; 211 212 typedef uint32_t audio_devices_t; 213 /** 214 * Stub audio output device. Used in policy configuration file on platforms without audio outputs. 215 * This alias value to AUDIO_DEVICE_OUT_DEFAULT is only used in the audio policy context. 216 */ 217 #define AUDIO_DEVICE_OUT_STUB AUDIO_DEVICE_OUT_DEFAULT 218 /** 219 * Stub audio input device. Used in policy configuration file on platforms without audio inputs. 220 * This alias value to AUDIO_DEVICE_IN_DEFAULT is only used in the audio policy context. 221 */ 222 #define AUDIO_DEVICE_IN_STUB AUDIO_DEVICE_IN_DEFAULT 223 224 /* Additional information about compressed streams offloaded to 225 * hardware playback 226 * The version and size fields must be initialized by the caller by using 227 * one of the constants defined here. 228 */ 229 typedef struct { 230 uint16_t version; // version of the info structure 231 uint16_t size; // total size of the structure including version and size 232 uint32_t sample_rate; // sample rate in Hz 233 audio_channel_mask_t channel_mask; // channel mask 234 audio_format_t format; // audio format 235 audio_stream_type_t stream_type; // stream type 236 uint32_t bit_rate; // bit rate in bits per second 237 int64_t duration_us; // duration in microseconds, -1 if unknown 238 bool has_video; // true if stream is tied to a video stream 239 bool is_streaming; // true if streaming, false if local playback 240 uint32_t bit_width; 241 uint32_t offload_buffer_size; // offload fragment size 242 audio_usage_t usage; 243 } audio_offload_info_t; 244 245 #define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \ 246 ((((maj) & 0xff) << 8) | ((min) & 0xff)) 247 248 #define AUDIO_OFFLOAD_INFO_VERSION_0_1 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 1) 249 #define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_1 250 251 static const audio_offload_info_t AUDIO_INFO_INITIALIZER = { 252 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT, 253 /* .size = */ sizeof(audio_offload_info_t), 254 /* .sample_rate = */ 0, 255 /* .channel_mask = */ 0, 256 /* .format = */ AUDIO_FORMAT_DEFAULT, 257 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL, 258 /* .bit_rate = */ 0, 259 /* .duration_us = */ 0, 260 /* .has_video = */ false, 261 /* .is_streaming = */ false, 262 /* .bit_width = */ 16, 263 /* .offload_buffer_size = */ 0, 264 /* .usage = */ AUDIO_USAGE_UNKNOWN 265 }; 266 267 /* common audio stream configuration parameters 268 * You should memset() the entire structure to zero before use to 269 * ensure forward compatibility 270 */ 271 struct audio_config { 272 uint32_t sample_rate; 273 audio_channel_mask_t channel_mask; 274 audio_format_t format; 275 audio_offload_info_t offload_info; 276 size_t frame_count; 277 }; 278 typedef struct audio_config audio_config_t; 279 280 static const audio_config_t AUDIO_CONFIG_INITIALIZER = { 281 /* .sample_rate = */ 0, 282 /* .channel_mask = */ AUDIO_CHANNEL_NONE, 283 /* .format = */ AUDIO_FORMAT_DEFAULT, 284 /* .offload_info = */ { 285 /* .version = */ AUDIO_OFFLOAD_INFO_VERSION_CURRENT, 286 /* .size = */ sizeof(audio_offload_info_t), 287 /* .sample_rate = */ 0, 288 /* .channel_mask = */ 0, 289 /* .format = */ AUDIO_FORMAT_DEFAULT, 290 /* .stream_type = */ AUDIO_STREAM_VOICE_CALL, 291 /* .bit_rate = */ 0, 292 /* .duration_us = */ 0, 293 /* .has_video = */ false, 294 /* .is_streaming = */ false, 295 /* .bit_width = */ 16, 296 /* .offload_buffer_size = */ 0, 297 /* .usage = */ AUDIO_USAGE_UNKNOWN 298 }, 299 /* .frame_count = */ 0, 300 }; 301 302 struct audio_config_base { 303 uint32_t sample_rate; 304 audio_channel_mask_t channel_mask; 305 audio_format_t format; 306 }; 307 308 typedef struct audio_config_base audio_config_base_t; 309 310 static const audio_config_base_t AUDIO_CONFIG_BASE_INITIALIZER = { 311 /* .sample_rate = */ 0, 312 /* .channel_mask = */ AUDIO_CHANNEL_NONE, 313 /* .format = */ AUDIO_FORMAT_DEFAULT 314 }; 315 316 /* audio hw module handle functions or structures referencing a module */ 317 typedef int audio_module_handle_t; 318 319 /****************************** 320 * Volume control 321 *****************************/ 322 323 /* If the audio hardware supports gain control on some audio paths, 324 * the platform can expose them in the audio_policy.conf file. The audio HAL 325 * will then implement gain control functions that will use the following data 326 * structures. */ 327 328 typedef uint32_t audio_gain_mode_t; 329 330 331 /* An audio_gain struct is a representation of a gain stage. 332 * A gain stage is always attached to an audio port. */ 333 struct audio_gain { 334 audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */ 335 audio_channel_mask_t channel_mask; /* channels which gain an be controlled. 336 N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */ 337 int min_value; /* minimum gain value in millibels */ 338 int max_value; /* maximum gain value in millibels */ 339 int default_value; /* default gain value in millibels */ 340 unsigned int step_value; /* gain step in millibels */ 341 unsigned int min_ramp_ms; /* minimum ramp duration in ms */ 342 unsigned int max_ramp_ms; /* maximum ramp duration in ms */ 343 }; 344 345 /* The gain configuration structure is used to get or set the gain values of a 346 * given port */ 347 struct audio_gain_config { 348 int index; /* index of the corresponding audio_gain in the 349 audio_port gains[] table */ 350 audio_gain_mode_t mode; /* mode requested for this command */ 351 audio_channel_mask_t channel_mask; /* channels which gain value follows. 352 N/A in joint mode */ 353 354 // note this "8" is not FCC_8, so it won't need to be changed for > 8 channels 355 int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels 356 for each channel ordered from LSb to MSb in 357 channel mask. The number of values is 1 in joint 358 mode or popcount(channel_mask) */ 359 unsigned int ramp_duration_ms; /* ramp duration in ms */ 360 }; 361 362 /****************************** 363 * Routing control 364 *****************************/ 365 366 /* Types defined here are used to describe an audio source or sink at internal 367 * framework interfaces (audio policy, patch panel) or at the audio HAL. 368 * Sink and sources are grouped in a concept of audio port representing an 369 * audio end point at the edge of the system managed by the module exposing 370 * the interface. */ 371 372 /* Each port has a unique ID or handle allocated by policy manager */ 373 typedef int audio_port_handle_t; 374 375 /* the maximum length for the human-readable device name */ 376 #define AUDIO_PORT_MAX_NAME_LEN 128 377 378 /* maximum audio device address length */ 379 #define AUDIO_DEVICE_MAX_ADDRESS_LEN 32 380 381 /* extension for audio port configuration structure when the audio port is a 382 * hardware device */ 383 struct audio_port_config_device_ext { 384 audio_module_handle_t hw_module; /* module the device is attached to */ 385 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */ 386 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */ 387 }; 388 389 /* extension for audio port configuration structure when the audio port is a 390 * sub mix */ 391 struct audio_port_config_mix_ext { 392 audio_module_handle_t hw_module; /* module the stream is attached to */ 393 audio_io_handle_t handle; /* I/O handle of the input/output stream */ 394 union { 395 //TODO: change use case for output streams: use strategy and mixer attributes 396 audio_stream_type_t stream; 397 audio_source_t source; 398 } usecase; 399 }; 400 401 /* extension for audio port configuration structure when the audio port is an 402 * audio session */ 403 struct audio_port_config_session_ext { 404 audio_session_t session; /* audio session */ 405 }; 406 407 /* audio port configuration structure used to specify a particular configuration of 408 * an audio port */ 409 struct audio_port_config { 410 audio_port_handle_t id; /* port unique ID */ 411 audio_port_role_t role; /* sink or source */ 412 audio_port_type_t type; /* device, mix ... */ 413 unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */ 414 unsigned int sample_rate; /* sampling rate in Hz */ 415 audio_channel_mask_t channel_mask; /* channel mask if applicable */ 416 audio_format_t format; /* format if applicable */ 417 struct audio_gain_config gain; /* gain to apply if applicable */ 418 union { 419 struct audio_port_config_device_ext device; /* device specific info */ 420 struct audio_port_config_mix_ext mix; /* mix specific info */ 421 struct audio_port_config_session_ext session; /* session specific info */ 422 } ext; 423 }; 424 425 426 /* max number of sampling rates in audio port */ 427 #define AUDIO_PORT_MAX_SAMPLING_RATES 32 428 /* max number of channel masks in audio port */ 429 #define AUDIO_PORT_MAX_CHANNEL_MASKS 32 430 /* max number of audio formats in audio port */ 431 #define AUDIO_PORT_MAX_FORMATS 32 432 /* max number of gain controls in audio port */ 433 #define AUDIO_PORT_MAX_GAINS 16 434 435 /* extension for audio port structure when the audio port is a hardware device */ 436 struct audio_port_device_ext { 437 audio_module_handle_t hw_module; /* module the device is attached to */ 438 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */ 439 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; 440 }; 441 442 /* extension for audio port structure when the audio port is a sub mix */ 443 struct audio_port_mix_ext { 444 audio_module_handle_t hw_module; /* module the stream is attached to */ 445 audio_io_handle_t handle; /* I/O handle of the input.output stream */ 446 audio_mix_latency_class_t latency_class; /* latency class */ 447 // other attributes: routing strategies 448 }; 449 450 /* extension for audio port structure when the audio port is an audio session */ 451 struct audio_port_session_ext { 452 audio_session_t session; /* audio session */ 453 }; 454 455 struct audio_port { 456 audio_port_handle_t id; /* port unique ID */ 457 audio_port_role_t role; /* sink or source */ 458 audio_port_type_t type; /* device, mix ... */ 459 char name[AUDIO_PORT_MAX_NAME_LEN]; 460 unsigned int num_sample_rates; /* number of sampling rates in following array */ 461 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES]; 462 unsigned int num_channel_masks; /* number of channel masks in following array */ 463 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS]; 464 unsigned int num_formats; /* number of formats in following array */ 465 audio_format_t formats[AUDIO_PORT_MAX_FORMATS]; 466 unsigned int num_gains; /* number of gains in following array */ 467 struct audio_gain gains[AUDIO_PORT_MAX_GAINS]; 468 struct audio_port_config active_config; /* current audio port configuration */ 469 union { 470 struct audio_port_device_ext device; 471 struct audio_port_mix_ext mix; 472 struct audio_port_session_ext session; 473 } ext; 474 }; 475 476 /* An audio patch represents a connection between one or more source ports and 477 * one or more sink ports. Patches are connected and disconnected by audio policy manager or by 478 * applications via framework APIs. 479 * Each patch is identified by a handle at the interface used to create that patch. For instance, 480 * when a patch is created by the audio HAL, the HAL allocates and returns a handle. 481 * This handle is unique to a given audio HAL hardware module. 482 * But the same patch receives another system wide unique handle allocated by the framework. 483 * This unique handle is used for all transactions inside the framework. 484 */ 485 typedef int audio_patch_handle_t; 486 487 #define AUDIO_PATCH_PORTS_MAX 16 488 489 struct audio_patch { 490 audio_patch_handle_t id; /* patch unique ID */ 491 unsigned int num_sources; /* number of sources in following array */ 492 struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX]; 493 unsigned int num_sinks; /* number of sinks in following array */ 494 struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX]; 495 }; 496 497 498 499 /* a HW synchronization source returned by the audio HAL */ 500 typedef uint32_t audio_hw_sync_t; 501 502 /* an invalid HW synchronization source indicating an error */ 503 #define AUDIO_HW_SYNC_INVALID 0 504 505 /** 506 * Mmap buffer descriptor returned by audio_stream->create_mmap_buffer(). 507 * note\ Used by streams opened in mmap mode. 508 */ 509 struct audio_mmap_buffer_info { 510 void* shared_memory_address; /**< base address of mmap memory buffer. 511 For use by local process only */ 512 int32_t shared_memory_fd; /**< FD for mmap memory buffer */ 513 int32_t buffer_size_frames; /**< total buffer size in frames */ 514 int32_t burst_size_frames; /**< transfer size granularity in frames */ 515 }; 516 517 /** 518 * Mmap buffer read/write position returned by audio_stream->get_mmap_position(). 519 * note\ Used by streams opened in mmap mode. 520 */ 521 struct audio_mmap_position { 522 int64_t time_nanoseconds; /**< timestamp in ns, CLOCK_MONOTONIC */ 523 int32_t position_frames; /**< increasing 32 bit frame count reset when stream->stop() 524 is called */ 525 }; 526 527 static inline bool audio_is_output_device(audio_devices_t device) 528 { 529 if (((device & AUDIO_DEVICE_BIT_IN) == 0) && 530 (popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL) == 0)) 531 return true; 532 else 533 return false; 534 } 535 536 static inline bool audio_is_input_device(audio_devices_t device) 537 { 538 if ((device & AUDIO_DEVICE_BIT_IN) != 0) { 539 device &= ~AUDIO_DEVICE_BIT_IN; 540 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_ALL) == 0)) 541 return true; 542 } 543 return false; 544 } 545 546 static inline bool audio_is_output_devices(audio_devices_t device) 547 { 548 return (device & AUDIO_DEVICE_BIT_IN) == 0; 549 } 550 551 static inline bool audio_is_a2dp_in_device(audio_devices_t device) 552 { 553 if ((device & AUDIO_DEVICE_BIT_IN) != 0) { 554 device &= ~AUDIO_DEVICE_BIT_IN; 555 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP)) 556 return true; 557 } 558 return false; 559 } 560 561 static inline bool audio_is_a2dp_out_device(audio_devices_t device) 562 { 563 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP)) 564 return true; 565 else 566 return false; 567 } 568 569 // Deprecated - use audio_is_a2dp_out_device() instead 570 static inline bool audio_is_a2dp_device(audio_devices_t device) 571 { 572 return audio_is_a2dp_out_device(device); 573 } 574 575 static inline bool audio_is_bluetooth_sco_device(audio_devices_t device) 576 { 577 if ((device & AUDIO_DEVICE_BIT_IN) == 0) { 578 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0)) 579 return true; 580 } else { 581 device &= ~AUDIO_DEVICE_BIT_IN; 582 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0)) 583 return true; 584 } 585 586 return false; 587 } 588 589 static inline bool audio_is_usb_out_device(audio_devices_t device) 590 { 591 return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB)); 592 } 593 594 static inline bool audio_is_usb_in_device(audio_devices_t device) 595 { 596 if ((device & AUDIO_DEVICE_BIT_IN) != 0) { 597 device &= ~AUDIO_DEVICE_BIT_IN; 598 if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0) 599 return true; 600 } 601 return false; 602 } 603 604 /* OBSOLETE - use audio_is_usb_out_device() instead. */ 605 static inline bool audio_is_usb_device(audio_devices_t device) 606 { 607 return audio_is_usb_out_device(device); 608 } 609 610 static inline bool audio_is_remote_submix_device(audio_devices_t device) 611 { 612 if ((audio_is_output_devices(device) && 613 (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) 614 || (!audio_is_output_devices(device) && 615 (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX)) 616 return true; 617 else 618 return false; 619 } 620 621 /* Returns true if: 622 * representation is valid, and 623 * there is at least one channel bit set which _could_ correspond to an input channel, and 624 * there are no channel bits set which could _not_ correspond to an input channel. 625 * Otherwise returns false. 626 */ 627 static inline bool audio_is_input_channel(audio_channel_mask_t channel) 628 { 629 uint32_t bits = audio_channel_mask_get_bits(channel); 630 switch (audio_channel_mask_get_representation(channel)) { 631 case AUDIO_CHANNEL_REPRESENTATION_POSITION: 632 if (bits & ~AUDIO_CHANNEL_IN_ALL) { 633 bits = 0; 634 } 635 // fall through 636 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 637 return bits != 0; 638 default: 639 return false; 640 } 641 } 642 643 /* Returns true if: 644 * representation is valid, and 645 * there is at least one channel bit set which _could_ correspond to an output channel, and 646 * there are no channel bits set which could _not_ correspond to an output channel. 647 * Otherwise returns false. 648 */ 649 static inline bool audio_is_output_channel(audio_channel_mask_t channel) 650 { 651 uint32_t bits = audio_channel_mask_get_bits(channel); 652 switch (audio_channel_mask_get_representation(channel)) { 653 case AUDIO_CHANNEL_REPRESENTATION_POSITION: 654 if (bits & ~AUDIO_CHANNEL_OUT_ALL) { 655 bits = 0; 656 } 657 // fall through 658 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 659 return bits != 0; 660 default: 661 return false; 662 } 663 } 664 665 /* Returns the number of channels from an input channel mask, 666 * used in the context of audio input or recording. 667 * If a channel bit is set which could _not_ correspond to an input channel, 668 * it is excluded from the count. 669 * Returns zero if the representation is invalid. 670 */ 671 static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel) 672 { 673 uint32_t bits = audio_channel_mask_get_bits(channel); 674 switch (audio_channel_mask_get_representation(channel)) { 675 case AUDIO_CHANNEL_REPRESENTATION_POSITION: 676 // TODO: We can now merge with from_out_mask and remove anding 677 bits &= AUDIO_CHANNEL_IN_ALL; 678 // fall through 679 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 680 return popcount(bits); 681 default: 682 return 0; 683 } 684 } 685 686 /* Returns the number of channels from an output channel mask, 687 * used in the context of audio output or playback. 688 * If a channel bit is set which could _not_ correspond to an output channel, 689 * it is excluded from the count. 690 * Returns zero if the representation is invalid. 691 */ 692 static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel) 693 { 694 uint32_t bits = audio_channel_mask_get_bits(channel); 695 switch (audio_channel_mask_get_representation(channel)) { 696 case AUDIO_CHANNEL_REPRESENTATION_POSITION: 697 // TODO: We can now merge with from_in_mask and remove anding 698 bits &= AUDIO_CHANNEL_OUT_ALL; 699 // fall through 700 case AUDIO_CHANNEL_REPRESENTATION_INDEX: 701 return popcount(bits); 702 default: 703 return 0; 704 } 705 } 706 707 /* Derive a channel mask for index assignment from a channel count. 708 * Returns the matching channel mask, 709 * or AUDIO_CHANNEL_NONE if the channel count is zero, 710 * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX. 711 */ 712 static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count( 713 uint32_t channel_count) 714 { 715 if (channel_count == 0) { 716 return AUDIO_CHANNEL_NONE; 717 } 718 if (channel_count > AUDIO_CHANNEL_COUNT_MAX) { 719 return AUDIO_CHANNEL_INVALID; 720 } 721 uint32_t bits = (1 << channel_count) - 1; 722 return audio_channel_mask_from_representation_and_bits( 723 AUDIO_CHANNEL_REPRESENTATION_INDEX, bits); 724 } 725 726 /* Derive an output channel mask for position assignment from a channel count. 727 * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel 728 * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad, 729 * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC 730 * for continuity with stereo. 731 * Returns the matching channel mask, 732 * or AUDIO_CHANNEL_NONE if the channel count is zero, 733 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the 734 * configurations for which a default output channel mask is defined. 735 */ 736 static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count) 737 { 738 uint32_t bits; 739 switch (channel_count) { 740 case 0: 741 return AUDIO_CHANNEL_NONE; 742 case 1: 743 bits = AUDIO_CHANNEL_OUT_MONO; 744 break; 745 case 2: 746 bits = AUDIO_CHANNEL_OUT_STEREO; 747 break; 748 case 3: 749 bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER; 750 break; 751 case 4: // 4.0 752 bits = AUDIO_CHANNEL_OUT_QUAD; 753 break; 754 case 5: // 5.0 755 bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER; 756 break; 757 case 6: // 5.1 758 bits = AUDIO_CHANNEL_OUT_5POINT1; 759 break; 760 case 7: // 6.1 761 bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER; 762 break; 763 case 8: 764 bits = AUDIO_CHANNEL_OUT_7POINT1; 765 break; 766 // FIXME FCC_8 767 default: 768 return AUDIO_CHANNEL_INVALID; 769 } 770 return audio_channel_mask_from_representation_and_bits( 771 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits); 772 } 773 774 /* Derive a default input channel mask from a channel count. 775 * Assumes a position mask for mono and stereo, or an index mask for channel counts > 2. 776 * Returns the matching channel mask, 777 * or AUDIO_CHANNEL_NONE if the channel count is zero, 778 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the 779 * configurations for which a default input channel mask is defined. 780 */ 781 static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count) 782 { 783 uint32_t bits; 784 switch (channel_count) { 785 case 0: 786 return AUDIO_CHANNEL_NONE; 787 case 1: 788 bits = AUDIO_CHANNEL_IN_MONO; 789 break; 790 case 2: 791 bits = AUDIO_CHANNEL_IN_STEREO; 792 break; 793 case 3: 794 case 4: 795 case 5: 796 case 6: 797 case 7: 798 case 8: 799 // FIXME FCC_8 800 return audio_channel_mask_for_index_assignment_from_count(channel_count); 801 default: 802 return AUDIO_CHANNEL_INVALID; 803 } 804 return audio_channel_mask_from_representation_and_bits( 805 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits); 806 } 807 808 static inline bool audio_is_valid_format(audio_format_t format) 809 { 810 switch (format & AUDIO_FORMAT_MAIN_MASK) { 811 case AUDIO_FORMAT_PCM: 812 switch (format) { 813 case AUDIO_FORMAT_PCM_16_BIT: 814 case AUDIO_FORMAT_PCM_8_BIT: 815 case AUDIO_FORMAT_PCM_32_BIT: 816 case AUDIO_FORMAT_PCM_8_24_BIT: 817 case AUDIO_FORMAT_PCM_FLOAT: 818 case AUDIO_FORMAT_PCM_24_BIT_PACKED: 819 return true; 820 default: 821 return false; 822 } 823 /* not reached */ 824 case AUDIO_FORMAT_MP3: 825 case AUDIO_FORMAT_AMR_NB: 826 case AUDIO_FORMAT_AMR_WB: 827 case AUDIO_FORMAT_AAC: 828 case AUDIO_FORMAT_AAC_ADTS: 829 case AUDIO_FORMAT_HE_AAC_V1: 830 case AUDIO_FORMAT_HE_AAC_V2: 831 case AUDIO_FORMAT_VORBIS: 832 case AUDIO_FORMAT_OPUS: 833 case AUDIO_FORMAT_AC3: 834 case AUDIO_FORMAT_E_AC3: 835 case AUDIO_FORMAT_DTS: 836 case AUDIO_FORMAT_DTS_HD: 837 case AUDIO_FORMAT_IEC61937: 838 case AUDIO_FORMAT_DOLBY_TRUEHD: 839 case AUDIO_FORMAT_QCELP: 840 case AUDIO_FORMAT_EVRC: 841 case AUDIO_FORMAT_EVRCB: 842 case AUDIO_FORMAT_EVRCWB: 843 case AUDIO_FORMAT_AAC_ADIF: 844 case AUDIO_FORMAT_AMR_WB_PLUS: 845 case AUDIO_FORMAT_MP2: 846 case AUDIO_FORMAT_EVRCNW: 847 case AUDIO_FORMAT_FLAC: 848 case AUDIO_FORMAT_ALAC: 849 case AUDIO_FORMAT_APE: 850 case AUDIO_FORMAT_WMA: 851 case AUDIO_FORMAT_WMA_PRO: 852 case AUDIO_FORMAT_DSD: 853 case AUDIO_FORMAT_AC4: 854 case AUDIO_FORMAT_LDAC: 855 return true; 856 default: 857 return false; 858 } 859 } 860 861 /** 862 * Extract the primary format, eg. PCM, AC3, etc. 863 */ 864 static inline audio_format_t audio_get_main_format(audio_format_t format) 865 { 866 return (audio_format_t)(format & AUDIO_FORMAT_MAIN_MASK); 867 } 868 869 /** 870 * Is the data plain PCM samples that can be scaled and mixed? 871 */ 872 static inline bool audio_is_linear_pcm(audio_format_t format) 873 { 874 return (audio_get_main_format(format) == AUDIO_FORMAT_PCM); 875 } 876 877 /** 878 * For this format, is the number of PCM audio frames directly proportional 879 * to the number of data bytes? 880 * 881 * In other words, is the format transported as PCM audio samples, 882 * but not necessarily scalable or mixable. 883 * This returns true for real PCM, but also for AUDIO_FORMAT_IEC61937, 884 * which is transported as 16 bit PCM audio, but where the encoded data 885 * cannot be mixed or scaled. 886 */ 887 static inline bool audio_has_proportional_frames(audio_format_t format) 888 { 889 audio_format_t mainFormat = audio_get_main_format(format); 890 return (mainFormat == AUDIO_FORMAT_PCM 891 || mainFormat == AUDIO_FORMAT_IEC61937); 892 } 893 894 static inline size_t audio_bytes_per_sample(audio_format_t format) 895 { 896 size_t size = 0; 897 898 switch (format) { 899 case AUDIO_FORMAT_PCM_32_BIT: 900 case AUDIO_FORMAT_PCM_8_24_BIT: 901 size = sizeof(int32_t); 902 break; 903 case AUDIO_FORMAT_PCM_24_BIT_PACKED: 904 size = sizeof(uint8_t) * 3; 905 break; 906 case AUDIO_FORMAT_PCM_16_BIT: 907 case AUDIO_FORMAT_IEC61937: 908 size = sizeof(int16_t); 909 break; 910 case AUDIO_FORMAT_PCM_8_BIT: 911 size = sizeof(uint8_t); 912 break; 913 case AUDIO_FORMAT_PCM_FLOAT: 914 size = sizeof(float); 915 break; 916 default: 917 break; 918 } 919 return size; 920 } 921 922 /* converts device address to string sent to audio HAL via set_parameters */ 923 static inline char *audio_device_address_to_parameter(audio_devices_t device, const char *address) 924 { 925 const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address="); 926 char param[kSize]; 927 928 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) 929 snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address); 930 else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) 931 snprintf(param, kSize, "%s=%s", "mix", address); 932 else 933 snprintf(param, kSize, "%s", address); 934 935 return strdup(param); 936 } 937 938 static inline bool audio_device_is_digital(audio_devices_t device) { 939 if ((device & AUDIO_DEVICE_BIT_IN) != 0) { 940 // input 941 return (~AUDIO_DEVICE_BIT_IN & device & (AUDIO_DEVICE_IN_ALL_USB | 942 AUDIO_DEVICE_IN_HDMI | 943 AUDIO_DEVICE_IN_SPDIF | 944 AUDIO_DEVICE_IN_IP | 945 AUDIO_DEVICE_IN_BUS)) != 0; 946 } else { 947 // output 948 return (device & (AUDIO_DEVICE_OUT_ALL_USB | 949 AUDIO_DEVICE_OUT_HDMI | 950 AUDIO_DEVICE_OUT_HDMI_ARC | 951 AUDIO_DEVICE_OUT_SPDIF | 952 AUDIO_DEVICE_OUT_IP | 953 AUDIO_DEVICE_OUT_BUS)) != 0; 954 } 955 } 956 957 // Unique effect ID (can be generated from the following site: 958 // http://www.itu.int/ITU-T/asn1/uuid.html) 959 // This struct is used for effects identification and in soundtrigger. 960 typedef struct audio_uuid_s { 961 uint32_t timeLow; 962 uint16_t timeMid; 963 uint16_t timeHiAndVersion; 964 uint16_t clockSeq; 965 uint8_t node[6]; 966 } audio_uuid_t; 967 968 __END_DECLS 969 970 /** 971 * List of known audio HAL modules. This is the base name of the audio HAL 972 * library composed of the "audio." prefix, one of the base names below and 973 * a suffix specific to the device. 974 * e.g: audio.primary.goldfish.so or audio.a2dp.default.so 975 * 976 * The same module names are used in audio policy configuration files. 977 */ 978 979 #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary" 980 #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp" 981 #define AUDIO_HARDWARE_MODULE_ID_USB "usb" 982 #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix" 983 #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload" 984 #define AUDIO_HARDWARE_MODULE_ID_STUB "stub" 985 986 /** 987 * Parameter definitions. 988 * Note that in the framework code it's recommended to use AudioParameter.h 989 * instead of these preprocessor defines, and for sure avoid just copying 990 * the constant values. 991 */ 992 993 #define AUDIO_PARAMETER_VALUE_ON "on" 994 #define AUDIO_PARAMETER_VALUE_OFF "off" 995 996 /** 997 * audio device parameters 998 */ 999 1000 /* BT SCO Noise Reduction + Echo Cancellation parameters */ 1001 #define AUDIO_PARAMETER_KEY_BT_NREC "bt_headset_nrec" 1002 1003 /* Get a new HW synchronization source identifier. 1004 * Return a valid source (positive integer) or AUDIO_HW_SYNC_INVALID if an error occurs 1005 * or no HW sync is available. */ 1006 #define AUDIO_PARAMETER_HW_AV_SYNC "hw_av_sync" 1007 1008 /* Screen state */ 1009 #define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state" 1010 1011 /** 1012 * audio stream parameters 1013 */ 1014 1015 #define AUDIO_PARAMETER_STREAM_ROUTING "routing" /* audio_devices_t */ 1016 #define AUDIO_PARAMETER_STREAM_FORMAT "format" /* audio_format_t */ 1017 #define AUDIO_PARAMETER_STREAM_CHANNELS "channels" /* audio_channel_mask_t */ 1018 #define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count" /* size_t */ 1019 #define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source" /* audio_source_t */ 1020 #define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" /* uint32_t */ 1021 1022 #define AUDIO_PARAMETER_DEVICE_CONNECT "connect" /* audio_devices_t */ 1023 #define AUDIO_PARAMETER_DEVICE_DISCONNECT "disconnect" /* audio_devices_t */ 1024 1025 /* Enable mono audio playback if 1, else should be 0. */ 1026 #define AUDIO_PARAMETER_MONO_OUTPUT "mono_output" 1027 1028 /* Set the HW synchronization source for an output stream. */ 1029 #define AUDIO_PARAMETER_STREAM_HW_AV_SYNC "hw_av_sync" 1030 1031 /* Query supported formats. The response is a '|' separated list of strings from 1032 * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */ 1033 #define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats" 1034 /* Query supported channel masks. The response is a '|' separated list of strings from 1035 * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */ 1036 #define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels" 1037 /* Query supported sampling rates. The response is a '|' separated list of integer values e.g: 1038 * "sup_sampling_rates=44100|48000" */ 1039 #define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates" 1040 1041 #define AUDIO_PARAMETER_VALUE_LIST_SEPARATOR "|" 1042 1043 /** 1044 * audio codec parameters 1045 */ 1046 1047 #define AUDIO_OFFLOAD_CODEC_PARAMS "music_offload_codec_param" 1048 #define AUDIO_OFFLOAD_CODEC_BIT_PER_SAMPLE "music_offload_bit_per_sample" 1049 #define AUDIO_OFFLOAD_CODEC_BIT_RATE "music_offload_bit_rate" 1050 #define AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE "music_offload_avg_bit_rate" 1051 #define AUDIO_OFFLOAD_CODEC_ID "music_offload_codec_id" 1052 #define AUDIO_OFFLOAD_CODEC_BLOCK_ALIGN "music_offload_block_align" 1053 #define AUDIO_OFFLOAD_CODEC_SAMPLE_RATE "music_offload_sample_rate" 1054 #define AUDIO_OFFLOAD_CODEC_ENCODE_OPTION "music_offload_encode_option" 1055 #define AUDIO_OFFLOAD_CODEC_NUM_CHANNEL "music_offload_num_channels" 1056 #define AUDIO_OFFLOAD_CODEC_DOWN_SAMPLING "music_offload_down_sampling" 1057 #define AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES "delay_samples" 1058 #define AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES "padding_samples" 1059 1060 1061 #endif // ANDROID_AUDIO_CORE_H 1062