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 | //--- Audio Effect Management |
| 23 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 24 | // Interface implemented by the EffectModule parent or owner (e.g an EffectChain) to abstract |
| 25 | // interactions between the EffectModule and the reset of the audio framework. |
| 26 | class EffectCallbackInterface : public RefBase { |
| 27 | public: |
| 28 | ~EffectCallbackInterface() override = default; |
| 29 | |
| 30 | // Trivial methods usually implemented with help from ThreadBase |
| 31 | virtual audio_io_handle_t io() const = 0; |
| 32 | virtual bool isOutput() const = 0; |
| 33 | virtual bool isOffload() const = 0; |
| 34 | virtual bool isOffloadOrDirect() const = 0; |
| 35 | virtual bool isOffloadOrMmap() const = 0; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 36 | virtual uint32_t sampleRate() const = 0; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 37 | virtual audio_channel_mask_t inChannelMask(int id) const = 0; |
| 38 | virtual uint32_t inChannelCount(int id) const = 0; |
| 39 | virtual audio_channel_mask_t outChannelMask() const = 0; |
| 40 | virtual uint32_t outChannelCount() const = 0; |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 41 | virtual audio_channel_mask_t hapticChannelMask() const = 0; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 42 | virtual size_t frameCount() const = 0; |
| 43 | |
| 44 | // Non trivial methods usually implemented with help from ThreadBase: |
| 45 | // pay attention to mutex locking order |
| 46 | virtual uint32_t latency() const { return 0; } |
| 47 | virtual status_t addEffectToHal(sp<EffectHalInterface> effect) = 0; |
| 48 | virtual status_t removeEffectFromHal(sp<EffectHalInterface> effect) = 0; |
| 49 | virtual void setVolumeForOutput(float left, float right) const = 0; |
| 50 | virtual bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) = 0; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 51 | virtual void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect, |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 52 | bool enabled, |
| 53 | bool threadLocked) = 0; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 54 | virtual void onEffectEnable(const sp<EffectBase>& effect) = 0; |
| 55 | virtual void onEffectDisable(const sp<EffectBase>& effect) = 0; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 56 | |
| 57 | // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order |
| 58 | virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid, |
| 59 | int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0; |
| 60 | virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 61 | virtual bool updateOrphanEffectChains(const sp<EffectBase>& effect) = 0; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 62 | |
| 63 | // Methods usually implemented with help from EffectChain: pay attention to mutex locking order |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 64 | virtual product_strategy_t strategy() const = 0; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 65 | virtual int32_t activeTrackCnt() const = 0; |
| 66 | virtual void resetVolume() = 0; |
| 67 | |
| 68 | virtual wp<EffectChain> chain() const = 0; |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 69 | |
| 70 | virtual bool isAudioPolicyReady() const = 0; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 73 | // EffectBase(EffectModule) and EffectChain classes both have their own mutex to protect |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 74 | // state changes or resource modifications. Always respect the following order |
| 75 | // if multiple mutexes must be acquired to avoid cross deadlock: |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 76 | // AudioFlinger -> ThreadBase -> EffectChain -> EffectBase(EffectModule) |
| 77 | // AudioHandle -> ThreadBase -> EffectChain -> EffectBase(EffectModule) |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 78 | |
| 79 | // NOTE: When implementing the EffectCallbackInterface, in an EffectChain or other, it is important |
| 80 | // to pay attention to this locking order as some callback methods can be called from a state where |
| 81 | // EffectModule and/or EffectChain mutexes are held. |
| 82 | |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 83 | // In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(), |
Eric Laurent | 3bc859b | 2016-12-05 11:07:22 -0800 | [diff] [blame] | 84 | // startOutput(), getInputForAttr(), releaseInput()...) should never be called with AudioFlinger or |
| 85 | // Threadbase mutex locked to avoid cross deadlock with other clients calling AudioPolicyService |
| 86 | // methods that in turn call AudioFlinger thus locking the same mutexes in the reverse order. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 87 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 88 | |
| 89 | // The EffectBase class contains common properties, state and behavior for and EffectModule or |
| 90 | // other derived classes managing an audio effect instance within the effect framework. |
| 91 | // It also contains the class mutex (see comment on locking order above). |
| 92 | class EffectBase : public RefBase { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 93 | public: |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 94 | EffectBase(const sp<EffectCallbackInterface>& callback, |
| 95 | effect_descriptor_t *desc, |
| 96 | int id, |
| 97 | audio_session_t sessionId, |
| 98 | bool pinned); |
| 99 | |
| 100 | ~EffectBase() override = default; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 101 | |
| 102 | enum effect_state { |
| 103 | IDLE, |
| 104 | RESTART, |
| 105 | STARTING, |
| 106 | ACTIVE, |
| 107 | STOPPING, |
| 108 | STOPPED, |
| 109 | DESTROYED |
| 110 | }; |
| 111 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 112 | int id() const { return mId; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 113 | effect_state state() const { |
| 114 | return mState; |
| 115 | } |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 116 | audio_session_t sessionId() const { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 117 | return mSessionId; |
| 118 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 119 | const effect_descriptor_t& desc() const { return mDescriptor; } |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 120 | bool isOffloadable() const |
| 121 | { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; } |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 122 | bool isImplementationSoftware() const |
| 123 | { return (mDescriptor.flags & EFFECT_FLAG_HW_ACC_MASK) == 0; } |
Eric Laurent | 6dd0fd9 | 2016-09-15 12:44:53 -0700 | [diff] [blame] | 124 | bool isProcessImplemented() const |
| 125 | { return (mDescriptor.flags & EFFECT_FLAG_NO_PROCESS) == 0; } |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 126 | bool isVolumeControl() const |
| 127 | { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) |
| 128 | == EFFECT_FLAG_VOLUME_CTRL; } |
Jasmine Cha | 934ecfb | 2019-01-23 18:19:14 +0800 | [diff] [blame] | 129 | bool isVolumeMonitor() const |
| 130 | { return (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) |
| 131 | == EFFECT_FLAG_VOLUME_MONITOR; } |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 132 | |
| 133 | virtual status_t setEnabled(bool enabled, bool fromHandle); |
| 134 | status_t setEnabled_l(bool enabled); |
| 135 | bool isEnabled() const; |
| 136 | |
| 137 | void setSuspended(bool suspended); |
| 138 | bool suspended() const; |
| 139 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 140 | virtual status_t command(int32_t __unused, |
| 141 | const std::vector<uint8_t>& __unused, |
| 142 | int32_t __unused, |
| 143 | std::vector<uint8_t>* __unused) { return NO_ERROR; }; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 144 | |
Andy Hung | fda4400 | 2021-06-03 17:23:16 -0700 | [diff] [blame] | 145 | // mCallback is atomic so this can be lock-free. |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 146 | void setCallback(const sp<EffectCallbackInterface>& callback) { mCallback = callback; } |
Andy Hung | fda4400 | 2021-06-03 17:23:16 -0700 | [diff] [blame] | 147 | sp<EffectCallbackInterface> getCallback() const { return mCallback.load(); } |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 148 | |
| 149 | status_t addHandle(EffectHandle *handle); |
| 150 | ssize_t disconnectHandle(EffectHandle *handle, bool unpinIfLast); |
| 151 | ssize_t removeHandle(EffectHandle *handle); |
Jaideep Sharma | ed868802 | 2020-08-07 14:09:16 +0530 | [diff] [blame] | 152 | ssize_t removeHandle_l(EffectHandle *handle); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 153 | EffectHandle* controlHandle_l(); |
| 154 | bool purgeHandles(); |
| 155 | |
| 156 | void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked); |
| 157 | |
| 158 | bool isPinned() const { return mPinned; } |
| 159 | void unPin() { mPinned = false; } |
| 160 | |
| 161 | void lock() { mLock.lock(); } |
| 162 | void unlock() { mLock.unlock(); } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 163 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 164 | status_t updatePolicyState(); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 165 | |
| 166 | virtual sp<EffectModule> asEffectModule() { return nullptr; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 167 | virtual sp<DeviceEffectProxy> asDeviceEffectProxy() { return nullptr; } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 168 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 169 | void dump(int fd, const Vector<String16>& args); |
| 170 | |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 171 | protected: |
| 172 | bool isInternal_l() const { |
| 173 | for (auto handle : mHandles) { |
| 174 | if (handle->client() != nullptr) { |
| 175 | return false; |
| 176 | } |
| 177 | } |
| 178 | return true; |
| 179 | } |
| 180 | |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 181 | private: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 182 | friend class AudioFlinger; // for mHandles |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 183 | bool mPinned = false; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(EffectBase); |
| 186 | |
| 187 | mutable Mutex mLock; // mutex for process, commands and handles list protection |
Andy Hung | fda4400 | 2021-06-03 17:23:16 -0700 | [diff] [blame] | 188 | mediautils::atomic_sp<EffectCallbackInterface> mCallback; // parent effect chain |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 189 | const int mId; // this instance unique ID |
| 190 | const audio_session_t mSessionId; // audio session ID |
| 191 | const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine |
| 192 | effect_state mState = IDLE; // current activation state |
Eric Laurent | d9eb496 | 2019-12-19 09:20:49 -0800 | [diff] [blame] | 193 | // effect is suspended: temporarily disabled by framework |
| 194 | bool mSuspended = false; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 195 | |
| 196 | Vector<EffectHandle *> mHandles; // list of client handles |
| 197 | // First handle in mHandles has highest priority and controls the effect module |
| 198 | |
| 199 | // Audio policy effect state management |
| 200 | // Mutex protecting transactions with audio policy manager as mLock cannot |
| 201 | // be held to avoid cross deadlocks with audio policy mutex |
| 202 | Mutex mPolicyLock; |
| 203 | // Effect is registered in APM or not |
| 204 | bool mPolicyRegistered = false; |
| 205 | // Effect enabled state communicated to APM. Enabled state corresponds to |
| 206 | // state requested by the EffectHandle with control |
| 207 | bool mPolicyEnabled = false; |
| 208 | }; |
| 209 | |
| 210 | // The EffectModule class is a wrapper object controlling the effect engine implementation |
| 211 | // in the effect library. It prevents concurrent calls to process() and command() functions |
| 212 | // from different client threads. It keeps a list of EffectHandle objects corresponding |
| 213 | // to all client applications using this effect and notifies applications of effect state, |
| 214 | // control or parameter changes. It manages the activation state machine to send appropriate |
| 215 | // reset, enable, disable commands to effect engine and provide volume |
| 216 | // ramping when effects are activated/deactivated. |
| 217 | // When controlling an auxiliary effect, the EffectModule also provides an input buffer used by |
| 218 | // the attached track(s) to accumulate their auxiliary channel. |
| 219 | class EffectModule : public EffectBase { |
| 220 | public: |
| 221 | EffectModule(const sp<EffectCallbackInterface>& callabck, |
| 222 | effect_descriptor_t *desc, |
| 223 | int id, |
| 224 | audio_session_t sessionId, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 225 | bool pinned, |
| 226 | audio_port_handle_t deviceId); |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 227 | virtual ~EffectModule(); |
| 228 | |
| 229 | void process(); |
| 230 | bool updateState(); |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 231 | status_t command(int32_t cmdCode, |
| 232 | const std::vector<uint8_t>& cmdData, |
| 233 | int32_t maxReplySize, |
| 234 | std::vector<uint8_t>* reply) override; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 235 | |
| 236 | void reset_l(); |
| 237 | status_t configure(); |
| 238 | status_t init(); |
| 239 | |
| 240 | uint32_t status() { |
| 241 | return mStatus; |
| 242 | } |
| 243 | |
| 244 | bool isProcessEnabled() const; |
| 245 | bool isOffloadedOrDirect() const; |
| 246 | bool isVolumeControlEnabled() const; |
| 247 | |
| 248 | void setInBuffer(const sp<EffectBufferHalInterface>& buffer); |
| 249 | int16_t *inBuffer() const { |
| 250 | return mInBuffer != 0 ? reinterpret_cast<int16_t*>(mInBuffer->ptr()) : NULL; |
| 251 | } |
| 252 | void setOutBuffer(const sp<EffectBufferHalInterface>& buffer); |
| 253 | int16_t *outBuffer() const { |
| 254 | return mOutBuffer != 0 ? reinterpret_cast<int16_t*>(mOutBuffer->ptr()) : NULL; |
| 255 | } |
| 256 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 257 | status_t setDevices(const AudioDeviceTypeAddrVector &devices); |
| 258 | status_t setInputDevice(const AudioDeviceTypeAddr &device); |
| 259 | status_t setVolume(uint32_t *left, uint32_t *right, bool controller); |
| 260 | status_t setMode(audio_mode_t mode); |
| 261 | status_t setAudioSource(audio_source_t source); |
| 262 | status_t start(); |
| 263 | status_t stop(); |
| 264 | |
| 265 | status_t setOffloaded(bool offloaded, audio_io_handle_t io); |
| 266 | bool isOffloaded() const; |
| 267 | void addEffectToHal_l(); |
| 268 | void release_l(); |
| 269 | |
| 270 | sp<EffectModule> asEffectModule() override { return this; } |
| 271 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 272 | static bool isHapticGenerator(const effect_uuid_t* type); |
| 273 | bool isHapticGenerator() const; |
| 274 | |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 275 | status_t setHapticIntensity(int id, int intensity); |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 276 | status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo); |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 277 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 278 | void dump(int fd, const Vector<String16>& args); |
| 279 | |
| 280 | private: |
| 281 | friend class AudioFlinger; // for mHandles |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 282 | |
| 283 | // Maximum time allocated to effect engines to complete the turn off sequence |
| 284 | static const uint32_t MAX_DISABLE_TIME_MS = 10000; |
| 285 | |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 286 | DISALLOW_COPY_AND_ASSIGN(EffectModule); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 287 | |
| 288 | status_t start_l(); |
| 289 | status_t stop_l(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 290 | status_t removeEffectFromHal_l(); |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 291 | status_t sendSetAudioDevicesCommand(const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 292 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 293 | effect_config_t mConfig; // input and output audio configuration |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 294 | sp<EffectHalInterface> mEffectInterface; // Effect module HAL |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 295 | sp<EffectBufferHalInterface> mInBuffer; // Buffers for interacting with HAL |
| 296 | sp<EffectBufferHalInterface> mOutBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 297 | status_t mStatus; // initialization status |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 298 | // First handle in mHandles has highest priority and controls the effect module |
| 299 | uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after |
| 300 | // sending disable command. |
| 301 | uint32_t mDisableWaitCnt; // current process() calls count during disable period. |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 302 | bool mOffloaded; // effect is currently offloaded to the audio DSP |
David Li | 6c8ac4b | 2021-06-22 22:17:52 +0800 | [diff] [blame] | 303 | bool mAddedToHal; // effect has been added to the audio HAL |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 304 | |
| 305 | #ifdef FLOAT_EFFECT_CHAIN |
| 306 | bool mSupportsFloat; // effect supports float processing |
Andy Hung | bded9c8 | 2017-11-30 18:47:35 -0800 | [diff] [blame] | 307 | sp<EffectBufferHalInterface> mInConversionBuffer; // Buffers for HAL conversion if needed. |
| 308 | sp<EffectBufferHalInterface> mOutConversionBuffer; |
Andy Hung | 9aad48c | 2017-11-29 10:29:19 -0800 | [diff] [blame] | 309 | uint32_t mInChannelCountRequested; |
| 310 | uint32_t mOutChannelCountRequested; |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 311 | #endif |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 312 | |
| 313 | class AutoLockReentrant { |
| 314 | public: |
| 315 | AutoLockReentrant(Mutex& mutex, pid_t allowedTid) |
| 316 | : mMutex(gettid() == allowedTid ? nullptr : &mutex) |
| 317 | { |
| 318 | if (mMutex != nullptr) mMutex->lock(); |
| 319 | } |
| 320 | ~AutoLockReentrant() { |
| 321 | if (mMutex != nullptr) mMutex->unlock(); |
| 322 | } |
| 323 | private: |
| 324 | Mutex * const mMutex; |
| 325 | }; |
| 326 | |
| 327 | static constexpr pid_t INVALID_PID = (pid_t)-1; |
| 328 | // this tid is allowed to call setVolume() without acquiring the mutex. |
| 329 | pid_t mSetVolumeReentrantTid = INVALID_PID; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | // The EffectHandle class implements the IEffect interface. It provides resources |
| 333 | // to receive parameter updates, keeps track of effect control |
| 334 | // ownership and state and has a pointer to the EffectModule object it is controlling. |
| 335 | // There is one EffectHandle object for each application controlling (or using) |
| 336 | // an effect module. |
| 337 | // The EffectHandle is obtained by calling AudioFlinger::createEffect(). |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 338 | class EffectHandle: public android::media::BnEffect { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 339 | public: |
| 340 | |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 341 | EffectHandle(const sp<EffectBase>& effect, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 342 | const sp<AudioFlinger::Client>& client, |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 343 | const sp<media::IEffectClient>& effectClient, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 344 | int32_t priority, bool notifyFramesProcessed); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 345 | virtual ~EffectHandle(); |
Glenn Kasten | e75da40 | 2013-11-20 13:54:52 -0800 | [diff] [blame] | 346 | virtual status_t initCheck(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 347 | |
| 348 | // IEffect |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 349 | android::binder::Status enable(int32_t* _aidl_return) override; |
| 350 | android::binder::Status disable(int32_t* _aidl_return) override; |
| 351 | android::binder::Status command(int32_t cmdCode, |
| 352 | const std::vector<uint8_t>& cmdData, |
| 353 | int32_t maxResponseSize, |
| 354 | std::vector<uint8_t>* response, |
| 355 | int32_t* _aidl_return) override; |
| 356 | android::binder::Status disconnect() override; |
| 357 | android::binder::Status getCblk(media::SharedFileRegion* _aidl_return) override; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 358 | |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 359 | sp<Client> client() const { return mClient; } |
| 360 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 361 | private: |
| 362 | void disconnect(bool unpinIfLast); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 363 | |
| 364 | // Give or take control of effect module |
| 365 | // - hasControl: true if control is given, false if removed |
| 366 | // - signal: true client app should be signaled of change, false otherwise |
| 367 | // - enabled: state of the effect when control is passed |
| 368 | void setControl(bool hasControl, bool signal, bool enabled); |
| 369 | void commandExecuted(uint32_t cmdCode, |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 370 | const std::vector<uint8_t>& cmdData, |
| 371 | const std::vector<uint8_t>& replyData); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 372 | void setEnabled(bool enabled); |
| 373 | bool enabled() const { return mEnabled; } |
| 374 | |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 375 | void framesProcessed(int32_t frames) const; |
| 376 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 377 | // Getters |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 378 | wp<EffectBase> effect() const { return mEffect; } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 379 | int id() const { |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 380 | sp<EffectBase> effect = mEffect.promote(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 381 | if (effect == 0) { |
| 382 | return 0; |
| 383 | } |
| 384 | return effect->id(); |
| 385 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 386 | int priority() const { return mPriority; } |
| 387 | bool hasControl() const { return mHasControl; } |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 388 | bool disconnected() const { return mDisconnected; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 389 | |
Glenn Kasten | 01d3acb | 2014-02-06 08:24:07 -0800 | [diff] [blame] | 390 | void dumpToBuffer(char* buffer, size_t size); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 391 | |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 392 | private: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 393 | friend class AudioFlinger; // for mEffect, mHasControl, mEnabled |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 394 | DISALLOW_COPY_AND_ASSIGN(EffectHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 395 | |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 396 | Mutex mLock; // protects IEffect method calls |
Andy Hung | 318e024 | 2020-12-21 10:33:49 -0800 | [diff] [blame] | 397 | const wp<EffectBase> mEffect; // pointer to controlled EffectModule |
| 398 | const sp<media::IEffectClient> mEffectClient; // callback interface for client notifications |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 399 | /*const*/ sp<Client> mClient; // client for shared memory allocation, see |
| 400 | // disconnect() |
| 401 | sp<IMemory> mCblkMemory; // shared memory for control block |
| 402 | effect_param_cblk_t* mCblk; // control block for deferred parameter setting via |
| 403 | // shared memory |
| 404 | uint8_t* mBuffer; // pointer to parameter area in shared memory |
| 405 | int mPriority; // client application priority to control the effect |
| 406 | bool mHasControl; // true if this handle is controlling the effect |
| 407 | bool mEnabled; // cached enable state: needed when the effect is |
| 408 | // restored after being suspended |
| 409 | bool mDisconnected; // Set to true by disconnect() |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 410 | const bool mNotifyFramesProcessed; // true if the client callback event |
| 411 | // EVENT_FRAMES_PROCESSED must be generated |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | // the EffectChain class represents a group of effects associated to one audio session. |
| 415 | // There can be any number of EffectChain objects per output mixer thread (PlaybackThread). |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 416 | // The EffectChain with session ID AUDIO_SESSION_OUTPUT_MIX contains global effects applied |
| 417 | // to the output mix. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 418 | // Effects in this chain can be insert or auxiliary. Effects in other chains (attached to |
| 419 | // tracks) are insert only. The EffectChain maintains an ordered list of effect module, the |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 420 | // order corresponding in the effect process order. When attached to a track (session ID != |
| 421 | // AUDIO_SESSION_OUTPUT_MIX), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 422 | // it also provide it's own input buffer used by the track as accumulation buffer. |
| 423 | class EffectChain : public RefBase { |
| 424 | public: |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 425 | EffectChain(const wp<ThreadBase>& wThread, audio_session_t sessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 426 | virtual ~EffectChain(); |
| 427 | |
| 428 | // special key used for an entry in mSuspendedEffects keyed vector |
| 429 | // corresponding to a suspend all request. |
| 430 | static const int kKeyForSuspendAll = 0; |
| 431 | |
| 432 | // minimum duration during which we force calling effect process when last track on |
| 433 | // a session is stopped or removed to allow effect tail to be rendered |
| 434 | static const int kProcessTailDurationMs = 1000; |
| 435 | |
| 436 | void process_l(); |
| 437 | |
| 438 | void lock() { |
| 439 | mLock.lock(); |
| 440 | } |
| 441 | void unlock() { |
| 442 | mLock.unlock(); |
| 443 | } |
| 444 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 445 | status_t createEffect_l(sp<EffectModule>& effect, |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 446 | effect_descriptor_t *desc, |
| 447 | int id, |
| 448 | audio_session_t sessionId, |
| 449 | bool pinned); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 450 | status_t addEffect_l(const sp<EffectModule>& handle); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 451 | status_t addEffect_ll(const sp<EffectModule>& handle); |
| 452 | size_t removeEffect_l(const sp<EffectModule>& handle, bool release = false); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 453 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 454 | audio_session_t sessionId() const { return mSessionId; } |
| 455 | void setSessionId(audio_session_t sessionId) { mSessionId = sessionId; } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 456 | |
| 457 | sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor); |
| 458 | sp<EffectModule> getEffectFromId_l(int id); |
| 459 | sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 460 | std::vector<int> getEffectIds(); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 461 | // FIXME use float to improve the dynamic range |
Eric Laurent | fa1e123 | 2016-08-02 19:01:49 -0700 | [diff] [blame] | 462 | bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false); |
| 463 | void resetVolume_l(); |
jiabin | 8f278ee | 2019-11-11 12:16:27 -0800 | [diff] [blame] | 464 | void setDevices_l(const AudioDeviceTypeAddrVector &devices); |
| 465 | void setInputDevice_l(const AudioDeviceTypeAddr &device); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 466 | void setMode_l(audio_mode_t mode); |
| 467 | void setAudioSource_l(audio_source_t source); |
| 468 | |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 469 | void setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 470 | mInBuffer = buffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 471 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 472 | effect_buffer_t *inBuffer() const { |
| 473 | return mInBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mInBuffer->ptr()) : NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 474 | } |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 475 | void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 476 | mOutBuffer = buffer; |
| 477 | } |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 478 | effect_buffer_t *outBuffer() const { |
| 479 | return mOutBuffer != 0 ? reinterpret_cast<effect_buffer_t*>(mOutBuffer->ptr()) : NULL; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void incTrackCnt() { android_atomic_inc(&mTrackCnt); } |
| 483 | void decTrackCnt() { android_atomic_dec(&mTrackCnt); } |
| 484 | int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); } |
| 485 | |
| 486 | void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); |
| 487 | mTailBufferCount = mMaxTailBuffers; } |
| 488 | void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); } |
| 489 | int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); } |
| 490 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 491 | product_strategy_t strategy() const { return mStrategy; } |
| 492 | void setStrategy(product_strategy_t strategy) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 493 | { mStrategy = strategy; } |
| 494 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 495 | // suspend or restore effects of the specified type. The number of suspend requests is counted |
| 496 | // and restore occurs once all suspend requests are cancelled. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 497 | void setEffectSuspended_l(const effect_uuid_t *type, |
| 498 | bool suspend); |
| 499 | // suspend all eligible effects |
| 500 | void setEffectSuspendedAll_l(bool suspend); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 501 | // check if effects should be suspended or restored when a given effect is enable or disabled |
| 502 | void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, bool enabled); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 503 | |
| 504 | void clearInputBuffer(); |
| 505 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 506 | // At least one non offloadable effect in the chain is enabled |
| 507 | bool isNonOffloadableEnabled(); |
Shingo Kitajima | 1f8df9a | 2018-05-29 11:35:06 +0900 | [diff] [blame] | 508 | bool isNonOffloadableEnabled_l(); |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 509 | |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 510 | void syncHalEffectsState(); |
| 511 | |
Andy Hung | d3bb0ad | 2016-10-11 17:16:43 -0700 | [diff] [blame] | 512 | // flags is an ORed set of audio_output_flags_t which is updated on return. |
| 513 | void checkOutputFlagCompatibility(audio_output_flags_t *flags) const; |
| 514 | |
| 515 | // flags is an ORed set of audio_input_flags_t which is updated on return. |
| 516 | void checkInputFlagCompatibility(audio_input_flags_t *flags) const; |
| 517 | |
| 518 | // Is this EffectChain compatible with the RAW audio flag. |
| 519 | bool isRawCompatible() const; |
| 520 | |
| 521 | // Is this EffectChain compatible with the FAST audio flag. |
| 522 | bool isFastCompatible() const; |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 523 | |
| 524 | // isCompatibleWithThread_l() must be called with thread->mLock held |
| 525 | bool isCompatibleWithThread_l(const sp<ThreadBase>& thread) const; |
| 526 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 527 | bool containsHapticGeneratingEffect_l(); |
| 528 | |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 529 | void setHapticIntensity_l(int id, int intensity); |
| 530 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 531 | sp<EffectCallbackInterface> effectCallback() const { return mEffectCallback; } |
| 532 | wp<ThreadBase> thread() const { return mEffectCallback->thread(); } |
| 533 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 534 | bool isFirstEffect(int id) const { return !mEffects.isEmpty() && id == mEffects[0]->id(); } |
| 535 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 536 | void dump(int fd, const Vector<String16>& args); |
| 537 | |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 538 | private: |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 539 | |
Andy Hung | 328d677 | 2021-01-12 12:32:21 -0800 | [diff] [blame] | 540 | // For transaction consistency, please consider holding the EffectChain lock before |
| 541 | // calling the EffectChain::EffectCallback methods, excepting |
| 542 | // createEffectHal and allocateHalBuffer. |
| 543 | // |
| 544 | // This prevents migration of the EffectChain to another PlaybackThread |
| 545 | // for the purposes of the EffectCallback. |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 546 | class EffectCallback : public EffectCallbackInterface { |
| 547 | public: |
Ytai Ben-Tsvi | 4f04336 | 2020-01-28 12:39:23 -0800 | [diff] [blame] | 548 | // Note: ctors taking a weak pointer to their owner must not promote it |
| 549 | // during construction (but may keep a reference for later promotion). |
| 550 | EffectCallback(const wp<EffectChain>& owner, |
Ytai Ben-Tsvi | 3de1bbf | 2020-01-21 16:41:17 -0800 | [diff] [blame] | 551 | const wp<ThreadBase>& thread) |
Andy Hung | 6626a01 | 2021-01-12 13:38:00 -0800 | [diff] [blame] | 552 | : mChain(owner) |
| 553 | , mThread(thread) |
| 554 | , mAudioFlinger(*gAudioFlinger) { |
Ytai Ben-Tsvi | 3de1bbf | 2020-01-21 16:41:17 -0800 | [diff] [blame] | 555 | } |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 556 | |
| 557 | status_t createEffectHal(const effect_uuid_t *pEffectUuid, |
| 558 | int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override; |
| 559 | status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) override; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 560 | bool updateOrphanEffectChains(const sp<EffectBase>& effect) override; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 561 | |
| 562 | audio_io_handle_t io() const override; |
| 563 | bool isOutput() const override; |
| 564 | bool isOffload() const override; |
| 565 | bool isOffloadOrDirect() const override; |
| 566 | bool isOffloadOrMmap() const override; |
| 567 | |
| 568 | uint32_t sampleRate() const override; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 569 | audio_channel_mask_t inChannelMask(int id) const override; |
| 570 | uint32_t inChannelCount(int id) const override; |
| 571 | audio_channel_mask_t outChannelMask() const override; |
| 572 | uint32_t outChannelCount() const override; |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 573 | audio_channel_mask_t hapticChannelMask() const override; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 574 | size_t frameCount() const override; |
| 575 | uint32_t latency() const override; |
| 576 | |
| 577 | status_t addEffectToHal(sp<EffectHalInterface> effect) override; |
| 578 | status_t removeEffectFromHal(sp<EffectHalInterface> effect) override; |
| 579 | bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override; |
| 580 | void setVolumeForOutput(float left, float right) const override; |
| 581 | |
| 582 | // check if effects should be suspended/restored when a given effect is enable/disabled |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 583 | void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect, |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 584 | bool enabled, bool threadLocked) override; |
| 585 | void resetVolume() override; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 586 | product_strategy_t strategy() const override; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 587 | int32_t activeTrackCnt() const override; |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 588 | void onEffectEnable(const sp<EffectBase>& effect) override; |
| 589 | void onEffectDisable(const sp<EffectBase>& effect) override; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 590 | |
| 591 | wp<EffectChain> chain() const override { return mChain; } |
| 592 | |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 593 | bool isAudioPolicyReady() const override { |
| 594 | return mAudioFlinger.isAudioPolicyReady(); |
| 595 | } |
| 596 | |
Andy Hung | 328d677 | 2021-01-12 12:32:21 -0800 | [diff] [blame] | 597 | wp<ThreadBase> thread() const { return mThread.load(); } |
Ytai Ben-Tsvi | 3de1bbf | 2020-01-21 16:41:17 -0800 | [diff] [blame] | 598 | |
| 599 | void setThread(const wp<ThreadBase>& thread) { |
| 600 | mThread = thread; |
Ytai Ben-Tsvi | 3de1bbf | 2020-01-21 16:41:17 -0800 | [diff] [blame] | 601 | } |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 602 | |
| 603 | private: |
Andy Hung | 318e024 | 2020-12-21 10:33:49 -0800 | [diff] [blame] | 604 | const wp<EffectChain> mChain; |
Andy Hung | 328d677 | 2021-01-12 12:32:21 -0800 | [diff] [blame] | 605 | mediautils::atomic_wp<ThreadBase> mThread; |
Andy Hung | 6626a01 | 2021-01-12 13:38:00 -0800 | [diff] [blame] | 606 | AudioFlinger &mAudioFlinger; // implementation detail: outer instance always exists. |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 607 | }; |
| 608 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 609 | friend class AudioFlinger; // for mThread, mEffects |
Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 610 | DISALLOW_COPY_AND_ASSIGN(EffectChain); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 611 | |
| 612 | class SuspendedEffectDesc : public RefBase { |
| 613 | public: |
| 614 | SuspendedEffectDesc() : mRefCount(0) {} |
| 615 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 616 | int mRefCount; // > 0 when suspended |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 617 | effect_uuid_t mType; |
| 618 | wp<EffectModule> mEffect; |
| 619 | }; |
| 620 | |
| 621 | // get a list of effect modules to suspend when an effect of the type |
| 622 | // passed is enabled. |
| 623 | void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects); |
| 624 | |
| 625 | // get an effect module if it is currently enable |
| 626 | sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type); |
| 627 | // true if the effect whose descriptor is passed can be suspended |
| 628 | // OEMs can modify the rules implemented in this method to exclude specific effect |
| 629 | // types or implementations from the suspend/restore mechanism. |
| 630 | bool isEffectEligibleForSuspend(const effect_descriptor_t& desc); |
| 631 | |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 632 | static bool isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type); |
| 633 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 634 | void clearInputBuffer_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 635 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 636 | void setThread(const sp<ThreadBase>& thread); |
| 637 | |
Zhou Song | d505c64 | 2020-02-20 16:35:37 +0800 | [diff] [blame] | 638 | // true if any effect module within the chain has volume control |
| 639 | bool hasVolumeControlEnabled_l() const; |
| 640 | |
Tomoharu Kasahara | 1990bd4 | 2014-12-12 14:04:11 +0900 | [diff] [blame] | 641 | void setVolumeForOutput_l(uint32_t left, uint32_t right); |
| 642 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 643 | mutable Mutex mLock; // mutex protecting effect list |
| 644 | Vector< sp<EffectModule> > mEffects; // list of effect modules |
| 645 | audio_session_t mSessionId; // audio session ID |
Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 646 | sp<EffectBufferHalInterface> mInBuffer; // chain input buffer |
| 647 | sp<EffectBufferHalInterface> mOutBuffer; // chain output buffer |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 648 | |
| 649 | // 'volatile' here means these are accessed with atomic operations instead of mutex |
| 650 | volatile int32_t mActiveTrackCnt; // number of active tracks connected |
| 651 | volatile int32_t mTrackCnt; // number of tracks connected |
| 652 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 653 | int32_t mTailBufferCount; // current effect tail buffer count |
| 654 | int32_t mMaxTailBuffers; // maximum effect tail buffers |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 655 | int mVolumeCtrlIdx; // index of insert effect having control over volume |
| 656 | uint32_t mLeftVolume; // previous volume on left channel |
| 657 | uint32_t mRightVolume; // previous volume on right channel |
| 658 | uint32_t mNewLeftVolume; // new volume on left channel |
| 659 | uint32_t mNewRightVolume; // new volume on right channel |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 660 | product_strategy_t mStrategy; // strategy for this effect chain |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 661 | // mSuspendedEffects lists all effects currently suspended in the chain. |
| 662 | // Use effect type UUID timelow field as key. There is no real risk of identical |
| 663 | // timeLow fields among effect type UUIDs. |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 664 | // Updated by setEffectSuspended_l() and setEffectSuspendedAll_l() only. |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 665 | KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects; |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 666 | |
| 667 | const sp<EffectCallback> mEffectCallback; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 668 | }; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 669 | |
| 670 | class DeviceEffectProxy : public EffectBase { |
| 671 | public: |
| 672 | DeviceEffectProxy (const AudioDeviceTypeAddr& device, |
| 673 | const sp<DeviceEffectManagerCallback>& callback, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 674 | effect_descriptor_t *desc, int id, bool notifyFramesProcessed) |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 675 | : EffectBase(callback, desc, id, AUDIO_SESSION_DEVICE, false), |
| 676 | mDevice(device), mManagerCallback(callback), |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 677 | mMyCallback(new ProxyCallback(wp<DeviceEffectProxy>(this), callback)), |
| 678 | mNotifyFramesProcessed(notifyFramesProcessed) {} |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 679 | |
| 680 | status_t setEnabled(bool enabled, bool fromHandle) override; |
| 681 | sp<DeviceEffectProxy> asDeviceEffectProxy() override { return this; } |
| 682 | |
| 683 | status_t init(const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches); |
| 684 | status_t onCreatePatch(audio_patch_handle_t patchHandle, const PatchPanel::Patch& patch); |
| 685 | void onReleasePatch(audio_patch_handle_t patchHandle); |
| 686 | |
| 687 | size_t removeEffect(const sp<EffectModule>& effect); |
| 688 | |
| 689 | status_t addEffectToHal(sp<EffectHalInterface> effect); |
| 690 | status_t removeEffectFromHal(sp<EffectHalInterface> effect); |
| 691 | |
| 692 | const AudioDeviceTypeAddr& device() { return mDevice; }; |
| 693 | bool isOutput() const; |
| 694 | uint32_t sampleRate() const; |
| 695 | audio_channel_mask_t channelMask() const; |
| 696 | uint32_t channelCount() const; |
| 697 | |
| 698 | void dump(int fd, int spaces); |
| 699 | |
| 700 | private: |
| 701 | |
| 702 | class ProxyCallback : public EffectCallbackInterface { |
| 703 | public: |
Ytai Ben-Tsvi | 4f04336 | 2020-01-28 12:39:23 -0800 | [diff] [blame] | 704 | // Note: ctors taking a weak pointer to their owner must not promote it |
| 705 | // during construction (but may keep a reference for later promotion). |
| 706 | ProxyCallback(const wp<DeviceEffectProxy>& owner, |
| 707 | const sp<DeviceEffectManagerCallback>& callback) |
| 708 | : mProxy(owner), mManagerCallback(callback) {} |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 709 | |
| 710 | status_t createEffectHal(const effect_uuid_t *pEffectUuid, |
| 711 | int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) override; |
| 712 | status_t allocateHalBuffer(size_t size __unused, |
| 713 | sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; } |
| 714 | bool updateOrphanEffectChains(const sp<EffectBase>& effect __unused) override { |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; } |
| 719 | bool isOutput() const override; |
| 720 | bool isOffload() const override { return false; } |
| 721 | bool isOffloadOrDirect() const override { return false; } |
| 722 | bool isOffloadOrMmap() const override { return false; } |
| 723 | |
| 724 | uint32_t sampleRate() const override; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 725 | audio_channel_mask_t inChannelMask(int id) const override; |
| 726 | uint32_t inChannelCount(int id) const override; |
| 727 | audio_channel_mask_t outChannelMask() const override; |
| 728 | uint32_t outChannelCount() const override; |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 729 | audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 730 | size_t frameCount() const override { return 0; } |
| 731 | uint32_t latency() const override { return 0; } |
| 732 | |
| 733 | status_t addEffectToHal(sp<EffectHalInterface> effect) override; |
| 734 | status_t removeEffectFromHal(sp<EffectHalInterface> effect) override; |
| 735 | |
| 736 | bool disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast) override; |
| 737 | void setVolumeForOutput(float left __unused, float right __unused) const override {} |
| 738 | |
| 739 | void checkSuspendOnEffectEnabled(const sp<EffectBase>& effect __unused, |
| 740 | bool enabled __unused, bool threadLocked __unused) override {} |
| 741 | void resetVolume() override {} |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 742 | product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 743 | int32_t activeTrackCnt() const override { return 0; } |
| 744 | void onEffectEnable(const sp<EffectBase>& effect __unused) override {} |
| 745 | void onEffectDisable(const sp<EffectBase>& effect __unused) override {} |
| 746 | |
| 747 | wp<EffectChain> chain() const override { return nullptr; } |
| 748 | |
Eric Laurent | d66d7a1 | 2021-07-13 13:35:32 +0200 | [diff] [blame] | 749 | bool isAudioPolicyReady() const override { |
| 750 | return mManagerCallback->isAudioPolicyReady(); |
| 751 | } |
| 752 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 753 | int newEffectId(); |
| 754 | |
| 755 | private: |
| 756 | const wp<DeviceEffectProxy> mProxy; |
| 757 | const sp<DeviceEffectManagerCallback> mManagerCallback; |
| 758 | }; |
| 759 | |
| 760 | status_t checkPort(const PatchPanel::Patch& patch, const struct audio_port_config *port, |
| 761 | sp<EffectHandle> *handle); |
| 762 | |
| 763 | const AudioDeviceTypeAddr mDevice; |
| 764 | const sp<DeviceEffectManagerCallback> mManagerCallback; |
| 765 | const sp<ProxyCallback> mMyCallback; |
| 766 | |
| 767 | Mutex mProxyLock; |
| 768 | std::map<audio_patch_handle_t, sp<EffectHandle>> mEffectHandles; // protected by mProxyLock |
| 769 | sp<EffectModule> mHalEffect; // protected by mProxyLock |
| 770 | struct audio_port_config mDevicePort = { .id = AUDIO_PORT_HANDLE_NONE }; |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 771 | const bool mNotifyFramesProcessed; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 772 | }; |