Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
| 18 | #define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 19 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | #include <mutex> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 24 | #include <media/AidlConversion.h> |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 25 | #include <media/AudioClient.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 26 | #include <utils/RefBase.h> |
| 27 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 28 | #include "fifo/FifoBuffer.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | #include "binding/AudioEndpointParcelable.h" |
| 30 | #include "binding/AAudioServiceMessage.h" |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 31 | #include "binding/AAudioStreamRequest.h" |
| 32 | #include "core/AAudioStreamParameters.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 33 | #include "utility/AAudioUtilities.h" |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 34 | #include "utility/AudioClock.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 36 | #include "AAudioCommandQueue.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 37 | #include "AAudioThread.h" |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 38 | #include "SharedRingBuffer.h" |
| 39 | #include "TimestampScheduler.h" |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | class AAudioService; |
| 43 | } |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 44 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 45 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 46 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 47 | class AAudioServiceEndpoint; |
| 48 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 49 | // We expect the queue to only have a few commands. |
| 50 | // This should be way more than we need. |
| 51 | #define QUEUE_UP_CAPACITY_COMMANDS (128) |
| 52 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 53 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | * Each instance of AAudioServiceStreamBase corresponds to a client stream. |
| 55 | * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 56 | */ |
| 57 | class AAudioServiceStreamBase |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 58 | : public virtual android::RefBase |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 59 | , public AAudioStreamParameters |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 60 | , public Runnable { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 61 | |
| 62 | public: |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 63 | explicit AAudioServiceStreamBase(android::AAudioService &aAudioService); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 64 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 65 | ~AAudioServiceStreamBase() override; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 66 | |
| 67 | enum { |
| 68 | ILLEGAL_THREAD_ID = 0 |
| 69 | }; |
| 70 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 71 | static std::string dumpHeader(); |
| 72 | |
| 73 | // does not include EOL |
| 74 | virtual std::string dump() const; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 75 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Open the device. |
| 78 | */ |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 79 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 80 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 81 | // We log the CLOSE from the close() method. We needed this separate method to log the OPEN |
| 82 | // because we had to wait until we generated the handle. |
| 83 | void logOpen(aaudio_handle_t streamHandle); |
| 84 | |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 85 | aaudio_result_t close() EXCLUDES(mLock); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 88 | * Start the flow of audio data. |
| 89 | * |
| 90 | * This is not guaranteed to be synchronous but it currently is. |
| 91 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 92 | */ |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 93 | aaudio_result_t start() EXCLUDES(mLock); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 96 | * Stop the flow of data so that start() can resume without loss of data. |
| 97 | * |
| 98 | * This is not guaranteed to be synchronous but it currently is. |
| 99 | * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete. |
| 100 | */ |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 101 | aaudio_result_t pause() EXCLUDES(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 104 | * Stop the flow of data after the currently queued data has finished playing. |
| 105 | * |
| 106 | * This is not guaranteed to be synchronous but it currently is. |
| 107 | * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete. |
| 108 | * |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 109 | */ |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 110 | aaudio_result_t stop() EXCLUDES(mLock); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 111 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 112 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 113 | * Discard any data held by the underlying HAL or Service. |
| 114 | * |
| 115 | * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete. |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 116 | */ |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 117 | aaudio_result_t flush() EXCLUDES(mLock); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Exit standby mode. The MMAP buffer will be reallocated. |
| 121 | */ |
| 122 | aaudio_result_t exitStandby(AudioEndpointParcelable *parcelable) EXCLUDES(mLock); |
| 123 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 124 | virtual aaudio_result_t startClient(const android::AudioClient& client, |
| 125 | const audio_attributes_t *attr __unused, |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 126 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 127 | ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 128 | return AAUDIO_ERROR_UNAVAILABLE; |
| 129 | } |
| 130 | |
| 131 | virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 132 | ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 133 | return AAUDIO_ERROR_UNAVAILABLE; |
| 134 | } |
| 135 | |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 136 | aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority) EXCLUDES(mLock); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 137 | |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 138 | aaudio_result_t unregisterAudioThread(pid_t clientThreadId) EXCLUDES(mLock); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 139 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 140 | bool isRunning() const { |
| 141 | return mState == AAUDIO_STREAM_STATE_STARTED; |
| 142 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 143 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 144 | /** |
| 145 | * Fill in a parcelable description of stream. |
| 146 | */ |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 147 | aaudio_result_t getDescription(AudioEndpointParcelable &parcelable) EXCLUDES(mLock); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 148 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 149 | void setRegisteredThread(pid_t pid) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 150 | mRegisteredClientThread = pid; |
| 151 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 152 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 153 | pid_t getRegisteredThread() const { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 154 | return mRegisteredClientThread; |
| 155 | } |
| 156 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | int32_t getFramesPerBurst() const { |
| 158 | return mFramesPerBurst; |
| 159 | } |
| 160 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 161 | void run() override; // to implement Runnable |
| 162 | |
Phil Burk | da433d0 | 2021-04-12 16:10:51 +0000 | [diff] [blame] | 163 | void disconnect() EXCLUDES(mLock); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | const android::AudioClient &getAudioClient() { |
| 166 | return mMmapClient; |
| 167 | } |
| 168 | |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 169 | uid_t getOwnerUserId() const { |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 170 | return VALUE_OR_FATAL(android::aidl2legacy_int32_t_uid_t( |
| 171 | mMmapClient.attributionSource.uid)); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 174 | pid_t getOwnerProcessId() const { |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 175 | return VALUE_OR_FATAL(android::aidl2legacy_int32_t_pid_t( |
| 176 | mMmapClient.attributionSource.pid)); |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 179 | aaudio_handle_t getHandle() const { |
| 180 | return mHandle; |
| 181 | } |
| 182 | void setHandle(aaudio_handle_t handle) { |
| 183 | mHandle = handle; |
| 184 | } |
| 185 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 186 | audio_port_handle_t getPortHandle() const { |
| 187 | return mClientHandle; |
| 188 | } |
| 189 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 190 | aaudio_stream_state_t getState() const { |
| 191 | return mState; |
| 192 | } |
| 193 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 194 | void onVolumeChanged(float volume); |
| 195 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Set false when the stream is started. |
| 198 | * Set true when data is first read from the stream. |
| 199 | * @param b |
| 200 | */ |
| 201 | void setFlowing(bool b) { |
| 202 | mFlowing = b; |
| 203 | } |
| 204 | |
| 205 | bool isFlowing() const { |
| 206 | return mFlowing; |
| 207 | } |
| 208 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 209 | /** |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 210 | * Set false when the stream should not longer be processed. |
| 211 | * This may be caused by a message queue overflow. |
| 212 | * Set true when stream is started. |
| 213 | * @param suspended |
| 214 | */ |
| 215 | void setSuspended(bool suspended) { |
| 216 | mSuspended = suspended; |
| 217 | } |
| 218 | |
| 219 | bool isSuspended() const { |
| 220 | return mSuspended; |
| 221 | } |
| 222 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 223 | bool isCloseNeeded() const { |
| 224 | return mCloseNeeded.load(); |
| 225 | } |
| 226 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Mark this stream as needing to be closed. |
| 229 | * Once marked for closing, it cannot be unmarked. |
| 230 | */ |
| 231 | void markCloseNeeded() { |
| 232 | mCloseNeeded.store(true); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 235 | virtual const char *getTypeText() const { return "Base"; } |
| 236 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 237 | protected: |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 238 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Open the device. |
| 241 | */ |
| 242 | aaudio_result_t open(const aaudio::AAudioStreamRequest &request, |
| 243 | aaudio_sharing_mode_t sharingMode); |
| 244 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 245 | aaudio_result_t start_l() REQUIRES(mLock); |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 246 | virtual aaudio_result_t close_l() REQUIRES(mLock); |
| 247 | virtual aaudio_result_t pause_l() REQUIRES(mLock); |
| 248 | virtual aaudio_result_t stop_l() REQUIRES(mLock); |
| 249 | void disconnect_l() REQUIRES(mLock); |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 250 | aaudio_result_t flush_l() REQUIRES(mLock); |
| 251 | |
| 252 | class RegisterAudioThreadParam : public AAudioCommandParam { |
| 253 | public: |
| 254 | RegisterAudioThreadParam(pid_t ownerPid, pid_t clientThreadId, int priority) |
| 255 | : AAudioCommandParam(), mOwnerPid(ownerPid), |
| 256 | mClientThreadId(clientThreadId), mPriority(priority) { } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 257 | ~RegisterAudioThreadParam() override = default; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 258 | |
| 259 | pid_t mOwnerPid; |
| 260 | pid_t mClientThreadId; |
| 261 | int mPriority; |
| 262 | }; |
| 263 | aaudio_result_t registerAudioThread_l( |
| 264 | pid_t ownerPid, pid_t clientThreadId, int priority) REQUIRES(mLock); |
| 265 | |
| 266 | class UnregisterAudioThreadParam : public AAudioCommandParam { |
| 267 | public: |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 268 | explicit UnregisterAudioThreadParam(pid_t clientThreadId) |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 269 | : AAudioCommandParam(), mClientThreadId(clientThreadId) { } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 270 | ~UnregisterAudioThreadParam() override = default; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 271 | |
| 272 | pid_t mClientThreadId; |
| 273 | }; |
| 274 | aaudio_result_t unregisterAudioThread_l(pid_t clientThreadId) REQUIRES(mLock); |
| 275 | |
| 276 | class GetDescriptionParam : public AAudioCommandParam { |
| 277 | public: |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 278 | explicit GetDescriptionParam(AudioEndpointParcelable* parcelable) |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 279 | : AAudioCommandParam(), mParcelable(parcelable) { } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 280 | ~GetDescriptionParam() override = default; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 281 | |
| 282 | AudioEndpointParcelable* mParcelable; |
| 283 | }; |
| 284 | aaudio_result_t getDescription_l(AudioEndpointParcelable* parcelable) REQUIRES(mLock); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 285 | |
| 286 | void setState(aaudio_stream_state_t state); |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 287 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Device specific startup. |
| 290 | * @return AAUDIO_OK or negative error. |
| 291 | */ |
| 292 | virtual aaudio_result_t startDevice(); |
| 293 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 294 | aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command); |
| 295 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 296 | aaudio_result_t sendCurrentTimestamp_l() REQUIRES(mLock); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 297 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 298 | aaudio_result_t sendXRunCount(int32_t xRunCount); |
| 299 | |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 300 | /** |
| 301 | * @param positionFrames |
| 302 | * @param timeNanos |
| 303 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 304 | */ |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 305 | virtual aaudio_result_t getFreeRunningPosition_l( |
| 306 | int64_t *positionFrames, int64_t *timeNanos) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 307 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 308 | virtual aaudio_result_t getHardwareTimestamp_l(int64_t *positionFrames, int64_t *timeNanos) = 0; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 309 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 310 | virtual aaudio_result_t getAudioDataDescription_l(AudioEndpointParcelable* parcelable) = 0; |
| 311 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 312 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 313 | aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 314 | |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 315 | bool isDisconnected_l() const REQUIRES(mLock) { |
| 316 | return mDisconnected; |
| 317 | } |
| 318 | void setDisconnected_l(bool flag) REQUIRES(mLock) { |
| 319 | mDisconnected = flag; |
| 320 | } |
| 321 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 322 | virtual aaudio_result_t standby_l() REQUIRES(mLock) { |
Jason Lin | 20d04e3 | 2022-11-22 13:39:49 +0800 | [diff] [blame] | 323 | return AAUDIO_ERROR_UNIMPLEMENTED; |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 324 | } |
| 325 | class ExitStandbyParam : public AAudioCommandParam { |
| 326 | public: |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 327 | explicit ExitStandbyParam(AudioEndpointParcelable* parcelable) |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 328 | : AAudioCommandParam(), mParcelable(parcelable) { } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 329 | ~ExitStandbyParam() override = default; |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 330 | |
| 331 | AudioEndpointParcelable* mParcelable; |
| 332 | }; |
| 333 | virtual aaudio_result_t exitStandby_l( |
| 334 | AudioEndpointParcelable* parcelable __unused) REQUIRES(mLock) { |
| 335 | return AAUDIO_ERROR_UNAVAILABLE; |
| 336 | } |
| 337 | bool isStandby_l() const REQUIRES(mLock) { |
| 338 | return mStandby; |
| 339 | } |
| 340 | void setStandby_l(bool standby) REQUIRES(mLock) { |
| 341 | mStandby = standby; |
| 342 | } |
| 343 | |
| 344 | bool isIdle_l() const REQUIRES(mLock) { |
| 345 | return mState == AAUDIO_STREAM_STATE_OPEN || mState == AAUDIO_STREAM_STATE_PAUSED |
| 346 | || mState == AAUDIO_STREAM_STATE_STOPPED; |
| 347 | } |
| 348 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 349 | virtual int64_t nextDataReportTime_l() REQUIRES(mLock) { |
| 350 | return std::numeric_limits<int64_t>::max(); |
| 351 | } |
| 352 | virtual void reportData_l() REQUIRES(mLock) { return; } |
| 353 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 354 | pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 355 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 356 | std::mutex mUpMessageQueueLock; |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 357 | std::shared_ptr<SharedRingBuffer> mUpMessageQueue; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 358 | |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 359 | enum : int32_t { |
| 360 | START, |
| 361 | PAUSE, |
| 362 | STOP, |
| 363 | FLUSH, |
| 364 | CLOSE, |
| 365 | DISCONNECT, |
| 366 | REGISTER_AUDIO_THREAD, |
| 367 | UNREGISTER_AUDIO_THREAD, |
| 368 | GET_DESCRIPTION, |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 369 | EXIT_STANDBY, |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 370 | }; |
| 371 | AAudioThread mCommandThread; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 372 | std::atomic<bool> mThreadEnabled{false}; |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 373 | AAudioCommandQueue mCommandQueue; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 374 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 375 | int32_t mFramesPerBurst = 0; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 376 | android::AudioClient mMmapClient; // set in open, used in MMAP start() |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 377 | // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 378 | audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 379 | |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 380 | SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 381 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 382 | android::AAudioService &mAudioService; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 383 | |
| 384 | // The mServiceEndpoint variable can be accessed by multiple threads. |
| 385 | // So we access it by locally promoting a weak pointer to a smart pointer, |
| 386 | // which is thread-safe. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 387 | android::sp<AAudioServiceEndpoint> mServiceEndpoint; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 388 | android::wp<AAudioServiceEndpoint> mServiceEndpointWeak; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 389 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 390 | std::string mMetricsId; // set once during open() |
| 391 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 392 | private: |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 393 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 394 | aaudio_result_t stopTimestampThread(); |
| 395 | |
| 396 | /** |
| 397 | * Send a message to the client with an int64_t data value. |
| 398 | */ |
| 399 | aaudio_result_t sendServiceEvent(aaudio_service_event_t event, |
| 400 | int64_t dataLong = 0); |
| 401 | /** |
| 402 | * Send a message to the client with a double data value. |
| 403 | */ |
| 404 | aaudio_result_t sendServiceEvent(aaudio_service_event_t event, |
| 405 | double dataDouble); |
| 406 | |
jiabin | ba75f21 | 2021-12-07 20:06:30 +0000 | [diff] [blame] | 407 | aaudio_result_t sendCommand(aaudio_command_opcode opCode, |
| 408 | std::shared_ptr<AAudioCommandParam> param = nullptr, |
| 409 | bool waitForReply = false, |
| 410 | int64_t timeoutNanos = 0); |
| 411 | |
| 412 | aaudio_result_t closeAndClear(); |
| 413 | |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 414 | /** |
| 415 | * @return true if the queue is getting full. |
| 416 | */ |
| 417 | bool isUpMessageQueueBusy(); |
| 418 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 419 | aaudio_handle_t mHandle = -1; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 420 | bool mFlowing = false; |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 421 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 422 | // This indicates that a stream that is being referenced by a binder call |
| 423 | // and needs to closed. |
| 424 | std::atomic<bool> mCloseNeeded{false}; // TODO remove |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 425 | |
| 426 | // This indicate that a running stream should not be processed because of an error, |
| 427 | // for example a full message queue. Note that this atomic is unrelated to mCloseNeeded. |
| 428 | std::atomic<bool> mSuspended{false}; |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 429 | |
Phil Burk | db46614 | 2021-04-16 16:50:00 +0000 | [diff] [blame] | 430 | bool mDisconnected GUARDED_BY(mLock) {false}; |
| 431 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 432 | bool mStandby GUARDED_BY(mLock) = false; |
| 433 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 434 | protected: |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 435 | // Locking order is important. |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 436 | // Acquire mLock before acquiring AAudioServiceEndpoint::mLockStreams |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 437 | // The lock will be held by the command thread. All operations needing the lock must run from |
| 438 | // the command thread. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 439 | std::mutex mLock; // Prevent start/stop/close etcetera from colliding |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 440 | }; |
| 441 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 442 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 443 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 444 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |