• 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 #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_setPackageName(AAudioStreamBuilder * builder,const char * packageName)90 AAUDIO_API void AAudioStreamBuilder_setPackageName(AAudioStreamBuilder* builder,
91                                                    const char* packageName)
92 {
93     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
94     std::optional<std::string> optionalPackageName;
95     if (packageName != nullptr) {
96       optionalPackageName = std::string(packageName);
97     }
98     // Only system apps can read the op package name. For regular apps the
99     // regular package name is a sufficient replacement
100     streamBuilder->setOpPackageName(optionalPackageName);
101 }
102 
AAudioStreamBuilder_setAttributionTag(AAudioStreamBuilder * builder,const char * attributionTag)103 AAUDIO_API void AAudioStreamBuilder_setAttributionTag(AAudioStreamBuilder* builder,
104                                                       const char* attributionTag)
105 {
106     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
107     std::optional<std::string> optionalAttrTag;
108     if (attributionTag != nullptr) {
109       optionalAttrTag = std::string(attributionTag);
110     }
111     streamBuilder->setAttributionTag(optionalAttrTag);
112 }
113 
AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder * builder,int32_t sampleRate)114 AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
115                                               int32_t sampleRate)
116 {
117     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
118     streamBuilder->setSampleRate(sampleRate);
119 }
120 
AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder * builder,int32_t channelCount)121 AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
122                                                     int32_t channelCount)
123 {
124     AAudioStreamBuilder_setSamplesPerFrame(builder, channelCount);
125 }
126 
AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder * builder,int32_t samplesPerFrame)127 AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
128                                                        int32_t samplesPerFrame)
129 {
130     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
131     streamBuilder->setSamplesPerFrame(samplesPerFrame);
132 }
133 
AAudioStreamBuilder_setDirection(AAudioStreamBuilder * builder,aaudio_direction_t direction)134 AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
135                                              aaudio_direction_t direction)
136 {
137     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
138     streamBuilder->setDirection(direction);
139 }
140 
AAudioStreamBuilder_setFormat(AAudioStreamBuilder * builder,aaudio_format_t format)141 AAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
142                                                    aaudio_format_t format)
143 {
144     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
145     // Use audio_format_t everywhere internally.
146     const audio_format_t internalFormat = AAudioConvert_aaudioToAndroidDataFormat(format);
147     streamBuilder->setFormat(internalFormat);
148 }
149 
AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder * builder,aaudio_sharing_mode_t sharingMode)150 AAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
151                                                         aaudio_sharing_mode_t sharingMode)
152 {
153     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
154     streamBuilder->setSharingMode(sharingMode);
155 }
156 
AAudioStreamBuilder_setUsage(AAudioStreamBuilder * builder,aaudio_usage_t usage)157 AAUDIO_API void AAudioStreamBuilder_setUsage(AAudioStreamBuilder* builder,
158                                              aaudio_usage_t usage) {
159     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
160     streamBuilder->setUsage(usage);
161 }
162 
AAudioStreamBuilder_setContentType(AAudioStreamBuilder * builder,aaudio_content_type_t contentType)163 AAUDIO_API void AAudioStreamBuilder_setContentType(AAudioStreamBuilder* builder,
164                                                    aaudio_content_type_t contentType) {
165     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
166     streamBuilder->setContentType(contentType);
167 }
168 
AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder * builder,aaudio_input_preset_t inputPreset)169 AAUDIO_API void AAudioStreamBuilder_setInputPreset(AAudioStreamBuilder* builder,
170                                                    aaudio_input_preset_t inputPreset) {
171     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
172     streamBuilder->setInputPreset(inputPreset);
173 }
174 
AAudioStreamBuilder_setPrivacySensitive(AAudioStreamBuilder * builder,bool privacySensitive)175 AAUDIO_API void AAudioStreamBuilder_setPrivacySensitive(AAudioStreamBuilder* builder,
176                                                    bool privacySensitive) {
177     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
178     streamBuilder->setPrivacySensitiveRequest(privacySensitive);
179 }
180 
AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder * builder,int32_t frames)181 AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
182                                                               int32_t frames)
183 {
184     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
185     streamBuilder->setBufferCapacity(frames);
186 }
187 
AAudioStreamBuilder_setAllowedCapturePolicy(AAudioStreamBuilder * builder,aaudio_allowed_capture_policy_t policy)188 AAUDIO_API void AAudioStreamBuilder_setAllowedCapturePolicy(
189         AAudioStreamBuilder* builder, aaudio_allowed_capture_policy_t policy) {
190     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
191     streamBuilder->setAllowedCapturePolicy(policy);
192 }
193 
AAudioStreamBuilder_setSessionId(AAudioStreamBuilder * builder,aaudio_session_id_t sessionId)194 AAUDIO_API void AAudioStreamBuilder_setSessionId(AAudioStreamBuilder* builder,
195                                                  aaudio_session_id_t sessionId)
196 {
197     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
198     streamBuilder->setSessionId(sessionId);
199 }
200 
AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder * builder,AAudioStream_dataCallback callback,void * userData)201 AAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
202                                                     AAudioStream_dataCallback callback,
203                                                     void *userData)
204 {
205     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
206     streamBuilder->setDataCallbackProc(callback);
207     streamBuilder->setDataCallbackUserData(userData);
208 }
209 
AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder * builder,AAudioStream_errorCallback callback,void * userData)210 AAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
211                                                  AAudioStream_errorCallback callback,
212                                                  void *userData)
213 {
214     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
215     streamBuilder->setErrorCallbackProc(callback);
216     streamBuilder->setErrorCallbackUserData(userData);
217 }
218 
AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder * builder,int32_t frames)219 AAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
220                                                 int32_t frames)
221 {
222     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
223     streamBuilder->setFramesPerDataCallback(frames);
224 }
225 
AAudioStreamBuilder_openStream(AAudioStreamBuilder * builder,AAudioStream ** streamPtr)226 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
227                                                      AAudioStream** streamPtr)
228 {
229     AudioStream *audioStream = nullptr;
230     aaudio_stream_id_t id = 0;
231     // Please leave these logs because they are very helpful when debugging.
232     ALOGI("%s() called ----------------------------------------", __func__);
233     AudioStreamBuilder *streamBuilder = COMMON_GET_FROM_BUILDER_OR_RETURN(streamPtr);
234     aaudio_result_t result = streamBuilder->build(&audioStream);
235     if (result == AAUDIO_OK) {
236         *streamPtr = (AAudioStream*) audioStream;
237         id = audioStream->getId();
238     } else {
239         *streamPtr = nullptr;
240     }
241     ALOGI("%s() returns %d = %s for s#%u ----------------",
242         __func__, result, AAudio_convertResultToText(result), id);
243     return result;
244 }
245 
AAudioStreamBuilder_delete(AAudioStreamBuilder * builder)246 AAUDIO_API aaudio_result_t  AAudioStreamBuilder_delete(AAudioStreamBuilder* builder)
247 {
248     AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
249     if (streamBuilder != nullptr) {
250         delete streamBuilder;
251         return AAUDIO_OK;
252     }
253     return AAUDIO_ERROR_NULL;
254 }
255 
AAudioStream_release(AAudioStream * stream)256 AAUDIO_API aaudio_result_t  AAudioStream_release(AAudioStream* stream) {
257     aaudio_result_t result = AAUDIO_ERROR_NULL;
258     AudioStream* audioStream = convertAAudioStreamToAudioStream(stream);
259     if (audioStream != nullptr) {
260         aaudio_stream_id_t id = audioStream->getId();
261         ALOGD("%s(s#%u) called ---------------", __func__, id);
262         result = audioStream->safeRelease();
263         // safeRelease() will only fail if called illegally, for example, from a callback.
264         // That would result in the release of an active stream, which would cause a crash.
265         if (result != AAUDIO_OK) {
266             ALOGW("%s(s#%u) failed. Release it from another thread.",
267                   __func__, id);
268         }
269         ALOGD("%s(s#%u) returned %d %s ---------", __func__,
270                 id, result, AAudio_convertResultToText(result));
271     }
272     return result;
273 }
274 
AAudioStream_close(AAudioStream * stream)275 AAUDIO_API aaudio_result_t  AAudioStream_close(AAudioStream* stream) {
276     aaudio_result_t result = AAUDIO_ERROR_NULL;
277     AudioStream* audioStream = convertAAudioStreamToAudioStream(stream);
278     if (audioStream != nullptr) {
279         aaudio_stream_id_t id = audioStream->getId();
280         ALOGD("%s(s#%u) called ---------------", __func__, id);
281         result = audioStream->safeReleaseClose();
282         // safeReleaseClose will only fail if called illegally, for example, from a callback.
283         // That would result in deleting an active stream, which would cause a crash.
284         if (result != AAUDIO_OK) {
285             ALOGW("%s(s#%u) failed. Close it from another thread.",
286                   __func__, id);
287         } else {
288             audioStream->unregisterPlayerBase();
289             // Allow the stream to be deleted.
290             AudioStreamBuilder::stopUsingStream(audioStream);
291         }
292         ALOGD("%s(s#%u) returned %d ---------", __func__, id, result);
293     }
294     return result;
295 }
296 
AAudioStream_requestStart(AAudioStream * stream)297 AAUDIO_API aaudio_result_t  AAudioStream_requestStart(AAudioStream* stream)
298 {
299     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
300     aaudio_stream_id_t id = audioStream->getId();
301     ALOGD("%s(s#%u) called --------------", __func__, id);
302     aaudio_result_t result = audioStream->systemStart();
303     ALOGD("%s(s#%u) returned %d ---------", __func__, id, result);
304     return result;
305 }
306 
AAudioStream_requestPause(AAudioStream * stream)307 AAUDIO_API aaudio_result_t  AAudioStream_requestPause(AAudioStream* stream)
308 {
309     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
310     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
311     return audioStream->systemPause();
312 }
313 
AAudioStream_requestFlush(AAudioStream * stream)314 AAUDIO_API aaudio_result_t  AAudioStream_requestFlush(AAudioStream* stream)
315 {
316     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
317     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
318     return audioStream->safeFlush();
319 }
320 
AAudioStream_requestStop(AAudioStream * stream)321 AAUDIO_API aaudio_result_t  AAudioStream_requestStop(AAudioStream* stream)
322 {
323     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
324     ALOGD("%s(s#%u) called", __func__, audioStream->getId());
325     return audioStream->systemStopFromApp();
326 }
327 
AAudioStream_waitForStateChange(AAudioStream * stream,aaudio_stream_state_t inputState,aaudio_stream_state_t * nextState,int64_t timeoutNanoseconds)328 AAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
329                                             aaudio_stream_state_t inputState,
330                                             aaudio_stream_state_t *nextState,
331                                             int64_t timeoutNanoseconds)
332 {
333 
334     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
335     return audioStream->waitForStateChange(inputState, nextState, timeoutNanoseconds);
336 }
337 
338 // ============================================================
339 // Stream - non-blocking I/O
340 // ============================================================
341 
AAudioStream_read(AAudioStream * stream,void * buffer,int32_t numFrames,int64_t timeoutNanoseconds)342 AAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
343                                void *buffer,
344                                int32_t numFrames,
345                                int64_t timeoutNanoseconds)
346 {
347     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
348     if (buffer == nullptr) {
349         return AAUDIO_ERROR_NULL;
350     }
351     if (numFrames < 0) {
352         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
353     } else if (numFrames == 0) {
354         return 0;
355     }
356 
357     aaudio_result_t result = audioStream->read(buffer, numFrames, timeoutNanoseconds);
358 
359     return result;
360 }
361 
AAudioStream_write(AAudioStream * stream,const void * buffer,int32_t numFrames,int64_t timeoutNanoseconds)362 AAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
363                                const void *buffer,
364                                int32_t numFrames,
365                                int64_t timeoutNanoseconds)
366 {
367     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
368     if (buffer == nullptr) {
369         return AAUDIO_ERROR_NULL;
370     }
371 
372     // Don't allow writes when playing with a callback.
373     if (audioStream->isDataCallbackActive()) {
374         // A developer requested this warning because it would have saved lots of debugging.
375         ALOGW("%s() - Cannot write to a callback stream when running.", __func__);
376         return AAUDIO_ERROR_INVALID_STATE;
377     }
378 
379     if (numFrames < 0) {
380         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
381     } else if (numFrames == 0) {
382         return 0;
383     }
384 
385     aaudio_result_t result = audioStream->write(buffer, numFrames, timeoutNanoseconds);
386 
387     return result;
388 }
389 
390 // ============================================================
391 // Stream - queries
392 // ============================================================
393 
AAudioStream_getSampleRate(AAudioStream * stream)394 AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream)
395 {
396     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
397     return audioStream->getSampleRate();
398 }
399 
AAudioStream_getChannelCount(AAudioStream * stream)400 AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
401 {
402     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
403     return audioStream->getSamplesPerFrame();
404 }
405 
AAudioStream_getSamplesPerFrame(AAudioStream * stream)406 AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
407 {
408     return AAudioStream_getChannelCount(stream);
409 }
410 
AAudioStream_getState(AAudioStream * stream)411 AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream)
412 {
413     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
414     return audioStream->getState();
415 }
416 
AAudioStream_getFormat(AAudioStream * stream)417 AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream)
418 {
419     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
420     // Use audio_format_t internally.
421     audio_format_t internalFormat = audioStream->getFormat();
422     return AAudioConvert_androidToAAudioDataFormat(internalFormat);
423 }
424 
AAudioStream_setBufferSizeInFrames(AAudioStream * stream,int32_t requestedFrames)425 AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
426                                                 int32_t requestedFrames)
427 {
428     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
429     return audioStream->setBufferSize(requestedFrames);
430 }
431 
AAudioStream_getBufferSizeInFrames(AAudioStream * stream)432 AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream)
433 {
434     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
435     return audioStream->getBufferSize();
436 }
437 
AAudioStream_getDirection(AAudioStream * stream)438 AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream)
439 {
440     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
441     return audioStream->getDirection();
442 }
443 
AAudioStream_getFramesPerBurst(AAudioStream * stream)444 AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream)
445 {
446     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
447     return audioStream->getFramesPerBurst();
448 }
449 
AAudioStream_getFramesPerDataCallback(AAudioStream * stream)450 AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream)
451 {
452     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
453     return audioStream->getFramesPerDataCallback();
454 }
455 
AAudioStream_getBufferCapacityInFrames(AAudioStream * stream)456 AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream)
457 {
458     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
459     return audioStream->getBufferCapacity();
460 }
461 
AAudioStream_getXRunCount(AAudioStream * stream)462 AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream)
463 {
464     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
465     return audioStream->getXRunCount();
466 }
467 
AAudioStream_getPerformanceMode(AAudioStream * stream)468 AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream)
469 {
470     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
471     return audioStream->getPerformanceMode();
472 }
473 
AAudioStream_getDeviceId(AAudioStream * stream)474 AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream)
475 {
476     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
477     return audioStream->getDeviceId();
478 }
479 
AAudioStream_getSharingMode(AAudioStream * stream)480 AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream)
481 {
482     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
483     return audioStream->getSharingMode();
484 }
485 
AAudioStream_getUsage(AAudioStream * stream)486 AAUDIO_API aaudio_usage_t AAudioStream_getUsage(AAudioStream* stream)
487 {
488     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
489     return audioStream->getUsage();
490 }
491 
AAudioStream_getContentType(AAudioStream * stream)492 AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* stream)
493 {
494     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
495     return audioStream->getContentType();
496 }
497 
AAudioStream_getInputPreset(AAudioStream * stream)498 AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream)
499 {
500     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
501     return audioStream->getInputPreset();
502 }
503 
AAudioStream_getAllowedCapturePolicy(AAudioStream * stream)504 AAUDIO_API aaudio_allowed_capture_policy_t AAudioStream_getAllowedCapturePolicy(
505         AAudioStream* stream)
506 {
507     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
508     return audioStream->getAllowedCapturePolicy();
509 }
510 
AAudioStream_getSessionId(AAudioStream * stream)511 AAUDIO_API int32_t AAudioStream_getSessionId(AAudioStream* stream)
512 {
513     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
514     return audioStream->getSessionId();
515 }
516 
AAudioStream_getFramesWritten(AAudioStream * stream)517 AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream)
518 {
519     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
520     return audioStream->getFramesWritten();
521 }
522 
AAudioStream_getFramesRead(AAudioStream * stream)523 AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream)
524 {
525     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
526     return audioStream->getFramesRead();
527 }
528 
AAudioStream_getTimestamp(AAudioStream * stream,clockid_t clockid,int64_t * framePosition,int64_t * timeNanoseconds)529 AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
530                                       clockid_t clockid,
531                                       int64_t *framePosition,
532                                       int64_t *timeNanoseconds)
533 {
534     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
535     if (framePosition == nullptr) {
536         return AAUDIO_ERROR_NULL;
537     } else if (timeNanoseconds == nullptr) {
538         return AAUDIO_ERROR_NULL;
539     } else if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_BOOTTIME) {
540         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
541     }
542 
543     return audioStream->getTimestamp(clockid, framePosition, timeNanoseconds);
544 }
545 
AAudio_getMMapPolicy()546 AAUDIO_API aaudio_policy_t AAudio_getMMapPolicy() {
547     return AudioGlobal_getMMapPolicy();
548 }
549 
AAudio_setMMapPolicy(aaudio_policy_t policy)550 AAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy) {
551     return AudioGlobal_setMMapPolicy(policy);
552 }
553 
AAudioStream_isMMapUsed(AAudioStream * stream)554 AAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream)
555 {
556     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
557     return audioStream->isMMap();
558 }
559 
AAudioStream_isPrivacySensitive(AAudioStream * stream)560 AAUDIO_API bool AAudioStream_isPrivacySensitive(AAudioStream* stream)
561 {
562     AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
563     return audioStream->isPrivacySensitive();
564 }
565