blob: 921248afb2ba61274c593624a5a8f2007be0d171 [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
Phil Burk3316d5e2017-02-15 11:23:01 -080023#include <time.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080024#include "AAudioDefinitions.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
Phil Burke2155ef2017-02-24 13:50:29 -080030typedef struct AAudioStreamStruct AAudioStream;
31typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -080032
Phil Burk5ed503c2017-02-01 09:38:15 -080033#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -080034#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -080035#endif
36
37// ============================================================
38// Audio System
39// ============================================================
40
41/**
Phil Burk5ed503c2017-02-01 09:38:15 -080042 * The text is the ASCII symbol corresponding to the returnCode,
43 * or an English message saying the returnCode is unrecognized.
44 * This is intended for developers to use when debugging.
45 * It is not for display to users.
46 *
47 * @return pointer to a text representation of an AAudio result code.
48 */
49AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
50
51/**
52 * The text is the ASCII symbol corresponding to the stream state,
53 * or an English message saying the state is unrecognized.
54 * This is intended for developers to use when debugging.
55 * It is not for display to users.
56 *
57 * @return pointer to a text representation of an AAudio state.
58 */
59AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
60
61// ============================================================
62// StreamBuilder
63// ============================================================
64
65/**
66 * Create a StreamBuilder that can be used to open a Stream.
67 *
68 * The deviceId is initially unspecified, meaning that the current default device will be used.
69 *
70 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -080071 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -080072 * The data format, samplesPerFrames and sampleRate are unspecified and will be
73 * chosen by the device when it is opened.
74 *
75 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
76 */
Phil Burke2155ef2017-02-24 13:50:29 -080077AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -080078
79/**
80 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -080081 * On Android, for example, the ID could be obtained from the Java AudioManager.
82 *
83 * By default, the primary device will be used.
84 *
Phil Burk3316d5e2017-02-15 11:23:01 -080085 * @param builder reference provided by AAudio_createStreamBuilder()
86 * @param deviceId device identifier or AAUDIO_DEVICE_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -080087 */
Phil Burke2155ef2017-02-24 13:50:29 -080088AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -080089 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -080090
91/**
92 * Request a sample rate in Hz.
93 * The stream may be opened with a different sample rate.
94 * So the application should query for the actual rate after the stream is opened.
95 *
96 * Technically, this should be called the "frame rate" or "frames per second",
97 * because it refers to the number of complete frames transferred per second.
98 * But it is traditionally called "sample rate". Se we use that term.
99 *
100 * Default is AAUDIO_UNSPECIFIED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800101
Phil Burk5ed503c2017-02-01 09:38:15 -0800102 */
Phil Burke2155ef2017-02-24 13:50:29 -0800103AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800104 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800105
106/**
107 * Request a number of samples per frame.
108 * The stream may be opened with a different value.
109 * So the application should query for the actual value after the stream is opened.
110 *
111 * Default is AAUDIO_UNSPECIFIED.
112 *
113 * Note, this quantity is sometimes referred to as "channel count".
Phil Burk5ed503c2017-02-01 09:38:15 -0800114 */
Phil Burke2155ef2017-02-24 13:50:29 -0800115AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800116 int32_t samplesPerFrame);
117
118/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800119 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
120 * The application should query for the actual format after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800121 */
Phil Burke2155ef2017-02-24 13:50:29 -0800122AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 aaudio_audio_format_t format);
124
125/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 * Request a mode for sharing the device.
127 * The requested sharing mode may not be available.
128 * So the application should query for the actual mode after the stream is opened.
129 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800130 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800131 * @param sharingMode AAUDIO_SHARING_MODE_LEGACY or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 */
Phil Burke2155ef2017-02-24 13:50:29 -0800133AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800134 aaudio_sharing_mode_t sharingMode);
135
136/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 * Request the direction for a stream. The default is AAUDIO_DIRECTION_OUTPUT.
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 direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 */
Phil Burke2155ef2017-02-24 13:50:29 -0800142AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800143 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800144
145/**
Phil Burk3df348f2017-02-08 11:41:55 -0800146 * Set the requested maximum buffer capacity in frames.
147 * The final AAudioStream capacity may differ, but will probably be at least this big.
148 *
149 * Default is AAUDIO_UNSPECIFIED.
150 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800151 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk3df348f2017-02-08 11:41:55 -0800152 * @param frames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800153 */
Phil Burke2155ef2017-02-24 13:50:29 -0800154AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800155 int32_t frames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800156
157/**
158 * Open a stream based on the options in the StreamBuilder.
159 *
160 * AAudioStream_close must be called when finished with the stream to recover
161 * the memory and to free the associated resources.
162 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800163 * @param builder reference provided by AAudio_createStreamBuilder()
164 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800165 * @return AAUDIO_OK or a negative error.
166 */
Phil Burke2155ef2017-02-24 13:50:29 -0800167AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
168 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169
170/**
171 * Delete the resources associated with the StreamBuilder.
172 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800173 * @param builder reference provided by AAudio_createStreamBuilder()
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_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800177
178// ============================================================
179// Stream Control
180// ============================================================
181
182/**
183 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
184 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800185 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 * @return AAUDIO_OK or a negative error.
187 */
Phil Burke2155ef2017-02-24 13:50:29 -0800188AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800189
190/**
191 * Asynchronously request to start playing the stream. For output streams, one should
192 * write to the stream to fill the buffer before starting.
193 * Otherwise it will underflow.
194 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
195 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800196 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800197 * @return AAUDIO_OK or a negative error.
198 */
Phil Burke2155ef2017-02-24 13:50:29 -0800199AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800200
201/**
202 * Asynchronous request for the stream to pause.
203 * Pausing a stream will freeze the data flow but not flush any buffers.
204 * Use AAudioStream_Start() to resume playback after a pause.
205 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
206 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800207 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800208 * @return AAUDIO_OK or a negative error.
209 */
Phil Burke2155ef2017-02-24 13:50:29 -0800210AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800211
212/**
213 * Asynchronous request for the stream to flush.
214 * Flushing will discard any pending data.
215 * This call only works if the stream is pausing or paused. TODO review
216 * Frame counters are not reset by a flush. They may be advanced.
217 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
218 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800219 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 * @return AAUDIO_OK or a negative error.
221 */
Phil Burke2155ef2017-02-24 13:50:29 -0800222AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800223
224/**
225 * Asynchronous request for the stream to stop.
226 * The stream will stop after all of the data currently buffered has been played.
227 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
228 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800229 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800230 * @return AAUDIO_OK or a negative error.
231 */
Phil Burke2155ef2017-02-24 13:50:29 -0800232AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800233
234/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800235 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800236 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800237 * This function will immediately return the state without updating the state.
238 * If you want to update the client state based on the server state then
239 * call AAudioStream_waitForStateChange() with currentState
240 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
241 *
242 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 * @param state pointer to a variable that will be set to the current state
Phil Burk5ed503c2017-02-01 09:38:15 -0800244 */
Phil Burke2155ef2017-02-24 13:50:29 -0800245AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800246
247/**
248 * Wait until the current state no longer matches the input state.
249 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800250 * This will update the current client state.
251 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 * <pre><code>
253 * aaudio_stream_state_t currentState;
254 * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
255 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
256 * result = AAudioStream_waitForStateChange(
257 * stream, currentState, &currentState, MY_TIMEOUT_NANOS);
258 * }
259 * </code></pre>
260 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800261 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800262 * @param inputState The state we want to avoid.
263 * @param nextState Pointer to a variable that will be set to the new state.
264 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
265 * @return AAUDIO_OK or a negative error.
266 */
Phil Burke2155ef2017-02-24 13:50:29 -0800267AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800268 aaudio_stream_state_t inputState,
269 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800270 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800271
272// ============================================================
273// Stream I/O
274// ============================================================
275
276/**
277 * Read data from the stream.
278 *
279 * The call will wait until the read is complete or until it runs out of time.
280 * If timeoutNanos is zero then this call will not wait.
281 *
282 * Note that timeoutNanoseconds is a relative duration in wall clock time.
283 * Time will not stop if the thread is asleep.
284 * So it will be implemented using CLOCK_BOOTTIME.
285 *
286 * This call is "strong non-blocking" unless it has to wait for data.
287 *
288 * @param stream A stream created using AAudioStreamBuilder_openStream().
289 * @param buffer The address of the first sample.
290 * @param numFrames Number of frames to read. Only complete frames will be written.
291 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800292 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800293 */
Phil Burke2155ef2017-02-24 13:50:29 -0800294AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800295 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800296 int32_t numFrames,
297 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800298
299/**
300 * Write data to the stream.
301 *
302 * The call will wait until the write is complete or until it runs out of time.
303 * If timeoutNanos is zero then this call will not wait.
304 *
305 * Note that timeoutNanoseconds is a relative duration in wall clock time.
306 * Time will not stop if the thread is asleep.
307 * So it will be implemented using CLOCK_BOOTTIME.
308 *
309 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
310 *
311 * @param stream A stream created using AAudioStreamBuilder_openStream().
312 * @param buffer The address of the first sample.
313 * @param numFrames Number of frames to write. Only complete frames will be written.
314 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
315 * @return The number of frames actually written or a negative error.
316 */
Phil Burke2155ef2017-02-24 13:50:29 -0800317AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800318 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800319 int32_t numFrames,
320 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800321
322
323// ============================================================
324// High priority audio threads
325// ============================================================
326
Phil Burke2155ef2017-02-24 13:50:29 -0800327typedef void *(*aaudio_audio_thread_proc_t)(void *);
Phil Burk5ed503c2017-02-01 09:38:15 -0800328
329/**
330 * Create a thread associated with a stream. The thread has special properties for
331 * low latency audio performance. This thread can be used to implement a callback API.
332 *
333 * Only one thread may be associated with a stream.
334 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800335 * If you are using multiple streams then we recommend that you only do
336 * blocking reads or writes on one stream. You can do non-blocking I/O on the
337 * other streams by setting the timeout to zero.
338 * This thread should be created for the stream that you will block on.
339 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800340 * Note that this API is in flux.
341 *
342 * @param stream A stream created using AAudioStreamBuilder_openStream().
343 * @param periodNanoseconds the estimated period at which the audio thread will need to wake up
Glenn Kastenf26ad102017-01-12 09:14:45 -0800344 * @param threadProc your thread entry point
Phil Burk5ed503c2017-02-01 09:38:15 -0800345 * @param arg an argument that will be passed to your thread entry point
346 * @return AAUDIO_OK or a negative error.
347 */
Phil Burke2155ef2017-02-24 13:50:29 -0800348AAUDIO_API aaudio_result_t AAudioStream_createThread(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800349 int64_t periodNanoseconds,
Phil Burke2155ef2017-02-24 13:50:29 -0800350 aaudio_audio_thread_proc_t threadProc,
Phil Burk5ed503c2017-02-01 09:38:15 -0800351 void *arg);
352
353/**
354 * Wait until the thread exits or an error occurs.
Phil Burk5ed503c2017-02-01 09:38:15 -0800355 *
356 * @param stream A stream created using AAudioStreamBuilder_openStream().
357 * @param returnArg a pointer to a variable to receive the return value
358 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
359 * @return AAUDIO_OK or a negative error.
360 */
Phil Burke2155ef2017-02-24 13:50:29 -0800361AAUDIO_API aaudio_result_t AAudioStream_joinThread(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800362 void **returnArg,
Phil Burk3316d5e2017-02-15 11:23:01 -0800363 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800364
365// ============================================================
366// Stream - queries
367// ============================================================
368
369
370/**
371 * This can be used to adjust the latency of the buffer by changing
372 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800373 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800374 * at run-time for each device.
375 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800376 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800377 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800378 * Note that you will probably not get the exact size you request.
379 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
380 *
381 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800382 * @param requestedFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800383 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800384 */
Phil Burke2155ef2017-02-24 13:50:29 -0800385AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800386 int32_t requestedFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800387
388/**
389 * Query the maximum number of frames that can be filled without blocking.
390 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800391 * @param stream reference provided by AAudioStreamBuilder_openStream()
392 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800393 */
Phil Burke2155ef2017-02-24 13:50:29 -0800394AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800395
396/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800397 * Query the number of frames that the application should read or write at
398 * one time for optimal performance. It is OK if an application writes
399 * a different number of frames. But the buffer size may need to be larger
400 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800401 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800402 * Note that this may or may not match the actual device burst size.
403 * For some endpoints, the burst size can vary dynamically.
404 * But these tend to be devices with high latency.
405 *
406 * @param stream reference provided by AAudioStreamBuilder_openStream()
407 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800408 */
Phil Burke2155ef2017-02-24 13:50:29 -0800409AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800410
411/**
412 * Query maximum buffer capacity in frames.
413 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800414 * @param stream reference provided by AAudioStreamBuilder_openStream()
415 * @return the buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800416 */
Phil Burke2155ef2017-02-24 13:50:29 -0800417AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800418
419/**
420 * An XRun is an Underrun or an Overrun.
421 * During playing, an underrun will occur if the stream is not written in time
422 * and the system runs out of valid data.
423 * During recording, an overrun will occur if the stream is not read in time
424 * and there is no place to put the incoming data so it is discarded.
425 *
426 * An underrun or overrun can cause an audible "pop" or "glitch".
427 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800428 * @param stream reference provided by AAudioStreamBuilder_openStream()
429 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800430 */
Phil Burke2155ef2017-02-24 13:50:29 -0800431AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800432
433/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800434 * @param stream reference provided by AAudioStreamBuilder_openStream()
435 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800436 */
Phil Burke2155ef2017-02-24 13:50:29 -0800437AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800438
439/**
440 * The samplesPerFrame is also known as channelCount.
441 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800442 * @param stream reference provided by AAudioStreamBuilder_openStream()
443 * @return actual samples per frame
Phil Burk5ed503c2017-02-01 09:38:15 -0800444 */
Phil Burke2155ef2017-02-24 13:50:29 -0800445AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800446
447/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800448 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800449 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800450 */
Phil Burke2155ef2017-02-24 13:50:29 -0800451AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800452
453/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800454 * @param stream reference provided by AAudioStreamBuilder_openStream()
455 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800456 */
Phil Burke2155ef2017-02-24 13:50:29 -0800457AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800458
459/**
460 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -0800461 * @param stream reference provided by AAudioStreamBuilder_openStream()
462 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -0800463 */
Phil Burke2155ef2017-02-24 13:50:29 -0800464AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800465
466/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800467 * @param stream reference provided by AAudioStreamBuilder_openStream()
468 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -0800469 */
Phil Burke2155ef2017-02-24 13:50:29 -0800470AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800471
472/**
473 * Passes back the number of frames that have been written since the stream was created.
474 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -0800475 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800476 *
477 * The frame position is monotonically increasing.
478 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800479 * @param stream reference provided by AAudioStreamBuilder_openStream()
480 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -0800481 */
Phil Burke2155ef2017-02-24 13:50:29 -0800482AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800483
484/**
485 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -0800486 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -0800487 * For an input stream, this will be advanced by the application calling read().
488 *
489 * The frame position is monotonically increasing.
490 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800491 * @param stream reference provided by AAudioStreamBuilder_openStream()
492 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -0800493 */
Phil Burke2155ef2017-02-24 13:50:29 -0800494AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800495
496/**
497 * Passes back the time at which a particular frame was presented.
498 * This can be used to synchronize audio with video or MIDI.
499 * It can also be used to align a recorded stream with a playback stream.
500 *
501 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
502 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
503 * Note that because requestStart() is asynchronous, timestamps will not be valid until
504 * a short time after calling requestStart().
505 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
506 * Just try calling again later.
507 *
508 * If an error occurs, then the position and time will not be modified.
509 *
510 * The position and time passed back are monotonically increasing.
511 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800512 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800513 * @param clockid AAUDIO_CLOCK_MONOTONIC or AAUDIO_CLOCK_BOOTTIME
514 * @param framePosition pointer to a variable to receive the position
515 * @param timeNanoseconds pointer to a variable to receive the time
516 * @return AAUDIO_OK or a negative error
517 */
Phil Burke2155ef2017-02-24 13:50:29 -0800518AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -0800519 clockid_t clockid,
520 int64_t *framePosition,
521 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800522
523#ifdef __cplusplus
524}
525#endif
526
527#endif //AAUDIO_AAUDIO_H