AudioStream.h revision c35f3ae6f361a346b6d823601c69dd704afe644a
1e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk/*
2e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Copyright 2016 The Android Open Source Project
3e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
4e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * you may not use this file except in compliance with the License.
6e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * You may obtain a copy of the License at
7e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
8e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk *
10e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * Unless required by applicable law or agreed to in writing, software
11e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * See the License for the specific language governing permissions and
14e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk * limitations under the License.
15e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk */
16e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#ifndef AAUDIO_AUDIOSTREAM_H
185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#define AAUDIO_AUDIOSTREAM_H
19e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
20d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk#include <atomic>
21d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk#include <stdint.h>
225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudioDefinitions.h>
235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h>
24d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
255ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include "AAudioUtilities.h"
26e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk#include "MonotonicCounter.h"
27e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
29e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
30e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStreamBuilder;
31e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
32e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk/**
335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * AAudio audio stream.
34e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk */
35e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStream {
36e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkpublic:
37e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
38e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    AudioStream();
39e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
40e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual ~AudioStream();
41e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
42e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
43e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== Begin ABSTRACT methods ===========================
44e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
45e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /* Asynchronous requests.
46e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Use waitForStateChange() to wait for completion.
47e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStart() = 0;
495ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestPause() = 0;
505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestFlush() = 0;
515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStop() = 0;
52e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    // TODO use aaudio_clockid_t all the way down to AudioClock
545ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t getTimestamp(clockid_t clockId,
555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                       aaudio_position_frames_t *framePosition,
565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                       aaudio_nanoseconds_t *timeNanoseconds) = 0;
57e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
58e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t updateState() = 0;
60e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
61e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
62e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== End ABSTRACT methods ===========================
63e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
645ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                          aaudio_stream_state_t *nextState,
665ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                          aaudio_nanoseconds_t timeoutNanoseconds);
67e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
68e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
69e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Open the stream using the parameters in the builder.
70e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Allocate the necessary resources.
71e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t open(const AudioStreamBuilder& builder);
73e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
74e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
75e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Close the stream and deallocate any resources from the open() call.
76e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * It is safe to call close() multiple times.
77e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t close() {
795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
80e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
81e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t setBufferSize(aaudio_size_frames_t requestedFrames,
835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                        aaudio_size_frames_t *actualFrames) {
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
85e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
86e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
875ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t createThread(aaudio_nanoseconds_t periodNanoseconds,
885ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                       aaudio_audio_thread_proc_t *threadProc,
89d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk                                       void *threadArg);
90e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
915ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t joinThread(void **returnArg, aaudio_nanoseconds_t timeoutNanoseconds);
92e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t registerThread() {
945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
95d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
96d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t unregisterThread() {
985ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
99d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
100d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
101d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    /**
102d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * Internal function used to call the audio thread passed by the user.
103d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * It is unfortunately public because it needs to be called by a static 'C' function.
104d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     */
105d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    void* wrapUserThread();
106d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
107e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== Queries ===========================
108e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1095ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_stream_state_t getState() const {
110e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mState;
111e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
112e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_size_frames_t getBufferSize() const {
1145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
115e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
116e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_size_frames_t getBufferCapacity() const {
1185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
119e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
120e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_size_frames_t getFramesPerBurst() const {
1225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
123e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
124e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
125e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual int32_t getXRunCount() const {
1265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
127e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
128e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
129e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    bool isPlaying() const {
1305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return mState == AAUDIO_STREAM_STATE_STARTING || mState == AAUDIO_STREAM_STATE_STARTED;
131e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
132e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSampleRate() const {
134e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSampleRate;
135e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
136e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_format_t getFormat()  const {
138e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFormat;
139e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
140e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSamplesPerFrame() const {
142e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame;
143e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
144e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_device_id_t getDeviceId() const {
146e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDeviceId;
147e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
148e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1495ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_sharing_mode_t getSharingMode() const {
150e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSharingMode;
151e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
152e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t getDirection() const {
154e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDirection;
155e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
156e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_size_bytes_t getBytesPerFrame() const {
158e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame * getBytesPerSample();
159e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
160e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_size_bytes_t getBytesPerSample() const {
1625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAudioConvert_formatToSizeInBytes(mFormat);
163e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
164e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1655ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_position_frames_t getFramesWritten() {
166e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesWritten.get();
167e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
168e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_position_frames_t getFramesRead() {
170e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesRead.get();
171e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
172e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
173e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
174e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== I/O ===========================
175e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // A Stream will only implement read() or write() depending on its direction.
1765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t write(const void *buffer,
1775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                             aaudio_size_frames_t numFrames,
1785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                             aaudio_nanoseconds_t timeoutNanoseconds) {
1795ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
180e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
181e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1825ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t read(void *buffer,
1835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                            aaudio_size_frames_t numFrames,
1845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                            aaudio_nanoseconds_t timeoutNanoseconds) {
1855ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
186e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
187e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
188e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprotected:
189e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_position_frames_t incrementFramesWritten(aaudio_size_frames_t frames) {
1915ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return static_cast<aaudio_position_frames_t>(mFramesWritten.increment(frames));
192e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
193e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1945ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_position_frames_t incrementFramesRead(aaudio_size_frames_t frames) {
1955ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return static_cast<aaudio_position_frames_t>(mFramesRead.increment(frames));
196e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
197e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
198e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
199e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Wait for a transition from one state to another.
2005ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk     * @return AAUDIO_OK if the endingState was observed, or AAUDIO_ERROR_UNEXPECTED_STATE
201e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     *   if any state that was not the startingState or endingState was observed
2025ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk     *   or AAUDIO_ERROR_TIMEOUT
203e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2045ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t waitForStateTransition(aaudio_stream_state_t startingState,
2055ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                              aaudio_stream_state_t endingState,
2065ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk                                              aaudio_nanoseconds_t timeoutNanoseconds);
207e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
208e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
209e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
210e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setSampleRate(aaudio_sample_rate_t sampleRate) {
212e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSampleRate = sampleRate;
213e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
214e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
215e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
216e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
217e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
218e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    void setSamplesPerFrame(int32_t samplesPerFrame) {
219e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSamplesPerFrame = samplesPerFrame;
220e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
221e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
222e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
223e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
224e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2255ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setSharingMode(aaudio_sharing_mode_t sharingMode) {
226e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSharingMode = sharingMode;
227e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
228e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
229e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
230e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
231e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2325ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setFormat(aaudio_audio_format_t format) {
233e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mFormat = format;
234e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
235e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setState(aaudio_stream_state_t state) {
237e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mState = state;
238e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
239e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
240d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
241d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
242d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burkprotected:
243e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesWritten;
244e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesRead;
245e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setPeriodNanoseconds(aaudio_nanoseconds_t periodNanoseconds) {
247d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release);
248d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
249d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
2505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_nanoseconds_t getPeriodNanoseconds() {
251d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        return mPeriodNanoseconds.load(std::memory_order_acquire);
252d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
253d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
254e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprivate:
255e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // These do not change after open().
2565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    int32_t              mSamplesPerFrame = AAUDIO_UNSPECIFIED;
2575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_sample_rate_t   mSampleRate = AAUDIO_UNSPECIFIED;
2585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_stream_state_t  mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
2595ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_device_id_t     mDeviceId = AAUDIO_UNSPECIFIED;
260c35f3ae6f361a346b6d823601c69dd704afe644aPhil Burk    aaudio_sharing_mode_t  mSharingMode = AAUDIO_SHARING_MODE_SHARED;
2615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_format_t  mFormat = AAUDIO_FORMAT_UNSPECIFIED;
2625ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t     mDirection = AAUDIO_DIRECTION_OUTPUT;
263e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
264d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // background thread ----------------------------------
265ec73c30ec4e89f3fba4d9517e704b92bc1ee6e08Phil Burk    bool                 mHasThread = false;
266d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    pthread_t            mThread; // initialized in constructor
267d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
268d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // These are set by the application thread and then read by the audio pthread.
2695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    std::atomic<aaudio_nanoseconds_t>  mPeriodNanoseconds; // for tuning SCHED_FIFO threads
270d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // TODO make atomic?
2715ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_thread_proc_t* mThreadProc = nullptr;
272d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    void*                mThreadArg = nullptr;
2735ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t        mThreadRegistrationResult = AAUDIO_OK;
274d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
275d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
276e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk};
277e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2785ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
279e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif /* AAUDIO_AUDIOSTREAM_H */
281