blob: bc65acc99c3adccaca80c38e78585635b5950e81 [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,
57 AAUDIO_FORMAT_PCM_FLOAT,
58 AAUDIO_FORMAT_PCM_I8_24,
59 AAUDIO_FORMAT_PCM_I32
60};
61typedef int32_t aaudio_format_t;
62
63/**
64 * @deprecated use aaudio_format_t instead
65 * TODO remove when tests and examples are updated
66 */
67typedef int32_t aaudio_audio_format_t;
68
69enum {
70 AAUDIO_OK,
71 AAUDIO_ERROR_BASE = -900, // TODO review
72 AAUDIO_ERROR_DISCONNECTED,
73 AAUDIO_ERROR_ILLEGAL_ARGUMENT,
74 AAUDIO_ERROR_INCOMPATIBLE,
75 AAUDIO_ERROR_INTERNAL, // an underlying API returned an error code
76 AAUDIO_ERROR_INVALID_STATE,
77 AAUDIO_ERROR_UNEXPECTED_STATE,
78 AAUDIO_ERROR_UNEXPECTED_VALUE,
79 AAUDIO_ERROR_INVALID_HANDLE,
80 AAUDIO_ERROR_INVALID_QUERY,
81 AAUDIO_ERROR_UNIMPLEMENTED,
82 AAUDIO_ERROR_UNAVAILABLE,
83 AAUDIO_ERROR_NO_FREE_HANDLES,
84 AAUDIO_ERROR_NO_MEMORY,
85 AAUDIO_ERROR_NULL,
86 AAUDIO_ERROR_TIMEOUT,
87 AAUDIO_ERROR_WOULD_BLOCK,
88 AAUDIO_ERROR_INVALID_FORMAT,
89 AAUDIO_ERROR_OUT_OF_RANGE,
90 AAUDIO_ERROR_NO_SERVICE,
91 AAUDIO_ERROR_INVALID_RATE
92};
93typedef int32_t aaudio_result_t;
94
95enum
96{
97 AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
98 AAUDIO_STREAM_STATE_UNKNOWN,
99 AAUDIO_STREAM_STATE_OPEN,
100 AAUDIO_STREAM_STATE_STARTING,
101 AAUDIO_STREAM_STATE_STARTED,
102 AAUDIO_STREAM_STATE_PAUSING,
103 AAUDIO_STREAM_STATE_PAUSED,
104 AAUDIO_STREAM_STATE_FLUSHING,
105 AAUDIO_STREAM_STATE_FLUSHED,
106 AAUDIO_STREAM_STATE_STOPPING,
107 AAUDIO_STREAM_STATE_STOPPED,
108 AAUDIO_STREAM_STATE_CLOSING,
109 AAUDIO_STREAM_STATE_CLOSED,
110 AAUDIO_STREAM_STATE_DISCONNECTED
111};
112typedef int32_t aaudio_stream_state_t;
113
114
115enum {
116 /**
117 * This will be the only stream using a particular source or sink.
118 * This mode will provide the lowest possible latency.
119 * You should close EXCLUSIVE streams immediately when you are not using them.
120 */
121 AAUDIO_SHARING_MODE_EXCLUSIVE,
122 /**
123 * Multiple applications will be mixed by the AAudio Server.
124 * This will have higher latency than the EXCLUSIVE mode.
125 */
126 AAUDIO_SHARING_MODE_SHARED
127};
128typedef int32_t aaudio_sharing_mode_t;
129
Phil Burke2155ef2017-02-24 13:50:29 -0800130typedef struct AAudioStreamStruct AAudioStream;
131typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800132
Phil Burk5ed503c2017-02-01 09:38:15 -0800133#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800134#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800135#endif
136
137// ============================================================
138// Audio System
139// ============================================================
140
141/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800142 * The text is the ASCII symbol corresponding to the returnCode,
143 * or an English message saying the returnCode is unrecognized.
144 * This is intended for developers to use when debugging.
145 * It is not for display to users.
146 *
147 * @return pointer to a text representation of an AAudio result code.
148 */
149AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
150
151/**
152 * The text is the ASCII symbol corresponding to the stream state,
153 * or an English message saying the state is unrecognized.
154 * This is intended for developers to use when debugging.
155 * It is not for display to users.
156 *
157 * @return pointer to a text representation of an AAudio state.
158 */
159AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
160
161// ============================================================
162// StreamBuilder
163// ============================================================
164
165/**
166 * Create a StreamBuilder that can be used to open a Stream.
167 *
168 * The deviceId is initially unspecified, meaning that the current default device will be used.
169 *
170 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800171 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800172 * The data format, samplesPerFrames and sampleRate are unspecified and will be
173 * chosen by the device when it is opened.
174 *
175 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
176 */
Phil Burke2155ef2017-02-24 13:50:29 -0800177AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800178
179/**
180 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800181 * On Android, for example, the ID could be obtained from the Java AudioManager.
182 *
Phil Burke057ca92017-03-28 11:31:34 -0700183 * The default, if you do not call this function, is AAUDIO_DEVICE_UNSPECIFIED,
184 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800185 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800186 * @param builder reference provided by AAudio_createStreamBuilder()
187 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800188 */
Phil Burke2155ef2017-02-24 13:50:29 -0800189AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800190 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800191
192/**
Phil Burke057ca92017-03-28 11:31:34 -0700193 * Request a sample rate in Hertz.
194 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800195 * The stream may be opened with a different sample rate.
196 * So the application should query for the actual rate after the stream is opened.
197 *
198 * Technically, this should be called the "frame rate" or "frames per second",
199 * because it refers to the number of complete frames transferred per second.
Phil Burke057ca92017-03-28 11:31:34 -0700200 * But it is traditionally called "sample rate". So we use that term.
Phil Burk5ed503c2017-02-01 09:38:15 -0800201 *
Phil Burke057ca92017-03-28 11:31:34 -0700202 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
203 *
204 * @param builder reference provided by AAudio_createStreamBuilder()
205 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 */
Phil Burke2155ef2017-02-24 13:50:29 -0800207AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800208 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800209
210/**
211 * Request a number of samples per frame.
Phil Burke057ca92017-03-28 11:31:34 -0700212 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800213 * The stream may be opened with a different value.
214 * So the application should query for the actual value after the stream is opened.
215 *
Phil Burke057ca92017-03-28 11:31:34 -0700216 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 *
218 * Note, this quantity is sometimes referred to as "channel count".
Phil Burke057ca92017-03-28 11:31:34 -0700219 *
220 * @param builder reference provided by AAudio_createStreamBuilder()
221 * @param samplesPerFrame Number of samples in one frame, ie. numChannels.
Phil Burk5ed503c2017-02-01 09:38:15 -0800222 */
Phil Burke2155ef2017-02-24 13:50:29 -0800223AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800224 int32_t samplesPerFrame);
225
226/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800227 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700228 *
229 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
230 *
231 * The stream may be opened with a different value.
232 * So the application should query for the actual value after the stream is opened.
233 *
234 * @param builder reference provided by AAudio_createStreamBuilder()
235 * @param format Most common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800236 */
Phil Burke2155ef2017-02-24 13:50:29 -0800237AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800238 aaudio_audio_format_t format);
239
240/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700242 *
243 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
244 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800245 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700246 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800247 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800248 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700249 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 */
Phil Burke2155ef2017-02-24 13:50:29 -0800251AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 aaudio_sharing_mode_t sharingMode);
253
254/**
Phil Burke057ca92017-03-28 11:31:34 -0700255 * Request the direction for a stream.
256 *
257 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800259 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800260 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 */
Phil Burke2155ef2017-02-24 13:50:29 -0800262AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800263 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800264
265/**
Phil Burke057ca92017-03-28 11:31:34 -0700266 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800267 * The final AAudioStream capacity may differ, but will probably be at least this big.
268 *
Phil Burke057ca92017-03-28 11:31:34 -0700269 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800270 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800271 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700272 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800273 */
Phil Burke2155ef2017-02-24 13:50:29 -0800274AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700275 int32_t numFrames);
276/**
277 * Return one of these values from the data callback function.
278 */
279enum {
280
281 /**
282 * Continue calling the callback.
283 */
284 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
285
286 /**
287 * Stop calling the callback.
288 *
289 * The application will still need to call AAudioStream_requestPause()
290 * or AAudioStream_requestStop().
291 */
292 AAUDIO_CALLBACK_RESULT_STOP,
293
294};
295typedef int32_t aaudio_data_callback_result_t;
296
297/**
298 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
299 *
300 * For an output stream, this function should render and write numFrames of data
301 * in the streams current data format to the audioData buffer.
302 *
303 * For an input stream, this function should read and process numFrames of data
304 * from the audioData buffer.
305 *
306 * Note that this callback function should be considered a "real-time" function.
307 * It must not do anything that could cause an unbounded delay because that can cause the
308 * audio to glitch or pop.
309 *
310 * These are things the function should NOT do:
311 * <ul>
312 * <li>allocate memory using, for example, malloc() or new</li>
313 * <li>any file operations such as opening, closing, reading or writing</li>
314 * <li>any network operations such as streaming</li>
315 * <li>use any mutexes or other synchronization primitives</li>
316 * <li>sleep</li>
317 * </ul>
318 *
319 * If you need to move data, eg. MIDI commands, in or out of the callback function then
320 * we recommend the use of non-blocking techniques such as an atomic FIFO.
321 *
322 * @param stream reference provided by AAudioStreamBuilder_openStream()
323 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
324 * @param audioData a pointer to the audio data
325 * @param numFrames the number of frames to be processed
326 * @return AAUDIO_CALLBACK_RESULT_*
327 */
328typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
329 AAudioStream *stream,
330 void *userData,
331 void *audioData,
332 int32_t numFrames);
333
334/**
335 * Request that AAudio call this functions when the stream is running.
336 *
337 * Note that when using this callback, the audio data will be passed in or out
338 * of the function as an argument.
339 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
340 * that has an active data callback.
341 *
342 * The callback function will start being called after AAudioStream_requestStart() is called.
343 * It will stop being called after AAudioStream_requestPause() or
344 * AAudioStream_requestStop() is called.
345 *
346 * This callback function will be called on a real-time thread owned by AAudio. See
347 * {@link aaudio_data_callback_proc_t} for more information.
348 *
349 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
350 *
351 * @param builder reference provided by AAudio_createStreamBuilder()
352 * @param callback pointer to a function that will process audio data.
353 * @param userData pointer to an application data structure that will be passed
354 * to the callback functions.
355 */
356AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
357 AAudioStream_dataCallback callback,
358 void *userData);
359
360/**
361 * Set the requested data callback buffer size in frames.
362 * See {@link AAudioStream_dataCallback}.
363 *
364 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
365 *
366 * For the lowest possible latency, do not call this function. AAudio will then
367 * call the dataProc callback function with whatever size is optimal.
368 * That size may vary from one callback to another.
369 *
370 * Only use this function if the application requires a specific number of frames for processing.
371 * The application might, for example, be using an FFT that requires
372 * a specific power-of-two sized buffer.
373 *
374 * AAudio may need to add additional buffering in order to adapt between the internal
375 * buffer size and the requested buffer size.
376 *
377 * If you do call this function then the requested size should be less than
378 * half the buffer capacity, to allow double buffering.
379 *
380 * @param builder reference provided by AAudio_createStreamBuilder()
381 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
382 */
383AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
384 int32_t numFrames);
385
386/**
387 * Prototype for the callback function that is passed to
388 * AAudioStreamBuilder_setErrorCallback().
389 *
390 * @param stream reference provided by AAudioStreamBuilder_openStream()
391 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
392 * @param error an AAUDIO_ERROR_* value.
393 */
394typedef void (*AAudioStream_errorCallback)(
395 AAudioStream *stream,
396 void *userData,
397 aaudio_result_t error);
398
399/**
400 * Request that AAudio call this functions if any error occurs on a callback thread.
401 *
402 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
403 * device to be unavailable.
404 * In response, this function could signal or launch another thread to reopen a
405 * stream on another device. Do not reopen the stream in this callback.
406 *
407 * This will not be called because of actions by the application, such as stopping
408 * or closing a stream.
409 *
410 * Another possible cause of error would be a timeout or an unanticipated internal error.
411 *
412 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
413 *
414 * @param builder reference provided by AAudio_createStreamBuilder()
415 * @param callback pointer to a function that will be called if an error occurs.
416 * @param userData pointer to an application data structure that will be passed
417 * to the callback functions.
418 */
419AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
420 AAudioStream_errorCallback callback,
421 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800422
423/**
424 * Open a stream based on the options in the StreamBuilder.
425 *
426 * AAudioStream_close must be called when finished with the stream to recover
427 * the memory and to free the associated resources.
428 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800429 * @param builder reference provided by AAudio_createStreamBuilder()
430 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800431 * @return AAUDIO_OK or a negative error.
432 */
Phil Burke2155ef2017-02-24 13:50:29 -0800433AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
434 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800435
436/**
437 * Delete the resources associated with the StreamBuilder.
438 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800439 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800440 * @return AAUDIO_OK or a negative error.
441 */
Phil Burke2155ef2017-02-24 13:50:29 -0800442AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800443
444// ============================================================
445// Stream Control
446// ============================================================
447
448/**
449 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
450 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800451 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800452 * @return AAUDIO_OK or a negative error.
453 */
Phil Burke2155ef2017-02-24 13:50:29 -0800454AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800455
456/**
457 * Asynchronously request to start playing the stream. For output streams, one should
458 * write to the stream to fill the buffer before starting.
459 * Otherwise it will underflow.
460 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
461 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800462 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800463 * @return AAUDIO_OK or a negative error.
464 */
Phil Burke2155ef2017-02-24 13:50:29 -0800465AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800466
467/**
468 * Asynchronous request for the stream to pause.
469 * Pausing a stream will freeze the data flow but not flush any buffers.
470 * Use AAudioStream_Start() to resume playback after a pause.
471 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
472 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800473 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800474 * @return AAUDIO_OK or a negative error.
475 */
Phil Burke2155ef2017-02-24 13:50:29 -0800476AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800477
478/**
479 * Asynchronous request for the stream to flush.
480 * Flushing will discard any pending data.
481 * This call only works if the stream is pausing or paused. TODO review
482 * Frame counters are not reset by a flush. They may be advanced.
483 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
484 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800485 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800486 * @return AAUDIO_OK or a negative error.
487 */
Phil Burke2155ef2017-02-24 13:50:29 -0800488AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800489
490/**
491 * Asynchronous request for the stream to stop.
492 * The stream will stop after all of the data currently buffered has been played.
493 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
494 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800495 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800496 * @return AAUDIO_OK or a negative error.
497 */
Phil Burke2155ef2017-02-24 13:50:29 -0800498AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800499
500/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800501 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800502 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800503 * This function will immediately return the state without updating the state.
504 * If you want to update the client state based on the server state then
505 * call AAudioStream_waitForStateChange() with currentState
506 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
507 *
508 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800509 * @param state pointer to a variable that will be set to the current state
Phil Burk5ed503c2017-02-01 09:38:15 -0800510 */
Phil Burke2155ef2017-02-24 13:50:29 -0800511AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800512
513/**
514 * Wait until the current state no longer matches the input state.
515 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800516 * This will update the current client state.
517 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800518 * <pre><code>
519 * aaudio_stream_state_t currentState;
520 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
521 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
522 * result = AAudioStream_waitForStateChange(
523 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
524 * }
525 * </code></pre>
526 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800527 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800528 * @param inputState The state we want to avoid.
529 * @param nextState Pointer to a variable that will be set to the new state.
530 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
531 * @return AAUDIO_OK or a negative error.
532 */
Phil Burke2155ef2017-02-24 13:50:29 -0800533AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800534 aaudio_stream_state_t inputState,
535 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800536 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800537
538// ============================================================
539// Stream I/O
540// ============================================================
541
542/**
543 * Read data from the stream.
544 *
545 * The call will wait until the read is complete or until it runs out of time.
546 * If timeoutNanos is zero then this call will not wait.
547 *
548 * Note that timeoutNanoseconds is a relative duration in wall clock time.
549 * Time will not stop if the thread is asleep.
550 * So it will be implemented using CLOCK_BOOTTIME.
551 *
552 * This call is "strong non-blocking" unless it has to wait for data.
553 *
554 * @param stream A stream created using AAudioStreamBuilder_openStream().
555 * @param buffer The address of the first sample.
556 * @param numFrames Number of frames to read. Only complete frames will be written.
557 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800558 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800559 */
Phil Burke2155ef2017-02-24 13:50:29 -0800560AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800561 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800562 int32_t numFrames,
563 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800564
565/**
566 * Write data to the stream.
567 *
568 * The call will wait until the write is complete or until it runs out of time.
569 * If timeoutNanos is zero then this call will not wait.
570 *
571 * Note that timeoutNanoseconds is a relative duration in wall clock time.
572 * Time will not stop if the thread is asleep.
573 * So it will be implemented using CLOCK_BOOTTIME.
574 *
575 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
576 *
577 * @param stream A stream created using AAudioStreamBuilder_openStream().
578 * @param buffer The address of the first sample.
579 * @param numFrames Number of frames to write. Only complete frames will be written.
580 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
581 * @return The number of frames actually written or a negative error.
582 */
Phil Burke2155ef2017-02-24 13:50:29 -0800583AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800584 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800585 int32_t numFrames,
586 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800587
588
589// ============================================================
590// High priority audio threads
591// ============================================================
592
Phil Burke057ca92017-03-28 11:31:34 -0700593/**
594 * @deprecated Use AudioStreamBuilder_setCallback()
595 */
Phil Burke2155ef2017-02-24 13:50:29 -0800596typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800597
598/**
Phil Burke057ca92017-03-28 11:31:34 -0700599 * @deprecated Use AudioStreamBuilder_setCallback()
600 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800601 * Create a thread associated with a stream. The thread has special properties for
602 * low latency audio performance. This thread can be used to implement a callback API.
603 *
604 * Only one thread may be associated with a stream.
605 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800606 * If you are using multiple streams then we recommend that you only do
607 * blocking reads or writes on one stream. You can do non-blocking I/O on the
608 * other streams by setting the timeout to zero.
609 * This thread should be created for the stream that you will block on.
610 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800611 * Note that this API is in flux.
612 *
613 * @param stream A stream created using AAudioStreamBuilder_openStream().
614 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800615 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800616 * @param arg an argument that will be passed to your thread entry point
617 * @return AAUDIO_OK or a negative error.
618 */
Phil Burke2155ef2017-02-24 13:50:29 -0800619AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800620 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800621 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800622 void *arg);
623
624/**
Phil Burke057ca92017-03-28 11:31:34 -0700625 * @deprecated Use AudioStreamBuilder_setCallback()
626 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800627 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800628 *
629 * @param stream A stream created using AAudioStreamBuilder_openStream().
630 * @param returnArg a pointer to a variable to receive the return value
631 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
632 * @return AAUDIO_OK or a negative error.
633 */
Phil Burke2155ef2017-02-24 13:50:29 -0800634AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800635 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800636 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800637
638// ============================================================
639// Stream - queries
640// ============================================================
641
642
643/**
644 * This can be used to adjust the latency of the buffer by changing
645 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800646 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800647 * at run-time for each device.
648 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800649 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800650 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800651 * Note that you will probably not get the exact size you request.
652 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
653 *
654 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700655 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800656 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800657 */
Phil Burke2155ef2017-02-24 13:50:29 -0800658AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700659 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800660
661/**
662 * Query the maximum number of frames that can be filled without blocking.
663 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800664 * @param stream reference provided by AAudioStreamBuilder_openStream()
665 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800666 */
Phil Burke2155ef2017-02-24 13:50:29 -0800667AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800668
669/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800670 * Query the number of frames that the application should read or write at
671 * one time for optimal performance. It is OK if an application writes
672 * a different number of frames. But the buffer size may need to be larger
673 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800674 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800675 * Note that this may or may not match the actual device burst size.
676 * For some endpoints, the burst size can vary dynamically.
677 * But these tend to be devices with high latency.
678 *
679 * @param stream reference provided by AAudioStreamBuilder_openStream()
680 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800681 */
Phil Burke2155ef2017-02-24 13:50:29 -0800682AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800683
684/**
685 * Query maximum buffer capacity in frames.
686 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800687 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700688 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800689 */
Phil Burke2155ef2017-02-24 13:50:29 -0800690AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800691
692/**
Phil Burke057ca92017-03-28 11:31:34 -0700693 * Query the size of the buffer that will be passed to the dataProc callback
694 * in the numFrames parameter.
695 *
696 * This call can be used if the application needs to know the value of numFrames before
697 * the stream is started. This is not normally necessary.
698 *
699 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
700 * then this will be the same size.
701 *
702 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
703 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
704 *
705 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
706 * may vary from one dataProc callback to the next.
707 *
708 * @param stream reference provided by AAudioStreamBuilder_openStream()
709 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
710 */
711AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
712
713/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800714 * An XRun is an Underrun or an Overrun.
715 * During playing, an underrun will occur if the stream is not written in time
716 * and the system runs out of valid data.
717 * During recording, an overrun will occur if the stream is not read in time
718 * and there is no place to put the incoming data so it is discarded.
719 *
720 * An underrun or overrun can cause an audible "pop" or "glitch".
721 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800722 * @param stream reference provided by AAudioStreamBuilder_openStream()
723 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800724 */
Phil Burke2155ef2017-02-24 13:50:29 -0800725AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800726
727/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800728 * @param stream reference provided by AAudioStreamBuilder_openStream()
729 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800730 */
Phil Burke2155ef2017-02-24 13:50:29 -0800731AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800732
733/**
734 * The samplesPerFrame is also known as channelCount.
735 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800736 * @param stream reference provided by AAudioStreamBuilder_openStream()
737 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800738 */
Phil Burke2155ef2017-02-24 13:50:29 -0800739AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800740
741/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800742 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800743 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800744 */
Phil Burke2155ef2017-02-24 13:50:29 -0800745AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800746
747/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800748 * @param stream reference provided by AAudioStreamBuilder_openStream()
749 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800750 */
Phil Burke2155ef2017-02-24 13:50:29 -0800751AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800752
753/**
754 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800755 * @param stream reference provided by AAudioStreamBuilder_openStream()
756 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800757 */
Phil Burke2155ef2017-02-24 13:50:29 -0800758AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800759
760/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800761 * @param stream reference provided by AAudioStreamBuilder_openStream()
762 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800763 */
Phil Burke2155ef2017-02-24 13:50:29 -0800764AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800765
766/**
767 * Passes back the number of frames that have been written since the stream was created.
768 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800769 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800770 *
771 * The frame position is monotonically increasing.
772 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800773 * @param stream reference provided by AAudioStreamBuilder_openStream()
774 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800775 */
Phil Burke2155ef2017-02-24 13:50:29 -0800776AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800777
778/**
779 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800780 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800781 * For an input stream, this will be advanced by the application calling read().
782 *
783 * The frame position is monotonically increasing.
784 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800785 * @param stream reference provided by AAudioStreamBuilder_openStream()
786 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800787 */
Phil Burke2155ef2017-02-24 13:50:29 -0800788AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800789
790/**
791 * Passes back the time at which a particular frame was presented.
792 * This can be used to synchronize audio with video or MIDI.
793 * It can also be used to align a recorded stream with a playback stream.
794 *
795 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
796 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
797 * Note that because requestStart() is asynchronous, timestamps will not be valid until
798 * a short time after calling requestStart().
799 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
800 * Just try calling again later.
801 *
802 * If an error occurs, then the position and time will not be modified.
803 *
804 * The position and time passed back are monotonically increasing.
805 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800806 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800807 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
808 * @param framePosition pointer to a variable to receive the position
809 * @param timeNanoseconds pointer to a variable to receive the time
810 * @return AAUDIO_OK or a negative error
811 */
Phil Burke2155ef2017-02-24 13:50:29 -0800812AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800813 clockid_t clockid,
814 int64_t *framePosition,
815 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800816
817#ifdef __cplusplus
818}
819#endif
820
821#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700822
823/** @} */