blob: 30fbdd627a7f3dd09f68054ed814e4dc1e632547 [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
Phil Burka4eb0d82017-04-12 15:44:06 -070045
46enum {
47 AAUDIO_DIRECTION_OUTPUT,
48 AAUDIO_DIRECTION_INPUT
49};
50typedef int32_t aaudio_direction_t;
51
52enum {
53 AAUDIO_FORMAT_INVALID = -1,
54 AAUDIO_FORMAT_UNSPECIFIED = 0,
55 AAUDIO_FORMAT_PCM_I16,
Phil Burk74733452017-04-18 19:50:28 -070056 AAUDIO_FORMAT_PCM_FLOAT
Phil Burka4eb0d82017-04-12 15:44:06 -070057};
58typedef int32_t aaudio_format_t;
59
Phil Burka4eb0d82017-04-12 15:44:06 -070060enum {
61 AAUDIO_OK,
62 AAUDIO_ERROR_BASE = -900, // TODO review
63 AAUDIO_ERROR_DISCONNECTED,
64 AAUDIO_ERROR_ILLEGAL_ARGUMENT,
Phil Burk17fff382017-05-16 14:06:45 -070065 // reserved
66 AAUDIO_ERROR_INTERNAL = AAUDIO_ERROR_ILLEGAL_ARGUMENT + 2,
Phil Burka4eb0d82017-04-12 15:44:06 -070067 AAUDIO_ERROR_INVALID_STATE,
Phil Burk17fff382017-05-16 14:06:45 -070068 // reserved
69 // reserved
70 AAUDIO_ERROR_INVALID_HANDLE = AAUDIO_ERROR_INVALID_STATE + 3,
71 // reserved
72 AAUDIO_ERROR_UNIMPLEMENTED = AAUDIO_ERROR_INVALID_HANDLE + 2,
Phil Burka4eb0d82017-04-12 15:44:06 -070073 AAUDIO_ERROR_UNAVAILABLE,
74 AAUDIO_ERROR_NO_FREE_HANDLES,
75 AAUDIO_ERROR_NO_MEMORY,
76 AAUDIO_ERROR_NULL,
77 AAUDIO_ERROR_TIMEOUT,
78 AAUDIO_ERROR_WOULD_BLOCK,
79 AAUDIO_ERROR_INVALID_FORMAT,
80 AAUDIO_ERROR_OUT_OF_RANGE,
81 AAUDIO_ERROR_NO_SERVICE,
82 AAUDIO_ERROR_INVALID_RATE
83};
84typedef int32_t aaudio_result_t;
85
86enum
87{
88 AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
89 AAUDIO_STREAM_STATE_UNKNOWN,
90 AAUDIO_STREAM_STATE_OPEN,
91 AAUDIO_STREAM_STATE_STARTING,
92 AAUDIO_STREAM_STATE_STARTED,
93 AAUDIO_STREAM_STATE_PAUSING,
94 AAUDIO_STREAM_STATE_PAUSED,
95 AAUDIO_STREAM_STATE_FLUSHING,
96 AAUDIO_STREAM_STATE_FLUSHED,
97 AAUDIO_STREAM_STATE_STOPPING,
98 AAUDIO_STREAM_STATE_STOPPED,
99 AAUDIO_STREAM_STATE_CLOSING,
100 AAUDIO_STREAM_STATE_CLOSED,
101 AAUDIO_STREAM_STATE_DISCONNECTED
102};
103typedef int32_t aaudio_stream_state_t;
104
105
106enum {
107 /**
108 * This will be the only stream using a particular source or sink.
109 * This mode will provide the lowest possible latency.
110 * You should close EXCLUSIVE streams immediately when you are not using them.
111 */
112 AAUDIO_SHARING_MODE_EXCLUSIVE,
113 /**
114 * Multiple applications will be mixed by the AAudio Server.
115 * This will have higher latency than the EXCLUSIVE mode.
116 */
117 AAUDIO_SHARING_MODE_SHARED
118};
119typedef int32_t aaudio_sharing_mode_t;
120
Phil Burke2fbb592017-05-01 15:05:52 -0700121
122enum {
123 /**
124 * No particular performance needs. Default.
125 */
126 AAUDIO_PERFORMANCE_MODE_NONE = 10,
127
128 /**
129 * Extending battery life is most important.
130 */
131 AAUDIO_PERFORMANCE_MODE_POWER_SAVING,
132
133 /**
134 * Reducing latency is most important.
135 */
136 AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
137};
138typedef int32_t aaudio_performance_mode_t;
139
Phil Burke2155ef2017-02-24 13:50:29 -0800140typedef struct AAudioStreamStruct AAudioStream;
141typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800142
Phil Burk5ed503c2017-02-01 09:38:15 -0800143#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800144#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800145#endif
146
147// ============================================================
148// Audio System
149// ============================================================
150
151/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800152 * The text is the ASCII symbol corresponding to the returnCode,
153 * or an English message saying the returnCode 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 result code.
158 */
159AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
160
161/**
162 * The text is the ASCII symbol corresponding to the stream state,
163 * or an English message saying the state is unrecognized.
164 * This is intended for developers to use when debugging.
165 * It is not for display to users.
166 *
167 * @return pointer to a text representation of an AAudio state.
168 */
169AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
170
171// ============================================================
172// StreamBuilder
173// ============================================================
174
175/**
176 * Create a StreamBuilder that can be used to open a Stream.
177 *
178 * The deviceId is initially unspecified, meaning that the current default device will be used.
179 *
180 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800181 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800182 * The data format, samplesPerFrames and sampleRate are unspecified and will be
183 * chosen by the device when it is opened.
184 *
185 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
186 */
Phil Burke2155ef2017-02-24 13:50:29 -0800187AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800188
189/**
190 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800191 * On Android, for example, the ID could be obtained from the Java AudioManager.
192 *
Glenn Kasten5f510d22017-05-30 15:52:15 -0700193 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED,
Phil Burke057ca92017-03-28 11:31:34 -0700194 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800195 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800196 * @param builder reference provided by AAudio_createStreamBuilder()
Glenn Kasten5f510d22017-05-30 15:52:15 -0700197 * @param deviceId device identifier or AAUDIO_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800198 */
Phil Burke2155ef2017-02-24 13:50:29 -0800199AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800200 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800201
202/**
Phil Burke057ca92017-03-28 11:31:34 -0700203 * Request a sample rate in Hertz.
204 *
Phil Burke057ca92017-03-28 11:31:34 -0700205 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700206 * An optimal value will then be chosen when the stream is opened.
207 * After opening a stream with an unspecified value, the application must
208 * query for the actual value, which may vary by device.
209 *
210 * If an exact value is specified then an opened stream will use that value.
211 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700212 *
213 * @param builder reference provided by AAudio_createStreamBuilder()
214 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800215 */
Phil Burke2155ef2017-02-24 13:50:29 -0800216AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800217 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800218
219/**
Phil Burk20523ed2017-04-24 17:04:01 -0700220 * Request a number of channels for the stream.
Phil Burke057ca92017-03-28 11:31:34 -0700221 *
Phil Burke057ca92017-03-28 11:31:34 -0700222 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700223 * An optimal value will then be chosen when the stream is opened.
224 * After opening a stream with an unspecified value, the application must
225 * query for the actual value, which may vary by device.
Phil Burk5ed503c2017-02-01 09:38:15 -0800226 *
Phil Burk8f624892017-05-11 11:44:20 -0700227 * If an exact value is specified then an opened stream will use that value.
228 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700229 *
230 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk20523ed2017-04-24 17:04:01 -0700231 * @param channelCount Number of channels desired.
Phil Burk5ed503c2017-02-01 09:38:15 -0800232 */
Phil Burk20523ed2017-04-24 17:04:01 -0700233AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
234 int32_t channelCount);
235
236/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800237 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700238 *
239 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700240 * An optimal value will then be chosen when the stream is opened.
241 * After opening a stream with an unspecified value, the application must
242 * query for the actual value, which may vary by device.
Phil Burke057ca92017-03-28 11:31:34 -0700243 *
Phil Burk8f624892017-05-11 11:44:20 -0700244 * If an exact value is specified then an opened stream will use that value.
245 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700246 *
247 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk8f624892017-05-11 11:44:20 -0700248 * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800249 */
Phil Burke2155ef2017-02-24 13:50:29 -0800250AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk9dca9822017-05-26 14:27:43 -0700251 aaudio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800252
253/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800254 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700255 *
256 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
257 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700259 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800260 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800261 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700262 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800263 */
Phil Burke2155ef2017-02-24 13:50:29 -0800264AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800265 aaudio_sharing_mode_t sharingMode);
266
267/**
Phil Burke057ca92017-03-28 11:31:34 -0700268 * Request the direction for a stream.
269 *
270 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800272 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800273 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800274 */
Phil Burke2155ef2017-02-24 13:50:29 -0800275AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800276 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800277
278/**
Phil Burke057ca92017-03-28 11:31:34 -0700279 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800280 * The final AAudioStream capacity may differ, but will probably be at least this big.
281 *
Phil Burke057ca92017-03-28 11:31:34 -0700282 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800283 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800284 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700285 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800286 */
Phil Burke2155ef2017-02-24 13:50:29 -0800287AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700288 int32_t numFrames);
Phil Burke2fbb592017-05-01 15:05:52 -0700289
290/**
291 * Set the requested performance mode.
292 *
293 * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
294 *
295 * @param builder reference provided by AAudio_createStreamBuilder()
296 * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
297 */
298AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
299 aaudio_performance_mode_t mode);
300
Phil Burke057ca92017-03-28 11:31:34 -0700301/**
302 * Return one of these values from the data callback function.
303 */
304enum {
305
306 /**
307 * Continue calling the callback.
308 */
309 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
310
311 /**
312 * Stop calling the callback.
313 *
314 * The application will still need to call AAudioStream_requestPause()
315 * or AAudioStream_requestStop().
316 */
317 AAUDIO_CALLBACK_RESULT_STOP,
318
319};
320typedef int32_t aaudio_data_callback_result_t;
321
322/**
323 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
324 *
325 * For an output stream, this function should render and write numFrames of data
326 * in the streams current data format to the audioData buffer.
327 *
328 * For an input stream, this function should read and process numFrames of data
329 * from the audioData buffer.
330 *
331 * Note that this callback function should be considered a "real-time" function.
332 * It must not do anything that could cause an unbounded delay because that can cause the
333 * audio to glitch or pop.
334 *
335 * These are things the function should NOT do:
336 * <ul>
337 * <li>allocate memory using, for example, malloc() or new</li>
338 * <li>any file operations such as opening, closing, reading or writing</li>
339 * <li>any network operations such as streaming</li>
340 * <li>use any mutexes or other synchronization primitives</li>
341 * <li>sleep</li>
342 * </ul>
343 *
344 * If you need to move data, eg. MIDI commands, in or out of the callback function then
345 * we recommend the use of non-blocking techniques such as an atomic FIFO.
346 *
347 * @param stream reference provided by AAudioStreamBuilder_openStream()
348 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
349 * @param audioData a pointer to the audio data
350 * @param numFrames the number of frames to be processed
351 * @return AAUDIO_CALLBACK_RESULT_*
352 */
353typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
354 AAudioStream *stream,
355 void *userData,
356 void *audioData,
357 int32_t numFrames);
358
359/**
360 * Request that AAudio call this functions when the stream is running.
361 *
362 * Note that when using this callback, the audio data will be passed in or out
363 * of the function as an argument.
364 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
365 * that has an active data callback.
366 *
367 * The callback function will start being called after AAudioStream_requestStart() is called.
368 * It will stop being called after AAudioStream_requestPause() or
369 * AAudioStream_requestStop() is called.
370 *
371 * This callback function will be called on a real-time thread owned by AAudio. See
Glenn Kasten0d804362017-04-13 09:20:14 -0700372 * {@link AAudioStream_dataCallback} for more information.
Phil Burke057ca92017-03-28 11:31:34 -0700373 *
374 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
375 *
376 * @param builder reference provided by AAudio_createStreamBuilder()
377 * @param callback pointer to a function that will process audio data.
378 * @param userData pointer to an application data structure that will be passed
379 * to the callback functions.
380 */
381AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
382 AAudioStream_dataCallback callback,
383 void *userData);
384
385/**
386 * Set the requested data callback buffer size in frames.
387 * See {@link AAudioStream_dataCallback}.
388 *
389 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
390 *
391 * For the lowest possible latency, do not call this function. AAudio will then
392 * call the dataProc callback function with whatever size is optimal.
393 * That size may vary from one callback to another.
394 *
395 * Only use this function if the application requires a specific number of frames for processing.
396 * The application might, for example, be using an FFT that requires
397 * a specific power-of-two sized buffer.
398 *
399 * AAudio may need to add additional buffering in order to adapt between the internal
400 * buffer size and the requested buffer size.
401 *
402 * If you do call this function then the requested size should be less than
403 * half the buffer capacity, to allow double buffering.
404 *
405 * @param builder reference provided by AAudio_createStreamBuilder()
406 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
407 */
408AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
409 int32_t numFrames);
410
411/**
412 * Prototype for the callback function that is passed to
413 * AAudioStreamBuilder_setErrorCallback().
414 *
415 * @param stream reference provided by AAudioStreamBuilder_openStream()
416 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
417 * @param error an AAUDIO_ERROR_* value.
418 */
419typedef void (*AAudioStream_errorCallback)(
420 AAudioStream *stream,
421 void *userData,
422 aaudio_result_t error);
423
424/**
425 * Request that AAudio call this functions if any error occurs on a callback thread.
426 *
427 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
428 * device to be unavailable.
429 * In response, this function could signal or launch another thread to reopen a
430 * stream on another device. Do not reopen the stream in this callback.
431 *
432 * This will not be called because of actions by the application, such as stopping
433 * or closing a stream.
434 *
435 * Another possible cause of error would be a timeout or an unanticipated internal error.
436 *
437 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
438 *
439 * @param builder reference provided by AAudio_createStreamBuilder()
440 * @param callback pointer to a function that will be called if an error occurs.
441 * @param userData pointer to an application data structure that will be passed
442 * to the callback functions.
443 */
444AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
445 AAudioStream_errorCallback callback,
446 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800447
448/**
449 * Open a stream based on the options in the StreamBuilder.
450 *
451 * AAudioStream_close must be called when finished with the stream to recover
452 * the memory and to free the associated resources.
453 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800454 * @param builder reference provided by AAudio_createStreamBuilder()
455 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800456 * @return AAUDIO_OK or a negative error.
457 */
Phil Burke2155ef2017-02-24 13:50:29 -0800458AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
459 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800460
461/**
462 * Delete the resources associated with the StreamBuilder.
463 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800464 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800465 * @return AAUDIO_OK or a negative error.
466 */
Phil Burke2155ef2017-02-24 13:50:29 -0800467AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800468
469// ============================================================
470// Stream Control
471// ============================================================
472
473/**
474 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
475 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800476 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800477 * @return AAUDIO_OK or a negative error.
478 */
Phil Burke2155ef2017-02-24 13:50:29 -0800479AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800480
481/**
482 * Asynchronously request to start playing the stream. For output streams, one should
483 * write to the stream to fill the buffer before starting.
484 * Otherwise it will underflow.
485 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
486 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800487 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800488 * @return AAUDIO_OK or a negative error.
489 */
Phil Burke2155ef2017-02-24 13:50:29 -0800490AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800491
492/**
493 * Asynchronous request for the stream to pause.
494 * Pausing a stream will freeze the data flow but not flush any buffers.
495 * Use AAudioStream_Start() to resume playback after a pause.
496 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
497 *
Phil Burk068c10f2017-05-08 16:36:41 -0700498 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
499 * For input streams use AAudioStream_requestStop().
500 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800501 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800502 * @return AAUDIO_OK or a negative error.
503 */
Phil Burke2155ef2017-02-24 13:50:29 -0800504AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800505
506/**
507 * Asynchronous request for the stream to flush.
508 * Flushing will discard any pending data.
509 * This call only works if the stream is pausing or paused. TODO review
510 * Frame counters are not reset by a flush. They may be advanced.
511 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
512 *
Phil Burk068c10f2017-05-08 16:36:41 -0700513 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
514 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800515 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800516 * @return AAUDIO_OK or a negative error.
517 */
Phil Burke2155ef2017-02-24 13:50:29 -0800518AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800519
520/**
521 * Asynchronous request for the stream to stop.
522 * The stream will stop after all of the data currently buffered has been played.
523 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
524 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800525 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800526 * @return AAUDIO_OK or a negative error.
527 */
Phil Burke2155ef2017-02-24 13:50:29 -0800528AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800529
530/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800531 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800532 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800533 * This function will immediately return the state without updating the state.
534 * If you want to update the client state based on the server state then
535 * call AAudioStream_waitForStateChange() with currentState
536 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
537 *
538 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800539 */
Phil Burke2155ef2017-02-24 13:50:29 -0800540AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800541
542/**
543 * Wait until the current state no longer matches the input state.
544 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800545 * This will update the current client state.
546 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800547 * <pre><code>
548 * aaudio_stream_state_t currentState;
549 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
550 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
551 * result = AAudioStream_waitForStateChange(
552 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
553 * }
554 * </code></pre>
555 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800556 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800557 * @param inputState The state we want to avoid.
558 * @param nextState Pointer to a variable that will be set to the new state.
559 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
560 * @return AAUDIO_OK or a negative error.
561 */
Phil Burke2155ef2017-02-24 13:50:29 -0800562AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800563 aaudio_stream_state_t inputState,
564 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800565 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800566
567// ============================================================
568// Stream I/O
569// ============================================================
570
571/**
572 * Read data from the stream.
573 *
574 * The call will wait until the read is complete or until it runs out of time.
575 * If timeoutNanos is zero then this call will not wait.
576 *
577 * Note that timeoutNanoseconds is a relative duration in wall clock time.
578 * Time will not stop if the thread is asleep.
579 * So it will be implemented using CLOCK_BOOTTIME.
580 *
581 * This call is "strong non-blocking" unless it has to wait for data.
582 *
583 * @param stream A stream created using AAudioStreamBuilder_openStream().
584 * @param buffer The address of the first sample.
585 * @param numFrames Number of frames to read. Only complete frames will be written.
586 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800587 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800588 */
Phil Burke2155ef2017-02-24 13:50:29 -0800589AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800590 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800591 int32_t numFrames,
592 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800593
594/**
595 * Write data to the stream.
596 *
597 * The call will wait until the write is complete or until it runs out of time.
598 * If timeoutNanos is zero then this call will not wait.
599 *
600 * Note that timeoutNanoseconds is a relative duration in wall clock time.
601 * Time will not stop if the thread is asleep.
602 * So it will be implemented using CLOCK_BOOTTIME.
603 *
604 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
605 *
606 * @param stream A stream created using AAudioStreamBuilder_openStream().
607 * @param buffer The address of the first sample.
608 * @param numFrames Number of frames to write. Only complete frames will be written.
609 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
610 * @return The number of frames actually written or a negative error.
611 */
Phil Burke2155ef2017-02-24 13:50:29 -0800612AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800613 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800614 int32_t numFrames,
615 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800616
Phil Burk5ed503c2017-02-01 09:38:15 -0800617// ============================================================
618// Stream - queries
619// ============================================================
620
Phil Burk5ed503c2017-02-01 09:38:15 -0800621/**
622 * This can be used to adjust the latency of the buffer by changing
623 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800624 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800625 * at run-time for each device.
626 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800627 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800628 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800629 * Note that you will probably not get the exact size you request.
630 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
631 *
632 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700633 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800634 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800635 */
Phil Burke2155ef2017-02-24 13:50:29 -0800636AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700637 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800638
639/**
640 * Query the maximum number of frames that can be filled without blocking.
641 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800642 * @param stream reference provided by AAudioStreamBuilder_openStream()
643 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800644 */
Phil Burke2155ef2017-02-24 13:50:29 -0800645AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800646
647/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800648 * Query the number of frames that the application should read or write at
649 * one time for optimal performance. It is OK if an application writes
650 * a different number of frames. But the buffer size may need to be larger
651 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800652 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800653 * Note that this may or may not match the actual device burst size.
654 * For some endpoints, the burst size can vary dynamically.
655 * But these tend to be devices with high latency.
656 *
657 * @param stream reference provided by AAudioStreamBuilder_openStream()
658 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800659 */
Phil Burke2155ef2017-02-24 13:50:29 -0800660AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800661
662/**
663 * Query maximum buffer capacity in frames.
664 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800665 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700666 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800667 */
Phil Burke2155ef2017-02-24 13:50:29 -0800668AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800669
670/**
Phil Burke057ca92017-03-28 11:31:34 -0700671 * Query the size of the buffer that will be passed to the dataProc callback
672 * in the numFrames parameter.
673 *
674 * This call can be used if the application needs to know the value of numFrames before
675 * the stream is started. This is not normally necessary.
676 *
677 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
678 * then this will be the same size.
679 *
680 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
681 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
682 *
683 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
684 * may vary from one dataProc callback to the next.
685 *
686 * @param stream reference provided by AAudioStreamBuilder_openStream()
687 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
688 */
689AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
690
691/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800692 * An XRun is an Underrun or an Overrun.
693 * During playing, an underrun will occur if the stream is not written in time
694 * and the system runs out of valid data.
695 * During recording, an overrun will occur if the stream is not read in time
696 * and there is no place to put the incoming data so it is discarded.
697 *
698 * An underrun or overrun can cause an audible "pop" or "glitch".
699 *
Phil Burk068c10f2017-05-08 16:36:41 -0700700 * Note that some INPUT devices may not support this function.
701 * In that case a 0 will always be returned.
702 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800703 * @param stream reference provided by AAudioStreamBuilder_openStream()
704 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800705 */
Phil Burke2155ef2017-02-24 13:50:29 -0800706AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800707
708/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800709 * @param stream reference provided by AAudioStreamBuilder_openStream()
710 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800711 */
Phil Burke2155ef2017-02-24 13:50:29 -0800712AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800713
714/**
Phil Burk20523ed2017-04-24 17:04:01 -0700715 * A stream has one or more channels of data.
716 * A frame will contain one sample for each channel.
717 *
718 * @param stream reference provided by AAudioStreamBuilder_openStream()
719 * @return actual number of channels
720 */
721AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);
722
723/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800724 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800725 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800726 */
Phil Burke2155ef2017-02-24 13:50:29 -0800727AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800728
729/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800730 * @param stream reference provided by AAudioStreamBuilder_openStream()
731 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800732 */
Phil Burk9dca9822017-05-26 14:27:43 -0700733AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800734
735/**
736 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800737 * @param stream reference provided by AAudioStreamBuilder_openStream()
738 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800739 */
Phil Burke2155ef2017-02-24 13:50:29 -0800740AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800741
742/**
Phil Burke2fbb592017-05-01 15:05:52 -0700743 * Get the performance mode used by the stream.
744 *
745 * @param stream reference provided by AAudioStreamBuilder_openStream()
746 */
747AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);
748
749/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800750 * @param stream reference provided by AAudioStreamBuilder_openStream()
751 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800752 */
Phil Burke2155ef2017-02-24 13:50:29 -0800753AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800754
755/**
756 * Passes back the number of frames that have been written since the stream was created.
757 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800758 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800759 *
760 * The frame position is monotonically increasing.
761 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800762 * @param stream reference provided by AAudioStreamBuilder_openStream()
763 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800764 */
Phil Burke2155ef2017-02-24 13:50:29 -0800765AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800766
767/**
768 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800769 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800770 * For an input stream, this will be advanced by the application calling read().
771 *
772 * The frame position is monotonically increasing.
773 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800774 * @param stream reference provided by AAudioStreamBuilder_openStream()
775 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800776 */
Phil Burke2155ef2017-02-24 13:50:29 -0800777AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800778
779/**
780 * Passes back the time at which a particular frame was presented.
781 * This can be used to synchronize audio with video or MIDI.
782 * It can also be used to align a recorded stream with a playback stream.
783 *
784 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
785 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
786 * Note that because requestStart() is asynchronous, timestamps will not be valid until
787 * a short time after calling requestStart().
788 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
789 * Just try calling again later.
790 *
791 * If an error occurs, then the position and time will not be modified.
792 *
793 * The position and time passed back are monotonically increasing.
794 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800795 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burkec159502017-07-25 17:33:47 -0700796 * @param clockid CLOCK_MONOTONIC or CLOCK_BOOTTIME
Phil Burk5ed503c2017-02-01 09:38:15 -0800797 * @param framePosition pointer to a variable to receive the position
798 * @param timeNanoseconds pointer to a variable to receive the time
799 * @return AAUDIO_OK or a negative error
800 */
Phil Burke2155ef2017-02-24 13:50:29 -0800801AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800802 clockid_t clockid,
803 int64_t *framePosition,
804 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800805
806#ifdef __cplusplus
807}
808#endif
809
810#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700811
812/** @} */