1 /*
2 * Copyright 2016 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 #define LOG_TAG "AAudio"
18 //#define LOG_NDEBUG 0
19
20 #include <assert.h>
21 #include <math.h>
22 #include <stdint.h>
23
24 #include <aaudio/AAudioTesting.h>
25 #include <android/media/audio/common/AudioMMapPolicy.h>
26 #include <cutils/properties.h>
27 #include <sys/types.h>
28 #include <system/audio.h>
29 #include <utils/Errors.h>
30 #include <utils/Log.h>
31
32 #include "aaudio/AAudio.h"
33 #include "core/AudioGlobal.h"
34 #include "utility/AAudioUtilities.h"
35
36 using namespace android;
37
38 using android::media::audio::common::AudioMMapPolicy;
39 using android::media::audio::common::AudioMMapPolicyInfo;
40
AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result)41 status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result) {
42 // This covers the case for AAUDIO_OK and for positive results.
43 if (result >= 0) {
44 return result;
45 }
46 status_t status;
47 switch (result) {
48 case AAUDIO_ERROR_DISCONNECTED:
49 case AAUDIO_ERROR_NO_SERVICE:
50 status = DEAD_OBJECT;
51 break;
52 case AAUDIO_ERROR_INVALID_HANDLE:
53 status = BAD_TYPE;
54 break;
55 case AAUDIO_ERROR_INVALID_STATE:
56 status = INVALID_OPERATION;
57 break;
58 case AAUDIO_ERROR_INVALID_RATE:
59 case AAUDIO_ERROR_INVALID_FORMAT:
60 case AAUDIO_ERROR_ILLEGAL_ARGUMENT:
61 case AAUDIO_ERROR_OUT_OF_RANGE:
62 status = BAD_VALUE;
63 break;
64 case AAUDIO_ERROR_WOULD_BLOCK:
65 status = WOULD_BLOCK;
66 break;
67 case AAUDIO_ERROR_NULL:
68 status = UNEXPECTED_NULL;
69 break;
70 case AAUDIO_ERROR_UNAVAILABLE:
71 status = NOT_ENOUGH_DATA;
72 break;
73
74 // TODO translate these result codes
75 case AAUDIO_ERROR_INTERNAL:
76 case AAUDIO_ERROR_UNIMPLEMENTED:
77 case AAUDIO_ERROR_NO_FREE_HANDLES:
78 case AAUDIO_ERROR_NO_MEMORY:
79 case AAUDIO_ERROR_TIMEOUT:
80 default:
81 status = UNKNOWN_ERROR;
82 break;
83 }
84 return status;
85 }
86
AAudioConvert_androidToAAudioResult(status_t status)87 aaudio_result_t AAudioConvert_androidToAAudioResult(status_t status) {
88 // This covers the case for OK and for positive result.
89 if (status >= 0) {
90 return status;
91 }
92 aaudio_result_t result;
93 switch (status) {
94 case BAD_TYPE:
95 result = AAUDIO_ERROR_INVALID_HANDLE;
96 break;
97 case DEAD_OBJECT:
98 result = AAUDIO_ERROR_NO_SERVICE;
99 break;
100 case INVALID_OPERATION:
101 result = AAUDIO_ERROR_INVALID_STATE;
102 break;
103 case UNEXPECTED_NULL:
104 result = AAUDIO_ERROR_NULL;
105 break;
106 case BAD_VALUE:
107 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
108 break;
109 case WOULD_BLOCK:
110 result = AAUDIO_ERROR_WOULD_BLOCK;
111 break;
112 case NOT_ENOUGH_DATA:
113 result = AAUDIO_ERROR_UNAVAILABLE;
114 break;
115 default:
116 result = AAUDIO_ERROR_INTERNAL;
117 break;
118 }
119 return result;
120 }
121
AAudioConvert_aaudioToAndroidSessionId(aaudio_session_id_t sessionId)122 audio_session_t AAudioConvert_aaudioToAndroidSessionId(aaudio_session_id_t sessionId) {
123 // If not a regular sessionId then convert to a safe value of AUDIO_SESSION_ALLOCATE.
124 return (sessionId == AAUDIO_SESSION_ID_ALLOCATE || sessionId == AAUDIO_SESSION_ID_NONE)
125 ? AUDIO_SESSION_ALLOCATE
126 : (audio_session_t) sessionId;
127 }
128
AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudioFormat)129 audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudioFormat) {
130 audio_format_t androidFormat;
131 switch (aaudioFormat) {
132 case AAUDIO_FORMAT_UNSPECIFIED:
133 androidFormat = AUDIO_FORMAT_DEFAULT;
134 break;
135 case AAUDIO_FORMAT_PCM_I16:
136 androidFormat = AUDIO_FORMAT_PCM_16_BIT;
137 break;
138 case AAUDIO_FORMAT_PCM_FLOAT:
139 androidFormat = AUDIO_FORMAT_PCM_FLOAT;
140 break;
141 case AAUDIO_FORMAT_PCM_I24_PACKED:
142 androidFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED;
143 break;
144 case AAUDIO_FORMAT_PCM_I32:
145 androidFormat = AUDIO_FORMAT_PCM_32_BIT;
146 break;
147 case AAUDIO_FORMAT_IEC61937:
148 androidFormat = AUDIO_FORMAT_IEC61937;
149 break;
150 default:
151 androidFormat = AUDIO_FORMAT_INVALID;
152 ALOGE("%s() 0x%08X unrecognized", __func__, aaudioFormat);
153 break;
154 }
155 return androidFormat;
156 }
157
AAudioConvert_androidToAAudioDataFormat(audio_format_t androidFormat)158 aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t androidFormat) {
159 aaudio_format_t aaudioFormat;
160 switch (androidFormat) {
161 case AUDIO_FORMAT_DEFAULT:
162 aaudioFormat = AAUDIO_FORMAT_UNSPECIFIED;
163 break;
164 case AUDIO_FORMAT_PCM_16_BIT:
165 aaudioFormat = AAUDIO_FORMAT_PCM_I16;
166 break;
167 case AUDIO_FORMAT_PCM_FLOAT:
168 aaudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
169 break;
170 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
171 aaudioFormat = AAUDIO_FORMAT_PCM_I24_PACKED;
172 break;
173 case AUDIO_FORMAT_PCM_32_BIT:
174 aaudioFormat = AAUDIO_FORMAT_PCM_I32;
175 break;
176 case AUDIO_FORMAT_IEC61937:
177 aaudioFormat = AAUDIO_FORMAT_IEC61937;
178 break;
179 default:
180 aaudioFormat = AAUDIO_FORMAT_INVALID;
181 ALOGE("%s() 0x%08X unrecognized", __func__, androidFormat);
182 break;
183 }
184 return aaudioFormat;
185 }
186
AAudioConvert_androidToNearestAAudioDataFormat(audio_format_t androidFormat)187 aaudio_format_t AAudioConvert_androidToNearestAAudioDataFormat(audio_format_t androidFormat) {
188 // Special case AUDIO_FORMAT_PCM_8_24_BIT because this function should be used to find the
189 // resolution of the data format. Setting AUDIO_FORMAT_PCM_8_24_BIT directly is not available
190 // from AAudio but hardware may use AUDIO_FORMAT_PCM_8_24_BIT under the hood.
191 if (androidFormat == AUDIO_FORMAT_PCM_8_24_BIT) {
192 ALOGD("%s() converting 8.24 to 24 bit packed", __func__);
193 return AAUDIO_FORMAT_PCM_I24_PACKED;
194 }
195 return AAudioConvert_androidToAAudioDataFormat(androidFormat);
196 }
197
198 // Make a message string from the condition.
199 #define STATIC_ASSERT(condition) static_assert(condition, #condition)
200
AAudioConvert_usageToInternal(aaudio_usage_t usage)201 audio_usage_t AAudioConvert_usageToInternal(aaudio_usage_t usage) {
202 // The public aaudio_content_type_t constants are supposed to have the same
203 // values as the internal audio_content_type_t values.
204 STATIC_ASSERT(AAUDIO_USAGE_MEDIA == AUDIO_USAGE_MEDIA);
205 STATIC_ASSERT(AAUDIO_USAGE_VOICE_COMMUNICATION == AUDIO_USAGE_VOICE_COMMUNICATION);
206 STATIC_ASSERT(AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING
207 == AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING);
208 STATIC_ASSERT(AAUDIO_USAGE_ALARM == AUDIO_USAGE_ALARM);
209 STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION == AUDIO_USAGE_NOTIFICATION);
210 STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION_RINGTONE
211 == AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE);
212 STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION_EVENT == AUDIO_USAGE_NOTIFICATION_EVENT);
213 STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY);
214 STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE
215 == AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE);
216 STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_SONIFICATION == AUDIO_USAGE_ASSISTANCE_SONIFICATION);
217 STATIC_ASSERT(AAUDIO_USAGE_GAME == AUDIO_USAGE_GAME);
218 STATIC_ASSERT(AAUDIO_USAGE_ASSISTANT == AUDIO_USAGE_ASSISTANT);
219 STATIC_ASSERT(AAUDIO_SYSTEM_USAGE_EMERGENCY == AUDIO_USAGE_EMERGENCY);
220 STATIC_ASSERT(AAUDIO_SYSTEM_USAGE_SAFETY == AUDIO_USAGE_SAFETY);
221 STATIC_ASSERT(AAUDIO_SYSTEM_USAGE_VEHICLE_STATUS == AUDIO_USAGE_VEHICLE_STATUS);
222 STATIC_ASSERT(AAUDIO_SYSTEM_USAGE_ANNOUNCEMENT == AUDIO_USAGE_ANNOUNCEMENT);
223 if (usage == AAUDIO_UNSPECIFIED) {
224 usage = AAUDIO_USAGE_MEDIA;
225 }
226 return (audio_usage_t) usage; // same value
227 }
228
AAudioConvert_contentTypeToInternal(aaudio_content_type_t contentType)229 audio_content_type_t AAudioConvert_contentTypeToInternal(aaudio_content_type_t contentType) {
230 // The public aaudio_content_type_t constants are supposed to have the same
231 // values as the internal audio_content_type_t values.
232 STATIC_ASSERT(AAUDIO_CONTENT_TYPE_MUSIC == AUDIO_CONTENT_TYPE_MUSIC);
233 STATIC_ASSERT(AAUDIO_CONTENT_TYPE_SPEECH == AUDIO_CONTENT_TYPE_SPEECH);
234 STATIC_ASSERT(AAUDIO_CONTENT_TYPE_SONIFICATION == AUDIO_CONTENT_TYPE_SONIFICATION);
235 STATIC_ASSERT(AAUDIO_CONTENT_TYPE_MOVIE == AUDIO_CONTENT_TYPE_MOVIE);
236 if (contentType == AAUDIO_UNSPECIFIED) {
237 contentType = AAUDIO_CONTENT_TYPE_MUSIC;
238 }
239 return (audio_content_type_t) contentType; // same value
240 }
241
AAudioConvert_inputPresetToAudioSource(aaudio_input_preset_t preset)242 audio_source_t AAudioConvert_inputPresetToAudioSource(aaudio_input_preset_t preset) {
243 // The public aaudio_input_preset_t constants are supposed to have the same
244 // values as the internal audio_source_t values.
245 STATIC_ASSERT(AAUDIO_UNSPECIFIED == AUDIO_SOURCE_DEFAULT);
246 STATIC_ASSERT(AAUDIO_INPUT_PRESET_GENERIC == AUDIO_SOURCE_MIC);
247 STATIC_ASSERT(AAUDIO_INPUT_PRESET_CAMCORDER == AUDIO_SOURCE_CAMCORDER);
248 STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_RECOGNITION == AUDIO_SOURCE_VOICE_RECOGNITION);
249 STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION == AUDIO_SOURCE_VOICE_COMMUNICATION);
250 STATIC_ASSERT(AAUDIO_INPUT_PRESET_UNPROCESSED == AUDIO_SOURCE_UNPROCESSED);
251 STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE == AUDIO_SOURCE_VOICE_PERFORMANCE);
252 if (preset == AAUDIO_UNSPECIFIED) {
253 preset = AAUDIO_INPUT_PRESET_VOICE_RECOGNITION;
254 }
255 return (audio_source_t) preset; // same value
256 }
257
AAudio_computeAudioFlagsMask(aaudio_allowed_capture_policy_t policy,aaudio_spatialization_behavior_t spatializationBehavior,bool isContentSpatialized,audio_output_flags_t outputFlags)258 audio_flags_mask_t AAudio_computeAudioFlagsMask(
259 aaudio_allowed_capture_policy_t policy,
260 aaudio_spatialization_behavior_t spatializationBehavior,
261 bool isContentSpatialized,
262 audio_output_flags_t outputFlags) {
263 audio_flags_mask_t flagsMask = AUDIO_FLAG_NONE;
264 switch (policy) {
265 case AAUDIO_UNSPECIFIED:
266 case AAUDIO_ALLOW_CAPTURE_BY_ALL:
267 // flagsMask is not modified
268 break;
269 case AAUDIO_ALLOW_CAPTURE_BY_SYSTEM:
270 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_NO_MEDIA_PROJECTION);
271 break;
272 case AAUDIO_ALLOW_CAPTURE_BY_NONE:
273 flagsMask = static_cast<audio_flags_mask_t>(flagsMask |
274 AUDIO_FLAG_NO_MEDIA_PROJECTION | AUDIO_FLAG_NO_SYSTEM_CAPTURE);
275 break;
276 default:
277 ALOGE("%s() 0x%08X unrecognized capture policy", __func__, policy);
278 // flagsMask is not modified
279 }
280
281 switch (spatializationBehavior) {
282 case AAUDIO_UNSPECIFIED:
283 case AAUDIO_SPATIALIZATION_BEHAVIOR_AUTO:
284 // flagsMask is not modified
285 break;
286 case AAUDIO_SPATIALIZATION_BEHAVIOR_NEVER:
287 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_NEVER_SPATIALIZE);
288 break;
289 default:
290 ALOGE("%s() 0x%08X unrecognized spatialization behavior",
291 __func__, spatializationBehavior);
292 // flagsMask is not modified
293 }
294
295 if (isContentSpatialized) {
296 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_CONTENT_SPATIALIZED);
297 }
298
299 if ((outputFlags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
300 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_HW_AV_SYNC);
301 }
302 if ((outputFlags & AUDIO_OUTPUT_FLAG_FAST) != 0) {
303 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_LOW_LATENCY);
304 } else if ((outputFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
305 flagsMask = static_cast<audio_flags_mask_t>(flagsMask | AUDIO_FLAG_DEEP_BUFFER);
306 }
307
308 return flagsMask;
309 }
310
AAudioConvert_privacySensitiveToAudioFlagsMask(bool privacySensitive)311 audio_flags_mask_t AAudioConvert_privacySensitiveToAudioFlagsMask(
312 bool privacySensitive) {
313 return privacySensitive ? AUDIO_FLAG_CAPTURE_PRIVATE : AUDIO_FLAG_NONE;
314 }
315
AAudioConvert_aaudioToAndroidChannelLayoutMask(aaudio_channel_mask_t channelMask,bool isInput)316 audio_channel_mask_t AAudioConvert_aaudioToAndroidChannelLayoutMask(
317 aaudio_channel_mask_t channelMask, bool isInput) {
318 if (isInput) {
319 switch (channelMask) {
320 case AAUDIO_CHANNEL_MONO:
321 return AUDIO_CHANNEL_IN_MONO;
322 case AAUDIO_CHANNEL_STEREO:
323 return AUDIO_CHANNEL_IN_STEREO;
324 case AAUDIO_CHANNEL_FRONT_BACK:
325 return AUDIO_CHANNEL_IN_FRONT_BACK;
326 case AAUDIO_CHANNEL_2POINT0POINT2:
327 return AUDIO_CHANNEL_IN_2POINT0POINT2;
328 case AAUDIO_CHANNEL_2POINT1POINT2:
329 return AUDIO_CHANNEL_IN_2POINT1POINT2;
330 case AAUDIO_CHANNEL_3POINT0POINT2:
331 return AUDIO_CHANNEL_IN_3POINT0POINT2;
332 case AAUDIO_CHANNEL_3POINT1POINT2:
333 return AUDIO_CHANNEL_IN_3POINT1POINT2;
334 case AAUDIO_CHANNEL_5POINT1:
335 return AUDIO_CHANNEL_IN_5POINT1;
336 default:
337 ALOGE("%s() %#x unrecognized", __func__, channelMask);
338 return AUDIO_CHANNEL_INVALID;
339 }
340 } else {
341 switch (channelMask) {
342 case AAUDIO_CHANNEL_MONO:
343 return AUDIO_CHANNEL_OUT_MONO;
344 case AAUDIO_CHANNEL_STEREO:
345 return AUDIO_CHANNEL_OUT_STEREO;
346 case AAUDIO_CHANNEL_2POINT1:
347 return AUDIO_CHANNEL_OUT_2POINT1;
348 case AAUDIO_CHANNEL_TRI:
349 return AUDIO_CHANNEL_OUT_TRI;
350 case AAUDIO_CHANNEL_TRI_BACK:
351 return AUDIO_CHANNEL_OUT_TRI_BACK;
352 case AAUDIO_CHANNEL_3POINT1:
353 return AUDIO_CHANNEL_OUT_3POINT1;
354 case AAUDIO_CHANNEL_2POINT0POINT2:
355 return AUDIO_CHANNEL_OUT_2POINT0POINT2;
356 case AAUDIO_CHANNEL_2POINT1POINT2:
357 return AUDIO_CHANNEL_OUT_2POINT1POINT2;
358 case AAUDIO_CHANNEL_3POINT0POINT2:
359 return AUDIO_CHANNEL_OUT_3POINT0POINT2;
360 case AAUDIO_CHANNEL_3POINT1POINT2:
361 return AUDIO_CHANNEL_OUT_3POINT1POINT2;
362 case AAUDIO_CHANNEL_QUAD:
363 return AUDIO_CHANNEL_OUT_QUAD;
364 case AAUDIO_CHANNEL_QUAD_SIDE:
365 return AUDIO_CHANNEL_OUT_QUAD_SIDE;
366 case AAUDIO_CHANNEL_SURROUND:
367 return AUDIO_CHANNEL_OUT_SURROUND;
368 case AAUDIO_CHANNEL_PENTA:
369 return AUDIO_CHANNEL_OUT_PENTA;
370 case AAUDIO_CHANNEL_5POINT1:
371 return AUDIO_CHANNEL_OUT_5POINT1;
372 case AAUDIO_CHANNEL_5POINT1_SIDE:
373 return AUDIO_CHANNEL_OUT_5POINT1_SIDE;
374 case AAUDIO_CHANNEL_5POINT1POINT2:
375 return AUDIO_CHANNEL_OUT_5POINT1POINT2;
376 case AAUDIO_CHANNEL_5POINT1POINT4:
377 return AUDIO_CHANNEL_OUT_5POINT1POINT4;
378 case AAUDIO_CHANNEL_6POINT1:
379 return AUDIO_CHANNEL_OUT_6POINT1;
380 case AAUDIO_CHANNEL_7POINT1:
381 return AUDIO_CHANNEL_OUT_7POINT1;
382 case AAUDIO_CHANNEL_7POINT1POINT2:
383 return AUDIO_CHANNEL_OUT_7POINT1POINT2;
384 case AAUDIO_CHANNEL_7POINT1POINT4:
385 return AUDIO_CHANNEL_OUT_7POINT1POINT4;
386 case AAUDIO_CHANNEL_9POINT1POINT4:
387 return AUDIO_CHANNEL_OUT_9POINT1POINT4;
388 case AAUDIO_CHANNEL_9POINT1POINT6:
389 return AUDIO_CHANNEL_OUT_9POINT1POINT6;
390 default:
391 ALOGE("%s() %#x unrecognized", __func__, channelMask);
392 return AUDIO_CHANNEL_INVALID;
393 }
394 }
395 }
396
AAudioConvert_androidToAAudioChannelLayoutMask(audio_channel_mask_t channelMask,bool isInput)397 aaudio_channel_mask_t AAudioConvert_androidToAAudioChannelLayoutMask(
398 audio_channel_mask_t channelMask, bool isInput) {
399 if (isInput) {
400 switch (channelMask) {
401 case AUDIO_CHANNEL_IN_MONO:
402 return AAUDIO_CHANNEL_MONO;
403 case AUDIO_CHANNEL_IN_STEREO:
404 return AAUDIO_CHANNEL_STEREO;
405 case AUDIO_CHANNEL_IN_FRONT_BACK:
406 return AAUDIO_CHANNEL_FRONT_BACK;
407 case AUDIO_CHANNEL_IN_2POINT0POINT2:
408 return AAUDIO_CHANNEL_2POINT0POINT2;
409 case AUDIO_CHANNEL_IN_2POINT1POINT2:
410 return AAUDIO_CHANNEL_2POINT1POINT2;
411 case AUDIO_CHANNEL_IN_3POINT0POINT2:
412 return AAUDIO_CHANNEL_3POINT0POINT2;
413 case AUDIO_CHANNEL_IN_3POINT1POINT2:
414 return AAUDIO_CHANNEL_3POINT1POINT2;
415 case AUDIO_CHANNEL_IN_5POINT1:
416 return AAUDIO_CHANNEL_5POINT1;
417 default:
418 ALOGE("%s() %#x unrecognized", __func__, channelMask);
419 return AAUDIO_CHANNEL_INVALID;
420 }
421 } else {
422 switch (channelMask) {
423 case AUDIO_CHANNEL_OUT_MONO:
424 return AAUDIO_CHANNEL_MONO;
425 case AUDIO_CHANNEL_OUT_STEREO:
426 return AAUDIO_CHANNEL_STEREO;
427 case AUDIO_CHANNEL_OUT_2POINT1:
428 return AAUDIO_CHANNEL_2POINT1;
429 case AUDIO_CHANNEL_OUT_TRI:
430 return AAUDIO_CHANNEL_TRI;
431 case AUDIO_CHANNEL_OUT_TRI_BACK:
432 return AAUDIO_CHANNEL_TRI_BACK;
433 case AUDIO_CHANNEL_OUT_3POINT1:
434 return AAUDIO_CHANNEL_3POINT1;
435 case AUDIO_CHANNEL_OUT_2POINT0POINT2:
436 return AAUDIO_CHANNEL_2POINT0POINT2;
437 case AUDIO_CHANNEL_OUT_2POINT1POINT2:
438 return AAUDIO_CHANNEL_2POINT1POINT2;
439 case AUDIO_CHANNEL_OUT_3POINT0POINT2:
440 return AAUDIO_CHANNEL_3POINT0POINT2;
441 case AUDIO_CHANNEL_OUT_3POINT1POINT2:
442 return AAUDIO_CHANNEL_3POINT1POINT2;
443 case AUDIO_CHANNEL_OUT_QUAD:
444 return AAUDIO_CHANNEL_QUAD;
445 case AUDIO_CHANNEL_OUT_QUAD_SIDE:
446 return AAUDIO_CHANNEL_QUAD_SIDE;
447 case AUDIO_CHANNEL_OUT_SURROUND:
448 return AAUDIO_CHANNEL_SURROUND;
449 case AUDIO_CHANNEL_OUT_PENTA:
450 return AAUDIO_CHANNEL_PENTA;
451 case AUDIO_CHANNEL_OUT_5POINT1:
452 return AAUDIO_CHANNEL_5POINT1;
453 case AUDIO_CHANNEL_OUT_5POINT1_SIDE:
454 return AAUDIO_CHANNEL_5POINT1_SIDE;
455 case AUDIO_CHANNEL_OUT_5POINT1POINT2:
456 return AAUDIO_CHANNEL_5POINT1POINT2;
457 case AUDIO_CHANNEL_OUT_5POINT1POINT4:
458 return AAUDIO_CHANNEL_5POINT1POINT4;
459 case AUDIO_CHANNEL_OUT_6POINT1:
460 return AAUDIO_CHANNEL_6POINT1;
461 case AUDIO_CHANNEL_OUT_7POINT1:
462 return AAUDIO_CHANNEL_7POINT1;
463 case AUDIO_CHANNEL_OUT_7POINT1POINT2:
464 return AAUDIO_CHANNEL_7POINT1POINT2;
465 case AUDIO_CHANNEL_OUT_7POINT1POINT4:
466 return AAUDIO_CHANNEL_7POINT1POINT4;
467 case AUDIO_CHANNEL_OUT_9POINT1POINT4:
468 return AAUDIO_CHANNEL_9POINT1POINT4;
469 case AUDIO_CHANNEL_OUT_9POINT1POINT6:
470 return AAUDIO_CHANNEL_9POINT1POINT6;
471 default:
472 ALOGE("%s() %#x unrecognized", __func__, channelMask);
473 return AAUDIO_CHANNEL_INVALID;
474 }
475 }
476 }
477
AAudioConvert_channelMaskToCount(aaudio_channel_mask_t channelMask)478 int32_t AAudioConvert_channelMaskToCount(aaudio_channel_mask_t channelMask) {
479 return __builtin_popcount(channelMask & ~AAUDIO_CHANNEL_BIT_INDEX);
480 }
481
AAudioConvert_channelCountToMask(int32_t channelCount)482 aaudio_channel_mask_t AAudioConvert_channelCountToMask(int32_t channelCount) {
483 if (channelCount < 0 || channelCount > AUDIO_CHANNEL_COUNT_MAX) {
484 return AAUDIO_CHANNEL_INVALID;
485 }
486
487 if (channelCount == 0) {
488 return AAUDIO_UNSPECIFIED;
489 }
490
491 // Return index mask if the channel count is greater than 2.
492 return AAUDIO_CHANNEL_BIT_INDEX | ((1 << channelCount) - 1);
493 }
494
AAudioConvert_androidToAAudioChannelIndexMask(audio_channel_mask_t channelMask)495 aaudio_channel_mask_t AAudioConvert_androidToAAudioChannelIndexMask(
496 audio_channel_mask_t channelMask) {
497 if (audio_channel_mask_get_representation(channelMask) != AUDIO_CHANNEL_REPRESENTATION_INDEX) {
498 ALOGE("%s() %#x not an index mask", __func__, channelMask);
499 return AAUDIO_CHANNEL_INVALID;
500 }
501 return (channelMask & ~AUDIO_CHANNEL_INDEX_HDR) | AAUDIO_CHANNEL_BIT_INDEX;
502 }
503
AAudioConvert_aaudioToAndroidChannelIndexMask(aaudio_channel_mask_t channelMask)504 audio_channel_mask_t AAudioConvert_aaudioToAndroidChannelIndexMask(
505 aaudio_channel_mask_t channelMask) {
506 if (!AAudio_isChannelIndexMask(channelMask)) {
507 ALOGE("%s() %#x not an index mask", __func__, channelMask);
508 return AUDIO_CHANNEL_INVALID;
509 }
510 return audio_channel_mask_for_index_assignment_from_count(
511 AAudioConvert_channelMaskToCount(channelMask));
512 }
513
AAudioConvert_androidToAAudioChannelMask(audio_channel_mask_t channelMask,bool isInput,bool indexMaskRequired)514 aaudio_channel_mask_t AAudioConvert_androidToAAudioChannelMask(
515 audio_channel_mask_t channelMask, bool isInput, bool indexMaskRequired) {
516 if (audio_channel_mask_get_representation(channelMask) == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
517 return AAudioConvert_androidToAAudioChannelIndexMask(channelMask);
518 }
519 if (indexMaskRequired) {
520 // Require index mask, `channelMask` here is a position mask.
521 const int channelCount = isInput ? audio_channel_count_from_in_mask(channelMask)
522 : audio_channel_count_from_out_mask(channelMask);
523 return AAudioConvert_channelCountToMask(channelCount);
524 }
525 return AAudioConvert_androidToAAudioChannelLayoutMask(channelMask, isInput);
526 }
527
AAudioConvert_aaudioToAndroidChannelMask(aaudio_channel_mask_t channelMask,bool isInput)528 audio_channel_mask_t AAudioConvert_aaudioToAndroidChannelMask(
529 aaudio_channel_mask_t channelMask, bool isInput) {
530 return AAudio_isChannelIndexMask(channelMask)
531 ? AAudioConvert_aaudioToAndroidChannelIndexMask(channelMask)
532 : AAudioConvert_aaudioToAndroidChannelLayoutMask(channelMask, isInput);
533 }
534
AAudio_isChannelIndexMask(aaudio_channel_mask_t channelMask)535 bool AAudio_isChannelIndexMask(aaudio_channel_mask_t channelMask) {
536 return (channelMask & AAUDIO_CHANNEL_BIT_INDEX) == AAUDIO_CHANNEL_BIT_INDEX;
537 }
538
AAudio_getChannelMaskForOpen(aaudio_channel_mask_t channelMask,int32_t samplesPerFrame,bool isInput)539 audio_channel_mask_t AAudio_getChannelMaskForOpen(
540 aaudio_channel_mask_t channelMask, int32_t samplesPerFrame, bool isInput) {
541 if (channelMask != AAUDIO_UNSPECIFIED) {
542 if (AAudio_isChannelIndexMask(channelMask) && samplesPerFrame <= 2) {
543 // When it is index mask and the count is less than 3, use position mask
544 // instead of index mask for opening a stream. This may need to be revisited
545 // when making channel index mask public.
546 return isInput ? audio_channel_in_mask_from_count(samplesPerFrame)
547 : audio_channel_out_mask_from_count(samplesPerFrame);
548 }
549 return AAudioConvert_aaudioToAndroidChannelMask(channelMask, isInput);
550 }
551
552 // Return stereo when unspecified.
553 return isInput ? AUDIO_CHANNEL_IN_STEREO : AUDIO_CHANNEL_OUT_STEREO;
554 }
555
AAudioConvert_framesToBytes(int32_t numFrames,int32_t bytesPerFrame,int32_t * sizeInBytes)556 int32_t AAudioConvert_framesToBytes(int32_t numFrames,
557 int32_t bytesPerFrame,
558 int32_t *sizeInBytes) {
559 *sizeInBytes = 0;
560
561 if (numFrames < 0 || bytesPerFrame < 0) {
562 ALOGE("negative size, numFrames = %d, frameSize = %d", numFrames, bytesPerFrame);
563 return AAUDIO_ERROR_OUT_OF_RANGE;
564 }
565
566 // Prevent numeric overflow.
567 if (numFrames > (INT32_MAX / bytesPerFrame)) {
568 ALOGE("size overflow, numFrames = %d, frameSize = %d", numFrames, bytesPerFrame);
569 return AAUDIO_ERROR_OUT_OF_RANGE;
570 }
571
572 *sizeInBytes = numFrames * bytesPerFrame;
573 return AAUDIO_OK;
574 }
575
AAudioProperty_getWakeupDelayMicros()576 int32_t AAudioProperty_getWakeupDelayMicros() {
577 const int32_t minMicros = 0; // arbitrary
578 const int32_t defaultMicros = 200; // arbitrary, based on some observed jitter
579 const int32_t maxMicros = 5000; // arbitrary, probably don't want more than 500
580 int32_t prop = property_get_int32(AAUDIO_PROP_WAKEUP_DELAY_USEC, defaultMicros);
581 if (prop < minMicros) {
582 ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, minMicros);
583 prop = minMicros;
584 } else if (prop > maxMicros) {
585 ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, maxMicros);
586 prop = maxMicros;
587 }
588 return prop;
589 }
590
AAudioProperty_getMinimumSleepMicros()591 int32_t AAudioProperty_getMinimumSleepMicros() {
592 const int32_t minMicros = 1; // arbitrary
593 // Higher values can increase latency for moderate workloads.
594 // Short values can cause the CPU to short cycle if there is a bug in
595 // calculating the wakeup times.
596 const int32_t defaultMicros = 100; // arbitrary
597 const int32_t maxMicros = 200; // arbitrary
598 int32_t prop = property_get_int32(AAUDIO_PROP_MINIMUM_SLEEP_USEC, defaultMicros);
599 if (prop < minMicros) {
600 ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, minMicros);
601 prop = minMicros;
602 } else if (prop > maxMicros) {
603 ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, maxMicros);
604 prop = maxMicros;
605 }
606 return prop;
607 }
608
AAudioProperty_getMMapOffsetMicros(const char * functionName,const char * propertyName)609 static int32_t AAudioProperty_getMMapOffsetMicros(const char *functionName,
610 const char *propertyName) {
611 const int32_t minMicros = -20000; // arbitrary
612 const int32_t defaultMicros = 0; // arbitrary
613 const int32_t maxMicros = 20000; // arbitrary
614 int32_t prop = property_get_int32(propertyName, defaultMicros);
615 if (prop < minMicros) {
616 ALOGW("%s: clipped %d to %d", functionName, prop, minMicros);
617 prop = minMicros;
618 } else if (prop > maxMicros) {
619 ALOGW("%s: clipped %d to %d", functionName, prop, minMicros);
620 prop = maxMicros;
621 }
622 return prop;
623 }
624
AAudioProperty_getInputMMapOffsetMicros()625 int32_t AAudioProperty_getInputMMapOffsetMicros() {
626 return AAudioProperty_getMMapOffsetMicros(__func__, AAUDIO_PROP_INPUT_MMAP_OFFSET_USEC);
627 }
628
AAudioProperty_getOutputMMapOffsetMicros()629 int32_t AAudioProperty_getOutputMMapOffsetMicros() {
630 return AAudioProperty_getMMapOffsetMicros(__func__, AAUDIO_PROP_OUTPUT_MMAP_OFFSET_USEC);
631 }
632
AAudioProperty_getLogMask()633 int32_t AAudioProperty_getLogMask() {
634 return property_get_int32(AAUDIO_PROP_LOG_MASK, 0);
635 }
636
AAudio_isFlushAllowed(aaudio_stream_state_t state)637 aaudio_result_t AAudio_isFlushAllowed(aaudio_stream_state_t state) {
638 aaudio_result_t result = AAUDIO_OK;
639 switch (state) {
640 // Proceed with flushing.
641 case AAUDIO_STREAM_STATE_OPEN:
642 case AAUDIO_STREAM_STATE_PAUSED:
643 case AAUDIO_STREAM_STATE_STOPPED:
644 case AAUDIO_STREAM_STATE_FLUSHED:
645 break;
646
647 // Transition from one inactive state to another.
648 case AAUDIO_STREAM_STATE_STARTING:
649 case AAUDIO_STREAM_STATE_STARTED:
650 case AAUDIO_STREAM_STATE_STOPPING:
651 case AAUDIO_STREAM_STATE_PAUSING:
652 case AAUDIO_STREAM_STATE_FLUSHING:
653 case AAUDIO_STREAM_STATE_CLOSING:
654 case AAUDIO_STREAM_STATE_CLOSED:
655 case AAUDIO_STREAM_STATE_DISCONNECTED:
656 default:
657 ALOGE("can only flush stream when PAUSED, OPEN or STOPPED, state = %s",
658 aaudio::AudioGlobal_convertStreamStateToText(state));
659 result = AAUDIO_ERROR_INVALID_STATE;
660 break;
661 }
662 return result;
663 }
664
665 namespace {
666
aidl2legacy_aaudio_policy(AudioMMapPolicy aidl)667 aaudio_policy_t aidl2legacy_aaudio_policy(AudioMMapPolicy aidl) {
668 switch (aidl) {
669 case AudioMMapPolicy::NEVER:
670 return AAUDIO_POLICY_NEVER;
671 case AudioMMapPolicy::AUTO:
672 return AAUDIO_POLICY_AUTO;
673 case AudioMMapPolicy::ALWAYS:
674 return AAUDIO_POLICY_ALWAYS;
675 case AudioMMapPolicy::UNSPECIFIED:
676 default:
677 return AAUDIO_UNSPECIFIED;
678 }
679 }
680
681 } // namespace
682
AAudio_getAAudioPolicy(const std::vector<AudioMMapPolicyInfo> & policyInfos,AudioMMapPolicy defaultPolicy)683 aaudio_policy_t AAudio_getAAudioPolicy(const std::vector<AudioMMapPolicyInfo>& policyInfos,
684 AudioMMapPolicy defaultPolicy) {
685 AudioMMapPolicy policy = defaultPolicy;
686 for (const auto& policyInfo : policyInfos) {
687 if (policyInfo.mmapPolicy == AudioMMapPolicy::NEVER) {
688 policy = policyInfo.mmapPolicy;
689 } else if (policyInfo.mmapPolicy == AudioMMapPolicy::AUTO ||
690 policyInfo.mmapPolicy == AudioMMapPolicy::ALWAYS) {
691 return AAUDIO_POLICY_AUTO;
692 }
693 }
694 return aidl2legacy_aaudio_policy(policy);
695 }
696