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_getHardwareSampleRate(AAudioStream * stream)421 AAUDIO_API int32_t AAudioStream_getHardwareSampleRate(AAudioStream* stream)
422 {
423 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
424 return audioStream->getHardwareSampleRate();
425 }
426
AAudioStream_getChannelCount(AAudioStream * stream)427 AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
428 {
429 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
430 return audioStream->getSamplesPerFrame();
431 }
432
AAudioStream_getHardwareChannelCount(AAudioStream * stream)433 AAUDIO_API int32_t AAudioStream_getHardwareChannelCount(AAudioStream* stream)
434 {
435 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
436 return audioStream->getHardwareSamplesPerFrame();
437 }
438
AAudioStream_getSamplesPerFrame(AAudioStream * stream)439 AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
440 {
441 return AAudioStream_getChannelCount(stream);
442 }
443
AAudioStream_getState(AAudioStream * stream)444 AAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream)
445 {
446 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
447 return audioStream->getStateExternal();
448 }
449
AAudioStream_getFormat(AAudioStream * stream)450 AAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream)
451 {
452 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
453 // Use audio_format_t internally.
454 audio_format_t internalFormat = audioStream->getFormat();
455 return AAudioConvert_androidToAAudioDataFormat(internalFormat);
456 }
457
AAudioStream_getHardwareFormat(AAudioStream * stream)458 AAUDIO_API aaudio_format_t AAudioStream_getHardwareFormat(AAudioStream* stream)
459 {
460 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
461 // Use audio_format_t internally.
462 audio_format_t internalFormat = audioStream->getHardwareFormat();
463 return AAudioConvert_androidToNearestAAudioDataFormat(internalFormat);
464 }
465
AAudioStream_setBufferSizeInFrames(AAudioStream * stream,int32_t requestedFrames)466 AAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
467 int32_t requestedFrames)
468 {
469 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
470 return audioStream->setBufferSize(requestedFrames);
471 }
472
AAudioStream_getBufferSizeInFrames(AAudioStream * stream)473 AAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream)
474 {
475 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
476 return audioStream->getBufferSize();
477 }
478
AAudioStream_getDirection(AAudioStream * stream)479 AAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream)
480 {
481 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
482 return audioStream->getDirection();
483 }
484
AAudioStream_getFramesPerBurst(AAudioStream * stream)485 AAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream)
486 {
487 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
488 return audioStream->getFramesPerBurst();
489 }
490
AAudioStream_getFramesPerDataCallback(AAudioStream * stream)491 AAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream)
492 {
493 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
494 return audioStream->getFramesPerDataCallback();
495 }
496
AAudioStream_getBufferCapacityInFrames(AAudioStream * stream)497 AAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream)
498 {
499 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
500 return audioStream->getBufferCapacity();
501 }
502
AAudioStream_getXRunCount(AAudioStream * stream)503 AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream)
504 {
505 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
506 return audioStream->getXRunCount();
507 }
508
AAudioStream_getPerformanceMode(AAudioStream * stream)509 AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream)
510 {
511 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
512 return audioStream->getPerformanceMode();
513 }
514
AAudioStream_getDeviceId(AAudioStream * stream)515 AAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream)
516 {
517 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
518 return audioStream->getDeviceId();
519 }
520
AAudioStream_getSharingMode(AAudioStream * stream)521 AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream)
522 {
523 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
524 return audioStream->getSharingMode();
525 }
526
AAudioStream_getUsage(AAudioStream * stream)527 AAUDIO_API aaudio_usage_t AAudioStream_getUsage(AAudioStream* stream)
528 {
529 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
530 return audioStream->getUsage();
531 }
532
AAudioStream_getContentType(AAudioStream * stream)533 AAUDIO_API aaudio_content_type_t AAudioStream_getContentType(AAudioStream* stream)
534 {
535 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
536 return audioStream->getContentType();
537 }
538
AAudioStream_getSpatializationBehavior(AAudioStream * stream)539 AAUDIO_API aaudio_spatialization_behavior_t AAudioStream_getSpatializationBehavior(
540 AAudioStream* stream)
541 {
542 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
543 return audioStream->getSpatializationBehavior();
544 }
545
AAudioStream_isContentSpatialized(AAudioStream * stream)546 AAUDIO_API bool AAudioStream_isContentSpatialized(AAudioStream* stream)
547 {
548 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
549 return audioStream->isContentSpatialized();
550 }
551
AAudioStream_getInputPreset(AAudioStream * stream)552 AAUDIO_API aaudio_input_preset_t AAudioStream_getInputPreset(AAudioStream* stream)
553 {
554 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
555 return audioStream->getInputPreset();
556 }
557
AAudioStream_getAllowedCapturePolicy(AAudioStream * stream)558 AAUDIO_API aaudio_allowed_capture_policy_t AAudioStream_getAllowedCapturePolicy(
559 AAudioStream* stream)
560 {
561 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
562 return audioStream->getAllowedCapturePolicy();
563 }
564
AAudioStream_getSessionId(AAudioStream * stream)565 AAUDIO_API int32_t AAudioStream_getSessionId(AAudioStream* stream)
566 {
567 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
568 return audioStream->getSessionId();
569 }
570
AAudioStream_getFramesWritten(AAudioStream * stream)571 AAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream)
572 {
573 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
574 return audioStream->getFramesWritten();
575 }
576
AAudioStream_getFramesRead(AAudioStream * stream)577 AAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream)
578 {
579 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
580 return audioStream->getFramesRead();
581 }
582
AAudioStream_getTimestamp(AAudioStream * stream,clockid_t clockid,int64_t * framePosition,int64_t * timeNanoseconds)583 AAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
584 clockid_t clockid,
585 int64_t *framePosition,
586 int64_t *timeNanoseconds)
587 {
588 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
589 if (framePosition == nullptr || timeNanoseconds == nullptr) {
590 return AAUDIO_ERROR_NULL;
591 } else if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_BOOTTIME) {
592 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
593 }
594
595 return audioStream->getTimestamp(clockid, framePosition, timeNanoseconds);
596 }
597
AAudio_getMMapPolicy()598 AAUDIO_API aaudio_policy_t AAudio_getMMapPolicy() {
599 return AudioGlobal_getMMapPolicy();
600 }
601
AAudio_setMMapPolicy(aaudio_policy_t policy)602 AAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy) {
603 return AudioGlobal_setMMapPolicy(policy);
604 }
605
AAudioStream_isMMapUsed(AAudioStream * stream)606 AAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream)
607 {
608 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
609 return audioStream->isMMap();
610 }
611
AAudioStream_isPrivacySensitive(AAudioStream * stream)612 AAUDIO_API bool AAudioStream_isPrivacySensitive(AAudioStream* stream)
613 {
614 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
615 return audioStream->isPrivacySensitive();
616 }
617
AAudioStream_getChannelMask(AAudioStream * stream)618 AAUDIO_API aaudio_channel_mask_t AAudioStream_getChannelMask(AAudioStream* stream)
619 {
620 AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
621 const aaudio_channel_mask_t channelMask = audioStream->getChannelMask();
622 // Do not return channel index masks as they are not public.
623 return AAudio_isChannelIndexMask(channelMask) ? AAUDIO_UNSPECIFIED : channelMask;
624 }
625