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