Effects.h revision c56f3426099a3cf2d07ccff8886050c7fbce140f
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project
481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License.
781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at
881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software
1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and
1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License.
1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifndef INCLUDING_FROM_AUDIOFLINGER_H
1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    #error This header file should only be included from AudioFlinger.h
2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
2181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//--- Audio Effect Management
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// EffectModule and EffectChain classes both have their own mutex to protect
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// state changes or resource modifications. Always respect the following order
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// if multiple mutexes must be acquired to avoid cross deadlock:
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioFlinger -> ThreadBase -> EffectChain -> EffectModule
28eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent// In addition, methods that lock the AudioPolicyService mutex (getOutputForEffect(),
29eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent// startOutput()...) should never be called with AudioFlinger or Threadbase mutex locked
30eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent// to avoid cross deadlock with other clients calling AudioPolicyService methods that in turn
31eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent// call AudioFlinger thus locking the same mutexes in the reverse order.
3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffectModule class is a wrapper object controlling the effect engine implementation
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// in the effect library. It prevents concurrent calls to process() and command() functions
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// from different client threads. It keeps a list of EffectHandle objects corresponding
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// to all client applications using this effect and notifies applications of effect state,
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// control or parameter changes. It manages the activation state machine to send appropriate
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// reset, enable, disable commands to effect engine and provide volume
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ramping when effects are activated/deactivated.
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// When controlling an auxiliary effect, the EffectModule also provides an input buffer used by
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// the attached track(s) to accumulate their auxiliary channel.
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass EffectModule : public RefBase {
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectModule(ThreadBase *thread,
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const wp<AudioFlinger::EffectChain>& chain,
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_descriptor_t *desc,
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int id,
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int sessionId);
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~EffectModule();
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum effect_state {
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        IDLE,
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        RESTART,
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STARTING,
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ACTIVE,
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STOPPING,
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        STOPPED,
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        DESTROYED
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int         id() const { return mId; }
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void process();
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void updateState();
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t command(uint32_t cmdCode,
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                     uint32_t cmdSize,
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                     void *pCmdData,
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                     uint32_t *replySize,
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                     void *pReplyData);
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void reset_l();
7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t configure();
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t init();
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_state state() const {
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mState;
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t status() {
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mStatus;
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int sessionId() const {
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mSessionId;
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    setEnabled(bool enabled);
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    setEnabled_l(bool enabled);
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isEnabled() const;
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isProcessEnabled() const;
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        setInBuffer(int16_t *buffer) { mConfig.inputCfg.buffer.s16 = buffer; }
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t     *inBuffer() { return mConfig.inputCfg.buffer.s16; }
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        setOutBuffer(int16_t *buffer) { mConfig.outputCfg.buffer.s16 = buffer; }
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t     *outBuffer() { return mConfig.outputCfg.buffer.s16; }
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        setChain(const wp<EffectChain>& chain) { mChain = chain; }
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        setThread(const wp<ThreadBase>& thread) { mThread = thread; }
9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const wp<ThreadBase>& thread() { return mThread; }
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t addHandle(EffectHandle *handle);
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t disconnect(EffectHandle *handle, bool unpinIfLast);
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t removeHandle(EffectHandle *handle);
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const effect_descriptor_t& desc() const { return mDescriptor; }
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<EffectChain>&     chain() { return mChain; }
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         setDevice(audio_devices_t device);
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         setVolume(uint32_t *left, uint32_t *right, bool controller);
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         setMode(audio_mode_t mode);
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         setAudioSource(audio_source_t source);
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         start();
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t         stop();
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             setSuspended(bool suspended);
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool             suspended() const;
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle*    controlHandle_l();
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool             isPinned() const { return mPinned; }
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             unPin() { mPinned = false; }
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool             purgeHandles();
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             lock() { mLock.lock(); }
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             unlock() { mLock.unlock(); }
1185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool             isOffloadable() const
1195baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                        { return (mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0; }
1205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    status_t         setOffloaded(bool offloaded, audio_io_handle_t io);
1215baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool             isOffloaded() const;
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             dump(int fd, const Vector<String16>& args);
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mHandles
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mPinned;
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Maximum time allocated to effect engines to complete the turn off sequence
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const uint32_t MAX_DISABLE_TIME_MS = 10000;
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectModule(const EffectModule&);
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectModule& operator = (const EffectModule&);
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t start_l();
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t stop_l();
137bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    status_t remove_effect_from_hal_l();
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentmutable Mutex               mLock;      // mutex for process, commands and handles list protection
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<ThreadBase>      mThread;    // parent thread
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<EffectChain>     mChain;     // parent effect chain
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int           mId;        // this instance unique ID
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int           mSessionId; // audio session ID
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_config_t     mConfig;    // input and output audio configuration
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_handle_t  mEffectInterface; // Effect module C API
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t            mStatus;    // initialization status
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_state        mState;     // current activation state
14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector<EffectHandle *> mHandles;    // list of client handles
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // First handle in mHandles has highest priority and controls the effect module
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mMaxDisableWaitCnt;    // maximum grace period before forcing an effect off after
15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // sending disable command.
15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mDisableWaitCnt;       // current process() calls count during disable period.
15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool     mSuspended;            // effect is suspended: temporarily disabled by framework
1555baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool     mOffloaded;            // effect is currently offloaded to the audio DSP
15681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffectHandle class implements the IEffect interface. It provides resources
15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// to receive parameter updates, keeps track of effect control
16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ownership and state and has a pointer to the EffectModule object it is controlling.
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// There is one EffectHandle object for each application controlling (or using)
16281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// an effect module.
16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffectHandle is obtained by calling AudioFlinger::createEffect().
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass EffectHandle: public android::BnEffect {
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle(const sp<EffectModule>& effect,
16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<AudioFlinger::Client>& client,
16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<IEffectClient>& effectClient,
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t priority);
17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~EffectHandle();
172e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten    virtual status_t initCheck();
17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // IEffect
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t enable();
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t disable();
17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t command(uint32_t cmdCode,
17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             uint32_t cmdSize,
17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             void *pCmdData,
18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             uint32_t *replySize,
18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             void *pReplyData);
18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void disconnect();
18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void disconnect(bool unpinIfLast);
18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual sp<IMemory> getCblk() const { return mCblkMemory; }
18781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t onTransact(uint32_t code, const Parcel& data,
18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Parcel* reply, uint32_t flags);
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Give or take control of effect module
19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - hasControl: true if control is given, false if removed
19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - signal: true client app should be signaled of change, false otherwise
19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - enabled: state of the effect when control is passed
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setControl(bool hasControl, bool signal, bool enabled);
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void commandExecuted(uint32_t cmdCode,
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         uint32_t cmdSize,
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         void *pCmdData,
19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         uint32_t replySize,
20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         void *pReplyData);
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEnabled(bool enabled);
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool enabled() const { return mEnabled; }
20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Getters
20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int id() const { return mEffect->id(); }
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int priority() const { return mPriority; }
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool hasControl() const { return mHasControl; }
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect() const { return mEffect; }
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // destroyed_l() must be called with the associated EffectModule mLock held
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool destroyed_l() const { return mDestroyed; }
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten    void dumpToBuffer(char* buffer, size_t size);
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;          // for mEffect, mHasControl, mEnabled
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle(const EffectHandle&);
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle& operator =(const EffectHandle&);
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> mEffect;           // pointer to controlled EffectModule
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IEffectClient> mEffectClient;    // callback interface for client notifications
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    /*const*/ sp<Client> mClient;       // client for shared memory allocation, see disconnect()
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMemory>         mCblkMemory;    // shared memory for control block
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_param_cblk_t* mCblk;         // control block for deferred parameter setting via
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // shared memory
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint8_t*            mBuffer;        // pointer to parameter area in shared memory
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mPriority;                      // client application priority to control the effect
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mHasControl;                   // true if this handle is controlling the effect
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mEnabled;                      // cached enable state: needed when the effect is
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // restored after being suspended
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mDestroyed;                    // Set to true by destructor. Access with EffectModule
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // mLock held
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// the EffectChain class represents a group of effects associated to one audio session.
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffecChain with session ID 0 contains global effects applied to the output mix.
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order corresponding in the effect process order. When attached to a track (session ID != 0),
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// it also provide it's own input buffer used by the track as accumulation buffer.
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass EffectChain : public RefBase {
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(const wp<ThreadBase>& wThread, int sessionId);
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(ThreadBase *thread, int sessionId);
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~EffectChain();
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // special key used for an entry in mSuspendedEffects keyed vector
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // corresponding to a suspend all request.
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int        kKeyForSuspendAll = 0;
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // minimum duration during which we force calling effect process when last track on
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // a session is stopped or removed to allow effect tail to be rendered
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int        kProcessTailDurationMs = 1000;
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void process_l();
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void lock() {
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.lock();
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void unlock() {
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.unlock();
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t addEffect_l(const sp<EffectModule>& handle);
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t removeEffect_l(const sp<EffectModule>& handle);
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int sessionId() const { return mSessionId; }
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setSessionId(int sessionId) { mSessionId = sessionId; }
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromId_l(int id);
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
273c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    // FIXME use float to improve the dynamic range
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool setVolume_l(uint32_t *left, uint32_t *right);
27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setDevice_l(audio_devices_t device);
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setMode_l(audio_mode_t mode);
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setAudioSource_l(audio_source_t source);
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mInBuffer = buffer;
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOwnInBuffer = ownsBuffer;
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *inBuffer() const {
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mInBuffer;
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setOutBuffer(int16_t *buffer) {
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutBuffer = buffer;
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *outBuffer() const {
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mOutBuffer;
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                               mTailBufferCount = mMaxTailBuffers; }
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy() const { return mStrategy; }
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setStrategy(uint32_t strategy)
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            { mStrategy = strategy; }
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend effect of the given type
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEffectSuspended_l(const effect_uuid_t *type,
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              bool suspend);
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend all eligible effects
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEffectSuspendedAll_l(bool suspend);
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check if effects should be suspend or restored when a given effect is enable or disabled
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool enabled);
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearInputBuffer();
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3175baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    // At least one non offloadable effect in the chain is enabled
3185baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool isNonOffloadableEnabled();
319813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
320813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dump(int fd, const Vector<String16>& args);
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;  // for mThread, mEffects
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(const EffectChain&);
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain& operator =(const EffectChain&);
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class SuspendedEffectDesc : public RefBase {
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        SuspendedEffectDesc() : mRefCount(0) {}
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int mRefCount;
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_uuid_t mType;
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<EffectModule> mEffect;
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get a list of effect modules to suspend when an effect of the type
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // passed is enabled.
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void                       getSuspendEligibleEffects(Vector< sp<EffectModule> > &effects);
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get an effect module if it is currently enable
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectIfEnabled(const effect_uuid_t *type);
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // true if the effect whose descriptor is passed can be suspended
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // OEMs can modify the rules implemented in this method to exclude specific effect
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // types or implementations from the suspend/restore mechanism.
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isEffectEligibleForSuspend(const effect_descriptor_t& desc);
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearInputBuffer_l(sp<ThreadBase> thread);
34981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<ThreadBase> mThread;     // parent mixer thread
35181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex mLock;                // mutex protecting effect list
35281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectModule> > mEffects; // list of effect modules
35381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mSessionId;             // audio session ID
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *mInBuffer;         // chain input buffer
35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *mOutBuffer;        // chain output buffer
35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' here means these are accessed with atomic operations instead of mutex
35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t mActiveTrackCnt;    // number of active tracks connected
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t mTrackCnt;          // number of tracks connected
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t mTailBufferCount;   // current effect tail buffer count
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t mMaxTailBuffers;    // maximum effect tail buffers
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mOwnInBuffer;          // true if the chain owns its input buffer
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mVolumeCtrlIdx;         // index of insert effect having control over volume
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mLeftVolume;       // previous volume on left channel
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mRightVolume;      // previous volume on right channel
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mNewLeftVolume;       // new volume on left channel
36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mNewRightVolume;      // new volume on right channel
36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mStrategy; // strategy for this effect chain
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mSuspendedEffects lists all effects currently suspended in the chain.
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Use effect type UUID timelow field as key. There is no real risk of identical
37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // timeLow fields among effect type UUIDs.
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Updated by updateSuspendedSessions_l() only.
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    KeyedVector< int, sp<SuspendedEffectDesc> > mSuspendedEffects;
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
376