15ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk/*
25ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Copyright (C) 2016 The Android Open Source Project
35ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
45ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Licensed under the Apache License, Version 2.0 (the "License");
55ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * you may not use this file except in compliance with the License.
65ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * You may obtain a copy of the License at
75ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
85ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *      http://www.apache.org/licenses/LICENSE-2.0
95ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk *
105ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * Unless required by applicable law or agreed to in writing, software
115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * distributed under the License is distributed on an "AS IS" BASIS,
125ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * See the License for the specific language governing permissions and
145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * limitations under the License.
155ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk */
165ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define LOG_TAG "AAudio"
185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk//#define LOG_NDEBUG 0
195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <utils/Log.h>
205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <time.h>
225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <pthread.h>
235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
245ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h>
25d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#include <aaudio/AAudioTesting.h>
265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "AudioStreamBuilder.h"
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "AudioStream.h"
295ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "AudioClock.h"
305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "client/AudioStreamInternal.h"
315ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "HandleTracker.h"
325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burkusing namespace aaudio;
345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// Macros for common code that includes a return.
375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// TODO Consider using do{}while(0) construct. I tried but it hung AndroidStudio
385ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define CONVERT_BUILDER_HANDLE_OR_RETURN() \
393316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    convertAAudioBuilderToStreamBuilder(builder);
405ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define COMMON_GET_FROM_BUILDER_OR_RETURN(resultPtr) \
425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    CONVERT_BUILDER_HANDLE_OR_RETURN() \
435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if ((resultPtr) == nullptr) { \
445ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NULL; \
455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define AAUDIO_CASE_ENUM(name) case name: return #name
485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
495ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkAAUDIO_API const char * AAudio_convertResultToText(aaudio_result_t returnCode) {
505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    switch (returnCode) {
515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_OK);
52c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_DISCONNECTED);
535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_ILLEGAL_ARGUMENT);
5417fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk        // reserved
555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_INTERNAL);
565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_STATE);
5717fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk        // reserved
5817fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk        // reserved
595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_HANDLE);
6017fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk         // reserved
615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_UNIMPLEMENTED);
625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_UNAVAILABLE);
635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_FREE_HANDLES);
645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_MEMORY);
655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_NULL);
665ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_TIMEOUT);
675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_WOULD_BLOCK);
68c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_FORMAT);
695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_OUT_OF_RANGE);
705ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_NO_SERVICE);
71c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_ERROR_INVALID_RATE);
725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
735ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return "Unrecognized AAudio error.";
745ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil BurkAAUDIO_API const char * AAudio_convertStreamStateToText(aaudio_stream_state_t state) {
775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    switch (state) {
785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_UNINITIALIZED);
793316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_UNKNOWN);
805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_OPEN);
815ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STARTING);
825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STARTED);
835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_PAUSING);
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_PAUSED);
855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_FLUSHING);
865ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_FLUSHED);
875ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STOPPING);
885ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_STOPPED);
89c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_DISCONNECTED);
905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_CLOSING);
915ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        AAUDIO_CASE_ENUM(AAUDIO_STREAM_STATE_CLOSED);
925ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return "Unrecognized AAudio state.";
945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
955ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#undef AAUDIO_CASE_ENUM
975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
98d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
99d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk/******************************************
100d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk * Static globals.
101d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk */
102d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burkstatic aaudio_policy_t s_MMapPolicy = AAUDIO_UNSPECIFIED;
103d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
104d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
105e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burkstatic AudioStream *convertAAudioStreamToAudioStream(AAudioStream* stream)
1065ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
107e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk    return (AudioStream*) stream;
1085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1095ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
110e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burkstatic AudioStreamBuilder *convertAAudioBuilderToStreamBuilder(AAudioStreamBuilder* builder)
1115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
112e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk    return (AudioStreamBuilder*) builder;
1135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
115e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudio_createStreamBuilder(AAudioStreamBuilder** builder)
1165ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
11717fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk    AudioStreamBuilder *audioStreamBuilder =  new(std::nothrow) AudioStreamBuilder();
1185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (audioStreamBuilder == nullptr) {
1195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NO_MEMORY;
1205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
121e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk    *builder = (AAudioStreamBuilder*) audioStreamBuilder;
1225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return AAUDIO_OK;
1235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1245ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
125e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil BurkAAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
126e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk                                                       aaudio_performance_mode_t mode)
127e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk{
128e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
129e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    streamBuilder->setPerformanceMode(mode);
130e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk}
131e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
132e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setDeviceId(AAudioStreamBuilder* builder,
133e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk                                                int32_t deviceId)
1345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
135e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    streamBuilder->setDeviceId(deviceId);
1375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1385ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
139e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
1403316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                              int32_t sampleRate)
1415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
142e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    streamBuilder->setSampleRate(sampleRate);
1445ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
14620523edbf2f597c53b90e76694331c807e559515Phil BurkAAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
14720523edbf2f597c53b90e76694331c807e559515Phil Burk                                                       int32_t channelCount)
14820523edbf2f597c53b90e76694331c807e559515Phil Burk{
14920523edbf2f597c53b90e76694331c807e559515Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
15020523edbf2f597c53b90e76694331c807e559515Phil Burk    streamBuilder->setSamplesPerFrame(channelCount);
15120523edbf2f597c53b90e76694331c807e559515Phil Burk}
15220523edbf2f597c53b90e76694331c807e559515Phil Burk
153e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
15420523edbf2f597c53b90e76694331c807e559515Phil Burk                                                       int32_t samplesPerFrame)
1555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
156e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    streamBuilder->setSamplesPerFrame(samplesPerFrame);
1585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
160e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
1615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                             aaudio_direction_t direction)
1625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
163e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    streamBuilder->setDirection(direction);
1655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1665ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
167e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setFormat(AAudioStreamBuilder* builder,
1689dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk                                                   aaudio_format_t format)
1695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
170e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    streamBuilder->setFormat(format);
1725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1735ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
174e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setSharingMode(AAudioStreamBuilder* builder,
1755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                                        aaudio_sharing_mode_t sharingMode)
1765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
177e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1783316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    streamBuilder->setSharingMode(sharingMode);
1795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
1805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
181e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
1823316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                                        int32_t frames)
1835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
184e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
1853316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    streamBuilder->setBufferCapacity(frames);
1863df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk}
1873df348fbaca567ca891503213ff8c344a1ea2e05Phil Burk
188e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil BurkAAUDIO_API void AAudioStreamBuilder_setDataCallback(AAudioStreamBuilder* builder,
189e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                                    AAudioStream_dataCallback callback,
190e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                                    void *userData)
191e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{
192e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
193e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    streamBuilder->setDataCallbackProc(callback);
194e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    streamBuilder->setDataCallbackUserData(userData);
195e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk}
19671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
197e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil BurkAAUDIO_API void AAudioStreamBuilder_setErrorCallback(AAudioStreamBuilder* builder,
198e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                                 AAudioStream_errorCallback callback,
199e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                                 void *userData)
200e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{
201e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
202e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    streamBuilder->setErrorCallbackProc(callback);
203e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    streamBuilder->setErrorCallbackUserData(userData);
204e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk}
205e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
206e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil BurkAAUDIO_API void AAudioStreamBuilder_setFramesPerDataCallback(AAudioStreamBuilder* builder,
207e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                                int32_t frames)
208e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk{
209e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
210e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    streamBuilder->setFramesPerDataCallback(frames);
211e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk}
212e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
213e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStreamBuilder_openStream(AAudioStreamBuilder* builder,
214e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk                                                     AAudioStream** streamPtr)
2155ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2164c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    AudioStream *audioStream = nullptr;
217cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk    // Please leave these logs because they are very helpful when debugging.
218cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk    ALOGD("AAudioStreamBuilder_openStream() called ----------------------------------------");
2195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    AudioStreamBuilder *streamBuilder = COMMON_GET_FROM_BUILDER_OR_RETURN(streamPtr);
2204c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    aaudio_result_t result = streamBuilder->build(&audioStream);
221cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk    ALOGD("AAudioStreamBuilder_openStream() returns %d = %s for (%p) ----------------",
222cf5f6d2825d9a8430a291042ca9c6f68e5b666d0Phil Burk          result, AAudio_convertResultToText(result), audioStream);
2234c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    if (result == AAUDIO_OK) {
2244c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk        *streamPtr = (AAudioStream*) audioStream;
2254c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    } else {
2264c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk        *streamPtr = nullptr;
2274c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    }
2284c5129b410884ec0400cbe65fce56d0ade12d11bPhil Burk    return result;
2295ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
231e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStreamBuilder_delete(AAudioStreamBuilder* builder)
2325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2333316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
2345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (streamBuilder != nullptr) {
2355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        delete streamBuilder;
2365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
2375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
23817fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk    return AAUDIO_ERROR_NULL;
2395ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2405ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
241e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStream_close(AAudioStream* stream)
2425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2433316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2443316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    ALOGD("AAudioStream_close(%p)", stream);
2455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (audioStream != nullptr) {
2465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        audioStream->close();
2475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        delete audioStream;
2485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
2495ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
25017fff38dd9d467bc5fb6cd5b9a6b183951c7750dPhil Burk    return AAUDIO_ERROR_NULL;
2515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
253e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStream_requestStart(AAudioStream* stream)
2545ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2553316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2563316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    ALOGD("AAudioStream_requestStart(%p)", stream);
2575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return audioStream->requestStart();
2585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
260e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStream_requestPause(AAudioStream* stream)
2615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2623316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2633316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    ALOGD("AAudioStream_requestPause(%p)", stream);
2645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return audioStream->requestPause();
2655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2665ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
267e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStream_requestFlush(AAudioStream* stream)
2685ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2693316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2703316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    ALOGD("AAudioStream_requestFlush(%p)", stream);
2715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return audioStream->requestFlush();
2725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2735ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
274e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t  AAudioStream_requestStop(AAudioStream* stream)
2755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2763316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2773316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    ALOGD("AAudioStream_requestStop(%p)", stream);
2785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return audioStream->requestStop();
2795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
281e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudioStream_waitForStateChange(AAudioStream* stream,
2825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                            aaudio_stream_state_t inputState,
2835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                            aaudio_stream_state_t *nextState,
2843316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                            int64_t timeoutNanoseconds)
2855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
2865ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
2873316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
2885ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return audioStream->waitForStateChange(inputState, nextState, timeoutNanoseconds);
2895ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
2905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
2915ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// ============================================================
2925ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// Stream - non-blocking I/O
2935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// ============================================================
2945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
295e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudioStream_read(AAudioStream* stream,
2965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                               void *buffer,
2973316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                               int32_t numFrames,
2983316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                               int64_t timeoutNanoseconds)
2995ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3003316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3015ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (buffer == nullptr) {
3025ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NULL;
3035ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
3045ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (numFrames < 0) {
3055ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
3065ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    } else if (numFrames == 0) {
3075ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return 0;
3085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
3095ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3105ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t result = audioStream->read(buffer, numFrames, timeoutNanoseconds);
3115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3125ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return result;
3135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
315e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudioStream_write(AAudioStream* stream,
3165ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                               const void *buffer,
3173316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                               int32_t numFrames,
3183316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                               int64_t timeoutNanoseconds)
3195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3203316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (buffer == nullptr) {
3225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NULL;
3235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
324e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
325e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    // Don't allow writes when playing with a callback.
32687c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    if (audioStream->getDataCallbackProc() != nullptr && audioStream->isActive()) {
327e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        ALOGE("Cannot write to a callback stream when running.");
328e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return AAUDIO_ERROR_INVALID_STATE;
329e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
330e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
3315ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (numFrames < 0) {
3325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
3335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    } else if (numFrames == 0) {
3345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return 0;
3355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
3365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t result = audioStream->write(buffer, numFrames, timeoutNanoseconds);
3385ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3395ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    return result;
3405ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// ============================================================
3435ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// Stream - queries
3445ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// ============================================================
3455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
346e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream)
3475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3483316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3493316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getSampleRate();
3505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
35220523edbf2f597c53b90e76694331c807e559515Phil BurkAAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
35320523edbf2f597c53b90e76694331c807e559515Phil Burk{
35420523edbf2f597c53b90e76694331c807e559515Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
35520523edbf2f597c53b90e76694331c807e559515Phil Burk    return audioStream->getSamplesPerFrame();
35620523edbf2f597c53b90e76694331c807e559515Phil Burk}
35720523edbf2f597c53b90e76694331c807e559515Phil Burk
358e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
3595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3603316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3613316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getSamplesPerFrame();
3625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
364e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_stream_state_t AAudioStream_getState(AAudioStream* stream)
3655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3663316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3673316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getState();
3685ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
3709dca9824da74d50be02bc81f539cc77b7bde678aPhil BurkAAUDIO_API aaudio_format_t AAudioStream_getFormat(AAudioStream* stream)
3715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3723316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3733316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getFormat();
3745ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3755ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
376e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudioStream_setBufferSizeInFrames(AAudioStream* stream,
3773316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                                int32_t requestedFrames)
3785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3793316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3803316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->setBufferSize(requestedFrames);
3815ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
383e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getBufferSizeInFrames(AAudioStream* stream)
3845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3853316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3863316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getBufferSize();
3875ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3885ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
389e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_direction_t AAudioStream_getDirection(AAudioStream* stream)
3905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3913316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3923316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getDirection();
3935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
3945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
395e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getFramesPerBurst(AAudioStream* stream)
3965ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
3973316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
3983316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getFramesPerBurst();
3995ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4005ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
401f2d8342c2e975f85766f3d967f8f5713b2d87892Phil BurkAAUDIO_API int32_t AAudioStream_getFramesPerDataCallback(AAudioStream* stream)
402f2d8342c2e975f85766f3d967f8f5713b2d87892Phil Burk{
403f2d8342c2e975f85766f3d967f8f5713b2d87892Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
404f2d8342c2e975f85766f3d967f8f5713b2d87892Phil Burk    return audioStream->getFramesPerDataCallback();
405f2d8342c2e975f85766f3d967f8f5713b2d87892Phil Burk}
406f2d8342c2e975f85766f3d967f8f5713b2d87892Phil Burk
407e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getBufferCapacityInFrames(AAudioStream* stream)
4085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4093316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4103316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getBufferCapacity();
4115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4125ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
413e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream)
4145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4153316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4163316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getXRunCount();
4175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
419e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil BurkAAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream)
420e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk{
421e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
422e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    return audioStream->getPerformanceMode();
423e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk}
424e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
425e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int32_t AAudioStream_getDeviceId(AAudioStream* stream)
4265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4273316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4283316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getDeviceId();
4295ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
431e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream)
4325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4333316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4343316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getSharingMode();
4355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
437e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int64_t AAudioStream_getFramesWritten(AAudioStream* stream)
4385ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4393316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4403316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getFramesWritten();
4415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4425ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
443e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API int64_t AAudioStream_getFramesRead(AAudioStream* stream)
4445ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4453316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4463316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getFramesRead();
4475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
4485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
449e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil BurkAAUDIO_API aaudio_result_t AAudioStream_getTimestamp(AAudioStream* stream,
4503316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                      clockid_t clockid,
4513316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                      int64_t *framePosition,
4523316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                      int64_t *timeNanoseconds)
4535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk{
4543316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
4555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    if (framePosition == nullptr) {
4565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NULL;
4575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    } else if (timeNanoseconds == nullptr) {
4585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_NULL;
4593316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    } else if (clockid != CLOCK_MONOTONIC && clockid != CLOCK_BOOTTIME) {
4605ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
4615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    }
4625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk
4633316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    return audioStream->getTimestamp(clockid, framePosition, timeNanoseconds);
4645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk}
465d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
466d04aeea00bdaa999821e1a15c437ea648bd07195Phil BurkAAUDIO_API aaudio_policy_t AAudio_getMMapPolicy() {
467d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    return s_MMapPolicy;
468d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk}
469d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
470d04aeea00bdaa999821e1a15c437ea648bd07195Phil BurkAAUDIO_API aaudio_result_t AAudio_setMMapPolicy(aaudio_policy_t policy) {
471d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    aaudio_result_t result = AAUDIO_OK;
472d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    switch(policy) {
473d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk        case AAUDIO_UNSPECIFIED:
474d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk        case AAUDIO_POLICY_NEVER:
475d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk        case AAUDIO_POLICY_AUTO:
476d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk        case AAUDIO_POLICY_ALWAYS:
477d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk            s_MMapPolicy = policy;
478d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk            break;
479d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk        default:
480d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk            result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
481d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk            break;
482d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    }
483d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    return result;
484d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk}
485d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk
486d04aeea00bdaa999821e1a15c437ea648bd07195Phil BurkAAUDIO_API bool AAudioStream_isMMapUsed(AAudioStream* stream)
487d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk{
488d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
489d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk    return audioStream->isMMap();
490d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk}
491