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
341bfe09a0b1755a79abd32b41c0dd433b88fc260cGlenn Kasten        // If you add any values here, also update ThreadBase::threadTypeToString()
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3797b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten    static const char *threadTypeToString(type_t type);
3897b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten
3981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
4072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                audio_devices_t outDevice, audio_devices_t inDevice, type_t type,
4172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady);
4281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~ThreadBase();
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    virtual status_t    readyToRun();
45cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpBase(int fd, const Vector<String16>& args);
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void dumpEffectChains(int fd, const Vector<String16>& args);
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void clearPowerManager();
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // base for record and playback
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum {
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        CFG_EVENT_IO,
541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_PRIO,
551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        CFG_EVENT_SET_PARAMETER,
561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_CREATE_AUDIO_PATCH,
571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CFG_EVENT_RELEASE_AUDIO_PATCH,
5881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEventData: public RefBase {
611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~ConfigEventData() {}
631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) = 0;
651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ConfigEventData() {}
671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Config event sequence by client if status needed (e.g binder thread calling setParameters()):
701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  1. create SetParameterConfigEvent. This sets mWaitStatus in config event
711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  2. Lock mLock
721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal
731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  4. sendConfigEvent_l() reads status from event->mStatus;
741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  5. sendConfigEvent_l() returns status
751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //  6. Unlock
761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //
771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // Parameter sequence by server: threadLoop calling processConfigEvents_l():
781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 1. Lock mLock
791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 2. If there is an entry in mConfigEvents proceed ...
801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 3. Read first entry in mConfigEvents
811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 4. Remove first entry from mConfigEvents
821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 5. Process
831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 6. Set event->mStatus
841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 7. event->mCond.signal
851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // 8. Unlock
861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class ConfigEvent: public RefBase {
8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual ~ConfigEvent() {}
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        void dump(char *buffer, size_t size) { mData->dump(buffer, size); }
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const int mType; // event type e.g. CFG_EVENT_IO
941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex mLock;     // mutex associated with mCond
951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Condition mCond; // condition for status return
961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status_t mStatus; // status communicated to sender
971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        bool mWaitStatus; // true if sender is waiting for status
9872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent        bool mRequiresSystemReady; // true if must wait for system ready to enter event queue
991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEventData> mData;     // event specific parameter data
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    protected:
102e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ConfigEvent(int type, bool requiresSystemReady = false) :
10372e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mType(type), mStatus(NO_ERROR), mWaitStatus(false),
10472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            mRequiresSystemReady(requiresSystemReady), mData(NULL) {}
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEventData : public ConfigEventData {
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1097c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        IoConfigEventData(audio_io_config_event event, pid_t pid) :
1107c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mEvent(event), mPid(pid) {}
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
11373e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent            snprintf(buffer, size, "IO event: event %d\n", mEvent);
11481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
11581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11673e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent        const audio_io_config_event mEvent;
1177c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        const pid_t                 mPid;
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class IoConfigEvent : public ConfigEvent {
12181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
1227c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent        IoConfigEvent(audio_io_config_event event, pid_t pid) :
1231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_IO) {
1247c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent            mData = new IoConfigEventData(event, pid);
1251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~IoConfigEvent() {}
1271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEventData : public ConfigEventData {
1301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
13183f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov        PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
13283f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov            mPid(pid), mTid(tid), mPrio(prio), mForApp(forApp) {}
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual  void dump(char *buffer, size_t size) {
13583f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov            snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d, for app? %d\n",
13683f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov                    mPid, mTid, mPrio, mForApp);
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mPid;
14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const pid_t mTid;
14181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const int32_t mPrio;
14283f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov        const bool mForApp;
14381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
14481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class PrioConfigEvent : public ConfigEvent {
1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
14783f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov        PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp) :
14872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent            ConfigEvent(CFG_EVENT_PRIO, true) {
14983f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov            mData = new PrioConfigEventData(pid, tid, prio, forApp);
1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~PrioConfigEvent() {}
1521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEventData : public ConfigEventData {
1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
156e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit SetParameterConfigEventData(String8 keyValuePairs) :
1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mKeyValuePairs(keyValuePairs) {}
1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual  void dump(char *buffer, size_t size) {
1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string());
1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        const String8 mKeyValuePairs;
1641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    class SetParameterConfigEvent : public ConfigEvent {
1671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    public:
168e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit SetParameterConfigEvent(String8 keyValuePairs) :
1691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ConfigEvent(CFG_EVENT_SET_PARAMETER) {
1701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mData = new SetParameterConfigEventData(keyValuePairs);
1711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mWaitStatus = true;
1721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
1731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        virtual ~SetParameterConfigEvent() {}
1741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    };
1751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
1761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEventData : public ConfigEventData {
1771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData(const struct audio_patch patch,
1791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        audio_patch_handle_t handle) :
1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mPatch(patch), mHandle(handle) {}
1811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
1831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
1841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        const struct audio_patch mPatch;
1871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
1881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
1891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class CreateAudioPatchConfigEvent : public ConfigEvent {
1911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEvent(const struct audio_patch patch,
1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                    audio_patch_handle_t handle) :
1941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) {
1951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new CreateAudioPatchConfigEventData(patch, handle);
1961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
1971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
1981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~CreateAudioPatchConfigEvent() {}
1991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
2001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEventData : public ConfigEventData {
2021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
203e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) :
2041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mHandle(handle) {}
2051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual  void dump(char *buffer, size_t size) {
2071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            snprintf(buffer, size, "Patch handle: %u\n", mHandle);
2081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_patch_handle_t mHandle;
2111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
2121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
2131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    class ReleaseAudioPatchConfigEvent : public ConfigEvent {
2141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    public:
215e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) :
2161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) {
2171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mData = new ReleaseAudioPatchConfigEventData(handle);
2181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mWaitStatus = true;
2191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
2201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        virtual ~ReleaseAudioPatchConfigEvent() {}
2211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    };
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    class PMDeathRecipient : public IBinder::DeathRecipient {
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    public:
225e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit    PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {}
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     ~PMDeathRecipient() {}
22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // IBinder::DeathRecipient
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        virtual     void        binderDied(const wp<IBinder>& who);
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    private:
232bf49308851509d38dbf3f57d9682a819d9f983c9Mikhail Naganov        DISALLOW_COPY_AND_ASSIGN(PMDeathRecipient);
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wp<ThreadBase> mThread;
23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const = 0;
23881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // static externally-visible
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t      type() const { return mType; }
241f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent                bool isDuplicating() const { return (mType == DUPLICATING); }
242f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id() const { return mId;}
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // dynamic externally-visible
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    sampleRate() const { return mSampleRate; }
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t channelMask() const { return mChannelMask; }
248463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t format() const { return mHALFormat; }
24983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                uint32_t channelCount() const { return mChannelCount; }
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects,
2519b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten                // and returns the [normal mix] buffer's frame count.
2529b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const = 0;
2534a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
2544a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                // Return's the HAL's frame count i.e. fast mixer buffer size.
2554a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten                size_t      frameCountHAL() const { return mFrameCount; }
2564a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten
257bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                size_t      frameSize() const { return mFrameSize; }
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be "virtual status_t requestExitAndWait()" and override same
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // method in Thread, but Thread::requestExitAndWait() is not yet virtual.
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        exit();
2621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
2631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                    status_t& status) = 0;
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    setParameters(const String8& keyValuePairs);
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys) = 0;
2667c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0) = 0;
2671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // sendConfigEvent_l() must be called with ThreadBase::mLock held
2681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // Can temporarily release the lock if waiting for a reply from
2691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                // processConfigEvents_l().
2701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendConfigEvent_l(sp<ConfigEvent>& event);
2717c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                void        sendIoConfigEvent(audio_io_config_event event, pid_t pid = 0);
2727c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                void        sendIoConfigEvent_l(audio_io_config_event event, pid_t pid = 0);
27383f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov                void        sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp);
27483f042776b131b149f803fff6ab184ae2c4d98cdMikhail Naganov                void        sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio, bool forApp);
2751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status_t    sendSetParameterConfigEvent_l(const String8& keyValuePair);
2761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendCreateAudioPatchConfigEvent(const struct audio_patch *patch,
2771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                            audio_patch_handle_t *handle);
2781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                status_t    sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle);
279021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent                void        processConfigEvents_l();
2801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     void        cacheParameters_l() = 0;
2811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
2821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               audio_patch_handle_t *handle) = 0;
2831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle) = 0;
28483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config) = 0;
2851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // see note at declaration of mStandby, mOutDevice and mInDevice
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        standby() const { return mStandby; }
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t outDevice() const { return mOutDevice; }
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t inDevice() const { return mInDevice; }
291293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung                audio_devices_t getDevice() const { return isOutput() ? mOutDevice : mInDevice; }
292293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung
293293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung    virtual     bool        isOutput() const = 0;
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2951dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     sp<StreamHalInterface> stream() const = 0;
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<EffectHandle> createEffect_l(
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<AudioFlinger::Client>& client,
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    const sp<IEffectClient>& effectClient,
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int32_t priority,
301d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                    audio_session_t sessionId,
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    effect_descriptor_t *desc,
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    int *enabled,
3040d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                                    status_t *status /*non-NULL*/,
3050d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                                    bool pinned);
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return values for hasAudioSession (bit field)
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                enum effect_state {
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    EFFECT_SESSION = 0x1,   // the audio session corresponds to at least one
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // effect
3114c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    TRACK_SESSION = 0x2,    // the audio session corresponds to at least one
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            // track
3134c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    FAST_SESSION = 0x4      // the audio session corresponds to at least one
3144c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                            // fast track
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                };
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect chain corresponding to session Id.
318d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<EffectChain> getEffectChain(audio_session_t sessionId);
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same as getEffectChain() but must be called with ThreadBase mutex locked
320d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<EffectChain> getEffectChain_l(audio_session_t sessionId) const;
32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add an effect chain to the chain list (mEffectChains)
32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t addEffectChain_l(const sp<EffectChain>& chain) = 0;
32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove an effect chain from the chain list (mEffectChains)
32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0;
32581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lock all effect chains Mutexes. Must be called before releasing the
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ThreadBase mutex before processing the mixer and effects. This guarantees the
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // integrity of the chains during the process.
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Also sets the parameter 'effectChains' to current value of mEffectChains.
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains);
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // unlock effect chains after process
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains);
332bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // get a copy of mEffectChains vector
333bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; };
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // set audio mode to all effect chains
33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setMode(audio_mode_t mode);
33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // get effect module with corresponding ID on specified audio session
337d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<AudioFlinger::EffectModule> getEffect(audio_session_t sessionId, int effectId);
338d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                sp<AudioFlinger::EffectModule> getEffect_l(audio_session_t sessionId, int effectId);
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // add and effect module. Also creates the effect chain is none exists for
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the effects audio session
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status_t addEffect_l(const sp< EffectModule>& effect);
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // remove and effect module. Also removes the effect chain is this was the last
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // effect
3440d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                void removeEffect_l(const sp< EffectModule>& effect, bool release = false);
3450d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                // disconnect an effect handle from module and destroy module if last handle
3460d5a2ed0a05a2bf337c68edb54f24c60e18032c1Eric Laurent                void disconnectEffectHandle(EffectHandle *handle, bool unpinIfLast);
34781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // detach all tracks connected to an auxiliary effect
3480f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     void detachAuxEffect_l(int effectId __unused) {}
3494c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // returns a combination of:
3504c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - EFFECT_SESSION if effects on this audio session exist in one chain
3514c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - TRACK_SESSION if tracks on this audio session exist
3524c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                // - FAST_SESSION if fast tracks on this audio session exist
3534c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual     uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0;
3544c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                uint32_t hasAudioSession(audio_session_t sessionId) const {
3554c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    Mutex::Autolock _l(mLock);
3564c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                    return hasAudioSession_l(sessionId);
3574c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                }
3584c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the value returned by default implementation is not important as the
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // strategy is only meaningful for PlaybackThread which implements this method
361d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                virtual uint32_t getStrategyForSession_l(audio_session_t sessionId __unused)
362d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                        { return 0; }
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
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();
415d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent                // suspend or restore effects of the specified type (or all if type is NULL)
416d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent                // on a given session. The number of suspend requests is counted and restore
417d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent                // occurs when all suspend requests are cancelled.
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void setEffectSuspended_l(const effect_uuid_t *type,
41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                          bool suspend,
420d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                          audio_session_t sessionId);
421d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent                // updated mSuspendedSessions when an effect is suspended or restored
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        updateSuspendedSessions_l(const effect_uuid_t *type,
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                      bool suspend,
424d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                                      audio_session_t sessionId);
42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // check if some effects must be suspended when an effect chain is added
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain);
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
428069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard                // sends the metadata of the active tracks to the HAL
429069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard    virtual     void        updateMetadata_l() = 0;
430069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
431014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                String16 getWakeLockTag();
432014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit() { }
4342ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     void        setMasterMono_l(bool mono __unused) { }
4352ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     bool        requireMonoBlend() { return false; }
43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for mEffectChains
43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
43981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const type_t            mType;
44081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Used by parameters, config events, addTrack_l, exit
44281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Condition               mWaitWorkCV;
44381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
44481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<AudioFlinger>  mAudioFlinger;
4459b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
446deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // updated by PlaybackThread::readOutputParameters_l() or
447deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten                // RecordThread::readInputParameters_l()
44881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t                mSampleRate;
44981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameCount;       // output HAL, direct output, record
45081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_channel_mask_t    mChannelMask;
451f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten                uint32_t                mChannelCount;
45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t                  mFrameSize;
45397b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten                // not HAL frame size, this is for output sink (to pipe to fast mixer)
454463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mFormat;           // Source format for Recording and
455463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format for Playback.
456463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // Sink format may be different than
457463be250de73907965faa6a216c00312bf81e049Andy Hung                                                           // HAL format if Fastmixer is used.
458463be250de73907965faa6a216c00312bf81e049Andy Hung                audio_format_t          mHALFormat;
45970949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten                size_t                  mBufferSize;       // HAL buffer size for read() or write()
46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                Vector< sp<ConfigEvent> >     mConfigEvents;
46272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                Vector< sp<ConfigEvent> >     mPendingConfigEvents; // events awaiting system ready
46381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
46481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // These fields are written and read by thread itself without lock or barrier,
4654944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                // and read by other threads without lock or barrier via standby(), outDevice()
46681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // and inDevice().
46781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Because of the absence of a lock or barrier, any other thread that reads
46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // these fields must use the information in isolation, or be prepared to deal
46981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // with possibility that it might be inconsistent with other information.
4704944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                bool                    mStandby;     // Whether thread is currently in standby.
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mOutDevice;   // output device
47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t         mInDevice;    // input device
4737c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent                audio_devices_t         mPrevOutDevice;   // previous output device
474e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent                audio_devices_t         mPrevInDevice;    // previous input device
475296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent                struct audio_patch      mPatch;
476f59497bd3c190e087202043de5450ef06e92b27dGlenn Kasten                audio_source_t          mAudioSource;
47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
47881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const audio_io_handle_t mId;
47981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                Vector< sp<EffectChain> > mEffectChains;
48081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
481d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                static const int        kThreadNameLength = 16; // prctl(PR_SET_NAME) limit
482d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten                char                    mThreadName[kThreadNameLength]; // guaranteed NUL-terminated
48381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IPowerManager>       mPowerManager;
48481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<IBinder>             mWakeLockToken;
48581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                const sp<PMDeathRecipient> mDeathRecipient;
486d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                // list of suspended effects per session and per type. The first (outer) vector is
487d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                // keyed by session ID, the second (inner) by type UUID timeLow field
488d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent                // Updated by updateSuspendedSessions_l() only.
489d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > >
49081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        mSuspendedSessions;
4912a6a945d65c986dcc779f70f18afde1d7a74397bSanna Catherine de Treville Wager                // TODO: add comment and adjust size as needed
492ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten                static const size_t     kLogSize = 4 * 1024;
4939e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                sp<NBLog::Writer>       mNBLogWriter;
49472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool                    mSystemReady;
495818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung                ExtendedTimestamp       mTimestamp;
4966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // A condition that must be evaluated by the thread loop has changed and
4976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // we must not wait for async write callback in the thread loop before evaluating it
4986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                    mSignalPending;
499dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
500dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // ActiveTracks is a sorted vector of track type T representing the
501dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // active tracks of threadLoop() to be considered by the locked prepare portion.
502dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // ActiveTracks should be accessed with the ThreadBase lock held.
503dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
504dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // During processing and I/O, the threadLoop does not hold the lock;
505dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // hence it does not directly use ActiveTracks.  Care should be taken
506dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // to hold local strong references or defer removal of tracks
507dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // if the threadLoop may still be accessing those tracks due to mix, etc.
508dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
509dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                // This class updates power information appropriately.
510dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                //
511dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
512dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                template <typename T>
513dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                class ActiveTracks {
514dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                public:
5152c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung                    explicit ActiveTracks(SimpleLog *localLog = nullptr)
516dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        : mActiveTracksGeneration(0)
517dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        , mLastActiveTracksGeneration(0)
5182c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung                        , mLocalLog(localLog)
519dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    { }
520dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
521dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ~ActiveTracks() {
522dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        ALOGW_IF(!mActiveTracks.isEmpty(),
523dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                "ActiveTracks should be empty in destructor");
524dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
525dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // returns the last track added (even though it may have been
526dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // subsequently removed from ActiveTracks).
527dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    //
528dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Used for DirectOutputThread to ensure a flush is called when transitioning
529dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // to a new track (even though it may be on the same session).
530dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Used for OffloadThread to ensure that volume and mixer state is
531dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // taken from the latest track added.
532dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    //
533dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // The latest track is saved with a weak pointer to prevent keeping an
534dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // otherwise useless track alive. Thus the function will return nullptr
535dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // if the latest track has subsequently been removed and destroyed.
536dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    sp<T> getLatest() {
537dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mLatestActiveTrack.promote();
538dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
539dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
540dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // SortedVector methods
541dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         add(const sp<T> &track);
542dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         remove(const sp<T> &track);
543dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    size_t          size() const {
544dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.size();
545dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
546dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    ssize_t         indexOf(const sp<T>& item) {
547dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.indexOf(item);
548dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
549dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    sp<T>           operator[](size_t index) const {
550dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks[index];
551dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
552dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    typename SortedVector<sp<T>>::iterator begin() {
553dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.begin();
554dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
555dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    typename SortedVector<sp<T>>::iterator end() {
556dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return mActiveTracks.end();
557dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
558dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
559dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Due to Binder recursion optimization, clear() and updatePowerState()
560dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // cannot be called from a Binder thread because they may call back into
561dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // the original calling process (system server) for BatteryNotifier
562dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // (which requires a Java environment that may not be present).
563dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // Hence, call clear() and updatePowerState() only from the
564dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // ThreadBase thread.
565dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    void            clear();
566dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    // periodically called in the threadLoop() to update power state uids.
567dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    void            updatePowerState(sp<ThreadBase> thread, bool force = false);
568dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
569c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard                    /** @return true if one or move active tracks was added or removed since the
570c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard                     *          last time this function was called or the vector was created. */
571069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard                    bool            readAndClearHasChanged();
572069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
573dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                private:
5742c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung                    void            logTrack(const char *funcName, const sp<T> &track) const;
5752c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung
576d01b0f18491c355d808a57cb272404480e69618fAndy Hung                    SortedVector<uid_t> getWakeLockUids() {
577d01b0f18491c355d808a57cb272404480e69618fAndy Hung                        SortedVector<uid_t> wakeLockUids;
578dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        for (const sp<T> &track : mActiveTracks) {
579dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            wakeLockUids.add(track->uid());
580dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        }
581dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        return wakeLockUids; // moved by underlying SharedBuffer
582dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    }
583dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
584dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    std::map<uid_t, std::pair<ssize_t /* previous */, ssize_t /* current */>>
585dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                        mBatteryCounter;
586dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    SortedVector<sp<T>> mActiveTracks;
587dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    int                 mActiveTracksGeneration;
588dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    int                 mLastActiveTracksGeneration;
589dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                    wp<T>               mLatestActiveTrack; // latest track added to ActiveTracks
5902c6c3bb76b2fa18ed3536331a1700250bd04c7ceAndy Hung                    SimpleLog * const   mLocalLog;
591c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard                    // If the vector has changed since last call to readAndClearHasChanged
592069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard                    bool                mHasChanged = false;
593dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                };
594293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung
595293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung                SimpleLog mLocalLog;
59681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
59781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass VolumeInterface {
5996acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent public:
6006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
6016acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual ~VolumeInterface() {}
6026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
6036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setMasterVolume(float value) = 0;
6046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setMasterMute(bool muted) = 0;
6056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setStreamVolume(audio_stream_type_t stream, float value) = 0;
6066acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual void        setStreamMute(audio_stream_type_t stream, bool muted) = 0;
6076acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual float       streamVolume(audio_stream_type_t stream) const = 0;
6086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
6096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
6106acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread ---
6126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback,
6136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    public VolumeInterface {
61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h"
61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    enum mixer_state {
61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_IDLE,             // no active tracks
62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MIXER_TRACKS_ENABLED,   // at least one active track, but no track has any data ready
621bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_TRACKS_READY,      // at least one active track, and at least one track has data
622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_TRACK,      // drain currently playing track
623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        MIXER_DRAIN_ALL,        // fully drain the hardware
62481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // standby mode does not have an enum value
62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend by audio policy manager is orthogonal to mixer state
62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    };
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
628e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // retry count before removing active track in case of underrun on offloaded thread:
629e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // we need to make sure that AudioTrack client has enough time to send large buffers
630e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is
631e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    // handled for offloaded tracks
632e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackRetriesOffload = 20;
633e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackStartupRetriesOffload = 100;
634e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent    static const int8_t kMaxTrackStopRetriesOffload = 2;
6358ed196ac0f4aa8ae811c012dfa6f596fddebf1bfAndy Hung    static constexpr uint32_t kMaxTracksPerUid = 40;
6361bc088a918d7038603230637d640941953b314d4Andy Hung    static constexpr size_t kMaxTracks = 256;
637e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent
6381bb9082596c9cac2995dc9b4465a7c6d5666d099rago    // Maximum delay (in nanoseconds) for upcoming buffers in suspend mode, otherwise
6391bb9082596c9cac2995dc9b4465a7c6d5666d099rago    // if delay is greater, the estimated time for timeLoopNextNs is reset.
6401bb9082596c9cac2995dc9b4465a7c6d5666d099rago    // This allows for catch-up to be done for small delays, while resetting the estimate
6411bb9082596c9cac2995dc9b4465a7c6d5666d099rago    // for initial conditions or large delays.
6421bb9082596c9cac2995dc9b4465a7c6d5666d099rago    static const nsecs_t kMaxNextBufferDelayNs = 100000000;
6431bb9082596c9cac2995dc9b4465a7c6d5666d099rago
64481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
645e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                   audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady);
64681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~PlaybackThread();
64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
64881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        dump(int fd, const Vector<String16>& args);
64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
65181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        threadLoop();
65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
65381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        onFirstRef();
65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6564c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
6574c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                                       audio_session_t sessionId);
6584c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
65981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that were lifted up out of threadLoop()
66181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix() = 0;
66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime() = 0;
663bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_drain();
66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
666bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
66981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // prepareTracks_l reads and writes mActiveTracks, and returns
67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the pending set of tracks to remove via Vector 'tracksToRemove'.  The caller
67181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is responsible for clearing or destroying this Vector later on, when it
67281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // is safe to do so. That will drop the final ref count and destroy the tracks.
67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0;
674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                void        removeTracks_l(const Vector< sp<Track> >& tracksToRemove);
675bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
6761dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    // StreamOutHalInterfaceCallback implementation
6771dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onWriteReady();
6781dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onDrainReady();
6791dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov    virtual     void        onError();
6801dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov
6813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetWriteBlocked(uint32_t sequence);
6823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                void        resetDraining(uint32_t sequence);
683bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
684bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
685bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
686bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        shouldStandby_l();
6874c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    virtual     void        onAddNewTrack_l();
6884527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George                void        onAsyncError(); // error reported by AsyncCallbackThread
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // ThreadBase virtuals
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        preExit();
69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
693646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent    virtual     bool        keepWakeLock() const { return true; }
694dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual     void        acquireWakeLock_l() {
695dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                ThreadBase::acquireWakeLock_l();
696dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                                mActiveTracks.updatePowerState(this, true /* force */);
697dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            }
698646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent
69981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
70081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     status_t    initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; }
70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // return estimated latency in milliseconds, as reported by HAL
70481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency() const;
70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // same, but lock must already be held
70681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    latency_l() const;
70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // VolumeInterface
7096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterVolume(float value);
7106acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterMute(bool muted);
7116acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
7126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
7136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     float       streamVolume(audio_stream_type_t stream) const;
71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track>   createTrack_l(
71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<AudioFlinger::Client>& client,
71781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_stream_type_t streamType,
7181f564acca544826daaf7dca44e39cec6016b82fdKevin Rocard                                const audio_attributes_t& attr,
71921da647792c0b78ab3943be0f32066015d5e8c34Eric Laurent                                uint32_t *sampleRate,
72081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_format_t format,
72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                audio_channel_mask_t channelMask,
72274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                                size_t *pFrameCount,
72321da647792c0b78ab3943be0f32066015d5e8c34Eric Laurent                                size_t *pNotificationFrameCount,
72421da647792c0b78ab3943be0f32066015d5e8c34Eric Laurent                                uint32_t notificationsPerBuffer,
72521da647792c0b78ab3943be0f32066015d5e8c34Eric Laurent                                float speed,
72681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                const sp<IMemory>& sharedBuffer,
727d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                                audio_session_t sessionId,
728050677873c10d4da308ac222f8533c96cca3207eEric Laurent                                audio_output_flags_t *flags,
72981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                pid_t tid,
7301f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung                                uid_t uid,
73120b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                status_t *status /*non-NULL*/,
73220b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                                audio_port_handle_t portId);
73381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* getOutput() const;
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* clearOutput();
7361dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov                virtual sp<StreamHalInterface> stream() const;
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // a very large number of suspend() will eventually wraparound, but unlikely
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        suspend() { (void) android_atomic_inc(&mSuspended); }
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        restore()
74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                {
74281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // if restore() is done without suspend(), get back into
74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    // range so that the next suspend() will operate correctly
74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    if (android_atomic_dec(&mSuspended) <= 0) {
74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                        android_atomic_release_store(0, &mSuspended);
74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                    }
74781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                }
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        isSuspended() const
74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { return android_atomic_acquire_load(&mSuspended) > 0; }
75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     String8     getParameters(const String8& keys);
7527c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
753377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                status_t    getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames);
754010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // Consider also removing and passing an explicit mMainBuffer initialization
755010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                // parameter to AF::PlaybackThread::Track::Track().
75694a1ee822686e920a33e312f4032f991731aea07rago                effect_buffer_t *sinkBuffer() const {
75794a1ee822686e920a33e312f4032f991731aea07rago                    return reinterpret_cast<effect_buffer_t *>(mSinkBuffer); };
75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void detachAuxEffect_l(int effectId);
760e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh                status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track>& track,
76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
762e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh                status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track>& track,
76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        int EffectId);
76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
7674c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
768d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                virtual uint32_t getStrategyForSession_l(audio_session_t sessionId);
76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual status_t setSyncEvent(const sp<SyncEvent>& event);
77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
773fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten
774fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten                // called with AudioFlinger lock held
77513084621072ea2e4c34e2a9d79793c621d9bf005Eric Laurent                        bool     invalidateTracks_l(audio_stream_type_t streamType);
77605317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George                virtual void     invalidateTracks(audio_stream_type_t streamType);
77781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7789b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual     size_t      frameCount() const { return mNormalFrameCount; }
7799b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
78083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                status_t    getTimestamp_l(AudioTimestamp& timestamp);
78183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
78283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        addPatchTrack(const sp<PatchTrack>& track);
78383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                void        deletePatchTrack(const sp<PatchTrack>& track);
78483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
78583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
786accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent
78710cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung                // Return the asynchronous signal wait time.
78810cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung    virtual     int64_t     computeWaitTimeNs_l() const { return INT64_MAX; }
78910cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung
790293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung    virtual     bool        isOutput() const override { return true; }
791293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung
7921bc088a918d7038603230637d640941953b314d4Andy Hung                // returns true if the track is allowed to be added to the thread.
7931bc088a918d7038603230637d640941953b314d4Andy Hung    virtual     bool        isTrackAllowed_l(
7941bc088a918d7038603230637d640941953b314d4Andy Hung                                    audio_channel_mask_t channelMask __unused,
7951bc088a918d7038603230637d640941953b314d4Andy Hung                                    audio_format_t format __unused,
7961bc088a918d7038603230637d640941953b314d4Andy Hung                                    audio_session_t sessionId __unused,
7971bc088a918d7038603230637d640941953b314d4Andy Hung                                    uid_t uid) const {
7981bc088a918d7038603230637d640941953b314d4Andy Hung                                return trackCountForUid_l(uid) < PlaybackThread::kMaxTracksPerUid
7991bc088a918d7038603230637d640941953b314d4Andy Hung                                       && mTracks.size() < PlaybackThread::kMaxTracks;
8001bc088a918d7038603230637d640941953b314d4Andy Hung                            }
8011bc088a918d7038603230637d640941953b314d4Andy Hung
80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
803deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // updated by readOutputParameters_l()
8049b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    size_t                          mNormalFrameCount;  // normal mixer and effects
8059b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
80608fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    bool                            mThreadThrottle;     // throttle the thread processing
80740eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleTimeMs; // throttle time for MIXER threads
80840eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung    uint32_t                        mThreadThrottleEndMs;  // notify once per throttling
80908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung    uint32_t                        mHalfBufferMs;       // half the buffer size in milliseconds
81008fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung
811010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    void*                           mSinkBuffer;         // frame size aligned sink buffer
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // TODO:
81498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Rearrange the buffer info into a struct/class with
81598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // clear, copy, construction, destruction methods.
81698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
81798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer also has associated with it:
81898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
81998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBufferSize: Sink Buffer Size
82098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mFormat: Sink Buffer Format
82198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
82269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Mixer Buffer (mMixerBuffer*)
82369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    //
82469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // In the case of floating point or multichannel data, which is not in the
82569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink format, it is required to accumulate in a higher precision or greater channel count
82669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // buffer before downmixing or data conversion to the sink buffer.
82769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
82869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer.
82969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferEnabled;
83069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
83169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
83269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
83369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    void*                           mMixerBuffer;
83469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
83569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize.
83669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    size_t                          mMixerBufferSize;
83769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
83869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only.
83969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    audio_format_t                  mMixerBufferFormat;
84069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
84169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
84269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // when mMixerBuffer contains valid data after mixing.
84369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    bool                            mMixerBufferValid;
84469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
84598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Effects Buffer (mEffectsBuffer*)
84698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
84798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // In the case of effects data, which is not in the sink format,
84898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // it is required to accumulate in a different buffer before data conversion
84998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // to the sink buffer.
85098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
85198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer.
85298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferEnabled;
85398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
85498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Storage, 32 byte aligned (may make this alignment a requirement later).
85598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames.
85698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    void*                           mEffectBuffer;
85798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
85898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize.
85998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    size_t                          mEffectBufferSize;
86098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
86198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only.
86298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    audio_format_t                  mEffectBufferFormat;
86398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
86498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // An internal flag set to true by MixerThread::prepareTracks_l()
86598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // when mEffectsBuffer contains valid data after mixing.
86698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    //
86798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // When this is set, all mixer data is routed into the effects buffer
86898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // for any processing (including output processing).
86998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    bool                            mEffectBufferValid;
87098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // suspend count, > 0 means suspended.  While suspended, the thread continues to pull from
87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // tracks and mix, but doesn't write to HAL.  A2DP and SCO HAL implementations can't handle
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // concurrent use of both of them, so Audio Policy Service suspends one of the threads to
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // workaround that restriction.
87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // 'volatile' means accessed via atomic operations and no lock.
87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    volatile int32_t                mSuspended;
87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
878818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung    int64_t                         mBytesWritten;
879c54b1ffc92b8ad27608a8af21033d7cab33cb3a0Andy Hung    int64_t                         mFramesWritten; // not reset on standby
880238fa3deff26d7e4d9e81bd0a88c936f16226c4eAndy Hung    int64_t                         mSuspendedFrames; // not reset on standby
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mMasterMute is in both PlaybackThread and in AudioFlinger.  When a
88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // PlaybackThread needs to find out if master-muted, it checks it's local
88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // copy rather than the one in AudioFlinger.  This optimization saves a lock.
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mMasterMute;
88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
888dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    ActiveTracks<Track>     mActiveTracks;
88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Time to sleep between cycles when:
89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        activeSleepTimeUs() const;      // mixer state MIXER_TRACKS_ENABLED
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        idleSleepTimeUs() const = 0;    // mixer state MIXER_IDLE
89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual uint32_t        suspendSleepTimeUs() const = 0; // audio policy manager suspended us
89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write()
89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // No sleep in standby mode; waits on a condition
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Code snippets that are temporarily lifted up out of threadLoop() until the merge
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        checkSilentMode_l();
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Non-trivial for DUPLICATING only
90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks() { }
90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks() { }
90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Cache various calculated values, at threadLoop() entry and after a parameter change
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
9101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                   audio_patch_handle_t *handle);
9111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
9121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
9136fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                bool        usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL)
9146fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && mHwSupportsPause
9156fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk                                    && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); }
9160f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent
9171bc088a918d7038603230637d640941953b314d4Andy Hung                uint32_t    trackCountForUid_l(uid_t uid) const;
918ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent
91981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    friend class AudioFlinger;      // for numerous
92281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
923bf49308851509d38dbf3f57d9682a819d9f983c9Mikhail Naganov    DISALLOW_COPY_AND_ASSIGN(PlaybackThread);
92481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
92581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t    addTrack_l(const sp<Track>& track);
926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool        destroyTrack_l(const sp<Track>& track);
92781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<Track>& track);
92881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
929deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    void        readOutputParameters_l();
930c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard    void        updateMetadata_l() final;
931c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard    virtual void sendMetadataToBackend_l(const StreamOutHalInterface::SourceMetadata& metadata);
93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void dumpInternals(int fd, const Vector<String16>& args);
93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9361bc088a918d7038603230637d640941953b314d4Andy Hung    // The Tracks class manages names for all tracks
9371bc088a918d7038603230637d640941953b314d4Andy Hung    // added and removed from the Thread.
9381bc088a918d7038603230637d640941953b314d4Andy Hung    template <typename T>
9391bc088a918d7038603230637d640941953b314d4Andy Hung    class Tracks {
9401bc088a918d7038603230637d640941953b314d4Andy Hung    public:
9411bc088a918d7038603230637d640941953b314d4Andy Hung        Tracks(bool saveDeletedTrackNames) :
9421bc088a918d7038603230637d640941953b314d4Andy Hung            mSaveDeletedTrackNames(saveDeletedTrackNames) { }
9431bc088a918d7038603230637d640941953b314d4Andy Hung
9441bc088a918d7038603230637d640941953b314d4Andy Hung        // SortedVector methods
9451bc088a918d7038603230637d640941953b314d4Andy Hung        ssize_t         add(const sp<T> &track);
9461bc088a918d7038603230637d640941953b314d4Andy Hung        ssize_t         remove(const sp<T> &track);
9471bc088a918d7038603230637d640941953b314d4Andy Hung        size_t          size() const {
9481bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks.size();
9491bc088a918d7038603230637d640941953b314d4Andy Hung        }
9501bc088a918d7038603230637d640941953b314d4Andy Hung        bool            isEmpty() const {
9511bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks.isEmpty();
9521bc088a918d7038603230637d640941953b314d4Andy Hung        }
9531bc088a918d7038603230637d640941953b314d4Andy Hung        ssize_t         indexOf(const sp<T> &item) {
9541bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks.indexOf(item);
9551bc088a918d7038603230637d640941953b314d4Andy Hung        }
9561bc088a918d7038603230637d640941953b314d4Andy Hung        sp<T>           operator[](size_t index) const {
9571bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks[index];
9581bc088a918d7038603230637d640941953b314d4Andy Hung        }
9591bc088a918d7038603230637d640941953b314d4Andy Hung        typename SortedVector<sp<T>>::iterator begin() {
9601bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks.begin();
9611bc088a918d7038603230637d640941953b314d4Andy Hung        }
9621bc088a918d7038603230637d640941953b314d4Andy Hung        typename SortedVector<sp<T>>::iterator end() {
9631bc088a918d7038603230637d640941953b314d4Andy Hung            return mTracks.end();
9641bc088a918d7038603230637d640941953b314d4Andy Hung        }
9651bc088a918d7038603230637d640941953b314d4Andy Hung
9661bc088a918d7038603230637d640941953b314d4Andy Hung        size_t          processDeletedTrackNames(std::function<void(int)> f) {
9671bc088a918d7038603230637d640941953b314d4Andy Hung            const size_t size = mDeletedTrackNames.size();
9681bc088a918d7038603230637d640941953b314d4Andy Hung            if (size > 0) {
9691bc088a918d7038603230637d640941953b314d4Andy Hung                for (const int name : mDeletedTrackNames) {
9701bc088a918d7038603230637d640941953b314d4Andy Hung                    f(name);
9711bc088a918d7038603230637d640941953b314d4Andy Hung                }
9721bc088a918d7038603230637d640941953b314d4Andy Hung            }
9731bc088a918d7038603230637d640941953b314d4Andy Hung            return size;
9741bc088a918d7038603230637d640941953b314d4Andy Hung        }
9751bc088a918d7038603230637d640941953b314d4Andy Hung
9761bc088a918d7038603230637d640941953b314d4Andy Hung        void            clearDeletedTrackNames() { mDeletedTrackNames.clear(); }
9771bc088a918d7038603230637d640941953b314d4Andy Hung
9781bc088a918d7038603230637d640941953b314d4Andy Hung    private:
9791bc088a918d7038603230637d640941953b314d4Andy Hung        // Track names pending deletion for MIXER type threads
9801bc088a918d7038603230637d640941953b314d4Andy Hung        const bool mSaveDeletedTrackNames; // true to enable tracking
9811bc088a918d7038603230637d640941953b314d4Andy Hung        std::set<int> mDeletedTrackNames;
9821bc088a918d7038603230637d640941953b314d4Andy Hung
9831bc088a918d7038603230637d640941953b314d4Andy Hung        // Fast lookup of previously deleted track names for reuse.
9841bc088a918d7038603230637d640941953b314d4Andy Hung        // This is an arbitrary decision (actually any non-negative
9851bc088a918d7038603230637d640941953b314d4Andy Hung        // integer that isn't in mTracks[*]->names() could be used) - we attempt
9861bc088a918d7038603230637d640941953b314d4Andy Hung        // to use the smallest possible available name.
9871bc088a918d7038603230637d640941953b314d4Andy Hung        std::set<int> mUnusedTrackNames;
9881bc088a918d7038603230637d640941953b314d4Andy Hung
9891bc088a918d7038603230637d640941953b314d4Andy Hung        SortedVector<sp<T>> mTracks; // wrapped SortedVector.
9901bc088a918d7038603230637d640941953b314d4Andy Hung    };
9911bc088a918d7038603230637d640941953b314d4Andy Hung
9921bc088a918d7038603230637d640941953b314d4Andy Hung    Tracks<Track>                   mTracks;
9931bc088a918d7038603230637d640941953b314d4Andy Hung
994223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    stream_type_t                   mStreamTypes[AUDIO_STREAM_CNT];
99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut                  *mOutput;
99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float                           mMasterVolume;
99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         mLastWriteTime;
99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumWrites;
100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int                             mNumDelayedWrites;
100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool                            mInWrite;
100281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rename these former local variables of threadLoop to standard "m" names
1004ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyTimeNs;
100525c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    size_t                          mSinkBufferSize;
100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l()
1008ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mActiveSleepTimeUs;
1009ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mIdleSleepTimeUs;
101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1011ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    uint32_t                        mSleepTimeUs;
101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mixer status returned by prepareTracks_l()
101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatus; // current cycle
101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // previous cycle when in prepareTracks_l()
101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state                     mMixerStatusIgnoringFastTracks;
101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                  // FIXME or a separate ready state per track
101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME move these declarations into the specific sub-class that needs them
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        sleepTimeShift;
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value
1024ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent    nsecs_t                         mStandbyDelayNs;
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER only
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t                         maxPeriod;
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING only
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                        writeFrames;
103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1032bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mBytesRemaining;
1033bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t                          mCurrentWriteLength;
1034bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool                            mUseAsyncWrite;
10353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is
10363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a write(), a flush() or a standby() occurs.
10373b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when a write blocks and indicates a callback is expected.
10383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence
10393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
10403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mWriteAckSequence;
10413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is
10423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // incremented each time a drain is requested or a flush() or standby() occurs.
10433b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is
10443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // expected.
10453b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence
10463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // callbacks are ignored.
10473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    uint32_t                        mDrainSequence;
1048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    sp<AsyncCallbackThread>         mCallbackThread;
1049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
105181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The HAL output sink is treated as non-blocking, but current implementation is blocking
105281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mOutputSink;
105381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If a fast mixer is present, the blocking pipe sink, otherwise clear
105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mPipeSink;
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mNormalSink;
105746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // For dumpsys
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Sink>          mTeeSink;
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<NBAIO_Source>        mTeeSource;
106146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t                mScreenState;   // cached copy of gScreenState
10632a6a945d65c986dcc779f70f18afde1d7a74397bSanna Catherine de Treville Wager    // TODO: add comment and adjust size as needed
1064eef598c5539aa2be5a106f2483b19e368e4b8c0eGlenn Kasten    static const size_t     kFastMixerLogSize = 8 * 1024;
10659e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    sp<NBLog::Writer>       mFastMixerNBLogWriter;
10662148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
10672148bf0e79c436b8764b9edc4c8f2730cce98a32Andy Hung
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const = 0;
10700f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const
107181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                { FastTrackUnderruns dummy; return dummy; }
107281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessed by both binder threads and within threadLoop(), lock on mutex needed
107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                unsigned    mFastTrackAvailMask;    // bit i set if fast track [i] is available
1076d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwSupportsPause;
1077d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mHwPaused;
1078d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent                bool        mFlushPending;
10797c29ec95f494d241faa07d0d70579729520e3114Eric Laurent                // volumes last sent to audio HAL with stream->setVolume()
10807c29ec95f494d241faa07d0d70579729520e3114Eric Laurent                float mLeftVolFloat;
10817c29ec95f494d241faa07d0d70579729520e3114Eric Laurent                float mRightVolFloat;
108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread {
108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread(const sp<AudioFlinger>& audioFlinger,
108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioStreamOut* output,
108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_io_handle_t id,
108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_devices_t device,
109072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                bool systemReady,
109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                type_t type = MIXER);
109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual             ~MixerThread();
109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
10971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11001bc088a918d7038603230637d640941953b314d4Andy Hung    virtual     bool        isTrackAllowed_l(
11011bc088a918d7038603230637d640941953b314d4Andy Hung                                    audio_channel_mask_t channelMask, audio_format_t format,
11021bc088a918d7038603230637d640941953b314d4Andy Hung                                    audio_session_t sessionId, uid_t uid) const override;
110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1109dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual void acquireWakeLock_l() {
1110dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung        PlaybackThread::acquireWakeLock_l();
1111818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung        if (hasFastMixer()) {
1112818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung            mFastMixer->setBoottimeOffset(
1113818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung                    mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]);
1114818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung        }
1115818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung    }
1116818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung
111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
1118bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove);
112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    correctLatency_l(uint32_t latency) const;
112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1125054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
1126054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent                                   audio_patch_handle_t *handle);
1127054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
1128054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent
112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer* mAudioMixer;    // normal mixer
113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // one-time initialization, no locks required
11324d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten                sp<FastMixer>     mFastMixer;     // non-0 if there is also a fast mixer
113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread
113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // contents are not guaranteed to be consistent, no locks required
113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                FastMixerDumpState mFastMixerDumpState;
113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueObserverDump mStateQueueObserverDump;
113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                StateQueueMutatorDump  mStateQueueMutatorDump;
114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioWatchdogDump mAudioWatchdogDump;
114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // accessible only within the threadLoop(), no locks required
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //          mFastMixer->sq()    // for mutating and pushing state
114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t     mFastMixerFutex;    // for cold idle
114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11472ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                std::atomic_bool mMasterMono;
114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
11494d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    virtual     bool        hasFastMixer() const { return mFastMixer != 0; }
115081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const {
1151dc2c50bad491d2c0c8a2efc0e24491076701c63cGlenn Kasten                              ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks);
115281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              return mFastMixerDumpState.mTracks[fastIndex].mUnderruns;
115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            }
115483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
11552ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hungprotected:
11562ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     void       setMasterMono_l(bool mono) {
11572ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               mMasterMono.store(mono);
11582ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               if (mFastMixer != nullptr) { /* hasFastMixer() */
11592ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                                   mFastMixer->setMasterMono(mMasterMono);
11602ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                               }
11612ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                           }
11622ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung                // the FastMixer performs mono blend if it exists.
116303c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten                // Blending with limiter is not idempotent,
116403c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten                // and blending without limiter is idempotent but inefficient to do twice.
11652ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung    virtual     bool       requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); }
116681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
116781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread {
116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
117181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1172e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                       audio_io_handle_t id, audio_devices_t device, bool systemReady);
117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DirectOutputThread();
117481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
117681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
11781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   status_t& status);
1179e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
118081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    idleSleepTimeUs() const;
118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    suspendSleepTimeUs() const;
118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1191d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     void        threadLoop_exit();
1192d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent    virtual     bool        shouldStandby_l();
119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119443b4dcc660e6da96285e4672ae371070ab845401Phil Burk    virtual     void        onAddNewTrack_l();
119543b4dcc660e6da96285e4672ae371070ab845401Phil Burk
119610cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung    bool mVolumeShaperActive;
119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1198bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
119972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                        audio_io_handle_t id, uint32_t device, ThreadBase::type_t type,
1200e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                        bool systemReady);
12015850c4c8eecbe0db3cee8511a4f82cb443e27d08Eric Laurent    void processVolume_l(Track *track, bool lastTrack);
1202bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepareTracks_l() tells threadLoop_mix() the name of the single active track
120481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track>               mActiveTrack;
120543b4dcc660e6da96285e4672ae371070ab845401Phil Burk
120643b4dcc660e6da96285e4672ae371070ab845401Phil Burk    wp<Track>               mPreviousTrack;         // used to detect track switch
120743b4dcc660e6da96285e4672ae371070ab845401Phil Burk
120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
121010cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung
121110cbff139360f3f642e0e3b3ccf2d463dbed22cfAndy Hung    virtual     int64_t     computeWaitTimeNs_l() const override;
121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1214bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread {
1215bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
1216bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1217bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1218e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent                        audio_io_handle_t id, uint32_t device, bool systemReady);
12196a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent    virtual                 ~OffloadThread() {};
1220e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    virtual     void        flushHw_l();
1221bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1222bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected:
1223bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // threadLoop snippets
1224bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove);
1225bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     void        threadLoop_exit();
1226bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1227bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback();
1228bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     bool        waitingAsyncCallback_l();
122905317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George    virtual     void        invalidateTracks(audio_stream_type_t streamType);
1230bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1231de0613d0ad8b8fb9d19a27185ae4d307a5b7fb9dEric Laurent    virtual     bool        keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); }
1232646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent
1233bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
1234bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedWriteLength;     // length in bytes of write interrupted by pause
1235bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t      mPausedBytesRemaining;  // bytes still waiting in mixbuffer after resume
1236646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent    bool        mKeepWakeLock;          // keep wake lock while waiting for write callback
1237f804475807407442d5596ab7378ed07d50664063Andy Hung    uint64_t    mOffloadUnderrunPosition; // Current frame position for offloaded playback
1238f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // used and valid only during underrun.  ~0 if
1239f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // no underrun has occurred during playback and
1240f804475807407442d5596ab7378ed07d50664063Andy Hung                                          // is not reset on standby.
1241bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
1242bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1243bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread {
1244bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic:
1245bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1246e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh    explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread);
1247bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1248bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual             ~AsyncCallbackThread();
1249bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1250bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread virtuals
1251bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual bool        threadLoop();
1252bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1253bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // RefBase
1254bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual void        onFirstRef();
1255bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1256bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            void        exit();
12573b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setWriteBlocked(uint32_t sequence);
12583b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetWriteBlocked();
12593b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        setDraining(uint32_t sequence);
12603b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            void        resetDraining();
12614527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George            void        setAsyncError();
1262bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1263bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate:
12644de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    const wp<PlaybackThread>   mPlaybackThread;
12653b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via
12663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used
12673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetWriteBlocked()
12684de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mWriteAckSequence;
12693b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // mDrainSequence corresponds to the last drain sequence passed by the offload thread via
12703b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // setDraining(). The sequence is shifted one bit to the left and the lsb is used
12713b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // to indicate that the callback has been received via resetDraining()
12724de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    uint32_t                   mDrainSequence;
12734de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Condition                  mWaitWorkCV;
12744de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent    Mutex                      mLock;
12754527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George    bool                       mAsyncError;
1276bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent};
1277bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread {
127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread,
128172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                      audio_io_handle_t id, bool systemReady);
128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual                 ~DuplicatingThread();
128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
12851bc088a918d7038603230637d640941953b314d4Andy Hung    virtual     void        dumpInternals(int fd, const Vector<String16>& args) override;
12861bc088a918d7038603230637d640941953b314d4Andy Hung
128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        addOutputTrack(MixerThread* thread);
128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                void        removeOutputTrack(MixerThread* thread);
128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    waitTimeMs() const { return mWaitTimeMs; }
1290069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
1291c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard                void        sendMetadataToBackend_l(
1292c86a7f7de52f0ff0059e8c6c4fa4619aa7fae5c5Kevin Rocard                        const StreamOutHalInterface::SourceMetadata& metadata) override;
129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
129481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     uint32_t    activeSleepTimeUs() const;
129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                bool        outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks);
129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threadLoop snippets
130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_mix();
130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_sleepTime();
1302bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    virtual     ssize_t     threadLoop_write();
130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        threadLoop_standby();
130481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        cacheParameters_l();
130581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
130681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
130781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // called from threadLoop, addOutputTrack, removeOutputTrack
130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        updateWaitTime_l();
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected:
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        saveOutputTracks();
131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     void        clearOutputTracks();
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t    mWaitTimeMs;
131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  outputTracks;
131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    SortedVector < sp<OutputTrack> >  mOutputTracks;
131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual     bool        hasFastMixer() const { return false; }
131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread
13226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase
132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
13266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class RecordTrack;
132773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
132873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung    /* The ResamplerBufferProvider is used to retrieve recorded input data from the
132973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * RecordThread.  It maintains local state on the relative position of the read
133073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     * position of the RecordTrack compared with the RecordThread.
133173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung     */
13326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    class ResamplerBufferProvider : public AudioBufferProvider
13336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    {
13346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    public:
1335e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh        explicit ResamplerBufferProvider(RecordTrack* recordTrack) :
133673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRecordTrack(recordTrack),
133773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung            mRsmpInUnrel(0), mRsmpInFront(0) { }
13386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual ~ResamplerBufferProvider() { }
133973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
134073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // called to set the ResamplerBufferProvider to head of the RecordThread data buffer,
134173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        // skipping any previous data read from the hal.
134273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void reset();
134373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
134473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        /* Synchronizes RecordTrack position with the RecordThread.
134573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Calculates available frames and handle overruns if the RecordThread
134673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * has advanced faster than the ResamplerBufferProvider has retrieved data.
134773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * TODO: why not do this for every getNextBuffer?
134873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *
134973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * Parameters
135073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         * framesAvailable:  pointer to optional output size_t to store record track
135173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *                   frames available.
135273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         *      hasOverrun:  pointer to optional boolean, returns true if track has overrun.
135373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung         */
135473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
135573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL);
135673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung
13576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // AudioBufferProvider interface
1358d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten        virtual status_t    getNextBuffer(AudioBufferProvider::Buffer* buffer);
13596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        virtual void        releaseBuffer(AudioBufferProvider::Buffer* buffer);
13606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    private:
13616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        RecordTrack * const mRecordTrack;
136273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        size_t              mRsmpInUnrel;   // unreleased frames remaining from
136373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // most recent getNextBuffer
136473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // for debug only
136573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung        int32_t             mRsmpInFront;   // next available frame
136673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung                                            // rolling counter that is never cleared
13676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    };
13686dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h"
137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            RecordThread(const sp<AudioFlinger>& audioFlinger,
137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    AudioStreamIn *input,
137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_io_handle_t id,
1374d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                    audio_devices_t outDevice,
137572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    audio_devices_t inDevice,
137672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent                    bool systemReady
137746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
137846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    , const sp<NBAIO_Sink>& teeSink
137946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
138046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                    );
138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            virtual     ~RecordThread();
138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // no addTrack_l ?
138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        destroyTrack_l(const sp<RecordTrack>& track);
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        removeTrack_l(const sp<RecordTrack>& track);
138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpInternals(int fd, const Vector<String16>& args);
138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void        dumpTracks(int fd, const Vector<String16>& args);
138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Thread virtuals
139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool        threadLoop();
1392555530ab903504ad3586bc24cd8a4f200f5c39aeEric Laurent    virtual void        preExit();
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // RefBase
139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual void        onFirstRef();
139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t    initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; }
1398e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
1399b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    virtual sp<MemoryDealer>    readOnlyHeap() const { return mReadOnlyHeap; }
1400b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
14016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    virtual sp<IMemory> pipeMemory() const { return mPipeMemory; }
14026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<AudioFlinger::RecordThread::RecordTrack>  createRecordTrack_l(
140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    const sp<AudioFlinger::Client>& client,
14051f564acca544826daaf7dca44e39cec6016b82fdKevin Rocard                    const audio_attributes_t& attr,
1406f14db3c3ebc1ea29b3eb5b7e9b944cabcb5f83ffEric Laurent                    uint32_t *pSampleRate,
140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_format_t format,
140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    audio_channel_mask_t channelMask,
140974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten                    size_t *pFrameCount,
1410d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                    audio_session_t sessionId,
1411f14db3c3ebc1ea29b3eb5b7e9b944cabcb5f83ffEric Laurent                    size_t *pNotificationFrameCount,
14121f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung                    uid_t uid,
1413050677873c10d4da308ac222f8533c96cca3207eEric Laurent                    audio_input_flags_t *flags,
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    pid_t tid,
141520b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                    status_t *status /*non-NULL*/,
141620b9ef0b55c9150ae11057ab997ae61be2d496efEric Laurent                    audio_port_handle_t portId);
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
141881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status_t    start(RecordTrack* recordTrack,
141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                              AudioSystem::sync_event_t event,
1420d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten                              audio_session_t triggerSession);
142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // ask the thread to stop the specified track, and
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // return true if the caller should then do it's part of the stopping process
1424a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten            bool        stop(RecordTrack* recordTrack);
142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            void        dump(int fd, const Vector<String16>& args);
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn* clearInput();
14281dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov            virtual sp<StreamHalInterface> stream() const;
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual bool        checkForNewParameter_l(const String8& keyValuePair,
14321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                               status_t& status);
14331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    virtual void        cacheParameters_l() {}
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual String8     getParameters(const String8& keys);
14357c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent    virtual void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
14361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    createAudioPatch_l(const struct audio_patch *patch,
14371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                           audio_patch_handle_t *handle);
14381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    virtual status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
143983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
144083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        addPatchRecord(const sp<PatchRecord>& record);
144183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            void        deletePatchRecord(const sp<PatchRecord>& record);
144283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
1443deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten            void        readInputParameters_l();
14445f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten    virtual uint32_t    getInputFramesLost();
144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t addEffectChain_l(const sp<EffectChain>& chain);
144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual size_t removeEffectChain_l(const sp<EffectChain>& chain);
14484c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const;
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Return the set of unique session IDs across all tracks.
145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The keys are the session IDs, and the associated values are meaningless.
145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME replace by Set [and implement Bag/Multiset for other uses].
1453d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten            KeyedVector<audio_session_t, bool> sessionIds() const;
145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual status_t setSyncEvent(const sp<SyncEvent>& event);
145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    virtual bool     isValidSyncEvent(const sp<SyncEvent>& event) const;
145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    static void syncStartEventCallback(const wp<SyncEvent>& event);
145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
14609b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten    virtual size_t      frameCount() const { return mFrameCount; }
14616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool        hasFastCapture() const { return mFastCapture != 0; }
146283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    virtual void        getAudioPortConfig(struct audio_port_config *config);
14639b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
14644c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent    virtual status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
14654c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent                                                   audio_session_t sessionId);
14664c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent
1467dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung    virtual void        acquireWakeLock_l() {
1468dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            ThreadBase::acquireWakeLock_l();
1469dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                            mActiveTracks.updatePowerState(this, true /* force */);
1470dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung                        }
1471293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung    virtual bool        isOutput() const override { return false; }
1472dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
1473d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent            void        checkBtNrec();
1474d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent
1475f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov            // Sets the UID records silence
1476f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov            void        setRecordSilenced(uid_t uid, bool silenced);
1477f4ddfefc8ba59a8486d91826154cc9447821409eSvet Ganov
1478653cc0ab6d5b714d1f26adcd08f9bc3269b62fa0jiabin            status_t    getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones);
1479653cc0ab6d5b714d1f26adcd08f9bc3269b62fa0jiabin
1480069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard            void        updateMetadata_l() override;
1481069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Enter standby if not already in standby, and set mStandby flag
148493e471f620454f7de73d190521568b1e25879767Glenn Kasten            void    standbyIfNotAlreadyInStandby();
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Call the HAL standby method unconditionally, and don't change mStandby flag
1487e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten            void    inputStandBy();
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1489d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent            void    checkBtNrec_l();
1490d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioStreamIn                       *mInput;
149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            SortedVector < sp<RecordTrack> >    mTracks;
14932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // mActiveTracks has dual roles:  it indicates the current active track(s), and
149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is used together with mStartStopCond to indicate start()/stop() progress
1495dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung            ActiveTracks<RecordTrack>           mActiveTracks;
1496dae27707fc7d8370eb200d25d1a7c6dd7ad5e201Andy Hung
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Condition                           mStartStopCond;
14989b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten
14998594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            // resampler converts input at HAL Hz to output at AudioRecord client Hz
15001b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten            void                               *mRsmpInBuffer;  // size = mRsmpInFramesOA
15018594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFrames;  // size of resampler input in frames
15028594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            size_t                              mRsmpInFramesP2;// size rounded up to a power-of-2
15031b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten            size_t                              mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation
15046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
15056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // rolling index that is never cleared
15068594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten            int32_t                             mRsmpInRear;    // last filled frame + 1
15078594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // For dumpsys
150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<NBAIO_Sink>                mTeeSink;
1510b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
1511b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            const sp<MemoryDealer>              mReadOnlyHeap;
15126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // one-time initialization, no locks required
1514b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten            sp<FastCapture>                     mFastCapture;   // non-0 if there is also
1515b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten                                                                // a fast capture
151672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent
15176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog thread
15186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // contents are not guaranteed to be consistent, no locks required
15206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureDumpState                mFastCaptureDumpState;
15216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
15226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME StateQueue observer and mutator dump fields
15236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
15246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME audio watchdog dump
15256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // accessible only within the threadLoop(), no locks required
15276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            //          mFastCapture->sq()      // for mutating and pushing state
15286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t     mFastCaptureFutex;      // for cold idle
15296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The HAL input source is treated as non-blocking,
15316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // but current implementation is blocking
15326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mInputSource;
15336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // The source for the normal capture thread to read from: mInputSource or mPipeSource
15346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mNormalSource;
15356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe sink written to by fast capture,
15366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
15376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Sink>                      mPipeSink;
15386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the non-blocking pipe source read by normal thread,
15396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // otherwise clear
15406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBAIO_Source>                    mPipeSource;
15416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // Depth of pipe from fast capture to normal thread and fast clients, always power of 2
15426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t                              mPipeFramesP2;
15436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // If a fast capture is present, the Pipe as IMemory, otherwise clear
15446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<IMemory>                         mPipeMemory;
15456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15462a6a945d65c986dcc779f70f18afde1d7a74397bSanna Catherine de Treville Wager            // TODO: add comment and adjust size as needed
15476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            static const size_t                 kFastCaptureLogSize = 4 * 1024;
15486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sp<NBLog::Writer>                   mFastCaptureNBLogWriter;
15496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
15506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            bool                                mFastTrackAvail;    // true if fast track available
1551d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent            // common state to all record threads
1552d8365c54d049aade9e02ccae722f311f846cad9aEric Laurent            std::atomic_bool                    mBtNrecSuspended;
155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
15546acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15556acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapThread : public ThreadBase
15566acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
15576acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent public:
15586acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15596acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent#include "MmapTracks.h"
15606acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15616acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
15626acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, sp<StreamHalInterface> stream,
15636acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
15646acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapThread();
15656acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15666acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        configure(const audio_attributes_t *attr,
15676acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_stream_type_t streamType,
15686acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_session_t sessionId,
15696acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      const sp<MmapStreamCallback>& callback,
15707aa0ccbcb6e8d35c894356015c23574619bcf648Eric Laurent                                      audio_port_handle_t deviceId,
15716acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_port_handle_t portId);
15726acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15736acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        disconnect();
15746acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15756acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // MmapStreamInterface
15766acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t createMmapBuffer(int32_t minSizeFrames,
15776acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      struct audio_mmap_buffer_info *info);
15786acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t getMmapPosition(struct audio_mmap_position *position);
1579a54f1283fdd9adbd64ecca4e14af56aaa0e8c825Eric Laurent    status_t start(const AudioClient& client, audio_port_handle_t *handle);
15806acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    status_t stop(audio_port_handle_t handle);
158118b570146c971fe729c391bfbb869391084e623dEric Laurent    status_t standby();
15826acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15836acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // RefBase
15846acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        onFirstRef();
15856acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15866acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    // Thread virtuals
15876acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        threadLoop();
15886acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        threadLoop_exit();
15906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        threadLoop_standby();
159118b570146c971fe729c391bfbb869391084e623dEric Laurent    virtual     bool        shouldStandby_l() { return false; }
1592331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent    virtual     status_t    exitStandby();
15936acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
15946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    initCheck() const { return (mHalStream == 0) ? NO_INIT : NO_ERROR; }
15956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     size_t      frameCount() const { return mFrameCount; }
15966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        checkForNewParameter_l(const String8& keyValuePair,
15976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                                    status_t& status);
15986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     String8     getParameters(const String8& keys);
15996acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        ioConfigChanged(audio_io_config_event event, pid_t pid = 0);
16006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        readHalParameters_l();
16016acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        cacheParameters_l() {}
16026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    createAudioPatch_l(const struct audio_patch *patch,
16036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                               audio_patch_handle_t *handle);
16046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    releaseAudioPatch_l(const audio_patch_handle_t handle);
16056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        getAudioPortConfig(struct audio_port_config *config);
16066acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16076acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     sp<StreamHalInterface> stream() const { return mHalStream; }
16086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    addEffectChain_l(const sp<EffectChain>& chain);
16096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     size_t      removeEffectChain_l(const sp<EffectChain>& chain);
16106acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    checkEffectCompatibility_l(const effect_descriptor_t *desc,
16116acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                                               audio_session_t sessionId);
16126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16136acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     uint32_t    hasAudioSession_l(audio_session_t sessionId) const;
16146acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     status_t    setSyncEvent(const sp<SyncEvent>& event);
16156acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     bool        isValidSyncEvent(const sp<SyncEvent>& event) const;
16166acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16176acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        checkSilentMode_l() {}
16186acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        processVolume_l() {}
16196acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        checkInvalidTracks_l();
16206acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16216acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     audio_stream_type_t streamType() { return AUDIO_STREAM_DEFAULT; }
16226acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16236acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        invalidateTracks(audio_stream_type_t streamType __unused) {}
16246acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
1625331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                // Sets the UID records silence
1626331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent    virtual     void        setRecordSilenced(uid_t uid __unused, bool silenced __unused) {}
1627331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent
16286acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        dump(int fd, const Vector<String16>& args);
16296acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
16306acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        dumpTracks(int fd, const Vector<String16>& args);
16316acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16326acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent protected:
16336acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16346acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_attributes_t      mAttr;
16356acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_session_t         mSessionId;
16367aa0ccbcb6e8d35c894356015c23574619bcf648Eric Laurent                audio_port_handle_t     mDeviceId;
16376acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_port_handle_t     mPortId;
16386acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16397f6b40d78b1976c78d1300e8a51fda36eeb50c5dPhil Burk                wp<MmapStreamCallback>  mCallback;
16406acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                sp<StreamHalInterface>  mHalStream;
16416acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                sp<DeviceHalInterface>  mHalDevice;
16426acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioHwDevice* const    mAudioHwDev;
16436acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                ActiveTracks<MmapTrack> mActiveTracks;
164467f97291bfb7caec5152d2c93913191729db5917Eric Laurent                float                   mHalVolFloat;
1645331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent
1646331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                int32_t                 mNoCallbackWarningCount;
1647331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent     static     constexpr int32_t       kMaxNoCallbackWarnings = 5;
16486acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
16496acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16506acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapPlaybackThread : public MmapThread, public VolumeInterface
16516acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
16526acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16536acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentpublic:
16546acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapPlaybackThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
16556acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, AudioStreamOut *output,
16566acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
16576acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapPlaybackThread() {}
16586acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16596acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        configure(const audio_attributes_t *attr,
16606acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_stream_type_t streamType,
16616acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_session_t sessionId,
16626acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      const sp<MmapStreamCallback>& callback,
16637aa0ccbcb6e8d35c894356015c23574619bcf648Eric Laurent                                      audio_port_handle_t deviceId,
16646acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                                      audio_port_handle_t portId);
16656acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16666acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamOut* clearOutput();
16676acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16686acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                // VolumeInterface
16696acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterVolume(float value);
16706acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setMasterMute(bool muted);
16716acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamVolume(audio_stream_type_t stream, float value);
16726acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        setStreamMute(audio_stream_type_t stream, bool muted);
16736acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     float       streamVolume(audio_stream_type_t stream) const;
16746acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16756acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                void        setMasterMute_l(bool muted) { mMasterMute = muted; }
16766acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16776acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        invalidateTracks(audio_stream_type_t streamType);
16786acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16796acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     audio_stream_type_t streamType() { return mStreamType; }
16806acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        checkSilentMode_l();
1681331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                void        processVolume_l() override;
16826acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16836acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     void        dumpInternals(int fd, const Vector<String16>& args);
16846acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
1685293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung    virtual     bool        isOutput() const override { return true; }
16866acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
1687069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard                void        updateMetadata_l() override;
1688069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
16896acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentprotected:
16906acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16916acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                audio_stream_type_t         mStreamType;
16926acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                float                       mMasterVolume;
16936acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                float                       mStreamVolume;
16946acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                        mMasterMute;
16956acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                bool                        mStreamMute;
16966acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamOut*             mOutput;
16976acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
16986acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
16996acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentclass MmapCaptureThread : public MmapThread
17006acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent{
17016acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17026acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentpublic:
17036acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    MmapCaptureThread(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
17046acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      AudioHwDevice *hwDev, AudioStreamIn *input,
17056acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                      audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady);
17066acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent    virtual     ~MmapCaptureThread() {}
17076acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17086acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamIn* clearInput();
17096acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
1710331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                status_t       exitStandby() override;
1711293558ad1977e24e65f7ba78f47382d33fc77d64Andy Hung    virtual     bool           isOutput() const override { return false; }
17126acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
1713069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard                void           updateMetadata_l() override;
1714331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                void           processVolume_l() override;
1715331679c8a5cd21a0e6a7a1d851e965e4721f7420Eric Laurent                void           setRecordSilenced(uid_t uid, bool silenced) override;
1716069c2719e753732d5af9ba51e5fcc34342b0c9f2Kevin Rocard
17176acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurentprotected:
17186acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent
17196acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent                AudioStreamIn*  mInput;
17206acd1d432f526ae9a055ddaece28bf93b474a776Eric Laurent};
1721