Threads.h revision 1f12a8ad958344c50733b948628ffa06db9c5bc6
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/* 281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project 481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License"); 681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License. 781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at 881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** http://www.apache.org/licenses/LICENSE-2.0 1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** 1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software 1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS, 1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and 1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License. 1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/ 1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifndef INCLUDING_FROM_AUDIOFLINGER_H 1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent #error This header file should only be included from AudioFlinger.h 2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 2181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass ThreadBase : public Thread { 2381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "TrackBase.h" 2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum type_t { 2881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MIXER, // Thread class is MixerThread 2981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DIRECT, // Thread class is DirectOutputThread 3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DUPLICATING, // Thread class is DuplicatingThread 31bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent RECORD, // Thread class is RecordThread 32bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent OFFLOAD // Thread class is OffloadThread 3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3597b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten static const char *threadTypeToString(type_t type); 3697b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten 3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id, 3872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_devices_t outDevice, audio_devices_t inDevice, type_t type, 3972e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool systemReady); 4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~ThreadBase(); 4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten virtual status_t readyToRun(); 43cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten 4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dumpBase(int fd, const Vector<String16>& args); 4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dumpEffectChains(int fd, const Vector<String16>& args); 4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void clearPowerManager(); 4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // base for record and playback 5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum { 5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent CFG_EVENT_IO, 521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent CFG_EVENT_PRIO, 531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent CFG_EVENT_SET_PARAMETER, 541c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CFG_EVENT_CREATE_AUDIO_PATCH, 551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CFG_EVENT_RELEASE_AUDIO_PATCH, 5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class ConfigEventData: public RefBase { 591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent public: 601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual ~ConfigEventData() {} 611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual void dump(char *buffer, size_t size) = 0; 631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent protected: 641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ConfigEventData() {} 651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent }; 661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Config event sequence by client if status needed (e.g binder thread calling setParameters()): 681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 1. create SetParameterConfigEvent. This sets mWaitStatus in config event 691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 2. Lock mLock 701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 3. Call sendConfigEvent_l(): Append to mConfigEvents and mWaitWorkCV.signal 711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 4. sendConfigEvent_l() reads status from event->mStatus; 721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 5. sendConfigEvent_l() returns status 731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 6. Unlock 741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Parameter sequence by server: threadLoop calling processConfigEvents_l(): 761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 1. Lock mLock 771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 2. If there is an entry in mConfigEvents proceed ... 781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 3. Read first entry in mConfigEvents 791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 4. Remove first entry from mConfigEvents 801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 5. Process 811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 6. Set event->mStatus 821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 7. event->mCond.signal 831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // 8. Unlock 841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class ConfigEvent: public RefBase { 8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent public: 8781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~ConfigEvent() {} 8881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent void dump(char *buffer, size_t size) { mData->dump(buffer, size); } 9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent const int mType; // event type e.g. CFG_EVENT_IO 921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Mutex mLock; // mutex associated with mCond 931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Condition mCond; // condition for status return 941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t mStatus; // status communicated to sender 951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent bool mWaitStatus; // true if sender is waiting for status 9672e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool mRequiresSystemReady; // true if must wait for system ready to enter event queue 971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent sp<ConfigEventData> mData; // event specific parameter data 9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent protected: 100e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit ConfigEvent(int type, bool requiresSystemReady = false) : 10172e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mType(type), mStatus(NO_ERROR), mWaitStatus(false), 10272e3f39146fce4686bd96f11057c051bea376dfbEric Laurent mRequiresSystemReady(requiresSystemReady), mData(NULL) {} 10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class IoConfigEventData : public ConfigEventData { 10681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent public: 1077c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent IoConfigEventData(audio_io_config_event event, pid_t pid) : 1087c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent mEvent(event), mPid(pid) {} 10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void dump(char *buffer, size_t size) { 11173e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent snprintf(buffer, size, "IO event: event %d\n", mEvent); 11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11473e26b661af50be2c0a4ff6c9ac85f7347a8b235Eric Laurent const audio_io_config_event mEvent; 1157c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent const pid_t mPid; 11681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 11781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class IoConfigEvent : public ConfigEvent { 11981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent public: 1207c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent IoConfigEvent(audio_io_config_event event, pid_t pid) : 1211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ConfigEvent(CFG_EVENT_IO) { 1227c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent mData = new IoConfigEventData(event, pid); 1231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 1241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual ~IoConfigEvent() {} 1251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent }; 12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class PrioConfigEventData : public ConfigEventData { 1281035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent public: 1291035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEventData(pid_t pid, pid_t tid, int32_t prio) : 1301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mPid(pid), mTid(tid), mPrio(prio) {} 13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void dump(char *buffer, size_t size) { 13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent snprintf(buffer, size, "Prio event: pid %d, tid %d, prio %d\n", mPid, mTid, mPrio); 13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const pid_t mPid; 13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const pid_t mTid; 13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const int32_t mPrio; 13981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 14081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class PrioConfigEvent : public ConfigEvent { 1421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent public: 1431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent PrioConfigEvent(pid_t pid, pid_t tid, int32_t prio) : 14472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent ConfigEvent(CFG_EVENT_PRIO, true) { 1451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mData = new PrioConfigEventData(pid, tid, prio); 1461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 1471035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual ~PrioConfigEvent() {} 1481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent }; 1491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 1501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class SetParameterConfigEventData : public ConfigEventData { 1511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent public: 152e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit SetParameterConfigEventData(String8 keyValuePairs) : 1531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mKeyValuePairs(keyValuePairs) {} 1541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 1551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual void dump(char *buffer, size_t size) { 1561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent snprintf(buffer, size, "KeyValue: %s\n", mKeyValuePairs.string()); 1571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 1581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 1591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent const String8 mKeyValuePairs; 1601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent }; 1611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 1621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent class SetParameterConfigEvent : public ConfigEvent { 1631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent public: 164e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit SetParameterConfigEvent(String8 keyValuePairs) : 1651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent ConfigEvent(CFG_EVENT_SET_PARAMETER) { 1661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mData = new SetParameterConfigEventData(keyValuePairs); 1671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent mWaitStatus = true; 1681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent } 1691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual ~SetParameterConfigEvent() {} 1701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent }; 1711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent 1721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent class CreateAudioPatchConfigEventData : public ConfigEventData { 1731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent public: 1741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEventData(const struct audio_patch patch, 1751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t handle) : 1761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mPatch(patch), mHandle(handle) {} 1771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 1781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual void dump(char *buffer, size_t size) { 1791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent snprintf(buffer, size, "Patch handle: %u\n", mHandle); 1801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 1811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 1821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent const struct audio_patch mPatch; 1831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t mHandle; 1841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent }; 1851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 1861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent class CreateAudioPatchConfigEvent : public ConfigEvent { 1871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent public: 1881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent CreateAudioPatchConfigEvent(const struct audio_patch patch, 1891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t handle) : 1901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ConfigEvent(CFG_EVENT_CREATE_AUDIO_PATCH) { 1911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mData = new CreateAudioPatchConfigEventData(patch, handle); 1921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mWaitStatus = true; 1931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 1941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual ~CreateAudioPatchConfigEvent() {} 1951c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent }; 1961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 1971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent class ReleaseAudioPatchConfigEventData : public ConfigEventData { 1981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent public: 199e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit ReleaseAudioPatchConfigEventData(const audio_patch_handle_t handle) : 2001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mHandle(handle) {} 2011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 2021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual void dump(char *buffer, size_t size) { 2031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent snprintf(buffer, size, "Patch handle: %u\n", mHandle); 2041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 2051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 2061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t mHandle; 2071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent }; 2081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 2091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent class ReleaseAudioPatchConfigEvent : public ConfigEvent { 2101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent public: 211e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit ReleaseAudioPatchConfigEvent(const audio_patch_handle_t handle) : 2121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent ConfigEvent(CFG_EVENT_RELEASE_AUDIO_PATCH) { 2131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mData = new ReleaseAudioPatchConfigEventData(handle); 2141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent mWaitStatus = true; 2151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent } 2161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual ~ReleaseAudioPatchConfigEvent() {} 2171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent }; 21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent class PMDeathRecipient : public IBinder::DeathRecipient { 22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent public: 221e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit PMDeathRecipient(const wp<ThreadBase>& thread) : mThread(thread) {} 22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~PMDeathRecipient() {} 22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // IBinder::DeathRecipient 22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void binderDied(const wp<IBinder>& who); 22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 22781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent private: 22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PMDeathRecipient(const PMDeathRecipient&); 22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PMDeathRecipient& operator = (const PMDeathRecipient&); 23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent wp<ThreadBase> mThread; 23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t initCheck() const = 0; 23581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 23681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // static externally-visible 23781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent type_t type() const { return mType; } 238f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent bool isDuplicating() const { return (mType == DUPLICATING); } 239f6870aefc5e31d4220f3778c4e79ff34a61f48adEric Laurent 24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id() const { return mId;} 24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // dynamic externally-visible 24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate() const { return mSampleRate; } 24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask() const { return mChannelMask; } 245463be250de73907965faa6a216c00312bf81e049Andy Hung audio_format_t format() const { return mHALFormat; } 24683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent uint32_t channelCount() const { return mChannelCount; } 24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Called by AudioFlinger::frameCount(audio_io_handle_t output) and effects, 2489b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten // and returns the [normal mix] buffer's frame count. 2499b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten virtual size_t frameCount() const = 0; 2504a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten 2514a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten // Return's the HAL's frame count i.e. fast mixer buffer size. 2524a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten size_t frameCountHAL() const { return mFrameCount; } 2534a8308b11b92e608cdaf29f73f7919e75706f9a2Glenn Kasten 254bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t frameSize() const { return mFrameSize; } 25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Should be "virtual status_t requestExitAndWait()" and override same 25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // method in Thread, but Thread::requestExitAndWait() is not yet virtual. 25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void exit(); 2591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual bool checkForNewParameter_l(const String8& keyValuePair, 2601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status) = 0; 26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t setParameters(const String8& keyValuePairs); 26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual String8 getParameters(const String8& keys) = 0; 2637c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0) = 0; 2641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // sendConfigEvent_l() must be called with ThreadBase::mLock held 2651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // Can temporarily release the lock if waiting for a reply from 2661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent // processConfigEvents_l(). 2671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t sendConfigEvent_l(sp<ConfigEvent>& event); 2687c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent void sendIoConfigEvent(audio_io_config_event event, pid_t pid = 0); 2697c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent void sendIoConfigEvent_l(audio_io_config_event event, pid_t pid = 0); 27072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent void sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio); 27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio); 2721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t sendSetParameterConfigEvent_l(const String8& keyValuePair); 2731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t sendCreateAudioPatchConfigEvent(const struct audio_patch *patch, 2741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle); 2751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent status_t sendReleaseAudioPatchConfigEvent(audio_patch_handle_t handle); 276021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent void processConfigEvents_l(); 2771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual void cacheParameters_l() = 0; 2781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t createAudioPatch_l(const struct audio_patch *patch, 2791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle) = 0; 2801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle) = 0; 28183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual void getAudioPortConfig(struct audio_port_config *config) = 0; 2821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // see note at declaration of mStandby, mOutDevice and mInDevice 28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool standby() const { return mStandby; } 28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t outDevice() const { return mOutDevice; } 28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t inDevice() const { return mInDevice; } 28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 2891dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual sp<StreamHalInterface> stream() const = 0; 29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> createEffect_l( 29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 295d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId, 29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *desc, 29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled, 2989156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status /*non-NULL*/); 29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return values for hasAudioSession (bit field) 30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum effect_state { 30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent EFFECT_SESSION = 0x1, // the audio session corresponds to at least one 30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effect 3044c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent TRACK_SESSION = 0x2, // the audio session corresponds to at least one 30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // track 3064c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent FAST_SESSION = 0x4 // the audio session corresponds to at least one 3074c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // fast track 30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get effect chain corresponding to session Id. 311d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten sp<EffectChain> getEffectChain(audio_session_t sessionId); 31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same as getEffectChain() but must be called with ThreadBase mutex locked 313d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten sp<EffectChain> getEffectChain_l(audio_session_t sessionId) const; 31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // add an effect chain to the chain list (mEffectChains) 31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t addEffectChain_l(const sp<EffectChain>& chain) = 0; 31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove an effect chain from the chain list (mEffectChains) 31781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual size_t removeEffectChain_l(const sp<EffectChain>& chain) = 0; 31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // lock all effect chains Mutexes. Must be called before releasing the 31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ThreadBase mutex before processing the mixer and effects. This guarantees the 32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // integrity of the chains during the process. 32181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Also sets the parameter 'effectChains' to current value of mEffectChains. 32281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void lockEffectChains_l(Vector< sp<EffectChain> >& effectChains); 32381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // unlock effect chains after process 32481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void unlockEffectChains(const Vector< sp<EffectChain> >& effectChains); 325bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // get a copy of mEffectChains vector 326bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<EffectChain> > getEffectChains_l() const { return mEffectChains; }; 32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // set audio mode to all effect chains 32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setMode(audio_mode_t mode); 32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // get effect module with corresponding ID on specified audio session 330d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten sp<AudioFlinger::EffectModule> getEffect(audio_session_t sessionId, int effectId); 331d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten sp<AudioFlinger::EffectModule> getEffect_l(audio_session_t sessionId, int effectId); 33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // add and effect module. Also creates the effect chain is none exists for 33381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the effects audio session 33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t addEffect_l(const sp< EffectModule>& effect); 33581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove and effect module. Also removes the effect chain is this was the last 33681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // effect 33781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void removeEffect_l(const sp< EffectModule>& effect); 33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // detach all tracks connected to an auxiliary effect 3390f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten virtual void detachAuxEffect_l(int effectId __unused) {} 3404c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // returns a combination of: 3414c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // - EFFECT_SESSION if effects on this audio session exist in one chain 3424c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // - TRACK_SESSION if tracks on this audio session exist 3434c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // - FAST_SESSION if fast tracks on this audio session exist 3444c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const = 0; 3454c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent uint32_t hasAudioSession(audio_session_t sessionId) const { 3464c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent Mutex::Autolock _l(mLock); 3474c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent return hasAudioSession_l(sessionId); 3484c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent } 3494c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent 35081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the value returned by default implementation is not important as the 35181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // strategy is only meaningful for PlaybackThread which implements this method 352d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten virtual uint32_t getStrategyForSession_l(audio_session_t sessionId __unused) 353d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten { return 0; } 35481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 35581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend or restore effect according to the type of effect passed. a NULL 35681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // type pointer means suspend all effects in the session 35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setEffectSuspended(const effect_uuid_t *type, 35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 359d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId = AUDIO_SESSION_OUTPUT_MIX); 36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check if some effects must be suspended/restored when an effect is enabled 36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or disabled 36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void checkSuspendOnEffectEnabled(const sp<EffectModule>& effect, 36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 364d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId = 365d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten AUDIO_SESSION_OUTPUT_MIX); 36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect, 36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool enabled, 368d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId = 369d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten AUDIO_SESSION_OUTPUT_MIX); 37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t setSyncEvent(const sp<SyncEvent>& event) = 0; 37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const = 0; 37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 374b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // Return a reference to a per-thread heap which can be used to allocate IMemory 375b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // objects that will be read-only to client processes, read/write to mediaserver, 376b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // and shared by all client processes of the thread. 377b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // The heap is per-thread rather than common across all threads, because 378b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // clients can't be trusted not to modify the offset of the IMemory they receive. 379b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten // If a thread does not have such a heap, this method returns 0. 380b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten virtual sp<MemoryDealer> readOnlyHeap() const { return 0; } 38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 3826181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten virtual sp<IMemory> pipeMemory() const { return 0; } 3836181ffd90a436f333c43a7f812391eee2c35005aGlenn Kasten 38472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent void systemReady(); 38572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent 3864c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent // checkEffectCompatibility_l() must be called with ThreadBase::mLock held 3874c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc, 3884c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent audio_session_t sessionId) = 0; 3894c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent 39081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mutable Mutex mLock; 39181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 39381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // entry describing an effect being suspended in mSuspendedSessions keyed vector 39581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent class SuspendedSessionDesc : public RefBase { 39681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent public: 39781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent SuspendedSessionDesc() : mRefCount(0) {} 39881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mRefCount; // number of active suspend requests 40081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_uuid_t mType; // effect type UUID 40181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 40281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4032f366df67c31119bb6dd726becd32d14b18e6573Andy Hung void acquireWakeLock(); 4042f366df67c31119bb6dd726becd32d14b18e6573Andy Hung virtual void acquireWakeLock_l(); 40581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void releaseWakeLock(); 40681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void releaseWakeLock_l(); 4071f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung void updateWakeLockUids_l(const SortedVector<uid_t> &uids); 408462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen void getPowerManager_l(); 40981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setEffectSuspended_l(const effect_uuid_t *type, 41081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 411d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId); 41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // updated mSuspendedSessions when an effect suspended or restored 41381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void updateSuspendedSessions_l(const effect_uuid_t *type, 41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool suspend, 415d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId); 41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check if some effects must be suspended when an effect chain is added 41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain); 41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 419014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath String16 getWakeLockTag(); 420014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath 42181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void preExit() { } 4222ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung virtual void setMasterMono_l(bool mono __unused) { } 4232ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung virtual bool requireMonoBlend() { return false; } 42481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent friend class AudioFlinger; // for mEffectChains 42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const type_t mType; 42881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Used by parameters, config events, addTrack_l, exit 43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Condition mWaitWorkCV; 43181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 43281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger> mAudioFlinger; 4339b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten 434deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // updated by PlaybackThread::readOutputParameters_l() or 435deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // RecordThread::readInputParameters_l() 43681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t mSampleRate; 43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mFrameCount; // output HAL, direct output, record 43881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t mChannelMask; 439f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten uint32_t mChannelCount; 44081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t mFrameSize; 44197b7b75b6a31330e213bdb96ccc60916218ad903Glenn Kasten // not HAL frame size, this is for output sink (to pipe to fast mixer) 442463be250de73907965faa6a216c00312bf81e049Andy Hung audio_format_t mFormat; // Source format for Recording and 443463be250de73907965faa6a216c00312bf81e049Andy Hung // Sink format for Playback. 444463be250de73907965faa6a216c00312bf81e049Andy Hung // Sink format may be different than 445463be250de73907965faa6a216c00312bf81e049Andy Hung // HAL format if Fastmixer is used. 446463be250de73907965faa6a216c00312bf81e049Andy Hung audio_format_t mHALFormat; 44770949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten size_t mBufferSize; // HAL buffer size for read() or write() 44881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 4491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent Vector< sp<ConfigEvent> > mConfigEvents; 45072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent Vector< sp<ConfigEvent> > mPendingConfigEvents; // events awaiting system ready 45181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 45281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // These fields are written and read by thread itself without lock or barrier, 4534944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten // and read by other threads without lock or barrier via standby(), outDevice() 45481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // and inDevice(). 45581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Because of the absence of a lock or barrier, any other thread that reads 45681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // these fields must use the information in isolation, or be prepared to deal 45781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // with possibility that it might be inconsistent with other information. 4584944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten bool mStandby; // Whether thread is currently in standby. 45981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t mOutDevice; // output device 46081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t mInDevice; // input device 4617c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent audio_devices_t mPrevOutDevice; // previous output device 462e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent audio_devices_t mPrevInDevice; // previous input device 463296fb13dd9b5e90d6a05cce897c3b1e7914a478aEric Laurent struct audio_patch mPatch; 464f59497bd3c190e087202043de5450ef06e92b27dGlenn Kasten audio_source_t mAudioSource; 46581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 46681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const audio_io_handle_t mId; 46781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > mEffectChains; 46881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 469d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten static const int kThreadNameLength = 16; // prctl(PR_SET_NAME) limit 470d7dca050c630bddbd73a6623271b34b4290460eeGlenn Kasten char mThreadName[kThreadNameLength]; // guaranteed NUL-terminated 47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IPowerManager> mPowerManager; 47281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<IBinder> mWakeLockToken; 47381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<PMDeathRecipient> mDeathRecipient; 474d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten // list of suspended effects per session and per type. The first (outer) vector is 475d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten // keyed by session ID, the second (inner) by type UUID timeLow field 476d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten KeyedVector< audio_session_t, KeyedVector< int, sp<SuspendedSessionDesc> > > 47781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mSuspendedSessions; 478ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten static const size_t kLogSize = 4 * 1024; 4799e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<NBLog::Writer> mNBLogWriter; 48072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool mSystemReady; 481818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung ExtendedTimestamp mTimestamp; 4822f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 4832f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // ActiveTracks is a sorted vector of track type T representing the 4842f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // active tracks of threadLoop() to be considered by the locked prepare portion. 4852f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // ActiveTracks should be accessed with the ThreadBase lock held. 4862f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // 4872f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // During processing and I/O, the threadLoop does not hold the lock; 4882f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // hence it does not directly use ActiveTracks. Care should be taken 4892f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // to hold local strong references or defer removal of tracks 4902f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // if the threadLoop may still be accessing those tracks due to mix, etc. 4912f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // 4922f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // This class updates power information appropriately. 4932f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // 4942f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 4952f366df67c31119bb6dd726becd32d14b18e6573Andy Hung template <typename T> 4962f366df67c31119bb6dd726becd32d14b18e6573Andy Hung class ActiveTracks { 4972f366df67c31119bb6dd726becd32d14b18e6573Andy Hung public: 4982f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ActiveTracks() 4992f366df67c31119bb6dd726becd32d14b18e6573Andy Hung : mActiveTracksGeneration(0) 5002f366df67c31119bb6dd726becd32d14b18e6573Andy Hung , mLastActiveTracksGeneration(0) 5012f366df67c31119bb6dd726becd32d14b18e6573Andy Hung { } 5022f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 5032f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ~ActiveTracks() { 5042f366df67c31119bb6dd726becd32d14b18e6573Andy Hung clear(); 5052f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5062f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // returns the last track added (even though it may have been 5072f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // subsequently removed from ActiveTracks). 5082f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // 5092f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // Used for DirectOutputThread to ensure a flush is called when transitioning 5102f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // to a new track (even though it may be on the same session). 5112f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // Used for OffloadThread to ensure that volume and mixer state is 5122f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // taken from the latest track added. 5132f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // 5142f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // The latest track is saved with a weak pointer to prevent keeping an 5152f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // otherwise useless track alive. Thus the function will return nullptr 5162f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // if the latest track has subsequently been removed and destroyed. 5172f366df67c31119bb6dd726becd32d14b18e6573Andy Hung sp<T> getLatest() { 5182f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mLatestActiveTrack.promote(); 5192f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5202f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 5212f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // Updates ActiveTracks client uids to the thread wakelock. 5222f366df67c31119bb6dd726becd32d14b18e6573Andy Hung void updateWakeLockUids(sp<ThreadBase> thread, bool force = false) { 5232f366df67c31119bb6dd726becd32d14b18e6573Andy Hung if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) { 5242f366df67c31119bb6dd726becd32d14b18e6573Andy Hung thread->updateWakeLockUids_l(getWakeLockUids()); 5252f366df67c31119bb6dd726becd32d14b18e6573Andy Hung mLastActiveTracksGeneration = mActiveTracksGeneration; 5262f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5272f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5282f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 5292f366df67c31119bb6dd726becd32d14b18e6573Andy Hung // SortedVector methods 5302f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ssize_t add(const sp<T> &track); 5312f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ssize_t remove(const sp<T> &track); 5322f366df67c31119bb6dd726becd32d14b18e6573Andy Hung size_t size() const { 5332f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mActiveTracks.size(); 5342f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5352f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ssize_t indexOf(const sp<T>& item) { 5362f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mActiveTracks.indexOf(item); 5372f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5382f366df67c31119bb6dd726becd32d14b18e6573Andy Hung sp<T> operator[](size_t index) const { 5392f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mActiveTracks[index]; 5402f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5412f366df67c31119bb6dd726becd32d14b18e6573Andy Hung typename SortedVector<sp<T>>::iterator begin() { 5422f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mActiveTracks.begin(); 5432f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5442f366df67c31119bb6dd726becd32d14b18e6573Andy Hung typename SortedVector<sp<T>>::iterator end() { 5452f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return mActiveTracks.end(); 5462f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5472f366df67c31119bb6dd726becd32d14b18e6573Andy Hung void clear(); 5482f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 5492f366df67c31119bb6dd726becd32d14b18e6573Andy Hung private: 5501f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung SortedVector<uid_t> getWakeLockUids() { 5511f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung SortedVector<uid_t> wakeLockUids; 5522f366df67c31119bb6dd726becd32d14b18e6573Andy Hung for (const sp<T> &track : mActiveTracks) { 5532f366df67c31119bb6dd726becd32d14b18e6573Andy Hung wakeLockUids.add(track->uid()); 5542f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5552f366df67c31119bb6dd726becd32d14b18e6573Andy Hung return wakeLockUids; // moved by underlying SharedBuffer 5562f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 5572f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 5582f366df67c31119bb6dd726becd32d14b18e6573Andy Hung SortedVector<sp<T>> mActiveTracks; 5592f366df67c31119bb6dd726becd32d14b18e6573Andy Hung int mActiveTracksGeneration; 5602f366df67c31119bb6dd726becd32d14b18e6573Andy Hung int mLastActiveTracksGeneration; 5612f366df67c31119bb6dd726becd32d14b18e6573Andy Hung wp<T> mLatestActiveTrack; // latest track added to ActiveTracks 5622f366df67c31119bb6dd726becd32d14b18e6573Andy Hung }; 56381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 56481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 56581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// --- PlaybackThread --- 5661dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganovclass PlaybackThread : public ThreadBase, public StreamOutHalInterfaceCallback { 56781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 56881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 56981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "PlaybackTracks.h" 57081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 57181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum mixer_state { 57281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MIXER_IDLE, // no active tracks 57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MIXER_TRACKS_ENABLED, // at least one active track, but no track has any data ready 574bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent MIXER_TRACKS_READY, // at least one active track, and at least one track has data 575bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent MIXER_DRAIN_TRACK, // drain currently playing track 576bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent MIXER_DRAIN_ALL, // fully drain the hardware 57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // standby mode does not have an enum value 57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend by audio policy manager is orthogonal to mixer state 57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent }; 58081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 581e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent // retry count before removing active track in case of underrun on offloaded thread: 582e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent // we need to make sure that AudioTrack client has enough time to send large buffers 583e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent //FIXME may be more appropriate if expressed in time units. Need to revise how underrun is 584e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent // handled for offloaded tracks 585e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent static const int8_t kMaxTrackRetriesOffload = 20; 586e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent static const int8_t kMaxTrackStartupRetriesOffload = 100; 587e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent static const int8_t kMaxTrackStopRetriesOffload = 2; 588ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent // 14 tracks max per client allows for 2 misbehaving application leaving 4 available tracks. 589ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent static const uint32_t kMaxTracksPerUid = 14; 590e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent 59181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 592e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady); 59381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~PlaybackThread(); 59481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dump(int fd, const Vector<String16>& args); 59681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 59781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Thread virtuals 59881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool threadLoop(); 59981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 60081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // RefBase 60181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void onFirstRef(); 60281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 6034c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc, 6044c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent audio_session_t sessionId); 6054c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent 60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Code snippets that were lifted up out of threadLoop() 60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_mix() = 0; 60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_sleepTime() = 0; 610bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual ssize_t threadLoop_write(); 611bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual void threadLoop_drain(); 61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_standby(); 613bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual void threadLoop_exit(); 61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove); 61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepareTracks_l reads and writes mActiveTracks, and returns 61781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the pending set of tracks to remove via Vector 'tracksToRemove'. The caller 61881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is responsible for clearing or destroying this Vector later on, when it 61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is safe to do so. That will drop the final ref count and destroy the tracks. 62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove) = 0; 621bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void removeTracks_l(const Vector< sp<Track> >& tracksToRemove); 622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 6231dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov // StreamOutHalInterfaceCallback implementation 6241dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual void onWriteReady(); 6251dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual void onDrainReady(); 6261dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual void onError(); 6271dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov 6283b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void resetWriteBlocked(uint32_t sequence); 6293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void resetDraining(uint32_t sequence); 630bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 631bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool waitingAsyncCallback(); 632bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool waitingAsyncCallback_l(); 633bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool shouldStandby_l(); 6344c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George virtual void onAddNewTrack_l(); 6354527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George void onAsyncError(); // error reported by AsyncCallbackThread 63681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 63781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ThreadBase virtuals 63881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void preExit(); 63981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 640646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent virtual bool keepWakeLock() const { return true; } 6412f366df67c31119bb6dd726becd32d14b18e6573Andy Hung virtual void acquireWakeLock_l() { 6422f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ThreadBase::acquireWakeLock_l(); 6432f366df67c31119bb6dd726becd32d14b18e6573Andy Hung mActiveTracks.updateWakeLockUids(this, true /* force */); 6442f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 645646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent 64681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 64781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 64881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t initCheck() const { return (mOutput == NULL) ? NO_INIT : NO_ERROR; } 64981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 65081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return estimated latency in milliseconds, as reported by HAL 65181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latency() const; 65281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same, but lock must already be held 65381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t latency_l() const; 65481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setMasterVolume(float value); 65681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setMasterMute(bool muted); 65781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setStreamVolume(audio_stream_type_t stream, float value); 65981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setStreamMute(audio_stream_type_t stream, bool muted); 66081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 66181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float streamVolume(audio_stream_type_t stream) const; 66281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 66381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> createTrack_l( 66481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 66581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_type_t streamType, 66681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 66781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 66881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 66974935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 67081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IMemory>& sharedBuffer, 671d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId, 672050677873c10d4da308ac222f8533c96cca3207eEric Laurent audio_output_flags_t *flags, 67381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 6741f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung uid_t uid, 6759156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status /*non-NULL*/); 67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* getOutput() const; 67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* clearOutput(); 6791dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual sp<StreamHalInterface> stream() const; 68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // a very large number of suspend() will eventually wraparound, but unlikely 68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void suspend() { (void) android_atomic_inc(&mSuspended); } 68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void restore() 68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if restore() is done without suspend(), get back into 68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // range so that the next suspend() will operate correctly 68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (android_atomic_dec(&mSuspended) <= 0) { 68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent android_atomic_release_store(0, &mSuspended); 68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool isSuspended() const 69281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { return android_atomic_acquire_load(&mSuspended) > 0; } 69381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual String8 getParameters(const String8& keys); 6957c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0); 696377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames); 697010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // FIXME rename mixBuffer() to sinkBuffer() and remove int16_t* dependency. 698010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // Consider also removing and passing an explicit mMainBuffer initialization 699010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung // parameter to AF::PlaybackThread::Track::Track(). 700010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung int16_t *mixBuffer() const { 701010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung return reinterpret_cast<int16_t *>(mSinkBuffer); }; 70281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 70381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void detachAuxEffect_l(int effectId); 704e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh status_t attachAuxEffect(const sp<AudioFlinger::PlaybackThread::Track>& track, 70581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int EffectId); 706e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh status_t attachAuxEffect_l(const sp<AudioFlinger::PlaybackThread::Track>& track, 70781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int EffectId); 70881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 70981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t addEffectChain_l(const sp<EffectChain>& chain); 71081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual size_t removeEffectChain_l(const sp<EffectChain>& chain); 7114c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const; 712d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten virtual uint32_t getStrategyForSession_l(audio_session_t sessionId); 71381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 71481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 71581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t setSyncEvent(const sp<SyncEvent>& event); 71681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const; 717fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten 718fb1fdc9d6603aa228362e7349451f6455c9849c2Glenn Kasten // called with AudioFlinger lock held 71913084621072ea2e4c34e2a9d79793c621d9bf005Eric Laurent bool invalidateTracks_l(audio_stream_type_t streamType); 72005317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George virtual void invalidateTracks(audio_stream_type_t streamType); 72181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 7229b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten virtual size_t frameCount() const { return mNormalFrameCount; } 7239b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten 72483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent status_t getTimestamp_l(AudioTimestamp& timestamp); 72583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 72683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void addPatchTrack(const sp<PatchTrack>& track); 72783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void deletePatchTrack(const sp<PatchTrack>& track); 72883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 72983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual void getAudioPortConfig(struct audio_port_config *config); 730accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent 73181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 732deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten // updated by readOutputParameters_l() 7339b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten size_t mNormalFrameCount; // normal mixer and effects 7349b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten 73508fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung bool mThreadThrottle; // throttle the thread processing 73640eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung uint32_t mThreadThrottleTimeMs; // throttle time for MIXER threads 73740eb1a1f8871909c272e72afaf7d5af84fea2412Andy Hung uint32_t mThreadThrottleEndMs; // notify once per throttling 73808fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung uint32_t mHalfBufferMs; // half the buffer size in milliseconds 73908fb1743f80437c38b4094070d851ea7f6d485e5Andy Hung 740010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung void* mSinkBuffer; // frame size aligned sink buffer 74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 74298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // TODO: 74398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Rearrange the buffer info into a struct/class with 74498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // clear, copy, construction, destruction methods. 74598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 74698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBuffer also has associated with it: 74798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 74898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mSinkBufferSize: Sink Buffer Size 74998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // mFormat: Sink Buffer Format 75098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 75169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Mixer Buffer (mMixerBuffer*) 75269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // 75369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // In the case of floating point or multichannel data, which is not in the 75469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // sink format, it is required to accumulate in a higher precision or greater channel count 75569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // buffer before downmixing or data conversion to the sink buffer. 75669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 75769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Set to "true" to enable the Mixer Buffer otherwise mixer output goes to sink buffer. 75869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung bool mMixerBufferEnabled; 75969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 76069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Storage, 32 byte aligned (may make this alignment a requirement later). 76169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames. 76269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung void* mMixerBuffer; 76369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 76469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // Size of mMixerBuffer in bytes: mNormalFrameCount * #channels * sampsize. 76569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung size_t mMixerBufferSize; 76669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 76769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // The audio format of mMixerBuffer. Set to AUDIO_FORMAT_PCM_(FLOAT|16_BIT) only. 76869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung audio_format_t mMixerBufferFormat; 76969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 77069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // An internal flag set to true by MixerThread::prepareTracks_l() 77169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung // when mMixerBuffer contains valid data after mixing. 77269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung bool mMixerBufferValid; 77369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung 77498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Effects Buffer (mEffectsBuffer*) 77598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 77698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // In the case of effects data, which is not in the sink format, 77798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // it is required to accumulate in a different buffer before data conversion 77898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // to the sink buffer. 77998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 78098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Set to "true" to enable the Effects Buffer otherwise effects output goes to sink buffer. 78198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung bool mEffectBufferEnabled; 78298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 78398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Storage, 32 byte aligned (may make this alignment a requirement later). 78498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Due to constraints on mNormalFrameCount, the buffer size is a multiple of 16 frames. 78598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung void* mEffectBuffer; 78698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 78798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // Size of mEffectsBuffer in bytes: mNormalFrameCount * #channels * sampsize. 78898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung size_t mEffectBufferSize; 78998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 79098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // The audio format of mEffectsBuffer. Set to AUDIO_FORMAT_PCM_16_BIT only. 79198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung audio_format_t mEffectBufferFormat; 79298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 79398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // An internal flag set to true by MixerThread::prepareTracks_l() 79498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // when mEffectsBuffer contains valid data after mixing. 79598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // 79698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // When this is set, all mixer data is routed into the effects buffer 79798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung // for any processing (including output processing). 79898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung bool mEffectBufferValid; 79998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung 80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // suspend count, > 0 means suspended. While suspended, the thread continues to pull from 80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // tracks and mix, but doesn't write to HAL. A2DP and SCO HAL implementations can't handle 80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // concurrent use of both of them, so Audio Policy Service suspends one of the threads to 80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // workaround that restriction. 80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 'volatile' means accessed via atomic operations and no lock. 80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent volatile int32_t mSuspended; 80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 807818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung int64_t mBytesWritten; 808c54b1ffc92b8ad27608a8af21033d7cab33cb3a0Andy Hung int64_t mFramesWritten; // not reset on standby 809238fa3deff26d7e4d9e81bd0a88c936f16226c4eAndy Hung int64_t mSuspendedFrames; // not reset on standby 81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mMasterMute is in both PlaybackThread and in AudioFlinger. When a 81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // PlaybackThread needs to find out if master-muted, it checks it's local 81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // copy rather than the one in AudioFlinger. This optimization saves a lock. 81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool mMasterMute; 81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void setMasterMute_l(bool muted) { mMasterMute = muted; } 81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 8172f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ActiveTracks<Track> mActiveTracks; 81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Allocate a track name for a given channel mask. 82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Returns name >= 0 if successful, -1 on failure. 821ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format, 822ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent audio_session_t sessionId, uid_t uid) = 0; 82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void deleteTrackName_l(int name) = 0; 82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Time to sleep between cycles when: 82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t activeSleepTimeUs() const; // mixer state MIXER_TRACKS_ENABLED 82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t idleSleepTimeUs() const = 0; // mixer state MIXER_IDLE 82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t suspendSleepTimeUs() const = 0; // audio policy manager suspended us 82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No sleep when mixer state == MIXER_TRACKS_READY; relies on audio HAL stream->write() 83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // No sleep in standby mode; waits on a condition 83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Code snippets that are temporarily lifted up out of threadLoop() until the merge 83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void checkSilentMode_l(); 83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Non-trivial for DUPLICATING only 83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void saveOutputTracks() { } 83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void clearOutputTracks() { } 83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Cache various calculated values, at threadLoop() entry and after a parameter change 84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void cacheParameters_l(); 84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t correctLatency_l(uint32_t latency) const; 84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 8441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t createAudioPatch_l(const struct audio_patch *patch, 8451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle); 8461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle); 8471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent 8486fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk bool usesHwAvSync() const { return (mType == DIRECT) && (mOutput != NULL) 8496fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk && mHwSupportsPause 8506fc2a7c81f62b1e21487ae37e11aae6241bc3eadPhil Burk && (mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC); } 8510f7b5f2b231caf87da9b20b74d086e5a9d6f4a9dEric Laurent 852ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent uint32_t trackCountForUid_l(uid_t uid); 853ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent 85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent friend class AudioFlinger; // for numerous 85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread& operator = (const PlaybackThread&); 85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t addTrack_l(const sp<Track>& track); 861bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool destroyTrack_l(const sp<Track>& track); 86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void removeTrack_l(const sp<Track>& track); 863ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent void broadcast_l(); 86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 865deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten void readOutputParameters_l(); 86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void dumpInternals(int fd, const Vector<String16>& args); 86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dumpTracks(int fd, const Vector<String16>& args); 86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent SortedVector< sp<Track> > mTracks; 871223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent stream_type_t mStreamTypes[AUDIO_STREAM_CNT]; 87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *mOutput; 87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float mMasterVolume; 87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t mLastWriteTime; 87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mNumWrites; 87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int mNumDelayedWrites; 87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool mInWrite; 87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME rename these former local variables of threadLoop to standard "m" names 881ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent nsecs_t mStandbyTimeNs; 88225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung size_t mSinkBufferSize; 88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // cached copies of activeSleepTimeUs() and idleSleepTimeUs() made by cacheParameters_l() 885ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent uint32_t mActiveSleepTimeUs; 886ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent uint32_t mIdleSleepTimeUs; 88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 888ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent uint32_t mSleepTimeUs; 88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mixer status returned by prepareTracks_l() 89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mMixerStatus; // current cycle 89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // previous cycle when in prepareTracks_l() 89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mixer_state mMixerStatusIgnoringFastTracks; 89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME or a separate ready state per track 89581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME move these declarations into the specific sub-class that needs them 89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER only 89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sleepTimeShift; 89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // same as AudioFlinger::mStandbyTimeInNsecs except for DIRECT which uses a shorter value 901ad9cb8b88e4f8face23f01d8dc89fa769dacb50fEric Laurent nsecs_t mStandbyDelayNs; 90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // MIXER only 90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent nsecs_t maxPeriod; 90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // DUPLICATING only 90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t writeFrames; 90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 909bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t mBytesRemaining; 910bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t mCurrentWriteLength; 911bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool mUseAsyncWrite; 9123b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // mWriteAckSequence contains current write sequence on bits 31-1. The write sequence is 9133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // incremented each time a write(), a flush() or a standby() occurs. 9143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // Bit 0 is set when a write blocks and indicates a callback is expected. 9153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // Bit 0 is reset by the async callback thread calling resetWriteBlocked(). Out of sequence 9163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // callbacks are ignored. 9173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t mWriteAckSequence; 9183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // mDrainSequence contains current drain sequence on bits 31-1. The drain sequence is 9193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // incremented each time a drain is requested or a flush() or standby() occurs. 9203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // Bit 0 is set when the drain() command is called at the HAL and indicates a callback is 9213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // expected. 9223b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // Bit 0 is reset by the async callback thread calling resetDraining(). Out of sequence 9233b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // callbacks are ignored. 9243b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent uint32_t mDrainSequence; 925ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent // A condition that must be evaluated by prepareTrack_l() has changed and we must not wait 926ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent // for async write callback in the thread loop before evaluating it 927bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent bool mSignalPending; 928bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent sp<AsyncCallbackThread> mCallbackThread; 929bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 93081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 93181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The HAL output sink is treated as non-blocking, but current implementation is blocking 93281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> mOutputSink; 93381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If a fast mixer is present, the blocking pipe sink, otherwise clear 93481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> mPipeSink; 93581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The current sink for the normal mixer to write it's (sub)mix, mOutputSink or mPipeSink 93681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> mNormalSink; 93746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 93881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For dumpsys 93981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> mTeeSink; 94081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Source> mTeeSource; 94146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 94281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t mScreenState; // cached copy of gScreenState 943ab7d72f0804fbb7e91ad9d2a16f826d97e20e5d0Glenn Kasten static const size_t kFastMixerLogSize = 4 * 1024; 9449e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<NBLog::Writer> mFastMixerNBLogWriter; 94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool hasFastMixer() const = 0; 9470f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex __unused) const 94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { FastTrackUnderruns dummy; return dummy; } 94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // accessed by both binder threads and within threadLoop(), lock on mutex needed 95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent unsigned mFastTrackAvailMask; // bit i set if fast track [i] is available 953d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool mHwSupportsPause; 954d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool mHwPaused; 955d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent bool mFlushPending; 95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass MixerThread : public PlaybackThread { 95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread(const sp<AudioFlinger>& audioFlinger, 96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut* output, 96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t device, 96472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool systemReady, 96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent type_t type = MIXER); 96681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~MixerThread(); 96781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 96881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Thread virtuals 96981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual bool checkForNewParameter_l(const String8& keyValuePair, 9711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status); 97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void dumpInternals(int fd, const Vector<String16>& args); 97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); 976ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format, 977ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent audio_session_t sessionId, uid_t uid); 97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void deleteTrackName_l(int name); 97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t idleSleepTimeUs() const; 98081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t suspendSleepTimeUs() const; 98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void cacheParameters_l(); 98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 9832f366df67c31119bb6dd726becd32d14b18e6573Andy Hung virtual void acquireWakeLock_l() { 9842f366df67c31119bb6dd726becd32d14b18e6573Andy Hung PlaybackThread::acquireWakeLock_l(); 985818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung if (hasFastMixer()) { 986818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung mFastMixer->setBoottimeOffset( 987818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME]); 988818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung } 989818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung } 990818e7a32ce3633980138aff2c2bfcc5158b3cfccAndy Hung 99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threadLoop snippets 992bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual ssize_t threadLoop_write(); 99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_standby(); 99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_mix(); 99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_sleepTime(); 99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove); 99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t correctLatency_l(uint32_t latency) const; 99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 999054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent virtual status_t createAudioPatch_l(const struct audio_patch *patch, 1000054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent audio_patch_handle_t *handle); 1001054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle); 1002054d9d3dea1390294650ac704acb4aa0a0731217Eric Laurent 100381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioMixer* mAudioMixer; // normal mixer 100481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // one-time initialization, no locks required 10064d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten sp<FastMixer> mFastMixer; // non-0 if there is also a fast mixer 100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<AudioWatchdog> mAudioWatchdog; // non-0 if there is an audio watchdog thread 100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // contents are not guaranteed to be consistent, no locks required 101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent FastMixerDumpState mFastMixerDumpState; 101181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP 101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueObserverDump mStateQueueObserverDump; 101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent StateQueueMutatorDump mStateQueueMutatorDump; 101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif 101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioWatchdogDump mAudioWatchdogDump; 101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // accessible only within the threadLoop(), no locks required 101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // mFastMixer->sq() // for mutating and pushing state 101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t mFastMixerFutex; // for cold idle 102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10212ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung std::atomic_bool mMasterMono; 102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 10234d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten virtual bool hasFastMixer() const { return mFastMixer != 0; } 102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual FastTrackUnderruns getFastTrackUnderruns(size_t fastIndex) const { 1025dc2c50bad491d2c0c8a2efc0e24491076701c63cGlenn Kasten ALOG_ASSERT(fastIndex < FastMixerState::sMaxFastTracks); 102681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mFastMixerDumpState.mTracks[fastIndex].mUnderruns; 102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 102883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 10292ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hungprotected: 10302ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung virtual void setMasterMono_l(bool mono) { 10312ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung mMasterMono.store(mono); 10322ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung if (mFastMixer != nullptr) { /* hasFastMixer() */ 10332ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung mFastMixer->setMasterMono(mMasterMono); 10342ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung } 10352ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung } 10362ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung // the FastMixer performs mono blend if it exists. 103703c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten // Blending with limiter is not idempotent, 103803c48d5afcb3dfc1e43df93e1f3b3b3e9292e148Glenn Kasten // and blending without limiter is idempotent but inefficient to do twice. 10392ddee19245641e86bca436dda23a0f5089bf2ab5Andy Hung virtual bool requireMonoBlend() { return mMasterMono.load() && !hasFastMixer(); } 104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DirectOutputThread : public PlaybackThread { 104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 1046e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent audio_io_handle_t id, audio_devices_t device, bool systemReady); 104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~DirectOutputThread(); 104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Thread virtuals 105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 10511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual bool checkForNewParameter_l(const String8& keyValuePair, 10521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status); 1053e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent virtual void flushHw_l(); 105481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 1056ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent virtual int getTrackName_l(audio_channel_mask_t channelMask, audio_format_t format, 1057ad7dd9610b6fafa81baf69607f4ac669da88182aEric Laurent audio_session_t sessionId, uid_t uid); 105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void deleteTrackName_l(int name); 105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t activeSleepTimeUs() const; 106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t idleSleepTimeUs() const; 106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t suspendSleepTimeUs() const; 106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void cacheParameters_l(); 106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threadLoop snippets 106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); 106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_mix(); 106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_sleepTime(); 1068d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent virtual void threadLoop_exit(); 1069d1f69b0b17acbd96987ecb2f3378abd394d05903Eric Laurent virtual bool shouldStandby_l(); 107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 107143b4dcc660e6da96285e4672ae371070ab845401Phil Burk virtual void onAddNewTrack_l(); 107243b4dcc660e6da96285e4672ae371070ab845401Phil Burk 107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // volumes last sent to audio HAL with stream->set_volume() 107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float mLeftVolFloat; 107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float mRightVolFloat; 107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1077bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 107872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_io_handle_t id, uint32_t device, ThreadBase::type_t type, 1079e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent bool systemReady); 1080062bfceaefab29f0f71db7d5a248b3f5f0572b6aAndy Hung void processVolume_l(sp<Track> track, bool lastTrack); 1081bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // prepareTracks_l() tells threadLoop_mix() the name of the single active track 108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Track> mActiveTrack; 108443b4dcc660e6da96285e4672ae371070ab845401Phil Burk 108543b4dcc660e6da96285e4672ae371070ab845401Phil Burk wp<Track> mPreviousTrack; // used to detect track switch 108643b4dcc660e6da96285e4672ae371070ab845401Phil Burk 108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool hasFastMixer() const { return false; } 108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 1091bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass OffloadThread : public DirectOutputThread { 1092bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic: 1093bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1094bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent OffloadThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, 1095e93cc03da360a1a0d2ad937c745ce8c8e8be81c2Eric Laurent audio_io_handle_t id, uint32_t device, bool systemReady); 10966a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent virtual ~OffloadThread() {}; 1097e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent virtual void flushHw_l(); 1098bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1099bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprotected: 1100bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // threadLoop snippets 1101bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual mixer_state prepareTracks_l(Vector< sp<Track> > *tracksToRemove); 1102bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual void threadLoop_exit(); 1103bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1104bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool waitingAsyncCallback(); 1105bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool waitingAsyncCallback_l(); 110605317d29b27e5fda654bea21b80d4423a03f49b3Haynes Mathew George virtual void invalidateTracks(audio_stream_type_t streamType); 1107bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1108de0613d0ad8b8fb9d19a27185ae4d307a5b7fb9dEric Laurent virtual bool keepWakeLock() const { return (mKeepWakeLock || (mDrainSequence & 1)); } 1109646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent 1110bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate: 1111bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t mPausedWriteLength; // length in bytes of write interrupted by pause 1112bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent size_t mPausedBytesRemaining; // bytes still waiting in mixbuffer after resume 1113646679779a8f952980a5d4219ad9c6f93efc4b92Eric Laurent bool mKeepWakeLock; // keep wake lock while waiting for write callback 1114f804475807407442d5596ab7378ed07d50664063Andy Hung uint64_t mOffloadUnderrunPosition; // Current frame position for offloaded playback 1115f804475807407442d5596ab7378ed07d50664063Andy Hung // used and valid only during underrun. ~0 if 1116f804475807407442d5596ab7378ed07d50664063Andy Hung // no underrun has occurred during playback and 1117f804475807407442d5596ab7378ed07d50664063Andy Hung // is not reset on standby. 1118bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}; 1119bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1120bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentclass AsyncCallbackThread : public Thread { 1121bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentpublic: 1122bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1123e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit AsyncCallbackThread(const wp<PlaybackThread>& playbackThread); 1124bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1125bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual ~AsyncCallbackThread(); 1126bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1127bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // Thread virtuals 1128bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual bool threadLoop(); 1129bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1130bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // RefBase 1131bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual void onFirstRef(); 1132bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1133bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent void exit(); 11343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void setWriteBlocked(uint32_t sequence); 11353b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void resetWriteBlocked(); 11363b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void setDraining(uint32_t sequence); 11373b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent void resetDraining(); 11384527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George void setAsyncError(); 1139bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1140bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentprivate: 11414de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent const wp<PlaybackThread> mPlaybackThread; 11423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // mWriteAckSequence corresponds to the last write sequence passed by the offload thread via 11433b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // setWriteBlocked(). The sequence is shifted one bit to the left and the lsb is used 11443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // to indicate that the callback has been received via resetWriteBlocked() 11454de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent uint32_t mWriteAckSequence; 11463b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // mDrainSequence corresponds to the last drain sequence passed by the offload thread via 11473b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // setDraining(). The sequence is shifted one bit to the left and the lsb is used 11483b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent // to indicate that the callback has been received via resetDraining() 11494de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent uint32_t mDrainSequence; 11504de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent Condition mWaitWorkCV; 11514de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent Mutex mLock; 11524527b9ebc6a37b861f5a3bba68bcc63dc8d69adaHaynes Mathew George bool mAsyncError; 1153bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}; 1154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 115581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass DuplicatingThread : public MixerThread { 115681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DuplicatingThread(const sp<AudioFlinger>& audioFlinger, MixerThread* mainThread, 115872e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_io_handle_t id, bool systemReady); 115981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~DuplicatingThread(); 116081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Thread virtuals 116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void addOutputTrack(MixerThread* thread); 116381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void removeOutputTrack(MixerThread* thread); 116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t waitTimeMs() const { return mWaitTimeMs; } 116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 116681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual uint32_t activeSleepTimeUs() const; 116781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool outputsReady(const SortedVector< sp<OutputTrack> > &outputTracks); 117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 117181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // threadLoop snippets 117281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_mix(); 117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_sleepTime(); 1174bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent virtual ssize_t threadLoop_write(); 117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void threadLoop_standby(); 117681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void cacheParameters_l(); 117781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 117881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 117981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // called from threadLoop, addOutputTrack, removeOutputTrack 118081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void updateWaitTime_l(); 118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprotected: 118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void saveOutputTracks(); 118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void clearOutputTracks(); 118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t mWaitTimeMs; 118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent SortedVector < sp<OutputTrack> > outputTracks; 118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent SortedVector < sp<OutputTrack> > mOutputTracks; 118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool hasFastMixer() const { return false; } 119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 119281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 119381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// record thread 11956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenclass RecordThread : public ThreadBase 119681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic: 119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 11996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten class RecordTrack; 120073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 120173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung /* The ResamplerBufferProvider is used to retrieve recorded input data from the 120273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * RecordThread. It maintains local state on the relative position of the read 120373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * position of the RecordTrack compared with the RecordThread. 120473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung */ 12056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten class ResamplerBufferProvider : public AudioBufferProvider 12066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten { 12076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten public: 1208e964d4e421e2d1ca937227a580c0c837091a11e3Chih-Hung Hsieh explicit ResamplerBufferProvider(RecordTrack* recordTrack) : 120973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRecordTrack(recordTrack), 121073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung mRsmpInUnrel(0), mRsmpInFront(0) { } 12116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten virtual ~ResamplerBufferProvider() { } 121273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 121373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // called to set the ResamplerBufferProvider to head of the RecordThread data buffer, 121473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // skipping any previous data read from the hal. 121573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung virtual void reset(); 121673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 121773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung /* Synchronizes RecordTrack position with the RecordThread. 121873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * Calculates available frames and handle overruns if the RecordThread 121973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * has advanced faster than the ResamplerBufferProvider has retrieved data. 122073c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * TODO: why not do this for every getNextBuffer? 122173c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * 122273c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * Parameters 122373c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * framesAvailable: pointer to optional output size_t to store record track 122473c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * frames available. 122573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung * hasOverrun: pointer to optional boolean, returns true if track has overrun. 122673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung */ 122773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 122873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung virtual void sync(size_t *framesAvailable = NULL, bool *hasOverrun = NULL); 122973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung 12306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // AudioBufferProvider interface 1231d79072e9dff59f767cce2cda1caab80ce5a0815bGlenn Kasten virtual status_t getNextBuffer(AudioBufferProvider::Buffer* buffer); 12326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten virtual void releaseBuffer(AudioBufferProvider::Buffer* buffer); 12336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten private: 12346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten RecordTrack * const mRecordTrack; 123573c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung size_t mRsmpInUnrel; // unreleased frames remaining from 123673c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // most recent getNextBuffer 123773c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // for debug only 123873c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung int32_t mRsmpInFront; // next available frame 123973c02e4277b399c2ec1555d32b6ad5df23bb83dcAndy Hung // rolling counter that is never cleared 12406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten }; 12416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 124297a893eb34f8687485c88eaf15917974a203f20bAndy Hung /* The RecordBufferConverter is used for format, channel, and sample rate 124397a893eb34f8687485c88eaf15917974a203f20bAndy Hung * conversion for a RecordTrack. 124497a893eb34f8687485c88eaf15917974a203f20bAndy Hung * 124597a893eb34f8687485c88eaf15917974a203f20bAndy Hung * TODO: Self contained, so move to a separate file later. 124697a893eb34f8687485c88eaf15917974a203f20bAndy Hung * 124797a893eb34f8687485c88eaf15917974a203f20bAndy Hung * RecordBufferConverter uses the convert() method rather than exposing a 124897a893eb34f8687485c88eaf15917974a203f20bAndy Hung * buffer provider interface; this is to save a memory copy. 124997a893eb34f8687485c88eaf15917974a203f20bAndy Hung */ 125097a893eb34f8687485c88eaf15917974a203f20bAndy Hung class RecordBufferConverter 125197a893eb34f8687485c88eaf15917974a203f20bAndy Hung { 125297a893eb34f8687485c88eaf15917974a203f20bAndy Hung public: 125397a893eb34f8687485c88eaf15917974a203f20bAndy Hung RecordBufferConverter( 125497a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 125597a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 125697a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 125797a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate); 125897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 125997a893eb34f8687485c88eaf15917974a203f20bAndy Hung ~RecordBufferConverter(); 126097a893eb34f8687485c88eaf15917974a203f20bAndy Hung 126197a893eb34f8687485c88eaf15917974a203f20bAndy Hung /* Converts input data from an AudioBufferProvider by format, channelMask, 126297a893eb34f8687485c88eaf15917974a203f20bAndy Hung * and sampleRate to a destination buffer. 126397a893eb34f8687485c88eaf15917974a203f20bAndy Hung * 126497a893eb34f8687485c88eaf15917974a203f20bAndy Hung * Parameters 126597a893eb34f8687485c88eaf15917974a203f20bAndy Hung * dst: buffer to place the converted data. 126697a893eb34f8687485c88eaf15917974a203f20bAndy Hung * provider: buffer provider to obtain source data. 126797a893eb34f8687485c88eaf15917974a203f20bAndy Hung * frames: number of frames to convert 126897a893eb34f8687485c88eaf15917974a203f20bAndy Hung * 126997a893eb34f8687485c88eaf15917974a203f20bAndy Hung * Returns the number of frames converted. 127097a893eb34f8687485c88eaf15917974a203f20bAndy Hung */ 127197a893eb34f8687485c88eaf15917974a203f20bAndy Hung size_t convert(void *dst, AudioBufferProvider *provider, size_t frames); 127297a893eb34f8687485c88eaf15917974a203f20bAndy Hung 127397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // returns NO_ERROR if constructor was successful 127497a893eb34f8687485c88eaf15917974a203f20bAndy Hung status_t initCheck() const { 127597a893eb34f8687485c88eaf15917974a203f20bAndy Hung // mSrcChannelMask set on successful updateParameters 127697a893eb34f8687485c88eaf15917974a203f20bAndy Hung return mSrcChannelMask != AUDIO_CHANNEL_INVALID ? NO_ERROR : NO_INIT; 127797a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 127897a893eb34f8687485c88eaf15917974a203f20bAndy Hung 127997a893eb34f8687485c88eaf15917974a203f20bAndy Hung // allows dynamic reconfigure of all parameters 128097a893eb34f8687485c88eaf15917974a203f20bAndy Hung status_t updateParameters( 128197a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t srcChannelMask, audio_format_t srcFormat, 128297a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t srcSampleRate, 128397a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t dstChannelMask, audio_format_t dstFormat, 128497a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t dstSampleRate); 128597a893eb34f8687485c88eaf15917974a203f20bAndy Hung 128697a893eb34f8687485c88eaf15917974a203f20bAndy Hung // called to reset resampler buffers on record track discontinuity 128797a893eb34f8687485c88eaf15917974a203f20bAndy Hung void reset() { 128897a893eb34f8687485c88eaf15917974a203f20bAndy Hung if (mResampler != NULL) { 128997a893eb34f8687485c88eaf15917974a203f20bAndy Hung mResampler->reset(); 129097a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 129197a893eb34f8687485c88eaf15917974a203f20bAndy Hung } 129297a893eb34f8687485c88eaf15917974a203f20bAndy Hung 129397a893eb34f8687485c88eaf15917974a203f20bAndy Hung private: 1294d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format conversion when not using resampler 1295d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void convertNoResampler(void *dst, const void *src, size_t frames); 1296d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 1297d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung // format conversion when using resampler; modifies src in-place 1298d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung void convertResampler(void *dst, /*not-a-const*/ void *src, size_t frames); 129997a893eb34f8687485c88eaf15917974a203f20bAndy Hung 130097a893eb34f8687485c88eaf15917974a203f20bAndy Hung // user provided information 130197a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t mSrcChannelMask; 130297a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_format_t mSrcFormat; 130397a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t mSrcSampleRate; 130497a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_channel_mask_t mDstChannelMask; 130597a893eb34f8687485c88eaf15917974a203f20bAndy Hung audio_format_t mDstFormat; 130697a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t mDstSampleRate; 130797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 130897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // derived information 130997a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t mSrcChannelCount; 131097a893eb34f8687485c88eaf15917974a203f20bAndy Hung uint32_t mDstChannelCount; 131197a893eb34f8687485c88eaf15917974a203f20bAndy Hung size_t mDstFrameSize; 131297a893eb34f8687485c88eaf15917974a203f20bAndy Hung 131397a893eb34f8687485c88eaf15917974a203f20bAndy Hung // format conversion buffer 131497a893eb34f8687485c88eaf15917974a203f20bAndy Hung void *mBuf; 131597a893eb34f8687485c88eaf15917974a203f20bAndy Hung size_t mBufFrames; 131697a893eb34f8687485c88eaf15917974a203f20bAndy Hung size_t mBufFrameSize; 131797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 131897a893eb34f8687485c88eaf15917974a203f20bAndy Hung // resampler info 131997a893eb34f8687485c88eaf15917974a203f20bAndy Hung AudioResampler *mResampler; 1320d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung 1321d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung bool mIsLegacyDownmix; // legacy stereo to mono conversion needed 1322d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung bool mIsLegacyUpmix; // legacy mono to stereo conversion needed 1323d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung bool mRequiresFloat; // data processing requires float (e.g. resampler) 1324d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung PassthruBufferProvider *mInputConverterProvider; // converts input to float 1325d330ee46022f34da76d14d0c4d2910526ecc2321Andy Hung int8_t mIdxAry[sizeof(uint32_t) * 8]; // used for channel mask conversion 132697a893eb34f8687485c88eaf15917974a203f20bAndy Hung }; 132797a893eb34f8687485c88eaf15917974a203f20bAndy Hung 132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "RecordTracks.h" 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordThread(const sp<AudioFlinger>& audioFlinger, 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input, 133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id, 1333d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent audio_devices_t outDevice, 133472e3f39146fce4686bd96f11057c051bea376dfbEric Laurent audio_devices_t inDevice, 133572e3f39146fce4686bd96f11057c051bea376dfbEric Laurent bool systemReady 133646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 133746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , const sp<NBAIO_Sink>& teeSink 133846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 133946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ); 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual ~RecordThread(); 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // no addTrack_l ? 134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void destroyTrack_l(const sp<RecordTrack>& track); 134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void removeTrack_l(const sp<RecordTrack>& track); 134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dumpInternals(int fd, const Vector<String16>& args); 134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dumpTracks(int fd, const Vector<String16>& args); 134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Thread virtuals 135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool threadLoop(); 135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // RefBase 135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual void onFirstRef(); 135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t initCheck() const { return (mInput == NULL) ? NO_INIT : NO_ERROR; } 1356e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 1357b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten virtual sp<MemoryDealer> readOnlyHeap() const { return mReadOnlyHeap; } 1358b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten 13596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten virtual sp<IMemory> pipeMemory() const { return mPipeMemory; } 13606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<AudioFlinger::RecordThread::RecordTrack> createRecordTrack_l( 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<AudioFlinger::Client>& client, 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 136674935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *pFrameCount, 1367d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t sessionId, 13687df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten size_t *notificationFrames, 13691f12a8ad958344c50733b948628ffa06db9c5bc6Andy Hung uid_t uid, 1370050677873c10d4da308ac222f8533c96cca3207eEric Laurent audio_input_flags_t *flags, 137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 13729156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten status_t *status /*non-NULL*/); 137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t start(RecordTrack* recordTrack, 137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::sync_event_t event, 1376d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten audio_session_t triggerSession); 137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // ask the thread to stop the specified track, and 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return true if the caller should then do it's part of the stopping process 1380a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten bool stop(RecordTrack* recordTrack); 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void dump(int fd, const Vector<String16>& args); 138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn* clearInput(); 13841dc98674f701dada94143b4d31b7221c58346c6cMikhail Naganov virtual sp<StreamHalInterface> stream() const; 138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 13871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual bool checkForNewParameter_l(const String8& keyValuePair, 13881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent status_t& status); 13891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent virtual void cacheParameters_l() {} 139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual String8 getParameters(const String8& keys); 13917c1ec5f038e63a5eb8b04434577c25bc23f5f410Eric Laurent virtual void ioConfigChanged(audio_io_config_event event, pid_t pid = 0); 13921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t createAudioPatch_l(const struct audio_patch *patch, 13931c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent audio_patch_handle_t *handle); 13941c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent virtual status_t releaseAudioPatch_l(const audio_patch_handle_t handle); 139583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 139683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void addPatchRecord(const sp<PatchRecord>& record); 139783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent void deletePatchRecord(const sp<PatchRecord>& record); 139883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent 1399deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten void readInputParameters_l(); 14005f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kasten virtual uint32_t getInputFramesLost(); 140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t addEffectChain_l(const sp<EffectChain>& chain); 140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual size_t removeEffectChain_l(const sp<EffectChain>& chain); 14044c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual uint32_t hasAudioSession_l(audio_session_t sessionId) const; 140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Return the set of unique session IDs across all tracks. 140781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The keys are the session IDs, and the associated values are meaningless. 140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME replace by Set [and implement Bag/Multiset for other uses]. 1409d848eb48c121c119e8ba7583efc75415fe102570Glenn Kasten KeyedVector<audio_session_t, bool> sessionIds() const; 141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual status_t setSyncEvent(const sp<SyncEvent>& event); 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent virtual bool isValidSyncEvent(const sp<SyncEvent>& event) const; 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent static void syncStartEventCallback(const wp<SyncEvent>& event); 141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 14169b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten virtual size_t frameCount() const { return mFrameCount; } 14176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten bool hasFastCapture() const { return mFastCapture != 0; } 141883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent virtual void getAudioPortConfig(struct audio_port_config *config); 14199b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten 14204c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent virtual status_t checkEffectCompatibility_l(const effect_descriptor_t *desc, 14214c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent audio_session_t sessionId); 14224c415062ad1bb53e9af8f644d8215837262b79bbEric Laurent 14232f366df67c31119bb6dd726becd32d14b18e6573Andy Hung virtual void acquireWakeLock_l() { 14242f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ThreadBase::acquireWakeLock_l(); 14252f366df67c31119bb6dd726becd32d14b18e6573Andy Hung mActiveTracks.updateWakeLockUids(this, true /* force */); 14262f366df67c31119bb6dd726becd32d14b18e6573Andy Hung } 14272f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate: 142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Enter standby if not already in standby, and set mStandby flag 143093e471f620454f7de73d190521568b1e25879767Glenn Kasten void standbyIfNotAlreadyInStandby(); 143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Call the HAL standby method unconditionally, and don't change mStandby flag 1433e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten void inputStandBy(); 143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *mInput; 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent SortedVector < sp<RecordTrack> > mTracks; 14372b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten // mActiveTracks has dual roles: it indicates the current active track(s), and 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // is used together with mStartStopCond to indicate start()/stop() progress 14392f366df67c31119bb6dd726becd32d14b18e6573Andy Hung ActiveTracks<RecordTrack> mActiveTracks; 14402f366df67c31119bb6dd726becd32d14b18e6573Andy Hung 144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Condition mStartStopCond; 14429b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten 14438594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // resampler converts input at HAL Hz to output at AudioRecord client Hz 14441b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten void *mRsmpInBuffer; // size = mRsmpInFramesOA 14458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t mRsmpInFrames; // size of resampler input in frames 14468594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten size_t mRsmpInFramesP2;// size rounded up to a power-of-2 14471b291841a58ce6b2b291232dfdd56134b2185c48Glenn Kasten size_t mRsmpInFramesOA;// mRsmpInFramesP2 + over-allocation 14486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten 14496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten // rolling index that is never cleared 14508594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten int32_t mRsmpInRear; // last filled frame + 1 14518594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten 145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For dumpsys 145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<NBAIO_Sink> mTeeSink; 1454b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten 1455b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten const sp<MemoryDealer> mReadOnlyHeap; 14566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // one-time initialization, no locks required 1458b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten sp<FastCapture> mFastCapture; // non-0 if there is also 1459b187de1ada34a9023c05d020a4592686ba761278Glenn Kasten // a fast capture 146072e3f39146fce4686bd96f11057c051bea376dfbEric Laurent 14616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME audio watchdog thread 14626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // contents are not guaranteed to be consistent, no locks required 14646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten FastCaptureDumpState mFastCaptureDumpState; 14656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP 14666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME StateQueue observer and mutator dump fields 14676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif 14686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // FIXME audio watchdog dump 14696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // accessible only within the threadLoop(), no locks required 14716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // mFastCapture->sq() // for mutating and pushing state 14726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten int32_t mFastCaptureFutex; // for cold idle 14736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // The HAL input source is treated as non-blocking, 14756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // but current implementation is blocking 14766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<NBAIO_Source> mInputSource; 14776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // The source for the normal capture thread to read from: mInputSource or mPipeSource 14786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<NBAIO_Source> mNormalSource; 14796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If a fast capture is present, the non-blocking pipe sink written to by fast capture, 14806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // otherwise clear 14816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<NBAIO_Sink> mPipeSink; 14826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If a fast capture is present, the non-blocking pipe source read by normal thread, 14836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // otherwise clear 14846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<NBAIO_Source> mPipeSource; 14856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // Depth of pipe from fast capture to normal thread and fast clients, always power of 2 14866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten size_t mPipeFramesP2; 14876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten // If a fast capture is present, the Pipe as IMemory, otherwise clear 14886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<IMemory> mPipeMemory; 14896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten static const size_t kFastCaptureLogSize = 4 * 1024; 14916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten sp<NBLog::Writer> mFastCaptureNBLogWriter; 14926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten 14936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten bool mFastTrackAvail; // true if fast track available 149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; 1495