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 | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 29 | |
| 30 | #include "utility/AAudioUtilities.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace android; |
| 33 | |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 34 | // This is 3 dB, (10^(3/20)), to match the maximum headroom in AudioTrack for float data. |
| 35 | // It is designed to allow occasional transient peaks. |
| 36 | #define MAX_HEADROOM (1.41253754f) |
| 37 | #define MIN_HEADROOM (0 - MAX_HEADROOM) |
| 38 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 39 | int32_t AAudioConvert_formatToSizeInBytes(aaudio_format_t format) { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 40 | int32_t size = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 41 | switch (format) { |
| 42 | case AAUDIO_FORMAT_PCM_I16: |
| 43 | size = sizeof(int16_t); |
| 44 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 45 | case AAUDIO_FORMAT_PCM_FLOAT: |
| 46 | size = sizeof(float); |
| 47 | break; |
| 48 | default: |
| 49 | break; |
| 50 | } |
| 51 | return size; |
| 52 | } |
| 53 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 54 | // TODO expose and call clamp16_from_float function in primitives.h |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 55 | static inline int16_t clamp16_from_float(float f) { |
Phil Burk | bba0900 | 2017-11-29 13:39:44 -0800 | [diff] [blame] | 56 | static const float scale = 1 << 15; |
| 57 | return (int16_t) roundf(fmaxf(fminf(f * scale, scale - 1.f), -scale)); |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static float clipAndClampFloatToPcm16(float sample, float scaler) { |
| 61 | // Clip to valid range of a float sample to prevent excessive volume. |
| 62 | if (sample > MAX_HEADROOM) sample = MAX_HEADROOM; |
| 63 | else if (sample < MIN_HEADROOM) sample = MIN_HEADROOM; |
| 64 | |
| 65 | // Scale and convert to a short. |
| 66 | float fval = sample * scaler; |
| 67 | return clamp16_from_float(fval); |
| 68 | } |
| 69 | |
| 70 | void AAudioConvert_floatToPcm16(const float *source, |
| 71 | int16_t *destination, |
| 72 | int32_t numSamples, |
| 73 | float amplitude) { |
| 74 | float scaler = amplitude; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 75 | for (int i = 0; i < numSamples; i++) { |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 76 | float sample = *source++; |
| 77 | *destination++ = clipAndClampFloatToPcm16(sample, scaler); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 81 | void AAudioConvert_floatToPcm16(const float *source, |
| 82 | int16_t *destination, |
| 83 | int32_t numFrames, |
| 84 | int32_t samplesPerFrame, |
| 85 | float amplitude1, |
| 86 | float amplitude2) { |
| 87 | float scaler = amplitude1; |
| 88 | // divide by numFrames so that we almost reach amplitude2 |
| 89 | float delta = (amplitude2 - amplitude1) / numFrames; |
| 90 | for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) { |
| 91 | for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) { |
| 92 | float sample = *source++; |
| 93 | *destination++ = clipAndClampFloatToPcm16(sample, scaler); |
| 94 | } |
| 95 | scaler += delta; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #define SHORT_SCALE 32768 |
| 100 | |
| 101 | void AAudioConvert_pcm16ToFloat(const int16_t *source, |
| 102 | float *destination, |
| 103 | int32_t numSamples, |
| 104 | float amplitude) { |
| 105 | float scaler = amplitude / SHORT_SCALE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 106 | for (int i = 0; i < numSamples; i++) { |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 107 | destination[i] = source[i] * scaler; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // This code assumes amplitude1 and amplitude2 are between 0.0 and 1.0 |
| 112 | void AAudioConvert_pcm16ToFloat(const int16_t *source, |
| 113 | float *destination, |
| 114 | int32_t numFrames, |
| 115 | int32_t samplesPerFrame, |
| 116 | float amplitude1, |
| 117 | float amplitude2) { |
| 118 | float scaler = amplitude1 / SHORT_SCALE; |
| 119 | float delta = (amplitude2 - amplitude1) / (SHORT_SCALE * (float) numFrames); |
| 120 | for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) { |
| 121 | for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) { |
| 122 | *destination++ = *source++ * scaler; |
| 123 | } |
| 124 | scaler += delta; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // This code assumes amplitude1 and amplitude2 are between 0.0 and 1.0 |
| 129 | void AAudio_linearRamp(const float *source, |
| 130 | float *destination, |
| 131 | int32_t numFrames, |
| 132 | int32_t samplesPerFrame, |
| 133 | float amplitude1, |
| 134 | float amplitude2) { |
| 135 | float scaler = amplitude1; |
| 136 | float delta = (amplitude2 - amplitude1) / numFrames; |
| 137 | for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) { |
| 138 | for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) { |
| 139 | float sample = *source++; |
| 140 | |
| 141 | // Clip to valid range of a float sample to prevent excessive volume. |
| 142 | if (sample > MAX_HEADROOM) sample = MAX_HEADROOM; |
| 143 | else if (sample < MIN_HEADROOM) sample = MIN_HEADROOM; |
| 144 | |
| 145 | *destination++ = sample * scaler; |
| 146 | } |
| 147 | scaler += delta; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // This code assumes amplitude1 and amplitude2 are between 0.0 and 1.0 |
| 152 | void AAudio_linearRamp(const int16_t *source, |
| 153 | int16_t *destination, |
| 154 | int32_t numFrames, |
| 155 | int32_t samplesPerFrame, |
| 156 | float amplitude1, |
| 157 | float amplitude2) { |
Phil Burk | bba0900 | 2017-11-29 13:39:44 -0800 | [diff] [blame] | 158 | // Because we are converting from int16 to 1nt16, we do not have to scale by 1/32768. |
| 159 | float scaler = amplitude1; |
| 160 | float delta = (amplitude2 - amplitude1) / numFrames; |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 161 | for (int frameIndex = 0; frameIndex < numFrames; frameIndex++) { |
| 162 | for (int sampleIndex = 0; sampleIndex < samplesPerFrame; sampleIndex++) { |
| 163 | // No need to clip because int16_t range is inherently limited. |
| 164 | float sample = *source++ * scaler; |
Phil Burk | bba0900 | 2017-11-29 13:39:44 -0800 | [diff] [blame] | 165 | *destination++ = (int16_t) roundf(sample); |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 166 | } |
| 167 | scaler += delta; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result) { |
| 172 | // This covers the case for AAUDIO_OK and for positive results. |
| 173 | if (result >= 0) { |
| 174 | return result; |
| 175 | } |
| 176 | status_t status; |
| 177 | switch (result) { |
| 178 | case AAUDIO_ERROR_DISCONNECTED: |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 179 | case AAUDIO_ERROR_NO_SERVICE: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 180 | status = DEAD_OBJECT; |
| 181 | break; |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 182 | case AAUDIO_ERROR_INVALID_HANDLE: |
| 183 | status = BAD_TYPE; |
| 184 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 185 | case AAUDIO_ERROR_INVALID_STATE: |
| 186 | status = INVALID_OPERATION; |
| 187 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 188 | case AAUDIO_ERROR_INVALID_RATE: |
| 189 | case AAUDIO_ERROR_INVALID_FORMAT: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 190 | case AAUDIO_ERROR_ILLEGAL_ARGUMENT: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 191 | case AAUDIO_ERROR_OUT_OF_RANGE: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 192 | status = BAD_VALUE; |
| 193 | break; |
| 194 | case AAUDIO_ERROR_WOULD_BLOCK: |
| 195 | status = WOULD_BLOCK; |
| 196 | break; |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 197 | case AAUDIO_ERROR_NULL: |
| 198 | status = UNEXPECTED_NULL; |
| 199 | break; |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 200 | case AAUDIO_ERROR_UNAVAILABLE: |
| 201 | status = NOT_ENOUGH_DATA; |
| 202 | break; |
| 203 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 204 | // TODO translate these result codes |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 205 | case AAUDIO_ERROR_INTERNAL: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 206 | case AAUDIO_ERROR_UNIMPLEMENTED: |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 207 | case AAUDIO_ERROR_NO_FREE_HANDLES: |
| 208 | case AAUDIO_ERROR_NO_MEMORY: |
| 209 | case AAUDIO_ERROR_TIMEOUT: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 210 | default: |
| 211 | status = UNKNOWN_ERROR; |
| 212 | break; |
| 213 | } |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | aaudio_result_t AAudioConvert_androidToAAudioResult(status_t status) { |
| 218 | // This covers the case for OK and for positive result. |
| 219 | if (status >= 0) { |
| 220 | return status; |
| 221 | } |
| 222 | aaudio_result_t result; |
| 223 | switch (status) { |
| 224 | case BAD_TYPE: |
| 225 | result = AAUDIO_ERROR_INVALID_HANDLE; |
| 226 | break; |
| 227 | case DEAD_OBJECT: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 228 | result = AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 229 | break; |
| 230 | case INVALID_OPERATION: |
| 231 | result = AAUDIO_ERROR_INVALID_STATE; |
| 232 | break; |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 233 | case UNEXPECTED_NULL: |
| 234 | result = AAUDIO_ERROR_NULL; |
| 235 | break; |
| 236 | case BAD_VALUE: |
| 237 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 238 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 239 | case WOULD_BLOCK: |
| 240 | result = AAUDIO_ERROR_WOULD_BLOCK; |
| 241 | break; |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 242 | case NOT_ENOUGH_DATA: |
| 243 | result = AAUDIO_ERROR_UNAVAILABLE; |
| 244 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 245 | default: |
| 246 | result = AAUDIO_ERROR_INTERNAL; |
| 247 | break; |
| 248 | } |
| 249 | return result; |
| 250 | } |
| 251 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 252 | audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudioFormat) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 253 | audio_format_t androidFormat; |
| 254 | switch (aaudioFormat) { |
| 255 | case AAUDIO_FORMAT_PCM_I16: |
| 256 | androidFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 257 | break; |
| 258 | case AAUDIO_FORMAT_PCM_FLOAT: |
| 259 | androidFormat = AUDIO_FORMAT_PCM_FLOAT; |
| 260 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 261 | default: |
| 262 | androidFormat = AUDIO_FORMAT_DEFAULT; |
| 263 | ALOGE("AAudioConvert_aaudioToAndroidDataFormat 0x%08X unrecognized", aaudioFormat); |
| 264 | break; |
| 265 | } |
| 266 | return androidFormat; |
| 267 | } |
| 268 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 269 | aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t androidFormat) { |
| 270 | aaudio_format_t aaudioFormat = AAUDIO_FORMAT_INVALID; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 271 | switch (androidFormat) { |
| 272 | case AUDIO_FORMAT_PCM_16_BIT: |
| 273 | aaudioFormat = AAUDIO_FORMAT_PCM_I16; |
| 274 | break; |
| 275 | case AUDIO_FORMAT_PCM_FLOAT: |
| 276 | aaudioFormat = AAUDIO_FORMAT_PCM_FLOAT; |
| 277 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 278 | default: |
| 279 | aaudioFormat = AAUDIO_FORMAT_INVALID; |
| 280 | ALOGE("AAudioConvert_androidToAAudioDataFormat 0x%08X unrecognized", androidFormat); |
| 281 | break; |
| 282 | } |
| 283 | return aaudioFormat; |
| 284 | } |
| 285 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 286 | int32_t AAudioConvert_framesToBytes(int32_t numFrames, |
| 287 | int32_t bytesPerFrame, |
| 288 | int32_t *sizeInBytes) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 289 | // TODO implement more elegantly |
| 290 | const int32_t maxChannels = 256; // ridiculously large |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 291 | const int32_t maxBytesPerFrame = maxChannels * sizeof(float); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 292 | // Prevent overflow by limiting multiplicands. |
| 293 | if (bytesPerFrame > maxBytesPerFrame || numFrames > (0x3FFFFFFF / maxBytesPerFrame)) { |
Yi Kong | 0f414de | 2017-12-15 13:48:50 -0800 | [diff] [blame] | 294 | ALOGE("size overflow, numFrames = %d, frameSize = %d", numFrames, bytesPerFrame); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 295 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 296 | } |
| 297 | *sizeInBytes = numFrames * bytesPerFrame; |
| 298 | return AAUDIO_OK; |
| 299 | } |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 300 | |
| 301 | static int32_t AAudioProperty_getMMapProperty(const char *propName, |
| 302 | int32_t defaultValue, |
| 303 | const char * caller) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 304 | int32_t prop = property_get_int32(propName, defaultValue); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 305 | switch (prop) { |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 306 | case AAUDIO_UNSPECIFIED: |
| 307 | case AAUDIO_POLICY_NEVER: |
| 308 | case AAUDIO_POLICY_ALWAYS: |
| 309 | case AAUDIO_POLICY_AUTO: |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 310 | break; |
| 311 | default: |
| 312 | ALOGE("%s: invalid = %d", caller, prop); |
| 313 | prop = defaultValue; |
| 314 | break; |
| 315 | } |
| 316 | return prop; |
| 317 | } |
| 318 | |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 319 | int32_t AAudioProperty_getMMapPolicy() { |
| 320 | return AAudioProperty_getMMapProperty(AAUDIO_PROP_MMAP_POLICY, |
| 321 | AAUDIO_UNSPECIFIED, __func__); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 324 | int32_t AAudioProperty_getMMapExclusivePolicy() { |
| 325 | return AAudioProperty_getMMapProperty(AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY, |
| 326 | AAUDIO_UNSPECIFIED, __func__); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | int32_t AAudioProperty_getMixerBursts() { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 330 | const int32_t defaultBursts = 2; // arbitrary, use 2 for double buffered |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 331 | const int32_t maxBursts = 1024; // arbitrary |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 332 | int32_t prop = property_get_int32(AAUDIO_PROP_MIXER_BURSTS, defaultBursts); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 333 | if (prop < 1 || prop > maxBursts) { |
| 334 | ALOGE("AAudioProperty_getMixerBursts: invalid = %d", prop); |
| 335 | prop = defaultBursts; |
| 336 | } |
| 337 | return prop; |
| 338 | } |
| 339 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 340 | int32_t AAudioProperty_getWakeupDelayMicros() { |
| 341 | const int32_t minMicros = 0; // arbitrary |
| 342 | const int32_t defaultMicros = 200; // arbitrary, based on some observed jitter |
| 343 | const int32_t maxMicros = 5000; // arbitrary, probably don't want more than 500 |
| 344 | int32_t prop = property_get_int32(AAUDIO_PROP_WAKEUP_DELAY_USEC, defaultMicros); |
| 345 | if (prop < minMicros) { |
| 346 | ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, minMicros); |
| 347 | prop = minMicros; |
| 348 | } else if (prop > maxMicros) { |
| 349 | ALOGW("AAudioProperty_getWakeupDelayMicros: clipped %d to %d", prop, maxMicros); |
| 350 | prop = maxMicros; |
| 351 | } |
| 352 | return prop; |
| 353 | } |
| 354 | |
| 355 | int32_t AAudioProperty_getMinimumSleepMicros() { |
| 356 | const int32_t minMicros = 20; // arbitrary |
| 357 | const int32_t defaultMicros = 200; // arbitrary |
| 358 | const int32_t maxMicros = 2000; // arbitrary |
| 359 | int32_t prop = property_get_int32(AAUDIO_PROP_MINIMUM_SLEEP_USEC, defaultMicros); |
| 360 | if (prop < minMicros) { |
| 361 | ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, minMicros); |
| 362 | prop = minMicros; |
| 363 | } else if (prop > maxMicros) { |
| 364 | ALOGW("AAudioProperty_getMinimumSleepMicros: clipped %d to %d", prop, maxMicros); |
| 365 | prop = maxMicros; |
| 366 | } |
| 367 | return prop; |
| 368 | } |
| 369 | |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 370 | int32_t AAudioProperty_getHardwareBurstMinMicros() { |
| 371 | const int32_t defaultMicros = 1000; // arbitrary |
| 372 | const int32_t maxMicros = 1000 * 1000; // arbitrary |
| 373 | int32_t prop = property_get_int32(AAUDIO_PROP_HW_BURST_MIN_USEC, defaultMicros); |
| 374 | if (prop < 1 || prop > maxMicros) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 375 | ALOGE("AAudioProperty_getHardwareBurstMinMicros: invalid = %d, use %d", |
| 376 | prop, defaultMicros); |
Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 377 | prop = defaultMicros; |
| 378 | } |
| 379 | return prop; |
| 380 | } |