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