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