Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2012, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 18 | #pragma once |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 19 | |
Andy Hung | 25a80ac | 2023-07-19 12:47:35 -0700 | [diff] [blame] | 20 | // ADD_BATTERY_DATA AUDIO_WATCHDOG FAST_THREAD_STATISTICS STATE_QUEUE_DUMP TEE_SINK |
| 21 | #include "Configuration.h" |
| 22 | #include "IAfThread.h" |
| 23 | #include "IAfTrack.h" |
| 24 | |
| 25 | #include <android-base/macros.h> // DISALLOW_COPY_AND_ASSIGN |
| 26 | #include <android/os/IPowerManager.h> |
| 27 | #include <afutils/AudioWatchdog.h> |
| 28 | #include <afutils/NBAIO_Tee.h> |
| 29 | #include <audio_utils/Balance.h> |
| 30 | #include <audio_utils/SimpleLog.h> |
| 31 | #include <datapath/ThreadMetrics.h> |
| 32 | #include <fastpath/FastCapture.h> |
| 33 | #include <fastpath/FastMixer.h> |
| 34 | #include <mediautils/Synchronization.h> |
| 35 | #include <mediautils/ThreadSnapshot.h> |
| 36 | #include <timing/MonotonicFrameCounter.h> |
| 37 | #include <utils/Log.h> |
| 38 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 39 | namespace android { |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 40 | |
| 41 | class AsyncCallbackThread; |
| 42 | |
| 43 | class ThreadBase : public virtual IAfThreadBase, public Thread { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 44 | public: |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 45 | static const char *threadTypeToString(type_t type); |
| 46 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 47 | // ThreadBase_ThreadLoop is a virtual mutex (always nullptr) that |
| 48 | // guards methods and variables that ONLY run and are accessed |
| 49 | // on the single threaded threadLoop(). |
| 50 | // |
| 51 | // As access is by a single thread, the variables are thread safe. |
| 52 | static audio_utils::mutex* ThreadBase_ThreadLoop; |
| 53 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 54 | IAfThreadCallback* afThreadCallback() const final { return mAfThreadCallback.get(); } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 55 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 56 | ThreadBase(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 57 | type_t type, bool systemReady, bool isOut); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 58 | ~ThreadBase() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 59 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 60 | status_t readyToRun() final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 61 | void clearPowerManager() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 62 | |
| 63 | // base for record and playback |
| 64 | enum { |
| 65 | CFG_EVENT_IO, |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 66 | CFG_EVENT_PRIO, |
| 67 | CFG_EVENT_SET_PARAMETER, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 68 | CFG_EVENT_CREATE_AUDIO_PATCH, |
| 69 | CFG_EVENT_RELEASE_AUDIO_PATCH, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 70 | CFG_EVENT_UPDATE_OUT_DEVICE, |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 71 | CFG_EVENT_RESIZE_BUFFER, |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 72 | CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS, |
| 73 | CFG_EVENT_HAL_LATENCY_MODES_CHANGED, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 76 | class ConfigEventData: public RefBase { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 77 | public: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 78 | virtual void dump(char *buffer, size_t size) = 0; |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 79 | protected: |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 80 | ConfigEventData() = default; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 83 | // Config event sequence by client if status needed (e.g binder thread calling setParameters()): |
| 84 | // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 85 | // 2. Lock mutex() |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 86 | // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal |
| 87 | // 4. sendConfigEvent_l() reads status from event->mStatus; |
| 88 | // 5. sendConfigEvent_l() returns status |
| 89 | // 6. Unlock |
| 90 | // |
| 91 | // Parameter sequence by server: threadLoop calling processConfigEvents_l(): |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 92 | // 1. Lock mutex() |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 93 | // 2. If there is an entry in mConfigEvents proceed ... |
| 94 | // 3. Read first entry in mConfigEvents |
| 95 | // 4. Remove first entry from mConfigEvents |
| 96 | // 5. Process |
| 97 | // 6. Set event->mStatus |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 98 | // 7. event->mCondition.notify_one() |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 99 | // 8. Unlock |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 100 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 101 | class ConfigEvent: public RefBase { |
| 102 | public: |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 103 | void dump(char *buffer, size_t size) { |
| 104 | snprintf(buffer, size, "Event type: %d\n", mType); |
| 105 | if (mData != nullptr) { |
| 106 | snprintf(buffer, size, "Data:\n"); |
| 107 | mData->dump(buffer, size); |
| 108 | } |
| 109 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 110 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 111 | audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::ConfigEvent_Mutex) { |
| 112 | return mMutex; |
| 113 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 114 | const int mType; // event type e.g. CFG_EVENT_IO |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 115 | // mutex associated with mCondition |
| 116 | mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kConfigEvent_Mutex}; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 117 | audio_utils::condition_variable mCondition; // condition for status return |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 118 | |
| 119 | // NO_THREAD_SAFETY_ANALYSIS Can we add GUARDED_BY? |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 120 | status_t mStatus; // status communicated to sender |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 121 | |
| 122 | bool mWaitStatus GUARDED_BY(mutex()); // true if sender is waiting for status |
| 123 | // true if must wait for system ready to enter event queue |
| 124 | bool mRequiresSystemReady GUARDED_BY(mutex()); |
| 125 | |
| 126 | // NO_THREAD_SAFETY_ANALYSIS Can we add GUARDED_BY? |
| 127 | sp<ConfigEventData> mData; // event specific parameter data |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 128 | |
| 129 | protected: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 130 | explicit ConfigEvent(int type, bool requiresSystemReady = false) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 131 | mType(type), mStatus(NO_ERROR), mWaitStatus(false), |
| 132 | mRequiresSystemReady(requiresSystemReady), mData(NULL) {} |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class IoConfigEventData : public ConfigEventData { |
| 136 | public: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 137 | IoConfigEventData(audio_io_config_event_t event, pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 138 | audio_port_handle_t portId) : |
| 139 | mEvent(event), mPid(pid), mPortId(portId) {} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 140 | |
| 141 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 142 | snprintf(buffer, size, "- IO event: event %d\n", mEvent); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 145 | const audio_io_config_event_t mEvent; |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 146 | const pid_t mPid; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 147 | const audio_port_handle_t mPortId; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 150 | class IoConfigEvent : public ConfigEvent { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 151 | public: |
Mikhail Naganov | 88536df | 2021-07-26 17:30:29 -0700 | [diff] [blame] | 152 | IoConfigEvent(audio_io_config_event_t event, pid_t pid, audio_port_handle_t portId) : |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 153 | ConfigEvent(CFG_EVENT_IO) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 154 | mData = new IoConfigEventData(event, pid, portId); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 155 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 156 | }; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 157 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 158 | class PrioConfigEventData : public ConfigEventData { |
| 159 | public: |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 160 | PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) : |
| 161 | mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 162 | |
| 163 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 164 | snprintf(buffer, size, "- Prio event: pid %d, tid %d, prio %d, for app? %d\n", |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 165 | mPid, mTid, mPrio, mForApp); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 168 | const pid_t mPid; |
| 169 | const pid_t mTid; |
| 170 | const int32_t mPrio; |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 171 | const bool mForApp; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 174 | class PrioConfigEvent : public ConfigEvent { |
| 175 | public: |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 176 | PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) : |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 177 | ConfigEvent(CFG_EVENT_PRIO, true) { |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 178 | mData = new PrioConfigEventData(pid, tid, prio, forApp); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 179 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | class SetParameterConfigEventData : public ConfigEventData { |
| 183 | public: |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 184 | explicit SetParameterConfigEventData(const String8& keyValuePairs) : |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 185 | mKeyValuePairs(keyValuePairs) {} |
| 186 | |
| 187 | virtual void dump(char *buffer, size_t size) { |
Tomasz Wasilczyk | 833345b | 2023-08-15 20:59:35 +0000 | [diff] [blame] | 188 | snprintf(buffer, size, "- KeyValue: %s\n", mKeyValuePairs.c_str()); |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | const String8 mKeyValuePairs; |
| 192 | }; |
| 193 | |
| 194 | class SetParameterConfigEvent : public ConfigEvent { |
| 195 | public: |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 196 | explicit SetParameterConfigEvent(const String8& keyValuePairs) : |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 197 | ConfigEvent(CFG_EVENT_SET_PARAMETER) { |
| 198 | mData = new SetParameterConfigEventData(keyValuePairs); |
| 199 | mWaitStatus = true; |
| 200 | } |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 203 | class CreateAudioPatchConfigEventData : public ConfigEventData { |
| 204 | public: |
| 205 | CreateAudioPatchConfigEventData(const struct audio_patch patch, |
| 206 | audio_patch_handle_t handle) : |
| 207 | mPatch(patch), mHandle(handle) {} |
| 208 | |
| 209 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 210 | snprintf(buffer, size, "- Patch handle: %u\n", mHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | const struct audio_patch mPatch; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 214 | audio_patch_handle_t mHandle; // cannot be const |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | class CreateAudioPatchConfigEvent : public ConfigEvent { |
| 218 | public: |
| 219 | CreateAudioPatchConfigEvent(const struct audio_patch patch, |
| 220 | audio_patch_handle_t handle) : |
| 221 | ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) { |
| 222 | mData = new CreateAudioPatchConfigEventData(patch, handle); |
| 223 | mWaitStatus = true; |
| 224 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | class ReleaseAudioPatchConfigEventData : public ConfigEventData { |
| 228 | public: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 229 | explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) : |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 230 | mHandle(handle) {} |
| 231 | |
| 232 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 233 | snprintf(buffer, size, "- Patch handle: %u\n", mHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 236 | const audio_patch_handle_t mHandle; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | class ReleaseAudioPatchConfigEvent : public ConfigEvent { |
| 240 | public: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 241 | explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) : |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 242 | ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) { |
| 243 | mData = new ReleaseAudioPatchConfigEventData(handle); |
| 244 | mWaitStatus = true; |
| 245 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 246 | }; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 247 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 248 | class UpdateOutDevicesConfigEventData : public ConfigEventData { |
| 249 | public: |
| 250 | explicit UpdateOutDevicesConfigEventData(const DeviceDescriptorBaseVector& outDevices) : |
| 251 | mOutDevices(outDevices) {} |
| 252 | |
| 253 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 254 | snprintf(buffer, size, "- Devices: %s", android::toString(mOutDevices).c_str()); |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 257 | const DeviceDescriptorBaseVector mOutDevices; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | class UpdateOutDevicesConfigEvent : public ConfigEvent { |
| 261 | public: |
| 262 | explicit UpdateOutDevicesConfigEvent(const DeviceDescriptorBaseVector& outDevices) : |
| 263 | ConfigEvent(CFG_EVENT_UPDATE_OUT_DEVICE) { |
| 264 | mData = new UpdateOutDevicesConfigEventData(outDevices); |
| 265 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 268 | class ResizeBufferConfigEventData : public ConfigEventData { |
| 269 | public: |
| 270 | explicit ResizeBufferConfigEventData(int32_t maxSharedAudioHistoryMs) : |
| 271 | mMaxSharedAudioHistoryMs(maxSharedAudioHistoryMs) {} |
| 272 | |
| 273 | virtual void dump(char *buffer, size_t size) { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 274 | snprintf(buffer, size, "- mMaxSharedAudioHistoryMs: %d", mMaxSharedAudioHistoryMs); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 277 | const int32_t mMaxSharedAudioHistoryMs; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | class ResizeBufferConfigEvent : public ConfigEvent { |
| 281 | public: |
| 282 | explicit ResizeBufferConfigEvent(int32_t maxSharedAudioHistoryMs) : |
| 283 | ConfigEvent(CFG_EVENT_RESIZE_BUFFER) { |
| 284 | mData = new ResizeBufferConfigEventData(maxSharedAudioHistoryMs); |
| 285 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 286 | }; |
| 287 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 288 | class CheckOutputStageEffectsEvent : public ConfigEvent { |
| 289 | public: |
| 290 | CheckOutputStageEffectsEvent() : |
| 291 | ConfigEvent(CFG_EVENT_CHECK_OUTPUT_STAGE_EFFECTS) { |
| 292 | } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 293 | }; |
| 294 | |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 295 | class HalLatencyModesChangedEvent : public ConfigEvent { |
| 296 | public: |
| 297 | HalLatencyModesChangedEvent() : |
| 298 | ConfigEvent(CFG_EVENT_HAL_LATENCY_MODES_CHANGED) { |
| 299 | } |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 300 | }; |
| 301 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 302 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 303 | class PMDeathRecipient : public IBinder::DeathRecipient { |
| 304 | public: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 305 | explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 306 | |
| 307 | // IBinder::DeathRecipient |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 308 | void binderDied(const wp<IBinder>& who) final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 309 | |
| 310 | private: |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 311 | DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 312 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 313 | const wp<ThreadBase> mThread; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 316 | type_t type() const final { return mType; } |
| 317 | bool isDuplicating() const final { return (mType == DUPLICATING); } |
| 318 | audio_io_handle_t id() const final { return mId;} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 319 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 320 | uint32_t sampleRate() const final { return mSampleRate; } |
| 321 | audio_channel_mask_t channelMask() const final { return mChannelMask; } |
| 322 | audio_channel_mask_t mixerChannelMask() const override { return mChannelMask; } |
| 323 | audio_format_t format() const final { return mHALFormat; } |
| 324 | uint32_t channelCount() const final { return mChannelCount; } |
| 325 | audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 326 | uint32_t hapticChannelCount() const override { return 0; } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 327 | uint32_t latency_l() const override { return 0; } // NO_THREAD_SAFETY_ANALYSIS |
| 328 | void setVolumeForOutput_l(float /* left */, float /* right */) const override |
| 329 | REQUIRES(mutex()) {} |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 330 | |
| 331 | // Return's the HAL's frame count i.e. fast mixer buffer size. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 332 | size_t frameCountHAL() const final { return mFrameCount; } |
| 333 | size_t frameSize() const final { return mFrameSize; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 334 | |
| 335 | // Should be "virtual status_t requestExitAndWait()" and override same |
| 336 | // method in Thread, but Thread::requestExitAndWait() is not yet virtual. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 337 | void exit() final EXCLUDES_ThreadBase_Mutex; |
| 338 | status_t setParameters(const String8& keyValuePairs) final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 339 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 340 | // sendConfigEvent_l() must be called with ThreadBase::mutex() held |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 341 | // Can temporarily release the lock if waiting for a reply from |
| 342 | // processConfigEvents_l(). |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 343 | status_t sendConfigEvent_l(sp<ConfigEvent>& event) REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 344 | void sendIoConfigEvent(audio_io_config_event_t event, pid_t pid = 0, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 345 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 346 | void sendIoConfigEvent_l(audio_io_config_event_t event, pid_t pid = 0, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 347 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final REQUIRES(mutex()); |
| 348 | void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) final |
| 349 | EXCLUDES_ThreadBase_Mutex; |
| 350 | void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp) final |
| 351 | REQUIRES(mutex()); |
| 352 | status_t sendSetParameterConfigEvent_l(const String8& keyValuePair) final REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 353 | status_t sendCreateAudioPatchConfigEvent(const struct audio_patch* patch, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 354 | audio_patch_handle_t* handle) final EXCLUDES_ThreadBase_Mutex; |
| 355 | status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle) final |
| 356 | EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 357 | status_t sendUpdateOutDeviceConfigEvent( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 358 | const DeviceDescriptorBaseVector& outDevices) final EXCLUDES_ThreadBase_Mutex; |
| 359 | void sendResizeBufferConfigEvent_l(int32_t maxSharedAudioHistoryMs) final REQUIRES(mutex()); |
| 360 | void sendCheckOutputStageEffectsEvent() final EXCLUDES_ThreadBase_Mutex; |
| 361 | void sendCheckOutputStageEffectsEvent_l() final REQUIRES(mutex()); |
| 362 | void sendHalLatencyModesChangedEvent_l() final REQUIRES(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 363 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 364 | void processConfigEvents_l() final REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 365 | void setCheckOutputStageEffects() override {} |
| 366 | void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override; |
| 367 | void toAudioPortConfig(struct audio_port_config* config) override; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 368 | void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override REQUIRES(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 369 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 370 | // see note at declaration of mStandby, mOutDevice and mInDevice |
| 371 | bool inStandby() const override { return mStandby; } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 372 | const DeviceTypeSet outDeviceTypes_l() const final REQUIRES(mutex()) { |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 373 | return getAudioDeviceTypes(mOutDeviceTypeAddrs); |
| 374 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 375 | audio_devices_t inDeviceType_l() const final REQUIRES(mutex()) { |
| 376 | return mInDeviceTypeAddr.mType; |
| 377 | } |
| 378 | DeviceTypeSet getDeviceTypes_l() const final REQUIRES(mutex()) { |
| 379 | return isOutput() ? outDeviceTypes_l() : DeviceTypeSet({inDeviceType_l()}); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 380 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 381 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 382 | const AudioDeviceTypeAddrVector& outDeviceTypeAddrs() const final { |
| 383 | return mOutDeviceTypeAddrs; |
| 384 | } |
| 385 | const AudioDeviceTypeAddr& inDeviceTypeAddr() const final { |
| 386 | return mInDeviceTypeAddr; |
| 387 | } |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 388 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 389 | bool isOutput() const final { return mIsOut; } |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 390 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 391 | bool isOffloadOrMmap() const final { |
| 392 | switch (mType) { |
| 393 | case OFFLOAD: |
| 394 | case MMAP_PLAYBACK: |
| 395 | case MMAP_CAPTURE: |
| 396 | return true; |
| 397 | default: |
| 398 | return false; |
| 399 | } |
| 400 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 401 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 402 | sp<IAfEffectHandle> createEffect_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 403 | const sp<Client>& client, |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 404 | const sp<media::IEffectClient>& effectClient, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 405 | int32_t priority, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 406 | audio_session_t sessionId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 407 | effect_descriptor_t *desc, |
| 408 | int *enabled, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 409 | status_t *status /*non-NULL*/, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 410 | bool pinned, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 411 | bool probe, |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 412 | bool notifyFramesProcessed) final |
| 413 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 414 | |
| 415 | // return values for hasAudioSession (bit field) |
| 416 | enum effect_state { |
| 417 | EFFECT_SESSION = 0x1, // the audio session corresponds to at least one |
| 418 | // effect |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 419 | TRACK_SESSION = 0x2, // the audio session corresponds to at least one |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 420 | // track |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 421 | FAST_SESSION = 0x4, // the audio session corresponds to at least one |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 422 | // fast track |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 423 | SPATIALIZED_SESSION = 0x8, // the audio session corresponds to at least one |
| 424 | // spatialized track |
| 425 | BIT_PERFECT_SESSION = 0x10 // the audio session corresponds to at least one |
| 426 | // bit-perfect track |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 427 | }; |
| 428 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 429 | // get effect chain corresponding to session Id. |
| 430 | sp<IAfEffectChain> getEffectChain(audio_session_t sessionId) const final; |
| 431 | // same as getEffectChain() but must be called with ThreadBase mutex locked |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 432 | sp<IAfEffectChain> getEffectChain_l(audio_session_t sessionId) const final REQUIRES(mutex()); |
| 433 | std::vector<int> getEffectIds_l(audio_session_t sessionId) const final REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 434 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 435 | // lock all effect chains Mutexes. Must be called before releasing the |
| 436 | // ThreadBase mutex before processing the mixer and effects. This guarantees the |
| 437 | // integrity of the chains during the process. |
| 438 | // Also sets the parameter 'effectChains' to current value of mEffectChains. |
Shunkai Yao | f484765 | 2024-01-12 00:25:20 +0000 | [diff] [blame^] | 439 | void lockEffectChains_l(Vector<sp<IAfEffectChain>>& effectChains) final |
| 440 | REQUIRES(mutex()) EXCLUDES_EffectChain_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 441 | // unlock effect chains after process |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 442 | void unlockEffectChains(const Vector<sp<IAfEffectChain>>& effectChains) final; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 443 | // get a copy of mEffectChains vector |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 444 | Vector<sp<IAfEffectChain>> getEffectChains_l() const final REQUIRES(mutex()) { |
| 445 | return mEffectChains; |
| 446 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 447 | // set audio mode to all effect chains |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 448 | void setMode(audio_mode_t mode) final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 449 | // get effect module with corresponding ID on specified audio session |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 450 | sp<IAfEffectModule> getEffect(audio_session_t sessionId, int effectId) const final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 451 | sp<IAfEffectModule> getEffect_l(audio_session_t sessionId, int effectId) const final |
| 452 | REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 453 | // add and effect module. Also creates the effect chain is none exists for |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 454 | // the effects audio session. Only called in a context of moving an effect |
| 455 | // from one thread to another |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 456 | status_t addEffect_ll(const sp<IAfEffectModule>& effect) final |
| 457 | REQUIRES(audio_utils::AudioFlinger_Mutex, mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 458 | // remove and effect module. Also removes the effect chain is this was the last |
| 459 | // effect |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 460 | void removeEffect_l(const sp<IAfEffectModule>& effect, bool release = false) final |
| 461 | REQUIRES(mutex()); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 462 | // disconnect an effect handle from module and destroy module if last handle |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 463 | void disconnectEffectHandle(IAfEffectHandle* handle, bool unpinIfLast) final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 464 | // detach all tracks connected to an auxiliary effect |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 465 | void detachAuxEffect_l(int /* effectId */) override REQUIRES(mutex()) {} |
Andy Hung | 99b1ba6 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 466 | // TODO(b/291317898) - remove hasAudioSession_l below. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 467 | uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) = 0; |
| 468 | uint32_t hasAudioSession(audio_session_t sessionId) const final EXCLUDES_ThreadBase_Mutex { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 469 | audio_utils::lock_guard _l(mutex()); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 470 | return hasAudioSession_l(sessionId); |
| 471 | } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 472 | |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 473 | template <typename T> |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 474 | uint32_t hasAudioSession_l(audio_session_t sessionId, const T& tracks) const |
| 475 | REQUIRES(mutex()) { |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 476 | uint32_t result = 0; |
| 477 | if (getEffectChain_l(sessionId) != 0) { |
| 478 | result = EFFECT_SESSION; |
| 479 | } |
| 480 | for (size_t i = 0; i < tracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 481 | const sp<IAfTrackBase>& track = tracks[i]; |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 482 | if (sessionId == track->sessionId() |
| 483 | && !track->isInvalid() // not yet removed from tracks. |
| 484 | && !track->isTerminated()) { |
| 485 | result |= TRACK_SESSION; |
| 486 | if (track->isFastTrack()) { |
| 487 | result |= FAST_SESSION; // caution, only represents first track. |
| 488 | } |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 489 | if (track->isSpatialized()) { |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 490 | result |= SPATIALIZED_SESSION; // caution, only first track. |
| 491 | } |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 492 | if (track->isBitPerfect()) { |
| 493 | result |= BIT_PERFECT_SESSION; |
| 494 | } |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | } |
| 498 | return result; |
| 499 | } |
| 500 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 501 | // the value returned by default implementation is not important as the |
| 502 | // strategy is only meaningful for PlaybackThread which implements this method |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 503 | product_strategy_t getStrategyForSession_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 504 | audio_session_t /* sessionId */) const override REQUIRES(mutex()){ |
| 505 | return static_cast<product_strategy_t>(0); |
| 506 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 507 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 508 | // check if some effects must be suspended/restored when an effect is enabled |
| 509 | // or disabled |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 510 | void checkSuspendOnEffectEnabled(bool enabled, |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 511 | audio_session_t sessionId, |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 512 | bool threadLocked) final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 513 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 514 | |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 515 | // Return a reference to a per-thread heap which can be used to allocate IMemory |
| 516 | // objects that will be read-only to client processes, read/write to mediaserver, |
| 517 | // and shared by all client processes of the thread. |
| 518 | // The heap is per-thread rather than common across all threads, because |
| 519 | // clients can't be trusted not to modify the offset of the IMemory they receive. |
| 520 | // If a thread does not have such a heap, this method returns 0. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 521 | sp<MemoryDealer> readOnlyHeap() const override { return nullptr; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 523 | sp<IMemory> pipeMemory() const override { return nullptr; } |
Glenn Kasten | 6181ffd | 2014-05-13 10:41:52 -0700 | [diff] [blame] | 524 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 525 | void systemReady() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 526 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 527 | void broadcast_l() final REQUIRES(mutex()); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 528 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 529 | bool isTimestampCorrectionEnabled_l() const override REQUIRES(mutex()) { return false; } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 530 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 531 | bool isMsdDevice() const final { return mIsMsdDevice; } |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 532 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 533 | void dump(int fd, const Vector<String16>& args) override; |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 534 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 535 | // deliver stats to mediametrics. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 536 | void sendStatistics(bool force) final |
| 537 | REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex; |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 538 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 539 | audio_utils::mutex& mutex() const final RETURN_CAPABILITY(audio_utils::ThreadBase_Mutex) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 540 | return mMutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 541 | } |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 542 | mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kThreadBase_Mutex}; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 543 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 544 | void onEffectEnable(const sp<IAfEffectModule>& effect) final EXCLUDES_ThreadBase_Mutex; |
| 545 | void onEffectDisable() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 546 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 547 | // invalidateTracksForAudioSession_l must be called with holding mutex(). |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 548 | void invalidateTracksForAudioSession_l(audio_session_t /* sessionId */) const override |
| 549 | REQUIRES(mutex()) {} |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 550 | // Invalidate all the tracks with the given audio session. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 551 | void invalidateTracksForAudioSession(audio_session_t sessionId) const final |
| 552 | EXCLUDES_ThreadBase_Mutex { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 553 | audio_utils::lock_guard _l(mutex()); |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 554 | invalidateTracksForAudioSession_l(sessionId); |
| 555 | } |
| 556 | |
| 557 | template <typename T> |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 558 | void invalidateTracksForAudioSession_l(audio_session_t sessionId, |
| 559 | const T& tracks) const REQUIRES(mutex()) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 560 | for (size_t i = 0; i < tracks.size(); ++i) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 561 | const sp<IAfTrackBase>& track = tracks[i]; |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 562 | if (sessionId == track->sessionId()) { |
| 563 | track->invalidate(); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 568 | void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override |
| 569 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
| 570 | void stopMelComputation_l() override |
| 571 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 572 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 573 | protected: |
| 574 | |
| 575 | // entry describing an effect being suspended in mSuspendedSessions keyed vector |
| 576 | class SuspendedSessionDesc : public RefBase { |
| 577 | public: |
| 578 | SuspendedSessionDesc() : mRefCount(0) {} |
| 579 | |
| 580 | int mRefCount; // number of active suspend requests |
| 581 | effect_uuid_t mType; // effect type UUID |
| 582 | }; |
| 583 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 584 | void acquireWakeLock() EXCLUDES_ThreadBase_Mutex; |
| 585 | virtual void acquireWakeLock_l() REQUIRES(mutex()); |
| 586 | void releaseWakeLock() EXCLUDES_ThreadBase_Mutex; |
| 587 | void releaseWakeLock_l() REQUIRES(mutex()); |
| 588 | void updateWakeLockUids_l(const SortedVector<uid_t> &uids) REQUIRES(mutex()); |
| 589 | void getPowerManager_l() REQUIRES(mutex()); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 590 | // suspend or restore effects of the specified type (or all if type is NULL) |
| 591 | // on a given session. The number of suspend requests is counted and restore |
| 592 | // occurs when all suspend requests are cancelled. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 593 | void setEffectSuspended_l(const effect_uuid_t *type, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 594 | bool suspend, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 595 | audio_session_t sessionId) final REQUIRES(mutex()); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 596 | // updated mSuspendedSessions when an effect is suspended or restored |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 597 | void updateSuspendedSessions_l(const effect_uuid_t *type, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 598 | bool suspend, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 599 | audio_session_t sessionId) REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 600 | // check if some effects must be suspended when an effect chain is added |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 601 | void checkSuspendOnAddEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 602 | |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 603 | /** |
| 604 | * waitWhileThreadBusy_l() serves as a mutex gate, which does not allow |
| 605 | * progress beyond the method while the PlaybackThread is busy (see setThreadBusy_l()). |
| 606 | * During the wait, the ThreadBase_Mutex is temporarily unlocked. |
| 607 | * |
| 608 | * This implementation uses a condition variable. Alternative methods to gate |
| 609 | * the thread may use a second mutex (i.e. entry based on scoped_lock(mutex, gating_mutex)), |
| 610 | * but those have less flexibility and more lock order issues. |
| 611 | * |
| 612 | * Current usage by Track::destroy(), Track::start(), Track::stop(), Track::pause(), |
| 613 | * and Track::flush() block this way, and the primary caller is through TrackHandle |
| 614 | * with no other mutexes held. |
| 615 | * |
| 616 | * Special tracks like PatchTrack and OutputTrack may also hold the another thread's |
| 617 | * ThreadBase_Mutex during this time. No other mutex is held. |
| 618 | */ |
| 619 | |
| 620 | void waitWhileThreadBusy_l(audio_utils::unique_lock& ul) final REQUIRES(mutex()) { |
| 621 | // the wait returns immediately if the predicate is satisfied. |
| 622 | mThreadBusyCv.wait(ul, [&]{ return mThreadBusy == false;}); |
| 623 | } |
| 624 | |
| 625 | void setThreadBusy_l(bool busy) REQUIRES(mutex()) { |
| 626 | if (busy == mThreadBusy) return; |
| 627 | mThreadBusy = busy; |
| 628 | if (busy == true) return; // no need to wake threads if we become busy. |
| 629 | mThreadBusyCv.notify_all(); |
| 630 | } |
| 631 | |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 632 | // sends the metadata of the active tracks to the HAL |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 633 | struct MetadataUpdate { |
| 634 | std::vector<playback_track_metadata_v7_t> playbackMetadataUpdate; |
| 635 | std::vector<record_track_metadata_v7_t> recordMetadataUpdate; |
| 636 | }; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 637 | // NO_THREAD_SAFETY_ANALYSIS, updateMetadata_l() should include ThreadBase_ThreadLoop |
| 638 | // but MmapThread::start() -> exitStandby_l() -> updateMetadata_l() prevents this. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 639 | virtual MetadataUpdate updateMetadata_l() REQUIRES(mutex()) = 0; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 640 | |
Narayan Kamath | 014e7fa | 2013-10-14 15:03:38 +0100 | [diff] [blame] | 641 | String16 getWakeLockTag(); |
| 642 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 643 | virtual void preExit() EXCLUDES_ThreadBase_Mutex {} |
| 644 | virtual void setMasterMono_l(bool mono __unused) REQUIRES(mutex()) {} |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 645 | virtual bool requireMonoBlend() { return false; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 646 | |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 647 | // called within the threadLoop to obtain timestamp from the HAL. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 648 | virtual status_t threadloop_getHalTimestamp_l( |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 649 | ExtendedTimestamp *timestamp __unused) const |
| 650 | REQUIRES(mutex(), ThreadBase_ThreadLoop) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 651 | return INVALID_OPERATION; |
| 652 | } |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 653 | public: |
Andy Hung | 99b1ba6 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 654 | // TODO(b/291317898) organize with publics |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 655 | product_strategy_t getStrategyForStream(audio_stream_type_t stream) const; |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 656 | protected: |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 657 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 658 | virtual void onHalLatencyModesChanged_l() REQUIRES(mutex()) {} |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 659 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 660 | virtual void dumpInternals_l(int fd __unused, const Vector<String16>& args __unused) |
| 661 | REQUIRES(mutex()) {} |
| 662 | virtual void dumpTracks_l(int fd __unused, const Vector<String16>& args __unused) |
| 663 | REQUIRES(mutex()) {} |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 664 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 665 | const type_t mType; |
| 666 | |
| 667 | // Used by parameters, config events, addTrack_l, exit |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 668 | audio_utils::condition_variable mWaitWorkCV; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 669 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 670 | const sp<IAfThreadCallback> mAfThreadCallback; |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 671 | ThreadMetrics mThreadMetrics; |
| 672 | const bool mIsOut; |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 673 | |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 674 | // mThreadBusy is checked under the ThreadBase_Mutex to ensure that |
| 675 | // TrackHandle operations do not proceed while the ThreadBase is busy |
| 676 | // with the track. mThreadBusy is only true if the track is active. |
| 677 | // |
| 678 | bool mThreadBusy = false; // GUARDED_BY(ThreadBase_Mutex) but read in lambda. |
| 679 | audio_utils::condition_variable mThreadBusyCv; |
| 680 | |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 681 | // updated by PlaybackThread::readOutputParameters_l() or |
| 682 | // RecordThread::readInputParameters_l() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 683 | uint32_t mSampleRate; |
| 684 | size_t mFrameCount; // output HAL, direct output, record |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 685 | audio_channel_mask_t mChannelMask; |
Glenn Kasten | f6ed423 | 2013-07-16 11:16:27 -0700 | [diff] [blame] | 686 | uint32_t mChannelCount; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 687 | size_t mFrameSize; |
Glenn Kasten | 97b7b75 | 2014-09-28 13:04:24 -0700 | [diff] [blame] | 688 | // not HAL frame size, this is for output sink (to pipe to fast mixer) |
Andy Hung | 463be25 | 2014-07-10 16:56:07 -0700 | [diff] [blame] | 689 | audio_format_t mFormat; // Source format for Recording and |
| 690 | // Sink format for Playback. |
| 691 | // Sink format may be different than |
| 692 | // HAL format if Fastmixer is used. |
| 693 | audio_format_t mHALFormat; |
Glenn Kasten | 70949c4 | 2013-08-06 07:40:12 -0700 | [diff] [blame] | 694 | size_t mBufferSize; // HAL buffer size for read() or write() |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 695 | |
| 696 | // output device types and addresses |
| 697 | AudioDeviceTypeAddrVector mOutDeviceTypeAddrs GUARDED_BY(mutex()); |
| 698 | AudioDeviceTypeAddr mInDeviceTypeAddr GUARDED_BY(mutex()); // input device type and address |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 699 | Vector<sp<ConfigEvent>> mConfigEvents GUARDED_BY(mutex()); |
| 700 | |
| 701 | // events awaiting system ready |
| 702 | Vector<sp<ConfigEvent>> mPendingConfigEvents GUARDED_BY(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 703 | |
| 704 | // These fields are written and read by thread itself without lock or barrier, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 705 | // and read by other threads without lock or barrier via standby(), outDeviceTypes() |
| 706 | // and inDeviceType(). |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 707 | // Because of the absence of a lock or barrier, any other thread that reads |
| 708 | // these fields must use the information in isolation, or be prepared to deal |
| 709 | // with possibility that it might be inconsistent with other information. |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 710 | bool mStandby; // Whether thread is currently in standby. |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 711 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 712 | // NO_THREAD_SAFETY_ANALYSIS - mPatch and mAudioSource should be guarded by mutex(). |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 713 | struct audio_patch mPatch; |
Glenn Kasten | f59497b | 2015-01-26 16:35:47 -0800 | [diff] [blame] | 714 | audio_source_t mAudioSource; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 715 | |
| 716 | const audio_io_handle_t mId; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 717 | Vector<sp<IAfEffectChain>> mEffectChains GUARDED_BY(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 718 | |
Glenn Kasten | d7dca05 | 2015-03-05 16:05:54 -0800 | [diff] [blame] | 719 | static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit |
| 720 | char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 721 | sp<os::IPowerManager> mPowerManager GUARDED_BY(mutex()); |
| 722 | sp<IBinder> mWakeLockToken GUARDED_BY(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 723 | const sp<PMDeathRecipient> mDeathRecipient; |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 724 | // list of suspended effects per session and per type. The first (outer) vector is |
| 725 | // keyed by session ID, the second (inner) by type UUID timeLow field |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 726 | // Updated by updateSuspendedSessions_l() only. |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 727 | KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > > |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 728 | mSuspendedSessions; |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 729 | // TODO: add comment and adjust size as needed |
Glenn Kasten | ab7d72f | 2013-02-27 09:05:28 -0800 | [diff] [blame] | 730 | static const size_t kLogSize = 4 * 1024; |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 731 | sp<NBLog::Writer> mNBLogWriter; |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 732 | bool mSystemReady; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 733 | |
| 734 | // NO_THREAD_SAFETY_ANALYSIS - mTimestamp and mTimestampVerifier should be |
| 735 | // accessed under mutex for the RecordThread. |
| 736 | ExtendedTimestamp mTimestamp; |
| 737 | TimestampVerifier<int64_t /* frame count */, int64_t /* time ns */> mTimestampVerifier; |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 738 | // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush |
| 739 | // TODO: add confirmation checks: |
| 740 | // 1) DIRECT threads and linear PCM format really resets to 0? |
| 741 | // 2) Is frame count really valid if not linear pcm? |
| 742 | // 3) Are all 64 bits of position returned, not just lowest 32 bits? |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 743 | // Timestamp corrected device should be a single device. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 744 | |
| 745 | audio_devices_t mTimestampCorrectedDevice = AUDIO_DEVICE_NONE; // CONST set in ctor |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 746 | |
| 747 | // ThreadLoop statistics per iteration. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 748 | std::atomic<int64_t> mLastIoBeginNs = -1; // set in threadLoop, read by dump() |
| 749 | int64_t mLastIoEndNs GUARDED_BY(ThreadBase_ThreadLoop) = -1; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 750 | |
Andy Hung | 44d648b | 2022-04-08 17:33:40 -0700 | [diff] [blame] | 751 | // ThreadSnapshot is thread-safe (internally locked) |
| 752 | mediautils::ThreadSnapshot mThreadSnapshot; |
| 753 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 754 | audio_utils::Statistics<double> mIoJitterMs GUARDED_BY(mutex()) {0.995 /* alpha */}; |
| 755 | audio_utils::Statistics<double> mProcessTimeMs GUARDED_BY(mutex()) {0.995 /* alpha */}; |
| 756 | |
| 757 | // NO_THREAD_SAFETY_ANALYSIS GUARDED_BY(mutex()) |
Andy Hung | e6c3711 | 2019-02-26 17:38:10 -0800 | [diff] [blame] | 758 | audio_utils::Statistics<double> mLatencyMs{0.995 /* alpha */}; |
Robert Wu | 06db0a3 | 2021-08-10 19:05:34 +0000 | [diff] [blame] | 759 | audio_utils::Statistics<double> mMonopipePipeDepthStats{0.999 /* alpha */}; |
Andy Hung | 446f4df | 2019-02-21 12:26:41 -0800 | [diff] [blame] | 760 | |
Andy Hung | d097981 | 2019-02-21 15:51:44 -0800 | [diff] [blame] | 761 | // Save the last count when we delivered statistics to mediametrics. |
| 762 | int64_t mLastRecordedTimestampVerifierN = 0; |
| 763 | int64_t mLastRecordedTimeNs = 0; // BOOTTIME to include suspend. |
| 764 | |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 765 | bool mIsMsdDevice = false; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 766 | // A condition that must be evaluated by the thread loop has changed and |
| 767 | // we must not wait for async write callback in the thread loop before evaluating it |
| 768 | bool mSignalPending; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 769 | |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 770 | #ifdef TEE_SINK |
| 771 | NBAIO_Tee mTee; |
| 772 | #endif |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 773 | // ActiveTracks is a sorted vector of track type T representing the |
| 774 | // active tracks of threadLoop() to be considered by the locked prepare portion. |
| 775 | // ActiveTracks should be accessed with the ThreadBase lock held. |
| 776 | // |
| 777 | // During processing and I/O, the threadLoop does not hold the lock; |
| 778 | // hence it does not directly use ActiveTracks. Care should be taken |
| 779 | // to hold local strong references or defer removal of tracks |
| 780 | // if the threadLoop may still be accessing those tracks due to mix, etc. |
| 781 | // |
| 782 | // This class updates power information appropriately. |
| 783 | // |
| 784 | |
| 785 | template <typename T> |
| 786 | class ActiveTracks { |
| 787 | public: |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 788 | explicit ActiveTracks(SimpleLog *localLog = nullptr) |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 789 | : mActiveTracksGeneration(0) |
| 790 | , mLastActiveTracksGeneration(0) |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 791 | , mLocalLog(localLog) |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 792 | { } |
| 793 | |
| 794 | ~ActiveTracks() { |
| 795 | ALOGW_IF(!mActiveTracks.isEmpty(), |
| 796 | "ActiveTracks should be empty in destructor"); |
| 797 | } |
| 798 | // returns the last track added (even though it may have been |
| 799 | // subsequently removed from ActiveTracks). |
| 800 | // |
| 801 | // Used for DirectOutputThread to ensure a flush is called when transitioning |
| 802 | // to a new track (even though it may be on the same session). |
| 803 | // Used for OffloadThread to ensure that volume and mixer state is |
| 804 | // taken from the latest track added. |
| 805 | // |
| 806 | // The latest track is saved with a weak pointer to prevent keeping an |
| 807 | // otherwise useless track alive. Thus the function will return nullptr |
| 808 | // if the latest track has subsequently been removed and destroyed. |
| 809 | sp<T> getLatest() { |
| 810 | return mLatestActiveTrack.promote(); |
| 811 | } |
| 812 | |
| 813 | // SortedVector methods |
| 814 | ssize_t add(const sp<T> &track); |
| 815 | ssize_t remove(const sp<T> &track); |
| 816 | size_t size() const { |
| 817 | return mActiveTracks.size(); |
| 818 | } |
Eric Tan | 39ec8d6 | 2018-07-24 09:49:29 -0700 | [diff] [blame] | 819 | bool isEmpty() const { |
| 820 | return mActiveTracks.isEmpty(); |
| 821 | } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 822 | ssize_t indexOf(const sp<T>& item) const { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 823 | return mActiveTracks.indexOf(item); |
| 824 | } |
| 825 | sp<T> operator[](size_t index) const { |
| 826 | return mActiveTracks[index]; |
| 827 | } |
| 828 | typename SortedVector<sp<T>>::iterator begin() { |
| 829 | return mActiveTracks.begin(); |
| 830 | } |
| 831 | typename SortedVector<sp<T>>::iterator end() { |
| 832 | return mActiveTracks.end(); |
| 833 | } |
| 834 | |
| 835 | // Due to Binder recursion optimization, clear() and updatePowerState() |
| 836 | // cannot be called from a Binder thread because they may call back into |
| 837 | // the original calling process (system server) for BatteryNotifier |
| 838 | // (which requires a Java environment that may not be present). |
| 839 | // Hence, call clear() and updatePowerState() only from the |
| 840 | // ThreadBase thread. |
| 841 | void clear(); |
| 842 | // periodically called in the threadLoop() to update power state uids. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 843 | void updatePowerState_l(const sp<ThreadBase>& thread, bool force = false) |
| 844 | REQUIRES(audio_utils::ThreadBase_Mutex); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 845 | |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 846 | /** @return true if one or move active tracks was added or removed since the |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 847 | * last time this function was called or the vector was created. |
| 848 | * true if volume of one of active tracks was changed. |
| 849 | */ |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 850 | bool readAndClearHasChanged(); |
| 851 | |
Eric Laurent | dda206a | 2022-07-08 17:28:35 +0200 | [diff] [blame] | 852 | /** Force updating track metadata to audio HAL stream next time |
| 853 | * readAndClearHasChanged() is called. |
| 854 | */ |
| 855 | void setHasChanged() { mHasChanged = true; } |
| 856 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 857 | private: |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 858 | void logTrack(const char *funcName, const sp<T> &track) const; |
| 859 | |
Andy Hung | d01b0f1 | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 860 | SortedVector<uid_t> getWakeLockUids() { |
| 861 | SortedVector<uid_t> wakeLockUids; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 862 | for (const sp<T> &track : mActiveTracks) { |
| 863 | wakeLockUids.add(track->uid()); |
| 864 | } |
| 865 | return wakeLockUids; // moved by underlying SharedBuffer |
| 866 | } |
| 867 | |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 868 | SortedVector<sp<T>> mActiveTracks; |
| 869 | int mActiveTracksGeneration; |
| 870 | int mLastActiveTracksGeneration; |
| 871 | wp<T> mLatestActiveTrack; // latest track added to ActiveTracks |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 872 | SimpleLog * const mLocalLog; |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 873 | // If the vector has changed since last call to readAndClearHasChanged |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 874 | bool mHasChanged = false; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 875 | }; |
Andy Hung | 293558a | 2017-03-21 12:19:20 -0700 | [diff] [blame] | 876 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 877 | SimpleLog mLocalLog; // locked internally |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 878 | |
Andy Hung | 6c498e9 | 2023-12-05 17:28:17 -0800 | [diff] [blame] | 879 | private: |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 880 | void dumpBase_l(int fd, const Vector<String16>& args) REQUIRES(mutex()); |
| 881 | void dumpEffectChains_l(int fd, const Vector<String16>& args) REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 882 | }; |
| 883 | |
| 884 | // --- PlaybackThread --- |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 885 | class PlaybackThread : public ThreadBase, public virtual IAfPlaybackThread, |
| 886 | public StreamOutHalInterfaceCallback, |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 887 | public virtual VolumeInterface, public StreamOutHalInterfaceEventCallback { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 888 | public: |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 889 | sp<IAfPlaybackThread> asIAfPlaybackThread() final { |
| 890 | return sp<IAfPlaybackThread>::fromExisting(this); |
| 891 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 892 | |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 893 | // retry count before removing active track in case of underrun on offloaded thread: |
| 894 | // we need to make sure that AudioTrack client has enough time to send large buffers |
| 895 | //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is |
| 896 | // handled for offloaded tracks |
| 897 | static const int8_t kMaxTrackRetriesOffload = 20; |
| 898 | static const int8_t kMaxTrackStartupRetriesOffload = 100; |
Andy Hung | 8ed196a | 2018-01-05 13:21:11 -0800 | [diff] [blame] | 899 | static constexpr uint32_t kMaxTracksPerUid = 40; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 900 | static constexpr size_t kMaxTracks = 256; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 901 | |
rago | 1bb9082 | 2017-05-02 18:31:48 -0700 | [diff] [blame] | 902 | // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise |
| 903 | // if delay is greater, the estimated time for timeLoopNextNs is reset. |
| 904 | // This allows for catch-up to be done for small delays, while resetting the estimate |
| 905 | // for initial conditions or large delays. |
| 906 | static const nsecs_t kMaxNextBufferDelayNs = 100000000; |
| 907 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 908 | PlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 909 | audio_io_handle_t id, type_t type, bool systemReady, |
| 910 | audio_config_base_t *mixerConfig = nullptr); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 911 | ~PlaybackThread() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 912 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 913 | // Thread virtuals |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 914 | bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 915 | |
| 916 | // RefBase |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 917 | void onFirstRef() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 918 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 919 | status_t checkEffectCompatibility_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 920 | const effect_descriptor_t* desc, audio_session_t sessionId) final REQUIRES(mutex()); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 921 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 922 | void addOutputTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 923 | mTracks.add(track); |
| 924 | } |
| 925 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 926 | protected: |
| 927 | // Code snippets that were lifted up out of threadLoop() |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 928 | virtual void threadLoop_mix() REQUIRES(ThreadBase_ThreadLoop) = 0; |
| 929 | virtual void threadLoop_sleepTime() REQUIRES(ThreadBase_ThreadLoop) = 0; |
| 930 | virtual ssize_t threadLoop_write() REQUIRES(ThreadBase_ThreadLoop); |
| 931 | virtual void threadLoop_drain() REQUIRES(ThreadBase_ThreadLoop); |
| 932 | virtual void threadLoop_standby() REQUIRES(ThreadBase_ThreadLoop); |
| 933 | virtual void threadLoop_exit() REQUIRES(ThreadBase_ThreadLoop); |
| 934 | virtual void threadLoop_removeTracks(const Vector<sp<IAfTrack>>& tracksToRemove) |
| 935 | REQUIRES(ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 936 | |
| 937 | // prepareTracks_l reads and writes mActiveTracks, and returns |
| 938 | // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller |
| 939 | // is responsible for clearing or destroying this Vector later on, when it |
| 940 | // is safe to do so. That will drop the final ref count and destroy the tracks. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 941 | virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) |
| 942 | REQUIRES(mutex(), ThreadBase_ThreadLoop) = 0; |
| 943 | |
| 944 | void removeTracks_l(const Vector<sp<IAfTrack>>& tracksToRemove) REQUIRES(mutex()); |
| 945 | status_t handleVoipVolume_l(float *volume) REQUIRES(mutex()); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 946 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 947 | // StreamOutHalInterfaceCallback implementation |
| 948 | virtual void onWriteReady(); |
| 949 | virtual void onDrainReady(); |
| 950 | virtual void onError(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 951 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 952 | public: // AsyncCallbackThread |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 953 | void resetWriteBlocked(uint32_t sequence); |
| 954 | void resetDraining(uint32_t sequence); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 955 | protected: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 956 | |
| 957 | virtual bool waitingAsyncCallback(); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 958 | virtual bool waitingAsyncCallback_l() REQUIRES(mutex()); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 959 | virtual bool shouldStandby_l() REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 960 | virtual void onAddNewTrack_l() REQUIRES(mutex()); |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 961 | public: // AsyncCallbackThread |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 962 | void onAsyncError(); // error reported by AsyncCallbackThread |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 963 | protected: |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 964 | // StreamHalInterfaceCodecFormatCallback implementation |
| 965 | void onCodecFormatChanged( |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 966 | const std::basic_string<uint8_t>& metadataBs) final; |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 967 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 968 | // ThreadBase virtuals |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 969 | void preExit() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 970 | |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 971 | virtual bool keepWakeLock() const { return true; } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 972 | virtual void acquireWakeLock_l() REQUIRES(mutex()) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 973 | ThreadBase::acquireWakeLock_l(); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 974 | mActiveTracks.updatePowerState_l(this, true /* force */); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 975 | } |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 976 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 977 | virtual void checkOutputStageEffects() |
| 978 | REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex {} |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 979 | virtual void setHalLatencyMode_l() {} |
| 980 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 981 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 982 | void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
| 983 | void dumpTracks_l(int fd, const Vector<String16>& args) final REQUIRES(mutex()); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 984 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 985 | public: |
| 986 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 987 | status_t initCheck() const final { return mOutput == nullptr ? NO_INIT : NO_ERROR; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 988 | |
| 989 | // return estimated latency in milliseconds, as reported by HAL |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 990 | uint32_t latency() const final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 991 | // same, but lock must already be held |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 992 | uint32_t latency_l() const final /* REQUIRES(mutex()) */; // NO_THREAD_SAFETY_ANALYSIS |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 993 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 994 | // VolumeInterface |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 995 | void setMasterVolume(float value) final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 996 | void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 997 | void setMasterMute(bool muted) final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 998 | void setStreamVolume(audio_stream_type_t stream, float value) final EXCLUDES_ThreadBase_Mutex; |
| 999 | void setStreamMute(audio_stream_type_t stream, bool muted) final EXCLUDES_ThreadBase_Mutex; |
| 1000 | float streamVolume(audio_stream_type_t stream) const final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1001 | void setVolumeForOutput_l(float left, float right) const final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1002 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1003 | sp<IAfTrack> createTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 1004 | const sp<Client>& client, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1005 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 1006 | const audio_attributes_t& attr, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1007 | uint32_t *sampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1008 | audio_format_t format, |
| 1009 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1010 | size_t *pFrameCount, |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1011 | size_t *pNotificationFrameCount, |
| 1012 | uint32_t notificationsPerBuffer, |
| 1013 | float speed, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1014 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1015 | audio_session_t sessionId, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1016 | audio_output_flags_t *flags, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1017 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1018 | const AttributionSourceState& attributionSource, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1019 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1020 | status_t *status /*non-NULL*/, |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 1021 | audio_port_handle_t portId, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1022 | const sp<media::IAudioTrackCallback>& callback, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1023 | bool isSpatialized, |
jiabin | 94ed47c | 2023-07-27 23:34:20 +0000 | [diff] [blame] | 1024 | bool isBitPerfect, |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1025 | audio_output_flags_t* afTrackFlags) final |
| 1026 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1027 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1028 | bool isTrackActive(const sp<IAfTrack>& track) const final { |
| 1029 | return mActiveTracks.indexOf(track) >= 0; |
| 1030 | } |
| 1031 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1032 | AudioStreamOut* getOutput_l() const final REQUIRES(mutex()) { return mOutput; } |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1033 | AudioStreamOut* getOutput() const final EXCLUDES_ThreadBase_Mutex; |
| 1034 | AudioStreamOut* clearOutput() final EXCLUDES_ThreadBase_Mutex; |
| 1035 | |
| 1036 | // NO_THREAD_SAFETY_ANALYSIS -- probably needs a lock. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1037 | sp<StreamHalInterface> stream() const final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1038 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1039 | // suspend(), restore(), and isSuspended() are implemented atomically. |
| 1040 | void suspend() final { ++mSuspended; } |
| 1041 | void restore() final { |
| 1042 | // if restore() is done without suspend(), get back into |
| 1043 | // range so that the next suspend() will operate correctly |
| 1044 | while (true) { |
| 1045 | int32_t suspended = mSuspended; |
| 1046 | if (suspended <= 0) { |
| 1047 | ALOGW("%s: invalid mSuspended %d <= 0", __func__, suspended); |
| 1048 | return; |
| 1049 | } |
| 1050 | const int32_t desired = suspended - 1; |
| 1051 | if (mSuspended.compare_exchange_weak(suspended, desired)) return; |
| 1052 | } |
| 1053 | } |
| 1054 | bool isSuspended() const final { return mSuspended > 0; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1055 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1056 | String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex; |
| 1057 | |
| 1058 | // Hold either the AudioFlinger::mutex or the ThreadBase::mutex |
| 1059 | void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0, |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1060 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1061 | status_t getRenderPosition(uint32_t* halFrames, uint32_t* dspFrames) const final |
| 1062 | EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1063 | // Consider also removing and passing an explicit mMainBuffer initialization |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1064 | // parameter to AF::IAfTrack::Track(). |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1065 | float* sinkBuffer() const final { |
Andy Hung | 319587b | 2023-05-23 14:01:03 -0700 | [diff] [blame] | 1066 | return reinterpret_cast<float *>(mSinkBuffer); }; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1067 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1068 | void detachAuxEffect_l(int effectId) final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1069 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1070 | status_t attachAuxEffect(const sp<IAfTrack>& track, int EffectId) final |
| 1071 | EXCLUDES_ThreadBase_Mutex; |
| 1072 | status_t attachAuxEffect_l(const sp<IAfTrack>& track, int EffectId) final REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1073 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1074 | status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex()); |
| 1075 | size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex()); |
| 1076 | uint32_t hasAudioSession_l(audio_session_t sessionId) const final REQUIRES(mutex()) { |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 1077 | return ThreadBase::hasAudioSession_l(sessionId, mTracks); |
| 1078 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1079 | product_strategy_t getStrategyForSession_l(audio_session_t sessionId) const final |
| 1080 | REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1081 | |
| 1082 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1083 | status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final |
| 1084 | EXCLUDES_ThreadBase_Mutex; |
| 1085 | // could be static. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1086 | bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final; |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 1087 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1088 | // Does this require the AudioFlinger mutex as well? |
| 1089 | bool invalidateTracks_l(audio_stream_type_t streamType) final |
| 1090 | REQUIRES(mutex()); |
| 1091 | bool invalidateTracks_l(std::set<audio_port_handle_t>& portIds) final |
| 1092 | REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1093 | void invalidateTracks(audio_stream_type_t streamType) override; |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 1094 | // Invalidate tracks by a set of port ids. The port id will be removed from |
| 1095 | // the given set if the corresponding track is found and invalidated. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1096 | void invalidateTracks(std::set<audio_port_handle_t>& portIds) override |
| 1097 | EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1098 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1099 | size_t frameCount() const final { return mNormalFrameCount; } |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 1100 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1101 | audio_channel_mask_t mixerChannelMask() const final { |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1102 | return mMixerChannelMask; |
| 1103 | } |
| 1104 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1105 | status_t getTimestamp_l(AudioTimestamp& timestamp) final |
| 1106 | REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1107 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1108 | void addPatchTrack(const sp<IAfPatchTrack>& track) final EXCLUDES_ThreadBase_Mutex; |
| 1109 | void deletePatchTrack(const sp<IAfPatchTrack>& track) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1110 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1111 | // NO_THREAD_SAFETY_ANALYSIS - fix this to use atomics. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1112 | void toAudioPortConfig(struct audio_port_config* config) final; |
Eric Laurent | accc147 | 2013-09-20 09:36:34 -0700 | [diff] [blame] | 1113 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1114 | // Return the asynchronous signal wait time. |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1115 | int64_t computeWaitTimeNs_l() const override REQUIRES(mutex()) { return INT64_MAX; } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1116 | // returns true if the track is allowed to be added to the thread. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1117 | bool isTrackAllowed_l( |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1118 | audio_channel_mask_t channelMask __unused, |
| 1119 | audio_format_t format __unused, |
| 1120 | audio_session_t sessionId __unused, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1121 | uid_t uid) const override REQUIRES(mutex()) { |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1122 | return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid |
| 1123 | && mTracks.size() < PlaybackThread::kMaxTracks; |
| 1124 | } |
| 1125 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1126 | bool isTimestampCorrectionEnabled_l() const final REQUIRES(mutex()) { |
| 1127 | return audio_is_output_devices(mTimestampCorrectedDevice) |
| 1128 | && outDeviceTypes_l().count(mTimestampCorrectedDevice) != 0; |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 1129 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1130 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1131 | // NO_THREAD_SAFETY_ANALYSIS - fix this to be atomic. |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1132 | bool isStreamInitialized() const final { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 1133 | return !(mOutput == nullptr || mOutput->stream == nullptr); |
| 1134 | } |
| 1135 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1136 | audio_channel_mask_t hapticChannelMask() const final { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1137 | return mHapticChannelMask; |
| 1138 | } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1139 | |
| 1140 | uint32_t hapticChannelCount() const final { |
| 1141 | return mHapticChannelCount; |
| 1142 | } |
| 1143 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1144 | bool supportsHapticPlayback() const final { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1145 | return (mHapticChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE; |
| 1146 | } |
| 1147 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1148 | void setDownStreamPatch(const struct audio_patch* patch) final EXCLUDES_ThreadBase_Mutex { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1149 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 1150 | mDownStreamPatch = *patch; |
| 1151 | } |
| 1152 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1153 | IAfTrack* getTrackById_l(audio_port_handle_t trackId) final REQUIRES(mutex()); |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 1154 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1155 | bool hasMixer() const final { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1156 | return mType == MIXER || mType == DUPLICATING || mType == SPATIALIZER; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1157 | } |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 1158 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1159 | status_t setRequestedLatencyMode( |
| 1160 | audio_latency_mode_t /* mode */) override { return INVALID_OPERATION; } |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 1161 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1162 | status_t getSupportedLatencyModes( |
| 1163 | std::vector<audio_latency_mode_t>* /* modes */) override { |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 1164 | return INVALID_OPERATION; |
| 1165 | } |
| 1166 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1167 | status_t setBluetoothVariableLatencyEnabled(bool /* enabled */) override{ |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1168 | return INVALID_OPERATION; |
| 1169 | } |
Eric Laurent | 5205764 | 2022-12-16 11:45:07 +0100 | [diff] [blame] | 1170 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1171 | void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) override |
| 1172 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
| 1173 | void stopMelComputation_l() override |
| 1174 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 1175 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1176 | void setStandby() final EXCLUDES_ThreadBase_Mutex { |
| 1177 | audio_utils::lock_guard _l(mutex()); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1178 | setStandby_l(); |
| 1179 | } |
| 1180 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1181 | void setStandby_l() final REQUIRES(mutex()) { |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1182 | mStandby = true; |
| 1183 | mHalStarted = false; |
| 1184 | mKernelPositionOnStandby = |
| 1185 | mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 1186 | } |
| 1187 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1188 | bool waitForHalStart() final EXCLUDES_ThreadBase_Mutex { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1189 | audio_utils::unique_lock _l(mutex()); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1190 | static const nsecs_t kWaitHalTimeoutNs = seconds(2); |
| 1191 | nsecs_t endWaitTimetNs = systemTime() + kWaitHalTimeoutNs; |
| 1192 | while (!mHalStarted) { |
| 1193 | nsecs_t timeNs = systemTime(); |
| 1194 | if (timeNs >= endWaitTimetNs) { |
| 1195 | break; |
| 1196 | } |
| 1197 | nsecs_t waitTimeLeftNs = endWaitTimetNs - timeNs; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1198 | mWaitHalStartCV.wait_for(_l, std::chrono::nanoseconds(waitTimeLeftNs)); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1199 | } |
| 1200 | return mHalStarted; |
| 1201 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1202 | protected: |
Glenn Kasten | deca2ae | 2014-02-07 10:25:56 -0800 | [diff] [blame] | 1203 | // updated by readOutputParameters_l() |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 1204 | size_t mNormalFrameCount; // normal mixer and effects |
| 1205 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1206 | // throttle the thread processing |
| 1207 | bool mThreadThrottle GUARDED_BY(ThreadBase_ThreadLoop); |
| 1208 | |
| 1209 | // throttle time for MIXER threads - atomic as read by dump() |
| 1210 | std::atomic<uint32_t> mThreadThrottleTimeMs; |
| 1211 | |
| 1212 | // notify once per throttling |
| 1213 | uint32_t mThreadThrottleEndMs GUARDED_BY(ThreadBase_ThreadLoop); |
| 1214 | |
| 1215 | // half the buffer size in milliseconds |
| 1216 | uint32_t mHalfBufferMs GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 08fb174 | 2015-05-31 23:22:10 -0700 | [diff] [blame] | 1217 | |
Andy Hung | 010a1a1 | 2014-03-13 13:57:33 -0700 | [diff] [blame] | 1218 | void* mSinkBuffer; // frame size aligned sink buffer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1219 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1220 | // TODO: |
| 1221 | // Rearrange the buffer info into a struct/class with |
| 1222 | // clear, copy, construction, destruction methods. |
| 1223 | // |
| 1224 | // mSinkBuffer also has associated with it: |
| 1225 | // |
| 1226 | // mSinkBufferSize: Sink Buffer Size |
| 1227 | // mFormat: Sink Buffer Format |
| 1228 | |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1229 | // Mixer Buffer (mMixerBuffer*) |
| 1230 | // |
| 1231 | // In the case of floating point or multichannel data, which is not in the |
| 1232 | // sink format, it is required to accumulate in a higher precision or greater channel count |
| 1233 | // buffer before downmixing or data conversion to the sink buffer. |
| 1234 | |
| 1235 | // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1236 | bool mMixerBufferEnabled GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1237 | |
| 1238 | // Storage, 32 byte aligned (may make this alignment a requirement later). |
| 1239 | // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1240 | void* mMixerBuffer GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1241 | |
| 1242 | // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1243 | size_t mMixerBufferSize GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1244 | |
| 1245 | // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1246 | audio_format_t mMixerBufferFormat GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1247 | |
| 1248 | // An internal flag set to true by MixerThread::prepareTracks_l() |
| 1249 | // when mMixerBuffer contains valid data after mixing. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1250 | bool mMixerBufferValid GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 69aed5f | 2014-02-25 17:24:40 -0800 | [diff] [blame] | 1251 | |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1252 | // Effects Buffer (mEffectsBuffer*) |
| 1253 | // |
| 1254 | // In the case of effects data, which is not in the sink format, |
| 1255 | // it is required to accumulate in a different buffer before data conversion |
| 1256 | // to the sink buffer. |
| 1257 | |
| 1258 | // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1259 | bool mEffectBufferEnabled; |
| 1260 | // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l() |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1261 | |
| 1262 | // Storage, 32 byte aligned (may make this alignment a requirement later). |
| 1263 | // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1264 | void* mEffectBuffer; |
| 1265 | // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l() |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1266 | |
| 1267 | // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1268 | size_t mEffectBufferSize; |
| 1269 | // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l() |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1270 | |
| 1271 | // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1272 | // NO_THREAD_SAFETY_ANALYSIS: Spatializer access this in addEffectChain_l() |
| 1273 | audio_format_t mEffectBufferFormat; |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1274 | |
| 1275 | // An internal flag set to true by MixerThread::prepareTracks_l() |
| 1276 | // when mEffectsBuffer contains valid data after mixing. |
| 1277 | // |
| 1278 | // When this is set, all mixer data is routed into the effects buffer |
| 1279 | // for any processing (including output processing). |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1280 | bool mEffectBufferValid GUARDED_BY(ThreadBase_ThreadLoop); |
Andy Hung | 98ef978 | 2014-03-04 14:46:50 -0800 | [diff] [blame] | 1281 | |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1282 | // Set to "true" to enable when data has already copied to sink |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1283 | bool mHasDataCopiedToSinkBuffer GUARDED_BY(ThreadBase_ThreadLoop) = false; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1284 | |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1285 | // Frame size aligned buffer used as input and output to all post processing effects |
| 1286 | // except the Spatializer in a SPATIALIZER thread. Non spatialized tracks are mixed into |
| 1287 | // this buffer so that post processing effects can be applied. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1288 | void* mPostSpatializerBuffer GUARDED_BY(mutex()) = nullptr; |
Eric Laurent | b62d036 | 2021-10-26 17:40:18 +0200 | [diff] [blame] | 1289 | |
| 1290 | // Size of mPostSpatializerBuffer in bytes |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1291 | size_t mPostSpatializerBufferSize GUARDED_BY(mutex()); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1292 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1293 | // suspend count, > 0 means suspended. While suspended, the thread continues to pull from |
| 1294 | // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle |
| 1295 | // concurrent use of both of them, so Audio Policy Service suspends one of the threads to |
| 1296 | // workaround that restriction. |
| 1297 | // 'volatile' means accessed via atomic operations and no lock. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1298 | std::atomic<int32_t> mSuspended; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1299 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1300 | int64_t mBytesWritten; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1301 | std::atomic<int64_t> mFramesWritten; // not reset on standby |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 1302 | int64_t mLastFramesWritten = -1; // track changes in timestamp |
| 1303 | // server frames written. |
Andy Hung | 238fa3d | 2016-07-28 10:53:22 -0700 | [diff] [blame] | 1304 | int64_t mSuspendedFrames; // not reset on standby |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 1305 | |
| 1306 | // mHapticChannelMask and mHapticChannelCount will only be valid when the thread support |
| 1307 | // haptic playback. |
| 1308 | audio_channel_mask_t mHapticChannelMask = AUDIO_CHANNEL_NONE; |
| 1309 | uint32_t mHapticChannelCount = 0; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1310 | |
| 1311 | audio_channel_mask_t mMixerChannelMask = AUDIO_CHANNEL_NONE; |
| 1312 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1313 | // mMasterMute is in both PlaybackThread and in AudioFlinger. When a |
| 1314 | // PlaybackThread needs to find out if master-muted, it checks it's local |
| 1315 | // copy rather than the one in AudioFlinger. This optimization saves a lock. |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1316 | bool mMasterMute GUARDED_BY(mutex()); |
| 1317 | void setMasterMute_l(bool muted) REQUIRES(mutex()) { mMasterMute = muted; } |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 1318 | |
| 1319 | auto discontinuityForStandbyOrFlush() const { // call on threadLoop or with lock. |
| 1320 | return ((mType == DIRECT && !audio_is_linear_pcm(mFormat)) |
| 1321 | || mType == OFFLOAD) |
| 1322 | ? mTimestampVerifier.DISCONTINUITY_MODE_ZERO |
| 1323 | : mTimestampVerifier.DISCONTINUITY_MODE_CONTINUOUS; |
| 1324 | } |
| 1325 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1326 | ActiveTracks<IAfTrack> mActiveTracks; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1327 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1328 | // Time to sleep between cycles when: |
| 1329 | virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED |
| 1330 | virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE |
| 1331 | virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us |
| 1332 | // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write() |
| 1333 | // No sleep in standby mode; waits on a condition |
| 1334 | |
| 1335 | // Code snippets that are temporarily lifted up out of threadLoop() until the merge |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1336 | |
| 1337 | // consider unification with MMapThread |
| 1338 | virtual void checkSilentMode_l() final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1339 | |
| 1340 | // Non-trivial for DUPLICATING only |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1341 | virtual void saveOutputTracks() REQUIRES(ThreadBase_ThreadLoop) {} |
| 1342 | virtual void clearOutputTracks() REQUIRES(ThreadBase_ThreadLoop) {} |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1343 | |
| 1344 | // Cache various calculated values, at threadLoop() entry and after a parameter change |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1345 | virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1346 | void setCheckOutputStageEffects() override { |
| 1347 | mCheckOutputStageEffects.store(true); |
| 1348 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1349 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1350 | virtual uint32_t correctLatency_l(uint32_t latency) const REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1351 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1352 | virtual status_t createAudioPatch_l(const struct audio_patch *patch, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1353 | audio_patch_handle_t *handle) REQUIRES(mutex()); |
| 1354 | virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) |
| 1355 | REQUIRES(mutex()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1356 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1357 | // NO_THREAD_SAFETY_ANALYSIS - fix this to use atomics |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1358 | bool usesHwAvSync() const final { return mType == DIRECT && mOutput != nullptr |
Phil Burk | 6fc2a7c | 2015-04-30 16:08:10 -0700 | [diff] [blame] | 1359 | && mHwSupportsPause |
| 1360 | && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); } |
Eric Laurent | 0f7b5f2 | 2014-12-19 10:43:21 -0800 | [diff] [blame] | 1361 | |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1362 | uint32_t trackCountForUid_l(uid_t uid) const; |
Eric Laurent | ad7dd96 | 2016-09-22 12:38:37 -0700 | [diff] [blame] | 1363 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1364 | void invalidateTracksForAudioSession_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1365 | audio_session_t sessionId) const override REQUIRES(mutex()) { |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 1366 | ThreadBase::invalidateTracksForAudioSession_l(sessionId, mTracks); |
| 1367 | } |
| 1368 | |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 1369 | DISALLOW_COPY_AND_ASSIGN(PlaybackThread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1370 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1371 | status_t addTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex()); |
| 1372 | bool destroyTrack_l(const sp<IAfTrack>& track) final REQUIRES(mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1373 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1374 | void removeTrack_l(const sp<IAfTrack>& track) REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1375 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1376 | void readOutputParameters_l() REQUIRES(mutex()); |
| 1377 | MetadataUpdate updateMetadata_l() final REQUIRES(mutex()); |
| 1378 | virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata) |
| 1379 | REQUIRES(mutex()) ; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1380 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1381 | void collectTimestamps_l() REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Dean Wheatley | 12473e9 | 2021-03-18 23:00:55 +1100 | [diff] [blame] | 1382 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1383 | // The Tracks class manages tracks added and removed from the Thread. |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1384 | template <typename T> |
| 1385 | class Tracks { |
| 1386 | public: |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1387 | explicit Tracks(bool saveDeletedTrackIds) : |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1388 | mSaveDeletedTrackIds(saveDeletedTrackIds) { } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1389 | |
| 1390 | // SortedVector methods |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1391 | ssize_t add(const sp<T> &track) { |
| 1392 | const ssize_t index = mTracks.add(track); |
| 1393 | LOG_ALWAYS_FATAL_IF(index < 0, "cannot add track"); |
| 1394 | return index; |
| 1395 | } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1396 | ssize_t remove(const sp<T> &track); |
| 1397 | size_t size() const { |
| 1398 | return mTracks.size(); |
| 1399 | } |
| 1400 | bool isEmpty() const { |
| 1401 | return mTracks.isEmpty(); |
| 1402 | } |
| 1403 | ssize_t indexOf(const sp<T> &item) { |
| 1404 | return mTracks.indexOf(item); |
| 1405 | } |
| 1406 | sp<T> operator[](size_t index) const { |
| 1407 | return mTracks[index]; |
| 1408 | } |
| 1409 | typename SortedVector<sp<T>>::iterator begin() { |
| 1410 | return mTracks.begin(); |
| 1411 | } |
| 1412 | typename SortedVector<sp<T>>::iterator end() { |
| 1413 | return mTracks.end(); |
| 1414 | } |
| 1415 | |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1416 | size_t processDeletedTrackIds(const std::function<void(int)>& f) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1417 | for (const int trackId : mDeletedTrackIds) { |
| 1418 | f(trackId); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1419 | } |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1420 | return mDeletedTrackIds.size(); |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1423 | void clearDeletedTrackIds() { mDeletedTrackIds.clear(); } |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1424 | |
| 1425 | private: |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1426 | // Tracks pending deletion for MIXER type threads |
| 1427 | const bool mSaveDeletedTrackIds; // true to enable tracking |
| 1428 | std::set<int> mDeletedTrackIds; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1429 | |
| 1430 | SortedVector<sp<T>> mTracks; // wrapped SortedVector. |
| 1431 | }; |
| 1432 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1433 | Tracks<IAfTrack> mTracks; |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1434 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1435 | stream_type_t mStreamTypes[AUDIO_STREAM_CNT]; |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1436 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1437 | AudioStreamOut *mOutput; |
| 1438 | |
| 1439 | float mMasterVolume; |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1440 | std::atomic<float> mMasterBalance{}; |
| 1441 | audio_utils::Balance mBalance; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1442 | int mNumWrites; |
| 1443 | int mNumDelayedWrites; |
| 1444 | bool mInWrite; |
| 1445 | |
| 1446 | // FIXME rename these former local variables of threadLoop to standard "m" names |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1447 | nsecs_t mStandbyTimeNs; |
Andy Hung | 25c2dac | 2014-02-27 14:56:00 -0800 | [diff] [blame] | 1448 | size_t mSinkBufferSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1449 | |
| 1450 | // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l() |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1451 | uint32_t mActiveSleepTimeUs; |
| 1452 | uint32_t mIdleSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1453 | |
Eric Laurent | ad9cb8b | 2015-05-26 16:38:19 -0700 | [diff] [blame] | 1454 | uint32_t mSleepTimeUs; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1455 | |
| 1456 | // mixer status returned by prepareTracks_l() |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1457 | mixer_state mMixerStatus GUARDED_BY(ThreadBase_ThreadLoop); // current cycle |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1458 | // previous cycle when in prepareTracks_l() |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1459 | mixer_state mMixerStatusIgnoringFastTracks GUARDED_BY(ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1460 | // FIXME or a separate ready state per track |
| 1461 | |
| 1462 | // FIXME move these declarations into the specific sub-class that needs them |
| 1463 | // MIXER only |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1464 | uint32_t sleepTimeShift GUARDED_BY(ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1465 | |
| 1466 | // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1467 | nsecs_t mStandbyDelayNs; // GUARDED_BY(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1468 | |
| 1469 | // MIXER only |
| 1470 | nsecs_t maxPeriod; |
| 1471 | |
| 1472 | // DUPLICATING only |
| 1473 | uint32_t writeFrames; |
| 1474 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1475 | size_t mBytesRemaining GUARDED_BY(ThreadBase_ThreadLoop); |
| 1476 | size_t mCurrentWriteLength GUARDED_BY(ThreadBase_ThreadLoop); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1477 | bool mUseAsyncWrite; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1478 | // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is |
| 1479 | // incremented each time a write(), a flush() or a standby() occurs. |
| 1480 | // Bit 0 is set when a write blocks and indicates a callback is expected. |
| 1481 | // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence |
| 1482 | // callbacks are ignored. |
| 1483 | uint32_t mWriteAckSequence; |
| 1484 | // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is |
| 1485 | // incremented each time a drain is requested or a flush() or standby() occurs. |
| 1486 | // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is |
| 1487 | // expected. |
| 1488 | // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence |
| 1489 | // callbacks are ignored. |
| 1490 | uint32_t mDrainSequence; |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1491 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1492 | sp<AsyncCallbackThread> mCallbackThread; |
| 1493 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1494 | audio_utils::mutex& audioTrackCbMutex() const { return mAudioTrackCbMutex; } |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 1495 | mutable audio_utils::mutex mAudioTrackCbMutex{ |
| 1496 | audio_utils::MutexOrder::kPlaybackThread_AudioTrackCbMutex}; |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 1497 | // Record of IAudioTrackCallback |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1498 | std::map<sp<IAfTrack>, sp<media::IAudioTrackCallback>> mAudioTrackCallbacks; |
jiabin | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 1499 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1500 | // The HAL output sink is treated as non-blocking, but current implementation is blocking |
| 1501 | sp<NBAIO_Sink> mOutputSink; |
| 1502 | // If a fast mixer is present, the blocking pipe sink, otherwise clear |
| 1503 | sp<NBAIO_Sink> mPipeSink; |
| 1504 | // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink |
| 1505 | sp<NBAIO_Sink> mNormalSink; |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 1506 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1507 | uint32_t mScreenState; // cached copy of gScreenState |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 1508 | // TODO: add comment and adjust size as needed |
Glenn Kasten | eef598c | 2017-04-03 14:41:13 -0700 | [diff] [blame] | 1509 | static const size_t kFastMixerLogSize = 8 * 1024; |
Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 1510 | sp<NBLog::Writer> mFastMixerNBLogWriter; |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 1511 | |
Dean Wheatley | 30d2842 | 2018-11-06 10:27:40 +1100 | [diff] [blame] | 1512 | // Downstream patch latency, available if mDownstreamLatencyStatMs.getN() > 0. |
| 1513 | audio_utils::Statistics<double> mDownstreamLatencyStatMs{0.999}; |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 1514 | |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1515 | // output stream start detection based on render position returned by the kernel |
| 1516 | // condition signalled when the output stream has started |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1517 | audio_utils::condition_variable mWaitHalStartCV; |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 1518 | // true when the output stream render position has moved, reset to false in standby |
| 1519 | bool mHalStarted = false; |
| 1520 | // last kernel render position saved when entering standby |
| 1521 | int64_t mKernelPositionOnStandby = 0; |
| 1522 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1523 | public: |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1524 | FastTrackUnderruns getFastTrackUnderruns(size_t /* fastIndex */) const override |
| 1525 | { return {}; } |
| 1526 | const std::atomic<int64_t>& framesWritten() const final { return mFramesWritten; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1527 | |
| 1528 | protected: |
| 1529 | // accessed by both binder threads and within threadLoop(), lock on mutex needed |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1530 | uint32_t& fastTrackAvailMask_l() final REQUIRES(mutex()) { return mFastTrackAvailMask; } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1531 | uint32_t mFastTrackAvailMask; // bit i set if fast track [i] is available |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1532 | bool mHwSupportsPause; |
| 1533 | bool mHwPaused; |
| 1534 | bool mFlushPending; |
Eric Laurent | 7c29ec9 | 2017-09-20 17:54:22 -0700 | [diff] [blame] | 1535 | // volumes last sent to audio HAL with stream->setVolume() |
| 1536 | float mLeftVolFloat; |
| 1537 | float mRightVolFloat; |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 1538 | |
| 1539 | // audio patch used by the downstream software patch. |
| 1540 | // Only used if ThreadBase::mIsMsdDevice is true. |
| 1541 | struct audio_patch mDownStreamPatch; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1542 | |
| 1543 | std::atomic_bool mCheckOutputStageEffects{}; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 1544 | |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 1545 | |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 1546 | // Provides periodic checking for timestamp advancement for underrun detection. |
| 1547 | class IsTimestampAdvancing { |
| 1548 | public: |
| 1549 | // The timestamp will not be checked any faster than the specified time. |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1550 | explicit IsTimestampAdvancing(nsecs_t minimumTimeBetweenChecksNs) |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 1551 | : mMinimumTimeBetweenChecksNs(minimumTimeBetweenChecksNs) |
| 1552 | { |
| 1553 | clear(); |
| 1554 | } |
| 1555 | // Check if the presentation position has advanced in the last periodic time. |
| 1556 | bool check(AudioStreamOut * output); |
| 1557 | // Clear the internal state when the playback state changes for the output |
| 1558 | // stream. |
| 1559 | void clear(); |
| 1560 | private: |
| 1561 | // The minimum time between timestamp checks. |
| 1562 | const nsecs_t mMinimumTimeBetweenChecksNs; |
| 1563 | // Add differential check on the timestamps to see if there is a change in the |
| 1564 | // timestamp frame position between the last call to check. |
| 1565 | uint64_t mPreviousPosition; |
| 1566 | // The time at which the last check occurred, to ensure we don't check too |
| 1567 | // frequently, giving the Audio HAL enough time to update its timestamps. |
| 1568 | nsecs_t mPreviousNs; |
| 1569 | // The valued is latched so we don't check timestamps too frequently. |
| 1570 | bool mLatchedValue; |
| 1571 | }; |
| 1572 | IsTimestampAdvancing mIsTimestampAdvancing; |
ziyangch | 8f194f1 | 2021-12-01 13:48:04 -0800 | [diff] [blame] | 1573 | |
Brian Lindahl | 65e9001 | 2022-07-27 18:01:07 +0200 | [diff] [blame] | 1574 | virtual void flushHw_l() { |
| 1575 | mIsTimestampAdvancing.clear(); |
| 1576 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1577 | }; |
| 1578 | |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1579 | class MixerThread : public PlaybackThread, |
| 1580 | public StreamOutHalInterfaceLatencyModeCallback { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1581 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1582 | MixerThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1583 | AudioStreamOut* output, |
| 1584 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1585 | bool systemReady, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1586 | type_t type = MIXER, |
| 1587 | audio_config_base_t *mixerConfig = nullptr); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1588 | ~MixerThread() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1589 | |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1590 | // RefBase |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1591 | void onFirstRef() override; |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1592 | |
| 1593 | // StreamOutHalInterfaceLatencyModeCallback |
| 1594 | void onRecommendedLatencyModeChanged( |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1595 | std::vector<audio_latency_mode_t> modes) final; |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1596 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1597 | // Thread virtuals |
| 1598 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1599 | bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) final |
| 1600 | REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1601 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1602 | bool isTrackAllowed_l( |
Andy Hung | 1bc088a | 2018-02-09 15:57:31 -0800 | [diff] [blame] | 1603 | audio_channel_mask_t channelMask, audio_format_t format, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1604 | audio_session_t sessionId, uid_t uid) const final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1605 | protected: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1606 | mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) override |
| 1607 | REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1608 | uint32_t idleSleepTimeUs() const final; |
| 1609 | uint32_t suspendSleepTimeUs() const final; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1610 | void cacheParameters_l() override REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1611 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1612 | void acquireWakeLock_l() final REQUIRES(mutex()) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 1613 | PlaybackThread::acquireWakeLock_l(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1614 | if (hasFastMixer()) { |
| 1615 | mFastMixer->setBoottimeOffset( |
| 1616 | mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]); |
| 1617 | } |
| 1618 | } |
| 1619 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1620 | void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1621 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1622 | // threadLoop snippets |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1623 | ssize_t threadLoop_write() override REQUIRES(ThreadBase_ThreadLoop); |
| 1624 | void threadLoop_standby() override REQUIRES(ThreadBase_ThreadLoop); |
| 1625 | void threadLoop_mix() override REQUIRES(ThreadBase_ThreadLoop); |
| 1626 | void threadLoop_sleepTime() override REQUIRES(ThreadBase_ThreadLoop); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1627 | uint32_t correctLatency_l(uint32_t latency) const final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1628 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1629 | status_t createAudioPatch_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1630 | const struct audio_patch* patch, audio_patch_handle_t* handle) |
| 1631 | final REQUIRES(mutex()); |
| 1632 | status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final REQUIRES(mutex()); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1633 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1634 | AudioMixer* mAudioMixer; // normal mixer |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1635 | |
| 1636 | // Support low latency mode by default as unless explicitly indicated by the audio HAL |
| 1637 | // we assume the audio path is compatible with the head tracking latency requirements |
| 1638 | std::vector<audio_latency_mode_t> mSupportedLatencyModes = {AUDIO_LATENCY_MODE_LOW}; |
| 1639 | // default to invalid value to force first update to the audio HAL |
| 1640 | audio_latency_mode_t mSetLatencyMode = |
| 1641 | (audio_latency_mode_t)AUDIO_LATENCY_MODE_INVALID; |
| 1642 | |
| 1643 | // Bluetooth Variable latency control logic is enabled or disabled for this thread |
| 1644 | std::atomic_bool mBluetoothLatencyModesEnabled; |
| 1645 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1646 | private: |
| 1647 | // one-time initialization, no locks required |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 1648 | sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1649 | sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread |
| 1650 | |
| 1651 | // contents are not guaranteed to be consistent, no locks required |
| 1652 | FastMixerDumpState mFastMixerDumpState; |
| 1653 | #ifdef STATE_QUEUE_DUMP |
| 1654 | StateQueueObserverDump mStateQueueObserverDump; |
| 1655 | StateQueueMutatorDump mStateQueueMutatorDump; |
| 1656 | #endif |
| 1657 | AudioWatchdogDump mAudioWatchdogDump; |
| 1658 | |
| 1659 | // accessible only within the threadLoop(), no locks required |
| 1660 | // mFastMixer->sq() // for mutating and pushing state |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1661 | int32_t mFastMixerFutex GUARDED_BY(ThreadBase_ThreadLoop); // for cold idle |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1662 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1663 | std::atomic_bool mMasterMono; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1664 | public: |
Glenn Kasten | 4d23ca3 | 2014-05-13 10:39:51 -0700 | [diff] [blame] | 1665 | virtual bool hasFastMixer() const { return mFastMixer != 0; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1666 | virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const { |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 1667 | ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1668 | return mFastMixerDumpState.mTracks[fastIndex].mUnderruns; |
| 1669 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1670 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1671 | status_t threadloop_getHalTimestamp_l( |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1672 | ExtendedTimestamp *timestamp) const override |
| 1673 | REQUIRES(mutex(), ThreadBase_ThreadLoop) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1674 | if (mNormalSink.get() != nullptr) { |
| 1675 | return mNormalSink->getTimestamp(*timestamp); |
| 1676 | } |
| 1677 | return INVALID_OPERATION; |
| 1678 | } |
| 1679 | |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1680 | status_t getSupportedLatencyModes( |
| 1681 | std::vector<audio_latency_mode_t>* modes) override; |
| 1682 | |
| 1683 | status_t setBluetoothVariableLatencyEnabled(bool enabled) override; |
| 1684 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1685 | protected: |
| 1686 | virtual void setMasterMono_l(bool mono) { |
| 1687 | mMasterMono.store(mono); |
| 1688 | if (mFastMixer != nullptr) { /* hasFastMixer() */ |
| 1689 | mFastMixer->setMasterMono(mMasterMono); |
| 1690 | } |
| 1691 | } |
| 1692 | // the FastMixer performs mono blend if it exists. |
Glenn Kasten | 03c48d5 | 2016-01-27 17:25:17 -0800 | [diff] [blame] | 1693 | // Blending with limiter is not idempotent, |
| 1694 | // and blending without limiter is idempotent but inefficient to do twice. |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1695 | virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1696 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1697 | void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1698 | mMasterBalance.store(balance); |
| 1699 | if (hasFastMixer()) { |
| 1700 | mFastMixer->setMasterBalance(balance); |
| 1701 | } |
| 1702 | } |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1703 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1704 | void updateHalSupportedLatencyModes_l() REQUIRES(mutex()); |
| 1705 | void onHalLatencyModesChanged_l() override REQUIRES(mutex()); |
| 1706 | void setHalLatencyMode_l() override REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1707 | }; |
| 1708 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1709 | class DirectOutputThread : public PlaybackThread, public virtual IAfDirectOutputThread { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1710 | public: |
| 1711 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1712 | sp<IAfDirectOutputThread> asIAfDirectOutputThread() final { |
| 1713 | return sp<IAfDirectOutputThread>::fromExisting(this); |
| 1714 | } |
| 1715 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1716 | DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 1717 | audio_io_handle_t id, bool systemReady, |
| 1718 | const audio_offload_info_t& offloadInfo) |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1719 | : DirectOutputThread(afThreadCallback, output, id, DIRECT, systemReady, offloadInfo) { } |
Andy Hung | 48f59ed | 2019-01-28 15:06:59 -0800 | [diff] [blame] | 1720 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1721 | ~DirectOutputThread() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1722 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1723 | status_t selectPresentation(int presentationId, int programId) final; |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 1724 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | // Thread virtuals |
| 1726 | |
Eric Laurent | 1035194 | 2014-05-08 18:49:52 -0700 | [diff] [blame] | 1727 | virtual bool checkForNewParameter_l(const String8& keyValuePair, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1728 | status_t& status) REQUIRES(mutex()); |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1729 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1730 | void flushHw_l() override REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1731 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1732 | void setMasterBalance(float balance) override EXCLUDES_ThreadBase_Mutex; |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1733 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1734 | protected: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1735 | virtual uint32_t activeSleepTimeUs() const; |
| 1736 | virtual uint32_t idleSleepTimeUs() const; |
| 1737 | virtual uint32_t suspendSleepTimeUs() const; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1738 | virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1739 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1740 | void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 1741 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1742 | // threadLoop snippets |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1743 | virtual mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) |
| 1744 | REQUIRES(mutex(), ThreadBase_ThreadLoop); |
| 1745 | virtual void threadLoop_mix() REQUIRES(ThreadBase_ThreadLoop); |
| 1746 | virtual void threadLoop_sleepTime() REQUIRES(ThreadBase_ThreadLoop); |
| 1747 | virtual void threadLoop_exit() REQUIRES(ThreadBase_ThreadLoop); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1748 | virtual bool shouldStandby_l() REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1749 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1750 | virtual void onAddNewTrack_l() REQUIRES(mutex()); |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 1751 | |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 1752 | const audio_offload_info_t mOffloadInfo; |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 1753 | |
| 1754 | audioflinger::MonotonicFrameCounter mMonotonicFrameCounter; // for VolumeShaper |
Andy Hung | 48f59ed | 2019-01-28 15:06:59 -0800 | [diff] [blame] | 1755 | bool mVolumeShaperActive = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1756 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1757 | DirectOutputThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 1758 | audio_io_handle_t id, ThreadBase::type_t type, bool systemReady, |
| 1759 | const audio_offload_info_t& offloadInfo); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1760 | void processVolume_l(IAfTrack *track, bool lastTrack) REQUIRES(mutex()); |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 1761 | bool isTunerStream() const { return (mOffloadInfo.content_id > 0); } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1762 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1763 | // prepareTracks_l() tells threadLoop_mix() the name of the single active track |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1764 | sp<IAfTrack> mActiveTrack; |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 1765 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1766 | wp<IAfTrack> mPreviousTrack; // used to detect track switch |
Phil Burk | 43b4dcc | 2015-06-09 16:53:44 -0700 | [diff] [blame] | 1767 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1768 | // This must be initialized for initial condition of mMasterBalance = 0 (disabled). |
| 1769 | float mMasterBalanceLeft = 1.f; |
| 1770 | float mMasterBalanceRight = 1.f; |
| 1771 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1772 | public: |
| 1773 | virtual bool hasFastMixer() const { return false; } |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1774 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1775 | virtual int64_t computeWaitTimeNs_l() const override REQUIRES(mutex()); |
Andy Hung | f323451 | 2018-07-03 14:51:47 -0700 | [diff] [blame] | 1776 | |
| 1777 | status_t threadloop_getHalTimestamp_l(ExtendedTimestamp *timestamp) const override { |
| 1778 | // For DIRECT and OFFLOAD threads, query the output sink directly. |
| 1779 | if (mOutput != nullptr) { |
| 1780 | uint64_t uposition64; |
| 1781 | struct timespec time; |
| 1782 | if (mOutput->getPresentationPosition( |
| 1783 | &uposition64, &time) == OK) { |
| 1784 | timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] |
| 1785 | = (int64_t)uposition64; |
| 1786 | timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] |
| 1787 | = audio_utils_ns_from_timespec(&time); |
| 1788 | return NO_ERROR; |
| 1789 | } |
| 1790 | } |
| 1791 | return INVALID_OPERATION; |
| 1792 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1793 | }; |
| 1794 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1795 | class OffloadThread : public DirectOutputThread { |
| 1796 | public: |
| 1797 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1798 | OffloadThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut* output, |
Gareth Fenn | b18c1a3 | 2022-10-05 13:42:36 -0700 | [diff] [blame] | 1799 | audio_io_handle_t id, bool systemReady, |
| 1800 | const audio_offload_info_t& offloadInfo); |
Eric Laurent | 6a51d7e | 2013-10-17 18:59:26 -0700 | [diff] [blame] | 1801 | virtual ~OffloadThread() {}; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1802 | void flushHw_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1803 | |
| 1804 | protected: |
| 1805 | // threadLoop snippets |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1806 | mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1807 | REQUIRES(mutex(), ThreadBase_ThreadLoop); |
| 1808 | void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1809 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1810 | bool waitingAsyncCallback() final; |
| 1811 | bool waitingAsyncCallback_l() final REQUIRES(mutex()); |
| 1812 | void invalidateTracks(audio_stream_type_t streamType) final EXCLUDES_ThreadBase_Mutex; |
| 1813 | void invalidateTracks(std::set<audio_port_handle_t>& portIds) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1814 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1815 | bool keepWakeLock() const final { return (mKeepWakeLock || (mDrainSequence & 1)); } |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 1816 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1817 | private: |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1818 | size_t mPausedWriteLength; // length in bytes of write interrupted by pause |
| 1819 | size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume |
Eric Laurent | 6466797 | 2016-03-30 18:19:46 -0700 | [diff] [blame] | 1820 | bool mKeepWakeLock; // keep wake lock while waiting for write callback |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1821 | }; |
| 1822 | |
| 1823 | class AsyncCallbackThread : public Thread { |
| 1824 | public: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1825 | explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1826 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1827 | // Thread virtuals |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1828 | bool threadLoop() final; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1829 | |
| 1830 | // RefBase |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1831 | void onFirstRef() final; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1832 | |
| 1833 | void exit(); |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1834 | void setWriteBlocked(uint32_t sequence); |
| 1835 | void resetWriteBlocked(); |
| 1836 | void setDraining(uint32_t sequence); |
| 1837 | void resetDraining(); |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 1838 | void setAsyncError(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1839 | |
| 1840 | private: |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 1841 | const wp<PlaybackThread> mPlaybackThread; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1842 | // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via |
| 1843 | // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used |
| 1844 | // to indicate that the callback has been received via resetWriteBlocked() |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 1845 | uint32_t mWriteAckSequence; |
Eric Laurent | 3b4529e | 2013-09-05 18:09:19 -0700 | [diff] [blame] | 1846 | // mDrainSequence corresponds to the last drain sequence passed by the offload thread via |
| 1847 | // setDraining(). The sequence is shifted one bit to the left and the lsb is used |
| 1848 | // to indicate that the callback has been received via resetDraining() |
Eric Laurent | 4de9559 | 2013-09-26 15:28:21 -0700 | [diff] [blame] | 1849 | uint32_t mDrainSequence; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1850 | audio_utils::condition_variable mWaitWorkCV; |
Andy Hung | a6f1cbb | 2023-10-12 18:18:13 -0700 | [diff] [blame] | 1851 | mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kAsyncCallbackThread_Mutex}; |
Haynes Mathew George | 4527b9e | 2016-07-07 19:54:17 -0700 | [diff] [blame] | 1852 | bool mAsyncError; |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1853 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1854 | audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::AsyncCallbackThread_Mutex) { |
| 1855 | return mMutex; |
| 1856 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1857 | }; |
| 1858 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1859 | class DuplicatingThread : public MixerThread, public IAfDuplicatingThread { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1860 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1861 | DuplicatingThread(const sp<IAfThreadCallback>& afThreadCallback, |
| 1862 | IAfPlaybackThread* mainThread, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1863 | audio_io_handle_t id, bool systemReady); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1864 | ~DuplicatingThread() override; |
| 1865 | |
| 1866 | sp<IAfDuplicatingThread> asIAfDuplicatingThread() final { |
| 1867 | return sp<IAfDuplicatingThread>::fromExisting(this); |
| 1868 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1869 | |
| 1870 | // Thread virtuals |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1871 | void addOutputTrack(IAfPlaybackThread* thread) final EXCLUDES_ThreadBase_Mutex; |
| 1872 | void removeOutputTrack(IAfPlaybackThread* thread) final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1873 | uint32_t waitTimeMs() const final { return mWaitTimeMs; } |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 1874 | |
Kevin Rocard | c86a7f7 | 2018-04-03 09:00:09 -0700 | [diff] [blame] | 1875 | void sendMetadataToBackend_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1876 | const StreamOutHalInterface::SourceMetadata& metadata) final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1877 | protected: |
| 1878 | virtual uint32_t activeSleepTimeUs() const; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1879 | void dumpInternals_l(int fd, const Vector<String16>& args) final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1880 | |
| 1881 | private: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1882 | bool outputsReady() REQUIRES(ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1883 | protected: |
| 1884 | // threadLoop snippets |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1885 | void threadLoop_mix() final REQUIRES(ThreadBase_ThreadLoop); |
| 1886 | void threadLoop_sleepTime() final REQUIRES(ThreadBase_ThreadLoop); |
| 1887 | ssize_t threadLoop_write() final REQUIRES(ThreadBase_ThreadLoop); |
| 1888 | void threadLoop_standby() final REQUIRES(ThreadBase_ThreadLoop); |
Andy Hung | 8a5abfd | 2023-12-07 19:35:12 -0800 | [diff] [blame] | 1889 | void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1890 | void cacheParameters_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1891 | |
| 1892 | private: |
| 1893 | // called from threadLoop, addOutputTrack, removeOutputTrack |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1894 | void updateWaitTime_l() REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1895 | protected: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1896 | void saveOutputTracks() final REQUIRES(mutex(), ThreadBase_ThreadLoop); |
| 1897 | void clearOutputTracks() final REQUIRES(mutex(), ThreadBase_ThreadLoop); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1898 | private: |
| 1899 | |
| 1900 | uint32_t mWaitTimeMs; |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1901 | // NO_THREAD_SAFETY_ANALYSIS GUARDED_BY(ThreadBase_ThreadLoop) |
| 1902 | SortedVector <sp<IAfOutputTrack>> outputTracks; |
| 1903 | SortedVector <sp<IAfOutputTrack>> mOutputTracks GUARDED_BY(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1904 | public: |
| 1905 | virtual bool hasFastMixer() const { return false; } |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1906 | status_t threadloop_getHalTimestamp_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1907 | ExtendedTimestamp *timestamp) const override REQUIRES(mutex()) { |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1908 | if (mOutputTracks.size() > 0) { |
| 1909 | // forward the first OutputTrack's kernel information for timestamp. |
| 1910 | const ExtendedTimestamp trackTimestamp = |
| 1911 | mOutputTracks[0]->getClientProxyTimestamp(); |
| 1912 | if (trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0) { |
| 1913 | timestamp->mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = |
| 1914 | trackTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]; |
| 1915 | timestamp->mPosition[ExtendedTimestamp::LOCATION_KERNEL] = |
| 1916 | trackTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 1917 | return OK; // discard server timestamp - that's ignored. |
| 1918 | } |
| 1919 | } |
| 1920 | return INVALID_OPERATION; |
| 1921 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1922 | }; |
| 1923 | |
Eric Laurent | b046394 | 2022-12-20 16:31:10 +0100 | [diff] [blame] | 1924 | class SpatializerThread : public MixerThread { |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1925 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1926 | SpatializerThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1927 | AudioStreamOut* output, |
| 1928 | audio_io_handle_t id, |
| 1929 | bool systemReady, |
| 1930 | audio_config_base_t *mixerConfig); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1931 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1932 | bool hasFastMixer() const final { return false; } |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1933 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1934 | status_t setRequestedLatencyMode(audio_latency_mode_t mode) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 1935 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1936 | protected: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1937 | void checkOutputStageEffects() final |
| 1938 | REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1939 | void setHalLatencyMode_l() final REQUIRES(mutex()); |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1940 | |
Andy Hung | e251446 | 2023-12-06 14:59:24 -0800 | [diff] [blame] | 1941 | void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop); |
| 1942 | |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1943 | private: |
Eric Laurent | 68a40a8 | 2022-05-03 18:15:04 +0200 | [diff] [blame] | 1944 | // Do not request a specific mode by default |
| 1945 | audio_latency_mode_t mRequestedLatencyMode = AUDIO_LATENCY_MODE_FREE; |
| 1946 | |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1947 | sp<IAfEffectHandle> mFinalDownMixer; |
Eric Laurent | b3f315a | 2021-07-13 15:09:05 +0200 | [diff] [blame] | 1948 | }; |
| 1949 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1950 | // record thread |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1951 | class RecordThread : public IAfRecordThread, public ThreadBase |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1952 | { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1953 | friend class ResamplerBufferProvider; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1954 | public: |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1955 | sp<IAfRecordThread> asIAfRecordThread() final { |
| 1956 | return sp<IAfRecordThread>::fromExisting(this); |
| 1957 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1958 | |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1959 | RecordThread(const sp<IAfThreadCallback>& afThreadCallback, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1960 | AudioStreamIn *input, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1961 | audio_io_handle_t id, |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 1962 | bool systemReady |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 1963 | ); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1964 | ~RecordThread() override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1965 | |
| 1966 | // no addTrack_l ? |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1967 | void destroyTrack_l(const sp<IAfRecordTrack>& track) final REQUIRES(mutex()); |
| 1968 | void removeTrack_l(const sp<IAfRecordTrack>& track) final REQUIRES(mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1969 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1970 | // Thread virtuals |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 1971 | bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1972 | void preExit() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1973 | |
| 1974 | // RefBase |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1975 | void onFirstRef() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1976 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1977 | status_t initCheck() const final { return mInput == nullptr ? NO_INIT : NO_ERROR; } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 1978 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1979 | sp<MemoryDealer> readOnlyHeap() const final { return mReadOnlyHeap; } |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 1980 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 1981 | sp<IMemory> pipeMemory() const final { return mPipeMemory; } |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 1982 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1983 | sp<IAfRecordTrack> createRecordTrack_l( |
Andy Hung | 88035ac | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 1984 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 1985 | const audio_attributes_t& attr, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1986 | uint32_t *pSampleRate, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1987 | audio_format_t format, |
| 1988 | audio_channel_mask_t channelMask, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1989 | size_t *pFrameCount, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1990 | audio_session_t sessionId, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1991 | size_t *pNotificationFrameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1992 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1993 | const AttributionSourceState& attributionSource, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1994 | audio_input_flags_t *flags, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1995 | pid_t tid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1996 | status_t *status /*non-NULL*/, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 1997 | audio_port_handle_t portId, |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 1998 | int32_t maxSharedAudioHistoryMs) final |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1999 | REQUIRES(audio_utils::AudioFlinger_Mutex) EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2000 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2001 | status_t start(IAfRecordTrack* recordTrack, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2002 | AudioSystem::sync_event_t event, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2003 | audio_session_t triggerSession) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2004 | |
| 2005 | // ask the thread to stop the specified track, and |
| 2006 | // return true if the caller should then do it's part of the stopping process |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2007 | bool stop(IAfRecordTrack* recordTrack) final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2008 | AudioStreamIn* getInput() const final { return mInput; } |
| 2009 | AudioStreamIn* clearInput() final; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2010 | |
Andy Hung | 99b1ba6 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 2011 | // TODO(b/291317898) Unify with IAfThreadBase |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2012 | virtual sp<StreamHalInterface> stream() const; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2013 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2014 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2015 | virtual bool checkForNewParameter_l(const String8& keyValuePair, |
| 2016 | status_t& status) REQUIRES(mutex()); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2017 | virtual void cacheParameters_l() REQUIRES(mutex(), ThreadBase_ThreadLoop) {} |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2018 | virtual String8 getParameters(const String8& keys) EXCLUDES_ThreadBase_Mutex; |
| 2019 | |
| 2020 | // Hold either the AudioFlinger::mutex or the ThreadBase::mutex |
| 2021 | void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0, |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2022 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2023 | virtual status_t createAudioPatch_l(const struct audio_patch *patch, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2024 | audio_patch_handle_t *handle) REQUIRES(mutex()); |
| 2025 | virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) REQUIRES(mutex()); |
| 2026 | void updateOutDevices(const DeviceDescriptorBaseVector& outDevices) override |
| 2027 | EXCLUDES_ThreadBase_Mutex; |
| 2028 | void resizeInputBuffer_l(int32_t maxSharedAudioHistoryMs) override REQUIRES(mutex()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2029 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2030 | void addPatchTrack(const sp<IAfPatchRecord>& record) final EXCLUDES_ThreadBase_Mutex; |
| 2031 | void deletePatchTrack(const sp<IAfPatchRecord>& record) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2032 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2033 | void readInputParameters_l() REQUIRES(mutex()); |
| 2034 | uint32_t getInputFramesLost() const final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2035 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2036 | virtual status_t addEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex()); |
| 2037 | virtual size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) REQUIRES(mutex()); |
| 2038 | uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) { |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 2039 | return ThreadBase::hasAudioSession_l(sessionId, mTracks); |
| 2040 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2041 | |
| 2042 | // Return the set of unique session IDs across all tracks. |
| 2043 | // The keys are the session IDs, and the associated values are meaningless. |
| 2044 | // FIXME replace by Set [and implement Bag/Multiset for other uses]. |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2045 | KeyedVector<audio_session_t, bool> sessionIds() const; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2046 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2047 | status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) override |
| 2048 | EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 2049 | bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2050 | |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 2051 | static void syncStartEventCallback(const wp<audioflinger::SyncEvent>& event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2052 | |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 2053 | virtual size_t frameCount() const { return mFrameCount; } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2054 | bool hasFastCapture() const final { return mFastCapture != 0; } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 2055 | virtual void toAudioPortConfig(struct audio_port_config *config); |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 2056 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2057 | virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc, |
| 2058 | audio_session_t sessionId) REQUIRES(mutex()); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2059 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2060 | virtual void acquireWakeLock_l() REQUIRES(mutex()) { |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2061 | ThreadBase::acquireWakeLock_l(); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2062 | mActiveTracks.updatePowerState_l(this, true /* force */); |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2065 | void checkBtNrec() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2066 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2067 | // Sets the UID records silence |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2068 | void setRecordSilenced(audio_port_handle_t portId, bool silenced) final |
| 2069 | EXCLUDES_ThreadBase_Mutex; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2070 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2071 | status_t getActiveMicrophones( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2072 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) const final |
| 2073 | EXCLUDES_ThreadBase_Mutex; |
| 2074 | status_t setPreferredMicrophoneDirection(audio_microphone_direction_t direction) final |
| 2075 | EXCLUDES_ThreadBase_Mutex; |
| 2076 | status_t setPreferredMicrophoneFieldDimension(float zoom) final EXCLUDES_ThreadBase_Mutex; |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2077 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2078 | MetadataUpdate updateMetadata_l() override REQUIRES(mutex()); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2079 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2080 | bool fastTrackAvailable() const final { return mFastTrackAvail; } |
| 2081 | void setFastTrackAvailable(bool available) final { mFastTrackAvail = available; } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 2082 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2083 | bool isTimestampCorrectionEnabled_l() const override REQUIRES(mutex()) { |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2084 | // checks popcount for exactly one device. |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 2085 | // Is currently disabled. Before enabling, |
| 2086 | // verify compressed record timestamps. |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2087 | return audio_is_input_device(mTimestampCorrectedDevice) |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2088 | && inDeviceType_l() == mTimestampCorrectedDevice; |
Andy Hung | c8fddf3 | 2018-08-08 18:32:37 -0700 | [diff] [blame] | 2089 | } |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2090 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2091 | status_t shareAudioHistory(const std::string& sharedAudioPackageName, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2092 | audio_session_t sharedSessionId = AUDIO_SESSION_NONE, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2093 | int64_t sharedAudioStartMs = -1) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2094 | status_t shareAudioHistory_l(const std::string& sharedAudioPackageName, |
| 2095 | audio_session_t sharedSessionId = AUDIO_SESSION_NONE, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2096 | int64_t sharedAudioStartMs = -1) REQUIRES(mutex()); |
| 2097 | void resetAudioHistory_l() final REQUIRES(mutex()); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2098 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2099 | bool isStreamInitialized() const final { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2100 | return !(mInput == nullptr || mInput->stream == nullptr); |
| 2101 | } |
| 2102 | |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2103 | protected: |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2104 | void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
| 2105 | void dumpTracks_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
Mikhail Naganov | 01dc5ca | 2019-03-29 10:12:12 -0700 | [diff] [blame] | 2106 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2107 | private: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2108 | // Enter standby if not already in standby, and set mStandby flag |
Glenn Kasten | 93e471f | 2013-08-19 08:40:07 -0700 | [diff] [blame] | 2109 | void standbyIfNotAlreadyInStandby(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2110 | |
| 2111 | // Call the HAL standby method unconditionally, and don't change mStandby flag |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2112 | void inputStandBy(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2113 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2114 | void checkBtNrec_l() REQUIRES(mutex()); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2115 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2116 | int32_t getOldestFront_l() REQUIRES(mutex()); |
| 2117 | void updateFronts_l(int32_t offset) REQUIRES(mutex()); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2118 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | AudioStreamIn *mInput; |
Mikhail Naganov | 2534b38 | 2019-09-25 13:05:02 -0700 | [diff] [blame] | 2120 | Source *mSource; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2121 | SortedVector <sp<IAfRecordTrack>> mTracks; |
Glenn Kasten | 2b80640 | 2013-11-20 16:37:38 -0800 | [diff] [blame] | 2122 | // mActiveTracks has dual roles: it indicates the current active track(s), and |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2123 | // is used together with mStartStopCV to indicate start()/stop() progress |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2124 | ActiveTracks<IAfRecordTrack> mActiveTracks; |
Andy Hung | dae2770 | 2016-10-31 14:01:16 -0700 | [diff] [blame] | 2125 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2126 | audio_utils::condition_variable mStartStopCV; |
Glenn Kasten | 9b58f63 | 2013-07-16 11:37:48 -0700 | [diff] [blame] | 2127 | |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2128 | // resampler converts input at HAL Hz to output at AudioRecord client Hz |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 2129 | void *mRsmpInBuffer; // size = mRsmpInFramesOA |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2130 | size_t mRsmpInFrames; // size of resampler input in frames |
| 2131 | size_t mRsmpInFramesP2;// size rounded up to a power-of-2 |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 2132 | size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2133 | |
| 2134 | // rolling index that is never cleared |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2135 | int32_t mRsmpInRear; // last filled frame + 1 |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2136 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2137 | // For dumpsys |
Glenn Kasten | b880f5e | 2014-05-07 08:43:45 -0700 | [diff] [blame] | 2138 | const sp<MemoryDealer> mReadOnlyHeap; |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 2139 | |
| 2140 | // one-time initialization, no locks required |
Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 2141 | sp<FastCapture> mFastCapture; // non-0 if there is also |
| 2142 | // a fast capture |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2143 | |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 2144 | // FIXME audio watchdog thread |
| 2145 | |
| 2146 | // contents are not guaranteed to be consistent, no locks required |
| 2147 | FastCaptureDumpState mFastCaptureDumpState; |
| 2148 | #ifdef STATE_QUEUE_DUMP |
| 2149 | // FIXME StateQueue observer and mutator dump fields |
| 2150 | #endif |
| 2151 | // FIXME audio watchdog dump |
| 2152 | |
| 2153 | // accessible only within the threadLoop(), no locks required |
| 2154 | // mFastCapture->sq() // for mutating and pushing state |
| 2155 | int32_t mFastCaptureFutex; // for cold idle |
| 2156 | |
| 2157 | // The HAL input source is treated as non-blocking, |
| 2158 | // but current implementation is blocking |
| 2159 | sp<NBAIO_Source> mInputSource; |
| 2160 | // The source for the normal capture thread to read from: mInputSource or mPipeSource |
| 2161 | sp<NBAIO_Source> mNormalSource; |
| 2162 | // If a fast capture is present, the non-blocking pipe sink written to by fast capture, |
| 2163 | // otherwise clear |
| 2164 | sp<NBAIO_Sink> mPipeSink; |
| 2165 | // If a fast capture is present, the non-blocking pipe source read by normal thread, |
| 2166 | // otherwise clear |
| 2167 | sp<NBAIO_Source> mPipeSource; |
| 2168 | // Depth of pipe from fast capture to normal thread and fast clients, always power of 2 |
| 2169 | size_t mPipeFramesP2; |
| 2170 | // If a fast capture is present, the Pipe as IMemory, otherwise clear |
| 2171 | sp<IMemory> mPipeMemory; |
| 2172 | |
Sanna Catherine de Treville Wager | 2a6a945 | 2017-07-28 11:02:01 -0700 | [diff] [blame] | 2173 | // TODO: add comment and adjust size as needed |
Glenn Kasten | 6dbb5e3 | 2014-05-13 10:38:42 -0700 | [diff] [blame] | 2174 | static const size_t kFastCaptureLogSize = 4 * 1024; |
| 2175 | sp<NBLog::Writer> mFastCaptureNBLogWriter; |
| 2176 | |
| 2177 | bool mFastTrackAvail; // true if fast track available |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 2178 | // common state to all record threads |
| 2179 | std::atomic_bool mBtNrecSuspended; |
Andy Hung | 6427e44 | 2018-08-09 12:51:02 -0700 | [diff] [blame] | 2180 | |
| 2181 | int64_t mFramesRead = 0; // continuous running counter. |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2182 | |
| 2183 | DeviceDescriptorBaseVector mOutDevices; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2184 | |
Eric Laurent | 5f0fd7b | 2021-05-07 16:33:26 +0200 | [diff] [blame] | 2185 | int32_t mMaxSharedAudioHistoryMs = 0; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2186 | std::string mSharedAudioPackageName = {}; |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 2187 | int32_t mSharedAudioStartFrames = -1; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2188 | audio_session_t mSharedAudioSessionId = AUDIO_SESSION_NONE; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2189 | }; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2190 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2191 | class MmapThread : public ThreadBase, public virtual IAfMmapThread |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2192 | { |
| 2193 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2194 | MmapThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 2195 | AudioHwDevice *hwDev, const sp<StreamHalInterface>& stream, bool systemReady, |
Andy Hung | cf10d74 | 2020-04-28 15:38:24 -0700 | [diff] [blame] | 2196 | bool isOut); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2197 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2198 | void configure(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2199 | audio_stream_type_t streamType, |
| 2200 | audio_session_t sessionId, |
| 2201 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 2202 | audio_port_handle_t deviceId, |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2203 | audio_port_handle_t portId) override EXCLUDES_ThreadBase_Mutex { |
| 2204 | audio_utils::lock_guard l(mutex()); |
| 2205 | configure_l(attr, streamType, sessionId, callback, deviceId, portId); |
| 2206 | } |
| 2207 | |
| 2208 | void configure_l(const audio_attributes_t* attr, |
| 2209 | audio_stream_type_t streamType, |
| 2210 | audio_session_t sessionId, |
| 2211 | const sp<MmapStreamCallback>& callback, |
| 2212 | audio_port_handle_t deviceId, |
| 2213 | audio_port_handle_t portId) REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2214 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2215 | void disconnect() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2216 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2217 | // MmapStreamInterface for adapter. |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2218 | status_t createMmapBuffer(int32_t minSizeFrames, struct audio_mmap_buffer_info* info) final |
| 2219 | EXCLUDES_ThreadBase_Mutex; |
| 2220 | status_t getMmapPosition(struct audio_mmap_position* position) const override |
| 2221 | EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2222 | status_t start(const AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 2223 | const audio_attributes_t *attr, |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2224 | audio_port_handle_t* handle) final EXCLUDES_ThreadBase_Mutex; |
| 2225 | status_t stop(audio_port_handle_t handle) final EXCLUDES_ThreadBase_Mutex; |
| 2226 | status_t standby() final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2227 | status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const |
| 2228 | EXCLUDES_ThreadBase_Mutex = 0; |
| 2229 | status_t reportData(const void* buffer, size_t frameCount) override EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2230 | |
| 2231 | // RefBase |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2232 | void onFirstRef() final; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2233 | |
| 2234 | // Thread virtuals |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2235 | bool threadLoop() final REQUIRES(ThreadBase_ThreadLoop) EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2236 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2237 | // Not in ThreadBase |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2238 | virtual void threadLoop_exit() final REQUIRES(ThreadBase_ThreadLoop); |
| 2239 | virtual void threadLoop_standby() final REQUIRES(ThreadBase_ThreadLoop); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2240 | virtual bool shouldStandby_l() final REQUIRES(mutex()){ return false; } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2241 | virtual status_t exitStandby_l() REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2242 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2243 | status_t initCheck() const final { return mHalStream == nullptr ? NO_INIT : NO_ERROR; } |
| 2244 | size_t frameCount() const final { return mFrameCount; } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2245 | bool checkForNewParameter_l(const String8& keyValuePair, status_t& status) |
| 2246 | final REQUIRES(mutex()); |
| 2247 | String8 getParameters(const String8& keys) final EXCLUDES_ThreadBase_Mutex; |
| 2248 | void ioConfigChanged_l(audio_io_config_event_t event, pid_t pid = 0, |
| 2249 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE) final |
| 2250 | /* holds either AF::mutex or TB::mutex */; |
| 2251 | void readHalParameters_l() REQUIRES(mutex()); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2252 | void cacheParameters_l() final REQUIRES(mutex(), ThreadBase_ThreadLoop) {} |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2253 | status_t createAudioPatch_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2254 | const struct audio_patch* patch, audio_patch_handle_t* handle) final |
| 2255 | REQUIRES(mutex()); |
| 2256 | status_t releaseAudioPatch_l(const audio_patch_handle_t handle) final |
| 2257 | REQUIRES(mutex()); |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2258 | // NO_THREAD_SAFETY_ANALYSIS |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2259 | void toAudioPortConfig(struct audio_port_config* config) override; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2260 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2261 | sp<StreamHalInterface> stream() const final { return mHalStream; } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2262 | status_t addEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex()); |
| 2263 | size_t removeEffectChain_l(const sp<IAfEffectChain>& chain) final REQUIRES(mutex()); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2264 | status_t checkEffectCompatibility_l( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2265 | const effect_descriptor_t *desc, audio_session_t sessionId) final REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2266 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2267 | uint32_t hasAudioSession_l(audio_session_t sessionId) const override REQUIRES(mutex()) { |
Andy Hung | c3d62f9 | 2019-03-14 13:38:51 -0700 | [diff] [blame] | 2268 | // Note: using mActiveTracks as no mTracks here. |
| 2269 | return ThreadBase::hasAudioSession_l(sessionId, mActiveTracks); |
| 2270 | } |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2271 | status_t setSyncEvent(const sp<audioflinger::SyncEvent>& event) final; |
| 2272 | bool isValidSyncEvent(const sp<audioflinger::SyncEvent>& event) const final; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2273 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2274 | virtual void checkSilentMode_l() REQUIRES(mutex()) {} // cannot be const (RecordThread) |
| 2275 | virtual void processVolume_l() REQUIRES(mutex()) {} |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2276 | void checkInvalidTracks_l() REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2277 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2278 | // Not in ThreadBase |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2279 | virtual audio_stream_type_t streamType_l() const REQUIRES(mutex()) { |
| 2280 | return AUDIO_STREAM_DEFAULT; |
| 2281 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2282 | virtual void invalidateTracks(audio_stream_type_t /* streamType */) |
| 2283 | EXCLUDES_ThreadBase_Mutex {} |
| 2284 | void invalidateTracks(std::set<audio_port_handle_t>& /* portIds */) override |
| 2285 | EXCLUDES_ThreadBase_Mutex {} |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2286 | |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2287 | // Sets the UID records silence |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2288 | void setRecordSilenced( |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2289 | audio_port_handle_t /* portId */, bool /* silenced */) override |
| 2290 | EXCLUDES_ThreadBase_Mutex {} |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2291 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2292 | bool isStreamInitialized() const override { return false; } |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2293 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2294 | void setClientSilencedState_l(audio_port_handle_t portId, bool silenced) REQUIRES(mutex()) { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 2295 | mClientSilencedStates[portId] = silenced; |
| 2296 | } |
| 2297 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2298 | size_t eraseClientSilencedState_l(audio_port_handle_t portId) REQUIRES(mutex()) { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 2299 | return mClientSilencedStates.erase(portId); |
| 2300 | } |
| 2301 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2302 | bool isClientSilenced_l(audio_port_handle_t portId) const REQUIRES(mutex()) { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 2303 | const auto it = mClientSilencedStates.find(portId); |
| 2304 | return it != mClientSilencedStates.end() ? it->second : false; |
| 2305 | } |
| 2306 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2307 | void setClientSilencedIfExists_l(audio_port_handle_t portId, bool silenced) |
| 2308 | REQUIRES(mutex()) { |
jiabin | 0960903 | 2022-06-15 19:26:01 +0000 | [diff] [blame] | 2309 | const auto it = mClientSilencedStates.find(portId); |
| 2310 | if (it != mClientSilencedStates.end()) { |
| 2311 | it->second = silenced; |
| 2312 | } |
| 2313 | } |
| 2314 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2315 | protected: |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2316 | void dumpInternals_l(int fd, const Vector<String16>& args) override REQUIRES(mutex()); |
| 2317 | void dumpTracks_l(int fd, const Vector<String16>& args) final REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2318 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2319 | /** |
| 2320 | * @brief mDeviceId current device port unique identifier |
| 2321 | */ |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2322 | audio_port_handle_t mDeviceId GUARDED_BY(mutex()) = AUDIO_PORT_HANDLE_NONE; |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2323 | |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2324 | audio_attributes_t mAttr GUARDED_BY(mutex()); |
| 2325 | audio_session_t mSessionId GUARDED_BY(mutex()); |
| 2326 | audio_port_handle_t mPortId GUARDED_BY(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2327 | |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2328 | wp<MmapStreamCallback> mCallback GUARDED_BY(mutex()); |
| 2329 | sp<StreamHalInterface> mHalStream; // NO_THREAD_SAFETY_ANALYSIS |
| 2330 | sp<DeviceHalInterface> mHalDevice GUARDED_BY(mutex()); |
| 2331 | AudioHwDevice* const mAudioHwDev GUARDED_BY(mutex()); |
| 2332 | ActiveTracks<IAfMmapTrack> mActiveTracks GUARDED_BY(mutex()); |
| 2333 | float mHalVolFloat GUARDED_BY(mutex()); |
| 2334 | std::map<audio_port_handle_t, bool> mClientSilencedStates GUARDED_BY(mutex()); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2335 | |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2336 | int32_t mNoCallbackWarningCount GUARDED_BY(mutex()); |
| 2337 | static constexpr int32_t kMaxNoCallbackWarnings = 5; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2338 | }; |
| 2339 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2340 | class MmapPlaybackThread : public MmapThread, public IAfMmapPlaybackThread, |
| 2341 | public virtual VolumeInterface { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2342 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2343 | MmapPlaybackThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2344 | AudioHwDevice *hwDev, AudioStreamOut *output, bool systemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2345 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2346 | sp<IAfMmapPlaybackThread> asIAfMmapPlaybackThread() final { |
| 2347 | return sp<IAfMmapPlaybackThread>::fromExisting(this); |
| 2348 | } |
| 2349 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2350 | void configure(const audio_attributes_t* attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2351 | audio_stream_type_t streamType, |
| 2352 | audio_session_t sessionId, |
| 2353 | const sp<MmapStreamCallback>& callback, |
Eric Laurent | 7aa0ccb | 2017-08-28 11:12:52 -0700 | [diff] [blame] | 2354 | audio_port_handle_t deviceId, |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2355 | audio_port_handle_t portId) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2356 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2357 | AudioStreamOut* clearOutput() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2358 | |
| 2359 | // VolumeInterface |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2360 | void setMasterVolume(float value) final; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2361 | // Needs implementation? |
| 2362 | void setMasterBalance(float /* value */) final EXCLUDES_ThreadBase_Mutex {} |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2363 | void setMasterMute(bool muted) final EXCLUDES_ThreadBase_Mutex; |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2364 | void setStreamVolume(audio_stream_type_t stream, float value) final EXCLUDES_ThreadBase_Mutex; |
| 2365 | void setStreamMute(audio_stream_type_t stream, bool muted) final EXCLUDES_ThreadBase_Mutex; |
| 2366 | float streamVolume(audio_stream_type_t stream) const final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2367 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2368 | void setMasterMute_l(bool muted) REQUIRES(mutex()) { mMasterMute = muted; } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2369 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2370 | void invalidateTracks(audio_stream_type_t streamType) final EXCLUDES_ThreadBase_Mutex; |
| 2371 | void invalidateTracks(std::set<audio_port_handle_t>& portIds) final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2372 | |
Andy Hung | 3f49ebb | 2023-09-19 14:48:41 -0700 | [diff] [blame] | 2373 | audio_stream_type_t streamType_l() const final REQUIRES(mutex()) { |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2374 | return mStreamType; |
| 2375 | } |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2376 | void checkSilentMode_l() final REQUIRES(mutex()); |
| 2377 | void processVolume_l() final REQUIRES(mutex()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2378 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2379 | MetadataUpdate updateMetadata_l() final REQUIRES(mutex()); |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2380 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2381 | void toAudioPortConfig(struct audio_port_config* config) final; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 2382 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2383 | status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 2384 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2385 | bool isStreamInitialized() const final { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2386 | return !(mOutput == nullptr || mOutput->stream == nullptr); |
| 2387 | } |
| 2388 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2389 | status_t reportData(const void* buffer, size_t frameCount) final; |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 2390 | |
Andy Hung | 972bec1 | 2023-08-31 16:13:39 -0700 | [diff] [blame] | 2391 | void startMelComputation_l(const sp<audio_utils::MelProcessor>& processor) final |
| 2392 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
| 2393 | void stopMelComputation_l() final |
| 2394 | REQUIRES(audio_utils::AudioFlinger_Mutex); |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2395 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2396 | protected: |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2397 | void dumpInternals_l(int fd, const Vector<String16>& args) final REQUIRES(mutex()); |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2398 | float streamVolume_l() const REQUIRES(mutex()) { |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 2399 | return mStreamTypes[mStreamType].volume; |
| 2400 | } |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2401 | bool streamMuted_l() const REQUIRES(mutex()) { |
Eric Laurent | 1f9b5e6 | 2023-07-03 18:14:07 +0200 | [diff] [blame] | 2402 | return mStreamTypes[mStreamType].mute; |
| 2403 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2404 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2405 | stream_type_t mStreamTypes[AUDIO_STREAM_CNT] GUARDED_BY(mutex()); |
| 2406 | audio_stream_type_t mStreamType GUARDED_BY(mutex()); |
| 2407 | float mMasterVolume GUARDED_BY(mutex()); |
| 2408 | bool mMasterMute GUARDED_BY(mutex()); |
| 2409 | AudioStreamOut* mOutput; // NO_THREAD_SAFETY_ANALYSIS |
Vlad Popa | 6fbbfbf | 2023-02-22 15:05:43 +0100 | [diff] [blame] | 2410 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2411 | mediautils::atomic_sp<audio_utils::MelProcessor> mMelProcessor; // locked internally |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2412 | }; |
| 2413 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2414 | class MmapCaptureThread : public MmapThread, public IAfMmapCaptureThread |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2415 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2416 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2417 | MmapCaptureThread(const sp<IAfThreadCallback>& afThreadCallback, audio_io_handle_t id, |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2418 | AudioHwDevice *hwDev, AudioStreamIn *input, bool systemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2419 | |
Andy Hung | 7aa7d10 | 2023-07-07 15:58:48 -0700 | [diff] [blame] | 2420 | sp<IAfMmapCaptureThread> asIAfMmapCaptureThread() final { |
| 2421 | return sp<IAfMmapCaptureThread>::fromExisting(this); |
| 2422 | } |
| 2423 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2424 | AudioStreamIn* clearInput() final EXCLUDES_ThreadBase_Mutex; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2425 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2426 | status_t exitStandby_l() REQUIRES(mutex()) final; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2427 | |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 2428 | MetadataUpdate updateMetadata_l() final REQUIRES(mutex()); |
| 2429 | void processVolume_l() final REQUIRES(mutex()); |
| 2430 | void setRecordSilenced(audio_port_handle_t portId, bool silenced) final |
| 2431 | EXCLUDES_ThreadBase_Mutex; |
Kevin Rocard | 069c271 | 2018-03-29 19:09:14 -0700 | [diff] [blame] | 2432 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2433 | void toAudioPortConfig(struct audio_port_config* config) final; |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 2434 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2435 | status_t getExternalPosition(uint64_t* position, int64_t* timeNanos) const final; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 2436 | |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2437 | bool isStreamInitialized() const final { |
Jasmine Cha | eaa10e4 | 2021-05-11 10:11:14 +0800 | [diff] [blame] | 2438 | return !(mInput == nullptr || mInput->stream == nullptr); |
| 2439 | } |
| 2440 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2441 | protected: |
| 2442 | |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2443 | AudioStreamIn* mInput; // NO_THREAD_SAFETY_ANALYSIS |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2444 | }; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2445 | |
| 2446 | class BitPerfectThread : public MixerThread { |
| 2447 | public: |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 2448 | BitPerfectThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut *output, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2449 | audio_io_handle_t id, bool systemReady); |
| 2450 | |
| 2451 | protected: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2452 | mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final |
| 2453 | REQUIRES(mutex(), ThreadBase_ThreadLoop); |
| 2454 | void threadLoop_mix() final REQUIRES(ThreadBase_ThreadLoop); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2455 | |
| 2456 | private: |
Andy Hung | 8d672e0 | 2023-09-15 18:19:28 -0700 | [diff] [blame] | 2457 | // These variables are only accessed on the threadLoop; hence need no mutex. |
| 2458 | bool mIsBitPerfect GUARDED_BY(ThreadBase_ThreadLoop) = false; |
| 2459 | float mVolumeLeft GUARDED_BY(ThreadBase_ThreadLoop) = 0.f; |
| 2460 | float mVolumeRight GUARDED_BY(ThreadBase_ThreadLoop) = 0.f; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 2461 | }; |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2462 | |
Andy Hung | ee58e4a | 2023-07-07 13:47:37 -0700 | [diff] [blame] | 2463 | } // namespace android |