Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | /** |
| 18 | * This is the 'C' ABI for AAudio. |
| 19 | */ |
| 20 | #ifndef AAUDIO_AAUDIO_H |
| 21 | #define AAUDIO_AAUDIO_H |
| 22 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 23 | #include <time.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 24 | #include "AAudioDefinitions.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 30 | typedef struct AAudioStreamStruct * AAudioStream; |
| 31 | typedef struct AAudioStreamBuilderStruct * AAudioStreamBuilder; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 33 | #define AAUDIO_STREAM_NONE ((AAudioStream)nullptr) |
| 34 | #define AAUDIO_STREAM_BUILDER_NONE ((AAudioStreamBuilder)nullptr) |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 35 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 36 | #ifndef AAUDIO_API |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 37 | #define AAUDIO_API /* export this symbol */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | // ============================================================ |
| 41 | // Audio System |
| 42 | // ============================================================ |
| 43 | |
| 44 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 45 | * The text is the ASCII symbol corresponding to the returnCode, |
| 46 | * or an English message saying the returnCode is unrecognized. |
| 47 | * This is intended for developers to use when debugging. |
| 48 | * It is not for display to users. |
| 49 | * |
| 50 | * @return pointer to a text representation of an AAudio result code. |
| 51 | */ |
| 52 | AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode); |
| 53 | |
| 54 | /** |
| 55 | * The text is the ASCII symbol corresponding to the stream state, |
| 56 | * or an English message saying the state is unrecognized. |
| 57 | * This is intended for developers to use when debugging. |
| 58 | * It is not for display to users. |
| 59 | * |
| 60 | * @return pointer to a text representation of an AAudio state. |
| 61 | */ |
| 62 | AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state); |
| 63 | |
| 64 | // ============================================================ |
| 65 | // StreamBuilder |
| 66 | // ============================================================ |
| 67 | |
| 68 | /** |
| 69 | * Create a StreamBuilder that can be used to open a Stream. |
| 70 | * |
| 71 | * The deviceId is initially unspecified, meaning that the current default device will be used. |
| 72 | * |
| 73 | * The default direction is AAUDIO_DIRECTION_OUTPUT. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 74 | * The default sharing mode is AAUDIO_SHARING_MODE_SHARED. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 75 | * The data format, samplesPerFrames and sampleRate are unspecified and will be |
| 76 | * chosen by the device when it is opened. |
| 77 | * |
| 78 | * AAudioStreamBuilder_delete() must be called when you are done using the builder. |
| 79 | */ |
| 80 | AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder *builder); |
| 81 | |
| 82 | /** |
| 83 | * Request an audio device identified device using an ID. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 84 | * On Android, for example, the ID could be obtained from the Java AudioManager. |
| 85 | * |
| 86 | * By default, the primary device will be used. |
| 87 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 88 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 89 | * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 90 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 91 | AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder builder, |
| 92 | int32_t deviceId); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Request a sample rate in Hz. |
| 96 | * The stream may be opened with a different sample rate. |
| 97 | * So the application should query for the actual rate after the stream is opened. |
| 98 | * |
| 99 | * Technically, this should be called the "frame rate" or "frames per second", |
| 100 | * because it refers to the number of complete frames transferred per second. |
| 101 | * But it is traditionally called "sample rate". Se we use that term. |
| 102 | * |
| 103 | * Default is AAUDIO_UNSPECIFIED. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 104 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 105 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 106 | AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder builder, |
| 107 | int32_t sampleRate); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Request a number of samples per frame. |
| 111 | * The stream may be opened with a different value. |
| 112 | * So the application should query for the actual value after the stream is opened. |
| 113 | * |
| 114 | * Default is AAUDIO_UNSPECIFIED. |
| 115 | * |
| 116 | * Note, this quantity is sometimes referred to as "channel count". |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 117 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 118 | AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder builder, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 119 | int32_t samplesPerFrame); |
| 120 | |
| 121 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 122 | * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16. |
| 123 | * The application should query for the actual format after the stream is opened. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 124 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 125 | AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder builder, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 126 | aaudio_audio_format_t format); |
| 127 | |
| 128 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 129 | * Request a mode for sharing the device. |
| 130 | * The requested sharing mode may not be available. |
| 131 | * So the application should query for the actual mode after the stream is opened. |
| 132 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 133 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 134 | * @param sharingMode AAUDIO_SHARING_MODE_LEGACY or AAUDIO_SHARING_MODE_EXCLUSIVE |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 135 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 136 | AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder builder, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 137 | aaudio_sharing_mode_t sharingMode); |
| 138 | |
| 139 | /** |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 140 | * Request the direction for a stream. The default is AAUDIO_DIRECTION_OUTPUT. |
| 141 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 142 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 143 | * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 144 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 145 | AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder builder, |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 146 | aaudio_direction_t direction); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 149 | * Set the requested maximum buffer capacity in frames. |
| 150 | * The final AAudioStream capacity may differ, but will probably be at least this big. |
| 151 | * |
| 152 | * Default is AAUDIO_UNSPECIFIED. |
| 153 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 154 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 155 | * @param frames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 156 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 157 | AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder builder, |
| 158 | int32_t frames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Open a stream based on the options in the StreamBuilder. |
| 162 | * |
| 163 | * AAudioStream_close must be called when finished with the stream to recover |
| 164 | * the memory and to free the associated resources. |
| 165 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 166 | * @param builder reference provided by AAudio_createStreamBuilder() |
| 167 | * @param stream pointer to a variable to receive the new stream reference |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 168 | * @return AAUDIO_OK or a negative error. |
| 169 | */ |
| 170 | AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder builder, |
| 171 | AAudioStream *stream); |
| 172 | |
| 173 | /** |
| 174 | * Delete the resources associated with the StreamBuilder. |
| 175 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 176 | * @param builder reference provided by AAudio_createStreamBuilder() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | * @return AAUDIO_OK or a negative error. |
| 178 | */ |
| 179 | AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder builder); |
| 180 | |
| 181 | // ============================================================ |
| 182 | // Stream Control |
| 183 | // ============================================================ |
| 184 | |
| 185 | /** |
| 186 | * Free the resources associated with a stream created by AAudioStreamBuilder_openStream() |
| 187 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 188 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 189 | * @return AAUDIO_OK or a negative error. |
| 190 | */ |
| 191 | AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream stream); |
| 192 | |
| 193 | /** |
| 194 | * Asynchronously request to start playing the stream. For output streams, one should |
| 195 | * write to the stream to fill the buffer before starting. |
| 196 | * Otherwise it will underflow. |
| 197 | * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED. |
| 198 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 199 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 200 | * @return AAUDIO_OK or a negative error. |
| 201 | */ |
| 202 | AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream stream); |
| 203 | |
| 204 | /** |
| 205 | * Asynchronous request for the stream to pause. |
| 206 | * Pausing a stream will freeze the data flow but not flush any buffers. |
| 207 | * Use AAudioStream_Start() to resume playback after a pause. |
| 208 | * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED. |
| 209 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 210 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 | * @return AAUDIO_OK or a negative error. |
| 212 | */ |
| 213 | AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream stream); |
| 214 | |
| 215 | /** |
| 216 | * Asynchronous request for the stream to flush. |
| 217 | * Flushing will discard any pending data. |
| 218 | * This call only works if the stream is pausing or paused. TODO review |
| 219 | * Frame counters are not reset by a flush. They may be advanced. |
| 220 | * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED. |
| 221 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 222 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 223 | * @return AAUDIO_OK or a negative error. |
| 224 | */ |
| 225 | AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream stream); |
| 226 | |
| 227 | /** |
| 228 | * Asynchronous request for the stream to stop. |
| 229 | * The stream will stop after all of the data currently buffered has been played. |
| 230 | * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED. |
| 231 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 232 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 233 | * @return AAUDIO_OK or a negative error. |
| 234 | */ |
| 235 | AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream stream); |
| 236 | |
| 237 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 238 | * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 239 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 240 | * This function will immediately return the state without updating the state. |
| 241 | * If you want to update the client state based on the server state then |
| 242 | * call AAudioStream_waitForStateChange() with currentState |
| 243 | * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout. |
| 244 | * |
| 245 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 246 | * @param state pointer to a variable that will be set to the current state |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 247 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 248 | AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * Wait until the current state no longer matches the input state. |
| 252 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 253 | * This will update the current client state. |
| 254 | * |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 255 | * <pre><code> |
| 256 | * aaudio_stream_state_t currentState; |
| 257 | * aaudio_result_t result = AAudioStream_getState(stream, ¤tState); |
| 258 | * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) { |
| 259 | * result = AAudioStream_waitForStateChange( |
| 260 | * stream, currentState, ¤tState, MY_TIMEOUT_NANOS); |
| 261 | * } |
| 262 | * </code></pre> |
| 263 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 264 | * @param stream A reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 265 | * @param inputState The state we want to avoid. |
| 266 | * @param nextState Pointer to a variable that will be set to the new state. |
| 267 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
| 268 | * @return AAUDIO_OK or a negative error. |
| 269 | */ |
| 270 | AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream stream, |
| 271 | aaudio_stream_state_t inputState, |
| 272 | aaudio_stream_state_t *nextState, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 273 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 274 | |
| 275 | // ============================================================ |
| 276 | // Stream I/O |
| 277 | // ============================================================ |
| 278 | |
| 279 | /** |
| 280 | * Read data from the stream. |
| 281 | * |
| 282 | * The call will wait until the read is complete or until it runs out of time. |
| 283 | * If timeoutNanos is zero then this call will not wait. |
| 284 | * |
| 285 | * Note that timeoutNanoseconds is a relative duration in wall clock time. |
| 286 | * Time will not stop if the thread is asleep. |
| 287 | * So it will be implemented using CLOCK_BOOTTIME. |
| 288 | * |
| 289 | * This call is "strong non-blocking" unless it has to wait for data. |
| 290 | * |
| 291 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 292 | * @param buffer The address of the first sample. |
| 293 | * @param numFrames Number of frames to read. Only complete frames will be written. |
| 294 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 295 | * @return The number of frames actually read or a negative error. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 296 | */ |
| 297 | AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream stream, |
| 298 | void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 299 | int32_t numFrames, |
| 300 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Write data to the stream. |
| 304 | * |
| 305 | * The call will wait until the write is complete or until it runs out of time. |
| 306 | * If timeoutNanos is zero then this call will not wait. |
| 307 | * |
| 308 | * Note that timeoutNanoseconds is a relative duration in wall clock time. |
| 309 | * Time will not stop if the thread is asleep. |
| 310 | * So it will be implemented using CLOCK_BOOTTIME. |
| 311 | * |
| 312 | * This call is "strong non-blocking" unless it has to wait for room in the buffer. |
| 313 | * |
| 314 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 315 | * @param buffer The address of the first sample. |
| 316 | * @param numFrames Number of frames to write. Only complete frames will be written. |
| 317 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
| 318 | * @return The number of frames actually written or a negative error. |
| 319 | */ |
| 320 | AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream stream, |
| 321 | const void *buffer, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 322 | int32_t numFrames, |
| 323 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 324 | |
| 325 | |
| 326 | // ============================================================ |
| 327 | // High priority audio threads |
| 328 | // ============================================================ |
| 329 | |
| 330 | typedef void *(aaudio_audio_thread_proc_t)(void *); |
| 331 | |
| 332 | /** |
| 333 | * Create a thread associated with a stream. The thread has special properties for |
| 334 | * low latency audio performance. This thread can be used to implement a callback API. |
| 335 | * |
| 336 | * Only one thread may be associated with a stream. |
| 337 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 338 | * If you are using multiple streams then we recommend that you only do |
| 339 | * blocking reads or writes on one stream. You can do non-blocking I/O on the |
| 340 | * other streams by setting the timeout to zero. |
| 341 | * This thread should be created for the stream that you will block on. |
| 342 | * |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 343 | * Note that this API is in flux. |
| 344 | * |
| 345 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 346 | * @param periodNanoseconds the estimated period at which the audio thread will need to wake up |
Glenn Kasten | f26ad10 | 2017-01-12 09:14:45 -0800 | [diff] [blame] | 347 | * @param threadProc your thread entry point |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 348 | * @param arg an argument that will be passed to your thread entry point |
| 349 | * @return AAUDIO_OK or a negative error. |
| 350 | */ |
| 351 | AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream stream, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 352 | int64_t periodNanoseconds, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 353 | aaudio_audio_thread_proc_t *threadProc, |
| 354 | void *arg); |
| 355 | |
| 356 | /** |
| 357 | * Wait until the thread exits or an error occurs. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 358 | * |
| 359 | * @param stream A stream created using AAudioStreamBuilder_openStream(). |
| 360 | * @param returnArg a pointer to a variable to receive the return value |
| 361 | * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion. |
| 362 | * @return AAUDIO_OK or a negative error. |
| 363 | */ |
| 364 | AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream stream, |
| 365 | void **returnArg, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 366 | int64_t timeoutNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 367 | |
| 368 | // ============================================================ |
| 369 | // Stream - queries |
| 370 | // ============================================================ |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * This can be used to adjust the latency of the buffer by changing |
| 375 | * the threshold where blocking will occur. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 376 | * By combining this with AAudioStream_getXRunCount(), the latency can be tuned |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 377 | * at run-time for each device. |
| 378 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 379 | * This cannot be set higher than AAudioStream_getBufferCapacityInFrames(). |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 380 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 381 | * Note that you will probably not get the exact size you request. |
| 382 | * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is. |
| 383 | * |
| 384 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 385 | * @param requestedFrames requested number of frames that can be filled without blocking |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 386 | * @return actual buffer size in frames or a negative error |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 387 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 388 | AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream stream, |
| 389 | int32_t requestedFrames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Query the maximum number of frames that can be filled without blocking. |
| 393 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 394 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 395 | * @return buffer size in frames. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 396 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 397 | AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 398 | |
| 399 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 400 | * Query the number of frames that the application should read or write at |
| 401 | * one time for optimal performance. It is OK if an application writes |
| 402 | * a different number of frames. But the buffer size may need to be larger |
| 403 | * in order to avoid underruns or overruns. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 404 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 405 | * Note that this may or may not match the actual device burst size. |
| 406 | * For some endpoints, the burst size can vary dynamically. |
| 407 | * But these tend to be devices with high latency. |
| 408 | * |
| 409 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 410 | * @return burst size |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 411 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 412 | AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * Query maximum buffer capacity in frames. |
| 416 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 417 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 418 | * @return the buffer capacity in frames |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 419 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 420 | AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 421 | |
| 422 | /** |
| 423 | * An XRun is an Underrun or an Overrun. |
| 424 | * During playing, an underrun will occur if the stream is not written in time |
| 425 | * and the system runs out of valid data. |
| 426 | * During recording, an overrun will occur if the stream is not read in time |
| 427 | * and there is no place to put the incoming data so it is discarded. |
| 428 | * |
| 429 | * An underrun or overrun can cause an audible "pop" or "glitch". |
| 430 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 431 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 432 | * @return the underrun or overrun count |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 433 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 434 | AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 435 | |
| 436 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 437 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 438 | * @return actual sample rate |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 439 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 440 | AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * The samplesPerFrame is also known as channelCount. |
| 444 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 445 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 446 | * @return actual samples per frame |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 447 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 448 | AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 449 | |
| 450 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 451 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 452 | * @param deviceId pointer to variable to receive the actual device ID |
| 453 | * @return AAUDIO_OK or a negative error. |
| 454 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 455 | AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream stream, |
| 456 | int32_t *deviceId); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 457 | |
| 458 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 459 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 460 | * @return actual data format |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 461 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 462 | AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * Provide actual sharing mode. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 466 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 467 | * @return actual sharing mode |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 468 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 469 | AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 470 | |
| 471 | /** |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 472 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 473 | * @return direction |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 474 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 475 | AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 476 | |
| 477 | /** |
| 478 | * Passes back the number of frames that have been written since the stream was created. |
| 479 | * For an output stream, this will be advanced by the application calling write(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 480 | * For an input stream, this will be advanced by the endpoint. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 481 | * |
| 482 | * The frame position is monotonically increasing. |
| 483 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 484 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 485 | * @return frames written |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 486 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 487 | AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 488 | |
| 489 | /** |
| 490 | * Passes back the number of frames that have been read since the stream was created. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 491 | * For an output stream, this will be advanced by the endpoint. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 492 | * For an input stream, this will be advanced by the application calling read(). |
| 493 | * |
| 494 | * The frame position is monotonically increasing. |
| 495 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 496 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
| 497 | * @return frames read |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 498 | */ |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 499 | AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream stream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * Passes back the time at which a particular frame was presented. |
| 503 | * This can be used to synchronize audio with video or MIDI. |
| 504 | * It can also be used to align a recorded stream with a playback stream. |
| 505 | * |
| 506 | * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED. |
| 507 | * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started. |
| 508 | * Note that because requestStart() is asynchronous, timestamps will not be valid until |
| 509 | * a short time after calling requestStart(). |
| 510 | * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error. |
| 511 | * Just try calling again later. |
| 512 | * |
| 513 | * If an error occurs, then the position and time will not be modified. |
| 514 | * |
| 515 | * The position and time passed back are monotonically increasing. |
| 516 | * |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 517 | * @param stream reference provided by AAudioStreamBuilder_openStream() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 518 | * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME |
| 519 | * @param framePosition pointer to a variable to receive the position |
| 520 | * @param timeNanoseconds pointer to a variable to receive the time |
| 521 | * @return AAUDIO_OK or a negative error |
| 522 | */ |
| 523 | AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream stream, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame^] | 524 | clockid_t clockid, |
| 525 | int64_t *framePosition, |
| 526 | int64_t *timeNanoseconds); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 527 | |
| 528 | #ifdef __cplusplus |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | #endif //AAUDIO_AAUDIO_H |