Threads.h revision 1035194cee4fbd57e35ea15c56e66cd09b63d56e
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
39cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    virtual status_t    readyToRun();
40cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpBase(int fd, const Vector<String16>& args);
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpEffectChains(int fd, const Vector<String16>& args);
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearPowerManager();
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // base for record and playback
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_IO,
491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_PRIO,
501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_SET_PARAMETER,
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEventData: public RefBase {
541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~ConfigEventData() {}
561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEventData() {}
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  2. Lock mLock
651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  4. sendConfigEvent_l() reads status from event->mStatus;
671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  5. sendConfigEvent_l() returns status
681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  6. Unlock
691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //
701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Parameter sequence by server: threadLoop calling processConfigEvents_l():
711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 1. Lock mLock
721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 2. If there is an entry in mConfigEvents proceed ...
731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 3. Read first entry in mConfigEvents
741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 4. Remove first entry from mConfigEvents
751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 5. Process
761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 6. Set event->mStatus
771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 7. event->mCond.signal
781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 8. Unlock
791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEvent: public RefBase {
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const int mType; // event type e.g. CFG_EVENT_IO
871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex mLock;     // mutex associated with mCond
881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Condition mCond; // condition for status return
891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status_t mStatus; // status communicated to sender
901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        bool mWaitStatus; // true if sender is waiting for status
911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEventData> mData;     // event specific parameter data
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEvent(int type) : mType(type), mStatus(NO_ERROR), mWaitStatus(false), mData(NULL) {}
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEventData : public ConfigEventData {
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        IoConfigEventData(int event, int param) :
1001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEvent(event), mParam(param) {}
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "IO event: event %d, param %d\n", mEvent, mParam);
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mEvent;
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mParam;
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEvent : public ConfigEvent {
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        IoConfigEvent(int event, int param) :
1131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_IO) {
1141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new IoConfigEventData(event, param);
1151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~IoConfigEvent() {}
1171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEventData : public ConfigEventData {
1201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) :
1221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mPid(pid), mTid(tid), mPrio(prio) {}
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEvent : public ConfigEvent {
1341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
1361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_PRIO) {
1371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new PrioConfigEventData(pid, tid, prio);
1381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~PrioConfigEvent() {}
1401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEventData : public ConfigEventData {
1431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEventData(String8 keyValuePairs) :
1451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mKeyValuePairs(keyValuePairs) {}
1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) {
1481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
1491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const String8 mKeyValuePairs;
1521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEvent : public ConfigEvent {
1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEvent(String8 keyValuePairs) :
1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_SET_PARAMETER) {
1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new SetParameterConfigEventData(keyValuePairs);
1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mWaitStatus = true;
1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~SetParameterConfigEvent() {}
1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
16481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
16681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
16781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
16881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
16981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
17181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
17281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
17481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const PMDeathRecipient&);
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient& operator = (const PMDeathRecipient&);
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
17781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
17881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
17981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
18081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
18181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
18281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
18381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
18481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
18581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
18681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
18781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
18881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    channelCount() const { return mChannelCount; }
18981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
19081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_format_t format() const { return mFormat; }
19181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
1929b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
1939b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
194bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
1991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                    status_t& status) = 0;
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
2031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        audioConfigChanged_l(
2041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                      const DefaultKeyedVector< pid_t,sp<NotificationClient> >& notificationClients,
2051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                      int event,
2061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                      int param = 0) = 0;
2071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // sendConfigEvent_l() must be called with ThreadBase::mLock held
2081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // Can temporarily release the lock if waiting for a reply from
2091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // processConfigEvents_l().
2101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent(int event, int param = 0);
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent_l(int event, int param = 0);
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
2141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
2151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                void        processConfigEvents_l(
2161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    const DefaultKeyedVector< pid_t,sp<NotificationClient> >& notificationClients);
2171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        cacheParameters_l() = 0;
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     audio_stream_t* stream() const = 0;
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int sessionId,
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
2339156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                    status_t *status /*non-NULL*/);
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void disconnectEffect(const sp< EffectModule>& effect,
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                      EffectHandle *handle,
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                      bool unpinIfLast);
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    TRACK_SESSION = 0x2     // the audio session corresponds to at least one
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain(int sessionId);
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain_l(int sessionId) const;
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
261bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
262bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId);
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect);
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
2750f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     void detachAuxEffect_l(int effectId __unused) {}
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // returns either EFFECT_SESSION if effects on this audio session exist in one
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // chain, or TRACK_SESSION if tracks on this audio session exist, or both
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const = 0;
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
2810f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                virtual uint32_t getStrategyForSession_l(int sessionId __unused) { return 0; }
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // suspend or restore effect according to the type of effect passed. a NULL
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // type pointer means suspend all effects in the session
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended(const effect_uuid_t *type,
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        bool suspend,
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        int sessionId = AUDIO_SESSION_OUTPUT_MIX);
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended/restored when an effect is enabled
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // or disabled
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 bool enabled,
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   bool enabled,
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   int sessionId = AUDIO_SESSION_OUTPUT_MIX);
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
300b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // Return a reference to a per-thread heap which can be used to allocate IMemory
301b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // objects that will be read-only to client processes, read/write to mediaserver,
302b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // and shared by all client processes of the thread.
303b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // The heap is per-thread rather than common across all threads, because
304b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // clients can't be trusted not to modify the offset of the IMemory they receive.
305b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // If a thread does not have such a heap, this method returns 0.
306b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                virtual sp<MemoryDealer>    readOnlyHeap() const { return 0; }
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable     Mutex                   mLock;
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // entry describing an effect being suspended in mSuspendedSessions keyed vector
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                class SuspendedSessionDesc : public RefBase {
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                public:
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    SuspendedSessionDesc() : mRefCount(0) {}
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int mRefCount;          // number of active suspend requests
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_uuid_t mType;    // effect type UUID
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
321e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock(int uid = -1);
322e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock_l(int uid = -1);
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock();
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock_l();
325462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids(const SortedVector<int> &uids);
326462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids_l(const SortedVector<int> &uids);
327462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        getPowerManager_l();
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          int sessionId);
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // updated mSuspendedSessions when an effect suspended or restored
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      int sessionId);
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
338014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
339014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
3509b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
351deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // updated by PlaybackThread::readOutputParameters_l() or
352deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // RecordThread::readInputParameters_l()
35381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
356f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_format_t          mFormat;
35970949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten                size_t                  mBufferSize;       // HAL buffer size for read() or write()
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                Vector< sp<ConfigEvent> >     mConfigEvents;
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
3644944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                // and read by other threads without lock or barrier via standby(), outDevice()
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
3694944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                bool                    mStandby;     // Whether thread is currently in standby.
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_source_t          mAudioSource; // (see audio.h, audio_source_t)
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                static const int        kNameLength = 16;   // prctl(PR_SET_NAME) limit
37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                char                    mName[kNameLength];
37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
38281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // list of suspended effects per session and per type. The first vector is
38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // keyed by session ID, the second by type UUID timeLow field
38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >
38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
386ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
3879e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
38881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
38981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass PlaybackThread : public ThreadBase {
39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
39381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
399bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
400bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
401bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
40281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
40381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
40481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
408bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent//FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled
409bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // for offloaded tracks
410bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
411bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
41381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type);
41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
428bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
429bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
431bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
43581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
439bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
440bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
441bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        writeCallback();
4423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        drainCallback();
4443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
445bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static      int         asyncCallback(stream_callback_event_t event, void *param, void *cookie);
447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
4514c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
45481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
45581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
45981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
46181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
46281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
46381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
46481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterVolume(float value);
46681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute(bool muted);
46781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamVolume(audio_stream_type_t stream, float value);
46981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamMute(audio_stream_type_t stream, bool muted);
47081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float       streamVolume(audio_stream_type_t stream) const;
47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
47581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
47681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
47881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
47974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
48081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
48181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                int sessionId,
48281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                IAudioFlinger::track_flags_t *flags,
48381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
484462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                int uid,
4859156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                status_t *status /*non-NULL*/);
48681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
48781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
48881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
48981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual audio_stream_t* stream() const;
49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
49181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
49281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
49381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
49481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
49581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
49681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
49781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
49881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
49981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
50081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
50181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
50281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
50381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
50481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
5051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        audioConfigChanged_l(
5061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    const DefaultKeyedVector< pid_t,sp<NotificationClient> >& notificationClients,
5071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    int event,
5081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    int param = 0);
509377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
510010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
511010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // Consider also removing and passing an explicit mMainBuffer initialization
512010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // parameter to AF::PlaybackThread::Track::Track().
513010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                int16_t     *mixBuffer() const {
514010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    return reinterpret_cast<int16_t *>(mSinkBuffer); };
51581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
51781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
51881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
51981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
52081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
52181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
52381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
52481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const;
52581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t getStrategyForSession_l(int sessionId);
52681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
52981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
530fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
531fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        void     invalidateTracks(audio_stream_type_t streamType);
53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5349b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
5359b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
5369b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
5379b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
539accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent                status_t         getTimestamp_l(AudioTimestamp& timestamp);
540accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
542deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // updated by readOutputParameters_l()
5439b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
5449b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
545010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    void*                           mSinkBuffer;         // frame size aligned sink buffer
54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // TODO:
54898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Rearrange the buffer info into a struct/class with
54998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // clear, copy, construction, destruction methods.
55098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
55198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer also has associated with it:
55298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
55398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBufferSize: Sink Buffer Size
55498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mFormat: Sink Buffer Format
55598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
55669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Mixer Buffer (mMixerBuffer*)
55769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    //
55869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // In the case of floating point or multichannel data, which is not in the
55969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink format, it is required to accumulate in a higher precision or greater channel count
56069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // buffer before downmixing or data conversion to the sink buffer.
56169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
56269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
56369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferEnabled;
56469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
56569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
56669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
56769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    void*                           mMixerBuffer;
56869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
56969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
57069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    size_t                          mMixerBufferSize;
57169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
57269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
57369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    audio_format_t                  mMixerBufferFormat;
57469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
57569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
57669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // when mMixerBuffer contains valid data after mixing.
57769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferValid;
57869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
57998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Effects Buffer (mEffectsBuffer*)
58098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
58198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // In the case of effects data, which is not in the sink format,
58298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // it is required to accumulate in a different buffer before data conversion
58398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // to the sink buffer.
58498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
58598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
58698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferEnabled;
58798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
58898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
58998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
59098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    void*                           mEffectBuffer;
59198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
59298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
59398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    size_t                          mEffectBufferSize;
59498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
59598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
59698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    audio_format_t                  mEffectBufferFormat;
59798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
59898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
59998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // when mEffectsBuffer contains valid data after mixing.
60098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
60198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // When this is set, all mixer data is routed into the effects buffer
60298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // for any processing (including output processing).
60398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferValid;
60498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
60581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples
61381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mFramesWritten would be better, or 64-bit even better
61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t                          mBytesWritten;
61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
62181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< wp<Track> >       mActiveTracks;  // FIXME check if this could be sp<>
623462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    SortedVector<int>               mWakeLockUids;
624462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    int                             mActiveTracksGeneration;
625fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    wp<Track>                       mLatestActiveTrack; // latest track added to mActiveTracks
62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Allocate a track name for a given channel mask.
62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //   Returns name >= 0 if successful, -1 on failure.
62981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual int             getTrackName_l(audio_channel_mask_t channelMask, int sessionId) = 0;
63081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void            deleteTrackName_l(int name) = 0;
63181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
63281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
63381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
63581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
63781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
63881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
63981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
64081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
64181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
64381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
64481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
64581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
64881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
65081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const Client&);
65681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread& operator = (const PlaybackThread&);
65781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
659bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
661ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    void        broadcast_l();
66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
663deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    void        readOutputParameters_l();
66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
66681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< sp<Track> >       mTracks;
66981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mStreamTypes[] uses 1 additional stream type internally for the OutputTrack used by
67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DuplicatingThread
67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT + 1];
67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyTime;
68225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    size_t                          mSinkBufferSize;
68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        activeSleepTime;
68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        idleSleepTime;
68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTime;
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
70181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyDelay;
70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
709bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
710bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
7123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
7133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
7143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
7153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
7163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
7173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
7183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
7193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
7203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
7213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
7223b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
7233b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
7243b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
725ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait
726ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // for async write callback in the thread loop before evaluating it
727bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mSignalPending;
728bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
729bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
73181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
73281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
73381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
73746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
74146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
743ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten    static const size_t     kFastMixerLogSize = 4 * 1024;
7449e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
7470f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
753bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten
754bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kastenprivate:
755bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    // timestamp latch:
756bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  D input is written by threadLoop_write while mutex is unlocked, and read while locked
757bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  Q output is written while locked, and read while locked
758bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    struct {
759bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        AudioTimestamp  mTimestamp;
760bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        uint32_t        mUnpresentedFrames;
761bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    } mLatchD, mLatchQ;
762bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchDValid;  // true means mLatchD is valid, and clock it into latch at next opportunity
763bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchQValid;  // true means mLatchQ is valid
76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
76781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
77381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
77481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
77681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
7781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
77981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
78081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
78281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
78381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
78481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
78581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
78681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
78781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
78881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
79181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
79381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
79481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
79581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
79981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixer*  mFastMixer;         // non-NULL if there is also a fast mixer
80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return mFastMixer != NULL; }
81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                       audio_io_handle_t id, audio_devices_t device);
82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
8331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, int sessionId);
83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
84481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volumes last sent to audio HAL with stream->set_volume()
84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mLeftVolFloat;
85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mRightVolFloat;
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
852bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
853bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type);
854bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    void processVolume_l(Track *track, bool lastTrack);
855bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
862bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
863bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
864bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
865bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
866bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device);
8676a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
868bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
869bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
870bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
871bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
872bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
873bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
874bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
875bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
876bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
8774c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
878bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
880bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        flushHw_l();
881bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
882bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
883bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        mHwPaused;
884bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        mFlushPending;
885bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
886bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
887d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent    wp<Track>   mPreviousTrack;         // used to detect track switch
888bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
889bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
890bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
891bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
892bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
8934de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
894bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
895bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
896bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
897bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
898bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
899bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
900bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
901bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
902bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
903bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
9043b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
9053b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
9063b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
9073b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
908bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
9104de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
9113b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
9123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
9133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
9144de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
9153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
9163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
9173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
9184de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
9194de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
9204de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
92681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                      audio_io_handle_t id);
92781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
94181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
94481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
9636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase
96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
96681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class RecordTrack;
9686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class ResamplerBufferProvider : public AudioBufferProvider
9696dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        // derives from AudioBufferProvider interface for use by resampler
9706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    {
9716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    public:
9726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        ResamplerBufferProvider(RecordTrack* recordTrack) : mRecordTrack(recordTrack) { }
9736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual ~ResamplerBufferProvider() { }
9746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // AudioBufferProvider interface
9756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
9766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
9776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    private:
9786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        RecordTrack * const mRecordTrack;
9796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    };
9806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
986d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
98746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    audio_devices_t inDevice
98846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
98946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
99046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
99146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1008e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
1009b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1010b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
101674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                    size_t *pFrameCount,
101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int sessionId,
1018462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    int uid,
1019ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten                    IAudioFlinger::track_flags_t *flags,
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
10219156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                    status_t *status /*non-NULL*/);
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              int triggerSession);
102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
1029a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual audio_stream_t* stream() const;
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual bool        checkForNewParameter_l(const String8& keyValuePair,
10371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                               status_t& status);
10381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        cacheParameters_l() {}
103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
10401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        audioConfigChanged_l(
10411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    const DefaultKeyedVector< pid_t,sp<NotificationClient> >& notificationClients,
10421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    int event,
10431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    int param = 0);
1044deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten            void        readInputParameters_l();
10455f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t    getInputFramesLost();
104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t hasAudioSession(int sessionId) const;
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            KeyedVector<int, bool> sessionIds() const;
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10619b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
10623a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            bool        hasFastCapture() const { return false; }
10639b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
106693e471f620454f7de73d190521568b1e25879767Glenn Kasten            void    standbyIfNotAlreadyInStandby();
106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
1069e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            void    inputStandBy();
107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
10732b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // mActiveTracks has dual roles:  it indicates the current active track(s), and
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
10752b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            SortedVector< sp<RecordTrack> >     mActiveTracks;
10762b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // generation counter for mActiveTracks
10772b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            int                                 mActiveTracksGen;
107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
10799b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
10808594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            // resampler converts input at HAL Hz to output at AudioRecord client Hz
10818594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int16_t                             *mRsmpInBuffer; // see new[] for details on the size
10828594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFrames;  // size of resampler input in frames
10838594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
10846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
10856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // rolling index that is never cleared
10868594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int32_t                             mRsmpInRear;    // last filled frame + 1
10878594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
1090b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
1091b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            const sp<MemoryDealer>              mReadOnlyHeap;
109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
1093