blob: 976996d226e46baf920e448ce9d456519ad38dd4 [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
Phil Burk2355edb2016-12-26 13:54:02 -080036#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080037#include "AAudioThread.h"
Phil Burk523b3042017-09-13 13:03:08 -070038
39namespace android {
40 class AAudioService;
41}
Phil Burk2355edb2016-12-26 13:54:02 -080042
Phil Burk5ed503c2017-02-01 09:38:15 -080043namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080044
Phil Burk39f02dd2017-08-04 09:13:31 -070045class AAudioServiceEndpoint;
46
Phil Burk2355edb2016-12-26 13:54:02 -080047// We expect the queue to only have a few commands.
48// This should be way more than we need.
49#define QUEUE_UP_CAPACITY_COMMANDS (128)
50
Phil Burkc0c70e32017-02-09 13:18:38 -080051/**
Phil Burk39f02dd2017-08-04 09:13:31 -070052 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
53 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080054 */
55class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070056 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070057 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070058 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080059
60public:
Phil Burk19e990e2018-03-22 13:59:34 -070061 explicit AAudioServiceStreamBase(android::AAudioService &aAudioService);
Phil Burk39f02dd2017-08-04 09:13:31 -070062
Phil Burk5ed503c2017-02-01 09:38:15 -080063 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080064
65 enum {
66 ILLEGAL_THREAD_ID = 0
67 };
68
Phil Burka5222e22017-07-28 13:31:14 -070069 static std::string dumpHeader();
70
71 // does not include EOL
72 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070073
Phil Burk2355edb2016-12-26 13:54:02 -080074 /**
75 * Open the device.
76 */
Phil Burk39f02dd2017-08-04 09:13:31 -070077 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080078
Phil Burka9876702020-04-20 18:16:15 -070079 // We log the CLOSE from the close() method. We needed this separate method to log the OPEN
80 // because we had to wait until we generated the handle.
81 void logOpen(aaudio_handle_t streamHandle);
82
Phil Burkda433d02021-04-12 16:10:51 +000083 aaudio_result_t close() EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -080084
85 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070086 * Start the flow of audio data.
87 *
88 * This is not guaranteed to be synchronous but it currently is.
89 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080090 */
Phil Burkda433d02021-04-12 16:10:51 +000091 aaudio_result_t start() EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -080092
93 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070094 * Stop the flow of data so that start() can resume without loss of data.
95 *
96 * This is not guaranteed to be synchronous but it currently is.
97 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
98 */
Phil Burkda433d02021-04-12 16:10:51 +000099 aaudio_result_t pause() EXCLUDES(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700100
101 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700102 * Stop the flow of data after the currently queued data has finished playing.
103 *
104 * This is not guaranteed to be synchronous but it currently is.
105 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
106 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700107 */
Phil Burkda433d02021-04-12 16:10:51 +0000108 aaudio_result_t stop() EXCLUDES(mLock);
Phil Burk98d6d922017-07-06 11:52:45 -0700109
Phil Burk2355edb2016-12-26 13:54:02 -0800110 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 * Discard any data held by the underlying HAL or Service.
112 *
113 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800114 */
Phil Burkda433d02021-04-12 16:10:51 +0000115 aaudio_result_t flush() EXCLUDES(mLock);
Phil Burk39f02dd2017-08-04 09:13:31 -0700116
jiabind1f1cb62020-03-24 11:57:57 -0700117 virtual aaudio_result_t startClient(const android::AudioClient& client,
118 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700121 return AAUDIO_ERROR_UNAVAILABLE;
122 }
123
124 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700126 return AAUDIO_ERROR_UNAVAILABLE;
127 }
128
Phil Burkda433d02021-04-12 16:10:51 +0000129 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700130
Phil Burkda433d02021-04-12 16:10:51 +0000131 aaudio_result_t unregisterAudioThread(pid_t clientThreadId) EXCLUDES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700132
Phil Burk11e8d332017-05-24 09:59:02 -0700133 bool isRunning() const {
134 return mState == AAUDIO_STREAM_STATE_STARTED;
135 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700136
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 /**
138 * Fill in a parcelable description of stream.
139 */
Phil Burkda433d02021-04-12 16:10:51 +0000140 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable) EXCLUDES(mLock);
Phil Burk2355edb2016-12-26 13:54:02 -0800141
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800143 mRegisteredClientThread = pid;
144 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800145
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800147 return mRegisteredClientThread;
148 }
149
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 int32_t getFramesPerBurst() const {
151 return mFramesPerBurst;
152 }
153
Phil Burkc0c70e32017-02-09 13:18:38 -0800154 void run() override; // to implement Runnable
155
Phil Burkda433d02021-04-12 16:10:51 +0000156 void disconnect() EXCLUDES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800157
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 const android::AudioClient &getAudioClient() {
159 return mMmapClient;
160 }
161
Phil Burk2ac035f2017-06-23 14:51:14 -0700162 uid_t getOwnerUserId() const {
Svet Ganov33761132021-05-13 22:51:08 +0000163 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_uid_t(
164 mMmapClient.attributionSource.uid));
Phil Burk2ac035f2017-06-23 14:51:14 -0700165 }
166
Phil Burkb63320a2017-06-30 10:28:20 -0700167 pid_t getOwnerProcessId() const {
Svet Ganov33761132021-05-13 22:51:08 +0000168 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_pid_t(
169 mMmapClient.attributionSource.pid));
Phil Burkb63320a2017-06-30 10:28:20 -0700170 }
171
Phil Burk11e8d332017-05-24 09:59:02 -0700172 aaudio_handle_t getHandle() const {
173 return mHandle;
174 }
175 void setHandle(aaudio_handle_t handle) {
176 mHandle = handle;
177 }
178
Phil Burkbbd52862018-04-13 11:37:42 -0700179 audio_port_handle_t getPortHandle() const {
180 return mClientHandle;
181 }
182
Phil Burk5a26e662017-07-07 12:44:48 -0700183 aaudio_stream_state_t getState() const {
184 return mState;
185 }
186
Phil Burk39f02dd2017-08-04 09:13:31 -0700187 void onVolumeChanged(float volume);
188
Phil Burk23296382017-11-20 15:45:11 -0800189 /**
190 * Set false when the stream is started.
191 * Set true when data is first read from the stream.
192 * @param b
193 */
194 void setFlowing(bool b) {
195 mFlowing = b;
196 }
197
198 bool isFlowing() const {
199 return mFlowing;
200 }
201
Phil Burk94862522017-09-13 21:31:36 -0700202 /**
Phil Burk762365c2018-12-10 16:02:16 -0800203 * Set false when the stream should not longer be processed.
204 * This may be caused by a message queue overflow.
205 * Set true when stream is started.
206 * @param suspended
207 */
208 void setSuspended(bool suspended) {
209 mSuspended = suspended;
210 }
211
212 bool isSuspended() const {
213 return mSuspended;
214 }
215
Phil Burk94862522017-09-13 21:31:36 -0700216 bool isCloseNeeded() const {
217 return mCloseNeeded.load();
218 }
219
Phil Burk2fe718b2018-05-14 12:28:32 -0700220 /**
221 * Mark this stream as needing to be closed.
222 * Once marked for closing, it cannot be unmarked.
223 */
224 void markCloseNeeded() {
225 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700226 }
227
Phil Burk19e990e2018-03-22 13:59:34 -0700228 virtual const char *getTypeText() const { return "Base"; }
229
Phil Burk2355edb2016-12-26 13:54:02 -0800230protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700231
Phil Burk39f02dd2017-08-04 09:13:31 -0700232 /**
233 * Open the device.
234 */
235 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
236 aaudio_sharing_mode_t sharingMode);
237
Phil Burk0bd745e2020-10-17 18:20:01 +0000238 virtual aaudio_result_t close_l() REQUIRES(mLock);
239 virtual aaudio_result_t pause_l() REQUIRES(mLock);
240 virtual aaudio_result_t stop_l() REQUIRES(mLock);
241 void disconnect_l() REQUIRES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700242
243 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700244
Phil Burkbcc36742017-08-31 17:24:51 -0700245 /**
246 * Device specific startup.
247 * @return AAUDIO_OK or negative error.
248 */
249 virtual aaudio_result_t startDevice();
250
Phil Burkc0c70e32017-02-09 13:18:38 -0800251 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
252
Phil Burkda433d02021-04-12 16:10:51 +0000253 aaudio_result_t sendCurrentTimestamp() EXCLUDES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800254
Phil Burk23296382017-11-20 15:45:11 -0800255 aaudio_result_t sendXRunCount(int32_t xRunCount);
256
Phil Burk940083c2017-07-17 17:00:02 -0700257 /**
258 * @param positionFrames
259 * @param timeNanos
260 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
261 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800262 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
263
Phil Burk97350f92017-07-21 15:59:44 -0700264 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
265
Phil Burk523b3042017-09-13 13:03:08 -0700266 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800267
Phil Burkec89b2e2017-06-20 15:05:06 -0700268 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800269
Phil Burkdb466142021-04-16 16:50:00 +0000270 bool isDisconnected_l() const REQUIRES(mLock) {
271 return mDisconnected;
272 }
273 void setDisconnected_l(bool flag) REQUIRES(mLock) {
274 mDisconnected = flag;
275 }
276
Eric Laurentcb4dae22017-07-01 19:39:32 -0700277 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800278
Phil Burk39f02dd2017-08-04 09:13:31 -0700279 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000280 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800281
Phil Burkbcc36742017-08-31 17:24:51 -0700282 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800283 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700284 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800285
Eric Laurentcb4dae22017-07-01 19:39:32 -0700286 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700287 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700288 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700289 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
290
Phil Burka53ffa62018-10-10 16:21:37 -0700291 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700292
Phil Burk39f02dd2017-08-04 09:13:31 -0700293 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700294
295 // The mServiceEndpoint variable can be accessed by multiple threads.
296 // So we access it by locally promoting a weak pointer to a smart pointer,
297 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700298 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700299 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700300
Phil Burka9876702020-04-20 18:16:15 -0700301 std::string mMetricsId; // set once during open()
302
Phil Burk11e8d332017-05-24 09:59:02 -0700303private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700304
Phil Burk7ebbc112020-05-13 15:55:17 -0700305 aaudio_result_t stopTimestampThread();
306
307 /**
308 * Send a message to the client with an int64_t data value.
309 */
310 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
311 int64_t dataLong = 0);
312 /**
313 * Send a message to the client with a double data value.
314 */
315 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
316 double dataDouble);
317
Phil Burkf878a8d2019-03-29 17:23:00 -0700318 /**
319 * @return true if the queue is getting full.
320 */
321 bool isUpMessageQueueBusy();
322
Eric Laurentcb4dae22017-07-01 19:39:32 -0700323 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800324 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700325
Phil Burk0bd745e2020-10-17 18:20:01 +0000326 // This indicates that a stream that is being referenced by a binder call
327 // and needs to closed.
328 std::atomic<bool> mCloseNeeded{false}; // TODO remove
Phil Burk762365c2018-12-10 16:02:16 -0800329
330 // This indicate that a running stream should not be processed because of an error,
331 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
332 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700333
Phil Burkdb466142021-04-16 16:50:00 +0000334 bool mDisconnected GUARDED_BY(mLock) {false};
335
Phil Burk0bd745e2020-10-17 18:20:01 +0000336protected:
Phil Burk7ebbc112020-05-13 15:55:17 -0700337 // Locking order is important.
Phil Burk0bd745e2020-10-17 18:20:01 +0000338 // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
Phil Burk7ebbc112020-05-13 15:55:17 -0700339 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800340};
341
Phil Burk5ed503c2017-02-01 09:38:15 -0800342} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800343
Phil Burk5ed503c2017-02-01 09:38:15 -0800344#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H