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