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