OSDN Git Service

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