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