blob: bc7ccdefbccd16fdd9db5bf70ab4b2966543d8f9 [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Phil Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
18#define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H
Phil Burk2355edb2016-12-26 13:54:02 -080019
Phil Burk71f35bb2017-04-13 16:05:07 -070020#include <assert.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <mutex>
Phil Burkdec33ab2017-01-17 14:48:16 -080022
Phil Burk0bd745e2020-10-17 18:20:01 +000023#include <android-base/thread_annotations.h>
Philip P. Moltmannbda45752020-07-17 16:41:18 -070024#include <media/AidlConversion.h>
Phil Burk97350f92017-07-21 15:59:44 -070025#include <media/AudioClient.h>
Phil Burk11e8d332017-05-24 09:59:02 -070026#include <utils/RefBase.h>
27
Phil Burk2355edb2016-12-26 13:54:02 -080028#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080029#include "binding/AudioEndpointParcelable.h"
30#include "binding/AAudioServiceMessage.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070031#include "binding/AAudioStreamRequest.h"
32#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080033#include "utility/AAudioUtilities.h"
Phil Burk97350f92017-07-21 15:59:44 -070034#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080035
jiabin2a594622021-10-14 00:32:25 +000036#include "AAudioCommandQueue.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080037#include "AAudioThread.h"
jiabin2a594622021-10-14 00:32:25 +000038#include "SharedRingBuffer.h"
39#include "TimestampScheduler.h"
Phil Burk523b3042017-09-13 13:03:08 -070040
41namespace android {
42 class AAudioService;
43}
Phil Burk2355edb2016-12-26 13:54:02 -080044
Phil Burk5ed503c2017-02-01 09:38:15 -080045namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080046
Phil Burk39f02dd2017-08-04 09:13:31 -070047class AAudioServiceEndpoint;
48
Phil Burk2355edb2016-12-26 13:54:02 -080049// We expect the queue to only have a few commands.
50// This should be way more than we need.
51#define QUEUE_UP_CAPACITY_COMMANDS (128)
52
Phil Burkc0c70e32017-02-09 13:18:38 -080053/**
Phil Burk39f02dd2017-08-04 09:13:31 -070054 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
55 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080056 */
57class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070058 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070059 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070060 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080061
62public:
Phil Burk19e990e2018-03-22 13:59:34 -070063 explicit AAudioServiceStreamBase(android::AAudioService &aAudioService);
Phil Burk39f02dd2017-08-04 09:13:31 -070064
jiabin613e6ae2022-12-21 20:20:11 +000065 ~AAudioServiceStreamBase() override;
Phil Burk2355edb2016-12-26 13:54:02 -080066
67 enum {
68 ILLEGAL_THREAD_ID = 0
69 };
70
Phil Burka5222e22017-07-28 13:31:14 -070071 static std::string dumpHeader();
72
73 // does not include EOL
74 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070075
Phil Burk2355edb2016-12-26 13:54:02 -080076 /**
77 * Open the device.
78 */
Phil Burk39f02dd2017-08-04 09:13:31 -070079 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080080
Phil Burka9876702020-04-20 18:16:15 -070081 // We log the CLOSE from the close() method. We needed this separate method to log the OPEN
82 // because we had to wait until we generated the handle.
83 void logOpen(aaudio_handle_t streamHandle);
84
Phil Burkda433d02021-04-12 16:10:51 +000085 aaudio_result_t close() EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -080086
87 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070088 * Start the flow of audio data.
89 *
90 * This is not guaranteed to be synchronous but it currently is.
91 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080092 */
Phil Burkda433d02021-04-12 16:10:51 +000093 aaudio_result_t start() EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -080094
95 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070096 * Stop the flow of data so that start() can resume without loss of data.
97 *
98 * This is not guaranteed to be synchronous but it currently is.
99 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
100 */
Phil Burkda433d02021-04-12 16:10:51 +0000101 aaudio_result_t pause() EXCLUDES(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700102
103 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700104 * Stop the flow of data after the currently queued data has finished playing.
105 *
106 * This is not guaranteed to be synchronous but it currently is.
107 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
108 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700109 */
Phil Burkda433d02021-04-12 16:10:51 +0000110 aaudio_result_t stop() EXCLUDES(mLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700111
Phil Burk2355edb2016-12-26 13:54:02 -0800112 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 * Discard any data held by the underlying HAL or Service.
114 *
115 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800116 */
Phil Burkda433d02021-04-12 16:10:51 +0000117 aaudio_result_t flush() EXCLUDES(mLock);
Phil Burk39f02dd2017-08-04 09:13:31 -0700118
jiabinf7f06152021-11-22 18:10:14 +0000119 /**
120 * Exit standby mode. The MMAP buffer will be reallocated.
121 */
122 aaudio_result_t exitStandby(AudioEndpointParcelable *parcelable) EXCLUDES(mLock);
123
jiabind1f1cb62020-03-24 11:57:57 -0700124 virtual aaudio_result_t startClient(const android::AudioClient& client,
125 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700126 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128 return AAUDIO_ERROR_UNAVAILABLE;
129 }
130
131 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700132 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700133 return AAUDIO_ERROR_UNAVAILABLE;
134 }
135
Phil Burkda433d02021-04-12 16:10:51 +0000136 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700137
Phil Burkda433d02021-04-12 16:10:51 +0000138 aaudio_result_t unregisterAudioThread(pid_t clientThreadId) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700139
Phil Burk11e8d332017-05-24 09:59:02 -0700140 bool isRunning() const {
141 return mState == AAUDIO_STREAM_STATE_STARTED;
142 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 /**
145 * Fill in a parcelable description of stream.
146 */
Phil Burkda433d02021-04-12 16:10:51 +0000147 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable) EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -0800148
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800150 mRegisteredClientThread = pid;
151 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800152
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800154 return mRegisteredClientThread;
155 }
156
Phil Burkc0c70e32017-02-09 13:18:38 -0800157 int32_t getFramesPerBurst() const {
158 return mFramesPerBurst;
159 }
160
Phil Burkc0c70e32017-02-09 13:18:38 -0800161 void run() override; // to implement Runnable
162
Phil Burkda433d02021-04-12 16:10:51 +0000163 void disconnect() EXCLUDES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800164
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 const android::AudioClient &getAudioClient() {
166 return mMmapClient;
167 }
168
Phil Burk2ac035f2017-06-23 14:51:14 -0700169 uid_t getOwnerUserId() const {
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000170 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_uid_t(
171 mMmapClient.attributionSource.uid));
Phil Burk2ac035f2017-06-23 14:51:14 -0700172 }
173
Phil Burkb63320a2017-06-30 10:28:20 -0700174 pid_t getOwnerProcessId() const {
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000175 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_pid_t(
176 mMmapClient.attributionSource.pid));
Phil Burkb63320a2017-06-30 10:28:20 -0700177 }
178
Phil Burk11e8d332017-05-24 09:59:02 -0700179 aaudio_handle_t getHandle() const {
180 return mHandle;
181 }
182 void setHandle(aaudio_handle_t handle) {
183 mHandle = handle;
184 }
185
Phil Burkbbd52862018-04-13 11:37:42 -0700186 audio_port_handle_t getPortHandle() const {
187 return mClientHandle;
188 }
189
Phil Burk5a26e662017-07-07 12:44:48 -0700190 aaudio_stream_state_t getState() const {
191 return mState;
192 }
193
Phil Burk39f02dd2017-08-04 09:13:31 -0700194 void onVolumeChanged(float volume);
195
Phil Burk23296382017-11-20 15:45:11 -0800196 /**
197 * Set false when the stream is started.
198 * Set true when data is first read from the stream.
199 * @param b
200 */
201 void setFlowing(bool b) {
202 mFlowing = b;
203 }
204
205 bool isFlowing() const {
206 return mFlowing;
207 }
208
Phil Burk94862522017-09-13 21:31:36 -0700209 /**
Phil Burk762365c2018-12-10 16:02:16 -0800210 * Set false when the stream should not longer be processed.
211 * This may be caused by a message queue overflow.
212 * Set true when stream is started.
213 * @param suspended
214 */
215 void setSuspended(bool suspended) {
216 mSuspended = suspended;
217 }
218
219 bool isSuspended() const {
220 return mSuspended;
221 }
222
Phil Burk94862522017-09-13 21:31:36 -0700223 bool isCloseNeeded() const {
224 return mCloseNeeded.load();
225 }
226
Phil Burk2fe718b2018-05-14 12:28:32 -0700227 /**
228 * Mark this stream as needing to be closed.
229 * Once marked for closing, it cannot be unmarked.
230 */
231 void markCloseNeeded() {
232 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700233 }
234
Phil Burk19e990e2018-03-22 13:59:34 -0700235 virtual const char *getTypeText() const { return "Base"; }
236
Phil Burk2355edb2016-12-26 13:54:02 -0800237protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700238
Phil Burk39f02dd2017-08-04 09:13:31 -0700239 /**
240 * Open the device.
241 */
242 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
243 aaudio_sharing_mode_t sharingMode);
244
jiabin2a594622021-10-14 00:32:25 +0000245 aaudio_result_t start_l() REQUIRES(mLock);
Phil Burk0bd745e2020-10-17 18:20:01 +0000246 virtual aaudio_result_t close_l() REQUIRES(mLock);
247 virtual aaudio_result_t pause_l() REQUIRES(mLock);
248 virtual aaudio_result_t stop_l() REQUIRES(mLock);
249 void disconnect_l() REQUIRES(mLock);
jiabin2a594622021-10-14 00:32:25 +0000250 aaudio_result_t flush_l() REQUIRES(mLock);
251
252 class RegisterAudioThreadParam : public AAudioCommandParam {
253 public:
254 RegisterAudioThreadParam(pid_t ownerPid, pid_t clientThreadId, int priority)
255 : AAudioCommandParam(), mOwnerPid(ownerPid),
256 mClientThreadId(clientThreadId), mPriority(priority) { }
jiabin613e6ae2022-12-21 20:20:11 +0000257 ~RegisterAudioThreadParam() override = default;
jiabin2a594622021-10-14 00:32:25 +0000258
259 pid_t mOwnerPid;
260 pid_t mClientThreadId;
261 int mPriority;
262 };
263 aaudio_result_t registerAudioThread_l(
264 pid_t ownerPid, pid_t clientThreadId, int priority) REQUIRES(mLock);
265
266 class UnregisterAudioThreadParam : public AAudioCommandParam {
267 public:
jiabin613e6ae2022-12-21 20:20:11 +0000268 explicit UnregisterAudioThreadParam(pid_t clientThreadId)
jiabin2a594622021-10-14 00:32:25 +0000269 : AAudioCommandParam(), mClientThreadId(clientThreadId) { }
jiabin613e6ae2022-12-21 20:20:11 +0000270 ~UnregisterAudioThreadParam() override = default;
jiabin2a594622021-10-14 00:32:25 +0000271
272 pid_t mClientThreadId;
273 };
274 aaudio_result_t unregisterAudioThread_l(pid_t clientThreadId) REQUIRES(mLock);
275
276 class GetDescriptionParam : public AAudioCommandParam {
277 public:
jiabin613e6ae2022-12-21 20:20:11 +0000278 explicit GetDescriptionParam(AudioEndpointParcelable* parcelable)
jiabin2a594622021-10-14 00:32:25 +0000279 : AAudioCommandParam(), mParcelable(parcelable) { }
jiabin613e6ae2022-12-21 20:20:11 +0000280 ~GetDescriptionParam() override = default;
jiabin2a594622021-10-14 00:32:25 +0000281
282 AudioEndpointParcelable* mParcelable;
283 };
284 aaudio_result_t getDescription_l(AudioEndpointParcelable* parcelable) REQUIRES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700285
286 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700287
Phil Burkbcc36742017-08-31 17:24:51 -0700288 /**
289 * Device specific startup.
290 * @return AAUDIO_OK or negative error.
291 */
292 virtual aaudio_result_t startDevice();
293
Phil Burkc0c70e32017-02-09 13:18:38 -0800294 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
295
jiabin2a594622021-10-14 00:32:25 +0000296 aaudio_result_t sendCurrentTimestamp_l() REQUIRES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800297
Phil Burk23296382017-11-20 15:45:11 -0800298 aaudio_result_t sendXRunCount(int32_t xRunCount);
299
Phil Burk940083c2017-07-17 17:00:02 -0700300 /**
301 * @param positionFrames
302 * @param timeNanos
303 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
304 */
jiabin2a594622021-10-14 00:32:25 +0000305 virtual aaudio_result_t getFreeRunningPosition_l(
306 int64_t *positionFrames, int64_t *timeNanos) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800307
jiabin2a594622021-10-14 00:32:25 +0000308 virtual aaudio_result_t getHardwareTimestamp_l(int64_t *positionFrames, int64_t *timeNanos) = 0;
Phil Burk97350f92017-07-21 15:59:44 -0700309
jiabin2a594622021-10-14 00:32:25 +0000310 virtual aaudio_result_t getAudioDataDescription_l(AudioEndpointParcelable* parcelable) = 0;
311
Phil Burkc0c70e32017-02-09 13:18:38 -0800312
Phil Burkec89b2e2017-06-20 15:05:06 -0700313 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800314
Phil Burkdb466142021-04-16 16:50:00 +0000315 bool isDisconnected_l() const REQUIRES(mLock) {
316 return mDisconnected;
317 }
318 void setDisconnected_l(bool flag) REQUIRES(mLock) {
319 mDisconnected = flag;
320 }
321
jiabinf7f06152021-11-22 18:10:14 +0000322 virtual aaudio_result_t standby_l() REQUIRES(mLock) {
Jason Lin20d04e32022-11-22 13:39:49 +0800323 return AAUDIO_ERROR_UNIMPLEMENTED;
jiabinf7f06152021-11-22 18:10:14 +0000324 }
325 class ExitStandbyParam : public AAudioCommandParam {
326 public:
jiabin613e6ae2022-12-21 20:20:11 +0000327 explicit ExitStandbyParam(AudioEndpointParcelable* parcelable)
jiabinf7f06152021-11-22 18:10:14 +0000328 : AAudioCommandParam(), mParcelable(parcelable) { }
jiabin613e6ae2022-12-21 20:20:11 +0000329 ~ExitStandbyParam() override = default;
jiabinf7f06152021-11-22 18:10:14 +0000330
331 AudioEndpointParcelable* mParcelable;
332 };
333 virtual aaudio_result_t exitStandby_l(
334 AudioEndpointParcelable* parcelable __unused) REQUIRES(mLock) {
335 return AAUDIO_ERROR_UNAVAILABLE;
336 }
337 bool isStandby_l() const REQUIRES(mLock) {
338 return mStandby;
339 }
340 void setStandby_l(bool standby) REQUIRES(mLock) {
341 mStandby = standby;
342 }
343
344 bool isIdle_l() const REQUIRES(mLock) {
345 return mState == AAUDIO_STREAM_STATE_OPEN || mState == AAUDIO_STREAM_STATE_PAUSED
346 || mState == AAUDIO_STREAM_STATE_STOPPED;
347 }
348
jiabinfc791ee2023-02-15 19:43:40 +0000349 virtual int64_t nextDataReportTime_l() REQUIRES(mLock) {
350 return std::numeric_limits<int64_t>::max();
351 }
352 virtual void reportData_l() REQUIRES(mLock) { return; }
353
Eric Laurentcb4dae22017-07-01 19:39:32 -0700354 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800355
Phil Burk39f02dd2017-08-04 09:13:31 -0700356 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000357 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800358
jiabin2a594622021-10-14 00:32:25 +0000359 enum : int32_t {
360 START,
361 PAUSE,
362 STOP,
363 FLUSH,
364 CLOSE,
365 DISCONNECT,
366 REGISTER_AUDIO_THREAD,
367 UNREGISTER_AUDIO_THREAD,
368 GET_DESCRIPTION,
jiabinf7f06152021-11-22 18:10:14 +0000369 EXIT_STANDBY,
jiabin2a594622021-10-14 00:32:25 +0000370 };
371 AAudioThread mCommandThread;
Phil Burk39f02dd2017-08-04 09:13:31 -0700372 std::atomic<bool> mThreadEnabled{false};
jiabin2a594622021-10-14 00:32:25 +0000373 AAudioCommandQueue mCommandQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800374
Eric Laurentcb4dae22017-07-01 19:39:32 -0700375 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700376 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700377 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700378 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
379
Phil Burka53ffa62018-10-10 16:21:37 -0700380 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700381
Phil Burk39f02dd2017-08-04 09:13:31 -0700382 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700383
384 // The mServiceEndpoint variable can be accessed by multiple threads.
385 // So we access it by locally promoting a weak pointer to a smart pointer,
386 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700387 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700388 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700389
Phil Burka9876702020-04-20 18:16:15 -0700390 std::string mMetricsId; // set once during open()
391
Phil Burk11e8d332017-05-24 09:59:02 -0700392private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700393
Phil Burk7ebbc112020-05-13 15:55:17 -0700394 aaudio_result_t stopTimestampThread();
395
396 /**
397 * Send a message to the client with an int64_t data value.
398 */
399 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
400 int64_t dataLong = 0);
401 /**
402 * Send a message to the client with a double data value.
403 */
404 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
405 double dataDouble);
406
jiabinba75f212021-12-07 20:06:30 +0000407 aaudio_result_t sendCommand(aaudio_command_opcode opCode,
408 std::shared_ptr<AAudioCommandParam> param = nullptr,
409 bool waitForReply = false,
410 int64_t timeoutNanos = 0);
411
412 aaudio_result_t closeAndClear();
413
Phil Burkf878a8d2019-03-29 17:23:00 -0700414 /**
415 * @return true if the queue is getting full.
416 */
417 bool isUpMessageQueueBusy();
418
Eric Laurentcb4dae22017-07-01 19:39:32 -0700419 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800420 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700421
Phil Burk0bd745e2020-10-17 18:20:01 +0000422 // This indicates that a stream that is being referenced by a binder call
423 // and needs to closed.
424 std::atomic<bool> mCloseNeeded{false}; // TODO remove
Phil Burk762365c2018-12-10 16:02:16 -0800425
426 // This indicate that a running stream should not be processed because of an error,
427 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
428 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700429
Phil Burkdb466142021-04-16 16:50:00 +0000430 bool mDisconnected GUARDED_BY(mLock) {false};
431
jiabinf7f06152021-11-22 18:10:14 +0000432 bool mStandby GUARDED_BY(mLock) = false;
433
Phil Burk0bd745e2020-10-17 18:20:01 +0000434protected:
Phil Burk7ebbc112020-05-13 15:55:17 -0700435 // Locking order is important.
Phil Burk0bd745e2020-10-17 18:20:01 +0000436 // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
jiabin2a594622021-10-14 00:32:25 +0000437 // The lock will be held by the command thread. All operations needing the lock must run from
438 // the command thread.
Phil Burk7ebbc112020-05-13 15:55:17 -0700439 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800440};
441
Phil Burk5ed503c2017-02-01 09:38:15 -0800442} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800443
Phil Burk5ed503c2017-02-01 09:38:15 -0800444#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H