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;
1221b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    void             addEffectToHal_l();
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void             dump(int fd, const Vector<String16>& args);
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mHandles
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                mPinned;
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Maximum time allocated to effect engines to complete the turn off sequence
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const uint32_t MAX_DISABLE_TIME_MS = 10000;
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectModule(const EffectModule&);
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectModule& operator = (const EffectModule&);
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t start_l();
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t stop_l();
138bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    status_t remove_effect_from_hal_l();
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentmutable Mutex               mLock;      // mutex for process, commands and handles list protection
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<ThreadBase>      mThread;    // parent thread
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    wp<EffectChain>     mChain;     // parent effect chain
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int           mId;        // this instance unique ID
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const int           mSessionId; // audio session ID
14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const effect_descriptor_t mDescriptor;// effect descriptor received from effect engine
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_config_t     mConfig;    // input and output audio configuration
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_handle_t  mEffectInterface; // Effect module C API
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t            mStatus;    // initialization status
14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_state        mState;     // current activation state
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector<EffectHandle *> mHandles;    // list of client handles
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // First handle in mHandles has highest priority and controls the effect module
15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mMaxDisableWaitCnt;    // maximum grace period before forcing an effect off after
15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // sending disable command.
15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t mDisableWaitCnt;       // current process() calls count during disable period.
15581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool     mSuspended;            // effect is suspended: temporarily disabled by framework
1565baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool     mOffloaded;            // effect is currently offloaded to the audio DSP
157aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    wp<AudioFlinger>    mAudioFlinger;
15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffectHandle class implements the IEffect interface. It provides resources
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// to receive parameter updates, keeps track of effect control
16281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ownership and state and has a pointer to the EffectModule object it is controlling.
16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// There is one EffectHandle object for each application controlling (or using)
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// an effect module.
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffectHandle is obtained by calling AudioFlinger::createEffect().
16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass EffectHandle: public android::BnEffect {
16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle(const sp<EffectModule>& effect,
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<AudioFlinger::Client>& client,
17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<IEffectClient>& effectClient,
17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t priority);
17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~EffectHandle();
174e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten    virtual status_t initCheck();
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // IEffect
17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t enable();
17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t disable();
17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t command(uint32_t cmdCode,
18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             uint32_t cmdSize,
18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             void *pCmdData,
18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             uint32_t *replySize,
18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                             void *pReplyData);
18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void disconnect();
18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void disconnect(bool unpinIfLast);
18781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual sp<IMemory> getCblk() const { return mCblkMemory; }
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t onTransact(uint32_t code, const Parcel& data,
19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Parcel* reply, uint32_t flags);
19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Give or take control of effect module
19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - hasControl: true if control is given, false if removed
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - signal: true client app should be signaled of change, false otherwise
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // - enabled: state of the effect when control is passed
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setControl(bool hasControl, bool signal, bool enabled);
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void commandExecuted(uint32_t cmdCode,
19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         uint32_t cmdSize,
20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         void *pCmdData,
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         uint32_t replySize,
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                         void *pReplyData);
20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEnabled(bool enabled);
20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool enabled() const { return mEnabled; }
20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Getters
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int id() const { return mEffect->id(); }
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int priority() const { return mPriority; }
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool hasControl() const { return mHasControl; }
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect() const { return mEffect; }
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // destroyed_l() must be called with the associated EffectModule mLock held
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool destroyed_l() const { return mDestroyed; }
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten    void dumpToBuffer(char* buffer, size_t size);
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;          // for mEffect, mHasControl, mEnabled
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle(const EffectHandle&);
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectHandle& operator =(const EffectHandle&);
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> mEffect;           // pointer to controlled EffectModule
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IEffectClient> mEffectClient;    // callback interface for client notifications
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    /*const*/ sp<Client> mClient;       // client for shared memory allocation, see disconnect()
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMemory>         mCblkMemory;    // shared memory for control block
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_param_cblk_t* mCblk;         // control block for deferred parameter setting via
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // shared memory
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint8_t*            mBuffer;        // pointer to parameter area in shared memory
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mPriority;                      // client application priority to control the effect
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mHasControl;                   // true if this handle is controlling the effect
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mEnabled;                      // cached enable state: needed when the effect is
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // restored after being suspended
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool mDestroyed;                    // Set to true by destructor. Access with EffectModule
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        // mLock held
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// the EffectChain class represents a group of effects associated to one audio session.
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// There can be any number of EffectChain objects per output mixer thread (PlaybackThread).
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// The EffecChain with session ID 0 contains global effects applied to the output mix.
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Effects in this chain can be insert or auxiliary. Effects in other chains (attached to
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// tracks) are insert only. The EffectChain maintains an ordered list of effect module, the
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order corresponding in the effect process order. When attached to a track (session ID != 0),
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// it also provide it's own input buffer used by the track as accumulation buffer.
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass EffectChain : public RefBase {
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(const wp<ThreadBase>& wThread, int sessionId);
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(ThreadBase *thread, int sessionId);
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual ~EffectChain();
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // special key used for an entry in mSuspendedEffects keyed vector
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // corresponding to a suspend all request.
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int        kKeyForSuspendAll = 0;
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // minimum duration during which we force calling effect process when last track on
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // a session is stopped or removed to allow effect tail to be rendered
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static const int        kProcessTailDurationMs = 1000;
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void process_l();
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void lock() {
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.lock();
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void unlock() {
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.unlock();
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t addEffect_l(const sp<EffectModule>& handle);
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t removeEffect_l(const sp<EffectModule>& handle);
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int sessionId() const { return mSessionId; }
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setSessionId(int sessionId) { mSessionId = sessionId; }
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor);
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromId_l(int id);
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> getEffectFromType_l(const effect_uuid_t *type);
275c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten    // FIXME use float to improve the dynamic range
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool setVolume_l(uint32_t *left, uint32_t *right);
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setDevice_l(audio_devices_t device);
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setMode_l(audio_mode_t mode);
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setAudioSource_l(audio_source_t source);
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setInBuffer(int16_t *buffer, bool ownsBuffer = false) {
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mInBuffer = buffer;
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOwnInBuffer = ownsBuffer;
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *inBuffer() const {
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mInBuffer;
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setOutBuffer(int16_t *buffer) {
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutBuffer = buffer;
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int16_t *outBuffer() const {
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return mOutBuffer;
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void incTrackCnt() { android_atomic_inc(&mTrackCnt); }
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t trackCnt() const { return android_atomic_acquire_load(&mTrackCnt); }
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                               mTailBufferCount = mMaxTailBuffers; }
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int32_t activeTrackCnt() const { return android_atomic_acquire_load(&mActiveTrackCnt); }
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t strategy() const { return mStrategy; }
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setStrategy(uint32_t strategy)
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            { mStrategy = strategy; }
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend effect of the given type
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEffectSuspended_l(const effect_uuid_t *type,
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              bool suspend);
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend all eligible effects
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void setEffectSuspendedAll_l(bool suspend);
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check if effects should be suspend or restored when a given effect is enable or disabled
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool enabled);
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearInputBuffer();
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3195baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    // At least one non offloadable effect in the chain is enabled
3205baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    bool isNonOffloadableEnabled();
321813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
322cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    // use release_cas because we don't care about the observed value, just want to make sure the
323cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    // new value is observable.
324cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    void forceVolume() { android_atomic_release_cas(false, true, &mForceVolume); }
325cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    // use acquire_cas because we are interested in the observed value and
326cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    // we are the only observers.
327cb4b6e9f05d1c0b3fcc5be5ebe3f7d6f32669b1aEric Laurent    bool isVolumeForced() { return (android_atomic_acquire_cas(true, false, &mForceVolume) == 0); }
328813e2a74853bde19e37d878c596a044b3f299efcEric Laurent
3291b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    void syncHalEffectsState();
3301b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dump(int fd, const Vector<String16>& args);
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;  // for mThread, mEffects
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain(const EffectChain&);
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    EffectChain& operator =(const EffectChain&);
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class SuspendedEffectDesc : public RefBase {
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        SuspendedEffectDesc() : mRefCount(0) {}
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
342