blob: 94cc9806fd9666a393001d21ff4b46f07ee49ebf [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/IAAudioService.h"
28#include "binding/AudioEndpointParcelable.h"
29#include "binding/AAudioServiceMessage.h"
30#include "utility/AAudioUtilities.h"
Phil Burk97350f92017-07-21 15:59:44 -070031#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
Phil Burk2355edb2016-12-26 13:54:02 -080033#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080034#include "AAudioThread.h"
Phil Burk523b3042017-09-13 13:03:08 -070035
36namespace android {
37 class AAudioService;
38}
Phil Burk2355edb2016-12-26 13:54:02 -080039
Phil Burk5ed503c2017-02-01 09:38:15 -080040namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080041
Phil Burk39f02dd2017-08-04 09:13:31 -070042class AAudioServiceEndpoint;
43
Phil Burk2355edb2016-12-26 13:54:02 -080044// We expect the queue to only have a few commands.
45// This should be way more than we need.
46#define QUEUE_UP_CAPACITY_COMMANDS (128)
47
Phil Burkc0c70e32017-02-09 13:18:38 -080048/**
Phil Burk39f02dd2017-08-04 09:13:31 -070049 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
50 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080051 */
52class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070053 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070054 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070055 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080056
57public:
Phil Burk19e990e2018-03-22 13:59:34 -070058 explicit AAudioServiceStreamBase(android::AAudioService &aAudioService);
Phil Burk39f02dd2017-08-04 09:13:31 -070059
Phil Burk5ed503c2017-02-01 09:38:15 -080060 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080061
62 enum {
63 ILLEGAL_THREAD_ID = 0
64 };
65
Phil Burka5222e22017-07-28 13:31:14 -070066 static std::string dumpHeader();
67
68 // does not include EOL
69 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070070
Phil Burk2355edb2016-12-26 13:54:02 -080071 /**
72 * Open the device.
73 */
Phil Burk39f02dd2017-08-04 09:13:31 -070074 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080075
Phil Burka9876702020-04-20 18:16:15 -070076 // We log the CLOSE from the close() method. We needed this separate method to log the OPEN
77 // because we had to wait until we generated the handle.
78 void logOpen(aaudio_handle_t streamHandle);
79
Phil Burk7ebbc112020-05-13 15:55:17 -070080 aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080081
82 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070083 * Start the flow of audio data.
84 *
85 * This is not guaranteed to be synchronous but it currently is.
86 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080087 */
Phil Burk7ebbc112020-05-13 15:55:17 -070088 aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080089
90 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070091 * Stop the flow of data so that start() can resume without loss of data.
92 *
93 * This is not guaranteed to be synchronous but it currently is.
94 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
95 */
Phil Burk7ebbc112020-05-13 15:55:17 -070096 aaudio_result_t pause();
Phil Burk71f35bb2017-04-13 16:05:07 -070097
98 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070099 * Stop the flow of data after the currently queued data has finished playing.
100 *
101 * This is not guaranteed to be synchronous but it currently is.
102 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
103 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700104 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700105 aaudio_result_t stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700106
Phil Burk2355edb2016-12-26 13:54:02 -0800107 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700108 * Discard any data held by the underlying HAL or Service.
109 *
110 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800111 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700112 aaudio_result_t flush();
Phil Burk39f02dd2017-08-04 09:13:31 -0700113
jiabind1f1cb62020-03-24 11:57:57 -0700114 virtual aaudio_result_t startClient(const android::AudioClient& client,
115 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700116 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700117 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700118 return AAUDIO_ERROR_UNAVAILABLE;
119 }
120
121 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700122 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700123 return AAUDIO_ERROR_UNAVAILABLE;
124 }
125
Phil Burk7ebbc112020-05-13 15:55:17 -0700126 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority);
127
128 aaudio_result_t unregisterAudioThread(pid_t clientThreadId);
129
Phil Burk11e8d332017-05-24 09:59:02 -0700130 bool isRunning() const {
131 return mState == AAUDIO_STREAM_STATE_STARTED;
132 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700133
Phil Burkc0c70e32017-02-09 13:18:38 -0800134 /**
135 * Fill in a parcelable description of stream.
136 */
137 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800138
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800140 mRegisteredClientThread = pid;
141 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800142
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800144 return mRegisteredClientThread;
145 }
146
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 int32_t getFramesPerBurst() const {
148 return mFramesPerBurst;
149 }
150
Phil Burkc0c70e32017-02-09 13:18:38 -0800151 void run() override; // to implement Runnable
152
Phil Burk5ef003b2017-06-30 11:43:37 -0700153 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800154
Phil Burk39f02dd2017-08-04 09:13:31 -0700155 const android::AudioClient &getAudioClient() {
156 return mMmapClient;
157 }
158
Phil Burk2ac035f2017-06-23 14:51:14 -0700159 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700160 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700161 }
162
Phil Burkb63320a2017-06-30 10:28:20 -0700163 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700164 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700165 }
166
Phil Burk11e8d332017-05-24 09:59:02 -0700167 aaudio_handle_t getHandle() const {
168 return mHandle;
169 }
170 void setHandle(aaudio_handle_t handle) {
171 mHandle = handle;
172 }
173
Phil Burkbbd52862018-04-13 11:37:42 -0700174 audio_port_handle_t getPortHandle() const {
175 return mClientHandle;
176 }
177
Phil Burk5a26e662017-07-07 12:44:48 -0700178 aaudio_stream_state_t getState() const {
179 return mState;
180 }
181
Phil Burk39f02dd2017-08-04 09:13:31 -0700182 void onVolumeChanged(float volume);
183
Phil Burk23296382017-11-20 15:45:11 -0800184 /**
185 * Set false when the stream is started.
186 * Set true when data is first read from the stream.
187 * @param b
188 */
189 void setFlowing(bool b) {
190 mFlowing = b;
191 }
192
193 bool isFlowing() const {
194 return mFlowing;
195 }
196
Phil Burk94862522017-09-13 21:31:36 -0700197 /**
Phil Burk762365c2018-12-10 16:02:16 -0800198 * Set false when the stream should not longer be processed.
199 * This may be caused by a message queue overflow.
200 * Set true when stream is started.
201 * @param suspended
202 */
203 void setSuspended(bool suspended) {
204 mSuspended = suspended;
205 }
206
207 bool isSuspended() const {
208 return mSuspended;
209 }
210
211 /**
Phil Burk94862522017-09-13 21:31:36 -0700212 * Atomically increment the number of active references to the stream by AAudioService.
Phil Burk2fe718b2018-05-14 12:28:32 -0700213 *
214 * This is called under a global lock in AAudioStreamTracker.
215 *
Phil Burk94862522017-09-13 21:31:36 -0700216 * @return value after the increment
217 */
Phil Burk2fe718b2018-05-14 12:28:32 -0700218 int32_t incrementServiceReferenceCount_l();
Phil Burk94862522017-09-13 21:31:36 -0700219
220 /**
221 * Atomically decrement the number of active references to the stream by AAudioService.
Phil Burk2fe718b2018-05-14 12:28:32 -0700222 * This should only be called after incrementServiceReferenceCount_l().
223 *
224 * This is called under a global lock in AAudioStreamTracker.
225 *
Phil Burk94862522017-09-13 21:31:36 -0700226 * @return value after the decrement
227 */
Phil Burk2fe718b2018-05-14 12:28:32 -0700228 int32_t decrementServiceReferenceCount_l();
Phil Burk94862522017-09-13 21:31:36 -0700229
230 bool isCloseNeeded() const {
231 return mCloseNeeded.load();
232 }
233
Phil Burk2fe718b2018-05-14 12:28:32 -0700234 /**
235 * Mark this stream as needing to be closed.
236 * Once marked for closing, it cannot be unmarked.
237 */
238 void markCloseNeeded() {
239 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700240 }
241
Phil Burk19e990e2018-03-22 13:59:34 -0700242 virtual const char *getTypeText() const { return "Base"; }
243
Phil Burk2355edb2016-12-26 13:54:02 -0800244protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700245
Phil Burk39f02dd2017-08-04 09:13:31 -0700246 /**
247 * Open the device.
248 */
249 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
250 aaudio_sharing_mode_t sharingMode);
251
Phil Burk7ebbc112020-05-13 15:55:17 -0700252 // These must be called under mLock
253 virtual aaudio_result_t close_l();
254 virtual aaudio_result_t pause_l();
255 virtual aaudio_result_t stop_l();
256 void disconnect_l();
257
258 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700259
Phil Burkbcc36742017-08-31 17:24:51 -0700260 /**
261 * Device specific startup.
262 * @return AAUDIO_OK or negative error.
263 */
264 virtual aaudio_result_t startDevice();
265
Phil Burkc0c70e32017-02-09 13:18:38 -0800266 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
267
268 aaudio_result_t sendCurrentTimestamp();
269
Phil Burk23296382017-11-20 15:45:11 -0800270 aaudio_result_t sendXRunCount(int32_t xRunCount);
271
Phil Burk940083c2017-07-17 17:00:02 -0700272 /**
273 * @param positionFrames
274 * @param timeNanos
275 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
276 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800277 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
278
Phil Burk97350f92017-07-21 15:59:44 -0700279 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
280
Phil Burk523b3042017-09-13 13:03:08 -0700281 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800282
Phil Burkec89b2e2017-06-20 15:05:06 -0700283 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800284
Eric Laurentcb4dae22017-07-01 19:39:32 -0700285 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800286
Eric Laurentcb4dae22017-07-01 19:39:32 -0700287 SharedRingBuffer* mUpMessageQueue;
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 std::mutex mUpMessageQueueLock;
Phil Burk2355edb2016-12-26 13:54:02 -0800289
Phil Burkbcc36742017-08-31 17:24:51 -0700290 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800291 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700292 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800293
Eric Laurentcb4dae22017-07-01 19:39:32 -0700294 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700295 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700296 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700297 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
298
Phil Burka53ffa62018-10-10 16:21:37 -0700299 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700300
Phil Burk39f02dd2017-08-04 09:13:31 -0700301 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700302
303 // The mServiceEndpoint variable can be accessed by multiple threads.
304 // So we access it by locally promoting a weak pointer to a smart pointer,
305 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700306 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700307 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700308
Phil Burka9876702020-04-20 18:16:15 -0700309 std::string mMetricsId; // set once during open()
310
Phil Burk11e8d332017-05-24 09:59:02 -0700311private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700312
Phil Burk7ebbc112020-05-13 15:55:17 -0700313 aaudio_result_t stopTimestampThread();
314
315 /**
316 * Send a message to the client with an int64_t data value.
317 */
318 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
319 int64_t dataLong = 0);
320 /**
321 * Send a message to the client with a double data value.
322 */
323 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
324 double dataDouble);
325
Phil Burkf878a8d2019-03-29 17:23:00 -0700326 /**
327 * @return true if the queue is getting full.
328 */
329 bool isUpMessageQueueBusy();
330
Eric Laurentcb4dae22017-07-01 19:39:32 -0700331 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800332 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700333
Phil Burk2fe718b2018-05-14 12:28:32 -0700334 // This is modified under a global lock in AAudioStreamTracker.
335 int32_t mCallingCount = 0;
336
Phil Burk762365c2018-12-10 16:02:16 -0800337 // This indicates that a stream that is being referenced by a binder call needs to closed.
Phil Burk94862522017-09-13 21:31:36 -0700338 std::atomic<bool> mCloseNeeded{false};
Phil Burk762365c2018-12-10 16:02:16 -0800339
340 // This indicate that a running stream should not be processed because of an error,
341 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
342 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700343
344 // Locking order is important.
345 // Always acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
346 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800347};
348
Phil Burk5ed503c2017-02-01 09:38:15 -0800349} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800350
Phil Burk5ed503c2017-02-01 09:38:15 -0800351#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H