AudioStream.h revision 5fbc2ddb4e340d7af02d146ff2e7c3c4da28d535
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
1575ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t getDirection() const {
158e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDirection;
159e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
160e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
161e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
162e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setSamplesPerFrame() and setFormat() have been called.
163e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
1643316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerFrame() const {
165e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame * getBytesPerSample();
166e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
167e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
168e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
169e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setFormat() has been called.
170e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
1713316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerSample() const {
1725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAudioConvert_formatToSizeInBytes(mFormat);
173e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
174e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1753316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t getFramesWritten() {
176e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesWritten.get();
177e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
178e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1793316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t getFramesRead() {
180e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFramesRead.get();
181e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
182e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
183e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback getDataCallbackProc() const {
184e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackProc;
185e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
186e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback getErrorCallbackProc() const {
187e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackProc;
188e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
189e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
190e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getDataCallbackUserData() const {
191e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackUserData;
192e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
193e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getErrorCallbackUserData() const {
194e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackUserData;
195e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
196e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
197e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t getFramesPerDataCallback() const {
198e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mFramesPerDataCallback;
199e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
200e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
201e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    bool isDataCallbackActive() {
202e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return (mDataCallbackProc != nullptr) && isPlaying();
203e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
204e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
205e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== I/O ===========================
206e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // A Stream will only implement read() or write() depending on its direction.
2075ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t write(const void *buffer,
2083316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                             int32_t numFrames,
2093316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                             int64_t timeoutNanoseconds) {
2105ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
211e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
212e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2135ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t read(void *buffer,
2143316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                            int32_t numFrames,
2153316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                            int64_t timeoutNanoseconds) {
2165ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
217e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
218e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
219e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprotected:
220e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2213316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t incrementFramesWritten(int32_t frames) {
2223316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk        return static_cast<int64_t>(mFramesWritten.increment(frames));
223e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
224e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2253316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int64_t incrementFramesRead(int32_t frames) {
2263316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk        return static_cast<int64_t>(mFramesRead.increment(frames));
227e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
228e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
229e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
230e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Wait for a transition from one state to another.
2315ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk     * @return AAUDIO_OK if the endingState was observed, or AAUDIO_ERROR_UNEXPECTED_STATE
232e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     *   if any state that was not the startingState or endingState was observed
2335ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk     *   or AAUDIO_ERROR_TIMEOUT
234e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t waitForStateTransition(aaudio_stream_state_t startingState,
2363316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                                   aaudio_stream_state_t endingState,
2373316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                                   int64_t timeoutNanoseconds);
238e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
239e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
240e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
241e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2423316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setSampleRate(int32_t sampleRate) {
243e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSampleRate = sampleRate;
244e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
245e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
246e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
247e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
248e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
249e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    void setSamplesPerFrame(int32_t samplesPerFrame) {
250e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSamplesPerFrame = samplesPerFrame;
251e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
252e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
253e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
254e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
255e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setSharingMode(aaudio_sharing_mode_t sharingMode) {
257e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSharingMode = sharingMode;
258e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
259e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
260e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
261e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
262e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
2635ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setFormat(aaudio_audio_format_t format) {
264e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mFormat = format;
265e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
266e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2675ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setState(aaudio_stream_state_t state) {
268e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mState = state;
269e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
270e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
271c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    void setDeviceId(int32_t deviceId) {
272c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        mDeviceId = deviceId;
273c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    }
274c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
275e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    std::mutex           mStreamMutex;
276e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
277e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    std::atomic<bool>    mCallbackEnabled;
278d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
279d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burkprotected:
280e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesWritten;
281e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    MonotonicCounter     mFramesRead;
282e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2833316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setPeriodNanoseconds(int64_t periodNanoseconds) {
284d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release);
285d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
286d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
2873316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int64_t getPeriodNanoseconds() {
288d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        return mPeriodNanoseconds.load(std::memory_order_acquire);
289d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
290d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
291e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprivate:
292e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // These do not change after open().
2933316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mSamplesPerFrame = AAUDIO_UNSPECIFIED;
2943316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mSampleRate = AAUDIO_UNSPECIFIED;
2953316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t                mDeviceId = AAUDIO_UNSPECIFIED;
296c35f3ae6f361a346b6d823601c69dd704afe644aPhil Burk    aaudio_sharing_mode_t  mSharingMode = AAUDIO_SHARING_MODE_SHARED;
2975ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_audio_format_t  mFormat = AAUDIO_FORMAT_UNSPECIFIED;
2985ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_direction_t     mDirection = AAUDIO_DIRECTION_OUTPUT;
2993316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    aaudio_stream_state_t  mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
300e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
301e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    // callback ----------------------------------
302e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
303e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback   mDataCallbackProc = nullptr;  // external callback functions
304e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mDataCallbackUserData = nullptr;
305e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t                     mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
306e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
307e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback  mErrorCallbackProc = nullptr;
308e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mErrorCallbackUserData = nullptr;
309e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
310d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // background thread ----------------------------------
3113316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    bool                   mHasThread = false;
3123316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    pthread_t              mThread; // initialized in constructor
313d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
314d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // These are set by the application thread and then read by the audio pthread.
3153316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    std::atomic<int64_t>   mPeriodNanoseconds; // for tuning SCHED_FIFO threads
316d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // TODO make atomic?
317e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk    aaudio_audio_thread_proc_t mThreadProc = nullptr;
3183316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void*                  mThreadArg = nullptr;
3195ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t        mThreadRegistrationResult = AAUDIO_OK;
320d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
321d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
322e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk};
323e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
3245ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
325e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
3265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif /* AAUDIO_AUDIOSTREAM_H */
327