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