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