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