| 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 | #ifndef UTILITY_AAUDIO_UTILITIES_H | 
|  | 18 | #define UTILITY_AAUDIO_UTILITIES_H | 
|  | 19 |  | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 20 | #include <algorithm> | 
|  | 21 | #include <functional> | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <stdint.h> | 
|  | 23 | #include <sys/types.h> | 
|  | 24 |  | 
|  | 25 | #include <utils/Errors.h> | 
| Kevin Rocard | 6d7582e | 2018-01-11 19:28:14 -0800 | [diff] [blame] | 26 | #include <system/audio.h> | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 27 |  | 
| Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 28 | #include "aaudio/AAudio.h" | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | /** | 
|  | 31 | * Convert an AAudio result into the closest matching Android status. | 
|  | 32 | */ | 
|  | 33 | android::status_t AAudioConvert_aaudioToAndroidStatus(aaudio_result_t result); | 
|  | 34 |  | 
|  | 35 | /** | 
|  | 36 | * Convert an Android status into the closest matching AAudio result. | 
|  | 37 | */ | 
|  | 38 | aaudio_result_t AAudioConvert_androidToAAudioResult(android::status_t status); | 
|  | 39 |  | 
| Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 40 | /** | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 41 | * Convert an aaudio_session_id_t to a value that is safe to pass to AudioFlinger. | 
|  | 42 | * @param sessionId | 
|  | 43 | * @return safe value | 
|  | 44 | */ | 
|  | 45 | audio_session_t AAudioConvert_aaudioToAndroidSessionId(aaudio_session_id_t sessionId); | 
|  | 46 |  | 
|  | 47 | /** | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 48 | * Calculate the number of bytes and prevent numeric overflow. | 
| Phil Burk | 7f68013 | 2018-03-12 14:48:06 -0700 | [diff] [blame] | 49 | * The *sizeInBytes will be set to zero if there is an error. | 
|  | 50 | * | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 51 | * @param numFrames frame count | 
|  | 52 | * @param bytesPerFrame size of a frame in bytes | 
| Phil Burk | 7f68013 | 2018-03-12 14:48:06 -0700 | [diff] [blame] | 53 | * @param sizeInBytes pointer to a variable to receive total size in bytes | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 54 | * @return AAUDIO_OK or negative error, eg. AAUDIO_ERROR_OUT_OF_RANGE | 
|  | 55 | */ | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 56 | int32_t AAudioConvert_framesToBytes(int32_t numFrames, | 
| Phil Burk | 7f68013 | 2018-03-12 14:48:06 -0700 | [diff] [blame] | 57 | int32_t bytesPerFrame, | 
|  | 58 | int32_t *sizeInBytes); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 59 |  | 
| Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 60 | audio_format_t AAudioConvert_aaudioToAndroidDataFormat(aaudio_format_t aaudio_format); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 61 |  | 
| Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 62 | aaudio_format_t AAudioConvert_androidToAAudioDataFormat(audio_format_t format); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 |  | 
| Phil Burk | d4ccc62 | 2017-12-20 15:32:44 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | /** | 
|  | 66 | * Note that this function does not validate the passed in value. | 
|  | 67 | * That is done somewhere else. | 
|  | 68 | * @return internal value | 
|  | 69 | */ | 
|  | 70 |  | 
|  | 71 | audio_usage_t AAudioConvert_usageToInternal(aaudio_usage_t usage); | 
|  | 72 |  | 
|  | 73 | /** | 
|  | 74 | * Note that this function does not validate the passed in value. | 
|  | 75 | * That is done somewhere else. | 
|  | 76 | * @return internal value | 
|  | 77 | */ | 
|  | 78 | audio_content_type_t AAudioConvert_contentTypeToInternal(aaudio_content_type_t contentType); | 
|  | 79 |  | 
|  | 80 | /** | 
|  | 81 | * Note that this function does not validate the passed in value. | 
|  | 82 | * That is done somewhere else. | 
|  | 83 | * @return internal audio source | 
|  | 84 | */ | 
|  | 85 | audio_source_t AAudioConvert_inputPresetToAudioSource(aaudio_input_preset_t preset); | 
|  | 86 |  | 
| Kevin Rocard | 68646ba | 2019-03-20 13:26:49 -0700 | [diff] [blame] | 87 | /** | 
|  | 88 | * Note that this function does not validate the passed in value. | 
|  | 89 | * That is done somewhere else. | 
|  | 90 | * @return internal audio flags mask | 
|  | 91 | */ | 
|  | 92 | audio_flags_mask_t AAudioConvert_allowCapturePolicyToAudioFlagsMask( | 
|  | 93 | aaudio_allowed_capture_policy_t policy); | 
|  | 94 |  | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 95 | // Note that this code may be replaced by Settings or by some other system configuration tool. | 
|  | 96 |  | 
| Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 97 | /** | 
|  | 98 | * Read system property. | 
|  | 99 | * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS | 
|  | 100 | */ | 
|  | 101 | int32_t AAudioProperty_getMMapPolicy(); | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 102 | #define AAUDIO_PROP_MMAP_POLICY           "aaudio.mmap_policy" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | /** | 
|  | 105 | * Read system property. | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 106 | * @return AAUDIO_UNSPECIFIED, AAUDIO_POLICY_NEVER or AAUDIO_POLICY_AUTO or AAUDIO_POLICY_ALWAYS | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 107 | */ | 
| Phil Burk | d04aeea | 2017-05-23 13:56:41 -0700 | [diff] [blame] | 108 | int32_t AAudioProperty_getMMapExclusivePolicy(); | 
| Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 109 | #define AAUDIO_PROP_MMAP_EXCLUSIVE_POLICY "aaudio.mmap_exclusive_policy" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | /** | 
|  | 112 | * Read system property. | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 113 | * @return number of bursts per AAudio service mixer cycle | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 114 | */ | 
|  | 115 | int32_t AAudioProperty_getMixerBursts(); | 
| Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 116 | #define AAUDIO_PROP_MIXER_BURSTS           "aaudio.mixer_bursts" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | /** | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 119 | * Read a system property that specifies the number of extra microseconds that a thread | 
|  | 120 | * should sleep when waiting for another thread to service a FIFO. This is used | 
|  | 121 | * to avoid the waking thread from being overly optimistic about the other threads | 
|  | 122 | * wakeup timing. This value should be set high enough to cover typical scheduling jitter | 
|  | 123 | * for a real-time thread. | 
|  | 124 | * | 
|  | 125 | * @return number of microseconds to delay the wakeup. | 
|  | 126 | */ | 
|  | 127 | int32_t AAudioProperty_getWakeupDelayMicros(); | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 128 | #define AAUDIO_PROP_WAKEUP_DELAY_USEC      "aaudio.wakeup_delay_usec" | 
|  | 129 |  | 
|  | 130 | /** | 
|  | 131 | * Read a system property that specifies the minimum sleep time when polling the FIFO. | 
|  | 132 | * | 
|  | 133 | * @return minimum number of microseconds to sleep. | 
|  | 134 | */ | 
|  | 135 | int32_t AAudioProperty_getMinimumSleepMicros(); | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 136 | #define AAUDIO_PROP_MINIMUM_SLEEP_USEC      "aaudio.minimum_sleep_usec" | 
|  | 137 |  | 
|  | 138 | /** | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 139 | * Read system property. | 
|  | 140 | * This is handy in case the DMA is bursting too quickly for the CPU to keep up. | 
|  | 141 | * For example, there may be a DMA burst every 100 usec but you only | 
|  | 142 | * want to feed the MMAP buffer every 2000 usec. | 
|  | 143 | * | 
|  | 144 | * This will affect the framesPerBurst for an MMAP stream. | 
|  | 145 | * | 
|  | 146 | * @return minimum number of microseconds for a MMAP HW burst | 
|  | 147 | */ | 
|  | 148 | int32_t AAudioProperty_getHardwareBurstMinMicros(); | 
| Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 149 | #define AAUDIO_PROP_HW_BURST_MIN_USEC      "aaudio.hw_burst_min_usec" | 
| Phil Burk | c8f69a0 | 2017-05-11 15:53:06 -0700 | [diff] [blame] | 150 |  | 
| Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 151 | /** | 
|  | 152 | * Read a system property that specifies an offset that will be added to MMAP timestamps. | 
|  | 153 | * This can be used to correct bias in the timestamp. | 
|  | 154 | * It can also be used to analyze the time distribution of the timestamp | 
|  | 155 | * by progressively modifying the offset and listening for glitches. | 
|  | 156 | * | 
|  | 157 | * @return number of microseconds to offset the time part of an MMAP timestamp | 
|  | 158 | */ | 
|  | 159 | int32_t AAudioProperty_getInputMMapOffsetMicros(); | 
|  | 160 | #define AAUDIO_PROP_INPUT_MMAP_OFFSET_USEC    "aaudio.in_mmap_offset_usec" | 
|  | 161 |  | 
|  | 162 | int32_t AAudioProperty_getOutputMMapOffsetMicros(); | 
|  | 163 | #define AAUDIO_PROP_OUTPUT_MMAP_OFFSET_USEC   "aaudio.out_mmap_offset_usec" | 
| Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 164 |  | 
| Phil Burk | ef34be5 | 2019-09-26 13:45:25 -0700 | [diff] [blame^] | 165 | // These are powers of two that can be combined as a bit mask. | 
|  | 166 | // AAUDIO_LOG_CLOCK_MODEL_HISTOGRAM must be enabled before the stream is opened. | 
|  | 167 | #define AAUDIO_LOG_CLOCK_MODEL_HISTOGRAM   1 | 
|  | 168 | #define AAUDIO_LOG_RESERVED_2              2 | 
|  | 169 | #define AAUDIO_LOG_RESERVED_4              4 | 
|  | 170 | #define AAUDIO_LOG_RESERVED_8              8 | 
|  | 171 |  | 
|  | 172 | /** | 
|  | 173 | * Use a mask to enable various logs in AAudio. | 
|  | 174 | * @return mask that enables various AAudio logs, such as AAUDIO_LOG_CLOCK_MODEL_HISTOGRAM | 
|  | 175 | */ | 
|  | 176 | int32_t AAudioProperty_getLogMask(); | 
|  | 177 | #define AAUDIO_PROP_LOG_MASK   "aaudio.log_mask" | 
|  | 178 |  | 
| Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 179 | /** | 
|  | 180 | * Is flush allowed for the given state? | 
|  | 181 | * @param state | 
|  | 182 | * @return AAUDIO_OK if allowed or an error | 
|  | 183 | */ | 
|  | 184 | aaudio_result_t AAudio_isFlushAllowed(aaudio_stream_state_t state); | 
|  | 185 |  | 
| Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 186 | /** | 
|  | 187 | * Try a function f until it returns true. | 
|  | 188 | * | 
|  | 189 | * The function is always called at least once. | 
|  | 190 | * | 
|  | 191 | * @param f the function to evaluate, which returns a bool. | 
|  | 192 | * @param times the number of times to evaluate f. | 
|  | 193 | * @param sleepMs the sleep time per check of f, if greater than 0. | 
|  | 194 | * @return true if f() eventually returns true. | 
|  | 195 | */ | 
|  | 196 | static inline bool AAudio_tryUntilTrue( | 
|  | 197 | std::function<bool()> f, int times, int sleepMs) { | 
|  | 198 | static const useconds_t US_PER_MS = 1000; | 
|  | 199 |  | 
|  | 200 | sleepMs = std::max(sleepMs, 0); | 
|  | 201 | for (;;) { | 
|  | 202 | if (f()) return true; | 
|  | 203 | if (times <= 1) return false; | 
|  | 204 | --times; | 
|  | 205 | usleep(sleepMs * US_PER_MS); | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 |  | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 209 |  | 
|  | 210 | /** | 
|  | 211 | * Simple double buffer for a structure that can be written occasionally and read occasionally. | 
|  | 212 | * This allows a SINGLE writer with multiple readers. | 
|  | 213 | * | 
|  | 214 | * It is OK if the FIFO overflows and we lose old values. | 
|  | 215 | * It is also OK if we read an old value. | 
|  | 216 | * Thread may return a non-atomic result if the other thread is rapidly writing | 
|  | 217 | * new values on another core. | 
|  | 218 | */ | 
|  | 219 | template <class T> | 
|  | 220 | class SimpleDoubleBuffer { | 
|  | 221 | public: | 
|  | 222 | SimpleDoubleBuffer() | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 223 | : mValues() {} | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 224 |  | 
|  | 225 | __attribute__((no_sanitize("integer"))) | 
|  | 226 | void write(T value) { | 
|  | 227 | int index = mCounter.load() & 1; | 
|  | 228 | mValues[index] = value; | 
|  | 229 | mCounter++; // Increment AFTER updating storage, OK if it wraps. | 
|  | 230 | } | 
|  | 231 |  | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 232 | /** | 
|  | 233 | * This should only be called by the same thread that calls write() or when | 
|  | 234 | * no other thread is calling write. | 
|  | 235 | */ | 
|  | 236 | void clear() { | 
|  | 237 | mCounter.store(0); | 
|  | 238 | } | 
|  | 239 |  | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 240 | T read() const { | 
|  | 241 | T result; | 
|  | 242 | int before; | 
|  | 243 | int after; | 
|  | 244 | int timeout = 3; | 
|  | 245 | do { | 
|  | 246 | // Check to see if a write occurred while were reading. | 
|  | 247 | before = mCounter.load(); | 
|  | 248 | int index = (before & 1) ^ 1; | 
|  | 249 | result = mValues[index]; | 
|  | 250 | after = mCounter.load(); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 251 | } while ((after != before) && (after > 0) && (--timeout > 0)); | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 252 | return result; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | /** | 
|  | 256 | * @return true if at least one value has been written | 
|  | 257 | */ | 
|  | 258 | bool isValid() const { | 
|  | 259 | return mCounter.load() > 0; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | private: | 
|  | 263 | T                    mValues[2]; | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 264 | std::atomic<int>     mCounter{0}; | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 265 | }; | 
|  | 266 |  | 
|  | 267 | class Timestamp { | 
|  | 268 | public: | 
|  | 269 | Timestamp() | 
|  | 270 | : mPosition(0) | 
|  | 271 | , mNanoseconds(0) {} | 
|  | 272 | Timestamp(int64_t position, int64_t nanoseconds) | 
|  | 273 | : mPosition(position) | 
|  | 274 | , mNanoseconds(nanoseconds) {} | 
|  | 275 |  | 
|  | 276 | int64_t getPosition() const { return mPosition; } | 
|  | 277 |  | 
|  | 278 | int64_t getNanoseconds() const { return mNanoseconds; } | 
|  | 279 |  | 
|  | 280 | private: | 
|  | 281 | // These cannot be const because we need to implement the copy assignment operator. | 
|  | 282 | int64_t mPosition; | 
|  | 283 | int64_t mNanoseconds; | 
|  | 284 | }; | 
|  | 285 |  | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 286 |  | 
|  | 287 | /** | 
|  | 288 | * Pass a request to another thread. | 
|  | 289 | * This is used when one thread, A, wants another thread, B, to do something. | 
|  | 290 | * A naive approach would be for A to set a flag and for B to clear it when done. | 
|  | 291 | * But that creates a race condition. This technique avoids the race condition. | 
|  | 292 | * | 
|  | 293 | * Assumes only one requester and one acknowledger. | 
|  | 294 | */ | 
|  | 295 | class AtomicRequestor { | 
|  | 296 | public: | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 297 |  | 
|  | 298 | __attribute__((no_sanitize("integer"))) | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 299 | void request() { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 300 | mRequested++; | 
|  | 301 | } | 
|  | 302 |  | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 303 | __attribute__((no_sanitize("integer"))) | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 304 | bool isRequested() { | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 305 | return (mRequested.load() - mAcknowledged.load()) > 0; | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
| Phil Burk | 2d5ba53 | 2017-09-06 14:36:11 -0700 | [diff] [blame] | 308 | __attribute__((no_sanitize("integer"))) | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 309 | void acknowledge() { | 
|  | 310 | mAcknowledged++; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | private: | 
|  | 314 | std::atomic<int> mRequested{0}; | 
|  | 315 | std::atomic<int> mAcknowledged{0}; | 
|  | 316 | }; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 317 | #endif //UTILITY_AAUDIO_UTILITIES_H |