blob: 106561155a80821c0d4ad6c5fdb63d720594cf19 [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
60/**
61 * @deprecated use aaudio_format_t instead
62 * TODO remove when tests and examples are updated
63 */
64typedef int32_t aaudio_audio_format_t;
65
66enum {
67 AAUDIO_OK,
68 AAUDIO_ERROR_BASE = -900, // TODO review
69 AAUDIO_ERROR_DISCONNECTED,
70 AAUDIO_ERROR_ILLEGAL_ARGUMENT,
71 AAUDIO_ERROR_INCOMPATIBLE,
72 AAUDIO_ERROR_INTERNAL, // an underlying API returned an error code
73 AAUDIO_ERROR_INVALID_STATE,
74 AAUDIO_ERROR_UNEXPECTED_STATE,
75 AAUDIO_ERROR_UNEXPECTED_VALUE,
76 AAUDIO_ERROR_INVALID_HANDLE,
77 AAUDIO_ERROR_INVALID_QUERY,
78 AAUDIO_ERROR_UNIMPLEMENTED,
79 AAUDIO_ERROR_UNAVAILABLE,
80 AAUDIO_ERROR_NO_FREE_HANDLES,
81 AAUDIO_ERROR_NO_MEMORY,
82 AAUDIO_ERROR_NULL,
83 AAUDIO_ERROR_TIMEOUT,
84 AAUDIO_ERROR_WOULD_BLOCK,
85 AAUDIO_ERROR_INVALID_FORMAT,
86 AAUDIO_ERROR_OUT_OF_RANGE,
87 AAUDIO_ERROR_NO_SERVICE,
88 AAUDIO_ERROR_INVALID_RATE
89};
90typedef int32_t aaudio_result_t;
91
92enum
93{
94 AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
95 AAUDIO_STREAM_STATE_UNKNOWN,
96 AAUDIO_STREAM_STATE_OPEN,
97 AAUDIO_STREAM_STATE_STARTING,
98 AAUDIO_STREAM_STATE_STARTED,
99 AAUDIO_STREAM_STATE_PAUSING,
100 AAUDIO_STREAM_STATE_PAUSED,
101 AAUDIO_STREAM_STATE_FLUSHING,
102 AAUDIO_STREAM_STATE_FLUSHED,
103 AAUDIO_STREAM_STATE_STOPPING,
104 AAUDIO_STREAM_STATE_STOPPED,
105 AAUDIO_STREAM_STATE_CLOSING,
106 AAUDIO_STREAM_STATE_CLOSED,
107 AAUDIO_STREAM_STATE_DISCONNECTED
108};
109typedef int32_t aaudio_stream_state_t;
110
111
112enum {
113 /**
114 * This will be the only stream using a particular source or sink.
115 * This mode will provide the lowest possible latency.
116 * You should close EXCLUSIVE streams immediately when you are not using them.
117 */
118 AAUDIO_SHARING_MODE_EXCLUSIVE,
119 /**
120 * Multiple applications will be mixed by the AAudio Server.
121 * This will have higher latency than the EXCLUSIVE mode.
122 */
123 AAUDIO_SHARING_MODE_SHARED
124};
125typedef int32_t aaudio_sharing_mode_t;
126
Phil Burke2fbb592017-05-01 15:05:52 -0700127
128enum {
129 /**
130 * No particular performance needs. Default.
131 */
132 AAUDIO_PERFORMANCE_MODE_NONE = 10,
133
134 /**
135 * Extending battery life is most important.
136 */
137 AAUDIO_PERFORMANCE_MODE_POWER_SAVING,
138
139 /**
140 * Reducing latency is most important.
141 */
142 AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
143};
144typedef int32_t aaudio_performance_mode_t;
145
Phil Burke2155ef2017-02-24 13:50:29 -0800146typedef struct AAudioStreamStruct AAudioStream;
147typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800148
Phil Burk5ed503c2017-02-01 09:38:15 -0800149#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800150#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800151#endif
152
153// ============================================================
154// Audio System
155// ============================================================
156
157/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800158 * The text is the ASCII symbol corresponding to the returnCode,
159 * or an English message saying the returnCode is unrecognized.
160 * This is intended for developers to use when debugging.
161 * It is not for display to users.
162 *
163 * @return pointer to a text representation of an AAudio result code.
164 */
165AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
166
167/**
168 * The text is the ASCII symbol corresponding to the stream state,
169 * or an English message saying the state is unrecognized.
170 * This is intended for developers to use when debugging.
171 * It is not for display to users.
172 *
173 * @return pointer to a text representation of an AAudio state.
174 */
175AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
176
177// ============================================================
178// StreamBuilder
179// ============================================================
180
181/**
182 * Create a StreamBuilder that can be used to open a Stream.
183 *
184 * The deviceId is initially unspecified, meaning that the current default device will be used.
185 *
186 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800187 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800188 * The data format, samplesPerFrames and sampleRate are unspecified and will be
189 * chosen by the device when it is opened.
190 *
191 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
192 */
Phil Burke2155ef2017-02-24 13:50:29 -0800193AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800194
195/**
196 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800197 * On Android, for example, the ID could be obtained from the Java AudioManager.
198 *
Glenn Kasten5f510d22017-05-30 15:52:15 -0700199 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED,
Phil Burke057ca92017-03-28 11:31:34 -0700200 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800201 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800202 * @param builder reference provided by AAudio_createStreamBuilder()
Glenn Kasten5f510d22017-05-30 15:52:15 -0700203 * @param deviceId device identifier or AAUDIO_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 */
Phil Burke2155ef2017-02-24 13:50:29 -0800205AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800206 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800207
208/**
Phil Burke057ca92017-03-28 11:31:34 -0700209 * Request a sample rate in Hertz.
210 *
Phil Burke057ca92017-03-28 11:31:34 -0700211 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700212 * An optimal value will then be chosen when the stream is opened.
213 * After opening a stream with an unspecified value, the application must
214 * query for the actual value, which may vary by device.
215 *
216 * If an exact value is specified then an opened stream will use that value.
217 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700218 *
219 * @param builder reference provided by AAudio_createStreamBuilder()
220 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 */
Phil Burke2155ef2017-02-24 13:50:29 -0800222AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800223 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800224
225/**
Phil Burk20523ed2017-04-24 17:04:01 -0700226 * Request a number of channels for the stream.
Phil Burke057ca92017-03-28 11:31:34 -0700227 *
Phil Burke057ca92017-03-28 11:31:34 -0700228 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700229 * An optimal value will then be chosen when the stream is opened.
230 * After opening a stream with an unspecified value, the application must
231 * query for the actual value, which may vary by device.
Phil Burk5ed503c2017-02-01 09:38:15 -0800232 *
Phil Burk8f624892017-05-11 11:44:20 -0700233 * If an exact value is specified then an opened stream will use that value.
234 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700235 *
236 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk20523ed2017-04-24 17:04:01 -0700237 * @param channelCount Number of channels desired.
Phil Burk5ed503c2017-02-01 09:38:15 -0800238 */
Phil Burk20523ed2017-04-24 17:04:01 -0700239AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
240 int32_t channelCount);
241
242/**
243 *
244 * @deprecated use AAudioStreamBuilder_setChannelCount()
245 */
246// TODO remove as soon as the NDK and OS are in sync, before RC1
Phil Burke2155ef2017-02-24 13:50:29 -0800247AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk20523ed2017-04-24 17:04:01 -0700248 int32_t samplesPerFrame);
Phil Burk5ed503c2017-02-01 09:38:15 -0800249
250/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800251 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700252 *
253 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700254 * An optimal value will then be chosen when the stream is opened.
255 * After opening a stream with an unspecified value, the application must
256 * query for the actual value, which may vary by device.
Phil Burke057ca92017-03-28 11:31:34 -0700257 *
Phil Burk8f624892017-05-11 11:44:20 -0700258 * If an exact value is specified then an opened stream will use that value.
259 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700260 *
261 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk8f624892017-05-11 11:44:20 -0700262 * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800263 */
Phil Burke2155ef2017-02-24 13:50:29 -0800264AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800265 aaudio_audio_format_t format);
266
267/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800268 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700269 *
270 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
271 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800272 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700273 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800274 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800275 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700276 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 */
Phil Burke2155ef2017-02-24 13:50:29 -0800278AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800279 aaudio_sharing_mode_t sharingMode);
280
281/**
Phil Burke057ca92017-03-28 11:31:34 -0700282 * Request the direction for a stream.
283 *
284 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800285 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800286 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800287 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800288 */
Phil Burke2155ef2017-02-24 13:50:29 -0800289AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800290 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800291
292/**
Phil Burke057ca92017-03-28 11:31:34 -0700293 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800294 * The final AAudioStream capacity may differ, but will probably be at least this big.
295 *
Phil Burke057ca92017-03-28 11:31:34 -0700296 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800297 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800298 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700299 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800300 */
Phil Burke2155ef2017-02-24 13:50:29 -0800301AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700302 int32_t numFrames);
Phil Burke2fbb592017-05-01 15:05:52 -0700303
304/**
305 * Set the requested performance mode.
306 *
307 * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
308 *
309 * @param builder reference provided by AAudio_createStreamBuilder()
310 * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
311 */
312AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
313 aaudio_performance_mode_t mode);
314
Phil Burke057ca92017-03-28 11:31:34 -0700315/**
316 * Return one of these values from the data callback function.
317 */
318enum {
319
320 /**
321 * Continue calling the callback.
322 */
323 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
324
325 /**
326 * Stop calling the callback.
327 *
328 * The application will still need to call AAudioStream_requestPause()
329 * or AAudioStream_requestStop().
330 */
331 AAUDIO_CALLBACK_RESULT_STOP,
332
333};
334typedef int32_t aaudio_data_callback_result_t;
335
336/**
337 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
338 *
339 * For an output stream, this function should render and write numFrames of data
340 * in the streams current data format to the audioData buffer.
341 *
342 * For an input stream, this function should read and process numFrames of data
343 * from the audioData buffer.
344 *
345 * Note that this callback function should be considered a "real-time" function.
346 * It must not do anything that could cause an unbounded delay because that can cause the
347 * audio to glitch or pop.
348 *
349 * These are things the function should NOT do:
350 * <ul>
351 * <li>allocate memory using, for example, malloc() or new</li>
352 * <li>any file operations such as opening, closing, reading or writing</li>
353 * <li>any network operations such as streaming</li>
354 * <li>use any mutexes or other synchronization primitives</li>
355 * <li>sleep</li>
356 * </ul>
357 *
358 * If you need to move data, eg. MIDI commands, in or out of the callback function then
359 * we recommend the use of non-blocking techniques such as an atomic FIFO.
360 *
361 * @param stream reference provided by AAudioStreamBuilder_openStream()
362 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
363 * @param audioData a pointer to the audio data
364 * @param numFrames the number of frames to be processed
365 * @return AAUDIO_CALLBACK_RESULT_*
366 */
367typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
368 AAudioStream *stream,
369 void *userData,
370 void *audioData,
371 int32_t numFrames);
372
373/**
374 * Request that AAudio call this functions when the stream is running.
375 *
376 * Note that when using this callback, the audio data will be passed in or out
377 * of the function as an argument.
378 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
379 * that has an active data callback.
380 *
381 * The callback function will start being called after AAudioStream_requestStart() is called.
382 * It will stop being called after AAudioStream_requestPause() or
383 * AAudioStream_requestStop() is called.
384 *
385 * This callback function will be called on a real-time thread owned by AAudio. See
Glenn Kasten0d804362017-04-13 09:20:14 -0700386 * {@link AAudioStream_dataCallback} for more information.
Phil Burke057ca92017-03-28 11:31:34 -0700387 *
388 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
389 *
390 * @param builder reference provided by AAudio_createStreamBuilder()
391 * @param callback pointer to a function that will process audio data.
392 * @param userData pointer to an application data structure that will be passed
393 * to the callback functions.
394 */
395AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
396 AAudioStream_dataCallback callback,
397 void *userData);
398
399/**
400 * Set the requested data callback buffer size in frames.
401 * See {@link AAudioStream_dataCallback}.
402 *
403 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
404 *
405 * For the lowest possible latency, do not call this function. AAudio will then
406 * call the dataProc callback function with whatever size is optimal.
407 * That size may vary from one callback to another.
408 *
409 * Only use this function if the application requires a specific number of frames for processing.
410 * The application might, for example, be using an FFT that requires
411 * a specific power-of-two sized buffer.
412 *
413 * AAudio may need to add additional buffering in order to adapt between the internal
414 * buffer size and the requested buffer size.
415 *
416 * If you do call this function then the requested size should be less than
417 * half the buffer capacity, to allow double buffering.
418 *
419 * @param builder reference provided by AAudio_createStreamBuilder()
420 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
421 */
422AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
423 int32_t numFrames);
424
425/**
426 * Prototype for the callback function that is passed to
427 * AAudioStreamBuilder_setErrorCallback().
428 *
429 * @param stream reference provided by AAudioStreamBuilder_openStream()
430 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
431 * @param error an AAUDIO_ERROR_* value.
432 */
433typedef void (*AAudioStream_errorCallback)(
434 AAudioStream *stream,
435 void *userData,
436 aaudio_result_t error);
437
438/**
439 * Request that AAudio call this functions if any error occurs on a callback thread.
440 *
441 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
442 * device to be unavailable.
443 * In response, this function could signal or launch another thread to reopen a
444 * stream on another device. Do not reopen the stream in this callback.
445 *
446 * This will not be called because of actions by the application, such as stopping
447 * or closing a stream.
448 *
449 * Another possible cause of error would be a timeout or an unanticipated internal error.
450 *
451 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
452 *
453 * @param builder reference provided by AAudio_createStreamBuilder()
454 * @param callback pointer to a function that will be called if an error occurs.
455 * @param userData pointer to an application data structure that will be passed
456 * to the callback functions.
457 */
458AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
459 AAudioStream_errorCallback callback,
460 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800461
462/**
463 * Open a stream based on the options in the StreamBuilder.
464 *
465 * AAudioStream_close must be called when finished with the stream to recover
466 * the memory and to free the associated resources.
467 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800468 * @param builder reference provided by AAudio_createStreamBuilder()
469 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800470 * @return AAUDIO_OK or a negative error.
471 */
Phil Burke2155ef2017-02-24 13:50:29 -0800472AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
473 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800474
475/**
476 * Delete the resources associated with the StreamBuilder.
477 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800478 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800479 * @return AAUDIO_OK or a negative error.
480 */
Phil Burke2155ef2017-02-24 13:50:29 -0800481AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800482
483// ============================================================
484// Stream Control
485// ============================================================
486
487/**
488 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
489 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800490 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800491 * @return AAUDIO_OK or a negative error.
492 */
Phil Burke2155ef2017-02-24 13:50:29 -0800493AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800494
495/**
496 * Asynchronously request to start playing the stream. For output streams, one should
497 * write to the stream to fill the buffer before starting.
498 * Otherwise it will underflow.
499 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
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_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800505
506/**
507 * Asynchronous request for the stream to pause.
508 * Pausing a stream will freeze the data flow but not flush any buffers.
509 * Use AAudioStream_Start() to resume playback after a pause.
510 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
511 *
Phil Burk068c10f2017-05-08 16:36:41 -0700512 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
513 * For input streams use AAudioStream_requestStop().
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_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800519
520/**
521 * Asynchronous request for the stream to flush.
522 * Flushing will discard any pending data.
523 * This call only works if the stream is pausing or paused. TODO review
524 * Frame counters are not reset by a flush. They may be advanced.
525 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
526 *
Phil Burk068c10f2017-05-08 16:36:41 -0700527 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
528 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800529 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800530 * @return AAUDIO_OK or a negative error.
531 */
Phil Burke2155ef2017-02-24 13:50:29 -0800532AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800533
534/**
535 * Asynchronous request for the stream to stop.
536 * The stream will stop after all of the data currently buffered has been played.
537 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
538 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800539 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800540 * @return AAUDIO_OK or a negative error.
541 */
Phil Burke2155ef2017-02-24 13:50:29 -0800542AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800543
544/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800545 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800546 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800547 * This function will immediately return the state without updating the state.
548 * If you want to update the client state based on the server state then
549 * call AAudioStream_waitForStateChange() with currentState
550 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
551 *
552 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800553 */
Phil Burke2155ef2017-02-24 13:50:29 -0800554AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800555
556/**
557 * Wait until the current state no longer matches the input state.
558 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800559 * This will update the current client state.
560 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800561 * <pre><code>
562 * aaudio_stream_state_t currentState;
563 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
564 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
565 * result = AAudioStream_waitForStateChange(
566 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
567 * }
568 * </code></pre>
569 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800570 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800571 * @param inputState The state we want to avoid.
572 * @param nextState Pointer to a variable that will be set to the new state.
573 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
574 * @return AAUDIO_OK or a negative error.
575 */
Phil Burke2155ef2017-02-24 13:50:29 -0800576AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800577 aaudio_stream_state_t inputState,
578 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800579 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800580
581// ============================================================
582// Stream I/O
583// ============================================================
584
585/**
586 * Read data from the stream.
587 *
588 * The call will wait until the read is complete or until it runs out of time.
589 * If timeoutNanos is zero then this call will not wait.
590 *
591 * Note that timeoutNanoseconds is a relative duration in wall clock time.
592 * Time will not stop if the thread is asleep.
593 * So it will be implemented using CLOCK_BOOTTIME.
594 *
595 * This call is "strong non-blocking" unless it has to wait for data.
596 *
597 * @param stream A stream created using AAudioStreamBuilder_openStream().
598 * @param buffer The address of the first sample.
599 * @param numFrames Number of frames to read. Only complete frames will be written.
600 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800601 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800602 */
Phil Burke2155ef2017-02-24 13:50:29 -0800603AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800604 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800605 int32_t numFrames,
606 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800607
608/**
609 * Write data to the stream.
610 *
611 * The call will wait until the write is complete or until it runs out of time.
612 * If timeoutNanos is zero then this call will not wait.
613 *
614 * Note that timeoutNanoseconds is a relative duration in wall clock time.
615 * Time will not stop if the thread is asleep.
616 * So it will be implemented using CLOCK_BOOTTIME.
617 *
618 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
619 *
620 * @param stream A stream created using AAudioStreamBuilder_openStream().
621 * @param buffer The address of the first sample.
622 * @param numFrames Number of frames to write. Only complete frames will be written.
623 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
624 * @return The number of frames actually written or a negative error.
625 */
Phil Burke2155ef2017-02-24 13:50:29 -0800626AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800627 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800628 int32_t numFrames,
629 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800630
Phil Burk5ed503c2017-02-01 09:38:15 -0800631// ============================================================
632// Stream - queries
633// ============================================================
634
Phil Burk5ed503c2017-02-01 09:38:15 -0800635/**
636 * This can be used to adjust the latency of the buffer by changing
637 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800638 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800639 * at run-time for each device.
640 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800641 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800642 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800643 * Note that you will probably not get the exact size you request.
644 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
645 *
646 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700647 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800648 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800649 */
Phil Burke2155ef2017-02-24 13:50:29 -0800650AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700651 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800652
653/**
654 * Query the maximum number of frames that can be filled without blocking.
655 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800656 * @param stream reference provided by AAudioStreamBuilder_openStream()
657 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800658 */
Phil Burke2155ef2017-02-24 13:50:29 -0800659AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800660
661/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800662 * Query the number of frames that the application should read or write at
663 * one time for optimal performance. It is OK if an application writes
664 * a different number of frames. But the buffer size may need to be larger
665 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800666 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800667 * Note that this may or may not match the actual device burst size.
668 * For some endpoints, the burst size can vary dynamically.
669 * But these tend to be devices with high latency.
670 *
671 * @param stream reference provided by AAudioStreamBuilder_openStream()
672 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800673 */
Phil Burke2155ef2017-02-24 13:50:29 -0800674AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800675
676/**
677 * Query maximum buffer capacity in frames.
678 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800679 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700680 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800681 */
Phil Burke2155ef2017-02-24 13:50:29 -0800682AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800683
684/**
Phil Burke057ca92017-03-28 11:31:34 -0700685 * Query the size of the buffer that will be passed to the dataProc callback
686 * in the numFrames parameter.
687 *
688 * This call can be used if the application needs to know the value of numFrames before
689 * the stream is started. This is not normally necessary.
690 *
691 * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
692 * then this will be the same size.
693 *
694 * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
695 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
696 *
697 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
698 * may vary from one dataProc callback to the next.
699 *
700 * @param stream reference provided by AAudioStreamBuilder_openStream()
701 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
702 */
703AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
704
705/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800706 * An XRun is an Underrun or an Overrun.
707 * During playing, an underrun will occur if the stream is not written in time
708 * and the system runs out of valid data.
709 * During recording, an overrun will occur if the stream is not read in time
710 * and there is no place to put the incoming data so it is discarded.
711 *
712 * An underrun or overrun can cause an audible "pop" or "glitch".
713 *
Phil Burk068c10f2017-05-08 16:36:41 -0700714 * Note that some INPUT devices may not support this function.
715 * In that case a 0 will always be returned.
716 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800717 * @param stream reference provided by AAudioStreamBuilder_openStream()
718 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800719 */
Phil Burke2155ef2017-02-24 13:50:29 -0800720AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800721
722/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800723 * @param stream reference provided by AAudioStreamBuilder_openStream()
724 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800725 */
Phil Burke2155ef2017-02-24 13:50:29 -0800726AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800727
728/**
Phil Burk20523ed2017-04-24 17:04:01 -0700729 * A stream has one or more channels of data.
730 * A frame will contain one sample for each channel.
731 *
732 * @param stream reference provided by AAudioStreamBuilder_openStream()
733 * @return actual number of channels
734 */
735AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);
736
737/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800738 * The samplesPerFrame is also known as channelCount.
739 *
Phil Burk20523ed2017-04-24 17:04:01 -0700740 * @deprecated use AAudioStream_getChannelCount()
Phil Burk3316d5e2017-02-15 11:23:01 -0800741 * @param stream reference provided by AAudioStreamBuilder_openStream()
742 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800743 */
Phil Burke2155ef2017-02-24 13:50:29 -0800744AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800745
746/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800747 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800748 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800749 */
Phil Burke2155ef2017-02-24 13:50:29 -0800750AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800751
752/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800753 * @param stream reference provided by AAudioStreamBuilder_openStream()
754 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800755 */
Phil Burke2155ef2017-02-24 13:50:29 -0800756AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800757
758/**
759 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800760 * @param stream reference provided by AAudioStreamBuilder_openStream()
761 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800762 */
Phil Burke2155ef2017-02-24 13:50:29 -0800763AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800764
765/**
Phil Burke2fbb592017-05-01 15:05:52 -0700766 * Get the performance mode used by the stream.
767 *
768 * @param stream reference provided by AAudioStreamBuilder_openStream()
769 */
770AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);
771
772/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800773 * @param stream reference provided by AAudioStreamBuilder_openStream()
774 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800775 */
Phil Burke2155ef2017-02-24 13:50:29 -0800776AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800777
778/**
779 * Passes back the number of frames that have been written since the stream was created.
780 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800781 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800782 *
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 written
Phil Burk5ed503c2017-02-01 09:38:15 -0800787 */
Phil Burke2155ef2017-02-24 13:50:29 -0800788AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800789
790/**
791 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800792 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800793 * For an input stream, this will be advanced by the application calling read().
794 *
795 * The frame position is monotonically increasing.
796 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800797 * @param stream reference provided by AAudioStreamBuilder_openStream()
798 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800799 */
Phil Burke2155ef2017-02-24 13:50:29 -0800800AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800801
802/**
803 * Passes back the time at which a particular frame was presented.
804 * This can be used to synchronize audio with video or MIDI.
805 * It can also be used to align a recorded stream with a playback stream.
806 *
807 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
808 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
809 * Note that because requestStart() is asynchronous, timestamps will not be valid until
810 * a short time after calling requestStart().
811 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
812 * Just try calling again later.
813 *
814 * If an error occurs, then the position and time will not be modified.
815 *
816 * The position and time passed back are monotonically increasing.
817 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800818 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800819 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
820 * @param framePosition pointer to a variable to receive the position
821 * @param timeNanoseconds pointer to a variable to receive the time
822 * @return AAUDIO_OK or a negative error
823 */
Phil Burke2155ef2017-02-24 13:50:29 -0800824AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800825 clockid_t clockid,
826 int64_t *framePosition,
827 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800828
829#ifdef __cplusplus
830}
831#endif
832
833#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700834
835/** @} */