| Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** | 
|  | 3 | ** Copyright 2007, The Android Open Source Project | 
|  | 4 | ** | 
|  | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | ** you may not use this file except in compliance with the License. | 
|  | 7 | ** You may obtain a copy of the License at | 
|  | 8 | ** | 
|  | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | ** | 
|  | 11 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | ** See the License for the specific language governing permissions and | 
|  | 15 | ** limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | #define LOG_TAG "AudioFlinger" | 
|  | 20 | //#define LOG_NDEBUG 0 | 
|  | 21 |  | 
| jiabin | 8cb192f | 2019-09-23 10:34:20 -0700 | [diff] [blame] | 22 | // Define AUDIO_ARRAYS_STATIC_CHECK to check all audio arrays are correct | 
|  | 23 | #define AUDIO_ARRAYS_STATIC_CHECK 1 | 
|  | 24 |  | 
| Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 25 | #include "Configuration.h" | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 26 | #include <dirent.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 27 | #include <math.h> | 
|  | 28 | #include <signal.h> | 
| Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 29 | #include <string> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <sys/time.h> | 
|  | 31 | #include <sys/resource.h> | 
| Mikhail Naganov | 3de4f26 | 2020-03-06 17:02:19 -0800 | [diff] [blame] | 32 | #include <thread> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 |  | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 34 | #include <android/os/IExternalVibratorService.h> | 
| Gloria Wang | 9ee159b | 2011-02-24 14:51:45 -0800 | [diff] [blame] | 35 | #include <binder/IPCThreadState.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #include <binder/IServiceManager.h> | 
|  | 37 | #include <utils/Log.h> | 
| Glenn Kasten | d8e6fd3 | 2012-05-07 11:07:57 -0700 | [diff] [blame] | 38 | #include <utils/Trace.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | #include <binder/Parcel.h> | 
| Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 40 | #include <media/audiohal/DeviceHalInterface.h> | 
|  | 41 | #include <media/audiohal/DevicesFactoryHalInterface.h> | 
|  | 42 | #include <media/audiohal/EffectsFactoryHalInterface.h> | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 43 | #include <media/AudioParameter.h> | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 44 | #include <media/TypeConverter.h> | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 45 | #include <memunreachable/memunreachable.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | #include <utils/String16.h> | 
|  | 47 | #include <utils/threads.h> | 
|  | 48 |  | 
| Steven Moreland | f0c02ce | 2018-02-23 14:53:55 -0800 | [diff] [blame] | 49 | #include <cutils/atomic.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | #include <cutils/properties.h> | 
|  | 51 |  | 
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 52 | #include <system/audio.h> | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 53 | #include <audiomanager/AudioManager.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | #include "AudioFlinger.h" | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 56 | #include "NBAIO_Tee.h" | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 58 | #include <media/AudioResamplerPublic.h> | 
|  | 59 |  | 
| Mikhail Naganov | 9fe9401 | 2016-10-14 14:57:40 -0700 | [diff] [blame] | 60 | #include <system/audio_effects/effect_visualizer.h> | 
|  | 61 | #include <system/audio_effects/effect_ns.h> | 
|  | 62 | #include <system/audio_effects/effect_aec.h> | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 |  | 
| Glenn Kasten | 3b21c50 | 2011-12-15 09:52:39 -0800 | [diff] [blame] | 64 | #include <audio_utils/primitives.h> | 
|  | 65 |  | 
| Eric Laurent | feb0db6 | 2011-07-22 09:04:31 -0700 | [diff] [blame] | 66 | #include <powermanager/PowerManager.h> | 
| Glenn Kasten | 190a46f | 2012-03-06 11:27:10 -0800 | [diff] [blame] | 67 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 68 | #include <media/IMediaLogService.h> | 
| Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 69 | #include <media/MemoryLeakTrackUtil.h> | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 70 | #include <media/nbaio/Pipe.h> | 
|  | 71 | #include <media/nbaio/PipeReader.h> | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 72 | #include <mediautils/BatteryNotifier.h> | 
| Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 73 | #include <mediautils/ServiceUtilities.h> | 
| Eric Laurent | b680e95 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 74 | #include <mediautils/TimeCheck.h> | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 75 | #include <private/android_filesystem_config.h> | 
| Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 76 |  | 
| rago | 3bd1c87 | 2016-09-26 12:58:14 -0700 | [diff] [blame] | 77 | //#define BUFLOG_NDEBUG 0 | 
|  | 78 | #include <BufLog.h> | 
|  | 79 |  | 
| Nicolas Roulet | fe1e144 | 2017-01-30 12:02:03 -0800 | [diff] [blame] | 80 | #include "TypedLogger.h" | 
|  | 81 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 | // ---------------------------------------------------------------------------- | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 83 |  | 
| John Grossman | 1c34519 | 2012-03-27 14:00:17 -0700 | [diff] [blame] | 84 | // Note: the following macro is used for extremely verbose logging message.  In | 
|  | 85 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to | 
|  | 86 | // 0; but one side effect of this is to turn all LOGV's as well.  Some messages | 
|  | 87 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT | 
|  | 88 | // turned on.  Do not uncomment the #def below unless you really know what you | 
|  | 89 | // are doing and want to see all of the extremely verbose messages. | 
|  | 90 | //#define VERY_VERY_VERBOSE_LOGGING | 
|  | 91 | #ifdef VERY_VERY_VERBOSE_LOGGING | 
|  | 92 | #define ALOGVV ALOGV | 
|  | 93 | #else | 
|  | 94 | #define ALOGVV(a...) do { } while(0) | 
|  | 95 | #endif | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 96 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 97 | namespace android { | 
|  | 98 |  | 
| Glenn Kasten | ec1d6b5 | 2011-12-12 09:04:45 -0800 | [diff] [blame] | 99 | static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n"; | 
|  | 100 | static const char kHardwareLockedString[] = "Hardware lock is taken\n"; | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 101 | static const char kClientLockedString[] = "Client lock is taken\n"; | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 102 | static const char kNoEffectsFactory[] = "Effects Factory is absent\n"; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 103 |  | 
| Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 104 |  | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 105 | nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; | 
| Eric Laurent | 7cafbb3 | 2011-11-22 18:50:29 -0800 | [diff] [blame] | 106 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 107 | uint32_t AudioFlinger::mScreenState; | 
| Glenn Kasten | 3ed2920 | 2012-08-07 15:24:44 -0700 | [diff] [blame] | 108 |  | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 109 | // In order to avoid invalidating offloaded tracks each time a Visualizer is turned on and off | 
|  | 110 | // we define a minimum time during which a global effect is considered enabled. | 
|  | 111 | static const nsecs_t kMinGlobalEffectEnabletimeNs = seconds(7200); | 
|  | 112 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 113 | Mutex gLock; | 
|  | 114 | wp<AudioFlinger> gAudioFlinger; | 
|  | 115 |  | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 116 | // Keep a strong reference to media.log service around forever. | 
|  | 117 | // The service is within our parent process so it can never die in a way that we could observe. | 
|  | 118 | // These two variables are const after initialization. | 
|  | 119 | static sp<IBinder> sMediaLogServiceAsBinder; | 
|  | 120 | static sp<IMediaLogService> sMediaLogService; | 
|  | 121 |  | 
|  | 122 | static pthread_once_t sMediaLogOnce = PTHREAD_ONCE_INIT; | 
|  | 123 |  | 
|  | 124 | static void sMediaLogInit() | 
|  | 125 | { | 
|  | 126 | sMediaLogServiceAsBinder = defaultServiceManager()->getService(String16("media.log")); | 
|  | 127 | if (sMediaLogServiceAsBinder != 0) { | 
|  | 128 | sMediaLogService = interface_cast<IMediaLogService>(sMediaLogServiceAsBinder); | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 132 | // Keep a strong reference to external vibrator service | 
|  | 133 | static sp<os::IExternalVibratorService> sExternalVibratorService; | 
|  | 134 |  | 
|  | 135 | static sp<os::IExternalVibratorService> getExternalVibratorService() { | 
|  | 136 | if (sExternalVibratorService == 0) { | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 137 | sp<IBinder> binder = defaultServiceManager()->getService( | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 138 | String16("external_vibrator_service")); | 
|  | 139 | if (binder != 0) { | 
|  | 140 | sExternalVibratorService = | 
|  | 141 | interface_cast<os::IExternalVibratorService>(binder); | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | return sExternalVibratorService; | 
|  | 145 | } | 
|  | 146 |  | 
| Mikhail Naganov | 3de4f26 | 2020-03-06 17:02:19 -0800 | [diff] [blame] | 147 | class DevicesFactoryHalCallbackImpl : public DevicesFactoryHalCallback { | 
|  | 148 | public: | 
|  | 149 | void onNewDevicesAvailable() override { | 
|  | 150 | // Start a detached thread to execute notification in parallel. | 
|  | 151 | // This is done to prevent mutual blocking of audio_flinger and | 
|  | 152 | // audio_policy services during system initialization. | 
|  | 153 | std::thread notifier([]() { | 
|  | 154 | AudioSystem::onNewAudioModulesAvailable(); | 
|  | 155 | }); | 
|  | 156 | notifier.detach(); | 
|  | 157 | } | 
|  | 158 | }; | 
|  | 159 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 160 | // ---------------------------------------------------------------------------- | 
|  | 161 |  | 
| Mikhail Naganov | 913d06c | 2016-11-01 12:49:22 -0700 | [diff] [blame] | 162 | std::string formatToString(audio_format_t format) { | 
|  | 163 | std::string result; | 
|  | 164 | FormatConverter::toString(format, result); | 
|  | 165 | return result; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | // ---------------------------------------------------------------------------- | 
|  | 169 |  | 
|  | 170 | AudioFlinger::AudioFlinger() | 
|  | 171 | : BnAudioFlinger(), | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 172 | mMediaLogNotifier(new AudioFlinger::MediaLogNotifier()), | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 173 | mPrimaryHardwareDev(NULL), | 
| Glenn Kasten | 9ea65d0 | 2014-01-17 10:21:24 -0800 | [diff] [blame] | 174 | mAudioHwDevs(NULL), | 
| Glenn Kasten | 7d6c35b | 2012-07-02 12:45:10 -0700 | [diff] [blame] | 175 | mHardwareStatus(AUDIO_HW_IDLE), | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 176 | mMasterVolume(1.0f), | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 177 | mMasterMute(false), | 
| Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 178 | // mNextUniqueId(AUDIO_UNIQUE_ID_USE_MAX), | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 179 | mMode(AUDIO_MODE_INVALID), | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 180 | mBtNrecIsOff(false), | 
|  | 181 | mIsLowRamDevice(true), | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 182 | mIsDeviceTypeKnown(false), | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 183 | mTotalMemory(0), | 
|  | 184 | mClientSharedHeapSize(kMinimumClientSharedHeapSizeBytes), | 
| Glenn Kasten | 4ea00a2 | 2014-06-02 08:29:22 -0700 | [diff] [blame] | 185 | mGlobalEffectEnableTime(0), | 
| Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 186 | mPatchPanel(this), | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 187 | mDeviceEffectManager(this), | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 188 | mSystemReady(false) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 189 | { | 
| Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 190 | // unsigned instead of audio_unique_id_use_t, because ++ operator is unavailable for enum | 
|  | 191 | for (unsigned use = AUDIO_UNIQUE_ID_USE_UNSPECIFIED; use < AUDIO_UNIQUE_ID_USE_MAX; use++) { | 
|  | 192 | // zero ID has a special meaning, so unavailable | 
|  | 193 | mNextUniqueIds[use] = AUDIO_UNIQUE_ID_USE_MAX; | 
|  | 194 | } | 
|  | 195 |  | 
| Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 196 | const bool doLog = property_get_bool("ro.test_harness", false); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 197 | if (doLog) { | 
| Glenn Kasten | b187de1 | 2014-12-30 08:18:15 -0800 | [diff] [blame] | 198 | mLogMemoryDealer = new MemoryDealer(kLogMemorySize, "LogWriters", | 
|  | 199 | MemoryHeapBase::READ_ONLY); | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 200 | (void) pthread_once(&sMediaLogOnce, sMediaLogInit); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 201 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 202 |  | 
| Wei Jia | 3f273d1 | 2015-11-24 09:06:49 -0800 | [diff] [blame] | 203 | // reset battery stats. | 
|  | 204 | // if the audio service has crashed, battery stats could be left | 
|  | 205 | // in bad state, reset the state upon service start. | 
|  | 206 | BatteryNotifier::getInstance().noteResetAudio(); | 
|  | 207 |  | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 208 | mDevicesFactoryHal = DevicesFactoryHalInterface::create(); | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 209 | mEffectsFactoryHal = EffectsFactoryHalInterface::create(); | 
|  | 210 |  | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 211 | mMediaLogNotifier->run("MediaLogNotifier"); | 
| Eric Laurent | b680e95 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 212 | std::vector<pid_t> halPids; | 
|  | 213 | mDevicesFactoryHal->getHalPids(&halPids); | 
|  | 214 | TimeCheck::setAudioHalPids(halPids); | 
| Dima Zavin | 5a61d2f | 2011-04-19 19:04:32 -0700 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | void AudioFlinger::onFirstRef() | 
|  | 218 | { | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 219 | Mutex::Autolock _l(mLock); | 
|  | 220 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 221 | /* TODO: move all this work into an Init() function */ | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 222 | char val_str[PROPERTY_VALUE_MAX] = { 0 }; | 
|  | 223 | if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) { | 
|  | 224 | uint32_t int_val; | 
|  | 225 | if (1 == sscanf(val_str, "%u", &int_val)) { | 
|  | 226 | mStandbyTimeInNsecs = milliseconds(int_val); | 
|  | 227 | ALOGI("Using %u mSec as standby time.", int_val); | 
|  | 228 | } else { | 
|  | 229 | mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs; | 
|  | 230 | ALOGI("Using default %u mSec as standby time.", | 
|  | 231 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); | 
|  | 232 | } | 
|  | 233 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 |  | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 235 | mMode = AUDIO_MODE_NORMAL; | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 236 |  | 
|  | 237 | gAudioFlinger = this; | 
| Mikhail Naganov | 3de4f26 | 2020-03-06 17:02:19 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | mDevicesFactoryHalCallback = new DevicesFactoryHalCallbackImpl; | 
|  | 240 | mDevicesFactoryHal->setCallbackOnce(mDevicesFactoryHalCallback); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
| Eric Laurent | b680e95 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 243 | status_t AudioFlinger::setAudioHalPids(const std::vector<pid_t>& pids) { | 
|  | 244 | TimeCheck::setAudioHalPids(pids); | 
|  | 245 | return NO_ERROR; | 
|  | 246 | } | 
|  | 247 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 248 | AudioFlinger::~AudioFlinger() | 
|  | 249 | { | 
|  | 250 | while (!mRecordThreads.isEmpty()) { | 
| Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 251 | // closeInput_nonvirtual() will remove specified entry from mRecordThreads | 
| Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 252 | closeInput_nonvirtual(mRecordThreads.keyAt(0)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 253 | } | 
|  | 254 | while (!mPlaybackThreads.isEmpty()) { | 
| Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 255 | // closeOutput_nonvirtual() will remove specified entry from mPlaybackThreads | 
| Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 256 | closeOutput_nonvirtual(mPlaybackThreads.keyAt(0)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 257 | } | 
| Andy Hung | b4946b6 | 2019-03-14 11:19:28 -0700 | [diff] [blame] | 258 | while (!mMmapThreads.isEmpty()) { | 
|  | 259 | const audio_io_handle_t io = mMmapThreads.keyAt(0); | 
|  | 260 | if (mMmapThreads.valueAt(0)->isOutput()) { | 
|  | 261 | closeOutput_nonvirtual(io); // removes entry from mMmapThreads | 
|  | 262 | } else { | 
|  | 263 | closeInput_nonvirtual(io);  // removes entry from mMmapThreads | 
|  | 264 | } | 
|  | 265 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 266 |  | 
| Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 267 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 268 | // no mHardwareLock needed, as there are no other references to this | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 269 | delete mAudioHwDevs.valueAt(i); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 270 | } | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | // Tell media.log service about any old writers that still need to be unregistered | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 273 | if (sMediaLogService != 0) { | 
|  | 274 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { | 
|  | 275 | sp<IMemory> iMemory(mUnregisteredWriters.top()->getIMemory()); | 
|  | 276 | mUnregisteredWriters.pop(); | 
|  | 277 | sMediaLogService->unregisterWriter(iMemory); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 278 | } | 
|  | 279 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 282 | //static | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 283 | __attribute__ ((visibility ("default"))) | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 284 | status_t MmapStreamInterface::openMmapStream(MmapStreamInterface::stream_direction_t direction, | 
|  | 285 | const audio_attributes_t *attr, | 
|  | 286 | audio_config_base_t *config, | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 287 | const AudioClient& client, | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 288 | audio_port_handle_t *deviceId, | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 289 | audio_session_t *sessionId, | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 290 | const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 291 | sp<MmapStreamInterface>& interface, | 
|  | 292 | audio_port_handle_t *handle) | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 293 | { | 
|  | 294 | sp<AudioFlinger> af; | 
|  | 295 | { | 
|  | 296 | Mutex::Autolock _l(gLock); | 
|  | 297 | af = gAudioFlinger.promote(); | 
|  | 298 | } | 
|  | 299 | status_t ret = NO_INIT; | 
|  | 300 | if (af != 0) { | 
|  | 301 | ret = af->openMmapStream( | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 302 | direction, attr, config, client, deviceId, | 
|  | 303 | sessionId, callback, interface, handle); | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 304 | } | 
|  | 305 | return ret; | 
|  | 306 | } | 
|  | 307 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 308 | status_t AudioFlinger::openMmapStream(MmapStreamInterface::stream_direction_t direction, | 
|  | 309 | const audio_attributes_t *attr, | 
|  | 310 | audio_config_base_t *config, | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 311 | const AudioClient& client, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 312 | audio_port_handle_t *deviceId, | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 313 | audio_session_t *sessionId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 314 | const sp<MmapStreamCallback>& callback, | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 315 | sp<MmapStreamInterface>& interface, | 
|  | 316 | audio_port_handle_t *handle) | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 317 | { | 
|  | 318 | status_t ret = initCheck(); | 
|  | 319 | if (ret != NO_ERROR) { | 
|  | 320 | return ret; | 
|  | 321 | } | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 322 | audio_session_t actualSessionId = *sessionId; | 
|  | 323 | if (actualSessionId == AUDIO_SESSION_ALLOCATE) { | 
|  | 324 | actualSessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 325 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 326 | audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT; | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 327 | audio_io_handle_t io = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 328 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 329 | audio_attributes_t localAttr = *attr; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 330 | if (direction == MmapStreamInterface::DIRECTION_OUTPUT) { | 
|  | 331 | audio_config_t fullConfig = AUDIO_CONFIG_INITIALIZER; | 
|  | 332 | fullConfig.sample_rate = config->sample_rate; | 
|  | 333 | fullConfig.channel_mask = config->channel_mask; | 
|  | 334 | fullConfig.format = config->format; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 335 | std::vector<audio_io_handle_t> secondaryOutputs; | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 336 |  | 
|  | 337 | ret = AudioSystem::getOutputForAttr(&localAttr, &io, | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 338 | actualSessionId, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 339 | &streamType, client.clientPid, client.clientUid, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 340 | &fullConfig, | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 341 | (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | | 
|  | 342 | AUDIO_OUTPUT_FLAG_DIRECT), | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 343 | deviceId, &portId, &secondaryOutputs); | 
|  | 344 | ALOGW_IF(!secondaryOutputs.empty(), | 
|  | 345 | "%s does not support secondary outputs, ignoring them", __func__); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 346 | } else { | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 347 | ret = AudioSystem::getInputForAttr(&localAttr, &io, | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 348 | RECORD_RIID_INVALID, | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 349 | actualSessionId, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 350 | client.clientPid, | 
|  | 351 | client.clientUid, | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 352 | client.packageName, | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 353 | config, | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 354 | AUDIO_INPUT_FLAG_MMAP_NOIRQ, deviceId, &portId); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 355 | } | 
|  | 356 | if (ret != NO_ERROR) { | 
|  | 357 | return ret; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | // at this stage, a MmapThread was created when openOutput() or openInput() was called by | 
|  | 361 | // audio policy manager and we can retrieve it | 
|  | 362 | sp<MmapThread> thread = mMmapThreads.valueFor(io); | 
|  | 363 | if (thread != 0) { | 
|  | 364 | interface = new MmapThreadHandle(thread); | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 365 | thread->configure(&localAttr, streamType, actualSessionId, callback, *deviceId, portId); | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 366 | *handle = portId; | 
| Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 367 | *sessionId = actualSessionId; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 368 | } else { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 369 | if (direction == MmapStreamInterface::DIRECTION_OUTPUT) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 370 | AudioSystem::releaseOutput(portId); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 371 | } else { | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 372 | AudioSystem::releaseInput(portId); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 373 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 374 | ret = NO_INIT; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | ALOGV("%s done status %d portId %d", __FUNCTION__, ret, portId); | 
|  | 378 |  | 
| Eric Laurent | fc23520 | 2016-12-20 18:48:17 -0800 | [diff] [blame] | 379 | return ret; | 
|  | 380 | } | 
|  | 381 |  | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 382 | /* static */ | 
|  | 383 | int AudioFlinger::onExternalVibrationStart(const sp<os::ExternalVibration>& externalVibration) { | 
|  | 384 | sp<os::IExternalVibratorService> evs = getExternalVibratorService(); | 
|  | 385 | if (evs != 0) { | 
|  | 386 | int32_t ret; | 
|  | 387 | binder::Status status = evs->onExternalVibrationStart(*externalVibration, &ret); | 
|  | 388 | if (status.isOk()) { | 
|  | 389 | return ret; | 
|  | 390 | } | 
|  | 391 | } | 
| jiabin | bf6b0ec | 2019-02-12 12:30:12 -0800 | [diff] [blame] | 392 | return AudioMixer::HAPTIC_SCALE_MUTE; | 
| jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
|  | 395 | /* static */ | 
|  | 396 | void AudioFlinger::onExternalVibrationStop(const sp<os::ExternalVibration>& externalVibration) { | 
|  | 397 | sp<os::IExternalVibratorService> evs = getExternalVibratorService(); | 
|  | 398 | if (evs != 0) { | 
|  | 399 | evs->onExternalVibrationStop(*externalVibration); | 
|  | 400 | } | 
|  | 401 | } | 
|  | 402 |  | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 403 | status_t AudioFlinger::addEffectToHal(audio_port_handle_t deviceId, | 
|  | 404 | audio_module_handle_t hwModuleId, sp<EffectHalInterface> effect) { | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 405 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 406 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(hwModuleId); | 
|  | 407 | if (audioHwDevice == nullptr) { | 
|  | 408 | return NO_INIT; | 
|  | 409 | } | 
|  | 410 | return audioHwDevice->hwDevice()->addDeviceEffect(deviceId, effect); | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | status_t AudioFlinger::removeEffectFromHal(audio_port_handle_t deviceId, | 
|  | 414 | audio_module_handle_t hwModuleId, sp<EffectHalInterface> effect) { | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 415 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 416 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(hwModuleId); | 
|  | 417 | if (audioHwDevice == nullptr) { | 
|  | 418 | return NO_INIT; | 
|  | 419 | } | 
|  | 420 | return audioHwDevice->hwDevice()->removeDeviceEffect(deviceId, effect); | 
|  | 421 | } | 
|  | 422 |  | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 423 | static const char * const audio_interfaces[] = { | 
|  | 424 | AUDIO_HARDWARE_MODULE_ID_PRIMARY, | 
|  | 425 | AUDIO_HARDWARE_MODULE_ID_A2DP, | 
|  | 426 | AUDIO_HARDWARE_MODULE_ID_USB, | 
|  | 427 | }; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 428 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 429 | AudioHwDevice* AudioFlinger::findSuitableHwDev_l( | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 430 | audio_module_handle_t module, | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 431 | audio_devices_t deviceType) | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 432 | { | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 433 | // if module is 0, the request comes from an old policy manager and we should load | 
|  | 434 | // well known modules | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 435 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 436 | if (module == 0) { | 
|  | 437 | ALOGW("findSuitableHwDev_l() loading well know audio hw modules"); | 
| Mikhail Naganov | bf49308 | 2017-04-17 17:37:12 -0700 | [diff] [blame] | 438 | for (size_t i = 0; i < arraysize(audio_interfaces); i++) { | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 439 | loadHwModule_l(audio_interfaces[i]); | 
|  | 440 | } | 
| Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 441 | // then try to find a module supporting the requested device. | 
|  | 442 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 443 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(i); | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 444 | sp<DeviceHalInterface> dev = audioHwDevice->hwDevice(); | 
|  | 445 | uint32_t supportedDevices; | 
|  | 446 | if (dev->getSupportedDevices(&supportedDevices) == OK && | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 447 | (supportedDevices & deviceType) == deviceType) { | 
| Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 448 | return audioHwDevice; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 449 | } | 
| Eric Laurent | f1c04f9 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 450 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 451 | } else { | 
|  | 452 | // check a match for the requested module handle | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 453 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueFor(module); | 
|  | 454 | if (audioHwDevice != NULL) { | 
|  | 455 | return audioHwDevice; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 456 | } | 
|  | 457 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 458 |  | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 459 | return NULL; | 
|  | 460 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 462 | void AudioFlinger::dumpClients(int fd, const Vector<String16>& args __unused) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 463 | { | 
|  | 464 | const size_t SIZE = 256; | 
|  | 465 | char buffer[SIZE]; | 
|  | 466 | String8 result; | 
|  | 467 |  | 
|  | 468 | result.append("Clients:\n"); | 
|  | 469 | for (size_t i = 0; i < mClients.size(); ++i) { | 
| Glenn Kasten | 77c1119 | 2012-01-25 14:27:41 -0800 | [diff] [blame] | 470 | sp<Client> client = mClients.valueAt(i).promote(); | 
|  | 471 | if (client != 0) { | 
|  | 472 | snprintf(buffer, SIZE, "  pid: %d\n", client->pid()); | 
|  | 473 | result.append(buffer); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 474 | } | 
|  | 475 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 476 |  | 
| Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 477 | result.append("Notification Clients:\n"); | 
|  | 478 | for (size_t i = 0; i < mNotificationClients.size(); ++i) { | 
|  | 479 | snprintf(buffer, SIZE, "  pid: %d\n", mNotificationClients.keyAt(i)); | 
|  | 480 | result.append(buffer); | 
|  | 481 | } | 
|  | 482 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 483 | result.append("Global session refs:\n"); | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 484 | result.append("  session   pid count\n"); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 485 | for (size_t i = 0; i < mAudioSessionRefs.size(); i++) { | 
|  | 486 | AudioSessionRef *r = mAudioSessionRefs[i]; | 
| Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 487 | snprintf(buffer, SIZE, "  %7d %5d %5d\n", r->mSessionid, r->mPid, r->mCnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 488 | result.append(buffer); | 
|  | 489 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 490 | write(fd, result.string(), result.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 491 | } | 
|  | 492 |  | 
|  | 493 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 494 | void AudioFlinger::dumpInternals(int fd, const Vector<String16>& args __unused) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 495 | { | 
|  | 496 | const size_t SIZE = 256; | 
|  | 497 | char buffer[SIZE]; | 
|  | 498 | String8 result; | 
| Glenn Kasten | a4454b4 | 2012-01-04 11:02:33 -0800 | [diff] [blame] | 499 | hardware_call_state hardwareStatus = mHardwareStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 500 |  | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 501 | snprintf(buffer, SIZE, "Hardware status: %d\n" | 
|  | 502 | "Standby Time mSec: %u\n", | 
|  | 503 | hardwareStatus, | 
|  | 504 | (uint32_t)(mStandbyTimeInNsecs / 1000000)); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 505 | result.append(buffer); | 
|  | 506 | write(fd, result.string(), result.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 507 | } | 
|  | 508 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 509 | void AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args __unused) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 510 | { | 
|  | 511 | const size_t SIZE = 256; | 
|  | 512 | char buffer[SIZE]; | 
|  | 513 | String8 result; | 
|  | 514 | snprintf(buffer, SIZE, "Permission Denial: " | 
|  | 515 | "can't dump AudioFlinger from pid=%d, uid=%d\n", | 
|  | 516 | IPCThreadState::self()->getCallingPid(), | 
|  | 517 | IPCThreadState::self()->getCallingUid()); | 
|  | 518 | result.append(buffer); | 
|  | 519 | write(fd, result.string(), result.size()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 522 | bool AudioFlinger::dumpTryLock(Mutex& mutex) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 523 | { | 
| Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 524 | status_t err = mutex.timedLock(kDumpLockTimeoutNs); | 
|  | 525 | return err == NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 526 | } | 
|  | 527 |  | 
|  | 528 | status_t AudioFlinger::dump(int fd, const Vector<String16>& args) | 
|  | 529 | { | 
| Glenn Kasten | 44deb05 | 2012-02-05 18:09:08 -0800 | [diff] [blame] | 530 | if (!dumpAllowed()) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | dumpPermissionDenial(fd, args); | 
|  | 532 | } else { | 
|  | 533 | // get state of hardware lock | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 534 | bool hardwareLocked = dumpTryLock(mHardwareLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 535 | if (!hardwareLocked) { | 
|  | 536 | String8 result(kHardwareLockedString); | 
|  | 537 | write(fd, result.string(), result.size()); | 
|  | 538 | } else { | 
|  | 539 | mHardwareLock.unlock(); | 
|  | 540 | } | 
|  | 541 |  | 
| Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 542 | const bool locked = dumpTryLock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 543 |  | 
|  | 544 | // failed to lock - AudioFlinger is probably deadlocked | 
|  | 545 | if (!locked) { | 
|  | 546 | String8 result(kDeadlockedString); | 
|  | 547 | write(fd, result.string(), result.size()); | 
|  | 548 | } | 
|  | 549 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 550 | bool clientLocked = dumpTryLock(mClientLock); | 
|  | 551 | if (!clientLocked) { | 
|  | 552 | String8 result(kClientLockedString); | 
|  | 553 | write(fd, result.string(), result.size()); | 
|  | 554 | } | 
| Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 555 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 556 | if (mEffectsFactoryHal != 0) { | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 557 | mEffectsFactoryHal->dumpEffects(fd); | 
|  | 558 | } else { | 
|  | 559 | String8 result(kNoEffectsFactory); | 
|  | 560 | write(fd, result.string(), result.size()); | 
|  | 561 | } | 
| Marco Nelissen | d89eadd | 2014-10-07 13:28:44 -0700 | [diff] [blame] | 562 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 563 | dumpClients(fd, args); | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 564 | if (clientLocked) { | 
|  | 565 | mClientLock.unlock(); | 
|  | 566 | } | 
|  | 567 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 568 | dumpInternals(fd, args); | 
|  | 569 |  | 
|  | 570 | // dump playback threads | 
|  | 571 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 572 | mPlaybackThreads.valueAt(i)->dump(fd, args); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | // dump record threads | 
|  | 576 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 577 | mRecordThreads.valueAt(i)->dump(fd, args); | 
|  | 578 | } | 
|  | 579 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 580 | // dump mmap threads | 
|  | 581 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 582 | mMmapThreads.valueAt(i)->dump(fd, args); | 
|  | 583 | } | 
|  | 584 |  | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 585 | // dump orphan effect chains | 
|  | 586 | if (mOrphanEffectChains.size() != 0) { | 
|  | 587 | write(fd, "  Orphan Effect Chains\n", strlen("  Orphan Effect Chains\n")); | 
|  | 588 | for (size_t i = 0; i < mOrphanEffectChains.size(); i++) { | 
|  | 589 | mOrphanEffectChains.valueAt(i)->dump(fd, args); | 
|  | 590 | } | 
|  | 591 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 592 | // dump all hardware devs | 
|  | 593 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 594 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); | 
|  | 595 | dev->dump(fd); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 596 | } | 
| Glenn Kasten | d06785b | 2012-09-30 12:29:28 -0700 | [diff] [blame] | 597 |  | 
| Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 598 | mPatchPanel.dump(fd); | 
|  | 599 |  | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 600 | mDeviceEffectManager.dump(fd); | 
|  | 601 |  | 
| Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 602 | // dump external setParameters | 
|  | 603 | auto dumpLogger = [fd](SimpleLog& logger, const char* name) { | 
|  | 604 | dprintf(fd, "\n%s setParameters:\n", name); | 
|  | 605 | logger.dump(fd, "    " /* prefix */); | 
|  | 606 | }; | 
|  | 607 | dumpLogger(mRejectedSetParameterLog, "Rejected"); | 
|  | 608 | dumpLogger(mAppSetParameterLog, "App"); | 
|  | 609 | dumpLogger(mSystemSetParameterLog, "System"); | 
|  | 610 |  | 
| Andy Hung | a8115dc | 2018-08-24 15:51:59 -0700 | [diff] [blame] | 611 | // dump historical threads in the last 10 seconds | 
|  | 612 | const std::string threadLog = mThreadLog.dumpToString( | 
|  | 613 | "Historical Thread Log ", 0 /* lines */, | 
|  | 614 | audio_utils_get_real_time_ns() - 10 * 60 * NANOS_PER_SECOND); | 
|  | 615 | write(fd, threadLog.c_str(), threadLog.size()); | 
|  | 616 |  | 
| rago | 3bd1c87 | 2016-09-26 12:58:14 -0700 | [diff] [blame] | 617 | BUFLOG_RESET; | 
|  | 618 |  | 
| Glenn Kasten | d65d73c | 2012-06-22 17:21:07 -0700 | [diff] [blame] | 619 | if (locked) { | 
|  | 620 | mLock.unlock(); | 
|  | 621 | } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 622 |  | 
| Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 623 | #ifdef TEE_SINK | 
|  | 624 | // NBAIO_Tee dump is safe to call outside of AF lock. | 
|  | 625 | NBAIO_Tee::dumpAll(fd, "_DUMP"); | 
|  | 626 | #endif | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 627 | // append a copy of media.log here by forwarding fd to it, but don't attempt | 
|  | 628 | // to lookup the service if it's not running, as it will block for a second | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 629 | if (sMediaLogServiceAsBinder != 0) { | 
|  | 630 | dprintf(fd, "\nmedia.log:\n"); | 
|  | 631 | Vector<String16> args; | 
|  | 632 | sMediaLogServiceAsBinder->dump(fd, args); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 633 | } | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 634 |  | 
|  | 635 | // check for optional arguments | 
| Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 636 | bool dumpMem = false; | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 637 | bool unreachableMemory = false; | 
|  | 638 | for (const auto &arg : args) { | 
| Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 639 | if (arg == String16("-m")) { | 
|  | 640 | dumpMem = true; | 
|  | 641 | } else if (arg == String16("--unreachable")) { | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 642 | unreachableMemory = true; | 
|  | 643 | } | 
|  | 644 | } | 
|  | 645 |  | 
| Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 646 | if (dumpMem) { | 
|  | 647 | dprintf(fd, "\nDumping memory:\n"); | 
|  | 648 | std::string s = dumpMemoryAddresses(100 /* limit */); | 
|  | 649 | write(fd, s.c_str(), s.size()); | 
|  | 650 | } | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 651 | if (unreachableMemory) { | 
|  | 652 | dprintf(fd, "\nDumping unreachable memory:\n"); | 
|  | 653 | // TODO - should limit be an argument parameter? | 
| Andy Hung | 07b745e | 2016-05-23 16:21:07 -0700 | [diff] [blame] | 654 | std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */); | 
| Andy Hung | 35fec5f | 2016-04-13 14:21:48 -0700 | [diff] [blame] | 655 | write(fd, s.c_str(), s.size()); | 
|  | 656 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 657 | } | 
|  | 658 | return NO_ERROR; | 
|  | 659 | } | 
|  | 660 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 661 | sp<AudioFlinger::Client> AudioFlinger::registerPid(pid_t pid) | 
| Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 662 | { | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 663 | Mutex::Autolock _cl(mClientLock); | 
| Glenn Kasten | 98ec94c | 2012-01-25 14:28:29 -0800 | [diff] [blame] | 664 | // If pid is already in the mClients wp<> map, then use that entry | 
|  | 665 | // (for which promote() is always != 0), otherwise create a new entry and Client. | 
|  | 666 | sp<Client> client = mClients.valueFor(pid).promote(); | 
|  | 667 | if (client == 0) { | 
|  | 668 | client = new Client(this, pid); | 
|  | 669 | mClients.add(pid, client); | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | return client; | 
|  | 673 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 674 |  | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 675 | sp<NBLog::Writer> AudioFlinger::newWriter_l(size_t size, const char *name) | 
|  | 676 | { | 
| Jiabin Huang | 6076608 | 2020-07-28 22:03:56 +0000 | [diff] [blame] | 677 | // If there is no memory allocated for logs, return a no-op writer that does nothing. | 
|  | 678 | // Similarly if we can't contact the media.log service, also return a no-op writer. | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 679 | if (mLogMemoryDealer == 0 || sMediaLogService == 0) { | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 680 | return new NBLog::Writer(); | 
|  | 681 | } | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 682 | sp<IMemory> shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); | 
|  | 683 | // If allocation fails, consult the vector of previously unregistered writers | 
|  | 684 | // and garbage-collect one or more them until an allocation succeeds | 
|  | 685 | if (shared == 0) { | 
|  | 686 | Mutex::Autolock _l(mUnregisteredWritersLock); | 
|  | 687 | for (size_t count = mUnregisteredWriters.size(); count > 0; count--) { | 
|  | 688 | { | 
|  | 689 | // Pick the oldest stale writer to garbage-collect | 
|  | 690 | sp<IMemory> iMemory(mUnregisteredWriters[0]->getIMemory()); | 
|  | 691 | mUnregisteredWriters.removeAt(0); | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 692 | sMediaLogService->unregisterWriter(iMemory); | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 693 | // Now the media.log remote reference to IMemory is gone.  When our last local | 
|  | 694 | // reference to IMemory also drops to zero at end of this block, | 
|  | 695 | // the IMemory destructor will deallocate the region from mLogMemoryDealer. | 
|  | 696 | } | 
|  | 697 | // Re-attempt the allocation | 
|  | 698 | shared = mLogMemoryDealer->allocate(NBLog::Timeline::sharedSize(size)); | 
|  | 699 | if (shared != 0) { | 
|  | 700 | goto success; | 
|  | 701 | } | 
|  | 702 | } | 
|  | 703 | // Even after garbage-collecting all old writers, there is still not enough memory, | 
| Jiabin Huang | 6076608 | 2020-07-28 22:03:56 +0000 | [diff] [blame] | 704 | // so return a no-op writer | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 705 | return new NBLog::Writer(); | 
|  | 706 | } | 
|  | 707 | success: | 
| Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 708 | NBLog::Shared *sharedRawPtr = (NBLog::Shared *) shared->pointer(); | 
|  | 709 | new((void *) sharedRawPtr) NBLog::Shared(); // placement new here, but the corresponding | 
|  | 710 | // explicit destructor not needed since it is POD | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 711 | sMediaLogService->registerWriter(shared, size, name); | 
| Glenn Kasten | 535e161 | 2016-12-05 12:19:36 -0800 | [diff] [blame] | 712 | return new NBLog::Writer(shared, size); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 713 | } | 
|  | 714 |  | 
|  | 715 | void AudioFlinger::unregisterWriter(const sp<NBLog::Writer>& writer) | 
|  | 716 | { | 
| Glenn Kasten | 685ef09 | 2013-02-04 08:15:34 -0800 | [diff] [blame] | 717 | if (writer == 0) { | 
|  | 718 | return; | 
|  | 719 | } | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 720 | sp<IMemory> iMemory(writer->getIMemory()); | 
|  | 721 | if (iMemory == 0) { | 
|  | 722 | return; | 
|  | 723 | } | 
| Glenn Kasten | 481fb67 | 2013-09-30 14:39:28 -0700 | [diff] [blame] | 724 | // Rather than removing the writer immediately, append it to a queue of old writers to | 
|  | 725 | // be garbage-collected later.  This allows us to continue to view old logs for a while. | 
|  | 726 | Mutex::Autolock _l(mUnregisteredWritersLock); | 
|  | 727 | mUnregisteredWriters.push(writer); | 
| Glenn Kasten | 9e58b55 | 2013-01-18 15:09:48 -0800 | [diff] [blame] | 728 | } | 
|  | 729 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 730 | // IAudioFlinger interface | 
|  | 731 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 732 | sp<IAudioTrack> AudioFlinger::createTrack(const CreateTrackInput& input, | 
|  | 733 | CreateTrackOutput& output, | 
|  | 734 | status_t *status) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 735 | { | 
|  | 736 | sp<PlaybackThread::Track> track; | 
|  | 737 | sp<TrackHandle> trackHandle; | 
|  | 738 | sp<Client> client; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 739 | status_t lStatus; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 740 | audio_stream_type_t streamType; | 
| Eric Laurent | 77881cd | 2018-02-09 16:01:31 -0800 | [diff] [blame] | 741 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 742 | std::vector<audio_io_handle_t> secondaryOutputs; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 743 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 744 | bool updatePid = (input.clientInfo.clientPid == -1); | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 745 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 746 | uid_t clientUid = input.clientInfo.clientUid; | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 747 | audio_io_handle_t effectThreadId = AUDIO_IO_HANDLE_NONE; | 
|  | 748 | std::vector<int> effectIds; | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 749 | audio_attributes_t localAttr = input.attr; | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 750 |  | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 751 | if (!isAudioServerOrMediaServerUid(callingUid)) { | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 752 | ALOGW_IF(clientUid != callingUid, | 
|  | 753 | "%s uid %d tried to pass itself off as %d", | 
|  | 754 | __FUNCTION__, callingUid, clientUid); | 
|  | 755 | clientUid = callingUid; | 
|  | 756 | updatePid = true; | 
|  | 757 | } | 
|  | 758 | pid_t clientPid = input.clientInfo.clientPid; | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 759 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 760 | if (updatePid) { | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 761 | ALOGW_IF(clientPid != -1 && clientPid != callingPid, | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 762 | "%s uid %d pid %d tried to pass itself off as pid %d", | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 763 | __func__, callingUid, callingPid, clientPid); | 
|  | 764 | clientPid = callingPid; | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 765 | } | 
|  | 766 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 767 | audio_session_t sessionId = input.sessionId; | 
|  | 768 | if (sessionId == AUDIO_SESSION_ALLOCATE) { | 
|  | 769 | sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 770 | } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { | 
|  | 771 | lStatus = BAD_VALUE; | 
|  | 772 | goto Exit; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 773 | } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 774 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 775 | output.sessionId = sessionId; | 
|  | 776 | output.outputId = AUDIO_IO_HANDLE_NONE; | 
|  | 777 | output.selectedDeviceId = input.selectedDeviceId; | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 778 | lStatus = AudioSystem::getOutputForAttr(&localAttr, &output.outputId, sessionId, &streamType, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 779 | clientPid, clientUid, &input.config, input.flags, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 780 | &output.selectedDeviceId, &portId, &secondaryOutputs); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 781 |  | 
|  | 782 | if (lStatus != NO_ERROR || output.outputId == AUDIO_IO_HANDLE_NONE) { | 
|  | 783 | ALOGE("createTrack() getOutputForAttr() return error %d or invalid output handle", lStatus); | 
|  | 784 | goto Exit; | 
|  | 785 | } | 
| Glenn Kasten | 263709e | 2012-01-06 08:40:01 -0800 | [diff] [blame] | 786 | // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC, | 
|  | 787 | // but if someone uses binder directly they could bypass that and cause us to crash | 
|  | 788 | if (uint32_t(streamType) >= AUDIO_STREAM_CNT) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 789 | ALOGE("createTrack() invalid stream type %d", streamType); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 790 | lStatus = BAD_VALUE; | 
|  | 791 | goto Exit; | 
|  | 792 | } | 
|  | 793 |  | 
| Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 794 | // further channel mask checks are performed by createTrack_l() depending on the thread type | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 795 | if (!audio_is_output_channel(input.config.channel_mask)) { | 
|  | 796 | ALOGE("createTrack() invalid channel mask %#x", input.config.channel_mask); | 
| Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 797 | lStatus = BAD_VALUE; | 
|  | 798 | goto Exit; | 
|  | 799 | } | 
|  | 800 |  | 
| Glenn Kasten | c4b88a8 | 2014-04-30 16:54:30 -0700 | [diff] [blame] | 801 | // further format checks are performed by createTrack_l() depending on the thread type | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 802 | if (!audio_is_valid_format(input.config.format)) { | 
|  | 803 | ALOGE("createTrack() invalid format %#x", input.config.format); | 
| Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 804 | lStatus = BAD_VALUE; | 
|  | 805 | goto Exit; | 
|  | 806 | } | 
|  | 807 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 808 | { | 
|  | 809 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 810 | PlaybackThread *thread = checkPlaybackThread_l(output.outputId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 811 | if (thread == NULL) { | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 812 | ALOGE("no playback thread found for output handle %d", output.outputId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 813 | lStatus = BAD_VALUE; | 
|  | 814 | goto Exit; | 
|  | 815 | } | 
|  | 816 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 817 | client = registerPid(clientPid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 818 |  | 
| Glenn Kasten | e848bd9 | 2014-03-13 15:00:32 -0700 | [diff] [blame] | 819 | PlaybackThread *effectThread = NULL; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 820 | // check if an effect chain with the same session ID is present on another | 
|  | 821 | // output thread and move it here. | 
|  | 822 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 823 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 824 | if (mPlaybackThreads.keyAt(i) != output.outputId) { | 
|  | 825 | uint32_t sessions = t->hasAudioSession(sessionId); | 
|  | 826 | if (sessions & ThreadBase::EFFECT_SESSION) { | 
|  | 827 | effectThread = t.get(); | 
|  | 828 | break; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 829 | } | 
|  | 830 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 831 | } | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 832 | ALOGV("createTrack() sessionId: %d", sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 833 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 834 | output.sampleRate = input.config.sample_rate; | 
|  | 835 | output.frameCount = input.frameCount; | 
|  | 836 | output.notificationFrameCount = input.notificationFrameCount; | 
|  | 837 | output.flags = input.flags; | 
|  | 838 |  | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 839 | track = thread->createTrack_l(client, streamType, localAttr, &output.sampleRate, | 
| Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 840 | input.config.format, input.config.channel_mask, | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 841 | &output.frameCount, &output.notificationFrameCount, | 
|  | 842 | input.notificationsPerBuffer, input.speed, | 
|  | 843 | input.sharedBuffer, sessionId, &output.flags, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 844 | callingPid, input.clientInfo.clientTid, clientUid, | 
|  | 845 | &lStatus, portId); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 846 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); | 
| Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 847 | // we don't abort yet if lStatus != NO_ERROR; there is still work to be done regardless | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 848 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 849 | output.afFrameCount = thread->frameCount(); | 
|  | 850 | output.afSampleRate = thread->sampleRate(); | 
|  | 851 | output.afLatencyMs = thread->latency(); | 
| Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 852 | output.portId = portId; | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 853 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 854 | if (lStatus == NO_ERROR) { | 
|  | 855 | // Connect secondary outputs. Failure on a secondary output must not imped the primary | 
|  | 856 | // Any secondary output setup failure will lead to a desync between the AP and AF until | 
|  | 857 | // the track is destroyed. | 
|  | 858 | TeePatches teePatches; | 
|  | 859 | for (audio_io_handle_t secondaryOutput : secondaryOutputs) { | 
|  | 860 | PlaybackThread *secondaryThread = checkPlaybackThread_l(secondaryOutput); | 
|  | 861 | if (secondaryThread == NULL) { | 
|  | 862 | ALOGE("no playback thread found for secondary output %d", output.outputId); | 
|  | 863 | continue; | 
|  | 864 | } | 
|  | 865 |  | 
| Kevin Rocard | 20a44f8 | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 866 | size_t sourceFrameCount = thread->frameCount() * output.sampleRate | 
|  | 867 | / thread->sampleRate(); | 
|  | 868 | size_t sinkFrameCount = secondaryThread->frameCount() * output.sampleRate | 
|  | 869 | / secondaryThread->sampleRate(); | 
|  | 870 | // If the secondary output has just been opened, the first secondaryThread write | 
|  | 871 | // will not block as it will fill the empty startup buffer of the HAL, | 
|  | 872 | // so a second sink buffer needs to be ready for the immediate next blocking write. | 
|  | 873 | // Additionally, have a margin of one main thread buffer as the scheduling jitter | 
|  | 874 | // can reorder the writes (eg if thread A&B have the same write intervale, | 
|  | 875 | // the scheduler could schedule AB...BA) | 
|  | 876 | size_t frameCountToBeReady = 2 * sinkFrameCount + sourceFrameCount; | 
|  | 877 | // Total secondary output buffer must be at least as the read frames plus | 
|  | 878 | // the margin of a few buffers on both sides in case the | 
|  | 879 | // threads scheduling has some jitter. | 
|  | 880 | // That value should not impact latency as the secondary track is started before | 
|  | 881 | // its buffer is full, see frameCountToBeReady. | 
|  | 882 | size_t frameCount = frameCountToBeReady + 2 * (sourceFrameCount + sinkFrameCount); | 
|  | 883 | // The frameCount should also not be smaller than the secondary thread min frame | 
|  | 884 | // count | 
|  | 885 | size_t minFrameCount = AudioSystem::calculateMinFrameCount( | 
|  | 886 | [&] { Mutex::Autolock _l(secondaryThread->mLock); | 
|  | 887 | return secondaryThread->latency_l(); }(), | 
|  | 888 | secondaryThread->mNormalFrameCount, | 
|  | 889 | secondaryThread->mSampleRate, | 
|  | 890 | output.sampleRate, | 
|  | 891 | input.speed); | 
|  | 892 | frameCount = std::max(frameCount, minFrameCount); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 893 |  | 
|  | 894 | using namespace std::chrono_literals; | 
|  | 895 | auto inChannelMask = audio_channel_mask_out_to_in(input.config.channel_mask); | 
|  | 896 | sp patchRecord = new RecordThread::PatchRecord(nullptr /* thread */, | 
|  | 897 | output.sampleRate, | 
|  | 898 | inChannelMask, | 
|  | 899 | input.config.format, | 
|  | 900 | frameCount, | 
|  | 901 | NULL /* buffer */, | 
|  | 902 | (size_t)0 /* bufferSize */, | 
|  | 903 | AUDIO_INPUT_FLAG_DIRECT, | 
|  | 904 | 0ns /* timeout */); | 
|  | 905 | status_t status = patchRecord->initCheck(); | 
|  | 906 | if (status != NO_ERROR) { | 
|  | 907 | ALOGE("Secondary output patchRecord init failed: %d", status); | 
|  | 908 | continue; | 
|  | 909 | } | 
| Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 910 |  | 
|  | 911 | // TODO: We could check compatibility of the secondaryThread with the PatchTrack | 
|  | 912 | // for fast usage: thread has fast mixer, sample rate matches, etc.; | 
|  | 913 | // for now, we exclude fast tracks by removing the Fast flag. | 
|  | 914 | const audio_output_flags_t outputFlags = | 
|  | 915 | (audio_output_flags_t)(output.flags & ~AUDIO_OUTPUT_FLAG_FAST); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 916 | sp patchTrack = new PlaybackThread::PatchTrack(secondaryThread, | 
|  | 917 | streamType, | 
|  | 918 | output.sampleRate, | 
|  | 919 | input.config.channel_mask, | 
|  | 920 | input.config.format, | 
|  | 921 | frameCount, | 
|  | 922 | patchRecord->buffer(), | 
|  | 923 | patchRecord->bufferSize(), | 
| Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 924 | outputFlags, | 
| Kevin Rocard | 20a44f8 | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 925 | 0ns /* timeout */, | 
|  | 926 | frameCountToBeReady); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 927 | status = patchTrack->initCheck(); | 
|  | 928 | if (status != NO_ERROR) { | 
|  | 929 | ALOGE("Secondary output patchTrack init failed: %d", status); | 
|  | 930 | continue; | 
|  | 931 | } | 
|  | 932 | teePatches.push_back({patchRecord, patchTrack}); | 
|  | 933 | secondaryThread->addPatchTrack(patchTrack); | 
| Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 934 | // In case the downstream patchTrack on the secondaryThread temporarily outlives | 
|  | 935 | // our created track, ensure the corresponding patchRecord is still alive. | 
|  | 936 | patchTrack->setPeerProxy(patchRecord, true /* holdReference */); | 
|  | 937 | patchRecord->setPeerProxy(patchTrack, false /* holdReference */); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 938 | } | 
|  | 939 | track->setTeePatches(std::move(teePatches)); | 
|  | 940 | } | 
|  | 941 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 942 | // move effect chain to this output thread if an effect on same session was waiting | 
|  | 943 | // for a track to be created | 
|  | 944 | if (lStatus == NO_ERROR && effectThread != NULL) { | 
| Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 945 | // no risk of deadlock because AudioFlinger::mLock is held | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 946 | Mutex::Autolock _dl(thread->mLock); | 
|  | 947 | Mutex::Autolock _sl(effectThread->mLock); | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 948 | if (moveEffectChain_l(sessionId, effectThread, thread) == NO_ERROR) { | 
|  | 949 | effectThreadId = thread->id(); | 
|  | 950 | effectIds = thread->getEffectIds_l(sessionId); | 
|  | 951 | } | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 952 | } | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 953 |  | 
|  | 954 | // Look for sync events awaiting for a session to be used. | 
| Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 955 | for (size_t i = 0; i < mPendingSyncEvents.size(); i++) { | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 956 | if (mPendingSyncEvents[i]->triggerSession() == sessionId) { | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 957 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { | 
| Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 958 | if (lStatus == NO_ERROR) { | 
| Glenn Kasten | d23eedc | 2012-08-02 13:35:47 -0700 | [diff] [blame] | 959 | (void) track->setSyncEvent(mPendingSyncEvents[i]); | 
| Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 960 | } else { | 
|  | 961 | mPendingSyncEvents[i]->cancel(); | 
|  | 962 | } | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 963 | mPendingSyncEvents.removeAt(i); | 
|  | 964 | i--; | 
|  | 965 | } | 
|  | 966 | } | 
|  | 967 | } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 968 |  | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 969 | setAudioHwSyncForSession_l(thread, sessionId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 970 | } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 971 |  | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 972 | if (lStatus != NO_ERROR) { | 
|  | 973 | // remove local strong reference to Client before deleting the Track so that the | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 974 | // Client destructor is called by the TrackBase destructor with mClientLock held | 
| Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 975 | // Don't hold mClientLock when releasing the reference on the track as the | 
|  | 976 | // destructor will acquire it. | 
|  | 977 | { | 
|  | 978 | Mutex::Autolock _cl(mClientLock); | 
|  | 979 | client.clear(); | 
|  | 980 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 981 | track.clear(); | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 982 | goto Exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 983 | } | 
|  | 984 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 985 | // effectThreadId is not NONE if an effect chain corresponding to the track session | 
|  | 986 | // was found on another thread and must be moved on this thread | 
|  | 987 | if (effectThreadId != AUDIO_IO_HANDLE_NONE) { | 
|  | 988 | AudioSystem::moveEffectsToIo(effectIds, effectThreadId); | 
|  | 989 | } | 
|  | 990 |  | 
| Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 991 | // return handle to client | 
|  | 992 | trackHandle = new TrackHandle(track); | 
|  | 993 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 994 | Exit: | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 995 | if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 996 | AudioSystem::releaseOutput(portId); | 
| Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 997 | } | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 998 | *status = lStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 999 | return trackHandle; | 
|  | 1000 | } | 
|  | 1001 |  | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1002 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1003 | { | 
|  | 1004 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1005 | ThreadBase *thread = checkThread_l(ioHandle); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1006 | if (thread == NULL) { | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1007 | ALOGW("sampleRate() unknown thread %d", ioHandle); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1008 | return 0; | 
|  | 1009 | } | 
|  | 1010 | return thread->sampleRate(); | 
|  | 1011 | } | 
|  | 1012 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1013 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1014 | { | 
|  | 1015 | Mutex::Autolock _l(mLock); | 
|  | 1016 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 1017 | if (thread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1018 | ALOGW("format() unknown thread %d", output); | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1019 | return AUDIO_FORMAT_INVALID; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1020 | } | 
|  | 1021 | return thread->format(); | 
|  | 1022 | } | 
|  | 1023 |  | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1024 | size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1025 | { | 
|  | 1026 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1027 | ThreadBase *thread = checkThread_l(ioHandle); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1028 | if (thread == NULL) { | 
| Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1029 | ALOGW("frameCount() unknown thread %d", ioHandle); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1030 | return 0; | 
|  | 1031 | } | 
| Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1032 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; | 
|  | 1033 | //       should examine all callers and fix them to handle smaller counts | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1034 | return thread->frameCount(); | 
|  | 1035 | } | 
|  | 1036 |  | 
| Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 1037 | size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const | 
|  | 1038 | { | 
|  | 1039 | Mutex::Autolock _l(mLock); | 
|  | 1040 | ThreadBase *thread = checkThread_l(ioHandle); | 
|  | 1041 | if (thread == NULL) { | 
|  | 1042 | ALOGW("frameCountHAL() unknown thread %d", ioHandle); | 
|  | 1043 | return 0; | 
|  | 1044 | } | 
|  | 1045 | return thread->frameCountHAL(); | 
|  | 1046 | } | 
|  | 1047 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1048 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1049 | { | 
|  | 1050 | Mutex::Autolock _l(mLock); | 
|  | 1051 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 1052 | if (thread == NULL) { | 
| Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 1053 | ALOGW("latency(): no playback thread found for output handle %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1054 | return 0; | 
|  | 1055 | } | 
|  | 1056 | return thread->latency(); | 
|  | 1057 | } | 
|  | 1058 |  | 
|  | 1059 | status_t AudioFlinger::setMasterVolume(float value) | 
|  | 1060 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1061 | status_t ret = initCheck(); | 
|  | 1062 | if (ret != NO_ERROR) { | 
|  | 1063 | return ret; | 
|  | 1064 | } | 
|  | 1065 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1066 | // check calling permissions | 
|  | 1067 | if (!settingsAllowed()) { | 
|  | 1068 | return PERMISSION_DENIED; | 
|  | 1069 | } | 
|  | 1070 |  | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1071 | Mutex::Autolock _l(mLock); | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1072 | mMasterVolume = value; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1073 |  | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1074 | // Set master volume in the HALs which support it. | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1075 | { | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1076 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1077 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 1078 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1079 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1080 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
|  | 1081 | if (dev->canSetMasterVolume()) { | 
|  | 1082 | dev->hwDevice()->setMasterVolume(value); | 
|  | 1083 | } | 
|  | 1084 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 1085 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1086 | } | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1087 | // Now set the master volume in each playback thread.  Playback threads | 
|  | 1088 | // assigned to HALs which do not have master volume support will apply | 
|  | 1089 | // master volume during the mix operation.  Threads with HALs which do | 
|  | 1090 | // support master volume will simply ignore the setting. | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1091 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 1092 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { | 
|  | 1093 | continue; | 
|  | 1094 | } | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1095 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1096 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1097 |  | 
|  | 1098 | return NO_ERROR; | 
|  | 1099 | } | 
|  | 1100 |  | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1101 | status_t AudioFlinger::setMasterBalance(float balance) | 
|  | 1102 | { | 
|  | 1103 | status_t ret = initCheck(); | 
|  | 1104 | if (ret != NO_ERROR) { | 
|  | 1105 | return ret; | 
|  | 1106 | } | 
|  | 1107 |  | 
|  | 1108 | // check calling permissions | 
|  | 1109 | if (!settingsAllowed()) { | 
|  | 1110 | return PERMISSION_DENIED; | 
|  | 1111 | } | 
|  | 1112 |  | 
|  | 1113 | // check range | 
|  | 1114 | if (isnan(balance) || fabs(balance) > 1.f) { | 
|  | 1115 | return BAD_VALUE; | 
|  | 1116 | } | 
|  | 1117 |  | 
|  | 1118 | Mutex::Autolock _l(mLock); | 
|  | 1119 |  | 
|  | 1120 | // short cut. | 
|  | 1121 | if (mMasterBalance == balance) return NO_ERROR; | 
|  | 1122 |  | 
|  | 1123 | mMasterBalance = balance; | 
|  | 1124 |  | 
|  | 1125 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 1126 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { | 
|  | 1127 | continue; | 
|  | 1128 | } | 
|  | 1129 | mPlaybackThreads.valueAt(i)->setMasterBalance(balance); | 
|  | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | return NO_ERROR; | 
|  | 1133 | } | 
|  | 1134 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 1135 | status_t AudioFlinger::setMode(audio_mode_t mode) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1136 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1137 | status_t ret = initCheck(); | 
|  | 1138 | if (ret != NO_ERROR) { | 
|  | 1139 | return ret; | 
|  | 1140 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1141 |  | 
|  | 1142 | // check calling permissions | 
|  | 1143 | if (!settingsAllowed()) { | 
|  | 1144 | return PERMISSION_DENIED; | 
|  | 1145 | } | 
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 1146 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1147 | ALOGW("Illegal value: setMode(%d)", mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1148 | return BAD_VALUE; | 
|  | 1149 | } | 
|  | 1150 |  | 
|  | 1151 | { // scope for the lock | 
|  | 1152 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1153 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 1154 | return INVALID_OPERATION; | 
|  | 1155 | } | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1156 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1157 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1158 | ret = dev->setMode(mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1159 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 1160 | } | 
|  | 1161 |  | 
|  | 1162 | if (NO_ERROR == ret) { | 
|  | 1163 | Mutex::Autolock _l(mLock); | 
|  | 1164 | mMode = mode; | 
| Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1165 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1166 | mPlaybackThreads.valueAt(i)->setMode(mode); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | return ret; | 
|  | 1170 | } | 
|  | 1171 |  | 
|  | 1172 | status_t AudioFlinger::setMicMute(bool state) | 
|  | 1173 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1174 | status_t ret = initCheck(); | 
|  | 1175 | if (ret != NO_ERROR) { | 
|  | 1176 | return ret; | 
|  | 1177 | } | 
|  | 1178 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1179 | // check calling permissions | 
|  | 1180 | if (!settingsAllowed()) { | 
|  | 1181 | return PERMISSION_DENIED; | 
|  | 1182 | } | 
|  | 1183 |  | 
|  | 1184 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1185 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 1186 | return INVALID_OPERATION; | 
|  | 1187 | } | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1188 | sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice(); | 
|  | 1189 | if (primaryDev == nullptr) { | 
|  | 1190 | ALOGW("%s: no primary HAL device", __func__); | 
|  | 1191 | return INVALID_OPERATION; | 
|  | 1192 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1193 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1194 | ret = primaryDev->setMicMute(state); | 
| Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 1195 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1196 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1197 | if (dev != primaryDev) { | 
|  | 1198 | (void)dev->setMicMute(state); | 
| Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 1199 | } | 
|  | 1200 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1201 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1202 | ALOGW_IF(ret != NO_ERROR, "%s: error %d setting state to HAL", __func__, ret); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1203 | return ret; | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | bool AudioFlinger::getMicMute() const | 
|  | 1207 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1208 | status_t ret = initCheck(); | 
|  | 1209 | if (ret != NO_ERROR) { | 
|  | 1210 | return false; | 
|  | 1211 | } | 
| Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1212 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1213 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 1214 | return false; | 
|  | 1215 | } | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1216 | sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice(); | 
|  | 1217 | if (primaryDev == nullptr) { | 
|  | 1218 | ALOGW("%s: no primary HAL device", __func__); | 
|  | 1219 | return false; | 
| Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 1220 | } | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1221 | bool state; | 
|  | 1222 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; | 
|  | 1223 | ret = primaryDev->getMicMute(&state); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1224 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | a135dd8 | 2020-05-14 09:07:39 -0700 | [diff] [blame] | 1225 | ALOGE_IF(ret != NO_ERROR, "%s: error %d getting state from HAL", __func__, ret); | 
|  | 1226 | return (ret == NO_ERROR) && state; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1227 | } | 
|  | 1228 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1229 | void AudioFlinger::setRecordSilenced(uid_t uid, bool silenced) | 
|  | 1230 | { | 
|  | 1231 | ALOGV("AudioFlinger::setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); | 
|  | 1232 |  | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1233 | AutoMutex lock(mLock); | 
|  | 1234 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1235 | mRecordThreads[i]->setRecordSilenced(uid, silenced); | 
|  | 1236 | } | 
|  | 1237 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 1238 | mMmapThreads[i]->setRecordSilenced(uid, silenced); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1239 | } | 
|  | 1240 | } | 
|  | 1241 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1242 | status_t AudioFlinger::setMasterMute(bool muted) | 
|  | 1243 | { | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1244 | status_t ret = initCheck(); | 
|  | 1245 | if (ret != NO_ERROR) { | 
|  | 1246 | return ret; | 
|  | 1247 | } | 
|  | 1248 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1249 | // check calling permissions | 
|  | 1250 | if (!settingsAllowed()) { | 
|  | 1251 | return PERMISSION_DENIED; | 
|  | 1252 | } | 
|  | 1253 |  | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1254 | Mutex::Autolock _l(mLock); | 
|  | 1255 | mMasterMute = muted; | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1256 |  | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1257 | // Set master mute in the HALs which support it. | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1258 | { | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1259 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1260 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 1261 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1262 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1263 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; | 
|  | 1264 | if (dev->canSetMasterMute()) { | 
|  | 1265 | dev->hwDevice()->setMasterMute(muted); | 
|  | 1266 | } | 
|  | 1267 | mHardwareStatus = AUDIO_HW_IDLE; | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1268 | } | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1269 | } | 
|  | 1270 |  | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1271 | // Now set the master mute in each playback thread.  Playback threads | 
| Glenn Kasten | a2f59b3 | 2020-08-03 16:37:24 -0700 | [diff] [blame] | 1272 | // assigned to HALs which do not have master mute support will apply master mute | 
|  | 1273 | // during the mix operation.  Threads with HALs which do support master mute | 
|  | 1274 | // will simply ignore the setting. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1275 | Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l(); | 
|  | 1276 | for (size_t i = 0; i < volumeInterfaces.size(); i++) { | 
|  | 1277 | volumeInterfaces[i]->setMasterMute(muted); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1278 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1279 |  | 
|  | 1280 | return NO_ERROR; | 
|  | 1281 | } | 
|  | 1282 |  | 
|  | 1283 | float AudioFlinger::masterVolume() const | 
|  | 1284 | { | 
| Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1285 | Mutex::Autolock _l(mLock); | 
|  | 1286 | return masterVolume_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1287 | } | 
|  | 1288 |  | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1289 | status_t AudioFlinger::getMasterBalance(float *balance) const | 
|  | 1290 | { | 
|  | 1291 | Mutex::Autolock _l(mLock); | 
|  | 1292 | *balance = getMasterBalance_l(); | 
|  | 1293 | return NO_ERROR; // if called through binder, may return a transactional error | 
|  | 1294 | } | 
|  | 1295 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1296 | bool AudioFlinger::masterMute() const | 
|  | 1297 | { | 
| Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1298 | Mutex::Autolock _l(mLock); | 
|  | 1299 | return masterMute_l(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1300 | } | 
|  | 1301 |  | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1302 | float AudioFlinger::masterVolume_l() const | 
|  | 1303 | { | 
| John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1304 | return mMasterVolume; | 
|  | 1305 | } | 
|  | 1306 |  | 
| Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1307 | float AudioFlinger::getMasterBalance_l() const | 
|  | 1308 | { | 
|  | 1309 | return mMasterBalance; | 
|  | 1310 | } | 
|  | 1311 |  | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1312 | bool AudioFlinger::masterMute_l() const | 
|  | 1313 | { | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1314 | return mMasterMute; | 
| John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1315 | } | 
|  | 1316 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1317 | status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const | 
|  | 1318 | { | 
|  | 1319 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1320 | ALOGW("checkStreamType() invalid stream %d", stream); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1321 | return BAD_VALUE; | 
|  | 1322 | } | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1323 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); | 
|  | 1324 | if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && !isAudioServerUid(callerUid)) { | 
|  | 1325 | ALOGW("checkStreamType() uid %d cannot use internal stream type %d", callerUid, stream); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1326 | return PERMISSION_DENIED; | 
|  | 1327 | } | 
|  | 1328 |  | 
|  | 1329 | return NO_ERROR; | 
|  | 1330 | } | 
|  | 1331 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1332 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, | 
|  | 1333 | audio_io_handle_t output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1334 | { | 
|  | 1335 | // check calling permissions | 
|  | 1336 | if (!settingsAllowed()) { | 
|  | 1337 | return PERMISSION_DENIED; | 
|  | 1338 | } | 
|  | 1339 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1340 | status_t status = checkStreamType(stream); | 
|  | 1341 | if (status != NO_ERROR) { | 
|  | 1342 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1343 | } | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1344 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 1345 | return BAD_VALUE; | 
|  | 1346 | } | 
| François Gaffie | 9e1533c | 2019-04-11 16:07:36 +0200 | [diff] [blame] | 1347 | LOG_ALWAYS_FATAL_IF(stream == AUDIO_STREAM_PATCH && value != 1.0f, | 
|  | 1348 | "AUDIO_STREAM_PATCH must have full scale volume"); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1349 |  | 
|  | 1350 | AutoMutex lock(mLock); | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1351 | VolumeInterface *volumeInterface = getVolumeInterface_l(output); | 
|  | 1352 | if (volumeInterface == NULL) { | 
|  | 1353 | return BAD_VALUE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1354 | } | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1355 | volumeInterface->setStreamVolume(stream, value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1356 |  | 
|  | 1357 | return NO_ERROR; | 
|  | 1358 | } | 
|  | 1359 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1360 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1361 | { | 
|  | 1362 | // check calling permissions | 
|  | 1363 | if (!settingsAllowed()) { | 
|  | 1364 | return PERMISSION_DENIED; | 
|  | 1365 | } | 
|  | 1366 |  | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1367 | status_t status = checkStreamType(stream); | 
|  | 1368 | if (status != NO_ERROR) { | 
|  | 1369 | return status; | 
|  | 1370 | } | 
|  | 1371 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH"); | 
|  | 1372 |  | 
|  | 1373 | if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1374 | ALOGE("setStreamMute() invalid stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1375 | return BAD_VALUE; | 
|  | 1376 | } | 
|  | 1377 |  | 
| Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 1378 | AutoMutex lock(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1379 | mStreamTypes[stream].mute = muted; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1380 | Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l(); | 
|  | 1381 | for (size_t i = 0; i < volumeInterfaces.size(); i++) { | 
|  | 1382 | volumeInterfaces[i]->setStreamMute(stream, muted); | 
|  | 1383 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1384 |  | 
|  | 1385 | return NO_ERROR; | 
|  | 1386 | } | 
|  | 1387 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1388 | float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1389 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1390 | status_t status = checkStreamType(stream); | 
|  | 1391 | if (status != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1392 | return 0.0f; | 
|  | 1393 | } | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1394 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 1395 | return 0.0f; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1396 | } | 
|  | 1397 |  | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1398 | AutoMutex lock(mLock); | 
|  | 1399 | VolumeInterface *volumeInterface = getVolumeInterface_l(output); | 
|  | 1400 | if (volumeInterface == NULL) { | 
|  | 1401 | return 0.0f; | 
|  | 1402 | } | 
|  | 1403 |  | 
|  | 1404 | return volumeInterface->streamVolume(stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1405 | } | 
|  | 1406 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1407 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1408 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1409 | status_t status = checkStreamType(stream); | 
|  | 1410 | if (status != NO_ERROR) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1411 | return true; | 
|  | 1412 | } | 
|  | 1413 |  | 
| Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1414 | AutoMutex lock(mLock); | 
|  | 1415 | return streamMute_l(stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1416 | } | 
|  | 1417 |  | 
| Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1418 |  | 
|  | 1419 | void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs) | 
|  | 1420 | { | 
|  | 1421 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 1422 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); | 
|  | 1423 | } | 
|  | 1424 | } | 
|  | 1425 |  | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1426 | void AudioFlinger::updateOutDevicesForRecordThreads_l(const DeviceDescriptorBaseVector& devices) | 
|  | 1427 | { | 
|  | 1428 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 1429 | mRecordThreads.valueAt(i)->updateOutDevices(devices); | 
|  | 1430 | } | 
|  | 1431 | } | 
|  | 1432 |  | 
| Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 1433 | // forwardAudioHwSyncToDownstreamPatches_l() must be called with AudioFlinger::mLock held | 
|  | 1434 | void AudioFlinger::forwardParametersToDownstreamPatches_l( | 
|  | 1435 | audio_io_handle_t upStream, const String8& keyValuePairs, | 
|  | 1436 | std::function<bool(const sp<PlaybackThread>&)> useThread) | 
|  | 1437 | { | 
|  | 1438 | std::vector<PatchPanel::SoftwarePatch> swPatches; | 
|  | 1439 | if (mPatchPanel.getDownstreamSoftwarePatches(upStream, &swPatches) != OK) return; | 
|  | 1440 | ALOGV_IF(!swPatches.empty(), "%s found %zu downstream patches for stream ID %d", | 
|  | 1441 | __func__, swPatches.size(), upStream); | 
|  | 1442 | for (const auto& swPatch : swPatches) { | 
|  | 1443 | sp<PlaybackThread> downStream = checkPlaybackThread_l(swPatch.getPlaybackThreadHandle()); | 
|  | 1444 | if (downStream != NULL && (useThread == nullptr || useThread(downStream))) { | 
|  | 1445 | downStream->setParameters(keyValuePairs); | 
|  | 1446 | } | 
|  | 1447 | } | 
|  | 1448 | } | 
|  | 1449 |  | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1450 | // Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon. | 
|  | 1451 | // Some keys are used for audio routing and audio path configuration and should be reserved for use | 
|  | 1452 | // by audio policy and audio flinger for functional, privacy and security reasons. | 
|  | 1453 | void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid) | 
|  | 1454 | { | 
|  | 1455 | static const String8 kReservedParameters[] = { | 
|  | 1456 | String8(AudioParameter::keyRouting), | 
|  | 1457 | String8(AudioParameter::keySamplingRate), | 
|  | 1458 | String8(AudioParameter::keyFormat), | 
|  | 1459 | String8(AudioParameter::keyChannels), | 
|  | 1460 | String8(AudioParameter::keyFrameCount), | 
|  | 1461 | String8(AudioParameter::keyInputSource), | 
|  | 1462 | String8(AudioParameter::keyMonoOutput), | 
| Mikhail Naganov | bb3b160 | 2019-07-08 15:28:43 -0700 | [diff] [blame] | 1463 | String8(AudioParameter::keyDeviceConnect), | 
|  | 1464 | String8(AudioParameter::keyDeviceDisconnect), | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1465 | String8(AudioParameter::keyStreamSupportedFormats), | 
|  | 1466 | String8(AudioParameter::keyStreamSupportedChannels), | 
|  | 1467 | String8(AudioParameter::keyStreamSupportedSamplingRates), | 
|  | 1468 | }; | 
|  | 1469 |  | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1470 | if (isAudioServerUid(callingUid)) { | 
|  | 1471 | return; // no need to filter if audioserver. | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1472 | } | 
|  | 1473 |  | 
|  | 1474 | AudioParameter param = AudioParameter(keyValuePairs); | 
|  | 1475 | String8 value; | 
| Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1476 | AudioParameter rejectedParam; | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1477 | for (auto& key : kReservedParameters) { | 
|  | 1478 | if (param.get(key, value) == NO_ERROR) { | 
| Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1479 | rejectedParam.add(key, value); | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1480 | param.remove(key); | 
|  | 1481 | } | 
|  | 1482 | } | 
| Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1483 | logFilteredParameters(param.size() + rejectedParam.size(), keyValuePairs, | 
|  | 1484 | rejectedParam.size(), rejectedParam.toString(), callingUid); | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1485 | keyValuePairs = param.toString(); | 
|  | 1486 | } | 
|  | 1487 |  | 
| Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1488 | void AudioFlinger::logFilteredParameters(size_t originalKVPSize, const String8& originalKVPs, | 
|  | 1489 | size_t rejectedKVPSize, const String8& rejectedKVPs, | 
|  | 1490 | uid_t callingUid) { | 
|  | 1491 | auto prefix = String8::format("UID %5d", callingUid); | 
|  | 1492 | auto suffix = String8::format("%zu KVP received: %s", originalKVPSize, originalKVPs.c_str()); | 
|  | 1493 | if (rejectedKVPSize != 0) { | 
|  | 1494 | auto error = String8::format("%zu KVP rejected: %s", rejectedKVPSize, rejectedKVPs.c_str()); | 
|  | 1495 | ALOGW("%s: %s, %s, %s", __func__, prefix.c_str(), error.c_str(), suffix.c_str()); | 
|  | 1496 | mRejectedSetParameterLog.log("%s, %s, %s", prefix.c_str(), error.c_str(), suffix.c_str()); | 
|  | 1497 | } else { | 
|  | 1498 | auto& logger = (isServiceUid(callingUid) ? mSystemSetParameterLog : mAppSetParameterLog); | 
|  | 1499 | logger.log("%s, %s", prefix.c_str(), suffix.c_str()); | 
|  | 1500 | } | 
|  | 1501 | } | 
|  | 1502 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1503 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1504 | { | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1505 | ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d", | 
|  | 1506 | ioHandle, keyValuePairs.string(), | 
|  | 1507 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1508 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1509 | // check calling permissions | 
|  | 1510 | if (!settingsAllowed()) { | 
|  | 1511 | return PERMISSION_DENIED; | 
|  | 1512 | } | 
|  | 1513 |  | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1514 | String8 filteredKeyValuePairs = keyValuePairs; | 
|  | 1515 | filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid()); | 
|  | 1516 |  | 
|  | 1517 | ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string()); | 
|  | 1518 |  | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1519 | // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface | 
|  | 1520 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1521 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1522 | // result will remain NO_INIT if no audio device is present | 
|  | 1523 | status_t final_result = NO_INIT; | 
| Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1524 | { | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1525 | AutoMutex lock(mHardwareLock); | 
|  | 1526 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; | 
|  | 1527 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1528 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1529 | status_t result = dev->setParameters(filteredKeyValuePairs); | 
| Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1530 | // return success if at least one audio device accepts the parameters as not all | 
|  | 1531 | // HALs are requested to support all parameters. If no audio device supports the | 
|  | 1532 | // requested parameters, the last error is reported. | 
|  | 1533 | if (final_result != NO_ERROR) { | 
|  | 1534 | final_result = result; | 
|  | 1535 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1536 | } | 
|  | 1537 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1538 | } | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1539 | // disable AEC and NS if the device is a BT SCO headset supporting those pre processings | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1540 | AudioParameter param = AudioParameter(filteredKeyValuePairs); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1541 | String8 value; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1542 | if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) { | 
|  | 1543 | bool btNrecIsOff = (value == AudioParameter::valueOff); | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1544 | if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) { | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1545 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1546 | mRecordThreads.valueAt(i)->checkBtNrec(); | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1547 | } | 
| Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1548 | } | 
|  | 1549 | } | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1550 | String8 screenState; | 
|  | 1551 | if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1552 | bool isOff = (screenState == AudioParameter::valueOff); | 
| Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1553 | if (isOff != (AudioFlinger::mScreenState & 1)) { | 
|  | 1554 | AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1555 | } | 
|  | 1556 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1557 | return final_result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1558 | } | 
|  | 1559 |  | 
|  | 1560 | // hold a strong ref on thread in case closeOutput() or closeInput() is called | 
|  | 1561 | // and the thread is exited once the lock is released | 
|  | 1562 | sp<ThreadBase> thread; | 
|  | 1563 | { | 
|  | 1564 | Mutex::Autolock _l(mLock); | 
|  | 1565 | thread = checkPlaybackThread_l(ioHandle); | 
| Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 1566 | if (thread == 0) { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1567 | thread = checkRecordThread_l(ioHandle); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1568 | if (thread == 0) { | 
|  | 1569 | thread = checkMmapThread_l(ioHandle); | 
|  | 1570 | } | 
| Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 1571 | } else if (thread == primaryPlaybackThread_l()) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1572 | // indicate output device change to all input threads for pre processing | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1573 | AudioParameter param = AudioParameter(filteredKeyValuePairs); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1574 | int value; | 
| Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 1575 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && | 
|  | 1576 | (value != 0)) { | 
| Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1577 | broacastParametersToRecordThreads_l(filteredKeyValuePairs); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1578 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1579 | } | 
|  | 1580 | } | 
| Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1581 | if (thread != 0) { | 
| Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 1582 | status_t result = thread->setParameters(filteredKeyValuePairs); | 
|  | 1583 | forwardParametersToDownstreamPatches_l(thread->id(), filteredKeyValuePairs); | 
|  | 1584 | return result; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1585 | } | 
|  | 1586 | return BAD_VALUE; | 
|  | 1587 | } | 
|  | 1588 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1589 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1590 | { | 
| Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1591 | ALOGVV("getParameters() io %d, keys %s, calling pid %d", | 
|  | 1592 | ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1593 |  | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1594 | Mutex::Autolock _l(mLock); | 
|  | 1595 |  | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1596 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1597 | String8 out_s8; | 
|  | 1598 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1599 | AutoMutex lock(mHardwareLock); | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1600 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1601 | String8 s; | 
| Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1602 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1603 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1604 | status_t result = dev->getParameters(keys, &s); | 
| Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1605 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1606 | if (result == OK) out_s8 += s; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1607 | } | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1608 | return out_s8; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1609 | } | 
|  | 1610 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1611 | ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle); | 
|  | 1612 | if (thread == NULL) { | 
|  | 1613 | thread = (ThreadBase *)checkRecordThread_l(ioHandle); | 
|  | 1614 | if (thread == NULL) { | 
|  | 1615 | thread = (ThreadBase *)checkMmapThread_l(ioHandle); | 
|  | 1616 | if (thread == NULL) { | 
| Mikhail Naganov | aa165e5 | 2017-07-12 10:39:16 -0700 | [diff] [blame] | 1617 | return String8(""); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1618 | } | 
|  | 1619 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1620 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1621 | return thread->getParameters(keys); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1622 | } | 
|  | 1623 |  | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 1624 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, | 
|  | 1625 | audio_channel_mask_t channelMask) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1626 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1627 | status_t ret = initCheck(); | 
|  | 1628 | if (ret != NO_ERROR) { | 
|  | 1629 | return 0; | 
|  | 1630 | } | 
| Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1631 | if ((sampleRate == 0) || | 
| Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1632 | !audio_is_valid_format(format) || !audio_has_proportional_frames(format) || | 
| Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1633 | !audio_is_input_channel(channelMask)) { | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1634 | return 0; | 
|  | 1635 | } | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1636 |  | 
| Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1637 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1638 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 1639 | return 0; | 
|  | 1640 | } | 
| Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1641 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1642 | audio_config_t config, proposed; | 
|  | 1643 | memset(&proposed, 0, sizeof(proposed)); | 
|  | 1644 | proposed.sample_rate = sampleRate; | 
|  | 1645 | proposed.channel_mask = channelMask; | 
|  | 1646 | proposed.format = format; | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1647 |  | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1648 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); | 
| Tao Wu | 1eb82df | 2019-11-05 10:20:18 -0800 | [diff] [blame] | 1649 | size_t frames = 0; | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1650 | for (;;) { | 
|  | 1651 | // Note: config is currently a const parameter for get_input_buffer_size() | 
|  | 1652 | // but we use a copy from proposed in case config changes from the call. | 
|  | 1653 | config = proposed; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1654 | status_t result = dev->getInputBufferSize(&config, &frames); | 
|  | 1655 | if (result == OK && frames != 0) { | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1656 | break; // hal success, config is the result | 
|  | 1657 | } | 
|  | 1658 | // change one parameter of the configuration each iteration to a more "common" value | 
|  | 1659 | // to see if the device will support it. | 
|  | 1660 | if (proposed.format != AUDIO_FORMAT_PCM_16_BIT) { | 
|  | 1661 | proposed.format = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 1662 | } else if (proposed.sample_rate != 44100) { // 44.1 is claimed as must in CDD as well as | 
|  | 1663 | proposed.sample_rate = 44100;           // legacy AudioRecord.java. TODO: Query hw? | 
|  | 1664 | } else { | 
|  | 1665 | ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, " | 
|  | 1666 | "format %#x, channelMask 0x%X", | 
|  | 1667 | sampleRate, format, channelMask); | 
|  | 1668 | break; // retries failed, break out of loop with frames == 0. | 
|  | 1669 | } | 
|  | 1670 | } | 
| Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1671 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1672 | if (frames > 0 && config.sample_rate != sampleRate) { | 
|  | 1673 | frames = destinationFramesPossible(frames, sampleRate, config.sample_rate); | 
|  | 1674 | } | 
|  | 1675 | return frames; // may be converted to bytes at the Java level. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1676 | } | 
|  | 1677 |  | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 1678 | uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1679 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1680 | Mutex::Autolock _l(mLock); | 
|  | 1681 |  | 
|  | 1682 | RecordThread *recordThread = checkRecordThread_l(ioHandle); | 
|  | 1683 | if (recordThread != NULL) { | 
|  | 1684 | return recordThread->getInputFramesLost(); | 
|  | 1685 | } | 
|  | 1686 | return 0; | 
|  | 1687 | } | 
|  | 1688 |  | 
|  | 1689 | status_t AudioFlinger::setVoiceVolume(float value) | 
|  | 1690 | { | 
| Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1691 | status_t ret = initCheck(); | 
|  | 1692 | if (ret != NO_ERROR) { | 
|  | 1693 | return ret; | 
|  | 1694 | } | 
|  | 1695 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1696 | // check calling permissions | 
|  | 1697 | if (!settingsAllowed()) { | 
|  | 1698 | return PERMISSION_DENIED; | 
|  | 1699 | } | 
|  | 1700 |  | 
|  | 1701 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 1702 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 1703 | return INVALID_OPERATION; | 
|  | 1704 | } | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1705 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); | 
| Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1706 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1707 | ret = dev->setVoiceVolume(value); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1708 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 1709 |  | 
|  | 1710 | return ret; | 
|  | 1711 | } | 
|  | 1712 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1713 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1714 | audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1715 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1716 | Mutex::Autolock _l(mLock); | 
|  | 1717 |  | 
|  | 1718 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); | 
|  | 1719 | if (playbackThread != NULL) { | 
|  | 1720 | return playbackThread->getRenderPosition(halFrames, dspFrames); | 
|  | 1721 | } | 
|  | 1722 |  | 
|  | 1723 | return BAD_VALUE; | 
|  | 1724 | } | 
|  | 1725 |  | 
|  | 1726 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) | 
|  | 1727 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1728 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1729 | if (client == 0) { | 
|  | 1730 | return; | 
|  | 1731 | } | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1732 | pid_t pid = IPCThreadState::self()->getCallingPid(); | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1733 | { | 
|  | 1734 | Mutex::Autolock _cl(mClientLock); | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1735 | if (mNotificationClients.indexOfKey(pid) < 0) { | 
|  | 1736 | sp<NotificationClient> notificationClient = new NotificationClient(this, | 
|  | 1737 | client, | 
|  | 1738 | pid); | 
|  | 1739 | ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1740 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1741 | mNotificationClients.add(pid, notificationClient); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1742 |  | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1743 | sp<IBinder> binder = IInterface::asBinder(client); | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1744 | binder->linkToDeath(notificationClient); | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1745 | } | 
|  | 1746 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1747 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1748 | // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the | 
| Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1749 | // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock. | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1750 | // the config change is always sent from playback or record threads to avoid deadlock | 
|  | 1751 | // with AudioSystem::gLock | 
|  | 1752 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1753 | mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1754 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1755 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1756 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1757 | mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1758 | } | 
|  | 1759 | } | 
|  | 1760 |  | 
|  | 1761 | void AudioFlinger::removeNotificationClient(pid_t pid) | 
|  | 1762 | { | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1763 | std::vector< sp<AudioFlinger::EffectModule> > removedEffects; | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1764 | { | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1765 | Mutex::Autolock _l(mLock); | 
|  | 1766 | { | 
|  | 1767 | Mutex::Autolock _cl(mClientLock); | 
|  | 1768 | mNotificationClients.removeItem(pid); | 
|  | 1769 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1770 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1771 | ALOGV("%d died, releasing its sessions", pid); | 
|  | 1772 | size_t num = mAudioSessionRefs.size(); | 
|  | 1773 | bool removed = false; | 
|  | 1774 | for (size_t i = 0; i < num; ) { | 
|  | 1775 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
|  | 1776 | ALOGV(" pid %d @ %zu", ref->mPid, i); | 
|  | 1777 | if (ref->mPid == pid) { | 
|  | 1778 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); | 
|  | 1779 | mAudioSessionRefs.removeAt(i); | 
|  | 1780 | delete ref; | 
|  | 1781 | removed = true; | 
|  | 1782 | num--; | 
|  | 1783 | } else { | 
|  | 1784 | i++; | 
|  | 1785 | } | 
|  | 1786 | } | 
|  | 1787 | if (removed) { | 
|  | 1788 | removedEffects = purgeStaleEffects_l(); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1789 | } | 
|  | 1790 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1791 | for (auto& effect : removedEffects) { | 
|  | 1792 | effect->updatePolicyState(); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1793 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1794 | } | 
|  | 1795 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1796 | void AudioFlinger::ioConfigChanged(audio_io_config_event event, | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1797 | const sp<AudioIoDescriptor>& ioDesc, | 
|  | 1798 | pid_t pid) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1799 | { | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1800 | Mutex::Autolock _l(mClientLock); | 
|  | 1801 | size_t size = mNotificationClients.size(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1802 | for (size_t i = 0; i < size; i++) { | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1803 | if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) { | 
|  | 1804 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc); | 
|  | 1805 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1806 | } | 
|  | 1807 | } | 
|  | 1808 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1809 | // removeClient_l() must be called with AudioFlinger::mClientLock held | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1810 | void AudioFlinger::removeClient_l(pid_t pid) | 
|  | 1811 | { | 
| Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1812 | ALOGV("removeClient_l() pid %d, calling pid %d", pid, | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1813 | IPCThreadState::self()->getCallingPid()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1814 | mClients.removeItem(pid); | 
|  | 1815 | } | 
|  | 1816 |  | 
| Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1817 | // getEffectThread_l() must be called with AudioFlinger::mLock held | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1818 | sp<AudioFlinger::ThreadBase> AudioFlinger::getEffectThread_l(audio_session_t sessionId, | 
|  | 1819 | int effectId) | 
| Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1820 | { | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1821 | sp<ThreadBase> thread; | 
| Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1822 |  | 
|  | 1823 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1824 | if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { | 
| Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1825 | ALOG_ASSERT(thread == 0); | 
|  | 1826 | thread = mPlaybackThreads.valueAt(i); | 
|  | 1827 | } | 
|  | 1828 | } | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1829 | if (thread != nullptr) { | 
|  | 1830 | return thread; | 
|  | 1831 | } | 
|  | 1832 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 1833 | if (mRecordThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { | 
|  | 1834 | ALOG_ASSERT(thread == 0); | 
|  | 1835 | thread = mRecordThreads.valueAt(i); | 
|  | 1836 | } | 
|  | 1837 | } | 
|  | 1838 | if (thread != nullptr) { | 
|  | 1839 | return thread; | 
|  | 1840 | } | 
|  | 1841 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 1842 | if (mMmapThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { | 
|  | 1843 | ALOG_ASSERT(thread == 0); | 
|  | 1844 | thread = mMmapThreads.valueAt(i); | 
|  | 1845 | } | 
|  | 1846 | } | 
| Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1847 | return thread; | 
|  | 1848 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1849 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1850 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1851 |  | 
|  | 1852 | // ---------------------------------------------------------------------------- | 
|  | 1853 |  | 
|  | 1854 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) | 
|  | 1855 | :   RefBase(), | 
|  | 1856 | mAudioFlinger(audioFlinger), | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1857 | mPid(pid) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1858 | { | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1859 | mMemoryDealer = new MemoryDealer( | 
|  | 1860 | audioFlinger->getClientSharedHeapSize(), | 
|  | 1861 | (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1862 | } | 
|  | 1863 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1864 | // Client destructor must be called with AudioFlinger::mClientLock held | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1865 | AudioFlinger::Client::~Client() | 
|  | 1866 | { | 
|  | 1867 | mAudioFlinger->removeClient_l(mPid); | 
|  | 1868 | } | 
|  | 1869 |  | 
| Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 1870 | sp<MemoryDealer> AudioFlinger::Client::heap() const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1871 | { | 
|  | 1872 | return mMemoryDealer; | 
|  | 1873 | } | 
|  | 1874 |  | 
|  | 1875 | // ---------------------------------------------------------------------------- | 
|  | 1876 |  | 
|  | 1877 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, | 
|  | 1878 | const sp<IAudioFlingerClient>& client, | 
|  | 1879 | pid_t pid) | 
| Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 1880 | : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1881 | { | 
|  | 1882 | } | 
|  | 1883 |  | 
|  | 1884 | AudioFlinger::NotificationClient::~NotificationClient() | 
|  | 1885 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1886 | } | 
|  | 1887 |  | 
| Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1888 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1889 | { | 
|  | 1890 | sp<NotificationClient> keep(this); | 
| Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 1891 | mAudioFlinger->removeNotificationClient(mPid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1892 | } | 
|  | 1893 |  | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1894 | // ---------------------------------------------------------------------------- | 
|  | 1895 | AudioFlinger::MediaLogNotifier::MediaLogNotifier() | 
|  | 1896 | : mPendingRequests(false) {} | 
|  | 1897 |  | 
|  | 1898 |  | 
|  | 1899 | void AudioFlinger::MediaLogNotifier::requestMerge() { | 
|  | 1900 | AutoMutex _l(mMutex); | 
|  | 1901 | mPendingRequests = true; | 
|  | 1902 | mCond.signal(); | 
|  | 1903 | } | 
|  | 1904 |  | 
|  | 1905 | bool AudioFlinger::MediaLogNotifier::threadLoop() { | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 1906 | // Should already have been checked, but just in case | 
|  | 1907 | if (sMediaLogService == 0) { | 
|  | 1908 | return false; | 
|  | 1909 | } | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1910 | // Wait until there are pending requests | 
|  | 1911 | { | 
|  | 1912 | AutoMutex _l(mMutex); | 
|  | 1913 | mPendingRequests = false; // to ignore past requests | 
|  | 1914 | while (!mPendingRequests) { | 
|  | 1915 | mCond.wait(mMutex); | 
|  | 1916 | // TODO may also need an exitPending check | 
|  | 1917 | } | 
|  | 1918 | mPendingRequests = false; | 
|  | 1919 | } | 
|  | 1920 | // Execute the actual MediaLogService binder call and ignore extra requests for a while | 
| Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 1921 | sMediaLogService->requestMergeWakeup(); | 
| Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1922 | usleep(kPostTriggerSleepPeriod); | 
|  | 1923 | return true; | 
|  | 1924 | } | 
|  | 1925 |  | 
|  | 1926 | void AudioFlinger::requestLogMerge() { | 
|  | 1927 | mMediaLogNotifier->requestMerge(); | 
|  | 1928 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1929 |  | 
|  | 1930 | // ---------------------------------------------------------------------------- | 
|  | 1931 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1932 | sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input, | 
|  | 1933 | CreateRecordOutput& output, | 
|  | 1934 | status_t *status) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1935 | { | 
|  | 1936 | sp<RecordThread::RecordTrack> recordTrack; | 
|  | 1937 | sp<RecordHandle> recordHandle; | 
|  | 1938 | sp<Client> client; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1939 | status_t lStatus; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1940 | audio_session_t sessionId = input.sessionId; | 
| Eric Laurent | 77881cd | 2018-02-09 16:01:31 -0800 | [diff] [blame] | 1941 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1942 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1943 | output.cblk.clear(); | 
|  | 1944 | output.buffers.clear(); | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 1945 | output.inputId = AUDIO_IO_HANDLE_NONE; | 
| Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1946 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1947 | bool updatePid = (input.clientInfo.clientPid == -1); | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1948 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1949 | uid_t clientUid = input.clientInfo.clientUid; | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1950 | if (!isAudioServerOrMediaServerUid(callingUid)) { | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1951 | ALOGW_IF(clientUid != callingUid, | 
|  | 1952 | "%s uid %d tried to pass itself off as %d", | 
|  | 1953 | __FUNCTION__, callingUid, clientUid); | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1954 | clientUid = callingUid; | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1955 | updatePid = true; | 
|  | 1956 | } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1957 | pid_t clientPid = input.clientInfo.clientPid; | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1958 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1959 | if (updatePid) { | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1960 | ALOGW_IF(clientPid != -1 && clientPid != callingPid, | 
| Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1961 | "%s uid %d pid %d tried to pass itself off as pid %d", | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1962 | __func__, callingUid, callingPid, clientPid); | 
|  | 1963 | clientPid = callingPid; | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1964 | } | 
|  | 1965 |  | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1966 | // we don't yet support anything other than linear PCM | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1967 | if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) { | 
|  | 1968 | ALOGE("createRecord() invalid format %#x", input.config.format); | 
| Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 1969 | lStatus = BAD_VALUE; | 
|  | 1970 | goto Exit; | 
|  | 1971 | } | 
|  | 1972 |  | 
| Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1973 | // further channel mask checks are performed by createRecordTrack_l() | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1974 | if (!audio_is_input_channel(input.config.channel_mask)) { | 
|  | 1975 | ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask); | 
| Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 1976 | lStatus = BAD_VALUE; | 
|  | 1977 | goto Exit; | 
|  | 1978 | } | 
|  | 1979 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1980 | if (sessionId == AUDIO_SESSION_ALLOCATE) { | 
|  | 1981 | sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 1982 | } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { | 
|  | 1983 | lStatus = BAD_VALUE; | 
|  | 1984 | goto Exit; | 
|  | 1985 | } | 
|  | 1986 |  | 
|  | 1987 | output.sessionId = sessionId; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1988 | output.selectedDeviceId = input.selectedDeviceId; | 
|  | 1989 | output.flags = input.flags; | 
|  | 1990 |  | 
|  | 1991 | client = registerPid(clientPid); | 
|  | 1992 |  | 
|  | 1993 | // Not a conventional loop, but a retry loop for at most two iterations total. | 
|  | 1994 | // Try first maybe with FAST flag then try again without FAST flag if that fails. | 
|  | 1995 | // Exits loop via break on no error of got exit on error | 
|  | 1996 | // The sp<> references will be dropped when re-entering scope. | 
|  | 1997 | // The lack of indentation is deliberate, to reduce code churn and ease merges. | 
|  | 1998 | for (;;) { | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 1999 | // release previously opened input if retrying. | 
|  | 2000 | if (output.inputId != AUDIO_IO_HANDLE_NONE) { | 
|  | 2001 | recordTrack.clear(); | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2002 | AudioSystem::releaseInput(portId); | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2003 | output.inputId = AUDIO_IO_HANDLE_NONE; | 
| Yung Ti Su | 5fac9c6 | 2018-05-15 15:07:43 +0800 | [diff] [blame] | 2004 | output.selectedDeviceId = input.selectedDeviceId; | 
| Eric Laurent | 77881cd | 2018-02-09 16:01:31 -0800 | [diff] [blame] | 2005 | portId = AUDIO_PORT_HANDLE_NONE; | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2006 | } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2007 | lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId, | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2008 | input.riid, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2009 | sessionId, | 
|  | 2010 | // FIXME compare to AudioTrack | 
|  | 2011 | clientPid, | 
|  | 2012 | clientUid, | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2013 | input.opPackageName, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2014 | &input.config, | 
|  | 2015 | output.flags, &output.selectedDeviceId, &portId); | 
| jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2016 | if (lStatus != NO_ERROR) { | 
|  | 2017 | ALOGE("createRecord() getInputForAttr return error %d", lStatus); | 
|  | 2018 | goto Exit; | 
|  | 2019 | } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2020 |  | 
| Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 2021 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2022 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2023 | RecordThread *thread = checkRecordThread_l(output.inputId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2024 | if (thread == NULL) { | 
| jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2025 | ALOGE("createRecord() checkRecordThread_l failed, input handle %d", output.inputId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2026 | lStatus = BAD_VALUE; | 
|  | 2027 | goto Exit; | 
|  | 2028 | } | 
|  | 2029 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2030 | ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2031 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2032 | output.sampleRate = input.config.sample_rate; | 
|  | 2033 | output.frameCount = input.frameCount; | 
|  | 2034 | output.notificationFrameCount = input.notificationFrameCount; | 
| Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 2035 |  | 
| Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2036 | recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2037 | input.config.format, input.config.channel_mask, | 
|  | 2038 | &output.frameCount, sessionId, | 
|  | 2039 | &output.notificationFrameCount, | 
| Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2040 | callingPid, clientUid, &output.flags, | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2041 | input.clientInfo.clientTid, | 
| Jean-Michel Trivi | b0de569 | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2042 | &lStatus, portId, | 
|  | 2043 | input.opPackageName); | 
| Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2044 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2045 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2046 | // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from | 
|  | 2047 | // audio policy manager without FAST constraint | 
|  | 2048 | if (lStatus == BAD_TYPE) { | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2049 | continue; | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2050 | } | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2051 |  | 
|  | 2052 | if (lStatus != NO_ERROR) { | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2053 | goto Exit; | 
|  | 2054 | } | 
|  | 2055 |  | 
|  | 2056 | // Check if one effect chain was awaiting for an AudioRecord to be created on this | 
|  | 2057 | // session and move it to this thread. | 
|  | 2058 | sp<EffectChain> chain = getOrphanEffectChain_l(sessionId); | 
|  | 2059 | if (chain != 0) { | 
|  | 2060 | Mutex::Autolock _l(thread->mLock); | 
|  | 2061 | thread->addEffectChain_l(chain); | 
|  | 2062 | } | 
|  | 2063 | break; | 
|  | 2064 | } | 
|  | 2065 | // End of retry loop. | 
|  | 2066 | // The lack of indentation is deliberate, to reduce code churn and ease merges. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2067 | } | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2068 |  | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2069 | output.cblk = recordTrack->getCblk(); | 
|  | 2070 | output.buffers = recordTrack->getBuffers(); | 
| Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2071 | output.portId = portId; | 
| Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2072 |  | 
|  | 2073 | // return handle to client | 
|  | 2074 | recordHandle = new RecordHandle(recordTrack); | 
|  | 2075 |  | 
|  | 2076 | Exit: | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2077 | if (lStatus != NO_ERROR) { | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2078 | // remove local strong reference to Client before deleting the RecordTrack so that the | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2079 | // Client destructor is called by the TrackBase destructor with mClientLock held | 
| Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 2080 | // Don't hold mClientLock when releasing the reference on the track as the | 
|  | 2081 | // destructor will acquire it. | 
|  | 2082 | { | 
|  | 2083 | Mutex::Autolock _cl(mClientLock); | 
|  | 2084 | client.clear(); | 
|  | 2085 | } | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2086 | recordTrack.clear(); | 
|  | 2087 | if (output.inputId != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2088 | AudioSystem::releaseInput(portId); | 
| Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2089 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2090 | } | 
|  | 2091 |  | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2092 | *status = lStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2093 | return recordHandle; | 
|  | 2094 | } | 
|  | 2095 |  | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2096 |  | 
|  | 2097 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2098 | // ---------------------------------------------------------------------------- | 
|  | 2099 |  | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2100 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) | 
|  | 2101 | { | 
| Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 2102 | if (name == NULL) { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2103 | return AUDIO_MODULE_HANDLE_NONE; | 
| Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 2104 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2105 | if (!settingsAllowed()) { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2106 | return AUDIO_MODULE_HANDLE_NONE; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2107 | } | 
|  | 2108 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2109 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2110 | return loadHwModule_l(name); | 
|  | 2111 | } | 
|  | 2112 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2113 | // loadHwModule_l() must be called with AudioFlinger::mLock and AudioFlinger::mHardwareLock held | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2114 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) | 
|  | 2115 | { | 
|  | 2116 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 2117 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { | 
|  | 2118 | ALOGW("loadHwModule() module %s already loaded", name); | 
|  | 2119 | return mAudioHwDevs.keyAt(i); | 
|  | 2120 | } | 
|  | 2121 | } | 
|  | 2122 |  | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2123 | sp<DeviceHalInterface> dev; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2124 |  | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2125 | int rc = mDevicesFactoryHal->openDevice(name, &dev); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2126 | if (rc) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2127 | ALOGE("loadHwModule() error %d loading module %s", rc, name); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2128 | return AUDIO_MODULE_HANDLE_NONE; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2129 | } | 
|  | 2130 |  | 
|  | 2131 | mHardwareStatus = AUDIO_HW_INIT; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2132 | rc = dev->initCheck(); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2133 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 2134 | if (rc) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2135 | ALOGE("loadHwModule() init check error %d for module %s", rc, name); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2136 | return AUDIO_MODULE_HANDLE_NONE; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2137 | } | 
|  | 2138 |  | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2139 | // Check and cache this HAL's level of support for master mute and master | 
|  | 2140 | // volume.  If this is the first HAL opened, and it supports the get | 
|  | 2141 | // methods, use the initial values provided by the HAL as the current | 
|  | 2142 | // master mute and volume settings. | 
|  | 2143 |  | 
|  | 2144 | AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2145 | if (0 == mAudioHwDevs.size()) { | 
|  | 2146 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; | 
|  | 2147 | float mv; | 
|  | 2148 | if (OK == dev->getMasterVolume(&mv)) { | 
|  | 2149 | mMasterVolume = mv; | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2150 | } | 
|  | 2151 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2152 | mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; | 
|  | 2153 | bool mm; | 
|  | 2154 | if (OK == dev->getMasterMute(&mm)) { | 
|  | 2155 | mMasterMute = mm; | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2156 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2157 | } | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2158 |  | 
|  | 2159 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; | 
|  | 2160 | if (OK == dev->setMasterVolume(mMasterVolume)) { | 
|  | 2161 | flags = static_cast<AudioHwDevice::Flags>(flags | | 
|  | 2162 | AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); | 
|  | 2163 | } | 
|  | 2164 |  | 
|  | 2165 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; | 
|  | 2166 | if (OK == dev->setMasterMute(mMasterMute)) { | 
|  | 2167 | flags = static_cast<AudioHwDevice::Flags>(flags | | 
|  | 2168 | AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); | 
|  | 2169 | } | 
|  | 2170 |  | 
|  | 2171 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 2172 |  | 
| Mikhail Naganov | 97373b0 | 2018-07-23 13:27:24 -0700 | [diff] [blame] | 2173 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { | 
| Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2174 | // An MSD module is inserted before hardware modules in order to mix encoded streams. | 
|  | 2175 | flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT); | 
|  | 2176 | } | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2177 |  | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2178 | audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2179 | AudioHwDevice *audioDevice = new AudioHwDevice(handle, name, dev, flags); | 
|  | 2180 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) { | 
|  | 2181 | mPrimaryHardwareDev = audioDevice; | 
|  | 2182 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
|  | 2183 | mPrimaryHardwareDev->hwDevice()->setMode(mMode); | 
|  | 2184 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 2185 | } | 
|  | 2186 |  | 
|  | 2187 | mAudioHwDevs.add(handle, audioDevice); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2188 |  | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2189 | ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2190 |  | 
|  | 2191 | return handle; | 
|  | 2192 |  | 
|  | 2193 | } | 
|  | 2194 |  | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2195 | // ---------------------------------------------------------------------------- | 
|  | 2196 |  | 
| Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 2197 | uint32_t AudioFlinger::getPrimaryOutputSamplingRate() | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2198 | { | 
|  | 2199 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 2200 | PlaybackThread *thread = fastPlaybackThread_l(); | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2201 | return thread != NULL ? thread->sampleRate() : 0; | 
|  | 2202 | } | 
|  | 2203 |  | 
| Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 2204 | size_t AudioFlinger::getPrimaryOutputFrameCount() | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2205 | { | 
|  | 2206 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 2207 | PlaybackThread *thread = fastPlaybackThread_l(); | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2208 | return thread != NULL ? thread->frameCountHAL() : 0; | 
|  | 2209 | } | 
|  | 2210 |  | 
|  | 2211 | // ---------------------------------------------------------------------------- | 
|  | 2212 |  | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2213 | status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2214 | { | 
|  | 2215 | uid_t uid = IPCThreadState::self()->getCallingUid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2216 | if (!isAudioServerOrSystemServerUid(uid)) { | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2217 | return PERMISSION_DENIED; | 
|  | 2218 | } | 
|  | 2219 | Mutex::Autolock _l(mLock); | 
|  | 2220 | if (mIsDeviceTypeKnown) { | 
|  | 2221 | return INVALID_OPERATION; | 
|  | 2222 | } | 
|  | 2223 | mIsLowRamDevice = isLowRamDevice; | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2224 | mTotalMemory = totalMemory; | 
|  | 2225 | // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager; | 
|  | 2226 | // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo(). | 
|  | 2227 | // mIsLowRamDevice generally represent devices with less than 1GB of memory, | 
|  | 2228 | // though actual setting is determined through device configuration. | 
|  | 2229 | constexpr int64_t GB = 1024 * 1024 * 1024; | 
|  | 2230 | mClientSharedHeapSize = | 
|  | 2231 | isLowRamDevice ? kMinimumClientSharedHeapSizeBytes | 
|  | 2232 | : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes | 
|  | 2233 | : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes | 
|  | 2234 | : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes | 
|  | 2235 | : 32 * kMinimumClientSharedHeapSizeBytes; | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2236 | mIsDeviceTypeKnown = true; | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2237 |  | 
|  | 2238 | // TODO: Cache the client shared heap size in a persistent property. | 
|  | 2239 | // It's possible that a native process or Java service or app accesses audioserver | 
|  | 2240 | // after it is registered by system server, but before AudioService updates | 
|  | 2241 | // the memory info.  This would occur immediately after boot or an audioserver | 
|  | 2242 | // crash and restore. Before update from AudioService, the client would get the | 
|  | 2243 | // minimum heap size. | 
|  | 2244 |  | 
|  | 2245 | ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu", | 
|  | 2246 | (isLowRamDevice ? "true" : "false"), | 
|  | 2247 | (long long)mTotalMemory, | 
|  | 2248 | mClientSharedHeapSize.load()); | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2249 | return NO_ERROR; | 
|  | 2250 | } | 
|  | 2251 |  | 
| Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2252 | size_t AudioFlinger::getClientSharedHeapSize() const | 
|  | 2253 | { | 
|  | 2254 | size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024; | 
|  | 2255 | if (heapSizeInBytes != 0) { // read-only property overrides all. | 
|  | 2256 | return heapSizeInBytes; | 
|  | 2257 | } | 
|  | 2258 | return mClientSharedHeapSize; | 
|  | 2259 | } | 
|  | 2260 |  | 
| Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 2261 | status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config) | 
|  | 2262 | { | 
|  | 2263 | ALOGV(__func__); | 
|  | 2264 |  | 
|  | 2265 | audio_module_handle_t module; | 
|  | 2266 | if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2267 | module = config->ext.device.hw_module; | 
|  | 2268 | } else { | 
|  | 2269 | module = config->ext.mix.hw_module; | 
|  | 2270 | } | 
|  | 2271 |  | 
|  | 2272 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2273 | AutoMutex lock(mHardwareLock); | 
| Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 2274 | ssize_t index = mAudioHwDevs.indexOfKey(module); | 
|  | 2275 | if (index < 0) { | 
|  | 2276 | ALOGW("%s() bad hw module %d", __func__, module); | 
|  | 2277 | return BAD_VALUE; | 
|  | 2278 | } | 
|  | 2279 |  | 
|  | 2280 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index); | 
|  | 2281 | return audioHwDevice->hwDevice()->setAudioPortConfig(config); | 
|  | 2282 | } | 
|  | 2283 |  | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2284 | audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId) | 
|  | 2285 | { | 
|  | 2286 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2287 |  | 
|  | 2288 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); | 
|  | 2289 | if (index >= 0) { | 
|  | 2290 | ALOGV("getAudioHwSyncForSession found ID %d for session %d", | 
|  | 2291 | mHwAvSyncIds.valueAt(index), sessionId); | 
|  | 2292 | return mHwAvSyncIds.valueAt(index); | 
|  | 2293 | } | 
|  | 2294 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2295 | sp<DeviceHalInterface> dev; | 
|  | 2296 | { | 
|  | 2297 | AutoMutex lock(mHardwareLock); | 
|  | 2298 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 2299 | return AUDIO_HW_SYNC_INVALID; | 
|  | 2300 | } | 
|  | 2301 | dev = mPrimaryHardwareDev->hwDevice(); | 
|  | 2302 | } | 
|  | 2303 | if (dev == nullptr) { | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2304 | return AUDIO_HW_SYNC_INVALID; | 
|  | 2305 | } | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2306 | String8 reply; | 
|  | 2307 | AudioParameter param; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2308 | if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) { | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2309 | param = AudioParameter(reply); | 
|  | 2310 | } | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2311 |  | 
|  | 2312 | int value; | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2313 | if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) { | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2314 | ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId); | 
|  | 2315 | return AUDIO_HW_SYNC_INVALID; | 
|  | 2316 | } | 
|  | 2317 |  | 
|  | 2318 | // allow only one session for a given HW A/V sync ID. | 
|  | 2319 | for (size_t i = 0; i < mHwAvSyncIds.size(); i++) { | 
|  | 2320 | if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) { | 
|  | 2321 | ALOGV("getAudioHwSyncForSession removing ID %d for session %d", | 
|  | 2322 | value, mHwAvSyncIds.keyAt(i)); | 
|  | 2323 | mHwAvSyncIds.removeItemsAt(i); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2324 | break; | 
|  | 2325 | } | 
|  | 2326 | } | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2327 |  | 
|  | 2328 | mHwAvSyncIds.add(sessionId, value); | 
|  | 2329 |  | 
|  | 2330 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 2331 | sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i); | 
|  | 2332 | uint32_t sessions = thread->hasAudioSession(sessionId); | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2333 | if (sessions & ThreadBase::TRACK_SESSION) { | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2334 | AudioParameter param = AudioParameter(); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2335 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), value); | 
| Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 2336 | String8 keyValuePairs = param.toString(); | 
|  | 2337 | thread->setParameters(keyValuePairs); | 
|  | 2338 | forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs, | 
|  | 2339 | [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); }); | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2340 | break; | 
|  | 2341 | } | 
|  | 2342 | } | 
|  | 2343 |  | 
|  | 2344 | ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId); | 
|  | 2345 | return (audio_hw_sync_t)value; | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2346 | } | 
|  | 2347 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2348 | status_t AudioFlinger::systemReady() | 
|  | 2349 | { | 
|  | 2350 | Mutex::Autolock _l(mLock); | 
|  | 2351 | ALOGI("%s", __FUNCTION__); | 
|  | 2352 | if (mSystemReady) { | 
|  | 2353 | ALOGW("%s called twice", __FUNCTION__); | 
|  | 2354 | return NO_ERROR; | 
|  | 2355 | } | 
|  | 2356 | mSystemReady = true; | 
|  | 2357 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 2358 | ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get(); | 
|  | 2359 | thread->systemReady(); | 
|  | 2360 | } | 
|  | 2361 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 2362 | ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get(); | 
|  | 2363 | thread->systemReady(); | 
|  | 2364 | } | 
|  | 2365 | return NO_ERROR; | 
|  | 2366 | } | 
|  | 2367 |  | 
| jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 2368 | status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) | 
|  | 2369 | { | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 2370 | AutoMutex lock(mHardwareLock); | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2371 | status_t status = INVALID_OPERATION; | 
|  | 2372 |  | 
|  | 2373 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { | 
|  | 2374 | std::vector<media::MicrophoneInfo> mics; | 
|  | 2375 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); | 
|  | 2376 | mHardwareStatus = AUDIO_HW_GET_MICROPHONES; | 
|  | 2377 | status_t devStatus = dev->hwDevice()->getMicrophones(&mics); | 
|  | 2378 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 2379 | if (devStatus == NO_ERROR) { | 
|  | 2380 | microphones->insert(microphones->begin(), mics.begin(), mics.end()); | 
|  | 2381 | // report success if at least one HW module supports the function. | 
|  | 2382 | status = NO_ERROR; | 
|  | 2383 | } | 
|  | 2384 | } | 
|  | 2385 |  | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 2386 | return status; | 
| jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 2387 | } | 
|  | 2388 |  | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2389 | // setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held | 
|  | 2390 | void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId) | 
|  | 2391 | { | 
|  | 2392 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); | 
|  | 2393 | if (index >= 0) { | 
|  | 2394 | audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index); | 
|  | 2395 | ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId); | 
|  | 2396 | AudioParameter param = AudioParameter(); | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2397 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId); | 
| Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 2398 | String8 keyValuePairs = param.toString(); | 
|  | 2399 | thread->setParameters(keyValuePairs); | 
|  | 2400 | forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs, | 
|  | 2401 | [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); }); | 
| Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2402 | } | 
|  | 2403 | } | 
|  | 2404 |  | 
|  | 2405 |  | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2406 | // ---------------------------------------------------------------------------- | 
|  | 2407 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2408 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2409 | sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module, | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2410 | audio_io_handle_t *output, | 
|  | 2411 | audio_config_t *config, | 
|  | 2412 | audio_devices_t deviceType, | 
|  | 2413 | const String8& address, | 
|  | 2414 | audio_output_flags_t flags) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2415 | { | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2416 | AudioHwDevice *outHwDev = findSuitableHwDev_l(module, deviceType); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2417 | if (outHwDev == NULL) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2418 | return 0; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2419 | } | 
|  | 2420 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2421 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2422 | *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); | 
|  | 2423 | } else { | 
|  | 2424 | // Audio Policy does not currently request a specific output handle. | 
|  | 2425 | // If this is ever needed, see openInput_l() for example code. | 
|  | 2426 | ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output); | 
|  | 2427 | return 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2428 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2429 |  | 
|  | 2430 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; | 
|  | 2431 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2432 | // FOR TESTING ONLY: | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2433 | // This if statement allows overriding the audio policy settings | 
|  | 2434 | // and forcing a specific format or channel mask to the HAL/Sink device for testing. | 
|  | 2435 | if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) { | 
|  | 2436 | // Check only for Normal Mixing mode | 
|  | 2437 | if (kEnableExtendedPrecision) { | 
|  | 2438 | // Specify format (uncomment one below to choose) | 
|  | 2439 | //config->format = AUDIO_FORMAT_PCM_FLOAT; | 
|  | 2440 | //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; | 
|  | 2441 | //config->format = AUDIO_FORMAT_PCM_32_BIT; | 
|  | 2442 | //config->format = AUDIO_FORMAT_PCM_8_24_BIT; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2443 | // ALOGV("openOutput_l() upgrading format to %#08x", config->format); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2444 | } | 
|  | 2445 | if (kEnableExtendedChannels) { | 
|  | 2446 | // Specify channel mask (uncomment one below to choose) | 
|  | 2447 | //config->channel_mask = audio_channel_out_mask_from_count(4);  // for USB 4ch | 
|  | 2448 | //config->channel_mask = audio_channel_mask_from_representation_and_bits( | 
|  | 2449 | //        AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1);  // another 4ch example | 
|  | 2450 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2451 | } | 
|  | 2452 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2453 | AudioStreamOut *outputStream = NULL; | 
|  | 2454 | status_t status = outHwDev->openOutputStream( | 
|  | 2455 | &outputStream, | 
|  | 2456 | *output, | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2457 | deviceType, | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2458 | flags, | 
|  | 2459 | config, | 
|  | 2460 | address.string()); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2461 |  | 
|  | 2462 | mHardwareStatus = AUDIO_HW_IDLE; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2463 |  | 
| Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2464 | if (status == NO_ERROR) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2465 | if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { | 
|  | 2466 | sp<MmapPlaybackThread> thread = | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2467 | new MmapPlaybackThread(this, *output, outHwDev, outputStream, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2468 | mMmapThreads.add(*output, thread); | 
|  | 2469 | ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p", | 
|  | 2470 | *output, thread.get()); | 
|  | 2471 | return thread; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2472 | } else { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2473 | sp<PlaybackThread> thread; | 
|  | 2474 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2475 | thread = new OffloadThread(this, outputStream, *output, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2476 | ALOGV("openOutput_l() created offload output: ID %d thread %p", | 
|  | 2477 | *output, thread.get()); | 
|  | 2478 | } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) | 
|  | 2479 | || !isValidPcmSinkFormat(config->format) | 
|  | 2480 | || !isValidPcmSinkChannelMask(config->channel_mask)) { | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2481 | thread = new DirectOutputThread(this, outputStream, *output, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2482 | ALOGV("openOutput_l() created direct output: ID %d thread %p", | 
|  | 2483 | *output, thread.get()); | 
|  | 2484 | } else { | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2485 | thread = new MixerThread(this, outputStream, *output, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2486 | ALOGV("openOutput_l() created mixer output: ID %d thread %p", | 
|  | 2487 | *output, thread.get()); | 
|  | 2488 | } | 
|  | 2489 | mPlaybackThreads.add(*output, thread); | 
| Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2490 | mPatchPanel.notifyStreamOpened(outHwDev, *output); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2491 | return thread; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2492 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2493 | } | 
|  | 2494 |  | 
|  | 2495 | return 0; | 
|  | 2496 | } | 
|  | 2497 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2498 | status_t AudioFlinger::openOutput(audio_module_handle_t module, | 
|  | 2499 | audio_io_handle_t *output, | 
|  | 2500 | audio_config_t *config, | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2501 | const sp<DeviceDescriptorBase>& device, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2502 | uint32_t *latencyMs, | 
|  | 2503 | audio_output_flags_t flags) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2504 | { | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2505 | ALOGI("openOutput() this %p, module %d Device %s, SamplingRate %d, Format %#08x, " | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2506 | "Channels %#x, flags %#x", | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2507 | this, module, | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2508 | device->toString().c_str(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2509 | config->sample_rate, | 
|  | 2510 | config->format, | 
|  | 2511 | config->channel_mask, | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2512 | flags); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2513 |  | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2514 | audio_devices_t deviceType = device->type(); | 
|  | 2515 | const String8 address = String8(device->address().c_str()); | 
|  | 2516 |  | 
|  | 2517 | if (deviceType == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2518 | return BAD_VALUE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2519 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2520 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2521 | Mutex::Autolock _l(mLock); | 
|  | 2522 |  | 
| jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2523 | sp<ThreadBase> thread = openOutput_l(module, output, config, deviceType, address, flags); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2524 | if (thread != 0) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2525 | if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) { | 
|  | 2526 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); | 
|  | 2527 | *latencyMs = playbackThread->latency(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2528 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2529 | // notify client processes of the new output creation | 
|  | 2530 | playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2531 |  | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 2532 | // the first primary output opened designates the primary hw device if no HW module | 
|  | 2533 | // named "primary" was already loaded. | 
|  | 2534 | AutoMutex lock(mHardwareLock); | 
|  | 2535 | if ((mPrimaryHardwareDev == nullptr) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { | 
| Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 2536 | ALOGI("Using module %d as the primary audio interface", module); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2537 | mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev; | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2538 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2539 | mHardwareStatus = AUDIO_HW_SET_MODE; | 
|  | 2540 | mPrimaryHardwareDev->hwDevice()->setMode(mMode); | 
|  | 2541 | mHardwareStatus = AUDIO_HW_IDLE; | 
|  | 2542 | } | 
|  | 2543 | } else { | 
|  | 2544 | MmapThread *mmapThread = (MmapThread *)thread.get(); | 
|  | 2545 | mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED); | 
| Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2546 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2547 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2548 | } | 
|  | 2549 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2550 | return NO_INIT; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2551 | } | 
|  | 2552 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2553 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, | 
|  | 2554 | audio_io_handle_t output2) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2555 | { | 
|  | 2556 | Mutex::Autolock _l(mLock); | 
|  | 2557 | MixerThread *thread1 = checkMixerThread_l(output1); | 
|  | 2558 | MixerThread *thread2 = checkMixerThread_l(output2); | 
|  | 2559 |  | 
|  | 2560 | if (thread1 == NULL || thread2 == NULL) { | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2561 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, | 
|  | 2562 | output2); | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2563 | return AUDIO_IO_HANDLE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2564 | } | 
|  | 2565 |  | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2566 | audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2567 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2568 | thread->addOutputTrack(thread2); | 
|  | 2569 | mPlaybackThreads.add(id, thread); | 
|  | 2570 | // notify client processes of the new output creation | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2571 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2572 | return id; | 
|  | 2573 | } | 
|  | 2574 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2575 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2576 | { | 
| Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2577 | return closeOutput_nonvirtual(output); | 
|  | 2578 | } | 
|  | 2579 |  | 
|  | 2580 | status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) | 
|  | 2581 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2582 | // keep strong reference on the playback thread so that | 
|  | 2583 | // it is not destroyed while exit() is executed | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2584 | sp<PlaybackThread> playbackThread; | 
|  | 2585 | sp<MmapPlaybackThread> mmapThread; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2586 | { | 
|  | 2587 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2588 | playbackThread = checkPlaybackThread_l(output); | 
|  | 2589 | if (playbackThread != NULL) { | 
|  | 2590 | ALOGV("closeOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2591 |  | 
| Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2592 | dumpToThreadLog_l(playbackThread); | 
| Andy Hung | a8115dc | 2018-08-24 15:51:59 -0700 | [diff] [blame] | 2593 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2594 | if (playbackThread->type() == ThreadBase::MIXER) { | 
|  | 2595 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 2596 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { | 
|  | 2597 | DuplicatingThread *dupThread = | 
|  | 2598 | (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); | 
|  | 2599 | dupThread->removeOutputTrack((MixerThread *)playbackThread.get()); | 
|  | 2600 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2601 | } | 
|  | 2602 | } | 
| Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2603 |  | 
|  | 2604 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2605 | mPlaybackThreads.removeItem(output); | 
|  | 2606 | // save all effects to the default thread | 
|  | 2607 | if (mPlaybackThreads.size()) { | 
|  | 2608 | PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); | 
|  | 2609 | if (dstThread != NULL) { | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2610 | // audioflinger lock is held so order of thread lock acquisition doesn't matter | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2611 | Mutex::Autolock _dl(dstThread->mLock); | 
|  | 2612 | Mutex::Autolock _sl(playbackThread->mLock); | 
|  | 2613 | Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l(); | 
|  | 2614 | for (size_t i = 0; i < effectChains.size(); i ++) { | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2615 | moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(), | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2616 | dstThread); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2617 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2618 | } | 
|  | 2619 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2620 | } else { | 
|  | 2621 | mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output); | 
|  | 2622 | if (mmapThread == 0) { | 
|  | 2623 | return BAD_VALUE; | 
|  | 2624 | } | 
| Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2625 | dumpToThreadLog_l(mmapThread); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2626 | mMmapThreads.removeItem(output); | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 2627 | ALOGD("closing mmapThread %p", mmapThread.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2628 | } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2629 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); | 
|  | 2630 | ioDesc->mIoHandle = output; | 
|  | 2631 | ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc); | 
| Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2632 | mPatchPanel.notifyStreamClosed(output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2633 | } | 
| Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 2634 | // The thread entity (active unit of execution) is no longer running here, | 
|  | 2635 | // but the ThreadBase container still exists. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2636 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2637 | if (playbackThread != 0) { | 
|  | 2638 | playbackThread->exit(); | 
|  | 2639 | if (!playbackThread->isDuplicating()) { | 
|  | 2640 | closeOutputFinish(playbackThread); | 
|  | 2641 | } | 
|  | 2642 | } else if (mmapThread != 0) { | 
| Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 2643 | ALOGD("mmapThread exit()"); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2644 | mmapThread->exit(); | 
|  | 2645 | AudioStreamOut *out = mmapThread->clearOutput(); | 
|  | 2646 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); | 
|  | 2647 | // from now on thread->mOutput is NULL | 
|  | 2648 | delete out; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2649 | } | 
|  | 2650 | return NO_ERROR; | 
|  | 2651 | } | 
|  | 2652 |  | 
| Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2653 | void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2654 | { | 
|  | 2655 | AudioStreamOut *out = thread->clearOutput(); | 
|  | 2656 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); | 
|  | 2657 | // from now on thread->mOutput is NULL | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2658 | delete out; | 
|  | 2659 | } | 
|  | 2660 |  | 
| Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 2661 | void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2662 | { | 
|  | 2663 | mPlaybackThreads.removeItem(thread->mId); | 
|  | 2664 | thread->exit(); | 
|  | 2665 | closeOutputFinish(thread); | 
|  | 2666 | } | 
|  | 2667 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2668 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2669 | { | 
|  | 2670 | Mutex::Autolock _l(mLock); | 
|  | 2671 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 2672 |  | 
|  | 2673 | if (thread == NULL) { | 
|  | 2674 | return BAD_VALUE; | 
|  | 2675 | } | 
|  | 2676 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2677 | ALOGV("suspendOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2678 | thread->suspend(); | 
|  | 2679 |  | 
|  | 2680 | return NO_ERROR; | 
|  | 2681 | } | 
|  | 2682 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2683 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2684 | { | 
|  | 2685 | Mutex::Autolock _l(mLock); | 
|  | 2686 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
|  | 2687 |  | 
|  | 2688 | if (thread == NULL) { | 
|  | 2689 | return BAD_VALUE; | 
|  | 2690 | } | 
|  | 2691 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2692 | ALOGV("restoreOutput() %d", output); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2693 |  | 
|  | 2694 | thread->restore(); | 
|  | 2695 |  | 
|  | 2696 | return NO_ERROR; | 
|  | 2697 | } | 
|  | 2698 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2699 | status_t AudioFlinger::openInput(audio_module_handle_t module, | 
|  | 2700 | audio_io_handle_t *input, | 
|  | 2701 | audio_config_t *config, | 
| Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2702 | audio_devices_t *devices, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2703 | const String8& address, | 
|  | 2704 | audio_source_t source, | 
| Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2705 | audio_input_flags_t flags) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2706 | { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2707 | Mutex::Autolock _l(mLock); | 
|  | 2708 |  | 
| Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2709 | if (*devices == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2710 | return BAD_VALUE; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2711 | } | 
|  | 2712 |  | 
| Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2713 | sp<ThreadBase> thread = openInput_l( | 
|  | 2714 | module, input, config, *devices, address, source, flags, AUDIO_DEVICE_NONE, String8{}); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2715 |  | 
|  | 2716 | if (thread != 0) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2717 | // notify client processes of the new input creation | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2718 | thread->ioConfigChanged(AUDIO_INPUT_OPENED); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2719 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2720 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2721 | return NO_INIT; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2722 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2723 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2724 | sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2725 | audio_io_handle_t *input, | 
|  | 2726 | audio_config_t *config, | 
| Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2727 | audio_devices_t devices, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2728 | const String8& address, | 
|  | 2729 | audio_source_t source, | 
| Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2730 | audio_input_flags_t flags, | 
|  | 2731 | audio_devices_t outputDevice, | 
|  | 2732 | const String8& outputDeviceAddress) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2733 | { | 
| Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2734 | AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices); | 
| Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2735 | if (inHwDev == NULL) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2736 | *input = AUDIO_IO_HANDLE_NONE; | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2737 | return 0; | 
| Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2738 | } | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2739 |  | 
| Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2740 | // Some flags are specific to framework and must not leak to the HAL. | 
|  | 2741 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FRAMEWORK_FLAGS); | 
|  | 2742 |  | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2743 | // Audio Policy can request a specific handle for hardware hotword. | 
|  | 2744 | // The goal here is not to re-open an already opened input. | 
|  | 2745 | // It is to use a pre-assigned I/O handle. | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2746 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2747 | *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
|  | 2748 | } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) { | 
|  | 2749 | ALOGE("openInput_l() requested input handle %d is invalid", *input); | 
|  | 2750 | return 0; | 
|  | 2751 | } else if (mRecordThreads.indexOfKey(*input) >= 0) { | 
|  | 2752 | // This should not happen in a transient state with current design. | 
|  | 2753 | ALOGE("openInput_l() requested input handle %d is already assigned", *input); | 
|  | 2754 | return 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2755 | } | 
| Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2756 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2757 | audio_config_t halconfig = *config; | 
| Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2758 | sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice(); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2759 | sp<StreamInHalInterface> inStream; | 
|  | 2760 | status_t status = inHwHal->openInputStream( | 
| Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2761 | *input, devices, &halconfig, flags, address.string(), source, | 
|  | 2762 | outputDevice, outputDeviceAddress, &inStream); | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2763 | ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d" | 
|  | 2764 | ", Format %#x, Channels %#x, flags %#x, status %d addr %s", | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2765 | inStream.get(), | 
| Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 2766 | devices, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2767 | halconfig.sample_rate, | 
|  | 2768 | halconfig.format, | 
|  | 2769 | halconfig.channel_mask, | 
| Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2770 | flags, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2771 | status, address.string()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2772 |  | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2773 | // If the input could not be opened with the requested parameters and we can handle the | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2774 | // conversion internally, try to open again with the proposed parameters. | 
| Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2775 | if (status == BAD_VALUE && | 
| Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2776 | audio_is_linear_pcm(config->format) && | 
|  | 2777 | audio_is_linear_pcm(halconfig.format) && | 
|  | 2778 | (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && | 
| vivek mehta | 7534666 | 2016-05-04 18:45:46 -0700 | [diff] [blame] | 2779 | (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) && | 
|  | 2780 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) { | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2781 | // FIXME describe the change proposed by HAL (save old values so we can log them here) | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2782 | ALOGV("openInput_l() reopening with proposed sampling rate and channel mask"); | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2783 | inStream.clear(); | 
|  | 2784 | status = inHwHal->openInputStream( | 
| Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2785 | *input, devices, &halconfig, flags, address.string(), source, | 
|  | 2786 | outputDevice, outputDeviceAddress, &inStream); | 
| Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2787 | // FIXME log this new status; HAL should not propose any further changes | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2788 | } | 
|  | 2789 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2790 | if (status == NO_ERROR && inStream != 0) { | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2791 | AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2792 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) { | 
|  | 2793 | sp<MmapCaptureThread> thread = | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2794 | new MmapCaptureThread(this, *input, inHwDev, inputStream, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2795 | mMmapThreads.add(*input, thread); | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2796 | ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input, | 
|  | 2797 | thread.get()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2798 | return thread; | 
|  | 2799 | } else { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2800 | // Start record thread | 
|  | 2801 | // RecordThread requires both input and output device indication to forward to audio | 
|  | 2802 | // pre processing modules | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2803 | sp<RecordThread> thread = new RecordThread(this, inputStream, *input, mSystemReady); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2804 | mRecordThreads.add(*input, thread); | 
|  | 2805 | ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get()); | 
|  | 2806 | return thread; | 
|  | 2807 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2808 | } | 
|  | 2809 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2810 | *input = AUDIO_IO_HANDLE_NONE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2811 | return 0; | 
|  | 2812 | } | 
|  | 2813 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2814 | status_t AudioFlinger::closeInput(audio_io_handle_t input) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2815 | { | 
| Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2816 | return closeInput_nonvirtual(input); | 
|  | 2817 | } | 
|  | 2818 |  | 
|  | 2819 | status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) | 
|  | 2820 | { | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2821 | // keep strong reference on the record thread so that | 
|  | 2822 | // it is not destroyed while exit() is executed | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2823 | sp<RecordThread> recordThread; | 
|  | 2824 | sp<MmapCaptureThread> mmapThread; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2825 | { | 
|  | 2826 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2827 | recordThread = checkRecordThread_l(input); | 
|  | 2828 | if (recordThread != 0) { | 
|  | 2829 | ALOGV("closeInput() %d", input); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2830 |  | 
| Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2831 | dumpToThreadLog_l(recordThread); | 
| Andy Hung | 0264e7d | 2018-09-17 19:30:46 -0700 | [diff] [blame] | 2832 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2833 | // If we still have effect chains, it means that a client still holds a handle | 
|  | 2834 | // on at least one effect. We must either move the chain to an existing thread with the | 
|  | 2835 | // same session ID or put it aside in case a new record thread is opened for a | 
|  | 2836 | // new capture on the same session | 
|  | 2837 | sp<EffectChain> chain; | 
|  | 2838 | { | 
|  | 2839 | Mutex::Autolock _sl(recordThread->mLock); | 
|  | 2840 | Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l(); | 
|  | 2841 | // Note: maximum one chain per record thread | 
|  | 2842 | if (effectChains.size() != 0) { | 
|  | 2843 | chain = effectChains[0]; | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2844 | } | 
|  | 2845 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2846 | if (chain != 0) { | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2847 | // first check if a record thread is already opened with a client on same session. | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2848 | // This should only happen in case of overlap between one thread tear down and the | 
|  | 2849 | // creation of its replacement | 
|  | 2850 | size_t i; | 
|  | 2851 | for (i = 0; i < mRecordThreads.size(); i++) { | 
|  | 2852 | sp<RecordThread> t = mRecordThreads.valueAt(i); | 
|  | 2853 | if (t == recordThread) { | 
|  | 2854 | continue; | 
|  | 2855 | } | 
|  | 2856 | if (t->hasAudioSession(chain->sessionId()) != 0) { | 
|  | 2857 | Mutex::Autolock _l(t->mLock); | 
|  | 2858 | ALOGV("closeInput() found thread %d for effect session %d", | 
|  | 2859 | t->id(), chain->sessionId()); | 
|  | 2860 | t->addEffectChain_l(chain); | 
|  | 2861 | break; | 
|  | 2862 | } | 
|  | 2863 | } | 
| Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2864 | // put the chain aside if we could not find a record thread with the same session id | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2865 | if (i == mRecordThreads.size()) { | 
|  | 2866 | putOrphanEffectChain_l(chain); | 
|  | 2867 | } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2868 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2869 | mRecordThreads.removeItem(input); | 
|  | 2870 | } else { | 
|  | 2871 | mmapThread = (MmapCaptureThread *)checkMmapThread_l(input); | 
|  | 2872 | if (mmapThread == 0) { | 
|  | 2873 | return BAD_VALUE; | 
|  | 2874 | } | 
| Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2875 | dumpToThreadLog_l(mmapThread); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2876 | mMmapThreads.removeItem(input); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2877 | } | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2878 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); | 
|  | 2879 | ioDesc->mIoHandle = input; | 
|  | 2880 | ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2881 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2882 | // FIXME: calling thread->exit() without mLock held should not be needed anymore now that | 
|  | 2883 | // we have a different lock for notification client | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2884 | if (recordThread != 0) { | 
|  | 2885 | closeInputFinish(recordThread); | 
|  | 2886 | } else if (mmapThread != 0) { | 
|  | 2887 | mmapThread->exit(); | 
|  | 2888 | AudioStreamIn *in = mmapThread->clearInput(); | 
|  | 2889 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); | 
|  | 2890 | // from now on thread->mInput is NULL | 
|  | 2891 | delete in; | 
|  | 2892 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2893 | return NO_ERROR; | 
|  | 2894 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2895 |  | 
| Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2896 | void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2897 | { | 
|  | 2898 | thread->exit(); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2899 | AudioStreamIn *in = thread->clearInput(); | 
| Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 2900 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2901 | // from now on thread->mInput is NULL | 
| Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2902 | delete in; | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2903 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2904 |  | 
| Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 2905 | void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread) | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2906 | { | 
|  | 2907 | mRecordThreads.removeItem(thread->mId); | 
|  | 2908 | closeInputFinish(thread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2909 | } | 
|  | 2910 |  | 
| Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2911 | status_t AudioFlinger::invalidateStream(audio_stream_type_t stream) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2912 | { | 
|  | 2913 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2914 | ALOGV("invalidateStream() stream %d", stream); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2915 |  | 
|  | 2916 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 2917 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
| Eric Laurent | 2216785 | 2012-06-20 12:26:32 -0700 | [diff] [blame] | 2918 | thread->invalidateTracks(stream); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2919 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2920 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 2921 | mMmapThreads[i]->invalidateTracks(stream); | 
|  | 2922 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2923 | return NO_ERROR; | 
|  | 2924 | } | 
|  | 2925 |  | 
|  | 2926 |  | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2927 | audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2928 | { | 
| Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 2929 | // This is a binder API, so a malicious client could pass in a bad parameter. | 
|  | 2930 | // Check for that before calling the internal API nextUniqueId(). | 
|  | 2931 | if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) { | 
|  | 2932 | ALOGE("newAudioUniqueId invalid use %d", use); | 
|  | 2933 | return AUDIO_UNIQUE_ID_ALLOCATE; | 
|  | 2934 | } | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2935 | return nextUniqueId(use); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2936 | } | 
|  | 2937 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2938 | void AudioFlinger::acquireAudioSessionId(audio_session_t audioSession, pid_t pid) | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2939 | { | 
|  | 2940 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2941 | pid_t caller = IPCThreadState::self()->getCallingPid(); | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2942 | ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2943 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); | 
|  | 2944 | if (pid != -1 && isAudioServerUid(callerUid)) { // check must match releaseAudioSessionId() | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2945 | caller = pid; | 
|  | 2946 | } | 
| Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2947 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2948 | { | 
|  | 2949 | Mutex::Autolock _cl(mClientLock); | 
|  | 2950 | // Ignore requests received from processes not known as notification client. The request | 
|  | 2951 | // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be | 
|  | 2952 | // called from a different pid leaving a stale session reference.  Also we don't know how | 
|  | 2953 | // to clear this reference if the client process dies. | 
|  | 2954 | if (mNotificationClients.indexOfKey(caller) < 0) { | 
|  | 2955 | ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); | 
|  | 2956 | return; | 
|  | 2957 | } | 
| Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2958 | } | 
|  | 2959 |  | 
| Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2960 | size_t num = mAudioSessionRefs.size(); | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 2961 | for (size_t i = 0; i < num; i++) { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2962 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); | 
| Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 2963 | if (ref->mSessionid == audioSession && ref->mPid == caller) { | 
|  | 2964 | ref->mCnt++; | 
|  | 2965 | ALOGV(" incremented refcount to %d", ref->mCnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2966 | return; | 
|  | 2967 | } | 
|  | 2968 | } | 
| Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 2969 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller)); | 
|  | 2970 | ALOGV(" added new entry for %d", audioSession); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2971 | } | 
|  | 2972 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2973 | void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2974 | { | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2975 | std::vector< sp<EffectModule> > removedEffects; | 
|  | 2976 | { | 
|  | 2977 | Mutex::Autolock _l(mLock); | 
|  | 2978 | pid_t caller = IPCThreadState::self()->getCallingPid(); | 
|  | 2979 | ALOGV("releasing %d from %d for %d", audioSession, caller, pid); | 
|  | 2980 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); | 
|  | 2981 | if (pid != -1 && isAudioServerUid(callerUid)) { // check must match acquireAudioSessionId() | 
|  | 2982 | caller = pid; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2983 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2984 | size_t num = mAudioSessionRefs.size(); | 
|  | 2985 | for (size_t i = 0; i < num; i++) { | 
|  | 2986 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
|  | 2987 | if (ref->mSessionid == audioSession && ref->mPid == caller) { | 
|  | 2988 | ref->mCnt--; | 
|  | 2989 | ALOGV(" decremented refcount to %d", ref->mCnt); | 
|  | 2990 | if (ref->mCnt == 0) { | 
|  | 2991 | mAudioSessionRefs.removeAt(i); | 
|  | 2992 | delete ref; | 
|  | 2993 | std::vector< sp<EffectModule> > effects = purgeStaleEffects_l(); | 
|  | 2994 | removedEffects.insert(removedEffects.end(), effects.begin(), effects.end()); | 
|  | 2995 | } | 
|  | 2996 | goto Exit; | 
|  | 2997 | } | 
|  | 2998 | } | 
|  | 2999 | // If the caller is audioserver it is likely that the session being released was acquired | 
|  | 3000 | // on behalf of a process not in notification clients and we ignore the warning. | 
|  | 3001 | ALOGW_IF(!isAudioServerUid(callerUid), | 
|  | 3002 | "session id %d not found for pid %d", audioSession, caller); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3003 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3004 |  | 
|  | 3005 | Exit: | 
|  | 3006 | for (auto& effect : removedEffects) { | 
|  | 3007 | effect->updatePolicyState(); | 
|  | 3008 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3009 | } | 
|  | 3010 |  | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3011 | bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession) | 
|  | 3012 | { | 
|  | 3013 | size_t num = mAudioSessionRefs.size(); | 
|  | 3014 | for (size_t i = 0; i < num; i++) { | 
|  | 3015 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); | 
|  | 3016 | if (ref->mSessionid == audioSession) { | 
|  | 3017 | return true; | 
|  | 3018 | } | 
|  | 3019 | } | 
|  | 3020 | return false; | 
|  | 3021 | } | 
|  | 3022 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3023 | std::vector<sp<AudioFlinger::EffectModule>> AudioFlinger::purgeStaleEffects_l() { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3024 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3025 | ALOGV("purging stale effects"); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3026 |  | 
|  | 3027 | Vector< sp<EffectChain> > chains; | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3028 | std::vector< sp<EffectModule> > removedEffects; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3029 |  | 
|  | 3030 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3031 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
| Mateusz Kaplon | 2a6e1b0 | 2017-03-30 16:50:50 +0200 | [diff] [blame] | 3032 | Mutex::Autolock _l(t->mLock); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3033 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 3034 | sp<EffectChain> ec = t->mEffectChains[j]; | 
| Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3035 | if (!audio_is_global_session(ec->sessionId())) { | 
| Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 3036 | chains.push(ec); | 
|  | 3037 | } | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3038 | } | 
|  | 3039 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3040 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3041 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 3042 | sp<RecordThread> t = mRecordThreads.valueAt(i); | 
| Mateusz Kaplon | 2a6e1b0 | 2017-03-30 16:50:50 +0200 | [diff] [blame] | 3043 | Mutex::Autolock _l(t->mLock); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3044 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 3045 | sp<EffectChain> ec = t->mEffectChains[j]; | 
|  | 3046 | chains.push(ec); | 
|  | 3047 | } | 
|  | 3048 | } | 
|  | 3049 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3050 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 3051 | sp<MmapThread> t = mMmapThreads.valueAt(i); | 
|  | 3052 | Mutex::Autolock _l(t->mLock); | 
|  | 3053 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { | 
|  | 3054 | sp<EffectChain> ec = t->mEffectChains[j]; | 
|  | 3055 | chains.push(ec); | 
|  | 3056 | } | 
|  | 3057 | } | 
|  | 3058 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3059 | for (size_t i = 0; i < chains.size(); i++) { | 
|  | 3060 | sp<EffectChain> ec = chains[i]; | 
|  | 3061 | int sessionid = ec->sessionId(); | 
| Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3062 | sp<ThreadBase> t = ec->thread().promote(); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3063 | if (t == 0) { | 
|  | 3064 | continue; | 
|  | 3065 | } | 
|  | 3066 | size_t numsessionrefs = mAudioSessionRefs.size(); | 
|  | 3067 | bool found = false; | 
|  | 3068 | for (size_t k = 0; k < numsessionrefs; k++) { | 
|  | 3069 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); | 
| Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 3070 | if (ref->mSessionid == sessionid) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3071 | ALOGV(" session %d still exists for %d with %d refs", | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3072 | sessionid, ref->mPid, ref->mCnt); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3073 | found = true; | 
|  | 3074 | break; | 
|  | 3075 | } | 
|  | 3076 | } | 
|  | 3077 | if (!found) { | 
| Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 3078 | Mutex::Autolock _l(t->mLock); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3079 | // remove all effects from the chain | 
|  | 3080 | while (ec->mEffects.size()) { | 
|  | 3081 | sp<EffectModule> effect = ec->mEffects[0]; | 
|  | 3082 | effect->unPin(); | 
| Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 3083 | t->removeEffect_l(effect, /*release*/ true); | 
| Eric Laurent | a5f44eb | 2012-06-25 11:38:29 -0700 | [diff] [blame] | 3084 | if (effect->purgeHandles()) { | 
| Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3085 | effect->checkSuspendOnEffectEnabled(false, true /*threadLocked*/); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3086 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3087 | removedEffects.push_back(effect); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3088 | } | 
|  | 3089 | } | 
|  | 3090 | } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3091 | return removedEffects; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3092 | } | 
|  | 3093 |  | 
| Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 3094 | // dumpToThreadLog_l() must be called with AudioFlinger::mLock held | 
|  | 3095 | void AudioFlinger::dumpToThreadLog_l(const sp<ThreadBase> &thread) | 
|  | 3096 | { | 
|  | 3097 | audio_utils::FdToString fdToString; | 
|  | 3098 | const int fd = fdToString.fd(); | 
|  | 3099 | if (fd >= 0) { | 
|  | 3100 | thread->dump(fd, {} /* args */); | 
|  | 3101 | mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose()); | 
|  | 3102 | } | 
|  | 3103 | } | 
|  | 3104 |  | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3105 | // checkThread_l() must be called with AudioFlinger::mLock held | 
|  | 3106 | AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const | 
|  | 3107 | { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3108 | ThreadBase *thread = checkMmapThread_l(ioHandle); | 
|  | 3109 | if (thread == 0) { | 
|  | 3110 | switch (audio_unique_id_get_use(ioHandle)) { | 
|  | 3111 | case AUDIO_UNIQUE_ID_USE_OUTPUT: | 
|  | 3112 | thread = checkPlaybackThread_l(ioHandle); | 
|  | 3113 | break; | 
|  | 3114 | case AUDIO_UNIQUE_ID_USE_INPUT: | 
|  | 3115 | thread = checkRecordThread_l(ioHandle); | 
|  | 3116 | break; | 
|  | 3117 | default: | 
|  | 3118 | break; | 
|  | 3119 | } | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3120 | } | 
|  | 3121 | return thread; | 
|  | 3122 | } | 
|  | 3123 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3124 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3125 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3126 | { | 
| Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3127 | return mPlaybackThreads.valueFor(output).get(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3128 | } | 
|  | 3129 |  | 
|  | 3130 | // checkMixerThread_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3131 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3132 | { | 
|  | 3133 | PlaybackThread *thread = checkPlaybackThread_l(output); | 
| Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3134 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3135 | } | 
|  | 3136 |  | 
|  | 3137 | // checkRecordThread_l() must be called with AudioFlinger::mLock held | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3138 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3139 | { | 
| Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3140 | return mRecordThreads.valueFor(input).get(); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3141 | } | 
|  | 3142 |  | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3143 | // checkMmapThread_l() must be called with AudioFlinger::mLock held | 
|  | 3144 | AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const | 
|  | 3145 | { | 
|  | 3146 | return mMmapThreads.valueFor(io).get(); | 
|  | 3147 | } | 
|  | 3148 |  | 
|  | 3149 |  | 
|  | 3150 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held | 
|  | 3151 | AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const | 
|  | 3152 | { | 
| Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3153 | VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get(); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3154 | if (volumeInterface == nullptr) { | 
|  | 3155 | MmapThread *mmapThread = mMmapThreads.valueFor(output).get(); | 
|  | 3156 | if (mmapThread != nullptr) { | 
|  | 3157 | if (mmapThread->isOutput()) { | 
| Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3158 | MmapPlaybackThread *mmapPlaybackThread = | 
|  | 3159 | static_cast<MmapPlaybackThread *>(mmapThread); | 
|  | 3160 | volumeInterface = mmapPlaybackThread; | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3161 | } | 
|  | 3162 | } | 
|  | 3163 | } | 
|  | 3164 | return volumeInterface; | 
|  | 3165 | } | 
|  | 3166 |  | 
|  | 3167 | Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const | 
|  | 3168 | { | 
|  | 3169 | Vector <VolumeInterface *> volumeInterfaces; | 
|  | 3170 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
| Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3171 | volumeInterfaces.add(mPlaybackThreads.valueAt(i).get()); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3172 | } | 
|  | 3173 | for (size_t i = 0; i < mMmapThreads.size(); i++) { | 
|  | 3174 | if (mMmapThreads.valueAt(i)->isOutput()) { | 
| Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3175 | MmapPlaybackThread *mmapPlaybackThread = | 
|  | 3176 | static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get()); | 
|  | 3177 | volumeInterfaces.add(mmapPlaybackThread); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3178 | } | 
|  | 3179 | } | 
|  | 3180 | return volumeInterfaces; | 
|  | 3181 | } | 
|  | 3182 |  | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3183 | audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use) | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3184 | { | 
| Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 3185 | // This is the internal API, so it is OK to assert on bad parameter. | 
| Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3186 | LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX); | 
| Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 3187 | const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1; | 
|  | 3188 | for (int retry = 0; retry < maxRetries; retry++) { | 
|  | 3189 | // The cast allows wraparound from max positive to min negative instead of abort | 
|  | 3190 | uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use], | 
|  | 3191 | (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel); | 
|  | 3192 | ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED); | 
|  | 3193 | // allow wrap by skipping 0 and -1 for session ids | 
|  | 3194 | if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) { | 
|  | 3195 | ALOGW_IF(retry != 0, "unique ID overflow for use %d", use); | 
|  | 3196 | return (audio_unique_id_t) (base | use); | 
|  | 3197 | } | 
|  | 3198 | } | 
|  | 3199 | // We have no way of recovering from wraparound | 
|  | 3200 | LOG_ALWAYS_FATAL("unique ID overflow for use %d", use); | 
|  | 3201 | // TODO Use a floor after wraparound.  This may need a mutex. | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3202 | } | 
|  | 3203 |  | 
| Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 3204 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3205 | { | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 3206 | AutoMutex lock(mHardwareLock); | 
|  | 3207 | if (mPrimaryHardwareDev == nullptr) { | 
|  | 3208 | return nullptr; | 
|  | 3209 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3210 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3211 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
| Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 3212 | if(thread->isDuplicating()) { | 
|  | 3213 | continue; | 
|  | 3214 | } | 
| Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 3215 | AudioStreamOut *output = thread->getOutput(); | 
| John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 3216 | if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3217 | return thread; | 
|  | 3218 | } | 
|  | 3219 | } | 
| Eric Laurent | 488d500 | 2020-05-14 09:13:30 -0700 | [diff] [blame] | 3220 | return nullptr; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3221 | } | 
|  | 3222 |  | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3223 | DeviceTypeSet AudioFlinger::primaryOutputDevice_l() const | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3224 | { | 
|  | 3225 | PlaybackThread *thread = primaryPlaybackThread_l(); | 
|  | 3226 |  | 
|  | 3227 | if (thread == NULL) { | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3228 | return DeviceTypeSet(); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3229 | } | 
|  | 3230 |  | 
| jiabin | 10d86fd | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3231 | return thread->outDeviceTypes(); | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3232 | } | 
|  | 3233 |  | 
| Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 3234 | AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const | 
|  | 3235 | { | 
|  | 3236 | size_t minFrameCount = 0; | 
|  | 3237 | PlaybackThread *minThread = NULL; | 
|  | 3238 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3239 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); | 
|  | 3240 | if (!thread->isDuplicating()) { | 
|  | 3241 | size_t frameCount = thread->frameCountHAL(); | 
|  | 3242 | if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount || | 
|  | 3243 | (frameCount == minFrameCount && thread->hasFastMixer() && | 
|  | 3244 | /*minThread != NULL &&*/ !minThread->hasFastMixer()))) { | 
|  | 3245 | minFrameCount = frameCount; | 
|  | 3246 | minThread = thread; | 
|  | 3247 | } | 
|  | 3248 | } | 
|  | 3249 | } | 
|  | 3250 | return minThread; | 
|  | 3251 | } | 
|  | 3252 |  | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3253 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3254 | audio_session_t triggerSession, | 
|  | 3255 | audio_session_t listenerSession, | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3256 | sync_event_callback_t callBack, | 
| Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3257 | const wp<RefBase>& cookie) | 
| Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3258 | { | 
|  | 3259 | Mutex::Autolock _l(mLock); | 
|  | 3260 |  | 
|  | 3261 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); | 
|  | 3262 | status_t playStatus = NAME_NOT_FOUND; | 
|  | 3263 | status_t recStatus = NAME_NOT_FOUND; | 
|  | 3264 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3265 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); | 
|  | 3266 | if (playStatus == NO_ERROR) { | 
|  | 3267 | return event; | 
|  | 3268 | } | 
|  | 3269 | } | 
|  | 3270 | for (size_t i = 0; i < mRecordThreads.size(); i++) { | 
|  | 3271 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); | 
|  | 3272 | if (recStatus == NO_ERROR) { | 
|  | 3273 | return event; | 
|  | 3274 | } | 
|  | 3275 | } | 
|  | 3276 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { | 
|  | 3277 | mPendingSyncEvents.add(event); | 
|  | 3278 | } else { | 
|  | 3279 | ALOGV("createSyncEvent() invalid event %d", event->type()); | 
|  | 3280 | event.clear(); | 
|  | 3281 | } | 
|  | 3282 | return event; | 
|  | 3283 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3284 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3285 | // ---------------------------------------------------------------------------- | 
|  | 3286 | //  Effect management | 
|  | 3287 | // ---------------------------------------------------------------------------- | 
|  | 3288 |  | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3289 | sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() { | 
|  | 3290 | return mEffectsFactoryHal; | 
|  | 3291 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3292 |  | 
| Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 3293 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3294 | { | 
|  | 3295 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3296 | if (mEffectsFactoryHal.get()) { | 
|  | 3297 | return mEffectsFactoryHal->queryNumberEffects(numEffects); | 
|  | 3298 | } else { | 
|  | 3299 | return -ENODEV; | 
|  | 3300 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3301 | } | 
|  | 3302 |  | 
| Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 3303 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3304 | { | 
|  | 3305 | Mutex::Autolock _l(mLock); | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3306 | if (mEffectsFactoryHal.get()) { | 
|  | 3307 | return mEffectsFactoryHal->getDescriptor(index, descriptor); | 
|  | 3308 | } else { | 
|  | 3309 | return -ENODEV; | 
|  | 3310 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3311 | } | 
|  | 3312 |  | 
| Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 3313 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, | 
| Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3314 | const effect_uuid_t *pTypeUuid, | 
|  | 3315 | uint32_t preferredTypeFlag, | 
|  | 3316 | effect_descriptor_t *descriptor) const | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3317 | { | 
| Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3318 | if (pUuid == NULL || pTypeUuid == NULL || descriptor == NULL) { | 
|  | 3319 | return BAD_VALUE; | 
|  | 3320 | } | 
|  | 3321 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3322 | Mutex::Autolock _l(mLock); | 
| Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3323 |  | 
|  | 3324 | if (!mEffectsFactoryHal.get()) { | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3325 | return -ENODEV; | 
|  | 3326 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3327 |  | 
| Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3328 | status_t status = NO_ERROR; | 
|  | 3329 | if (!EffectsFactoryHalInterface::isNullUuid(pUuid)) { | 
|  | 3330 | // If uuid is specified, request effect descriptor from that. | 
|  | 3331 | status = mEffectsFactoryHal->getDescriptor(pUuid, descriptor); | 
|  | 3332 | } else if (!EffectsFactoryHalInterface::isNullUuid(pTypeUuid)) { | 
|  | 3333 | // If uuid is not specified, look for an available implementation | 
|  | 3334 | // of the required type instead. | 
|  | 3335 |  | 
|  | 3336 | // Use a temporary descriptor to avoid modifying |descriptor| in the failure case. | 
|  | 3337 | effect_descriptor_t desc; | 
|  | 3338 | desc.flags = 0; // prevent compiler warning | 
|  | 3339 |  | 
|  | 3340 | uint32_t numEffects = 0; | 
|  | 3341 | status = mEffectsFactoryHal->queryNumberEffects(&numEffects); | 
|  | 3342 | if (status < 0) { | 
|  | 3343 | ALOGW("getEffectDescriptor() error %d from FactoryHal queryNumberEffects", status); | 
|  | 3344 | return status; | 
|  | 3345 | } | 
|  | 3346 |  | 
|  | 3347 | bool found = false; | 
|  | 3348 | for (uint32_t i = 0; i < numEffects; i++) { | 
|  | 3349 | status = mEffectsFactoryHal->getDescriptor(i, &desc); | 
|  | 3350 | if (status < 0) { | 
|  | 3351 | ALOGW("getEffectDescriptor() error %d from FactoryHal getDescriptor", status); | 
|  | 3352 | continue; | 
|  | 3353 | } | 
|  | 3354 | if (memcmp(&desc.type, pTypeUuid, sizeof(effect_uuid_t)) == 0) { | 
|  | 3355 | // If matching type found save effect descriptor. | 
|  | 3356 | found = true; | 
|  | 3357 | *descriptor = desc; | 
|  | 3358 |  | 
|  | 3359 | // If there's no preferred flag or this descriptor matches the preferred | 
|  | 3360 | // flag, success! If this descriptor doesn't match the preferred | 
|  | 3361 | // flag, continue enumeration in case a better matching version of this | 
|  | 3362 | // effect type is available. Note that this means if no effect with a | 
|  | 3363 | // correct flag is found, the descriptor returned will correspond to the | 
|  | 3364 | // last effect that at least had a matching type uuid (if any). | 
|  | 3365 | if (preferredTypeFlag == EFFECT_FLAG_TYPE_MASK || | 
|  | 3366 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == preferredTypeFlag) { | 
|  | 3367 | break; | 
|  | 3368 | } | 
|  | 3369 | } | 
|  | 3370 | } | 
|  | 3371 |  | 
|  | 3372 | if (!found) { | 
|  | 3373 | status = NAME_NOT_FOUND; | 
|  | 3374 | ALOGW("getEffectDescriptor(): Effect not found by type."); | 
|  | 3375 | } | 
|  | 3376 | } else { | 
|  | 3377 | status = BAD_VALUE; | 
|  | 3378 | ALOGE("getEffectDescriptor(): Either uuid or type uuid must be non-null UUIDs."); | 
|  | 3379 | } | 
|  | 3380 | return status; | 
|  | 3381 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3382 |  | 
| Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 3383 | sp<IEffect> AudioFlinger::createEffect( | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3384 | effect_descriptor_t *pDesc, | 
|  | 3385 | const sp<IEffectClient>& effectClient, | 
|  | 3386 | int32_t priority, | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3387 | audio_io_handle_t io, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3388 | audio_session_t sessionId, | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3389 | const AudioDeviceTypeAddr& device, | 
| Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 3390 | const String16& opPackageName, | 
| Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3391 | pid_t pid, | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3392 | status_t *status, | 
|  | 3393 | int *id, | 
|  | 3394 | int *enabled) | 
|  | 3395 | { | 
|  | 3396 | status_t lStatus = NO_ERROR; | 
|  | 3397 | sp<EffectHandle> handle; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3398 | effect_descriptor_t desc; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3399 |  | 
| Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3400 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3401 | if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) { | 
| Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3402 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
|  | 3403 | ALOGW_IF(pid != -1 && pid != callingPid, | 
|  | 3404 | "%s uid %d pid %d tried to pass itself off as pid %d", | 
|  | 3405 | __func__, callingUid, callingPid, pid); | 
|  | 3406 | pid = callingPid; | 
|  | 3407 | } | 
|  | 3408 |  | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3409 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p", | 
|  | 3410 | pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get()); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3411 |  | 
|  | 3412 | if (pDesc == NULL) { | 
|  | 3413 | lStatus = BAD_VALUE; | 
|  | 3414 | goto Exit; | 
|  | 3415 | } | 
|  | 3416 |  | 
| Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3417 | if (mEffectsFactoryHal == 0) { | 
| Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3418 | ALOGE("%s: no effects factory hal", __func__); | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3419 | lStatus = NO_INIT; | 
|  | 3420 | goto Exit; | 
|  | 3421 | } | 
|  | 3422 |  | 
| Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3423 | // check audio settings permission for global effects | 
|  | 3424 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
|  | 3425 | if (!settingsAllowed()) { | 
|  | 3426 | ALOGE("%s: no permission for AUDIO_SESSION_OUTPUT_MIX", __func__); | 
|  | 3427 | lStatus = PERMISSION_DENIED; | 
|  | 3428 | goto Exit; | 
|  | 3429 | } | 
|  | 3430 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { | 
|  | 3431 | if (!isAudioServerUid(callingUid)) { | 
|  | 3432 | ALOGE("%s: only APM can create using AUDIO_SESSION_OUTPUT_STAGE", __func__); | 
|  | 3433 | lStatus = PERMISSION_DENIED; | 
|  | 3434 | goto Exit; | 
|  | 3435 | } | 
|  | 3436 |  | 
|  | 3437 | if (io == AUDIO_IO_HANDLE_NONE) { | 
|  | 3438 | ALOGE("%s: APM must specify output when using AUDIO_SESSION_OUTPUT_STAGE", __func__); | 
|  | 3439 | lStatus = BAD_VALUE; | 
|  | 3440 | goto Exit; | 
|  | 3441 | } | 
| Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3442 | } else if (sessionId == AUDIO_SESSION_DEVICE) { | 
|  | 3443 | if (!modifyDefaultAudioEffectsAllowed(pid, callingUid)) { | 
|  | 3444 | ALOGE("%s: device effect permission denied for uid %d", __func__, callingUid); | 
|  | 3445 | lStatus = PERMISSION_DENIED; | 
|  | 3446 | goto Exit; | 
|  | 3447 | } | 
| Eric Laurent | fefebb5 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 3448 | if (io != AUDIO_IO_HANDLE_NONE) { | 
|  | 3449 | ALOGE("%s: io handle should not be specified for device effect", __func__); | 
|  | 3450 | lStatus = BAD_VALUE; | 
|  | 3451 | goto Exit; | 
|  | 3452 | } | 
| Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3453 | } else { | 
|  | 3454 | // general sessionId. | 
|  | 3455 |  | 
|  | 3456 | if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { | 
|  | 3457 | ALOGE("%s: invalid sessionId %d", __func__, sessionId); | 
|  | 3458 | lStatus = BAD_VALUE; | 
|  | 3459 | goto Exit; | 
|  | 3460 | } | 
|  | 3461 |  | 
|  | 3462 | // TODO: should we check if the callingUid (limited to pid) is in mAudioSessionRefs | 
|  | 3463 | // to prevent creating an effect when one doesn't actually have track with that session? | 
|  | 3464 | } | 
|  | 3465 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3466 | { | 
| Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3467 | // Get the full effect descriptor from the uuid/type. | 
|  | 3468 | // If the session is the output mix, prefer an auxiliary effect, | 
|  | 3469 | // otherwise no preference. | 
|  | 3470 | uint32_t preferredType = (sessionId == AUDIO_SESSION_OUTPUT_MIX ? | 
|  | 3471 | EFFECT_FLAG_TYPE_AUXILIARY : EFFECT_FLAG_TYPE_MASK); | 
|  | 3472 | lStatus = getEffectDescriptor(&pDesc->uuid, &pDesc->type, preferredType, &desc); | 
|  | 3473 | if (lStatus < 0) { | 
|  | 3474 | ALOGW("createEffect() error %d from getEffectDescriptor", lStatus); | 
|  | 3475 | goto Exit; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3476 | } | 
|  | 3477 |  | 
|  | 3478 | // Do not allow auxiliary effects on a session different from 0 (output mix) | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 3479 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3480 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { | 
|  | 3481 | lStatus = INVALID_OPERATION; | 
|  | 3482 | goto Exit; | 
|  | 3483 | } | 
|  | 3484 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3485 | // check recording permission for visualizer | 
|  | 3486 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && | 
| Svet Ganov | 6e64137 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 3487 | // TODO: Do we need to start/stop op - i.e. is there recording being performed? | 
| Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3488 | !recordingAllowed(opPackageName, pid, callingUid)) { | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3489 | lStatus = PERMISSION_DENIED; | 
|  | 3490 | goto Exit; | 
|  | 3491 | } | 
|  | 3492 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3493 | // return effect descriptor | 
| Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 3494 | *pDesc = desc; | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 3495 | if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) { | 
| Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 3496 | // if the output returned by getOutputForEffect() is removed before we lock the | 
|  | 3497 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it | 
|  | 3498 | // and we will exit safely | 
|  | 3499 | io = AudioSystem::getOutputForEffect(&desc); | 
|  | 3500 | ALOGV("createEffect got output %d", io); | 
|  | 3501 | } | 
|  | 3502 |  | 
|  | 3503 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3504 |  | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3505 | if (sessionId == AUDIO_SESSION_DEVICE) { | 
|  | 3506 | sp<Client> client = registerPid(pid); | 
|  | 3507 | ALOGV("%s device type %d address %s", __func__, device.mType, device.getAddress()); | 
|  | 3508 | handle = mDeviceEffectManager.createEffect_l( | 
|  | 3509 | &desc, device, client, effectClient, mPatchPanel.patches_l(), | 
|  | 3510 | enabled, &lStatus); | 
|  | 3511 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
|  | 3512 | // remove local strong reference to Client with mClientLock held | 
|  | 3513 | Mutex::Autolock _cl(mClientLock); | 
|  | 3514 | client.clear(); | 
|  | 3515 | } else { | 
|  | 3516 | // handle must be valid here, but check again to be safe. | 
|  | 3517 | if (handle.get() != nullptr && id != nullptr) *id = handle->id(); | 
|  | 3518 | } | 
|  | 3519 | goto Register; | 
|  | 3520 | } | 
|  | 3521 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3522 | // If output is not specified try to find a matching audio session ID in one of the | 
|  | 3523 | // output threads. | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3524 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX | 
|  | 3525 | // because of code checking output when entering the function. | 
| Andy Hung | 444bb55 | 2019-03-14 17:06:39 -0700 | [diff] [blame] | 3526 | // Note: io is never AUDIO_IO_HANDLE_NONE when creating an effect on an input by APM. | 
|  | 3527 | // An AudioEffect created from the Java API will have io as AUDIO_IO_HANDLE_NONE. | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 3528 | if (io == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 3529 | // look for the thread where the specified audio session is present | 
| Andy Hung | e778c42 | 2019-03-14 15:04:30 -0700 | [diff] [blame] | 3530 | io = findIoHandleBySessionId_l(sessionId, mPlaybackThreads); | 
|  | 3531 | if (io == AUDIO_IO_HANDLE_NONE) { | 
|  | 3532 | io = findIoHandleBySessionId_l(sessionId, mRecordThreads); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3533 | } | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3534 | if (io == AUDIO_IO_HANDLE_NONE) { | 
| Andy Hung | e778c42 | 2019-03-14 15:04:30 -0700 | [diff] [blame] | 3535 | io = findIoHandleBySessionId_l(sessionId, mMmapThreads); | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3536 | } | 
| Andy Hung | 444bb55 | 2019-03-14 17:06:39 -0700 | [diff] [blame] | 3537 |  | 
|  | 3538 | // If you wish to create a Record preprocessing AudioEffect in Java, | 
|  | 3539 | // you MUST create an AudioRecord first and keep it alive so it is picked up above. | 
|  | 3540 | // Otherwise it will fail when created on a Playback thread by legacy | 
|  | 3541 | // handling below.  Ditto with Mmap, the associated Mmap track must be created | 
|  | 3542 | // before creating the AudioEffect or the io handle must be specified. | 
|  | 3543 | // | 
|  | 3544 | // Detect if the effect is created after an AudioRecord is destroyed. | 
|  | 3545 | if (getOrphanEffectChain_l(sessionId).get() != nullptr) { | 
|  | 3546 | ALOGE("%s: effect %s with no specified io handle is denied because the AudioRecord" | 
|  | 3547 | " for session %d no longer exists", | 
|  | 3548 | __func__, desc.name, sessionId); | 
|  | 3549 | lStatus = PERMISSION_DENIED; | 
|  | 3550 | goto Exit; | 
|  | 3551 | } | 
|  | 3552 |  | 
|  | 3553 | // Legacy handling of creating an effect on an expired or made-up | 
|  | 3554 | // session id.  We think that it is a Playback effect. | 
|  | 3555 | // | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3556 | // If no output thread contains the requested session ID, default to | 
|  | 3557 | // first output. The effect chain will be moved to the correct output | 
|  | 3558 | // thread when a track with the same session ID is created | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 3559 | if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) { | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3560 | io = mPlaybackThreads.keyAt(0); | 
|  | 3561 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3562 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); | 
| Andy Hung | 1631f06 | 2019-03-12 19:39:03 -0700 | [diff] [blame] | 3563 | } else if (checkPlaybackThread_l(io) != nullptr) { | 
|  | 3564 | // allow only one effect chain per sessionId on mPlaybackThreads. | 
|  | 3565 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3566 | const audio_io_handle_t checkIo = mPlaybackThreads.keyAt(i); | 
|  | 3567 | if (io == checkIo) continue; | 
|  | 3568 | const uint32_t sessionType = | 
|  | 3569 | mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId); | 
|  | 3570 | if ((sessionType & ThreadBase::EFFECT_SESSION) != 0) { | 
|  | 3571 | ALOGE("%s: effect %s io %d denied because session %d effect exists on io %d", | 
|  | 3572 | __func__, desc.name, (int)io, (int)sessionId, (int)checkIo); | 
|  | 3573 | android_errorWriteLog(0x534e4554, "123237974"); | 
|  | 3574 | lStatus = BAD_VALUE; | 
|  | 3575 | goto Exit; | 
|  | 3576 | } | 
|  | 3577 | } | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3578 | } | 
|  | 3579 | ThreadBase *thread = checkRecordThread_l(io); | 
|  | 3580 | if (thread == NULL) { | 
|  | 3581 | thread = checkPlaybackThread_l(io); | 
|  | 3582 | if (thread == NULL) { | 
| Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3583 | thread = checkMmapThread_l(io); | 
|  | 3584 | if (thread == NULL) { | 
|  | 3585 | ALOGE("createEffect() unknown output thread"); | 
|  | 3586 | lStatus = BAD_VALUE; | 
|  | 3587 | goto Exit; | 
|  | 3588 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3589 | } | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3590 | } else { | 
|  | 3591 | // Check if one effect chain was awaiting for an effect to be created on this | 
|  | 3592 | // session and used it instead of creating a new one. | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3593 | sp<EffectChain> chain = getOrphanEffectChain_l(sessionId); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3594 | if (chain != 0) { | 
| Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 3595 | Mutex::Autolock _l(thread->mLock); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3596 | thread->addEffectChain_l(chain); | 
|  | 3597 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3598 | } | 
| Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3599 |  | 
| Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3600 | sp<Client> client = registerPid(pid); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3601 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3602 | // create effect on selected output thread | 
| Eric Laurent | a20c4e9 | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3603 | bool pinned = !audio_is_global_session(sessionId) && isSessionAcquired_l(sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3604 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3605 | &desc, enabled, &lStatus, pinned); | 
| haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3606 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { | 
| Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 3607 | // remove local strong reference to Client with mClientLock held | 
|  | 3608 | Mutex::Autolock _cl(mClientLock); | 
|  | 3609 | client.clear(); | 
| haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3610 | } else { | 
|  | 3611 | // handle must be valid here, but check again to be safe. | 
|  | 3612 | if (handle.get() != nullptr && id != nullptr) *id = handle->id(); | 
| Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 3613 | } | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3614 | } | 
|  | 3615 |  | 
| Eric Laurent | 9b2064c | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3616 | Register: | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3617 | if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) { | 
|  | 3618 | // Check CPU and memory usage | 
| Eric Laurent | e0b9a36 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 3619 | sp<EffectBase> effect = handle->effect().promote(); | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3620 | if (effect != nullptr) { | 
|  | 3621 | status_t rStatus = effect->updatePolicyState(); | 
|  | 3622 | if (rStatus != NO_ERROR) { | 
|  | 3623 | lStatus = rStatus; | 
|  | 3624 | } | 
|  | 3625 | } | 
|  | 3626 | } else { | 
| haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3627 | handle.clear(); | 
|  | 3628 | } | 
|  | 3629 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3630 | Exit: | 
| Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 3631 | *status = lStatus; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3632 | return handle; | 
|  | 3633 | } | 
|  | 3634 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3635 | status_t AudioFlinger::moveEffects(audio_session_t sessionId, audio_io_handle_t srcOutput, | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3636 | audio_io_handle_t dstOutput) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3637 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3638 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3639 | sessionId, srcOutput, dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3640 | Mutex::Autolock _l(mLock); | 
|  | 3641 | if (srcOutput == dstOutput) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3642 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3643 | return NO_ERROR; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3644 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3645 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); | 
|  | 3646 | if (srcThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3647 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3648 | return BAD_VALUE; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3649 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3650 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); | 
|  | 3651 | if (dstThread == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3652 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3653 | return BAD_VALUE; | 
|  | 3654 | } | 
|  | 3655 |  | 
|  | 3656 | Mutex::Autolock _dl(dstThread->mLock); | 
|  | 3657 | Mutex::Autolock _sl(srcThread->mLock); | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3658 | return moveEffectChain_l(sessionId, srcThread, dstThread); | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3659 | } | 
|  | 3660 |  | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 3661 |  | 
|  | 3662 | void AudioFlinger::setEffectSuspended(int effectId, | 
|  | 3663 | audio_session_t sessionId, | 
|  | 3664 | bool suspended) | 
|  | 3665 | { | 
|  | 3666 | Mutex::Autolock _l(mLock); | 
|  | 3667 |  | 
|  | 3668 | sp<ThreadBase> thread = getEffectThread_l(sessionId, effectId); | 
|  | 3669 | if (thread == nullptr) { | 
|  | 3670 | return; | 
|  | 3671 | } | 
|  | 3672 | Mutex::Autolock _sl(thread->mLock); | 
|  | 3673 | sp<EffectModule> effect = thread->getEffect_l(sessionId, effectId); | 
|  | 3674 | thread->setEffectSuspended_l(&effect->desc().type, suspended, sessionId); | 
|  | 3675 | } | 
|  | 3676 |  | 
|  | 3677 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3678 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3679 | status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3680 | AudioFlinger::PlaybackThread *srcThread, | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3681 | AudioFlinger::PlaybackThread *dstThread) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3682 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3683 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3684 | sessionId, srcThread, dstThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3685 |  | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3686 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3687 | if (chain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3688 | ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p", | 
| Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3689 | sessionId, srcThread); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3690 | return INVALID_OPERATION; | 
|  | 3691 | } | 
|  | 3692 |  | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 3693 | // Check whether the destination thread and all effects in the chain are compatible | 
|  | 3694 | if (!chain->isCompatibleWithThread_l(dstThread)) { | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3695 | ALOGW("moveEffectChain_l() effect chain failed because" | 
| Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 3696 | " destination thread %p is not compatible with effects in the chain", | 
|  | 3697 | dstThread); | 
| Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3698 | return INVALID_OPERATION; | 
|  | 3699 | } | 
|  | 3700 |  | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3701 | // remove chain first. This is useful only if reconfiguring effect chain on same output thread, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3702 | // so that a new chain is created with correct parameters when first effect is added. This is | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 3703 | // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3704 | // removed. | 
|  | 3705 | srcThread->removeEffectChain_l(chain); | 
|  | 3706 |  | 
|  | 3707 | // transfer all effects one by one so that new effect chain is created on new thread with | 
|  | 3708 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3709 | sp<EffectChain> dstChain; | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3710 | uint32_t strategy = 0; // prevent compiler warning | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3711 | sp<EffectModule> effect = chain->getEffectFromId_l(0); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3712 | Vector< sp<EffectModule> > removed; | 
|  | 3713 | status_t status = NO_ERROR; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3714 | while (effect != 0) { | 
|  | 3715 | srcThread->removeEffect_l(effect); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3716 | removed.add(effect); | 
|  | 3717 | status = dstThread->addEffect_l(effect); | 
|  | 3718 | if (status != NO_ERROR) { | 
|  | 3719 | break; | 
|  | 3720 | } | 
| Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 3721 | // removeEffect_l() has stopped the effect if it was active so it must be restarted | 
|  | 3722 | if (effect->state() == EffectModule::ACTIVE || | 
|  | 3723 | effect->state() == EffectModule::STOPPING) { | 
|  | 3724 | effect->start(); | 
|  | 3725 | } | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3726 | // if the move request is not received from audio policy manager, the effect must be | 
|  | 3727 | // re-registered with the new strategy and output | 
|  | 3728 | if (dstChain == 0) { | 
| Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3729 | dstChain = effect->callback()->chain().promote(); | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3730 | if (dstChain == 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3731 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3732 | status = NO_INIT; | 
|  | 3733 | break; | 
| Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3734 | } | 
|  | 3735 | strategy = dstChain->strategy(); | 
|  | 3736 | } | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3737 | effect = chain->getEffectFromId_l(0); | 
|  | 3738 | } | 
|  | 3739 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3740 | if (status != NO_ERROR) { | 
|  | 3741 | for (size_t i = 0; i < removed.size(); i++) { | 
|  | 3742 | srcThread->addEffect_l(removed[i]); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3743 | } | 
|  | 3744 | } | 
|  | 3745 |  | 
|  | 3746 | return status; | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3747 | } | 
|  | 3748 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3749 | status_t AudioFlinger::moveAuxEffectToIo(int EffectId, | 
|  | 3750 | const sp<PlaybackThread>& dstThread, | 
|  | 3751 | sp<PlaybackThread> *srcThread) | 
|  | 3752 | { | 
|  | 3753 | status_t status = NO_ERROR; | 
|  | 3754 | Mutex::Autolock _l(mLock); | 
| Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 3755 | sp<PlaybackThread> thread = | 
|  | 3756 | static_cast<PlaybackThread *>(getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId).get()); | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3757 |  | 
|  | 3758 | if (EffectId != 0 && thread != 0 && dstThread != thread.get()) { | 
|  | 3759 | Mutex::Autolock _dl(dstThread->mLock); | 
|  | 3760 | Mutex::Autolock _sl(thread->mLock); | 
|  | 3761 | sp<EffectChain> srcChain = thread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
|  | 3762 | sp<EffectChain> dstChain; | 
|  | 3763 | if (srcChain == 0) { | 
|  | 3764 | return INVALID_OPERATION; | 
|  | 3765 | } | 
|  | 3766 |  | 
|  | 3767 | sp<EffectModule> effect = srcChain->getEffectFromId_l(EffectId); | 
|  | 3768 | if (effect == 0) { | 
|  | 3769 | return INVALID_OPERATION; | 
|  | 3770 | } | 
|  | 3771 | thread->removeEffect_l(effect); | 
|  | 3772 | status = dstThread->addEffect_l(effect); | 
|  | 3773 | if (status != NO_ERROR) { | 
|  | 3774 | thread->addEffect_l(effect); | 
|  | 3775 | status = INVALID_OPERATION; | 
|  | 3776 | goto Exit; | 
|  | 3777 | } | 
|  | 3778 |  | 
| Eric Laurent | 5d88539 | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3779 | dstChain = effect->callback()->chain().promote(); | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3780 | if (dstChain == 0) { | 
|  | 3781 | thread->addEffect_l(effect); | 
|  | 3782 | status = INVALID_OPERATION; | 
|  | 3783 | } | 
|  | 3784 |  | 
|  | 3785 | Exit: | 
|  | 3786 | // removeEffect_l() has stopped the effect if it was active so it must be restarted | 
|  | 3787 | if (effect->state() == EffectModule::ACTIVE || | 
|  | 3788 | effect->state() == EffectModule::STOPPING) { | 
|  | 3789 | effect->start(); | 
|  | 3790 | } | 
|  | 3791 | } | 
|  | 3792 |  | 
|  | 3793 | if (status == NO_ERROR && srcThread != nullptr) { | 
|  | 3794 | *srcThread = thread; | 
|  | 3795 | } | 
|  | 3796 | return status; | 
|  | 3797 | } | 
|  | 3798 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3799 | bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3800 | { | 
|  | 3801 | if (mGlobalEffectEnableTime != 0 && | 
|  | 3802 | ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { | 
|  | 3803 | return true; | 
|  | 3804 | } | 
|  | 3805 |  | 
|  | 3806 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3807 | sp<EffectChain> ec = | 
|  | 3808 | mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3809 | if (ec != 0 && ec->isNonOffloadableEnabled()) { | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3810 | return true; | 
|  | 3811 | } | 
|  | 3812 | } | 
|  | 3813 | return false; | 
|  | 3814 | } | 
|  | 3815 |  | 
| Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3816 | void AudioFlinger::onNonOffloadableGlobalEffectEnable() | 
| Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3817 | { | 
|  | 3818 | Mutex::Autolock _l(mLock); | 
|  | 3819 |  | 
|  | 3820 | mGlobalEffectEnableTime = systemTime(); | 
|  | 3821 |  | 
|  | 3822 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { | 
|  | 3823 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); | 
|  | 3824 | if (t->mType == ThreadBase::OFFLOAD) { | 
|  | 3825 | t->invalidateTracks(AUDIO_STREAM_MUSIC); | 
|  | 3826 | } | 
|  | 3827 | } | 
|  | 3828 |  | 
|  | 3829 | } | 
|  | 3830 |  | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3831 | status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain) | 
|  | 3832 | { | 
| Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 3833 | // clear possible suspended state before parking the chain so that it starts in default state | 
|  | 3834 | // when attached to a new record thread | 
|  | 3835 | chain->setEffectSuspended_l(FX_IID_AEC, false); | 
|  | 3836 | chain->setEffectSuspended_l(FX_IID_NS, false); | 
|  | 3837 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3838 | audio_session_t session = chain->sessionId(); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3839 | ssize_t index = mOrphanEffectChains.indexOfKey(session); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3840 | ALOGV("putOrphanEffectChain_l session %d index %zd", session, index); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3841 | if (index >= 0) { | 
|  | 3842 | ALOGW("putOrphanEffectChain_l chain for session %d already present", session); | 
|  | 3843 | return ALREADY_EXISTS; | 
|  | 3844 | } | 
|  | 3845 | mOrphanEffectChains.add(session, chain); | 
|  | 3846 | return NO_ERROR; | 
|  | 3847 | } | 
|  | 3848 |  | 
|  | 3849 | sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session) | 
|  | 3850 | { | 
|  | 3851 | sp<EffectChain> chain; | 
|  | 3852 | ssize_t index = mOrphanEffectChains.indexOfKey(session); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3853 | ALOGV("getOrphanEffectChain_l session %d index %zd", session, index); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3854 | if (index >= 0) { | 
|  | 3855 | chain = mOrphanEffectChains.valueAt(index); | 
|  | 3856 | mOrphanEffectChains.removeItemsAt(index); | 
|  | 3857 | } | 
|  | 3858 | return chain; | 
|  | 3859 | } | 
|  | 3860 |  | 
|  | 3861 | bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect) | 
|  | 3862 | { | 
|  | 3863 | Mutex::Autolock _l(mLock); | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3864 | audio_session_t session = effect->sessionId(); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3865 | ssize_t index = mOrphanEffectChains.indexOfKey(session); | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3866 | ALOGV("updateOrphanEffectChains session %d index %zd", session, index); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3867 | if (index >= 0) { | 
|  | 3868 | sp<EffectChain> chain = mOrphanEffectChains.valueAt(index); | 
| Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3869 | if (chain->removeEffect_l(effect, true) == 0) { | 
| Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3870 | ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index); | 
| Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3871 | mOrphanEffectChains.removeItemsAt(index); | 
|  | 3872 | } | 
|  | 3873 | return true; | 
|  | 3874 | } | 
|  | 3875 | return false; | 
|  | 3876 | } | 
|  | 3877 |  | 
|  | 3878 |  | 
| Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3879 | // ---------------------------------------------------------------------------- | 
|  | 3880 |  | 
|  | 3881 | status_t AudioFlinger::onTransact( | 
|  | 3882 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 3883 | { | 
|  | 3884 | return BnAudioFlinger::onTransact(code, data, reply, flags); | 
|  | 3885 | } | 
|  | 3886 |  | 
| Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3887 | } // namespace android |