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