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 | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 23 | #include <media/AudioClient.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
| 25 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 26 | #include "fifo/FifoBuffer.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include "binding/AudioEndpointParcelable.h" |
| 28 | #include "binding/AAudioServiceMessage.h" |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame^] | 29 | #include "binding/AAudioStreamRequest.h" |
| 30 | #include "core/AAudioStreamParameters.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 31 | #include "utility/AAudioUtilities.h" |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 32 | #include "utility/AudioClock.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 33 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 34 | #include "SharedRingBuffer.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 35 | #include "AAudioThread.h" |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | class AAudioService; |
| 39 | } |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 40 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 41 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 42 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 43 | class AAudioServiceEndpoint; |
| 44 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 45 | // 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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 49 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 50 | * 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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 | */ |
| 53 | class AAudioServiceStreamBase |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 54 | : public virtual android::RefBase |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 55 | , public AAudioStreamParameters |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 56 | , public Runnable { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 57 | |
| 58 | public: |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 59 | explicit AAudioServiceStreamBase(android::AAudioService &aAudioService); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 60 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 61 | virtual ~AAudioServiceStreamBase(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 62 | |
| 63 | enum { |
| 64 | ILLEGAL_THREAD_ID = 0 |
| 65 | }; |
| 66 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 67 | static std::string dumpHeader(); |
| 68 | |
| 69 | // does not include EOL |
| 70 | virtual std::string dump() const; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 71 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Open the device. |
| 74 | */ |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 75 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 76 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 77 | // 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 Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 81 | aaudio_result_t close(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 84 | * 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 88 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 89 | aaudio_result_t start(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | * 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 Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 97 | aaudio_result_t pause(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 100 | * 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 Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 105 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 106 | aaudio_result_t stop(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 107 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 108 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 109 | * 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 112 | */ |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 113 | aaudio_result_t flush(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 114 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 115 | virtual aaudio_result_t startClient(const android::AudioClient& client, |
| 116 | const audio_attributes_t *attr __unused, |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 117 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 119 | return AAUDIO_ERROR_UNAVAILABLE; |
| 120 | } |
| 121 | |
| 122 | virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 123 | ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 124 | return AAUDIO_ERROR_UNAVAILABLE; |
| 125 | } |
| 126 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 127 | aaudio_result_t registerAudioThread(pid_t clientThreadId, int priority); |
| 128 | |
| 129 | aaudio_result_t unregisterAudioThread(pid_t clientThreadId); |
| 130 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 131 | bool isRunning() const { |
| 132 | return mState == AAUDIO_STREAM_STATE_STARTED; |
| 133 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 134 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Fill in a parcelable description of stream. |
| 137 | */ |
| 138 | aaudio_result_t getDescription(AudioEndpointParcelable &parcelable); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 139 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 140 | void setRegisteredThread(pid_t pid) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 141 | mRegisteredClientThread = pid; |
| 142 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 143 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 144 | pid_t getRegisteredThread() const { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 145 | return mRegisteredClientThread; |
| 146 | } |
| 147 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 148 | int32_t getFramesPerBurst() const { |
| 149 | return mFramesPerBurst; |
| 150 | } |
| 151 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 152 | void run() override; // to implement Runnable |
| 153 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 154 | void disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 155 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 156 | const android::AudioClient &getAudioClient() { |
| 157 | return mMmapClient; |
| 158 | } |
| 159 | |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 160 | uid_t getOwnerUserId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 161 | return mMmapClient.clientUid; |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 164 | pid_t getOwnerProcessId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 165 | return mMmapClient.clientPid; |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 168 | aaudio_handle_t getHandle() const { |
| 169 | return mHandle; |
| 170 | } |
| 171 | void setHandle(aaudio_handle_t handle) { |
| 172 | mHandle = handle; |
| 173 | } |
| 174 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 175 | audio_port_handle_t getPortHandle() const { |
| 176 | return mClientHandle; |
| 177 | } |
| 178 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 179 | aaudio_stream_state_t getState() const { |
| 180 | return mState; |
| 181 | } |
| 182 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 183 | void onVolumeChanged(float volume); |
| 184 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 185 | /** |
| 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 Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 198 | /** |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 199 | * 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 Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 213 | * Atomically increment the number of active references to the stream by AAudioService. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 214 | * |
| 215 | * This is called under a global lock in AAudioStreamTracker. |
| 216 | * |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 217 | * @return value after the increment |
| 218 | */ |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 219 | int32_t incrementServiceReferenceCount_l(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Atomically decrement the number of active references to the stream by AAudioService. |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 223 | * This should only be called after incrementServiceReferenceCount_l(). |
| 224 | * |
| 225 | * This is called under a global lock in AAudioStreamTracker. |
| 226 | * |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 227 | * @return value after the decrement |
| 228 | */ |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 229 | int32_t decrementServiceReferenceCount_l(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 230 | |
| 231 | bool isCloseNeeded() const { |
| 232 | return mCloseNeeded.load(); |
| 233 | } |
| 234 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 235 | /** |
| 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 Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 243 | virtual const char *getTypeText() const { return "Base"; } |
| 244 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 245 | protected: |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 246 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Open the device. |
| 249 | */ |
| 250 | aaudio_result_t open(const aaudio::AAudioStreamRequest &request, |
| 251 | aaudio_sharing_mode_t sharingMode); |
| 252 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 253 | // 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 Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 260 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Device specific startup. |
| 263 | * @return AAUDIO_OK or negative error. |
| 264 | */ |
| 265 | virtual aaudio_result_t startDevice(); |
| 266 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 267 | aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command); |
| 268 | |
| 269 | aaudio_result_t sendCurrentTimestamp(); |
| 270 | |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 271 | aaudio_result_t sendXRunCount(int32_t xRunCount); |
| 272 | |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 273 | /** |
| 274 | * @param positionFrames |
| 275 | * @param timeNanos |
| 276 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 277 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 278 | virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 279 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 280 | virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 281 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 282 | virtual aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 283 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 284 | aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 285 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 286 | pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 287 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 288 | std::mutex mUpMessageQueueLock; |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 289 | std::shared_ptr<SharedRingBuffer> mUpMessageQueue; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 290 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 291 | AAudioThread mTimestampThread; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 292 | // This is used by one thread to tell another thread to exit. So it must be atomic. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 293 | std::atomic<bool> mThreadEnabled{false}; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 294 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 295 | int32_t mFramesPerBurst = 0; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 296 | android::AudioClient mMmapClient; // set in open, used in MMAP start() |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 297 | // TODO rename mClientHandle to mPortHandle to be more consistent with AudioFlinger. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 298 | audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 299 | |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 300 | SimpleDoubleBuffer<Timestamp> mAtomicStreamTimestamp; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 301 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 302 | android::AAudioService &mAudioService; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 303 | |
| 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 Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 307 | android::sp<AAudioServiceEndpoint> mServiceEndpoint; |
Phil Burk | 6e2770e | 2018-05-01 13:03:52 -0700 | [diff] [blame] | 308 | android::wp<AAudioServiceEndpoint> mServiceEndpointWeak; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 309 | |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 310 | std::string mMetricsId; // set once during open() |
| 311 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 312 | private: |
Phil Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 313 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 314 | 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 Burk | f878a8d | 2019-03-29 17:23:00 -0700 | [diff] [blame] | 327 | /** |
| 328 | * @return true if the queue is getting full. |
| 329 | */ |
| 330 | bool isUpMessageQueueBusy(); |
| 331 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 332 | aaudio_handle_t mHandle = -1; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 333 | bool mFlowing = false; |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 334 | |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 335 | // This is modified under a global lock in AAudioStreamTracker. |
| 336 | int32_t mCallingCount = 0; |
| 337 | |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 338 | // This indicates that a stream that is being referenced by a binder call needs to closed. |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 339 | std::atomic<bool> mCloseNeeded{false}; |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 340 | |
| 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 Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 344 | |
| 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 Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 348 | }; |
| 349 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 350 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 351 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 352 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |