blob: 7324137373709a72ddb7b4fe26b3455240d25de8 [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/**
18 * This is the 'C' ABI for AAudio.
19 */
20#ifndef AAUDIO_AAUDIO_H
21#define AAUDIO_AAUDIO_H
22
23#include "AAudioDefinitions.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29typedef aaudio_handle_t AAudioStream;
30typedef aaudio_handle_t AAudioStreamBuilder;
31
32#define AAUDIO_STREAM_NONE ((AAudioStream)AAUDIO_HANDLE_INVALID)
33#define AAUDIO_STREAM_BUILDER_NONE ((AAudioStreamBuilder)AAUDIO_HANDLE_INVALID)
34
35/* AAUDIO_API will probably get defined in a Makefile for a specific platform. */
36#ifndef AAUDIO_API
37#define AAUDIO_API /* for exporting symbols */
38#endif
39
40// ============================================================
41// Audio System
42// ============================================================
43
44/**
45 * @return time in the same clock domain as the timestamps
46 */
47AAUDIO_API aaudio_nanoseconds_t AAudio_getNanoseconds(aaudio_clockid_t clockid);
48
49/**
50 * The text is the ASCII symbol corresponding to the returnCode,
51 * or an English message saying the returnCode is unrecognized.
52 * This is intended for developers to use when debugging.
53 * It is not for display to users.
54 *
55 * @return pointer to a text representation of an AAudio result code.
56 */
57AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
58
59/**
60 * The text is the ASCII symbol corresponding to the stream state,
61 * or an English message saying the state is unrecognized.
62 * This is intended for developers to use when debugging.
63 * It is not for display to users.
64 *
65 * @return pointer to a text representation of an AAudio state.
66 */
67AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
68
69// ============================================================
70// StreamBuilder
71// ============================================================
72
73/**
74 * Create a StreamBuilder that can be used to open a Stream.
75 *
76 * The deviceId is initially unspecified, meaning that the current default device will be used.
77 *
78 * The default direction is AAUDIO_DIRECTION_OUTPUT.
79 * The default sharing mode is AAUDIO_SHARING_MODE_LEGACY.
80 * The data format, samplesPerFrames and sampleRate are unspecified and will be
81 * chosen by the device when it is opened.
82 *
83 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
84 */
85AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder *builder);
86
87/**
88 * Request an audio device identified device using an ID.
89 * The ID is platform specific.
90 * On Android, for example, the ID could be obtained from the Java AudioManager.
91 *
92 * By default, the primary device will be used.
93 *
94 * @param builder handle provided by AAudio_createStreamBuilder()
95 * @param deviceId platform specific identifier or AAUDIO_DEVICE_UNSPECIFIED
96 * @return AAUDIO_OK or a negative error.
97 */
98AAUDIO_API aaudio_result_t AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder builder,
99 aaudio_device_id_t deviceId);
100/**
101 * Passes back requested device ID.
102 * @return AAUDIO_OK or a negative error.
103 */
104AAUDIO_API aaudio_result_t AAudioStreamBuilder_getDeviceId(AAudioStreamBuilder builder,
105 aaudio_device_id_t *deviceId);
106
107/**
108 * Request a sample rate in Hz.
109 * The stream may be opened with a different sample rate.
110 * So the application should query for the actual rate after the stream is opened.
111 *
112 * Technically, this should be called the "frame rate" or "frames per second",
113 * because it refers to the number of complete frames transferred per second.
114 * But it is traditionally called "sample rate". Se we use that term.
115 *
116 * Default is AAUDIO_UNSPECIFIED.
117 *
118 * @return AAUDIO_OK or a negative error.
119 */
120AAUDIO_API aaudio_result_t AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder builder,
121 aaudio_sample_rate_t sampleRate);
122
123/**
124 * Returns sample rate in Hertz (samples per second).
125 * @return AAUDIO_OK or a negative error.
126 */
127AAUDIO_API aaudio_result_t AAudioStreamBuilder_getSampleRate(AAudioStreamBuilder builder,
128 aaudio_sample_rate_t *sampleRate);
129
130
131/**
132 * Request a number of samples per frame.
133 * The stream may be opened with a different value.
134 * So the application should query for the actual value after the stream is opened.
135 *
136 * Default is AAUDIO_UNSPECIFIED.
137 *
138 * Note, this quantity is sometimes referred to as "channel count".
139 *
140 * @return AAUDIO_OK or a negative error.
141 */
142AAUDIO_API aaudio_result_t AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder builder,
143 int32_t samplesPerFrame);
144
145/**
146 * Note, this quantity is sometimes referred to as "channel count".
147 *
148 * @param builder handle provided by AAudio_createStreamBuilder()
149 * @param samplesPerFrame pointer to a variable to be set to samplesPerFrame.
150 * @return AAUDIO_OK or a negative error.
151 */
152AAUDIO_API aaudio_result_t AAudioStreamBuilder_getSamplesPerFrame(AAudioStreamBuilder builder,
153 int32_t *samplesPerFrame);
154
155
156/**
157 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
158 * The application should query for the actual format after the stream is opened.
159 *
160 * @return AAUDIO_OK or a negative error.
161 */
162AAUDIO_API aaudio_result_t AAudioStreamBuilder_setFormat(AAudioStreamBuilder builder,
163 aaudio_audio_format_t format);
164
165/**
166 * @return AAUDIO_OK or a negative error.
167 */
168AAUDIO_API aaudio_result_t AAudioStreamBuilder_getFormat(AAudioStreamBuilder builder,
169 aaudio_audio_format_t *format);
170
171/**
172 * Request a mode for sharing the device.
173 * The requested sharing mode may not be available.
174 * So the application should query for the actual mode after the stream is opened.
175 *
176 * @param builder handle provided by AAudio_createStreamBuilder()
177 * @param sharingMode AAUDIO_SHARING_MODE_LEGACY or AAUDIO_SHARING_MODE_EXCLUSIVE
178 * @return AAUDIO_OK or a negative error.
179 */
180AAUDIO_API aaudio_result_t AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder builder,
181 aaudio_sharing_mode_t sharingMode);
182
183/**
184 * Return requested sharing mode.
185 * @return AAUDIO_OK or a negative error
186 */
187AAUDIO_API aaudio_result_t AAudioStreamBuilder_getSharingMode(AAudioStreamBuilder builder,
188 aaudio_sharing_mode_t *sharingMode);
189
190/**
191 * Request the direction for a stream. The default is AAUDIO_DIRECTION_OUTPUT.
192 *
193 * @param builder handle provided by AAudio_createStreamBuilder()
194 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
195 * @return AAUDIO_OK or a negative error.
196 */
197AAUDIO_API aaudio_result_t AAudioStreamBuilder_setDirection(AAudioStreamBuilder builder,
198 aaudio_direction_t direction);
199
200/**
201 * @param builder handle provided by AAudio_createStreamBuilder()
202 * @param direction pointer to a variable to be set to the currently requested direction.
203 * @return AAUDIO_OK or a negative error.
204 */
205AAUDIO_API aaudio_result_t AAudioStreamBuilder_getDirection(AAudioStreamBuilder builder,
206 aaudio_direction_t *direction);
207
208/**
209 * Open a stream based on the options in the StreamBuilder.
210 *
211 * AAudioStream_close must be called when finished with the stream to recover
212 * the memory and to free the associated resources.
213 *
214 * @param builder handle provided by AAudio_createStreamBuilder()
215 * @param stream pointer to a variable to receive the new stream handle
216 * @return AAUDIO_OK or a negative error.
217 */
218AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder builder,
219 AAudioStream *stream);
220
221/**
222 * Delete the resources associated with the StreamBuilder.
223 *
224 * @param builder handle provided by AAudio_createStreamBuilder()
225 * @return AAUDIO_OK or a negative error.
226 */
227AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder builder);
228
229// ============================================================
230// Stream Control
231// ============================================================
232
233/**
234 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
235 *
236 * @param stream handle provided by AAudioStreamBuilder_openStream()
237 * @return AAUDIO_OK or a negative error.
238 */
239AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream stream);
240
241/**
242 * Asynchronously request to start playing the stream. For output streams, one should
243 * write to the stream to fill the buffer before starting.
244 * Otherwise it will underflow.
245 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
246 *
247 * @param stream handle provided by AAudioStreamBuilder_openStream()
248 * @return AAUDIO_OK or a negative error.
249 */
250AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream stream);
251
252/**
253 * Asynchronous request for the stream to pause.
254 * Pausing a stream will freeze the data flow but not flush any buffers.
255 * Use AAudioStream_Start() to resume playback after a pause.
256 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
257 *
258 * @param stream handle provided by AAudioStreamBuilder_openStream()
259 * @return AAUDIO_OK or a negative error.
260 */
261AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream stream);
262
263/**
264 * Asynchronous request for the stream to flush.
265 * Flushing will discard any pending data.
266 * This call only works if the stream is pausing or paused. TODO review
267 * Frame counters are not reset by a flush. They may be advanced.
268 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
269 *
270 * @param stream handle provided by AAudioStreamBuilder_openStream()
271 * @return AAUDIO_OK or a negative error.
272 */
273AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream stream);
274
275/**
276 * Asynchronous request for the stream to stop.
277 * The stream will stop after all of the data currently buffered has been played.
278 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
279 *
280 * @param stream handle provided by AAudioStreamBuilder_openStream()
281 * @return AAUDIO_OK or a negative error.
282 */
283AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream stream);
284
285/**
286 * Query the current state, eg. AAUDIO_STREAM_STATE_PAUSING
287 *
288 * @param stream handle provided by AAudioStreamBuilder_openStream()
289 * @param state pointer to a variable that will be set to the current state
290 * @return AAUDIO_OK or a negative error.
291 */
292AAUDIO_API aaudio_result_t AAudioStream_getState(AAudioStream stream, aaudio_stream_state_t *state);
293
294/**
295 * Wait until the current state no longer matches the input state.
296 *
297 * <pre><code>
298 * aaudio_stream_state_t currentState;
299 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
300 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
301 * result = AAudioStream_waitForStateChange(
302 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
303 * }
304 * </code></pre>
305 *
306 * @param stream A handle provided by AAudioStreamBuilder_openStream()
307 * @param inputState The state we want to avoid.
308 * @param nextState Pointer to a variable that will be set to the new state.
309 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
310 * @return AAUDIO_OK or a negative error.
311 */
312AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream stream,
313 aaudio_stream_state_t inputState,
314 aaudio_stream_state_t *nextState,
315 aaudio_nanoseconds_t timeoutNanoseconds);
316
317// ============================================================
318// Stream I/O
319// ============================================================
320
321/**
322 * Read data from the stream.
323 *
324 * The call will wait until the read is complete or until it runs out of time.
325 * If timeoutNanos is zero then this call will not wait.
326 *
327 * Note that timeoutNanoseconds is a relative duration in wall clock time.
328 * Time will not stop if the thread is asleep.
329 * So it will be implemented using CLOCK_BOOTTIME.
330 *
331 * This call is "strong non-blocking" unless it has to wait for data.
332 *
333 * @param stream A stream created using AAudioStreamBuilder_openStream().
334 * @param buffer The address of the first sample.
335 * @param numFrames Number of frames to read. Only complete frames will be written.
336 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
337 * @return The number of frames actually written or a negative error.
338 */
339AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream stream,
340 void *buffer,
341 aaudio_size_frames_t numFrames,
342 aaudio_nanoseconds_t timeoutNanoseconds);
343
344/**
345 * Write data to the stream.
346 *
347 * The call will wait until the write is complete or until it runs out of time.
348 * If timeoutNanos is zero then this call will not wait.
349 *
350 * Note that timeoutNanoseconds is a relative duration in wall clock time.
351 * Time will not stop if the thread is asleep.
352 * So it will be implemented using CLOCK_BOOTTIME.
353 *
354 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
355 *
356 * @param stream A stream created using AAudioStreamBuilder_openStream().
357 * @param buffer The address of the first sample.
358 * @param numFrames Number of frames to write. Only complete frames will be written.
359 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
360 * @return The number of frames actually written or a negative error.
361 */
362AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream stream,
363 const void *buffer,
364 aaudio_size_frames_t numFrames,
365 aaudio_nanoseconds_t timeoutNanoseconds);
366
367
368// ============================================================
369// High priority audio threads
370// ============================================================
371
372typedef void *(aaudio_audio_thread_proc_t)(void *);
373
374/**
375 * Create a thread associated with a stream. The thread has special properties for
376 * low latency audio performance. This thread can be used to implement a callback API.
377 *
378 * Only one thread may be associated with a stream.
379 *
380 * Note that this API is in flux.
381 *
382 * @param stream A stream created using AAudioStreamBuilder_openStream().
383 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
384 * @param startRoutine your thread entry point
385 * @param arg an argument that will be passed to your thread entry point
386 * @return AAUDIO_OK or a negative error.
387 */
388AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream stream,
389 aaudio_nanoseconds_t periodNanoseconds,
390 aaudio_audio_thread_proc_t *threadProc,
391 void *arg);
392
393/**
394 * Wait until the thread exits or an error occurs.
395 * The thread handle will be deleted.
396 *
397 * @param stream A stream created using AAudioStreamBuilder_openStream().
398 * @param returnArg a pointer to a variable to receive the return value
399 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
400 * @return AAUDIO_OK or a negative error.
401 */
402AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream stream,
403 void **returnArg,
404 aaudio_nanoseconds_t timeoutNanoseconds);
405
406// ============================================================
407// Stream - queries
408// ============================================================
409
410
411/**
412 * This can be used to adjust the latency of the buffer by changing
413 * the threshold where blocking will occur.
414 * By combining this with AAudioStream_getUnderrunCount(), the latency can be tuned
415 * at run-time for each device.
416 *
417 * This cannot be set higher than AAudioStream_getBufferCapacity().
418 *
419 * @param stream handle provided by AAudioStreamBuilder_openStream()
420 * @param requestedFrames requested number of frames that can be filled without blocking
421 * @param actualFrames receives final number of frames
422 * @return AAUDIO_OK or a negative error
423 */
424AAUDIO_API aaudio_result_t AAudioStream_setBufferSize(AAudioStream stream,
425 aaudio_size_frames_t requestedFrames,
426 aaudio_size_frames_t *actualFrames);
427
428/**
429 * Query the maximum number of frames that can be filled without blocking.
430 *
431 * @param stream handle provided by AAudioStreamBuilder_openStream()
432 * @param frames pointer to variable to receive the buffer size
433 * @return AAUDIO_OK or a negative error.
434 */
435AAUDIO_API aaudio_result_t AAudioStream_getBufferSize(AAudioStream stream, aaudio_size_frames_t *frames);
436
437/**
438 * Query the number of frames that are read or written by the endpoint at one time.
439 *
440 * @param stream handle provided by AAudioStreamBuilder_openStream()
441 * @param frames pointer to variable to receive the burst size
442 * @return AAUDIO_OK or a negative error.
443 */
444AAUDIO_API aaudio_result_t AAudioStream_getFramesPerBurst(AAudioStream stream, aaudio_size_frames_t *frames);
445
446/**
447 * Query maximum buffer capacity in frames.
448 *
449 * @param stream handle provided by AAudioStreamBuilder_openStream()
450 * @param frames pointer to variable to receive the buffer capacity
451 * @return AAUDIO_OK or a negative error.
452 */
453AAUDIO_API aaudio_result_t AAudioStream_getBufferCapacity(AAudioStream stream, aaudio_size_frames_t *frames);
454
455/**
456 * An XRun is an Underrun or an Overrun.
457 * During playing, an underrun will occur if the stream is not written in time
458 * and the system runs out of valid data.
459 * During recording, an overrun will occur if the stream is not read in time
460 * and there is no place to put the incoming data so it is discarded.
461 *
462 * An underrun or overrun can cause an audible "pop" or "glitch".
463 *
464 * @param stream handle provided by AAudioStreamBuilder_openStream()
465 * @param xRunCount pointer to variable to receive the underrun or overrun count
466 * @return AAUDIO_OK or a negative error.
467 */
468AAUDIO_API aaudio_result_t AAudioStream_getXRunCount(AAudioStream stream, int32_t *xRunCount);
469
470/**
471 * @param stream handle provided by AAudioStreamBuilder_openStream()
472 * @param sampleRate pointer to variable to receive the actual sample rate
473 * @return AAUDIO_OK or a negative error.
474 */
475AAUDIO_API aaudio_result_t AAudioStream_getSampleRate(AAudioStream stream, aaudio_sample_rate_t *sampleRate);
476
477/**
478 * The samplesPerFrame is also known as channelCount.
479 *
480 * @param stream handle provided by AAudioStreamBuilder_openStream()
481 * @param samplesPerFrame pointer to variable to receive the actual samples per frame
482 * @return AAUDIO_OK or a negative error.
483 */
484AAUDIO_API aaudio_result_t AAudioStream_getSamplesPerFrame(AAudioStream stream, int32_t *samplesPerFrame);
485
486/**
487 * @param stream handle provided by AAudioStreamBuilder_openStream()
488 * @param deviceId pointer to variable to receive the actual device ID
489 * @return AAUDIO_OK or a negative error.
490 */
491AAUDIO_API aaudio_result_t AAudioStream_getDeviceId(AAudioStream stream, aaudio_device_id_t *deviceId);
492
493/**
494 * @param stream handle provided by AAudioStreamBuilder_openStream()
495 * @param format pointer to variable to receive the actual data format
496 * @return AAUDIO_OK or a negative error.
497 */
498AAUDIO_API aaudio_result_t AAudioStream_getFormat(AAudioStream stream, aaudio_audio_format_t *format);
499
500/**
501 * Provide actual sharing mode.
502 * @param stream handle provided by AAudioStreamBuilder_openStream()
503 * @param sharingMode pointer to variable to receive the actual sharing mode
504 * @return AAUDIO_OK or a negative error.
505 */
506AAUDIO_API aaudio_result_t AAudioStream_getSharingMode(AAudioStream stream,
507 aaudio_sharing_mode_t *sharingMode);
508
509/**
510 * @param stream handle provided by AAudioStreamBuilder_openStream()
511 * @param direction pointer to a variable to be set to the current direction.
512 * @return AAUDIO_OK or a negative error.
513 */
514AAUDIO_API aaudio_result_t AAudioStream_getDirection(AAudioStream stream, aaudio_direction_t *direction);
515
516/**
517 * Passes back the number of frames that have been written since the stream was created.
518 * For an output stream, this will be advanced by the application calling write().
519 * For an input stream, this will be advanced by the device or service.
520 *
521 * The frame position is monotonically increasing.
522 *
523 * @param stream handle provided by AAudioStreamBuilder_openStream()
524 * @param frames pointer to variable to receive the frames written
525 * @return AAUDIO_OK or a negative error.
526 */
527AAUDIO_API aaudio_result_t AAudioStream_getFramesWritten(AAudioStream stream,
528 aaudio_position_frames_t *frames);
529
530/**
531 * Passes back the number of frames that have been read since the stream was created.
532 * For an output stream, this will be advanced by the device or service.
533 * For an input stream, this will be advanced by the application calling read().
534 *
535 * The frame position is monotonically increasing.
536 *
537 * @param stream handle provided by AAudioStreamBuilder_openStream()
538 * @param frames pointer to variable to receive the frames written
539 * @return AAUDIO_OK or a negative error.
540 */
541AAUDIO_API aaudio_result_t AAudioStream_getFramesRead(AAudioStream stream, aaudio_position_frames_t *frames);
542
543/**
544 * Passes back the time at which a particular frame was presented.
545 * This can be used to synchronize audio with video or MIDI.
546 * It can also be used to align a recorded stream with a playback stream.
547 *
548 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
549 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
550 * Note that because requestStart() is asynchronous, timestamps will not be valid until
551 * a short time after calling requestStart().
552 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
553 * Just try calling again later.
554 *
555 * If an error occurs, then the position and time will not be modified.
556 *
557 * The position and time passed back are monotonically increasing.
558 *
559 * @param stream A handle provided by AAudioStreamBuilder_openStream()
560 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
561 * @param framePosition pointer to a variable to receive the position
562 * @param timeNanoseconds pointer to a variable to receive the time
563 * @return AAUDIO_OK or a negative error
564 */
565AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream stream,
566 aaudio_clockid_t clockid,
567 aaudio_position_frames_t *framePosition,
568 aaudio_nanoseconds_t *timeNanoseconds);
569
570#ifdef __cplusplus
571}
572#endif
573
574#endif //AAUDIO_AAUDIO_H