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