blob: 5da8114aac1cfbc70ce590ee75814f5aeb60f543 [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
Phil Burka45be8b2017-04-05 14:45:48 -070018 * @addtogroup Audio
19 * @{
20 */
21
22/**
23 * @file AAudio.h
24 */
25
26/**
27 * This is the 'C' API for AAudio.
Phil Burk5ed503c2017-02-01 09:38:15 -080028 */
29#ifndef AAUDIO_AAUDIO_H
30#define AAUDIO_AAUDIO_H
31
Phil Burk3316d5e2017-02-15 11:23:01 -080032#include <time.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080033
34#ifdef __cplusplus
35extern "C" {
36#endif
37
Phil Burka4eb0d82017-04-12 15:44:06 -070038/**
39 * This is used to represent a value that has not been specified.
40 * For example, an application could use AAUDIO_UNSPECIFIED to indicate
41 * that is did not not care what the specific value of a parameter was
42 * and would accept whatever it was given.
43 */
44#define AAUDIO_UNSPECIFIED 0
Phil Burka4eb0d82017-04-12 15:44:06 -070045
46enum {
47 AAUDIO_DIRECTION_OUTPUT,
48 AAUDIO_DIRECTION_INPUT
49};
50typedef int32_t aaudio_direction_t;
51
52enum {
53 AAUDIO_FORMAT_INVALID = -1,
54 AAUDIO_FORMAT_UNSPECIFIED = 0,
55 AAUDIO_FORMAT_PCM_I16,
Phil Burk74733452017-04-18 19:50:28 -070056 AAUDIO_FORMAT_PCM_FLOAT
Phil Burka4eb0d82017-04-12 15:44:06 -070057};
58typedef int32_t aaudio_format_t;
59
Phil Burka4eb0d82017-04-12 15:44:06 -070060enum {
61 AAUDIO_OK,
62 AAUDIO_ERROR_BASE = -900, // TODO review
63 AAUDIO_ERROR_DISCONNECTED,
64 AAUDIO_ERROR_ILLEGAL_ARGUMENT,
Phil Burk17fff382017-05-16 14:06:45 -070065 // reserved
66 AAUDIO_ERROR_INTERNAL = AAUDIO_ERROR_ILLEGAL_ARGUMENT + 2,
Phil Burka4eb0d82017-04-12 15:44:06 -070067 AAUDIO_ERROR_INVALID_STATE,
Phil Burk17fff382017-05-16 14:06:45 -070068 // reserved
69 // reserved
70 AAUDIO_ERROR_INVALID_HANDLE = AAUDIO_ERROR_INVALID_STATE + 3,
71 // reserved
72 AAUDIO_ERROR_UNIMPLEMENTED = AAUDIO_ERROR_INVALID_HANDLE + 2,
Phil Burka4eb0d82017-04-12 15:44:06 -070073 AAUDIO_ERROR_UNAVAILABLE,
74 AAUDIO_ERROR_NO_FREE_HANDLES,
75 AAUDIO_ERROR_NO_MEMORY,
76 AAUDIO_ERROR_NULL,
77 AAUDIO_ERROR_TIMEOUT,
78 AAUDIO_ERROR_WOULD_BLOCK,
79 AAUDIO_ERROR_INVALID_FORMAT,
80 AAUDIO_ERROR_OUT_OF_RANGE,
81 AAUDIO_ERROR_NO_SERVICE,
82 AAUDIO_ERROR_INVALID_RATE
83};
84typedef int32_t aaudio_result_t;
85
86enum
87{
88 AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
89 AAUDIO_STREAM_STATE_UNKNOWN,
90 AAUDIO_STREAM_STATE_OPEN,
91 AAUDIO_STREAM_STATE_STARTING,
92 AAUDIO_STREAM_STATE_STARTED,
93 AAUDIO_STREAM_STATE_PAUSING,
94 AAUDIO_STREAM_STATE_PAUSED,
95 AAUDIO_STREAM_STATE_FLUSHING,
96 AAUDIO_STREAM_STATE_FLUSHED,
97 AAUDIO_STREAM_STATE_STOPPING,
98 AAUDIO_STREAM_STATE_STOPPED,
99 AAUDIO_STREAM_STATE_CLOSING,
100 AAUDIO_STREAM_STATE_CLOSED,
101 AAUDIO_STREAM_STATE_DISCONNECTED
102};
103typedef int32_t aaudio_stream_state_t;
104
105
106enum {
107 /**
108 * This will be the only stream using a particular source or sink.
109 * This mode will provide the lowest possible latency.
110 * You should close EXCLUSIVE streams immediately when you are not using them.
111 */
112 AAUDIO_SHARING_MODE_EXCLUSIVE,
113 /**
114 * Multiple applications will be mixed by the AAudio Server.
115 * This will have higher latency than the EXCLUSIVE mode.
116 */
117 AAUDIO_SHARING_MODE_SHARED
118};
119typedef int32_t aaudio_sharing_mode_t;
120
Phil Burke2fbb592017-05-01 15:05:52 -0700121
122enum {
123 /**
124 * No particular performance needs. Default.
125 */
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800126 AAUDIO_PERFORMANCE_MODE_NONE = 10,
Phil Burke2fbb592017-05-01 15:05:52 -0700127
128 /**
129 * Extending battery life is most important.
Phil Burked0a3fe2017-12-05 14:27:43 -0800130 *
131 * This mode is not supported in input streams.
132 * Mode NONE will be used if this is requested.
Phil Burke2fbb592017-05-01 15:05:52 -0700133 */
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800134 AAUDIO_PERFORMANCE_MODE_POWER_SAVING,
Phil Burke2fbb592017-05-01 15:05:52 -0700135
136 /**
137 * Reducing latency is most important.
138 */
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800139 AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
Phil Burke2fbb592017-05-01 15:05:52 -0700140};
141typedef int32_t aaudio_performance_mode_t;
142
Phil Burk361b1422017-12-20 14:24:16 -0800143/**
144 * The USAGE attribute expresses "why" you are playing a sound, what is this sound used for.
145 * This information is used by certain platforms or routing policies
146 * to make more refined volume or routing decisions.
147 *
148 * Note that these match the equivalent values in AudioAttributes in the Android Java API.
149 */
150enum {
151 /**
152 * Use this for streaming media, music performance, video, podcasts, etcetera.
153 */
154 AAUDIO_USAGE_MEDIA = 1,
155
156 /**
157 * Use this for voice over IP, telephony, etcetera.
158 */
159 AAUDIO_USAGE_VOICE_COMMUNICATION = 2,
160
161 /**
162 * Use this for sounds associated with telephony such as busy tones, DTMF, etcetera.
163 */
164 AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING = 3,
165
166 /**
167 * Use this to demand the users attention.
168 */
169 AAUDIO_USAGE_ALARM = 4,
170
171 /**
172 * Use this for notifying the user when a message has arrived or some
173 * other background event has occured.
174 */
175 AAUDIO_USAGE_NOTIFICATION = 5,
176
177 /**
178 * Use this when the phone rings.
179 */
180 AAUDIO_USAGE_NOTIFICATION_RINGTONE = 6,
181
182 /**
183 * Use this to attract the users attention when, for example, the battery is low.
184 */
185 AAUDIO_USAGE_NOTIFICATION_EVENT = 10,
186
187 /**
188 * Use this for screen readers, etcetera.
189 */
190 AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY = 11,
191
192 /**
193 * Use this for driving or navigation directions.
194 */
195 AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12,
196
197 /**
198 * Use this for user interface sounds, beeps, etcetera.
199 */
200 AAUDIO_USAGE_ASSISTANCE_SONIFICATION = 13,
201
202 /**
203 * Use this for game audio and sound effects.
204 */
205 AAUDIO_USAGE_GAME = 14,
206
207 /**
208 * Use this for audio responses to user queries, audio instructions or help utterances.
209 */
210 AAUDIO_USAGE_ASSISTANT = 16
211};
212typedef int32_t aaudio_usage_t;
213
214/**
215 * The CONTENT_TYPE attribute describes "what" you are playing.
216 * It expresses the general category of the content. This information is optional.
217 * But in case it is known (for instance {@link #AAUDIO_CONTENT_TYPE_MOVIE} for a
218 * movie streaming service or {@link #AAUDIO_CONTENT_TYPE_SPEECH} for
219 * an audio book application) this information might be used by the audio framework to
220 * enforce audio focus.
221 *
222 * Note that these match the equivalent values in AudioAttributes in the Android Java API.
223 */
224enum {
225
226 /**
227 * Use this for spoken voice, audio books, etcetera.
228 */
229 AAUDIO_CONTENT_TYPE_SPEECH = 1,
230
231 /**
232 * Use this for pre-recorded or live music.
233 */
234 AAUDIO_CONTENT_TYPE_MUSIC = 2,
235
236 /**
237 * Use this for a movie or video soundtrack.
238 */
239 AAUDIO_CONTENT_TYPE_MOVIE = 3,
240
241 /**
242 * Use this for sound is designed to accompany a user action,
243 * such as a click or beep sound made when the user presses a button.
244 */
245 AAUDIO_CONTENT_TYPE_SONIFICATION = 4
246};
247typedef int32_t aaudio_content_type_t;
248
249/**
250 * Defines the audio source.
251 * An audio source defines both a default physical source of audio signal, and a recording
252 * configuration.
253 *
254 * Note that these match the equivalent values in MediaRecorder.AudioSource in the Android Java API.
255 */
256enum {
257 /**
258 * Use this preset when other presets do not apply.
259 */
260 AAUDIO_INPUT_PRESET_GENERIC = 1,
261
262 /**
263 * Use this preset when recording video.
264 */
265 AAUDIO_INPUT_PRESET_CAMCORDER = 5,
266
267 /**
268 * Use this preset when doing speech recognition.
269 */
270 AAUDIO_INPUT_PRESET_VOICE_RECOGNITION = 6,
271
272 /**
273 * Use this preset when doing telephony or voice messaging.
274 */
275 AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION = 7,
276
277 /**
278 * Use this preset to obtain an input with no effects.
279 * Note that this input will not have automatic gain control
280 * so the recorded volume may be very low.
281 */
282 AAUDIO_INPUT_PRESET_UNPROCESSED = 9,
283};
284typedef int32_t aaudio_input_preset_t;
285
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800286enum {
287 /**
288 * Do not allocate a session ID.
289 * Effects cannot be used with this stream.
290 * Default.
291 */
292 AAUDIO_SESSION_ID_NONE = -1,
293
294 /**
295 * Allocate a session ID that can be used to attach and control
296 * effects using the Java AudioEffects API.
297 * Note that the use of this flag may result in higher latency.
298 *
299 * Note that this matches the value of AudioManager.AUDIO_SESSION_ID_GENERATE.
300 */
301 AAUDIO_SESSION_ID_ALLOCATE = 0,
302};
303typedef int32_t aaudio_session_id_t;
304
Phil Burke2155ef2017-02-24 13:50:29 -0800305typedef struct AAudioStreamStruct AAudioStream;
306typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800307
Phil Burk5ed503c2017-02-01 09:38:15 -0800308#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800309#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800310#endif
311
312// ============================================================
313// Audio System
314// ============================================================
315
316/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800317 * The text is the ASCII symbol corresponding to the returnCode,
318 * or an English message saying the returnCode is unrecognized.
319 * This is intended for developers to use when debugging.
320 * It is not for display to users.
321 *
322 * @return pointer to a text representation of an AAudio result code.
323 */
324AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
325
326/**
327 * The text is the ASCII symbol corresponding to the stream state,
328 * or an English message saying the state is unrecognized.
329 * This is intended for developers to use when debugging.
330 * It is not for display to users.
331 *
332 * @return pointer to a text representation of an AAudio state.
333 */
334AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
335
336// ============================================================
337// StreamBuilder
338// ============================================================
339
340/**
341 * Create a StreamBuilder that can be used to open a Stream.
342 *
343 * The deviceId is initially unspecified, meaning that the current default device will be used.
344 *
345 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800346 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800347 * The data format, samplesPerFrames and sampleRate are unspecified and will be
348 * chosen by the device when it is opened.
349 *
350 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
351 */
Phil Burke2155ef2017-02-24 13:50:29 -0800352AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800353
354/**
355 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800356 * On Android, for example, the ID could be obtained from the Java AudioManager.
357 *
Glenn Kasten5f510d22017-05-30 15:52:15 -0700358 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED,
Phil Burke057ca92017-03-28 11:31:34 -0700359 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800360 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800361 * @param builder reference provided by AAudio_createStreamBuilder()
Glenn Kasten5f510d22017-05-30 15:52:15 -0700362 * @param deviceId device identifier or AAUDIO_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800363 */
Phil Burke2155ef2017-02-24 13:50:29 -0800364AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800365 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800366
367/**
Phil Burke057ca92017-03-28 11:31:34 -0700368 * Request a sample rate in Hertz.
369 *
Phil Burke057ca92017-03-28 11:31:34 -0700370 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700371 * An optimal value will then be chosen when the stream is opened.
372 * After opening a stream with an unspecified value, the application must
373 * query for the actual value, which may vary by device.
374 *
375 * If an exact value is specified then an opened stream will use that value.
376 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700377 *
378 * @param builder reference provided by AAudio_createStreamBuilder()
379 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800380 */
Phil Burke2155ef2017-02-24 13:50:29 -0800381AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800382 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800383
384/**
Phil Burk20523ed2017-04-24 17:04:01 -0700385 * Request a number of channels for the stream.
Phil Burke057ca92017-03-28 11:31:34 -0700386 *
Phil Burke057ca92017-03-28 11:31:34 -0700387 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700388 * An optimal value will then be chosen when the stream is opened.
389 * After opening a stream with an unspecified value, the application must
390 * query for the actual value, which may vary by device.
Phil Burk5ed503c2017-02-01 09:38:15 -0800391 *
Phil Burk8f624892017-05-11 11:44:20 -0700392 * If an exact value is specified then an opened stream will use that value.
393 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700394 *
395 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk20523ed2017-04-24 17:04:01 -0700396 * @param channelCount Number of channels desired.
Phil Burk5ed503c2017-02-01 09:38:15 -0800397 */
Phil Burk20523ed2017-04-24 17:04:01 -0700398AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
399 int32_t channelCount);
400
401/**
Phil Burke74240d2017-08-03 15:25:43 -0700402 * Identical to AAudioStreamBuilder_setChannelCount().
403 *
404 * @param builder reference provided by AAudio_createStreamBuilder()
405 * @param samplesPerFrame Number of samples in a frame.
406 */
407AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
408 int32_t samplesPerFrame);
409
410/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800411 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700412 *
413 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700414 * An optimal value will then be chosen when the stream is opened.
415 * After opening a stream with an unspecified value, the application must
416 * query for the actual value, which may vary by device.
Phil Burke057ca92017-03-28 11:31:34 -0700417 *
Phil Burk8f624892017-05-11 11:44:20 -0700418 * If an exact value is specified then an opened stream will use that value.
419 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700420 *
421 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk8f624892017-05-11 11:44:20 -0700422 * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800423 */
Phil Burke2155ef2017-02-24 13:50:29 -0800424AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk9dca9822017-05-26 14:27:43 -0700425 aaudio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800426
427/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800428 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700429 *
430 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
431 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800432 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700433 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800434 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800435 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700436 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800437 */
Phil Burke2155ef2017-02-24 13:50:29 -0800438AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800439 aaudio_sharing_mode_t sharingMode);
440
441/**
Phil Burke057ca92017-03-28 11:31:34 -0700442 * Request the direction for a stream.
443 *
444 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800445 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800446 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800447 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800448 */
Phil Burke2155ef2017-02-24 13:50:29 -0800449AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800450 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800451
452/**
Phil Burke057ca92017-03-28 11:31:34 -0700453 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800454 * The final AAudioStream capacity may differ, but will probably be at least this big.
455 *
Phil Burke057ca92017-03-28 11:31:34 -0700456 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800457 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800458 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700459 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800460 */
Phil Burke2155ef2017-02-24 13:50:29 -0800461AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700462 int32_t numFrames);
Phil Burke2fbb592017-05-01 15:05:52 -0700463
464/**
465 * Set the requested performance mode.
466 *
467 * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
468 *
469 * @param builder reference provided by AAudio_createStreamBuilder()
470 * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
471 */
472AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
473 aaudio_performance_mode_t mode);
474
Phil Burke057ca92017-03-28 11:31:34 -0700475/**
Phil Burk361b1422017-12-20 14:24:16 -0800476 * Set the intended use case for the stream.
477 *
478 * The AAudio system will use this information to optimize the
479 * behavior of the stream.
480 * This could, for example, affect how volume and focus is handled for the stream.
481 *
482 * The default, if you do not call this function, is AAUDIO_USAGE_MEDIA.
483 *
484 * @param builder reference provided by AAudio_createStreamBuilder()
485 * @param usage the desired usage, eg. AAUDIO_USAGE_GAME
486 */
487AAUDIO_API void AAudioStreamBuilder_setUsage(AAudioStreamBuilder* builder,
488 aaudio_usage_t usage);
489
490/**
491 * Set the type of audio data that the stream will carry.
492 *
493 * The AAudio system will use this information to optimize the
494 * behavior of the stream.
495 * This could, for example, affect whether a stream is paused when a notification occurs.
496 *
497 * The default, if you do not call this function, is AAUDIO_CONTENT_TYPE_MUSIC.
498 *
499 * @param builder reference provided by AAudio_createStreamBuilder()
500 * @param contentType the type of audio data, eg. AAUDIO_CONTENT_TYPE_SPEECH
501 */
502AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
503 aaudio_content_type_t contentType);
504
505/**
506 * Set the input (capture) preset for the stream.
507 *
508 * The AAudio system will use this information to optimize the
509 * behavior of the stream.
510 * This could, for example, affect which microphones are used and how the
511 * recorded data is processed.
512 *
513 * The default, if you do not call this function, is AAUDIO_INPUT_PRESET_GENERIC.
514 *
515 * @param builder reference provided by AAudio_createStreamBuilder()
516 * @param inputPreset the desired configuration for recording
517 */
518AAUDIO_API void AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder* builder,
519 aaudio_input_preset_t inputPreset);
520
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800521/** Set the requested session ID.
522 *
523 * The session ID can be used to associate a stream with effects processors.
524 * The effects are controlled using the Android AudioEffect Java API.
525 *
526 * The default, if you do not call this function, is AAUDIO_SESSION_ID_NONE.
527 *
528 * If set to AAUDIO_SESSION_ID_ALLOCATE then a session ID will be allocated
529 * when the stream is opened.
530 *
531 * The allocated session ID can be obtained by calling AAudioStream_getSessionId()
532 * and then used with this function when opening another stream.
533 * This allows effects to be shared between streams.
534 *
535 * Session IDs from AAudio can be used the Android Java APIs and vice versa.
536 * So a session ID from an AAudio stream can be passed to Java
537 * and effects applied using the Java AudioEffect API.
538 *
539 * Allocated session IDs will always be positive and nonzero.
540 *
541 * @param builder reference provided by AAudio_createStreamBuilder()
542 * @param sessionId an allocated sessionID or AAUDIO_SESSION_ID_ALLOCATE
543 */
544AAUDIO_API void AAudioStreamBuilder_setSessionId(AAudioStreamBuilder* builder,
545 aaudio_session_id_t sessionId);
546
Phil Burk361b1422017-12-20 14:24:16 -0800547/**
Phil Burke057ca92017-03-28 11:31:34 -0700548 * Return one of these values from the data callback function.
549 */
550enum {
551
552 /**
553 * Continue calling the callback.
554 */
555 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
556
557 /**
558 * Stop calling the callback.
559 *
560 * The application will still need to call AAudioStream_requestPause()
561 * or AAudioStream_requestStop().
562 */
563 AAUDIO_CALLBACK_RESULT_STOP,
564
565};
566typedef int32_t aaudio_data_callback_result_t;
567
568/**
569 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
570 *
571 * For an output stream, this function should render and write numFrames of data
572 * in the streams current data format to the audioData buffer.
573 *
574 * For an input stream, this function should read and process numFrames of data
575 * from the audioData buffer.
576 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800577 * The audio data is passed through the buffer. So do NOT call AAudioStream_read() or
578 * AAudioStream_write() on the stream that is making the callback.
579 *
580 * Note that numFrames can vary unless AAudioStreamBuilder_setFramesPerDataCallback()
581 * is called.
582 *
583 * Also note that this callback function should be considered a "real-time" function.
Phil Burke057ca92017-03-28 11:31:34 -0700584 * It must not do anything that could cause an unbounded delay because that can cause the
585 * audio to glitch or pop.
586 *
587 * These are things the function should NOT do:
588 * <ul>
589 * <li>allocate memory using, for example, malloc() or new</li>
590 * <li>any file operations such as opening, closing, reading or writing</li>
591 * <li>any network operations such as streaming</li>
592 * <li>use any mutexes or other synchronization primitives</li>
593 * <li>sleep</li>
Phil Burked0a3fe2017-12-05 14:27:43 -0800594 * <li>stop or close the stream</li>
Phil Burke057ca92017-03-28 11:31:34 -0700595 * </ul>
596 *
597 * If you need to move data, eg. MIDI commands, in or out of the callback function then
598 * we recommend the use of non-blocking techniques such as an atomic FIFO.
599 *
600 * @param stream reference provided by AAudioStreamBuilder_openStream()
601 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
602 * @param audioData a pointer to the audio data
Phil Burked0a3fe2017-12-05 14:27:43 -0800603 * @param numFrames the number of frames to be processed, which can vary
Phil Burke057ca92017-03-28 11:31:34 -0700604 * @return AAUDIO_CALLBACK_RESULT_*
605 */
606typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
607 AAudioStream *stream,
608 void *userData,
609 void *audioData,
610 int32_t numFrames);
611
612/**
613 * Request that AAudio call this functions when the stream is running.
614 *
615 * Note that when using this callback, the audio data will be passed in or out
616 * of the function as an argument.
617 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
618 * that has an active data callback.
619 *
620 * The callback function will start being called after AAudioStream_requestStart() is called.
621 * It will stop being called after AAudioStream_requestPause() or
622 * AAudioStream_requestStop() is called.
623 *
624 * This callback function will be called on a real-time thread owned by AAudio. See
Glenn Kasten0d804362017-04-13 09:20:14 -0700625 * {@link AAudioStream_dataCallback} for more information.
Phil Burke057ca92017-03-28 11:31:34 -0700626 *
627 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
628 *
629 * @param builder reference provided by AAudio_createStreamBuilder()
630 * @param callback pointer to a function that will process audio data.
631 * @param userData pointer to an application data structure that will be passed
632 * to the callback functions.
633 */
634AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
635 AAudioStream_dataCallback callback,
636 void *userData);
637
638/**
639 * Set the requested data callback buffer size in frames.
640 * See {@link AAudioStream_dataCallback}.
641 *
642 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
643 *
644 * For the lowest possible latency, do not call this function. AAudio will then
645 * call the dataProc callback function with whatever size is optimal.
646 * That size may vary from one callback to another.
647 *
648 * Only use this function if the application requires a specific number of frames for processing.
649 * The application might, for example, be using an FFT that requires
650 * a specific power-of-two sized buffer.
651 *
652 * AAudio may need to add additional buffering in order to adapt between the internal
653 * buffer size and the requested buffer size.
654 *
655 * If you do call this function then the requested size should be less than
656 * half the buffer capacity, to allow double buffering.
657 *
658 * @param builder reference provided by AAudio_createStreamBuilder()
659 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
660 */
661AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
662 int32_t numFrames);
663
664/**
665 * Prototype for the callback function that is passed to
666 * AAudioStreamBuilder_setErrorCallback().
667 *
668 * @param stream reference provided by AAudioStreamBuilder_openStream()
669 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
670 * @param error an AAUDIO_ERROR_* value.
671 */
672typedef void (*AAudioStream_errorCallback)(
673 AAudioStream *stream,
674 void *userData,
675 aaudio_result_t error);
676
677/**
Phil Burked0a3fe2017-12-05 14:27:43 -0800678 * Request that AAudio call this function if any error occurs or the stream is disconnected.
Phil Burke057ca92017-03-28 11:31:34 -0700679 *
680 * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
Phil Burked0a3fe2017-12-05 14:27:43 -0800681 * device to be unavailable or "disconnected".
Phil Burke057ca92017-03-28 11:31:34 -0700682 * Another possible cause of error would be a timeout or an unanticipated internal error.
683 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800684 * In response, this function should signal or create another thread to stop
685 * and close this stream. The other thread could then reopen a stream on another device.
686 * Do not stop or close the stream, or reopen the new stream, directly from this callback.
687 *
688 * This callback will not be called because of actions by the application, such as stopping
689 * or closing a stream.
690 *
Phil Burke057ca92017-03-28 11:31:34 -0700691 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
692 *
693 * @param builder reference provided by AAudio_createStreamBuilder()
694 * @param callback pointer to a function that will be called if an error occurs.
695 * @param userData pointer to an application data structure that will be passed
696 * to the callback functions.
697 */
698AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
699 AAudioStream_errorCallback callback,
700 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800701
702/**
703 * Open a stream based on the options in the StreamBuilder.
704 *
705 * AAudioStream_close must be called when finished with the stream to recover
706 * the memory and to free the associated resources.
707 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800708 * @param builder reference provided by AAudio_createStreamBuilder()
709 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800710 * @return AAUDIO_OK or a negative error.
711 */
Phil Burke2155ef2017-02-24 13:50:29 -0800712AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
713 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800714
715/**
716 * Delete the resources associated with the StreamBuilder.
717 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800718 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800719 * @return AAUDIO_OK or a negative error.
720 */
Phil Burke2155ef2017-02-24 13:50:29 -0800721AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800722
723// ============================================================
724// Stream Control
725// ============================================================
726
727/**
728 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
729 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800730 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800731 * @return AAUDIO_OK or a negative error.
732 */
Phil Burke2155ef2017-02-24 13:50:29 -0800733AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800734
735/**
736 * Asynchronously request to start playing the stream. For output streams, one should
737 * write to the stream to fill the buffer before starting.
738 * Otherwise it will underflow.
739 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
740 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800741 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800742 * @return AAUDIO_OK or a negative error.
743 */
Phil Burke2155ef2017-02-24 13:50:29 -0800744AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800745
746/**
747 * Asynchronous request for the stream to pause.
748 * Pausing a stream will freeze the data flow but not flush any buffers.
749 * Use AAudioStream_Start() to resume playback after a pause.
750 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
751 *
Phil Burk068c10f2017-05-08 16:36:41 -0700752 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
753 * For input streams use AAudioStream_requestStop().
754 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800755 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800756 * @return AAUDIO_OK or a negative error.
757 */
Phil Burke2155ef2017-02-24 13:50:29 -0800758AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800759
760/**
761 * Asynchronous request for the stream to flush.
762 * Flushing will discard any pending data.
763 * This call only works if the stream is pausing or paused. TODO review
764 * Frame counters are not reset by a flush. They may be advanced.
765 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
766 *
Phil Burk068c10f2017-05-08 16:36:41 -0700767 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
768 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800769 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800770 * @return AAUDIO_OK or a negative error.
771 */
Phil Burke2155ef2017-02-24 13:50:29 -0800772AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800773
774/**
775 * Asynchronous request for the stream to stop.
776 * The stream will stop after all of the data currently buffered has been played.
777 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
778 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800779 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800780 * @return AAUDIO_OK or a negative error.
781 */
Phil Burke2155ef2017-02-24 13:50:29 -0800782AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800783
784/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800785 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800786 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800787 * This function will immediately return the state without updating the state.
788 * If you want to update the client state based on the server state then
789 * call AAudioStream_waitForStateChange() with currentState
790 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
791 *
792 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800793 */
Phil Burke2155ef2017-02-24 13:50:29 -0800794AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800795
796/**
797 * Wait until the current state no longer matches the input state.
798 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800799 * This will update the current client state.
800 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800801 * <pre><code>
Phil Burked0a3fe2017-12-05 14:27:43 -0800802 * aaudio_result_t result = AAUDIO_OK;
803 * aaudio_stream_state_t currentState = AAudioStream_getState(stream);
804 * aaudio_stream_state_t inputState = currentState;
805 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSED) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800806 * result = AAudioStream_waitForStateChange(
Phil Burked0a3fe2017-12-05 14:27:43 -0800807 * stream, inputState, &currentState, MY_TIMEOUT_NANOS);
808 * inputState = currentState;
Phil Burk5ed503c2017-02-01 09:38:15 -0800809 * }
810 * </code></pre>
811 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800812 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800813 * @param inputState The state we want to avoid.
814 * @param nextState Pointer to a variable that will be set to the new state.
815 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
816 * @return AAUDIO_OK or a negative error.
817 */
Phil Burke2155ef2017-02-24 13:50:29 -0800818AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800819 aaudio_stream_state_t inputState,
820 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800821 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800822
823// ============================================================
824// Stream I/O
825// ============================================================
826
827/**
828 * Read data from the stream.
829 *
830 * The call will wait until the read is complete or until it runs out of time.
831 * If timeoutNanos is zero then this call will not wait.
832 *
833 * Note that timeoutNanoseconds is a relative duration in wall clock time.
834 * Time will not stop if the thread is asleep.
835 * So it will be implemented using CLOCK_BOOTTIME.
836 *
837 * This call is "strong non-blocking" unless it has to wait for data.
838 *
839 * @param stream A stream created using AAudioStreamBuilder_openStream().
840 * @param buffer The address of the first sample.
841 * @param numFrames Number of frames to read. Only complete frames will be written.
842 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800843 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800844 */
Phil Burke2155ef2017-02-24 13:50:29 -0800845AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800846 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800847 int32_t numFrames,
848 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800849
850/**
851 * Write data to the stream.
852 *
853 * The call will wait until the write is complete or until it runs out of time.
854 * If timeoutNanos is zero then this call will not wait.
855 *
856 * Note that timeoutNanoseconds is a relative duration in wall clock time.
857 * Time will not stop if the thread is asleep.
858 * So it will be implemented using CLOCK_BOOTTIME.
859 *
860 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
861 *
862 * @param stream A stream created using AAudioStreamBuilder_openStream().
863 * @param buffer The address of the first sample.
864 * @param numFrames Number of frames to write. Only complete frames will be written.
865 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
866 * @return The number of frames actually written or a negative error.
867 */
Phil Burke2155ef2017-02-24 13:50:29 -0800868AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800869 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800870 int32_t numFrames,
871 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800872
Phil Burk5ed503c2017-02-01 09:38:15 -0800873// ============================================================
874// Stream - queries
875// ============================================================
876
Phil Burk5ed503c2017-02-01 09:38:15 -0800877/**
878 * This can be used to adjust the latency of the buffer by changing
879 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800880 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800881 * at run-time for each device.
882 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800883 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800884 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800885 * Note that you will probably not get the exact size you request.
886 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
887 *
888 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700889 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800890 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800891 */
Phil Burke2155ef2017-02-24 13:50:29 -0800892AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700893 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800894
895/**
896 * Query the maximum number of frames that can be filled without blocking.
897 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800898 * @param stream reference provided by AAudioStreamBuilder_openStream()
899 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800900 */
Phil Burke2155ef2017-02-24 13:50:29 -0800901AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800902
903/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800904 * Query the number of frames that the application should read or write at
905 * one time for optimal performance. It is OK if an application writes
906 * a different number of frames. But the buffer size may need to be larger
907 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800908 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800909 * Note that this may or may not match the actual device burst size.
910 * For some endpoints, the burst size can vary dynamically.
911 * But these tend to be devices with high latency.
912 *
913 * @param stream reference provided by AAudioStreamBuilder_openStream()
914 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800915 */
Phil Burke2155ef2017-02-24 13:50:29 -0800916AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800917
918/**
919 * Query maximum buffer capacity in frames.
920 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800921 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700922 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800923 */
Phil Burke2155ef2017-02-24 13:50:29 -0800924AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800925
926/**
Phil Burke057ca92017-03-28 11:31:34 -0700927 * Query the size of the buffer that will be passed to the dataProc callback
928 * in the numFrames parameter.
929 *
930 * This call can be used if the application needs to know the value of numFrames before
931 * the stream is started. This is not normally necessary.
932 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800933 * If a specific size was requested by calling AAudioStreamBuilder_setFramesPerDataCallback()
Phil Burke057ca92017-03-28 11:31:34 -0700934 * then this will be the same size.
935 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800936 * If AAudioStreamBuilder_setFramesPerDataCallback() was not called then this will
Phil Burke057ca92017-03-28 11:31:34 -0700937 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
938 *
939 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
940 * may vary from one dataProc callback to the next.
941 *
942 * @param stream reference provided by AAudioStreamBuilder_openStream()
943 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
944 */
945AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
946
947/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800948 * An XRun is an Underrun or an Overrun.
949 * During playing, an underrun will occur if the stream is not written in time
950 * and the system runs out of valid data.
951 * During recording, an overrun will occur if the stream is not read in time
952 * and there is no place to put the incoming data so it is discarded.
953 *
954 * An underrun or overrun can cause an audible "pop" or "glitch".
955 *
Phil Burk068c10f2017-05-08 16:36:41 -0700956 * Note that some INPUT devices may not support this function.
957 * In that case a 0 will always be returned.
958 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800959 * @param stream reference provided by AAudioStreamBuilder_openStream()
960 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800961 */
Phil Burke2155ef2017-02-24 13:50:29 -0800962AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800963
964/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800965 * @param stream reference provided by AAudioStreamBuilder_openStream()
966 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800967 */
Phil Burke2155ef2017-02-24 13:50:29 -0800968AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800969
970/**
Phil Burk20523ed2017-04-24 17:04:01 -0700971 * A stream has one or more channels of data.
972 * A frame will contain one sample for each channel.
973 *
974 * @param stream reference provided by AAudioStreamBuilder_openStream()
975 * @return actual number of channels
976 */
977AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);
978
979/**
Phil Burke74240d2017-08-03 15:25:43 -0700980 * Identical to AAudioStream_getChannelCount().
981 *
982 * @param stream reference provided by AAudioStreamBuilder_openStream()
983 * @return actual number of samples frame
984 */
985AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
986
987/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800988 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -0800989 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -0800990 */
Phil Burke2155ef2017-02-24 13:50:29 -0800991AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800992
993/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800994 * @param stream reference provided by AAudioStreamBuilder_openStream()
995 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -0800996 */
Phil Burk9dca9822017-05-26 14:27:43 -0700997AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800998
999/**
1000 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -08001001 * @param stream reference provided by AAudioStreamBuilder_openStream()
1002 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -08001003 */
Phil Burke2155ef2017-02-24 13:50:29 -08001004AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001005
1006/**
Phil Burke2fbb592017-05-01 15:05:52 -07001007 * Get the performance mode used by the stream.
1008 *
1009 * @param stream reference provided by AAudioStreamBuilder_openStream()
1010 */
1011AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);
1012
1013/**
Phil Burk3316d5e2017-02-15 11:23:01 -08001014 * @param stream reference provided by AAudioStreamBuilder_openStream()
1015 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -08001016 */
Phil Burke2155ef2017-02-24 13:50:29 -08001017AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001018
1019/**
1020 * Passes back the number of frames that have been written since the stream was created.
1021 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -08001022 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -08001023 *
1024 * The frame position is monotonically increasing.
1025 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001026 * @param stream reference provided by AAudioStreamBuilder_openStream()
1027 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -08001028 */
Phil Burke2155ef2017-02-24 13:50:29 -08001029AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001030
1031/**
1032 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -08001033 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -08001034 * For an input stream, this will be advanced by the application calling read().
1035 *
1036 * The frame position is monotonically increasing.
1037 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001038 * @param stream reference provided by AAudioStreamBuilder_openStream()
1039 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -08001040 */
Phil Burke2155ef2017-02-24 13:50:29 -08001041AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001042
1043/**
Phil Burk7e7dcaa2018-01-03 15:16:15 -08001044 * Passes back the session ID associated with this stream.
1045 *
1046 * The session ID can be used to associate a stream with effects processors.
1047 * The effects are controlled using the Android AudioEffect Java API.
1048 *
1049 * If AAudioStreamBuilder_setSessionId() was called with AAUDIO_SESSION_ID_ALLOCATE
1050 * then a new session ID should be allocated once when the stream is opened.
1051 *
1052 * If AAudioStreamBuilder_setSessionId() was called with a previously allocated
1053 * session ID then that value should be returned.
1054 *
1055 * If AAudioStreamBuilder_setSessionId() was not called then this function should
1056 * return AAUDIO_SESSION_ID_NONE.
1057 *
1058 * The sessionID for a stream should not change once the stream has been opened.
1059 *
1060 * @param stream reference provided by AAudioStreamBuilder_openStream()
1061 * @return session ID or AAUDIO_SESSION_ID_NONE
1062 */
1063AAUDIO_API aaudio_session_id_t AAudioStream_getSessionId(AAudioStream* stream);
1064
1065/**
Phil Burk5ed503c2017-02-01 09:38:15 -08001066 * Passes back the time at which a particular frame was presented.
1067 * This can be used to synchronize audio with video or MIDI.
1068 * It can also be used to align a recorded stream with a playback stream.
1069 *
1070 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
1071 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
1072 * Note that because requestStart() is asynchronous, timestamps will not be valid until
1073 * a short time after calling requestStart().
1074 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
1075 * Just try calling again later.
1076 *
1077 * If an error occurs, then the position and time will not be modified.
1078 *
1079 * The position and time passed back are monotonically increasing.
1080 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001081 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burkec159502017-07-25 17:33:47 -07001082 * @param clockid CLOCK_MONOTONIC or CLOCK_BOOTTIME
Phil Burk5ed503c2017-02-01 09:38:15 -08001083 * @param framePosition pointer to a variable to receive the position
1084 * @param timeNanoseconds pointer to a variable to receive the time
1085 * @return AAUDIO_OK or a negative error
1086 */
Phil Burke2155ef2017-02-24 13:50:29 -08001087AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -08001088 clockid_t clockid,
1089 int64_t *framePosition,
1090 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -08001091
Phil Burk361b1422017-12-20 14:24:16 -08001092/**
1093 * Return the use case for the stream.
1094 *
1095 * @param stream reference provided by AAudioStreamBuilder_openStream()
1096 * @return frames read
1097 */
1098AAUDIO_API aaudio_usage_t AAudioStream_getUsage(AAudioStream* stream);
1099
1100/**
1101 * Return the content type for the stream.
1102 *
1103 * @param stream reference provided by AAudioStreamBuilder_openStream()
1104 * @return content type, for example AAUDIO_CONTENT_TYPE_MUSIC
1105 */
1106AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* stream);
1107
1108/**
1109 * Return the input preset for the stream.
1110 *
1111 * @param stream reference provided by AAudioStreamBuilder_openStream()
1112 * @return input preset, for example AAUDIO_INPUT_PRESET_CAMCORDER
1113 */
1114AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream);
1115
Phil Burk5ed503c2017-02-01 09:38:15 -08001116#ifdef __cplusplus
1117}
1118#endif
1119
1120#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -07001121
1122/** @} */