blob: 447d644dfa59254859f06929ab447429eef18be3 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
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 Burk3316d5e2017-02-15 11:23:01 -080023#include <time.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080024#include "AAudioDefinitions.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
Phil Burke2155ef2017-02-24 13:50:29 -080030typedef struct AAudioStreamStruct AAudioStream;
31typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -080032
Phil Burk5ed503c2017-02-01 09:38:15 -080033#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -080034#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -080035#endif
36
37// ============================================================
38// Audio System
39// ============================================================
40
41/**
Phil Burk5ed503c2017-02-01 09:38:15 -080042 * The text is the ASCII symbol corresponding to the returnCode,
43 * or an English message saying the returnCode is unrecognized.
44 * This is intended for developers to use when debugging.
45 * It is not for display to users.
46 *
47 * @return pointer to a text representation of an AAudio result code.
48 */
49AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
50
51/**
52 * The text is the ASCII symbol corresponding to the stream state,
53 * or an English message saying the state is unrecognized.
54 * This is intended for developers to use when debugging.
55 * It is not for display to users.
56 *
57 * @return pointer to a text representation of an AAudio state.
58 */
59AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
60
61// ============================================================
62// StreamBuilder
63// ============================================================
64
65/**
66 * Create a StreamBuilder that can be used to open a Stream.
67 *
68 * The deviceId is initially unspecified, meaning that the current default device will be used.
69 *
70 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -080071 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -080072 * The data format, samplesPerFrames and sampleRate are unspecified and will be
73 * chosen by the device when it is opened.
74 *
75 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
76 */
Phil Burke2155ef2017-02-24 13:50:29 -080077AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -080078
79/**
80 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -080081 * On Android, for example, the ID could be obtained from the Java AudioManager.
82 *
Phil Burk2ba6e662017-03-28 11:31:34 -070083 * The default, if you do not call this function, is AAUDIO_DEVICE_UNSPECIFIED,
84 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -080085 *
Phil Burk3316d5e2017-02-15 11:23:01 -080086 * @param builder reference provided by AAudio_createStreamBuilder()
87 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -080088 */
Phil Burke2155ef2017-02-24 13:50:29 -080089AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -080090 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -080091
92/**
Phil Burk2ba6e662017-03-28 11:31:34 -070093 * Request a sample rate in Hertz.
94 *
Phil Burk5ed503c2017-02-01 09:38:15 -080095 * The stream may be opened with a different sample rate.
96 * So the application should query for the actual rate after the stream is opened.
97 *
98 * Technically, this should be called the "frame rate" or "frames per second",
99 * because it refers to the number of complete frames transferred per second.
Phil Burk2ba6e662017-03-28 11:31:34 -0700100 * But it is traditionally called "sample rate". So we use that term.
Phil Burk5ed503c2017-02-01 09:38:15 -0800101 *
Phil Burk2ba6e662017-03-28 11:31:34 -0700102 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
103 *
104 * @param builder reference provided by AAudio_createStreamBuilder()
105 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800106 */
Phil Burke2155ef2017-02-24 13:50:29 -0800107AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800108 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800109
110/**
111 * Request a number of samples per frame.
Phil Burk2ba6e662017-03-28 11:31:34 -0700112 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800113 * The stream may be opened with a different value.
114 * So the application should query for the actual value after the stream is opened.
115 *
Phil Burk2ba6e662017-03-28 11:31:34 -0700116 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800117 *
118 * Note, this quantity is sometimes referred to as "channel count".
Phil Burk2ba6e662017-03-28 11:31:34 -0700119 *
120 * @param builder reference provided by AAudio_createStreamBuilder()
121 * @param samplesPerFrame Number of samples in one frame, ie. numChannels.
Phil Burk5ed503c2017-02-01 09:38:15 -0800122 */
Phil Burke2155ef2017-02-24 13:50:29 -0800123AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800124 int32_t samplesPerFrame);
125
126/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800127 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burk2ba6e662017-03-28 11:31:34 -0700128 *
129 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
130 *
131 * The stream may be opened with a different value.
132 * So the application should query for the actual value after the stream is opened.
133 *
134 * @param builder reference provided by AAudio_createStreamBuilder()
135 * @param format Most common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800136 */
Phil Burke2155ef2017-02-24 13:50:29 -0800137AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 aaudio_audio_format_t format);
139
140/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 * Request a mode for sharing the device.
Phil Burk2ba6e662017-03-28 11:31:34 -0700142 *
143 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
144 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800145 * The requested sharing mode may not be available.
Phil Burk2ba6e662017-03-28 11:31:34 -0700146 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800148 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk2ba6e662017-03-28 11:31:34 -0700149 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 */
Phil Burke2155ef2017-02-24 13:50:29 -0800151AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800152 aaudio_sharing_mode_t sharingMode);
153
154/**
Phil Burk2ba6e662017-03-28 11:31:34 -0700155 * Request the direction for a stream.
156 *
157 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800158 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800159 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800160 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 */
Phil Burke2155ef2017-02-24 13:50:29 -0800162AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800163 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800164
165/**
Phil Burk2ba6e662017-03-28 11:31:34 -0700166 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800167 * The final AAudioStream capacity may differ, but will probably be at least this big.
168 *
Phil Burk2ba6e662017-03-28 11:31:34 -0700169 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800170 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800171 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk2ba6e662017-03-28 11:31:34 -0700172 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800173 */
Phil Burke2155ef2017-02-24 13:50:29 -0800174AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burk2ba6e662017-03-28 11:31:34 -0700175 int32_t numFrames);
176/**
177 * Return one of these values from the data callback function.
178 */
179enum {
180
181 /**
182 * Continue calling the callback.
183 */
184 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
185
186 /**
187 * Stop calling the callback.
188 *
189 * The application will still need to call AAudioStream_requestPause()
190 * or AAudioStream_requestStop().
191 */
192 AAUDIO_CALLBACK_RESULT_STOP,
193
194};
195typedef int32_t aaudio_data_callback_result_t;
196
197/**
198 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
199 *
200 * For an output stream, this function should render and write numFrames of data
201 * in the streams current data format to the audioData buffer.
202 *
203 * For an input stream, this function should read and process numFrames of data
204 * from the audioData buffer.
205 *
206 * Note that this callback function should be considered a "real-time" function.
207 * It must not do anything that could cause an unbounded delay because that can cause the
208 * audio to glitch or pop.
209 *
210 * These are things the function should NOT do:
211 * <ul>
212 * <li>allocate memory using, for example, malloc() or new</li>
213 * <li>any file operations such as opening, closing, reading or writing</li>
214 * <li>any network operations such as streaming</li>
215 * <li>use any mutexes or other synchronization primitives</li>
216 * <li>sleep</li>
217 * </ul>
218 *
219 * If you need to move data, eg. MIDI commands, in or out of the callback function then
220 * we recommend the use of non-blocking techniques such as an atomic FIFO.
221 *
222 * @param stream reference provided by AAudioStreamBuilder_openStream()
223 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
224 * @param audioData a pointer to the audio data
225 * @param numFrames the number of frames to be processed
226 * @return AAUDIO_CALLBACK_RESULT_*
227 */
228typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
229 AAudioStream *stream,
230 void *userData,
231 void *audioData,
232 int32_t numFrames);
233
234/**
235 * Request that AAudio call this functions when the stream is running.
236 *
237 * Note that when using this callback, the audio data will be passed in or out
238 * of the function as an argument.
239 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
240 * that has an active data callback.
241 *
242 * The callback function will start being called after AAudioStream_requestStart() is called.
243 * It will stop being called after AAudioStream_requestPause() or
244 * AAudioStream_requestStop() is called.
245 *
246 * This callback function will be called on a real-time thread owned by AAudio. See
247 * {@link aaudio_data_callback_proc_t} for more information.
248 *
249 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
250 *
251 * @param builder reference provided by AAudio_createStreamBuilder()
252 * @param callback pointer to a function that will process audio data.
253 * @param userData pointer to an application data structure that will be passed
254 * to the callback functions.
255 */
256AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
257 AAudioStream_dataCallback callback,
258 void *userData);
259
260/**
261 * Set the requested data callback buffer size in frames.
262 * See {@link AAudioStream_dataCallback}.
263 *
264 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
265 *
266 * For the lowest possible latency, do not call this function. AAudio will then
267 * call the dataProc callback function with whatever size is optimal.
268 * That size may vary from one callback to another.
269 *
270 * Only use this function if the application requires a specific number of frames for processing.
271 * The application might, for example, be using an FFT that requires
272 * a specific power-of-two sized buffer.
273 *
274 * AAudio may need to add additional buffering in order to adapt between the internal
275 * buffer size and the requested buffer size.
276 *
277 * If you do call this function then the requested size should be less than
278 * half the buffer capacity, to allow double buffering.
279 *
280 * @param builder reference provided by AAudio_createStreamBuilder()
281 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
282 */
283AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
284 int32_t numFrames);
285
286/**
287 * Prototype for the callback function that is passed to
288 * AAudioStreamBuilder_setErrorCallback().
289 *
290 * @param stream reference provided by AAudioStreamBuilder_openStream()
291 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
292 * @param error an AAUDIO_ERROR_* value.
293 */
294typedef void (*AAudioStream_errorCallback)(
295 AAudioStream *stream,
296 void *userData,
297 aaudio_result_t error);
298
299/**
300 * Request that AAudio call this functions if any error occurs on a callback thread.
301 *
302 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
303 * device to be unavailable.
304 * In response, this function could signal or launch another thread to reopen a
305 * stream on another device. Do not reopen the stream in this callback.
306 *
307 * This will not be called because of actions by the application, such as stopping
308 * or closing a stream.
309 *
310 * Another possible cause of error would be a timeout or an unanticipated internal error.
311 *
312 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
313 *
314 * @param builder reference provided by AAudio_createStreamBuilder()
315 * @param callback pointer to a function that will be called if an error occurs.
316 * @param userData pointer to an application data structure that will be passed
317 * to the callback functions.
318 */
319AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
320 AAudioStream_errorCallback callback,
321 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800322
323/**
324 * Open a stream based on the options in the StreamBuilder.
325 *
326 * AAudioStream_close must be called when finished with the stream to recover
327 * the memory and to free the associated resources.
328 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800329 * @param builder reference provided by AAudio_createStreamBuilder()
330 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800331 * @return AAUDIO_OK or a negative error.
332 */
Phil Burke2155ef2017-02-24 13:50:29 -0800333AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
334 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800335
336/**
337 * Delete the resources associated with the StreamBuilder.
338 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800339 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800340 * @return AAUDIO_OK or a negative error.
341 */
Phil Burke2155ef2017-02-24 13:50:29 -0800342AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800343
344// ============================================================
345// Stream Control
346// ============================================================
347
348/**
349 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
350 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800351 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800352 * @return AAUDIO_OK or a negative error.
353 */
Phil Burke2155ef2017-02-24 13:50:29 -0800354AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800355
356/**
357 * Asynchronously request to start playing the stream. For output streams, one should
358 * write to the stream to fill the buffer before starting.
359 * Otherwise it will underflow.
360 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
361 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800362 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800363 * @return AAUDIO_OK or a negative error.
364 */
Phil Burke2155ef2017-02-24 13:50:29 -0800365AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800366
367/**
368 * Asynchronous request for the stream to pause.
369 * Pausing a stream will freeze the data flow but not flush any buffers.
370 * Use AAudioStream_Start() to resume playback after a pause.
371 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
372 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800373 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800374 * @return AAUDIO_OK or a negative error.
375 */
Phil Burke2155ef2017-02-24 13:50:29 -0800376AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800377
378/**
379 * Asynchronous request for the stream to flush.
380 * Flushing will discard any pending data.
381 * This call only works if the stream is pausing or paused. TODO review
382 * Frame counters are not reset by a flush. They may be advanced.
383 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
384 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800385 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800386 * @return AAUDIO_OK or a negative error.
387 */
Phil Burke2155ef2017-02-24 13:50:29 -0800388AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800389
390/**
391 * Asynchronous request for the stream to stop.
392 * The stream will stop after all of the data currently buffered has been played.
393 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
394 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800395 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800396 * @return AAUDIO_OK or a negative error.
397 */
Phil Burke2155ef2017-02-24 13:50:29 -0800398AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800399
400/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800401 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800402 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800403 * This function will immediately return the state without updating the state.
404 * If you want to update the client state based on the server state then
405 * call AAudioStream_waitForStateChange() with currentState
406 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
407 *
408 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800409 * @param state pointer to a variable that will be set to the current state
Phil Burk5ed503c2017-02-01 09:38:15 -0800410 */
Phil Burke2155ef2017-02-24 13:50:29 -0800411AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800412
413/**
414 * Wait until the current state no longer matches the input state.
415 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800416 * This will update the current client state.
417 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800418 * <pre><code>
419 * aaudio_stream_state_t currentState;
420 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
421 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
422 * result = AAudioStream_waitForStateChange(
423 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
424 * }
425 * </code></pre>
426 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800427 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800428 * @param inputState The state we want to avoid.
429 * @param nextState Pointer to a variable that will be set to the new state.
430 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
431 * @return AAUDIO_OK or a negative error.
432 */
Phil Burke2155ef2017-02-24 13:50:29 -0800433AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800434 aaudio_stream_state_t inputState,
435 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800436 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800437
438// ============================================================
439// Stream I/O
440// ============================================================
441
442/**
443 * Read data from the stream.
444 *
445 * The call will wait until the read is complete or until it runs out of time.
446 * If timeoutNanos is zero then this call will not wait.
447 *
448 * Note that timeoutNanoseconds is a relative duration in wall clock time.
449 * Time will not stop if the thread is asleep.
450 * So it will be implemented using CLOCK_BOOTTIME.
451 *
452 * This call is "strong non-blocking" unless it has to wait for data.
453 *
454 * @param stream A stream created using AAudioStreamBuilder_openStream().
455 * @param buffer The address of the first sample.
456 * @param numFrames Number of frames to read. Only complete frames will be written.
457 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800458 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800459 */
Phil Burke2155ef2017-02-24 13:50:29 -0800460AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800461 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800462 int32_t numFrames,
463 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800464
465/**
466 * Write data to the stream.
467 *
468 * The call will wait until the write is complete or until it runs out of time.
469 * If timeoutNanos is zero then this call will not wait.
470 *
471 * Note that timeoutNanoseconds is a relative duration in wall clock time.
472 * Time will not stop if the thread is asleep.
473 * So it will be implemented using CLOCK_BOOTTIME.
474 *
475 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
476 *
477 * @param stream A stream created using AAudioStreamBuilder_openStream().
478 * @param buffer The address of the first sample.
479 * @param numFrames Number of frames to write. Only complete frames will be written.
480 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
481 * @return The number of frames actually written or a negative error.
482 */
Phil Burke2155ef2017-02-24 13:50:29 -0800483AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800484 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800485 int32_t numFrames,
486 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800487
488
489// ============================================================
490// High priority audio threads
491// ============================================================
492
Phil Burk2ba6e662017-03-28 11:31:34 -0700493/**
494 * @deprecated Use AudioStreamBuilder_setCallback()
495 */
Phil Burke2155ef2017-02-24 13:50:29 -0800496typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800497
498/**
Phil Burk2ba6e662017-03-28 11:31:34 -0700499 * @deprecated Use AudioStreamBuilder_setCallback()
500 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800501 * Create a thread associated with a stream. The thread has special properties for
502 * low latency audio performance. This thread can be used to implement a callback API.
503 *
504 * Only one thread may be associated with a stream.
505 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800506 * If you are using multiple streams then we recommend that you only do
507 * blocking reads or writes on one stream. You can do non-blocking I/O on the
508 * other streams by setting the timeout to zero.
509 * This thread should be created for the stream that you will block on.
510 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800511 * Note that this API is in flux.
512 *
513 * @param stream A stream created using AAudioStreamBuilder_openStream().
514 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800515 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800516 * @param arg an argument that will be passed to your thread entry point
517 * @return AAUDIO_OK or a negative error.
518 */
Phil Burke2155ef2017-02-24 13:50:29 -0800519AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800520 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800521 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800522 void *arg);
523
524/**
Phil Burk2ba6e662017-03-28 11:31:34 -0700525 * @deprecated Use AudioStreamBuilder_setCallback()
526 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800527 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800528 *
529 * @param stream A stream created using AAudioStreamBuilder_openStream().
530 * @param returnArg a pointer to a variable to receive the return value
531 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
532 * @return AAUDIO_OK or a negative error.
533 */
Phil Burke2155ef2017-02-24 13:50:29 -0800534AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800535 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800536 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800537
538// ============================================================
539// Stream - queries
540// ============================================================
541
542
543/**
544 * This can be used to adjust the latency of the buffer by changing
545 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800546 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800547 * at run-time for each device.
548 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800549 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800550 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800551 * Note that you will probably not get the exact size you request.
552 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
553 *
554 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk2ba6e662017-03-28 11:31:34 -0700555 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800556 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800557 */
Phil Burke2155ef2017-02-24 13:50:29 -0800558AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burk2ba6e662017-03-28 11:31:34 -0700559 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800560
561/**
562 * Query the maximum number of frames that can be filled without blocking.
563 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800564 * @param stream reference provided by AAudioStreamBuilder_openStream()
565 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800566 */
Phil Burke2155ef2017-02-24 13:50:29 -0800567AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800568
569/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800570 * Query the number of frames that the application should read or write at
571 * one time for optimal performance. It is OK if an application writes
572 * a different number of frames. But the buffer size may need to be larger
573 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800574 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800575 * Note that this may or may not match the actual device burst size.
576 * For some endpoints, the burst size can vary dynamically.
577 * But these tend to be devices with high latency.
578 *
579 * @param stream reference provided by AAudioStreamBuilder_openStream()
580 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800581 */
Phil Burke2155ef2017-02-24 13:50:29 -0800582AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800583
584/**
585 * Query maximum buffer capacity in frames.
586 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800587 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk2ba6e662017-03-28 11:31:34 -0700588 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800589 */
Phil Burke2155ef2017-02-24 13:50:29 -0800590AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800591
592/**
Phil Burk2ba6e662017-03-28 11:31:34 -0700593 * Query the size of the buffer that will be passed to the dataProc callback
594 * in the numFrames parameter.
595 *
596 * This call can be used if the application needs to know the value of numFrames before
597 * the stream is started. This is not normally necessary.
598 *
599 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
600 * then this will be the same size.
601 *
602 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
603 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
604 *
605 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
606 * may vary from one dataProc callback to the next.
607 *
608 * @param stream reference provided by AAudioStreamBuilder_openStream()
609 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
610 */
611AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
612
613/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800614 * An XRun is an Underrun or an Overrun.
615 * During playing, an underrun will occur if the stream is not written in time
616 * and the system runs out of valid data.
617 * During recording, an overrun will occur if the stream is not read in time
618 * and there is no place to put the incoming data so it is discarded.
619 *
620 * An underrun or overrun can cause an audible "pop" or "glitch".
621 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800622 * @param stream reference provided by AAudioStreamBuilder_openStream()
623 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800624 */
Phil Burke2155ef2017-02-24 13:50:29 -0800625AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800626
627/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800628 * @param stream reference provided by AAudioStreamBuilder_openStream()
629 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800630 */
Phil Burke2155ef2017-02-24 13:50:29 -0800631AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800632
633/**
634 * The samplesPerFrame is also known as channelCount.
635 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800636 * @param stream reference provided by AAudioStreamBuilder_openStream()
637 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800638 */
Phil Burke2155ef2017-02-24 13:50:29 -0800639AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800640
641/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800642 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800643 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800644 */
Phil Burke2155ef2017-02-24 13:50:29 -0800645AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800646
647/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800648 * @param stream reference provided by AAudioStreamBuilder_openStream()
649 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800650 */
Phil Burke2155ef2017-02-24 13:50:29 -0800651AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800652
653/**
654 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800655 * @param stream reference provided by AAudioStreamBuilder_openStream()
656 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800657 */
Phil Burke2155ef2017-02-24 13:50:29 -0800658AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800659
660/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800661 * @param stream reference provided by AAudioStreamBuilder_openStream()
662 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800663 */
Phil Burke2155ef2017-02-24 13:50:29 -0800664AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800665
666/**
667 * Passes back the number of frames that have been written since the stream was created.
668 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800669 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800670 *
671 * The frame position is monotonically increasing.
672 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800673 * @param stream reference provided by AAudioStreamBuilder_openStream()
674 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800675 */
Phil Burke2155ef2017-02-24 13:50:29 -0800676AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800677
678/**
679 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800680 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800681 * For an input stream, this will be advanced by the application calling read().
682 *
683 * The frame position is monotonically increasing.
684 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800685 * @param stream reference provided by AAudioStreamBuilder_openStream()
686 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800687 */
Phil Burke2155ef2017-02-24 13:50:29 -0800688AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800689
690/**
691 * Passes back the time at which a particular frame was presented.
692 * This can be used to synchronize audio with video or MIDI.
693 * It can also be used to align a recorded stream with a playback stream.
694 *
695 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
696 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
697 * Note that because requestStart() is asynchronous, timestamps will not be valid until
698 * a short time after calling requestStart().
699 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
700 * Just try calling again later.
701 *
702 * If an error occurs, then the position and time will not be modified.
703 *
704 * The position and time passed back are monotonically increasing.
705 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800706 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800707 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
708 * @param framePosition pointer to a variable to receive the position
709 * @param timeNanoseconds pointer to a variable to receive the time
710 * @return AAUDIO_OK or a negative error
711 */
Phil Burke2155ef2017-02-24 13:50:29 -0800712AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800713 clockid_t clockid,
714 int64_t *framePosition,
715 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800716
717#ifdef __cplusplus
718}
719#endif
720
721#endif //AAUDIO_AAUDIO_H