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