Threads.h revision 40eb1a1f8871909c272e72afaf7d5af84fea2412
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
3597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    static const char *threadTypeToString(type_t type);
3697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
3872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                audio_devices_t outDevice, audio_devices_t inDevice, type_t type,
3972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady);
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~ThreadBase();
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    virtual status_t    readyToRun();
43cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpBase(int fd, const Vector<String16>& args);
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpEffectChains(int fd, const Vector<String16>& args);
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearPowerManager();
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // base for record and playback
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_IO,
521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_PRIO,
531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_SET_PARAMETER,
541c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_CREATE_AUDIO_PATCH,
551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_RELEASE_AUDIO_PATCH,
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEventData: public RefBase {
591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~ConfigEventData() {}
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEventData() {}
651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  2. Lock mLock
701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  4. sendConfigEvent_l() reads status from event->mStatus;
721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  5. sendConfigEvent_l() returns status
731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  6. Unlock
741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //
751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Parameter sequence by server: threadLoop calling processConfigEvents_l():
761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 1. Lock mLock
771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 2. If there is an entry in mConfigEvents proceed ...
781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 3. Read first entry in mConfigEvents
791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 4. Remove first entry from mConfigEvents
801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 5. Process
811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 6. Set event->mStatus
821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 7. event->mCond.signal
831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 8. Unlock
841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEvent: public RefBase {
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const int mType; // event type e.g. CFG_EVENT_IO
921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex mLock;     // mutex associated with mCond
931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Condition mCond; // condition for status return
941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status_t mStatus; // status communicated to sender
951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        bool mWaitStatus; // true if sender is waiting for status
9672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEventData> mData;     // event specific parameter data
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
10072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        ConfigEvent(int type, bool requiresSystemReady = false) :
10172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mType(type), mStatus(NO_ERROR), mWaitStatus(false),
10272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEventData : public ConfigEventData {
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
10773e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        IoConfigEventData(audio_io_config_event event) :
10873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            mEvent(event) {}
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
11173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            snprintf(buffer, size, "IO event: event %d\n", mEvent);
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const audio_io_config_event mEvent;
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEvent : public ConfigEvent {
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
11973e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        IoConfigEvent(audio_io_config_event event) :
1201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_IO) {
12173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            mData = new IoConfigEventData(event);
1221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~IoConfigEvent() {}
1241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEventData : public ConfigEventData {
1271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) :
1291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mPid(pid), mTid(tid), mPrio(prio) {}
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEvent : public ConfigEvent {
1411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
14372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            ConfigEvent(CFG_EVENT_PRIO, true) {
1441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new PrioConfigEventData(pid, tid, prio);
1451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~PrioConfigEvent() {}
1471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEventData : public ConfigEventData {
1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEventData(String8 keyValuePairs) :
1521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mKeyValuePairs(keyValuePairs) {}
1531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) {
1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
1561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const String8 mKeyValuePairs;
1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEvent : public ConfigEvent {
1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        SetParameterConfigEvent(String8 keyValuePairs) :
1641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_SET_PARAMETER) {
1651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new SetParameterConfigEventData(keyValuePairs);
1661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mWaitStatus = true;
1671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~SetParameterConfigEvent() {}
1691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEventData : public ConfigEventData {
1721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData(const struct audio_patch patch,
1741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        audio_patch_handle_t handle) :
1751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mPatch(patch), mHandle(handle) {}
1761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
1781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
1791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        const struct audio_patch mPatch;
1821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
1831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEvent : public ConfigEvent {
1861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEvent(const struct audio_patch patch,
1881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                    audio_patch_handle_t handle) :
1891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new CreateAudioPatchConfigEventData(patch, handle);
1911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~CreateAudioPatchConfigEvent() {}
1941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEventData : public ConfigEventData {
1971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
1991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mHandle(handle) {}
2001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
2021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
2031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
2061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
2071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEvent : public ConfigEvent {
2091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
2101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
2111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
2121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new ReleaseAudioPatchConfigEventData(handle);
2131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
2141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~ReleaseAudioPatchConfigEvent() {}
2161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const PMDeathRecipient&);
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient& operator = (const PMDeathRecipient&);
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
237f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                bool isDuplicating() const { return (mType == DUPLICATING); }
238f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
244463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t format() const { return mHALFormat; }
24583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                uint32_t channelCount() const { return mChannelCount; }
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
2479b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
2489b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
249bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
2541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                    status_t& status) = 0;
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
25873e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event) = 0;
2591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // sendConfigEvent_l() must be called with ThreadBase::mLock held
2601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // Can temporarily release the lock if waiting for a reply from
2611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // processConfigEvents_l().
2621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
26373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                void        sendIoConfigEvent(audio_io_config_event event);
26473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent                void        sendIoConfigEvent_l(audio_io_config_event event);
26572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                void        sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio);
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
2671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
2681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
2691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                            audio_patch_handle_t *handle);
2701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
271021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                void        processConfigEvents_l();
2721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        cacheParameters_l() = 0;
2731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
2741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               audio_patch_handle_t *handle) = 0;
2751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
27683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config) = 0;
2771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     audio_stream_t* stream() const = 0;
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int sessionId,
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
2939156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                    status_t *status /*non-NULL*/);
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    TRACK_SESSION = 0x2     // the audio session corresponds to at least one
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain(int sessionId);
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectChain> getEffectChain_l(int sessionId) const;
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
318bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
319bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect(int sessionId, int effectId);
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioFlinger::EffectModule> getEffect_l(int sessionId, int effectId);
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect);
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
3320f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     void detachAuxEffect_l(int effectId __unused) {}
33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // returns either EFFECT_SESSION if effects on this audio session exist in one
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // chain, or TRACK_SESSION if tracks on this audio session exist, or both
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const = 0;
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
3380f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                virtual uint32_t getStrategyForSession_l(int sessionId __unused) { return 0; }
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // suspend or restore effect according to the type of effect passed. a NULL
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // type pointer means suspend all effects in the session
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended(const effect_uuid_t *type,
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        bool suspend,
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        int sessionId = AUDIO_SESSION_OUTPUT_MIX);
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended/restored when an effect is enabled
34681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // or disabled
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
34881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 bool enabled,
34981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 int sessionId = AUDIO_SESSION_OUTPUT_MIX);
35081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
35181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   bool enabled,
35281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   int sessionId = AUDIO_SESSION_OUTPUT_MIX);
35381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
357b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // Return a reference to a per-thread heap which can be used to allocate IMemory
358b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // objects that will be read-only to client processes, read/write to mediaserver,
359b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // and shared by all client processes of the thread.
360b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // The heap is per-thread rather than common across all threads, because
361b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // clients can't be trusted not to modify the offset of the IMemory they receive.
362b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // If a thread does not have such a heap, this method returns 0.
363b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                virtual sp<MemoryDealer>    readOnlyHeap() const { return 0; }
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3656181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten                virtual sp<IMemory> pipeMemory() const { return 0; }
3666181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten
36772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        void systemReady();
36872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable     Mutex                   mLock;
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // entry describing an effect being suspended in mSuspendedSessions keyed vector
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                class SuspendedSessionDesc : public RefBase {
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                public:
37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    SuspendedSessionDesc() : mRefCount(0) {}
37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int mRefCount;          // number of active suspend requests
37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_uuid_t mType;    // effect type UUID
38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
382e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock(int uid = -1);
383e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                void        acquireWakeLock_l(int uid = -1);
38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock();
38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock_l();
386462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids(const SortedVector<int> &uids);
387462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        updateWakeLockUids_l(const SortedVector<int> &uids);
388462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        getPowerManager_l();
38981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
39081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          int sessionId);
39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // updated mSuspendedSessions when an effect suspended or restored
39381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
39481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      int sessionId);
39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
399014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
400014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
40181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
40281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
40481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
40681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
40881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
40981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
4119b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
412deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // updated by PlaybackThread::readOutputParameters_l() or
413deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // RecordThread::readInputParameters_l()
41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
417f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
41997b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten                // not HAL frame size, this is for output sink (to pipe to fast mixer)
420463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mFormat;           // Source format for Recording and
421463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format for Playback.
422463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format may be different than
423463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // HAL format if Fastmixer is used.
424463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mHALFormat;
42570949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten                size_t                  mBufferSize;       // HAL buffer size for read() or write()
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                Vector< sp<ConfigEvent> >     mConfigEvents;
42872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                Vector< sp<ConfigEvent> >     mPendingConfigEvents; // events awaiting system ready
42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
4314944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                // and read by other threads without lock or barrier via standby(), outDevice()
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
43581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
4364944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                bool                    mStandby;     // Whether thread is currently in standby.
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
439296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                struct audio_patch      mPatch;
440f59497bd3c190e087202043de5450ef06e92b27dGlenn Kasten                audio_source_t          mAudioSource;
44181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
44381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
44481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
445d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                static const int        kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
446d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                char                    mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
44781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
44881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
44981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
45081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // list of suspended effects per session and per type. The first vector is
45181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // keyed by session ID, the second by type UUID timeLow field
45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                KeyedVector< int, KeyedVector< int, sp<SuspendedSessionDesc> > >
45381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
454ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
4559e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
45672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool                    mSystemReady;
45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
45881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass PlaybackThread : public ThreadBase {
46181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
46281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
46481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
46681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
46781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
47381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
475bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
476bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
477bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent//FIXME may be more appropriate if expressed in time units. Need to revise how underrun is handled
478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // for offloaded tracks
479bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
480bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
48181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
48272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady);
48381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
48481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
48581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
48681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
48781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
48881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
48981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
49181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
49281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
49381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
49481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
49581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
49681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
497bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
49981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
50181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
50281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
50381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
50481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
50581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
50681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
50781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
508bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
509bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
510bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        writeCallback();
5113b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
512bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        drainCallback();
5133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
514bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
515bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    static      int         asyncCallback(stream_callback_event_t event, void *param, void *cookie);
516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
518bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
519bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
5204c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
52181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
52381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
52481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
52681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
52881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
52981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
53081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
53181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
53281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
53381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterVolume(float value);
53581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute(bool muted);
53681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
53781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamVolume(audio_stream_type_t stream, float value);
53881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setStreamMute(audio_stream_type_t stream, bool muted);
53981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float       streamVolume(audio_stream_type_t stream) const;
54181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
54381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
54481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
54581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
54681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
54781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
54874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
54981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
55081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                int sessionId,
55181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                IAudioFlinger::track_flags_t *flags,
55281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
553462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                int uid,
5549156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                                status_t *status /*non-NULL*/);
55581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
55681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
55781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
55881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual audio_stream_t* stream() const;
55981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
56181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
56281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
56381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
56481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
56581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
56681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
57473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event);
575377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
576010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
577010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // Consider also removing and passing an explicit mMainBuffer initialization
578010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // parameter to AF::PlaybackThread::Track::Track().
579010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                int16_t     *mixBuffer() const {
580010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    return reinterpret_cast<int16_t *>(mSinkBuffer); };
58181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
58381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track> track,
58481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
58581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track> track,
58681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
58781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
58981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
59081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t hasAudioSession(int sessionId) const;
59181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual uint32_t getStrategyForSession_l(int sessionId);
59281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
59581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
596fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
597fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
59881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        void     invalidateTracks(audio_stream_type_t streamType);
59981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6009b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
6019b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
6029b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
6039b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
60481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
60583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                status_t    getTimestamp_l(AudioTimestamp& timestamp);
60683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
60783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        addPatchTrack(const sp<PatchTrack>& track);
60883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        deletePatchTrack(const sp<PatchTrack>& track);
60983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
61083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
611accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
613deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // updated by readOutputParameters_l()
6149b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
6159b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
61608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    bool                            mThreadThrottle;     // throttle the thread processing
61740eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleTimeMs; // throttle time for MIXER threads
61840eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleEndMs;  // notify once per throttling
61908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    uint32_t                        mHalfBufferMs;       // half the buffer size in milliseconds
62008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
621010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    void*                           mSinkBuffer;         // frame size aligned sink buffer
62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // TODO:
62498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Rearrange the buffer info into a struct/class with
62598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // clear, copy, construction, destruction methods.
62698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
62798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer also has associated with it:
62898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
62998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBufferSize: Sink Buffer Size
63098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mFormat: Sink Buffer Format
63198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
63269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Mixer Buffer (mMixerBuffer*)
63369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    //
63469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // In the case of floating point or multichannel data, which is not in the
63569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink format, it is required to accumulate in a higher precision or greater channel count
63669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // buffer before downmixing or data conversion to the sink buffer.
63769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
63869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
63969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferEnabled;
64069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
64169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
64269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
64369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    void*                           mMixerBuffer;
64469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
64569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
64669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    size_t                          mMixerBufferSize;
64769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
64869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
64969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    audio_format_t                  mMixerBufferFormat;
65069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
65169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
65269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // when mMixerBuffer contains valid data after mixing.
65369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferValid;
65469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
65598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Effects Buffer (mEffectsBuffer*)
65698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
65798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // In the case of effects data, which is not in the sink format,
65898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // it is required to accumulate in a different buffer before data conversion
65998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // to the sink buffer.
66098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
66198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
66298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferEnabled;
66398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
66498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
66598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
66698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    void*                           mEffectBuffer;
66798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
66898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
66998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    size_t                          mEffectBufferSize;
67098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
67198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
67298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    audio_format_t                  mEffectBufferFormat;
67398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
67498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
67598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // when mEffectsBuffer contains valid data after mixing.
67698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
67798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // When this is set, all mixer data is routed into the effects buffer
67898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // for any processing (including output processing).
67998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferValid;
68098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME overflows every 6+ hours at 44.1 kHz stereo 16-bit samples
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mFramesWritten would be better, or 64-bit even better
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t                          mBytesWritten;
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< wp<Track> >       mActiveTracks;  // FIXME check if this could be sp<>
699462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    SortedVector<int>               mWakeLockUids;
700462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    int                             mActiveTracksGeneration;
701fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    wp<Track>                       mLatestActiveTrack; // latest track added to mActiveTracks
70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Allocate a track name for a given channel mask.
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //   Returns name >= 0 if successful, -1 on failure.
705e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual int             getTrackName_l(audio_channel_mask_t channelMask,
706e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId) = 0;
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void            deleteTrackName_l(int name) = 0;
70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
71081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
71181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
71281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
71381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
71881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
72281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
72481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
72581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
72781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
7291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                   audio_patch_handle_t *handle);
7301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
7311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
7326fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                bool        usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL)
7336fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && mHwSupportsPause
7346fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
7350f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread& operator = (const PlaybackThread&);
74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
743bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
745ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    void        broadcast_l();
74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
747deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    void        readOutputParameters_l();
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< sp<Track> >       mTracks;
753223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT];
75481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
75581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
75781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
763ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyTimeNs;
76425c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    size_t                          mSinkBufferSize;
76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
767ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mActiveSleepTimeUs;
768ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mIdleSleepTimeUs;
76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
770ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mSleepTimeUs;
77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
77381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
77481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
77581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
77681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
77781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
77981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
78081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
78181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
783ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyDelayNs;
78481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
78681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
78781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
78981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
79081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
791bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
792bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
793bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
7943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
7953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
7963b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
7973b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
7983b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
7993b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
8003b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
8013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
8023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
8033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
8043b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
8053b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
8063b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
807ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait
808ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    // for async write callback in the thread loop before evaluating it
809bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mSignalPending;
810bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
811bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
81946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
82346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
825ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten    static const size_t     kFastMixerLogSize = 4 * 1024;
8269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
8290f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
835d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwSupportsPause;
836d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwPaused;
837d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mFlushPending;
838bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kastenprivate:
839bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    // timestamp latch:
840bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  D input is written by threadLoop_write while mutex is unlocked, and read while locked
841bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    //  Q output is written while locked, and read while locked
842bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    struct {
843bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        AudioTimestamp  mTimestamp;
844bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        uint32_t        mUnpresentedFrames;
8454c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten        KeyedVector<Track *, uint32_t> mFramesReleased;
846bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    } mLatchD, mLatchQ;
8474c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten    bool mLatchDValid;  // true means mLatchD is valid
8484c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                        //     (except for mFramesReleased which is filled in later),
8494c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                        //     and clock it into latch at next opportunity
850bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten    bool mLatchQValid;  // true means mLatchQ is valid
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
85972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady,
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
8661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
871e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual     int         getTrackName_l(audio_channel_mask_t channelMask,
872e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId);
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
879bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
886054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
887054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                   audio_patch_handle_t *handle);
888054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
889054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
8934d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten                sp<FastMixer>     mFastMixer;     // non-0 if there is also a fast mixer
89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
9094d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    virtual     bool        hasFastMixer() const { return mFastMixer != 0; }
91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
91181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              ALOG_ASSERT(fastIndex < FastMixerState::kMaxFastTracks);
91281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
91483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
91881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
92172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                       audio_io_handle_t id, audio_devices_t device, bool systemReady);
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
9271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
928e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
931e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    virtual     int         getTrackName_l(audio_channel_mask_t channelMask,
932e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                                           audio_format_t format, int sessionId);
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
93781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
94181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
943d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     void        threadLoop_exit();
944d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     bool        shouldStandby_l();
94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
94643b4dcc660e6da96285e4672ae371070ab845401Phil Burk    virtual     void        onAddNewTrack_l();
94743b4dcc660e6da96285e4672ae371070ab845401Phil Burk
94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volumes last sent to audio HAL with stream->set_volume()
94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mLeftVolFloat;
95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mRightVolFloat;
95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
95372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type,
95472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        bool systemReady);
955bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    void processVolume_l(Track *track, bool lastTrack);
956bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
95943b4dcc660e6da96285e4672ae371070ab845401Phil Burk
96043b4dcc660e6da96285e4672ae371070ab845401Phil Burk    wp<Track>               mPreviousTrack;         // used to detect track switch
96143b4dcc660e6da96285e4672ae371070ab845401Phil Burk
96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
966bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
969bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
97072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        audio_io_handle_t id, uint32_t device, bool systemReady);
9716a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
972e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
973bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
974bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
975bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
978bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
979bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
980bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
982bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
983bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
984bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
985bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
986bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
987bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
988bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
989bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
9904de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
991bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
993bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
994bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
995bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
996bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
997bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
998bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
999bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1000bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
10013b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
10023b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
10033b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
10043b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
1005bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1006bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
10074de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
10083b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
10093b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
10103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
10114de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
10123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
10133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
10143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
10154de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
10164de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
10174de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
1018bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
1019bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
102372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                      audio_io_handle_t id, bool systemReady);
102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1039bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
10606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase
106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class RecordTrack;
106573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
106673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    /* The ResamplerBufferProvider is used to retrieve recorded input data from the
106773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * RecordThread.  It maintains local state on the relative position of the read
106873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * position of the RecordTrack compared with the RecordThread.
106973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     */
10706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class ResamplerBufferProvider : public AudioBufferProvider
10716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    {
10726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    public:
107373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        ResamplerBufferProvider(RecordTrack* recordTrack) :
107473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRecordTrack(recordTrack),
107573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRsmpInUnrel(0), mRsmpInFront(0) { }
10766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual ~ResamplerBufferProvider() { }
107773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
107873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
107973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // skipping any previous data read from the hal.
108073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void reset();
108173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
108273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        /* Synchronizes RecordTrack position with the RecordThread.
108373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Calculates available frames and handle overruns if the RecordThread
108473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * has advanced faster than the ResamplerBufferProvider has retrieved data.
108573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * TODO: why not do this for every getNextBuffer?
108673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *
108773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Parameters
108873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * framesAvailable:  pointer to optional output size_t to store record track
108973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *                   frames available.
109073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *      hasOverrun:  pointer to optional boolean, returns true if track has overrun.
109173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         */
109273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
109373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL);
109473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
10956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // AudioBufferProvider interface
10966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts);
10976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
10986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    private:
10996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        RecordTrack * const mRecordTrack;
110073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        size_t              mRsmpInUnrel;   // unreleased frames remaining from
110173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // most recent getNextBuffer
110273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // for debug only
110373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        int32_t             mRsmpInFront;   // next available frame
110473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // rolling counter that is never cleared
11056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    };
11066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
110797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    /* The RecordBufferConverter is used for format, channel, and sample rate
110897a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * conversion for a RecordTrack.
110997a893eb34f8687485c88eaf15917974a203f20bAndy Hung     *
111097a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * TODO: Self contained, so move to a separate file later.
111197a893eb34f8687485c88eaf15917974a203f20bAndy Hung     *
111297a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * RecordBufferConverter uses the convert() method rather than exposing a
111397a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * buffer provider interface; this is to save a memory copy.
111497a893eb34f8687485c88eaf15917974a203f20bAndy Hung     */
111597a893eb34f8687485c88eaf15917974a203f20bAndy Hung    class RecordBufferConverter
111697a893eb34f8687485c88eaf15917974a203f20bAndy Hung    {
111797a893eb34f8687485c88eaf15917974a203f20bAndy Hung    public:
111897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        RecordBufferConverter(
111997a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
112097a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t srcSampleRate,
112197a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
112297a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t dstSampleRate);
112397a893eb34f8687485c88eaf15917974a203f20bAndy Hung
112497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        ~RecordBufferConverter();
112597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
112697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        /* Converts input data from an AudioBufferProvider by format, channelMask,
112797a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * and sampleRate to a destination buffer.
112897a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *
112997a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * Parameters
113097a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *      dst:  buffer to place the converted data.
113197a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * provider:  buffer provider to obtain source data.
113297a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *   frames:  number of frames to convert
113397a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *
113497a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * Returns the number of frames converted.
113597a893eb34f8687485c88eaf15917974a203f20bAndy Hung         */
113697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t convert(void *dst, AudioBufferProvider *provider, size_t frames);
113797a893eb34f8687485c88eaf15917974a203f20bAndy Hung
113897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // returns NO_ERROR if constructor was successful
113997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        status_t initCheck() const {
114097a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mSrcChannelMask set on successful updateParameters
114197a893eb34f8687485c88eaf15917974a203f20bAndy Hung            return mSrcChannelMask != AUDIO_CHANNEL_INVALID ? NO_ERROR : NO_INIT;
114297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
114397a893eb34f8687485c88eaf15917974a203f20bAndy Hung
114497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // allows dynamic reconfigure of all parameters
114597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        status_t updateParameters(
114697a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
114797a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t srcSampleRate,
114897a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
114997a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t dstSampleRate);
115097a893eb34f8687485c88eaf15917974a203f20bAndy Hung
115197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // called to reset resampler buffers on record track discontinuity
115297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void reset() {
115397a893eb34f8687485c88eaf15917974a203f20bAndy Hung            if (mResampler != NULL) {
115497a893eb34f8687485c88eaf15917974a203f20bAndy Hung                mResampler->reset();
115597a893eb34f8687485c88eaf15917974a203f20bAndy Hung            }
115697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
115797a893eb34f8687485c88eaf15917974a203f20bAndy Hung
115897a893eb34f8687485c88eaf15917974a203f20bAndy Hung    private:
1159d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // format conversion when not using resampler
1160d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void convertNoResampler(void *dst, const void *src, size_t frames);
1161d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
1162d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // format conversion when using resampler; modifies src in-place
1163d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void convertResampler(void *dst, /*not-a-const*/ void *src, size_t frames);
116497a893eb34f8687485c88eaf15917974a203f20bAndy Hung
116597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // user provided information
116697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t mSrcChannelMask;
116797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_format_t       mSrcFormat;
116897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mSrcSampleRate;
116997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t mDstChannelMask;
117097a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_format_t       mDstFormat;
117197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mDstSampleRate;
117297a893eb34f8687485c88eaf15917974a203f20bAndy Hung
117397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // derived information
117497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mSrcChannelCount;
117597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mDstChannelCount;
117697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mDstFrameSize;
117797a893eb34f8687485c88eaf15917974a203f20bAndy Hung
117897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // format conversion buffer
117997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void                *mBuf;
118097a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mBufFrames;
118197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mBufFrameSize;
118297a893eb34f8687485c88eaf15917974a203f20bAndy Hung
118397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // resampler info
118497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        AudioResampler      *mResampler;
1185d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
1186d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mIsLegacyDownmix;  // legacy stereo to mono conversion needed
1187d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mIsLegacyUpmix;    // legacy mono to stereo conversion needed
1188d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mRequiresFloat;    // data processing requires float (e.g. resampler)
1189d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        PassthruBufferProvider *mInputConverterProvider;    // converts input to float
1190d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        int8_t               mIdxAry[sizeof(uint32_t) * 8]; // used for channel mask conversion
119197a893eb34f8687485c88eaf15917974a203f20bAndy Hung    };
119297a893eb34f8687485c88eaf15917974a203f20bAndy Hung
119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
119681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
1198d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
119972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    audio_devices_t inDevice,
120072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    bool systemReady
120146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
120246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
120346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
120446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1221e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
1222b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1223b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
12246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
12256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
123174935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                    size_t *pFrameCount,
123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int sessionId,
12337df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                    size_t *notificationFrames,
1234462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    int uid,
1235ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten                    IAudioFlinger::track_flags_t *flags,
123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
12379156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten                    status_t *status /*non-NULL*/);
123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              int triggerSession);
124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
124381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
1245a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual audio_stream_t* stream() const;
125081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual bool        checkForNewParameter_l(const String8& keyValuePair,
12531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                               status_t& status);
12541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        cacheParameters_l() {}
125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
125673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent    virtual void        ioConfigChanged(audio_io_config_event event);
12571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    createAudioPatch_l(const struct audio_patch *patch,
12581c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                           audio_patch_handle_t *handle);
12591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
126083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
126183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        addPatchRecord(const sp<PatchRecord>& record);
126283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        deletePatchRecord(const sp<PatchRecord>& record);
126383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1264deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten            void        readInputParameters_l();
12655f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t    getInputFramesLost();
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t hasAudioSession(int sessionId) const;
127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            KeyedVector<int, bool> sessionIds() const;
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12819b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
12826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool        hasFastCapture() const { return mFastCapture != 0; }
128383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void        getAudioPortConfig(struct audio_port_config *config);
12849b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
128793e471f620454f7de73d190521568b1e25879767Glenn Kasten            void    standbyIfNotAlreadyInStandby();
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
1290e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            void    inputStandBy();
129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
12942b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // mActiveTracks has dual roles:  it indicates the current active track(s), and
129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
12962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            SortedVector< sp<RecordTrack> >     mActiveTracks;
12972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // generation counter for mActiveTracks
12982b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            int                                 mActiveTracksGen;
129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
13009b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
13018594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            // resampler converts input at HAL Hz to output at AudioRecord client Hz
13025744661e85981f8a9456bf470e2761235fc026daAndy Hung            void                               *mRsmpInBuffer; //
13038594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFrames;  // size of resampler input in frames
13048594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
13056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
13066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // rolling index that is never cleared
13078594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int32_t                             mRsmpInRear;    // last filled frame + 1
13088594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
1311b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
1312b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            const sp<MemoryDealer>              mReadOnlyHeap;
13136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // one-time initialization, no locks required
1315b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten            sp<FastCapture>                     mFastCapture;   // non-0 if there is also
1316b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                                                                // a fast capture
131772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
13186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog thread
13196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // contents are not guaranteed to be consistent, no locks required
13216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureDumpState                mFastCaptureDumpState;
13226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
13236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME StateQueue observer and mutator dump fields
13246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
13256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog dump
13266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // accessible only within the threadLoop(), no locks required
13286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            //          mFastCapture->sq()      // for mutating and pushing state
13296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t     mFastCaptureFutex;      // for cold idle
13306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The HAL input source is treated as non-blocking,
13326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // but current implementation is blocking
13336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mInputSource;
13346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The source for the normal capture thread to read from: mInputSource or mPipeSource
13356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mNormalSource;
13366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
13376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
13386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Sink>                      mPipeSink;
13396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe source read by normal thread,
13406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
13416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mPipeSource;
13426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
13436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t                              mPipeFramesP2;
13446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the Pipe as IMemory, otherwise clear
13456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<IMemory>                         mPipeMemory;
13466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            static const size_t                 kFastCaptureLogSize = 4 * 1024;
13486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBLog::Writer>                   mFastCaptureNBLogWriter;
13496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
13506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool                                mFastTrackAvail;    // true if fast track available
135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
1352