AudioStream.h revision 71f35bb687476694882a617ba4a810a0bb56fe23
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>
21e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk#include <mutex>
22d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk#include <stdint.h>
235ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h>
24d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
25c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "utility/AAudioUtilities.h"
26c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "utility/MonotonicCounter.h"
27e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
285ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
29e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
305fbc2ddb4e340d7af02d146ff2e7c3c4da28d535Phil Burktypedef void *(*aaudio_audio_thread_proc_t)(void *);
315fbc2ddb4e340d7af02d146ff2e7c3c4da28d535Phil Burk
32e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStreamBuilder;
33e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
34e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk/**
355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * AAudio audio stream.
36e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk */
37e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStream {
38e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkpublic:
39e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
40e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    AudioStream();
41e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
42e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual ~AudioStream();
43e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
44e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
45e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== Begin ABSTRACT methods ===========================
46e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
47e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /* Asynchronous requests.
48e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Use waitForStateChange() to wait for completion.
49e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStart() = 0;
515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestPause() = 0;
525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestFlush() = 0;
535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStop() = 0;
54e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
555ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t getTimestamp(clockid_t clockId,
563316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                       int64_t *framePosition,
573316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                       int64_t *timeNanoseconds) = 0;
58e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
59e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
60e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
61e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * Update state while in the middle of waitForStateChange()
62e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * @return
63e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
64e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    virtual aaudio_result_t updateStateWhileWaiting() = 0;
65e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
66e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
67e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== End ABSTRACT methods ===========================
68e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
695ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
70e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                               aaudio_stream_state_t *nextState,
71e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                               int64_t timeoutNanoseconds);
72e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
73e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
74e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Open the stream using the parameters in the builder.
75e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Allocate the necessary resources.
76e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
775ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t open(const AudioStreamBuilder& builder);
78e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
79e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
80e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Close the stream and deallocate any resources from the open() call.
81e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * It is safe to call close() multiple times.
82e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
835ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t close() {
845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
85e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
86e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
873316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual aaudio_result_t setBufferSize(int32_t requestedFrames) {
885ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
89e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
90e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
913316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual aaudio_result_t createThread(int64_t periodNanoseconds,
92e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk                                       aaudio_audio_thread_proc_t threadProc,
93d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk                                       void *threadArg);
94e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
953316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual aaudio_result_t joinThread(void **returnArg, int64_t timeoutNanoseconds);
96e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t registerThread() {
985ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
99d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
100d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
1015ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t unregisterThread() {
1025ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
103d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
104d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
105d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    /**
106d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * Internal function used to call the audio thread passed by the user.
107d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * It is unfortunately public because it needs to be called by a static 'C' function.
108d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     */
109d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    void* wrapUserThread();
110d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
111e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== Queries ===========================
112e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1133316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    aaudio_stream_state_t getState() const {
114e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mState;
115e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
116e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1173316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getBufferSize() const {
1185ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
119e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
120e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1213316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getBufferCapacity() const {
1225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
123e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
124e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1253316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getFramesPerBurst() const {
1265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
127e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
128e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
129e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual int32_t getXRunCount() const {
1305ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
131e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
132e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
133e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    bool isPlaying() const {
1345ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return mState == AAUDIO_STREAM_STATE_STARTING || mState == AAUDIO_STREAM_STATE_STARTED;
135e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
136e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1375ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSampleRate() const {
138e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSampleRate;
139e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
140e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1415ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_format_t getFormat()  const {
142e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFormat;
143e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
144e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSamplesPerFrame() const {
146e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame;
147e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
148e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1493316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getDeviceId() const {
150e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDeviceId;
151e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
152e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1535ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_sharing_mode_t getSharingMode() const {
154e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSharingMode;
155e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
156e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
15771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    bool isSharingModeMatchRequired() const {
15871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk        return mSharingModeMatchRequired;
15971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    }
16071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
1615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t getDirection() const {
162e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDirection;
163e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
164e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
165e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
166e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setSamplesPerFrame() and setFormat() have been called.
167e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
1683316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerFrame() const {
169e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame * getBytesPerSample();
170e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
171e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
172e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
173e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setFormat() has been called.
174e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
1753316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerSample() const {
1765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAudioConvert_formatToSizeInBytes(mFormat);
177e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
178e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1793316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t getFramesWritten() {
180e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesWritten.get();
181e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
182e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1833316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t getFramesRead() {
184e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesRead.get();
185e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
186e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
187e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback getDataCallbackProc() const {
188e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackProc;
189e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
190e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback getErrorCallbackProc() const {
191e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackProc;
192e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
193e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
194e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getDataCallbackUserData() const {
195e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackUserData;
196e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
197e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getErrorCallbackUserData() const {
198e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackUserData;
199e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
200e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
201e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t getFramesPerDataCallback() const {
202e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mFramesPerDataCallback;
203e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
204e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
205e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    bool isDataCallbackActive() {
206e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return (mDataCallbackProc != nullptr) && isPlaying();
207e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
208e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
209e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== I/O ===========================
210e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // A Stream will only implement read() or write() depending on its direction.
2115ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t write(const void *buffer,
2123316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                             int32_t numFrames,
2133316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                             int64_t timeoutNanoseconds) {
2145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
215e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
216e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t read(void *buffer,
2183316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                            int32_t numFrames,
2193316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                            int64_t timeoutNanoseconds) {
2205ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
221e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
222e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
223e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprotected:
224e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2253316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t incrementFramesWritten(int32_t frames) {
2263316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk        return static_cast<int64_t>(mFramesWritten.increment(frames));
227e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
228e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2293316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t incrementFramesRead(int32_t frames) {
2303316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk        return static_cast<int64_t>(mFramesRead.increment(frames));
231e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
232e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
233e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
234e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
235e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2363316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setSampleRate(int32_t sampleRate) {
237e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSampleRate = sampleRate;
238e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
239e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
240e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
241e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
242e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
243e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    void setSamplesPerFrame(int32_t samplesPerFrame) {
244e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSamplesPerFrame = samplesPerFrame;
245e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
246e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
247e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
248e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
249e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2505ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setSharingMode(aaudio_sharing_mode_t sharingMode) {
251e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSharingMode = sharingMode;
252e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
253e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
254e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
255e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
256e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setFormat(aaudio_audio_format_t format) {
258e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mFormat = format;
259e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
260e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2615ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setState(aaudio_stream_state_t state) {
262e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mState = state;
263e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
264e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
265c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    void setDeviceId(int32_t deviceId) {
266c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        mDeviceId = deviceId;
267c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    }
268c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
269e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    std::mutex           mStreamMutex;
270e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
271e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    std::atomic<bool>    mCallbackEnabled;
272d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
273d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burkprotected:
274e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesWritten;
275e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesRead;
276e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2773316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setPeriodNanoseconds(int64_t periodNanoseconds) {
278d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release);
279d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
280d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
2813316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int64_t getPeriodNanoseconds() {
282d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        return mPeriodNanoseconds.load(std::memory_order_acquire);
283d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
284d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
285e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprivate:
286e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // These do not change after open().
2873316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mSamplesPerFrame = AAUDIO_UNSPECIFIED;
2883316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mSampleRate = AAUDIO_UNSPECIFIED;
2893316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mDeviceId = AAUDIO_UNSPECIFIED;
290c35f3ae6f361a346b6d823601c69dd704afe644aPhil Burk    aaudio_sharing_mode_t  mSharingMode = AAUDIO_SHARING_MODE_SHARED;
29171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    bool                   mSharingModeMatchRequired = false; // must match sharing mode requested
2925ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_format_t  mFormat = AAUDIO_FORMAT_UNSPECIFIED;
2935ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t     mDirection = AAUDIO_DIRECTION_OUTPUT;
2943316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    aaudio_stream_state_t  mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
295e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
296e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    // callback ----------------------------------
297e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
298e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback   mDataCallbackProc = nullptr;  // external callback functions
299e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mDataCallbackUserData = nullptr;
300e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t                     mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
301e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
302e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback  mErrorCallbackProc = nullptr;
303e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mErrorCallbackUserData = nullptr;
304e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
305d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // background thread ----------------------------------
3063316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    bool                   mHasThread = false;
3073316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    pthread_t              mThread; // initialized in constructor
308d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
309d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // These are set by the application thread and then read by the audio pthread.
3103316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    std::atomic<int64_t>   mPeriodNanoseconds; // for tuning SCHED_FIFO threads
311d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // TODO make atomic?
312e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk    aaudio_audio_thread_proc_t mThreadProc = nullptr;
3133316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void*                  mThreadArg = nullptr;
3145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t        mThreadRegistrationResult = AAUDIO_OK;
315d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
316d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
317e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk};
318e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
3195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
320e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
3215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif /* AAUDIO_AUDIOSTREAM_H */
322