Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 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 | #include <utils/Log.h> |
| 20 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <utils/Errors.h> |
| 25 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 26 | #include "aaudio/AAudio.h" |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 27 | #include <aaudio/AAudioTesting.h> |
Phil Burk | bba0900 | 2017-11-29 13:39:44 -0800 | [diff] [blame] | 28 | #include <math.h> |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 29 | #include <system/audio-base.h> |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 30 | #include <assert.h> |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 31 | |
| 32 | #include "utility/AAudioUtilities.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 33 | |
| 34 | using namespace android; |
| 35 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 36 | status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result) { |
| 37 | // This covers the case for AAUDIO_OK and for positive results. |
| 38 | if (result >= 0) { |
| 39 | return result; |
| 40 | } |
| 41 | status_t status; |
| 42 | switch (result) { |
| 43 | case AAUDIO_ERROR_DISCONNECTED: |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 44 | case AAUDIO_ERROR_NO_SERVICE: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 45 | status = DEAD_OBJECT; |
| 46 | break; |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 47 | case AAUDIO_ERROR_INVALID_HANDLE: |
| 48 | status = BAD_TYPE; |
| 49 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | case AAUDIO_ERROR_INVALID_STATE: |
| 51 | status = INVALID_OPERATION; |
| 52 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 53 | case AAUDIO_ERROR_INVALID_RATE: |
| 54 | case AAUDIO_ERROR_INVALID_FORMAT: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 55 | case AAUDIO_ERROR_ILLEGAL_ARGUMENT: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 56 | case AAUDIO_ERROR_OUT_OF_RANGE: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 57 | status = BAD_VALUE; |
| 58 | break; |
| 59 | case AAUDIO_ERROR_WOULD_BLOCK: |
| 60 | status = WOULD_BLOCK; |
| 61 | break; |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 62 | case AAUDIO_ERROR_NULL: |
| 63 | status = UNEXPECTED_NULL; |
| 64 | break; |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 65 | case AAUDIO_ERROR_UNAVAILABLE: |
| 66 | status = NOT_ENOUGH_DATA; |
| 67 | break; |
| 68 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 69 | // TODO translate these result codes |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 70 | case AAUDIO_ERROR_INTERNAL: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 71 | case AAUDIO_ERROR_UNIMPLEMENTED: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 72 | case AAUDIO_ERROR_NO_FREE_HANDLES: |
| 73 | case AAUDIO_ERROR_NO_MEMORY: |
| 74 | case AAUDIO_ERROR_TIMEOUT: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 75 | default: |
| 76 | status = UNKNOWN_ERROR; |
| 77 | break; |
| 78 | } |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | aaudio_result_t AAudioConvert_androidToAAudioResult(status_t status) { |
| 83 | // This covers the case for OK and for positive result. |
| 84 | if (status >= 0) { |
| 85 | return status; |
| 86 | } |
| 87 | aaudio_result_t result; |
| 88 | switch (status) { |
| 89 | case BAD_TYPE: |
| 90 | result = AAUDIO_ERROR_INVALID_HANDLE; |
| 91 | break; |
| 92 | case DEAD_OBJECT: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 93 | result = AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 94 | break; |
| 95 | case INVALID_OPERATION: |
| 96 | result = AAUDIO_ERROR_INVALID_STATE; |
| 97 | break; |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 98 | case UNEXPECTED_NULL: |
| 99 | result = AAUDIO_ERROR_NULL; |
| 100 | break; |
| 101 | case BAD_VALUE: |
| 102 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 103 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 104 | case WOULD_BLOCK: |
| 105 | result = AAUDIO_ERROR_WOULD_BLOCK; |
| 106 | break; |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 107 | case NOT_ENOUGH_DATA: |
| 108 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 109 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | default: |
| 111 | result = AAUDIO_ERROR_INTERNAL; |
| 112 | break; |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 117 | audio_session_t AAudioConvert_aaudioToAndroidSessionId(aaudio_session_id_t sessionId) { |
Phil Burk | 67fdd89 | 2018-01-23 15:28:55 -0800 | [diff] [blame] | 118 | // If not a regular sessionId then convert to a safe value of AUDIO_SESSION_ALLOCATE. |
| 119 | return (sessionId == AAUDIO_SESSION_ID_ALLOCATE || sessionId == AAUDIO_SESSION_ID_NONE) |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 120 | ? AUDIO_SESSION_ALLOCATE |
| 121 | : (audio_session_t) sessionId; |
| 122 | } |
| 123 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 124 | audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudioFormat) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 125 | audio_format_t androidFormat; |
| 126 | switch (aaudioFormat) { |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 127 | case AAUDIO_FORMAT_UNSPECIFIED: |
| 128 | androidFormat = AUDIO_FORMAT_DEFAULT; |
| 129 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 130 | case AAUDIO_FORMAT_PCM_I16: |
| 131 | androidFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 132 | break; |
| 133 | case AAUDIO_FORMAT_PCM_FLOAT: |
| 134 | androidFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 135 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 136 | default: |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 137 | androidFormat = AUDIO_FORMAT_INVALID; |
| 138 | ALOGE("%s() 0x%08X unrecognized", __func__, aaudioFormat); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | return androidFormat; |
| 142 | } |
| 143 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 144 | aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t androidFormat) { |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 145 | aaudio_format_t aaudioFormat; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 146 | switch (androidFormat) { |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 147 | case AUDIO_FORMAT_DEFAULT: |
| 148 | aaudioFormat = AAUDIO_FORMAT_UNSPECIFIED; |
| 149 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 | case AUDIO_FORMAT_PCM_16_BIT: |
| 151 | aaudioFormat = AAUDIO_FORMAT_PCM_I16; |
| 152 | break; |
| 153 | case AUDIO_FORMAT_PCM_FLOAT: |
| 154 | aaudioFormat = AAUDIO_FORMAT_PCM_FLOAT; |
| 155 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 156 | default: |
| 157 | aaudioFormat = AAUDIO_FORMAT_INVALID; |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 158 | ALOGE("%s() 0x%08X unrecognized", __func__, androidFormat); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | return aaudioFormat; |
| 162 | } |
| 163 | |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 164 | // Make a message string from the condition. |
| 165 | #define STATIC_ASSERT(condition) static_assert(condition, #condition) |
| 166 | |
| 167 | audio_usage_t AAudioConvert_usageToInternal(aaudio_usage_t usage) { |
| 168 | // The public aaudio_content_type_t constants are supposed to have the same |
| 169 | // values as the internal audio_content_type_t values. |
| 170 | STATIC_ASSERT(AAUDIO_USAGE_MEDIA == AUDIO_USAGE_MEDIA); |
| 171 | STATIC_ASSERT(AAUDIO_USAGE_VOICE_COMMUNICATION == AUDIO_USAGE_VOICE_COMMUNICATION); |
| 172 | STATIC_ASSERT(AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING |
| 173 | == AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING); |
| 174 | STATIC_ASSERT(AAUDIO_USAGE_ALARM == AUDIO_USAGE_ALARM); |
| 175 | STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION == AUDIO_USAGE_NOTIFICATION); |
| 176 | STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION_RINGTONE |
| 177 | == AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE); |
| 178 | STATIC_ASSERT(AAUDIO_USAGE_NOTIFICATION_EVENT == AUDIO_USAGE_NOTIFICATION_EVENT); |
| 179 | STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY == AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY); |
| 180 | STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE |
| 181 | == AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE); |
| 182 | STATIC_ASSERT(AAUDIO_USAGE_ASSISTANCE_SONIFICATION == AUDIO_USAGE_ASSISTANCE_SONIFICATION); |
| 183 | STATIC_ASSERT(AAUDIO_USAGE_GAME == AUDIO_USAGE_GAME); |
| 184 | STATIC_ASSERT(AAUDIO_USAGE_ASSISTANT == AUDIO_USAGE_ASSISTANT); |
| 185 | if (usage == AAUDIO_UNSPECIFIED) { |
| 186 | usage = AAUDIO_USAGE_MEDIA; |
| 187 | } |
| 188 | return (audio_usage_t) usage; // same value |
| 189 | } |
| 190 | |
| 191 | audio_content_type_t AAudioConvert_contentTypeToInternal(aaudio_content_type_t contentType) { |
| 192 | // The public aaudio_content_type_t constants are supposed to have the same |
| 193 | // values as the internal audio_content_type_t values. |
| 194 | STATIC_ASSERT(AAUDIO_CONTENT_TYPE_MUSIC == AUDIO_CONTENT_TYPE_MUSIC); |
| 195 | STATIC_ASSERT(AAUDIO_CONTENT_TYPE_SPEECH == AUDIO_CONTENT_TYPE_SPEECH); |
| 196 | STATIC_ASSERT(AAUDIO_CONTENT_TYPE_SONIFICATION == AUDIO_CONTENT_TYPE_SONIFICATION); |
| 197 | STATIC_ASSERT(AAUDIO_CONTENT_TYPE_MOVIE == AUDIO_CONTENT_TYPE_MOVIE); |
| 198 | if (contentType == AAUDIO_UNSPECIFIED) { |
| 199 | contentType = AAUDIO_CONTENT_TYPE_MUSIC; |
| 200 | } |
| 201 | return (audio_content_type_t) contentType; // same value |
| 202 | } |
| 203 | |
| 204 | audio_source_t AAudioConvert_inputPresetToAudioSource(aaudio_input_preset_t preset) { |
| 205 | // The public aaudio_input_preset_t constants are supposed to have the same |
| 206 | // values as the internal audio_source_t values. |
| 207 | STATIC_ASSERT(AAUDIO_UNSPECIFIED == AUDIO_SOURCE_DEFAULT); |
| 208 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_GENERIC == AUDIO_SOURCE_MIC); |
| 209 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_CAMCORDER == AUDIO_SOURCE_CAMCORDER); |
| 210 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_RECOGNITION == AUDIO_SOURCE_VOICE_RECOGNITION); |
| 211 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION == AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 212 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_UNPROCESSED == AUDIO_SOURCE_UNPROCESSED); |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 213 | STATIC_ASSERT(AAUDIO_INPUT_PRESET_VOICE_PERFORMANCE == AUDIO_SOURCE_VOICE_PERFORMANCE); |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 214 | if (preset == AAUDIO_UNSPECIFIED) { |
Phil Burk | eaef9b9 | 2018-01-18 09:09:42 -0800 | [diff] [blame] | 215 | preset = AAUDIO_INPUT_PRESET_VOICE_RECOGNITION; |
Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 216 | } |
| 217 | return (audio_source_t) preset; // same value |
| 218 | } |
| 219 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 220 | int32_t AAudioConvert_framesToBytes(int32_t numFrames, |
Phil Burk | 7f68013 | 2018-03-12 14:48:06 -0700 | [diff] [blame] | 221 | int32_t bytesPerFrame, |
| 222 | int32_t *sizeInBytes) { |
| 223 | *sizeInBytes = 0; |
| 224 | |
| 225 | if (numFrames < 0 || bytesPerFrame < 0) { |
| 226 | ALOGE("negative size, numFrames = %d, frameSize = %d", numFrames, bytesPerFrame); |
| 227 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 228 | } |
| 229 | |
| 230 | // Prevent numeric overflow. |
| 231 | if (numFrames > (INT32_MAX / bytesPerFrame)) { |
Yi Kong | 0f414de | 2017-12-15 13:48:50 -0800 | [diff] [blame] | 232 | ALOGE("size overflow, numFrames = %d, frameSize = %d", numFrames, bytesPerFrame); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 233 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 234 | } |
Phil Burk | 7f68013 | 2018-03-12 14:48:06 -0700 | [diff] [blame] | 235 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 236 | *sizeInBytes = numFrames * bytesPerFrame; |
| 237 | return AAUDIO_OK; |
| 238 | } |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 239 | |
| 240 | static int32_t AAudioProperty_getMMapProperty(const char *propName, |
| 241 | int32_t defaultValue, |
| 242 | const char * caller) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 243 | int32_t prop = property_get_int32(propName, defaultValue); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 244 | switch (prop) { |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 245 | case AAUDIO_UNSPECIFIED: |
| 246 | case AAUDIO_POLICY_NEVER: |
| 247 | case AAUDIO_POLICY_ALWAYS: |
| 248 | case AAUDIO_POLICY_AUTO: |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 249 | break; |
| 250 | default: |
| 251 | ALOGE("%s: invalid = %d", caller, prop); |
| 252 | prop = defaultValue; |
| 253 | break; |
| 254 | } |
| 255 | return prop; |
| 256 | } |
| 257 | |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 258 | int32_t AAudioProperty_getMMapPolicy() { |
| 259 | return AAudioProperty_getMMapProperty(AAUDIO_PROP_MMAP_POLICY, |
| 260 | AAUDIO_UNSPECIFIED, __func__); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 263 | int32_t AAudioProperty_getMMapExclusivePolicy() { |
| 264 | return AAudioProperty_getMMapProperty(AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY, |
| 265 | AAUDIO_UNSPECIFIED, __func__); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int32_t AAudioProperty_getMixerBursts() { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 269 | const int32_t defaultBursts = 2; // arbitrary, use 2 for double buffered |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 270 | const int32_t maxBursts = 1024; // arbitrary |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 271 | int32_t prop = property_get_int32(AAUDIO_PROP_MIXER_BURSTS, defaultBursts); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 272 | if (prop < 1 || prop > maxBursts) { |
| 273 | ALOGE("AAudioProperty_getMixerBursts: invalid = %d", prop); |
| 274 | prop = defaultBursts; |
| 275 | } |
| 276 | return prop; |
| 277 | } |
| 278 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 279 | int32_t AAudioProperty_getWakeupDelayMicros() { |
| 280 | const int32_t minMicros = 0; // arbitrary |
| 281 | const int32_t defaultMicros = 200; // arbitrary, based on some observed jitter |
| 282 | const int32_t maxMicros = 5000; // arbitrary, probably don't want more than 500 |
| 283 | int32_t prop = property_get_int32(AAUDIO_PROP_WAKEUP_DELAY_USEC, defaultMicros); |
| 284 | if (prop < minMicros) { |
| 285 | ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, minMicros); |
| 286 | prop = minMicros; |
| 287 | } else if (prop > maxMicros) { |
| 288 | ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, maxMicros); |
| 289 | prop = maxMicros; |
| 290 | } |
| 291 | return prop; |
| 292 | } |
| 293 | |
| 294 | int32_t AAudioProperty_getMinimumSleepMicros() { |
| 295 | const int32_t minMicros = 20; // arbitrary |
| 296 | const int32_t defaultMicros = 200; // arbitrary |
| 297 | const int32_t maxMicros = 2000; // arbitrary |
| 298 | int32_t prop = property_get_int32(AAUDIO_PROP_MINIMUM_SLEEP_USEC, defaultMicros); |
| 299 | if (prop < minMicros) { |
| 300 | ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, minMicros); |
| 301 | prop = minMicros; |
| 302 | } else if (prop > maxMicros) { |
| 303 | ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, maxMicros); |
| 304 | prop = maxMicros; |
| 305 | } |
| 306 | return prop; |
| 307 | } |
| 308 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 309 | int32_t AAudioProperty_getHardwareBurstMinMicros() { |
| 310 | const int32_t defaultMicros = 1000; // arbitrary |
| 311 | const int32_t maxMicros = 1000 * 1000; // arbitrary |
| 312 | int32_t prop = property_get_int32(AAUDIO_PROP_HW_BURST_MIN_USEC, defaultMicros); |
| 313 | if (prop < 1 || prop > maxMicros) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 314 | ALOGE("AAudioProperty_getHardwareBurstMinMicros: invalid = %d, use %d", |
| 315 | prop, defaultMicros); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 316 | prop = defaultMicros; |
| 317 | } |
| 318 | return prop; |
| 319 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 320 | |
| 321 | aaudio_result_t AAudio_isFlushAllowed(aaudio_stream_state_t state) { |
| 322 | aaudio_result_t result = AAUDIO_OK; |
| 323 | switch (state) { |
| 324 | // Proceed with flushing. |
| 325 | case AAUDIO_STREAM_STATE_OPEN: |
| 326 | case AAUDIO_STREAM_STATE_PAUSED: |
| 327 | case AAUDIO_STREAM_STATE_STOPPED: |
| 328 | case AAUDIO_STREAM_STATE_FLUSHED: |
| 329 | break; |
| 330 | |
| 331 | // Transition from one inactive state to another. |
| 332 | case AAUDIO_STREAM_STATE_STARTING: |
| 333 | case AAUDIO_STREAM_STATE_STARTED: |
| 334 | case AAUDIO_STREAM_STATE_STOPPING: |
| 335 | case AAUDIO_STREAM_STATE_PAUSING: |
| 336 | case AAUDIO_STREAM_STATE_FLUSHING: |
| 337 | case AAUDIO_STREAM_STATE_CLOSING: |
| 338 | case AAUDIO_STREAM_STATE_CLOSED: |
| 339 | case AAUDIO_STREAM_STATE_DISCONNECTED: |
| 340 | default: |
| 341 | ALOGE("can only flush stream when PAUSED, OPEN or STOPPED, state = %s", |
| 342 | AAudio_convertStreamStateToText(state)); |
| 343 | result = AAUDIO_ERROR_INVALID_STATE; |
| 344 | break; |
| 345 | } |
| 346 | return result; |
| 347 | } |