• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @addtogroup Audio
19  * @{
20  */
21 
22 /**
23  * @file AAudio.h
24  */
25 
26 /**
27  * This is the 'C' API for AAudio.
28  */
29 #ifndef AAUDIO_AAUDIO_H
30 #define AAUDIO_AAUDIO_H
31 
32 #include <time.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
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
45 
46 enum {
47     AAUDIO_DIRECTION_OUTPUT,
48     AAUDIO_DIRECTION_INPUT
49 };
50 typedef int32_t aaudio_direction_t;
51 
52 enum {
53     AAUDIO_FORMAT_INVALID = -1,
54     AAUDIO_FORMAT_UNSPECIFIED = 0,
55     AAUDIO_FORMAT_PCM_I16,
56     AAUDIO_FORMAT_PCM_FLOAT
57 };
58 typedef int32_t aaudio_format_t;
59 
60 enum {
61     AAUDIO_OK,
62     AAUDIO_ERROR_BASE = -900, // TODO review
63     AAUDIO_ERROR_DISCONNECTED,
64     AAUDIO_ERROR_ILLEGAL_ARGUMENT,
65     // reserved
66     AAUDIO_ERROR_INTERNAL = AAUDIO_ERROR_ILLEGAL_ARGUMENT + 2,
67     AAUDIO_ERROR_INVALID_STATE,
68     // reserved
69     // reserved
70     AAUDIO_ERROR_INVALID_HANDLE = AAUDIO_ERROR_INVALID_STATE + 3,
71     // reserved
72     AAUDIO_ERROR_UNIMPLEMENTED = AAUDIO_ERROR_INVALID_HANDLE + 2,
73     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 };
84 typedef int32_t  aaudio_result_t;
85 
86 enum
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 };
103 typedef int32_t aaudio_stream_state_t;
104 
105 
106 enum {
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 };
119 typedef int32_t aaudio_sharing_mode_t;
120 
121 
122 enum {
123     /**
124      * No particular performance needs. Default.
125      */
126             AAUDIO_PERFORMANCE_MODE_NONE = 10,
127 
128     /**
129      * Extending battery life is most important.
130      */
131             AAUDIO_PERFORMANCE_MODE_POWER_SAVING,
132 
133     /**
134      * Reducing latency is most important.
135      */
136             AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
137 };
138 typedef int32_t aaudio_performance_mode_t;
139 
140 typedef struct AAudioStreamStruct         AAudioStream;
141 typedef struct AAudioStreamBuilderStruct  AAudioStreamBuilder;
142 
143 #ifndef AAUDIO_API
144 #define AAUDIO_API /* export this symbol */
145 #endif
146 
147 // ============================================================
148 // Audio System
149 // ============================================================
150 
151 /**
152  * The text is the ASCII symbol corresponding to the returnCode,
153  * or an English message saying the returnCode is unrecognized.
154  * This is intended for developers to use when debugging.
155  * It is not for display to users.
156  *
157  * @return pointer to a text representation of an AAudio result code.
158  */
159 AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode);
160 
161 /**
162  * The text is the ASCII symbol corresponding to the stream state,
163  * or an English message saying the state is unrecognized.
164  * This is intended for developers to use when debugging.
165  * It is not for display to users.
166  *
167  * @return pointer to a text representation of an AAudio state.
168  */
169 AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state);
170 
171 // ============================================================
172 // StreamBuilder
173 // ============================================================
174 
175 /**
176  * Create a StreamBuilder that can be used to open a Stream.
177  *
178  * The deviceId is initially unspecified, meaning that the current default device will be used.
179  *
180  * The default direction is AAUDIO_DIRECTION_OUTPUT.
181  * The default sharing mode is AAUDIO_SHARING_MODE_SHARED.
182  * The data format, samplesPerFrames and sampleRate are unspecified and will be
183  * chosen by the device when it is opened.
184  *
185  * AAudioStreamBuilder_delete() must be called when you are done using the builder.
186  */
187 AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder);
188 
189 /**
190  * Request an audio device identified device using an ID.
191  * On Android, for example, the ID could be obtained from the Java AudioManager.
192  *
193  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED,
194  * in which case the primary device will be used.
195  *
196  * @param builder reference provided by AAudio_createStreamBuilder()
197  * @param deviceId device identifier or AAUDIO_UNSPECIFIED
198  */
199 AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
200                                                      int32_t deviceId);
201 
202 /**
203  * Request a sample rate in Hertz.
204  *
205  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
206  * An optimal value will then be chosen when the stream is opened.
207  * After opening a stream with an unspecified value, the application must
208  * query for the actual value, which may vary by device.
209  *
210  * If an exact value is specified then an opened stream will use that value.
211  * If a stream cannot be opened with the specified value then the open will fail.
212  *
213  * @param builder reference provided by AAudio_createStreamBuilder()
214  * @param sampleRate frames per second. Common rates include 44100 and 48000 Hz.
215  */
216 AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
217                                                        int32_t sampleRate);
218 
219 /**
220  * Request a number of channels for the stream.
221  *
222  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
223  * An optimal value will then be chosen when the stream is opened.
224  * After opening a stream with an unspecified value, the application must
225  * query for the actual value, which may vary by device.
226  *
227  * If an exact value is specified then an opened stream will use that value.
228  * If a stream cannot be opened with the specified value then the open will fail.
229  *
230  * @param builder reference provided by AAudio_createStreamBuilder()
231  * @param channelCount Number of channels desired.
232  */
233 AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
234                                                    int32_t channelCount);
235 
236 /**
237  * Identical to AAudioStreamBuilder_setChannelCount().
238  *
239  * @param builder reference provided by AAudio_createStreamBuilder()
240  * @param samplesPerFrame Number of samples in a frame.
241  */
242 AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
243                                                        int32_t samplesPerFrame);
244 
245 /**
246  * Request a sample data format, for example AAUDIO_FORMAT_PCM_I16.
247  *
248  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
249  * An optimal value will then be chosen when the stream is opened.
250  * After opening a stream with an unspecified value, the application must
251  * query for the actual value, which may vary by device.
252  *
253  * If an exact value is specified then an opened stream will use that value.
254  * If a stream cannot be opened with the specified value then the open will fail.
255  *
256  * @param builder reference provided by AAudio_createStreamBuilder()
257  * @param format common formats are AAUDIO_FORMAT_PCM_FLOAT and AAUDIO_FORMAT_PCM_I16.
258  */
259 AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
260                                                    aaudio_format_t format);
261 
262 /**
263  * Request a mode for sharing the device.
264  *
265  * The default, if you do not call this function, is AAUDIO_SHARING_MODE_SHARED.
266  *
267  * The requested sharing mode may not be available.
268  * The application can query for the actual mode after the stream is opened.
269  *
270  * @param builder reference provided by AAudio_createStreamBuilder()
271  * @param sharingMode AAUDIO_SHARING_MODE_SHARED or AAUDIO_SHARING_MODE_EXCLUSIVE
272  */
273 AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
274                                                         aaudio_sharing_mode_t sharingMode);
275 
276 /**
277  * Request the direction for a stream.
278  *
279  * The default, if you do not call this function, is AAUDIO_DIRECTION_OUTPUT.
280  *
281  * @param builder reference provided by AAudio_createStreamBuilder()
282  * @param direction AAUDIO_DIRECTION_OUTPUT or AAUDIO_DIRECTION_INPUT
283  */
284 AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
285                                                             aaudio_direction_t direction);
286 
287 /**
288  * Set the requested buffer capacity in frames.
289  * The final AAudioStream capacity may differ, but will probably be at least this big.
290  *
291  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
292  *
293  * @param builder reference provided by AAudio_createStreamBuilder()
294  * @param numFrames the desired buffer capacity in frames or AAUDIO_UNSPECIFIED
295  */
296 AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
297                                                                  int32_t numFrames);
298 
299 /**
300  * Set the requested performance mode.
301  *
302  * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
303  *
304  * @param builder reference provided by AAudio_createStreamBuilder()
305  * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
306  */
307 AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
308                                                 aaudio_performance_mode_t mode);
309 
310 /**
311  * Return one of these values from the data callback function.
312  */
313 enum {
314 
315     /**
316      * Continue calling the callback.
317      */
318     AAUDIO_CALLBACK_RESULT_CONTINUE = 0,
319 
320     /**
321      * Stop calling the callback.
322      *
323      * The application will still need to call AAudioStream_requestPause()
324      * or AAudioStream_requestStop().
325      */
326     AAUDIO_CALLBACK_RESULT_STOP,
327 
328 };
329 typedef int32_t aaudio_data_callback_result_t;
330 
331 /**
332  * Prototype for the data function that is passed to AAudioStreamBuilder_setDataCallback().
333  *
334  * For an output stream, this function should render and write numFrames of data
335  * in the streams current data format to the audioData buffer.
336  *
337  * For an input stream, this function should read and process numFrames of data
338  * from the audioData buffer.
339  *
340  * Note that this callback function should be considered a "real-time" function.
341  * It must not do anything that could cause an unbounded delay because that can cause the
342  * audio to glitch or pop.
343  *
344  * These are things the function should NOT do:
345  * <ul>
346  * <li>allocate memory using, for example, malloc() or new</li>
347  * <li>any file operations such as opening, closing, reading or writing</li>
348  * <li>any network operations such as streaming</li>
349  * <li>use any mutexes or other synchronization primitives</li>
350  * <li>sleep</li>
351  * </ul>
352  *
353  * If you need to move data, eg. MIDI commands, in or out of the callback function then
354  * we recommend the use of non-blocking techniques such as an atomic FIFO.
355  *
356  * @param stream reference provided by AAudioStreamBuilder_openStream()
357  * @param userData the same address that was passed to AAudioStreamBuilder_setCallback()
358  * @param audioData a pointer to the audio data
359  * @param numFrames the number of frames to be processed
360  * @return AAUDIO_CALLBACK_RESULT_*
361  */
362 typedef aaudio_data_callback_result_t (*AAudioStream_dataCallback)(
363         AAudioStream *stream,
364         void *userData,
365         void *audioData,
366         int32_t numFrames);
367 
368 /**
369  * Request that AAudio call this functions when the stream is running.
370  *
371  * Note that when using this callback, the audio data will be passed in or out
372  * of the function as an argument.
373  * So you cannot call AAudioStream_write() or AAudioStream_read() on the same stream
374  * that has an active data callback.
375  *
376  * The callback function will start being called after AAudioStream_requestStart() is called.
377  * It will stop being called after AAudioStream_requestPause() or
378  * AAudioStream_requestStop() is called.
379  *
380  * This callback function will be called on a real-time thread owned by AAudio. See
381  * {@link AAudioStream_dataCallback} for more information.
382  *
383  * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
384  *
385  * @param builder reference provided by AAudio_createStreamBuilder()
386  * @param callback pointer to a function that will process audio data.
387  * @param userData pointer to an application data structure that will be passed
388  *          to the callback functions.
389  */
390 AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
391                                                  AAudioStream_dataCallback callback,
392                                                  void *userData);
393 
394 /**
395  * Set the requested data callback buffer size in frames.
396  * See {@link AAudioStream_dataCallback}.
397  *
398  * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
399  *
400  * For the lowest possible latency, do not call this function. AAudio will then
401  * call the dataProc callback function with whatever size is optimal.
402  * That size may vary from one callback to another.
403  *
404  * Only use this function if the application requires a specific number of frames for processing.
405  * The application might, for example, be using an FFT that requires
406  * a specific power-of-two sized buffer.
407  *
408  * AAudio may need to add additional buffering in order to adapt between the internal
409  * buffer size and the requested buffer size.
410  *
411  * If you do call this function then the requested size should be less than
412  * half the buffer capacity, to allow double buffering.
413  *
414  * @param builder reference provided by AAudio_createStreamBuilder()
415  * @param numFrames the desired buffer size in frames or AAUDIO_UNSPECIFIED
416  */
417 AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
418                                                              int32_t numFrames);
419 
420 /**
421  * Prototype for the callback function that is passed to
422  * AAudioStreamBuilder_setErrorCallback().
423  *
424  * @param stream reference provided by AAudioStreamBuilder_openStream()
425  * @param userData the same address that was passed to AAudioStreamBuilder_setErrorCallback()
426  * @param error an AAUDIO_ERROR_* value.
427  */
428 typedef void (*AAudioStream_errorCallback)(
429         AAudioStream *stream,
430         void *userData,
431         aaudio_result_t error);
432 
433 /**
434  * Request that AAudio call this functions if any error occurs on a callback thread.
435  *
436  * It will be called, for example, if a headset or a USB device is unplugged causing the stream's
437  * device to be unavailable.
438  * In response, this function could signal or launch another thread to reopen a
439  * stream on another device. Do not reopen the stream in this callback.
440  *
441  * This will not be called because of actions by the application, such as stopping
442  * or closing a stream.
443  *
444  * Another possible cause of error would be a timeout or an unanticipated internal error.
445  *
446  * Note that the AAudio callbacks will never be called simultaneously from multiple threads.
447  *
448  * @param builder reference provided by AAudio_createStreamBuilder()
449  * @param callback pointer to a function that will be called if an error occurs.
450  * @param userData pointer to an application data structure that will be passed
451  *          to the callback functions.
452  */
453 AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
454                                                 AAudioStream_errorCallback callback,
455                                                 void *userData);
456 
457 /**
458  * Open a stream based on the options in the StreamBuilder.
459  *
460  * AAudioStream_close must be called when finished with the stream to recover
461  * the memory and to free the associated resources.
462  *
463  * @param builder reference provided by AAudio_createStreamBuilder()
464  * @param stream pointer to a variable to receive the new stream reference
465  * @return AAUDIO_OK or a negative error.
466  */
467 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
468                                                      AAudioStream** stream);
469 
470 /**
471  * Delete the resources associated with the StreamBuilder.
472  *
473  * @param builder reference provided by AAudio_createStreamBuilder()
474  * @return AAUDIO_OK or a negative error.
475  */
476 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_delete(AAudioStreamBuilder* builder);
477 
478 // ============================================================
479 // Stream Control
480 // ============================================================
481 
482 /**
483  * Free the resources associated with a stream created by AAudioStreamBuilder_openStream()
484  *
485  * @param stream reference provided by AAudioStreamBuilder_openStream()
486  * @return AAUDIO_OK or a negative error.
487  */
488 AAUDIO_API aaudio_result_t  AAudioStream_close(AAudioStream* stream);
489 
490 /**
491  * Asynchronously request to start playing the stream. For output streams, one should
492  * write to the stream to fill the buffer before starting.
493  * Otherwise it will underflow.
494  * After this call the state will be in AAUDIO_STREAM_STATE_STARTING or AAUDIO_STREAM_STATE_STARTED.
495  *
496  * @param stream reference provided by AAudioStreamBuilder_openStream()
497  * @return AAUDIO_OK or a negative error.
498  */
499 AAUDIO_API aaudio_result_t  AAudioStream_requestStart(AAudioStream* stream);
500 
501 /**
502  * Asynchronous request for the stream to pause.
503  * Pausing a stream will freeze the data flow but not flush any buffers.
504  * Use AAudioStream_Start() to resume playback after a pause.
505  * After this call the state will be in AAUDIO_STREAM_STATE_PAUSING or AAUDIO_STREAM_STATE_PAUSED.
506  *
507  * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
508  * For input streams use AAudioStream_requestStop().
509  *
510  * @param stream reference provided by AAudioStreamBuilder_openStream()
511  * @return AAUDIO_OK or a negative error.
512  */
513 AAUDIO_API aaudio_result_t  AAudioStream_requestPause(AAudioStream* stream);
514 
515 /**
516  * Asynchronous request for the stream to flush.
517  * Flushing will discard any pending data.
518  * This call only works if the stream is pausing or paused. TODO review
519  * Frame counters are not reset by a flush. They may be advanced.
520  * After this call the state will be in AAUDIO_STREAM_STATE_FLUSHING or AAUDIO_STREAM_STATE_FLUSHED.
521  *
522  * This will return AAUDIO_ERROR_UNIMPLEMENTED for input streams.
523  *
524  * @param stream reference provided by AAudioStreamBuilder_openStream()
525  * @return AAUDIO_OK or a negative error.
526  */
527 AAUDIO_API aaudio_result_t  AAudioStream_requestFlush(AAudioStream* stream);
528 
529 /**
530  * Asynchronous request for the stream to stop.
531  * The stream will stop after all of the data currently buffered has been played.
532  * After this call the state will be in AAUDIO_STREAM_STATE_STOPPING or AAUDIO_STREAM_STATE_STOPPED.
533  *
534  * @param stream reference provided by AAudioStreamBuilder_openStream()
535  * @return AAUDIO_OK or a negative error.
536  */
537 AAUDIO_API aaudio_result_t  AAudioStream_requestStop(AAudioStream* stream);
538 
539 /**
540  * Query the current state of the client, eg. AAUDIO_STREAM_STATE_PAUSING
541  *
542  * This function will immediately return the state without updating the state.
543  * If you want to update the client state based on the server state then
544  * call AAudioStream_waitForStateChange() with currentState
545  * set to AAUDIO_STREAM_STATE_UNKNOWN and a zero timeout.
546  *
547  * @param stream reference provided by AAudioStreamBuilder_openStream()
548  */
549 AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream);
550 
551 /**
552  * Wait until the current state no longer matches the input state.
553  *
554  * This will update the current client state.
555  *
556  * <pre><code>
557  * aaudio_stream_state_t currentState;
558  * aaudio_result_t result = AAudioStream_getState(stream, &currentState);
559  * while (result == AAUDIO_OK && currentState != AAUDIO_STREAM_STATE_PAUSING) {
560  *     result = AAudioStream_waitForStateChange(
561  *                                   stream, currentState, &currentState, MY_TIMEOUT_NANOS);
562  * }
563  * </code></pre>
564  *
565  * @param stream A reference provided by AAudioStreamBuilder_openStream()
566  * @param inputState The state we want to avoid.
567  * @param nextState Pointer to a variable that will be set to the new state.
568  * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
569  * @return AAUDIO_OK or a negative error.
570  */
571 AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
572                                             aaudio_stream_state_t inputState,
573                                             aaudio_stream_state_t *nextState,
574                                             int64_t timeoutNanoseconds);
575 
576 // ============================================================
577 // Stream I/O
578 // ============================================================
579 
580 /**
581  * Read data from the stream.
582  *
583  * The call will wait until the read is complete or until it runs out of time.
584  * If timeoutNanos is zero then this call will not wait.
585  *
586  * Note that timeoutNanoseconds is a relative duration in wall clock time.
587  * Time will not stop if the thread is asleep.
588  * So it will be implemented using CLOCK_BOOTTIME.
589  *
590  * This call is "strong non-blocking" unless it has to wait for data.
591  *
592  * @param stream A stream created using AAudioStreamBuilder_openStream().
593  * @param buffer The address of the first sample.
594  * @param numFrames Number of frames to read. Only complete frames will be written.
595  * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
596  * @return The number of frames actually read or a negative error.
597  */
598 AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
599                                void *buffer,
600                                int32_t numFrames,
601                                int64_t timeoutNanoseconds);
602 
603 /**
604  * Write data to the stream.
605  *
606  * The call will wait until the write is complete or until it runs out of time.
607  * If timeoutNanos is zero then this call will not wait.
608  *
609  * Note that timeoutNanoseconds is a relative duration in wall clock time.
610  * Time will not stop if the thread is asleep.
611  * So it will be implemented using CLOCK_BOOTTIME.
612  *
613  * This call is "strong non-blocking" unless it has to wait for room in the buffer.
614  *
615  * @param stream A stream created using AAudioStreamBuilder_openStream().
616  * @param buffer The address of the first sample.
617  * @param numFrames Number of frames to write. Only complete frames will be written.
618  * @param timeoutNanoseconds Maximum number of nanoseconds to wait for completion.
619  * @return The number of frames actually written or a negative error.
620  */
621 AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
622                                const void *buffer,
623                                int32_t numFrames,
624                                int64_t timeoutNanoseconds);
625 
626 // ============================================================
627 // Stream - queries
628 // ============================================================
629 
630 /**
631  * This can be used to adjust the latency of the buffer by changing
632  * the threshold where blocking will occur.
633  * By combining this with AAudioStream_getXRunCount(), the latency can be tuned
634  * at run-time for each device.
635  *
636  * This cannot be set higher than AAudioStream_getBufferCapacityInFrames().
637  *
638  * Note that you will probably not get the exact size you request.
639  * Call AAudioStream_getBufferSizeInFrames() to see what the actual final size is.
640  *
641  * @param stream reference provided by AAudioStreamBuilder_openStream()
642  * @param numFrames requested number of frames that can be filled without blocking
643  * @return actual buffer size in frames or a negative error
644  */
645 AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
646                                                       int32_t numFrames);
647 
648 /**
649  * Query the maximum number of frames that can be filled without blocking.
650  *
651  * @param stream reference provided by AAudioStreamBuilder_openStream()
652  * @return buffer size in frames.
653  */
654 AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream);
655 
656 /**
657  * Query the number of frames that the application should read or write at
658  * one time for optimal performance. It is OK if an application writes
659  * a different number of frames. But the buffer size may need to be larger
660  * in order to avoid underruns or overruns.
661  *
662  * Note that this may or may not match the actual device burst size.
663  * For some endpoints, the burst size can vary dynamically.
664  * But these tend to be devices with high latency.
665  *
666  * @param stream reference provided by AAudioStreamBuilder_openStream()
667  * @return burst size
668  */
669 AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream);
670 
671 /**
672  * Query maximum buffer capacity in frames.
673  *
674  * @param stream reference provided by AAudioStreamBuilder_openStream()
675  * @return  buffer capacity in frames
676  */
677 AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream);
678 
679 /**
680  * Query the size of the buffer that will be passed to the dataProc callback
681  * in the numFrames parameter.
682  *
683  * This call can be used if the application needs to know the value of numFrames before
684  * the stream is started. This is not normally necessary.
685  *
686  * If a specific size was requested by calling AAudioStreamBuilder_setCallbackSizeInFrames()
687  * then this will be the same size.
688  *
689  * If AAudioStreamBuilder_setCallbackSizeInFrames() was not called then this will
690  * return the size chosen by AAudio, or AAUDIO_UNSPECIFIED.
691  *
692  * AAUDIO_UNSPECIFIED indicates that the callback buffer size for this stream
693  * may vary from one dataProc callback to the next.
694  *
695  * @param stream reference provided by AAudioStreamBuilder_openStream()
696  * @return callback buffer size in frames or AAUDIO_UNSPECIFIED
697  */
698 AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream);
699 
700 /**
701  * An XRun is an Underrun or an Overrun.
702  * During playing, an underrun will occur if the stream is not written in time
703  * and the system runs out of valid data.
704  * During recording, an overrun will occur if the stream is not read in time
705  * and there is no place to put the incoming data so it is discarded.
706  *
707  * An underrun or overrun can cause an audible "pop" or "glitch".
708  *
709  * Note that some INPUT devices may not support this function.
710  * In that case a 0 will always be returned.
711  *
712  * @param stream reference provided by AAudioStreamBuilder_openStream()
713  * @return the underrun or overrun count
714  */
715 AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
716 
717 /**
718  * @param stream reference provided by AAudioStreamBuilder_openStream()
719  * @return actual sample rate
720  */
721 AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
722 
723 /**
724  * A stream has one or more channels of data.
725  * A frame will contain one sample for each channel.
726  *
727  * @param stream reference provided by AAudioStreamBuilder_openStream()
728  * @return actual number of channels
729  */
730 AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);
731 
732 /**
733  * Identical to AAudioStream_getChannelCount().
734  *
735  * @param stream reference provided by AAudioStreamBuilder_openStream()
736  * @return actual number of samples frame
737  */
738 AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream);
739 
740 /**
741  * @param stream reference provided by AAudioStreamBuilder_openStream()
742  * @return actual device ID
743  */
744 AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream);
745 
746 /**
747  * @param stream reference provided by AAudioStreamBuilder_openStream()
748  * @return actual data format
749  */
750 AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream);
751 
752 /**
753  * Provide actual sharing mode.
754  * @param stream reference provided by AAudioStreamBuilder_openStream()
755  * @return  actual sharing mode
756  */
757 AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);
758 
759 /**
760  * Get the performance mode used by the stream.
761  *
762  * @param stream reference provided by AAudioStreamBuilder_openStream()
763  */
764 AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);
765 
766 /**
767  * @param stream reference provided by AAudioStreamBuilder_openStream()
768  * @return direction
769  */
770 AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream);
771 
772 /**
773  * Passes back the number of frames that have been written since the stream was created.
774  * For an output stream, this will be advanced by the application calling write().
775  * For an input stream, this will be advanced by the endpoint.
776  *
777  * The frame position is monotonically increasing.
778  *
779  * @param stream reference provided by AAudioStreamBuilder_openStream()
780  * @return frames written
781  */
782 AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream);
783 
784 /**
785  * Passes back the number of frames that have been read since the stream was created.
786  * For an output stream, this will be advanced by the endpoint.
787  * For an input stream, this will be advanced by the application calling read().
788  *
789  * The frame position is monotonically increasing.
790  *
791  * @param stream reference provided by AAudioStreamBuilder_openStream()
792  * @return frames read
793  */
794 AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream);
795 
796 /**
797  * Passes back the time at which a particular frame was presented.
798  * This can be used to synchronize audio with video or MIDI.
799  * It can also be used to align a recorded stream with a playback stream.
800  *
801  * Timestamps are only valid when the stream is in AAUDIO_STREAM_STATE_STARTED.
802  * AAUDIO_ERROR_INVALID_STATE will be returned if the stream is not started.
803  * Note that because requestStart() is asynchronous, timestamps will not be valid until
804  * a short time after calling requestStart().
805  * So AAUDIO_ERROR_INVALID_STATE should not be considered a fatal error.
806  * Just try calling again later.
807  *
808  * If an error occurs, then the position and time will not be modified.
809  *
810  * The position and time passed back are monotonically increasing.
811  *
812  * @param stream reference provided by AAudioStreamBuilder_openStream()
813  * @param clockid CLOCK_MONOTONIC or CLOCK_BOOTTIME
814  * @param framePosition pointer to a variable to receive the position
815  * @param timeNanoseconds pointer to a variable to receive the time
816  * @return AAUDIO_OK or a negative error
817  */
818 AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
819                                       clockid_t clockid,
820                                       int64_t *framePosition,
821                                       int64_t *timeNanoseconds);
822 
823 #ifdef __cplusplus
824 }
825 #endif
826 
827 #endif //AAUDIO_AAUDIO_H
828 
829 /** @} */
830