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