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