blob: aa8e8cfa3264c9b594a4cce21f942f9923fc028a [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
Phil Burk5ed503c2017-02-01 09:38:15 -080065 virtual ~AAudioServiceStreamBase();
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
jiabind1f1cb62020-03-24 11:57:57 -0700119 virtual aaudio_result_t startClient(const android::AudioClient& client,
120 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700121 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700123 return AAUDIO_ERROR_UNAVAILABLE;
124 }
125
126 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700128 return AAUDIO_ERROR_UNAVAILABLE;
129 }
130
Phil Burkda433d02021-04-12 16:10:51 +0000131 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700132
Phil Burkda433d02021-04-12 16:10:51 +0000133 aaudio_result_t unregisterAudioThread(pid_t clientThreadId) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700134
Phil Burk11e8d332017-05-24 09:59:02 -0700135 bool isRunning() const {
136 return mState == AAUDIO_STREAM_STATE_STARTED;
137 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700138
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 /**
140 * Fill in a parcelable description of stream.
141 */
Phil Burkda433d02021-04-12 16:10:51 +0000142 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable) EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -0800143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800145 mRegisteredClientThread = pid;
146 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800147
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800149 return mRegisteredClientThread;
150 }
151
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 int32_t getFramesPerBurst() const {
153 return mFramesPerBurst;
154 }
155
Phil Burkc0c70e32017-02-09 13:18:38 -0800156 void run() override; // to implement Runnable
157
Phil Burkda433d02021-04-12 16:10:51 +0000158 void disconnect() EXCLUDES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800159
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 const android::AudioClient &getAudioClient() {
161 return mMmapClient;
162 }
163
Phil Burk2ac035f2017-06-23 14:51:14 -0700164 uid_t getOwnerUserId() const {
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000165 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_uid_t(
166 mMmapClient.attributionSource.uid));
Phil Burk2ac035f2017-06-23 14:51:14 -0700167 }
168
Phil Burkb63320a2017-06-30 10:28:20 -0700169 pid_t getOwnerProcessId() const {
Svet Ganov3e5f14f2021-05-13 22:51:08 +0000170 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_pid_t(
171 mMmapClient.attributionSource.pid));
Phil Burkb63320a2017-06-30 10:28:20 -0700172 }
173
Phil Burk11e8d332017-05-24 09:59:02 -0700174 aaudio_handle_t getHandle() const {
175 return mHandle;
176 }
177 void setHandle(aaudio_handle_t handle) {
178 mHandle = handle;
179 }
180
Phil Burkbbd52862018-04-13 11:37:42 -0700181 audio_port_handle_t getPortHandle() const {
182 return mClientHandle;
183 }
184
Phil Burk5a26e662017-07-07 12:44:48 -0700185 aaudio_stream_state_t getState() const {
186 return mState;
187 }
188
Phil Burk39f02dd2017-08-04 09:13:31 -0700189 void onVolumeChanged(float volume);
190
Phil Burk23296382017-11-20 15:45:11 -0800191 /**
192 * Set false when the stream is started.
193 * Set true when data is first read from the stream.
194 * @param b
195 */
196 void setFlowing(bool b) {
197 mFlowing = b;
198 }
199
200 bool isFlowing() const {
201 return mFlowing;
202 }
203
Phil Burk94862522017-09-13 21:31:36 -0700204 /**
Phil Burk762365c2018-12-10 16:02:16 -0800205 * Set false when the stream should not longer be processed.
206 * This may be caused by a message queue overflow.
207 * Set true when stream is started.
208 * @param suspended
209 */
210 void setSuspended(bool suspended) {
211 mSuspended = suspended;
212 }
213
214 bool isSuspended() const {
215 return mSuspended;
216 }
217
Phil Burk94862522017-09-13 21:31:36 -0700218 bool isCloseNeeded() const {
219 return mCloseNeeded.load();
220 }
221
Phil Burk2fe718b2018-05-14 12:28:32 -0700222 /**
223 * Mark this stream as needing to be closed.
224 * Once marked for closing, it cannot be unmarked.
225 */
226 void markCloseNeeded() {
227 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700228 }
229
Phil Burk19e990e2018-03-22 13:59:34 -0700230 virtual const char *getTypeText() const { return "Base"; }
231
Phil Burk2355edb2016-12-26 13:54:02 -0800232protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700233
Phil Burk39f02dd2017-08-04 09:13:31 -0700234 /**
235 * Open the device.
236 */
237 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
238 aaudio_sharing_mode_t sharingMode);
239
jiabin2a594622021-10-14 00:32:25 +0000240 aaudio_result_t start_l() REQUIRES(mLock);
Phil Burk0bd745e2020-10-17 18:20:01 +0000241 virtual aaudio_result_t close_l() REQUIRES(mLock);
242 virtual aaudio_result_t pause_l() REQUIRES(mLock);
243 virtual aaudio_result_t stop_l() REQUIRES(mLock);
244 void disconnect_l() REQUIRES(mLock);
jiabin2a594622021-10-14 00:32:25 +0000245 aaudio_result_t flush_l() REQUIRES(mLock);
246
247 class RegisterAudioThreadParam : public AAudioCommandParam {
248 public:
249 RegisterAudioThreadParam(pid_t ownerPid, pid_t clientThreadId, int priority)
250 : AAudioCommandParam(), mOwnerPid(ownerPid),
251 mClientThreadId(clientThreadId), mPriority(priority) { }
252 ~RegisterAudioThreadParam() = default;
253
254 pid_t mOwnerPid;
255 pid_t mClientThreadId;
256 int mPriority;
257 };
258 aaudio_result_t registerAudioThread_l(
259 pid_t ownerPid, pid_t clientThreadId, int priority) REQUIRES(mLock);
260
261 class UnregisterAudioThreadParam : public AAudioCommandParam {
262 public:
263 UnregisterAudioThreadParam(pid_t clientThreadId)
264 : AAudioCommandParam(), mClientThreadId(clientThreadId) { }
265 ~UnregisterAudioThreadParam() = default;
266
267 pid_t mClientThreadId;
268 };
269 aaudio_result_t unregisterAudioThread_l(pid_t clientThreadId) REQUIRES(mLock);
270
271 class GetDescriptionParam : public AAudioCommandParam {
272 public:
273 GetDescriptionParam(AudioEndpointParcelable* parcelable)
274 : AAudioCommandParam(), mParcelable(parcelable) { }
275 ~GetDescriptionParam() = default;
276
277 AudioEndpointParcelable* mParcelable;
278 };
279 aaudio_result_t getDescription_l(AudioEndpointParcelable* parcelable) REQUIRES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700280
281 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700282
Phil Burkbcc36742017-08-31 17:24:51 -0700283 /**
284 * Device specific startup.
285 * @return AAUDIO_OK or negative error.
286 */
287 virtual aaudio_result_t startDevice();
288
Phil Burkc0c70e32017-02-09 13:18:38 -0800289 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
290
jiabin2a594622021-10-14 00:32:25 +0000291 aaudio_result_t sendCurrentTimestamp_l() REQUIRES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800292
Phil Burk23296382017-11-20 15:45:11 -0800293 aaudio_result_t sendXRunCount(int32_t xRunCount);
294
Phil Burk940083c2017-07-17 17:00:02 -0700295 /**
296 * @param positionFrames
297 * @param timeNanos
298 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
299 */
jiabin2a594622021-10-14 00:32:25 +0000300 virtual aaudio_result_t getFreeRunningPosition_l(
301 int64_t *positionFrames, int64_t *timeNanos) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800302
jiabin2a594622021-10-14 00:32:25 +0000303 virtual aaudio_result_t getHardwareTimestamp_l(int64_t *positionFrames, int64_t *timeNanos) = 0;
Phil Burk97350f92017-07-21 15:59:44 -0700304
jiabin2a594622021-10-14 00:32:25 +0000305 virtual aaudio_result_t getAudioDataDescription_l(AudioEndpointParcelable* parcelable) = 0;
306
Phil Burkc0c70e32017-02-09 13:18:38 -0800307
Phil Burkec89b2e2017-06-20 15:05:06 -0700308 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800309
Phil Burkdb466142021-04-16 16:50:00 +0000310 bool isDisconnected_l() const REQUIRES(mLock) {
311 return mDisconnected;
312 }
313 void setDisconnected_l(bool flag) REQUIRES(mLock) {
314 mDisconnected = flag;
315 }
316
Eric Laurentcb4dae22017-07-01 19:39:32 -0700317 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800318
Phil Burk39f02dd2017-08-04 09:13:31 -0700319 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000320 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800321
jiabin2a594622021-10-14 00:32:25 +0000322 enum : int32_t {
323 START,
324 PAUSE,
325 STOP,
326 FLUSH,
327 CLOSE,
328 DISCONNECT,
329 REGISTER_AUDIO_THREAD,
330 UNREGISTER_AUDIO_THREAD,
331 GET_DESCRIPTION,
332 };
333 AAudioThread mCommandThread;
Phil Burk39f02dd2017-08-04 09:13:31 -0700334 std::atomic<bool> mThreadEnabled{false};
jiabin2a594622021-10-14 00:32:25 +0000335 AAudioCommandQueue mCommandQueue;
Phil Burkc0c70e32017-02-09 13:18:38 -0800336
Eric Laurentcb4dae22017-07-01 19:39:32 -0700337 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700338 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700339 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700340 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
341
Phil Burka53ffa62018-10-10 16:21:37 -0700342 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700343
Phil Burk39f02dd2017-08-04 09:13:31 -0700344 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700345
346 // The mServiceEndpoint variable can be accessed by multiple threads.
347 // So we access it by locally promoting a weak pointer to a smart pointer,
348 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700349 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700350 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700351
Phil Burka9876702020-04-20 18:16:15 -0700352 std::string mMetricsId; // set once during open()
353
Phil Burk11e8d332017-05-24 09:59:02 -0700354private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700355
Phil Burk7ebbc112020-05-13 15:55:17 -0700356 aaudio_result_t stopTimestampThread();
357
358 /**
359 * Send a message to the client with an int64_t data value.
360 */
361 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
362 int64_t dataLong = 0);
363 /**
364 * Send a message to the client with a double data value.
365 */
366 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
367 double dataDouble);
368
Phil Burkf878a8d2019-03-29 17:23:00 -0700369 /**
370 * @return true if the queue is getting full.
371 */
372 bool isUpMessageQueueBusy();
373
Eric Laurentcb4dae22017-07-01 19:39:32 -0700374 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800375 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700376
Phil Burk0bd745e2020-10-17 18:20:01 +0000377 // This indicates that a stream that is being referenced by a binder call
378 // and needs to closed.
379 std::atomic<bool> mCloseNeeded{false}; // TODO remove
Phil Burk762365c2018-12-10 16:02:16 -0800380
381 // This indicate that a running stream should not be processed because of an error,
382 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
383 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700384
Phil Burkdb466142021-04-16 16:50:00 +0000385 bool mDisconnected GUARDED_BY(mLock) {false};
386
Phil Burk0bd745e2020-10-17 18:20:01 +0000387protected:
Phil Burk7ebbc112020-05-13 15:55:17 -0700388 // Locking order is important.
Phil Burk0bd745e2020-10-17 18:20:01 +0000389 // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
jiabin2a594622021-10-14 00:32:25 +0000390 // The lock will be held by the command thread. All operations needing the lock must run from
391 // the command thread.
Phil Burk7ebbc112020-05-13 15:55:17 -0700392 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800393};
394
Phil Burk5ed503c2017-02-01 09:38:15 -0800395} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800396
Phil Burk5ed503c2017-02-01 09:38:15 -0800397#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H