blob: f9efc2a8451af1c5c86eefa3c8d9c089c38632fd [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 Burk97350f92017-07-21 15:59:44 -070023#include <media/AudioClient.h>
Phil Burk11e8d332017-05-24 09:59:02 -070024#include <utils/RefBase.h>
25
Phil Burk2355edb2016-12-26 13:54:02 -080026#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include "binding/AudioEndpointParcelable.h"
28#include "binding/AAudioServiceMessage.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070029#include "binding/AAudioStreamRequest.h"
30#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080031#include "utility/AAudioUtilities.h"
Phil Burk97350f92017-07-21 15:59:44 -070032#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080033
Phil Burk2355edb2016-12-26 13:54:02 -080034#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080035#include "AAudioThread.h"
Phil Burk523b3042017-09-13 13:03:08 -070036
37namespace android {
38 class AAudioService;
39}
Phil Burk2355edb2016-12-26 13:54:02 -080040
Phil Burk5ed503c2017-02-01 09:38:15 -080041namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080042
Phil Burk39f02dd2017-08-04 09:13:31 -070043class AAudioServiceEndpoint;
44
Phil Burk2355edb2016-12-26 13:54:02 -080045// We expect the queue to only have a few commands.
46// This should be way more than we need.
47#define QUEUE_UP_CAPACITY_COMMANDS (128)
48
Phil Burkc0c70e32017-02-09 13:18:38 -080049/**
Phil Burk39f02dd2017-08-04 09:13:31 -070050 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
51 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080052 */
53class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070054 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070055 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070056 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080057
58public:
Phil Burk19e990e2018-03-22 13:59:34 -070059 explicit AAudioServiceStreamBase(android::AAudioService &aAudioService);
Phil Burk39f02dd2017-08-04 09:13:31 -070060
Phil Burk5ed503c2017-02-01 09:38:15 -080061 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080062
63 enum {
64 ILLEGAL_THREAD_ID = 0
65 };
66
Phil Burka5222e22017-07-28 13:31:14 -070067 static std::string dumpHeader();
68
69 // does not include EOL
70 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070071
Phil Burk2355edb2016-12-26 13:54:02 -080072 /**
73 * Open the device.
74 */
Phil Burk39f02dd2017-08-04 09:13:31 -070075 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080076
Phil Burka9876702020-04-20 18:16:15 -070077 // We log the CLOSE from the close() method. We needed this separate method to log the OPEN
78 // because we had to wait until we generated the handle.
79 void logOpen(aaudio_handle_t streamHandle);
80
Phil Burk7ebbc112020-05-13 15:55:17 -070081 aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080082
83 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070084 * Start the flow of audio data.
85 *
86 * This is not guaranteed to be synchronous but it currently is.
87 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080088 */
Phil Burk7ebbc112020-05-13 15:55:17 -070089 aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080090
91 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070092 * Stop the flow of data so that start() can resume without loss of data.
93 *
94 * This is not guaranteed to be synchronous but it currently is.
95 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
96 */
Phil Burk7ebbc112020-05-13 15:55:17 -070097 aaudio_result_t pause();
Phil Burk71f35bb2017-04-13 16:05:07 -070098
99 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700100 * Stop the flow of data after the currently queued data has finished playing.
101 *
102 * This is not guaranteed to be synchronous but it currently is.
103 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
104 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700105 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700106 aaudio_result_t stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700107
Phil Burk2355edb2016-12-26 13:54:02 -0800108 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700109 * Discard any data held by the underlying HAL or Service.
110 *
111 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800112 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700113 aaudio_result_t flush();
Phil Burk39f02dd2017-08-04 09:13:31 -0700114
jiabind1f1cb62020-03-24 11:57:57 -0700115 virtual aaudio_result_t startClient(const android::AudioClient& client,
116 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700117 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700119 return AAUDIO_ERROR_UNAVAILABLE;
120 }
121
122 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700123 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700124 return AAUDIO_ERROR_UNAVAILABLE;
125 }
126
Phil Burk7ebbc112020-05-13 15:55:17 -0700127 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority);
128
129 aaudio_result_t unregisterAudioThread(pid_t clientThreadId);
130
Phil Burk11e8d332017-05-24 09:59:02 -0700131 bool isRunning() const {
132 return mState == AAUDIO_STREAM_STATE_STARTED;
133 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700134
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 /**
136 * Fill in a parcelable description of stream.
137 */
138 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800139
Phil Burkc0c70e32017-02-09 13:18:38 -0800140 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800141 mRegisteredClientThread = pid;
142 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800145 return mRegisteredClientThread;
146 }
147
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 int32_t getFramesPerBurst() const {
149 return mFramesPerBurst;
150 }
151
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 void run() override; // to implement Runnable
153
Phil Burk5ef003b2017-06-30 11:43:37 -0700154 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800155
Phil Burk39f02dd2017-08-04 09:13:31 -0700156 const android::AudioClient &getAudioClient() {
157 return mMmapClient;
158 }
159
Phil Burk2ac035f2017-06-23 14:51:14 -0700160 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700161 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700162 }
163
Phil Burkb63320a2017-06-30 10:28:20 -0700164 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700165 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700166 }
167
Phil Burk11e8d332017-05-24 09:59:02 -0700168 aaudio_handle_t getHandle() const {
169 return mHandle;
170 }
171 void setHandle(aaudio_handle_t handle) {
172 mHandle = handle;
173 }
174
Phil Burkbbd52862018-04-13 11:37:42 -0700175 audio_port_handle_t getPortHandle() const {
176 return mClientHandle;
177 }
178
Phil Burk5a26e662017-07-07 12:44:48 -0700179 aaudio_stream_state_t getState() const {
180 return mState;
181 }
182
Phil Burk39f02dd2017-08-04 09:13:31 -0700183 void onVolumeChanged(float volume);
184
Phil Burk23296382017-11-20 15:45:11 -0800185 /**
186 * Set false when the stream is started.
187 * Set true when data is first read from the stream.
188 * @param b
189 */
190 void setFlowing(bool b) {
191 mFlowing = b;
192 }
193
194 bool isFlowing() const {
195 return mFlowing;
196 }
197
Phil Burk94862522017-09-13 21:31:36 -0700198 /**
Phil Burk762365c2018-12-10 16:02:16 -0800199 * Set false when the stream should not longer be processed.
200 * This may be caused by a message queue overflow.
201 * Set true when stream is started.
202 * @param suspended
203 */
204 void setSuspended(bool suspended) {
205 mSuspended = suspended;
206 }
207
208 bool isSuspended() const {
209 return mSuspended;
210 }
211
212 /**
Phil Burk94862522017-09-13 21:31:36 -0700213 * Atomically increment the number of active references to the stream by AAudioService.
Phil Burk2fe718b2018-05-14 12:28:32 -0700214 *
215 * This is called under a global lock in AAudioStreamTracker.
216 *
Phil Burk94862522017-09-13 21:31:36 -0700217 * @return value after the increment
218 */
Phil Burk2fe718b2018-05-14 12:28:32 -0700219 int32_t incrementServiceReferenceCount_l();
Phil Burk94862522017-09-13 21:31:36 -0700220
221 /**
222 * Atomically decrement the number of active references to the stream by AAudioService.
Phil Burk2fe718b2018-05-14 12:28:32 -0700223 * This should only be called after incrementServiceReferenceCount_l().
224 *
225 * This is called under a global lock in AAudioStreamTracker.
226 *
Phil Burk94862522017-09-13 21:31:36 -0700227 * @return value after the decrement
228 */
Phil Burk2fe718b2018-05-14 12:28:32 -0700229 int32_t decrementServiceReferenceCount_l();
Phil Burk94862522017-09-13 21:31:36 -0700230
231 bool isCloseNeeded() const {
232 return mCloseNeeded.load();
233 }
234
Phil Burk2fe718b2018-05-14 12:28:32 -0700235 /**
236 * Mark this stream as needing to be closed.
237 * Once marked for closing, it cannot be unmarked.
238 */
239 void markCloseNeeded() {
240 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700241 }
242
Phil Burk19e990e2018-03-22 13:59:34 -0700243 virtual const char *getTypeText() const { return "Base"; }
244
Phil Burk2355edb2016-12-26 13:54:02 -0800245protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700246
Phil Burk39f02dd2017-08-04 09:13:31 -0700247 /**
248 * Open the device.
249 */
250 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
251 aaudio_sharing_mode_t sharingMode);
252
Phil Burk7ebbc112020-05-13 15:55:17 -0700253 // These must be called under mLock
254 virtual aaudio_result_t close_l();
255 virtual aaudio_result_t pause_l();
256 virtual aaudio_result_t stop_l();
257 void disconnect_l();
258
259 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700260
Phil Burkbcc36742017-08-31 17:24:51 -0700261 /**
262 * Device specific startup.
263 * @return AAUDIO_OK or negative error.
264 */
265 virtual aaudio_result_t startDevice();
266
Phil Burkc0c70e32017-02-09 13:18:38 -0800267 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
268
269 aaudio_result_t sendCurrentTimestamp();
270
Phil Burk23296382017-11-20 15:45:11 -0800271 aaudio_result_t sendXRunCount(int32_t xRunCount);
272
Phil Burk940083c2017-07-17 17:00:02 -0700273 /**
274 * @param positionFrames
275 * @param timeNanos
276 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
277 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800278 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
279
Phil Burk97350f92017-07-21 15:59:44 -0700280 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
281
Phil Burk523b3042017-09-13 13:03:08 -0700282 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800283
Phil Burkec89b2e2017-06-20 15:05:06 -0700284 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800285
Eric Laurentcb4dae22017-07-01 19:39:32 -0700286 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800287
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000289 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800290
Phil Burkbcc36742017-08-31 17:24:51 -0700291 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800292 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700293 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800294
Eric Laurentcb4dae22017-07-01 19:39:32 -0700295 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700296 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700297 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700298 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
299
Phil Burka53ffa62018-10-10 16:21:37 -0700300 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700301
Phil Burk39f02dd2017-08-04 09:13:31 -0700302 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700303
304 // The mServiceEndpoint variable can be accessed by multiple threads.
305 // So we access it by locally promoting a weak pointer to a smart pointer,
306 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700307 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700308 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700309
Phil Burka9876702020-04-20 18:16:15 -0700310 std::string mMetricsId; // set once during open()
311
Phil Burk11e8d332017-05-24 09:59:02 -0700312private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700313
Phil Burk7ebbc112020-05-13 15:55:17 -0700314 aaudio_result_t stopTimestampThread();
315
316 /**
317 * Send a message to the client with an int64_t data value.
318 */
319 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
320 int64_t dataLong = 0);
321 /**
322 * Send a message to the client with a double data value.
323 */
324 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
325 double dataDouble);
326
Phil Burkf878a8d2019-03-29 17:23:00 -0700327 /**
328 * @return true if the queue is getting full.
329 */
330 bool isUpMessageQueueBusy();
331
Eric Laurentcb4dae22017-07-01 19:39:32 -0700332 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800333 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700334
Phil Burk2fe718b2018-05-14 12:28:32 -0700335 // This is modified under a global lock in AAudioStreamTracker.
336 int32_t mCallingCount = 0;
337
Phil Burk762365c2018-12-10 16:02:16 -0800338 // This indicates that a stream that is being referenced by a binder call needs to closed.
Phil Burk94862522017-09-13 21:31:36 -0700339 std::atomic<bool> mCloseNeeded{false};
Phil Burk762365c2018-12-10 16:02:16 -0800340
341 // This indicate that a running stream should not be processed because of an error,
342 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
343 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700344
345 // Locking order is important.
346 // Always acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
347 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800348};
349
Phil Burk5ed503c2017-02-01 09:38:15 -0800350} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800351
Phil Burk5ed503c2017-02-01 09:38:15 -0800352#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H