blob: 2207cb8c0659604d36c903de71348439de3de238 [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.
Phil Burk42452c02018-04-10 12:43:33 -0700149 *
150 * Added in API level 28.
Phil Burk361b1422017-12-20 14:24:16 -0800151 */
152enum {
153 /**
154 * Use this for streaming media, music performance, video, podcasts, etcetera.
155 */
156 AAUDIO_USAGE_MEDIA = 1,
157
158 /**
159 * Use this for voice over IP, telephony, etcetera.
160 */
161 AAUDIO_USAGE_VOICE_COMMUNICATION = 2,
162
163 /**
164 * Use this for sounds associated with telephony such as busy tones, DTMF, etcetera.
165 */
166 AAUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING = 3,
167
168 /**
169 * Use this to demand the users attention.
170 */
171 AAUDIO_USAGE_ALARM = 4,
172
173 /**
174 * Use this for notifying the user when a message has arrived or some
175 * other background event has occured.
176 */
177 AAUDIO_USAGE_NOTIFICATION = 5,
178
179 /**
180 * Use this when the phone rings.
181 */
182 AAUDIO_USAGE_NOTIFICATION_RINGTONE = 6,
183
184 /**
185 * Use this to attract the users attention when, for example, the battery is low.
186 */
187 AAUDIO_USAGE_NOTIFICATION_EVENT = 10,
188
189 /**
190 * Use this for screen readers, etcetera.
191 */
192 AAUDIO_USAGE_ASSISTANCE_ACCESSIBILITY = 11,
193
194 /**
195 * Use this for driving or navigation directions.
196 */
197 AAUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12,
198
199 /**
200 * Use this for user interface sounds, beeps, etcetera.
201 */
202 AAUDIO_USAGE_ASSISTANCE_SONIFICATION = 13,
203
204 /**
205 * Use this for game audio and sound effects.
206 */
207 AAUDIO_USAGE_GAME = 14,
208
209 /**
210 * Use this for audio responses to user queries, audio instructions or help utterances.
211 */
212 AAUDIO_USAGE_ASSISTANT = 16
213};
214typedef int32_t aaudio_usage_t;
215
216/**
217 * The CONTENT_TYPE attribute describes "what" you are playing.
218 * It expresses the general category of the content. This information is optional.
219 * But in case it is known (for instance {@link #AAUDIO_CONTENT_TYPE_MOVIE} for a
220 * movie streaming service or {@link #AAUDIO_CONTENT_TYPE_SPEECH} for
221 * an audio book application) this information might be used by the audio framework to
222 * enforce audio focus.
223 *
224 * Note that these match the equivalent values in AudioAttributes in the Android Java API.
Phil Burk42452c02018-04-10 12:43:33 -0700225 *
226 * Added in API level 28.
Phil Burk361b1422017-12-20 14:24:16 -0800227 */
228enum {
229
230 /**
231 * Use this for spoken voice, audio books, etcetera.
232 */
233 AAUDIO_CONTENT_TYPE_SPEECH = 1,
234
235 /**
236 * Use this for pre-recorded or live music.
237 */
238 AAUDIO_CONTENT_TYPE_MUSIC = 2,
239
240 /**
241 * Use this for a movie or video soundtrack.
242 */
243 AAUDIO_CONTENT_TYPE_MOVIE = 3,
244
245 /**
246 * Use this for sound is designed to accompany a user action,
247 * such as a click or beep sound made when the user presses a button.
248 */
249 AAUDIO_CONTENT_TYPE_SONIFICATION = 4
250};
251typedef int32_t aaudio_content_type_t;
252
253/**
254 * Defines the audio source.
255 * An audio source defines both a default physical source of audio signal, and a recording
256 * configuration.
257 *
258 * Note that these match the equivalent values in MediaRecorder.AudioSource in the Android Java API.
Phil Burk42452c02018-04-10 12:43:33 -0700259 *
260 * Added in API level 28.
Phil Burk361b1422017-12-20 14:24:16 -0800261 */
262enum {
263 /**
264 * Use this preset when other presets do not apply.
265 */
266 AAUDIO_INPUT_PRESET_GENERIC = 1,
267
268 /**
269 * Use this preset when recording video.
270 */
271 AAUDIO_INPUT_PRESET_CAMCORDER = 5,
272
273 /**
274 * Use this preset when doing speech recognition.
275 */
276 AAUDIO_INPUT_PRESET_VOICE_RECOGNITION = 6,
277
278 /**
279 * Use this preset when doing telephony or voice messaging.
280 */
281 AAUDIO_INPUT_PRESET_VOICE_COMMUNICATION = 7,
282
283 /**
284 * Use this preset to obtain an input with no effects.
285 * Note that this input will not have automatic gain control
286 * so the recorded volume may be very low.
287 */
288 AAUDIO_INPUT_PRESET_UNPROCESSED = 9,
289};
290typedef int32_t aaudio_input_preset_t;
291
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800292enum {
293 /**
294 * Do not allocate a session ID.
295 * Effects cannot be used with this stream.
296 * Default.
Phil Burk42452c02018-04-10 12:43:33 -0700297 *
298 * Added in API level 28.
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800299 */
300 AAUDIO_SESSION_ID_NONE = -1,
301
302 /**
303 * Allocate a session ID that can be used to attach and control
304 * effects using the Java AudioEffects API.
305 * Note that the use of this flag may result in higher latency.
306 *
307 * Note that this matches the value of AudioManager.AUDIO_SESSION_ID_GENERATE.
Phil Burk42452c02018-04-10 12:43:33 -0700308 *
309 * Added in API level 28.
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800310 */
311 AAUDIO_SESSION_ID_ALLOCATE = 0,
312};
313typedef int32_t aaudio_session_id_t;
314
Phil Burke2155ef2017-02-24 13:50:29 -0800315typedef struct AAudioStreamStruct AAudioStream;
316typedef struct AAudioStreamBuilderStruct AAudioStreamBuilder;
Phil Burk5ed503c2017-02-01 09:38:15 -0800317
Phil Burk5ed503c2017-02-01 09:38:15 -0800318#ifndef AAUDIO_API
Phil Burk3316d5e2017-02-15 11:23:01 -0800319#define AAUDIO_API /* export this symbol */
Phil Burk5ed503c2017-02-01 09:38:15 -0800320#endif
321
322// ============================================================
323// Audio System
324// ============================================================
325
326/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800327 * The text is the ASCII symbol corresponding to the returnCode,
328 * or an English message saying the returnCode 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 result code.
333 */
334AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
335
336/**
337 * The text is the ASCII symbol corresponding to the stream state,
338 * or an English message saying the state is unrecognized.
339 * This is intended for developers to use when debugging.
340 * It is not for display to users.
341 *
342 * @return pointer to a text representation of an AAudio state.
343 */
344AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
345
346// ============================================================
347// StreamBuilder
348// ============================================================
349
350/**
351 * Create a StreamBuilder that can be used to open a Stream.
352 *
353 * The deviceId is initially unspecified, meaning that the current default device will be used.
354 *
355 * The default direction is AAUDIO_DIRECTION_OUTPUT.
Phil Burk3316d5e2017-02-15 11:23:01 -0800356 * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
Phil Burk5ed503c2017-02-01 09:38:15 -0800357 * The data format, samplesPerFrames and sampleRate are unspecified and will be
358 * chosen by the device when it is opened.
359 *
360 * AAudioStreamBuilder_delete() must be called when you are done using the builder.
361 */
Phil Burke2155ef2017-02-24 13:50:29 -0800362AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800363
364/**
365 * Request an audio device identified device using an ID.
Phil Burk5ed503c2017-02-01 09:38:15 -0800366 * On Android, for example, the ID could be obtained from the Java AudioManager.
367 *
Glenn Kasten5f510d22017-05-30 15:52:15 -0700368 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED,
Phil Burke057ca92017-03-28 11:31:34 -0700369 * in which case the primary device will be used.
Phil Burk5ed503c2017-02-01 09:38:15 -0800370 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800371 * @param builder reference provided by AAudio_createStreamBuilder()
Glenn Kasten5f510d22017-05-30 15:52:15 -0700372 * @param deviceId device identifier or AAUDIO_UNSPECIFIED
Phil Burk5ed503c2017-02-01 09:38:15 -0800373 */
Phil Burke2155ef2017-02-24 13:50:29 -0800374AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800375 int32_t deviceId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800376
377/**
Phil Burke057ca92017-03-28 11:31:34 -0700378 * Request a sample rate in Hertz.
379 *
Phil Burke057ca92017-03-28 11:31:34 -0700380 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700381 * An optimal value will then be chosen when the stream is opened.
382 * After opening a stream with an unspecified value, the application must
383 * query for the actual value, which may vary by device.
384 *
385 * If an exact value is specified then an opened stream will use that value.
386 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700387 *
388 * @param builder reference provided by AAudio_createStreamBuilder()
389 * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
Phil Burk5ed503c2017-02-01 09:38:15 -0800390 */
Phil Burke2155ef2017-02-24 13:50:29 -0800391AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
Phil Burk3316d5e2017-02-15 11:23:01 -0800392 int32_t sampleRate);
Phil Burk5ed503c2017-02-01 09:38:15 -0800393
394/**
Phil Burk20523ed2017-04-24 17:04:01 -0700395 * Request a number of channels for the stream.
Phil Burke057ca92017-03-28 11:31:34 -0700396 *
Phil Burke057ca92017-03-28 11:31:34 -0700397 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700398 * An optimal value will then be chosen when the stream is opened.
399 * After opening a stream with an unspecified value, the application must
400 * query for the actual value, which may vary by device.
Phil Burk5ed503c2017-02-01 09:38:15 -0800401 *
Phil Burk8f624892017-05-11 11:44:20 -0700402 * If an exact value is specified then an opened stream will use that value.
403 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700404 *
405 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk20523ed2017-04-24 17:04:01 -0700406 * @param channelCount Number of channels desired.
Phil Burk5ed503c2017-02-01 09:38:15 -0800407 */
Phil Burk20523ed2017-04-24 17:04:01 -0700408AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
409 int32_t channelCount);
410
411/**
Phil Burke74240d2017-08-03 15:25:43 -0700412 * Identical to AAudioStreamBuilder_setChannelCount().
413 *
414 * @param builder reference provided by AAudio_createStreamBuilder()
415 * @param samplesPerFrame Number of samples in a frame.
416 */
417AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
418 int32_t samplesPerFrame);
419
420/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800421 * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
Phil Burke057ca92017-03-28 11:31:34 -0700422 *
423 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk8f624892017-05-11 11:44:20 -0700424 * An optimal value will then be chosen when the stream is opened.
425 * After opening a stream with an unspecified value, the application must
426 * query for the actual value, which may vary by device.
Phil Burke057ca92017-03-28 11:31:34 -0700427 *
Phil Burk8f624892017-05-11 11:44:20 -0700428 * If an exact value is specified then an opened stream will use that value.
429 * If a stream cannot be opened with the specified value then the open will fail.
Phil Burke057ca92017-03-28 11:31:34 -0700430 *
431 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk8f624892017-05-11 11:44:20 -0700432 * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
Phil Burk5ed503c2017-02-01 09:38:15 -0800433 */
Phil Burke2155ef2017-02-24 13:50:29 -0800434AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
Phil Burk9dca9822017-05-26 14:27:43 -0700435 aaudio_format_t format);
Phil Burk5ed503c2017-02-01 09:38:15 -0800436
437/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800438 * Request a mode for sharing the device.
Phil Burke057ca92017-03-28 11:31:34 -0700439 *
440 * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
441 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800442 * The requested sharing mode may not be available.
Phil Burke057ca92017-03-28 11:31:34 -0700443 * The application can query for the actual mode after the stream is opened.
Phil Burk5ed503c2017-02-01 09:38:15 -0800444 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800445 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700446 * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
Phil Burk5ed503c2017-02-01 09:38:15 -0800447 */
Phil Burke2155ef2017-02-24 13:50:29 -0800448AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
Phil Burk5ed503c2017-02-01 09:38:15 -0800449 aaudio_sharing_mode_t sharingMode);
450
451/**
Phil Burke057ca92017-03-28 11:31:34 -0700452 * Request the direction for a stream.
453 *
454 * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
Phil Burk5ed503c2017-02-01 09:38:15 -0800455 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800456 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800457 * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
Phil Burk5ed503c2017-02-01 09:38:15 -0800458 */
Phil Burke2155ef2017-02-24 13:50:29 -0800459AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
Phil Burk3df348f2017-02-08 11:41:55 -0800460 aaudio_direction_t direction);
Phil Burk5ed503c2017-02-01 09:38:15 -0800461
462/**
Phil Burke057ca92017-03-28 11:31:34 -0700463 * Set the requested buffer capacity in frames.
Phil Burk3df348f2017-02-08 11:41:55 -0800464 * The final AAudioStream capacity may differ, but will probably be at least this big.
465 *
Phil Burke057ca92017-03-28 11:31:34 -0700466 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
Phil Burk3df348f2017-02-08 11:41:55 -0800467 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800468 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burke057ca92017-03-28 11:31:34 -0700469 * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
Phil Burk3df348f2017-02-08 11:41:55 -0800470 */
Phil Burke2155ef2017-02-24 13:50:29 -0800471AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
Phil Burke057ca92017-03-28 11:31:34 -0700472 int32_t numFrames);
Phil Burke2fbb592017-05-01 15:05:52 -0700473
474/**
475 * Set the requested performance mode.
476 *
477 * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
478 *
479 * @param builder reference provided by AAudio_createStreamBuilder()
480 * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
481 */
482AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
483 aaudio_performance_mode_t mode);
484
Phil Burke057ca92017-03-28 11:31:34 -0700485/**
Phil Burk361b1422017-12-20 14:24:16 -0800486 * Set the intended use case for the stream.
487 *
488 * The AAudio system will use this information to optimize the
489 * behavior of the stream.
490 * This could, for example, affect how volume and focus is handled for the stream.
491 *
492 * The default, if you do not call this function, is AAUDIO_USAGE_MEDIA.
493 *
Phil Burk42452c02018-04-10 12:43:33 -0700494 * Added in API level 28.
495 *
Phil Burk361b1422017-12-20 14:24:16 -0800496 * @param builder reference provided by AAudio_createStreamBuilder()
497 * @param usage the desired usage, eg. AAUDIO_USAGE_GAME
498 */
499AAUDIO_API void AAudioStreamBuilder_setUsage(AAudioStreamBuilder* builder,
500 aaudio_usage_t usage);
501
502/**
503 * Set the type of audio data that the stream will carry.
504 *
505 * The AAudio system will use this information to optimize the
506 * behavior of the stream.
507 * This could, for example, affect whether a stream is paused when a notification occurs.
508 *
509 * The default, if you do not call this function, is AAUDIO_CONTENT_TYPE_MUSIC.
510 *
Phil Burk42452c02018-04-10 12:43:33 -0700511 * Added in API level 28.
512 *
Phil Burk361b1422017-12-20 14:24:16 -0800513 * @param builder reference provided by AAudio_createStreamBuilder()
514 * @param contentType the type of audio data, eg. AAUDIO_CONTENT_TYPE_SPEECH
515 */
516AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
517 aaudio_content_type_t contentType);
518
519/**
520 * Set the input (capture) preset for the stream.
521 *
522 * The AAudio system will use this information to optimize the
523 * behavior of the stream.
524 * This could, for example, affect which microphones are used and how the
525 * recorded data is processed.
526 *
Phil Burkeaef9b92018-01-18 09:09:42 -0800527 * The default, if you do not call this function, is AAUDIO_INPUT_PRESET_VOICE_RECOGNITION.
528 * That is because VOICE_RECOGNITION is the preset with the lowest latency
529 * on many platforms.
Phil Burk361b1422017-12-20 14:24:16 -0800530 *
Phil Burk42452c02018-04-10 12:43:33 -0700531 * Added in API level 28.
532 *
Phil Burk361b1422017-12-20 14:24:16 -0800533 * @param builder reference provided by AAudio_createStreamBuilder()
534 * @param inputPreset the desired configuration for recording
535 */
536AAUDIO_API void AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder* builder,
537 aaudio_input_preset_t inputPreset);
538
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800539/** Set the requested session ID.
540 *
541 * The session ID can be used to associate a stream with effects processors.
542 * The effects are controlled using the Android AudioEffect Java API.
543 *
544 * The default, if you do not call this function, is AAUDIO_SESSION_ID_NONE.
545 *
546 * If set to AAUDIO_SESSION_ID_ALLOCATE then a session ID will be allocated
547 * when the stream is opened.
548 *
549 * The allocated session ID can be obtained by calling AAudioStream_getSessionId()
550 * and then used with this function when opening another stream.
551 * This allows effects to be shared between streams.
552 *
553 * Session IDs from AAudio can be used the Android Java APIs and vice versa.
554 * So a session ID from an AAudio stream can be passed to Java
555 * and effects applied using the Java AudioEffect API.
556 *
557 * Allocated session IDs will always be positive and nonzero.
558 *
Phil Burk42452c02018-04-10 12:43:33 -0700559 * Added in API level 28.
560 *
Phil Burk7e7dcaa2018-01-03 15:16:15 -0800561 * @param builder reference provided by AAudio_createStreamBuilder()
562 * @param sessionId an allocated sessionID or AAUDIO_SESSION_ID_ALLOCATE
563 */
564AAUDIO_API void AAudioStreamBuilder_setSessionId(AAudioStreamBuilder* builder,
565 aaudio_session_id_t sessionId);
566
Phil Burk361b1422017-12-20 14:24:16 -0800567/**
Phil Burke057ca92017-03-28 11:31:34 -0700568 * Return one of these values from the data callback function.
569 */
570enum {
571
572 /**
573 * Continue calling the callback.
574 */
575 AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
576
577 /**
578 * Stop calling the callback.
579 *
580 * The application will still need to call AAudioStream_requestPause()
581 * or AAudioStream_requestStop().
582 */
583 AAUDIO_CALLBACK_RESULT_STOP,
584
585};
586typedef int32_t aaudio_data_callback_result_t;
587
588/**
589 * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
590 *
591 * For an output stream, this function should render and write numFrames of data
592 * in the streams current data format to the audioData buffer.
593 *
594 * For an input stream, this function should read and process numFrames of data
595 * from the audioData buffer.
596 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800597 * The audio data is passed through the buffer. So do NOT call AAudioStream_read() or
598 * AAudioStream_write() on the stream that is making the callback.
599 *
600 * Note that numFrames can vary unless AAudioStreamBuilder_setFramesPerDataCallback()
601 * is called.
602 *
603 * Also note that this callback function should be considered a "real-time" function.
Phil Burke057ca92017-03-28 11:31:34 -0700604 * It must not do anything that could cause an unbounded delay because that can cause the
605 * audio to glitch or pop.
606 *
607 * These are things the function should NOT do:
608 * <ul>
609 * <li>allocate memory using, for example, malloc() or new</li>
610 * <li>any file operations such as opening, closing, reading or writing</li>
611 * <li>any network operations such as streaming</li>
612 * <li>use any mutexes or other synchronization primitives</li>
613 * <li>sleep</li>
Phil Burked0a3fe2017-12-05 14:27:43 -0800614 * <li>stop or close the stream</li>
Phil Burke057ca92017-03-28 11:31:34 -0700615 * </ul>
616 *
617 * If you need to move data, eg. MIDI commands, in or out of the callback function then
618 * we recommend the use of non-blocking techniques such as an atomic FIFO.
619 *
620 * @param stream reference provided by AAudioStreamBuilder_openStream()
621 * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
622 * @param audioData a pointer to the audio data
Phil Burked0a3fe2017-12-05 14:27:43 -0800623 * @param numFrames the number of frames to be processed, which can vary
Phil Burke057ca92017-03-28 11:31:34 -0700624 * @return AAUDIO_CALLBACK_RESULT_*
625 */
626typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
627 AAudioStream *stream,
628 void *userData,
629 void *audioData,
630 int32_t numFrames);
631
632/**
633 * Request that AAudio call this functions when the stream is running.
634 *
635 * Note that when using this callback, the audio data will be passed in or out
636 * of the function as an argument.
637 * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
638 * that has an active data callback.
639 *
640 * The callback function will start being called after AAudioStream_requestStart() is called.
641 * It will stop being called after AAudioStream_requestPause() or
642 * AAudioStream_requestStop() is called.
643 *
644 * This callback function will be called on a real-time thread owned by AAudio. See
Glenn Kasten0d804362017-04-13 09:20:14 -0700645 * {@link AAudioStream_dataCallback} for more information.
Phil Burke057ca92017-03-28 11:31:34 -0700646 *
647 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
648 *
649 * @param builder reference provided by AAudio_createStreamBuilder()
650 * @param callback pointer to a function that will process audio data.
651 * @param userData pointer to an application data structure that will be passed
652 * to the callback functions.
653 */
654AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
655 AAudioStream_dataCallback callback,
656 void *userData);
657
658/**
659 * Set the requested data callback buffer size in frames.
660 * See {@link AAudioStream_dataCallback}.
661 *
662 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
663 *
664 * For the lowest possible latency, do not call this function. AAudio will then
665 * call the dataProc callback function with whatever size is optimal.
666 * That size may vary from one callback to another.
667 *
668 * Only use this function if the application requires a specific number of frames for processing.
669 * The application might, for example, be using an FFT that requires
670 * a specific power-of-two sized buffer.
671 *
672 * AAudio may need to add additional buffering in order to adapt between the internal
673 * buffer size and the requested buffer size.
674 *
675 * If you do call this function then the requested size should be less than
676 * half the buffer capacity, to allow double buffering.
677 *
678 * @param builder reference provided by AAudio_createStreamBuilder()
679 * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
680 */
681AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
682 int32_t numFrames);
683
684/**
685 * Prototype for the callback function that is passed to
686 * AAudioStreamBuilder_setErrorCallback().
687 *
688 * @param stream reference provided by AAudioStreamBuilder_openStream()
689 * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
690 * @param error an AAUDIO_ERROR_* value.
691 */
692typedef void (*AAudioStream_errorCallback)(
693 AAudioStream *stream,
694 void *userData,
695 aaudio_result_t error);
696
697/**
Phil Burked0a3fe2017-12-05 14:27:43 -0800698 * Request that AAudio call this function if any error occurs or the stream is disconnected.
Phil Burke057ca92017-03-28 11:31:34 -0700699 *
700 * 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 -0800701 * device to be unavailable or "disconnected".
Phil Burke057ca92017-03-28 11:31:34 -0700702 * Another possible cause of error would be a timeout or an unanticipated internal error.
703 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800704 * In response, this function should signal or create another thread to stop
705 * and close this stream. The other thread could then reopen a stream on another device.
706 * Do not stop or close the stream, or reopen the new stream, directly from this callback.
707 *
708 * This callback will not be called because of actions by the application, such as stopping
709 * or closing a stream.
710 *
Phil Burke057ca92017-03-28 11:31:34 -0700711 * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
712 *
713 * @param builder reference provided by AAudio_createStreamBuilder()
714 * @param callback pointer to a function that will be called if an error occurs.
715 * @param userData pointer to an application data structure that will be passed
716 * to the callback functions.
717 */
718AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
719 AAudioStream_errorCallback callback,
720 void *userData);
Phil Burk5ed503c2017-02-01 09:38:15 -0800721
722/**
723 * Open a stream based on the options in the StreamBuilder.
724 *
725 * AAudioStream_close must be called when finished with the stream to recover
726 * the memory and to free the associated resources.
727 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800728 * @param builder reference provided by AAudio_createStreamBuilder()
729 * @param stream pointer to a variable to receive the new stream reference
Phil Burk5ed503c2017-02-01 09:38:15 -0800730 * @return AAUDIO_OK or a negative error.
731 */
Phil Burke2155ef2017-02-24 13:50:29 -0800732AAUDIO_API aaudio_result_t AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
733 AAudioStream** stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800734
735/**
736 * Delete the resources associated with the StreamBuilder.
737 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800738 * @param builder reference provided by AAudio_createStreamBuilder()
Phil Burk5ed503c2017-02-01 09:38:15 -0800739 * @return AAUDIO_OK or a negative error.
740 */
Phil Burke2155ef2017-02-24 13:50:29 -0800741AAUDIO_API aaudio_result_t AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
Phil Burk5ed503c2017-02-01 09:38:15 -0800742
743// ============================================================
744// Stream Control
745// ============================================================
746
747/**
748 * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
749 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800750 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800751 * @return AAUDIO_OK or a negative error.
752 */
Phil Burke2155ef2017-02-24 13:50:29 -0800753AAUDIO_API aaudio_result_t AAudioStream_close(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800754
755/**
756 * Asynchronously request to start playing the stream. For output streams, one should
757 * write to the stream to fill the buffer before starting.
758 * Otherwise it will underflow.
759 * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
760 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800761 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800762 * @return AAUDIO_OK or a negative error.
763 */
Phil Burke2155ef2017-02-24 13:50:29 -0800764AAUDIO_API aaudio_result_t AAudioStream_requestStart(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800765
766/**
767 * Asynchronous request for the stream to pause.
768 * Pausing a stream will freeze the data flow but not flush any buffers.
769 * Use AAudioStream_Start() to resume playback after a pause.
770 * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
771 *
Phil Burk068c10f2017-05-08 16:36:41 -0700772 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
773 * For input streams use AAudioStream_requestStop().
774 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800775 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800776 * @return AAUDIO_OK or a negative error.
777 */
Phil Burke2155ef2017-02-24 13:50:29 -0800778AAUDIO_API aaudio_result_t AAudioStream_requestPause(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800779
780/**
781 * Asynchronous request for the stream to flush.
782 * Flushing will discard any pending data.
783 * This call only works if the stream is pausing or paused. TODO review
784 * Frame counters are not reset by a flush. They may be advanced.
785 * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
786 *
Phil Burk068c10f2017-05-08 16:36:41 -0700787 * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
788 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800789 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800790 * @return AAUDIO_OK or a negative error.
791 */
Phil Burke2155ef2017-02-24 13:50:29 -0800792AAUDIO_API aaudio_result_t AAudioStream_requestFlush(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800793
794/**
795 * Asynchronous request for the stream to stop.
796 * The stream will stop after all of the data currently buffered has been played.
797 * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
798 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800799 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800800 * @return AAUDIO_OK or a negative error.
801 */
Phil Burke2155ef2017-02-24 13:50:29 -0800802AAUDIO_API aaudio_result_t AAudioStream_requestStop(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800803
804/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800805 * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
Phil Burk5ed503c2017-02-01 09:38:15 -0800806 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800807 * This function will immediately return the state without updating the state.
808 * If you want to update the client state based on the server state then
809 * call AAudioStream_waitForStateChange() with currentState
810 * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
811 *
812 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800813 */
Phil Burke2155ef2017-02-24 13:50:29 -0800814AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800815
816/**
817 * Wait until the current state no longer matches the input state.
818 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800819 * This will update the current client state.
820 *
Phil Burk5ed503c2017-02-01 09:38:15 -0800821 * <pre><code>
Phil Burked0a3fe2017-12-05 14:27:43 -0800822 * aaudio_result_t result = AAUDIO_OK;
823 * aaudio_stream_state_t currentState = AAudioStream_getState(stream);
824 * aaudio_stream_state_t inputState = currentState;
825 * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSED) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800826 * result = AAudioStream_waitForStateChange(
Phil Burked0a3fe2017-12-05 14:27:43 -0800827 * stream, inputState, &currentState, MY_TIMEOUT_NANOS);
828 * inputState = currentState;
Phil Burk5ed503c2017-02-01 09:38:15 -0800829 * }
830 * </code></pre>
831 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800832 * @param stream A reference provided by AAudioStreamBuilder_openStream()
Phil Burk5ed503c2017-02-01 09:38:15 -0800833 * @param inputState The state we want to avoid.
834 * @param nextState Pointer to a variable that will be set to the new state.
835 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
836 * @return AAUDIO_OK or a negative error.
837 */
Phil Burke2155ef2017-02-24 13:50:29 -0800838AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800839 aaudio_stream_state_t inputState,
840 aaudio_stream_state_t *nextState,
Phil Burk3316d5e2017-02-15 11:23:01 -0800841 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800842
843// ============================================================
844// Stream I/O
845// ============================================================
846
847/**
848 * Read data from the stream.
849 *
850 * The call will wait until the read is complete or until it runs out of time.
851 * If timeoutNanos is zero then this call will not wait.
852 *
853 * Note that timeoutNanoseconds is a relative duration in wall clock time.
854 * Time will not stop if the thread is asleep.
855 * So it will be implemented using CLOCK_BOOTTIME.
856 *
857 * This call is "strong non-blocking" unless it has to wait for data.
858 *
859 * @param stream A stream created using AAudioStreamBuilder_openStream().
860 * @param buffer The address of the first sample.
861 * @param numFrames Number of frames to read. Only complete frames will be written.
862 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
Phil Burk3316d5e2017-02-15 11:23:01 -0800863 * @return The number of frames actually read or a negative error.
Phil Burk5ed503c2017-02-01 09:38:15 -0800864 */
Phil Burke2155ef2017-02-24 13:50:29 -0800865AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800866 void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800867 int32_t numFrames,
868 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800869
870/**
871 * Write data to the stream.
872 *
873 * The call will wait until the write is complete or until it runs out of time.
874 * If timeoutNanos is zero then this call will not wait.
875 *
876 * Note that timeoutNanoseconds is a relative duration in wall clock time.
877 * Time will not stop if the thread is asleep.
878 * So it will be implemented using CLOCK_BOOTTIME.
879 *
880 * This call is "strong non-blocking" unless it has to wait for room in the buffer.
881 *
882 * @param stream A stream created using AAudioStreamBuilder_openStream().
883 * @param buffer The address of the first sample.
884 * @param numFrames Number of frames to write. Only complete frames will be written.
885 * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
886 * @return The number of frames actually written or a negative error.
887 */
Phil Burke2155ef2017-02-24 13:50:29 -0800888AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
Phil Burk5ed503c2017-02-01 09:38:15 -0800889 const void *buffer,
Phil Burk3316d5e2017-02-15 11:23:01 -0800890 int32_t numFrames,
891 int64_t timeoutNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -0800892
Phil Burk5ed503c2017-02-01 09:38:15 -0800893// ============================================================
894// Stream - queries
895// ============================================================
896
Phil Burk5ed503c2017-02-01 09:38:15 -0800897/**
898 * This can be used to adjust the latency of the buffer by changing
899 * the threshold where blocking will occur.
Phil Burk3316d5e2017-02-15 11:23:01 -0800900 * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
Phil Burk5ed503c2017-02-01 09:38:15 -0800901 * at run-time for each device.
902 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800903 * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
Phil Burk5ed503c2017-02-01 09:38:15 -0800904 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800905 * Note that you will probably not get the exact size you request.
906 * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
907 *
908 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700909 * @param numFrames requested number of frames that can be filled without blocking
Phil Burk3316d5e2017-02-15 11:23:01 -0800910 * @return actual buffer size in frames or a negative error
Phil Burk5ed503c2017-02-01 09:38:15 -0800911 */
Phil Burke2155ef2017-02-24 13:50:29 -0800912AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
Phil Burke057ca92017-03-28 11:31:34 -0700913 int32_t numFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800914
915/**
916 * Query the maximum number of frames that can be filled without blocking.
917 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800918 * @param stream reference provided by AAudioStreamBuilder_openStream()
919 * @return buffer size in frames.
Phil Burk5ed503c2017-02-01 09:38:15 -0800920 */
Phil Burke2155ef2017-02-24 13:50:29 -0800921AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800922
923/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800924 * Query the number of frames that the application should read or write at
925 * one time for optimal performance. It is OK if an application writes
926 * a different number of frames. But the buffer size may need to be larger
927 * in order to avoid underruns or overruns.
Phil Burk5ed503c2017-02-01 09:38:15 -0800928 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800929 * Note that this may or may not match the actual device burst size.
930 * For some endpoints, the burst size can vary dynamically.
931 * But these tend to be devices with high latency.
932 *
933 * @param stream reference provided by AAudioStreamBuilder_openStream()
934 * @return burst size
Phil Burk5ed503c2017-02-01 09:38:15 -0800935 */
Phil Burke2155ef2017-02-24 13:50:29 -0800936AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800937
938/**
939 * Query maximum buffer capacity in frames.
940 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800941 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke057ca92017-03-28 11:31:34 -0700942 * @return buffer capacity in frames
Phil Burk5ed503c2017-02-01 09:38:15 -0800943 */
Phil Burke2155ef2017-02-24 13:50:29 -0800944AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800945
946/**
Phil Burke057ca92017-03-28 11:31:34 -0700947 * Query the size of the buffer that will be passed to the dataProc callback
948 * in the numFrames parameter.
949 *
950 * This call can be used if the application needs to know the value of numFrames before
951 * the stream is started. This is not normally necessary.
952 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800953 * If a specific size was requested by calling AAudioStreamBuilder_setFramesPerDataCallback()
Phil Burke057ca92017-03-28 11:31:34 -0700954 * then this will be the same size.
955 *
Phil Burked0a3fe2017-12-05 14:27:43 -0800956 * If AAudioStreamBuilder_setFramesPerDataCallback() was not called then this will
Phil Burke057ca92017-03-28 11:31:34 -0700957 * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
958 *
959 * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
960 * may vary from one dataProc callback to the next.
961 *
962 * @param stream reference provided by AAudioStreamBuilder_openStream()
963 * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
964 */
965AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
966
967/**
Phil Burk5ed503c2017-02-01 09:38:15 -0800968 * An XRun is an Underrun or an Overrun.
969 * During playing, an underrun will occur if the stream is not written in time
970 * and the system runs out of valid data.
971 * During recording, an overrun will occur if the stream is not read in time
972 * and there is no place to put the incoming data so it is discarded.
973 *
974 * An underrun or overrun can cause an audible "pop" or "glitch".
975 *
Phil Burk068c10f2017-05-08 16:36:41 -0700976 * Note that some INPUT devices may not support this function.
977 * In that case a 0 will always be returned.
978 *
Phil Burk3316d5e2017-02-15 11:23:01 -0800979 * @param stream reference provided by AAudioStreamBuilder_openStream()
980 * @return the underrun or overrun count
Phil Burk5ed503c2017-02-01 09:38:15 -0800981 */
Phil Burke2155ef2017-02-24 13:50:29 -0800982AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800983
984/**
Phil Burk3316d5e2017-02-15 11:23:01 -0800985 * @param stream reference provided by AAudioStreamBuilder_openStream()
986 * @return actual sample rate
Phil Burk5ed503c2017-02-01 09:38:15 -0800987 */
Phil Burke2155ef2017-02-24 13:50:29 -0800988AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800989
990/**
Phil Burk20523ed2017-04-24 17:04:01 -0700991 * A stream has one or more channels of data.
992 * A frame will contain one sample for each channel.
993 *
994 * @param stream reference provided by AAudioStreamBuilder_openStream()
995 * @return actual number of channels
996 */
997AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);
998
999/**
Phil Burke74240d2017-08-03 15:25:43 -07001000 * Identical to AAudioStream_getChannelCount().
1001 *
1002 * @param stream reference provided by AAudioStreamBuilder_openStream()
1003 * @return actual number of samples frame
1004 */
1005AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
1006
1007/**
Phil Burk3316d5e2017-02-15 11:23:01 -08001008 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burke2155ef2017-02-24 13:50:29 -08001009 * @return actual device ID
Phil Burk5ed503c2017-02-01 09:38:15 -08001010 */
Phil Burke2155ef2017-02-24 13:50:29 -08001011AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001012
1013/**
Phil Burk3316d5e2017-02-15 11:23:01 -08001014 * @param stream reference provided by AAudioStreamBuilder_openStream()
1015 * @return actual data format
Phil Burk5ed503c2017-02-01 09:38:15 -08001016 */
Phil Burk9dca9822017-05-26 14:27:43 -07001017AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001018
1019/**
1020 * Provide actual sharing mode.
Phil Burk3316d5e2017-02-15 11:23:01 -08001021 * @param stream reference provided by AAudioStreamBuilder_openStream()
1022 * @return actual sharing mode
Phil Burk5ed503c2017-02-01 09:38:15 -08001023 */
Phil Burke2155ef2017-02-24 13:50:29 -08001024AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001025
1026/**
Phil Burke2fbb592017-05-01 15:05:52 -07001027 * Get the performance mode used by the stream.
1028 *
1029 * @param stream reference provided by AAudioStreamBuilder_openStream()
1030 */
1031AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);
1032
1033/**
Phil Burk3316d5e2017-02-15 11:23:01 -08001034 * @param stream reference provided by AAudioStreamBuilder_openStream()
1035 * @return direction
Phil Burk5ed503c2017-02-01 09:38:15 -08001036 */
Phil Burke2155ef2017-02-24 13:50:29 -08001037AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001038
1039/**
1040 * Passes back the number of frames that have been written since the stream was created.
1041 * For an output stream, this will be advanced by the application calling write().
Phil Burk3316d5e2017-02-15 11:23:01 -08001042 * For an input stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -08001043 *
1044 * The frame position is monotonically increasing.
1045 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001046 * @param stream reference provided by AAudioStreamBuilder_openStream()
1047 * @return frames written
Phil Burk5ed503c2017-02-01 09:38:15 -08001048 */
Phil Burke2155ef2017-02-24 13:50:29 -08001049AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001050
1051/**
1052 * Passes back the number of frames that have been read since the stream was created.
Phil Burk3316d5e2017-02-15 11:23:01 -08001053 * For an output stream, this will be advanced by the endpoint.
Phil Burk5ed503c2017-02-01 09:38:15 -08001054 * For an input stream, this will be advanced by the application calling read().
1055 *
1056 * The frame position is monotonically increasing.
1057 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001058 * @param stream reference provided by AAudioStreamBuilder_openStream()
1059 * @return frames read
Phil Burk5ed503c2017-02-01 09:38:15 -08001060 */
Phil Burke2155ef2017-02-24 13:50:29 -08001061AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
Phil Burk5ed503c2017-02-01 09:38:15 -08001062
1063/**
Phil Burk7e7dcaa2018-01-03 15:16:15 -08001064 * Passes back the session ID associated with this stream.
1065 *
1066 * The session ID can be used to associate a stream with effects processors.
1067 * The effects are controlled using the Android AudioEffect Java API.
1068 *
1069 * If AAudioStreamBuilder_setSessionId() was called with AAUDIO_SESSION_ID_ALLOCATE
1070 * then a new session ID should be allocated once when the stream is opened.
1071 *
1072 * If AAudioStreamBuilder_setSessionId() was called with a previously allocated
1073 * session ID then that value should be returned.
1074 *
1075 * If AAudioStreamBuilder_setSessionId() was not called then this function should
1076 * return AAUDIO_SESSION_ID_NONE.
1077 *
1078 * The sessionID for a stream should not change once the stream has been opened.
1079 *
Phil Burk42452c02018-04-10 12:43:33 -07001080 * Added in API level 28.
1081 *
Phil Burk7e7dcaa2018-01-03 15:16:15 -08001082 * @param stream reference provided by AAudioStreamBuilder_openStream()
1083 * @return session ID or AAUDIO_SESSION_ID_NONE
1084 */
1085AAUDIO_API aaudio_session_id_t AAudioStream_getSessionId(AAudioStream* stream);
1086
1087/**
Phil Burk5ed503c2017-02-01 09:38:15 -08001088 * Passes back the time at which a particular frame was presented.
1089 * This can be used to synchronize audio with video or MIDI.
1090 * It can also be used to align a recorded stream with a playback stream.
1091 *
1092 * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
1093 * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
1094 * Note that because requestStart() is asynchronous, timestamps will not be valid until
1095 * a short time after calling requestStart().
1096 * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
1097 * Just try calling again later.
1098 *
1099 * If an error occurs, then the position and time will not be modified.
1100 *
1101 * The position and time passed back are monotonically increasing.
1102 *
Phil Burk3316d5e2017-02-15 11:23:01 -08001103 * @param stream reference provided by AAudioStreamBuilder_openStream()
Phil Burkec159502017-07-25 17:33:47 -07001104 * @param clockid CLOCK_MONOTONIC or CLOCK_BOOTTIME
Phil Burk5ed503c2017-02-01 09:38:15 -08001105 * @param framePosition pointer to a variable to receive the position
1106 * @param timeNanoseconds pointer to a variable to receive the time
1107 * @return AAUDIO_OK or a negative error
1108 */
Phil Burke2155ef2017-02-24 13:50:29 -08001109AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
Phil Burk3316d5e2017-02-15 11:23:01 -08001110 clockid_t clockid,
1111 int64_t *framePosition,
1112 int64_t *timeNanoseconds);
Phil Burk5ed503c2017-02-01 09:38:15 -08001113
Phil Burk361b1422017-12-20 14:24:16 -08001114/**
1115 * Return the use case for the stream.
1116 *
Phil Burk42452c02018-04-10 12:43:33 -07001117 * Added in API level 28.
1118 *
Phil Burk361b1422017-12-20 14:24:16 -08001119 * @param stream reference provided by AAudioStreamBuilder_openStream()
1120 * @return frames read
1121 */
1122AAUDIO_API aaudio_usage_t AAudioStream_getUsage(AAudioStream* stream);
1123
1124/**
1125 * Return the content type for the stream.
1126 *
Phil Burk42452c02018-04-10 12:43:33 -07001127 * Added in API level 28.
1128 *
Phil Burk361b1422017-12-20 14:24:16 -08001129 * @param stream reference provided by AAudioStreamBuilder_openStream()
1130 * @return content type, for example AAUDIO_CONTENT_TYPE_MUSIC
1131 */
1132AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* stream);
1133
1134/**
1135 * Return the input preset for the stream.
1136 *
Phil Burk42452c02018-04-10 12:43:33 -07001137 * Added in API level 28.
1138 *
Phil Burk361b1422017-12-20 14:24:16 -08001139 * @param stream reference provided by AAudioStreamBuilder_openStream()
1140 * @return input preset, for example AAUDIO_INPUT_PRESET_CAMCORDER
1141 */
1142AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream);
1143
Phil Burk5ed503c2017-02-01 09:38:15 -08001144#ifdef __cplusplus
1145}
1146#endif
1147
1148#endif //AAUDIO_AAUDIO_H
Phil Burka45be8b2017-04-05 14:45:48 -07001149
1150/** @} */