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>
24965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#include <binder/IServiceManager.h>
25965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#include <binder/Status.h>
26965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#include <utils/StrongPointer.h>
27d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
28965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#include "media/VolumeShaper.h"
29965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#include "media/PlayerBase.h"
30c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "utility/AAudioUtilities.h"
31c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk#include "utility/MonotonicCounter.h"
32e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
33965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk// Cannot get android::media::VolumeShaper to compile!
34965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#define AAUDIO_USE_VOLUME_SHAPER  0
35965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burknamespace aaudio {
37e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
385fbc2ddb4e340d7af02d146ff2e7c3c4da28d535Phil Burktypedef void *(*aaudio_audio_thread_proc_t)(void *);
395fbc2ddb4e340d7af02d146ff2e7c3c4da28d535Phil Burk
40e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStreamBuilder;
41e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
42134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burkconstexpr pid_t        CALLBACK_THREAD_NONE = 0;
43134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
44e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk/**
455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk * AAudio audio stream.
46e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk */
47e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkclass AudioStream {
48e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkpublic:
49e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
50e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    AudioStream();
51e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
52e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual ~AudioStream();
53e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
54134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    /**
55134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     * Lock a mutex and make sure we are not calling from a callback function.
56134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     * @return result of requestStart();
57134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     */
58134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t safeStart();
59134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
60134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t safePause();
61134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
62134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t safeFlush();
63134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
64134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t safeStop();
65134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
66134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t safeClose();
67e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
68e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== Begin ABSTRACT methods ===========================
69134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burkprotected:
70e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
71e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /* Asynchronous requests.
72e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Use waitForStateChange() to wait for completion.
73e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
745ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStart() = 0;
75b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk
765cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    /**
775cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk     * Check the state to see if Pause if currently legal.
785cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk     *
795cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk     * @param result pointer to return code
805cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk     * @return true if OK to continue, if false then return result
815cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk     */
825cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    bool checkPauseStateTransition(aaudio_result_t *result);
835cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk
845cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    virtual bool isFlushSupported() const {
855cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk        // Only implement FLUSH for OUTPUT streams.
865cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk        return false;
875cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    }
885cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk
895cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    virtual bool isPauseSupported() const {
905cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk        // Only implement PAUSE for OUTPUT streams.
915cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk        return false;
925cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    }
935cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk
94b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk    virtual aaudio_result_t requestPause()
95b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk    {
96b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk        // Only implement this for OUTPUT streams.
97b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
98b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk    }
99b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk
100b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk    virtual aaudio_result_t requestFlush() {
101b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk        // Only implement this for OUTPUT streams.
102b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
103b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk    }
104b336e89d57d51b80b8bf5f0a0caf7667209ffb1dPhil Burk
1055ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t requestStop() = 0;
106e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
107134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burkpublic:
1085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t getTimestamp(clockid_t clockId,
1093316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                       int64_t *framePosition,
1103316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk                                       int64_t *timeNanoseconds) = 0;
111e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
112e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
113e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
1140befec600314a79c0d142c3edd99f7d1c75e1afePhil Burk     * Update state machine.()
115e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * @return
116e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
1170befec600314a79c0d142c3edd99f7d1c75e1afePhil Burk    virtual aaudio_result_t updateStateMachine() = 0;
118e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
119e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // =========== End ABSTRACT methods ===========================
120e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState,
122e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                               aaudio_stream_state_t *nextState,
123e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk                                               int64_t timeoutNanoseconds);
124e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
125e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
126e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Open the stream using the parameters in the builder.
127e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Allocate the necessary resources.
128e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
1295ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t open(const AudioStreamBuilder& builder);
130e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
131e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
132e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * Close the stream and deallocate any resources from the open() call.
133e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * It is safe to call close() multiple times.
134e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
1355ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t close() {
1365ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
137e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
138e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1392ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk    virtual aaudio_result_t setBufferSize(int32_t requestedFrames) = 0;
140e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1413316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual aaudio_result_t createThread(int64_t periodNanoseconds,
142e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk                                       aaudio_audio_thread_proc_t threadProc,
143d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk                                       void *threadArg);
144e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
14587c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    aaudio_result_t joinThread(void **returnArg, int64_t timeoutNanoseconds);
146e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t registerThread() {
1485ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
149d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
150d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
1515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    virtual aaudio_result_t unregisterThread() {
1525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_OK;
153d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
154d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
155d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    /**
156d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * Internal function used to call the audio thread passed by the user.
157d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     * It is unfortunately public because it needs to be called by a static 'C' function.
158d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk     */
159d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    void* wrapUserThread();
160d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
161e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== Queries ===========================
162e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1633316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    aaudio_stream_state_t getState() const {
164e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mState;
165e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
166e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1673316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getBufferSize() const {
1685ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
169e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
170e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1713316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getBufferCapacity() const {
1725ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
173e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
174e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1753316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    virtual int32_t getFramesPerBurst() const {
1765ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
177e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
178e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
179e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    virtual int32_t getXRunCount() const {
1805ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
181e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
182e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
18387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    bool isActive() const {
1845ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return mState == AAUDIO_STREAM_STATE_STARTING || mState == AAUDIO_STREAM_STATE_STARTED;
185e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
186e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
187e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    virtual bool isMMap() {
188e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk        return false;
189e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    }
190e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
1915ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSampleRate() const {
192e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSampleRate;
193e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
194e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1959dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk    aaudio_format_t getFormat()  const {
196e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mFormat;
197e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
198e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
1995ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_result_t getSamplesPerFrame() const {
200e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame;
201e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
202e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
203e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    virtual int32_t getPerformanceMode() const {
204e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk        return mPerformanceMode;
205e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    }
206e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
207e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    void setPerformanceMode(aaudio_performance_mode_t performanceMode) {
208e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk        mPerformanceMode = performanceMode;
209e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk    }
210e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
2113316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getDeviceId() const {
212e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mDeviceId;
213e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
214e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2155ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    aaudio_sharing_mode_t getSharingMode() const {
216e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSharingMode;
217e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
218e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
21971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    bool isSharingModeMatchRequired() const {
22071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk        return mSharingModeMatchRequired;
22171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk    }
22271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk
22387c9f646a94259d7c321c3b3d5947fa1778f5ac2Phil Burk    virtual aaudio_direction_t getDirection() const = 0;
224e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
225d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    aaudio_usage_t getUsage() const {
226d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk        return mUsage;
227d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    }
228d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk
229d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    aaudio_content_type_t getContentType() const {
230d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk        return mContentType;
231d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    }
232d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk
233d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    aaudio_input_preset_t getInputPreset() const {
234d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk        return mInputPreset;
235d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk    }
236d4ccc624201d5c74ff6e14db1b68743b41fe795cPhil Burk
2374e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk    int32_t getSessionId() const {
2384e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk        return mSessionId;
2394e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk    }
2404e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk
241e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
242e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setSamplesPerFrame() and setFormat() have been called.
243e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
2443316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerFrame() const {
245e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        return mSamplesPerFrame * getBytesPerSample();
246e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
247e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
248e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    /**
249e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     * This is only valid after setFormat() has been called.
250e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk     */
2513316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int32_t getBytesPerSample() const {
2525ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAudioConvert_formatToSizeInBytes(mFormat);
253e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
254e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
2553d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    /**
2563d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * This is only valid after setSamplesPerFrame() and setDeviceFormat() have been called.
2573d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     */
2583d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    int32_t getBytesPerDeviceFrame() const {
2593d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk        return mSamplesPerFrame * getBytesPerDeviceSample();
2603d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    }
2613d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
2623d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    /**
2633d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * This is only valid after setDeviceFormat() has been called.
2643d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     */
2653d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    int32_t getBytesPerDeviceSample() const {
2663d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk        return AAudioConvert_formatToSizeInBytes(getDeviceFormat());
2673d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    }
2683d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
269ec89b2e2f8c84a7d3936db1a888034f4a4b0df16Phil Burk    virtual int64_t getFramesWritten() = 0;
270e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
271ec89b2e2f8c84a7d3936db1a888034f4a4b0df16Phil Burk    virtual int64_t getFramesRead() = 0;
272e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
273e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback getDataCallbackProc() const {
274e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackProc;
275e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
276134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
277e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback getErrorCallbackProc() const {
278e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackProc;
279e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
280e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
281134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_data_callback_result_t maybeCallDataCallback(void *audioData, int32_t numFrames);
282134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
283134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    void maybeCallErrorCallback(aaudio_result_t result);
284134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
285e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getDataCallbackUserData() const {
286e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mDataCallbackUserData;
287e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
288134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
289e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void *getErrorCallbackUserData() const {
290e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mErrorCallbackUserData;
291e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
292e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
293e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t getFramesPerDataCallback() const {
294e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk        return mFramesPerDataCallback;
295e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
296e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
297134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    /**
298134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     * @return true if data callback has been specified
299134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     */
300134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    bool isDataCallbackSet() const {
301134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk        return mDataCallbackProc != nullptr;
302e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    }
303e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
304134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    /**
305134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     * @return true if data callback has been specified and stream is running
306134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     */
307134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    bool isDataCallbackActive() const {
308134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk        return isDataCallbackSet() && isActive();
309134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    }
310134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
311134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    /**
312134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     * @return true if called from the same thread as the callback
313134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk     */
314134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    bool collidesWithCallback() const;
315134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
316e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // ============== I/O ===========================
317e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // A Stream will only implement read() or write() depending on its direction.
3182ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk    virtual aaudio_result_t write(const void *buffer __unused,
3192ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk                             int32_t numFrames __unused,
3202ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk                             int64_t timeoutNanoseconds __unused) {
3215ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
322e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
323e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
3242ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk    virtual aaudio_result_t read(void *buffer __unused,
3252ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk                            int32_t numFrames __unused,
3262ac035fdc577a843d07f385dbe3c4e2229d886dcPhil Burk                            int64_t timeoutNanoseconds __unused) {
3275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk        return AAUDIO_ERROR_UNIMPLEMENTED;
328e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
329e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
330965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // This is used by the AudioManager to duck and mute the stream when changing audio focus.
33155e5eabf96af5a2964bfab1e64d81ad9be9ac975Phil Burk    void setDuckAndMuteVolume(float duckAndMuteVolume);
332965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
333134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    float getDuckAndMuteVolume() const {
334965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        return mDuckAndMuteVolume;
335965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
336965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
337965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // Implement this in the output subclasses.
338965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    virtual android::status_t doSetVolume() { return android::NO_ERROR; }
339965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
340965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#if AAUDIO_USE_VOLUME_SHAPER
3418a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    virtual ::android::binder::Status applyVolumeShaper(
342965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            const ::android::media::VolumeShaper::Configuration& configuration __unused,
3438a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk            const ::android::media::VolumeShaper::Operation& operation __unused);
344965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#endif
345965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3468a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    /**
3478a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * Register this stream's PlayerBase with the AudioManager if needed.
3488a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * Only register output streams.
3498a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * This should only be called for client streams and not for streams
3508a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * that run in the service.
3518a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     */
3528a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    void registerPlayerBase() {
3538a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
354965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            mPlayerBase->registerWithAudioManager();
355965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
356965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
357965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3588a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    /**
3598a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * Unregister this stream's PlayerBase with the AudioManager.
3608a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     * This will only unregister if already registered.
3618a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk     */
3628a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    void unregisterPlayerBase() {
3638a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        mPlayerBase->unregisterWithAudioManager();
364965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
365965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
366965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // Pass start request through PlayerBase for tracking.
367965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    aaudio_result_t systemStart() {
368965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        mPlayerBase->start();
369965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        // Pass aaudio_result_t around the PlayerBase interface, which uses status__t.
370965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        return mPlayerBase->getResult();
371965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
372965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3735cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    // Pass pause request through PlayerBase for tracking.
374965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    aaudio_result_t systemPause() {
375965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        mPlayerBase->pause();
376965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        return mPlayerBase->getResult();
377965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
378965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3795cc83c3b8acc43b760be23a4e3b96fbbb06d8e93Phil Burk    // Pass stop request through PlayerBase for tracking.
380965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    aaudio_result_t systemStop() {
381965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        mPlayerBase->stop();
382965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        return mPlayerBase->getResult();
383965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    }
384965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
385e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprotected:
386e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
387965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // PlayerBase allows the system to control the stream.
388965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // Calling through PlayerBase->start() notifies the AudioManager of the player state.
389965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // The AudioManager also can start/stop a stream by calling mPlayerBase->playerStart().
390965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    // systemStart() ==> mPlayerBase->start()   mPlayerBase->playerStart() ==> requestStart()
391965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    //                        \                           /
392965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    //                         ------ AudioManager -------
393965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    class MyPlayerBase : public android::PlayerBase {
394965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    public:
3958a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        explicit MyPlayerBase(AudioStream *parent);
396965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3978a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        virtual ~MyPlayerBase();
398965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
3998a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        /**
4008a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         * Register for volume changes and remote control.
4018a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         */
4028a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        void registerWithAudioManager();
4038a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk
4048a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        /**
4058a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         * UnRegister.
4068a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         */
4078a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        void unregisterWithAudioManager();
4088a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk
4098a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        /**
4108a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         * Just calls unregisterWithAudioManager().
4118a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk         */
4128a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        void destroy() override;
413965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
4148a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        void clearParentReference() { mParent = nullptr; }
4158a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk
4168a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        android::status_t playerStart() override {
4178a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk            // mParent should NOT be null. So go ahead and crash if it is.
418134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk            mResult = mParent->safeStart();
419965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return AAudioConvert_aaudioToAndroidStatus(mResult);
420965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
421965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
4228a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        android::status_t playerPause() override {
423134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk            mResult = mParent->safePause();
424965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return AAudioConvert_aaudioToAndroidStatus(mResult);
425965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
426965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
4278a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        android::status_t playerStop() override {
428134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk            mResult = mParent->safeStop();
429965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return AAudioConvert_aaudioToAndroidStatus(mResult);
430965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
431965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
4328a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        android::status_t playerSetVolume() override {
433965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            // No pan and only left volume is taken into account from IPLayer interface
434965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            mParent->setDuckAndMuteVolume(mVolumeMultiplierL  /* * mPanMultiplierL */);
435965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return android::NO_ERROR;
436965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
437965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
438965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#if AAUDIO_USE_VOLUME_SHAPER
4398a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        ::android::binder::Status applyVolumeShaper(
4408a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk                const ::android::media::VolumeShaper::Configuration& configuration,
4418a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk                const ::android::media::VolumeShaper::Operation& operation) {
442965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return mParent->applyVolumeShaper(configuration, operation);
443965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
444965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk#endif
445965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
446965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        aaudio_result_t getResult() {
447965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk            return mResult;
448965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        }
449965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
4508a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk    private:
4518a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        AudioStream          *mParent;
452965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk        aaudio_result_t       mResult = AAUDIO_OK;
4538a8a9e5d91c8cc110b9916982f4c5242efca33e3Phil Burk        bool                  mRegistered = false;
454965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    };
455965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
456e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
457e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
458a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     * TODO for multiple setters: assert(mState == AAUDIO_STREAM_STATE_UNINITIALIZED)
459e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
4603316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setSampleRate(int32_t sampleRate) {
461e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSampleRate = sampleRate;
462e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
463e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
464e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
465e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
466e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
467e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    void setSamplesPerFrame(int32_t samplesPerFrame) {
468e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSamplesPerFrame = samplesPerFrame;
469e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
470e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
471e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
472e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
473e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
4745ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk    void setSharingMode(aaudio_sharing_mode_t sharingMode) {
475e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mSharingMode = sharingMode;
476e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
477e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
478e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    /**
479e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     * This should not be called after the open() call.
480e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk     */
4819dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk    void setFormat(aaudio_format_t format) {
482e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk        mFormat = format;
483e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    }
484e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
4853d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    /**
4863d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * This should not be called after the open() call.
4873d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     */
4883d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    void setDeviceFormat(aaudio_format_t format) {
4893d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk        mDeviceFormat = format;
4903d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    }
4913d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
4923d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    aaudio_format_t getDeviceFormat() const {
4933d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk        return mDeviceFormat;
4943d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    }
4953d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
49619e990e480245af25ec03ff3a3f53b820b0732cdPhil Burk    void setState(aaudio_stream_state_t state);
497e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
498c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    void setDeviceId(int32_t deviceId) {
499c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk        mDeviceId = deviceId;
500c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk    }
501c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fbPhil Burk
5024e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk    void setSessionId(int32_t sessionId) {
5034e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk        mSessionId = sessionId;
5044e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk    }
5054e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk
506965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    std::atomic<bool>    mCallbackEnabled{false};
507965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
508965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    float                mDuckAndMuteVolume = 1.0f;
509d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
510d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burkprotected:
511e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
5123d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    /**
5133d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * Either convert the data from device format to app format and return a pointer
5143d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * to the conversion buffer,
5153d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * OR just pass back the original pointer.
5163d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     *
5173d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * Note that this is only used for the INPUT path.
5183d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     *
5193d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * @param audioData
5203d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * @param numFrames
5213d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     * @return original pointer or the conversion buffer
5223d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk     */
5233d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    virtual const void * maybeConvertDeviceData(const void *audioData, int32_t numFrames) {
5243d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk        return audioData;
5253d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    }
5263d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
5273316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    void setPeriodNanoseconds(int64_t periodNanoseconds) {
528d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release);
529d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
530d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
5313316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk    int64_t getPeriodNanoseconds() {
532d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk        return mPeriodNanoseconds.load(std::memory_order_acquire);
533d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    }
534d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
535a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    /**
536a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     * This should not be called after the open() call.
537a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     */
538a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    void setUsage(aaudio_usage_t usage) {
539a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk        mUsage = usage;
540a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    }
541a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk
542a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    /**
543a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     * This should not be called after the open() call.
544a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     */
545a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    void setContentType(aaudio_content_type_t contentType) {
546a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk        mContentType = contentType;
547a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    }
548a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk
549a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    /**
550a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     * This should not be called after the open() call.
551a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk     */
552a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    void setInputPreset(aaudio_input_preset_t inputPreset) {
553a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk        mInputPreset = inputPreset;
554a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk    }
555a62fb9565d73d9ec9b56da65929afc0f686c696ePhil Burk
556e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burkprivate:
557134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
558134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    std::mutex                 mStreamLock;
559134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk
560965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk    const android::sp<MyPlayerBase>   mPlayerBase;
561965650e5c36f39cb5e6d780a22ff711fbf5a37f5Phil Burk
562e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk    // These do not change after open().
563134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    int32_t                     mSamplesPerFrame = AAUDIO_UNSPECIFIED;
564134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    int32_t                     mSampleRate = AAUDIO_UNSPECIFIED;
565134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    int32_t                     mDeviceId = AAUDIO_UNSPECIFIED;
566134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_sharing_mode_t       mSharingMode = AAUDIO_SHARING_MODE_SHARED;
567134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    bool                        mSharingModeMatchRequired = false; // must match sharing mode requested
568134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_format_t             mFormat = AAUDIO_FORMAT_UNSPECIFIED;
569134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_stream_state_t       mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
570134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_performance_mode_t   mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
5714e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk
572eaef9b9c7b3c6c4d4435ce5e32e9fe952a2f69fePhil Burk    aaudio_usage_t              mUsage           = AAUDIO_UNSPECIFIED;
573eaef9b9c7b3c6c4d4435ce5e32e9fe952a2f69fePhil Burk    aaudio_content_type_t       mContentType     = AAUDIO_UNSPECIFIED;
574eaef9b9c7b3c6c4d4435ce5e32e9fe952a2f69fePhil Burk    aaudio_input_preset_t       mInputPreset     = AAUDIO_UNSPECIFIED;
575e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk
5764e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk    int32_t                     mSessionId = AAUDIO_UNSPECIFIED;
5774e1af9fc9c1108d4514e92774f750bcd434dbbccPhil Burk
5783d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    // Sometimes the hardware is operating with a different format from the app.
5793d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    // Then we require conversion in AAudio.
5803d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk    aaudio_format_t             mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
5813d786cb59f5acd5484fa4fb968ba2de9dcf283adPhil Burk
582e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    // callback ----------------------------------
583e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
584e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_dataCallback   mDataCallbackProc = nullptr;  // external callback functions
585e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mDataCallbackUserData = nullptr;
586e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    int32_t                     mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
587134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    std::atomic<pid_t>          mDataCallbackThread{CALLBACK_THREAD_NONE};
588e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
589e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    AAudioStream_errorCallback  mErrorCallbackProc = nullptr;
590e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk    void                       *mErrorCallbackUserData = nullptr;
591134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    std::atomic<pid_t>          mErrorCallbackThread{CALLBACK_THREAD_NONE};
592e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk
593d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // background thread ----------------------------------
594134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    bool                        mHasThread = false;
595134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    pthread_t                   mThread; // initialized in constructor
596d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
597d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // These are set by the application thread and then read by the audio pthread.
598134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    std::atomic<int64_t>        mPeriodNanoseconds; // for tuning SCHED_FIFO threads
599d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk    // TODO make atomic?
600134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_audio_thread_proc_t  mThreadProc = nullptr;
601134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    void                       *mThreadArg = nullptr;
602134f197bf7e2785ed9de336dc8e22e870c2ba6bdPhil Burk    aaudio_result_t             mThreadRegistrationResult = AAUDIO_OK;
603d8bdcabbac30d48ed17fa76c83cb9ee95c290a07Phil Burk
604e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk};
605e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
6065ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk} /* namespace aaudio */
607e1ce491a25faf06fdeab00dd938515f71f28b095Phil Burk
6085ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#endif /* AAUDIO_AUDIOSTREAM_H */
609