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 Laurentclass ThreadBase : public Thread {
2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "TrackBase.h"
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum type_t {
2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER,              // Thread class is MixerThread
2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        DIRECT,             // Thread class is DirectOutputThread
3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        DUPLICATING,        // Thread class is DuplicatingThread
31bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        RECORD,             // Thread class is RecordThread
32bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        OFFLOAD             // Thread class is OffloadThread
3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice, audio_devices_t inDevice, type_t type);
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~ThreadBase();
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpBase(int fd, const Vector<String16>& args);
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpEffectChains(int fd, const Vector<String16>& args);
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearPowerManager();
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // base for record and playback
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_IO,
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_PRIO
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class ConfigEvent {
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ConfigEvent(int type) : mType(type) {}
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                 int type() const { return mType; }
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mType;
6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class IoConfigEvent : public ConfigEvent {
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        IoConfigEvent(int event, int param) :
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ConfigEvent(CFG_EVENT_IO), mEvent(event), mParam(event) {}
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~IoConfigEvent() {}
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int event() const { return mEvent; }
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int param() const { return mParam; }
7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "IO event: event %d, param %d\n", mEvent, mParam);
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mEvent;
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mParam;
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PrioConfigEvent : public ConfigEvent {
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ConfigEvent(CFG_EVENT_PRIO), mPid(pid), mTid(tid), mPrio(prio) {}
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~PrioConfigEvent() {}
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                pid_t pid() const { return mPid; }
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                pid_t tid() const { return mTid; }
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t prio() const { return mPrio; }
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const PMDeathRecipient&);
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient& operator = (const PMDeathRecipient&);
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    channelCount() const { return mChannelCount; }
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_format_t format() const { return mFormat; }
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
1299b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
1309b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
131bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        checkForNewParameters_l() = 0;
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        audioConfigChanged_l(int event, int param = 0) = 0;
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent(int event, int param = 0);
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent_l(int event, int param = 0);
14281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        processConfigEvents();
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
14981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     audio_stream_t* stream() const = 0;
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
15381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
15481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
15581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
15681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int sessionId,
15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
15881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
15981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    status_t *status);
16081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void disconnectEffect(const sp< EffectModule>& effect,
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                      EffectHandle *handle,
16281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                      bool unpinIfLast);
16381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    TRACK_SESSION = 0x2     // the audio session corresponds to at least one
16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain(int sessionId);
17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain_l(int sessionId) const;
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
187bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
188bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
19281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId);
19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect);
20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId) {}
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // returns either EFFECT_SESSION if effects on this audio session exist in one
20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // chain, or TRACK_SESSION if tracks on this audio session exist, or both
20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const = 0;
20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t getStrategyForSession_l(int sessionId) { return 0; }
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // suspend or restore effect according to the type of effect passed. a NULL
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // type pointer means suspend all effects in the session
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended(const effect_uuid_t *type,
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        bool suspend,
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        int sessionId = AUDIO_SESSION_OUTPUT_MIX);
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended/restored when an effect is enabled
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // or disabled
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 bool enabled,
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   bool enabled,
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   int sessionId = AUDIO_SESSION_OUTPUT_MIX);
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable     Mutex                   mLock;
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // entry describing an effect being suspended in mSuspendedSessions keyed vector
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                class SuspendedSessionDesc : public RefBase {
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                public:
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    SuspendedSessionDesc() : mRefCount(0) {}
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int mRefCount;          // number of active suspend requests
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_uuid_t mType;    // effect type UUID
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
240e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock(int uid = -1);
241e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock_l(int uid = -1);
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock();
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock_l();
2449cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen                void        updateWakeLockUids(const SortedVector<int> &uids);
2459cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen                void        updateWakeLockUids_l(const SortedVector<int> &uids);
2469cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen                void        getPowerManager_l();
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          int sessionId);
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // updated mSuspendedSessions when an effect suspended or restored
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      int sessionId);
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
257014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
258014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
2699b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
2709b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // updated by PlaybackThread::readOutputParameters() or
2719b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // RecordThread::readInputParameters()
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
275f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_format_t          mFormat;
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Parameter sequence by client: binder thread calling setParameters():
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  1. Lock mLock
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  2. Append to mNewParameters
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  3. mWaitWorkCV.signal
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  4. mParamCond.waitRelative with timeout
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  5. read mParamStatus
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  6. mWaitWorkCV.signal
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  7. Unlock
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Parameter sequence by server: threadLoop calling checkForNewParameters_l():
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 1. Lock mLock
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 2. If there is an entry in mNewParameters proceed ...
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 2. Read first entry in mNewParameters
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 3. Process
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 4. Remove first entry from mNewParameters
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 5. Set mParamStatus
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 6. mParamCond.signal
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 7. mWaitWorkCV.wait with timeout (this is to avoid overwriting mParamStatus)
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // 8. Unlock
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mParamCond;
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector<String8>         mNewParameters;
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t                mParamStatus;
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
302c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten                // vector owns each ConfigEvent *, so must delete after removing
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector<ConfigEvent *>     mConfigEvents;
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and read by other threads without lock or barrier via standby() , outDevice()
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool                    mStandby;   // Whether thread is currently in standby.
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_source_t          mAudioSource; // (see audio.h, audio_source_t)
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                static const int        kNameLength = 16;   // prctl(PR_SET_NAME) limit
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                char                    mName[kNameLength];
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // list of suspended effects per session and per type. The first vector is
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // keyed by session ID, the second by type UUID timeLow field
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
328ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
3299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass PlaybackThread : public ThreadBase {
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
341bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
342bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
343bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
348bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
349bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
350bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent//FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled
351bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // for offloaded tracks
352bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
353bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type);
35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    readyToRun();
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
382bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
383bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
384bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        writeCallback();
3853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
386bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        drainCallback();
3873b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
388bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
389bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static      int         asyncCallback(stream_callback_event_t event, void *param, void *cookie);
390bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
391bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
392bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
393bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
394bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
40081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
40281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
40481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
40681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
40781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterVolume(float value);
40981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute(bool muted);
41081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamVolume(audio_stream_type_t stream, float value);
41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamMute(audio_stream_type_t stream, bool muted);
41381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float       streamVolume(audio_stream_type_t stream) const;
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
42181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                size_t frameCount,
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
42481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                int sessionId,
42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                IAudioFlinger::track_flags_t *flags,
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
4279cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen                                int uid,
42881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                status_t *status);
42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual audio_stream_t* stream() const;
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
43581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
43981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
44081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
44181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
44281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
44381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
44481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
44581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
44681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
44881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        audioConfigChanged_l(int event, int param = 0);
44981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t    getRenderPosition(size_t *halFrames, size_t *dspFrames);
45081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int16_t     *mixBuffer() const { return mMixBuffer; };
45181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
45381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
45481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
45581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
45681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
45981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const;
46181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t getStrategyForSession_l(int sessionId);
46281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
46581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
466fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
467fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        void     invalidateTracks(audio_stream_type_t streamType);
46981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4709b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
4719b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
4729b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
4739b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
475accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent                status_t         getTimestamp_l(AudioTimestamp& timestamp);
476accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
4789b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    // updated by readOutputParameters()
4799b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
4809b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    int16_t*                        mMixBuffer;         // frame size aligned mix buffer
482bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    int8_t*                         mAllocMixBuffer;    // mixer buffer allocation address
48381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
48481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
48581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
48681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
48781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
48881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
48981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
49181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples
49281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mFramesWritten would be better, or 64-bit even better
49381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t                          mBytesWritten;
49481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
49581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
49681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
49781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
49881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
49981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
50081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
50181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< wp<Track> >       mActiveTracks;  // FIXME check if this could be sp<>
5029cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen    SortedVector<int>               mWakeLockUids;
5039cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen    int                             mActiveTracksGeneration;
504fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    wp<Track>                       mLatestActiveTrack; // latest track added to mActiveTracks
50581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
50681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Allocate a track name for a given channel mask.
50781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //   Returns name >= 0 if successful, -1 on failure.
50881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual int             getTrackName_l(audio_channel_mask_t channelMask, int sessionId) = 0;
50981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void            deleteTrackName_l(int name) = 0;
51081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
51281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
51381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
51481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
51581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
51681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
51781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
51981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
52081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
52281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
52381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
52481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
52681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
52781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
52981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
53181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const Client&);
53581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread& operator = (const PlaybackThread&);
53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
540ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    void        broadcast_l();
54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        readOutputParameters();
54381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
54581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< sp<Track> >       mTracks;
54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by
54981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DuplicatingThread
55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT + 1];
55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
55481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
55681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
55781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
55881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
55981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
56081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyTime;
56181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t                          mixBufferSize;
56281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
56481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        activeSleepTime;
56581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        idleSleepTime;
56681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTime;
56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
58081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyDelay;
58181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
58481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
58781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
589bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
590bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
5913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
5923b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
5933b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
5943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
5953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
5963b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
5973b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
5983b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
5993b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
6003b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
6013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
6023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
6033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
604ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait
605ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // for async write callback in the thread loop before evaluating it
606bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mSignalPending;
607bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
608bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
61381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
61646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
62046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
62181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
622ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten    static const size_t     kFastMixerLogSize = 4 * 1024;
6239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
62481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
63081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
63181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
632bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        flushOutput_l();
633bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten
634bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kastenprivate:
635bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    // timestamp latch:
636bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  D input is written by threadLoop_write while mutex is unlocked, and read while locked
637bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  Q output is written while locked, and read while locked
638bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    struct {
639bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        AudioTimestamp  mTimestamp;
640bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        uint32_t        mUnpresentedFrames;
641bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    } mLatchD, mLatchQ;
642bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchDValid;  // true means mLatchD is valid, and clock it into latch at next opportunity
643bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchQValid;  // true means mLatchQ is valid
64481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
64581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
64881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
65081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
65181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
65381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
65681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        checkForNewParameters_l();
65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
65981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
66181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
66381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
66681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixer*  mFastMixer;         // non-NULL if there is also a fast mixer
68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return mFastMixer != NULL; }
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
70081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
70181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
70681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                       audio_io_handle_t id, audio_devices_t device);
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
71081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        checkForNewParameters_l();
71281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
71881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
71981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
72281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
72381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
72481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
72581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volumes last sent to audio HAL with stream->set_volume()
72781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mLeftVolFloat;
72881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mRightVolFloat;
72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
730bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
731bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type);
732bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    void processVolume_l(Track *track, bool lastTrack);
733bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
740bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
741bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
742bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
743bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
744bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device);
7456a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
746bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
747bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
748bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
749bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
750bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
751bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        flushOutput_l();
752bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
753bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
754bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
755bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
756bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
757bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        flushHw_l();
759bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
760bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
761bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        mHwPaused;
762bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        mFlushPending;
763bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
764bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
765d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent    wp<Track>   mPreviousTrack;         // used to detect track switch
766bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
767bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
768bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
769bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
770bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
7714de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
772bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
773bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
774bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
775bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
776bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
778bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
781bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
7823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
7833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
7843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
7853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
786bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
7884de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
7893b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
7903b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
7913b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
7924de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
7933b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
7943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
7953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
7964de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
7974de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
7984de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
799bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
800bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                      audio_io_handle_t id);
80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
820bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass RecordThread : public ThreadBase, public AudioBufferProvider
84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // derives from AudioBufferProvider interface for use by resampler
84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
84481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
853d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
85446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    audio_devices_t inDevice
85546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
85646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
85746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
85846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    readyToRun();
87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    size_t frameCount,
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int sessionId,
8839cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen                    int uid,
884ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten                    IAudioFlinger::track_flags_t *flags,
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    status_t *status);
88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              int triggerSession);
89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
894a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual audio_stream_t* stream() const;
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // AudioBufferProvider interface
90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        checkForNewParameters_l();
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        audioConfigChanged_l(int event, int param = 0);
90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        readInputParameters();
90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual unsigned int  getInputFramesLost();
90981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
91181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
91281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t hasAudioSession(int sessionId) const;
91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
91781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            KeyedVector<int, bool> sessionIds() const;
91881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent           void handleSyncStartEvent(const sp<SyncEvent>& event);
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9259b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
92690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            bool        hasFastRecorder() const { return false; }
9279b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void clearSyncStartEvent();
93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void standby();
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void inputStandBy();
93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // mActiveTrack has dual roles:  it indicates the current active track, and
94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
94181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<RecordTrack>                     mActiveTrack;
94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
9439b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
9449b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten            // updated by RecordThread::readInputParameters()
94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioResampler                      *mResampler;
94634af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten            // interleaved stereo pairs of fixed-point signed Q19.12
94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t                             *mRsmpOutBuffer;
94834af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten            int16_t                             *mRsmpInBuffer; // [mFrameCount * mChannelCount]
94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t                              mRsmpInIndex;
950548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten            size_t                              mBufferSize;    // stream buffer size for read()
95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const uint32_t                      mReqChannelCount;
95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const uint32_t                      mReqSampleRate;
95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t                             mBytesRead;
95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // sync event triggering actual audio capture. Frames read before this event will
95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // be dropped and therefore not read by the application.
95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<SyncEvent>                       mSyncStartEvent;
95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // number of captured frames to drop after the start sync event has been received.
95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // when < 0, maximum frames to drop before starting capture even if sync event is
95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // not received
96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ssize_t                             mFramestoDrop;
96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
965