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,
511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_CREATE_AUDIO_PATCH,
521c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_RELEASE_AUDIO_PATCH,
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEventData: public RefBase {
561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~ConfigEventData() {}
581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEventData() {}
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  2. Lock mLock
671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  4. sendConfigEvent_l() reads status from event->mStatus;
691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  5. sendConfigEvent_l() returns status
701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  6. Unlock
711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //
721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Parameter sequence by server: threadLoop calling processConfigEvents_l():
731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 1. Lock mLock
741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 2. If there is an entry in mConfigEvents proceed ...
751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 3. Read first entry in mConfigEvents
761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 4. Remove first entry from mConfigEvents
771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 5. Process
781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 6. Set event->mStatus
791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 7. event->mCond.signal
801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 8. Unlock
811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEvent: public RefBase {
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const int mType; // event type e.g. CFG_EVENT_IO
891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex mLock;     // mutex associated with mCond
901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Condition mCond; // condition for status return
911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status_t mStatus; // status communicated to sender
921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        bool mWaitStatus; // true if sender is waiting for status
931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEventData> mData;     // event specific parameter data
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEvent(int type) : mType(type), mStatus(NO_ERROR), mWaitStatus(false), mData(NULL) {}
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEventData : public ConfigEventData {
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        IoConfigEventData(int event, int param) :
1021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEvent(event), mParam(param) {}
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "IO event: event %d, param %d\n", mEvent, mParam);
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mEvent;
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int mParam;
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEvent : public ConfigEvent {
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        IoConfigEvent(int event, int param) :
1151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_IO) {
1161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new IoConfigEventData(event, param);
1171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~IoConfigEvent() {}
1191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEventData : public ConfigEventData {
1221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) :
1241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mPid(pid), mTid(tid), mPrio(prio) {}
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEvent : public ConfigEvent {
1361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
1381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_PRIO) {
1391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new PrioConfigEventData(pid, tid, prio);
1401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~PrioConfigEvent() {}
1421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEventData : public ConfigEventData {
1451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEventData(String8 keyValuePairs) :
1471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mKeyValuePairs(keyValuePairs) {}
1481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) {
1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const String8 mKeyValuePairs;
1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEvent : public ConfigEvent {
1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEvent(String8 keyValuePairs) :
1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_SET_PARAMETER) {
1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new SetParameterConfigEventData(keyValuePairs);
1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mWaitStatus = true;
1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~SetParameterConfigEvent() {}
1641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEventData : public ConfigEventData {
1671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData(const struct audio_patch patch,
1691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        audio_patch_handle_t handle) :
1701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mPatch(patch), mHandle(handle) {}
1711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
1731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
1741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        const struct audio_patch mPatch;
1771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
1781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEvent : public ConfigEvent {
1811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEvent(const struct audio_patch patch,
1831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                    audio_patch_handle_t handle) :
1841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
1851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new CreateAudioPatchConfigEventData(patch, handle);
1861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
1871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~CreateAudioPatchConfigEvent() {}
1891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEventData : public ConfigEventData {
1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
1941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mHandle(handle) {}
1951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
1971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
1981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
2011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
2021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEvent : public ConfigEvent {
2041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
2051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
2061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
2071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new ReleaseAudioPatchConfigEventData(handle);
2081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
2091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~ReleaseAudioPatchConfigEvent() {}
2111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const PMDeathRecipient&);
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient& operator = (const PMDeathRecipient&);
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
237463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t format() const { return mHALFormat; }
23883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                uint32_t channelCount() const { return mChannelCount; }
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
2409b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
2419b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
242bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
2471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                    status_t& status) = 0;
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
251021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    virtual     void        audioConfigChanged(int event, int param = 0) = 0;
2521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // sendConfigEvent_l() must be called with ThreadBase::mLock held
2531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // Can temporarily release the lock if waiting for a reply from
2541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // processConfigEvents_l().
2551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent(int event, int param = 0);
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendIoConfigEvent_l(int event, int param = 0);
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
2591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
2601c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
2611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                            audio_patch_handle_t *handle);
2621c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
263021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                void        processConfigEvents_l();
2641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        cacheParameters_l() = 0;
2651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
2661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               audio_patch_handle_t *handle) = 0;
2671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
26883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config) = 0;
2691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     audio_stream_t* stream() const = 0;
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int sessionId,
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
2859156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                    status_t *status /*non-NULL*/);
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    TRACK_SESSION = 0x2     // the audio session corresponds to at least one
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain(int sessionId);
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain_l(int sessionId) const;
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
310bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
311bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId);
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect);
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
3240f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     void detachAuxEffect_l(int effectId __unused) {}
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // returns either EFFECT_SESSION if effects on this audio session exist in one
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // chain, or TRACK_SESSION if tracks on this audio session exist, or both
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const = 0;
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
3300f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                virtual uint32_t getStrategyForSession_l(int sessionId __unused) { return 0; }
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // suspend or restore effect according to the type of effect passed. a NULL
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // type pointer means suspend all effects in the session
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended(const effect_uuid_t *type,
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        bool suspend,
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        int sessionId = AUDIO_SESSION_OUTPUT_MIX);
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended/restored when an effect is enabled
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // or disabled
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 bool enabled,
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   bool enabled,
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   int sessionId = AUDIO_SESSION_OUTPUT_MIX);
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
349b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // Return a reference to a per-thread heap which can be used to allocate IMemory
350b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // objects that will be read-only to client processes, read/write to mediaserver,
351b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // and shared by all client processes of the thread.
352b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // The heap is per-thread rather than common across all threads, because
353b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // clients can't be trusted not to modify the offset of the IMemory they receive.
354b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // If a thread does not have such a heap, this method returns 0.
355b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                virtual sp<MemoryDealer>    readOnlyHeap() const { return 0; }
35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3576181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten                virtual sp<IMemory> pipeMemory() const { return 0; }
3586181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable     Mutex                   mLock;
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // entry describing an effect being suspended in mSuspendedSessions keyed vector
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                class SuspendedSessionDesc : public RefBase {
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                public:
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    SuspendedSessionDesc() : mRefCount(0) {}
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int mRefCount;          // number of active suspend requests
36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_uuid_t mType;    // effect type UUID
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
372e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock(int uid = -1);
373e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock_l(int uid = -1);
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock();
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock_l();
376462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids(const SortedVector<int> &uids);
377462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids_l(const SortedVector<int> &uids);
378462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        getPowerManager_l();
37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          int sessionId);
38281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // updated mSuspendedSessions when an effect suspended or restored
38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      int sessionId);
38681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
38781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
38881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
389014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
390014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
39481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
4019b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
402deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // updated by PlaybackThread::readOutputParameters_l() or
403deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // RecordThread::readInputParameters_l()
40481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
40681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
407f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
40881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
409463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mFormat;           // Source format for Recording and
410463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format for Playback.
411463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format may be different than
412463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // HAL format if Fastmixer is used.
413463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mHALFormat;
41470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten                size_t                  mBufferSize;       // HAL buffer size for read() or write()
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                Vector< sp<ConfigEvent> >     mConfigEvents;
41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
4194944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                // and read by other threads without lock or barrier via standby(), outDevice()
42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
42181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
4244944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                bool                    mStandby;     // Whether thread is currently in standby.
42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_source_t          mAudioSource; // (see audio.h, audio_source_t)
42881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                static const int        kNameLength = 16;   // prctl(PR_SET_NAME) limit
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                char                    mName[kNameLength];
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
43581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // list of suspended effects per session and per type. The first vector is
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // keyed by session ID, the second by type UUID timeLow field
43981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >
44081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
441ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
4429e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
44381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
44481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
44681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass PlaybackThread : public ThreadBase {
44781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
44881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
45081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
45381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
45881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
45981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
461bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
462bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent//FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled
464bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // for offloaded tracks
465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
466bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
46781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type);
46981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
47081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
47581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
47881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
48081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
48181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
48281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
48581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
48781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
48881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
48981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
49181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
49281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
49381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
494bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
495bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
496bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        writeCallback();
4973b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        drainCallback();
4993b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
501bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static      int         asyncCallback(stream_callback_event_t event, void *param, void *cookie);
502bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
503bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
504bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
505bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
5064c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
50781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
50881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
50981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
51081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
51281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
51481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
51681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
51781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
51881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
51981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterVolume(float value);
52181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute(bool muted);
52281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamVolume(audio_stream_type_t stream, float value);
52481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamMute(audio_stream_type_t stream, bool muted);
52581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float       streamVolume(audio_stream_type_t stream) const;
52781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
52981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
53081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
53181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
53474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
53581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                int sessionId,
53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                IAudioFlinger::track_flags_t *flags,
53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
539462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                int uid,
5409156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                status_t *status /*non-NULL*/);
54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
54381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual audio_stream_t* stream() const;
54581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
54881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
54981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
55381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
55481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
55681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
55781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
55881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
55981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
560021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    virtual     void        audioConfigChanged(int event, int param = 0);
561377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
562010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
563010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // Consider also removing and passing an explicit mMainBuffer initialization
564010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // parameter to AF::PlaybackThread::Track::Track().
565010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                int16_t     *mixBuffer() const {
566010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    return reinterpret_cast<int16_t *>(mSinkBuffer); };
56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const;
57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t getStrategyForSession_l(int sessionId);
57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
58181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
582fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
583fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
58481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        void     invalidateTracks(audio_stream_type_t streamType);
58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5869b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
5879b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
5889b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
5899b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
59081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                status_t    getTimestamp_l(AudioTimestamp& timestamp);
59283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
59383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        addPatchTrack(const sp<PatchTrack>& track);
59483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        deletePatchTrack(const sp<PatchTrack>& track);
59583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
59683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
597accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
59881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
599deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // updated by readOutputParameters_l()
6009b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
6019b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
602010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    void*                           mSinkBuffer;         // frame size aligned sink buffer
60381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
60498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // TODO:
60598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Rearrange the buffer info into a struct/class with
60698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // clear, copy, construction, destruction methods.
60798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
60898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer also has associated with it:
60998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
61098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBufferSize: Sink Buffer Size
61198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mFormat: Sink Buffer Format
61298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
61369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Mixer Buffer (mMixerBuffer*)
61469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    //
61569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // In the case of floating point or multichannel data, which is not in the
61669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink format, it is required to accumulate in a higher precision or greater channel count
61769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // buffer before downmixing or data conversion to the sink buffer.
61869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
61969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
62069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferEnabled;
62169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
62269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
62369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
62469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    void*                           mMixerBuffer;
62569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
62669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
62769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    size_t                          mMixerBufferSize;
62869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
62969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
63069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    audio_format_t                  mMixerBufferFormat;
63169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
63269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
63369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // when mMixerBuffer contains valid data after mixing.
63469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferValid;
63569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
63698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Effects Buffer (mEffectsBuffer*)
63798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
63898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // In the case of effects data, which is not in the sink format,
63998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // it is required to accumulate in a different buffer before data conversion
64098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // to the sink buffer.
64198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
64298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
64398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferEnabled;
64498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
64598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
64698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
64798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    void*                           mEffectBuffer;
64898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
64998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
65098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    size_t                          mEffectBufferSize;
65198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
65298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
65398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    audio_format_t                  mEffectBufferFormat;
65498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
65598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
65698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // when mEffectsBuffer contains valid data after mixing.
65798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
65898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // When this is set, all mixer data is routed into the effects buffer
65998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // for any processing (including output processing).
66098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferValid;
66198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
66381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
66681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples
67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mFramesWritten would be better, or 64-bit even better
67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t                          mBytesWritten;
67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< wp<Track> >       mActiveTracks;  // FIXME check if this could be sp<>
680462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    SortedVector<int>               mWakeLockUids;
681462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    int                             mActiveTracksGeneration;
682fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    wp<Track>                       mLatestActiveTrack; // latest track added to mActiveTracks
68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Allocate a track name for a given channel mask.
68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //   Returns name >= 0 if successful, -1 on failure.
686e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual int             getTrackName_l(audio_channel_mask_t channelMask,
687e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId) = 0;
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void            deleteTrackName_l(int name) = 0;
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
70181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
70681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
7101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                   audio_patch_handle_t *handle);
7111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
7121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
7130f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                bool        usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL) &&
7140f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent                                                (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
7150f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent
71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
71981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread& operator = (const PlaybackThread&);
72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
723bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
72481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
725ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    void        broadcast_l();
72681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
727deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    void        readOutputParameters_l();
72881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
73181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< sp<Track> >       mTracks;
733223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT];
73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyTime;
74425c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    size_t                          mSinkBufferSize;
74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
74781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        activeSleepTime;
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        idleSleepTime;
74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTime;
75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
75381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
75481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
75581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
75681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
75781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         standbyDelay;
76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
76781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
771bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
772bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
773bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
7743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
7753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
7763b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
7773b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
7783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
7793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
7803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
7813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
7823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
7833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
7843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
7853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
7863b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
787ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait
788ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // for async write callback in the thread loop before evaluating it
789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mSignalPending;
790bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
791bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
79381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
79481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
79581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
79946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
80346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
805ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten    static const size_t     kFastMixerLogSize = 4 * 1024;
8069e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
8090f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
815d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwSupportsPause;
816d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwPaused;
817d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mFlushPending;
818bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kastenprivate:
819bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    // timestamp latch:
820bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  D input is written by threadLoop_write while mutex is unlocked, and read while locked
821bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  Q output is written while locked, and read while locked
822bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    struct {
823bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        AudioTimestamp  mTimestamp;
824bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        uint32_t        mUnpresentedFrames;
8254c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten        KeyedVector<Track *, uint32_t> mFramesReleased;
826bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    } mLatchD, mLatchQ;
8274c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten    bool mLatchDValid;  // true means mLatchD is valid
8284c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                        //     (except for mFramesReleased which is filled in later),
8294c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                        //     and clock it into latch at next opportunity
830bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchQValid;  // true means mLatchQ is valid
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
8451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
850e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual     int         getTrackName_l(audio_channel_mask_t channelMask,
851e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId);
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
858bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
8684d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten                sp<FastMixer>     mFastMixer;     // non-0 if there is also a fast mixer
86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
8844d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    virtual     bool        hasFastMixer() const { return mFastMixer != 0; }
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
88881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
88983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                       audio_io_handle_t id, audio_devices_t device);
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
9021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
903e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
906e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual     int         getTrackName_l(audio_channel_mask_t channelMask,
907e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId);
90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
90981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
91181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
91281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
91781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
918d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     void        threadLoop_exit();
919d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     bool        shouldStandby_l();
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volumes last sent to audio HAL with stream->set_volume()
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mLeftVolFloat;
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mRightVolFloat;
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type);
927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    void processVolume_l(Track *track, bool lastTrack);
928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
937bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
938bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
939bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        audio_io_handle_t id, uint32_t device);
9406a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
941e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
942bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
943bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
944bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
945bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
946bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
947bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
948bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
949bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
9504c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
951bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
953bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
954bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
955d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent    wp<Track>   mPreviousTrack;         // used to detect track switch
956bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
957bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
958bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
960bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
9614de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
963bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
964bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
965bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
966bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
969bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
970bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
9723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
9733b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
9743b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
9753b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
9784de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
9793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
9803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
9813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
9824de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
9833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
9843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
9853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
9864de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
9874de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
9884de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
990bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                      audio_io_handle_t id);
99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1010bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
10316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase
103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class RecordTrack;
10366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class ResamplerBufferProvider : public AudioBufferProvider
10376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        // derives from AudioBufferProvider interface for use by resampler
10386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    {
10396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    public:
10406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        ResamplerBufferProvider(RecordTrack* recordTrack) : mRecordTrack(recordTrack) { }
10416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual ~ResamplerBufferProvider() { }
10426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // AudioBufferProvider interface
10436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
10446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
10456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    private:
10466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        RecordTrack * const mRecordTrack;
10476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    };
10486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
1054d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
105546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    audio_devices_t inDevice
105646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
105746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
105846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
105946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1076e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
1077b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1078b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
10796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
10806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
108674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                    size_t *pFrameCount,
108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int sessionId,
10887df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                    size_t *notificationFrames,
1089462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    int uid,
1090ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten                    IAudioFlinger::track_flags_t *flags,
109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
10929156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                    status_t *status /*non-NULL*/);
109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              int triggerSession);
109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
1100a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual audio_stream_t* stream() const;
110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual bool        checkForNewParameter_l(const String8& keyValuePair,
11081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                               status_t& status);
11091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        cacheParameters_l() {}
111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
1111021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    virtual void        audioConfigChanged(int event, int param = 0);
11121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    createAudioPatch_l(const struct audio_patch *patch,
11131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                           audio_patch_handle_t *handle);
11141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
111583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
111683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        addPatchRecord(const sp<PatchRecord>& record);
111783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        deletePatchRecord(const sp<PatchRecord>& record);
111883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1119deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten            void        readInputParameters_l();
11205f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t    getInputFramesLost();
112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t hasAudioSession(int sessionId) const;
112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            KeyedVector<int, bool> sessionIds() const;
113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11369b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
11376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool        hasFastCapture() const { return mFastCapture != 0; }
113883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void        getAudioPortConfig(struct audio_port_config *config);
11399b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
114293e471f620454f7de73d190521568b1e25879767Glenn Kasten            void    standbyIfNotAlreadyInStandby();
114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
1145e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            void    inputStandBy();
114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
11492b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // mActiveTracks has dual roles:  it indicates the current active track(s), and
115081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
11512b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            SortedVector< sp<RecordTrack> >     mActiveTracks;
11522b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // generation counter for mActiveTracks
11532b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            int                                 mActiveTracksGen;
115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
11559b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
11568594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            // resampler converts input at HAL Hz to output at AudioRecord client Hz
11578594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int16_t                             *mRsmpInBuffer; // see new[] for details on the size
11588594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFrames;  // size of resampler input in frames
11598594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
11606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
11616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // rolling index that is never cleared
11628594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int32_t                             mRsmpInRear;    // last filled frame + 1
11638594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
1166b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
1167b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            const sp<MemoryDealer>              mReadOnlyHeap;
11686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
11696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // one-time initialization, no locks required
11706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<FastCapture>                     mFastCapture;   // non-0 if there is also a fast capture
11716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog thread
11726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
11736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // contents are not guaranteed to be consistent, no locks required
11746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureDumpState                mFastCaptureDumpState;
11756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
11766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME StateQueue observer and mutator dump fields
11776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
11786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog dump
11796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
11806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // accessible only within the threadLoop(), no locks required
11816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            //          mFastCapture->sq()      // for mutating and pushing state
11826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t     mFastCaptureFutex;      // for cold idle
11836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
11846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The HAL input source is treated as non-blocking,
11856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // but current implementation is blocking
11866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mInputSource;
11876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The source for the normal capture thread to read from: mInputSource or mPipeSource
11886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mNormalSource;
11896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
11906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
11916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Sink>                      mPipeSink;
11926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe source read by normal thread,
11936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
11946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mPipeSource;
11956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
11966dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t                              mPipeFramesP2;
11976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the Pipe as IMemory, otherwise clear
11986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<IMemory>                         mPipeMemory;
11996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
12006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            static const size_t                 kFastCaptureLogSize = 4 * 1024;
12016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBLog::Writer>                   mFastCaptureNBLogWriter;
12026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
12036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool                                mFastTrackAvail;    // true if fast track available
120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
1205