AudioFlinger.cpp revision 9ea65d0f4a564478343b1a722fae4ce5883670c3
199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/* 265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** 365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Copyright 2007, The Android Open Source Project 465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** 565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License"); 665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** you may not use this file except in compliance with the License. 765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** You may obtain a copy of the License at 865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** 965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** http://www.apache.org/licenses/LICENSE-2.0 1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** 1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Unless required by applicable law or agreed to in writing, software 1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS, 1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** See the License for the specific language governing permissions and 1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** limitations under the License. 1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian*/ 1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioFlinger" 2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0 2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 22153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h" 23da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <dirent.h> 2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <math.h> 2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <signal.h> 2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h> 2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/resource.h> 2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 299ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang#include <binder/IPCThreadState.h> 3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h> 3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h> 32d8e6fd35ec2b59ee7d873daf1f1d9d348221c7bcGlenn Kasten#include <utils/Trace.h> 3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/Parcel.h> 3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h> 3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h> 3638ccae2c0324daa305f3fe77d25fdf5edec0b0e1Eric Laurent#include <utils/Atomic.h> 3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 38fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <cutils/bitops.h> 3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h> 4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4164760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h> 427394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <hardware/audio.h> 4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioMixer.h" 4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioFlinger.h" 4644deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h" 4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <media/EffectsFactoryApi.h> 496d8b694d999e9be7d5dcc336535832a80fb6f61fEric Laurent#include <audio_effects/effect_visualizer.h> 5059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_ns.h> 5159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent#include <audio_effects/effect_aec.h> 5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 533b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h> 543b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten 55feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent#include <powermanager/PowerManager.h> 56190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 574ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <common_time/cc_helper.h> 5858912562617941964939a4182cda71eaeb153d4bGlenn Kasten 599e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten#include <media/IMediaLogService.h> 609e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 61da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/Pipe.h> 62da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#include <media/nbaio/PipeReader.h> 631ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h> 644182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten#include <private/android_filesystem_config.h> 65da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 681c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// Note: the following macro is used for extremely verbose logging message. In 691c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to 701c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// 0; but one side effect of this is to turn all LOGV's as well. Some messages 711c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are so verbose that we want to suppress them even when we have ALOG_ASSERT 721c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// turned on. Do not uncomment the #def below unless you really know what you 731c345196edc61694f29307a1826a64a0d26028dcJohn Grossman// are doing and want to see all of the extremely verbose messages. 741c345196edc61694f29307a1826a64a0d26028dcJohn Grossman//#define VERY_VERY_VERBOSE_LOGGING 751c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#ifdef VERY_VERY_VERBOSE_LOGGING 761c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV ALOGV 771c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#else 781c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#define ALOGVV(a...) do { } while(0) 791c345196edc61694f29307a1826a64a0d26028dcJohn Grossman#endif 80de070137f11d346fba77605bd76a44c040a618fcEric Laurent 8165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android { 8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 83ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; 84ec1d6b5e17281a066d618f7fcd2b63b3ce11f421Glenn Kastenstatic const char kHardwareLockedString[] = "Hardware lock is taken\n"; 8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 8658912562617941964939a4182cda71eaeb153d4bGlenn Kasten 874ff14bae91075eb274eb1c2975982358946e7e63John Grossmannsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; 887cafbb32999049873d4746ba83bd20c88abe6ce6Eric Laurent 8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::mScreenState; 903ed292031dc50c56110cdadb1e3778117e3be76aGlenn Kasten 9146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 92da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkInputEnabled = false; 93da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkOutputEnabled = false; 94da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenbool AudioFlinger::mTeeSinkTrackEnabled = false; 95da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 96da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkInputFrames = kTeeSinkInputFramesDefault; 97da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkOutputFrames = kTeeSinkOutputFramesDefault; 98da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastensize_t AudioFlinger::mTeeSinkTrackFrames = kTeeSinkTrackFramesDefault; 9946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 100da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 101813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off 102813e2a74853bde19e37d878c596a044b3f299efcEric Laurent// we define a minimum time during which a global effect is considered enabled. 103813e2a74853bde19e37d878c596a044b3f299efcEric Laurentstatic const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200); 104813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 10565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 107b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissenconst char *formatToString(audio_format_t format) { 108b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen switch(format) { 109b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_8_BIT: return "pcm8"; 110b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_16_BIT: return "pcm16"; 111b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_32_BIT: return "pcm32"; 112b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_8_24_BIT: return "pcm8.24"; 113b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED: return "pcm24"; 114b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_PCM_SUB_FLOAT: return "pcmfloat"; 115b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_MP3: return "mp3"; 116b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_AMR_NB: return "amr-nb"; 117b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_AMR_WB: return "amr-wb"; 118b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_AAC: return "aac"; 119b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_HE_AAC_V1: return "he-aac-v1"; 120b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_HE_AAC_V2: return "he-aac-v2"; 121b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen case AUDIO_FORMAT_VORBIS: return "vorbis"; 122b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen default: 123b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen break; 124b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen } 125b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen return "unknown"; 126b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen} 127b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen 128f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev) 129799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{ 130f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent const hw_module_t *mod; 131799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin int rc; 132799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 133f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); 134f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, 135f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 136f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if (rc) { 137799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin goto out; 138f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 139f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = audio_hw_device_open(mod, dev); 140f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, 141f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 142f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if (rc) { 143799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin goto out; 144f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 145f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { 146f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); 147f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = BAD_VALUE; 148f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent goto out; 149f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 150799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return 0; 151799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 152799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout: 153799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin *dev = NULL; 154799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return rc; 155799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin} 156799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 15765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger() 16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian : BnAudioFlinger(), 1614ff14bae91075eb274eb1c2975982358946e7e63John Grossman mPrimaryHardwareDev(NULL), 1629ea65d0f4a564478343b1a722fae4ce5883670c3Glenn Kasten mAudioHwDevs(NULL), 1637d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten mHardwareStatus(AUDIO_HW_IDLE), 1644ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMasterVolume(1.0f), 1654ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMasterMute(false), 1664ff14bae91075eb274eb1c2975982358946e7e63John Grossman mNextUniqueId(1), 1674ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMode(AUDIO_MODE_INVALID), 1684182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mBtNrecIsOff(false), 1694182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsLowRamDevice(true), 170813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mIsDeviceTypeKnown(false), 171813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mGlobalEffectEnableTime(0) 17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 173949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten getpid_cached = getpid(); 1749e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten char value[PROPERTY_VALUE_MAX]; 1759e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1); 1769e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (doLog) { 1779e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters"); 1789e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 17946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 180da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) property_get("ro.debuggable", value, "0"); 181da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int debuggable = atoi(value); 182da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int teeEnabled = 0; 183da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (debuggable) { 184da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) property_get("af.tee", value, "0"); 185da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten teeEnabled = atoi(value); 186da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 187f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten // FIXME symbolic constants here 1886e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten if (teeEnabled & 1) { 189da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkInputEnabled = true; 1906e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten } 1916e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten if (teeEnabled & 2) { 192da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkOutputEnabled = true; 1936e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten } 1946e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten if (teeEnabled & 4) { 195da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkTrackEnabled = true; 1966e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten } 19746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 1985a61d2f277af3098fc10b2881babca16391362daDima Zavin} 1995a61d2f277af3098fc10b2881babca16391362daDima Zavin 2005a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef() 2015a61d2f277af3098fc10b2881babca16391362daDima Zavin{ 202799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin int rc = 0; 203fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 204935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent Mutex::Autolock _l(mLock); 205935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent 206799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin /* TODO: move all this work into an Init() function */ 2074ff14bae91075eb274eb1c2975982358946e7e63John Grossman char val_str[PROPERTY_VALUE_MAX] = { 0 }; 2084ff14bae91075eb274eb1c2975982358946e7e63John Grossman if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { 2094ff14bae91075eb274eb1c2975982358946e7e63John Grossman uint32_t int_val; 2104ff14bae91075eb274eb1c2975982358946e7e63John Grossman if (1 == sscanf(val_str, "%u", &int_val)) { 2114ff14bae91075eb274eb1c2975982358946e7e63John Grossman mStandbyTimeInNsecs = milliseconds(int_val); 2124ff14bae91075eb274eb1c2975982358946e7e63John Grossman ALOGI("Using %u mSec as standby time.", int_val); 2134ff14bae91075eb274eb1c2975982358946e7e63John Grossman } else { 2144ff14bae91075eb274eb1c2975982358946e7e63John Grossman mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; 2154ff14bae91075eb274eb1c2975982358946e7e63John Grossman ALOGI("Using default %u mSec as standby time.", 2164ff14bae91075eb274eb1c2975982358946e7e63John Grossman (uint32_t)(mStandbyTimeInNsecs / 1000000)); 2174ff14bae91075eb274eb1c2975982358946e7e63John Grossman } 2184ff14bae91075eb274eb1c2975982358946e7e63John Grossman } 21965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 220a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mMode = AUDIO_MODE_NORMAL; 22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger() 22465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 22565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian while (!mRecordThreads.isEmpty()) { 226c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten // closeInput_nonvirtual() will remove specified entry from mRecordThreads 227d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten closeInput_nonvirtual(mRecordThreads.keyAt(0)); 22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian while (!mPlaybackThreads.isEmpty()) { 230c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads 231d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); 23265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 233799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 2342b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 2352b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten // no mHardwareLock needed, as there are no other references to this 236a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); 237a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent delete mAudioHwDevs.valueAt(i); 23865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 239481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten 240481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Tell media.log service about any old writers that still need to be unregistered 241481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 242481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten if (binder != 0) { 243481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); 244481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { 245481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory()); 246481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mUnregisteredWriters.pop(); 247481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mediaLogService->unregisterWriter(iMemory); 248481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 249481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 250481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten 25165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 25265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 253a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = { 254a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_PRIMARY, 255a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_A2DP, 256a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_USB, 257a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent}; 258a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) 259a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 260ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l( 261ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_module_handle_t module, 262ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_devices_t devices) 263799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{ 264a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // if module is 0, the request comes from an old policy manager and we should load 265a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // well known modules 266a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent if (module == 0) { 267a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); 268a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { 269a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent loadHwModule_l(audio_interfaces[i]); 270a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 271f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent // then try to find a module supporting the requested device. 272f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 273f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); 274f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent audio_hw_device_t *dev = audioHwDevice->hwDevice(); 275f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent if ((dev->get_supported_devices != NULL) && 276f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent (dev->get_supported_devices(dev) & devices) == devices) 277f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent return audioHwDevice; 278f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent } 279a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } else { 280a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // check a match for the requested module handle 281ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); 282ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (audioHwDevice != NULL) { 283ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman return audioHwDevice; 284a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 285a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 286a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 287799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return NULL; 288799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin} 28965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2900f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused) 29165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 29265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 29465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append("Clients:\n"); 29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mClients.size(); ++i) { 29877c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten sp<Client> client = mClients.valueAt(i).promote(); 29977c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten if (client != 0) { 30077c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten snprintf(buffer, SIZE, " pid: %d\n", client->pid()); 30177c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten result.append(buffer); 30265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 3043a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen 305d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent result.append("Notification Clients:\n"); 306d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent for (size_t i = 0; i < mNotificationClients.size(); ++i) { 307d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i)); 308d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent result.append(buffer); 309d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent } 310d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 3113a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen result.append("Global session refs:\n"); 312b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen result.append(" session pid count\n"); 3133a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { 3143a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen AudioSessionRef *r = mAudioSessionRefs[i]; 315b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen snprintf(buffer, SIZE, " %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt); 3163a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen result.append(buffer); 3173a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 3220f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused) 32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 327a4454b4765c5905f14186893b0688be375642283Glenn Kasten hardware_call_state hardwareStatus = mHardwareStatus; 32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 3294ff14bae91075eb274eb1c2975982358946e7e63John Grossman snprintf(buffer, SIZE, "Hardware status: %d\n" 3304ff14bae91075eb274eb1c2975982358946e7e63John Grossman "Standby Time mSec: %u\n", 3314ff14bae91075eb274eb1c2975982358946e7e63John Grossman hardwareStatus, 3324ff14bae91075eb274eb1c2975982358946e7e63John Grossman (uint32_t)(mStandbyTimeInNsecs / 1000000)); 33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append(buffer); 33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 3370f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused) 33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian "can't dump AudioFlinger from pid=%d, uid=%d\n", 34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian IPCThreadState::self()->getCallingPid(), 34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian IPCThreadState::self()->getCallingUid()); 34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append(buffer); 34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 35081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex) 35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian bool locked = false; 35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (int i = 0; i < kDumpLockRetries; ++i) { 35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (mutex.tryLock() == NO_ERROR) { 35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian locked = true; 35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian break; 35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 3587dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten usleep(kDumpLockSleepUs); 35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return locked; 36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args) 36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 36544deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten if (!dumpAllowed()) { 36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpPermissionDenial(fd, args); 36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // get state of hardware lock 36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool hardwareLocked = dumpTryLock(mHardwareLock); 37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!hardwareLocked) { 37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result(kHardwareLockedString); 37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareLock.unlock(); 37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = dumpTryLock(mLock); 37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // failed to lock - AudioFlinger is probably deadlocked 38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!locked) { 38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result(kDeadlockedString); 38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpClients(fd, args); 38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpInternals(fd, args); 38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // dump playback threads 38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mPlaybackThreads.valueAt(i)->dump(fd, args); 39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // dump record threads 39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mRecordThreads.size(); i++) { 39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mRecordThreads.valueAt(i)->dump(fd, args); 39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 398799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin // dump all hardware devs 399799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 400a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 401799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin dev->dump(dev, fd); 40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 403d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 40446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 405d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten // dump the serially shared record tee sink 406d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten if (mRecordTeeSource != 0) { 407d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten dumpTee(fd, mRecordTeeSource); 408d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten } 40946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 410d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 411d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten if (locked) { 412d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten mLock.unlock(); 413d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten } 4149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 4159e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // append a copy of media.log here by forwarding fd to it, but don't attempt 4169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // to lookup the service if it's not running, as it will block for a second 4179e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (mLogMemoryDealer != 0) { 4189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 4199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (binder != 0) { 4209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten fdprintf(fd, "\nmedia.log:\n"); 4219e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten Vector<String16> args; 4229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten binder->dump(fd, args); 4239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4249e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 42998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kastensp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) 43098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{ 43198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten // If pid is already in the mClients wp<> map, then use that entry 43298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten // (for which promote() is always != 0), otherwise create a new entry and Client. 43398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten sp<Client> client = mClients.valueFor(pid).promote(); 43498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten if (client == 0) { 43598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten client = new Client(this, pid); 43698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten mClients.add(pid, client); 43798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten } 43898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten 43998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten return client; 44098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten} 44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4429e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) 4439e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{ 444481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // If there is no memory allocated for logs, return a dummy writer that does nothing 4459e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (mLogMemoryDealer == 0) { 4469e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten return new NBLog::Writer(); 4479e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4489e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 449481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Similarly if we can't contact the media.log service, also return a dummy writer 450481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten if (binder == 0) { 451481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten return new NBLog::Writer(); 4529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 453481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IMediaLogService> mediaLogService(interface_cast<IMediaLogService>(binder)); 454481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); 455481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // If allocation fails, consult the vector of previously unregistered writers 456481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // and garbage-collect one or more them until an allocation succeeds 457481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten if (shared == 0) { 458481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten Mutex::Autolock _l(mUnregisteredWritersLock); 459481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { 460481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten { 461481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Pick the oldest stale writer to garbage-collect 462481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory()); 463481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mUnregisteredWriters.removeAt(0); 464481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mediaLogService->unregisterWriter(iMemory); 465481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Now the media.log remote reference to IMemory is gone. When our last local 466481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // reference to IMemory also drops to zero at end of this block, 467481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // the IMemory destructor will deallocate the region from mLogMemoryDealer. 468481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 469481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Re-attempt the allocation 470481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); 471481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten if (shared != 0) { 472481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten goto success; 473481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 474481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 475481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Even after garbage-collecting all old writers, there is still not enough memory, 476481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // so return a dummy writer 477481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten return new NBLog::Writer(); 478481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten } 479481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kastensuccess: 480481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mediaLogService->registerWriter(shared, size, name); 481481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten return new NBLog::Writer(size, shared); 4829e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten} 4839e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 4849e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) 4859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{ 486685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten if (writer == 0) { 487685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten return; 488685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten } 4899e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IMemory> iMemory(writer->getIMemory()); 4909e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (iMemory == 0) { 4919e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten return; 4929e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 493481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // Rather than removing the writer immediately, append it to a queue of old writers to 494481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten // be garbage-collected later. This allows us to continue to view old logs for a while. 495481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten Mutex::Autolock _l(mUnregisteredWritersLock); 496481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten mUnregisteredWriters.push(writer); 4979e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten} 4989e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 49965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface 50065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 50165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 50265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack( 503fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten audio_stream_type_t streamType, 50465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian uint32_t sampleRate, 50558f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten audio_format_t format, 506254af180475346b6186b49c297f340c9c4817511Glenn Kasten audio_channel_mask_t channelMask, 50774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *frameCount, 508e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten IAudioFlinger::track_flags_t *flags, 50965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const sp<IMemory>& sharedBuffer, 51072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output, 5113acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten pid_t tid, 51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian int *sessionId, 513462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen int clientUid, 51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t *status) 51565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<PlaybackThread::Track> track; 51765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<TrackHandle> trackHandle; 51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<Client> client; 51965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t lStatus; 52065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian int lSessionId; 52165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 522263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, 523263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten // but if someone uses binder directly they could bypass that and cause us to crash 524263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { 52529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("createTrack() invalid stream type %d", streamType); 52665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian lStatus = BAD_VALUE; 52765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian goto Exit; 52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 52965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 53053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten // further sample rate checks are performed by createTrack_l() depending on the thread type 53153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten if (sampleRate == 0) { 53253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten ALOGE("createTrack() invalid sample rate %u", sampleRate); 53353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten lStatus = BAD_VALUE; 53453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten goto Exit; 53553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten } 53653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten 53753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten // further channel mask checks are performed by createTrack_l() depending on the thread type 53853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten if (!audio_is_output_channel(channelMask)) { 53953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten ALOGE("createTrack() invalid channel mask %#x", channelMask); 54053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten lStatus = BAD_VALUE; 54153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten goto Exit; 54253b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten } 54353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten 54460a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten // client is responsible for conversion of 8-bit PCM to 16-bit PCM, 54560a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten // and we don't yet support 8.24 or 32-bit PCM 54653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten if (!audio_is_valid_format(format) || 54753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT)) { 548cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("createTrack() invalid format %#x", format); 54960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten lStatus = BAD_VALUE; 550663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten goto Exit; 551663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten } 552663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten 553663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten if (sharedBuffer != 0 && sharedBuffer->pointer() == NULL) { 554663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten ALOGE("createTrack() sharedBuffer is non-0 but has NULL pointer()"); 555663c2247b71086e30bfd3192979d1dd7f15c539eGlenn Kasten lStatus = BAD_VALUE; 55660a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten goto Exit; 55760a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten } 55860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten 55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { 56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 56265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 563c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGE("no playback thread found for output handle %d", output); 56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian lStatus = BAD_VALUE; 56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian goto Exit; 56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 56765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 5688d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 56998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten client = registerPid_l(pid); 57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 571e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten PlaybackThread *effectThread = NULL; 572aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { 573570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten lSessionId = *sessionId; 574f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent // check if an effect chain with the same session ID is present on another 575f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent // output thread and move it here. 576de070137f11d346fba77605bd76a44c040a618fcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 57739e94f8f723d445447fdee0822291e664b631f60Eric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 57839e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (mPlaybackThreads.keyAt(i) != output) { 579570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten uint32_t sessions = t->hasAudioSession(lSessionId); 58039e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (sessions & PlaybackThread::EFFECT_SESSION) { 58139e94f8f723d445447fdee0822291e664b631f60Eric Laurent effectThread = t.get(); 582f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent break; 58339e94f8f723d445447fdee0822291e664b631f60Eric Laurent } 584de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 585de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 587de070137f11d346fba77605bd76a44c040a618fcEric Laurent // if no audio session id is provided, create one here 5887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent lSessionId = nextUniqueId(); 58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (sessionId != NULL) { 59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *sessionId = lSessionId; 59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 59265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 5933856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("createTrack() lSessionId: %d", lSessionId); 59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian track = thread->createTrack_l(client, streamType, sampleRate, format, 596462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, clientUid, &lStatus); 59703e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); 5982fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless 59939e94f8f723d445447fdee0822291e664b631f60Eric Laurent 60039e94f8f723d445447fdee0822291e664b631f60Eric Laurent // move effect chain to this output thread if an effect on same session was waiting 60139e94f8f723d445447fdee0822291e664b631f60Eric Laurent // for a track to be created 60239e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (lStatus == NO_ERROR && effectThread != NULL) { 6032fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten // no risk of deadlock because AudioFlinger::mLock is held 60439e94f8f723d445447fdee0822291e664b631f60Eric Laurent Mutex::Autolock _dl(thread->mLock); 60539e94f8f723d445447fdee0822291e664b631f60Eric Laurent Mutex::Autolock _sl(effectThread->mLock); 60639e94f8f723d445447fdee0822291e664b631f60Eric Laurent moveEffectChain_l(lSessionId, effectThread, thread, true); 60739e94f8f723d445447fdee0822291e664b631f60Eric Laurent } 608a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent 609a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent // Look for sync events awaiting for a session to be used. 610a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { 611a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { 612a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { 6132986460984580833161bdaabc7f17da1005a8961Eric Laurent if (lStatus == NO_ERROR) { 614d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten (void) track->setSyncEvent(mPendingSyncEvents[i]); 6152986460984580833161bdaabc7f17da1005a8961Eric Laurent } else { 6162986460984580833161bdaabc7f17da1005a8961Eric Laurent mPendingSyncEvents[i]->cancel(); 6172986460984580833161bdaabc7f17da1005a8961Eric Laurent } 618a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent mPendingSyncEvents.removeAt(i); 619a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent i--; 620a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 621a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 622a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 623e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 625e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 6263ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten if (lStatus != NO_ERROR) { 6273ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten // remove local strong reference to Client before deleting the Track so that the 6283ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten // Client destructor is called by the TrackBase destructor with mLock held 62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian client.clear(); 63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian track.clear(); 6313ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten goto Exit; 63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 63365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 6343ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten // return handle to client 6353ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten trackHandle = new TrackHandle(track); 6363ef14ef30359376006a233f6a21a165d4b65a7dfGlenn Kasten 63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit: 6389156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return trackHandle; 64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 64272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const 64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 6475ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("sampleRate() unknown thread %d", output); 64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->sampleRate(); 65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 65372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenint AudioFlinger::channelCount(audio_io_handle_t output) const 65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 6585ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("channelCount() unknown thread %d", output); 65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->channelCount(); 66265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 66472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const 66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 6695ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("format() unknown thread %d", output); 67058f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten return AUDIO_FORMAT_INVALID; 67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->format(); 67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 67572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const 67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 6805ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("frameCount() unknown thread %d", output); 68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 68358912562617941964939a4182cda71eaeb153d4bGlenn Kasten // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; 68458912562617941964939a4182cda71eaeb153d4bGlenn Kasten // should examine all callers and fix them to handle smaller counts 68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->frameCount(); 68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 68872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const 68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 693c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGW("latency(): no playback thread found for output handle %d", output); 69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->latency(); 69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value) 70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 701a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 702a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 703a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 704a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 705a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 711a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 712ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mMasterVolume = value; 713a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 714ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Set master volume in the HALs which support it. 715ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 716ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AutoMutex lock(mHardwareLock); 717ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 7184ff14bae91075eb274eb1c2975982358946e7e63John Grossman 719ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 720ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (dev->canSetMasterVolume()) { 721ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->hwDevice()->set_master_volume(dev->hwDevice(), value); 722935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent } 723ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_IDLE; 72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 72565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 726ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Now set the master volume in each playback thread. Playback threads 727ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // assigned to HALs which do not have master volume support will apply 728ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // master volume during the mix operation. Threads with HALs which do 729ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // support master volume will simply ignore the setting. 7308d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 731ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mPlaybackThreads.valueAt(i)->setMasterVolume(value); 73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 73465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 736f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode) 73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 738a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 739a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 740a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 741a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 74265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 74365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 74465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 74665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 747930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten if (uint32_t(mode) >= AUDIO_MODE_CNT) { 7485ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("Illegal value: setMode(%d)", mode); 74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { // scope for the lock 75365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 754ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_SET_MODE; 756ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_mode(dev, mode); 75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 76065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (NO_ERROR == ret) { 76165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mMode = mode; 7638d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 764e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setMode(mode); 76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 77065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state) 77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 772a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 773a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 774a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 775a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 776a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 77765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 783ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; 785ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_mic_mute(dev, state); 78665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const 79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 792a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 793a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 794a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return false; 795a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 796a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 797fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin bool state = AUDIO_MODE_INVALID; 7982b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten AutoMutex lock(mHardwareLock); 799ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; 801ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->get_mic_mute(dev, &state); 80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return state; 80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted) 80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 808d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman status_t ret = initCheck(); 809d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman if (ret != NO_ERROR) { 810d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman return ret; 811d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 812d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 81565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 81765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 818ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman Mutex::Autolock _l(mLock); 819ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mMasterMute = muted; 820d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 821ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Set master mute in the HALs which support it. 822ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 823ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AutoMutex lock(mHardwareLock); 824ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 825d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 826ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 827ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (dev->canSetMasterMute()) { 828ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->hwDevice()->set_master_mute(dev->hwDevice(), muted); 829d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 830ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_IDLE; 831d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 832d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 833ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Now set the master mute in each playback thread. Playback threads 834ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // assigned to HALs which do not have master mute support will apply master 835ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // mute during the mix operation. Threads with HALs which do support master 836ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // mute will simply ignore the setting. 8378d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 838ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mPlaybackThreads.valueAt(i)->setMasterMute(muted); 83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const 84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 8459806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten Mutex::Autolock _l(mLock); 8469806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten return masterVolume_l(); 84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const 85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 8519806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten Mutex::Autolock _l(mLock); 8529806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten return masterMute_l(); 85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 8554ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const 8564ff14bae91075eb274eb1c2975982358946e7e63John Grossman{ 8574ff14bae91075eb274eb1c2975982358946e7e63John Grossman return mMasterVolume; 8584ff14bae91075eb274eb1c2975982358946e7e63John Grossman} 8594ff14bae91075eb274eb1c2975982358946e7e63John Grossman 860d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const 861d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{ 862ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman return mMasterMute; 863d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman} 864d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 86572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, 86672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output) 86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 873263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 87429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("setStreamVolume() invalid stream %d", stream); 87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 87865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mLock); 87965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = NULL; 88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (output) { 88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkPlaybackThread_l(output); 88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 88665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 88765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mStreamTypes[stream].volume = value; 88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 8908d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 891e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); 89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread->setStreamVolume(stream, value); 89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 900fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) 90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 907263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT || 908fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { 90929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("setStreamMute() invalid stream %d", stream); 91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 913935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent AutoMutex lock(mLock); 91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mStreamTypes[stream].mute = muted; 91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) 916e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); 91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 91965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 92065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 92172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const 92265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 923263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0.0f; 92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mLock); 92865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian float volume; 92965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (output) { 93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 93165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0.0f; 93365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian volume = thread->streamVolume(stream); 93565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 9366637baae4244aec731c4014da72418d330636ae1Glenn Kasten volume = streamVolume_l(stream); 93765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 93965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return volume; 94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 94165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 942fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const 94365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 944263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return true; 94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 9486637baae4244aec731c4014da72418d330636ae1Glenn Kasten AutoMutex lock(mLock); 9496637baae4244aec731c4014da72418d330636ae1Glenn Kasten return streamMute_l(stream); 95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 95272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) 95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 954827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d", 955827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid()); 95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 95765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // ioHandle == 0 means the parameters are global to the audio hardware interface 96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (ioHandle == 0) { 964a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 965799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin status_t final_result = NO_ERROR; 9668abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten { 967a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AutoMutex lock(mHardwareLock); 968a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mHardwareStatus = AUDIO_HW_SET_PARAMETER; 969a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 970a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 971a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent status_t result = dev->set_parameters(dev, keyValuePairs.string()); 972a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent final_result = result ?: final_result; 973a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 974a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 9758abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten } 97659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 97759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent AudioParameter param = AudioParameter(keyValuePairs); 97859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent String8 value; 97959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { 980bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); 981bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent if (mBtNrecIsOff != btNrecIsOff) { 98259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 98359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent sp<RecordThread> thread = mRecordThreads.valueAt(i); 984f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent audio_devices_t device = thread->inDevice(); 985510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; 986510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten // collect all of the thread's session IDs 987510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten KeyedVector<int, bool> ids = thread->sessionIds(); 988510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten // suspend effects associated with those session IDs 989510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten for (size_t j = 0; j < ids.size(); ++j) { 990510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten int sessionId = ids.keyAt(j); 99159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent thread->setEffectSuspended(FX_IID_AEC, 99259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent suspend, 993510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten sessionId); 99459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent thread->setEffectSuspended(FX_IID_NS, 99559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent suspend, 996510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten sessionId); 99759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 99859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 999bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent mBtNrecIsOff = btNrecIsOff; 100059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 100159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 100228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten String8 screenState; 100328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { 100428ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten bool isOff = screenState == "off"; 100581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isOff != (AudioFlinger::mScreenState & 1)) { 100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; 100728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 100828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 1009799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return final_result; 101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 101165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // hold a strong ref on thread in case closeOutput() or closeInput() is called 101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // and the thread is exited once the lock is released 101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<ThreadBase> thread; 101565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { 101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkPlaybackThread_l(ioHandle); 1018d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten if (thread == 0) { 101965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkRecordThread_l(ioHandle); 10207fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten } else if (thread == primaryPlaybackThread_l()) { 10217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent // indicate output device change to all input threads for pre processing 10227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent AudioParameter param = AudioParameter(keyValuePairs); 10237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent int value; 102489d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && 102589d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent (value != 0)) { 10267c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 10277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent mRecordThreads.valueAt(i)->setParameters(keyValuePairs); 10287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent } 10297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent } 103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 103165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 10327378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten if (thread != 0) { 10337378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten return thread->setParameters(keyValuePairs); 103465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 103565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 103665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 103765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 103872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const 103965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1040827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGVV("getParameters() io %d, keys %s, calling pid %d", 1041827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); 104265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1043a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 1044a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 104565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (ioHandle == 0) { 1046fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin String8 out_s8; 1047fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 1048799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 10498abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten char *s; 10508abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten { 10518abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten AutoMutex lock(mHardwareLock); 10528abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_GET_PARAMETER; 1053a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 10548abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten s = dev->get_parameters(dev, keys.string()); 10558abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_IDLE; 10568abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten } 1057ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman out_s8 += String8(s ? s : ""); 1058799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin free(s); 1059799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin } 1060fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin return out_s8; 106165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 106265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 106365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); 106465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (playbackThread != NULL) { 106565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return playbackThread->getParameters(keys); 106665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 106765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian RecordThread *recordThread = checkRecordThread_l(ioHandle); 106865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (recordThread != NULL) { 106965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return recordThread->getParameters(keys); 107065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 107165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return String8(""); 107265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 107365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1074dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, 1075dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten audio_channel_mask_t channelMask) const 107665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1077a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 1078a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 1079a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return 0; 1080a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 1081a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 10822b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten AutoMutex lock(mHardwareLock); 10832b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; 1084ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1085ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald memset(&config, 0, sizeof(config)); 1086ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = sampleRate; 1087ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = channelMask; 1088ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = format; 1089ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 1090ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 1091ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman size_t size = dev->get_input_buffer_size(dev, &config); 10922b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten mHardwareStatus = AUDIO_HW_IDLE; 10932b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten return size; 109465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 109565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 10965f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const 109765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 109865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 109965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 110065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian RecordThread *recordThread = checkRecordThread_l(ioHandle); 110165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (recordThread != NULL) { 110265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return recordThread->getInputFramesLost(); 110365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 110465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 110565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 110665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 110765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value) 110865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1109a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 1110a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 1111a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 1112a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 1113a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 111465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 111565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 111665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 111765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 111865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 111965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 1120ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 11218abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; 1122ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_voice_volume(dev, value); 112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 112465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 112765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1128377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, 112972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output) const 113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t status; 113265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 113365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 113465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 113565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *playbackThread = checkPlaybackThread_l(output); 113665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (playbackThread != NULL) { 113765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return playbackThread->getRenderPosition(halFrames, dspFrames); 113865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 113965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 114065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 114165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 114265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 114365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) 114465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 114565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 114665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 114765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1148bb001926447d0b7dc71ca8bb3c9856f3136d8f4cGlenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 114965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (mNotificationClients.indexOfKey(pid) < 0) { 115065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<NotificationClient> notificationClient = new NotificationClient(this, 115165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian client, 115265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian pid); 11533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); 115465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 115565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mNotificationClients.add(pid, notificationClient); 115665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 115765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<IBinder> binder = client->asBinder(); 115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian binder->linkToDeath(notificationClient); 115965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 116065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // the config change is always sent from playback or record threads to avoid deadlock 116165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // with AudioSystem::gLock 116265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1163896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED); 116465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 116565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 116665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mRecordThreads.size(); i++) { 1167896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED); 116865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 116965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 117065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 117265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid) 117365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 117465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 117565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1176a3b09254d44cd8d66ec947abe547538c4cfeaa89Glenn Kasten mNotificationClients.removeItem(pid); 11773a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen 11783856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("%d died, releasing its sessions", pid); 11798d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten size_t num = mAudioSessionRefs.size(); 11803a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen bool removed = false; 11818d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i< num; ) { 11823a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 1183012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten ALOGV(" pid %d @ %d", ref->mPid, i); 1184012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten if (ref->mPid == pid) { 1185012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); 11863a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen mAudioSessionRefs.removeAt(i); 11873a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen delete ref; 11883a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen removed = true; 11893a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen num--; 11908d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten } else { 11918d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten i++; 11923a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 11933a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 11943a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen if (removed) { 11953a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen purgeStaleEffects_l(); 11963a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 119765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 119865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 119965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// audioConfigChanged_l() must be called with AudioFlinger::mLock held 1200b81cc8c6f3eec9edb255ea99b6a6f243585b1e38Glenn Kastenvoid AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2) 120165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 120265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian size_t size = mNotificationClients.size(); 120365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < size; i++) { 120484afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, 120584afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten param2); 120665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 120765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 120965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// removeClient_l() must be called with AudioFlinger::mLock held 121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid) 121165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1212827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGV("removeClient_l() pid %d, calling pid %d", pid, 121385ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten IPCThreadState::self()->getCallingPid()); 121465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mClients.removeItem(pid); 121565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 121665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1217717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held 1218717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId) 1219717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{ 1220717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent sp<PlaybackThread> thread; 1221717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent 1222717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1223717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { 1224717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent ALOG_ASSERT(thread == 0); 1225717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent thread = mPlaybackThreads.valueAt(i); 1226717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent } 1227717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent } 1228717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent 1229717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent return thread; 1230717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent} 123165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 123465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 123565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 123681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) 123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : RefBase(), 123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger(audioFlinger), 123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below 124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), 124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPid(pid), 124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount(0) 124365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer 124565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 124665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Client destructor must be called with AudioFlinger::mLock held 124881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client() 124965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 125081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->removeClient_l(mPid); 125165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 125265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const 125465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mMemoryDealer; 125665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 125765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack() 126165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const int kMaxTimedTracksPerClient = 4; 126365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mTimedTrackLock); 126565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mTimedTrackCount >= kMaxTimedTracksPerClient) { 126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("can not create timed track - pid %d has exceeded the limit", 126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPid); 126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 127065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 127165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 127281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount++; 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 127465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 127565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 127681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track 127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack() 127865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mTimedTrackLock); 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount--; 1281896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent} 1282896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent 128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 128581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, 128681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IAudioFlingerClient>& client, 128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t pid) 128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) 1289896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{ 129065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 129165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 129281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient() 129365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 129465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 129565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 12960f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) 129765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 129881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NotificationClient> keep(this); 129981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->removeNotificationClient(mPid); 130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 130165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 130265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 130465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1305893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) { 1306893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown return audio_is_remote_submix_device(inDevice); 1307893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown} 1308893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown 130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord( 131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t input, 131181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 131474935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten size_t *frameCount, 1315eeca32671896739e84050da5992d5f151a1629deGlenn Kasten IAudioFlinger::track_flags_t *flags, 131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 131781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *sessionId, 131881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 13191d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{ 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> recordTrack; 132181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordHandle> recordHandle; 132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Client> client; 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int lSessionId; 13251d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 132681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check calling permissions 132781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!recordingAllowed()) { 1328e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten ALOGE("openRecord() permission denied: recording not allowed"); 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 13321d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 133353b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten // further sample rate checks are performed by createRecordTrack_l() 133453b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten if (sampleRate == 0) { 133553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten ALOGE("openRecord() invalid sample rate %u", sampleRate); 133653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten lStatus = BAD_VALUE; 133753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten goto Exit; 133853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten } 133953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten 13400e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten // we don't yet support anything other than 16-bit PCM 13410e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten if (!(audio_is_valid_format(format) && 13420e0e846f3d86906ff990025981e514d6bcf44d8eGlenn Kasten audio_is_linear_pcm(format) && format == AUDIO_FORMAT_PCM_16_BIT)) { 1343cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGE("openRecord() invalid format %#x", format); 1344291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten lStatus = BAD_VALUE; 134553b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten goto Exit; 134653b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten } 134753b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten 134853b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten // further channel mask checks are performed by createRecordTrack_l() 134953b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten if (!audio_is_input_channel(channelMask)) { 135053b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten ALOGE("openRecord() invalid channel mask %#x", channelMask); 135153b5d098b7d5d4412b9b9fe08daaf84c56b716e9Glenn Kasten lStatus = BAD_VALUE; 1352291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten goto Exit; 1353291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 1354291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten 135505997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten { 135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 1357e848bd9abb3bbbd4c66f9fb9d1442f7663d7ba40Glenn Kasten RecordThread *thread = checkRecordThread_l(input); 135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 1359e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten ALOGE("openRecord() checkRecordThread_l failed"); 136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 13621d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent } 13631d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 1364893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice()) 1365893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown && !captureAudioOutputAllowed()) { 1366e93cf2ca27ae6f4a81d4ef548bbf10a34db6d98fGlenn Kasten ALOGE("openRecord() permission denied: capture not allowed"); 1367893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown lStatus = PERMISSION_DENIED; 1368893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown goto Exit; 1369893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown } 1370893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown 13718d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent client = registerPid_l(pid); 1373feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 1374aea7ea06394bcb155972d82055d4ea59962e4051Glenn Kasten if (sessionId != NULL && *sessionId != AUDIO_SESSION_ALLOCATE) { 137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lSessionId = *sessionId; 1376feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } else { 1377570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten // if no audio session id is provided, create one here 137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lSessionId = nextUniqueId(); 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != NULL) { 138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *sessionId = lSessionId; 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1382feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 1383570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten ALOGV("openRecord() lSessionId: %d", lSessionId); 1384570f633e0b02d1bc25f3312b92e72cc29a40ca38Glenn Kasten 1385462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen // TODO: the uid should be passed in as a parameter to openRecord 138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, 1387462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen frameCount, lSessionId, 1388462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen IPCThreadState::self()->getCallingUid(), 1389462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen flags, tid, &lStatus); 139003e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); 1391feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 1392e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten 139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove local strong reference to Client before deleting the RecordTrack so that the 139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Client destructor is called by the TrackBase destructor with mLock held 139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent client.clear(); 139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack.clear(); 139881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 1399feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 1400feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 14012fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten // return handle to client 140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordHandle = new RecordHandle(recordTrack); 1403feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 140481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 14059156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 140681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return recordHandle; 1407feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent} 1408feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 1409feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 14101d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name) 141459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 141581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!settingsAllowed()) { 141681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 141859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent Mutex::Autolock _l(mLock); 141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return loadHwModule_l(name); 142059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 142159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held 142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) 142459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { 142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("loadHwModule() module %s already loaded", name); 142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mAudioHwDevs.keyAt(i); 142959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 143059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 143159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *dev; 143359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int rc = load_audio_interface(name, &dev); 143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (rc) { 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() error %d loading module %s ", rc, name); 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 143859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 143959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 144081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_INIT; 144181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent rc = dev->init_check(dev); 144281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (rc) { 144481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() init check error %d for module %s ", rc, name); 144581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 144659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 144759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check and cache this HAL's level of support for master mute and master 144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // volume. If this is the first HAL opened, and it supports the get 145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // methods, use the initial values provided by the HAL as the current 145181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // master mute and volume settings. 145259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); 145481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for auto-lock pattern 145581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mHardwareLock); 145659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (0 == mAudioHwDevs.size()) { 145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; 145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (NULL != dev->get_master_volume) { 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float mv; 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (OK == dev->get_master_volume(dev, &mv)) { 146281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = mv; 146381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 146481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; 146781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (NULL != dev->get_master_mute) { 146881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool mm; 146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (OK == dev->get_master_mute(dev, &mm)) { 147081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = mm; 147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 147359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((NULL != dev->set_master_volume) && 147781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (OK == dev->set_master_volume(dev, mMasterVolume))) { 147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags = static_cast<AudioHwDevice::Flags>(flags | 147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); 148059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 148159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((NULL != dev->set_master_mute) && 148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (OK == dev->set_master_mute(dev, mMasterMute))) { 148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags = static_cast<AudioHwDevice::Flags>(flags | 148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); 148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 148859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 149059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 149159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_module_handle_t handle = nextUniqueId(); 149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags)); 149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", 149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, dev->common.module->name, dev->common.module->id, handle); 149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 150059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 150159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate() 150559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 150659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent Mutex::Autolock _l(mLock); 150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL ? thread->sampleRate() : 0; 1509a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent} 151059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount() 1512a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{ 151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 151481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 151581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL ? thread->frameCountHAL() : 0; 151659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 151759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 151865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 151965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 15204182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice) 15214182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{ 15224182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten uid_t uid = IPCThreadState::self()->getCallingUid(); 15234182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten if (uid != AID_SYSTEM) { 15244182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return PERMISSION_DENIED; 15254182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten } 15264182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten Mutex::Autolock _l(mLock); 15274182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten if (mIsDeviceTypeKnown) { 15284182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return INVALID_OPERATION; 15294182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten } 15304182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsLowRamDevice = isLowRamDevice; 15314182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsDeviceTypeKnown = true; 15324182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return NO_ERROR; 15334182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten} 15344182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten 15354182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ---------------------------------------------------------------------------- 15364182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten 153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, 153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t *pDevices, 153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pSamplingRate, 154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t *pFormat, 154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t *pChannelMask, 154281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pLatencyMs, 1543ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald audio_output_flags_t flags, 1544ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald const audio_offload_info_t *offloadInfo) 154565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1546ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1547fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten memset(&config, 0, sizeof(config)); 1548ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0; 1549ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0; 1550ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT; 1551937098b9b564a779ff1c8c2d9e60769ee5c69810Glenn Kasten if (offloadInfo != NULL) { 1552ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.offload_info = *offloadInfo; 1553ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald } 1554ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 1555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x", 155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent module, 155781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pDevices != NULL) ? *pDevices : 0, 155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags); 1562bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput(), offloadInfo %p version 0x%04x", 1563e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version); 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDevices == NULL || *pDevices == 0) { 156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 1567ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman } 1568ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman 156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 157065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 15713255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten AudioHwDevice *outHwDev = findSuitableHwDev_l(module, *pDevices); 15726e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten if (outHwDev == NULL) { 157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 15746e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten } 157565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *hwDevHal = outHwDev->hwDevice(); 157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 157865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; 158065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 15813255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten audio_stream_out_t *outStream = NULL; 158234542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten status_t status = hwDevHal->open_output_stream(hwDevHal, 158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent id, 158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *pDevices, 158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (audio_output_flags_t)flags, 158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &config, 158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &outStream); 158865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 1590bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, " 159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "Channels %x, status %d", 159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outStream, 159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 159581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status); 159758912562617941964939a4182cda71eaeb153d4bGlenn Kasten 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR && outStream != NULL) { 1599bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags); 160065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 16013255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten PlaybackThread *thread; 1602bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 1603bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent thread = new OffloadThread(this, output, id, *pDevices); 1604bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput() created offload output: ID %d thread %p", id, thread); 1605bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || 160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.format != AUDIO_FORMAT_PCM_16_BIT) || 160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { 160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = new DirectOutputThread(this, output, id, *pDevices); 160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); 161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = new MixerThread(this, output, id, *pDevices); 161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); 161365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.add(id, thread); 161588cbea8a918bbaf5e06e48aadd5af5e81d58d232Glenn Kasten 16167c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pSamplingRate != NULL) { 16177c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pSamplingRate = config.sample_rate; 16187c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 16197c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pFormat != NULL) { 16207c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pFormat = config.format; 16217c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 16227c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pChannelMask != NULL) { 16237c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pChannelMask = config.channel_mask; 16247c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 16257c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pLatencyMs != NULL) { 16267c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pLatencyMs = thread->latency(); 16277c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 162865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new output creation 163081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 163165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first primary output opened designates the primary hw device 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { 163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("Using module %d has the primary audio interface", module); 163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPrimaryHardwareDev = outHwDev; 163681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 163781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mHardwareLock); 163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MODE; 163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hwDevHal->set_mode(hwDevHal, mMode); 164081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 164281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 164381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 164465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 164581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 164665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 164765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 164881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, 164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t output2) 165065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 165181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 165281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread *thread1 = checkMixerThread_l(output1); 165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread *thread2 = checkMixerThread_l(output2); 165481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread1 == NULL || thread2 == NULL) { 165681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, 165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent output2); 165881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 165965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 166065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); 166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->addOutputTrack(thread2); 166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.add(id, thread); 166581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new output creation 166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 166865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 166965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output) 16712bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{ 167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return closeOutput_nonvirtual(output); 16732bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi} 16742bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi 167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) 167665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // keep strong reference on the playback thread so that 167881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is not destroyed while exit() is executed 167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<PlaybackThread> thread; 168081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 168181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkPlaybackThread_l(output); 168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 168565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 168665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("closeOutput() %d", output); 1688de070137f11d346fba77605bd76a44c040a618fcEric Laurent 168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread->type() == ThreadBase::MIXER) { 169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 169181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { 169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DuplicatingThread *dupThread = 169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); 169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dupThread->removeOutputTrack((MixerThread *)thread.get()); 1695bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1696de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1697de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1698de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1699bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1700bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.removeItem(output); 1702bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // save all effects to the default thread 1703bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mPlaybackThreads.size()) { 1704bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); 1705bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (dstThread != NULL) { 1706bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // audioflinger lock is held here so the acquisition order of thread locks does not 1707bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // matter 1708bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _dl(dstThread->mLock); 1709bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _sl(thread->mLock); 1710bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); 1711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 1712bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true); 1713bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1714bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1715bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1716bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); 17173acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten } 171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->exit(); 171981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The thread entity (active unit of execution) is no longer running here, 172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but the ThreadBase container still exists. 17213acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten 172281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread->type() != ThreadBase::DUPLICATING) { 172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *out = thread->clearOutput(); 172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); 172581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from now on thread->mOutput is NULL 172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent out->hwDev()->close_output_stream(out->hwDev(), out->stream); 172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete out; 172865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 172981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 173065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 173165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 173281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output) 1733e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{ 173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 173681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 173881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1739e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent } 1740e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent 174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("suspendOutput() %d", output); 174281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->suspend(); 1743e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent 174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 174565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 174665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output) 174865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 17496637baae4244aec731c4014da72418d330636ae1Glenn Kasten Mutex::Autolock _l(mLock); 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 175165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1754ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman } 175565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("restoreOutput() %d", output); 175765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->restore(); 175965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 176165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 176265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 176381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, 176481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t *pDevices, 176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pSamplingRate, 176681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t *pFormat, 176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t *pChannelMask) 176865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1769ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1770fb872cc6f77f6d74011fab703b3edd7023c736cbGlenn Kasten memset(&config, 0, sizeof(config)); 1771ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0; 1772ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0; 1773ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT; 1774ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 177581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t reqSamplingRate = config.sample_rate; 177681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t reqFormat = config.format; 1777f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten audio_channel_mask_t reqChannelMask = config.channel_mask; 177865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 177981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDevices == NULL || *pDevices == 0) { 178081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 1781b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent } 1782b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent 178381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 178465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 17853255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten AudioHwDevice *inHwDev = findSuitableHwDev_l(module, *pDevices); 17866e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten if (inHwDev == NULL) { 178781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 17886e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten } 1789fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 179081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *inHwHal = inHwDev->hwDevice(); 179181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 1792b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 17933255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten audio_stream_in_t *inStream = NULL; 17943255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten status_t status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, 179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &inStream); 1796cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %#x, Channels %x, " 179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "status %d", 179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inStream, 179981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 180081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 180281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status); 180365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 180481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the input could not be opened with the requested parameters and we can handle the 180581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conversion internally, try to open again with the proposed parameters. The AudioFlinger can 180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs. 180781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == BAD_VALUE && 180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && 180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.sample_rate <= 2 * reqSamplingRate) && 1810f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannelMask) <= FCC_2)) { 18118594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // FIXME describe the change proposed by HAL (save old values so we can log them here) 181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openInput() reopening with proposed sampling rate and channel mask"); 181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inStream = NULL; 181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream); 18158594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten // FIXME log this new status; HAL should not propose any further changes 181665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 181765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 181881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR && inStream != NULL) { 181958912562617941964939a4182cda71eaeb153d4bGlenn Kasten 182046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 182181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Try to re-use most recently used Pipe to archive a copy of input for dumpsys, 182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or (re-)create if current Pipe is idle and does not match the new format 182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> teeSink; 182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum { 182581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_NO, // don't copy input 182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_NEW, // copy input using a new pipe 182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_OLD, // copy input using an existing pipe 182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } kind; 182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common), 183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent popcount(inStream->common.get_channels(&inStream->common))); 1831da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (!mTeeSinkInputEnabled) { 1832da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten kind = TEE_SINK_NO; 18336e0d67d7b496ce17c0970a4ffd3a6f808860949cGlenn Kasten } else if (!Format_isValid(format)) { 183481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NO; 183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mRecordTeeSink == 0) { 183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NEW; 183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mRecordTeeSink->getStrongCount() != 1) { 183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NO; 1839f66b42242342017c26eb97de544dae31dd2537caGlenn Kasten } else if (Format_isEqual(format, mRecordTeeSink->format())) { 184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_OLD; 18414adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten } else { 184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NEW; 184381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 184481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kind) { 184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_NEW: { 1846da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten Pipe *pipe = new Pipe(mTeeSinkInputFrames, format); 184781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); 185081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PipeReader *pipeReader = new PipeReader(*pipe); 185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent numCounterOffers = 0; 185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); 185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 185581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordTeeSink = pipe; 185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordTeeSource = pipeReader; 185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent teeSink = pipe; 18584adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten } 185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_OLD: 186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent teeSink = mRecordTeeSink; 186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_NO: 186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 186658912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 186746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 1868da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); 187065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start record thread 187234af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten // RecordThread requires both input and output device indication to forward to audio 187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // pre processing modules 18743255095af6677caeeebf81c8891ce8db745617ffGlenn Kasten RecordThread *thread = new RecordThread(this, 187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent input, 187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent id, 1877d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent primaryOutputDevice_l(), 187846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten *pDevices 187946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 188046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , teeSink 188146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 188246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ); 188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordThreads.add(id, thread); 188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openInput() created record thread: ID %d thread %p", id, thread); 18857c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pSamplingRate != NULL) { 18867c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pSamplingRate = reqSamplingRate; 18877c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 18887c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pFormat != NULL) { 18897c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pFormat = config.format; 18907c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 18917c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pChannelMask != NULL) { 1892f506e9495f2b2123b3bec7d42ec8ef13c6213ee8Glenn Kasten *pChannelMask = reqChannelMask; 18937c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 189465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new input creation 189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); 189781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 18981afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent } 189981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 190081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 190165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 190265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 190381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input) 190465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 190581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return closeInput_nonvirtual(input); 190665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 190765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) 1909de070137f11d346fba77605bd76a44c040a618fcEric Laurent{ 191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // keep strong reference on the record thread so that 191181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is not destroyed while exit() is executed 191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread> thread; 191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 191581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkRecordThread_l(input); 191681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 191781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1918de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 191981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 192081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("closeInput() %d", input); 192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); 192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordThreads.removeItem(input); 1923de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->exit(); 192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The thread entity (active unit of execution) is no longer running here, 192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but the ThreadBase container still exists. 1927de070137f11d346fba77605bd76a44c040a618fcEric Laurent 192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *in = thread->clearInput(); 192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); 193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from now on thread->mInput is NULL 193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent in->hwDev()->close_input_stream(in->hwDev(), in->stream); 193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete in; 193365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 1935b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent} 1936b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 1937d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kastenstatus_t AudioFlinger::invalidateStream(audio_stream_type_t stream) 1938b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{ 1939b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent Mutex::Autolock _l(mLock); 1940d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten ALOGV("invalidateStream() stream %d", stream); 1941b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->invalidateTracks(stream); 1945b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent } 194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 1948b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent} 1949b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::newAudioSessionId() 1952162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{ 195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return nextUniqueId(); 1954162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent} 1955162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent 1956d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::acquireAudioSessionId(int audioSession, pid_t pid) 1957a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{ 1958a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent Mutex::Autolock _l(mLock); 195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t caller = IPCThreadState::self()->getCallingPid(); 1960d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); 1961d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen if (pid != -1 && (caller == getpid_cached)) { 1962d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen caller = pid; 1963d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen } 1964d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 1965d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // Ignore requests received from processes not known as notification client. The request 1966d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be 1967d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // called from a different pid leaving a stale session reference. Also we don't know how 1968d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // to clear this reference if the client process dies. 1969d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent if (mNotificationClients.indexOfKey(caller) < 0) { 1970d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); 1971d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent return; 1972d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent } 1973d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t num = mAudioSessionRefs.size(); 197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i< num; i++) { 197681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); 197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == audioSession && ref->mPid == caller) { 197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ref->mCnt++; 197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" incremented refcount to %d", ref->mCnt); 198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 1981a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 1982a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); 198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" added new entry for %d", audioSession); 1985a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent} 1986a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent 1987d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissenvoid AudioFlinger::releaseAudioSessionId(int audioSession, pid_t pid) 198844a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{ 198981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t caller = IPCThreadState::self()->getCallingPid(); 1991d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen ALOGV("releasing %d from %d for %d", audioSession, caller, pid); 1992d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen if (pid != -1 && (caller == getpid_cached)) { 1993d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen caller = pid; 1994d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen } 199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t num = mAudioSessionRefs.size(); 199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i< num; i++) { 199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == audioSession && ref->mPid == caller) { 199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ref->mCnt--; 200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" decremented refcount to %d", ref->mCnt); 200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mCnt == 0) { 200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSessionRefs.removeAt(i); 200381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete ref; 200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent purgeStaleEffects_l(); 200544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 200681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 200744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 200844a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 2009d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // If the caller is mediaserver it is likely that the session being released was acquired 2010d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // on behalf of a process not in notification clients and we ignore the warning. 2011d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller); 201244a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent} 201344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent 201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() { 201565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("purging stale effects"); 2017fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten 201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > chains; 201958912562617941964939a4182cda71eaeb153d4bGlenn Kasten 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < t->mEffectChains.size(); j++) { 202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = t->mEffectChains[j]; 202481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { 202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chains.push(ec); 202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2027c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten } 202858912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread> t = mRecordThreads.valueAt(i); 203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < t->mEffectChains.size(); j++) { 203281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = t->mEffectChains[j]; 203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chains.push(ec); 203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2035300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten } 203665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < chains.size(); i++) { 203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = chains[i]; 203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionid = ec->sessionId(); 204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> t = ec->mThread.promote(); 204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numsessionrefs = mAudioSessionRefs.size(); 204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool found = false; 204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t k = 0; k < numsessionrefs; k++) { 204781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); 204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == sessionid) { 204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" session %d still exists for %d with %d refs", 205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionid, ref->mPid, ref->mCnt); 205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent found = true; 205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 205358912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 205458912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!found) { 2056e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten Mutex::Autolock _l(t->mLock); 205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all effects from the chain 205881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (ec->mEffects.size()) { 205981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = ec->mEffects[0]; 206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->unPin(); 206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent t->removeEffect_l(effect); 206281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->purgeHandles()) { 206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); 206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 206581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 206681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 2067c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten } 206858912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 207065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 207165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held 207381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const 2074190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{ 207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mPlaybackThreads.valueFor(output).get(); 2076190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten} 2077190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held 207981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const 208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; 208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 2084190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held 208681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const 208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mRecordThreads.valueFor(input).get(); 208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 2090190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId() 209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return android_atomic_inc(&mNextUniqueId); 209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 209583efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten 209681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const 209737d825e72a6c606553a745da1212590a425996d3Glenn Kasten{ 209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = thread->getOutput(); 210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { 210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread; 210337d825e72a6c606553a745da1212590a425996d3Glenn Kasten } 210437d825e72a6c606553a745da1212590a425996d3Glenn Kasten } 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 210637d825e72a6c606553a745da1212590a425996d3Glenn Kasten} 210737d825e72a6c606553a745da1212590a425996d3Glenn Kasten 210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const 210965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 2111000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 21149f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten } 2115000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread->outDevice(); 2117000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2118688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten 211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, 212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession, 212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int listenerSession, 212281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sync_event_callback_t callBack, 21238ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent wp<RefBase> cookie) 212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 212673ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten 212781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); 212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t playStatus = NAME_NOT_FOUND; 212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t recStatus = NAME_NOT_FOUND; 213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); 213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playStatus == NO_ERROR) { 213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 213565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 213781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); 213881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recStatus == NO_ERROR) { 213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 21409f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten } 214165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { 214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPendingSyncEvents.add(event); 214481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 214581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createSyncEvent() invalid event %d", event->type()); 214681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent event.clear(); 214781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 214881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 214965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 215065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Effect management 215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 215458912562617941964939a4182cda71eaeb153d4bGlenn Kasten 215558912562617941964939a4182cda71eaeb153d4bGlenn Kasten 215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const 2157000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectQueryNumberEffects(numEffects); 2160000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2161000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 216281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const 216358912562617941964939a4182cda71eaeb153d4bGlenn Kasten{ 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectQueryEffect(index, descriptor); 216658912562617941964939a4182cda71eaeb153d4bGlenn Kasten} 216758912562617941964939a4182cda71eaeb153d4bGlenn Kasten 216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, 216981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *descriptor) const 2170000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectGetDescriptor(pUuid, descriptor); 2173000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2174000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 21768d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect( 217781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *pDesc, 217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 218081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t io, 218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status, 218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *id, 218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled) 2185000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus = NO_ERROR; 218781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 218881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc; 2189000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 21908d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", 219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid, effectClient.get(), priority, sessionId, io); 2193000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 219481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDesc == NULL) { 219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2197952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 2198000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check audio settings permission for global effects 220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { 220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2203952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 2204000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects 220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that can only be created by audio policy manager (running in same process) 220781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { 220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2210952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 221165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!EffectIsNullUuid(&pDesc->uuid)) { 221481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if uuid is specified, request effect descriptor 221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); 221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); 221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if uuid is not specified, look for an available implementation 222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // of the required type in effect factory 222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectIsNullUuid(&pDesc->type)) { 222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() no effect type"); 222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2227288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t numEffects = 0; 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t d; 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent d.flags = 0; // prevent compiler warning 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool found = false; 2232288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten 223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectQueryNumberEffects(&numEffects); 223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); 223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (uint32_t i = 0; i < numEffects; i++) { 223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectQueryEffect(i, &desc); 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); 224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 2243d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { 224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If matching type found save effect descriptor. If the session is 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 0 and the effect is not auxiliary, continue enumeration in case 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // an auxiliary version of this effect type is available 224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent found = true; 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent d = desc; 225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != AUDIO_SESSION_OUTPUT_MIX || 225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 2252d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten break; 2253d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 2254d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 2255288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!found) { 225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() effect not found"); 225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For same effect type, chose auxiliary version over insert version if 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // connect to output mix (Compliance to OpenSL ES) 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX && 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = d; 226658912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 226758912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 226858912562617941964939a4182cda71eaeb153d4bGlenn Kasten 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not allow auxiliary effects on a session different from 0 (output mix) 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != AUDIO_SESSION_OUTPUT_MIX && 227181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 227281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = INVALID_OPERATION; 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 22743dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent } 227565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check recording permission for visualizer 227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && 227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !recordingAllowed()) { 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 228265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return effect descriptor 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *pDesc = desc; 2285eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent if (io == 0 && sessionId == AUDIO_SESSION_OUTPUT_MIX) { 2286eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent // if the output returned by getOutputForEffect() is removed before we lock the 2287eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent // mutex below, the call to checkPlaybackThread_l(io) below will detect it 2288eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent // and we will exit safely 2289eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent io = AudioSystem::getOutputForEffect(&desc); 2290eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent ALOGV("createEffect got output %d", io); 2291eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent } 2292eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent 2293eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent Mutex::Autolock _l(mLock); 229465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If output is not specified try to find a matching audio session ID in one of the 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output threads. 229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because of code checking output when entering the function. 229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Note: io is never 0 when creating an effect on an input 230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0) { 23015baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { 23025baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // output must be specified by AudioPolicyManager when using session 23035baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // AUDIO_SESSION_OUTPUT_STAGE 23045baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 23055baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 23065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 2307eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent // look for the thread where the specified audio session is present 2308eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 2309eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 2310eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent io = mPlaybackThreads.keyAt(i); 2311eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent break; 2312eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent } 2313d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten } 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0) { 2315eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 2316eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 2317eb3c337a3d6c74ec857dfc8be7eeafe634614bcdEric Laurent io = mRecordThreads.keyAt(i); 231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 23192986460984580833161bdaabc7f17da1005a8961Eric Laurent } 232065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 232165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no output thread contains the requested session ID, default to 232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // first output. The effect chain will be moved to the correct output 232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // thread when a track with the same session ID is created 232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0 && mPlaybackThreads.size()) { 232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent io = mPlaybackThreads.keyAt(0); 232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect() got io %d for effect %s", io, desc.name); 232965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadBase *thread = checkRecordThread_l(io); 233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkPlaybackThread_l(io); 233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createEffect() unknown output thread"); 233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2337288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 2338288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 2339288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten 234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Client> client = registerPid_l(pid); 234158912562617941964939a4182cda71eaeb153d4bGlenn Kasten 234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect on selected output thread 234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = thread->createEffect_l(client, effectClient, priority, sessionId, 234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &desc, enabled, &lStatus); 234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (handle != 0 && id != NULL) { 234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *id = handle->id(); 234765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 234865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 234965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 235081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 23519156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten *status = lStatus; 235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 235366fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten} 235466fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten 235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, 235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t dstOutput) 235765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", 235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcOutput, dstOutput); 236065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (srcOutput == dstOutput) { 236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() same dst and src outputs %d", dstOutput); 236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); 236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (srcThread == NULL) { 236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() bad srcOutput %d", srcOutput); 236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); 237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstThread == NULL) { 237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() bad dstOutput %d", dstOutput); 237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 237465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 237565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _dl(dstThread->mLock); 237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _sl(srcThread->mLock); 23785baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent return moveEffectChain_l(sessionId, srcThread, dstThread, false); 237965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 238065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held 238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId, 238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::PlaybackThread *srcThread, 238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::PlaybackThread *dstThread, 238581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reRegister) 238665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", 238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcThread, dstThread); 238965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); 239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", 239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcThread); 239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 23969ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang 239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove chain first. This is useful only if reconfiguring effect chain on same output thread, 239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that a new chain is created with correct parameters when first effect is added. This is 239981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is 240081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // removed. 240181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent srcThread->removeEffectChain_l(chain); 24029ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang 240381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // transfer all effects one by one so that new effect chain is created on new thread with 240481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // correct buffer sizes and audio parameters and effect engines reconfigured accordingly 240581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> dstChain; 240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = 0; // prevent compiler warning 240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = chain->getEffectFromId_l(0); 24085baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent Vector< sp<EffectModule> > removed; 24095baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status_t status = NO_ERROR; 241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (effect != 0) { 241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent srcThread->removeEffect_l(effect); 24125baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent removed.add(effect); 24135baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status = dstThread->addEffect_l(effect); 24145baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (status != NO_ERROR) { 24155baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 24165baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 241781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // removeEffect_l() has stopped the effect if it was active so it must be restarted 241881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->state() == EffectModule::ACTIVE || 241981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->state() == EffectModule::STOPPING) { 242081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->start(); 242165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 242281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the move request is not received from audio policy manager, the effect must be 242381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // re-registered with the new strategy and output 242481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstChain == 0) { 242581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dstChain = effect->chain().promote(); 242681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstChain == 0) { 242781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); 24285baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status = NO_INIT; 24295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 243065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 243181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy = dstChain->strategy(); 243265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 243381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (reRegister) { 243481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 243581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::registerEffect(&effect->desc(), 24365baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent dstThread->id(), 243781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, 243881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, 243981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->id()); 2440d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); 244181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 244281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromId_l(0); 244358912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 244458912562617941964939a4182cda71eaeb153d4bGlenn Kasten 24455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (status != NO_ERROR) { 24465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent for (size_t i = 0; i < removed.size(); i++) { 24475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent srcThread->addEffect_l(removed[i]); 24485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (dstChain != 0 && reRegister) { 24495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent AudioSystem::unregisterEffect(removed[i]->id()); 24505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent AudioSystem::registerEffect(&removed[i]->desc(), 24515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent srcThread->id(), 24525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent strategy, 24535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent sessionId, 24545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent removed[i]->id()); 2455d72b7c0180ee83fc3754629ed68fc5887a125c4cEric Laurent AudioSystem::setEffectEnabled(effect->id(), effect->isEnabled()); 24565baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 24575baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 24585baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 24595baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 24605baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent return status; 246165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 246265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 24635baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() 2464813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{ 2465813e2a74853bde19e37d878c596a044b3f299efcEric Laurent if (mGlobalEffectEnableTime != 0 && 2466813e2a74853bde19e37d878c596a044b3f299efcEric Laurent ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { 2467813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return true; 2468813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2469813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2470813e2a74853bde19e37d878c596a044b3f299efcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 2471813e2a74853bde19e37d878c596a044b3f299efcEric Laurent sp<EffectChain> ec = 2472813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 24735baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (ec != 0 && ec->isNonOffloadableEnabled()) { 2474813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return true; 2475813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2476813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2477813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return false; 2478813e2a74853bde19e37d878c596a044b3f299efcEric Laurent} 2479813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 24805baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable() 2481813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{ 2482813e2a74853bde19e37d878c596a044b3f299efcEric Laurent Mutex::Autolock _l(mLock); 2483813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2484813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mGlobalEffectEnableTime = systemTime(); 2485813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2486813e2a74853bde19e37d878c596a044b3f299efcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 2487813e2a74853bde19e37d878c596a044b3f299efcEric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 2488813e2a74853bde19e37d878c596a044b3f299efcEric Laurent if (t->mType == ThreadBase::OFFLOAD) { 2489813e2a74853bde19e37d878c596a044b3f299efcEric Laurent t->invalidateTracks(AUDIO_STREAM_MUSIC); 2490813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2491813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2492813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2493813e2a74853bde19e37d878c596a044b3f299efcEric Laurent} 2494813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2495da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry { 2496da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav 2497da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten char mName[MAX_NAME]; 2498da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}; 2499da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 2500da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2) 2501da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{ 2502da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName); 2503da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten} 2504da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 250546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 2506d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id) 250765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 2508d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten NBAIO_Source *teeSource = source.get(); 2509fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (teeSource != NULL) { 2510da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // .wav rotation 2511da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // There is a benign race condition if 2 threads call this simultaneously. 2512da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // They would both traverse the directory, but the result would simply be 2513da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // failures at unlink() which are ignored. It's also unlikely since 2514da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // normally dumpsys is only done by bugreport or from the command line. 2515da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten char teePath[32+256]; 2516da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(teePath, "/data/misc/media"); 2517da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t teePathLen = strlen(teePath); 2518da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten DIR *dir = opendir(teePath); 2519da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten teePath[teePathLen++] = '/'; 2520da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (dir != NULL) { 2521da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort 2522da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep 2523da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct Entry entries[MAX_SORT]; 2524da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t entryCount = 0; 2525da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten while (entryCount < MAX_SORT) { 2526da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct dirent de; 2527da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct dirent *result = NULL; 2528da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int rc = readdir_r(dir, &de, &result); 2529da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (rc != 0) { 2530da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOGW("readdir_r failed %d", rc); 2531da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2532da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2533da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (result == NULL) { 2534da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2535da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2536da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (result != &de) { 2537da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOGW("readdir_r returned unexpected result %p != %p", result, &de); 2538da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2539da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2540da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // ignore non .wav file entries 2541da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t nameLen = strlen(de.d_name); 2542da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (nameLen <= 4 || nameLen >= MAX_NAME || 2543da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcmp(&de.d_name[nameLen - 4], ".wav")) { 2544da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten continue; 2545da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2546da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(entries[entryCount++].mName, de.d_name); 2547da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2548da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) closedir(dir); 2549da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (entryCount > MAX_KEEP) { 2550da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten qsort(entries, entryCount, sizeof(Entry), comparEntry); 2551da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) { 2552da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(&teePath[teePathLen], entries[i].mName); 2553da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) unlink(teePath); 2554da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2555da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2556da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } else { 2557da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2558da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno)); 2559da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2560da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2561d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten char teeTime[16]; 2562fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten struct timeval tv; 2563fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten gettimeofday(&tv, NULL); 2564fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten struct tm tm; 2565fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten localtime_r(&tv.tv_sec, &tm); 2566da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm); 2567da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id); 2568da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd 2569da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); 2570fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (teeFd >= 0) { 2571fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten char wavHeader[44]; 2572fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten memcpy(wavHeader, 2573fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten "RIFF\0\0\0\0WAVEfmt \20\0\0\0\1\0\2\0\104\254\0\0\0\0\0\0\4\0\20\0data\0\0\0\0", 2574fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten sizeof(wavHeader)); 2575fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten NBAIO_Format format = teeSource->format(); 2576fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten unsigned channelCount = Format_channelCount(format); 2577fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten ALOG_ASSERT(channelCount <= FCC_2); 25783b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten uint32_t sampleRate = Format_sampleRate(format); 2579fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[22] = channelCount; // number of channels 2580fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[24] = sampleRate; // sample rate 2581fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[25] = sampleRate >> 8; 2582fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[32] = channelCount * 2; // block alignment 2583fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, wavHeader, sizeof(wavHeader)); 2584fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten size_t total = 0; 2585fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten bool firstRead = true; 2586fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten for (;;) { 2587fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten#define TEE_SINK_READ 1024 2588fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten short buffer[TEE_SINK_READ * FCC_2]; 2589fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten size_t count = TEE_SINK_READ; 25902c3b2da3049627264b7c6b449a1622f002210f03John Grossman ssize_t actual = teeSource->read(buffer, count, 25912c3b2da3049627264b7c6b449a1622f002210f03John Grossman AudioBufferProvider::kInvalidPTS); 2592fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten bool wasFirstRead = firstRead; 2593fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten firstRead = false; 2594fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (actual <= 0) { 2595fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (actual == (ssize_t) OVERRUN && wasFirstRead) { 2596fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten continue; 2597fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2598fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten break; 2599fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2600a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent ALOG_ASSERT(actual <= (ssize_t)count); 2601fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, buffer, actual * channelCount * sizeof(short)); 2602fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten total += actual; 2603fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2604fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten lseek(teeFd, (off_t) 4, SEEK_SET); 2605fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten uint32_t temp = 44 + total * channelCount * sizeof(short) - 8; 2606fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, &temp, sizeof(temp)); 2607fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten lseek(teeFd, (off_t) 40, SEEK_SET); 2608fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten temp = total * channelCount * sizeof(short); 2609fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, &temp, sizeof(temp)); 2610fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten close(teeFd); 2611da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2612da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "tee copied to %s\n", teePath); 2613da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2614fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } else { 2615da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2616da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno)); 2617da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2618fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2619fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2620d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten} 262146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 2622d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 262365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 262465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 262565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact( 262665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 262765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 262865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BnAudioFlinger::onTransact(code, data, reply, flags); 262965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 263065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 263165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android 2632