blob: c42df0f4533003a8f5dabe4bdbdf7a0960028642 [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 {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700163 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_uid_t(mMmapClient.identity.uid));
Phil Burk2ac035f2017-06-23 14:51:14 -0700164 }
165
Phil Burkb63320a2017-06-30 10:28:20 -0700166 pid_t getOwnerProcessId() const {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700167 return VALUE_OR_FATAL(android::aidl2legacy_int32_t_pid_t(mMmapClient.identity.pid));
Phil Burkb63320a2017-06-30 10:28:20 -0700168 }
169
Phil Burk11e8d332017-05-24 09:59:02 -0700170 aaudio_handle_t getHandle() const {
171 return mHandle;
172 }
173 void setHandle(aaudio_handle_t handle) {
174 mHandle = handle;
175 }
176
Phil Burkbbd52862018-04-13 11:37:42 -0700177 audio_port_handle_t getPortHandle() const {
178 return mClientHandle;
179 }
180
Phil Burk5a26e662017-07-07 12:44:48 -0700181 aaudio_stream_state_t getState() const {
182 return mState;
183 }
184
Phil Burk39f02dd2017-08-04 09:13:31 -0700185 void onVolumeChanged(float volume);
186
Phil Burk23296382017-11-20 15:45:11 -0800187 /**
188 * Set false when the stream is started.
189 * Set true when data is first read from the stream.
190 * @param b
191 */
192 void setFlowing(bool b) {
193 mFlowing = b;
194 }
195
196 bool isFlowing() const {
197 return mFlowing;
198 }
199
Phil Burk94862522017-09-13 21:31:36 -0700200 /**
Phil Burk762365c2018-12-10 16:02:16 -0800201 * Set false when the stream should not longer be processed.
202 * This may be caused by a message queue overflow.
203 * Set true when stream is started.
204 * @param suspended
205 */
206 void setSuspended(bool suspended) {
207 mSuspended = suspended;
208 }
209
210 bool isSuspended() const {
211 return mSuspended;
212 }
213
Phil Burk94862522017-09-13 21:31:36 -0700214 bool isCloseNeeded() const {
215 return mCloseNeeded.load();
216 }
217
Phil Burk2fe718b2018-05-14 12:28:32 -0700218 /**
219 * Mark this stream as needing to be closed.
220 * Once marked for closing, it cannot be unmarked.
221 */
222 void markCloseNeeded() {
223 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700224 }
225
Phil Burk19e990e2018-03-22 13:59:34 -0700226 virtual const char *getTypeText() const { return "Base"; }
227
Phil Burk2355edb2016-12-26 13:54:02 -0800228protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700229
Phil Burk39f02dd2017-08-04 09:13:31 -0700230 /**
231 * Open the device.
232 */
233 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
234 aaudio_sharing_mode_t sharingMode);
235
Phil Burk0bd745e2020-10-17 18:20:01 +0000236 virtual aaudio_result_t close_l() REQUIRES(mLock);
237 virtual aaudio_result_t pause_l() REQUIRES(mLock);
238 virtual aaudio_result_t stop_l() REQUIRES(mLock);
239 void disconnect_l() REQUIRES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700240
241 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700242
Phil Burkbcc36742017-08-31 17:24:51 -0700243 /**
244 * Device specific startup.
245 * @return AAUDIO_OK or negative error.
246 */
247 virtual aaudio_result_t startDevice();
248
Phil Burkc0c70e32017-02-09 13:18:38 -0800249 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
250
Phil Burkda433d02021-04-12 16:10:51 +0000251 aaudio_result_t sendCurrentTimestamp() EXCLUDES(mLock);
Phil Burkc0c70e32017-02-09 13:18:38 -0800252
Phil Burk23296382017-11-20 15:45:11 -0800253 aaudio_result_t sendXRunCount(int32_t xRunCount);
254
Phil Burk940083c2017-07-17 17:00:02 -0700255 /**
256 * @param positionFrames
257 * @param timeNanos
258 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
259 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800260 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
261
Phil Burk97350f92017-07-21 15:59:44 -0700262 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
263
Phil Burk523b3042017-09-13 13:03:08 -0700264 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800265
Phil Burkec89b2e2017-06-20 15:05:06 -0700266 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800267
Phil Burkdb466142021-04-16 16:50:00 +0000268 bool isDisconnected_l() const REQUIRES(mLock) {
269 return mDisconnected;
270 }
271 void setDisconnected_l(bool flag) REQUIRES(mLock) {
272 mDisconnected = flag;
273 }
274
Eric Laurentcb4dae22017-07-01 19:39:32 -0700275 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800276
Phil Burk39f02dd2017-08-04 09:13:31 -0700277 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000278 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800279
Phil Burkbcc36742017-08-31 17:24:51 -0700280 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800281 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700282 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800283
Eric Laurentcb4dae22017-07-01 19:39:32 -0700284 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700285 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700286 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700287 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
288
Phil Burka53ffa62018-10-10 16:21:37 -0700289 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700290
Phil Burk39f02dd2017-08-04 09:13:31 -0700291 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700292
293 // The mServiceEndpoint variable can be accessed by multiple threads.
294 // So we access it by locally promoting a weak pointer to a smart pointer,
295 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700296 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700297 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700298
Phil Burka9876702020-04-20 18:16:15 -0700299 std::string mMetricsId; // set once during open()
300
Phil Burk11e8d332017-05-24 09:59:02 -0700301private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700302
Phil Burk7ebbc112020-05-13 15:55:17 -0700303 aaudio_result_t stopTimestampThread();
304
305 /**
306 * Send a message to the client with an int64_t data value.
307 */
308 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
309 int64_t dataLong = 0);
310 /**
311 * Send a message to the client with a double data value.
312 */
313 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
314 double dataDouble);
315
Phil Burkf878a8d2019-03-29 17:23:00 -0700316 /**
317 * @return true if the queue is getting full.
318 */
319 bool isUpMessageQueueBusy();
320
Eric Laurentcb4dae22017-07-01 19:39:32 -0700321 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800322 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700323
Phil Burk0bd745e2020-10-17 18:20:01 +0000324 // This indicates that a stream that is being referenced by a binder call
325 // and needs to closed.
326 std::atomic<bool> mCloseNeeded{false}; // TODO remove
Phil Burk762365c2018-12-10 16:02:16 -0800327
328 // This indicate that a running stream should not be processed because of an error,
329 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
330 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700331
Phil Burkdb466142021-04-16 16:50:00 +0000332 bool mDisconnected GUARDED_BY(mLock) {false};
333
Phil Burk0bd745e2020-10-17 18:20:01 +0000334protected:
Phil Burk7ebbc112020-05-13 15:55:17 -0700335 // Locking order is important.
Phil Burk0bd745e2020-10-17 18:20:01 +0000336 // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
Phil Burk7ebbc112020-05-13 15:55:17 -0700337 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800338};
339
Phil Burk5ed503c2017-02-01 09:38:15 -0800340} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800341
Phil Burk5ed503c2017-02-01 09:38:15 -0800342#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H