blob: 551dcc90a23608ef9c7f77c7b2cb7dbe6baba827 [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#include "AAudioDefinitions.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Phil Burke2155ef2017-02-24 13:50:29 -080039typedef struct AAudioStreamStruct AAudioStream;
40typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -080041
Phil Burk5ed503c2017-02-01 09:38:15 -080042#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -080043#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -080044#endif
45
46// ============================================================
47// Audio System
48// ============================================================
49
50/**
Phil Burk5ed503c2017-02-01 09:38:15 -080051 * The text is the ASCII symbol corresponding to the returnCode,
52 * or an English message saying the returnCode is unrecognized.
53 * This is intended for developers to use when debugging.
54 * It is not for display to users.
55 *
56 * @return pointer to a text representation of an AAudio result code.
57 */
58AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
59
60/**
61 * The text is the ASCII symbol corresponding to the stream state,
62 * or an English message saying the state is unrecognized.
63 * This is intended for developers to use when debugging.
64 * It is not for display to users.
65 *
66 * @return pointer to a text representation of an AAudio state.
67 */
68AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
69
70// ============================================================
71// StreamBuilder
72// ============================================================
73
74/**
75 * Create a StreamBuilder that can be used to open a Stream.
76 *
77 * The deviceId is initially unspecified, meaning that the current default device will be used.
78 *
79 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -080080 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -080081 * The data format, samplesPerFrames and sampleRate are unspecified and will be
82 * chosen by the device when it is opened.
83 *
84 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
85 */
Phil Burke2155ef2017-02-24 13:50:29 -080086AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -080087
88/**
89 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -080090 * On Android, for example, the ID could be obtained from the Java AudioManager.
91 *
92 * By default, the primary device will be used.
93 *
Phil Burk3316d5e2017-02-15 11:23:01 -080094 * @param builder reference provided by AAudio_createStreamBuilder()
95 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -080096 */
Phil Burke2155ef2017-02-24 13:50:29 -080097AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -080098 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -080099
100/**
101 * Request a sample rate in Hz.
102 * The stream may be opened with a different sample rate.
103 * So the application should query for the actual rate after the stream is opened.
104 *
105 * Technically, this should be called the "frame rate" or "frames per second",
106 * because it refers to the number of complete frames transferred per second.
107 * But it is traditionally called "sample rate". Se we use that term.
108 *
109 * Default is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800110
Phil Burk5ed503c2017-02-01 09:38:15 -0800111 */
Phil Burke2155ef2017-02-24 13:50:29 -0800112AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800113 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800114
115/**
116 * Request a number of samples per frame.
117 * The stream may be opened with a different value.
118 * So the application should query for the actual value after the stream is opened.
119 *
120 * Default is AAUDIO_UNSPECIFIED.
121 *
122 * Note, this quantity is sometimes referred to as "channel count".
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 */
Phil Burke2155ef2017-02-24 13:50:29 -0800124AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800125 int32_t samplesPerFrame);
126
127/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800128 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
129 * The application should query for the actual format after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800130 */
Phil Burke2155ef2017-02-24 13:50:29 -0800131AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 aaudio_audio_format_t format);
133
134/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800135 * Request a mode for sharing the device.
136 * The requested sharing mode may not be available.
137 * So the application should query for the actual mode after the stream is opened.
138 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800139 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 * @param sharingMode AAUDIO_SHARING_MODE_LEGACY or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 */
Phil Burke2155ef2017-02-24 13:50:29 -0800142AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800143 aaudio_sharing_mode_t sharingMode);
144
145/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800146 * Request the direction for a stream. The default is AAUDIO_DIRECTION_OUTPUT.
147 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800148 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800149 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 */
Phil Burke2155ef2017-02-24 13:50:29 -0800151AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800152 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800153
154/**
Phil Burk3df348f2017-02-08 11:41:55 -0800155 * Set the requested maximum buffer capacity in frames.
156 * The final AAudioStream capacity may differ, but will probably be at least this big.
157 *
158 * Default is AAUDIO_UNSPECIFIED.
159 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800160 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk3df348f2017-02-08 11:41:55 -0800161 * @param frames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800162 */
Phil Burke2155ef2017-02-24 13:50:29 -0800163AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800164 int32_t frames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165
166/**
167 * Open a stream based on the options in the StreamBuilder.
168 *
169 * AAudioStream_close must be called when finished with the stream to recover
170 * the memory and to free the associated resources.
171 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800172 * @param builder reference provided by AAudio_createStreamBuilder()
173 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 * @return AAUDIO_OK or a negative error.
175 */
Phil Burke2155ef2017-02-24 13:50:29 -0800176AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
177 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800178
179/**
180 * Delete the resources associated with the StreamBuilder.
181 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800182 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800183 * @return AAUDIO_OK or a negative error.
184 */
Phil Burke2155ef2017-02-24 13:50:29 -0800185AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800186
187// ============================================================
188// Stream Control
189// ============================================================
190
191/**
192 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
193 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800194 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800195 * @return AAUDIO_OK or a negative error.
196 */
Phil Burke2155ef2017-02-24 13:50:29 -0800197AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800198
199/**
200 * Asynchronously request to start playing the stream. For output streams, one should
201 * write to the stream to fill the buffer before starting.
202 * Otherwise it will underflow.
203 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
204 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800205 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 * @return AAUDIO_OK or a negative error.
207 */
Phil Burke2155ef2017-02-24 13:50:29 -0800208AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800209
210/**
211 * Asynchronous request for the stream to pause.
212 * Pausing a stream will freeze the data flow but not flush any buffers.
213 * Use AAudioStream_Start() to resume playback after a pause.
214 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
215 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800216 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 * @return AAUDIO_OK or a negative error.
218 */
Phil Burke2155ef2017-02-24 13:50:29 -0800219AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800220
221/**
222 * Asynchronous request for the stream to flush.
223 * Flushing will discard any pending data.
224 * This call only works if the stream is pausing or paused. TODO review
225 * Frame counters are not reset by a flush. They may be advanced.
226 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
227 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800228 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800229 * @return AAUDIO_OK or a negative error.
230 */
Phil Burke2155ef2017-02-24 13:50:29 -0800231AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800232
233/**
234 * Asynchronous request for the stream to stop.
235 * The stream will stop after all of the data currently buffered has been played.
236 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
237 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800238 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 * @return AAUDIO_OK or a negative error.
240 */
Phil Burke2155ef2017-02-24 13:50:29 -0800241AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800242
243/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800244 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800245 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800246 * This function will immediately return the state without updating the state.
247 * If you want to update the client state based on the server state then
248 * call AAudioStream_waitForStateChange() with currentState
249 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
250 *
251 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 * @param state pointer to a variable that will be set to the current state
Phil Burk5ed503c2017-02-01 09:38:15 -0800253 */
Phil Burke2155ef2017-02-24 13:50:29 -0800254AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800255
256/**
257 * Wait until the current state no longer matches the input state.
258 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800259 * This will update the current client state.
260 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 * <pre><code>
262 * aaudio_stream_state_t currentState;
263 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
264 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
265 * result = AAudioStream_waitForStateChange(
266 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
267 * }
268 * </code></pre>
269 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800270 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 * @param inputState The state we want to avoid.
272 * @param nextState Pointer to a variable that will be set to the new state.
273 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
274 * @return AAUDIO_OK or a negative error.
275 */
Phil Burke2155ef2017-02-24 13:50:29 -0800276AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 aaudio_stream_state_t inputState,
278 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800279 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800280
281// ============================================================
282// Stream I/O
283// ============================================================
284
285/**
286 * Read data from the stream.
287 *
288 * The call will wait until the read is complete or until it runs out of time.
289 * If timeoutNanos is zero then this call will not wait.
290 *
291 * Note that timeoutNanoseconds is a relative duration in wall clock time.
292 * Time will not stop if the thread is asleep.
293 * So it will be implemented using CLOCK_BOOTTIME.
294 *
295 * This call is "strong non-blocking" unless it has to wait for data.
296 *
297 * @param stream A stream created using AAudioStreamBuilder_openStream().
298 * @param buffer The address of the first sample.
299 * @param numFrames Number of frames to read. Only complete frames will be written.
300 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800301 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800302 */
Phil Burke2155ef2017-02-24 13:50:29 -0800303AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800304 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800305 int32_t numFrames,
306 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800307
308/**
309 * Write data to the stream.
310 *
311 * The call will wait until the write is complete or until it runs out of time.
312 * If timeoutNanos is zero then this call will not wait.
313 *
314 * Note that timeoutNanoseconds is a relative duration in wall clock time.
315 * Time will not stop if the thread is asleep.
316 * So it will be implemented using CLOCK_BOOTTIME.
317 *
318 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
319 *
320 * @param stream A stream created using AAudioStreamBuilder_openStream().
321 * @param buffer The address of the first sample.
322 * @param numFrames Number of frames to write. Only complete frames will be written.
323 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
324 * @return The number of frames actually written or a negative error.
325 */
Phil Burke2155ef2017-02-24 13:50:29 -0800326AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800327 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800328 int32_t numFrames,
329 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800330
331
332// ============================================================
333// High priority audio threads
334// ============================================================
335
Phil Burke2155ef2017-02-24 13:50:29 -0800336typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800337
338/**
339 * Create a thread associated with a stream. The thread has special properties for
340 * low latency audio performance. This thread can be used to implement a callback API.
341 *
342 * Only one thread may be associated with a stream.
343 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800344 * If you are using multiple streams then we recommend that you only do
345 * blocking reads or writes on one stream. You can do non-blocking I/O on the
346 * other streams by setting the timeout to zero.
347 * This thread should be created for the stream that you will block on.
348 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800349 * Note that this API is in flux.
350 *
351 * @param stream A stream created using AAudioStreamBuilder_openStream().
352 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800353 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800354 * @param arg an argument that will be passed to your thread entry point
355 * @return AAUDIO_OK or a negative error.
356 */
Phil Burke2155ef2017-02-24 13:50:29 -0800357AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800358 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800359 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800360 void *arg);
361
362/**
363 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800364 *
365 * @param stream A stream created using AAudioStreamBuilder_openStream().
366 * @param returnArg a pointer to a variable to receive the return value
367 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
368 * @return AAUDIO_OK or a negative error.
369 */
Phil Burke2155ef2017-02-24 13:50:29 -0800370AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800371 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800372 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800373
374// ============================================================
375// Stream - queries
376// ============================================================
377
378
379/**
380 * This can be used to adjust the latency of the buffer by changing
381 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800382 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800383 * at run-time for each device.
384 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800385 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800386 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800387 * Note that you will probably not get the exact size you request.
388 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
389 *
390 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800391 * @param requestedFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800392 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800393 */
Phil Burke2155ef2017-02-24 13:50:29 -0800394AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800395 int32_t requestedFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800396
397/**
398 * Query the maximum number of frames that can be filled without blocking.
399 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800400 * @param stream reference provided by AAudioStreamBuilder_openStream()
401 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800402 */
Phil Burke2155ef2017-02-24 13:50:29 -0800403AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800404
405/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800406 * Query the number of frames that the application should read or write at
407 * one time for optimal performance. It is OK if an application writes
408 * a different number of frames. But the buffer size may need to be larger
409 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800410 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800411 * Note that this may or may not match the actual device burst size.
412 * For some endpoints, the burst size can vary dynamically.
413 * But these tend to be devices with high latency.
414 *
415 * @param stream reference provided by AAudioStreamBuilder_openStream()
416 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800417 */
Phil Burke2155ef2017-02-24 13:50:29 -0800418AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800419
420/**
421 * Query maximum buffer capacity in frames.
422 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800423 * @param stream reference provided by AAudioStreamBuilder_openStream()
424 * @return the buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800425 */
Phil Burke2155ef2017-02-24 13:50:29 -0800426AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800427
428/**
429 * An XRun is an Underrun or an Overrun.
430 * During playing, an underrun will occur if the stream is not written in time
431 * and the system runs out of valid data.
432 * During recording, an overrun will occur if the stream is not read in time
433 * and there is no place to put the incoming data so it is discarded.
434 *
435 * An underrun or overrun can cause an audible "pop" or "glitch".
436 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800437 * @param stream reference provided by AAudioStreamBuilder_openStream()
438 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800439 */
Phil Burke2155ef2017-02-24 13:50:29 -0800440AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800441
442/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800443 * @param stream reference provided by AAudioStreamBuilder_openStream()
444 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800445 */
Phil Burke2155ef2017-02-24 13:50:29 -0800446AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800447
448/**
449 * The samplesPerFrame is also known as channelCount.
450 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800451 * @param stream reference provided by AAudioStreamBuilder_openStream()
452 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800453 */
Phil Burke2155ef2017-02-24 13:50:29 -0800454AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800455
456/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800457 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800458 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800459 */
Phil Burke2155ef2017-02-24 13:50:29 -0800460AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800461
462/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800463 * @param stream reference provided by AAudioStreamBuilder_openStream()
464 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800465 */
Phil Burke2155ef2017-02-24 13:50:29 -0800466AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800467
468/**
469 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800470 * @param stream reference provided by AAudioStreamBuilder_openStream()
471 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800472 */
Phil Burke2155ef2017-02-24 13:50:29 -0800473AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800474
475/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800476 * @param stream reference provided by AAudioStreamBuilder_openStream()
477 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800478 */
Phil Burke2155ef2017-02-24 13:50:29 -0800479AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800480
481/**
482 * Passes back the number of frames that have been written since the stream was created.
483 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800484 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800485 *
486 * The frame position is monotonically increasing.
487 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800488 * @param stream reference provided by AAudioStreamBuilder_openStream()
489 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800490 */
Phil Burke2155ef2017-02-24 13:50:29 -0800491AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800492
493/**
494 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800495 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800496 * For an input stream, this will be advanced by the application calling read().
497 *
498 * The frame position is monotonically increasing.
499 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800500 * @param stream reference provided by AAudioStreamBuilder_openStream()
501 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800502 */
Phil Burke2155ef2017-02-24 13:50:29 -0800503AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800504
505/**
506 * Passes back the time at which a particular frame was presented.
507 * This can be used to synchronize audio with video or MIDI.
508 * It can also be used to align a recorded stream with a playback stream.
509 *
510 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
511 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
512 * Note that because requestStart() is asynchronous, timestamps will not be valid until
513 * a short time after calling requestStart().
514 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
515 * Just try calling again later.
516 *
517 * If an error occurs, then the position and time will not be modified.
518 *
519 * The position and time passed back are monotonically increasing.
520 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800521 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800522 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
523 * @param framePosition pointer to a variable to receive the position
524 * @param timeNanoseconds pointer to a variable to receive the time
525 * @return AAUDIO_OK or a negative error
526 */
Phil Burke2155ef2017-02-24 13:50:29 -0800527AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800528 clockid_t clockid,
529 int64_t *framePosition,
530 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800531
532#ifdef __cplusplus
533}
534#endif
535
536#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -0700537
538/** @} */