AudioFlinger.cpp revision d1b28d41dbda203ffb420ba2e36cbe736b163ff8
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 107f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurentstatic int load_audio_interface(const char *if_name, audio_hw_device_t **dev) 108799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{ 109f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent const hw_module_t *mod; 110799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin int rc; 111799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 112f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); 113f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE_IF(rc, "%s couldn't load audio hw module %s.%s (%s)", __func__, 114f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 115f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if (rc) { 116799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin goto out; 117f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 118f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = audio_hw_device_open(mod, dev); 119f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE_IF(rc, "%s couldn't open audio hw device in %s.%s (%s)", __func__, 120f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 121f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if (rc) { 122799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin goto out; 123f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 124f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent if ((*dev)->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { 125f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); 126f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent rc = BAD_VALUE; 127f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent goto out; 128f7ffb8bf0a58037f0bc9662c5275005a4e539948Eric Laurent } 129799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return 0; 130799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 131799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavinout: 132799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin *dev = NULL; 133799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return rc; 134799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin} 135799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 13665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 13765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 13865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::AudioFlinger() 13965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian : BnAudioFlinger(), 1404ff14bae91075eb274eb1c2975982358946e7e63John Grossman mPrimaryHardwareDev(NULL), 1417d6c35bf132a46c0a8a9826491882495fc98bd8cGlenn Kasten mHardwareStatus(AUDIO_HW_IDLE), 1424ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMasterVolume(1.0f), 1434ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMasterMute(false), 1444ff14bae91075eb274eb1c2975982358946e7e63John Grossman mNextUniqueId(1), 1454ff14bae91075eb274eb1c2975982358946e7e63John Grossman mMode(AUDIO_MODE_INVALID), 1464182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mBtNrecIsOff(false), 1474182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsLowRamDevice(true), 148813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mIsDeviceTypeKnown(false), 149813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mGlobalEffectEnableTime(0) 15065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 151949a926cadbc961fbb649c91d76d7aee8ea4d7bdGlenn Kasten getpid_cached = getpid(); 1529e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten char value[PROPERTY_VALUE_MAX]; 1539e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1); 1549e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (doLog) { 1559e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters"); 1569e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 15746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 158da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) property_get("ro.debuggable", value, "0"); 159da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int debuggable = atoi(value); 160da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int teeEnabled = 0; 161da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (debuggable) { 162da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) property_get("af.tee", value, "0"); 163da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten teeEnabled = atoi(value); 164da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 165da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (teeEnabled & 1) 166da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkInputEnabled = true; 167da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (teeEnabled & 2) 168da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkOutputEnabled = true; 169da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (teeEnabled & 4) 170da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten mTeeSinkTrackEnabled = true; 17146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 1725a61d2f277af3098fc10b2881babca16391362daDima Zavin} 1735a61d2f277af3098fc10b2881babca16391362daDima Zavin 1745a61d2f277af3098fc10b2881babca16391362daDima Zavinvoid AudioFlinger::onFirstRef() 1755a61d2f277af3098fc10b2881babca16391362daDima Zavin{ 176799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin int rc = 0; 177fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 178935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent Mutex::Autolock _l(mLock); 179935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent 180799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin /* TODO: move all this work into an Init() function */ 1814ff14bae91075eb274eb1c2975982358946e7e63John Grossman char val_str[PROPERTY_VALUE_MAX] = { 0 }; 1824ff14bae91075eb274eb1c2975982358946e7e63John Grossman if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { 1834ff14bae91075eb274eb1c2975982358946e7e63John Grossman uint32_t int_val; 1844ff14bae91075eb274eb1c2975982358946e7e63John Grossman if (1 == sscanf(val_str, "%u", &int_val)) { 1854ff14bae91075eb274eb1c2975982358946e7e63John Grossman mStandbyTimeInNsecs = milliseconds(int_val); 1864ff14bae91075eb274eb1c2975982358946e7e63John Grossman ALOGI("Using %u mSec as standby time.", int_val); 1874ff14bae91075eb274eb1c2975982358946e7e63John Grossman } else { 1884ff14bae91075eb274eb1c2975982358946e7e63John Grossman mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; 1894ff14bae91075eb274eb1c2975982358946e7e63John Grossman ALOGI("Using default %u mSec as standby time.", 1904ff14bae91075eb274eb1c2975982358946e7e63John Grossman (uint32_t)(mStandbyTimeInNsecs / 1000000)); 1914ff14bae91075eb274eb1c2975982358946e7e63John Grossman } 1924ff14bae91075eb274eb1c2975982358946e7e63John Grossman } 19365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 194a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mMode = AUDIO_MODE_NORMAL; 19565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 19765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioFlinger::~AudioFlinger() 19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 19965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian while (!mRecordThreads.isEmpty()) { 200c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten // closeInput_nonvirtual() will remove specified entry from mRecordThreads 201d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten closeInput_nonvirtual(mRecordThreads.keyAt(0)); 20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 20365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian while (!mPlaybackThreads.isEmpty()) { 204c3ae93f21280859086ae371428ffd32f39e76d50Glenn Kasten // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads 205d96c5724818fb47917bb5e7abe37799735e1ec0eGlenn Kasten closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); 20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 207799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin 2082b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 2092b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten // no mHardwareLock needed, as there are no other references to this 210a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_close(mAudioHwDevs.valueAt(i)->hwDevice()); 211a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent delete mAudioHwDevs.valueAt(i); 21265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 21365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 215a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurentstatic const char * const audio_interfaces[] = { 216a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_PRIMARY, 217a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_A2DP, 218a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AUDIO_HARDWARE_MODULE_ID_USB, 219a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent}; 220a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0]))) 221a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 222ee578c0330319f04a48bccbdb26b53fea0388d04John GrossmanAudioFlinger::AudioHwDevice* AudioFlinger::findSuitableHwDev_l( 223ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_module_handle_t module, 224ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_devices_t devices) 225799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin{ 226a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // if module is 0, the request comes from an old policy manager and we should load 227a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // well known modules 228a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent if (module == 0) { 229a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); 230a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) { 231a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent loadHwModule_l(audio_interfaces[i]); 232a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 233f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent // then try to find a module supporting the requested device. 234f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 235f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); 236f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent audio_hw_device_t *dev = audioHwDevice->hwDevice(); 237f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent if ((dev->get_supported_devices != NULL) && 238f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent (dev->get_supported_devices(dev) & devices) == devices) 239f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent return audioHwDevice; 240f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent } 241a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } else { 242a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent // check a match for the requested module handle 243ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); 244ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (audioHwDevice != NULL) { 245ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman return audioHwDevice; 246a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 247a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 248a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 249799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return NULL; 250799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin} 25165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 252be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpClients(int fd, const Vector<String16>& args) 25365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 25465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 25665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 25765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 25865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append("Clients:\n"); 25965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mClients.size(); ++i) { 26077c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten sp<Client> client = mClients.valueAt(i).promote(); 26177c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten if (client != 0) { 26277c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten snprintf(buffer, SIZE, " pid: %d\n", client->pid()); 26377c1119ea0b5cb32287088ceeeb7e3b6bd14a85dGlenn Kasten result.append(buffer); 26465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 26565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 2663a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen 267d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent result.append("Notification Clients:\n"); 268d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent for (size_t i = 0; i < mNotificationClients.size(); ++i) { 269d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent snprintf(buffer, SIZE, " pid: %d\n", mNotificationClients.keyAt(i)); 270d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent result.append(buffer); 271d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent } 272d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 2733a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen result.append("Global session refs:\n"); 274012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten result.append(" session pid count\n"); 2753a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { 2763a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen AudioSessionRef *r = mAudioSessionRefs[i]; 277012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten snprintf(buffer, SIZE, " %7d %3d %3d\n", r->mSessionid, r->mPid, r->mCnt); 2783a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen result.append(buffer); 2793a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 28065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 28165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 28265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 28365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 284be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpInternals(int fd, const Vector<String16>& args) 28565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 28665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 28765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 28865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 289a4454b4765c5905f14186893b0688be375642283Glenn Kasten hardware_call_state hardwareStatus = mHardwareStatus; 29065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 2914ff14bae91075eb274eb1c2975982358946e7e63John Grossman snprintf(buffer, SIZE, "Hardware status: %d\n" 2924ff14bae91075eb274eb1c2975982358946e7e63John Grossman "Standby Time mSec: %u\n", 2934ff14bae91075eb274eb1c2975982358946e7e63John Grossman hardwareStatus, 2944ff14bae91075eb274eb1c2975982358946e7e63John Grossman (uint32_t)(mStandbyTimeInNsecs / 1000000)); 29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append(buffer); 29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 299be5f05e0fdfc4e3799653702187861a2afa072eeGlenn Kastenvoid AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args) 30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 30165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const size_t SIZE = 256; 30265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian char buffer[SIZE]; 30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result; 30465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 30565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian "can't dump AudioFlinger from pid=%d, uid=%d\n", 30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian IPCThreadState::self()->getCallingPid(), 30765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian IPCThreadState::self()->getCallingUid()); 30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian result.append(buffer); 30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::dumpTryLock(Mutex& mutex) 31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian bool locked = false; 31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (int i = 0; i < kDumpLockRetries; ++i) { 31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (mutex.tryLock() == NO_ERROR) { 31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian locked = true; 31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian break; 31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 3207dede876998ff56351d495ec3a798c1b131193e8Glenn Kasten usleep(kDumpLockSleepUs); 32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return locked; 32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::dump(int fd, const Vector<String16>& args) 32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 32744deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten if (!dumpAllowed()) { 32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpPermissionDenial(fd, args); 32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // get state of hardware lock 33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool hardwareLocked = dumpTryLock(mHardwareLock); 33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!hardwareLocked) { 33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result(kHardwareLockedString); 33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareLock.unlock(); 33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool locked = dumpTryLock(mLock); 34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // failed to lock - AudioFlinger is probably deadlocked 34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!locked) { 34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian String8 result(kDeadlockedString); 34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian write(fd, result.string(), result.size()); 34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpClients(fd, args); 34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian dumpInternals(fd, args); 34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // dump playback threads 35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mPlaybackThreads.valueAt(i)->dump(fd, args); 35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // dump record threads 35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mRecordThreads.size(); i++) { 35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mRecordThreads.valueAt(i)->dump(fd, args); 35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 360799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin // dump all hardware devs 361799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 362a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 363799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin dev->dump(dev, fd); 36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 365d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 36646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 367d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten // dump the serially shared record tee sink 368d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten if (mRecordTeeSource != 0) { 369d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten dumpTee(fd, mRecordTeeSource); 370d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten } 37146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 372d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 373d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten if (locked) { 374d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten mLock.unlock(); 375d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten } 3769e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 3779e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // append a copy of media.log here by forwarding fd to it, but don't attempt 3789e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // to lookup the service if it's not running, as it will block for a second 3799e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (mLogMemoryDealer != 0) { 3809e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 3819e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (binder != 0) { 3829e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten fdprintf(fd, "\nmedia.log:\n"); 3839e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten Vector<String16> args; 3849e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten binder->dump(fd, args); 3859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 3869e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 39198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kastensp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid) 39298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten{ 39398ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten // If pid is already in the mClients wp<> map, then use that entry 39498ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten // (for which promote() is always != 0), otherwise create a new entry and Client. 39598ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten sp<Client> client = mClients.valueFor(pid).promote(); 39698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten if (client == 0) { 39798ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten client = new Client(this, pid); 39898ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten mClients.add(pid, client); 39998ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten } 40098ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten 40198ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten return client; 40298ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten} 40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4049e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastensp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) 4059e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{ 4069e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (mLogMemoryDealer == 0) { 4079e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten return new NBLog::Writer(); 4089e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4099e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); 4109e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<NBLog::Writer> writer = new NBLog::Writer(size, shared); 4119e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 4129e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (binder != 0) { 4139e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten interface_cast<IMediaLogService>(binder)->registerWriter(shared, size, name); 4149e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4159e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten return writer; 4169e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten} 4179e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 4189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kastenvoid AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) 4199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten{ 420685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten if (writer == 0) { 421685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten return; 422685ef09bcaf5de6abf2064d552296f70eaec6761Glenn Kasten } 4239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IMemory> iMemory(writer->getIMemory()); 4249e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (iMemory == 0) { 4259e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten return; 4269e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4279e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten sp<IBinder> binder = defaultServiceManager()->getService(String16("media.log")); 4289e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten if (binder != 0) { 4299e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten interface_cast<IMediaLogService>(binder)->unregisterWriter(iMemory); 4309e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // Now the media.log remote reference to IMemory is gone. 4319e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // When our last local reference to IMemory also drops to zero, 4329e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten // the IMemory destructor will deallocate the region from mMemoryDealer. 4339e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten } 4349e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten} 4359e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten 43665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// IAudioFlinger interface 43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiansp<IAudioTrack> AudioFlinger::createTrack( 440fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten audio_stream_type_t streamType, 44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian uint32_t sampleRate, 44258f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten audio_format_t format, 443254af180475346b6186b49c297f340c9c4817511Glenn Kasten audio_channel_mask_t channelMask, 444e33054eb968cbf8ccaee1b0ff0301403902deed6Glenn Kasten size_t frameCount, 445e0b07179a48ee50fda931d2aa1b3c751d167e4d7Glenn Kasten IAudioFlinger::track_flags_t *flags, 44665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian const sp<IMemory>& sharedBuffer, 44772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output, 4483acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten pid_t tid, 44965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian int *sessionId, 450d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten String8& name, 45165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t *status) 45265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 45365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<PlaybackThread::Track> track; 45465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<TrackHandle> trackHandle; 45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<Client> client; 45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t lStatus; 45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian int lSessionId; 45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 459263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, 460263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten // but if someone uses binder directly they could bypass that and cause us to crash 461263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { 46229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("createTrack() invalid stream type %d", streamType); 46365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian lStatus = BAD_VALUE; 46465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian goto Exit; 46565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 46665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 46760a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten // client is responsible for conversion of 8-bit PCM to 16-bit PCM, 46860a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten // and we don't yet support 8.24 or 32-bit PCM 46960a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten if (audio_is_linear_pcm(format) && format != AUDIO_FORMAT_PCM_16_BIT) { 47060a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten ALOGE("createTrack() invalid format %d", format); 47160a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten lStatus = BAD_VALUE; 47260a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten goto Exit; 47360a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten } 47460a839204713e0f8258d082af83262b1eb33a6c3Glenn Kasten 47565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { 47665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 47765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 47839e94f8f723d445447fdee0822291e664b631f60Eric Laurent PlaybackThread *effectThread = NULL; 47965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 480c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGE("no playback thread found for output handle %d", output); 48165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian lStatus = BAD_VALUE; 48265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian goto Exit; 48365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 48465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4858d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 48698ec94c5854daccc3474758524e7f4adfe535ce0Glenn Kasten client = registerPid_l(pid); 48765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 4883856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId); 489fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { 490f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent // check if an effect chain with the same session ID is present on another 491f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent // output thread and move it here. 492de070137f11d346fba77605bd76a44c040a618fcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 49339e94f8f723d445447fdee0822291e664b631f60Eric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 49439e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (mPlaybackThreads.keyAt(i) != output) { 49539e94f8f723d445447fdee0822291e664b631f60Eric Laurent uint32_t sessions = t->hasAudioSession(*sessionId); 49639e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (sessions & PlaybackThread::EFFECT_SESSION) { 49739e94f8f723d445447fdee0822291e664b631f60Eric Laurent effectThread = t.get(); 498f436fdcf93bd417fd3c9d2a8b19fd221d894b5e3Eric Laurent break; 49939e94f8f723d445447fdee0822291e664b631f60Eric Laurent } 500de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 501de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 50265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian lSessionId = *sessionId; 50365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 504de070137f11d346fba77605bd76a44c040a618fcEric Laurent // if no audio session id is provided, create one here 5057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent lSessionId = nextUniqueId(); 50665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (sessionId != NULL) { 50765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *sessionId = lSessionId; 50865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 50965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 5103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("createTrack() lSessionId: %d", lSessionId); 51165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian track = thread->createTrack_l(client, streamType, sampleRate, format, 5133acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten channelMask, frameCount, sharedBuffer, lSessionId, flags, tid, &lStatus); 51439e94f8f723d445447fdee0822291e664b631f60Eric Laurent 51539e94f8f723d445447fdee0822291e664b631f60Eric Laurent // move effect chain to this output thread if an effect on same session was waiting 51639e94f8f723d445447fdee0822291e664b631f60Eric Laurent // for a track to be created 51739e94f8f723d445447fdee0822291e664b631f60Eric Laurent if (lStatus == NO_ERROR && effectThread != NULL) { 51839e94f8f723d445447fdee0822291e664b631f60Eric Laurent Mutex::Autolock _dl(thread->mLock); 51939e94f8f723d445447fdee0822291e664b631f60Eric Laurent Mutex::Autolock _sl(effectThread->mLock); 52039e94f8f723d445447fdee0822291e664b631f60Eric Laurent moveEffectChain_l(lSessionId, effectThread, thread, true); 52139e94f8f723d445447fdee0822291e664b631f60Eric Laurent } 522a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent 523a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent // Look for sync events awaiting for a session to be used. 524a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent for (int i = 0; i < (int)mPendingSyncEvents.size(); i++) { 525a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent if (mPendingSyncEvents[i]->triggerSession() == lSessionId) { 526a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { 5272986460984580833161bdaabc7f17da1005a8961Eric Laurent if (lStatus == NO_ERROR) { 528d23eedca9b5a1812891c05d89850ab7ee707040dGlenn Kasten (void) track->setSyncEvent(mPendingSyncEvents[i]); 5292986460984580833161bdaabc7f17da1005a8961Eric Laurent } else { 5302986460984580833161bdaabc7f17da1005a8961Eric Laurent mPendingSyncEvents[i]->cancel(); 5312986460984580833161bdaabc7f17da1005a8961Eric Laurent } 532a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent mPendingSyncEvents.removeAt(i); 533a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent i--; 534a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 535a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 536a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 53765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 53865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (lStatus == NO_ERROR) { 539d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten // s for server's pid, n for normal mixer name, f for fast index 540d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten name = String8::format("s:%d;n:%d;f:%d", getpid_cached, track->name() - AudioMixer::TRACK0, 541d054c32443a493513ab63529b0c8b1aca290278cGlenn Kasten track->fastIndex()); 54265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian trackHandle = new TrackHandle(track); 54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 54465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // remove local strong reference to Client before deleting the Track so that the Client 54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // destructor is called by the TrackBase destructor with mLock held 54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian client.clear(); 54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian track.clear(); 54865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianExit: 551e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten if (status != NULL) { 55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *status = lStatus; 55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return trackHandle; 55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 55772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const 55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 5625ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("sampleRate() unknown thread %d", output); 56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->sampleRate(); 56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 56765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 56872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenint AudioFlinger::channelCount(audio_io_handle_t output) const 56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 5735ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("channelCount() unknown thread %d", output); 57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 57565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->channelCount(); 57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 57972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenaudio_format_t AudioFlinger::format(audio_io_handle_t output) const 58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 5845ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("format() unknown thread %d", output); 58558f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten return AUDIO_FORMAT_INVALID; 58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->format(); 58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 59072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastensize_t AudioFlinger::frameCount(audio_io_handle_t output) const 59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 59265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 59365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 5955ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("frameCount() unknown thread %d", output); 59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 59858912562617941964939a4182cda71eaeb153d4bGlenn Kasten // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; 59958912562617941964939a4182cda71eaeb153d4bGlenn Kasten // should examine all callers and fix them to handle smaller counts 60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->frameCount(); 60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 60372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenuint32_t AudioFlinger::latency(audio_io_handle_t output) const 60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 608c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten ALOGW("latency(): no playback thread found for output handle %d", output); 60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return thread->latency(); 61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterVolume(float value) 61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 616a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 617a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 618a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 619a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 620a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 626a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 627ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mMasterVolume = value; 628a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 629ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Set master volume in the HALs which support it. 630ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 631ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AutoMutex lock(mHardwareLock); 632ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 6334ff14bae91075eb274eb1c2975982358946e7e63John Grossman 634ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 635ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (dev->canSetMasterVolume()) { 636ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->hwDevice()->set_master_volume(dev->hwDevice(), value); 637935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent } 638ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_IDLE; 63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 641ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Now set the master volume in each playback thread. Playback threads 642ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // assigned to HALs which do not have master volume support will apply 643ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // master volume during the mix operation. Threads with HALs which do 644ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // support master volume will simply ignore the setting. 6458d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 646ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mPlaybackThreads.valueAt(i)->setMasterVolume(value); 64765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 651f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioFlinger::setMode(audio_mode_t mode) 65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 653a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 654a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 655a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 656a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 662930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten if (uint32_t(mode) >= AUDIO_MODE_CNT) { 6635ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block ALOGW("Illegal value: setMode(%d)", mode); 66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { // scope for the lock 66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 669ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 67065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_SET_MODE; 671ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_mode(dev, mode); 67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (NO_ERROR == ret) { 67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mMode = mode; 6788d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 679e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setMode(mode); 68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 68365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMicMute(bool state) 68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 687a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 688a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 689a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 690a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 691a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 698ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; 700ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_mic_mute(dev, state); 70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::getMicMute() const 70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 707a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 708a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 709a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return false; 710a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 711a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 712fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin bool state = AUDIO_MODE_INVALID; 7132b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten AutoMutex lock(mHardwareLock); 714ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; 716ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->get_mic_mute(dev, &state); 71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return state; 71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 72165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setMasterMute(bool muted) 72265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 723d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman status_t ret = initCheck(); 724d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman if (ret != NO_ERROR) { 725d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman return ret; 726d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 727d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 73165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 733ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman Mutex::Autolock _l(mLock); 734ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mMasterMute = muted; 735d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 736ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Set master mute in the HALs which support it. 737ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 738ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AutoMutex lock(mHardwareLock); 739ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman AudioHwDevice *dev = mAudioHwDevs.valueAt(i); 740d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 741ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 742ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman if (dev->canSetMasterMute()) { 743ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman dev->hwDevice()->set_master_mute(dev->hwDevice(), muted); 744d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 745ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mHardwareStatus = AUDIO_HW_IDLE; 746d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman } 747d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 748ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // Now set the master mute in each playback thread. Playback threads 749ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // assigned to HALs which do not have master mute support will apply master 750ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // mute during the mix operation. Threads with HALs which do support master 751ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman // mute will simply ignore the setting. 7528d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) 753ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman mPlaybackThreads.valueAt(i)->setMasterMute(muted); 75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianfloat AudioFlinger::masterVolume() const 75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 7609806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten Mutex::Autolock _l(mLock); 7619806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten return masterVolume_l(); 76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioFlinger::masterMute() const 76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 7669806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten Mutex::Autolock _l(mLock); 7679806710f5d6722cfc5783c7eca3512451a0f2035Glenn Kasten return masterMute_l(); 76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 7704ff14bae91075eb274eb1c2975982358946e7e63John Grossmanfloat AudioFlinger::masterVolume_l() const 7714ff14bae91075eb274eb1c2975982358946e7e63John Grossman{ 7724ff14bae91075eb274eb1c2975982358946e7e63John Grossman return mMasterVolume; 7734ff14bae91075eb274eb1c2975982358946e7e63John Grossman} 7744ff14bae91075eb274eb1c2975982358946e7e63John Grossman 775d8f178d613821c3f61a5c5e391eb275339e526a9John Grossmanbool AudioFlinger::masterMute_l() const 776d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman{ 777ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman return mMasterMute; 778d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman} 779d8f178d613821c3f61a5c5e391eb275339e526a9John Grossman 78072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, 78172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output) 78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 78665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 78765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 788263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 78929357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("setStreamVolume() invalid stream %d", stream); 79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mLock); 79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = NULL; 79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (output) { 79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkPlaybackThread_l(output); 79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 79865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 79965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mStreamTypes[stream].volume = value; 80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 8058d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 806e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value); 80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 80865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 80965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread->setStreamVolume(stream, value); 81065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 81165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 81465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 815fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) 81665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 81765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 82065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 82165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 822263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT || 823fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { 82429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block ALOGE("setStreamMute() invalid stream %d", stream); 82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 828935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent AutoMutex lock(mLock); 82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mStreamTypes[stream].mute = muted; 83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) 831e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted); 83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 83365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return NO_ERROR; 83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 83672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenfloat AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const 83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 838263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0.0f; 84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mLock); 84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian float volume; 84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (output) { 84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *thread = checkPlaybackThread_l(output); 84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (thread == NULL) { 84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0.0f; 84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian volume = thread->streamVolume(stream); 85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } else { 8516637baae4244aec731c4014da72418d330636ae1Glenn Kasten volume = streamVolume_l(stream); 85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return volume; 85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 857fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioFlinger::streamMute(audio_stream_type_t stream) const 85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 859263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten if (uint32_t(stream) >= AUDIO_STREAM_CNT) { 86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return true; 86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 8636637baae4244aec731c4014da72418d330636ae1Glenn Kasten AutoMutex lock(mLock); 8646637baae4244aec731c4014da72418d330636ae1Glenn Kasten return streamMute_l(stream); 86565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 86772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) 86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 869827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d", 870827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ioHandle, keyValuePairs.string(), IPCThreadState::self()->getCallingPid()); 87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // ioHandle == 0 means the parameters are global to the audio hardware interface 87865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (ioHandle == 0) { 879a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 880799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin status_t final_result = NO_ERROR; 8818abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten { 882a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent AutoMutex lock(mHardwareLock); 883a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mHardwareStatus = AUDIO_HW_SET_PARAMETER; 884a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 885a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 886a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent status_t result = dev->set_parameters(dev, keyValuePairs.string()); 887a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent final_result = result ?: final_result; 888a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent } 889a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 8908abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten } 89159bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent // disable AEC and NS if the device is a BT SCO headset supporting those pre processings 89259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent AudioParameter param = AudioParameter(keyValuePairs); 89359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent String8 value; 89459bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) { 895bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF); 896bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent if (mBtNrecIsOff != btNrecIsOff) { 89759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 89859bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent sp<RecordThread> thread = mRecordThreads.valueAt(i); 899f1c04f952916cf70407051c9f824ab84fb2b6e09Eric Laurent audio_devices_t device = thread->inDevice(); 900510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff; 901510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten // collect all of the thread's session IDs 902510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten KeyedVector<int, bool> ids = thread->sessionIds(); 903510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten // suspend effects associated with those session IDs 904510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten for (size_t j = 0; j < ids.size(); ++j) { 905510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten int sessionId = ids.keyAt(j); 90659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent thread->setEffectSuspended(FX_IID_AEC, 90759bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent suspend, 908510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten sessionId); 90959bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent thread->setEffectSuspended(FX_IID_NS, 91059bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent suspend, 911510a3d6b8018a77683dac466127ffd0af34bef6eGlenn Kasten sessionId); 91259bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 91359bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 914bee5337da7659b3b7128622ba1f42618b11df5beEric Laurent mBtNrecIsOff = btNrecIsOff; 91559bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 91659bd0da8373af0e5159b799495fda51e03120ea4Eric Laurent } 91728ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten String8 screenState; 91828ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { 91928ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten bool isOff = screenState == "off"; 92081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (isOff != (AudioFlinger::mScreenState & 1)) { 92181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; 92228ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 92328ed2f93324988767b5658eba7c1fa781a275183Glenn Kasten } 924799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin return final_result; 92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 92765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // hold a strong ref on thread in case closeOutput() or closeInput() is called 92865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // and the thread is exited once the lock is released 92965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<ThreadBase> thread; 93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian { 93165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkPlaybackThread_l(ioHandle); 933d5903ec1332630f2992a6f0d5ca69d13a185c665Glenn Kasten if (thread == 0) { 93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian thread = checkRecordThread_l(ioHandle); 9357fc9a6fdf146ded90b51c52f4a05d797294dcb85Glenn Kasten } else if (thread == primaryPlaybackThread_l()) { 9367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent // indicate output device change to all input threads for pre processing 9377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent AudioParameter param = AudioParameter(keyValuePairs); 9387c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent int value; 93989d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && 94089d94e79dad032fb18ddc655e6068e4231d3f0aaEric Laurent (value != 0)) { 9417c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 9427c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent mRecordThreads.valueAt(i)->setParameters(keyValuePairs); 9437c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent } 9447c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent } 94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 9477378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten if (thread != 0) { 9487378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten return thread->setParameters(keyValuePairs); 94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 95372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn KastenString8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const 95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 955827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGVV("getParameters() io %d, keys %s, calling pid %d", 956827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); 95765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 958a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent Mutex::Autolock _l(mLock); 959a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent 96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (ioHandle == 0) { 961fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin String8 out_s8; 962fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 963799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 9648abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten char *s; 9658abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten { 9668abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten AutoMutex lock(mHardwareLock); 9678abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_GET_PARAMETER; 968a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent audio_hw_device_t *dev = mAudioHwDevs.valueAt(i)->hwDevice(); 9698abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten s = dev->get_parameters(dev, keys.string()); 9708abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_IDLE; 9718abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten } 972ef7740be67a4d7b6b033ebed59c3d4a9c74a2c18John Grossman out_s8 += String8(s ? s : ""); 973799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin free(s); 974799a70e7028a4d714436c3a744a775acfbd31aaeDima Zavin } 975fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin return out_s8; 97665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle); 97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (playbackThread != NULL) { 98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return playbackThread->getParameters(keys); 98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian RecordThread *recordThread = checkRecordThread_l(ioHandle); 98365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (recordThread != NULL) { 98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return recordThread->getParameters(keys); 98565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 98665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return String8(""); 98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 98865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 989dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kastensize_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, 990dd8104cc5367262f0e5f13df4e79f131e8d560bbGlenn Kasten audio_channel_mask_t channelMask) const 99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 992a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 993a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 994a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return 0; 995a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 996a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 9972b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten AutoMutex lock(mHardwareLock); 9982b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; 999ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1000ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald memset(&config, 0, sizeof(config)); 1001ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = sampleRate; 1002ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = channelMask; 1003ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = format; 1004ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 1005ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 1006ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman size_t size = dev->get_input_buffer_size(dev, &config); 10072b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten mHardwareStatus = AUDIO_HW_IDLE; 10082b213bc220768d2b984239511cd4554a96bc0079Glenn Kasten return size; 100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 101172ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenunsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const 101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 101565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian RecordThread *recordThread = checkRecordThread_l(ioHandle); 101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (recordThread != NULL) { 101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return recordThread->getInputFramesLost(); 101865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 101965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return 0; 102065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 102265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::setVoiceVolume(float value) 102365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1024a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent status_t ret = initCheck(); 1025a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent if (ret != NO_ERROR) { 1026a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent return ret; 1027a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent } 1028a1884f9e9ec3836683efd7eb333ee442e8bc9d56Eric Laurent 102965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // check calling permissions 103065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (!settingsAllowed()) { 103165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return PERMISSION_DENIED; 103265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 103365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 103465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian AutoMutex lock(mHardwareLock); 1035ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman audio_hw_device_t *dev = mPrimaryHardwareDev->hwDevice(); 10368abf44d2f2bcd20a2835570efe89d89c19db426aGlenn Kasten mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; 1037ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman ret = dev->set_voice_volume(dev, value); 103865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mHardwareStatus = AUDIO_HW_IDLE; 103965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 104065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return ret; 104165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 104265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 104326c77556efc30800466b60b3975bc35a70c8c28bGlenn Kastenstatus_t AudioFlinger::getRenderPosition(size_t *halFrames, size_t *dspFrames, 104472ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten audio_io_handle_t output) const 104565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 104665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian status_t status; 104765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 104865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 104965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 105065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian PlaybackThread *playbackThread = checkPlaybackThread_l(output); 105165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (playbackThread != NULL) { 105265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return playbackThread->getRenderPosition(halFrames, dspFrames); 105365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 105465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 105565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BAD_VALUE; 105665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 105765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 105865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) 105965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 106065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 106165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 106265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1063bb001926447d0b7dc71ca8bb3c9856f3136d8f4cGlenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 106465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian if (mNotificationClients.indexOfKey(pid) < 0) { 106565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<NotificationClient> notificationClient = new NotificationClient(this, 106665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian client, 106765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian pid); 10683856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); 106965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 107065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mNotificationClients.add(pid, notificationClient); 107165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 107265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian sp<IBinder> binder = client->asBinder(); 107365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian binder->linkToDeath(notificationClient); 107465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 107565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // the config change is always sent from playback or record threads to avoid deadlock 107665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian // with AudioSystem::gLock 107765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1078896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::OUTPUT_OPENED); 107965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 108065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 108165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < mRecordThreads.size(); i++) { 1082896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent mRecordThreads.valueAt(i)->sendIoConfigEvent(AudioSystem::INPUT_OPENED); 108365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 108465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 108565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 108665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 108765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeNotificationClient(pid_t pid) 108865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 108965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 109065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1091a3b09254d44cd8d66ec947abe547538c4cfeaa89Glenn Kasten mNotificationClients.removeItem(pid); 10923a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen 10933856b090cd04ba5dd4a59a12430ed724d5995909Steve Block ALOGV("%d died, releasing its sessions", pid); 10948d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten size_t num = mAudioSessionRefs.size(); 10953a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen bool removed = false; 10968d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten for (size_t i = 0; i< num; ) { 10973a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 1098012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten ALOGV(" pid %d @ %d", ref->mPid, i); 1099012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten if (ref->mPid == pid) { 1100012ca6b4f69fb24385025c0e84b8f816525a3032Glenn Kasten ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); 11013a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen mAudioSessionRefs.removeAt(i); 11023a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen delete ref; 11033a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen removed = true; 11043a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen num--; 11058d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten } else { 11068d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten i++; 11073a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 11083a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 11093a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen if (removed) { 11103a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen purgeStaleEffects_l(); 11113a34befc6fb04a4945a849e8bda8b84e4bf973feMarco Nelissen } 111265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 111365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 111465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// audioConfigChanged_l() must be called with AudioFlinger::mLock held 1115b81cc8c6f3eec9edb255ea99b6a6f243585b1e38Glenn Kastenvoid AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, const void *param2) 111665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 111765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian size_t size = mNotificationClients.size(); 111865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian for (size_t i = 0; i < size; i++) { 111984afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle, 112084afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten param2); 112165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 112265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 112465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// removeClient_l() must be called with AudioFlinger::mLock held 112565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioFlinger::removeClient_l(pid_t pid) 112665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 1127827e5f1237757aee78b677efcf0f7c44fd0dd3d8Glenn Kasten ALOGV("removeClient_l() pid %d, calling pid %d", pid, 112885ab62c4b433df3f1a9826bed1c9bec07a86c750Glenn Kasten IPCThreadState::self()->getCallingPid()); 112965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian mClients.removeItem(pid); 113065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 113165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1132717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent// getEffectThread_l() must be called with AudioFlinger::mLock held 1133717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurentsp<AudioFlinger::PlaybackThread> AudioFlinger::getEffectThread_l(int sessionId, int EffectId) 1134717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent{ 1135717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent sp<PlaybackThread> thread; 1136717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent 1137717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 1138717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, EffectId) != 0) { 1139717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent ALOG_ASSERT(thread == 0); 1140717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent thread = mPlaybackThreads.valueAt(i); 1141717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent } 1142717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent } 1143717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent 1144717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent return thread; 1145717e128691f083a9469a1d0e363ac6ecd5c65d58Eric Laurent} 114665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 114965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 115065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 115181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) 115281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : RefBase(), 115381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger(audioFlinger), 115481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below 115581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")), 115681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPid(pid), 115781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount(0) 115865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 115981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer 116065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 116165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Client destructor must be called with AudioFlinger::mLock held 116381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::Client::~Client() 116465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->removeClient_l(mPid); 116665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 116765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<MemoryDealer> AudioFlinger::Client::heap() const 116965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mMemoryDealer; 117165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 117265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 117381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Reserve one of the limited slots for a timed audio track associated 117481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// with this client 117581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::Client::reserveTimedTrack() 117665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 117781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const int kMaxTimedTracksPerClient = 4; 117865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 117981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mTimedTrackLock); 118065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 118181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mTimedTrackCount >= kMaxTimedTracksPerClient) { 118281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("can not create timed track - pid %d has exceeded the limit", 118381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPid); 118481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return false; 118565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 118665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount++; 118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return true; 118965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 119065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 119181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Release a slot for a timed audio track 119281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::Client::releaseTimedTrack() 119365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 119481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mTimedTrackLock); 119581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mTimedTrackCount--; 1196896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent} 1197896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent 119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 119981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 120081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, 120181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IAudioFlingerClient>& client, 120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t pid) 120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) 1204896adcd3ae6a1c7010e526327eff54e16179987bEric Laurent{ 120565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 120665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 120781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::NotificationClient::~NotificationClient() 120865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 120965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 121065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who) 121265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NotificationClient> keep(this); 121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioFlinger->removeNotificationClient(mPid); 121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 121665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 121765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 121965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1220893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brownstatic bool deviceRequiresCaptureAudioOutputPermission(audio_devices_t inDevice) { 1221893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown return audio_is_remote_submix_device(inDevice); 1222893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown} 1223893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown 122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<IAudioRecord> AudioFlinger::openRecord( 122581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t input, 122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t sampleRate, 122781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t format, 122881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t channelMask, 122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t frameCount, 1230eeca32671896739e84050da5992d5f151a1629deGlenn Kasten IAudioFlinger::track_flags_t *flags, 123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t tid, 123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *sessionId, 123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status) 12341d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent{ 123581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread::RecordTrack> recordTrack; 123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordHandle> recordHandle; 123781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Client> client; 123881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus; 123981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordThread *thread; 124081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t inFrameCount; 124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int lSessionId; 12421d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 124381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check calling permissions 124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!recordingAllowed()) { 124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 12481d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 1249291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten if (format != AUDIO_FORMAT_PCM_16_BIT) { 1250291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten ALOGE("openRecord() invalid format %d", format); 1251291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten lStatus = BAD_VALUE; 1252291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten goto Exit; 1253291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten } 1254291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten 125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // add client to list 125681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for mLock 125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkRecordThread_l(input); 125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 12621d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent } 12631d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 1264893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown if (deviceRequiresCaptureAudioOutputPermission(thread->inDevice()) 1265893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown && !captureAudioOutputAllowed()) { 1266893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown lStatus = PERMISSION_DENIED; 1267893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown goto Exit; 1268893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown } 1269893a5642871114fca3b2a00c6ff8e5699ce3e3edJeff Brown 12708d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 127181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent client = registerPid_l(pid); 1272feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 127381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no audio session id is provided, create one here 127481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) { 127581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lSessionId = *sessionId; 1276feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } else { 127781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lSessionId = nextUniqueId(); 127881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != NULL) { 127981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *sessionId = lSessionId; 128081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1281feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 128281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create new record track. 128381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The record track uses one track in mHardwareMixerThread by convention. 128481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack = thread->createRecordTrack_l(client, sampleRate, format, channelMask, 1285ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten frameCount, lSessionId, flags, tid, &lStatus); 1286feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 128781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus != NO_ERROR) { 128881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove local strong reference to Client before deleting the RecordTrack so that the 128981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Client destructor is called by the TrackBase destructor with mLock held 129081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent client.clear(); 129181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordTrack.clear(); 129281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 1293feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 1294feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 129581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return to handle to client 129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recordHandle = new RecordHandle(recordTrack); 129781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = NO_ERROR; 1298feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 129981784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 130081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status) { 130181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *status = lStatus; 1302feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent } 130381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return recordHandle; 1304feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent} 1305feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 1306feb0db689c17dced50afaee54c659f1676e2d505Eric Laurent 13071d2bff0e588afe183a1baaae731519b4e957bbdbEric Laurent 130881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule(const char *name) 131159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!settingsAllowed()) { 131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 131559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent Mutex::Autolock _l(mLock); 131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return loadHwModule_l(name); 131759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 131859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 131981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// loadHwModule_l() must be called with AudioFlinger::mLock held 132081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) 132159255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 132281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mAudioHwDevs.size(); i++) { 132381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { 132481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("loadHwModule() module %s already loaded", name); 132581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mAudioHwDevs.keyAt(i); 132659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 132759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 132859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *dev; 133059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int rc = load_audio_interface(name, &dev); 133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (rc) { 133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() error %d loading module %s ", rc, name); 133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 133559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 133659255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_INIT; 133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent rc = dev->init_check(dev); 133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (rc) { 134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() init check error %d for module %s ", rc, name); 134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 134359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 134459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Check and cache this HAL's level of support for master mute and master 134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // volume. If this is the first HAL opened, and it supports the get 134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // methods, use the initial values provided by the HAL as the current 134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // master mute and volume settings. 134959255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); 135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { // scope for auto-lock pattern 135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mHardwareLock); 135359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (0 == mAudioHwDevs.size()) { 135581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; 135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (NULL != dev->get_master_volume) { 135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent float mv; 135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (OK == dev->get_master_volume(dev, &mv)) { 135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterVolume = mv; 136081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 136381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; 136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (NULL != dev->get_master_mute) { 136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool mm; 136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (OK == dev->get_master_mute(dev, &mm)) { 136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mMasterMute = mm; 136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 137059255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; 137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((NULL != dev->set_master_volume) && 137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (OK == dev->set_master_volume(dev, mMasterVolume))) { 137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags = static_cast<AudioHwDevice::Flags>(flags | 137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); 137759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 137859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; 138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((NULL != dev->set_master_mute) && 138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (OK == dev->set_master_mute(dev, mMasterMute))) { 138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags = static_cast<AudioHwDevice::Flags>(flags | 138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); 138481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 138559255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 138759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent } 138859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 138981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_module_handle_t handle = nextUniqueId(); 139081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioHwDevs.add(handle, new AudioHwDevice(name, dev, flags)); 139181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 139281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("loadHwModule() Loaded %s audio interface from %s (%s) handle %d", 139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent name, dev->common.module->name, dev->common.module->id, handle); 139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 139759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 139859255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::getPrimaryOutputSamplingRate() 140259255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent{ 140359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent Mutex::Autolock _l(mLock); 140481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 140581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL ? thread->sampleRate() : 0; 1406a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent} 140759255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 140881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::getPrimaryOutputFrameCount() 1409a85a74a8219c03f2b1d1ef98f3f02e55f89f89a3Eric Laurent{ 141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL ? thread->frameCountHAL() : 0; 141359255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent} 141459255e4fc7d8ff52874b85b1988dc0785140cf81Eric Laurent 141565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 141665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 14174182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kastenstatus_t AudioFlinger::setLowRamDevice(bool isLowRamDevice) 14184182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten{ 14194182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten uid_t uid = IPCThreadState::self()->getCallingUid(); 14204182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten if (uid != AID_SYSTEM) { 14214182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return PERMISSION_DENIED; 14224182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten } 14234182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten Mutex::Autolock _l(mLock); 14244182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten if (mIsDeviceTypeKnown) { 14254182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return INVALID_OPERATION; 14264182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten } 14274182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsLowRamDevice = isLowRamDevice; 14284182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten mIsDeviceTypeKnown = true; 14294182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten return NO_ERROR; 14304182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten} 14314182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten 14324182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten// ---------------------------------------------------------------------------- 14334182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten 143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openOutput(audio_module_handle_t module, 143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t *pDevices, 143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pSamplingRate, 143781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t *pFormat, 143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t *pChannelMask, 143981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pLatencyMs, 1440ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald audio_output_flags_t flags, 1441ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald const audio_offload_info_t *offloadInfo) 144265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = NULL; 1444ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1445ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0; 1446ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0; 1447ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT; 1448ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald if (offloadInfo) { 1449ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.offload_info = *offloadInfo; 1450ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald } 1451ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 145281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_out_t *outStream = NULL; 145381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice *outHwDev; 1454ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman 1455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput(), module %d Device %x, SamplingRate %d, Format %#08x, Channels %x, flags %x", 145681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent module, 145781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (pDevices != NULL) ? *pDevices : 0, 145881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 145981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 146081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 146181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent flags); 1462bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput(), offloadInfo %p version 0x%04x", 1463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent offloadInfo, offloadInfo == NULL ? -1 : offloadInfo->version ); 146481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 146581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDevices == NULL || *pDevices == 0) { 146681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 1467ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman } 1468ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman 146981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 147065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 147181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outHwDev = findSuitableHwDev_l(module, *pDevices); 147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (outHwDev == NULL) 147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 147465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *hwDevHal = outHwDev->hwDevice(); 147681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 147765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; 147965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 148034542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten status_t status = hwDevHal->open_output_stream(hwDevHal, 148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent id, 148281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *pDevices, 148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (audio_output_flags_t)flags, 148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &config, 148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &outStream); 148665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 1488bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %#08x, " 148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "Channels %x, status %d", 149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent outStream, 149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status); 149558912562617941964939a4182cda71eaeb153d4bGlenn Kasten 149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR && outStream != NULL) { 1497bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream, flags); 149865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 1499bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { 1500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent thread = new OffloadThread(this, output, id, *pDevices); 1501bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent ALOGV("openOutput() created offload output: ID %d thread %p", id, thread); 1502bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) || 150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.format != AUDIO_FORMAT_PCM_16_BIT) || 150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.channel_mask != AUDIO_CHANNEL_OUT_STEREO)) { 150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = new DirectOutputThread(this, output, id, *pDevices); 150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openOutput() created direct output: ID %d thread %p", id, thread); 150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 150881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = new MixerThread(this, output, id, *pDevices); 150981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread); 151065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.add(id, thread); 151288cbea8a918bbaf5e06e48aadd5af5e81d58d232Glenn Kasten 15137c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pSamplingRate != NULL) { 15147c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pSamplingRate = config.sample_rate; 15157c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 15167c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pFormat != NULL) { 15177c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pFormat = config.format; 15187c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 15197c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pChannelMask != NULL) { 15207c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pChannelMask = config.channel_mask; 15217c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 15227c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pLatencyMs != NULL) { 15237c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pLatencyMs = thread->latency(); 15247c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 152565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 152681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new output creation 152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 152865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // the first primary output opened designates the primary hw device 153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((mPrimaryHardwareDev == NULL) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { 153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGI("Using module %d has the primary audio interface", module); 153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPrimaryHardwareDev = outHwDev; 153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AutoMutex lock(mHardwareLock); 153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_SET_MODE; 153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent hwDevHal->set_mode(hwDevHal, mMode); 153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mHardwareStatus = AUDIO_HW_IDLE; 153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 154165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 154281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 154365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 154465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, 154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t output2) 154765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 154981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread *thread1 = checkMixerThread_l(output1); 155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent MixerThread *thread2 = checkMixerThread_l(output2); 155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread1 == NULL || thread2 == NULL) { 155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, 155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent output2); 155581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 155665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 155765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DuplicatingThread *thread = new DuplicatingThread(this, thread1, id); 156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->addOutputTrack(thread2); 156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.add(id, thread); 156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new output creation 156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED); 156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 156565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 156665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput(audio_io_handle_t output) 15682bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi{ 156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return closeOutput_nonvirtual(output); 15702bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi} 15712bfc6b42b3733c12485dd51ed95191956abc3e4eJean-Michel Trivi 157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) 157365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // keep strong reference on the playback thread so that 157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is not destroyed while exit() is executed 157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<PlaybackThread> thread; 157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 157881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkPlaybackThread_l(output); 158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 158265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 158365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("closeOutput() %d", output); 1585de070137f11d346fba77605bd76a44c040a618fcEric Laurent 158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread->type() == ThreadBase::MIXER) { 158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) { 158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent DuplicatingThread *dupThread = 159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); 159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dupThread->removeOutputTrack((MixerThread *)thread.get()); 1592bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1593de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1594de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1595de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 1596bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 1597bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent 159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPlaybackThreads.removeItem(output); 1599bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // save all effects to the default thread 1600bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (mPlaybackThreads.size()) { 1601bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); 1602bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent if (dstThread != NULL) { 1603bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // audioflinger lock is held here so the acquisition order of thread locks does not 1604bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent // matter 1605bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _dl(dstThread->mLock); 1606bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Mutex::Autolock _sl(thread->mLock); 1607bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent Vector< sp<EffectChain> > effectChains = thread->getEffectChains_l(); 1608bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent for (size_t i = 0; i < effectChains.size(); i ++) { 1609bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent moveEffectChain_l(effectChains[i]->sessionId(), thread.get(), dstThread, true); 1610bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1611bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1612bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent } 1613bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL); 16143acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten } 161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->exit(); 161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The thread entity (active unit of execution) is no longer running here, 161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but the ThreadBase container still exists. 16183acbd053c842e76e1a40fc8a0bf62de87eebf00fGlenn Kasten 161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread->type() != ThreadBase::DUPLICATING) { 162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *out = thread->clearOutput(); 162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); 162281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from now on thread->mOutput is NULL 162381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent out->hwDev()->close_output_stream(out->hwDev(), out->stream); 162481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete out; 162565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 162681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 162765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 162865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 162981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::suspendOutput(audio_io_handle_t output) 1630e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent{ 163181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 163281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 163381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 163481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 163581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1636e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent } 1637e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent 163881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("suspendOutput() %d", output); 163981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->suspend(); 1640e737cda649acbfa43fc1b74612a83f2fac9aa449Eric Laurent 164181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 164265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 164365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 164481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::restoreOutput(audio_io_handle_t output) 164565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 16466637baae4244aec731c4014da72418d330636ae1Glenn Kasten Mutex::Autolock _l(mLock); 164781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 164865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 165081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1651ee578c0330319f04a48bccbdb26b53fea0388d04John Grossman } 165265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 165381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("restoreOutput() %d", output); 165465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 165581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->restore(); 165665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 165781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 165865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 165965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_io_handle_t AudioFlinger::openInput(audio_module_handle_t module, 166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_devices_t *pDevices, 166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t *pSamplingRate, 166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t *pFormat, 166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t *pChannelMask) 166565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t status; 166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent RecordThread *thread = NULL; 1668ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald struct audio_config config; 1669ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.sample_rate = (pSamplingRate != NULL) ? *pSamplingRate : 0; 1670ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.channel_mask = (pChannelMask != NULL) ? *pChannelMask : 0; 1671ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald config.format = (pFormat != NULL) ? *pFormat : AUDIO_FORMAT_DEFAULT; 1672ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald 167381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t reqSamplingRate = config.sample_rate; 167481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_format_t reqFormat = config.format; 167581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_channel_mask_t reqChannels = config.channel_mask; 167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_stream_in_t *inStream = NULL; 167781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioHwDevice *inHwDev; 167865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDevices == NULL || *pDevices == 0) { 168081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 1681b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent } 1682b469b9490b3cd9e0f0466d9b9ab228f6c793b82eEric Laurent 168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 168465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inHwDev = findSuitableHwDev_l(module, *pDevices); 168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (inHwDev == NULL) 168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 1688fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin 168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_hw_device_t *inHwHal = inHwDev->hwDevice(); 169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t id = nextUniqueId(); 1691b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, 169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &inStream); 169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, " 169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent "status %d", 169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inStream, 169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.sample_rate, 169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.format, 169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent config.channel_mask, 170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status); 170165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 170281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If the input could not be opened with the requested parameters and we can handle the 170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // conversion internally, try to open again with the proposed parameters. The AudioFlinger can 170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // resample the input and do mono to stereo or stereo to mono conversions on 16 bit PCM inputs. 170581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == BAD_VALUE && 170681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reqFormat == config.format && config.format == AUDIO_FORMAT_PCM_16_BIT && 170781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (config.sample_rate <= 2 * reqSamplingRate) && 170881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (popcount(config.channel_mask) <= FCC_2) && (popcount(reqChannels) <= FCC_2)) { 170981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openInput() reopening with proposed sampling rate and channel mask"); 171081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent inStream = NULL; 171181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status = inHwHal->open_input_stream(inHwHal, id, *pDevices, &config, &inStream); 171265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 171365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 171481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status == NO_ERROR && inStream != NULL) { 171558912562617941964939a4182cda71eaeb153d4bGlenn Kasten 171646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Try to re-use most recently used Pipe to archive a copy of input for dumpsys, 171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // or (re-)create if current Pipe is idle and does not match the new format 171981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<NBAIO_Sink> teeSink; 172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent enum { 172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_NO, // don't copy input 172281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_NEW, // copy input using a new pipe 172381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent TEE_SINK_OLD, // copy input using an existing pipe 172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } kind; 172581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent NBAIO_Format format = Format_from_SR_C(inStream->common.get_sample_rate(&inStream->common), 172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent popcount(inStream->common.get_channels(&inStream->common))); 1727da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (!mTeeSinkInputEnabled) { 1728da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten kind = TEE_SINK_NO; 1729da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } else if (format == Format_Invalid) { 173081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NO; 173181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mRecordTeeSink == 0) { 173281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NEW; 173381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (mRecordTeeSink->getStrongCount() != 1) { 173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NO; 173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else if (format == mRecordTeeSink->format()) { 173681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_OLD; 17374adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten } else { 173881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent kind = TEE_SINK_NEW; 173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent switch (kind) { 174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_NEW: { 1742da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten Pipe *pipe = new Pipe(mTeeSinkInputFrames, format); 174381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numCounterOffers = 0; 174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const NBAIO_Format offers[1] = {format}; 174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers); 174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PipeReader *pipeReader = new PipeReader(*pipe); 174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent numCounterOffers = 0; 174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers); 175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(index == 0); 175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordTeeSink = pipe; 175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordTeeSource = pipeReader; 175381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent teeSink = pipe; 17544adcede0dc54a85c31abaf139921aebd7a072d8eGlenn Kasten } 175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 175681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_OLD: 175781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent teeSink = mRecordTeeSink; 175881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 175981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent case TEE_SINK_NO: 176081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent default: 176181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 176258912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 176346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 1764da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 176581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream); 176665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 176781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Start record thread 176834af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten // RecordThread requires both input and output device indication to forward to audio 176981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // pre processing modules 177081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = new RecordThread(this, 177181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent input, 177281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reqSamplingRate, 177381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent reqChannels, 177481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent id, 1775d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent primaryOutputDevice_l(), 177646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten *pDevices 177746909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 177846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten , teeSink 177946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 178046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten ); 178181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordThreads.add(id, thread); 178281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("openInput() created record thread: ID %d thread %p", id, thread); 17837c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pSamplingRate != NULL) { 17847c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pSamplingRate = reqSamplingRate; 17857c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 17867c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pFormat != NULL) { 17877c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pFormat = config.format; 17887c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 17897c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten if (pChannelMask != NULL) { 17907c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten *pChannelMask = reqChannels; 17917c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten } 179265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 179381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // notify client processes of the new input creation 179481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED); 179581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return id; 17961afc26db11b71c43f63a0f72a45a803f1a7910ddEric Laurent } 179781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 179881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 179965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 180065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 180181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput(audio_io_handle_t input) 180265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 180381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return closeInput_nonvirtual(input); 180465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 180565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 180681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) 1807de070137f11d346fba77605bd76a44c040a618fcEric Laurent{ 180881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // keep strong reference on the record thread so that 180981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // it is not destroyed while exit() is executed 181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread> thread; 181181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkRecordThread_l(input); 181481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == 0) { 181581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 1816de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 181781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 181881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("closeInput() %d", input); 181981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL); 182081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mRecordThreads.removeItem(input); 1821de070137f11d346fba77605bd76a44c040a618fcEric Laurent } 182281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->exit(); 182381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // The thread entity (active unit of execution) is no longer running here, 182481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // but the ThreadBase container still exists. 1825de070137f11d346fba77605bd76a44c040a618fcEric Laurent 182681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamIn *in = thread->clearInput(); 182781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); 182881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // from now on thread->mInput is NULL 182981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent in->hwDev()->close_input_stream(in->hwDev(), in->stream); 183081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete in; 183165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 183281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 1833b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent} 1834b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 183581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) 1836b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent{ 1837b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent Mutex::Autolock _l(mLock); 183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("setStreamOutput() stream %d to output %d", stream, output); 1839b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 184181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 184281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread->invalidateTracks(stream); 1843b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent } 184481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 184581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 1846b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent} 1847b8ba0a979067a4efb0b3819bf17770793e41c15eEric Laurent 184881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 184981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentint AudioFlinger::newAudioSessionId() 1850162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent{ 185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return nextUniqueId(); 1852162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent} 1853162b40bbaf3c3a24f61a6636bef6f80a9c0a31ddEric Laurent 185481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::acquireAudioSessionId(int audioSession) 1855a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent{ 1856a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent Mutex::Autolock _l(mLock); 185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t caller = IPCThreadState::self()->getCallingPid(); 185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("acquiring %d from %d", audioSession, caller); 1859d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 1860d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // Ignore requests received from processes not known as notification client. The request 1861d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be 1862d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // called from a different pid leaving a stale session reference. Also we don't know how 1863d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // to clear this reference if the client process dies. 1864d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent if (mNotificationClients.indexOfKey(caller) < 0) { 1865d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent ALOGV("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); 1866d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent return; 1867d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent } 1868d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent 186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t num = mAudioSessionRefs.size(); 187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i< num; i++) { 187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); 187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == audioSession && ref->mPid == caller) { 187381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ref->mCnt++; 187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" incremented refcount to %d", ref->mCnt); 187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 1876a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 1877a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent } 187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); 187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" added new entry for %d", audioSession); 1880a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent} 1881a011e35b22f95f558d81dc9c94b68b1465c4661dEric Laurent 188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::releaseAudioSessionId(int audioSession) 188344a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent{ 188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid_t caller = IPCThreadState::self()->getCallingPid(); 188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("releasing %d from %d", audioSession, caller); 188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t num = mAudioSessionRefs.size(); 188881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i< num; i++) { 188981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); 189081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == audioSession && ref->mPid == caller) { 189181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ref->mCnt--; 189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" decremented refcount to %d", ref->mCnt); 189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mCnt == 0) { 189481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mAudioSessionRefs.removeAt(i); 189581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent delete ref; 189681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent purgeStaleEffects_l(); 189744a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 189881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 189944a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 190044a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent } 1901d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // If the caller is mediaserver it is likely that the session being released was acquired 1902d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent // on behalf of a process not in notification clients and we ignore the warning. 1903d1b28d41dbda203ffb420ba2e36cbe736b163ff8Eric Laurent ALOGW_IF(caller != getpid_cached, "session id %d not found for pid %d", audioSession, caller); 190444a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent} 190544a957f06400a338e7af20b3d16c4c4ae22a673cEric Laurent 190681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::purgeStaleEffects_l() { 190765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 190881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("purging stale effects"); 1909fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten 191081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Vector< sp<EffectChain> > chains; 191158912562617941964939a4182cda71eaeb153d4bGlenn Kasten 191281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 191381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 191481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < t->mEffectChains.size(); j++) { 191581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = t->mEffectChains[j]; 191681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) { 191781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chains.push(ec); 191881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1919c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten } 192058912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 192181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<RecordThread> t = mRecordThreads.valueAt(i); 192381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t j = 0; j < t->mEffectChains.size(); j++) { 192481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = t->mEffectChains[j]; 192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent chains.push(ec); 192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1927300a2ee9327c05fbf9d3a5fd595b558097c7c5e8Glenn Kasten } 192865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < chains.size(); i++) { 193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> ec = chains[i]; 193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionid = ec->sessionId(); 193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<ThreadBase> t = ec->mThread.promote(); 193381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (t == 0) { 193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 193581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent size_t numsessionrefs = mAudioSessionRefs.size(); 193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool found = false; 193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t k = 0; k < numsessionrefs; k++) { 193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); 194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (ref->mSessionid == sessionid) { 194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV(" session %d still exists for %d with %d refs", 194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionid, ref->mPid, ref->mCnt); 194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent found = true; 194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 194558912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 194658912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!found) { 194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l (t->mLock); 194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove all effects from the chain 195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (ec->mEffects.size()) { 195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = ec->mEffects[0]; 195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->unPin(); 195381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent t->removeEffect_l(effect); 195481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->purgeHandles()) { 195581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId()); 195681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 195781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 1959c15d6657a17d7cef91f800f40d11760e2e7340afGlenn Kasten } 196058912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return; 196265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 196365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkPlaybackThread_l() must be called with AudioFlinger::mLock held 196581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const 1966190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten{ 196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mPlaybackThreads.valueFor(output).get(); 1968190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten} 1969190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkMixerThread_l() must be called with AudioFlinger::mLock held 197181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const 197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = checkPlaybackThread_l(output); 197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; 197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 1976190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// checkRecordThread_l() must be called with AudioFlinger::mLock held 197881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const 197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return mRecordThreads.valueFor(input).get(); 198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 1982190a46f7c84e160386610c0c4cecb9767fb5503bGlenn Kasten 198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::nextUniqueId() 198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return android_atomic_inc(&mNextUniqueId); 198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} 198783efdd0fc08cd5aedf50b45741a8a87be8dc4b41Glenn Kasten 198881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const 198937d825e72a6c606553a745da1212590a425996d3Glenn Kasten{ 199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); 199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioStreamOut *output = thread->getOutput(); 199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { 199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread; 199537d825e72a6c606553a745da1212590a425996d3Glenn Kasten } 199637d825e72a6c606553a745da1212590a425996d3Glenn Kasten } 199781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NULL; 199837d825e72a6c606553a745da1212590a425996d3Glenn Kasten} 199937d825e72a6c606553a745da1212590a425996d3Glenn Kasten 200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_devices_t AudioFlinger::primaryOutputDevice_l() const 200165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *thread = primaryPlaybackThread_l(); 2003000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return 0; 20069f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten } 2007000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return thread->outDevice(); 2009000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2010688a64030834ea2f52cc9765676ddf6aa34df767Glenn Kasten 201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, 201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int triggerSession, 201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int listenerSession, 201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sync_event_callback_t callBack, 201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent void *cookie) 201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{ 201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 201873ca0f5837d5448f7a5eb159a09cd0ebe82b4de9Glenn Kasten 201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); 202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t playStatus = NAME_NOT_FOUND; 202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t recStatus = NAME_NOT_FOUND; 202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); 202481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playStatus == NO_ERROR) { 202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 202765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); 203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (recStatus == NO_ERROR) { 203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 20329f34a36d9cdb9595c288e50ffe00da038bc8abb9Glenn Kasten } 203365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { 203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent mPendingSyncEvents.add(event); 203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createSyncEvent() invalid event %d", event->type()); 203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent event.clear(); 203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return event; 204165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 204265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Effect management 204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ---------------------------------------------------------------------------- 204658912562617941964939a4182cda71eaeb153d4bGlenn Kasten 204758912562617941964939a4182cda71eaeb153d4bGlenn Kasten 204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const 2049000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectQueryNumberEffects(numEffects); 2052000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2053000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const 205558912562617941964939a4182cda71eaeb153d4bGlenn Kasten{ 205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectQueryEffect(index, descriptor); 205858912562617941964939a4182cda71eaeb153d4bGlenn Kasten} 205958912562617941964939a4182cda71eaeb153d4bGlenn Kasten 206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, 206181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *descriptor) const 2062000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 206381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 206481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return EffectGetDescriptor(pUuid, descriptor); 2065000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten} 2066000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 206781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent 20688d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kastensp<IEffect> AudioFlinger::createEffect( 206981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t *pDesc, 207081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent const sp<IEffectClient>& effectClient, 207181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int32_t priority, 207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t io, 207381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int sessionId, 207481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t *status, 207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *id, 207681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent int *enabled) 2077000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten{ 207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent status_t lStatus = NO_ERROR; 207981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectHandle> handle; 208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t desc; 2081000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 20828d6cc842e8d525405c68e57fdf3bc5da0b4d7e87Glenn Kasten pid_t pid = IPCThreadState::self()->getCallingPid(); 208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d", 208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent pid, effectClient.get(), priority, sessionId, io); 2085000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (pDesc == NULL) { 208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2089952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 2090000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check audio settings permission for global effects 209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) { 209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2095952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 2096000f0e39b4d0c88441297a05ab5f8da6832c1640Glenn Kasten 209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects 209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // that can only be created by audio policy manager (running in same process) 209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) { 210081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 210181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2102952eeb27682a9b2ddfa761f24b6eb5e18fe5d814Glenn Kasten } 210365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent { 210581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _l(mLock); 2106288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten 210765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 210881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!EffectIsNullUuid(&pDesc->uuid)) { 210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if uuid is specified, request effect descriptor 211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectGetDescriptor(&pDesc->uuid, &desc); 211181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus); 211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } else { 211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if uuid is not specified, look for an available implementation 211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // of the required type in effect factory 211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (EffectIsNullUuid(&pDesc->type)) { 211981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() no effect type"); 212081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2122288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t numEffects = 0; 212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect_descriptor_t d; 212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent d.flags = 0; // prevent compiler warning 212681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool found = false; 2127288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten 212881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectQueryNumberEffects(&numEffects); 212981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 213081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus); 213181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 213281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 213381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent for (uint32_t i = 0; i < numEffects; i++) { 213481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = EffectQueryEffect(i, &desc); 213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (lStatus < 0) { 213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() error %d from EffectQueryEffect", lStatus); 213781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent continue; 2138d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 213981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) { 214081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If matching type found save effect descriptor. If the session is 214181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // 0 and the effect is not auxiliary, continue enumeration in case 214281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // an auxiliary version of this effect type is available 214381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent found = true; 214481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent d = desc; 214581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != AUDIO_SESSION_OUTPUT_MIX || 214681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 2147d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten break; 2148d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 2149d08f48c2ad2941d62b313007955c7145075d562cGlenn Kasten } 2150288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 215181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (!found) { 215281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 215381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("createEffect() effect not found"); 215481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 215581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 215681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // For same effect type, chose auxiliary version over insert version if 215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // connect to output mix (Compliance to OpenSL ES) 215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX && 215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) { 216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent desc = d; 216158912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 216258912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 216358912562617941964939a4182cda71eaeb153d4bGlenn Kasten 216481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Do not allow auxiliary effects on a session different from 0 (output mix) 216581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (sessionId != AUDIO_SESSION_OUTPUT_MIX && 216681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { 216781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = INVALID_OPERATION; 216881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 21693dbe3201479828e84abe02e1fdd0a5d414c0ddb8Eric Laurent } 217065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 217181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // check recording permission for visualizer 217281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && 217381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent !recordingAllowed()) { 217481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = PERMISSION_DENIED; 217581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 217681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 217765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 217881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // return effect descriptor 217981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *pDesc = desc; 218065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 218181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If output is not specified try to find a matching audio session ID in one of the 218281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // output threads. 218381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX 218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // because of code checking output when entering the function. 218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // Note: io is never 0 when creating an effect on an input 218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0) { 21875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { 21885baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // output must be specified by AudioPolicyManager when using session 21895baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // AUDIO_SESSION_OUTPUT_STAGE 21905baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent lStatus = BAD_VALUE; 21915baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent goto Exit; 21925baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 21935baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { 21945baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // if the output returned by getOutputForEffect() is removed before we lock the 21955baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // mutex below, the call to checkPlaybackThread_l(io) below will detect it 21965baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // and we will exit safely 21975baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent io = AudioSystem::getOutputForEffect(&desc); 21985baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent ALOGV("createEffect got output %d", io); 2199d65d73c4ae74d084751b417615a78cbe7a51372aGlenn Kasten } 220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0) { 22015baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent // look for the thread where the specified audio session is present 22025baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 22035baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 22045baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent io = mPlaybackThreads.keyAt(i); 220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent break; 22062986460984580833161bdaabc7f17da1005a8961Eric Laurent } 220765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 22085baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (io == 0) { 22095baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent for (size_t i = 0; i < mRecordThreads.size(); i++) { 22105baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) { 22115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent io = mRecordThreads.keyAt(i); 22125baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 22135baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 22145baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 22155baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 221665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // If no output thread contains the requested session ID, default to 221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // first output. The effect chain will be moved to the correct output 221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // thread when a track with the same session ID is created 222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (io == 0 && mPlaybackThreads.size()) { 222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent io = mPlaybackThreads.keyAt(0); 222281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("createEffect() got io %d for effect %s", io, desc.name); 222465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ThreadBase *thread = checkRecordThread_l(io); 222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent thread = checkPlaybackThread_l(io); 222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (thread == NULL) { 222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGE("createEffect() unknown output thread"); 223081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent lStatus = BAD_VALUE; 223181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent goto Exit; 2232288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 2233288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten } 2234288ed2103d96f3aabd7e6bea3c080ab6db164049Glenn Kasten 223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<Client> client = registerPid_l(pid); 223658912562617941964939a4182cda71eaeb153d4bGlenn Kasten 223781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // create effect on selected output thread 223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent handle = thread->createEffect_l(client, effectClient, priority, sessionId, 223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent &desc, enabled, &lStatus); 224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (handle != 0 && id != NULL) { 224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *id = handle->id(); 224265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 224365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 224465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 224581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit: 224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (status != NULL) { 224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent *status = lStatus; 224858912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return handle; 225066fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten} 225166fcab972e9218d47c58a915f391b2f48a09903aGlenn Kasten 225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput, 225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent audio_io_handle_t dstOutput) 225465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", 225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcOutput, dstOutput); 225765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian Mutex::Autolock _l(mLock); 225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (srcOutput == dstOutput) { 225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() same dst and src outputs %d", dstOutput); 226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return NO_ERROR; 226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); 226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (srcThread == NULL) { 226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() bad srcOutput %d", srcOutput); 226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); 226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstThread == NULL) { 226981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffects() bad dstOutput %d", dstOutput); 227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return BAD_VALUE; 227165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 227265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _dl(dstThread->mLock); 227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent Mutex::Autolock _sl(srcThread->mLock); 22755baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent return moveEffectChain_l(sessionId, srcThread, dstThread, false); 227665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 227765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// moveEffectChain_l must be called with both srcThread and dstThread mLocks held 227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::moveEffectChain_l(int sessionId, 228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::PlaybackThread *srcThread, 228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioFlinger::PlaybackThread *dstThread, 228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent bool reRegister) 228365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", 228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcThread, dstThread); 228665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); 228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (chain == 0) { 228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", 229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, srcThread); 229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent return INVALID_OPERATION; 229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 22939ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang 229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // remove chain first. This is useful only if reconfiguring effect chain on same output thread, 229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // so that a new chain is created with correct parameters when first effect is added. This is 229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is 229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // removed. 229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent srcThread->removeEffectChain_l(chain); 22999ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang 230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // transfer all effects one by one so that new effect chain is created on new thread with 230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // correct buffer sizes and audio parameters and effect engines reconfigured accordingly 230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectChain> dstChain; 230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent uint32_t strategy = 0; // prevent compiler warning 230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sp<EffectModule> effect = chain->getEffectFromId_l(0); 23055baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent Vector< sp<EffectModule> > removed; 23065baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status_t status = NO_ERROR; 230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent while (effect != 0) { 230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent srcThread->removeEffect_l(effect); 23095baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent removed.add(effect); 23105baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status = dstThread->addEffect_l(effect); 23115baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (status != NO_ERROR) { 23125baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 23135baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // removeEffect_l() has stopped the effect if it was active so it must be restarted 231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (effect->state() == EffectModule::ACTIVE || 231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->state() == EffectModule::STOPPING) { 231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->start(); 231865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // if the move request is not received from audio policy manager, the effect must be 232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent // re-registered with the new strategy and output 232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstChain == 0) { 232281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent dstChain = effect->chain().promote(); 232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (dstChain == 0) { 232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); 23255baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent status = NO_INIT; 23265baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent break; 232765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy = dstChain->strategy(); 232965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian } 233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent if (reRegister) { 233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::unregisterEffect(effect->id()); 233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent AudioSystem::registerEffect(&effect->desc(), 23335baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent dstThread->id(), 233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent strategy, 233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent sessionId, 233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect->id()); 233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent } 233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent effect = chain->getEffectFromId_l(0); 233958912562617941964939a4182cda71eaeb153d4bGlenn Kasten } 234058912562617941964939a4182cda71eaeb153d4bGlenn Kasten 23415baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (status != NO_ERROR) { 23425baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent for (size_t i = 0; i < removed.size(); i++) { 23435baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent srcThread->addEffect_l(removed[i]); 23445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (dstChain != 0 && reRegister) { 23455baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent AudioSystem::unregisterEffect(removed[i]->id()); 23465baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent AudioSystem::registerEffect(&removed[i]->desc(), 23475baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent srcThread->id(), 23485baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent strategy, 23495baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent sessionId, 23505baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent removed[i]->id()); 23515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 23525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 23535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent } 23545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent 23555baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent return status; 235665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 235765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 23585baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentbool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() 2359813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{ 2360813e2a74853bde19e37d878c596a044b3f299efcEric Laurent if (mGlobalEffectEnableTime != 0 && 2361813e2a74853bde19e37d878c596a044b3f299efcEric Laurent ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { 2362813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return true; 2363813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2364813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2365813e2a74853bde19e37d878c596a044b3f299efcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 2366813e2a74853bde19e37d878c596a044b3f299efcEric Laurent sp<EffectChain> ec = 2367813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); 23685baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent if (ec != 0 && ec->isNonOffloadableEnabled()) { 2369813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return true; 2370813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2371813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2372813e2a74853bde19e37d878c596a044b3f299efcEric Laurent return false; 2373813e2a74853bde19e37d878c596a044b3f299efcEric Laurent} 2374813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 23755baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurentvoid AudioFlinger::onNonOffloadableGlobalEffectEnable() 2376813e2a74853bde19e37d878c596a044b3f299efcEric Laurent{ 2377813e2a74853bde19e37d878c596a044b3f299efcEric Laurent Mutex::Autolock _l(mLock); 2378813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2379813e2a74853bde19e37d878c596a044b3f299efcEric Laurent mGlobalEffectEnableTime = systemTime(); 2380813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2381813e2a74853bde19e37d878c596a044b3f299efcEric Laurent for (size_t i = 0; i < mPlaybackThreads.size(); i++) { 2382813e2a74853bde19e37d878c596a044b3f299efcEric Laurent sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); 2383813e2a74853bde19e37d878c596a044b3f299efcEric Laurent if (t->mType == ThreadBase::OFFLOAD) { 2384813e2a74853bde19e37d878c596a044b3f299efcEric Laurent t->invalidateTracks(AUDIO_STREAM_MUSIC); 2385813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2386813e2a74853bde19e37d878c596a044b3f299efcEric Laurent } 2387813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2388813e2a74853bde19e37d878c596a044b3f299efcEric Laurent} 2389813e2a74853bde19e37d878c596a044b3f299efcEric Laurent 2390da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenstruct Entry { 2391da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_NAME 32 // %Y%m%d%H%M%S_%d.wav 2392da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten char mName[MAX_NAME]; 2393da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten}; 2394da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 2395da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kastenint comparEntry(const void *p1, const void *p2) 2396da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten{ 2397da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten return strcmp(((const Entry *) p1)->mName, ((const Entry *) p2)->mName); 2398da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten} 2399da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten 240046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK 2401d06785bebf7e43d4a011b62a252771373ada910cGlenn Kastenvoid AudioFlinger::dumpTee(int fd, const sp<NBAIO_Source>& source, audio_io_handle_t id) 240265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 2403d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten NBAIO_Source *teeSource = source.get(); 2404fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (teeSource != NULL) { 2405da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // .wav rotation 2406da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // There is a benign race condition if 2 threads call this simultaneously. 2407da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // They would both traverse the directory, but the result would simply be 2408da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // failures at unlink() which are ignored. It's also unlikely since 2409da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // normally dumpsys is only done by bugreport or from the command line. 2410da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten char teePath[32+256]; 2411da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(teePath, "/data/misc/media"); 2412da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t teePathLen = strlen(teePath); 2413da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten DIR *dir = opendir(teePath); 2414da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten teePath[teePathLen++] = '/'; 2415da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (dir != NULL) { 2416da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_SORT 20 // number of entries to sort 2417da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten#define MAX_KEEP 10 // number of entries to keep 2418da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct Entry entries[MAX_SORT]; 2419da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t entryCount = 0; 2420da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten while (entryCount < MAX_SORT) { 2421da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct dirent de; 2422da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten struct dirent *result = NULL; 2423da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int rc = readdir_r(dir, &de, &result); 2424da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (rc != 0) { 2425da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOGW("readdir_r failed %d", rc); 2426da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2427da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2428da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (result == NULL) { 2429da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2430da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2431da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (result != &de) { 2432da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten ALOGW("readdir_r returned unexpected result %p != %p", result, &de); 2433da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten break; 2434da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2435da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // ignore non .wav file entries 2436da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten size_t nameLen = strlen(de.d_name); 2437da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (nameLen <= 4 || nameLen >= MAX_NAME || 2438da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcmp(&de.d_name[nameLen - 4], ".wav")) { 2439da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten continue; 2440da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2441da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(entries[entryCount++].mName, de.d_name); 2442da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2443da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) closedir(dir); 2444da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (entryCount > MAX_KEEP) { 2445da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten qsort(entries, entryCount, sizeof(Entry), comparEntry); 2446da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten for (size_t i = 0; i < entryCount - MAX_KEEP; ++i) { 2447da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strcpy(&teePath[teePathLen], entries[i].mName); 2448da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten (void) unlink(teePath); 2449da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2450da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2451da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } else { 2452da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2453da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "unable to rotate tees in %s: %s\n", teePath, strerror(errno)); 2454da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2455da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2456d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten char teeTime[16]; 2457fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten struct timeval tv; 2458fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten gettimeofday(&tv, NULL); 2459fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten struct tm tm; 2460fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten localtime_r(&tv.tv_sec, &tm); 2461da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten strftime(teeTime, sizeof(teeTime), "%Y%m%d%H%M%S", &tm); 2462da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten snprintf(&teePath[teePathLen], sizeof(teePath) - teePathLen, "%s_%d.wav", teeTime, id); 2463da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten // if 2 dumpsys are done within 1 second, and rotation didn't work, then discard 2nd 2464da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten int teeFd = open(teePath, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, S_IRUSR | S_IWUSR); 2465fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (teeFd >= 0) { 2466fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten char wavHeader[44]; 2467fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten memcpy(wavHeader, 2468fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn 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", 2469fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten sizeof(wavHeader)); 2470fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten NBAIO_Format format = teeSource->format(); 2471fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten unsigned channelCount = Format_channelCount(format); 2472fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten ALOG_ASSERT(channelCount <= FCC_2); 24733b16c766d1ae2cfd8487e8ffb2b23936fc0a8e17Glenn Kasten uint32_t sampleRate = Format_sampleRate(format); 2474fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[22] = channelCount; // number of channels 2475fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[24] = sampleRate; // sample rate 2476fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[25] = sampleRate >> 8; 2477fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten wavHeader[32] = channelCount * 2; // block alignment 2478fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, wavHeader, sizeof(wavHeader)); 2479fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten size_t total = 0; 2480fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten bool firstRead = true; 2481fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten for (;;) { 2482fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten#define TEE_SINK_READ 1024 2483fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten short buffer[TEE_SINK_READ * FCC_2]; 2484fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten size_t count = TEE_SINK_READ; 24852c3b2da3049627264b7c6b449a1622f002210f03John Grossman ssize_t actual = teeSource->read(buffer, count, 24862c3b2da3049627264b7c6b449a1622f002210f03John Grossman AudioBufferProvider::kInvalidPTS); 2487fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten bool wasFirstRead = firstRead; 2488fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten firstRead = false; 2489fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (actual <= 0) { 2490fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten if (actual == (ssize_t) OVERRUN && wasFirstRead) { 2491fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten continue; 2492fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2493fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten break; 2494fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2495a5f44ebaf58911805b4fb7fb479b19fd89d2e39bEric Laurent ALOG_ASSERT(actual <= (ssize_t)count); 2496fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, buffer, actual * channelCount * sizeof(short)); 2497fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten total += actual; 2498fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2499fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten lseek(teeFd, (off_t) 4, SEEK_SET); 2500fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten uint32_t temp = 44 + total * channelCount * sizeof(short) - 8; 2501fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, &temp, sizeof(temp)); 2502fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten lseek(teeFd, (off_t) 40, SEEK_SET); 2503fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten temp = total * channelCount * sizeof(short); 2504fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten write(teeFd, &temp, sizeof(temp)); 2505fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten close(teeFd); 2506da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2507da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "tee copied to %s\n", teePath); 2508da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2509fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } else { 2510da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten if (fd >= 0) { 2511da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten fdprintf(fd, "unable to create tee %s: %s\n", teePath, strerror(errno)); 2512da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten } 2513fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2514fbae5dae5187aca9d974cbe15ec818e9c6f56705Glenn Kasten } 2515d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten} 251646909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif 2517d06785bebf7e43d4a011b62a252771373ada910cGlenn Kasten 251865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ---------------------------------------------------------------------------- 251965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 252065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioFlinger::onTransact( 252165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 252265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{ 252365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian return BnAudioFlinger::onTransact(code, data, reply, flags); 252465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian} 252565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian 252665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android 2527