Threads.h revision 6acd1d432f526ae9a055ddaece28bf93b474a776
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
326acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        OFFLOAD,            // Thread class is OffloadThread
336acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent        MMAP                // control thread for MMAP stream
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    static const char *threadTypeToString(type_t type);
3797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten
3881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
3972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                audio_devices_t outDevice, audio_devices_t inDevice, type_t type,
4072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady);
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~ThreadBase();
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    virtual status_t    readyToRun();
44cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpBase(int fd, const Vector<String16>& args);
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpEffectChains(int fd, const Vector<String16>& args);
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearPowerManager();
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // base for record and playback
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_IO,
531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_PRIO,
541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_SET_PARAMETER,
551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_CREATE_AUDIO_PATCH,
561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_RELEASE_AUDIO_PATCH,
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEventData: public RefBase {
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~ConfigEventData() {}
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEventData() {}
661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  2. Lock mLock
711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  4. sendConfigEvent_l() reads status from event->mStatus;
731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  5. sendConfigEvent_l() returns status
741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  6. Unlock
751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //
761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Parameter sequence by server: threadLoop calling processConfigEvents_l():
771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 1. Lock mLock
781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 2. If there is an entry in mConfigEvents proceed ...
791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 3. Read first entry in mConfigEvents
801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 4. Remove first entry from mConfigEvents
811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 5. Process
821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 6. Set event->mStatus
831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 7. event->mCond.signal
841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 8. Unlock
851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEvent: public RefBase {
8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const int mType; // event type e.g. CFG_EVENT_IO
931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex mLock;     // mutex associated with mCond
941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Condition mCond; // condition for status return
951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status_t mStatus; // status communicated to sender
961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        bool mWaitStatus; // true if sender is waiting for status
9772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEventData> mData;     // event specific parameter data
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
101e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ConfigEvent(int type, bool requiresSystemReady = false) :
10272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mType(type), mStatus(NO_ERROR), mWaitStatus(false),
10372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEventData : public ConfigEventData {
10781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1087c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        IoConfigEventData(audio_io_config_event event, pid_t pid) :
1097c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mEvent(event), mPid(pid) {}
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
11273e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            snprintf(buffer, size, "IO event: event %d\n", mEvent);
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11573e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const audio_io_config_event mEvent;
1167c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        const pid_t                 mPid;
11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEvent : public ConfigEvent {
12081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1217c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        IoConfigEvent(audio_io_config_event event, pid_t pid) :
1221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_IO) {
1237c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mData = new IoConfigEventData(event, pid);
1241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~IoConfigEvent() {}
1261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEventData : public ConfigEventData {
1291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) :
1311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mPid(pid), mTid(tid), mPrio(prio) {}
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio);
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEvent : public ConfigEvent {
1431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
1441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) :
14572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            ConfigEvent(CFG_EVENT_PRIO, true) {
1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new PrioConfigEventData(pid, tid, prio);
1471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~PrioConfigEvent() {}
1491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEventData : public ConfigEventData {
1521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
153e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit SetParameterConfigEventData(String8 keyValuePairs) :
1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mKeyValuePairs(keyValuePairs) {}
1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) {
1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const String8 mKeyValuePairs;
1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEvent : public ConfigEvent {
1641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
165e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit SetParameterConfigEvent(String8 keyValuePairs) :
1661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_SET_PARAMETER) {
1671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new SetParameterConfigEventData(keyValuePairs);
1681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mWaitStatus = true;
1691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~SetParameterConfigEvent() {}
1711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEventData : public ConfigEventData {
1741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData(const struct audio_patch patch,
1761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        audio_patch_handle_t handle) :
1771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mPatch(patch), mHandle(handle) {}
1781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
1811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        const struct audio_patch mPatch;
1841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
1851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEvent : public ConfigEvent {
1881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEvent(const struct audio_patch patch,
1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                    audio_patch_handle_t handle) :
1911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new CreateAudioPatchConfigEventData(patch, handle);
1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
1941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~CreateAudioPatchConfigEvent() {}
1961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEventData : public ConfigEventData {
1991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
200e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
2011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mHandle(handle) {}
2021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
2041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
2051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
2081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
2091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEvent : public ConfigEvent {
2111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
212e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
2131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
2141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new ReleaseAudioPatchConfigEventData(handle);
2151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
2161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~ReleaseAudioPatchConfigEvent() {}
2181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
222e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient(const PMDeathRecipient&);
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    PMDeathRecipient& operator = (const PMDeathRecipient&);
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
239f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                bool isDuplicating() const { return (mType == DUPLICATING); }
240f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
246463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t format() const { return mHALFormat; }
24783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                uint32_t channelCount() const { return mChannelCount; }
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
2499b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
2509b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
2514a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
2524a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
2534a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
2544a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
255bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
2601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                    status_t& status) = 0;
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
2647c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0) = 0;
2651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // sendConfigEvent_l() must be called with ThreadBase::mLock held
2661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // Can temporarily release the lock if waiting for a reply from
2671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // processConfigEvents_l().
2681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
2697c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                void        sendIoConfigEvent(audio_io_config_event event, pid_t pid = 0);
2707c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                void        sendIoConfigEvent_l(audio_io_config_event event, pid_t pid = 0);
27172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                void        sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio);
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio);
2731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
2741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
2751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                            audio_patch_handle_t *handle);
2761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
277021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                void        processConfigEvents_l();
2781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        cacheParameters_l() = 0;
2791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
2801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               audio_patch_handle_t *handle) = 0;
2811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
28283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config) = 0;
2831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2901dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     sp<StreamHalInterface> stream() const = 0;
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
296d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                    audio_session_t sessionId,
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
2990d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                                    status_t *status /*non-NULL*/,
3000d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                                    bool pinned);
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
3064c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    TRACK_SESSION = 0x2,    // the audio session corresponds to at least one
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
3084c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    FAST_SESSION = 0x4      // the audio session corresponds to at least one
3094c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                            // fast track
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
313d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<EffectChain> getEffectChain(audio_session_t sessionId);
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
315d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<EffectChain> getEffectChain_l(audio_session_t sessionId) const;
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
327bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
328bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
332d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<AudioFlinger::EffectModule> getEffect(audio_session_t sessionId, int effectId);
333d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<AudioFlinger::EffectModule> getEffect_l(audio_session_t sessionId, int effectId);
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
3390d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect, bool release = false);
3400d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                // disconnect an effect handle from module and destroy module if last handle
3410d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                void disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast);
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
3430f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     void detachAuxEffect_l(int effectId __unused) {}
3444c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // returns a combination of:
3454c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - EFFECT_SESSION if effects on this audio session exist in one chain
3464c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - TRACK_SESSION if tracks on this audio session exist
3474c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - FAST_SESSION if fast tracks on this audio session exist
3484c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual     uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
3494c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                uint32_t hasAudioSession(audio_session_t sessionId) const {
3504c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    Mutex::Autolock _l(mLock);
3514c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    return hasAudioSession_l(sessionId);
3524c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                }
3534c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
356d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                virtual uint32_t getStrategyForSession_l(audio_session_t sessionId __unused)
357d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                        { return 0; }
35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // suspend or restore effect according to the type of effect passed. a NULL
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // type pointer means suspend all effects in the session
36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended(const effect_uuid_t *type,
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        bool suspend,
363d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                        audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX);
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended/restored when an effect is enabled
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // or disabled
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                 bool enabled,
368d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                 audio_session_t sessionId =
369d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                        AUDIO_SESSION_OUTPUT_MIX);
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                   bool enabled,
372d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                   audio_session_t sessionId =
373d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                        AUDIO_SESSION_OUTPUT_MIX);
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t    setSyncEvent(const sp<SyncEvent>& event) = 0;
37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool        isValidSyncEvent(const sp<SyncEvent>& event) const = 0;
37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
378b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // Return a reference to a per-thread heap which can be used to allocate IMemory
379b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // objects that will be read-only to client processes, read/write to mediaserver,
380b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // and shared by all client processes of the thread.
381b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // The heap is per-thread rather than common across all threads, because
382b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // clients can't be trusted not to modify the offset of the IMemory they receive.
383b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                // If a thread does not have such a heap, this method returns 0.
384b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten                virtual sp<MemoryDealer>    readOnlyHeap() const { return 0; }
38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3866181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten                virtual sp<IMemory> pipeMemory() const { return 0; }
3876181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten
38872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        void systemReady();
38972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
3904c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
3914c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                virtual status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
3924c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                                               audio_session_t sessionId) = 0;
3934c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
3946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                        void        broadcast_l();
3956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutable     Mutex                   mLock;
39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // entry describing an effect being suspended in mSuspendedSessions keyed vector
40181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                class SuspendedSessionDesc : public RefBase {
40281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                public:
40381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    SuspendedSessionDesc() : mRefCount(0) {}
40481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    int mRefCount;          // number of active suspend requests
40681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    effect_uuid_t mType;    // effect type UUID
40781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
40881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
409dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                void        acquireWakeLock();
410dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                virtual void acquireWakeLock_l();
41181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock();
41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        releaseWakeLock_l();
413d01b0f18491c355d808a57cb272404480e69618fAndy Hung                void        updateWakeLockUids_l(const SortedVector<uid_t> &uids);
414462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                void        getPowerManager_l();
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
417d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                          audio_session_t sessionId);
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // updated mSuspendedSessions when an effect suspended or restored
41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
421d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                      audio_session_t sessionId);
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
42481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
425014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
426014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
4282ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     void        setMasterMono_l(bool mono __unused) { }
4292ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     bool        requireMonoBlend() { return false; }
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
4399b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
440deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // updated by PlaybackThread::readOutputParameters_l() or
441deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // RecordThread::readInputParameters_l()
44281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
44381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
44481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
445f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
44681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
44797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten                // not HAL frame size, this is for output sink (to pipe to fast mixer)
448463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mFormat;           // Source format for Recording and
449463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format for Playback.
450463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format may be different than
451463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // HAL format if Fastmixer is used.
452463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mHALFormat;
45370949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten                size_t                  mBufferSize;       // HAL buffer size for read() or write()
45481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                Vector< sp<ConfigEvent> >     mConfigEvents;
45672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                Vector< sp<ConfigEvent> >     mPendingConfigEvents; // events awaiting system ready
45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
45881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
4594944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                // and read by other threads without lock or barrier via standby(), outDevice()
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
46181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
46281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
46381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
4644944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                bool                    mStandby;     // Whether thread is currently in standby.
46581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
46681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
4677c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                audio_devices_t         mPrevOutDevice;   // previous output device
468e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent                audio_devices_t         mPrevInDevice;    // previous input device
469296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                struct audio_patch      mPatch;
470f59497bd3c190e087202043de5450ef06e92b27dGlenn Kasten                audio_source_t          mAudioSource;
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
47381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
475d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                static const int        kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
476d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                char                    mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
47881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
47981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
480d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                // list of suspended effects per session and per type. The first (outer) vector is
481d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                // keyed by session ID, the second (inner) by type UUID timeLow field
482d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
48381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
484ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
4859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
48672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool                    mSystemReady;
487818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung                ExtendedTimestamp       mTimestamp;
4886acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // A condition that must be evaluated by the thread loop has changed and
4896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // we must not wait for async write callback in the thread loop before evaluating it
4906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                    mSignalPending;
491dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
492dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // ActiveTracks is a sorted vector of track type T representing the
493dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // active tracks of threadLoop() to be considered by the locked prepare portion.
494dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // ActiveTracks should be accessed with the ThreadBase lock held.
495dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
496dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // During processing and I/O, the threadLoop does not hold the lock;
497dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // hence it does not directly use ActiveTracks.  Care should be taken
498dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // to hold local strong references or defer removal of tracks
499dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // if the threadLoop may still be accessing those tracks due to mix, etc.
500dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
501dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // This class updates power information appropriately.
502dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
503dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
504dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                template <typename T>
505dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                class ActiveTracks {
506dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                public:
507dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ActiveTracks()
508dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        : mActiveTracksGeneration(0)
509dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        , mLastActiveTracksGeneration(0)
510dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    { }
511dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
512dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ~ActiveTracks() {
513dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        ALOGW_IF(!mActiveTracks.isEmpty(),
514dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                "ActiveTracks should be empty in destructor");
515dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
516dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // returns the last track added (even though it may have been
517dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // subsequently removed from ActiveTracks).
518dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    //
519dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Used for DirectOutputThread to ensure a flush is called when transitioning
520dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // to a new track (even though it may be on the same session).
521dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Used for OffloadThread to ensure that volume and mixer state is
522dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // taken from the latest track added.
523dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    //
524dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // The latest track is saved with a weak pointer to prevent keeping an
525dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // otherwise useless track alive. Thus the function will return nullptr
526dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // if the latest track has subsequently been removed and destroyed.
527dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    sp<T> getLatest() {
528dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mLatestActiveTrack.promote();
529dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
530dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
531dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // SortedVector methods
532dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         add(const sp<T> &track);
533dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         remove(const sp<T> &track);
534dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    size_t          size() const {
535dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.size();
536dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
537dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         indexOf(const sp<T>& item) {
538dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.indexOf(item);
539dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
540dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    sp<T>           operator[](size_t index) const {
541dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks[index];
542dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
543dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    typename SortedVector<sp<T>>::iterator begin() {
544dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.begin();
545dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
546dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    typename SortedVector<sp<T>>::iterator end() {
547dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.end();
548dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
549dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
550dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Due to Binder recursion optimization, clear() and updatePowerState()
551dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // cannot be called from a Binder thread because they may call back into
552dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // the original calling process (system server) for BatteryNotifier
553dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // (which requires a Java environment that may not be present).
554dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Hence, call clear() and updatePowerState() only from the
555dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // ThreadBase thread.
556dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    void            clear();
557dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // periodically called in the threadLoop() to update power state uids.
558dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    void            updatePowerState(sp<ThreadBase> thread, bool force = false);
559dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
560dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                private:
561d01b0f18491c355d808a57cb272404480e69618fAndy Hung                    SortedVector<uid_t> getWakeLockUids() {
562d01b0f18491c355d808a57cb272404480e69618fAndy Hung                        SortedVector<uid_t> wakeLockUids;
563dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        for (const sp<T> &track : mActiveTracks) {
564dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            wakeLockUids.add(track->uid());
565dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        }
566dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return wakeLockUids; // moved by underlying SharedBuffer
567dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
568dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
569dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    std::map<uid_t, std::pair<ssize_t /* previous */, ssize_t /* current */>>
570dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                        mBatteryCounter;
571dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    SortedVector<sp<T>> mActiveTracks;
572dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    int                 mActiveTracksGeneration;
573dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    int                 mLastActiveTracksGeneration;
574dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    wp<T>               mLatestActiveTrack; // latest track added to ActiveTracks
575dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                };
57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5786acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass VolumeInterface {
5796acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent public:
5806acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
5816acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual ~VolumeInterface() {}
5826acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
5836acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setMasterVolume(float value) = 0;
5846acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setMasterMute(bool muted) = 0;
5856acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setStreamVolume(audio_stream_type_t stream, float value) = 0;
5866acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setStreamMute(audio_stream_type_t stream, bool muted) = 0;
5876acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual float       streamVolume(audio_stream_type_t stream) const = 0;
5886acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
5896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
5906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
59181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
5926acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback,
5936acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    public VolumeInterface {
59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
59581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
59781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
59981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
60081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
601bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
602bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
603bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
60481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
60581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
608e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
609e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
610e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
611e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // handled for offloaded tracks
612e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
613e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackStartupRetriesOffload = 100;
614e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackStopRetriesOffload = 2;
615ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent    // 14 tracks max per client allows for 2 misbehaving application leaving 4 available tracks.
616ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent    static const uint32_t kMaxTracksPerUid = 14;
617e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent
61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
619e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady);
62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
62181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
62381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
62981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6304c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
6314c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                                       audio_session_t sessionId);
6324c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
63381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
63581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
637bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
638bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
63981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
640bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
64181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
64281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
64481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
64581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
64681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
648bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
649bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
6501dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    // StreamOutHalInterfaceCallback implementation
6511dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onWriteReady();
6521dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onDrainReady();
6531dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onError();
6541dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov
6553b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
6563b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
657bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
658bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
659bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
660bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
6614c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
6624527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George                void        onAsyncError(); // error reported by AsyncCallbackThread
66381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
66681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
667646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent    virtual     bool        keepWakeLock() const { return true; }
668dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual     void        acquireWakeLock_l() {
669dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                ThreadBase::acquireWakeLock_l();
670dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                mActiveTracks.updatePowerState(this, true /* force */);
671dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            }
672646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent
67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6826acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // VolumeInterface
6836acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterVolume(float value);
6846acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterMute(bool muted);
6856acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
6866acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
6876acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     float       streamVolume(audio_stream_type_t stream) const;
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                uint32_t sampleRate,
69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
69574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
697d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                audio_session_t sessionId,
698050677873c10d4da308ac222f8533c96cca3207eEric Laurent                                audio_output_flags_t *flags,
69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
7001f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung                                uid_t uid,
70120b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                status_t *status /*non-NULL*/,
70220b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                audio_port_handle_t portId);
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
7061dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov                virtual sp<StreamHalInterface> stream() const;
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
70981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
71081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
71181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
71281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
71381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
71881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
71981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
7227c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
723377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
724010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency.
725010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // Consider also removing and passing an explicit mMainBuffer initialization
726010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // parameter to AF::PlaybackThread::Track::Track().
727010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                int16_t     *mixBuffer() const {
728010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    return reinterpret_cast<int16_t *>(mSinkBuffer); };
72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
731e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track>& track,
73281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
733e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track>& track,
73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
7384c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
739d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                virtual uint32_t getStrategyForSession_l(audio_session_t sessionId);
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
744fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
745fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
74613084621072ea2e4c34e2a9d79793c621d9bf005Eric Laurent                        bool     invalidateTracks_l(audio_stream_type_t streamType);
74705317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George                virtual void     invalidateTracks(audio_stream_type_t streamType);
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7499b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
7509b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
75183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                status_t    getTimestamp_l(AudioTimestamp& timestamp);
75283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
75383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        addPatchTrack(const sp<PatchTrack>& track);
75483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        deletePatchTrack(const sp<PatchTrack>& track);
75583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
75683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
757accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
759deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // updated by readOutputParameters_l()
7609b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
7619b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
76208fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    bool                            mThreadThrottle;     // throttle the thread processing
76340eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleTimeMs; // throttle time for MIXER threads
76440eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleEndMs;  // notify once per throttling
76508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    uint32_t                        mHalfBufferMs;       // half the buffer size in milliseconds
76608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
767010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    void*                           mSinkBuffer;         // frame size aligned sink buffer
76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // TODO:
77098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Rearrange the buffer info into a struct/class with
77198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // clear, copy, construction, destruction methods.
77298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
77398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer also has associated with it:
77498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
77598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBufferSize: Sink Buffer Size
77698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mFormat: Sink Buffer Format
77798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
77869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Mixer Buffer (mMixerBuffer*)
77969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    //
78069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // In the case of floating point or multichannel data, which is not in the
78169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink format, it is required to accumulate in a higher precision or greater channel count
78269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // buffer before downmixing or data conversion to the sink buffer.
78369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
78469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
78569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferEnabled;
78669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
78769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
78869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
78969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    void*                           mMixerBuffer;
79069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
79169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
79269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    size_t                          mMixerBufferSize;
79369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
79469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
79569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    audio_format_t                  mMixerBufferFormat;
79669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
79769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
79869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // when mMixerBuffer contains valid data after mixing.
79969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferValid;
80069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
80198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Effects Buffer (mEffectsBuffer*)
80298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
80398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // In the case of effects data, which is not in the sink format,
80498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // it is required to accumulate in a different buffer before data conversion
80598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // to the sink buffer.
80698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
80798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
80898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferEnabled;
80998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
81098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
81198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
81298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    void*                           mEffectBuffer;
81398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
81498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
81598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    size_t                          mEffectBufferSize;
81698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
81798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
81898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    audio_format_t                  mEffectBufferFormat;
81998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
82098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
82198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // when mEffectsBuffer contains valid data after mixing.
82298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
82398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // When this is set, all mixer data is routed into the effects buffer
82498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // for any processing (including output processing).
82598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferValid;
82698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
834818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung    int64_t                         mBytesWritten;
835c54b1ffc92b8ad27608a8af21033d7cab33cb3a0Andy Hung    int64_t                         mFramesWritten; // not reset on standby
836238fa3deff26d7e4d9e81bd0a88c936f16226c4eAndy Hung    int64_t                         mSuspendedFrames; // not reset on standby
83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
844dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    ActiveTracks<Track>     mActiveTracks;
84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Allocate a track name for a given channel mask.
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //   Returns name >= 0 if successful, -1 on failure.
848ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent    virtual int             getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
849ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent                                           audio_session_t sessionId, uid_t uid) = 0;
85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void            deleteTrackName_l(int name) = 0;
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
8721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                   audio_patch_handle_t *handle);
8731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
8741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
8756fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                bool        usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL)
8766fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && mHwSupportsPause
8776fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
8780f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent
879ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent                uint32_t    trackCountForUid_l(uid_t uid);
880ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread& operator = (const PlaybackThread&);
88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
888bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
891deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    void        readOutputParameters_l();
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector< sp<Track> >       mTracks;
897223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT];
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
907ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyTimeNs;
90825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    size_t                          mSinkBufferSize;
90981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
911ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mActiveSleepTimeUs;
912ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mIdleSleepTimeUs;
91381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
914ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mSleepTimeUs;
91581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
91781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
91881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
92381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
927ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyDelayNs;
92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
937bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
9383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
9393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
9403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
9413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
9423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
9433b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
9443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
9453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
9463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
9473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
9483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
9493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
9503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
951bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
952bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
96046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
96446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
966ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten    static const size_t     kFastMixerLogSize = 4 * 1024;
9679e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
9682148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
9692148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    // Do not call from a sched_fifo thread as it uses a system time call
9702148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    // and obtains a local mutex.
9712148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    class LocalLog {
9722148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    public:
9732148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        void log(const char *fmt, ...) {
9742148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            va_list val;
9752148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            va_start(val, fmt);
9762148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
9772148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            // format to buffer
9782148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            char buffer[512];
9792148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            int length = vsnprintf(buffer, sizeof(buffer), fmt, val);
9802148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            if (length >= (signed)sizeof(buffer)) {
9812148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                length = sizeof(buffer) - 1;
9822148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            }
9832148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
9842148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            // strip out trailing newline
9852148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            while (length > 0 && buffer[length - 1] == '\n') {
9862148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                buffer[--length] = 0;
9872148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            }
9882148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
9892148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            // store in circular array
9902148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            AutoMutex _l(mLock);
9912148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            mLog.emplace_back(
9922148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    std::make_pair(systemTime(SYSTEM_TIME_REALTIME), std::string(buffer)));
9932148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            if (mLog.size() > kLogSize) {
9942148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                mLog.pop_front();
9952148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            }
9962148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
9972148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            va_end(val);
9982148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        }
9992148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
10002148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        void dump(int fd, const Vector<String16>& args, const char *prefix = "") {
10012148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            if (!AudioFlinger::dumpTryLock(mLock)) return; // a local lock, shouldn't happen
10022148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            if (mLog.size() > 0) {
10032148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                bool dumpAll = false;
10042148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                for (const auto &arg : args) {
10052148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    if (arg == String16("--locallog")) {
10062148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                        dumpAll = true;
10072148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    }
10082148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                }
10092148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
10102148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                dprintf(fd, "Local Log:\n");
10112148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                auto it = mLog.begin();
1012f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                if (!dumpAll) {
1013f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                    const size_t lines =
1014f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                            (size_t)property_get_int32("audio.locallog.lines", kLogPrint);
1015f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                    if (mLog.size() > lines) {
1016f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                        it += (mLog.size() - lines);
1017f28bcf5c057dd9738a09f3cb367cf0b44087135dAndy Hung                    }
10182148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                }
10192148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                for (; it != mLog.end(); ++it) {
10202148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    const int64_t ns = it->first;
10212148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    const int ns_per_sec = 1000000000;
10222148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    const time_t sec = ns / ns_per_sec;
10232148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    struct tm tm;
10242148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    localtime_r(&sec, &tm);
10252148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
10262148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                    dprintf(fd, "%s%02d-%02d %02d:%02d:%02d.%03d %s\n",
10272148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                            prefix,
10282148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                            tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
10292148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                            tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
10302148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                            (int)(ns % ns_per_sec / 1000000),
10312148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                            it->second.c_str());
10322148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung                }
10332148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            }
10342148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung            mLock.unlock();
10352148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        }
10362148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
10372148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    private:
10382148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        Mutex mLock;
10392148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        static const size_t kLogSize = 256; // full history
10402148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        static const size_t kLogPrint = 32; // default print history
10412148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung        std::deque<std::pair<int64_t, std::string>> mLog;
10422148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung    } mLocalLog;
10432148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
10460f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
1052d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwSupportsPause;
1053d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwPaused;
1054d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mFlushPending;
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
106372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady,
106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
10701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1075ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
1076ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent                                           audio_session_t sessionId, uid_t uid);
107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1082dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual void acquireWakeLock_l() {
1083dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung        PlaybackThread::acquireWakeLock_l();
1084818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung        if (hasFastMixer()) {
1085818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung            mFastMixer->setBoottimeOffset(
1086818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung                    mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1087818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung        }
1088818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung    }
1089818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung
109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
1091bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1098054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
1099054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                   audio_patch_handle_t *handle);
1100054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
1101054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
11054d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten                sp<FastMixer>     mFastMixer;     // non-0 if there is also a fast mixer
110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
110981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
111181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
111281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
111381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
111481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
111881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11202ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                std::atomic_bool mMasterMono;
112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
11224d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    virtual     bool        hasFastMixer() const { return mFastMixer != 0; }
112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
1124dc2c50bad491d2c0c8a2efc0e24491076701c63cGlenn Kasten                              ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
112783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
11282ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hungprotected:
11292ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     void       setMasterMono_l(bool mono) {
11302ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               mMasterMono.store(mono);
11312ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               if (mFastMixer != nullptr) { /* hasFastMixer() */
11322ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                                   mFastMixer->setMasterMono(mMasterMono);
11332ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               }
11342ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                           }
11352ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                // the FastMixer performs mono blend if it exists.
113603c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten                // Blending with limiter is not idempotent,
113703c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten                // and blending without limiter is idempotent but inefficient to do twice.
11382ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     bool       requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1145e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                       audio_io_handle_t id, audio_devices_t device, bool systemReady);
114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
114981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
11511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
1152e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
1155ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent    virtual     int         getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format,
1156ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent                                           audio_session_t sessionId, uid_t uid);
115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        deleteTrackName_l(int name);
115881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
115981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
116081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
116181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
116381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
116681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1167d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     void        threadLoop_exit();
1168d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     bool        shouldStandby_l();
116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
117043b4dcc660e6da96285e4672ae371070ab845401Phil Burk    virtual     void        onAddNewTrack_l();
117143b4dcc660e6da96285e4672ae371070ab845401Phil Burk
117281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // volumes last sent to audio HAL with stream->set_volume()
117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mLeftVolFloat;
117481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float mRightVolFloat;
117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1176bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
117772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type,
1178e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                        bool systemReady);
11795850c4c8eecbe0db3cee8511a4f82cb443e27d08Eric Laurent    void processVolume_l(Track *track, bool lastTrack);
1180bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
118343b4dcc660e6da96285e4672ae371070ab845401Phil Burk
118443b4dcc660e6da96285e4672ae371070ab845401Phil Burk    wp<Track>               mPreviousTrack;         // used to detect track switch
118543b4dcc660e6da96285e4672ae371070ab845401Phil Burk
118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1190bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
1191bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
1192bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1193bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1194e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                        audio_io_handle_t id, uint32_t device, bool systemReady);
11956a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
1196e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
1197bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1198bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
1199bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
1200bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1201bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
1202bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1203bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
1204bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
120505317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George    virtual     void        invalidateTracks(audio_stream_type_t streamType);
1206bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1207de0613d0ad8b8fb9d19a27185ae4d307a5b7fb9dEric Laurent    virtual     bool        keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
1208646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent
1209bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
1210bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
1211bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
1212646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent    bool        mKeepWakeLock;          // keep wake lock while waiting for write callback
1213f804475807407442d5596ab7378ed07d50664063Andy Hung    uint64_t    mOffloadUnderrunPosition; // Current frame position for offloaded playback
1214f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // used and valid only during underrun.  ~0 if
1215f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // no underrun has occurred during playback and
1216f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // is not reset on standby.
1217bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
1218bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1219bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
1220bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
1221bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1222e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh    explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
1223bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1224bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
1225bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1226bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
1227bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
1228bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1229bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
1230bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
1231bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1232bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
12333b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
12343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
12353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
12363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
12374527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George            void        setAsyncError();
1238bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1239bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
12404de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
12413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
12423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
12433b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
12444de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
12453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
12463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
12473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
12484de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
12494de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
12504de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
12514527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George    bool                       mAsyncError;
1252bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
1253bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
125772e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                      audio_io_handle_t id, bool systemReady);
125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1273bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
128181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
128581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
12936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase
129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class RecordTrack;
129873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
129973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    /* The ResamplerBufferProvider is used to retrieve recorded input data from the
130073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * RecordThread.  It maintains local state on the relative position of the read
130173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * position of the RecordTrack compared with the RecordThread.
130273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     */
13036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class ResamplerBufferProvider : public AudioBufferProvider
13046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    {
13056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    public:
1306e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ResamplerBufferProvider(RecordTrack* recordTrack) :
130773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRecordTrack(recordTrack),
130873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRsmpInUnrel(0), mRsmpInFront(0) { }
13096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual ~ResamplerBufferProvider() { }
131073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
131173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
131273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // skipping any previous data read from the hal.
131373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void reset();
131473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
131573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        /* Synchronizes RecordTrack position with the RecordThread.
131673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Calculates available frames and handle overruns if the RecordThread
131773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * has advanced faster than the ResamplerBufferProvider has retrieved data.
131873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * TODO: why not do this for every getNextBuffer?
131973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *
132073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Parameters
132173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * framesAvailable:  pointer to optional output size_t to store record track
132273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *                   frames available.
132373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *      hasOverrun:  pointer to optional boolean, returns true if track has overrun.
132473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         */
132573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
132673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL);
132773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
13286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // AudioBufferProvider interface
1329d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer);
13306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
13316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    private:
13326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        RecordTrack * const mRecordTrack;
133373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        size_t              mRsmpInUnrel;   // unreleased frames remaining from
133473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // most recent getNextBuffer
133573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // for debug only
133673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        int32_t             mRsmpInFront;   // next available frame
133773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // rolling counter that is never cleared
13386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    };
13396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
134097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    /* The RecordBufferConverter is used for format, channel, and sample rate
134197a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * conversion for a RecordTrack.
134297a893eb34f8687485c88eaf15917974a203f20bAndy Hung     *
134397a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * TODO: Self contained, so move to a separate file later.
134497a893eb34f8687485c88eaf15917974a203f20bAndy Hung     *
134597a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * RecordBufferConverter uses the convert() method rather than exposing a
134697a893eb34f8687485c88eaf15917974a203f20bAndy Hung     * buffer provider interface; this is to save a memory copy.
134797a893eb34f8687485c88eaf15917974a203f20bAndy Hung     */
134897a893eb34f8687485c88eaf15917974a203f20bAndy Hung    class RecordBufferConverter
134997a893eb34f8687485c88eaf15917974a203f20bAndy Hung    {
135097a893eb34f8687485c88eaf15917974a203f20bAndy Hung    public:
135197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        RecordBufferConverter(
135297a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
135397a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t srcSampleRate,
135497a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
135597a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t dstSampleRate);
135697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
135797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        ~RecordBufferConverter();
135897a893eb34f8687485c88eaf15917974a203f20bAndy Hung
135997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        /* Converts input data from an AudioBufferProvider by format, channelMask,
136097a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * and sampleRate to a destination buffer.
136197a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *
136297a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * Parameters
136397a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *      dst:  buffer to place the converted data.
136497a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * provider:  buffer provider to obtain source data.
136597a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *   frames:  number of frames to convert
136697a893eb34f8687485c88eaf15917974a203f20bAndy Hung         *
136797a893eb34f8687485c88eaf15917974a203f20bAndy Hung         * Returns the number of frames converted.
136897a893eb34f8687485c88eaf15917974a203f20bAndy Hung         */
136997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t convert(void *dst, AudioBufferProvider *provider, size_t frames);
137097a893eb34f8687485c88eaf15917974a203f20bAndy Hung
137197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // returns NO_ERROR if constructor was successful
137297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        status_t initCheck() const {
137397a893eb34f8687485c88eaf15917974a203f20bAndy Hung            // mSrcChannelMask set on successful updateParameters
137497a893eb34f8687485c88eaf15917974a203f20bAndy Hung            return mSrcChannelMask != AUDIO_CHANNEL_INVALID ? NO_ERROR : NO_INIT;
137597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
137697a893eb34f8687485c88eaf15917974a203f20bAndy Hung
137797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // allows dynamic reconfigure of all parameters
137897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        status_t updateParameters(
137997a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t srcChannelMask, audio_format_t srcFormat,
138097a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t srcSampleRate,
138197a893eb34f8687485c88eaf15917974a203f20bAndy Hung                audio_channel_mask_t dstChannelMask, audio_format_t dstFormat,
138297a893eb34f8687485c88eaf15917974a203f20bAndy Hung                uint32_t dstSampleRate);
138397a893eb34f8687485c88eaf15917974a203f20bAndy Hung
138497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // called to reset resampler buffers on record track discontinuity
138597a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void reset() {
138697a893eb34f8687485c88eaf15917974a203f20bAndy Hung            if (mResampler != NULL) {
138797a893eb34f8687485c88eaf15917974a203f20bAndy Hung                mResampler->reset();
138897a893eb34f8687485c88eaf15917974a203f20bAndy Hung            }
138997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        }
139097a893eb34f8687485c88eaf15917974a203f20bAndy Hung
139197a893eb34f8687485c88eaf15917974a203f20bAndy Hung    private:
1392d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // format conversion when not using resampler
1393d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void convertNoResampler(void *dst, const void *src, size_t frames);
1394d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
1395d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        // format conversion when using resampler; modifies src in-place
1396d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        void convertResampler(void *dst, /*not-a-const*/ void *src, size_t frames);
139797a893eb34f8687485c88eaf15917974a203f20bAndy Hung
139897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // user provided information
139997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t mSrcChannelMask;
140097a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_format_t       mSrcFormat;
140197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mSrcSampleRate;
140297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_channel_mask_t mDstChannelMask;
140397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        audio_format_t       mDstFormat;
140497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mDstSampleRate;
140597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
140697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // derived information
140797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mSrcChannelCount;
140897a893eb34f8687485c88eaf15917974a203f20bAndy Hung        uint32_t             mDstChannelCount;
140997a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mDstFrameSize;
141097a893eb34f8687485c88eaf15917974a203f20bAndy Hung
141197a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // format conversion buffer
141297a893eb34f8687485c88eaf15917974a203f20bAndy Hung        void                *mBuf;
141397a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mBufFrames;
141497a893eb34f8687485c88eaf15917974a203f20bAndy Hung        size_t               mBufFrameSize;
141597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
141697a893eb34f8687485c88eaf15917974a203f20bAndy Hung        // resampler info
141797a893eb34f8687485c88eaf15917974a203f20bAndy Hung        AudioResampler      *mResampler;
1418d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung
1419d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mIsLegacyDownmix;  // legacy stereo to mono conversion needed
1420d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mIsLegacyUpmix;    // legacy mono to stereo conversion needed
1421d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        bool                 mRequiresFloat;    // data processing requires float (e.g. resampler)
1422d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        PassthruBufferProvider *mInputConverterProvider;    // converts input to float
1423d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung        int8_t               mIdxAry[sizeof(uint32_t) * 8]; // used for channel mask conversion
142497a893eb34f8687485c88eaf15917974a203f20bAndy Hung    };
142597a893eb34f8687485c88eaf15917974a203f20bAndy Hung
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
1431d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
143272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    audio_devices_t inDevice,
143372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    bool systemReady
143446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
143546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
143646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
143746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1454e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
1455b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1456b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
14576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
14586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    uint32_t sampleRate,
146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
146474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                    size_t *pFrameCount,
1465d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                    audio_session_t sessionId,
14667df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten                    size_t *notificationFrames,
14671f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung                    uid_t uid,
1468050677873c10d4da308ac222f8533c96cca3207eEric Laurent                    audio_input_flags_t *flags,
146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
147020b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                    status_t *status /*non-NULL*/,
147120b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                    audio_port_handle_t portId);
147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
1475d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                              audio_session_t triggerSession);
147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
1479a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
148081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
14831dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov            virtual sp<StreamHalInterface> stream() const;
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual bool        checkForNewParameter_l(const String8& keyValuePair,
14871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                               status_t& status);
14881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        cacheParameters_l() {}
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
14907c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
14911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    createAudioPatch_l(const struct audio_patch *patch,
14921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                           audio_patch_handle_t *handle);
14931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
149483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
149583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        addPatchRecord(const sp<PatchRecord>& record);
149683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        deletePatchRecord(const sp<PatchRecord>& record);
149783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1498deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten            void        readInputParameters_l();
14995f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t    getInputFramesLost();
150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
15034c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
1508d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            KeyedVector<audio_session_t, bool> sessionIds() const;
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
151281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15159b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
15166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool        hasFastCapture() const { return mFastCapture != 0; }
151783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void        getAudioPortConfig(struct audio_port_config *config);
15189b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
15194c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
15204c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                                   audio_session_t sessionId);
15214c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
1522dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual void        acquireWakeLock_l() {
1523dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            ThreadBase::acquireWakeLock_l();
1524dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            mActiveTracks.updatePowerState(this, true /* force */);
1525dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        }
1526dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
152993e471f620454f7de73d190521568b1e25879767Glenn Kasten            void    standbyIfNotAlreadyInStandby();
153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
1532e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            void    inputStandBy();
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
15362b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // mActiveTracks has dual roles:  it indicates the current active track(s), and
153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
1538dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung            ActiveTracks<RecordTrack>           mActiveTracks;
1539dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
15419b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
15428594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            // resampler converts input at HAL Hz to output at AudioRecord client Hz
15431b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten            void                               *mRsmpInBuffer;  // size = mRsmpInFramesOA
15448594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFrames;  // size of resampler input in frames
15458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
15461b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten            size_t                              mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
15476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
15486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // rolling index that is never cleared
15498594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int32_t                             mRsmpInRear;    // last filled frame + 1
15508594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
1553b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
1554b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            const sp<MemoryDealer>              mReadOnlyHeap;
15556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // one-time initialization, no locks required
1557b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten            sp<FastCapture>                     mFastCapture;   // non-0 if there is also
1558b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                                                                // a fast capture
155972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
15606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog thread
15616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // contents are not guaranteed to be consistent, no locks required
15636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureDumpState                mFastCaptureDumpState;
15646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
15656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME StateQueue observer and mutator dump fields
15666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
15676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog dump
15686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // accessible only within the threadLoop(), no locks required
15706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            //          mFastCapture->sq()      // for mutating and pushing state
15716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t     mFastCaptureFutex;      // for cold idle
15726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The HAL input source is treated as non-blocking,
15746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // but current implementation is blocking
15756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mInputSource;
15766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The source for the normal capture thread to read from: mInputSource or mPipeSource
15776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mNormalSource;
15786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
15796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
15806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Sink>                      mPipeSink;
15816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe source read by normal thread,
15826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
15836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mPipeSource;
15846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
15856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t                              mPipeFramesP2;
15866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the Pipe as IMemory, otherwise clear
15876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<IMemory>                         mPipeMemory;
15886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            static const size_t                 kFastCaptureLogSize = 4 * 1024;
15906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBLog::Writer>                   mFastCaptureNBLogWriter;
15916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool                                mFastTrackAvail;    // true if fast track available
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
15946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapThread : public ThreadBase
15966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
15976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent public:
15986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15996acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent#include "MmapTracks.h"
16006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16016acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
16026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, sp<StreamHalInterface> stream,
16036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
16046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapThread();
16056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16066acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        configure(const audio_attributes_t *attr,
16076acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_stream_type_t streamType,
16086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_session_t sessionId,
16096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      const sp<MmapStreamCallback>& callback,
16106acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_port_handle_t portId);
16116acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        disconnect();
16136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16146acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // MmapStreamInterface
16156acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t createMmapBuffer(int32_t minSizeFrames,
16166acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      struct audio_mmap_buffer_info *info);
16176acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t getMmapPosition(struct audio_mmap_position *position);
16186acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t start(const MmapStreamInterface::Client& client, audio_port_handle_t *handle);
16196acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t stop(audio_port_handle_t handle);
16206acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16216acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // RefBase
16226acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        onFirstRef();
16236acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16246acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // Thread virtuals
16256acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        threadLoop();
16266acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16276acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        threadLoop_exit();
16286acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        threadLoop_standby();
16296acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16306acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    initCheck() const { return (mHalStream == 0) ? NO_INIT : NO_ERROR; }
16316acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     size_t      frameCount() const { return mFrameCount; }
16326acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
16336acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                                    status_t& status);
16346acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     String8     getParameters(const String8& keys);
16356acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
16366acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        readHalParameters_l();
16376acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        cacheParameters_l() {}
16386acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
16396acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                               audio_patch_handle_t *handle);
16406acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
16416acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
16426acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16436acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     sp<StreamHalInterface> stream() const { return mHalStream; }
16446acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    addEffectChain_l(const sp<EffectChain>& chain);
16456acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     size_t      removeEffectChain_l(const sp<EffectChain>& chain);
16466acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
16476acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                                               audio_session_t sessionId);
16486acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16496acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     uint32_t    hasAudioSession_l(audio_session_t sessionId) const;
16506acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    setSyncEvent(const sp<SyncEvent>& event);
16516acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        isValidSyncEvent(const sp<SyncEvent>& event) const;
16526acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16536acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        checkSilentMode_l() {}
16546acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        processVolume_l() {}
16556acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        checkInvalidTracks_l();
16566acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16576acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     audio_stream_type_t streamType() { return AUDIO_STREAM_DEFAULT; }
16586acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16596acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        invalidateTracks(audio_stream_type_t streamType __unused) {}
16606acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16616acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        dump(int fd, const Vector<String16>& args);
16626acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
16636acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        dumpTracks(int fd, const Vector<String16>& args);
16646acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16656acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        isOutput() const = 0;
16666acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16676acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent protected:
16686acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16696acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_attributes_t      mAttr;
16706acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_session_t         mSessionId;
16716acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_port_handle_t     mPortId;
16726acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16736acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                sp<MmapStreamCallback>  mCallback;
16746acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                sp<StreamHalInterface>  mHalStream;
16756acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                sp<DeviceHalInterface>  mHalDevice;
16766acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioHwDevice* const    mAudioHwDev;
16776acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                ActiveTracks<MmapTrack> mActiveTracks;
16786acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
16796acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16806acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapPlaybackThread : public MmapThread, public VolumeInterface
16816acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
16826acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16836acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentpublic:
16846acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapPlaybackThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
16856acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, AudioStreamOut *output,
16866acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
16876acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapPlaybackThread() {}
16886acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        configure(const audio_attributes_t *attr,
16906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_stream_type_t streamType,
16916acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_session_t sessionId,
16926acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      const sp<MmapStreamCallback>& callback,
16936acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_port_handle_t portId);
16946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamOut* clearOutput();
16966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // VolumeInterface
16986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterVolume(float value);
16996acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterMute(bool muted);
17006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
17016acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
17026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     float       streamVolume(audio_stream_type_t stream) const;
17036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
17056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17066acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        invalidateTracks(audio_stream_type_t streamType);
17076acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     audio_stream_type_t streamType() { return mStreamType; }
17096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        checkSilentMode_l();
17106acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        processVolume_l();
17116acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
17136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17146acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        isOutput() const { return true; }
17156acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17166acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentprotected:
17176acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17186acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_stream_type_t         mStreamType;
17196acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                float                       mMasterVolume;
17206acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                float                       mStreamVolume;
17216acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                        mMasterMute;
17226acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                        mStreamMute;
17236acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                float                       mHalVolFloat;
17246acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamOut*             mOutput;
17256acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
17266acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17276acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapCaptureThread : public MmapThread
17286acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
17296acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17306acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentpublic:
17316acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapCaptureThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
17326acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, AudioStreamIn *input,
17336acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
17346acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapCaptureThread() {}
17356acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17366acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamIn* clearInput();
17376acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17386acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool           isOutput() const { return false; }
17396acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17406acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentprotected:
17416acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17426acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamIn*  mInput;
17436acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
1744