blob: 0f752b7c666031577a674ac3e01a1fda9d11517e [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>
Phil Burk97350f92017-07-21 15:59:44 -070024#include <media/AudioClient.h>
Phil Burk11e8d332017-05-24 09:59:02 -070025#include <utils/RefBase.h>
26
Phil Burk2355edb2016-12-26 13:54:02 -080027#include "fifo/FifoBuffer.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include "binding/AudioEndpointParcelable.h"
29#include "binding/AAudioServiceMessage.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070030#include "binding/AAudioStreamRequest.h"
31#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032#include "utility/AAudioUtilities.h"
Phil Burk97350f92017-07-21 15:59:44 -070033#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034
Phil Burk2355edb2016-12-26 13:54:02 -080035#include "SharedRingBuffer.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080036#include "AAudioThread.h"
Phil Burk523b3042017-09-13 13:03:08 -070037
38namespace android {
39 class AAudioService;
40}
Phil Burk2355edb2016-12-26 13:54:02 -080041
Phil Burk5ed503c2017-02-01 09:38:15 -080042namespace aaudio {
Phil Burk2355edb2016-12-26 13:54:02 -080043
Phil Burk39f02dd2017-08-04 09:13:31 -070044class AAudioServiceEndpoint;
45
Phil Burk2355edb2016-12-26 13:54:02 -080046// We expect the queue to only have a few commands.
47// This should be way more than we need.
48#define QUEUE_UP_CAPACITY_COMMANDS (128)
49
Phil Burkc0c70e32017-02-09 13:18:38 -080050/**
Phil Burk39f02dd2017-08-04 09:13:31 -070051 * Each instance of AAudioServiceStreamBase corresponds to a client stream.
52 * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port.
Phil Burkc0c70e32017-02-09 13:18:38 -080053 */
54class AAudioServiceStreamBase
Phil Burk11e8d332017-05-24 09:59:02 -070055 : public virtual android::RefBase
Phil Burk39f02dd2017-08-04 09:13:31 -070056 , public AAudioStreamParameters
Phil Burk11e8d332017-05-24 09:59:02 -070057 , public Runnable {
Phil Burk2355edb2016-12-26 13:54:02 -080058
59public:
Phil Burk19e990e2018-03-22 13:59:34 -070060 explicit AAudioServiceStreamBase(android::AAudioService &aAudioService);
Phil Burk39f02dd2017-08-04 09:13:31 -070061
Phil Burk5ed503c2017-02-01 09:38:15 -080062 virtual ~AAudioServiceStreamBase();
Phil Burk2355edb2016-12-26 13:54:02 -080063
64 enum {
65 ILLEGAL_THREAD_ID = 0
66 };
67
Phil Burka5222e22017-07-28 13:31:14 -070068 static std::string dumpHeader();
69
70 // does not include EOL
71 virtual std::string dump() const;
Phil Burk4501b352017-06-29 18:12:36 -070072
Phil Burk2355edb2016-12-26 13:54:02 -080073 /**
74 * Open the device.
75 */
Phil Burk39f02dd2017-08-04 09:13:31 -070076 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -080077
Phil Burka9876702020-04-20 18:16:15 -070078 // We log the CLOSE from the close() method. We needed this separate method to log the OPEN
79 // because we had to wait until we generated the handle.
80 void logOpen(aaudio_handle_t streamHandle);
81
Phil Burk7ebbc112020-05-13 15:55:17 -070082 aaudio_result_t close();
Phil Burk2355edb2016-12-26 13:54:02 -080083
84 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070085 * Start the flow of audio data.
86 *
87 * This is not guaranteed to be synchronous but it currently is.
88 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -080089 */
Phil Burk7ebbc112020-05-13 15:55:17 -070090 aaudio_result_t start();
Phil Burk2355edb2016-12-26 13:54:02 -080091
92 /**
Phil Burk39f02dd2017-08-04 09:13:31 -070093 * Stop the flow of data so that start() can resume without loss of data.
94 *
95 * This is not guaranteed to be synchronous but it currently is.
96 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
97 */
Phil Burk7ebbc112020-05-13 15:55:17 -070098 aaudio_result_t pause();
Phil Burk71f35bb2017-04-13 16:05:07 -070099
100 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700101 * Stop the flow of data after the currently queued data has finished playing.
102 *
103 * This is not guaranteed to be synchronous but it currently is.
104 * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete.
105 *
Phil Burk71f35bb2017-04-13 16:05:07 -0700106 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700107 aaudio_result_t stop();
Phil Burk98d6d922017-07-06 11:52:45 -0700108
Phil Burk2355edb2016-12-26 13:54:02 -0800109 /**
Phil Burk39f02dd2017-08-04 09:13:31 -0700110 * Discard any data held by the underlying HAL or Service.
111 *
112 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
Phil Burk2355edb2016-12-26 13:54:02 -0800113 */
Phil Burk7ebbc112020-05-13 15:55:17 -0700114 aaudio_result_t flush();
Phil Burk39f02dd2017-08-04 09:13:31 -0700115
jiabind1f1cb62020-03-24 11:57:57 -0700116 virtual aaudio_result_t startClient(const android::AudioClient& client,
117 const audio_attributes_t *attr __unused,
Eric Laurentcb4dae22017-07-01 19:39:32 -0700118 audio_port_handle_t *clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700119 ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700120 return AAUDIO_ERROR_UNAVAILABLE;
121 }
122
123 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700124 ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700125 return AAUDIO_ERROR_UNAVAILABLE;
126 }
127
Phil Burk7ebbc112020-05-13 15:55:17 -0700128 aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority);
129
130 aaudio_result_t unregisterAudioThread(pid_t clientThreadId);
131
Phil Burk11e8d332017-05-24 09:59:02 -0700132 bool isRunning() const {
133 return mState == AAUDIO_STREAM_STATE_STARTED;
134 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700135
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 /**
137 * Fill in a parcelable description of stream.
138 */
139 aaudio_result_t getDescription(AudioEndpointParcelable &parcelable);
Phil Burk2355edb2016-12-26 13:54:02 -0800140
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 void setRegisteredThread(pid_t pid) {
Phil Burk2355edb2016-12-26 13:54:02 -0800142 mRegisteredClientThread = pid;
143 }
Phil Burkdec33ab2017-01-17 14:48:16 -0800144
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 pid_t getRegisteredThread() const {
Phil Burk2355edb2016-12-26 13:54:02 -0800146 return mRegisteredClientThread;
147 }
148
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 int32_t getFramesPerBurst() const {
150 return mFramesPerBurst;
151 }
152
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 void run() override; // to implement Runnable
154
Phil Burk5ef003b2017-06-30 11:43:37 -0700155 void disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 const android::AudioClient &getAudioClient() {
158 return mMmapClient;
159 }
160
Phil Burk2ac035f2017-06-23 14:51:14 -0700161 uid_t getOwnerUserId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700162 return mMmapClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700163 }
164
Phil Burkb63320a2017-06-30 10:28:20 -0700165 pid_t getOwnerProcessId() const {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700166 return mMmapClient.clientPid;
Phil Burkb63320a2017-06-30 10:28:20 -0700167 }
168
Phil Burk11e8d332017-05-24 09:59:02 -0700169 aaudio_handle_t getHandle() const {
170 return mHandle;
171 }
172 void setHandle(aaudio_handle_t handle) {
173 mHandle = handle;
174 }
175
Phil Burkbbd52862018-04-13 11:37:42 -0700176 audio_port_handle_t getPortHandle() const {
177 return mClientHandle;
178 }
179
Phil Burk5a26e662017-07-07 12:44:48 -0700180 aaudio_stream_state_t getState() const {
181 return mState;
182 }
183
Phil Burk39f02dd2017-08-04 09:13:31 -0700184 void onVolumeChanged(float volume);
185
Phil Burk23296382017-11-20 15:45:11 -0800186 /**
187 * Set false when the stream is started.
188 * Set true when data is first read from the stream.
189 * @param b
190 */
191 void setFlowing(bool b) {
192 mFlowing = b;
193 }
194
195 bool isFlowing() const {
196 return mFlowing;
197 }
198
Phil Burk94862522017-09-13 21:31:36 -0700199 /**
Phil Burk762365c2018-12-10 16:02:16 -0800200 * Set false when the stream should not longer be processed.
201 * This may be caused by a message queue overflow.
202 * Set true when stream is started.
203 * @param suspended
204 */
205 void setSuspended(bool suspended) {
206 mSuspended = suspended;
207 }
208
209 bool isSuspended() const {
210 return mSuspended;
211 }
212
Phil Burk94862522017-09-13 21:31:36 -0700213 bool isCloseNeeded() const {
214 return mCloseNeeded.load();
215 }
216
Phil Burk2fe718b2018-05-14 12:28:32 -0700217 /**
218 * Mark this stream as needing to be closed.
219 * Once marked for closing, it cannot be unmarked.
220 */
221 void markCloseNeeded() {
222 mCloseNeeded.store(true);
Phil Burk94862522017-09-13 21:31:36 -0700223 }
224
Phil Burk19e990e2018-03-22 13:59:34 -0700225 virtual const char *getTypeText() const { return "Base"; }
226
Phil Burk2355edb2016-12-26 13:54:02 -0800227protected:
Phil Burk98d6d922017-07-06 11:52:45 -0700228
Phil Burk39f02dd2017-08-04 09:13:31 -0700229 /**
230 * Open the device.
231 */
232 aaudio_result_t open(const aaudio::AAudioStreamRequest &request,
233 aaudio_sharing_mode_t sharingMode);
234
Phil Burk0bd745e2020-10-17 18:20:01 +0000235 virtual aaudio_result_t close_l() REQUIRES(mLock);
236 virtual aaudio_result_t pause_l() REQUIRES(mLock);
237 virtual aaudio_result_t stop_l() REQUIRES(mLock);
238 void disconnect_l() REQUIRES(mLock);
Phil Burk7ebbc112020-05-13 15:55:17 -0700239
240 void setState(aaudio_stream_state_t state);
Phil Burk5a26e662017-07-07 12:44:48 -0700241
Phil Burkbcc36742017-08-31 17:24:51 -0700242 /**
243 * Device specific startup.
244 * @return AAUDIO_OK or negative error.
245 */
246 virtual aaudio_result_t startDevice();
247
Phil Burkc0c70e32017-02-09 13:18:38 -0800248 aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command);
249
250 aaudio_result_t sendCurrentTimestamp();
251
Phil Burk23296382017-11-20 15:45:11 -0800252 aaudio_result_t sendXRunCount(int32_t xRunCount);
253
Phil Burk940083c2017-07-17 17:00:02 -0700254 /**
255 * @param positionFrames
256 * @param timeNanos
257 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error
258 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800259 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0;
260
Phil Burk97350f92017-07-21 15:59:44 -0700261 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0;
262
Phil Burk523b3042017-09-13 13:03:08 -0700263 virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0;
Phil Burkc0c70e32017-02-09 13:18:38 -0800264
Phil Burkec89b2e2017-06-20 15:05:06 -0700265 aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED;
Phil Burk2355edb2016-12-26 13:54:02 -0800266
Eric Laurentcb4dae22017-07-01 19:39:32 -0700267 pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID;
Phil Burk2355edb2016-12-26 13:54:02 -0800268
Phil Burk39f02dd2017-08-04 09:13:31 -0700269 std::mutex mUpMessageQueueLock;
Phil Burk8f4fe502020-07-15 23:54:50 +0000270 std::shared_ptr<SharedRingBuffer> mUpMessageQueue;
Phil Burk2355edb2016-12-26 13:54:02 -0800271
Phil Burkbcc36742017-08-31 17:24:51 -0700272 AAudioThread mTimestampThread;
Phil Burkc0c70e32017-02-09 13:18:38 -0800273 // This is used by one thread to tell another thread to exit. So it must be atomic.
Phil Burk39f02dd2017-08-04 09:13:31 -0700274 std::atomic<bool> mThreadEnabled{false};
Phil Burkc0c70e32017-02-09 13:18:38 -0800275
Eric Laurentcb4dae22017-07-01 19:39:32 -0700276 int32_t mFramesPerBurst = 0;
Phil Burk39f02dd2017-08-04 09:13:31 -0700277 android::AudioClient mMmapClient; // set in open, used in MMAP start()
Phil Burkbbd52862018-04-13 11:37:42 -0700278 // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger.
Eric Laurentcb4dae22017-07-01 19:39:32 -0700279 audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE;
280
Phil Burka53ffa62018-10-10 16:21:37 -0700281 SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp;
Phil Burk97350f92017-07-21 15:59:44 -0700282
Phil Burk39f02dd2017-08-04 09:13:31 -0700283 android::AAudioService &mAudioService;
Phil Burk6e2770e2018-05-01 13:03:52 -0700284
285 // The mServiceEndpoint variable can be accessed by multiple threads.
286 // So we access it by locally promoting a weak pointer to a smart pointer,
287 // which is thread-safe.
Phil Burk39f02dd2017-08-04 09:13:31 -0700288 android::sp<AAudioServiceEndpoint> mServiceEndpoint;
Phil Burk6e2770e2018-05-01 13:03:52 -0700289 android::wp<AAudioServiceEndpoint> mServiceEndpointWeak;
Phil Burk39f02dd2017-08-04 09:13:31 -0700290
Phil Burka9876702020-04-20 18:16:15 -0700291 std::string mMetricsId; // set once during open()
292
Phil Burk11e8d332017-05-24 09:59:02 -0700293private:
Phil Burkf878a8d2019-03-29 17:23:00 -0700294
Phil Burk7ebbc112020-05-13 15:55:17 -0700295 aaudio_result_t stopTimestampThread();
296
297 /**
298 * Send a message to the client with an int64_t data value.
299 */
300 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
301 int64_t dataLong = 0);
302 /**
303 * Send a message to the client with a double data value.
304 */
305 aaudio_result_t sendServiceEvent(aaudio_service_event_t event,
306 double dataDouble);
307
Phil Burkf878a8d2019-03-29 17:23:00 -0700308 /**
309 * @return true if the queue is getting full.
310 */
311 bool isUpMessageQueueBusy();
312
Eric Laurentcb4dae22017-07-01 19:39:32 -0700313 aaudio_handle_t mHandle = -1;
Phil Burk23296382017-11-20 15:45:11 -0800314 bool mFlowing = false;
Phil Burk94862522017-09-13 21:31:36 -0700315
Phil Burk0bd745e2020-10-17 18:20:01 +0000316 // This indicates that a stream that is being referenced by a binder call
317 // and needs to closed.
318 std::atomic<bool> mCloseNeeded{false}; // TODO remove
Phil Burk762365c2018-12-10 16:02:16 -0800319
320 // This indicate that a running stream should not be processed because of an error,
321 // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded.
322 std::atomic<bool> mSuspended{false};
Phil Burk7ebbc112020-05-13 15:55:17 -0700323
Phil Burk0bd745e2020-10-17 18:20:01 +0000324protected:
Phil Burk7ebbc112020-05-13 15:55:17 -0700325 // Locking order is important.
Phil Burk0bd745e2020-10-17 18:20:01 +0000326 // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams
Phil Burk7ebbc112020-05-13 15:55:17 -0700327 std::mutex mLock; // Prevent start/stop/close etcetera from colliding
Phil Burk2355edb2016-12-26 13:54:02 -0800328};
329
Phil Burk5ed503c2017-02-01 09:38:15 -0800330} /* namespace aaudio */
Phil Burk2355edb2016-12-26 13:54:02 -0800331
Phil Burk5ed503c2017-02-01 09:38:15 -0800332#endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H