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