blob: 2749ce02e883583d508774db8b6515401f379306 [file] [log] [blame]
Dima Zavine8e4be52011-05-12 10:25:33 -07001/*
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>
Eric Laurenta768c302014-07-28 11:33:43 -070023#include <stdio.h>
Dima Zavine8e4be52011-05-12 10:25:33 -070024#include <sys/cdefs.h>
25#include <sys/types.h>
26
27#include <cutils/bitops.h>
28
29__BEGIN_DECLS
30
31/* The enums were moved here mostly from
32 * frameworks/base/include/media/AudioSystem.h
33 */
34
Jeff Brown8ecc7af2013-08-16 20:09:37 -070035/* device address used to refer to the standard remote submix */
36#define AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS "0"
37
Glenn Kasten1c3e16f2011-12-19 09:21:48 -080038/* AudioFlinger and AudioPolicy services use I/O handles to identify audio sources and sinks */
Dima Zavine8e4be52011-05-12 10:25:33 -070039typedef int audio_io_handle_t;
Glenn Kasten1c3e16f2011-12-19 09:21:48 -080040#define AUDIO_IO_HANDLE_NONE 0
Dima Zavine8e4be52011-05-12 10:25:33 -070041
42/* Audio stream types */
43typedef enum {
Glenn Kasten1c3e16f2011-12-19 09:21:48 -080044 /* These values must kept in sync with
45 * frameworks/base/media/java/android/media/AudioSystem.java
46 */
Dima Zavine8e4be52011-05-12 10:25:33 -070047 AUDIO_STREAM_DEFAULT = -1,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -080048 AUDIO_STREAM_MIN = 0,
Dima Zavine8e4be52011-05-12 10:25:33 -070049 AUDIO_STREAM_VOICE_CALL = 0,
50 AUDIO_STREAM_SYSTEM = 1,
51 AUDIO_STREAM_RING = 2,
52 AUDIO_STREAM_MUSIC = 3,
53 AUDIO_STREAM_ALARM = 4,
54 AUDIO_STREAM_NOTIFICATION = 5,
55 AUDIO_STREAM_BLUETOOTH_SCO = 6,
Glenn Kasten432b7c62014-04-14 17:47:03 -070056 AUDIO_STREAM_ENFORCED_AUDIBLE = 7, /* Sounds that cannot be muted by user
57 * and must be routed to speaker
58 */
Dima Zavine8e4be52011-05-12 10:25:33 -070059 AUDIO_STREAM_DTMF = 8,
Eric Laurent47f52f22014-11-11 14:03:35 -080060 AUDIO_STREAM_TTS = 9, /* Transmitted Through Speaker.
61 * Plays over speaker only, silent on other devices.
62 */
63 AUDIO_STREAM_ACCESSIBILITY = 10, /* For accessibility talk back prompts */
64 AUDIO_STREAM_REROUTING = 11, /* For dynamic policy output mixes */
65 AUDIO_STREAM_PATCH = 12, /* For internal audio flinger tracks. Fixed volume */
66 AUDIO_STREAM_PUBLIC_CNT = AUDIO_STREAM_TTS + 1,
67 AUDIO_STREAM_CNT = AUDIO_STREAM_PATCH + 1,
Dima Zavine8e4be52011-05-12 10:25:33 -070068} audio_stream_type_t;
69
70/* Do not change these values without updating their counterparts
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -070071 * in frameworks/base/media/java/android/media/AudioAttributes.java
72 */
73typedef enum {
74 AUDIO_CONTENT_TYPE_UNKNOWN = 0,
75 AUDIO_CONTENT_TYPE_SPEECH = 1,
76 AUDIO_CONTENT_TYPE_MUSIC = 2,
77 AUDIO_CONTENT_TYPE_MOVIE = 3,
78 AUDIO_CONTENT_TYPE_SONIFICATION = 4,
79
80 AUDIO_CONTENT_TYPE_CNT,
81 AUDIO_CONTENT_TYPE_MAX = AUDIO_CONTENT_TYPE_CNT - 1,
82} audio_content_type_t;
83
84/* Do not change these values without updating their counterparts
85 * in frameworks/base/media/java/android/media/AudioAttributes.java
86 */
87typedef enum {
88 AUDIO_USAGE_UNKNOWN = 0,
89 AUDIO_USAGE_MEDIA = 1,
90 AUDIO_USAGE_VOICE_COMMUNICATION = 2,
91 AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING = 3,
92 AUDIO_USAGE_ALARM = 4,
93 AUDIO_USAGE_NOTIFICATION = 5,
94 AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE = 6,
95 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST = 7,
96 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT = 8,
97 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED = 9,
98 AUDIO_USAGE_NOTIFICATION_EVENT = 10,
99 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY = 11,
100 AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12,
101 AUDIO_USAGE_ASSISTANCE_SONIFICATION = 13,
102 AUDIO_USAGE_GAME = 14,
103
104 AUDIO_USAGE_CNT,
105 AUDIO_USAGE_MAX = AUDIO_USAGE_CNT - 1,
106} audio_usage_t;
107
108typedef uint32_t audio_flags_mask_t;
109
110/* Do not change these values without updating their counterparts
111 * in frameworks/base/media/java/android/media/AudioAttributes.java
112 */
113enum {
114 AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1,
115 AUDIO_FLAG_SECURE = 0x2,
116 AUDIO_FLAG_SCO = 0x4,
Eric Laurentd91e6df2014-08-01 14:33:10 -0700117 AUDIO_FLAG_BEACON = 0x8,
Eric Laurentf6cce342014-09-19 17:45:18 -0700118 AUDIO_FLAG_HW_AV_SYNC = 0x10,
119 AUDIO_FLAG_HW_HOTWORD = 0x20,
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -0700120};
121
122/* Do not change these values without updating their counterparts
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800123 * in frameworks/base/media/java/android/media/MediaRecorder.java,
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700124 * frameworks/av/services/audiopolicy/AudioPolicyService.cpp,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800125 * and system/media/audio_effects/include/audio_effects/audio_effects_conf.h!
Dima Zavine8e4be52011-05-12 10:25:33 -0700126 */
127typedef enum {
128 AUDIO_SOURCE_DEFAULT = 0,
129 AUDIO_SOURCE_MIC = 1,
130 AUDIO_SOURCE_VOICE_UPLINK = 2,
131 AUDIO_SOURCE_VOICE_DOWNLINK = 3,
132 AUDIO_SOURCE_VOICE_CALL = 4,
133 AUDIO_SOURCE_CAMCORDER = 5,
134 AUDIO_SOURCE_VOICE_RECOGNITION = 6,
135 AUDIO_SOURCE_VOICE_COMMUNICATION = 7,
Jean-Michel Trivie11866a2012-08-16 17:53:30 -0700136 AUDIO_SOURCE_REMOTE_SUBMIX = 8, /* Source for the mix to be presented remotely. */
137 /* An example of remote presentation is Wifi Display */
138 /* where a dongle attached to a TV can be used to */
139 /* play the mix captured by this audio source. */
Dima Zavine8e4be52011-05-12 10:25:33 -0700140 AUDIO_SOURCE_CNT,
141 AUDIO_SOURCE_MAX = AUDIO_SOURCE_CNT - 1,
Carson Liao4c4cf462014-10-06 20:46:24 +0800142 AUDIO_SOURCE_FM_TUNER = 1998,
Eric Laurent04c12ca2013-09-09 10:39:54 -0700143 AUDIO_SOURCE_HOTWORD = 1999, /* A low-priority, preemptible audio source for
144 for background software hotword detection.
145 Same tuning as AUDIO_SOURCE_VOICE_RECOGNITION.
146 Used only internally to the framework. Not exposed
147 at the audio HAL. */
Dima Zavine8e4be52011-05-12 10:25:33 -0700148} audio_source_t;
149
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -0700150/* Audio attributes */
151#define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
152typedef struct {
153 audio_content_type_t content_type;
154 audio_usage_t usage;
155 audio_source_t source;
156 audio_flags_mask_t flags;
157 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
158} audio_attributes_t;
159
Dima Zavine8e4be52011-05-12 10:25:33 -0700160/* special audio session values
161 * (XXX: should this be living in the audio effects land?)
162 */
163typedef enum {
164 /* session for effects attached to a particular output stream
165 * (value must be less than 0)
166 */
167 AUDIO_SESSION_OUTPUT_STAGE = -1,
168
169 /* session for effects applied to output mix. These effects can
170 * be moved by audio policy manager to another output stream
171 * (value must be 0)
172 */
173 AUDIO_SESSION_OUTPUT_MIX = 0,
Glenn Kastenb4f2b4e2012-06-25 14:57:36 -0700174
175 /* application does not specify an explicit session ID to be used,
176 * and requests a new session ID to be allocated
177 * TODO use unique values for AUDIO_SESSION_OUTPUT_MIX and AUDIO_SESSION_ALLOCATE,
178 * after all uses have been updated from 0 to the appropriate symbol, and have been tested.
179 */
180 AUDIO_SESSION_ALLOCATE = 0,
Dima Zavine8e4be52011-05-12 10:25:33 -0700181} audio_session_t;
182
Eric Laurent1e92ed52014-07-28 09:26:06 -0700183/* a unique ID allocated by AudioFlinger for use as a audio_io_handle_t or audio_session_t */
184typedef int audio_unique_id_t;
185
186#define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
187
Dima Zavine8e4be52011-05-12 10:25:33 -0700188/* Audio sub formats (see enum audio_format). */
189
190/* PCM sub formats */
191typedef enum {
Glenn Kastenc0a51092014-01-31 07:51:17 -0800192 /* All of these are in native byte order */
Eric Laurent9714b272011-05-26 13:52:47 -0700193 AUDIO_FORMAT_PCM_SUB_16_BIT = 0x1, /* DO NOT CHANGE - PCM signed 16 bits */
194 AUDIO_FORMAT_PCM_SUB_8_BIT = 0x2, /* DO NOT CHANGE - PCM unsigned 8 bits */
195 AUDIO_FORMAT_PCM_SUB_32_BIT = 0x3, /* PCM signed .31 fixed point */
196 AUDIO_FORMAT_PCM_SUB_8_24_BIT = 0x4, /* PCM signed 7.24 fixed point */
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800197 AUDIO_FORMAT_PCM_SUB_FLOAT = 0x5, /* PCM single-precision floating point */
Glenn Kastenc0a51092014-01-31 07:51:17 -0800198 AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED = 0x6, /* PCM signed .23 fixed point packed in 3 bytes */
Dima Zavine8e4be52011-05-12 10:25:33 -0700199} audio_format_pcm_sub_fmt_t;
200
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800201/* The audio_format_*_sub_fmt_t declarations are not currently used */
202
Dima Zavine8e4be52011-05-12 10:25:33 -0700203/* MP3 sub format field definition : can use 11 LSBs in the same way as MP3
204 * frame header to specify bit rate, stereo mode, version...
205 */
206typedef enum {
207 AUDIO_FORMAT_MP3_SUB_NONE = 0x0,
208} audio_format_mp3_sub_fmt_t;
209
210/* AMR NB/WB sub format field definition: specify frame block interleaving,
211 * bandwidth efficient or octet aligned, encoding mode for recording...
212 */
213typedef enum {
214 AUDIO_FORMAT_AMR_SUB_NONE = 0x0,
215} audio_format_amr_sub_fmt_t;
216
217/* AAC sub format field definition: specify profile or bitrate for recording... */
218typedef enum {
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530219 AUDIO_FORMAT_AAC_SUB_MAIN = 0x1,
220 AUDIO_FORMAT_AAC_SUB_LC = 0x2,
221 AUDIO_FORMAT_AAC_SUB_SSR = 0x4,
222 AUDIO_FORMAT_AAC_SUB_LTP = 0x8,
223 AUDIO_FORMAT_AAC_SUB_HE_V1 = 0x10,
224 AUDIO_FORMAT_AAC_SUB_SCALABLE = 0x20,
225 AUDIO_FORMAT_AAC_SUB_ERLC = 0x40,
226 AUDIO_FORMAT_AAC_SUB_LD = 0x80,
227 AUDIO_FORMAT_AAC_SUB_HE_V2 = 0x100,
228 AUDIO_FORMAT_AAC_SUB_ELD = 0x200,
Dima Zavine8e4be52011-05-12 10:25:33 -0700229} audio_format_aac_sub_fmt_t;
230
231/* VORBIS sub format field definition: specify quality for recording... */
232typedef enum {
233 AUDIO_FORMAT_VORBIS_SUB_NONE = 0x0,
234} audio_format_vorbis_sub_fmt_t;
235
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800236/* Audio format consists of a main format field (upper 8 bits) and a sub format
Dima Zavine8e4be52011-05-12 10:25:33 -0700237 * field (lower 24 bits).
238 *
239 * The main format indicates the main codec type. The sub format field
240 * indicates options and parameters for each format. The sub format is mainly
241 * used for record to indicate for instance the requested bitrate or profile.
242 * It can also be used for certain formats to give informations not present in
243 * the encoded audio stream (e.g. octet alignement for AMR).
244 */
245typedef enum {
246 AUDIO_FORMAT_INVALID = 0xFFFFFFFFUL,
247 AUDIO_FORMAT_DEFAULT = 0,
248 AUDIO_FORMAT_PCM = 0x00000000UL, /* DO NOT CHANGE */
249 AUDIO_FORMAT_MP3 = 0x01000000UL,
250 AUDIO_FORMAT_AMR_NB = 0x02000000UL,
251 AUDIO_FORMAT_AMR_WB = 0x03000000UL,
252 AUDIO_FORMAT_AAC = 0x04000000UL,
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530253 AUDIO_FORMAT_HE_AAC_V1 = 0x05000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V1*/
254 AUDIO_FORMAT_HE_AAC_V2 = 0x06000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V2*/
Dima Zavine8e4be52011-05-12 10:25:33 -0700255 AUDIO_FORMAT_VORBIS = 0x07000000UL,
Vignesh Venkatasubramanian76edb1c2014-01-29 09:47:56 -0800256 AUDIO_FORMAT_OPUS = 0x08000000UL,
Eric Laurentce30de32014-06-10 15:39:16 -0700257 AUDIO_FORMAT_AC3 = 0x09000000UL,
258 AUDIO_FORMAT_E_AC3 = 0x0A000000UL,
Dima Zavine8e4be52011-05-12 10:25:33 -0700259 AUDIO_FORMAT_MAIN_MASK = 0xFF000000UL,
260 AUDIO_FORMAT_SUB_MASK = 0x00FFFFFFUL,
261
262 /* Aliases */
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800263 /* note != AudioFormat.ENCODING_PCM_16BIT */
Dima Zavine8e4be52011-05-12 10:25:33 -0700264 AUDIO_FORMAT_PCM_16_BIT = (AUDIO_FORMAT_PCM |
265 AUDIO_FORMAT_PCM_SUB_16_BIT),
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800266 /* note != AudioFormat.ENCODING_PCM_8BIT */
Dima Zavine8e4be52011-05-12 10:25:33 -0700267 AUDIO_FORMAT_PCM_8_BIT = (AUDIO_FORMAT_PCM |
268 AUDIO_FORMAT_PCM_SUB_8_BIT),
Eric Laurent9714b272011-05-26 13:52:47 -0700269 AUDIO_FORMAT_PCM_32_BIT = (AUDIO_FORMAT_PCM |
270 AUDIO_FORMAT_PCM_SUB_32_BIT),
271 AUDIO_FORMAT_PCM_8_24_BIT = (AUDIO_FORMAT_PCM |
272 AUDIO_FORMAT_PCM_SUB_8_24_BIT),
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800273 AUDIO_FORMAT_PCM_FLOAT = (AUDIO_FORMAT_PCM |
274 AUDIO_FORMAT_PCM_SUB_FLOAT),
Glenn Kastenc0a51092014-01-31 07:51:17 -0800275 AUDIO_FORMAT_PCM_24_BIT_PACKED = (AUDIO_FORMAT_PCM |
276 AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED),
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530277 AUDIO_FORMAT_AAC_MAIN = (AUDIO_FORMAT_AAC |
278 AUDIO_FORMAT_AAC_SUB_MAIN),
279 AUDIO_FORMAT_AAC_LC = (AUDIO_FORMAT_AAC |
280 AUDIO_FORMAT_AAC_SUB_LC),
281 AUDIO_FORMAT_AAC_SSR = (AUDIO_FORMAT_AAC |
282 AUDIO_FORMAT_AAC_SUB_SSR),
283 AUDIO_FORMAT_AAC_LTP = (AUDIO_FORMAT_AAC |
284 AUDIO_FORMAT_AAC_SUB_LTP),
285 AUDIO_FORMAT_AAC_HE_V1 = (AUDIO_FORMAT_AAC |
286 AUDIO_FORMAT_AAC_SUB_HE_V1),
287 AUDIO_FORMAT_AAC_SCALABLE = (AUDIO_FORMAT_AAC |
288 AUDIO_FORMAT_AAC_SUB_SCALABLE),
289 AUDIO_FORMAT_AAC_ERLC = (AUDIO_FORMAT_AAC |
290 AUDIO_FORMAT_AAC_SUB_ERLC),
291 AUDIO_FORMAT_AAC_LD = (AUDIO_FORMAT_AAC |
292 AUDIO_FORMAT_AAC_SUB_LD),
293 AUDIO_FORMAT_AAC_HE_V2 = (AUDIO_FORMAT_AAC |
294 AUDIO_FORMAT_AAC_SUB_HE_V2),
295 AUDIO_FORMAT_AAC_ELD = (AUDIO_FORMAT_AAC |
296 AUDIO_FORMAT_AAC_SUB_ELD),
Dima Zavine8e4be52011-05-12 10:25:33 -0700297} audio_format_t;
298
Glenn Kasten432b7c62014-04-14 17:47:03 -0700299/* For the channel mask for position assignment representation */
Glenn Kasten16a7a042012-07-03 15:21:05 -0700300enum {
Glenn Kasten432b7c62014-04-14 17:47:03 -0700301
302/* These can be a complete audio_channel_mask_t. */
303
Eric Laurent4cd05012014-02-18 12:40:36 -0800304 AUDIO_CHANNEL_NONE = 0x0,
Glenn Kasten432b7c62014-04-14 17:47:03 -0700305 AUDIO_CHANNEL_INVALID = 0xC0000000,
306
307/* These can be the bits portion of an audio_channel_mask_t
308 * with representation AUDIO_CHANNEL_REPRESENTATION_POSITION.
309 * Using these bits as a complete audio_channel_mask_t is deprecated.
310 */
311
Dima Zavine8e4be52011-05-12 10:25:33 -0700312 /* output channels */
Jean-Michel Trivi6d4f3972011-07-25 16:19:07 -0700313 AUDIO_CHANNEL_OUT_FRONT_LEFT = 0x1,
314 AUDIO_CHANNEL_OUT_FRONT_RIGHT = 0x2,
315 AUDIO_CHANNEL_OUT_FRONT_CENTER = 0x4,
316 AUDIO_CHANNEL_OUT_LOW_FREQUENCY = 0x8,
317 AUDIO_CHANNEL_OUT_BACK_LEFT = 0x10,
318 AUDIO_CHANNEL_OUT_BACK_RIGHT = 0x20,
319 AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x40,
320 AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x80,
321 AUDIO_CHANNEL_OUT_BACK_CENTER = 0x100,
322 AUDIO_CHANNEL_OUT_SIDE_LEFT = 0x200,
323 AUDIO_CHANNEL_OUT_SIDE_RIGHT = 0x400,
324 AUDIO_CHANNEL_OUT_TOP_CENTER = 0x800,
325 AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT = 0x1000,
326 AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER = 0x2000,
327 AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT = 0x4000,
328 AUDIO_CHANNEL_OUT_TOP_BACK_LEFT = 0x8000,
329 AUDIO_CHANNEL_OUT_TOP_BACK_CENTER = 0x10000,
330 AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT = 0x20000,
Dima Zavine8e4be52011-05-12 10:25:33 -0700331
Glenn Kasten432b7c62014-04-14 17:47:03 -0700332/* TODO: should these be considered complete channel masks, or only bits? */
333
Dima Zavine8e4be52011-05-12 10:25:33 -0700334 AUDIO_CHANNEL_OUT_MONO = AUDIO_CHANNEL_OUT_FRONT_LEFT,
335 AUDIO_CHANNEL_OUT_STEREO = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
336 AUDIO_CHANNEL_OUT_FRONT_RIGHT),
337 AUDIO_CHANNEL_OUT_QUAD = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
338 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
339 AUDIO_CHANNEL_OUT_BACK_LEFT |
340 AUDIO_CHANNEL_OUT_BACK_RIGHT),
Glenn Kastenb8d59392014-04-29 08:41:46 -0700341 AUDIO_CHANNEL_OUT_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD,
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700342 /* like AUDIO_CHANNEL_OUT_QUAD_BACK with *_SIDE_* instead of *_BACK_* */
343 AUDIO_CHANNEL_OUT_QUAD_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
344 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
345 AUDIO_CHANNEL_OUT_SIDE_LEFT |
346 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700347 AUDIO_CHANNEL_OUT_5POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
348 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
349 AUDIO_CHANNEL_OUT_FRONT_CENTER |
350 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
351 AUDIO_CHANNEL_OUT_BACK_LEFT |
352 AUDIO_CHANNEL_OUT_BACK_RIGHT),
Glenn Kastenb8d59392014-04-29 08:41:46 -0700353 AUDIO_CHANNEL_OUT_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1,
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700354 /* like AUDIO_CHANNEL_OUT_5POINT1_BACK with *_SIDE_* instead of *_BACK_* */
355 AUDIO_CHANNEL_OUT_5POINT1_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
356 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
357 AUDIO_CHANNEL_OUT_FRONT_CENTER |
358 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
359 AUDIO_CHANNEL_OUT_SIDE_LEFT |
360 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700361 // matches the correct AudioFormat.CHANNEL_OUT_7POINT1_SURROUND definition for 7.1
Dima Zavine8e4be52011-05-12 10:25:33 -0700362 AUDIO_CHANNEL_OUT_7POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
363 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
364 AUDIO_CHANNEL_OUT_FRONT_CENTER |
365 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
366 AUDIO_CHANNEL_OUT_BACK_LEFT |
367 AUDIO_CHANNEL_OUT_BACK_RIGHT |
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700368 AUDIO_CHANNEL_OUT_SIDE_LEFT |
369 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700370 AUDIO_CHANNEL_OUT_ALL = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
371 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
372 AUDIO_CHANNEL_OUT_FRONT_CENTER |
373 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
374 AUDIO_CHANNEL_OUT_BACK_LEFT |
375 AUDIO_CHANNEL_OUT_BACK_RIGHT |
376 AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER |
377 AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700378 AUDIO_CHANNEL_OUT_BACK_CENTER|
379 AUDIO_CHANNEL_OUT_SIDE_LEFT|
380 AUDIO_CHANNEL_OUT_SIDE_RIGHT|
381 AUDIO_CHANNEL_OUT_TOP_CENTER|
382 AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT|
383 AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER|
384 AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT|
385 AUDIO_CHANNEL_OUT_TOP_BACK_LEFT|
386 AUDIO_CHANNEL_OUT_TOP_BACK_CENTER|
387 AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700388
Glenn Kasten432b7c62014-04-14 17:47:03 -0700389/* These are bits only, not complete values */
390
Dima Zavine8e4be52011-05-12 10:25:33 -0700391 /* input channels */
392 AUDIO_CHANNEL_IN_LEFT = 0x4,
393 AUDIO_CHANNEL_IN_RIGHT = 0x8,
394 AUDIO_CHANNEL_IN_FRONT = 0x10,
395 AUDIO_CHANNEL_IN_BACK = 0x20,
396 AUDIO_CHANNEL_IN_LEFT_PROCESSED = 0x40,
397 AUDIO_CHANNEL_IN_RIGHT_PROCESSED = 0x80,
398 AUDIO_CHANNEL_IN_FRONT_PROCESSED = 0x100,
399 AUDIO_CHANNEL_IN_BACK_PROCESSED = 0x200,
400 AUDIO_CHANNEL_IN_PRESSURE = 0x400,
401 AUDIO_CHANNEL_IN_X_AXIS = 0x800,
402 AUDIO_CHANNEL_IN_Y_AXIS = 0x1000,
403 AUDIO_CHANNEL_IN_Z_AXIS = 0x2000,
404 AUDIO_CHANNEL_IN_VOICE_UPLINK = 0x4000,
405 AUDIO_CHANNEL_IN_VOICE_DNLINK = 0x8000,
406
Glenn Kasten432b7c62014-04-14 17:47:03 -0700407/* TODO: should these be considered complete channel masks, or only bits, or deprecated? */
408
Dima Zavine8e4be52011-05-12 10:25:33 -0700409 AUDIO_CHANNEL_IN_MONO = AUDIO_CHANNEL_IN_FRONT,
410 AUDIO_CHANNEL_IN_STEREO = (AUDIO_CHANNEL_IN_LEFT | AUDIO_CHANNEL_IN_RIGHT),
Eric Laurent6c70cee2013-01-17 17:31:49 -0800411 AUDIO_CHANNEL_IN_FRONT_BACK = (AUDIO_CHANNEL_IN_FRONT | AUDIO_CHANNEL_IN_BACK),
Dima Zavine8e4be52011-05-12 10:25:33 -0700412 AUDIO_CHANNEL_IN_ALL = (AUDIO_CHANNEL_IN_LEFT |
413 AUDIO_CHANNEL_IN_RIGHT |
414 AUDIO_CHANNEL_IN_FRONT |
415 AUDIO_CHANNEL_IN_BACK|
416 AUDIO_CHANNEL_IN_LEFT_PROCESSED |
417 AUDIO_CHANNEL_IN_RIGHT_PROCESSED |
418 AUDIO_CHANNEL_IN_FRONT_PROCESSED |
419 AUDIO_CHANNEL_IN_BACK_PROCESSED|
420 AUDIO_CHANNEL_IN_PRESSURE |
421 AUDIO_CHANNEL_IN_X_AXIS |
422 AUDIO_CHANNEL_IN_Y_AXIS |
423 AUDIO_CHANNEL_IN_Z_AXIS |
424 AUDIO_CHANNEL_IN_VOICE_UPLINK |
425 AUDIO_CHANNEL_IN_VOICE_DNLINK),
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -0800426};
427
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700428/* A channel mask per se only defines the presence or absence of a channel, not the order.
429 * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
Glenn Kasten432b7c62014-04-14 17:47:03 -0700430 *
431 * audio_channel_mask_t is an opaque type and its internal layout should not
432 * be assumed as it may change in the future.
433 * Instead, always use the functions declared in this header to examine.
434 *
435 * These are the current representations:
436 *
437 * AUDIO_CHANNEL_REPRESENTATION_POSITION
438 * is a channel mask representation for position assignment.
439 * Each low-order bit corresponds to the spatial position of a transducer (output),
440 * or interpretation of channel (input).
441 * The user of a channel mask needs to know the context of whether it is for output or input.
442 * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
443 * It is not permitted for no bits to be set.
444 *
445 * AUDIO_CHANNEL_REPRESENTATION_INDEX
446 * is a channel mask representation for index assignment.
447 * Each low-order bit corresponds to a selected channel.
448 * There is no platform interpretation of the various bits.
449 * There is no concept of output or input.
450 * It is not permitted for no bits to be set.
451 *
452 * All other representations are reserved for future use.
453 *
454 * Warning: current representation distinguishes between input and output, but this will not the be
455 * case in future revisions of the platform. Wherever there is an ambiguity between input and output
456 * that is currently resolved by checking the channel mask, the implementer should look for ways to
457 * fix it with additional information outside of the mask.
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700458 */
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -0800459typedef uint32_t audio_channel_mask_t;
Dima Zavine8e4be52011-05-12 10:25:33 -0700460
Glenn Kasten432b7c62014-04-14 17:47:03 -0700461/* Maximum number of channels for all representations */
462#define AUDIO_CHANNEL_COUNT_MAX 30
463
464/* log(2) of maximum number of representations, not part of public API */
465#define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
466
467/* Representations */
468typedef enum {
469 AUDIO_CHANNEL_REPRESENTATION_POSITION = 0, // must be zero for compatibility
470 // 1 is reserved for future use
471 AUDIO_CHANNEL_REPRESENTATION_INDEX = 2,
472 // 3 is reserved for future use
473} audio_channel_representation_t;
474
475/* The return value is undefined if the channel mask is invalid. */
476static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
477{
478 return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
479}
480
481/* The return value is undefined if the channel mask is invalid. */
482static inline audio_channel_representation_t audio_channel_mask_get_representation(
483 audio_channel_mask_t channel)
484{
485 // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
486 return (audio_channel_representation_t)
487 ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
488}
489
490/* Returns true if the channel mask is valid,
491 * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
492 * This function is unable to determine whether a channel mask for position assignment
493 * is invalid because an output mask has an invalid output bit set,
494 * or because an input mask has an invalid input bit set.
495 * All other APIs that take a channel mask assume that it is valid.
496 */
497static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
498{
499 uint32_t bits = audio_channel_mask_get_bits(channel);
500 audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
501 switch (representation) {
502 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
503 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
504 break;
505 default:
506 bits = 0;
507 break;
508 }
509 return bits != 0;
510}
511
512/* Not part of public API */
513static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
514 audio_channel_representation_t representation, uint32_t bits)
515{
516 return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
517}
518
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800519/* Expresses the convention when stereo audio samples are stored interleaved
520 * in an array. This should improve readability by allowing code to use
521 * symbolic indices instead of hard-coded [0] and [1].
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700522 *
523 * For multi-channel beyond stereo, the platform convention is that channels
524 * are interleaved in order from least significant channel mask bit
525 * to most significant channel mask bit, with unused bits skipped.
526 * Any exceptions to this convention will be noted at the appropriate API.
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800527 */
528enum {
529 AUDIO_INTERLEAVE_LEFT = 0,
530 AUDIO_INTERLEAVE_RIGHT = 1,
531};
532
Dima Zavine8e4be52011-05-12 10:25:33 -0700533typedef enum {
534 AUDIO_MODE_INVALID = -2,
535 AUDIO_MODE_CURRENT = -1,
536 AUDIO_MODE_NORMAL = 0,
537 AUDIO_MODE_RINGTONE = 1,
538 AUDIO_MODE_IN_CALL = 2,
539 AUDIO_MODE_IN_COMMUNICATION = 3,
540
541 AUDIO_MODE_CNT,
542 AUDIO_MODE_MAX = AUDIO_MODE_CNT - 1,
543} audio_mode_t;
544
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800545/* This enum is deprecated */
Dima Zavine8e4be52011-05-12 10:25:33 -0700546typedef enum {
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800547 AUDIO_IN_ACOUSTICS_NONE = 0,
Dima Zavine8e4be52011-05-12 10:25:33 -0700548 AUDIO_IN_ACOUSTICS_AGC_ENABLE = 0x0001,
549 AUDIO_IN_ACOUSTICS_AGC_DISABLE = 0,
550 AUDIO_IN_ACOUSTICS_NS_ENABLE = 0x0002,
551 AUDIO_IN_ACOUSTICS_NS_DISABLE = 0,
552 AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004,
553 AUDIO_IN_ACOUSTICS_TX_DISABLE = 0,
554} audio_in_acoustics_t;
555
Glenn Kasten16a7a042012-07-03 15:21:05 -0700556enum {
Eric Laurenteeeee802012-08-28 14:29:07 -0700557 AUDIO_DEVICE_NONE = 0x0,
558 /* reserved bits */
559 AUDIO_DEVICE_BIT_IN = 0x80000000,
560 AUDIO_DEVICE_BIT_DEFAULT = 0x40000000,
Dima Zavine8e4be52011-05-12 10:25:33 -0700561 /* output devices */
562 AUDIO_DEVICE_OUT_EARPIECE = 0x1,
563 AUDIO_DEVICE_OUT_SPEAKER = 0x2,
564 AUDIO_DEVICE_OUT_WIRED_HEADSET = 0x4,
565 AUDIO_DEVICE_OUT_WIRED_HEADPHONE = 0x8,
566 AUDIO_DEVICE_OUT_BLUETOOTH_SCO = 0x10,
567 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20,
568 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40,
569 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP = 0x80,
570 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100,
571 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200,
572 AUDIO_DEVICE_OUT_AUX_DIGITAL = 0x400,
Eric Laurentabcb9992014-05-16 15:11:08 -0700573 AUDIO_DEVICE_OUT_HDMI = AUDIO_DEVICE_OUT_AUX_DIGITAL,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800574 /* uses an analog connection (multiplexed over the USB connector pins for instance) */
Dima Zavine8e4be52011-05-12 10:25:33 -0700575 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800,
576 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800577 /* USB accessory mode: your Android device is a USB device and the dock is a USB host */
Eric Laurent79f90bd2012-04-06 08:57:48 -0700578 AUDIO_DEVICE_OUT_USB_ACCESSORY = 0x2000,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800579 /* USB host mode: your Android device is a USB host and the dock is a USB device */
Eric Laurent79f90bd2012-04-06 08:57:48 -0700580 AUDIO_DEVICE_OUT_USB_DEVICE = 0x4000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700581 AUDIO_DEVICE_OUT_REMOTE_SUBMIX = 0x8000,
Eric Laurentabcb9992014-05-16 15:11:08 -0700582 /* Telephony voice TX path */
583 AUDIO_DEVICE_OUT_TELEPHONY_TX = 0x10000,
584 /* Analog jack with line impedance detected */
585 AUDIO_DEVICE_OUT_LINE = 0x20000,
586 /* HDMI Audio Return Channel */
587 AUDIO_DEVICE_OUT_HDMI_ARC = 0x40000,
588 /* S/PDIF out */
589 AUDIO_DEVICE_OUT_SPDIF = 0x80000,
590 /* FM transmitter out */
591 AUDIO_DEVICE_OUT_FM = 0x100000,
Jungshik Jangcadd5bb2014-07-15 19:40:32 +0900592 /* Line out for av devices */
593 AUDIO_DEVICE_OUT_AUX_LINE = 0x200000,
Jon Eklundfcfa8642014-06-20 16:03:18 -0500594 /* limited-output speaker device for acoustic safety */
595 AUDIO_DEVICE_OUT_SPEAKER_SAFE = 0x400000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700596 AUDIO_DEVICE_OUT_DEFAULT = AUDIO_DEVICE_BIT_DEFAULT,
Dima Zavine8e4be52011-05-12 10:25:33 -0700597 AUDIO_DEVICE_OUT_ALL = (AUDIO_DEVICE_OUT_EARPIECE |
598 AUDIO_DEVICE_OUT_SPEAKER |
599 AUDIO_DEVICE_OUT_WIRED_HEADSET |
600 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
601 AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
602 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
603 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
604 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
605 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
606 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
Eric Laurentabcb9992014-05-16 15:11:08 -0700607 AUDIO_DEVICE_OUT_HDMI |
Dima Zavine8e4be52011-05-12 10:25:33 -0700608 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
609 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
Eric Laurent79f90bd2012-04-06 08:57:48 -0700610 AUDIO_DEVICE_OUT_USB_ACCESSORY |
611 AUDIO_DEVICE_OUT_USB_DEVICE |
Eric Laurenteeeee802012-08-28 14:29:07 -0700612 AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
Eric Laurentabcb9992014-05-16 15:11:08 -0700613 AUDIO_DEVICE_OUT_TELEPHONY_TX |
614 AUDIO_DEVICE_OUT_LINE |
615 AUDIO_DEVICE_OUT_HDMI_ARC |
616 AUDIO_DEVICE_OUT_SPDIF |
617 AUDIO_DEVICE_OUT_FM |
Jungshik Jangcadd5bb2014-07-15 19:40:32 +0900618 AUDIO_DEVICE_OUT_AUX_LINE |
Jon Eklundfcfa8642014-06-20 16:03:18 -0500619 AUDIO_DEVICE_OUT_SPEAKER_SAFE |
Dima Zavine8e4be52011-05-12 10:25:33 -0700620 AUDIO_DEVICE_OUT_DEFAULT),
621 AUDIO_DEVICE_OUT_ALL_A2DP = (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
622 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
623 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
624 AUDIO_DEVICE_OUT_ALL_SCO = (AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
625 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
626 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
Eric Laurent79f90bd2012-04-06 08:57:48 -0700627 AUDIO_DEVICE_OUT_ALL_USB = (AUDIO_DEVICE_OUT_USB_ACCESSORY |
628 AUDIO_DEVICE_OUT_USB_DEVICE),
Dima Zavine8e4be52011-05-12 10:25:33 -0700629
630 /* input devices */
Eric Laurenteeeee802012-08-28 14:29:07 -0700631 AUDIO_DEVICE_IN_COMMUNICATION = AUDIO_DEVICE_BIT_IN | 0x1,
632 AUDIO_DEVICE_IN_AMBIENT = AUDIO_DEVICE_BIT_IN | 0x2,
633 AUDIO_DEVICE_IN_BUILTIN_MIC = AUDIO_DEVICE_BIT_IN | 0x4,
634 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET = AUDIO_DEVICE_BIT_IN | 0x8,
635 AUDIO_DEVICE_IN_WIRED_HEADSET = AUDIO_DEVICE_BIT_IN | 0x10,
636 AUDIO_DEVICE_IN_AUX_DIGITAL = AUDIO_DEVICE_BIT_IN | 0x20,
Eric Laurentabcb9992014-05-16 15:11:08 -0700637 AUDIO_DEVICE_IN_HDMI = AUDIO_DEVICE_IN_AUX_DIGITAL,
638 /* Telephony voice RX path */
Eric Laurenteeeee802012-08-28 14:29:07 -0700639 AUDIO_DEVICE_IN_VOICE_CALL = AUDIO_DEVICE_BIT_IN | 0x40,
Eric Laurentabcb9992014-05-16 15:11:08 -0700640 AUDIO_DEVICE_IN_TELEPHONY_RX = AUDIO_DEVICE_IN_VOICE_CALL,
Eric Laurenteeeee802012-08-28 14:29:07 -0700641 AUDIO_DEVICE_IN_BACK_MIC = AUDIO_DEVICE_BIT_IN | 0x80,
642 AUDIO_DEVICE_IN_REMOTE_SUBMIX = AUDIO_DEVICE_BIT_IN | 0x100,
643 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x200,
644 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x400,
645 AUDIO_DEVICE_IN_USB_ACCESSORY = AUDIO_DEVICE_BIT_IN | 0x800,
646 AUDIO_DEVICE_IN_USB_DEVICE = AUDIO_DEVICE_BIT_IN | 0x1000,
Eric Laurentabcb9992014-05-16 15:11:08 -0700647 /* FM tuner input */
648 AUDIO_DEVICE_IN_FM_TUNER = AUDIO_DEVICE_BIT_IN | 0x2000,
649 /* TV tuner input */
650 AUDIO_DEVICE_IN_TV_TUNER = AUDIO_DEVICE_BIT_IN | 0x4000,
651 /* Analog jack with line impedance detected */
652 AUDIO_DEVICE_IN_LINE = AUDIO_DEVICE_BIT_IN | 0x8000,
653 /* S/PDIF in */
654 AUDIO_DEVICE_IN_SPDIF = AUDIO_DEVICE_BIT_IN | 0x10000,
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700655 AUDIO_DEVICE_IN_BLUETOOTH_A2DP = AUDIO_DEVICE_BIT_IN | 0x20000,
Terry Heob63dd8a2014-06-27 15:25:56 +0900656 AUDIO_DEVICE_IN_LOOPBACK = AUDIO_DEVICE_BIT_IN | 0x40000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700657 AUDIO_DEVICE_IN_DEFAULT = AUDIO_DEVICE_BIT_IN | AUDIO_DEVICE_BIT_DEFAULT,
Dima Zavine8e4be52011-05-12 10:25:33 -0700658
659 AUDIO_DEVICE_IN_ALL = (AUDIO_DEVICE_IN_COMMUNICATION |
660 AUDIO_DEVICE_IN_AMBIENT |
661 AUDIO_DEVICE_IN_BUILTIN_MIC |
662 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET |
663 AUDIO_DEVICE_IN_WIRED_HEADSET |
Eric Laurentabcb9992014-05-16 15:11:08 -0700664 AUDIO_DEVICE_IN_HDMI |
665 AUDIO_DEVICE_IN_TELEPHONY_RX |
Dima Zavine8e4be52011-05-12 10:25:33 -0700666 AUDIO_DEVICE_IN_BACK_MIC |
Eric Laurenteeeee802012-08-28 14:29:07 -0700667 AUDIO_DEVICE_IN_REMOTE_SUBMIX |
668 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET |
669 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET |
670 AUDIO_DEVICE_IN_USB_ACCESSORY |
671 AUDIO_DEVICE_IN_USB_DEVICE |
Eric Laurentabcb9992014-05-16 15:11:08 -0700672 AUDIO_DEVICE_IN_FM_TUNER |
673 AUDIO_DEVICE_IN_TV_TUNER |
674 AUDIO_DEVICE_IN_LINE |
675 AUDIO_DEVICE_IN_SPDIF |
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700676 AUDIO_DEVICE_IN_BLUETOOTH_A2DP |
Terry Heob63dd8a2014-06-27 15:25:56 +0900677 AUDIO_DEVICE_IN_LOOPBACK |
Dima Zavine8e4be52011-05-12 10:25:33 -0700678 AUDIO_DEVICE_IN_DEFAULT),
679 AUDIO_DEVICE_IN_ALL_SCO = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
Paul McLean00b89722014-04-18 14:29:22 -0700680 AUDIO_DEVICE_IN_ALL_USB = (AUDIO_DEVICE_IN_USB_ACCESSORY |
681 AUDIO_DEVICE_IN_USB_DEVICE),
Glenn Kasten16a7a042012-07-03 15:21:05 -0700682};
683
684typedef uint32_t audio_devices_t;
Dima Zavine8e4be52011-05-12 10:25:33 -0700685
Eric Laurent545ab462012-04-16 18:05:39 -0700686/* the audio output flags serve two purposes:
687 * - when an AudioTrack is created they indicate a "wish" to be connected to an
688 * output stream with attributes corresponding to the specified flags
689 * - when present in an output profile descriptor listed for a particular audio
690 * hardware module, they indicate that an output stream can be opened that
691 * supports the attributes indicated by the flags.
692 * the audio policy manager will try to match the flags in the request
693 * (when getOuput() is called) to an available output stream.
694 */
695typedef enum {
Eric Laurent9aec3622012-04-13 16:52:58 -0700696 AUDIO_OUTPUT_FLAG_NONE = 0x0, // no attributes
697 AUDIO_OUTPUT_FLAG_DIRECT = 0x1, // this output directly connects a track
698 // to one output stream: no software mixer
699 AUDIO_OUTPUT_FLAG_PRIMARY = 0x2, // this output is the primary output of
700 // the device. It is unique and must be
701 // present. It is opened by default and
702 // receives routing, audio mode and volume
703 // controls related to voice calls.
704 AUDIO_OUTPUT_FLAG_FAST = 0x4, // output supports "fast tracks",
705 // defined elsewhere
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000706 AUDIO_OUTPUT_FLAG_DEEP_BUFFER = 0x8, // use deep audio buffers
707 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD = 0x10, // offload playback of compressed
708 // streams to hardware codec
Eric Laurentd91e6df2014-08-01 14:33:10 -0700709 AUDIO_OUTPUT_FLAG_NON_BLOCKING = 0x20, // use non-blocking write
710 AUDIO_OUTPUT_FLAG_HW_AV_SYNC = 0x40 // output uses a hardware A/V synchronization source
Eric Laurent545ab462012-04-16 18:05:39 -0700711} audio_output_flags_t;
712
Glenn Kastenc051ffd2013-08-01 07:35:33 -0700713/* The audio input flags are analogous to audio output flags.
714 * Currently they are used only when an AudioRecord is created,
715 * to indicate a preference to be connected to an input stream with
716 * attributes corresponding to the specified flags.
717 */
718typedef enum {
Eric Laurentf6cce342014-09-19 17:45:18 -0700719 AUDIO_INPUT_FLAG_NONE = 0x0, // no attributes
720 AUDIO_INPUT_FLAG_FAST = 0x1, // prefer an input that supports "fast tracks"
721 AUDIO_INPUT_FLAG_HW_HOTWORD = 0x2, // prefer an input that captures from hw hotword source
Glenn Kastenc051ffd2013-08-01 07:35:33 -0700722} audio_input_flags_t;
723
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000724/* Additional information about compressed streams offloaded to
725 * hardware playback
726 * The version and size fields must be initialized by the caller by using
727 * one of the constants defined here.
728 */
729typedef struct {
730 uint16_t version; // version of the info structure
731 uint16_t size; // total size of the structure including version and size
732 uint32_t sample_rate; // sample rate in Hz
733 audio_channel_mask_t channel_mask; // channel mask
734 audio_format_t format; // audio format
735 audio_stream_type_t stream_type; // stream type
736 uint32_t bit_rate; // bit rate in bits per second
737 int64_t duration_us; // duration in microseconds, -1 if unknown
738 bool has_video; // true if stream is tied to a video stream
739 bool is_streaming; // true if streaming, false if local playback
740} audio_offload_info_t;
741
742#define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \
743 ((((maj) & 0xff) << 8) | ((min) & 0xff))
744
745#define AUDIO_OFFLOAD_INFO_VERSION_0_1 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 1)
746#define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_1
747
748static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
749 version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
750 size: sizeof(audio_offload_info_t),
Mark Salyzyn48878422014-05-22 16:08:52 -0700751 sample_rate: 0,
752 channel_mask: 0,
753 format: AUDIO_FORMAT_DEFAULT,
754 stream_type: AUDIO_STREAM_VOICE_CALL,
755 bit_rate: 0,
756 duration_us: 0,
757 has_video: false,
758 is_streaming: false
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000759};
760
Eric Laurent0a381a32014-07-27 16:18:21 -0700761/* common audio stream configuration parameters
762 * You should memset() the entire structure to zero before use to
763 * ensure forward compatibility
764 */
765struct audio_config {
766 uint32_t sample_rate;
767 audio_channel_mask_t channel_mask;
768 audio_format_t format;
769 audio_offload_info_t offload_info;
770 size_t frame_count;
771};
772typedef struct audio_config audio_config_t;
773
774static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
775 sample_rate: 0,
776 channel_mask: AUDIO_CHANNEL_NONE,
777 format: AUDIO_FORMAT_DEFAULT,
778 offload_info: {
779 version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
780 size: sizeof(audio_offload_info_t),
781 sample_rate: 0,
782 channel_mask: 0,
783 format: AUDIO_FORMAT_DEFAULT,
784 stream_type: AUDIO_STREAM_VOICE_CALL,
785 bit_rate: 0,
786 duration_us: 0,
787 has_video: false,
788 is_streaming: false
789 },
790 frame_count: 0,
791};
792
Eric Laurent4cd05012014-02-18 12:40:36 -0800793
794/* audio hw module handle functions or structures referencing a module */
795typedef int audio_module_handle_t;
796
797/******************************
798 * Volume control
799 *****************************/
800
801/* If the audio hardware supports gain control on some audio paths,
802 * the platform can expose them in the audio_policy.conf file. The audio HAL
803 * will then implement gain control functions that will use the following data
804 * structures. */
805
806/* Type of gain control exposed by an audio port */
807#define AUDIO_GAIN_MODE_JOINT 0x1 /* supports joint channel gain control */
808#define AUDIO_GAIN_MODE_CHANNELS 0x2 /* supports separate channel gain control */
809#define AUDIO_GAIN_MODE_RAMP 0x4 /* supports gain ramps */
810
811typedef uint32_t audio_gain_mode_t;
812
813
814/* An audio_gain struct is a representation of a gain stage.
815 * A gain stage is always attached to an audio port. */
816struct audio_gain {
817 audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */
818 audio_channel_mask_t channel_mask; /* channels which gain an be controlled.
819 N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
820 int min_value; /* minimum gain value in millibels */
821 int max_value; /* maximum gain value in millibels */
822 int default_value; /* default gain value in millibels */
823 unsigned int step_value; /* gain step in millibels */
824 unsigned int min_ramp_ms; /* minimum ramp duration in ms */
825 unsigned int max_ramp_ms; /* maximum ramp duration in ms */
826};
827
828/* The gain configuration structure is used to get or set the gain values of a
829 * given port */
830struct audio_gain_config {
831 int index; /* index of the corresponding audio_gain in the
832 audio_port gains[] table */
833 audio_gain_mode_t mode; /* mode requested for this command */
834 audio_channel_mask_t channel_mask; /* channels which gain value follows.
835 N/A in joint mode */
Jungshik Jangf7e5aea2014-07-11 09:50:45 +0900836 int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
837 for each channel ordered from LSb to MSb in
838 channel mask. The number of values is 1 in joint
839 mode or popcount(channel_mask) */
Eric Laurent4cd05012014-02-18 12:40:36 -0800840 unsigned int ramp_duration_ms; /* ramp duration in ms */
841};
842
843/******************************
844 * Routing control
845 *****************************/
846
847/* Types defined here are used to describe an audio source or sink at internal
848 * framework interfaces (audio policy, patch panel) or at the audio HAL.
849 * Sink and sources are grouped in a concept of “audio port” representing an
850 * audio end point at the edge of the system managed by the module exposing
851 * the interface. */
852
853/* Audio port role: either source or sink */
854typedef enum {
855 AUDIO_PORT_ROLE_NONE,
856 AUDIO_PORT_ROLE_SOURCE,
857 AUDIO_PORT_ROLE_SINK,
858} audio_port_role_t;
859
860/* Audio port type indicates if it is a session (e.g AudioTrack),
861 * a mix (e.g PlaybackThread output) or a physical device
862 * (e.g AUDIO_DEVICE_OUT_SPEAKER) */
863typedef enum {
864 AUDIO_PORT_TYPE_NONE,
865 AUDIO_PORT_TYPE_DEVICE,
866 AUDIO_PORT_TYPE_MIX,
867 AUDIO_PORT_TYPE_SESSION,
868} audio_port_type_t;
869
870/* Each port has a unique ID or handle allocated by policy manager */
871typedef int audio_port_handle_t;
872#define AUDIO_PORT_HANDLE_NONE 0
873
874
875/* maximum audio device address length */
876#define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
877
878/* extension for audio port configuration structure when the audio port is a
879 * hardware device */
880struct audio_port_config_device_ext {
881 audio_module_handle_t hw_module; /* module the device is attached to */
882 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
883 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
884};
885
886/* extension for audio port configuration structure when the audio port is a
887 * sub mix */
888struct audio_port_config_mix_ext {
889 audio_module_handle_t hw_module; /* module the stream is attached to */
890 audio_io_handle_t handle; /* I/O handle of the input/output stream */
891 union {
892 //TODO: change use case for output streams: use strategy and mixer attributes
893 audio_stream_type_t stream;
894 audio_source_t source;
895 } usecase;
896};
897
898/* extension for audio port configuration structure when the audio port is an
899 * audio session */
900struct audio_port_config_session_ext {
901 audio_session_t session; /* audio session */
902};
903
904/* Flags indicating which fields are to be considered in struct audio_port_config */
905#define AUDIO_PORT_CONFIG_SAMPLE_RATE 0x1
906#define AUDIO_PORT_CONFIG_CHANNEL_MASK 0x2
907#define AUDIO_PORT_CONFIG_FORMAT 0x4
908#define AUDIO_PORT_CONFIG_GAIN 0x8
909#define AUDIO_PORT_CONFIG_ALL (AUDIO_PORT_CONFIG_SAMPLE_RATE | \
910 AUDIO_PORT_CONFIG_CHANNEL_MASK | \
911 AUDIO_PORT_CONFIG_FORMAT | \
912 AUDIO_PORT_CONFIG_GAIN)
913
914/* audio port configuration structure used to specify a particular configuration of
915 * an audio port */
916struct audio_port_config {
917 audio_port_handle_t id; /* port unique ID */
918 audio_port_role_t role; /* sink or source */
919 audio_port_type_t type; /* device, mix ... */
920 unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */
921 unsigned int sample_rate; /* sampling rate in Hz */
922 audio_channel_mask_t channel_mask; /* channel mask if applicable */
923 audio_format_t format; /* format if applicable */
924 struct audio_gain_config gain; /* gain to apply if applicable */
925 union {
926 struct audio_port_config_device_ext device; /* device specific info */
927 struct audio_port_config_mix_ext mix; /* mix specific info */
928 struct audio_port_config_session_ext session; /* session specific info */
929 } ext;
930};
931
932
933/* max number of sampling rates in audio port */
934#define AUDIO_PORT_MAX_SAMPLING_RATES 16
935/* max number of channel masks in audio port */
936#define AUDIO_PORT_MAX_CHANNEL_MASKS 16
937/* max number of audio formats in audio port */
938#define AUDIO_PORT_MAX_FORMATS 16
939/* max number of gain controls in audio port */
940#define AUDIO_PORT_MAX_GAINS 16
941
942/* extension for audio port structure when the audio port is a hardware device */
943struct audio_port_device_ext {
944 audio_module_handle_t hw_module; /* module the device is attached to */
945 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
946 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
947};
948
949/* Latency class of the audio mix */
950typedef enum {
951 AUDIO_LATENCY_LOW,
952 AUDIO_LATENCY_NORMAL,
953} audio_mix_latency_class_t;
954
955/* extension for audio port structure when the audio port is a sub mix */
956struct audio_port_mix_ext {
957 audio_module_handle_t hw_module; /* module the stream is attached to */
958 audio_io_handle_t handle; /* I/O handle of the input.output stream */
959 audio_mix_latency_class_t latency_class; /* latency class */
960 // other attributes: routing strategies
961};
962
963/* extension for audio port structure when the audio port is an audio session */
964struct audio_port_session_ext {
965 audio_session_t session; /* audio session */
966};
967
968
969struct audio_port {
970 audio_port_handle_t id; /* port unique ID */
971 audio_port_role_t role; /* sink or source */
972 audio_port_type_t type; /* device, mix ... */
973 unsigned int num_sample_rates; /* number of sampling rates in following array */
974 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
975 unsigned int num_channel_masks; /* number of channel masks in following array */
976 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
977 unsigned int num_formats; /* number of formats in following array */
978 audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
979 unsigned int num_gains; /* number of gains in following array */
980 struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
981 struct audio_port_config active_config; /* current audio port configuration */
982 union {
983 struct audio_port_device_ext device;
984 struct audio_port_mix_ext mix;
985 struct audio_port_session_ext session;
986 } ext;
987};
988
989/* An audio patch represents a connection between one or more source ports and
990 * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
991 * applications via framework APIs.
992 * Each patch is identified by a handle at the interface used to create that patch. For instance,
993 * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
994 * This handle is unique to a given audio HAL hardware module.
995 * But the same patch receives another system wide unique handle allocated by the framework.
996 * This unique handle is used for all transactions inside the framework.
997 */
998typedef int audio_patch_handle_t;
999#define AUDIO_PATCH_HANDLE_NONE 0
1000
1001#define AUDIO_PATCH_PORTS_MAX 16
1002
1003struct audio_patch {
1004 audio_patch_handle_t id; /* patch unique ID */
1005 unsigned int num_sources; /* number of sources in following array */
1006 struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
1007 unsigned int num_sinks; /* number of sinks in following array */
1008 struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
1009};
1010
1011
Eric Laurentd91e6df2014-08-01 14:33:10 -07001012
1013/* a HW synchronization source returned by the audio HAL */
1014typedef uint32_t audio_hw_sync_t;
1015
1016/* an invalid HW synchronization source indicating an error */
1017#define AUDIO_HW_SYNC_INVALID 0
1018
Dima Zavine8e4be52011-05-12 10:25:33 -07001019static inline bool audio_is_output_device(audio_devices_t device)
1020{
Eric Laurenteeeee802012-08-28 14:29:07 -07001021 if (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
1022 (popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL) == 0))
Dima Zavine8e4be52011-05-12 10:25:33 -07001023 return true;
1024 else
1025 return false;
1026}
1027
1028static inline bool audio_is_input_device(audio_devices_t device)
1029{
Eric Laurenteeeee802012-08-28 14:29:07 -07001030 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1031 device &= ~AUDIO_DEVICE_BIT_IN;
1032 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_ALL) == 0))
1033 return true;
1034 }
1035 return false;
Dima Zavine8e4be52011-05-12 10:25:33 -07001036}
1037
Eric Laurenteeeee802012-08-28 14:29:07 -07001038static inline bool audio_is_output_devices(audio_devices_t device)
1039{
1040 return (device & AUDIO_DEVICE_BIT_IN) == 0;
1041}
1042
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001043static inline bool audio_is_a2dp_in_device(audio_devices_t device)
1044{
1045 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1046 device &= ~AUDIO_DEVICE_BIT_IN;
1047 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP))
1048 return true;
1049 }
1050 return false;
1051}
Eric Laurenteeeee802012-08-28 14:29:07 -07001052
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001053static inline bool audio_is_a2dp_out_device(audio_devices_t device)
Dima Zavine8e4be52011-05-12 10:25:33 -07001054{
1055 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP))
1056 return true;
1057 else
1058 return false;
1059}
1060
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001061// Deprecated - use audio_is_a2dp_out_device() instead
1062static inline bool audio_is_a2dp_device(audio_devices_t device)
1063{
1064 return audio_is_a2dp_out_device(device);
1065}
1066
Dima Zavine8e4be52011-05-12 10:25:33 -07001067static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
1068{
Eric Laurentb5266302014-04-24 13:36:47 -07001069 if ((device & AUDIO_DEVICE_BIT_IN) == 0) {
1070 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0))
1071 return true;
1072 } else {
1073 device &= ~AUDIO_DEVICE_BIT_IN;
1074 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0))
1075 return true;
1076 }
1077
1078 return false;
Dima Zavine8e4be52011-05-12 10:25:33 -07001079}
1080
Paul McLean00b89722014-04-18 14:29:22 -07001081static inline bool audio_is_usb_out_device(audio_devices_t device)
1082{
1083 return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB));
1084}
1085
1086static inline bool audio_is_usb_in_device(audio_devices_t device)
1087{
1088 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1089 device &= ~AUDIO_DEVICE_BIT_IN;
1090 if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0)
1091 return true;
1092 }
1093 return false;
1094}
1095
1096/* OBSOLETE - use audio_is_usb_out_device() instead. */
Eric Laurent79f90bd2012-04-06 08:57:48 -07001097static inline bool audio_is_usb_device(audio_devices_t device)
1098{
Paul McLean00b89722014-04-18 14:29:22 -07001099 return audio_is_usb_out_device(device);
Eric Laurent79f90bd2012-04-06 08:57:48 -07001100}
1101
Jean-Michel Trivie11866a2012-08-16 17:53:30 -07001102static inline bool audio_is_remote_submix_device(audio_devices_t device)
1103{
Jeff Brown8ecc7af2013-08-16 20:09:37 -07001104 if ((device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) == AUDIO_DEVICE_OUT_REMOTE_SUBMIX
1105 || (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX)
Jean-Michel Trivie11866a2012-08-16 17:53:30 -07001106 return true;
1107 else
1108 return false;
1109}
1110
Glenn Kasten432b7c62014-04-14 17:47:03 -07001111/* Returns true if:
1112 * representation is valid, and
1113 * there is at least one channel bit set which _could_ correspond to an input channel, and
1114 * there are no channel bits set which could _not_ correspond to an input channel.
1115 * Otherwise returns false.
1116 */
Glenn Kastenf7326dc2013-07-19 14:50:21 -07001117static inline bool audio_is_input_channel(audio_channel_mask_t channel)
Dima Zavine8e4be52011-05-12 10:25:33 -07001118{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001119 uint32_t bits = audio_channel_mask_get_bits(channel);
1120 switch (audio_channel_mask_get_representation(channel)) {
1121 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1122 if (bits & ~AUDIO_CHANNEL_IN_ALL) {
1123 bits = 0;
1124 }
1125 // fall through
1126 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1127 return bits != 0;
1128 default:
Dima Zavine8e4be52011-05-12 10:25:33 -07001129 return false;
Glenn Kasten432b7c62014-04-14 17:47:03 -07001130 }
Dima Zavine8e4be52011-05-12 10:25:33 -07001131}
1132
Glenn Kasten432b7c62014-04-14 17:47:03 -07001133/* Returns true if:
1134 * representation is valid, and
1135 * there is at least one channel bit set which _could_ correspond to an output channel, and
1136 * there are no channel bits set which could _not_ correspond to an output channel.
1137 * Otherwise returns false.
1138 */
Glenn Kastenf7326dc2013-07-19 14:50:21 -07001139static inline bool audio_is_output_channel(audio_channel_mask_t channel)
Dima Zavine8e4be52011-05-12 10:25:33 -07001140{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001141 uint32_t bits = audio_channel_mask_get_bits(channel);
1142 switch (audio_channel_mask_get_representation(channel)) {
1143 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1144 if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
1145 bits = 0;
1146 }
1147 // fall through
1148 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1149 return bits != 0;
1150 default:
Dima Zavine8e4be52011-05-12 10:25:33 -07001151 return false;
Glenn Kasten432b7c62014-04-14 17:47:03 -07001152 }
Dima Zavine8e4be52011-05-12 10:25:33 -07001153}
1154
Andy Hunga7e8f862014-05-15 18:35:38 -07001155/* Returns the number of channels from an input channel mask,
1156 * used in the context of audio input or recording.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001157 * If a channel bit is set which could _not_ correspond to an input channel,
1158 * it is excluded from the count.
1159 * Returns zero if the representation is invalid.
Andy Hunga7e8f862014-05-15 18:35:38 -07001160 */
1161static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
1162{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001163 uint32_t bits = audio_channel_mask_get_bits(channel);
1164 switch (audio_channel_mask_get_representation(channel)) {
1165 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1166 // TODO: We can now merge with from_out_mask and remove anding
1167 bits &= AUDIO_CHANNEL_IN_ALL;
1168 // fall through
1169 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1170 return popcount(bits);
1171 default:
1172 return 0;
1173 }
Andy Hunga7e8f862014-05-15 18:35:38 -07001174}
1175
1176/* Returns the number of channels from an output channel mask,
1177 * used in the context of audio output or playback.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001178 * If a channel bit is set which could _not_ correspond to an output channel,
1179 * it is excluded from the count.
1180 * Returns zero if the representation is invalid.
Andy Hunga7e8f862014-05-15 18:35:38 -07001181 */
1182static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
1183{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001184 uint32_t bits = audio_channel_mask_get_bits(channel);
1185 switch (audio_channel_mask_get_representation(channel)) {
1186 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1187 // TODO: We can now merge with from_in_mask and remove anding
1188 bits &= AUDIO_CHANNEL_OUT_ALL;
1189 // fall through
1190 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1191 return popcount(bits);
1192 default:
1193 return 0;
1194 }
Andy Hunga7e8f862014-05-15 18:35:38 -07001195}
1196
Glenn Kasten432b7c62014-04-14 17:47:03 -07001197/* Derive an output channel mask for position assignment from a channel count.
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001198 * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
1199 * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
1200 * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
1201 * for continuity with stereo.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001202 * Returns the matching channel mask,
1203 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1204 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1205 * configurations for which a default output channel mask is defined.
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001206 */
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001207static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001208{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001209 uint32_t bits;
Glenn Kasten1c3e16f2011-12-19 09:21:48 -08001210 switch (channel_count) {
Glenn Kasten432b7c62014-04-14 17:47:03 -07001211 case 0:
1212 return AUDIO_CHANNEL_NONE;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001213 case 1:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001214 bits = AUDIO_CHANNEL_OUT_MONO;
1215 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001216 case 2:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001217 bits = AUDIO_CHANNEL_OUT_STEREO;
1218 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001219 case 3:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001220 bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER;
1221 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001222 case 4: // 4.0
Glenn Kasten432b7c62014-04-14 17:47:03 -07001223 bits = AUDIO_CHANNEL_OUT_QUAD;
1224 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001225 case 5: // 5.0
Glenn Kasten432b7c62014-04-14 17:47:03 -07001226 bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
1227 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001228 case 6: // 5.1
Glenn Kasten432b7c62014-04-14 17:47:03 -07001229 bits = AUDIO_CHANNEL_OUT_5POINT1;
1230 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001231 case 7: // 6.1
Glenn Kasten432b7c62014-04-14 17:47:03 -07001232 bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
1233 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001234 case 8:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001235 bits = AUDIO_CHANNEL_OUT_7POINT1;
1236 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001237 default:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001238 return AUDIO_CHANNEL_INVALID;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001239 }
Glenn Kasten432b7c62014-04-14 17:47:03 -07001240 return audio_channel_mask_from_representation_and_bits(
1241 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001242}
1243
Glenn Kasten432b7c62014-04-14 17:47:03 -07001244/* Derive an input channel mask for position assignment from a channel count.
1245 * Currently handles only mono and stereo.
1246 * Returns the matching channel mask,
1247 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1248 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1249 * configurations for which a default input channel mask is defined.
1250 */
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001251static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count)
1252{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001253 uint32_t bits;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001254 switch (channel_count) {
Glenn Kasten432b7c62014-04-14 17:47:03 -07001255 case 0:
1256 return AUDIO_CHANNEL_NONE;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001257 case 1:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001258 bits = AUDIO_CHANNEL_IN_MONO;
1259 break;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001260 case 2:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001261 bits = AUDIO_CHANNEL_IN_STEREO;
1262 break;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001263 default:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001264 return AUDIO_CHANNEL_INVALID;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001265 }
Glenn Kasten432b7c62014-04-14 17:47:03 -07001266 return audio_channel_mask_from_representation_and_bits(
1267 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1268}
1269
1270/* Derive a channel mask for index assignment from a channel count.
1271 * Returns the matching channel mask,
1272 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1273 * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
1274 */
1275static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
1276 uint32_t channel_count)
1277{
1278 if (channel_count == 0) {
1279 return AUDIO_CHANNEL_NONE;
1280 }
1281 if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
1282 return AUDIO_CHANNEL_INVALID;
1283 }
1284 uint32_t bits = (1 << channel_count) - 1;
1285 return audio_channel_mask_from_representation_and_bits(
1286 AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001287}
1288
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001289static inline bool audio_is_valid_format(audio_format_t format)
Dima Zavine8e4be52011-05-12 10:25:33 -07001290{
1291 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1292 case AUDIO_FORMAT_PCM:
Glenn Kasten45b95812014-03-07 12:31:03 -08001293 switch (format) {
1294 case AUDIO_FORMAT_PCM_16_BIT:
1295 case AUDIO_FORMAT_PCM_8_BIT:
1296 case AUDIO_FORMAT_PCM_32_BIT:
1297 case AUDIO_FORMAT_PCM_8_24_BIT:
1298 case AUDIO_FORMAT_PCM_FLOAT:
1299 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1300 return true;
1301 default:
Eric Laurentda382242011-06-16 21:13:43 -07001302 return false;
1303 }
Glenn Kasten45b95812014-03-07 12:31:03 -08001304 /* not reached */
Dima Zavine8e4be52011-05-12 10:25:33 -07001305 case AUDIO_FORMAT_MP3:
1306 case AUDIO_FORMAT_AMR_NB:
1307 case AUDIO_FORMAT_AMR_WB:
1308 case AUDIO_FORMAT_AAC:
1309 case AUDIO_FORMAT_HE_AAC_V1:
1310 case AUDIO_FORMAT_HE_AAC_V2:
1311 case AUDIO_FORMAT_VORBIS:
Eric Laurentce30de32014-06-10 15:39:16 -07001312 case AUDIO_FORMAT_OPUS:
1313 case AUDIO_FORMAT_AC3:
1314 case AUDIO_FORMAT_E_AC3:
Dima Zavine8e4be52011-05-12 10:25:33 -07001315 return true;
1316 default:
1317 return false;
1318 }
1319}
1320
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001321static inline bool audio_is_linear_pcm(audio_format_t format)
Dima Zavine8e4be52011-05-12 10:25:33 -07001322{
Eric Laurentda382242011-06-16 21:13:43 -07001323 return ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM);
Dima Zavine8e4be52011-05-12 10:25:33 -07001324}
1325
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001326static inline size_t audio_bytes_per_sample(audio_format_t format)
Eric Laurentda382242011-06-16 21:13:43 -07001327{
1328 size_t size = 0;
1329
1330 switch (format) {
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001331 case AUDIO_FORMAT_PCM_32_BIT:
1332 case AUDIO_FORMAT_PCM_8_24_BIT:
1333 size = sizeof(int32_t);
1334 break;
Andy Hung173f4992014-03-06 17:18:58 -08001335 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1336 size = sizeof(uint8_t) * 3;
1337 break;
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001338 case AUDIO_FORMAT_PCM_16_BIT:
1339 size = sizeof(int16_t);
1340 break;
1341 case AUDIO_FORMAT_PCM_8_BIT:
1342 size = sizeof(uint8_t);
1343 break;
Glenn Kasten1c3e16f2011-12-19 09:21:48 -08001344 case AUDIO_FORMAT_PCM_FLOAT:
1345 size = sizeof(float);
1346 break;
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001347 default:
1348 break;
Eric Laurentda382242011-06-16 21:13:43 -07001349 }
1350 return size;
1351}
Dima Zavine8e4be52011-05-12 10:25:33 -07001352
Eric Laurenta768c302014-07-28 11:33:43 -07001353/* converts device address to string sent to audio HAL via set_parameters */
1354static char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
1355{
1356 const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address=");
1357 char param[kSize];
1358
1359 if (device & AUDIO_DEVICE_OUT_ALL_A2DP)
1360 snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
1361 else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
1362 snprintf(param, kSize, "%s=%s", "mix", address);
1363 else
1364 snprintf(param, kSize, "%s", address);
1365
1366 return strdup(param);
1367}
1368
1369
Dima Zavine8e4be52011-05-12 10:25:33 -07001370__END_DECLS
1371
1372#endif // ANDROID_AUDIO_CORE_H