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