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 #define LOG_TAG "AAudio"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <inttypes.h>
22 #include <mutex>
23 #include <time.h>
24 #include <pthread.h>
25 
26 #include <aaudio/AAudio.h>
27 #include <aaudio/AAudioTesting.h>
28 #include "AudioClock.h"
29 #include "AudioGlobal.h"
30 #include "AudioStreamBuilder.h"
31 #include "AudioStream.h"
32 #include "binding/AAudioCommon.h"
33 #include "client/AudioStreamInternal.h"
34 
35 using namespace aaudio;
36 
37 // Macros for common code that includes a return.
38 // TODO Consider using do{}while(0) construct. I tried but it hung AndroidStudio
39 #define CONVERT_BUILDER_HANDLE_OR_RETURN() \
40     convertAAudioBuilderToStreamBuilder(builder);
41 
42 #define COMMON_GET_FROM_BUILDER_OR_RETURN(resultPtr) \
43     CONVERT_BUILDER_HANDLE_OR_RETURN() \
44     if ((resultPtr) == nullptr) { \
45         return AAUDIO_ERROR_NULL; \
46     }
47 
AAudio_convertResultToText(aaudio_result_t returnCode)48 AAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode) {
49     return AudioGlobal_convertResultToText(returnCode);
50 }
51 
AAudio_convertStreamStateToText(aaudio_stream_state_t state)52 AAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state) {
53     return AudioGlobal_convertStreamStateToText(state);
54 }
55 
convertAAudioStreamToAudioStream(AAudioStream * stream)56 static AudioStream *convertAAudioStreamToAudioStream(AAudioStream* stream)
57 {
58     return (AudioStream*) stream;
59 }
60 
convertAAudioBuilderToStreamBuilder(AAudioStreamBuilder * builder)61 static AudioStreamBuilder *convertAAudioBuilderToStreamBuilder(AAudioStreamBuilder* builder)
62 {
63     return (AudioStreamBuilder*) builder;
64 }
65 
AAudio_createStreamBuilder(AAudioStreamBuilder ** builder)66 AAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder)
67 {
68     AudioStreamBuilder *audioStreamBuilder =  new(std::nothrow) AudioStreamBuilder();
69     if (audioStreamBuilder == nullptr) {
70         return AAUDIO_ERROR_NO_MEMORY;
71     }
72     *builder = (AAudioStreamBuilder*) audioStreamBuilder;
73     return AAUDIO_OK;
74 }
75 
AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder * builder,aaudio_performance_mode_t mode)76 AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
77                                                        aaudio_performance_mode_t mode)
78 {
79     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
80     streamBuilder->setPerformanceMode(mode);
81 }
82 
AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder * builder,int32_t deviceId)83 AAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
84                                                 int32_t deviceId)
85 {
86     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
87     streamBuilder->setDeviceId(deviceId);
88 }
89 
AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder * builder,int32_t sampleRate)90 AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
91                                               int32_t sampleRate)
92 {
93     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
94     streamBuilder->setSampleRate(sampleRate);
95 }
96 
AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder * builder,int32_t channelCount)97 AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
98                                                     int32_t channelCount)
99 {
100     AAudioStreamBuilder_setSamplesPerFrame(builder, channelCount);
101 }
102 
AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder * builder,int32_t samplesPerFrame)103 AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
104                                                        int32_t samplesPerFrame)
105 {
106     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
107     streamBuilder->setSamplesPerFrame(samplesPerFrame);
108 }
109 
AAudioStreamBuilder_setDirection(AAudioStreamBuilder * builder,aaudio_direction_t direction)110 AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
111                                              aaudio_direction_t direction)
112 {
113     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
114     streamBuilder->setDirection(direction);
115 }
116 
AAudioStreamBuilder_setFormat(AAudioStreamBuilder * builder,aaudio_format_t format)117 AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
118                                                    aaudio_format_t format)
119 {
120     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
121     // Use audio_format_t everywhere internally.
122     const audio_format_t internalFormat = AAudioConvert_aaudioToAndroidDataFormat(format);
123     streamBuilder->setFormat(internalFormat);
124 }
125 
AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder * builder,aaudio_sharing_mode_t sharingMode)126 AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
127                                                         aaudio_sharing_mode_t sharingMode)
128 {
129     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
130     streamBuilder->setSharingMode(sharingMode);
131 }
132 
AAudioStreamBuilder_setUsage(AAudioStreamBuilder * builder,aaudio_usage_t usage)133 AAUDIO_API void AAudioStreamBuilder_setUsage(AAudioStreamBuilder* builder,
134                                              aaudio_usage_t usage) {
135     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
136     streamBuilder->setUsage(usage);
137 }
138 
AAudioStreamBuilder_setContentType(AAudioStreamBuilder * builder,aaudio_content_type_t contentType)139 AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
140                                                    aaudio_content_type_t contentType) {
141     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
142     streamBuilder->setContentType(contentType);
143 }
144 
AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder * builder,aaudio_input_preset_t inputPreset)145 AAUDIO_API void AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder* builder,
146                                                    aaudio_input_preset_t inputPreset) {
147     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
148     streamBuilder->setInputPreset(inputPreset);
149 }
150 
AAudioStreamBuilder_setPrivacySensitive(AAudioStreamBuilder * builder,bool privacySensitive)151 AAUDIO_API void AAudioStreamBuilder_setPrivacySensitive(AAudioStreamBuilder* builder,
152                                                    bool privacySensitive) {
153     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
154     streamBuilder->setPrivacySensitiveRequest(privacySensitive);
155 }
156 
AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder * builder,int32_t frames)157 AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
158                                                               int32_t frames)
159 {
160     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
161     streamBuilder->setBufferCapacity(frames);
162 }
163 
AAudioStreamBuilder_setAllowedCapturePolicy(AAudioStreamBuilder * builder,aaudio_allowed_capture_policy_t policy)164 AAUDIO_API void AAudioStreamBuilder_setAllowedCapturePolicy(
165         AAudioStreamBuilder* builder, aaudio_allowed_capture_policy_t policy) {
166     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
167     streamBuilder->setAllowedCapturePolicy(policy);
168 }
169 
AAudioStreamBuilder_setSessionId(AAudioStreamBuilder * builder,aaudio_session_id_t sessionId)170 AAUDIO_API void AAudioStreamBuilder_setSessionId(AAudioStreamBuilder* builder,
171                                                  aaudio_session_id_t sessionId)
172 {
173     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
174     streamBuilder->setSessionId(sessionId);
175 }
176 
AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder * builder,AAudioStream_dataCallback callback,void * userData)177 AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
178                                                     AAudioStream_dataCallback callback,
179                                                     void *userData)
180 {
181     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
182     streamBuilder->setDataCallbackProc(callback);
183     streamBuilder->setDataCallbackUserData(userData);
184 }
185 
AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder * builder,AAudioStream_errorCallback callback,void * userData)186 AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
187                                                  AAudioStream_errorCallback callback,
188                                                  void *userData)
189 {
190     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
191     streamBuilder->setErrorCallbackProc(callback);
192     streamBuilder->setErrorCallbackUserData(userData);
193 }
194 
AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder * builder,int32_t frames)195 AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
196                                                 int32_t frames)
197 {
198     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
199     streamBuilder->setFramesPerDataCallback(frames);
200 }
201 
AAudioStreamBuilder_openStream(AAudioStreamBuilder * builder,AAudioStream ** streamPtr)202 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
203                                                      AAudioStream** streamPtr)
204 {
205     AudioStream *audioStream = nullptr;
206     aaudio_stream_id_t id = 0;
207     // Please leave these logs because they are very helpful when debugging.
208     ALOGI("%s() called ----------------------------------------", __func__);
209     AudioStreamBuilder *streamBuilder = COMMON_GET_FROM_BUILDER_OR_RETURN(streamPtr);
210     aaudio_result_t result = streamBuilder->build(&audioStream);
211     if (result == AAUDIO_OK) {
212         audioStream->registerPlayerBase();
213         *streamPtr = (AAudioStream*) audioStream;
214         id = audioStream->getId();
215     } else {
216         *streamPtr = nullptr;
217     }
218     ALOGI("%s() returns %d = %s for s#%u ----------------",
219         __func__, result, AAudio_convertResultToText(result), id);
220     return result;
221 }
222 
AAudioStreamBuilder_delete(AAudioStreamBuilder * builder)223 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_delete(AAudioStreamBuilder* builder)
224 {
225     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
226     if (streamBuilder != nullptr) {
227         delete streamBuilder;
228         return AAUDIO_OK;
229     }
230     return AAUDIO_ERROR_NULL;
231 }
232 
AAudioStream_release(AAudioStream * stream)233 AAUDIO_API aaudio_result_t  AAudioStream_release(AAudioStream* stream) {
234     aaudio_result_t result = AAUDIO_ERROR_NULL;
235     AudioStream* audioStream = convertAAudioStreamToAudioStream(stream);
236     if (audioStream != nullptr) {
237         aaudio_stream_id_t id = audioStream->getId();
238         ALOGD("%s(s#%u) called ---------------", __func__, id);
239         result = audioStream->safeRelease();
240         // safeRelease() will only fail if called illegally, for example, from a callback.
241         // That would result in the release of an active stream, which would cause a crash.
242         if (result != AAUDIO_OK) {
243             ALOGW("%s(s#%u) failed. Release it from another thread.",
244                   __func__, id);
245         }
246         ALOGD("%s(s#%u) returned %d %s ---------", __func__,
247                 id, result, AAudio_convertResultToText(result));
248     }
249     return result;
250 }
251 
AAudioStream_close(AAudioStream * stream)252 AAUDIO_API aaudio_result_t  AAudioStream_close(AAudioStream* stream) {
253     aaudio_result_t result = AAUDIO_ERROR_NULL;
254     AudioStream* audioStream = convertAAudioStreamToAudioStream(stream);
255     if (audioStream != nullptr) {
256         aaudio_stream_id_t id = audioStream->getId();
257         ALOGD("%s(s#%u) called ---------------", __func__, id);
258         result = audioStream->safeRelease();
259         // safeRelease will only fail if called illegally, for example, from a callback.
260         // That would result in deleting an active stream, which would cause a crash.
261         if (result != AAUDIO_OK) {
262             ALOGW("%s(s#%u) failed. Close it from another thread.",
263                   __func__, id);
264         } else {
265             audioStream->unregisterPlayerBase();
266              // Mark CLOSED to keep destructors from asserting.
267             audioStream->closeFinal();
268             delete audioStream;
269         }
270         ALOGD("%s(s#%u) returned %d ---------", __func__, id, result);
271     }
272     return result;
273 }
274 
AAudioStream_requestStart(AAudioStream * stream)275 AAUDIO_API aaudio_result_t  AAudioStream_requestStart(AAudioStream* stream)
276 {
277     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
278     aaudio_stream_id_t id = audioStream->getId();
279     ALOGD("%s(s#%u) called --------------", __func__, id);
280     aaudio_result_t result = audioStream->systemStart();
281     ALOGD("%s(s#%u) returned %d ---------", __func__, id, result);
282     return result;
283 }
284 
AAudioStream_requestPause(AAudioStream * stream)285 AAUDIO_API aaudio_result_t  AAudioStream_requestPause(AAudioStream* stream)
286 {
287     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
288     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
289     return audioStream->systemPause();
290 }
291 
AAudioStream_requestFlush(AAudioStream * stream)292 AAUDIO_API aaudio_result_t  AAudioStream_requestFlush(AAudioStream* stream)
293 {
294     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
295     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
296     return audioStream->safeFlush();
297 }
298 
AAudioStream_requestStop(AAudioStream * stream)299 AAUDIO_API aaudio_result_t  AAudioStream_requestStop(AAudioStream* stream)
300 {
301     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
302     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
303     return audioStream->systemStopFromApp();
304 }
305 
AAudioStream_waitForStateChange(AAudioStream * stream,aaudio_stream_state_t inputState,aaudio_stream_state_t * nextState,int64_t timeoutNanoseconds)306 AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
307                                             aaudio_stream_state_t inputState,
308                                             aaudio_stream_state_t *nextState,
309                                             int64_t timeoutNanoseconds)
310 {
311 
312     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
313     return audioStream->waitForStateChange(inputState, nextState, timeoutNanoseconds);
314 }
315 
316 // ============================================================
317 // Stream - non-blocking I/O
318 // ============================================================
319 
AAudioStream_read(AAudioStream * stream,void * buffer,int32_t numFrames,int64_t timeoutNanoseconds)320 AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
321                                void *buffer,
322                                int32_t numFrames,
323                                int64_t timeoutNanoseconds)
324 {
325     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
326     if (buffer == nullptr) {
327         return AAUDIO_ERROR_NULL;
328     }
329     if (numFrames < 0) {
330         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
331     } else if (numFrames == 0) {
332         return 0;
333     }
334 
335     aaudio_result_t result = audioStream->read(buffer, numFrames, timeoutNanoseconds);
336 
337     return result;
338 }
339 
AAudioStream_write(AAudioStream * stream,const void * buffer,int32_t numFrames,int64_t timeoutNanoseconds)340 AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
341                                const void *buffer,
342                                int32_t numFrames,
343                                int64_t timeoutNanoseconds)
344 {
345     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
346     if (buffer == nullptr) {
347         return AAUDIO_ERROR_NULL;
348     }
349 
350     // Don't allow writes when playing with a callback.
351     if (audioStream->isDataCallbackActive()) {
352         ALOGD("Cannot write to a callback stream when running.");
353         return AAUDIO_ERROR_INVALID_STATE;
354     }
355 
356     if (numFrames < 0) {
357         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
358     } else if (numFrames == 0) {
359         return 0;
360     }
361 
362     aaudio_result_t result = audioStream->write(buffer, numFrames, timeoutNanoseconds);
363 
364     return result;
365 }
366 
367 // ============================================================
368 // Stream - queries
369 // ============================================================
370 
AAudioStream_getSampleRate(AAudioStream * stream)371 AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream)
372 {
373     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
374     return audioStream->getSampleRate();
375 }
376 
AAudioStream_getChannelCount(AAudioStream * stream)377 AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
378 {
379     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
380     return audioStream->getSamplesPerFrame();
381 }
382 
AAudioStream_getSamplesPerFrame(AAudioStream * stream)383 AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
384 {
385     return AAudioStream_getChannelCount(stream);
386 }
387 
AAudioStream_getState(AAudioStream * stream)388 AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream)
389 {
390     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
391     return audioStream->getState();
392 }
393 
AAudioStream_getFormat(AAudioStream * stream)394 AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream)
395 {
396     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
397     // Use audio_format_t internally.
398     audio_format_t internalFormat = audioStream->getFormat();
399     return AAudioConvert_androidToAAudioDataFormat(internalFormat);
400 }
401 
AAudioStream_setBufferSizeInFrames(AAudioStream * stream,int32_t requestedFrames)402 AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
403                                                 int32_t requestedFrames)
404 {
405     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
406     return audioStream->setBufferSize(requestedFrames);
407 }
408 
AAudioStream_getBufferSizeInFrames(AAudioStream * stream)409 AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream)
410 {
411     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
412     return audioStream->getBufferSize();
413 }
414 
AAudioStream_getDirection(AAudioStream * stream)415 AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream)
416 {
417     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
418     return audioStream->getDirection();
419 }
420 
AAudioStream_getFramesPerBurst(AAudioStream * stream)421 AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream)
422 {
423     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
424     return audioStream->getFramesPerBurst();
425 }
426 
AAudioStream_getFramesPerDataCallback(AAudioStream * stream)427 AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream)
428 {
429     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
430     return audioStream->getFramesPerDataCallback();
431 }
432 
AAudioStream_getBufferCapacityInFrames(AAudioStream * stream)433 AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream)
434 {
435     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
436     return audioStream->getBufferCapacity();
437 }
438 
AAudioStream_getXRunCount(AAudioStream * stream)439 AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream)
440 {
441     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
442     return audioStream->getXRunCount();
443 }
444 
AAudioStream_getPerformanceMode(AAudioStream * stream)445 AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream)
446 {
447     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
448     return audioStream->getPerformanceMode();
449 }
450 
AAudioStream_getDeviceId(AAudioStream * stream)451 AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream)
452 {
453     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
454     return audioStream->getDeviceId();
455 }
456 
AAudioStream_getSharingMode(AAudioStream * stream)457 AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream)
458 {
459     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
460     return audioStream->getSharingMode();
461 }
462 
AAudioStream_getUsage(AAudioStream * stream)463 AAUDIO_API aaudio_usage_t AAudioStream_getUsage(AAudioStream* stream)
464 {
465     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
466     return audioStream->getUsage();
467 }
468 
AAudioStream_getContentType(AAudioStream * stream)469 AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* stream)
470 {
471     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
472     return audioStream->getContentType();
473 }
474 
AAudioStream_getInputPreset(AAudioStream * stream)475 AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream)
476 {
477     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
478     return audioStream->getInputPreset();
479 }
480 
AAudioStream_getAllowedCapturePolicy(AAudioStream * stream)481 AAUDIO_API aaudio_allowed_capture_policy_t AAudioStream_getAllowedCapturePolicy(
482         AAudioStream* stream)
483 {
484     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
485     return audioStream->getAllowedCapturePolicy();
486 }
487 
AAudioStream_getSessionId(AAudioStream * stream)488 AAUDIO_API int32_t AAudioStream_getSessionId(AAudioStream* stream)
489 {
490     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
491     return audioStream->getSessionId();
492 }
493 
AAudioStream_getFramesWritten(AAudioStream * stream)494 AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream)
495 {
496     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
497     return audioStream->getFramesWritten();
498 }
499 
AAudioStream_getFramesRead(AAudioStream * stream)500 AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream)
501 {
502     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
503     return audioStream->getFramesRead();
504 }
505 
AAudioStream_getTimestamp(AAudioStream * stream,clockid_t clockid,int64_t * framePosition,int64_t * timeNanoseconds)506 AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
507                                       clockid_t clockid,
508                                       int64_t *framePosition,
509                                       int64_t *timeNanoseconds)
510 {
511     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
512     if (framePosition == nullptr) {
513         return AAUDIO_ERROR_NULL;
514     } else if (timeNanoseconds == nullptr) {
515         return AAUDIO_ERROR_NULL;
516     } else if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_BOOTTIME) {
517         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
518     }
519 
520     return audioStream->getTimestamp(clockid, framePosition, timeNanoseconds);
521 }
522 
AAudio_getMMapPolicy()523 AAUDIO_API aaudio_policy_t AAudio_getMMapPolicy() {
524     return AudioGlobal_getMMapPolicy();
525 }
526 
AAudio_setMMapPolicy(aaudio_policy_t policy)527 AAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy) {
528     return AudioGlobal_setMMapPolicy(policy);
529 }
530 
AAudioStream_isMMapUsed(AAudioStream * stream)531 AAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream)
532 {
533     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
534     return audioStream->isMMap();
535 }
536 
AAudioStream_isPrivacySensitive(AAudioStream * stream)537 AAUDIO_API bool AAudioStream_isPrivacySensitive(AAudioStream* stream)
538 {
539     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
540     return audioStream->isPrivacySensitive();
541 }
542