blob: 25ad5f8ef976c33b8670e4217d3f984c13096829 [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/**
Phil Burka45be8b2017-04-05 14:45:48 -070018 * @addtogroup Audio
19 * @{
20 */
21
22/**
23 * @file AAudio.h
24 */
25
26/**
27 * This is the 'C' API for AAudio.
Phil Burk5ed503c2017-02-01 09:38:15 -080028 */
29#ifndef AAUDIO_AAUDIO_H
30#define AAUDIO_AAUDIO_H
31
Phil Burk3316d5e2017-02-15 11:23:01 -080032#include <time.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080033#include "AAudioDefinitions.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Phil Burke2155ef2017-02-24 13:50:29 -080039typedef struct AAudioStreamStruct AAudioStream;
40typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -080041
Phil Burk5ed503c2017-02-01 09:38:15 -080042#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -080043#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -080044#endif
45
46// ============================================================
47// Audio System
48// ============================================================
49
50/**
Phil Burk5ed503c2017-02-01 09:38:15 -080051 * The text is the ASCII symbol corresponding to the returnCode,
52 * or an English message saying the returnCode is unrecognized.
53 * This is intended for developers to use when debugging.
54 * It is not for display to users.
55 *
56 * @return pointer to a text representation of an AAudio result code.
57 */
58AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
59
60/**
61 * The text is the ASCII symbol corresponding to the stream state,
62 * or an English message saying the state is unrecognized.
63 * This is intended for developers to use when debugging.
64 * It is not for display to users.
65 *
66 * @return pointer to a text representation of an AAudio state.
67 */
68AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
69
70// ============================================================
71// StreamBuilder
72// ============================================================
73
74/**
75 * Create a StreamBuilder that can be used to open a Stream.
76 *
77 * The deviceId is initially unspecified, meaning that the current default device will be used.
78 *
79 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -080080 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -080081 * The data format, samplesPerFrames and sampleRate are unspecified and will be
82 * chosen by the device when it is opened.
83 *
84 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
85 */
Phil Burke2155ef2017-02-24 13:50:29 -080086AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -080087
88/**
89 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -080090 * On Android, for example, the ID could be obtained from the Java AudioManager.
91 *
Phil Burke057ca92017-03-28 11:31:34 -070092 * The default, if you do not call this function, is AAUDIO_DEVICE_UNSPECIFIED,
93 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -080094 *
Phil Burk3316d5e2017-02-15 11:23:01 -080095 * @param builder reference provided by AAudio_createStreamBuilder()
96 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -080097 */
Phil Burke2155ef2017-02-24 13:50:29 -080098AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -080099 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800100
101/**
Phil Burke057ca92017-03-28 11:31:34 -0700102 * Request a sample rate in Hertz.
103 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800104 * The stream may be opened with a different sample rate.
105 * So the application should query for the actual rate after the stream is opened.
106 *
107 * Technically, this should be called the "frame rate" or "frames per second",
108 * because it refers to the number of complete frames transferred per second.
Phil Burke057ca92017-03-28 11:31:34 -0700109 * But it is traditionally called "sample rate". So we use that term.
Phil Burk5ed503c2017-02-01 09:38:15 -0800110 *
Phil Burke057ca92017-03-28 11:31:34 -0700111 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
112 *
113 * @param builder reference provided by AAudio_createStreamBuilder()
114 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800115 */
Phil Burke2155ef2017-02-24 13:50:29 -0800116AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800117 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800118
119/**
120 * Request a number of samples per frame.
Phil Burke057ca92017-03-28 11:31:34 -0700121 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800122 * The stream may be opened with a different value.
123 * So the application should query for the actual value after the stream is opened.
124 *
Phil Burke057ca92017-03-28 11:31:34 -0700125 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 *
127 * Note, this quantity is sometimes referred to as "channel count".
Phil Burke057ca92017-03-28 11:31:34 -0700128 *
129 * @param builder reference provided by AAudio_createStreamBuilder()
130 * @param samplesPerFrame Number of samples in one frame, ie. numChannels.
Phil Burk5ed503c2017-02-01 09:38:15 -0800131 */
Phil Burke2155ef2017-02-24 13:50:29 -0800132AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800133 int32_t samplesPerFrame);
134
135/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800136 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700137 *
138 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
139 *
140 * The stream may be opened with a different value.
141 * So the application should query for the actual value after the stream is opened.
142 *
143 * @param builder reference provided by AAudio_createStreamBuilder()
144 * @param format Most common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800145 */
Phil Burke2155ef2017-02-24 13:50:29 -0800146AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 aaudio_audio_format_t format);
148
149/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700151 *
152 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
153 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800154 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700155 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800156 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800157 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700158 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800159 */
Phil Burke2155ef2017-02-24 13:50:29 -0800160AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 aaudio_sharing_mode_t sharingMode);
162
163/**
Phil Burke057ca92017-03-28 11:31:34 -0700164 * Request the direction for a stream.
165 *
166 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800167 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800168 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800169 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800170 */
Phil Burke2155ef2017-02-24 13:50:29 -0800171AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800172 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800173
174/**
Phil Burke057ca92017-03-28 11:31:34 -0700175 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800176 * The final AAudioStream capacity may differ, but will probably be at least this big.
177 *
Phil Burke057ca92017-03-28 11:31:34 -0700178 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800179 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800180 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700181 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800182 */
Phil Burke2155ef2017-02-24 13:50:29 -0800183AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700184 int32_t numFrames);
185/**
186 * Return one of these values from the data callback function.
187 */
188enum {
189
190 /**
191 * Continue calling the callback.
192 */
193 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
194
195 /**
196 * Stop calling the callback.
197 *
198 * The application will still need to call AAudioStream_requestPause()
199 * or AAudioStream_requestStop().
200 */
201 AAUDIO_CALLBACK_RESULT_STOP,
202
203};
204typedef int32_t aaudio_data_callback_result_t;
205
206/**
207 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
208 *
209 * For an output stream, this function should render and write numFrames of data
210 * in the streams current data format to the audioData buffer.
211 *
212 * For an input stream, this function should read and process numFrames of data
213 * from the audioData buffer.
214 *
215 * Note that this callback function should be considered a "real-time" function.
216 * It must not do anything that could cause an unbounded delay because that can cause the
217 * audio to glitch or pop.
218 *
219 * These are things the function should NOT do:
220 * <ul>
221 * <li>allocate memory using, for example, malloc() or new</li>
222 * <li>any file operations such as opening, closing, reading or writing</li>
223 * <li>any network operations such as streaming</li>
224 * <li>use any mutexes or other synchronization primitives</li>
225 * <li>sleep</li>
226 * </ul>
227 *
228 * If you need to move data, eg. MIDI commands, in or out of the callback function then
229 * we recommend the use of non-blocking techniques such as an atomic FIFO.
230 *
231 * @param stream reference provided by AAudioStreamBuilder_openStream()
232 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
233 * @param audioData a pointer to the audio data
234 * @param numFrames the number of frames to be processed
235 * @return AAUDIO_CALLBACK_RESULT_*
236 */
237typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
238 AAudioStream *stream,
239 void *userData,
240 void *audioData,
241 int32_t numFrames);
242
243/**
244 * Request that AAudio call this functions when the stream is running.
245 *
246 * Note that when using this callback, the audio data will be passed in or out
247 * of the function as an argument.
248 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
249 * that has an active data callback.
250 *
251 * The callback function will start being called after AAudioStream_requestStart() is called.
252 * It will stop being called after AAudioStream_requestPause() or
253 * AAudioStream_requestStop() is called.
254 *
255 * This callback function will be called on a real-time thread owned by AAudio. See
256 * {@link aaudio_data_callback_proc_t} for more information.
257 *
258 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
259 *
260 * @param builder reference provided by AAudio_createStreamBuilder()
261 * @param callback pointer to a function that will process audio data.
262 * @param userData pointer to an application data structure that will be passed
263 * to the callback functions.
264 */
265AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
266 AAudioStream_dataCallback callback,
267 void *userData);
268
269/**
270 * Set the requested data callback buffer size in frames.
271 * See {@link AAudioStream_dataCallback}.
272 *
273 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
274 *
275 * For the lowest possible latency, do not call this function. AAudio will then
276 * call the dataProc callback function with whatever size is optimal.
277 * That size may vary from one callback to another.
278 *
279 * Only use this function if the application requires a specific number of frames for processing.
280 * The application might, for example, be using an FFT that requires
281 * a specific power-of-two sized buffer.
282 *
283 * AAudio may need to add additional buffering in order to adapt between the internal
284 * buffer size and the requested buffer size.
285 *
286 * If you do call this function then the requested size should be less than
287 * half the buffer capacity, to allow double buffering.
288 *
289 * @param builder reference provided by AAudio_createStreamBuilder()
290 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
291 */
292AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
293 int32_t numFrames);
294
295/**
296 * Prototype for the callback function that is passed to
297 * AAudioStreamBuilder_setErrorCallback().
298 *
299 * @param stream reference provided by AAudioStreamBuilder_openStream()
300 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
301 * @param error an AAUDIO_ERROR_* value.
302 */
303typedef void (*AAudioStream_errorCallback)(
304 AAudioStream *stream,
305 void *userData,
306 aaudio_result_t error);
307
308/**
309 * Request that AAudio call this functions if any error occurs on a callback thread.
310 *
311 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
312 * device to be unavailable.
313 * In response, this function could signal or launch another thread to reopen a
314 * stream on another device. Do not reopen the stream in this callback.
315 *
316 * This will not be called because of actions by the application, such as stopping
317 * or closing a stream.
318 *
319 * Another possible cause of error would be a timeout or an unanticipated internal error.
320 *
321 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
322 *
323 * @param builder reference provided by AAudio_createStreamBuilder()
324 * @param callback pointer to a function that will be called if an error occurs.
325 * @param userData pointer to an application data structure that will be passed
326 * to the callback functions.
327 */
328AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
329 AAudioStream_errorCallback callback,
330 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800331
332/**
333 * Open a stream based on the options in the StreamBuilder.
334 *
335 * AAudioStream_close must be called when finished with the stream to recover
336 * the memory and to free the associated resources.
337 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800338 * @param builder reference provided by AAudio_createStreamBuilder()
339 * @param stream pointer to a variable to receive the new stream reference
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_openStream(AAudioStreamBuilder* builder,
343 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800344
345/**
346 * Delete the resources associated with the StreamBuilder.
347 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800348 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 * @return AAUDIO_OK or a negative error.
350 */
Phil Burke2155ef2017-02-24 13:50:29 -0800351AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800352
353// ============================================================
354// Stream Control
355// ============================================================
356
357/**
358 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
359 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800360 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800361 * @return AAUDIO_OK or a negative error.
362 */
Phil Burke2155ef2017-02-24 13:50:29 -0800363AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800364
365/**
366 * Asynchronously request to start playing the stream. For output streams, one should
367 * write to the stream to fill the buffer before starting.
368 * Otherwise it will underflow.
369 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
370 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800371 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800372 * @return AAUDIO_OK or a negative error.
373 */
Phil Burke2155ef2017-02-24 13:50:29 -0800374AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800375
376/**
377 * Asynchronous request for the stream to pause.
378 * Pausing a stream will freeze the data flow but not flush any buffers.
379 * Use AAudioStream_Start() to resume playback after a pause.
380 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
381 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800382 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800383 * @return AAUDIO_OK or a negative error.
384 */
Phil Burke2155ef2017-02-24 13:50:29 -0800385AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800386
387/**
388 * Asynchronous request for the stream to flush.
389 * Flushing will discard any pending data.
390 * This call only works if the stream is pausing or paused. TODO review
391 * Frame counters are not reset by a flush. They may be advanced.
392 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
393 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800394 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800395 * @return AAUDIO_OK or a negative error.
396 */
Phil Burke2155ef2017-02-24 13:50:29 -0800397AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800398
399/**
400 * Asynchronous request for the stream to stop.
401 * The stream will stop after all of the data currently buffered has been played.
402 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
403 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800404 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800405 * @return AAUDIO_OK or a negative error.
406 */
Phil Burke2155ef2017-02-24 13:50:29 -0800407AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800408
409/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800410 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800411 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800412 * This function will immediately return the state without updating the state.
413 * If you want to update the client state based on the server state then
414 * call AAudioStream_waitForStateChange() with currentState
415 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
416 *
417 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800418 * @param state pointer to a variable that will be set to the current state
Phil Burk5ed503c2017-02-01 09:38:15 -0800419 */
Phil Burke2155ef2017-02-24 13:50:29 -0800420AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800421
422/**
423 * Wait until the current state no longer matches the input state.
424 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800425 * This will update the current client state.
426 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800427 * <pre><code>
428 * aaudio_stream_state_t currentState;
429 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
430 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
431 * result = AAudioStream_waitForStateChange(
432 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
433 * }
434 * </code></pre>
435 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800436 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800437 * @param inputState The state we want to avoid.
438 * @param nextState Pointer to a variable that will be set to the new state.
439 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
440 * @return AAUDIO_OK or a negative error.
441 */
Phil Burke2155ef2017-02-24 13:50:29 -0800442AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800443 aaudio_stream_state_t inputState,
444 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800445 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800446
447// ============================================================
448// Stream I/O
449// ============================================================
450
451/**
452 * Read data from the stream.
453 *
454 * The call will wait until the read is complete or until it runs out of time.
455 * If timeoutNanos is zero then this call will not wait.
456 *
457 * Note that timeoutNanoseconds is a relative duration in wall clock time.
458 * Time will not stop if the thread is asleep.
459 * So it will be implemented using CLOCK_BOOTTIME.
460 *
461 * This call is "strong non-blocking" unless it has to wait for data.
462 *
463 * @param stream A stream created using AAudioStreamBuilder_openStream().
464 * @param buffer The address of the first sample.
465 * @param numFrames Number of frames to read. Only complete frames will be written.
466 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800467 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800468 */
Phil Burke2155ef2017-02-24 13:50:29 -0800469AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800470 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800471 int32_t numFrames,
472 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800473
474/**
475 * Write data to the stream.
476 *
477 * The call will wait until the write is complete or until it runs out of time.
478 * If timeoutNanos is zero then this call will not wait.
479 *
480 * Note that timeoutNanoseconds is a relative duration in wall clock time.
481 * Time will not stop if the thread is asleep.
482 * So it will be implemented using CLOCK_BOOTTIME.
483 *
484 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
485 *
486 * @param stream A stream created using AAudioStreamBuilder_openStream().
487 * @param buffer The address of the first sample.
488 * @param numFrames Number of frames to write. Only complete frames will be written.
489 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
490 * @return The number of frames actually written or a negative error.
491 */
Phil Burke2155ef2017-02-24 13:50:29 -0800492AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800493 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800494 int32_t numFrames,
495 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800496
497
498// ============================================================
499// High priority audio threads
500// ============================================================
501
Phil Burke057ca92017-03-28 11:31:34 -0700502/**
503 * @deprecated Use AudioStreamBuilder_setCallback()
504 */
Phil Burke2155ef2017-02-24 13:50:29 -0800505typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800506
507/**
Phil Burke057ca92017-03-28 11:31:34 -0700508 * @deprecated Use AudioStreamBuilder_setCallback()
509 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800510 * Create a thread associated with a stream. The thread has special properties for
511 * low latency audio performance. This thread can be used to implement a callback API.
512 *
513 * Only one thread may be associated with a stream.
514 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800515 * If you are using multiple streams then we recommend that you only do
516 * blocking reads or writes on one stream. You can do non-blocking I/O on the
517 * other streams by setting the timeout to zero.
518 * This thread should be created for the stream that you will block on.
519 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800520 * Note that this API is in flux.
521 *
522 * @param stream A stream created using AAudioStreamBuilder_openStream().
523 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800524 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800525 * @param arg an argument that will be passed to your thread entry point
526 * @return AAUDIO_OK or a negative error.
527 */
Phil Burke2155ef2017-02-24 13:50:29 -0800528AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800529 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800530 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800531 void *arg);
532
533/**
Phil Burke057ca92017-03-28 11:31:34 -0700534 * @deprecated Use AudioStreamBuilder_setCallback()
535 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800536 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800537 *
538 * @param stream A stream created using AAudioStreamBuilder_openStream().
539 * @param returnArg a pointer to a variable to receive the return value
540 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
541 * @return AAUDIO_OK or a negative error.
542 */
Phil Burke2155ef2017-02-24 13:50:29 -0800543AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800544 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800545 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800546
547// ============================================================
548// Stream - queries
549// ============================================================
550
551
552/**
553 * This can be used to adjust the latency of the buffer by changing
554 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800555 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800556 * at run-time for each device.
557 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800558 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800559 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800560 * Note that you will probably not get the exact size you request.
561 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
562 *
563 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700564 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800565 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800566 */
Phil Burke2155ef2017-02-24 13:50:29 -0800567AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700568 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800569
570/**
571 * Query the maximum number of frames that can be filled without blocking.
572 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800573 * @param stream reference provided by AAudioStreamBuilder_openStream()
574 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800575 */
Phil Burke2155ef2017-02-24 13:50:29 -0800576AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800577
578/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800579 * Query the number of frames that the application should read or write at
580 * one time for optimal performance. It is OK if an application writes
581 * a different number of frames. But the buffer size may need to be larger
582 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800583 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800584 * Note that this may or may not match the actual device burst size.
585 * For some endpoints, the burst size can vary dynamically.
586 * But these tend to be devices with high latency.
587 *
588 * @param stream reference provided by AAudioStreamBuilder_openStream()
589 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800590 */
Phil Burke2155ef2017-02-24 13:50:29 -0800591AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800592
593/**
594 * Query maximum buffer capacity in frames.
595 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800596 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700597 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800598 */
Phil Burke2155ef2017-02-24 13:50:29 -0800599AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800600
601/**
Phil Burke057ca92017-03-28 11:31:34 -0700602 * Query the size of the buffer that will be passed to the dataProc callback
603 * in the numFrames parameter.
604 *
605 * This call can be used if the application needs to know the value of numFrames before
606 * the stream is started. This is not normally necessary.
607 *
608 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
609 * then this will be the same size.
610 *
611 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
612 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
613 *
614 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
615 * may vary from one dataProc callback to the next.
616 *
617 * @param stream reference provided by AAudioStreamBuilder_openStream()
618 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
619 */
620AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
621
622/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800623 * An XRun is an Underrun or an Overrun.
624 * During playing, an underrun will occur if the stream is not written in time
625 * and the system runs out of valid data.
626 * During recording, an overrun will occur if the stream is not read in time
627 * and there is no place to put the incoming data so it is discarded.
628 *
629 * An underrun or overrun can cause an audible "pop" or "glitch".
630 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800631 * @param stream reference provided by AAudioStreamBuilder_openStream()
632 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800633 */
Phil Burke2155ef2017-02-24 13:50:29 -0800634AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800635
636/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800637 * @param stream reference provided by AAudioStreamBuilder_openStream()
638 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800639 */
Phil Burke2155ef2017-02-24 13:50:29 -0800640AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800641
642/**
643 * The samplesPerFrame is also known as channelCount.
644 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800645 * @param stream reference provided by AAudioStreamBuilder_openStream()
646 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800647 */
Phil Burke2155ef2017-02-24 13:50:29 -0800648AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800649
650/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800651 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800652 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800653 */
Phil Burke2155ef2017-02-24 13:50:29 -0800654AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800655
656/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800657 * @param stream reference provided by AAudioStreamBuilder_openStream()
658 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800659 */
Phil Burke2155ef2017-02-24 13:50:29 -0800660AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800661
662/**
663 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800664 * @param stream reference provided by AAudioStreamBuilder_openStream()
665 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800666 */
Phil Burke2155ef2017-02-24 13:50:29 -0800667AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800668
669/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800670 * @param stream reference provided by AAudioStreamBuilder_openStream()
671 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800672 */
Phil Burke2155ef2017-02-24 13:50:29 -0800673AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800674
675/**
676 * Passes back the number of frames that have been written since the stream was created.
677 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800678 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800679 *
680 * The frame position is monotonically increasing.
681 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800682 * @param stream reference provided by AAudioStreamBuilder_openStream()
683 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800684 */
Phil Burke2155ef2017-02-24 13:50:29 -0800685AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800686
687/**
688 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800689 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800690 * For an input stream, this will be advanced by the application calling read().
691 *
692 * The frame position is monotonically increasing.
693 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800694 * @param stream reference provided by AAudioStreamBuilder_openStream()
695 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800696 */
Phil Burke2155ef2017-02-24 13:50:29 -0800697AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800698
699/**
700 * Passes back the time at which a particular frame was presented.
701 * This can be used to synchronize audio with video or MIDI.
702 * It can also be used to align a recorded stream with a playback stream.
703 *
704 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
705 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
706 * Note that because requestStart() is asynchronous, timestamps will not be valid until
707 * a short time after calling requestStart().
708 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
709 * Just try calling again later.
710 *
711 * If an error occurs, then the position and time will not be modified.
712 *
713 * The position and time passed back are monotonically increasing.
714 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800715 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800716 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
717 * @param framePosition pointer to a variable to receive the position
718 * @param timeNanoseconds pointer to a variable to receive the time
719 * @return AAUDIO_OK or a negative error
720 */
Phil Burke2155ef2017-02-24 13:50:29 -0800721AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800722 clockid_t clockid,
723 int64_t *framePosition,
724 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800725
726#ifdef __cplusplus
727}
728#endif
729
730#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700731
732/** @} */