blob: 1c7024029b593d6297b4b4dc0849d25bc1fb423f [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,
60 AUDIO_STREAM_TTS = 9,
61
62 AUDIO_STREAM_CNT,
63 AUDIO_STREAM_MAX = AUDIO_STREAM_CNT - 1,
64} audio_stream_type_t;
65
66/* Do not change these values without updating their counterparts
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -070067 * in frameworks/base/media/java/android/media/AudioAttributes.java
68 */
69typedef enum {
70 AUDIO_CONTENT_TYPE_UNKNOWN = 0,
71 AUDIO_CONTENT_TYPE_SPEECH = 1,
72 AUDIO_CONTENT_TYPE_MUSIC = 2,
73 AUDIO_CONTENT_TYPE_MOVIE = 3,
74 AUDIO_CONTENT_TYPE_SONIFICATION = 4,
75
76 AUDIO_CONTENT_TYPE_CNT,
77 AUDIO_CONTENT_TYPE_MAX = AUDIO_CONTENT_TYPE_CNT - 1,
78} audio_content_type_t;
79
80/* Do not change these values without updating their counterparts
81 * in frameworks/base/media/java/android/media/AudioAttributes.java
82 */
83typedef enum {
84 AUDIO_USAGE_UNKNOWN = 0,
85 AUDIO_USAGE_MEDIA = 1,
86 AUDIO_USAGE_VOICE_COMMUNICATION = 2,
87 AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING = 3,
88 AUDIO_USAGE_ALARM = 4,
89 AUDIO_USAGE_NOTIFICATION = 5,
90 AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE = 6,
91 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST = 7,
92 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT = 8,
93 AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED = 9,
94 AUDIO_USAGE_NOTIFICATION_EVENT = 10,
95 AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY = 11,
96 AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12,
97 AUDIO_USAGE_ASSISTANCE_SONIFICATION = 13,
98 AUDIO_USAGE_GAME = 14,
99
100 AUDIO_USAGE_CNT,
101 AUDIO_USAGE_MAX = AUDIO_USAGE_CNT - 1,
102} audio_usage_t;
103
104typedef uint32_t audio_flags_mask_t;
105
106/* Do not change these values without updating their counterparts
107 * in frameworks/base/media/java/android/media/AudioAttributes.java
108 */
109enum {
110 AUDIO_FLAG_AUDIBILITY_ENFORCED = 0x1,
111 AUDIO_FLAG_SECURE = 0x2,
112 AUDIO_FLAG_SCO = 0x4,
Eric Laurentd91e6df2014-08-01 14:33:10 -0700113 AUDIO_FLAG_BEACON = 0x8,
114 AUDIO_FLAG_HW_AV_SYNC = 0x10
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -0700115};
116
117/* Do not change these values without updating their counterparts
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800118 * in frameworks/base/media/java/android/media/MediaRecorder.java,
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700119 * frameworks/av/services/audiopolicy/AudioPolicyService.cpp,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800120 * and system/media/audio_effects/include/audio_effects/audio_effects_conf.h!
Dima Zavine8e4be52011-05-12 10:25:33 -0700121 */
122typedef enum {
123 AUDIO_SOURCE_DEFAULT = 0,
124 AUDIO_SOURCE_MIC = 1,
125 AUDIO_SOURCE_VOICE_UPLINK = 2,
126 AUDIO_SOURCE_VOICE_DOWNLINK = 3,
127 AUDIO_SOURCE_VOICE_CALL = 4,
128 AUDIO_SOURCE_CAMCORDER = 5,
129 AUDIO_SOURCE_VOICE_RECOGNITION = 6,
130 AUDIO_SOURCE_VOICE_COMMUNICATION = 7,
Jean-Michel Trivie11866a2012-08-16 17:53:30 -0700131 AUDIO_SOURCE_REMOTE_SUBMIX = 8, /* Source for the mix to be presented remotely. */
132 /* An example of remote presentation is Wifi Display */
133 /* where a dongle attached to a TV can be used to */
134 /* play the mix captured by this audio source. */
Dima Zavine8e4be52011-05-12 10:25:33 -0700135 AUDIO_SOURCE_CNT,
136 AUDIO_SOURCE_MAX = AUDIO_SOURCE_CNT - 1,
Eric Laurent04c12ca2013-09-09 10:39:54 -0700137 AUDIO_SOURCE_HOTWORD = 1999, /* A low-priority, preemptible audio source for
138 for background software hotword detection.
139 Same tuning as AUDIO_SOURCE_VOICE_RECOGNITION.
140 Used only internally to the framework. Not exposed
141 at the audio HAL. */
Dima Zavine8e4be52011-05-12 10:25:33 -0700142} audio_source_t;
143
Jean-Michel Trivi0d580e82014-05-21 14:43:14 -0700144/* Audio attributes */
145#define AUDIO_ATTRIBUTES_TAGS_MAX_SIZE 256
146typedef struct {
147 audio_content_type_t content_type;
148 audio_usage_t usage;
149 audio_source_t source;
150 audio_flags_mask_t flags;
151 char tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE]; /* UTF8 */
152} audio_attributes_t;
153
Dima Zavine8e4be52011-05-12 10:25:33 -0700154/* special audio session values
155 * (XXX: should this be living in the audio effects land?)
156 */
157typedef enum {
158 /* session for effects attached to a particular output stream
159 * (value must be less than 0)
160 */
161 AUDIO_SESSION_OUTPUT_STAGE = -1,
162
163 /* session for effects applied to output mix. These effects can
164 * be moved by audio policy manager to another output stream
165 * (value must be 0)
166 */
167 AUDIO_SESSION_OUTPUT_MIX = 0,
Glenn Kastenb4f2b4e2012-06-25 14:57:36 -0700168
169 /* application does not specify an explicit session ID to be used,
170 * and requests a new session ID to be allocated
171 * TODO use unique values for AUDIO_SESSION_OUTPUT_MIX and AUDIO_SESSION_ALLOCATE,
172 * after all uses have been updated from 0 to the appropriate symbol, and have been tested.
173 */
174 AUDIO_SESSION_ALLOCATE = 0,
Dima Zavine8e4be52011-05-12 10:25:33 -0700175} audio_session_t;
176
Eric Laurent1e92ed52014-07-28 09:26:06 -0700177/* a unique ID allocated by AudioFlinger for use as a audio_io_handle_t or audio_session_t */
178typedef int audio_unique_id_t;
179
180#define AUDIO_UNIQUE_ID_ALLOCATE AUDIO_SESSION_ALLOCATE
181
Dima Zavine8e4be52011-05-12 10:25:33 -0700182/* Audio sub formats (see enum audio_format). */
183
184/* PCM sub formats */
185typedef enum {
Glenn Kastenc0a51092014-01-31 07:51:17 -0800186 /* All of these are in native byte order */
Eric Laurent9714b272011-05-26 13:52:47 -0700187 AUDIO_FORMAT_PCM_SUB_16_BIT = 0x1, /* DO NOT CHANGE - PCM signed 16 bits */
188 AUDIO_FORMAT_PCM_SUB_8_BIT = 0x2, /* DO NOT CHANGE - PCM unsigned 8 bits */
189 AUDIO_FORMAT_PCM_SUB_32_BIT = 0x3, /* PCM signed .31 fixed point */
190 AUDIO_FORMAT_PCM_SUB_8_24_BIT = 0x4, /* PCM signed 7.24 fixed point */
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800191 AUDIO_FORMAT_PCM_SUB_FLOAT = 0x5, /* PCM single-precision floating point */
Glenn Kastenc0a51092014-01-31 07:51:17 -0800192 AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED = 0x6, /* PCM signed .23 fixed point packed in 3 bytes */
Dima Zavine8e4be52011-05-12 10:25:33 -0700193} audio_format_pcm_sub_fmt_t;
194
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800195/* The audio_format_*_sub_fmt_t declarations are not currently used */
196
Dima Zavine8e4be52011-05-12 10:25:33 -0700197/* MP3 sub format field definition : can use 11 LSBs in the same way as MP3
198 * frame header to specify bit rate, stereo mode, version...
199 */
200typedef enum {
201 AUDIO_FORMAT_MP3_SUB_NONE = 0x0,
202} audio_format_mp3_sub_fmt_t;
203
204/* AMR NB/WB sub format field definition: specify frame block interleaving,
205 * bandwidth efficient or octet aligned, encoding mode for recording...
206 */
207typedef enum {
208 AUDIO_FORMAT_AMR_SUB_NONE = 0x0,
209} audio_format_amr_sub_fmt_t;
210
211/* AAC sub format field definition: specify profile or bitrate for recording... */
212typedef enum {
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530213 AUDIO_FORMAT_AAC_SUB_MAIN = 0x1,
214 AUDIO_FORMAT_AAC_SUB_LC = 0x2,
215 AUDIO_FORMAT_AAC_SUB_SSR = 0x4,
216 AUDIO_FORMAT_AAC_SUB_LTP = 0x8,
217 AUDIO_FORMAT_AAC_SUB_HE_V1 = 0x10,
218 AUDIO_FORMAT_AAC_SUB_SCALABLE = 0x20,
219 AUDIO_FORMAT_AAC_SUB_ERLC = 0x40,
220 AUDIO_FORMAT_AAC_SUB_LD = 0x80,
221 AUDIO_FORMAT_AAC_SUB_HE_V2 = 0x100,
222 AUDIO_FORMAT_AAC_SUB_ELD = 0x200,
Dima Zavine8e4be52011-05-12 10:25:33 -0700223} audio_format_aac_sub_fmt_t;
224
225/* VORBIS sub format field definition: specify quality for recording... */
226typedef enum {
227 AUDIO_FORMAT_VORBIS_SUB_NONE = 0x0,
228} audio_format_vorbis_sub_fmt_t;
229
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800230/* Audio format consists of a main format field (upper 8 bits) and a sub format
Dima Zavine8e4be52011-05-12 10:25:33 -0700231 * field (lower 24 bits).
232 *
233 * The main format indicates the main codec type. The sub format field
234 * indicates options and parameters for each format. The sub format is mainly
235 * used for record to indicate for instance the requested bitrate or profile.
236 * It can also be used for certain formats to give informations not present in
237 * the encoded audio stream (e.g. octet alignement for AMR).
238 */
239typedef enum {
240 AUDIO_FORMAT_INVALID = 0xFFFFFFFFUL,
241 AUDIO_FORMAT_DEFAULT = 0,
242 AUDIO_FORMAT_PCM = 0x00000000UL, /* DO NOT CHANGE */
243 AUDIO_FORMAT_MP3 = 0x01000000UL,
244 AUDIO_FORMAT_AMR_NB = 0x02000000UL,
245 AUDIO_FORMAT_AMR_WB = 0x03000000UL,
246 AUDIO_FORMAT_AAC = 0x04000000UL,
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530247 AUDIO_FORMAT_HE_AAC_V1 = 0x05000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V1*/
248 AUDIO_FORMAT_HE_AAC_V2 = 0x06000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V2*/
Dima Zavine8e4be52011-05-12 10:25:33 -0700249 AUDIO_FORMAT_VORBIS = 0x07000000UL,
Vignesh Venkatasubramanian76edb1c2014-01-29 09:47:56 -0800250 AUDIO_FORMAT_OPUS = 0x08000000UL,
Eric Laurentce30de32014-06-10 15:39:16 -0700251 AUDIO_FORMAT_AC3 = 0x09000000UL,
252 AUDIO_FORMAT_E_AC3 = 0x0A000000UL,
Dima Zavine8e4be52011-05-12 10:25:33 -0700253 AUDIO_FORMAT_MAIN_MASK = 0xFF000000UL,
254 AUDIO_FORMAT_SUB_MASK = 0x00FFFFFFUL,
255
256 /* Aliases */
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800257 /* note != AudioFormat.ENCODING_PCM_16BIT */
Dima Zavine8e4be52011-05-12 10:25:33 -0700258 AUDIO_FORMAT_PCM_16_BIT = (AUDIO_FORMAT_PCM |
259 AUDIO_FORMAT_PCM_SUB_16_BIT),
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800260 /* note != AudioFormat.ENCODING_PCM_8BIT */
Dima Zavine8e4be52011-05-12 10:25:33 -0700261 AUDIO_FORMAT_PCM_8_BIT = (AUDIO_FORMAT_PCM |
262 AUDIO_FORMAT_PCM_SUB_8_BIT),
Eric Laurent9714b272011-05-26 13:52:47 -0700263 AUDIO_FORMAT_PCM_32_BIT = (AUDIO_FORMAT_PCM |
264 AUDIO_FORMAT_PCM_SUB_32_BIT),
265 AUDIO_FORMAT_PCM_8_24_BIT = (AUDIO_FORMAT_PCM |
266 AUDIO_FORMAT_PCM_SUB_8_24_BIT),
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800267 AUDIO_FORMAT_PCM_FLOAT = (AUDIO_FORMAT_PCM |
268 AUDIO_FORMAT_PCM_SUB_FLOAT),
Glenn Kastenc0a51092014-01-31 07:51:17 -0800269 AUDIO_FORMAT_PCM_24_BIT_PACKED = (AUDIO_FORMAT_PCM |
270 AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED),
aarti jadhav-gaikwad923e7402014-06-18 15:23:09 +0530271 AUDIO_FORMAT_AAC_MAIN = (AUDIO_FORMAT_AAC |
272 AUDIO_FORMAT_AAC_SUB_MAIN),
273 AUDIO_FORMAT_AAC_LC = (AUDIO_FORMAT_AAC |
274 AUDIO_FORMAT_AAC_SUB_LC),
275 AUDIO_FORMAT_AAC_SSR = (AUDIO_FORMAT_AAC |
276 AUDIO_FORMAT_AAC_SUB_SSR),
277 AUDIO_FORMAT_AAC_LTP = (AUDIO_FORMAT_AAC |
278 AUDIO_FORMAT_AAC_SUB_LTP),
279 AUDIO_FORMAT_AAC_HE_V1 = (AUDIO_FORMAT_AAC |
280 AUDIO_FORMAT_AAC_SUB_HE_V1),
281 AUDIO_FORMAT_AAC_SCALABLE = (AUDIO_FORMAT_AAC |
282 AUDIO_FORMAT_AAC_SUB_SCALABLE),
283 AUDIO_FORMAT_AAC_ERLC = (AUDIO_FORMAT_AAC |
284 AUDIO_FORMAT_AAC_SUB_ERLC),
285 AUDIO_FORMAT_AAC_LD = (AUDIO_FORMAT_AAC |
286 AUDIO_FORMAT_AAC_SUB_LD),
287 AUDIO_FORMAT_AAC_HE_V2 = (AUDIO_FORMAT_AAC |
288 AUDIO_FORMAT_AAC_SUB_HE_V2),
289 AUDIO_FORMAT_AAC_ELD = (AUDIO_FORMAT_AAC |
290 AUDIO_FORMAT_AAC_SUB_ELD),
Dima Zavine8e4be52011-05-12 10:25:33 -0700291} audio_format_t;
292
Glenn Kasten432b7c62014-04-14 17:47:03 -0700293/* For the channel mask for position assignment representation */
Glenn Kasten16a7a042012-07-03 15:21:05 -0700294enum {
Glenn Kasten432b7c62014-04-14 17:47:03 -0700295
296/* These can be a complete audio_channel_mask_t. */
297
Eric Laurent4cd05012014-02-18 12:40:36 -0800298 AUDIO_CHANNEL_NONE = 0x0,
Glenn Kasten432b7c62014-04-14 17:47:03 -0700299 AUDIO_CHANNEL_INVALID = 0xC0000000,
300
301/* These can be the bits portion of an audio_channel_mask_t
302 * with representation AUDIO_CHANNEL_REPRESENTATION_POSITION.
303 * Using these bits as a complete audio_channel_mask_t is deprecated.
304 */
305
Dima Zavine8e4be52011-05-12 10:25:33 -0700306 /* output channels */
Jean-Michel Trivi6d4f3972011-07-25 16:19:07 -0700307 AUDIO_CHANNEL_OUT_FRONT_LEFT = 0x1,
308 AUDIO_CHANNEL_OUT_FRONT_RIGHT = 0x2,
309 AUDIO_CHANNEL_OUT_FRONT_CENTER = 0x4,
310 AUDIO_CHANNEL_OUT_LOW_FREQUENCY = 0x8,
311 AUDIO_CHANNEL_OUT_BACK_LEFT = 0x10,
312 AUDIO_CHANNEL_OUT_BACK_RIGHT = 0x20,
313 AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x40,
314 AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x80,
315 AUDIO_CHANNEL_OUT_BACK_CENTER = 0x100,
316 AUDIO_CHANNEL_OUT_SIDE_LEFT = 0x200,
317 AUDIO_CHANNEL_OUT_SIDE_RIGHT = 0x400,
318 AUDIO_CHANNEL_OUT_TOP_CENTER = 0x800,
319 AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT = 0x1000,
320 AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER = 0x2000,
321 AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT = 0x4000,
322 AUDIO_CHANNEL_OUT_TOP_BACK_LEFT = 0x8000,
323 AUDIO_CHANNEL_OUT_TOP_BACK_CENTER = 0x10000,
324 AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT = 0x20000,
Dima Zavine8e4be52011-05-12 10:25:33 -0700325
Glenn Kasten432b7c62014-04-14 17:47:03 -0700326/* TODO: should these be considered complete channel masks, or only bits? */
327
Dima Zavine8e4be52011-05-12 10:25:33 -0700328 AUDIO_CHANNEL_OUT_MONO = AUDIO_CHANNEL_OUT_FRONT_LEFT,
329 AUDIO_CHANNEL_OUT_STEREO = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
330 AUDIO_CHANNEL_OUT_FRONT_RIGHT),
331 AUDIO_CHANNEL_OUT_QUAD = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
332 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
333 AUDIO_CHANNEL_OUT_BACK_LEFT |
334 AUDIO_CHANNEL_OUT_BACK_RIGHT),
Glenn Kastenb8d59392014-04-29 08:41:46 -0700335 AUDIO_CHANNEL_OUT_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD,
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700336 /* like AUDIO_CHANNEL_OUT_QUAD_BACK with *_SIDE_* instead of *_BACK_* */
337 AUDIO_CHANNEL_OUT_QUAD_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
338 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
339 AUDIO_CHANNEL_OUT_SIDE_LEFT |
340 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700341 AUDIO_CHANNEL_OUT_5POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
342 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
343 AUDIO_CHANNEL_OUT_FRONT_CENTER |
344 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
345 AUDIO_CHANNEL_OUT_BACK_LEFT |
346 AUDIO_CHANNEL_OUT_BACK_RIGHT),
Glenn Kastenb8d59392014-04-29 08:41:46 -0700347 AUDIO_CHANNEL_OUT_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1,
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700348 /* like AUDIO_CHANNEL_OUT_5POINT1_BACK with *_SIDE_* instead of *_BACK_* */
349 AUDIO_CHANNEL_OUT_5POINT1_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
350 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
351 AUDIO_CHANNEL_OUT_FRONT_CENTER |
352 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
353 AUDIO_CHANNEL_OUT_SIDE_LEFT |
354 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700355 // matches the correct AudioFormat.CHANNEL_OUT_7POINT1_SURROUND definition for 7.1
Dima Zavine8e4be52011-05-12 10:25:33 -0700356 AUDIO_CHANNEL_OUT_7POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
357 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
358 AUDIO_CHANNEL_OUT_FRONT_CENTER |
359 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
360 AUDIO_CHANNEL_OUT_BACK_LEFT |
361 AUDIO_CHANNEL_OUT_BACK_RIGHT |
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700362 AUDIO_CHANNEL_OUT_SIDE_LEFT |
363 AUDIO_CHANNEL_OUT_SIDE_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700364 AUDIO_CHANNEL_OUT_ALL = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
365 AUDIO_CHANNEL_OUT_FRONT_RIGHT |
366 AUDIO_CHANNEL_OUT_FRONT_CENTER |
367 AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
368 AUDIO_CHANNEL_OUT_BACK_LEFT |
369 AUDIO_CHANNEL_OUT_BACK_RIGHT |
370 AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER |
371 AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
Jean-Michel Trivi33fad992011-07-24 16:31:03 -0700372 AUDIO_CHANNEL_OUT_BACK_CENTER|
373 AUDIO_CHANNEL_OUT_SIDE_LEFT|
374 AUDIO_CHANNEL_OUT_SIDE_RIGHT|
375 AUDIO_CHANNEL_OUT_TOP_CENTER|
376 AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT|
377 AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER|
378 AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT|
379 AUDIO_CHANNEL_OUT_TOP_BACK_LEFT|
380 AUDIO_CHANNEL_OUT_TOP_BACK_CENTER|
381 AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT),
Dima Zavine8e4be52011-05-12 10:25:33 -0700382
Glenn Kasten432b7c62014-04-14 17:47:03 -0700383/* These are bits only, not complete values */
384
Dima Zavine8e4be52011-05-12 10:25:33 -0700385 /* input channels */
386 AUDIO_CHANNEL_IN_LEFT = 0x4,
387 AUDIO_CHANNEL_IN_RIGHT = 0x8,
388 AUDIO_CHANNEL_IN_FRONT = 0x10,
389 AUDIO_CHANNEL_IN_BACK = 0x20,
390 AUDIO_CHANNEL_IN_LEFT_PROCESSED = 0x40,
391 AUDIO_CHANNEL_IN_RIGHT_PROCESSED = 0x80,
392 AUDIO_CHANNEL_IN_FRONT_PROCESSED = 0x100,
393 AUDIO_CHANNEL_IN_BACK_PROCESSED = 0x200,
394 AUDIO_CHANNEL_IN_PRESSURE = 0x400,
395 AUDIO_CHANNEL_IN_X_AXIS = 0x800,
396 AUDIO_CHANNEL_IN_Y_AXIS = 0x1000,
397 AUDIO_CHANNEL_IN_Z_AXIS = 0x2000,
398 AUDIO_CHANNEL_IN_VOICE_UPLINK = 0x4000,
399 AUDIO_CHANNEL_IN_VOICE_DNLINK = 0x8000,
400
Glenn Kasten432b7c62014-04-14 17:47:03 -0700401/* TODO: should these be considered complete channel masks, or only bits, or deprecated? */
402
Dima Zavine8e4be52011-05-12 10:25:33 -0700403 AUDIO_CHANNEL_IN_MONO = AUDIO_CHANNEL_IN_FRONT,
404 AUDIO_CHANNEL_IN_STEREO = (AUDIO_CHANNEL_IN_LEFT | AUDIO_CHANNEL_IN_RIGHT),
Eric Laurent6c70cee2013-01-17 17:31:49 -0800405 AUDIO_CHANNEL_IN_FRONT_BACK = (AUDIO_CHANNEL_IN_FRONT | AUDIO_CHANNEL_IN_BACK),
Dima Zavine8e4be52011-05-12 10:25:33 -0700406 AUDIO_CHANNEL_IN_ALL = (AUDIO_CHANNEL_IN_LEFT |
407 AUDIO_CHANNEL_IN_RIGHT |
408 AUDIO_CHANNEL_IN_FRONT |
409 AUDIO_CHANNEL_IN_BACK|
410 AUDIO_CHANNEL_IN_LEFT_PROCESSED |
411 AUDIO_CHANNEL_IN_RIGHT_PROCESSED |
412 AUDIO_CHANNEL_IN_FRONT_PROCESSED |
413 AUDIO_CHANNEL_IN_BACK_PROCESSED|
414 AUDIO_CHANNEL_IN_PRESSURE |
415 AUDIO_CHANNEL_IN_X_AXIS |
416 AUDIO_CHANNEL_IN_Y_AXIS |
417 AUDIO_CHANNEL_IN_Z_AXIS |
418 AUDIO_CHANNEL_IN_VOICE_UPLINK |
419 AUDIO_CHANNEL_IN_VOICE_DNLINK),
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -0800420};
421
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700422/* A channel mask per se only defines the presence or absence of a channel, not the order.
423 * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
Glenn Kasten432b7c62014-04-14 17:47:03 -0700424 *
425 * audio_channel_mask_t is an opaque type and its internal layout should not
426 * be assumed as it may change in the future.
427 * Instead, always use the functions declared in this header to examine.
428 *
429 * These are the current representations:
430 *
431 * AUDIO_CHANNEL_REPRESENTATION_POSITION
432 * is a channel mask representation for position assignment.
433 * Each low-order bit corresponds to the spatial position of a transducer (output),
434 * or interpretation of channel (input).
435 * The user of a channel mask needs to know the context of whether it is for output or input.
436 * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
437 * It is not permitted for no bits to be set.
438 *
439 * AUDIO_CHANNEL_REPRESENTATION_INDEX
440 * is a channel mask representation for index assignment.
441 * Each low-order bit corresponds to a selected channel.
442 * There is no platform interpretation of the various bits.
443 * There is no concept of output or input.
444 * It is not permitted for no bits to be set.
445 *
446 * All other representations are reserved for future use.
447 *
448 * Warning: current representation distinguishes between input and output, but this will not the be
449 * case in future revisions of the platform. Wherever there is an ambiguity between input and output
450 * that is currently resolved by checking the channel mask, the implementer should look for ways to
451 * fix it with additional information outside of the mask.
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700452 */
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -0800453typedef uint32_t audio_channel_mask_t;
Dima Zavine8e4be52011-05-12 10:25:33 -0700454
Glenn Kasten432b7c62014-04-14 17:47:03 -0700455/* Maximum number of channels for all representations */
456#define AUDIO_CHANNEL_COUNT_MAX 30
457
458/* log(2) of maximum number of representations, not part of public API */
459#define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
460
461/* Representations */
462typedef enum {
463 AUDIO_CHANNEL_REPRESENTATION_POSITION = 0, // must be zero for compatibility
464 // 1 is reserved for future use
465 AUDIO_CHANNEL_REPRESENTATION_INDEX = 2,
466 // 3 is reserved for future use
467} audio_channel_representation_t;
468
469/* The return value is undefined if the channel mask is invalid. */
470static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
471{
472 return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
473}
474
475/* The return value is undefined if the channel mask is invalid. */
476static inline audio_channel_representation_t audio_channel_mask_get_representation(
477 audio_channel_mask_t channel)
478{
479 // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
480 return (audio_channel_representation_t)
481 ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
482}
483
484/* Returns true if the channel mask is valid,
485 * or returns false for AUDIO_CHANNEL_NONE, AUDIO_CHANNEL_INVALID, and other invalid values.
486 * This function is unable to determine whether a channel mask for position assignment
487 * is invalid because an output mask has an invalid output bit set,
488 * or because an input mask has an invalid input bit set.
489 * All other APIs that take a channel mask assume that it is valid.
490 */
491static inline bool audio_channel_mask_is_valid(audio_channel_mask_t channel)
492{
493 uint32_t bits = audio_channel_mask_get_bits(channel);
494 audio_channel_representation_t representation = audio_channel_mask_get_representation(channel);
495 switch (representation) {
496 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
497 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
498 break;
499 default:
500 bits = 0;
501 break;
502 }
503 return bits != 0;
504}
505
506/* Not part of public API */
507static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
508 audio_channel_representation_t representation, uint32_t bits)
509{
510 return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
511}
512
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800513/* Expresses the convention when stereo audio samples are stored interleaved
514 * in an array. This should improve readability by allowing code to use
515 * symbolic indices instead of hard-coded [0] and [1].
Glenn Kastenee7c17b2014-04-29 08:43:19 -0700516 *
517 * For multi-channel beyond stereo, the platform convention is that channels
518 * are interleaved in order from least significant channel mask bit
519 * to most significant channel mask bit, with unused bits skipped.
520 * Any exceptions to this convention will be noted at the appropriate API.
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800521 */
522enum {
523 AUDIO_INTERLEAVE_LEFT = 0,
524 AUDIO_INTERLEAVE_RIGHT = 1,
525};
526
Dima Zavine8e4be52011-05-12 10:25:33 -0700527typedef enum {
528 AUDIO_MODE_INVALID = -2,
529 AUDIO_MODE_CURRENT = -1,
530 AUDIO_MODE_NORMAL = 0,
531 AUDIO_MODE_RINGTONE = 1,
532 AUDIO_MODE_IN_CALL = 2,
533 AUDIO_MODE_IN_COMMUNICATION = 3,
534
535 AUDIO_MODE_CNT,
536 AUDIO_MODE_MAX = AUDIO_MODE_CNT - 1,
537} audio_mode_t;
538
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800539/* This enum is deprecated */
Dima Zavine8e4be52011-05-12 10:25:33 -0700540typedef enum {
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800541 AUDIO_IN_ACOUSTICS_NONE = 0,
Dima Zavine8e4be52011-05-12 10:25:33 -0700542 AUDIO_IN_ACOUSTICS_AGC_ENABLE = 0x0001,
543 AUDIO_IN_ACOUSTICS_AGC_DISABLE = 0,
544 AUDIO_IN_ACOUSTICS_NS_ENABLE = 0x0002,
545 AUDIO_IN_ACOUSTICS_NS_DISABLE = 0,
546 AUDIO_IN_ACOUSTICS_TX_IIR_ENABLE = 0x0004,
547 AUDIO_IN_ACOUSTICS_TX_DISABLE = 0,
548} audio_in_acoustics_t;
549
Glenn Kasten16a7a042012-07-03 15:21:05 -0700550enum {
Eric Laurenteeeee802012-08-28 14:29:07 -0700551 AUDIO_DEVICE_NONE = 0x0,
552 /* reserved bits */
553 AUDIO_DEVICE_BIT_IN = 0x80000000,
554 AUDIO_DEVICE_BIT_DEFAULT = 0x40000000,
Dima Zavine8e4be52011-05-12 10:25:33 -0700555 /* output devices */
556 AUDIO_DEVICE_OUT_EARPIECE = 0x1,
557 AUDIO_DEVICE_OUT_SPEAKER = 0x2,
558 AUDIO_DEVICE_OUT_WIRED_HEADSET = 0x4,
559 AUDIO_DEVICE_OUT_WIRED_HEADPHONE = 0x8,
560 AUDIO_DEVICE_OUT_BLUETOOTH_SCO = 0x10,
561 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20,
562 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40,
563 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP = 0x80,
564 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100,
565 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200,
566 AUDIO_DEVICE_OUT_AUX_DIGITAL = 0x400,
Eric Laurentabcb9992014-05-16 15:11:08 -0700567 AUDIO_DEVICE_OUT_HDMI = AUDIO_DEVICE_OUT_AUX_DIGITAL,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800568 /* uses an analog connection (multiplexed over the USB connector pins for instance) */
Dima Zavine8e4be52011-05-12 10:25:33 -0700569 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800,
570 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800571 /* USB accessory mode: your Android device is a USB device and the dock is a USB host */
Eric Laurent79f90bd2012-04-06 08:57:48 -0700572 AUDIO_DEVICE_OUT_USB_ACCESSORY = 0x2000,
Glenn Kasten1c3e16f2011-12-19 09:21:48 -0800573 /* USB host mode: your Android device is a USB host and the dock is a USB device */
Eric Laurent79f90bd2012-04-06 08:57:48 -0700574 AUDIO_DEVICE_OUT_USB_DEVICE = 0x4000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700575 AUDIO_DEVICE_OUT_REMOTE_SUBMIX = 0x8000,
Eric Laurentabcb9992014-05-16 15:11:08 -0700576 /* Telephony voice TX path */
577 AUDIO_DEVICE_OUT_TELEPHONY_TX = 0x10000,
578 /* Analog jack with line impedance detected */
579 AUDIO_DEVICE_OUT_LINE = 0x20000,
580 /* HDMI Audio Return Channel */
581 AUDIO_DEVICE_OUT_HDMI_ARC = 0x40000,
582 /* S/PDIF out */
583 AUDIO_DEVICE_OUT_SPDIF = 0x80000,
584 /* FM transmitter out */
585 AUDIO_DEVICE_OUT_FM = 0x100000,
Jungshik Jangcadd5bb2014-07-15 19:40:32 +0900586 /* Line out for av devices */
587 AUDIO_DEVICE_OUT_AUX_LINE = 0x200000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700588 AUDIO_DEVICE_OUT_DEFAULT = AUDIO_DEVICE_BIT_DEFAULT,
Dima Zavine8e4be52011-05-12 10:25:33 -0700589 AUDIO_DEVICE_OUT_ALL = (AUDIO_DEVICE_OUT_EARPIECE |
590 AUDIO_DEVICE_OUT_SPEAKER |
591 AUDIO_DEVICE_OUT_WIRED_HEADSET |
592 AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
593 AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
594 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
595 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
596 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
597 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
598 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
Eric Laurentabcb9992014-05-16 15:11:08 -0700599 AUDIO_DEVICE_OUT_HDMI |
Dima Zavine8e4be52011-05-12 10:25:33 -0700600 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
601 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
Eric Laurent79f90bd2012-04-06 08:57:48 -0700602 AUDIO_DEVICE_OUT_USB_ACCESSORY |
603 AUDIO_DEVICE_OUT_USB_DEVICE |
Eric Laurenteeeee802012-08-28 14:29:07 -0700604 AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
Eric Laurentabcb9992014-05-16 15:11:08 -0700605 AUDIO_DEVICE_OUT_TELEPHONY_TX |
606 AUDIO_DEVICE_OUT_LINE |
607 AUDIO_DEVICE_OUT_HDMI_ARC |
608 AUDIO_DEVICE_OUT_SPDIF |
609 AUDIO_DEVICE_OUT_FM |
Jungshik Jangcadd5bb2014-07-15 19:40:32 +0900610 AUDIO_DEVICE_OUT_AUX_LINE |
Dima Zavine8e4be52011-05-12 10:25:33 -0700611 AUDIO_DEVICE_OUT_DEFAULT),
612 AUDIO_DEVICE_OUT_ALL_A2DP = (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
613 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
614 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
615 AUDIO_DEVICE_OUT_ALL_SCO = (AUDIO_DEVICE_OUT_BLUETOOTH_SCO |
616 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
617 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT),
Eric Laurent79f90bd2012-04-06 08:57:48 -0700618 AUDIO_DEVICE_OUT_ALL_USB = (AUDIO_DEVICE_OUT_USB_ACCESSORY |
619 AUDIO_DEVICE_OUT_USB_DEVICE),
Dima Zavine8e4be52011-05-12 10:25:33 -0700620
621 /* input devices */
Eric Laurenteeeee802012-08-28 14:29:07 -0700622 AUDIO_DEVICE_IN_COMMUNICATION = AUDIO_DEVICE_BIT_IN | 0x1,
623 AUDIO_DEVICE_IN_AMBIENT = AUDIO_DEVICE_BIT_IN | 0x2,
624 AUDIO_DEVICE_IN_BUILTIN_MIC = AUDIO_DEVICE_BIT_IN | 0x4,
625 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET = AUDIO_DEVICE_BIT_IN | 0x8,
626 AUDIO_DEVICE_IN_WIRED_HEADSET = AUDIO_DEVICE_BIT_IN | 0x10,
627 AUDIO_DEVICE_IN_AUX_DIGITAL = AUDIO_DEVICE_BIT_IN | 0x20,
Eric Laurentabcb9992014-05-16 15:11:08 -0700628 AUDIO_DEVICE_IN_HDMI = AUDIO_DEVICE_IN_AUX_DIGITAL,
629 /* Telephony voice RX path */
Eric Laurenteeeee802012-08-28 14:29:07 -0700630 AUDIO_DEVICE_IN_VOICE_CALL = AUDIO_DEVICE_BIT_IN | 0x40,
Eric Laurentabcb9992014-05-16 15:11:08 -0700631 AUDIO_DEVICE_IN_TELEPHONY_RX = AUDIO_DEVICE_IN_VOICE_CALL,
Eric Laurenteeeee802012-08-28 14:29:07 -0700632 AUDIO_DEVICE_IN_BACK_MIC = AUDIO_DEVICE_BIT_IN | 0x80,
633 AUDIO_DEVICE_IN_REMOTE_SUBMIX = AUDIO_DEVICE_BIT_IN | 0x100,
634 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x200,
635 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET = AUDIO_DEVICE_BIT_IN | 0x400,
636 AUDIO_DEVICE_IN_USB_ACCESSORY = AUDIO_DEVICE_BIT_IN | 0x800,
637 AUDIO_DEVICE_IN_USB_DEVICE = AUDIO_DEVICE_BIT_IN | 0x1000,
Eric Laurentabcb9992014-05-16 15:11:08 -0700638 /* FM tuner input */
639 AUDIO_DEVICE_IN_FM_TUNER = AUDIO_DEVICE_BIT_IN | 0x2000,
640 /* TV tuner input */
641 AUDIO_DEVICE_IN_TV_TUNER = AUDIO_DEVICE_BIT_IN | 0x4000,
642 /* Analog jack with line impedance detected */
643 AUDIO_DEVICE_IN_LINE = AUDIO_DEVICE_BIT_IN | 0x8000,
644 /* S/PDIF in */
645 AUDIO_DEVICE_IN_SPDIF = AUDIO_DEVICE_BIT_IN | 0x10000,
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700646 AUDIO_DEVICE_IN_BLUETOOTH_A2DP = AUDIO_DEVICE_BIT_IN | 0x20000,
Terry Heob63dd8a2014-06-27 15:25:56 +0900647 AUDIO_DEVICE_IN_LOOPBACK = AUDIO_DEVICE_BIT_IN | 0x40000,
Eric Laurenteeeee802012-08-28 14:29:07 -0700648 AUDIO_DEVICE_IN_DEFAULT = AUDIO_DEVICE_BIT_IN | AUDIO_DEVICE_BIT_DEFAULT,
Dima Zavine8e4be52011-05-12 10:25:33 -0700649
650 AUDIO_DEVICE_IN_ALL = (AUDIO_DEVICE_IN_COMMUNICATION |
651 AUDIO_DEVICE_IN_AMBIENT |
652 AUDIO_DEVICE_IN_BUILTIN_MIC |
653 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET |
654 AUDIO_DEVICE_IN_WIRED_HEADSET |
Eric Laurentabcb9992014-05-16 15:11:08 -0700655 AUDIO_DEVICE_IN_HDMI |
656 AUDIO_DEVICE_IN_TELEPHONY_RX |
Dima Zavine8e4be52011-05-12 10:25:33 -0700657 AUDIO_DEVICE_IN_BACK_MIC |
Eric Laurenteeeee802012-08-28 14:29:07 -0700658 AUDIO_DEVICE_IN_REMOTE_SUBMIX |
659 AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET |
660 AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET |
661 AUDIO_DEVICE_IN_USB_ACCESSORY |
662 AUDIO_DEVICE_IN_USB_DEVICE |
Eric Laurentabcb9992014-05-16 15:11:08 -0700663 AUDIO_DEVICE_IN_FM_TUNER |
664 AUDIO_DEVICE_IN_TV_TUNER |
665 AUDIO_DEVICE_IN_LINE |
666 AUDIO_DEVICE_IN_SPDIF |
Mike Lockwoodc8183af2014-05-12 12:56:03 -0700667 AUDIO_DEVICE_IN_BLUETOOTH_A2DP |
Terry Heob63dd8a2014-06-27 15:25:56 +0900668 AUDIO_DEVICE_IN_LOOPBACK |
Dima Zavine8e4be52011-05-12 10:25:33 -0700669 AUDIO_DEVICE_IN_DEFAULT),
670 AUDIO_DEVICE_IN_ALL_SCO = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET,
Paul McLean00b89722014-04-18 14:29:22 -0700671 AUDIO_DEVICE_IN_ALL_USB = (AUDIO_DEVICE_IN_USB_ACCESSORY |
672 AUDIO_DEVICE_IN_USB_DEVICE),
Glenn Kasten16a7a042012-07-03 15:21:05 -0700673};
674
675typedef uint32_t audio_devices_t;
Dima Zavine8e4be52011-05-12 10:25:33 -0700676
Eric Laurent545ab462012-04-16 18:05:39 -0700677/* the audio output flags serve two purposes:
678 * - when an AudioTrack is created they indicate a "wish" to be connected to an
679 * output stream with attributes corresponding to the specified flags
680 * - when present in an output profile descriptor listed for a particular audio
681 * hardware module, they indicate that an output stream can be opened that
682 * supports the attributes indicated by the flags.
683 * the audio policy manager will try to match the flags in the request
684 * (when getOuput() is called) to an available output stream.
685 */
686typedef enum {
Eric Laurent9aec3622012-04-13 16:52:58 -0700687 AUDIO_OUTPUT_FLAG_NONE = 0x0, // no attributes
688 AUDIO_OUTPUT_FLAG_DIRECT = 0x1, // this output directly connects a track
689 // to one output stream: no software mixer
690 AUDIO_OUTPUT_FLAG_PRIMARY = 0x2, // this output is the primary output of
691 // the device. It is unique and must be
692 // present. It is opened by default and
693 // receives routing, audio mode and volume
694 // controls related to voice calls.
695 AUDIO_OUTPUT_FLAG_FAST = 0x4, // output supports "fast tracks",
696 // defined elsewhere
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000697 AUDIO_OUTPUT_FLAG_DEEP_BUFFER = 0x8, // use deep audio buffers
698 AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD = 0x10, // offload playback of compressed
699 // streams to hardware codec
Eric Laurentd91e6df2014-08-01 14:33:10 -0700700 AUDIO_OUTPUT_FLAG_NON_BLOCKING = 0x20, // use non-blocking write
701 AUDIO_OUTPUT_FLAG_HW_AV_SYNC = 0x40 // output uses a hardware A/V synchronization source
Eric Laurent545ab462012-04-16 18:05:39 -0700702} audio_output_flags_t;
703
Glenn Kastenc051ffd2013-08-01 07:35:33 -0700704/* The audio input flags are analogous to audio output flags.
705 * Currently they are used only when an AudioRecord is created,
706 * to indicate a preference to be connected to an input stream with
707 * attributes corresponding to the specified flags.
708 */
709typedef enum {
710 AUDIO_INPUT_FLAG_NONE = 0x0, // no attributes
711 AUDIO_INPUT_FLAG_FAST = 0x1, // prefer an input that supports "fast tracks"
712} audio_input_flags_t;
713
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000714/* Additional information about compressed streams offloaded to
715 * hardware playback
716 * The version and size fields must be initialized by the caller by using
717 * one of the constants defined here.
718 */
719typedef struct {
720 uint16_t version; // version of the info structure
721 uint16_t size; // total size of the structure including version and size
722 uint32_t sample_rate; // sample rate in Hz
723 audio_channel_mask_t channel_mask; // channel mask
724 audio_format_t format; // audio format
725 audio_stream_type_t stream_type; // stream type
726 uint32_t bit_rate; // bit rate in bits per second
727 int64_t duration_us; // duration in microseconds, -1 if unknown
728 bool has_video; // true if stream is tied to a video stream
729 bool is_streaming; // true if streaming, false if local playback
730} audio_offload_info_t;
731
732#define AUDIO_MAKE_OFFLOAD_INFO_VERSION(maj,min) \
733 ((((maj) & 0xff) << 8) | ((min) & 0xff))
734
735#define AUDIO_OFFLOAD_INFO_VERSION_0_1 AUDIO_MAKE_OFFLOAD_INFO_VERSION(0, 1)
736#define AUDIO_OFFLOAD_INFO_VERSION_CURRENT AUDIO_OFFLOAD_INFO_VERSION_0_1
737
738static const audio_offload_info_t AUDIO_INFO_INITIALIZER = {
739 version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
740 size: sizeof(audio_offload_info_t),
Mark Salyzyn48878422014-05-22 16:08:52 -0700741 sample_rate: 0,
742 channel_mask: 0,
743 format: AUDIO_FORMAT_DEFAULT,
744 stream_type: AUDIO_STREAM_VOICE_CALL,
745 bit_rate: 0,
746 duration_us: 0,
747 has_video: false,
748 is_streaming: false
Richard Fitzgerald05529a12013-03-25 16:07:43 +0000749};
750
Eric Laurent0a381a32014-07-27 16:18:21 -0700751/* common audio stream configuration parameters
752 * You should memset() the entire structure to zero before use to
753 * ensure forward compatibility
754 */
755struct audio_config {
756 uint32_t sample_rate;
757 audio_channel_mask_t channel_mask;
758 audio_format_t format;
759 audio_offload_info_t offload_info;
760 size_t frame_count;
761};
762typedef struct audio_config audio_config_t;
763
764static const audio_config_t AUDIO_CONFIG_INITIALIZER = {
765 sample_rate: 0,
766 channel_mask: AUDIO_CHANNEL_NONE,
767 format: AUDIO_FORMAT_DEFAULT,
768 offload_info: {
769 version: AUDIO_OFFLOAD_INFO_VERSION_CURRENT,
770 size: sizeof(audio_offload_info_t),
771 sample_rate: 0,
772 channel_mask: 0,
773 format: AUDIO_FORMAT_DEFAULT,
774 stream_type: AUDIO_STREAM_VOICE_CALL,
775 bit_rate: 0,
776 duration_us: 0,
777 has_video: false,
778 is_streaming: false
779 },
780 frame_count: 0,
781};
782
Eric Laurent4cd05012014-02-18 12:40:36 -0800783
784/* audio hw module handle functions or structures referencing a module */
785typedef int audio_module_handle_t;
786
787/******************************
788 * Volume control
789 *****************************/
790
791/* If the audio hardware supports gain control on some audio paths,
792 * the platform can expose them in the audio_policy.conf file. The audio HAL
793 * will then implement gain control functions that will use the following data
794 * structures. */
795
796/* Type of gain control exposed by an audio port */
797#define AUDIO_GAIN_MODE_JOINT 0x1 /* supports joint channel gain control */
798#define AUDIO_GAIN_MODE_CHANNELS 0x2 /* supports separate channel gain control */
799#define AUDIO_GAIN_MODE_RAMP 0x4 /* supports gain ramps */
800
801typedef uint32_t audio_gain_mode_t;
802
803
804/* An audio_gain struct is a representation of a gain stage.
805 * A gain stage is always attached to an audio port. */
806struct audio_gain {
807 audio_gain_mode_t mode; /* e.g. AUDIO_GAIN_MODE_JOINT */
808 audio_channel_mask_t channel_mask; /* channels which gain an be controlled.
809 N/A if AUDIO_GAIN_MODE_CHANNELS is not supported */
810 int min_value; /* minimum gain value in millibels */
811 int max_value; /* maximum gain value in millibels */
812 int default_value; /* default gain value in millibels */
813 unsigned int step_value; /* gain step in millibels */
814 unsigned int min_ramp_ms; /* minimum ramp duration in ms */
815 unsigned int max_ramp_ms; /* maximum ramp duration in ms */
816};
817
818/* The gain configuration structure is used to get or set the gain values of a
819 * given port */
820struct audio_gain_config {
821 int index; /* index of the corresponding audio_gain in the
822 audio_port gains[] table */
823 audio_gain_mode_t mode; /* mode requested for this command */
824 audio_channel_mask_t channel_mask; /* channels which gain value follows.
825 N/A in joint mode */
Jungshik Jangf7e5aea2014-07-11 09:50:45 +0900826 int values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
827 for each channel ordered from LSb to MSb in
828 channel mask. The number of values is 1 in joint
829 mode or popcount(channel_mask) */
Eric Laurent4cd05012014-02-18 12:40:36 -0800830 unsigned int ramp_duration_ms; /* ramp duration in ms */
831};
832
833/******************************
834 * Routing control
835 *****************************/
836
837/* Types defined here are used to describe an audio source or sink at internal
838 * framework interfaces (audio policy, patch panel) or at the audio HAL.
839 * Sink and sources are grouped in a concept of “audio port” representing an
840 * audio end point at the edge of the system managed by the module exposing
841 * the interface. */
842
843/* Audio port role: either source or sink */
844typedef enum {
845 AUDIO_PORT_ROLE_NONE,
846 AUDIO_PORT_ROLE_SOURCE,
847 AUDIO_PORT_ROLE_SINK,
848} audio_port_role_t;
849
850/* Audio port type indicates if it is a session (e.g AudioTrack),
851 * a mix (e.g PlaybackThread output) or a physical device
852 * (e.g AUDIO_DEVICE_OUT_SPEAKER) */
853typedef enum {
854 AUDIO_PORT_TYPE_NONE,
855 AUDIO_PORT_TYPE_DEVICE,
856 AUDIO_PORT_TYPE_MIX,
857 AUDIO_PORT_TYPE_SESSION,
858} audio_port_type_t;
859
860/* Each port has a unique ID or handle allocated by policy manager */
861typedef int audio_port_handle_t;
862#define AUDIO_PORT_HANDLE_NONE 0
863
864
865/* maximum audio device address length */
866#define AUDIO_DEVICE_MAX_ADDRESS_LEN 32
867
868/* extension for audio port configuration structure when the audio port is a
869 * hardware device */
870struct audio_port_config_device_ext {
871 audio_module_handle_t hw_module; /* module the device is attached to */
872 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
873 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN]; /* device address. "" if N/A */
874};
875
876/* extension for audio port configuration structure when the audio port is a
877 * sub mix */
878struct audio_port_config_mix_ext {
879 audio_module_handle_t hw_module; /* module the stream is attached to */
880 audio_io_handle_t handle; /* I/O handle of the input/output stream */
881 union {
882 //TODO: change use case for output streams: use strategy and mixer attributes
883 audio_stream_type_t stream;
884 audio_source_t source;
885 } usecase;
886};
887
888/* extension for audio port configuration structure when the audio port is an
889 * audio session */
890struct audio_port_config_session_ext {
891 audio_session_t session; /* audio session */
892};
893
894/* Flags indicating which fields are to be considered in struct audio_port_config */
895#define AUDIO_PORT_CONFIG_SAMPLE_RATE 0x1
896#define AUDIO_PORT_CONFIG_CHANNEL_MASK 0x2
897#define AUDIO_PORT_CONFIG_FORMAT 0x4
898#define AUDIO_PORT_CONFIG_GAIN 0x8
899#define AUDIO_PORT_CONFIG_ALL (AUDIO_PORT_CONFIG_SAMPLE_RATE | \
900 AUDIO_PORT_CONFIG_CHANNEL_MASK | \
901 AUDIO_PORT_CONFIG_FORMAT | \
902 AUDIO_PORT_CONFIG_GAIN)
903
904/* audio port configuration structure used to specify a particular configuration of
905 * an audio port */
906struct audio_port_config {
907 audio_port_handle_t id; /* port unique ID */
908 audio_port_role_t role; /* sink or source */
909 audio_port_type_t type; /* device, mix ... */
910 unsigned int config_mask; /* e.g AUDIO_PORT_CONFIG_ALL */
911 unsigned int sample_rate; /* sampling rate in Hz */
912 audio_channel_mask_t channel_mask; /* channel mask if applicable */
913 audio_format_t format; /* format if applicable */
914 struct audio_gain_config gain; /* gain to apply if applicable */
915 union {
916 struct audio_port_config_device_ext device; /* device specific info */
917 struct audio_port_config_mix_ext mix; /* mix specific info */
918 struct audio_port_config_session_ext session; /* session specific info */
919 } ext;
920};
921
922
923/* max number of sampling rates in audio port */
924#define AUDIO_PORT_MAX_SAMPLING_RATES 16
925/* max number of channel masks in audio port */
926#define AUDIO_PORT_MAX_CHANNEL_MASKS 16
927/* max number of audio formats in audio port */
928#define AUDIO_PORT_MAX_FORMATS 16
929/* max number of gain controls in audio port */
930#define AUDIO_PORT_MAX_GAINS 16
931
932/* extension for audio port structure when the audio port is a hardware device */
933struct audio_port_device_ext {
934 audio_module_handle_t hw_module; /* module the device is attached to */
935 audio_devices_t type; /* device type (e.g AUDIO_DEVICE_OUT_SPEAKER) */
936 char address[AUDIO_DEVICE_MAX_ADDRESS_LEN];
937};
938
939/* Latency class of the audio mix */
940typedef enum {
941 AUDIO_LATENCY_LOW,
942 AUDIO_LATENCY_NORMAL,
943} audio_mix_latency_class_t;
944
945/* extension for audio port structure when the audio port is a sub mix */
946struct audio_port_mix_ext {
947 audio_module_handle_t hw_module; /* module the stream is attached to */
948 audio_io_handle_t handle; /* I/O handle of the input.output stream */
949 audio_mix_latency_class_t latency_class; /* latency class */
950 // other attributes: routing strategies
951};
952
953/* extension for audio port structure when the audio port is an audio session */
954struct audio_port_session_ext {
955 audio_session_t session; /* audio session */
956};
957
958
959struct audio_port {
960 audio_port_handle_t id; /* port unique ID */
961 audio_port_role_t role; /* sink or source */
962 audio_port_type_t type; /* device, mix ... */
963 unsigned int num_sample_rates; /* number of sampling rates in following array */
964 unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
965 unsigned int num_channel_masks; /* number of channel masks in following array */
966 audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
967 unsigned int num_formats; /* number of formats in following array */
968 audio_format_t formats[AUDIO_PORT_MAX_FORMATS];
969 unsigned int num_gains; /* number of gains in following array */
970 struct audio_gain gains[AUDIO_PORT_MAX_GAINS];
971 struct audio_port_config active_config; /* current audio port configuration */
972 union {
973 struct audio_port_device_ext device;
974 struct audio_port_mix_ext mix;
975 struct audio_port_session_ext session;
976 } ext;
977};
978
979/* An audio patch represents a connection between one or more source ports and
980 * one or more sink ports. Patches are connected and disconnected by audio policy manager or by
981 * applications via framework APIs.
982 * Each patch is identified by a handle at the interface used to create that patch. For instance,
983 * when a patch is created by the audio HAL, the HAL allocates and returns a handle.
984 * This handle is unique to a given audio HAL hardware module.
985 * But the same patch receives another system wide unique handle allocated by the framework.
986 * This unique handle is used for all transactions inside the framework.
987 */
988typedef int audio_patch_handle_t;
989#define AUDIO_PATCH_HANDLE_NONE 0
990
991#define AUDIO_PATCH_PORTS_MAX 16
992
993struct audio_patch {
994 audio_patch_handle_t id; /* patch unique ID */
995 unsigned int num_sources; /* number of sources in following array */
996 struct audio_port_config sources[AUDIO_PATCH_PORTS_MAX];
997 unsigned int num_sinks; /* number of sinks in following array */
998 struct audio_port_config sinks[AUDIO_PATCH_PORTS_MAX];
999};
1000
1001
Eric Laurentd91e6df2014-08-01 14:33:10 -07001002
1003/* a HW synchronization source returned by the audio HAL */
1004typedef uint32_t audio_hw_sync_t;
1005
1006/* an invalid HW synchronization source indicating an error */
1007#define AUDIO_HW_SYNC_INVALID 0
1008
Dima Zavine8e4be52011-05-12 10:25:33 -07001009static inline bool audio_is_output_device(audio_devices_t device)
1010{
Eric Laurenteeeee802012-08-28 14:29:07 -07001011 if (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
1012 (popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL) == 0))
Dima Zavine8e4be52011-05-12 10:25:33 -07001013 return true;
1014 else
1015 return false;
1016}
1017
1018static inline bool audio_is_input_device(audio_devices_t device)
1019{
Eric Laurenteeeee802012-08-28 14:29:07 -07001020 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1021 device &= ~AUDIO_DEVICE_BIT_IN;
1022 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_ALL) == 0))
1023 return true;
1024 }
1025 return false;
Dima Zavine8e4be52011-05-12 10:25:33 -07001026}
1027
Eric Laurenteeeee802012-08-28 14:29:07 -07001028static inline bool audio_is_output_devices(audio_devices_t device)
1029{
1030 return (device & AUDIO_DEVICE_BIT_IN) == 0;
1031}
1032
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001033static inline bool audio_is_a2dp_in_device(audio_devices_t device)
1034{
1035 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1036 device &= ~AUDIO_DEVICE_BIT_IN;
1037 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_IN_BLUETOOTH_A2DP))
1038 return true;
1039 }
1040 return false;
1041}
Eric Laurenteeeee802012-08-28 14:29:07 -07001042
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001043static inline bool audio_is_a2dp_out_device(audio_devices_t device)
Dima Zavine8e4be52011-05-12 10:25:33 -07001044{
1045 if ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_A2DP))
1046 return true;
1047 else
1048 return false;
1049}
1050
Mike Lockwoodc8183af2014-05-12 12:56:03 -07001051// Deprecated - use audio_is_a2dp_out_device() instead
1052static inline bool audio_is_a2dp_device(audio_devices_t device)
1053{
1054 return audio_is_a2dp_out_device(device);
1055}
1056
Dima Zavine8e4be52011-05-12 10:25:33 -07001057static inline bool audio_is_bluetooth_sco_device(audio_devices_t device)
1058{
Eric Laurentb5266302014-04-24 13:36:47 -07001059 if ((device & AUDIO_DEVICE_BIT_IN) == 0) {
1060 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_OUT_ALL_SCO) == 0))
1061 return true;
1062 } else {
1063 device &= ~AUDIO_DEVICE_BIT_IN;
1064 if ((popcount(device) == 1) && ((device & ~AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) == 0))
1065 return true;
1066 }
1067
1068 return false;
Dima Zavine8e4be52011-05-12 10:25:33 -07001069}
1070
Paul McLean00b89722014-04-18 14:29:22 -07001071static inline bool audio_is_usb_out_device(audio_devices_t device)
1072{
1073 return ((popcount(device) == 1) && (device & AUDIO_DEVICE_OUT_ALL_USB));
1074}
1075
1076static inline bool audio_is_usb_in_device(audio_devices_t device)
1077{
1078 if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
1079 device &= ~AUDIO_DEVICE_BIT_IN;
1080 if (popcount(device) == 1 && (device & AUDIO_DEVICE_IN_ALL_USB) != 0)
1081 return true;
1082 }
1083 return false;
1084}
1085
1086/* OBSOLETE - use audio_is_usb_out_device() instead. */
Eric Laurent79f90bd2012-04-06 08:57:48 -07001087static inline bool audio_is_usb_device(audio_devices_t device)
1088{
Paul McLean00b89722014-04-18 14:29:22 -07001089 return audio_is_usb_out_device(device);
Eric Laurent79f90bd2012-04-06 08:57:48 -07001090}
1091
Jean-Michel Trivie11866a2012-08-16 17:53:30 -07001092static inline bool audio_is_remote_submix_device(audio_devices_t device)
1093{
Jeff Brown8ecc7af2013-08-16 20:09:37 -07001094 if ((device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) == AUDIO_DEVICE_OUT_REMOTE_SUBMIX
1095 || (device & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX)
Jean-Michel Trivie11866a2012-08-16 17:53:30 -07001096 return true;
1097 else
1098 return false;
1099}
1100
Glenn Kasten432b7c62014-04-14 17:47:03 -07001101/* Returns true if:
1102 * representation is valid, and
1103 * there is at least one channel bit set which _could_ correspond to an input channel, and
1104 * there are no channel bits set which could _not_ correspond to an input channel.
1105 * Otherwise returns false.
1106 */
Glenn Kastenf7326dc2013-07-19 14:50:21 -07001107static inline bool audio_is_input_channel(audio_channel_mask_t channel)
Dima Zavine8e4be52011-05-12 10:25:33 -07001108{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001109 uint32_t bits = audio_channel_mask_get_bits(channel);
1110 switch (audio_channel_mask_get_representation(channel)) {
1111 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1112 if (bits & ~AUDIO_CHANNEL_IN_ALL) {
1113 bits = 0;
1114 }
1115 // fall through
1116 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1117 return bits != 0;
1118 default:
Dima Zavine8e4be52011-05-12 10:25:33 -07001119 return false;
Glenn Kasten432b7c62014-04-14 17:47:03 -07001120 }
Dima Zavine8e4be52011-05-12 10:25:33 -07001121}
1122
Glenn Kasten432b7c62014-04-14 17:47:03 -07001123/* Returns true if:
1124 * representation is valid, and
1125 * there is at least one channel bit set which _could_ correspond to an output channel, and
1126 * there are no channel bits set which could _not_ correspond to an output channel.
1127 * Otherwise returns false.
1128 */
Glenn Kastenf7326dc2013-07-19 14:50:21 -07001129static inline bool audio_is_output_channel(audio_channel_mask_t channel)
Dima Zavine8e4be52011-05-12 10:25:33 -07001130{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001131 uint32_t bits = audio_channel_mask_get_bits(channel);
1132 switch (audio_channel_mask_get_representation(channel)) {
1133 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1134 if (bits & ~AUDIO_CHANNEL_OUT_ALL) {
1135 bits = 0;
1136 }
1137 // fall through
1138 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1139 return bits != 0;
1140 default:
Dima Zavine8e4be52011-05-12 10:25:33 -07001141 return false;
Glenn Kasten432b7c62014-04-14 17:47:03 -07001142 }
Dima Zavine8e4be52011-05-12 10:25:33 -07001143}
1144
Andy Hunga7e8f862014-05-15 18:35:38 -07001145/* Returns the number of channels from an input channel mask,
1146 * used in the context of audio input or recording.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001147 * If a channel bit is set which could _not_ correspond to an input channel,
1148 * it is excluded from the count.
1149 * Returns zero if the representation is invalid.
Andy Hunga7e8f862014-05-15 18:35:38 -07001150 */
1151static inline uint32_t audio_channel_count_from_in_mask(audio_channel_mask_t channel)
1152{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001153 uint32_t bits = audio_channel_mask_get_bits(channel);
1154 switch (audio_channel_mask_get_representation(channel)) {
1155 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1156 // TODO: We can now merge with from_out_mask and remove anding
1157 bits &= AUDIO_CHANNEL_IN_ALL;
1158 // fall through
1159 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1160 return popcount(bits);
1161 default:
1162 return 0;
1163 }
Andy Hunga7e8f862014-05-15 18:35:38 -07001164}
1165
1166/* Returns the number of channels from an output channel mask,
1167 * used in the context of audio output or playback.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001168 * If a channel bit is set which could _not_ correspond to an output channel,
1169 * it is excluded from the count.
1170 * Returns zero if the representation is invalid.
Andy Hunga7e8f862014-05-15 18:35:38 -07001171 */
1172static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
1173{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001174 uint32_t bits = audio_channel_mask_get_bits(channel);
1175 switch (audio_channel_mask_get_representation(channel)) {
1176 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
1177 // TODO: We can now merge with from_in_mask and remove anding
1178 bits &= AUDIO_CHANNEL_OUT_ALL;
1179 // fall through
1180 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
1181 return popcount(bits);
1182 default:
1183 return 0;
1184 }
Andy Hunga7e8f862014-05-15 18:35:38 -07001185}
1186
Glenn Kasten432b7c62014-04-14 17:47:03 -07001187/* Derive an output channel mask for position assignment from a channel count.
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001188 * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
1189 * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
1190 * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
1191 * for continuity with stereo.
Glenn Kasten432b7c62014-04-14 17:47:03 -07001192 * Returns the matching channel mask,
1193 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1194 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1195 * configurations for which a default output channel mask is defined.
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001196 */
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001197static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001198{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001199 uint32_t bits;
Glenn Kasten1c3e16f2011-12-19 09:21:48 -08001200 switch (channel_count) {
Glenn Kasten432b7c62014-04-14 17:47:03 -07001201 case 0:
1202 return AUDIO_CHANNEL_NONE;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001203 case 1:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001204 bits = AUDIO_CHANNEL_OUT_MONO;
1205 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001206 case 2:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001207 bits = AUDIO_CHANNEL_OUT_STEREO;
1208 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001209 case 3:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001210 bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER;
1211 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001212 case 4: // 4.0
Glenn Kasten432b7c62014-04-14 17:47:03 -07001213 bits = AUDIO_CHANNEL_OUT_QUAD;
1214 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001215 case 5: // 5.0
Glenn Kasten432b7c62014-04-14 17:47:03 -07001216 bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
1217 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001218 case 6: // 5.1
Glenn Kasten432b7c62014-04-14 17:47:03 -07001219 bits = AUDIO_CHANNEL_OUT_5POINT1;
1220 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001221 case 7: // 6.1
Glenn Kasten432b7c62014-04-14 17:47:03 -07001222 bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
1223 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001224 case 8:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001225 bits = AUDIO_CHANNEL_OUT_7POINT1;
1226 break;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001227 default:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001228 return AUDIO_CHANNEL_INVALID;
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001229 }
Glenn Kasten432b7c62014-04-14 17:47:03 -07001230 return audio_channel_mask_from_representation_and_bits(
1231 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
Jean-Michel Trivi4ab051a2012-03-02 16:41:35 -08001232}
1233
Glenn Kasten432b7c62014-04-14 17:47:03 -07001234/* Derive an input channel mask for position assignment from a channel count.
1235 * Currently handles only mono and stereo.
1236 * Returns the matching channel mask,
1237 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1238 * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
1239 * configurations for which a default input channel mask is defined.
1240 */
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001241static inline audio_channel_mask_t audio_channel_in_mask_from_count(uint32_t channel_count)
1242{
Glenn Kasten432b7c62014-04-14 17:47:03 -07001243 uint32_t bits;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001244 switch (channel_count) {
Glenn Kasten432b7c62014-04-14 17:47:03 -07001245 case 0:
1246 return AUDIO_CHANNEL_NONE;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001247 case 1:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001248 bits = AUDIO_CHANNEL_IN_MONO;
1249 break;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001250 case 2:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001251 bits = AUDIO_CHANNEL_IN_STEREO;
1252 break;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001253 default:
Glenn Kasten432b7c62014-04-14 17:47:03 -07001254 return AUDIO_CHANNEL_INVALID;
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001255 }
Glenn Kasten432b7c62014-04-14 17:47:03 -07001256 return audio_channel_mask_from_representation_and_bits(
1257 AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
1258}
1259
1260/* Derive a channel mask for index assignment from a channel count.
1261 * Returns the matching channel mask,
1262 * or AUDIO_CHANNEL_NONE if the channel count is zero,
1263 * or AUDIO_CHANNEL_INVALID if the channel count exceeds AUDIO_CHANNEL_COUNT_MAX.
1264 */
1265static inline audio_channel_mask_t audio_channel_mask_for_index_assignment_from_count(
1266 uint32_t channel_count)
1267{
1268 if (channel_count == 0) {
1269 return AUDIO_CHANNEL_NONE;
1270 }
1271 if (channel_count > AUDIO_CHANNEL_COUNT_MAX) {
1272 return AUDIO_CHANNEL_INVALID;
1273 }
1274 uint32_t bits = (1 << channel_count) - 1;
1275 return audio_channel_mask_from_representation_and_bits(
1276 AUDIO_CHANNEL_REPRESENTATION_INDEX, bits);
Glenn Kasten89f7ba32012-03-14 13:45:31 -07001277}
1278
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001279static inline bool audio_is_valid_format(audio_format_t format)
Dima Zavine8e4be52011-05-12 10:25:33 -07001280{
1281 switch (format & AUDIO_FORMAT_MAIN_MASK) {
1282 case AUDIO_FORMAT_PCM:
Glenn Kasten45b95812014-03-07 12:31:03 -08001283 switch (format) {
1284 case AUDIO_FORMAT_PCM_16_BIT:
1285 case AUDIO_FORMAT_PCM_8_BIT:
1286 case AUDIO_FORMAT_PCM_32_BIT:
1287 case AUDIO_FORMAT_PCM_8_24_BIT:
1288 case AUDIO_FORMAT_PCM_FLOAT:
1289 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1290 return true;
1291 default:
Eric Laurentda382242011-06-16 21:13:43 -07001292 return false;
1293 }
Glenn Kasten45b95812014-03-07 12:31:03 -08001294 /* not reached */
Dima Zavine8e4be52011-05-12 10:25:33 -07001295 case AUDIO_FORMAT_MP3:
1296 case AUDIO_FORMAT_AMR_NB:
1297 case AUDIO_FORMAT_AMR_WB:
1298 case AUDIO_FORMAT_AAC:
1299 case AUDIO_FORMAT_HE_AAC_V1:
1300 case AUDIO_FORMAT_HE_AAC_V2:
1301 case AUDIO_FORMAT_VORBIS:
Eric Laurentce30de32014-06-10 15:39:16 -07001302 case AUDIO_FORMAT_OPUS:
1303 case AUDIO_FORMAT_AC3:
1304 case AUDIO_FORMAT_E_AC3:
Dima Zavine8e4be52011-05-12 10:25:33 -07001305 return true;
1306 default:
1307 return false;
1308 }
1309}
1310
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001311static inline bool audio_is_linear_pcm(audio_format_t format)
Dima Zavine8e4be52011-05-12 10:25:33 -07001312{
Eric Laurentda382242011-06-16 21:13:43 -07001313 return ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM);
Dima Zavine8e4be52011-05-12 10:25:33 -07001314}
1315
Glenn Kasten828bf0c2012-01-12 12:34:42 -08001316static inline size_t audio_bytes_per_sample(audio_format_t format)
Eric Laurentda382242011-06-16 21:13:43 -07001317{
1318 size_t size = 0;
1319
1320 switch (format) {
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001321 case AUDIO_FORMAT_PCM_32_BIT:
1322 case AUDIO_FORMAT_PCM_8_24_BIT:
1323 size = sizeof(int32_t);
1324 break;
Andy Hung173f4992014-03-06 17:18:58 -08001325 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
1326 size = sizeof(uint8_t) * 3;
1327 break;
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001328 case AUDIO_FORMAT_PCM_16_BIT:
1329 size = sizeof(int16_t);
1330 break;
1331 case AUDIO_FORMAT_PCM_8_BIT:
1332 size = sizeof(uint8_t);
1333 break;
Glenn Kasten1c3e16f2011-12-19 09:21:48 -08001334 case AUDIO_FORMAT_PCM_FLOAT:
1335 size = sizeof(float);
1336 break;
Jean-Michel Trivi427d2b42012-03-05 15:38:15 -08001337 default:
1338 break;
Eric Laurentda382242011-06-16 21:13:43 -07001339 }
1340 return size;
1341}
Dima Zavine8e4be52011-05-12 10:25:33 -07001342
Eric Laurenta768c302014-07-28 11:33:43 -07001343/* converts device address to string sent to audio HAL via set_parameters */
1344static char *audio_device_address_to_parameter(audio_devices_t device, const char *address)
1345{
1346 const size_t kSize = AUDIO_DEVICE_MAX_ADDRESS_LEN + sizeof("a2dp_sink_address=");
1347 char param[kSize];
1348
1349 if (device & AUDIO_DEVICE_OUT_ALL_A2DP)
1350 snprintf(param, kSize, "%s=%s", "a2dp_sink_address", address);
1351 else if (device & AUDIO_DEVICE_OUT_REMOTE_SUBMIX)
1352 snprintf(param, kSize, "%s=%s", "mix", address);
1353 else
1354 snprintf(param, kSize, "%s", address);
1355
1356 return strdup(param);
1357}
1358
1359
Dima Zavine8e4be52011-05-12 10:25:33 -07001360__END_DECLS
1361
1362#endif // ANDROID_AUDIO_CORE_H