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