blob: 4a39f18cdd59ffa06e0a5e7aeb278c151d66912e [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
34#ifdef __cplusplus
35extern "C" {
36#endif
37
Phil Burka4eb0d82017-04-12 15:44:06 -070038/**
39 * This is used to represent a value that has not been specified.
40 * For example, an application could use AAUDIO_UNSPECIFIED to indicate
41 * that is did not not care what the specific value of a parameter was
42 * and would accept whatever it was given.
43 */
44#define AAUDIO_UNSPECIFIED 0
45#define AAUDIO_DEVICE_UNSPECIFIED 0
46
47enum {
48 AAUDIO_DIRECTION_OUTPUT,
49 AAUDIO_DIRECTION_INPUT
50};
51typedef int32_t aaudio_direction_t;
52
53enum {
54 AAUDIO_FORMAT_INVALID = -1,
55 AAUDIO_FORMAT_UNSPECIFIED = 0,
56 AAUDIO_FORMAT_PCM_I16,
Phil Burk74733452017-04-18 19:50:28 -070057 AAUDIO_FORMAT_PCM_FLOAT
Phil Burka4eb0d82017-04-12 15:44:06 -070058};
59typedef int32_t aaudio_format_t;
60
61/**
62 * @deprecated use aaudio_format_t instead
63 * TODO remove when tests and examples are updated
64 */
65typedef int32_t aaudio_audio_format_t;
66
67enum {
68 AAUDIO_OK,
69 AAUDIO_ERROR_BASE = -900, // TODO review
70 AAUDIO_ERROR_DISCONNECTED,
71 AAUDIO_ERROR_ILLEGAL_ARGUMENT,
72 AAUDIO_ERROR_INCOMPATIBLE,
73 AAUDIO_ERROR_INTERNAL, // an underlying API returned an error code
74 AAUDIO_ERROR_INVALID_STATE,
75 AAUDIO_ERROR_UNEXPECTED_STATE,
76 AAUDIO_ERROR_UNEXPECTED_VALUE,
77 AAUDIO_ERROR_INVALID_HANDLE,
78 AAUDIO_ERROR_INVALID_QUERY,
79 AAUDIO_ERROR_UNIMPLEMENTED,
80 AAUDIO_ERROR_UNAVAILABLE,
81 AAUDIO_ERROR_NO_FREE_HANDLES,
82 AAUDIO_ERROR_NO_MEMORY,
83 AAUDIO_ERROR_NULL,
84 AAUDIO_ERROR_TIMEOUT,
85 AAUDIO_ERROR_WOULD_BLOCK,
86 AAUDIO_ERROR_INVALID_FORMAT,
87 AAUDIO_ERROR_OUT_OF_RANGE,
88 AAUDIO_ERROR_NO_SERVICE,
89 AAUDIO_ERROR_INVALID_RATE
90};
91typedef int32_t aaudio_result_t;
92
93enum
94{
95 AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
96 AAUDIO_STREAM_STATE_UNKNOWN,
97 AAUDIO_STREAM_STATE_OPEN,
98 AAUDIO_STREAM_STATE_STARTING,
99 AAUDIO_STREAM_STATE_STARTED,
100 AAUDIO_STREAM_STATE_PAUSING,
101 AAUDIO_STREAM_STATE_PAUSED,
102 AAUDIO_STREAM_STATE_FLUSHING,
103 AAUDIO_STREAM_STATE_FLUSHED,
104 AAUDIO_STREAM_STATE_STOPPING,
105 AAUDIO_STREAM_STATE_STOPPED,
106 AAUDIO_STREAM_STATE_CLOSING,
107 AAUDIO_STREAM_STATE_CLOSED,
108 AAUDIO_STREAM_STATE_DISCONNECTED
109};
110typedef int32_t aaudio_stream_state_t;
111
112
113enum {
114 /**
115 * This will be the only stream using a particular source or sink.
116 * This mode will provide the lowest possible latency.
117 * You should close EXCLUSIVE streams immediately when you are not using them.
118 */
119 AAUDIO_SHARING_MODE_EXCLUSIVE,
120 /**
121 * Multiple applications will be mixed by the AAudio Server.
122 * This will have higher latency than the EXCLUSIVE mode.
123 */
124 AAUDIO_SHARING_MODE_SHARED
125};
126typedef int32_t aaudio_sharing_mode_t;
127
Phil Burke2155ef2017-02-24 13:50:29 -0800128typedef struct AAudioStreamStruct AAudioStream;
129typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800130
Phil Burk5ed503c2017-02-01 09:38:15 -0800131#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800132#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800133#endif
134
135// ============================================================
136// Audio System
137// ============================================================
138
139/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 * The text is the ASCII symbol corresponding to the returnCode,
141 * or an English message saying the returnCode is unrecognized.
142 * This is intended for developers to use when debugging.
143 * It is not for display to users.
144 *
145 * @return pointer to a text representation of an AAudio result code.
146 */
147AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
148
149/**
150 * The text is the ASCII symbol corresponding to the stream state,
151 * or an English message saying the state is unrecognized.
152 * This is intended for developers to use when debugging.
153 * It is not for display to users.
154 *
155 * @return pointer to a text representation of an AAudio state.
156 */
157AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
158
159// ============================================================
160// StreamBuilder
161// ============================================================
162
163/**
164 * Create a StreamBuilder that can be used to open a Stream.
165 *
166 * The deviceId is initially unspecified, meaning that the current default device will be used.
167 *
168 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800169 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800170 * The data format, samplesPerFrames and sampleRate are unspecified and will be
171 * chosen by the device when it is opened.
172 *
173 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
174 */
Phil Burke2155ef2017-02-24 13:50:29 -0800175AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800176
177/**
178 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800179 * On Android, for example, the ID could be obtained from the Java AudioManager.
180 *
Phil Burke057ca92017-03-28 11:31:34 -0700181 * The default, if you do not call this function, is AAUDIO_DEVICE_UNSPECIFIED,
182 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800183 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800184 * @param builder reference provided by AAudio_createStreamBuilder()
185 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 */
Phil Burke2155ef2017-02-24 13:50:29 -0800187AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800188 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800189
190/**
Phil Burke057ca92017-03-28 11:31:34 -0700191 * Request a sample rate in Hertz.
192 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800193 * The stream may be opened with a different sample rate.
194 * So the application should query for the actual rate after the stream is opened.
195 *
196 * Technically, this should be called the "frame rate" or "frames per second",
197 * because it refers to the number of complete frames transferred per second.
Phil Burke057ca92017-03-28 11:31:34 -0700198 * But it is traditionally called "sample rate". So we use that term.
Phil Burk5ed503c2017-02-01 09:38:15 -0800199 *
Phil Burke057ca92017-03-28 11:31:34 -0700200 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
201 *
202 * @param builder reference provided by AAudio_createStreamBuilder()
203 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 */
Phil Burke2155ef2017-02-24 13:50:29 -0800205AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800206 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800207
208/**
209 * Request a number of samples per frame.
Phil Burke057ca92017-03-28 11:31:34 -0700210 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800211 * The stream may be opened with a different value.
212 * So the application should query for the actual value after the stream is opened.
213 *
Phil Burke057ca92017-03-28 11:31:34 -0700214 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800215 *
216 * Note, this quantity is sometimes referred to as "channel count".
Phil Burke057ca92017-03-28 11:31:34 -0700217 *
218 * @param builder reference provided by AAudio_createStreamBuilder()
219 * @param samplesPerFrame Number of samples in one frame, ie. numChannels.
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 */
Phil Burke2155ef2017-02-24 13:50:29 -0800221AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800222 int32_t samplesPerFrame);
223
224/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800225 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700226 *
227 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
228 *
229 * The stream may be opened with a different value.
230 * So the application should query for the actual value after the stream is opened.
231 *
232 * @param builder reference provided by AAudio_createStreamBuilder()
233 * @param format Most common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800234 */
Phil Burke2155ef2017-02-24 13:50:29 -0800235AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800236 aaudio_audio_format_t format);
237
238/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700240 *
241 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
242 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700244 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800245 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800246 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700247 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800248 */
Phil Burke2155ef2017-02-24 13:50:29 -0800249AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 aaudio_sharing_mode_t sharingMode);
251
252/**
Phil Burke057ca92017-03-28 11:31:34 -0700253 * Request the direction for a stream.
254 *
255 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800256 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800257 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 */
Phil Burke2155ef2017-02-24 13:50:29 -0800260AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800261 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800262
263/**
Phil Burke057ca92017-03-28 11:31:34 -0700264 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800265 * The final AAudioStream capacity may differ, but will probably be at least this big.
266 *
Phil Burke057ca92017-03-28 11:31:34 -0700267 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800268 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800269 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700270 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800271 */
Phil Burke2155ef2017-02-24 13:50:29 -0800272AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700273 int32_t numFrames);
274/**
275 * Return one of these values from the data callback function.
276 */
277enum {
278
279 /**
280 * Continue calling the callback.
281 */
282 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
283
284 /**
285 * Stop calling the callback.
286 *
287 * The application will still need to call AAudioStream_requestPause()
288 * or AAudioStream_requestStop().
289 */
290 AAUDIO_CALLBACK_RESULT_STOP,
291
292};
293typedef int32_t aaudio_data_callback_result_t;
294
295/**
296 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
297 *
298 * For an output stream, this function should render and write numFrames of data
299 * in the streams current data format to the audioData buffer.
300 *
301 * For an input stream, this function should read and process numFrames of data
302 * from the audioData buffer.
303 *
304 * Note that this callback function should be considered a "real-time" function.
305 * It must not do anything that could cause an unbounded delay because that can cause the
306 * audio to glitch or pop.
307 *
308 * These are things the function should NOT do:
309 * <ul>
310 * <li>allocate memory using, for example, malloc() or new</li>
311 * <li>any file operations such as opening, closing, reading or writing</li>
312 * <li>any network operations such as streaming</li>
313 * <li>use any mutexes or other synchronization primitives</li>
314 * <li>sleep</li>
315 * </ul>
316 *
317 * If you need to move data, eg. MIDI commands, in or out of the callback function then
318 * we recommend the use of non-blocking techniques such as an atomic FIFO.
319 *
320 * @param stream reference provided by AAudioStreamBuilder_openStream()
321 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
322 * @param audioData a pointer to the audio data
323 * @param numFrames the number of frames to be processed
324 * @return AAUDIO_CALLBACK_RESULT_*
325 */
326typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
327 AAudioStream *stream,
328 void *userData,
329 void *audioData,
330 int32_t numFrames);
331
332/**
333 * Request that AAudio call this functions when the stream is running.
334 *
335 * Note that when using this callback, the audio data will be passed in or out
336 * of the function as an argument.
337 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
338 * that has an active data callback.
339 *
340 * The callback function will start being called after AAudioStream_requestStart() is called.
341 * It will stop being called after AAudioStream_requestPause() or
342 * AAudioStream_requestStop() is called.
343 *
344 * This callback function will be called on a real-time thread owned by AAudio. See
Glenn Kasten0d804362017-04-13 09:20:14 -0700345 * {@link AAudioStream_dataCallback} for more information.
Phil Burke057ca92017-03-28 11:31:34 -0700346 *
347 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
348 *
349 * @param builder reference provided by AAudio_createStreamBuilder()
350 * @param callback pointer to a function that will process audio data.
351 * @param userData pointer to an application data structure that will be passed
352 * to the callback functions.
353 */
354AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
355 AAudioStream_dataCallback callback,
356 void *userData);
357
358/**
359 * Set the requested data callback buffer size in frames.
360 * See {@link AAudioStream_dataCallback}.
361 *
362 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
363 *
364 * For the lowest possible latency, do not call this function. AAudio will then
365 * call the dataProc callback function with whatever size is optimal.
366 * That size may vary from one callback to another.
367 *
368 * Only use this function if the application requires a specific number of frames for processing.
369 * The application might, for example, be using an FFT that requires
370 * a specific power-of-two sized buffer.
371 *
372 * AAudio may need to add additional buffering in order to adapt between the internal
373 * buffer size and the requested buffer size.
374 *
375 * If you do call this function then the requested size should be less than
376 * half the buffer capacity, to allow double buffering.
377 *
378 * @param builder reference provided by AAudio_createStreamBuilder()
379 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
380 */
381AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
382 int32_t numFrames);
383
384/**
385 * Prototype for the callback function that is passed to
386 * AAudioStreamBuilder_setErrorCallback().
387 *
388 * @param stream reference provided by AAudioStreamBuilder_openStream()
389 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
390 * @param error an AAUDIO_ERROR_* value.
391 */
392typedef void (*AAudioStream_errorCallback)(
393 AAudioStream *stream,
394 void *userData,
395 aaudio_result_t error);
396
397/**
398 * Request that AAudio call this functions if any error occurs on a callback thread.
399 *
400 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
401 * device to be unavailable.
402 * In response, this function could signal or launch another thread to reopen a
403 * stream on another device. Do not reopen the stream in this callback.
404 *
405 * This will not be called because of actions by the application, such as stopping
406 * or closing a stream.
407 *
408 * Another possible cause of error would be a timeout or an unanticipated internal error.
409 *
410 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
411 *
412 * @param builder reference provided by AAudio_createStreamBuilder()
413 * @param callback pointer to a function that will be called if an error occurs.
414 * @param userData pointer to an application data structure that will be passed
415 * to the callback functions.
416 */
417AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
418 AAudioStream_errorCallback callback,
419 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800420
421/**
422 * Open a stream based on the options in the StreamBuilder.
423 *
424 * AAudioStream_close must be called when finished with the stream to recover
425 * the memory and to free the associated resources.
426 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800427 * @param builder reference provided by AAudio_createStreamBuilder()
428 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800429 * @return AAUDIO_OK or a negative error.
430 */
Phil Burke2155ef2017-02-24 13:50:29 -0800431AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
432 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800433
434/**
435 * Delete the resources associated with the StreamBuilder.
436 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800437 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800438 * @return AAUDIO_OK or a negative error.
439 */
Phil Burke2155ef2017-02-24 13:50:29 -0800440AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800441
442// ============================================================
443// Stream Control
444// ============================================================
445
446/**
447 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
448 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800449 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800450 * @return AAUDIO_OK or a negative error.
451 */
Phil Burke2155ef2017-02-24 13:50:29 -0800452AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800453
454/**
455 * Asynchronously request to start playing the stream. For output streams, one should
456 * write to the stream to fill the buffer before starting.
457 * Otherwise it will underflow.
458 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
459 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800460 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800461 * @return AAUDIO_OK or a negative error.
462 */
Phil Burke2155ef2017-02-24 13:50:29 -0800463AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800464
465/**
466 * Asynchronous request for the stream to pause.
467 * Pausing a stream will freeze the data flow but not flush any buffers.
468 * Use AAudioStream_Start() to resume playback after a pause.
469 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
470 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800471 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800472 * @return AAUDIO_OK or a negative error.
473 */
Phil Burke2155ef2017-02-24 13:50:29 -0800474AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800475
476/**
477 * Asynchronous request for the stream to flush.
478 * Flushing will discard any pending data.
479 * This call only works if the stream is pausing or paused. TODO review
480 * Frame counters are not reset by a flush. They may be advanced.
481 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
482 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800483 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800484 * @return AAUDIO_OK or a negative error.
485 */
Phil Burke2155ef2017-02-24 13:50:29 -0800486AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800487
488/**
489 * Asynchronous request for the stream to stop.
490 * The stream will stop after all of the data currently buffered has been played.
491 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
492 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800493 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800494 * @return AAUDIO_OK or a negative error.
495 */
Phil Burke2155ef2017-02-24 13:50:29 -0800496AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800497
498/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800499 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800500 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800501 * This function will immediately return the state without updating the state.
502 * If you want to update the client state based on the server state then
503 * call AAudioStream_waitForStateChange() with currentState
504 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
505 *
506 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800507 */
Phil Burke2155ef2017-02-24 13:50:29 -0800508AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800509
510/**
511 * Wait until the current state no longer matches the input state.
512 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800513 * This will update the current client state.
514 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800515 * <pre><code>
516 * aaudio_stream_state_t currentState;
517 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
518 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
519 * result = AAudioStream_waitForStateChange(
520 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
521 * }
522 * </code></pre>
523 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800524 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800525 * @param inputState The state we want to avoid.
526 * @param nextState Pointer to a variable that will be set to the new state.
527 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
528 * @return AAUDIO_OK or a negative error.
529 */
Phil Burke2155ef2017-02-24 13:50:29 -0800530AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800531 aaudio_stream_state_t inputState,
532 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800533 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800534
535// ============================================================
536// Stream I/O
537// ============================================================
538
539/**
540 * Read data from the stream.
541 *
542 * The call will wait until the read is complete or until it runs out of time.
543 * If timeoutNanos is zero then this call will not wait.
544 *
545 * Note that timeoutNanoseconds is a relative duration in wall clock time.
546 * Time will not stop if the thread is asleep.
547 * So it will be implemented using CLOCK_BOOTTIME.
548 *
549 * This call is "strong non-blocking" unless it has to wait for data.
550 *
551 * @param stream A stream created using AAudioStreamBuilder_openStream().
552 * @param buffer The address of the first sample.
553 * @param numFrames Number of frames to read. Only complete frames will be written.
554 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800555 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800556 */
Phil Burke2155ef2017-02-24 13:50:29 -0800557AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800558 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800559 int32_t numFrames,
560 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800561
562/**
563 * Write data to the stream.
564 *
565 * The call will wait until the write is complete or until it runs out of time.
566 * If timeoutNanos is zero then this call will not wait.
567 *
568 * Note that timeoutNanoseconds is a relative duration in wall clock time.
569 * Time will not stop if the thread is asleep.
570 * So it will be implemented using CLOCK_BOOTTIME.
571 *
572 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
573 *
574 * @param stream A stream created using AAudioStreamBuilder_openStream().
575 * @param buffer The address of the first sample.
576 * @param numFrames Number of frames to write. Only complete frames will be written.
577 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
578 * @return The number of frames actually written or a negative error.
579 */
Phil Burke2155ef2017-02-24 13:50:29 -0800580AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800581 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800582 int32_t numFrames,
583 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800584
585
586// ============================================================
587// High priority audio threads
588// ============================================================
589
Phil Burke057ca92017-03-28 11:31:34 -0700590/**
591 * @deprecated Use AudioStreamBuilder_setCallback()
592 */
Phil Burke2155ef2017-02-24 13:50:29 -0800593typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800594
595/**
Phil Burke057ca92017-03-28 11:31:34 -0700596 * @deprecated Use AudioStreamBuilder_setCallback()
597 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800598 * Create a thread associated with a stream. The thread has special properties for
599 * low latency audio performance. This thread can be used to implement a callback API.
600 *
601 * Only one thread may be associated with a stream.
602 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800603 * If you are using multiple streams then we recommend that you only do
604 * blocking reads or writes on one stream. You can do non-blocking I/O on the
605 * other streams by setting the timeout to zero.
606 * This thread should be created for the stream that you will block on.
607 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800608 * Note that this API is in flux.
609 *
610 * @param stream A stream created using AAudioStreamBuilder_openStream().
611 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800612 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800613 * @param arg an argument that will be passed to your thread entry point
614 * @return AAUDIO_OK or a negative error.
615 */
Phil Burke2155ef2017-02-24 13:50:29 -0800616AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800617 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800618 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800619 void *arg);
620
621/**
Phil Burke057ca92017-03-28 11:31:34 -0700622 * @deprecated Use AudioStreamBuilder_setCallback()
623 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800624 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800625 *
626 * @param stream A stream created using AAudioStreamBuilder_openStream().
627 * @param returnArg a pointer to a variable to receive the return value
628 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
629 * @return AAUDIO_OK or a negative error.
630 */
Phil Burke2155ef2017-02-24 13:50:29 -0800631AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800632 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800633 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800634
635// ============================================================
636// Stream - queries
637// ============================================================
638
639
640/**
641 * This can be used to adjust the latency of the buffer by changing
642 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800643 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800644 * at run-time for each device.
645 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800646 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800647 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800648 * Note that you will probably not get the exact size you request.
649 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
650 *
651 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700652 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800653 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800654 */
Phil Burke2155ef2017-02-24 13:50:29 -0800655AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700656 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800657
658/**
659 * Query the maximum number of frames that can be filled without blocking.
660 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800661 * @param stream reference provided by AAudioStreamBuilder_openStream()
662 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800663 */
Phil Burke2155ef2017-02-24 13:50:29 -0800664AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800665
666/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800667 * Query the number of frames that the application should read or write at
668 * one time for optimal performance. It is OK if an application writes
669 * a different number of frames. But the buffer size may need to be larger
670 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800671 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800672 * Note that this may or may not match the actual device burst size.
673 * For some endpoints, the burst size can vary dynamically.
674 * But these tend to be devices with high latency.
675 *
676 * @param stream reference provided by AAudioStreamBuilder_openStream()
677 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800678 */
Phil Burke2155ef2017-02-24 13:50:29 -0800679AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800680
681/**
682 * Query maximum buffer capacity in frames.
683 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800684 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700685 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800686 */
Phil Burke2155ef2017-02-24 13:50:29 -0800687AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800688
689/**
Phil Burke057ca92017-03-28 11:31:34 -0700690 * Query the size of the buffer that will be passed to the dataProc callback
691 * in the numFrames parameter.
692 *
693 * This call can be used if the application needs to know the value of numFrames before
694 * the stream is started. This is not normally necessary.
695 *
696 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
697 * then this will be the same size.
698 *
699 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
700 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
701 *
702 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
703 * may vary from one dataProc callback to the next.
704 *
705 * @param stream reference provided by AAudioStreamBuilder_openStream()
706 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
707 */
708AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
709
710/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800711 * An XRun is an Underrun or an Overrun.
712 * During playing, an underrun will occur if the stream is not written in time
713 * and the system runs out of valid data.
714 * During recording, an overrun will occur if the stream is not read in time
715 * and there is no place to put the incoming data so it is discarded.
716 *
717 * An underrun or overrun can cause an audible "pop" or "glitch".
718 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800719 * @param stream reference provided by AAudioStreamBuilder_openStream()
720 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800721 */
Phil Burke2155ef2017-02-24 13:50:29 -0800722AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800723
724/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800725 * @param stream reference provided by AAudioStreamBuilder_openStream()
726 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800727 */
Phil Burke2155ef2017-02-24 13:50:29 -0800728AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800729
730/**
731 * The samplesPerFrame is also known as channelCount.
732 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800733 * @param stream reference provided by AAudioStreamBuilder_openStream()
734 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800735 */
Phil Burke2155ef2017-02-24 13:50:29 -0800736AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800737
738/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800739 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800740 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800741 */
Phil Burke2155ef2017-02-24 13:50:29 -0800742AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800743
744/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800745 * @param stream reference provided by AAudioStreamBuilder_openStream()
746 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800747 */
Phil Burke2155ef2017-02-24 13:50:29 -0800748AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800749
750/**
751 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800752 * @param stream reference provided by AAudioStreamBuilder_openStream()
753 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800754 */
Phil Burke2155ef2017-02-24 13:50:29 -0800755AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800756
757/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800758 * @param stream reference provided by AAudioStreamBuilder_openStream()
759 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800760 */
Phil Burke2155ef2017-02-24 13:50:29 -0800761AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800762
763/**
764 * Passes back the number of frames that have been written since the stream was created.
765 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800766 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800767 *
768 * The frame position is monotonically increasing.
769 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800770 * @param stream reference provided by AAudioStreamBuilder_openStream()
771 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800772 */
Phil Burke2155ef2017-02-24 13:50:29 -0800773AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800774
775/**
776 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800777 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800778 * For an input stream, this will be advanced by the application calling read().
779 *
780 * The frame position is monotonically increasing.
781 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800782 * @param stream reference provided by AAudioStreamBuilder_openStream()
783 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800784 */
Phil Burke2155ef2017-02-24 13:50:29 -0800785AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800786
787/**
788 * Passes back the time at which a particular frame was presented.
789 * This can be used to synchronize audio with video or MIDI.
790 * It can also be used to align a recorded stream with a playback stream.
791 *
792 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
793 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
794 * Note that because requestStart() is asynchronous, timestamps will not be valid until
795 * a short time after calling requestStart().
796 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
797 * Just try calling again later.
798 *
799 * If an error occurs, then the position and time will not be modified.
800 *
801 * The position and time passed back are monotonically increasing.
802 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800803 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800804 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
805 * @param framePosition pointer to a variable to receive the position
806 * @param timeNanoseconds pointer to a variable to receive the time
807 * @return AAUDIO_OK or a negative error
808 */
Phil Burke2155ef2017-02-24 13:50:29 -0800809AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800810 clockid_t clockid,
811 int64_t *framePosition,
812 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800813
814#ifdef __cplusplus
815}
816#endif
817
818#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700819
820/** @} */