Effects.h revision cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1a
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 28// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(), 29// startOutput()...) should never be called with AudioFlinger or Threadbase mutex locked 30// to avoid cross deadlock with other clients calling AudioPolicyService methods that in turn 31// call AudioFlinger thus locking the same mutexes in the reverse order. 32 33// The EffectModule class is a wrapper object controlling the effect engine implementation 34// in the effect library. It prevents concurrent calls to process() and command() functions 35// from different client threads. It keeps a list of EffectHandle objects corresponding 36// to all client applications using this effect and notifies applications of effect state, 37// control or parameter changes. It manages the activation state machine to send appropriate 38// reset, enable, disable commands to effect engine and provide volume 39// ramping when effects are activated/deactivated. 40// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by 41// the attached track(s) to accumulate their auxiliary channel. 42class EffectModule : public RefBase { 43public: 44 EffectModule(ThreadBase *thread, 45 const wp<AudioFlinger::EffectChain>& chain, 46 effect_descriptor_t *desc, 47 int id, 48 int sessionId); 49 virtual ~EffectModule(); 50 51 enum effect_state { 52 IDLE, 53 RESTART, 54 STARTING, 55 ACTIVE, 56 STOPPING, 57 STOPPED, 58 DESTROYED 59 }; 60 61 int id() const { return mId; } 62 void process(); 63 void updateState(); 64 status_t command(uint32_t cmdCode, 65 uint32_t cmdSize, 66 void *pCmdData, 67 uint32_t *replySize, 68 void *pReplyData); 69 70 void reset_l(); 71 status_t configure(); 72 status_t init(); 73 effect_state state() const { 74 return mState; 75 } 76 uint32_t status() { 77 return mStatus; 78 } 79 int sessionId() const { 80 return mSessionId; 81 } 82 status_t setEnabled(bool enabled); 83 status_t setEnabled_l(bool enabled); 84 bool isEnabled() const; 85 bool isProcessEnabled() const; 86 87 void setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; } 88 int16_t *inBuffer() { return mConfig.inputCfg.buffer.s16; } 89 void setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; } 90 int16_t *outBuffer() { return mConfig.outputCfg.buffer.s16; } 91 void setChain(const wp<EffectChain>& chain) { mChain = chain; } 92 void setThread(const wp<ThreadBase>& thread) { mThread = thread; } 93 const wp<ThreadBase>& thread() { return mThread; } 94 95 status_t addHandle(EffectHandle *handle); 96 size_t disconnect(EffectHandle *handle, bool unpinIfLast); 97 size_t removeHandle(EffectHandle *handle); 98 99 const effect_descriptor_t& desc() const { return mDescriptor; } 100 wp<EffectChain>& chain() { return mChain; } 101 102 status_t setDevice(audio_devices_t device); 103 status_t setVolume(uint32_t *left, uint32_t *right, bool controller); 104 status_t setMode(audio_mode_t mode); 105 status_t setAudioSource(audio_source_t source); 106 status_t start(); 107 status_t stop(); 108 void setSuspended(bool suspended); 109 bool suspended() const; 110 111 EffectHandle* controlHandle_l(); 112 113 bool isPinned() const { return mPinned; } 114 void unPin() { mPinned = false; } 115 bool purgeHandles(); 116 void lock() { mLock.lock(); } 117 void unlock() { mLock.unlock(); } 118 bool isOffloadable() const 119 { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; } 120 status_t setOffloaded(bool offloaded, audio_io_handle_t io); 121 bool isOffloaded() const; 122 123 void dump(int fd, const Vector<String16>& args); 124 125protected: 126 friend class AudioFlinger; // for mHandles 127 bool mPinned; 128 129 // Maximum time allocated to effect engines to complete the turn off sequence 130 static const uint32_t MAX_DISABLE_TIME_MS = 10000; 131 132 EffectModule(const EffectModule&); 133 EffectModule& operator = (const EffectModule&); 134 135 status_t start_l(); 136 status_t stop_l(); 137 status_t remove_effect_from_hal_l(); 138 139mutable Mutex mLock; // mutex for process, commands and handles list protection 140 wp<ThreadBase> mThread; // parent thread 141 wp<EffectChain> mChain; // parent effect chain 142 const int mId; // this instance unique ID 143 const int mSessionId; // audio session ID 144 const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine 145 effect_config_t mConfig; // input and output audio configuration 146 effect_handle_t mEffectInterface; // Effect module C API 147 status_t mStatus; // initialization status 148 effect_state mState; // current activation state 149 Vector<EffectHandle *> mHandles; // list of client handles 150 // First handle in mHandles has highest priority and controls the effect module 151 uint32_t mMaxDisableWaitCnt; // maximum grace period before forcing an effect off after 152 // sending disable command. 153 uint32_t mDisableWaitCnt; // current process() calls count during disable period. 154 bool mSuspended; // effect is suspended: temporarily disabled by framework 155 bool mOffloaded; // effect is currently offloaded to the audio DSP 156 wp<AudioFlinger> mAudioFlinger; 157}; 158 159// The EffectHandle class implements the IEffect interface. It provides resources 160// to receive parameter updates, keeps track of effect control 161// ownership and state and has a pointer to the EffectModule object it is controlling. 162// There is one EffectHandle object for each application controlling (or using) 163// an effect module. 164// The EffectHandle is obtained by calling AudioFlinger::createEffect(). 165class EffectHandle: public android::BnEffect { 166public: 167 168 EffectHandle(const sp<EffectModule>& effect, 169 const sp<AudioFlinger::Client>& client, 170 const sp<IEffectClient>& effectClient, 171 int32_t priority); 172 virtual ~EffectHandle(); 173 virtual status_t initCheck(); 174 175 // IEffect 176 virtual status_t enable(); 177 virtual status_t disable(); 178 virtual status_t command(uint32_t cmdCode, 179 uint32_t cmdSize, 180 void *pCmdData, 181 uint32_t *replySize, 182 void *pReplyData); 183 virtual void disconnect(); 184private: 185 void disconnect(bool unpinIfLast); 186public: 187 virtual sp<IMemory> getCblk() const { return mCblkMemory; } 188 virtual status_t onTransact(uint32_t code, const Parcel& data, 189 Parcel* reply, uint32_t flags); 190 191 192 // Give or take control of effect module 193 // - hasControl: true if control is given, false if removed 194 // - signal: true client app should be signaled of change, false otherwise 195 // - enabled: state of the effect when control is passed 196 void setControl(bool hasControl, bool signal, bool enabled); 197 void commandExecuted(uint32_t cmdCode, 198 uint32_t cmdSize, 199 void *pCmdData, 200 uint32_t replySize, 201 void *pReplyData); 202 void setEnabled(bool enabled); 203 bool enabled() const { return mEnabled; } 204 205 // Getters 206 int id() const { return mEffect->id(); } 207 int priority() const { return mPriority; } 208 bool hasControl() const { return mHasControl; } 209 sp<EffectModule> effect() const { return mEffect; } 210 // destroyed_l() must be called with the associated EffectModule mLock held 211 bool destroyed_l() const { return mDestroyed; } 212 213 void dumpToBuffer(char* buffer, size_t size); 214 215protected: 216 friend class AudioFlinger; // for mEffect, mHasControl, mEnabled 217 EffectHandle(const EffectHandle&); 218 EffectHandle& operator =(const EffectHandle&); 219 220 sp<EffectModule> mEffect; // pointer to controlled EffectModule 221 sp<IEffectClient> mEffectClient; // callback interface for client notifications 222 /*const*/ sp<Client> mClient; // client for shared memory allocation, see disconnect() 223 sp<IMemory> mCblkMemory; // shared memory for control block 224 effect_param_cblk_t* mCblk; // control block for deferred parameter setting via 225 // shared memory 226 uint8_t* mBuffer; // pointer to parameter area in shared memory 227 int mPriority; // client application priority to control the effect 228 bool mHasControl; // true if this handle is controlling the effect 229 bool mEnabled; // cached enable state: needed when the effect is 230 // restored after being suspended 231 bool mDestroyed; // Set to true by destructor. Access with EffectModule 232 // mLock held 233}; 234 235// the EffectChain class represents a group of effects associated to one audio session. 236// There can be any number of EffectChain objects per output mixer thread (PlaybackThread). 237// The EffecChain with session ID 0 contains global effects applied to the output mix. 238// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to 239// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the 240// order corresponding in the effect process order. When attached to a track (session ID != 0), 241// it also provide it's own input buffer used by the track as accumulation buffer. 242class EffectChain : public RefBase { 243public: 244 EffectChain(const wp<ThreadBase>& wThread, int sessionId); 245 EffectChain(ThreadBase *thread, int sessionId); 246 virtual ~EffectChain(); 247 248 // special key used for an entry in mSuspendedEffects keyed vector 249 // corresponding to a suspend all request. 250 static const int kKeyForSuspendAll = 0; 251 252 // minimum duration during which we force calling effect process when last track on 253 // a session is stopped or removed to allow effect tail to be rendered 254 static const int kProcessTailDurationMs = 1000; 255 256 void process_l(); 257 258 void lock() { 259 mLock.lock(); 260 } 261 void unlock() { 262 mLock.unlock(); 263 } 264 265 status_t addEffect_l(const sp<EffectModule>& handle); 266 size_t removeEffect_l(const sp<EffectModule>& handle); 267 268 int sessionId() const { return mSessionId; } 269 void setSessionId(int sessionId) { mSessionId = sessionId; } 270 271 sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor); 272 sp<EffectModule> getEffectFromId_l(int id); 273 sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type); 274 // FIXME use float to improve the dynamic range 275 bool setVolume_l(uint32_t *left, uint32_t *right); 276 void setDevice_l(audio_devices_t device); 277 void setMode_l(audio_mode_t mode); 278 void setAudioSource_l(audio_source_t source); 279 280 void setInBuffer(int16_t *buffer, bool ownsBuffer = false) { 281 mInBuffer = buffer; 282 mOwnInBuffer = ownsBuffer; 283 } 284 int16_t *inBuffer() const { 285 return mInBuffer; 286 } 287 void setOutBuffer(int16_t *buffer) { 288 mOutBuffer = buffer; 289 } 290 int16_t *outBuffer() const { 291 return mOutBuffer; 292 } 293 294 void incTrackCnt() { android_atomic_inc(&mTrackCnt); } 295 void decTrackCnt() { android_atomic_dec(&mTrackCnt); } 296 int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); } 297 298 void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); 299 mTailBufferCount = mMaxTailBuffers; } 300 void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); } 301 int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); } 302 303 uint32_t strategy() const { return mStrategy; } 304 void setStrategy(uint32_t strategy) 305 { mStrategy = strategy; } 306 307 // suspend effect of the given type 308 void setEffectSuspended_l(const effect_uuid_t *type, 309 bool suspend); 310 // suspend all eligible effects 311 void setEffectSuspendedAll_l(bool suspend); 312 // check if effects should be suspend or restored when a given effect is enable or disabled 313 void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 314 bool enabled); 315 316 void clearInputBuffer(); 317 318 // At least one non offloadable effect in the chain is enabled 319 bool isNonOffloadableEnabled(); 320 321 // use release_cas because we don't care about the observed value, just want to make sure the 322 // new value is observable. 323 void forceVolume() { android_atomic_release_cas(false, true, &mForceVolume); } 324 // use acquire_cas because we are interested in the observed value and 325 // we are the only observers. 326 bool isVolumeForced() { return (android_atomic_acquire_cas(true, false, &mForceVolume) == 0); } 327 328 void dump(int fd, const Vector<String16>& args); 329 330protected: 331 friend class AudioFlinger; // for mThread, mEffects 332 EffectChain(const EffectChain&); 333 EffectChain& operator =(const EffectChain&); 334 335 class SuspendedEffectDesc : public RefBase { 336 public: 337 SuspendedEffectDesc() : mRefCount(0) {} 338 339 int mRefCount; 340 effect_uuid_t mType; 341 wp<EffectModule> mEffect; 342 }; 343 344 // get a list of effect modules to suspend when an effect of the type 345 // passed is enabled. 346 void getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects); 347 348 // get an effect module if it is currently enable 349 sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type); 350 // true if the effect whose descriptor is passed can be suspended 351 // OEMs can modify the rules implemented in this method to exclude specific effect 352 // types or implementations from the suspend/restore mechanism. 353 bool isEffectEligibleForSuspend(const effect_descriptor_t& desc); 354 355 void clearInputBuffer_l(sp<ThreadBase> thread); 356 357 void setThread(const sp<ThreadBase>& thread); 358 359 wp<ThreadBase> mThread; // parent mixer thread 360 Mutex mLock; // mutex protecting effect list 361 Vector< sp<EffectModule> > mEffects; // list of effect modules 362 int mSessionId; // audio session ID 363 int16_t *mInBuffer; // chain input buffer 364 int16_t *mOutBuffer; // chain output buffer 365 366 // 'volatile' here means these are accessed with atomic operations instead of mutex 367 volatile int32_t mActiveTrackCnt; // number of active tracks connected 368 volatile int32_t mTrackCnt; // number of tracks connected 369 370 int32_t mTailBufferCount; // current effect tail buffer count 371 int32_t mMaxTailBuffers; // maximum effect tail buffers 372 bool mOwnInBuffer; // true if the chain owns its input buffer 373 int mVolumeCtrlIdx; // index of insert effect having control over volume 374 uint32_t mLeftVolume; // previous volume on left channel 375 uint32_t mRightVolume; // previous volume on right channel 376 uint32_t mNewLeftVolume; // new volume on left channel 377 uint32_t mNewRightVolume; // new volume on right channel 378 uint32_t mStrategy; // strategy for this effect chain 379 // mSuspendedEffects lists all effects currently suspended in the chain. 380 // Use effect type UUID timelow field as key. There is no real risk of identical 381 // timeLow fields among effect type UUIDs. 382 // Updated by updateSuspendedSessions_l() only. 383 KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects; 384 volatile int32_t mForceVolume; // force next volume command because a new effect was enabled 385}; 386