Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 1 | /* |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
jiabin | a392a55 | 2019-09-23 10:34:20 -0700 | [diff] [blame] | 22 | // Define AUDIO_ARRAYS_STATIC_CHECK to check all audio arrays are correct |
| 23 | #define AUDIO_ARRAYS_STATIC_CHECK 1 |
| 24 | |
Glenn Kasten | 153b9fe | 2013-07-15 11:23:36 -0700 | [diff] [blame] | 25 | #include "Configuration.h" |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 26 | #include <dirent.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 27 | #include <math.h> |
| 28 | #include <signal.h> |
Eric Tan | 7b65115 | 2018-07-13 10:17:19 -0700 | [diff] [blame] | 29 | #include <string> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 30 | #include <sys/time.h> |
| 31 | #include <sys/resource.h> |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 32 | #include <thread> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | |
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 | 42896a0 | 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 | 00abf0d | 2020-04-22 19:28:22 -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 | 88b30d2 | 2020-03-09 19:43:13 +0000 | [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 | b82e6b7 | 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 | 42896a0 | 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 | 88b30d2 | 2020-03-09 19:43:13 +0000 | [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 | 42896a0 | 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, |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [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 | b82e6b7 | 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 | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 414 | AutoMutex lock(mHardwareLock); |
Eric Laurent | b82e6b7 | 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 | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 424 | AutoMutex lock(mHardwareLock); |
Eric Laurent | b82e6b7 | 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 | 4381040 | 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 | 00abf0d | 2020-04-22 19:28:22 -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 | 4381040 | 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 | b82e6b7 | 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 | { |
Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 687 | // If there is no memory allocated for logs, return a dummy writer that does nothing. |
| 688 | // Similarly if we can't contact the media.log service, also return a dummy writer. |
| 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, |
| 714 | // so return a dummy writer |
| 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, |
Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 789 | clientPid, clientUid, &input.config, input.flags, |
| 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 | f6eb4c3 | 2020-02-25 14:06:25 -0800 | [diff] [blame] | 855 | &lStatus, portId, input.audioTrackCallback); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 856 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (track == 0)); |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 857 | // 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] | 858 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 859 | output.afFrameCount = thread->frameCount(); |
| 860 | output.afSampleRate = thread->sampleRate(); |
| 861 | output.afLatencyMs = thread->latency(); |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 862 | output.portId = portId; |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 863 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 864 | if (lStatus == NO_ERROR) { |
| 865 | // Connect secondary outputs. Failure on a secondary output must not imped the primary |
| 866 | // Any secondary output setup failure will lead to a desync between the AP and AF until |
| 867 | // the track is destroyed. |
| 868 | TeePatches teePatches; |
| 869 | for (audio_io_handle_t secondaryOutput : secondaryOutputs) { |
| 870 | PlaybackThread *secondaryThread = checkPlaybackThread_l(secondaryOutput); |
| 871 | if (secondaryThread == NULL) { |
| 872 | ALOGE("no playback thread found for secondary output %d", output.outputId); |
| 873 | continue; |
| 874 | } |
| 875 | |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 876 | size_t sourceFrameCount = thread->frameCount() * output.sampleRate |
| 877 | / thread->sampleRate(); |
| 878 | size_t sinkFrameCount = secondaryThread->frameCount() * output.sampleRate |
| 879 | / secondaryThread->sampleRate(); |
| 880 | // If the secondary output has just been opened, the first secondaryThread write |
| 881 | // will not block as it will fill the empty startup buffer of the HAL, |
| 882 | // so a second sink buffer needs to be ready for the immediate next blocking write. |
| 883 | // Additionally, have a margin of one main thread buffer as the scheduling jitter |
| 884 | // can reorder the writes (eg if thread A&B have the same write intervale, |
| 885 | // the scheduler could schedule AB...BA) |
| 886 | size_t frameCountToBeReady = 2 * sinkFrameCount + sourceFrameCount; |
| 887 | // Total secondary output buffer must be at least as the read frames plus |
| 888 | // the margin of a few buffers on both sides in case the |
| 889 | // threads scheduling has some jitter. |
| 890 | // That value should not impact latency as the secondary track is started before |
| 891 | // its buffer is full, see frameCountToBeReady. |
| 892 | size_t frameCount = frameCountToBeReady + 2 * (sourceFrameCount + sinkFrameCount); |
| 893 | // The frameCount should also not be smaller than the secondary thread min frame |
| 894 | // count |
| 895 | size_t minFrameCount = AudioSystem::calculateMinFrameCount( |
| 896 | [&] { Mutex::Autolock _l(secondaryThread->mLock); |
| 897 | return secondaryThread->latency_l(); }(), |
| 898 | secondaryThread->mNormalFrameCount, |
| 899 | secondaryThread->mSampleRate, |
| 900 | output.sampleRate, |
| 901 | input.speed); |
| 902 | frameCount = std::max(frameCount, minFrameCount); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 903 | |
| 904 | using namespace std::chrono_literals; |
| 905 | auto inChannelMask = audio_channel_mask_out_to_in(input.config.channel_mask); |
| 906 | sp patchRecord = new RecordThread::PatchRecord(nullptr /* thread */, |
| 907 | output.sampleRate, |
| 908 | inChannelMask, |
| 909 | input.config.format, |
| 910 | frameCount, |
| 911 | NULL /* buffer */, |
| 912 | (size_t)0 /* bufferSize */, |
| 913 | AUDIO_INPUT_FLAG_DIRECT, |
| 914 | 0ns /* timeout */); |
| 915 | status_t status = patchRecord->initCheck(); |
| 916 | if (status != NO_ERROR) { |
| 917 | ALOGE("Secondary output patchRecord init failed: %d", status); |
| 918 | continue; |
| 919 | } |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 920 | |
| 921 | // TODO: We could check compatibility of the secondaryThread with the PatchTrack |
| 922 | // for fast usage: thread has fast mixer, sample rate matches, etc.; |
| 923 | // for now, we exclude fast tracks by removing the Fast flag. |
| 924 | const audio_output_flags_t outputFlags = |
| 925 | (audio_output_flags_t)(output.flags & ~AUDIO_OUTPUT_FLAG_FAST); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 926 | sp patchTrack = new PlaybackThread::PatchTrack(secondaryThread, |
| 927 | streamType, |
| 928 | output.sampleRate, |
| 929 | input.config.channel_mask, |
| 930 | input.config.format, |
| 931 | frameCount, |
| 932 | patchRecord->buffer(), |
| 933 | patchRecord->bufferSize(), |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 934 | outputFlags, |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 935 | 0ns /* timeout */, |
| 936 | frameCountToBeReady); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 937 | status = patchTrack->initCheck(); |
| 938 | if (status != NO_ERROR) { |
| 939 | ALOGE("Secondary output patchTrack init failed: %d", status); |
| 940 | continue; |
| 941 | } |
| 942 | teePatches.push_back({patchRecord, patchTrack}); |
| 943 | secondaryThread->addPatchTrack(patchTrack); |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 944 | // In case the downstream patchTrack on the secondaryThread temporarily outlives |
| 945 | // our created track, ensure the corresponding patchRecord is still alive. |
| 946 | patchTrack->setPeerProxy(patchRecord, true /* holdReference */); |
| 947 | patchRecord->setPeerProxy(patchTrack, false /* holdReference */); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 948 | } |
| 949 | track->setTeePatches(std::move(teePatches)); |
| 950 | } |
| 951 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 952 | // move effect chain to this output thread if an effect on same session was waiting |
| 953 | // for a track to be created |
| 954 | if (lStatus == NO_ERROR && effectThread != NULL) { |
Glenn Kasten | 2fc1473 | 2013-08-05 14:58:14 -0700 | [diff] [blame] | 955 | // no risk of deadlock because AudioFlinger::mLock is held |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 956 | Mutex::Autolock _dl(thread->mLock); |
| 957 | Mutex::Autolock _sl(effectThread->mLock); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 958 | if (moveEffectChain_l(sessionId, effectThread, thread) == NO_ERROR) { |
| 959 | effectThreadId = thread->id(); |
| 960 | effectIds = thread->getEffectIds_l(sessionId); |
| 961 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 962 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 963 | |
| 964 | // Look for sync events awaiting for a session to be used. |
Mark Salyzyn | 3ab368e | 2014-04-15 14:55:53 -0700 | [diff] [blame] | 965 | for (size_t i = 0; i < mPendingSyncEvents.size(); i++) { |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 966 | if (mPendingSyncEvents[i]->triggerSession() == sessionId) { |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 967 | if (thread->isValidSyncEvent(mPendingSyncEvents[i])) { |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 968 | if (lStatus == NO_ERROR) { |
Glenn Kasten | d23eedc | 2012-08-02 13:35:47 -0700 | [diff] [blame] | 969 | (void) track->setSyncEvent(mPendingSyncEvents[i]); |
Eric Laurent | 2986460 | 2012-05-08 18:57:51 -0700 | [diff] [blame] | 970 | } else { |
| 971 | mPendingSyncEvents[i]->cancel(); |
| 972 | } |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 973 | mPendingSyncEvents.removeAt(i); |
| 974 | i--; |
| 975 | } |
| 976 | } |
| 977 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 978 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 979 | setAudioHwSyncForSession_l(thread, sessionId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 980 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 981 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 982 | if (lStatus != NO_ERROR) { |
| 983 | // 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] | 984 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 985 | // Don't hold mClientLock when releasing the reference on the track as the |
| 986 | // destructor will acquire it. |
| 987 | { |
| 988 | Mutex::Autolock _cl(mClientLock); |
| 989 | client.clear(); |
| 990 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 991 | track.clear(); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 992 | goto Exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 995 | // effectThreadId is not NONE if an effect chain corresponding to the track session |
| 996 | // was found on another thread and must be moved on this thread |
| 997 | if (effectThreadId != AUDIO_IO_HANDLE_NONE) { |
| 998 | AudioSystem::moveEffectsToIo(effectIds, effectThreadId); |
| 999 | } |
| 1000 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 1001 | // return handle to client |
| 1002 | trackHandle = new TrackHandle(track); |
| 1003 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1004 | Exit: |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1005 | if (lStatus != NO_ERROR && output.outputId != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 1006 | AudioSystem::releaseOutput(portId); |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 1007 | } |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 1008 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1009 | return trackHandle; |
| 1010 | } |
| 1011 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1012 | uint32_t AudioFlinger::sampleRate(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1013 | { |
| 1014 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1015 | ThreadBase *thread = checkThread_l(ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1016 | if (thread == NULL) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1017 | ALOGW("sampleRate() unknown thread %d", ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | return thread->sampleRate(); |
| 1021 | } |
| 1022 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1023 | audio_format_t AudioFlinger::format(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1024 | { |
| 1025 | Mutex::Autolock _l(mLock); |
| 1026 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1027 | if (thread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1028 | ALOGW("format() unknown thread %d", output); |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 1029 | return AUDIO_FORMAT_INVALID; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1030 | } |
| 1031 | return thread->format(); |
| 1032 | } |
| 1033 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1034 | size_t AudioFlinger::frameCount(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1035 | { |
| 1036 | Mutex::Autolock _l(mLock); |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1037 | ThreadBase *thread = checkThread_l(ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1038 | if (thread == NULL) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 1039 | ALOGW("frameCount() unknown thread %d", ioHandle); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1040 | return 0; |
| 1041 | } |
Glenn Kasten | 5891256 | 2012-04-03 10:45:00 -0700 | [diff] [blame] | 1042 | // FIXME currently returns the normal mixer's frame count to avoid confusing legacy callers; |
| 1043 | // should examine all callers and fix them to handle smaller counts |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1044 | return thread->frameCount(); |
| 1045 | } |
| 1046 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 1047 | size_t AudioFlinger::frameCountHAL(audio_io_handle_t ioHandle) const |
| 1048 | { |
| 1049 | Mutex::Autolock _l(mLock); |
| 1050 | ThreadBase *thread = checkThread_l(ioHandle); |
| 1051 | if (thread == NULL) { |
| 1052 | ALOGW("frameCountHAL() unknown thread %d", ioHandle); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | return thread->frameCountHAL(); |
| 1056 | } |
| 1057 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1058 | uint32_t AudioFlinger::latency(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1059 | { |
| 1060 | Mutex::Autolock _l(mLock); |
| 1061 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 1062 | if (thread == NULL) { |
Glenn Kasten | c9b2e20 | 2013-02-26 11:32:32 -0800 | [diff] [blame] | 1063 | ALOGW("latency(): no playback thread found for output handle %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | return thread->latency(); |
| 1067 | } |
| 1068 | |
| 1069 | status_t AudioFlinger::setMasterVolume(float value) |
| 1070 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1071 | status_t ret = initCheck(); |
| 1072 | if (ret != NO_ERROR) { |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1076 | // check calling permissions |
| 1077 | if (!settingsAllowed()) { |
| 1078 | return PERMISSION_DENIED; |
| 1079 | } |
| 1080 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1081 | Mutex::Autolock _l(mLock); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1082 | mMasterVolume = value; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1083 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1084 | // Set master volume in the HALs which support it. |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1085 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1086 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1087 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1088 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1089 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1090 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 1091 | if (dev->canSetMasterVolume()) { |
| 1092 | dev->hwDevice()->setMasterVolume(value); |
| 1093 | } |
| 1094 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 1095 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1096 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1097 | // Now set the master volume in each playback thread. Playback threads |
| 1098 | // assigned to HALs which do not have master volume support will apply |
| 1099 | // master volume during the mix operation. Threads with HALs which do |
| 1100 | // support master volume will simply ignore the setting. |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1101 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1102 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 1103 | continue; |
| 1104 | } |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1105 | mPlaybackThreads.valueAt(i)->setMasterVolume(value); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1106 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1107 | |
| 1108 | return NO_ERROR; |
| 1109 | } |
| 1110 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1111 | status_t AudioFlinger::setMasterBalance(float balance) |
| 1112 | { |
| 1113 | status_t ret = initCheck(); |
| 1114 | if (ret != NO_ERROR) { |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
| 1118 | // check calling permissions |
| 1119 | if (!settingsAllowed()) { |
| 1120 | return PERMISSION_DENIED; |
| 1121 | } |
| 1122 | |
| 1123 | // check range |
| 1124 | if (isnan(balance) || fabs(balance) > 1.f) { |
| 1125 | return BAD_VALUE; |
| 1126 | } |
| 1127 | |
| 1128 | Mutex::Autolock _l(mLock); |
| 1129 | |
| 1130 | // short cut. |
| 1131 | if (mMasterBalance == balance) return NO_ERROR; |
| 1132 | |
| 1133 | mMasterBalance = balance; |
| 1134 | |
| 1135 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 1136 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 1137 | continue; |
| 1138 | } |
| 1139 | mPlaybackThreads.valueAt(i)->setMasterBalance(balance); |
| 1140 | } |
| 1141 | |
| 1142 | return NO_ERROR; |
| 1143 | } |
| 1144 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 1145 | status_t AudioFlinger::setMode(audio_mode_t mode) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1146 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1147 | status_t ret = initCheck(); |
| 1148 | if (ret != NO_ERROR) { |
| 1149 | return ret; |
| 1150 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1151 | |
| 1152 | // check calling permissions |
| 1153 | if (!settingsAllowed()) { |
| 1154 | return PERMISSION_DENIED; |
| 1155 | } |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 1156 | if (uint32_t(mode) >= AUDIO_MODE_CNT) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1157 | ALOGW("Illegal value: setMode(%d)", mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1158 | return BAD_VALUE; |
| 1159 | } |
| 1160 | |
| 1161 | { // scope for the lock |
| 1162 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1163 | if (mPrimaryHardwareDev == nullptr) { |
| 1164 | return INVALID_OPERATION; |
| 1165 | } |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1166 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1167 | mHardwareStatus = AUDIO_HW_SET_MODE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1168 | ret = dev->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1169 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1170 | } |
| 1171 | |
| 1172 | if (NO_ERROR == ret) { |
| 1173 | Mutex::Autolock _l(mLock); |
| 1174 | mMode = mode; |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 1175 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 1176 | mPlaybackThreads.valueAt(i)->setMode(mode); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
| 1182 | status_t AudioFlinger::setMicMute(bool state) |
| 1183 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1184 | status_t ret = initCheck(); |
| 1185 | if (ret != NO_ERROR) { |
| 1186 | return ret; |
| 1187 | } |
| 1188 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1189 | // check calling permissions |
| 1190 | if (!settingsAllowed()) { |
| 1191 | return PERMISSION_DENIED; |
| 1192 | } |
| 1193 | |
| 1194 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1195 | if (mPrimaryHardwareDev == nullptr) { |
| 1196 | return INVALID_OPERATION; |
| 1197 | } |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1198 | sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice(); |
| 1199 | if (primaryDev == nullptr) { |
| 1200 | ALOGW("%s: no primary HAL device", __func__); |
| 1201 | return INVALID_OPERATION; |
| 1202 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1203 | mHardwareStatus = AUDIO_HW_SET_MIC_MUTE; |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1204 | ret = primaryDev->setMicMute(state); |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 1205 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1206 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1207 | if (dev != primaryDev) { |
| 1208 | (void)dev->setMicMute(state); |
Eric Laurent | 2f035f5 | 2014-09-14 12:11:52 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1211 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1212 | 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] | 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | bool AudioFlinger::getMicMute() const |
| 1217 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1218 | status_t ret = initCheck(); |
| 1219 | if (ret != NO_ERROR) { |
| 1220 | return false; |
| 1221 | } |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1222 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1223 | if (mPrimaryHardwareDev == nullptr) { |
| 1224 | return false; |
| 1225 | } |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1226 | sp<DeviceHalInterface> primaryDev = mPrimaryHardwareDev->hwDevice(); |
| 1227 | if (primaryDev == nullptr) { |
| 1228 | ALOGW("%s: no primary HAL device", __func__); |
| 1229 | return false; |
Ricardo Garcia | 3e91b5d | 2015-02-19 10:54:52 -0800 | [diff] [blame] | 1230 | } |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1231 | bool state; |
| 1232 | mHardwareStatus = AUDIO_HW_GET_MIC_MUTE; |
| 1233 | ret = primaryDev->getMicMute(&state); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1234 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 2325bf0 | 2020-04-08 19:38:13 -0700 | [diff] [blame] | 1235 | ALOGE_IF(ret != NO_ERROR, "%s: error %d getting state from HAL", __func__, ret); |
| 1236 | return (ret == NO_ERROR) && state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 1239 | void AudioFlinger::setRecordSilenced(audio_port_handle_t portId, bool silenced) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1240 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 1241 | ALOGV("AudioFlinger::setRecordSilenced(portId:%d, silenced:%d)", portId, silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1242 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1243 | AutoMutex lock(mLock); |
| 1244 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 1245 | mRecordThreads[i]->setRecordSilenced(portId, silenced); |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1246 | } |
| 1247 | for (size_t i = 0; i < mMmapThreads.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 1248 | mMmapThreads[i]->setRecordSilenced(portId, silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1252 | status_t AudioFlinger::setMasterMute(bool muted) |
| 1253 | { |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1254 | status_t ret = initCheck(); |
| 1255 | if (ret != NO_ERROR) { |
| 1256 | return ret; |
| 1257 | } |
| 1258 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1259 | // check calling permissions |
| 1260 | if (!settingsAllowed()) { |
| 1261 | return PERMISSION_DENIED; |
| 1262 | } |
| 1263 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1264 | Mutex::Autolock _l(mLock); |
| 1265 | mMasterMute = muted; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1266 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1267 | // Set master mute in the HALs which support it. |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1268 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1269 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1270 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 1271 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1272 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1273 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 1274 | if (dev->canSetMasterMute()) { |
| 1275 | dev->hwDevice()->setMasterMute(muted); |
| 1276 | } |
| 1277 | mHardwareStatus = AUDIO_HW_IDLE; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1278 | } |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1281 | // Now set the master mute in each playback thread. Playback threads |
| 1282 | // assigned to HALs which do not have master mute support will apply master |
| 1283 | // mute during the mix operation. Threads with HALs which do support master |
| 1284 | // mute will simply ignore the setting. |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1285 | Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l(); |
| 1286 | for (size_t i = 0; i < volumeInterfaces.size(); i++) { |
| 1287 | volumeInterfaces[i]->setMasterMute(muted); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 1288 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1289 | |
| 1290 | return NO_ERROR; |
| 1291 | } |
| 1292 | |
| 1293 | float AudioFlinger::masterVolume() const |
| 1294 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1295 | Mutex::Autolock _l(mLock); |
| 1296 | return masterVolume_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1299 | status_t AudioFlinger::getMasterBalance(float *balance) const |
| 1300 | { |
| 1301 | Mutex::Autolock _l(mLock); |
| 1302 | *balance = getMasterBalance_l(); |
| 1303 | return NO_ERROR; // if called through binder, may return a transactional error |
| 1304 | } |
| 1305 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1306 | bool AudioFlinger::masterMute() const |
| 1307 | { |
Glenn Kasten | 9806710 | 2011-12-13 11:47:54 -0800 | [diff] [blame] | 1308 | Mutex::Autolock _l(mLock); |
| 1309 | return masterMute_l(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1312 | float AudioFlinger::masterVolume_l() const |
| 1313 | { |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 1314 | return mMasterVolume; |
| 1315 | } |
| 1316 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1317 | float AudioFlinger::getMasterBalance_l() const |
| 1318 | { |
| 1319 | return mMasterBalance; |
| 1320 | } |
| 1321 | |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1322 | bool AudioFlinger::masterMute_l() const |
| 1323 | { |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 1324 | return mMasterMute; |
John Grossman | d8f178d | 2012-07-20 14:51:35 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1327 | status_t AudioFlinger::checkStreamType(audio_stream_type_t stream) const |
| 1328 | { |
| 1329 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1330 | ALOGW("checkStreamType() invalid stream %d", stream); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1331 | return BAD_VALUE; |
| 1332 | } |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1333 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); |
| 1334 | if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT && !isAudioServerUid(callerUid)) { |
| 1335 | ALOGW("checkStreamType() uid %d cannot use internal stream type %d", callerUid, stream); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1336 | return PERMISSION_DENIED; |
| 1337 | } |
| 1338 | |
| 1339 | return NO_ERROR; |
| 1340 | } |
| 1341 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1342 | status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value, |
| 1343 | audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1344 | { |
| 1345 | // check calling permissions |
| 1346 | if (!settingsAllowed()) { |
| 1347 | return PERMISSION_DENIED; |
| 1348 | } |
| 1349 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1350 | status_t status = checkStreamType(stream); |
| 1351 | if (status != NO_ERROR) { |
| 1352 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1353 | } |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1354 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1355 | return BAD_VALUE; |
| 1356 | } |
François Gaffie | 9e1533c | 2019-04-11 16:07:36 +0200 | [diff] [blame] | 1357 | LOG_ALWAYS_FATAL_IF(stream == AUDIO_STREAM_PATCH && value != 1.0f, |
| 1358 | "AUDIO_STREAM_PATCH must have full scale volume"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1359 | |
| 1360 | AutoMutex lock(mLock); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1361 | VolumeInterface *volumeInterface = getVolumeInterface_l(output); |
| 1362 | if (volumeInterface == NULL) { |
| 1363 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1364 | } |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1365 | volumeInterface->setStreamVolume(stream, value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1366 | |
| 1367 | return NO_ERROR; |
| 1368 | } |
| 1369 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1370 | status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1371 | { |
| 1372 | // check calling permissions |
| 1373 | if (!settingsAllowed()) { |
| 1374 | return PERMISSION_DENIED; |
| 1375 | } |
| 1376 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1377 | status_t status = checkStreamType(stream); |
| 1378 | if (status != NO_ERROR) { |
| 1379 | return status; |
| 1380 | } |
| 1381 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH, "attempt to mute AUDIO_STREAM_PATCH"); |
| 1382 | |
| 1383 | if (uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1384 | ALOGE("setStreamMute() invalid stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1385 | return BAD_VALUE; |
| 1386 | } |
| 1387 | |
Eric Laurent | 9357520 | 2011-01-18 18:39:02 -0800 | [diff] [blame] | 1388 | AutoMutex lock(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1389 | mStreamTypes[stream].mute = muted; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1390 | Vector<VolumeInterface *> volumeInterfaces = getAllVolumeInterfaces_l(); |
| 1391 | for (size_t i = 0; i < volumeInterfaces.size(); i++) { |
| 1392 | volumeInterfaces[i]->setStreamMute(stream, muted); |
| 1393 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1394 | |
| 1395 | return NO_ERROR; |
| 1396 | } |
| 1397 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1398 | 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] | 1399 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1400 | status_t status = checkStreamType(stream); |
| 1401 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1402 | return 0.0f; |
| 1403 | } |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1404 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1405 | return 0.0f; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 1408 | AutoMutex lock(mLock); |
| 1409 | VolumeInterface *volumeInterface = getVolumeInterface_l(output); |
| 1410 | if (volumeInterface == NULL) { |
| 1411 | return 0.0f; |
| 1412 | } |
| 1413 | |
| 1414 | return volumeInterface->streamVolume(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 1417 | bool AudioFlinger::streamMute(audio_stream_type_t stream) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1418 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1419 | status_t status = checkStreamType(stream); |
| 1420 | if (status != NO_ERROR) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1421 | return true; |
| 1422 | } |
| 1423 | |
Glenn Kasten | 6637baa | 2012-01-09 09:40:36 -0800 | [diff] [blame] | 1424 | AutoMutex lock(mLock); |
| 1425 | return streamMute_l(stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 1428 | |
| 1429 | void AudioFlinger::broacastParametersToRecordThreads_l(const String8& keyValuePairs) |
| 1430 | { |
| 1431 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1432 | mRecordThreads.valueAt(i)->setParameters(keyValuePairs); |
| 1433 | } |
| 1434 | } |
| 1435 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 1436 | void AudioFlinger::updateOutDevicesForRecordThreads_l(const DeviceDescriptorBaseVector& devices) |
| 1437 | { |
| 1438 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1439 | mRecordThreads.valueAt(i)->updateOutDevices(devices); |
| 1440 | } |
| 1441 | } |
| 1442 | |
Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 1443 | // forwardAudioHwSyncToDownstreamPatches_l() must be called with AudioFlinger::mLock held |
| 1444 | void AudioFlinger::forwardParametersToDownstreamPatches_l( |
| 1445 | audio_io_handle_t upStream, const String8& keyValuePairs, |
| 1446 | std::function<bool(const sp<PlaybackThread>&)> useThread) |
| 1447 | { |
| 1448 | std::vector<PatchPanel::SoftwarePatch> swPatches; |
| 1449 | if (mPatchPanel.getDownstreamSoftwarePatches(upStream, &swPatches) != OK) return; |
| 1450 | ALOGV_IF(!swPatches.empty(), "%s found %zu downstream patches for stream ID %d", |
| 1451 | __func__, swPatches.size(), upStream); |
| 1452 | for (const auto& swPatch : swPatches) { |
| 1453 | sp<PlaybackThread> downStream = checkPlaybackThread_l(swPatch.getPlaybackThreadHandle()); |
| 1454 | if (downStream != NULL && (useThread == nullptr || useThread(downStream))) { |
| 1455 | downStream->setParameters(keyValuePairs); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1460 | // Filter reserved keys from setParameters() before forwarding to audio HAL or acting upon. |
| 1461 | // Some keys are used for audio routing and audio path configuration and should be reserved for use |
| 1462 | // by audio policy and audio flinger for functional, privacy and security reasons. |
| 1463 | void AudioFlinger::filterReservedParameters(String8& keyValuePairs, uid_t callingUid) |
| 1464 | { |
| 1465 | static const String8 kReservedParameters[] = { |
| 1466 | String8(AudioParameter::keyRouting), |
| 1467 | String8(AudioParameter::keySamplingRate), |
| 1468 | String8(AudioParameter::keyFormat), |
| 1469 | String8(AudioParameter::keyChannels), |
| 1470 | String8(AudioParameter::keyFrameCount), |
| 1471 | String8(AudioParameter::keyInputSource), |
| 1472 | String8(AudioParameter::keyMonoOutput), |
Mikhail Naganov | f23fcf6 | 2019-07-08 15:28:43 -0700 | [diff] [blame] | 1473 | String8(AudioParameter::keyDeviceConnect), |
| 1474 | String8(AudioParameter::keyDeviceDisconnect), |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1475 | String8(AudioParameter::keyStreamSupportedFormats), |
| 1476 | String8(AudioParameter::keyStreamSupportedChannels), |
| 1477 | String8(AudioParameter::keyStreamSupportedSamplingRates), |
| 1478 | }; |
| 1479 | |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1480 | if (isAudioServerUid(callingUid)) { |
| 1481 | return; // no need to filter if audioserver. |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | AudioParameter param = AudioParameter(keyValuePairs); |
| 1485 | String8 value; |
Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1486 | AudioParameter rejectedParam; |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1487 | for (auto& key : kReservedParameters) { |
| 1488 | if (param.get(key, value) == NO_ERROR) { |
Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1489 | rejectedParam.add(key, value); |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1490 | param.remove(key); |
| 1491 | } |
| 1492 | } |
Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1493 | logFilteredParameters(param.size() + rejectedParam.size(), keyValuePairs, |
| 1494 | rejectedParam.size(), rejectedParam.toString(), callingUid); |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1495 | keyValuePairs = param.toString(); |
| 1496 | } |
| 1497 | |
Kevin Rocard | a0a5d2a | 2018-08-06 15:03:18 -0700 | [diff] [blame] | 1498 | void AudioFlinger::logFilteredParameters(size_t originalKVPSize, const String8& originalKVPs, |
| 1499 | size_t rejectedKVPSize, const String8& rejectedKVPs, |
| 1500 | uid_t callingUid) { |
| 1501 | auto prefix = String8::format("UID %5d", callingUid); |
| 1502 | auto suffix = String8::format("%zu KVP received: %s", originalKVPSize, originalKVPs.c_str()); |
| 1503 | if (rejectedKVPSize != 0) { |
| 1504 | auto error = String8::format("%zu KVP rejected: %s", rejectedKVPSize, rejectedKVPs.c_str()); |
| 1505 | ALOGW("%s: %s, %s, %s", __func__, prefix.c_str(), error.c_str(), suffix.c_str()); |
| 1506 | mRejectedSetParameterLog.log("%s, %s, %s", prefix.c_str(), error.c_str(), suffix.c_str()); |
| 1507 | } else { |
| 1508 | auto& logger = (isServiceUid(callingUid) ? mSystemSetParameterLog : mAppSetParameterLog); |
| 1509 | logger.log("%s, %s", prefix.c_str(), suffix.c_str()); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1513 | status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1514 | { |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1515 | ALOGV("setParameters(): io %d, keyvalue %s, calling pid %d calling uid %d", |
| 1516 | ioHandle, keyValuePairs.string(), |
| 1517 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1518 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1519 | // check calling permissions |
| 1520 | if (!settingsAllowed()) { |
| 1521 | return PERMISSION_DENIED; |
| 1522 | } |
| 1523 | |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1524 | String8 filteredKeyValuePairs = keyValuePairs; |
| 1525 | filterReservedParameters(filteredKeyValuePairs, IPCThreadState::self()->getCallingUid()); |
| 1526 | |
| 1527 | ALOGV("%s: filtered keyvalue %s", __func__, filteredKeyValuePairs.string()); |
| 1528 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1529 | // AUDIO_IO_HANDLE_NONE means the parameters are global to the audio hardware interface |
| 1530 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1531 | Mutex::Autolock _l(mLock); |
Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1532 | // result will remain NO_INIT if no audio device is present |
| 1533 | status_t final_result = NO_INIT; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1534 | { |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1535 | AutoMutex lock(mHardwareLock); |
| 1536 | mHardwareStatus = AUDIO_HW_SET_PARAMETER; |
| 1537 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1538 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1539 | status_t result = dev->setParameters(filteredKeyValuePairs); |
Eric Laurent | d21bcd2 | 2016-09-08 18:25:54 -0700 | [diff] [blame] | 1540 | // return success if at least one audio device accepts the parameters as not all |
| 1541 | // HALs are requested to support all parameters. If no audio device supports the |
| 1542 | // requested parameters, the last error is reported. |
| 1543 | if (final_result != NO_ERROR) { |
| 1544 | final_result = result; |
| 1545 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1546 | } |
| 1547 | mHardwareStatus = AUDIO_HW_IDLE; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1548 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1549 | // 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] | 1550 | AudioParameter param = AudioParameter(filteredKeyValuePairs); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1551 | String8 value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1552 | if (param.get(String8(AudioParameter::keyBtNrec), value) == NO_ERROR) { |
| 1553 | bool btNrecIsOff = (value == AudioParameter::valueOff); |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1554 | if (mBtNrecIsOff.exchange(btNrecIsOff) != btNrecIsOff) { |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1555 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 1556 | mRecordThreads.valueAt(i)->checkBtNrec(); |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1557 | } |
Eric Laurent | 59bd0da | 2011-08-01 09:52:20 -0700 | [diff] [blame] | 1558 | } |
| 1559 | } |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1560 | String8 screenState; |
| 1561 | if (param.get(String8(AudioParameter::keyScreenState), screenState) == NO_ERROR) { |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 1562 | bool isOff = (screenState == AudioParameter::valueOff); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1563 | if (isOff != (AudioFlinger::mScreenState & 1)) { |
| 1564 | AudioFlinger::mScreenState = ((AudioFlinger::mScreenState & ~1) + 2) | isOff; |
Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 1565 | } |
| 1566 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1567 | return final_result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | // hold a strong ref on thread in case closeOutput() or closeInput() is called |
| 1571 | // and the thread is exited once the lock is released |
| 1572 | sp<ThreadBase> thread; |
| 1573 | { |
| 1574 | Mutex::Autolock _l(mLock); |
| 1575 | thread = checkPlaybackThread_l(ioHandle); |
Glenn Kasten | d5903ec | 2012-03-18 10:33:27 -0700 | [diff] [blame] | 1576 | if (thread == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1577 | thread = checkRecordThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1578 | if (thread == 0) { |
| 1579 | thread = checkMmapThread_l(ioHandle); |
| 1580 | } |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 1581 | } else if (thread == primaryPlaybackThread_l()) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1582 | // indicate output device change to all input threads for pre processing |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1583 | AudioParameter param = AudioParameter(filteredKeyValuePairs); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1584 | int value; |
Eric Laurent | 89d94e7 | 2012-03-16 20:37:59 -0700 | [diff] [blame] | 1585 | if ((param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) && |
| 1586 | (value != 0)) { |
Eric Laurent | f1047e8 | 2018-04-16 19:18:20 -0700 | [diff] [blame] | 1587 | broacastParametersToRecordThreads_l(filteredKeyValuePairs); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1588 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1589 | } |
| 1590 | } |
Glenn Kasten | 7378ca5 | 2012-01-20 13:44:40 -0800 | [diff] [blame] | 1591 | if (thread != 0) { |
Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 1592 | status_t result = thread->setParameters(filteredKeyValuePairs); |
| 1593 | forwardParametersToDownstreamPatches_l(thread->id(), filteredKeyValuePairs); |
| 1594 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1595 | } |
| 1596 | return BAD_VALUE; |
| 1597 | } |
| 1598 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1599 | String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1600 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1601 | ALOGVV("getParameters() io %d, keys %s, calling pid %d", |
| 1602 | ioHandle, keys.string(), IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1603 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 1604 | Mutex::Autolock _l(mLock); |
| 1605 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1606 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1607 | String8 out_s8; |
| 1608 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1609 | AutoMutex lock(mHardwareLock); |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1610 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1611 | String8 s; |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1612 | mHardwareStatus = AUDIO_HW_GET_PARAMETER; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1613 | sp<DeviceHalInterface> dev = mAudioHwDevs.valueAt(i)->hwDevice(); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1614 | status_t result = dev->getParameters(keys, &s); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1615 | mHardwareStatus = AUDIO_HW_IDLE; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1616 | if (result == OK) out_s8 += s; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 1617 | } |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1618 | return out_s8; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1621 | ThreadBase *thread = (ThreadBase *)checkPlaybackThread_l(ioHandle); |
| 1622 | if (thread == NULL) { |
| 1623 | thread = (ThreadBase *)checkRecordThread_l(ioHandle); |
| 1624 | if (thread == NULL) { |
| 1625 | thread = (ThreadBase *)checkMmapThread_l(ioHandle); |
| 1626 | if (thread == NULL) { |
Mikhail Naganov | aa165e5 | 2017-07-12 10:39:16 -0700 | [diff] [blame] | 1627 | return String8(""); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1628 | } |
| 1629 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1630 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1631 | return thread->getParameters(keys); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 1634 | size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 1635 | audio_channel_mask_t channelMask) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1636 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1637 | status_t ret = initCheck(); |
| 1638 | if (ret != NO_ERROR) { |
| 1639 | return 0; |
| 1640 | } |
Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1641 | if ((sampleRate == 0) || |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 1642 | !audio_is_valid_format(format) || !audio_has_proportional_frames(format) || |
Eric Laurent | cdf924a | 2016-02-04 15:57:56 -0800 | [diff] [blame] | 1643 | !audio_is_input_channel(channelMask)) { |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1644 | return 0; |
| 1645 | } |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1646 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1647 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1648 | if (mPrimaryHardwareDev == nullptr) { |
| 1649 | return 0; |
| 1650 | } |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1651 | mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1652 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1653 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Judy Hsiao | abab522 | 2019-11-09 18:14:58 +0800 | [diff] [blame] | 1654 | std::vector<audio_channel_mask_t> channelMasks = {channelMask}; |
| 1655 | if (channelMask != AUDIO_CHANNEL_IN_MONO) |
| 1656 | channelMasks.push_back(AUDIO_CHANNEL_IN_MONO); |
| 1657 | if (channelMask != AUDIO_CHANNEL_IN_STEREO) |
| 1658 | channelMasks.push_back(AUDIO_CHANNEL_IN_STEREO); |
| 1659 | |
| 1660 | std::vector<audio_format_t> formats = {format}; |
| 1661 | if (format != AUDIO_FORMAT_PCM_16_BIT) |
| 1662 | formats.push_back(AUDIO_FORMAT_PCM_16_BIT); |
| 1663 | |
| 1664 | std::vector<uint32_t> sampleRates = {sampleRate}; |
| 1665 | static const uint32_t SR_44100 = 44100; |
| 1666 | static const uint32_t SR_48000 = 48000; |
| 1667 | |
| 1668 | if (sampleRate != SR_48000) |
| 1669 | sampleRates.push_back(SR_48000); |
| 1670 | if (sampleRate != SR_44100) |
| 1671 | sampleRates.push_back(SR_44100); |
| 1672 | |
Glenn Kasten | 2b213bc | 2012-02-02 14:05:20 -0800 | [diff] [blame] | 1673 | mHardwareStatus = AUDIO_HW_IDLE; |
Judy Hsiao | abab522 | 2019-11-09 18:14:58 +0800 | [diff] [blame] | 1674 | |
Robin Lee | 9899ff7 | 2019-12-24 22:02:08 +0100 | [diff] [blame] | 1675 | // Change parameters of the configuration each iteration until we find a |
| 1676 | // configuration that the device will support. |
| 1677 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Judy Hsiao | abab522 | 2019-11-09 18:14:58 +0800 | [diff] [blame] | 1678 | for (auto testChannelMask : channelMasks) { |
| 1679 | config.channel_mask = testChannelMask; |
| 1680 | for (auto testFormat : formats) { |
| 1681 | config.format = testFormat; |
| 1682 | for (auto testSampleRate : sampleRates) { |
| 1683 | config.sample_rate = testSampleRate; |
Robin Lee | 9899ff7 | 2019-12-24 22:02:08 +0100 | [diff] [blame] | 1684 | |
Judy Hsiao | abab522 | 2019-11-09 18:14:58 +0800 | [diff] [blame] | 1685 | size_t bytes = 0; |
| 1686 | status_t result = dev->getInputBufferSize(&config, &bytes); |
| 1687 | if (result != OK || bytes == 0) { |
| 1688 | continue; |
| 1689 | } |
| 1690 | |
| 1691 | if (config.sample_rate != sampleRate || config.channel_mask != channelMask || |
| 1692 | config.format != format) { |
| 1693 | uint32_t dstChannelCount = audio_channel_count_from_in_mask(channelMask); |
| 1694 | uint32_t srcChannelCount = |
| 1695 | audio_channel_count_from_in_mask(config.channel_mask); |
| 1696 | size_t srcFrames = |
| 1697 | bytes / audio_bytes_per_frame(srcChannelCount, config.format); |
| 1698 | size_t dstFrames = destinationFramesPossible( |
| 1699 | srcFrames, config.sample_rate, sampleRate); |
| 1700 | bytes = dstFrames * audio_bytes_per_frame(dstChannelCount, format); |
| 1701 | } |
| 1702 | return bytes; |
| 1703 | } |
| 1704 | } |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 1705 | } |
Judy Hsiao | abab522 | 2019-11-09 18:14:58 +0800 | [diff] [blame] | 1706 | |
| 1707 | ALOGW("getInputBufferSize failed with minimum buffer size sampleRate %u, " |
| 1708 | "format %#x, channelMask %#x",sampleRate, format, channelMask); |
| 1709 | return 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 1712 | uint32_t AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1713 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1714 | Mutex::Autolock _l(mLock); |
| 1715 | |
| 1716 | RecordThread *recordThread = checkRecordThread_l(ioHandle); |
| 1717 | if (recordThread != NULL) { |
| 1718 | return recordThread->getInputFramesLost(); |
| 1719 | } |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | status_t AudioFlinger::setVoiceVolume(float value) |
| 1724 | { |
Eric Laurent | a1884f9 | 2011-08-23 08:25:03 -0700 | [diff] [blame] | 1725 | status_t ret = initCheck(); |
| 1726 | if (ret != NO_ERROR) { |
| 1727 | return ret; |
| 1728 | } |
| 1729 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1730 | // check calling permissions |
| 1731 | if (!settingsAllowed()) { |
| 1732 | return PERMISSION_DENIED; |
| 1733 | } |
| 1734 | |
| 1735 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 1736 | if (mPrimaryHardwareDev == nullptr) { |
| 1737 | return INVALID_OPERATION; |
| 1738 | } |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1739 | sp<DeviceHalInterface> dev = mPrimaryHardwareDev->hwDevice(); |
Glenn Kasten | 8abf44d | 2012-02-02 14:16:03 -0800 | [diff] [blame] | 1740 | mHardwareStatus = AUDIO_HW_SET_VOICE_VOLUME; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 1741 | ret = dev->setVoiceVolume(value); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1742 | mHardwareStatus = AUDIO_HW_IDLE; |
| 1743 | |
| 1744 | return ret; |
| 1745 | } |
| 1746 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1747 | status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 1748 | audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1749 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1750 | Mutex::Autolock _l(mLock); |
| 1751 | |
| 1752 | PlaybackThread *playbackThread = checkPlaybackThread_l(output); |
| 1753 | if (playbackThread != NULL) { |
| 1754 | return playbackThread->getRenderPosition(halFrames, dspFrames); |
| 1755 | } |
| 1756 | |
| 1757 | return BAD_VALUE; |
| 1758 | } |
| 1759 | |
| 1760 | void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client) |
| 1761 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1762 | Mutex::Autolock _l(mLock); |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 1763 | if (client == 0) { |
| 1764 | return; |
| 1765 | } |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1766 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Andy Hung | 5bdc535 | 2019-12-23 14:36:31 -0800 | [diff] [blame] | 1767 | const uid_t uid = IPCThreadState::self()->getCallingUid(); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1768 | { |
| 1769 | Mutex::Autolock _cl(mClientLock); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1770 | if (mNotificationClients.indexOfKey(pid) < 0) { |
| 1771 | sp<NotificationClient> notificationClient = new NotificationClient(this, |
| 1772 | client, |
Andy Hung | 5bdc535 | 2019-12-23 14:36:31 -0800 | [diff] [blame] | 1773 | pid, |
| 1774 | uid); |
| 1775 | ALOGV("registerClient() client %p, pid %d, uid %u", |
| 1776 | notificationClient.get(), pid, uid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1777 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1778 | mNotificationClients.add(pid, notificationClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1779 | |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1780 | sp<IBinder> binder = IInterface::asBinder(client); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1781 | binder->linkToDeath(notificationClient); |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1782 | } |
| 1783 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1784 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1785 | // mClientLock should not be held here because ThreadBase::sendIoConfigEvent() will lock the |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 1786 | // ThreadBase mutex and the locking order is ThreadBase::mLock then AudioFlinger::mClientLock. |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1787 | // the config change is always sent from playback or record threads to avoid deadlock |
| 1788 | // with AudioSystem::gLock |
| 1789 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1790 | mPlaybackThreads.valueAt(i)->sendIoConfigEvent(AUDIO_OUTPUT_REGISTERED, pid); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1791 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1792 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1793 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1794 | mRecordThreads.valueAt(i)->sendIoConfigEvent(AUDIO_INPUT_REGISTERED, pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | void AudioFlinger::removeNotificationClient(pid_t pid) |
| 1799 | { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1800 | std::vector< sp<AudioFlinger::EffectModule> > removedEffects; |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1801 | { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1802 | Mutex::Autolock _l(mLock); |
| 1803 | { |
| 1804 | Mutex::Autolock _cl(mClientLock); |
| 1805 | mNotificationClients.removeItem(pid); |
| 1806 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1807 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1808 | ALOGV("%d died, releasing its sessions", pid); |
| 1809 | size_t num = mAudioSessionRefs.size(); |
| 1810 | bool removed = false; |
| 1811 | for (size_t i = 0; i < num; ) { |
| 1812 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 1813 | ALOGV(" pid %d @ %zu", ref->mPid, i); |
| 1814 | if (ref->mPid == pid) { |
| 1815 | ALOGV(" removing entry for pid %d session %d", pid, ref->mSessionid); |
| 1816 | mAudioSessionRefs.removeAt(i); |
| 1817 | delete ref; |
| 1818 | removed = true; |
| 1819 | num--; |
| 1820 | } else { |
| 1821 | i++; |
| 1822 | } |
| 1823 | } |
| 1824 | if (removed) { |
| 1825 | removedEffects = purgeStaleEffects_l(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1826 | } |
| 1827 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1828 | for (auto& effect : removedEffects) { |
| 1829 | effect->updatePolicyState(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 1830 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 1833 | void AudioFlinger::ioConfigChanged(audio_io_config_event event, |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1834 | const sp<AudioIoDescriptor>& ioDesc, |
| 1835 | pid_t pid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1836 | { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1837 | Mutex::Autolock _l(mClientLock); |
| 1838 | size_t size = mNotificationClients.size(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1839 | for (size_t i = 0; i < size; i++) { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1840 | if ((pid == 0) || (mNotificationClients.keyAt(i) == pid)) { |
| 1841 | mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioDesc); |
| 1842 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1846 | // removeClient_l() must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1847 | void AudioFlinger::removeClient_l(pid_t pid) |
| 1848 | { |
Glenn Kasten | 827e5f1 | 2012-11-02 10:00:06 -0700 | [diff] [blame] | 1849 | ALOGV("removeClient_l() pid %d, calling pid %d", pid, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 1850 | IPCThreadState::self()->getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1851 | mClients.removeItem(pid); |
| 1852 | } |
| 1853 | |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1854 | // getEffectThread_l() must be called with AudioFlinger::mLock held |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1855 | sp<AudioFlinger::ThreadBase> AudioFlinger::getEffectThread_l(audio_session_t sessionId, |
| 1856 | int effectId) |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1857 | { |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1858 | sp<ThreadBase> thread; |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1859 | |
| 1860 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1861 | if (mPlaybackThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1862 | ALOG_ASSERT(thread == 0); |
| 1863 | thread = mPlaybackThreads.valueAt(i); |
| 1864 | } |
| 1865 | } |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 1866 | if (thread != nullptr) { |
| 1867 | return thread; |
| 1868 | } |
| 1869 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 1870 | if (mRecordThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { |
| 1871 | ALOG_ASSERT(thread == 0); |
| 1872 | thread = mRecordThreads.valueAt(i); |
| 1873 | } |
| 1874 | } |
| 1875 | if (thread != nullptr) { |
| 1876 | return thread; |
| 1877 | } |
| 1878 | for (size_t i = 0; i < mMmapThreads.size(); i++) { |
| 1879 | if (mMmapThreads.valueAt(i)->getEffect(sessionId, effectId) != 0) { |
| 1880 | ALOG_ASSERT(thread == 0); |
| 1881 | thread = mMmapThreads.valueAt(i); |
| 1882 | } |
| 1883 | } |
Eric Laurent | 717e128 | 2012-06-29 16:36:52 -0700 | [diff] [blame] | 1884 | return thread; |
| 1885 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1886 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1887 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1888 | |
| 1889 | // ---------------------------------------------------------------------------- |
| 1890 | |
| 1891 | AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid) |
| 1892 | : RefBase(), |
| 1893 | mAudioFlinger(audioFlinger), |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1894 | mPid(pid) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1895 | { |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1896 | mMemoryDealer = new MemoryDealer( |
| 1897 | audioFlinger->getClientSharedHeapSize(), |
| 1898 | (std::string("AudioFlinger::Client(") + std::to_string(pid) + ")").c_str()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 1901 | // Client destructor must be called with AudioFlinger::mClientLock held |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1902 | AudioFlinger::Client::~Client() |
| 1903 | { |
| 1904 | mAudioFlinger->removeClient_l(mPid); |
| 1905 | } |
| 1906 | |
Glenn Kasten | 435dbe6 | 2012-01-30 10:15:48 -0800 | [diff] [blame] | 1907 | sp<MemoryDealer> AudioFlinger::Client::heap() const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1908 | { |
| 1909 | return mMemoryDealer; |
| 1910 | } |
| 1911 | |
| 1912 | // ---------------------------------------------------------------------------- |
| 1913 | |
| 1914 | AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger, |
| 1915 | const sp<IAudioFlingerClient>& client, |
Andy Hung | 5bdc535 | 2019-12-23 14:36:31 -0800 | [diff] [blame] | 1916 | pid_t pid, |
| 1917 | uid_t uid) |
| 1918 | : mAudioFlinger(audioFlinger), mPid(pid), mUid(uid), mAudioFlingerClient(client) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1919 | { |
| 1920 | } |
| 1921 | |
| 1922 | AudioFlinger::NotificationClient::~NotificationClient() |
| 1923 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 1926 | void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who __unused) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1927 | { |
| 1928 | sp<NotificationClient> keep(this); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 1929 | mAudioFlinger->removeNotificationClient(mPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1932 | // ---------------------------------------------------------------------------- |
| 1933 | AudioFlinger::MediaLogNotifier::MediaLogNotifier() |
| 1934 | : mPendingRequests(false) {} |
| 1935 | |
| 1936 | |
| 1937 | void AudioFlinger::MediaLogNotifier::requestMerge() { |
| 1938 | AutoMutex _l(mMutex); |
| 1939 | mPendingRequests = true; |
| 1940 | mCond.signal(); |
| 1941 | } |
| 1942 | |
| 1943 | bool AudioFlinger::MediaLogNotifier::threadLoop() { |
Glenn Kasten | 04b96fc | 2017-04-10 14:58:47 -0700 | [diff] [blame] | 1944 | // Should already have been checked, but just in case |
| 1945 | if (sMediaLogService == 0) { |
| 1946 | return false; |
| 1947 | } |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1948 | // Wait until there are pending requests |
| 1949 | { |
| 1950 | AutoMutex _l(mMutex); |
| 1951 | mPendingRequests = false; // to ignore past requests |
| 1952 | while (!mPendingRequests) { |
| 1953 | mCond.wait(mMutex); |
| 1954 | // TODO may also need an exitPending check |
| 1955 | } |
| 1956 | mPendingRequests = false; |
| 1957 | } |
| 1958 | // 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] | 1959 | sMediaLogService->requestMergeWakeup(); |
Nicolas Roulet | dcdfaec | 2017-02-14 10:18:39 -0800 | [diff] [blame] | 1960 | usleep(kPostTriggerSleepPeriod); |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
| 1964 | void AudioFlinger::requestLogMerge() { |
| 1965 | mMediaLogNotifier->requestMerge(); |
| 1966 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1967 | |
| 1968 | // ---------------------------------------------------------------------------- |
| 1969 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1970 | sp<media::IAudioRecord> AudioFlinger::createRecord(const CreateRecordInput& input, |
| 1971 | CreateRecordOutput& output, |
| 1972 | status_t *status) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1973 | { |
| 1974 | sp<RecordThread::RecordTrack> recordTrack; |
| 1975 | sp<RecordHandle> recordHandle; |
| 1976 | sp<Client> client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1977 | status_t lStatus; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1978 | audio_session_t sessionId = input.sessionId; |
Eric Laurent | 77881cd | 2018-02-09 16:01:31 -0800 | [diff] [blame] | 1979 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1980 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1981 | output.cblk.clear(); |
| 1982 | output.buffers.clear(); |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 1983 | output.inputId = AUDIO_IO_HANDLE_NONE; |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 1984 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1985 | bool updatePid = (input.clientInfo.clientPid == -1); |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1986 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1987 | uid_t clientUid = input.clientInfo.clientUid; |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 1988 | if (!isAudioServerOrMediaServerUid(callingUid)) { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1989 | ALOGW_IF(clientUid != callingUid, |
| 1990 | "%s uid %d tried to pass itself off as %d", |
| 1991 | __FUNCTION__, callingUid, clientUid); |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1992 | clientUid = callingUid; |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1993 | updatePid = true; |
| 1994 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1995 | pid_t clientPid = input.clientInfo.clientPid; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1996 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1997 | if (updatePid) { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 1998 | ALOGW_IF(clientPid != -1 && clientPid != callingPid, |
Haynes Mathew George | 9ea77cd | 2016-04-06 17:07:48 -0700 | [diff] [blame] | 1999 | "%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] | 2000 | __func__, callingUid, callingPid, clientPid); |
| 2001 | clientPid = callingPid; |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2004 | // we don't yet support anything other than linear PCM |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2005 | if (!audio_is_valid_format(input.config.format) || !audio_is_linear_pcm(input.config.format)) { |
| 2006 | ALOGE("createRecord() invalid format %#x", input.config.format); |
Glenn Kasten | 291bb6d | 2013-07-16 17:23:39 -0700 | [diff] [blame] | 2007 | lStatus = BAD_VALUE; |
| 2008 | goto Exit; |
| 2009 | } |
| 2010 | |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 2011 | // further channel mask checks are performed by createRecordTrack_l() |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2012 | if (!audio_is_input_channel(input.config.channel_mask)) { |
| 2013 | ALOGE("createRecord() invalid channel mask %#x", input.config.channel_mask); |
Glenn Kasten | 53b5d09 | 2014-02-05 10:00:23 -0800 | [diff] [blame] | 2014 | lStatus = BAD_VALUE; |
| 2015 | goto Exit; |
| 2016 | } |
| 2017 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2018 | if (sessionId == AUDIO_SESSION_ALLOCATE) { |
| 2019 | sessionId = (audio_session_t) newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 2020 | } else if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { |
| 2021 | lStatus = BAD_VALUE; |
| 2022 | goto Exit; |
| 2023 | } |
| 2024 | |
| 2025 | output.sessionId = sessionId; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2026 | output.selectedDeviceId = input.selectedDeviceId; |
| 2027 | output.flags = input.flags; |
| 2028 | |
| 2029 | client = registerPid(clientPid); |
| 2030 | |
| 2031 | // Not a conventional loop, but a retry loop for at most two iterations total. |
| 2032 | // Try first maybe with FAST flag then try again without FAST flag if that fails. |
| 2033 | // Exits loop via break on no error of got exit on error |
| 2034 | // The sp<> references will be dropped when re-entering scope. |
| 2035 | // The lack of indentation is deliberate, to reduce code churn and ease merges. |
| 2036 | for (;;) { |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2037 | // release previously opened input if retrying. |
| 2038 | if (output.inputId != AUDIO_IO_HANDLE_NONE) { |
| 2039 | recordTrack.clear(); |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2040 | AudioSystem::releaseInput(portId); |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2041 | output.inputId = AUDIO_IO_HANDLE_NONE; |
Yung Ti Su | 5fac9c6 | 2018-05-15 15:07:43 +0800 | [diff] [blame] | 2042 | output.selectedDeviceId = input.selectedDeviceId; |
Eric Laurent | 77881cd | 2018-02-09 16:01:31 -0800 | [diff] [blame] | 2043 | portId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2044 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2045 | lStatus = AudioSystem::getInputForAttr(&input.attr, &output.inputId, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2046 | input.riid, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2047 | sessionId, |
| 2048 | // FIXME compare to AudioTrack |
| 2049 | clientPid, |
| 2050 | clientUid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2051 | input.opPackageName, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2052 | &input.config, |
| 2053 | output.flags, &output.selectedDeviceId, &portId); |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2054 | if (lStatus != NO_ERROR) { |
| 2055 | ALOGE("createRecord() getInputForAttr return error %d", lStatus); |
| 2056 | goto Exit; |
| 2057 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2058 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 2059 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2060 | Mutex::Autolock _l(mLock); |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2061 | RecordThread *thread = checkRecordThread_l(output.inputId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2062 | if (thread == NULL) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2063 | ALOGE("createRecord() checkRecordThread_l failed, input handle %d", output.inputId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2064 | lStatus = BAD_VALUE; |
| 2065 | goto Exit; |
| 2066 | } |
| 2067 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2068 | ALOGV("createRecord() lSessionId: %d input %d", sessionId, output.inputId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2069 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2070 | output.sampleRate = input.config.sample_rate; |
| 2071 | output.frameCount = input.frameCount; |
| 2072 | output.notificationFrameCount = input.notificationFrameCount; |
Glenn Kasten | 570f633 | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 2073 | |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2074 | recordTrack = thread->createRecordTrack_l(client, input.attr, &output.sampleRate, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2075 | input.config.format, input.config.channel_mask, |
| 2076 | &output.frameCount, sessionId, |
| 2077 | &output.notificationFrameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2078 | callingPid, clientUid, &output.flags, |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2079 | input.clientInfo.clientTid, |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2080 | &lStatus, portId, |
| 2081 | input.opPackageName); |
Haynes Mathew George | 03e9e83 | 2013-12-13 15:40:13 -0800 | [diff] [blame] | 2082 | LOG_ALWAYS_FATAL_IF((lStatus == NO_ERROR) && (recordTrack == 0)); |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2083 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2084 | // lStatus == BAD_TYPE means FAST flag was rejected: request a new input from |
| 2085 | // audio policy manager without FAST constraint |
| 2086 | if (lStatus == BAD_TYPE) { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2087 | continue; |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2088 | } |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2089 | |
| 2090 | if (lStatus != NO_ERROR) { |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2091 | goto Exit; |
| 2092 | } |
| 2093 | |
| 2094 | // Check if one effect chain was awaiting for an AudioRecord to be created on this |
| 2095 | // session and move it to this thread. |
| 2096 | sp<EffectChain> chain = getOrphanEffectChain_l(sessionId); |
| 2097 | if (chain != 0) { |
| 2098 | Mutex::Autolock _l(thread->mLock); |
| 2099 | thread->addEffectChain_l(chain); |
| 2100 | } |
| 2101 | break; |
| 2102 | } |
| 2103 | // End of retry loop. |
| 2104 | // 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] | 2105 | } |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 2106 | |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2107 | output.cblk = recordTrack->getCblk(); |
| 2108 | output.buffers = recordTrack->getBuffers(); |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2109 | output.portId = portId; |
Eric Laurent | f14db3c | 2017-12-08 14:20:36 -0800 | [diff] [blame] | 2110 | |
| 2111 | // return handle to client |
| 2112 | recordHandle = new RecordHandle(recordTrack); |
| 2113 | |
| 2114 | Exit: |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 2115 | if (lStatus != NO_ERROR) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2116 | // 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] | 2117 | // Client destructor is called by the TrackBase destructor with mClientLock held |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 2118 | // Don't hold mClientLock when releasing the reference on the track as the |
| 2119 | // destructor will acquire it. |
| 2120 | { |
| 2121 | Mutex::Autolock _cl(mClientLock); |
| 2122 | client.clear(); |
| 2123 | } |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2124 | recordTrack.clear(); |
| 2125 | if (output.inputId != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2126 | AudioSystem::releaseInput(portId); |
Eric Laurent | 896c967 | 2017-12-08 14:08:45 -0800 | [diff] [blame] | 2127 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 2130 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2131 | return recordHandle; |
| 2132 | } |
| 2133 | |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2134 | |
| 2135 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2136 | // ---------------------------------------------------------------------------- |
| 2137 | |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2138 | audio_module_handle_t AudioFlinger::loadHwModule(const char *name) |
| 2139 | { |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 2140 | if (name == NULL) { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2141 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | 44622db | 2014-08-01 19:00:33 -0700 | [diff] [blame] | 2142 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2143 | if (!settingsAllowed()) { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2144 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2145 | } |
| 2146 | Mutex::Autolock _l(mLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2147 | AutoMutex lock(mHardwareLock); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2148 | return loadHwModule_l(name); |
| 2149 | } |
| 2150 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2151 | // loadHwModule_l() must be called with AudioFlinger::mLock and AudioFlinger::mHardwareLock held |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2152 | audio_module_handle_t AudioFlinger::loadHwModule_l(const char *name) |
| 2153 | { |
| 2154 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 2155 | if (strncmp(mAudioHwDevs.valueAt(i)->moduleName(), name, strlen(name)) == 0) { |
| 2156 | ALOGW("loadHwModule() module %s already loaded", name); |
| 2157 | return mAudioHwDevs.keyAt(i); |
| 2158 | } |
| 2159 | } |
| 2160 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2161 | sp<DeviceHalInterface> dev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2162 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2163 | int rc = mDevicesFactoryHal->openDevice(name, &dev); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2164 | if (rc) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2165 | ALOGE("loadHwModule() error %d loading module %s", rc, name); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2166 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | mHardwareStatus = AUDIO_HW_INIT; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2170 | rc = dev->initCheck(); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2171 | mHardwareStatus = AUDIO_HW_IDLE; |
| 2172 | if (rc) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2173 | ALOGE("loadHwModule() init check error %d for module %s", rc, name); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2174 | return AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2177 | // Check and cache this HAL's level of support for master mute and master |
| 2178 | // volume. If this is the first HAL opened, and it supports the get |
| 2179 | // methods, use the initial values provided by the HAL as the current |
| 2180 | // master mute and volume settings. |
| 2181 | |
| 2182 | AudioHwDevice::Flags flags = static_cast<AudioHwDevice::Flags>(0); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2183 | if (0 == mAudioHwDevs.size()) { |
| 2184 | mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME; |
| 2185 | float mv; |
| 2186 | if (OK == dev->getMasterVolume(&mv)) { |
| 2187 | mMasterVolume = mv; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2190 | mHardwareStatus = AUDIO_HW_GET_MASTER_MUTE; |
| 2191 | bool mm; |
| 2192 | if (OK == dev->getMasterMute(&mm)) { |
| 2193 | mMasterMute = mm; |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 2194 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2195 | } |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2196 | |
| 2197 | mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME; |
| 2198 | if (OK == dev->setMasterVolume(mMasterVolume)) { |
| 2199 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 2200 | AudioHwDevice::AHWD_CAN_SET_MASTER_VOLUME); |
| 2201 | } |
| 2202 | |
| 2203 | mHardwareStatus = AUDIO_HW_SET_MASTER_MUTE; |
| 2204 | if (OK == dev->setMasterMute(mMasterMute)) { |
| 2205 | flags = static_cast<AudioHwDevice::Flags>(flags | |
| 2206 | AudioHwDevice::AHWD_CAN_SET_MASTER_MUTE); |
| 2207 | } |
| 2208 | |
| 2209 | mHardwareStatus = AUDIO_HW_IDLE; |
| 2210 | |
Mikhail Naganov | 97373b0 | 2018-07-23 13:27:24 -0700 | [diff] [blame] | 2211 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2212 | // An MSD module is inserted before hardware modules in order to mix encoded streams. |
| 2213 | flags = static_cast<AudioHwDevice::Flags>(flags | AudioHwDevice::AHWD_IS_INSERT); |
| 2214 | } |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2215 | |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2216 | audio_module_handle_t handle = (audio_module_handle_t) nextUniqueId(AUDIO_UNIQUE_ID_USE_MODULE); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2217 | AudioHwDevice *audioDevice = new AudioHwDevice(handle, name, dev, flags); |
| 2218 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) { |
| 2219 | mPrimaryHardwareDev = audioDevice; |
| 2220 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 2221 | mPrimaryHardwareDev->hwDevice()->setMode(mMode); |
| 2222 | mHardwareStatus = AUDIO_HW_IDLE; |
| 2223 | } |
| 2224 | |
| 2225 | mAudioHwDevs.add(handle, audioDevice); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2226 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2227 | ALOGI("loadHwModule() Loaded %s audio interface, handle %d", name, handle); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2228 | |
| 2229 | return handle; |
| 2230 | |
| 2231 | } |
| 2232 | |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2233 | // ---------------------------------------------------------------------------- |
| 2234 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 2235 | uint32_t AudioFlinger::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2236 | { |
| 2237 | Mutex::Autolock _l(mLock); |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 2238 | PlaybackThread *thread = fastPlaybackThread_l(); |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2239 | return thread != NULL ? thread->sampleRate() : 0; |
| 2240 | } |
| 2241 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 2242 | size_t AudioFlinger::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2243 | { |
| 2244 | Mutex::Autolock _l(mLock); |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 2245 | PlaybackThread *thread = fastPlaybackThread_l(); |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 2246 | return thread != NULL ? thread->frameCountHAL() : 0; |
| 2247 | } |
| 2248 | |
| 2249 | // ---------------------------------------------------------------------------- |
| 2250 | |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2251 | status_t AudioFlinger::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2252 | { |
| 2253 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2254 | if (!isAudioServerOrSystemServerUid(uid)) { |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2255 | return PERMISSION_DENIED; |
| 2256 | } |
| 2257 | Mutex::Autolock _l(mLock); |
| 2258 | if (mIsDeviceTypeKnown) { |
| 2259 | return INVALID_OPERATION; |
| 2260 | } |
| 2261 | mIsLowRamDevice = isLowRamDevice; |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2262 | mTotalMemory = totalMemory; |
| 2263 | // mIsLowRamDevice and mTotalMemory are obtained through ActivityManager; |
| 2264 | // see ActivityManager.isLowRamDevice() and ActivityManager.getMemoryInfo(). |
| 2265 | // mIsLowRamDevice generally represent devices with less than 1GB of memory, |
| 2266 | // though actual setting is determined through device configuration. |
| 2267 | constexpr int64_t GB = 1024 * 1024 * 1024; |
| 2268 | mClientSharedHeapSize = |
| 2269 | isLowRamDevice ? kMinimumClientSharedHeapSizeBytes |
| 2270 | : mTotalMemory < 2 * GB ? 4 * kMinimumClientSharedHeapSizeBytes |
| 2271 | : mTotalMemory < 3 * GB ? 8 * kMinimumClientSharedHeapSizeBytes |
| 2272 | : mTotalMemory < 4 * GB ? 16 * kMinimumClientSharedHeapSizeBytes |
| 2273 | : 32 * kMinimumClientSharedHeapSizeBytes; |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2274 | mIsDeviceTypeKnown = true; |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2275 | |
| 2276 | // TODO: Cache the client shared heap size in a persistent property. |
| 2277 | // It's possible that a native process or Java service or app accesses audioserver |
| 2278 | // after it is registered by system server, but before AudioService updates |
| 2279 | // the memory info. This would occur immediately after boot or an audioserver |
| 2280 | // crash and restore. Before update from AudioService, the client would get the |
| 2281 | // minimum heap size. |
| 2282 | |
| 2283 | ALOGD("isLowRamDevice:%s totalMemory:%lld mClientSharedHeapSize:%zu", |
| 2284 | (isLowRamDevice ? "true" : "false"), |
| 2285 | (long long)mTotalMemory, |
| 2286 | mClientSharedHeapSize.load()); |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2287 | return NO_ERROR; |
| 2288 | } |
| 2289 | |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 2290 | size_t AudioFlinger::getClientSharedHeapSize() const |
| 2291 | { |
| 2292 | size_t heapSizeInBytes = property_get_int32("ro.af.client_heap_size_kbyte", 0) * 1024; |
| 2293 | if (heapSizeInBytes != 0) { // read-only property overrides all. |
| 2294 | return heapSizeInBytes; |
| 2295 | } |
| 2296 | return mClientSharedHeapSize; |
| 2297 | } |
| 2298 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 2299 | status_t AudioFlinger::setAudioPortConfig(const struct audio_port_config *config) |
| 2300 | { |
| 2301 | ALOGV(__func__); |
| 2302 | |
| 2303 | audio_module_handle_t module; |
| 2304 | if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2305 | module = config->ext.device.hw_module; |
| 2306 | } else { |
| 2307 | module = config->ext.mix.hw_module; |
| 2308 | } |
| 2309 | |
| 2310 | Mutex::Autolock _l(mLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2311 | AutoMutex lock(mHardwareLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 2312 | ssize_t index = mAudioHwDevs.indexOfKey(module); |
| 2313 | if (index < 0) { |
| 2314 | ALOGW("%s() bad hw module %d", __func__, module); |
| 2315 | return BAD_VALUE; |
| 2316 | } |
| 2317 | |
| 2318 | AudioHwDevice *audioHwDevice = mAudioHwDevs.valueAt(index); |
| 2319 | return audioHwDevice->hwDevice()->setAudioPortConfig(config); |
| 2320 | } |
| 2321 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2322 | audio_hw_sync_t AudioFlinger::getAudioHwSyncForSession(audio_session_t sessionId) |
| 2323 | { |
| 2324 | Mutex::Autolock _l(mLock); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2325 | |
| 2326 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 2327 | if (index >= 0) { |
| 2328 | ALOGV("getAudioHwSyncForSession found ID %d for session %d", |
| 2329 | mHwAvSyncIds.valueAt(index), sessionId); |
| 2330 | return mHwAvSyncIds.valueAt(index); |
| 2331 | } |
| 2332 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2333 | sp<DeviceHalInterface> dev; |
| 2334 | { |
| 2335 | AutoMutex lock(mHardwareLock); |
| 2336 | if (mPrimaryHardwareDev == nullptr) { |
| 2337 | return AUDIO_HW_SYNC_INVALID; |
| 2338 | } |
| 2339 | dev = mPrimaryHardwareDev->hwDevice(); |
| 2340 | } |
| 2341 | if (dev == nullptr) { |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2342 | return AUDIO_HW_SYNC_INVALID; |
| 2343 | } |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2344 | String8 reply; |
| 2345 | AudioParameter param; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2346 | if (dev->getParameters(String8(AudioParameter::keyHwAvSync), &reply) == OK) { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2347 | param = AudioParameter(reply); |
| 2348 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2349 | |
| 2350 | int value; |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2351 | if (param.getInt(String8(AudioParameter::keyHwAvSync), value) != NO_ERROR) { |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2352 | ALOGW("getAudioHwSyncForSession error getting sync for session %d", sessionId); |
| 2353 | return AUDIO_HW_SYNC_INVALID; |
| 2354 | } |
| 2355 | |
| 2356 | // allow only one session for a given HW A/V sync ID. |
| 2357 | for (size_t i = 0; i < mHwAvSyncIds.size(); i++) { |
| 2358 | if (mHwAvSyncIds.valueAt(i) == (audio_hw_sync_t)value) { |
| 2359 | ALOGV("getAudioHwSyncForSession removing ID %d for session %d", |
| 2360 | value, mHwAvSyncIds.keyAt(i)); |
| 2361 | mHwAvSyncIds.removeItemsAt(i); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2362 | break; |
| 2363 | } |
| 2364 | } |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2365 | |
| 2366 | mHwAvSyncIds.add(sessionId, value); |
| 2367 | |
| 2368 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2369 | sp<PlaybackThread> thread = mPlaybackThreads.valueAt(i); |
| 2370 | uint32_t sessions = thread->hasAudioSession(sessionId); |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 2371 | if (sessions & ThreadBase::TRACK_SESSION) { |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2372 | AudioParameter param = AudioParameter(); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2373 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), value); |
Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 2374 | String8 keyValuePairs = param.toString(); |
| 2375 | thread->setParameters(keyValuePairs); |
| 2376 | forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs, |
| 2377 | [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); }); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2378 | break; |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | ALOGV("getAudioHwSyncForSession adding ID %d for session %d", value, sessionId); |
| 2383 | return (audio_hw_sync_t)value; |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2386 | status_t AudioFlinger::systemReady() |
| 2387 | { |
| 2388 | Mutex::Autolock _l(mLock); |
| 2389 | ALOGI("%s", __FUNCTION__); |
| 2390 | if (mSystemReady) { |
| 2391 | ALOGW("%s called twice", __FUNCTION__); |
| 2392 | return NO_ERROR; |
| 2393 | } |
| 2394 | mSystemReady = true; |
| 2395 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2396 | ThreadBase *thread = (ThreadBase *)mPlaybackThreads.valueAt(i).get(); |
| 2397 | thread->systemReady(); |
| 2398 | } |
| 2399 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 2400 | ThreadBase *thread = (ThreadBase *)mRecordThreads.valueAt(i).get(); |
| 2401 | thread->systemReady(); |
| 2402 | } |
| 2403 | return NO_ERROR; |
| 2404 | } |
| 2405 | |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 2406 | status_t AudioFlinger::getMicrophones(std::vector<media::MicrophoneInfo> *microphones) |
| 2407 | { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 2408 | AutoMutex lock(mHardwareLock); |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2409 | status_t status = INVALID_OPERATION; |
| 2410 | |
| 2411 | for (size_t i = 0; i < mAudioHwDevs.size(); i++) { |
| 2412 | std::vector<media::MicrophoneInfo> mics; |
| 2413 | AudioHwDevice *dev = mAudioHwDevs.valueAt(i); |
| 2414 | mHardwareStatus = AUDIO_HW_GET_MICROPHONES; |
| 2415 | status_t devStatus = dev->hwDevice()->getMicrophones(&mics); |
| 2416 | mHardwareStatus = AUDIO_HW_IDLE; |
| 2417 | if (devStatus == NO_ERROR) { |
| 2418 | microphones->insert(microphones->begin(), mics.begin(), mics.end()); |
| 2419 | // report success if at least one HW module supports the function. |
| 2420 | status = NO_ERROR; |
| 2421 | } |
| 2422 | } |
| 2423 | |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 2424 | return status; |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2427 | // setAudioHwSyncForSession_l() must be called with AudioFlinger::mLock held |
| 2428 | void AudioFlinger::setAudioHwSyncForSession_l(PlaybackThread *thread, audio_session_t sessionId) |
| 2429 | { |
| 2430 | ssize_t index = mHwAvSyncIds.indexOfKey(sessionId); |
| 2431 | if (index >= 0) { |
| 2432 | audio_hw_sync_t syncId = mHwAvSyncIds.valueAt(index); |
| 2433 | ALOGV("setAudioHwSyncForSession_l found ID %d for session %d", syncId, sessionId); |
| 2434 | AudioParameter param = AudioParameter(); |
Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 2435 | param.addInt(String8(AudioParameter::keyStreamHwAvSync), syncId); |
Mikhail Naganov | b261ef5 | 2018-07-16 13:34:38 -0700 | [diff] [blame] | 2436 | String8 keyValuePairs = param.toString(); |
| 2437 | thread->setParameters(keyValuePairs); |
| 2438 | forwardParametersToDownstreamPatches_l(thread->id(), keyValuePairs, |
| 2439 | [](const sp<PlaybackThread>& thread) { return thread->usesHwAvSync(); }); |
Eric Laurent | fa90e84 | 2014-10-17 18:12:31 -0700 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 2444 | // ---------------------------------------------------------------------------- |
| 2445 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2446 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2447 | sp<AudioFlinger::ThreadBase> AudioFlinger::openOutput_l(audio_module_handle_t module, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2448 | audio_io_handle_t *output, |
| 2449 | audio_config_t *config, |
| 2450 | audio_devices_t deviceType, |
| 2451 | const String8& address, |
| 2452 | audio_output_flags_t flags) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2453 | { |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2454 | AudioHwDevice *outHwDev = findSuitableHwDev_l(module, deviceType); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2455 | if (outHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2456 | return 0; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2459 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2460 | *output = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); |
| 2461 | } else { |
| 2462 | // Audio Policy does not currently request a specific output handle. |
| 2463 | // If this is ever needed, see openInput_l() for example code. |
| 2464 | ALOGE("openOutput_l requested output handle %d is not AUDIO_IO_HANDLE_NONE", *output); |
| 2465 | return 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2466 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2467 | |
| 2468 | mHardwareStatus = AUDIO_HW_OUTPUT_OPEN; |
| 2469 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2470 | // FOR TESTING ONLY: |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2471 | // This if statement allows overriding the audio policy settings |
| 2472 | // and forcing a specific format or channel mask to the HAL/Sink device for testing. |
| 2473 | if (!(flags & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT))) { |
| 2474 | // Check only for Normal Mixing mode |
| 2475 | if (kEnableExtendedPrecision) { |
| 2476 | // Specify format (uncomment one below to choose) |
| 2477 | //config->format = AUDIO_FORMAT_PCM_FLOAT; |
| 2478 | //config->format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
| 2479 | //config->format = AUDIO_FORMAT_PCM_32_BIT; |
| 2480 | //config->format = AUDIO_FORMAT_PCM_8_24_BIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2481 | // ALOGV("openOutput_l() upgrading format to %#08x", config->format); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 2482 | } |
| 2483 | if (kEnableExtendedChannels) { |
| 2484 | // Specify channel mask (uncomment one below to choose) |
| 2485 | //config->channel_mask = audio_channel_out_mask_from_count(4); // for USB 4ch |
| 2486 | //config->channel_mask = audio_channel_mask_from_representation_and_bits( |
| 2487 | // AUDIO_CHANNEL_REPRESENTATION_INDEX, (1 << 4) - 1); // another 4ch example |
| 2488 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2489 | } |
| 2490 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2491 | AudioStreamOut *outputStream = NULL; |
| 2492 | status_t status = outHwDev->openOutputStream( |
| 2493 | &outputStream, |
| 2494 | *output, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2495 | deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2496 | flags, |
| 2497 | config, |
| 2498 | address.string()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2499 | |
| 2500 | mHardwareStatus = AUDIO_HW_IDLE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2501 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 2502 | if (status == NO_ERROR) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2503 | if (flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) { |
| 2504 | sp<MmapPlaybackThread> thread = |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2505 | new MmapPlaybackThread(this, *output, outHwDev, outputStream, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2506 | mMmapThreads.add(*output, thread); |
| 2507 | ALOGV("openOutput_l() created mmap playback thread: ID %d thread %p", |
| 2508 | *output, thread.get()); |
| 2509 | return thread; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2510 | } else { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2511 | sp<PlaybackThread> thread; |
| 2512 | if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2513 | thread = new OffloadThread(this, outputStream, *output, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2514 | ALOGV("openOutput_l() created offload output: ID %d thread %p", |
| 2515 | *output, thread.get()); |
| 2516 | } else if ((flags & AUDIO_OUTPUT_FLAG_DIRECT) |
| 2517 | || !isValidPcmSinkFormat(config->format) |
| 2518 | || !isValidPcmSinkChannelMask(config->channel_mask)) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2519 | thread = new DirectOutputThread(this, outputStream, *output, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2520 | ALOGV("openOutput_l() created direct output: ID %d thread %p", |
| 2521 | *output, thread.get()); |
| 2522 | } else { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2523 | thread = new MixerThread(this, outputStream, *output, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2524 | ALOGV("openOutput_l() created mixer output: ID %d thread %p", |
| 2525 | *output, thread.get()); |
| 2526 | } |
| 2527 | mPlaybackThreads.add(*output, thread); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2528 | mPatchPanel.notifyStreamOpened(outHwDev, *output); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2529 | return thread; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2530 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2536 | status_t AudioFlinger::openOutput(audio_module_handle_t module, |
| 2537 | audio_io_handle_t *output, |
| 2538 | audio_config_t *config, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2539 | const sp<DeviceDescriptorBase>& device, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2540 | uint32_t *latencyMs, |
| 2541 | audio_output_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2542 | { |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2543 | ALOGI("openOutput() this %p, module %d Device %s, SamplingRate %d, Format %#08x, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2544 | "Channels %#x, flags %#x", |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2545 | this, module, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2546 | device->toString().c_str(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2547 | config->sample_rate, |
| 2548 | config->format, |
| 2549 | config->channel_mask, |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2550 | flags); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2551 | |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2552 | audio_devices_t deviceType = device->type(); |
| 2553 | const String8 address = String8(device->address().c_str()); |
| 2554 | |
| 2555 | if (deviceType == AUDIO_DEVICE_NONE) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2556 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2557 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2558 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2559 | Mutex::Autolock _l(mLock); |
| 2560 | |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 2561 | sp<ThreadBase> thread = openOutput_l(module, output, config, deviceType, address, flags); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2562 | if (thread != 0) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2563 | if ((flags & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == 0) { |
| 2564 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 2565 | *latencyMs = playbackThread->latency(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2566 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2567 | // notify client processes of the new output creation |
| 2568 | playbackThread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2569 | |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 2570 | // the first primary output opened designates the primary hw device if no HW module |
| 2571 | // named "primary" was already loaded. |
| 2572 | AutoMutex lock(mHardwareLock); |
| 2573 | if ((mPrimaryHardwareDev == nullptr) && (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) { |
Glenn Kasten | 1bfe09a | 2017-02-21 13:05:56 -0800 | [diff] [blame] | 2574 | ALOGI("Using module %d as the primary audio interface", module); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2575 | mPrimaryHardwareDev = playbackThread->getOutput()->audioHwDev; |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2576 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2577 | mHardwareStatus = AUDIO_HW_SET_MODE; |
| 2578 | mPrimaryHardwareDev->hwDevice()->setMode(mMode); |
| 2579 | mHardwareStatus = AUDIO_HW_IDLE; |
| 2580 | } |
| 2581 | } else { |
| 2582 | MmapThread *mmapThread = (MmapThread *)thread.get(); |
| 2583 | mmapThread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Eric Laurent | a4c5a55 | 2012-03-29 10:12:40 -0700 | [diff] [blame] | 2584 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2585 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2588 | return NO_INIT; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2591 | audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1, |
| 2592 | audio_io_handle_t output2) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2593 | { |
| 2594 | Mutex::Autolock _l(mLock); |
| 2595 | MixerThread *thread1 = checkMixerThread_l(output1); |
| 2596 | MixerThread *thread2 = checkMixerThread_l(output2); |
| 2597 | |
| 2598 | if (thread1 == NULL || thread2 == NULL) { |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2599 | ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, |
| 2600 | output2); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 2601 | return AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2604 | audio_io_handle_t id = nextUniqueId(AUDIO_UNIQUE_ID_USE_OUTPUT); |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 2605 | DuplicatingThread *thread = new DuplicatingThread(this, thread1, id, mSystemReady); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2606 | thread->addOutputTrack(thread2); |
| 2607 | mPlaybackThreads.add(id, thread); |
| 2608 | // notify client processes of the new output creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2609 | thread->ioConfigChanged(AUDIO_OUTPUT_OPENED); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2610 | return id; |
| 2611 | } |
| 2612 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2613 | status_t AudioFlinger::closeOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2614 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2615 | return closeOutput_nonvirtual(output); |
| 2616 | } |
| 2617 | |
| 2618 | status_t AudioFlinger::closeOutput_nonvirtual(audio_io_handle_t output) |
| 2619 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2620 | // keep strong reference on the playback thread so that |
| 2621 | // it is not destroyed while exit() is executed |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2622 | sp<PlaybackThread> playbackThread; |
| 2623 | sp<MmapPlaybackThread> mmapThread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2624 | { |
| 2625 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2626 | playbackThread = checkPlaybackThread_l(output); |
| 2627 | if (playbackThread != NULL) { |
| 2628 | ALOGV("closeOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2629 | |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2630 | dumpToThreadLog_l(playbackThread); |
Andy Hung | a8115dc | 2018-08-24 15:51:59 -0700 | [diff] [blame] | 2631 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2632 | if (playbackThread->type() == ThreadBase::MIXER) { |
| 2633 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2634 | if (mPlaybackThreads.valueAt(i)->isDuplicating()) { |
| 2635 | DuplicatingThread *dupThread = |
| 2636 | (DuplicatingThread *)mPlaybackThreads.valueAt(i).get(); |
| 2637 | dupThread->removeOutputTrack((MixerThread *)playbackThread.get()); |
| 2638 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2639 | } |
| 2640 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 2641 | |
| 2642 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2643 | mPlaybackThreads.removeItem(output); |
| 2644 | // save all effects to the default thread |
| 2645 | if (mPlaybackThreads.size()) { |
| 2646 | PlaybackThread *dstThread = checkPlaybackThread_l(mPlaybackThreads.keyAt(0)); |
| 2647 | if (dstThread != NULL) { |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2648 | // 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] | 2649 | Mutex::Autolock _dl(dstThread->mLock); |
| 2650 | Mutex::Autolock _sl(playbackThread->mLock); |
| 2651 | Vector< sp<EffectChain> > effectChains = playbackThread->getEffectChains_l(); |
| 2652 | for (size_t i = 0; i < effectChains.size(); i ++) { |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2653 | moveEffectChain_l(effectChains[i]->sessionId(), playbackThread.get(), |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2654 | dstThread); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2655 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2656 | } |
| 2657 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2658 | } else { |
| 2659 | mmapThread = (MmapPlaybackThread *)checkMmapThread_l(output); |
| 2660 | if (mmapThread == 0) { |
| 2661 | return BAD_VALUE; |
| 2662 | } |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2663 | dumpToThreadLog_l(mmapThread); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2664 | mMmapThreads.removeItem(output); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 2665 | ALOGD("closing mmapThread %p", mmapThread.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2666 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2667 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 2668 | ioDesc->mIoHandle = output; |
| 2669 | ioConfigChanged(AUDIO_OUTPUT_CLOSED, ioDesc); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 2670 | mPatchPanel.notifyStreamClosed(output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2671 | } |
Glenn Kasten | b28686f | 2012-01-06 08:39:38 -0800 | [diff] [blame] | 2672 | // The thread entity (active unit of execution) is no longer running here, |
| 2673 | // but the ThreadBase container still exists. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2674 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2675 | if (playbackThread != 0) { |
| 2676 | playbackThread->exit(); |
| 2677 | if (!playbackThread->isDuplicating()) { |
| 2678 | closeOutputFinish(playbackThread); |
| 2679 | } |
| 2680 | } else if (mmapThread != 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 2681 | ALOGD("mmapThread exit()"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2682 | mmapThread->exit(); |
| 2683 | AudioStreamOut *out = mmapThread->clearOutput(); |
| 2684 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
| 2685 | // from now on thread->mOutput is NULL |
| 2686 | delete out; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2687 | } |
| 2688 | return NO_ERROR; |
| 2689 | } |
| 2690 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2691 | void AudioFlinger::closeOutputFinish(const sp<PlaybackThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2692 | { |
| 2693 | AudioStreamOut *out = thread->clearOutput(); |
| 2694 | ALOG_ASSERT(out != NULL, "out shouldn't be NULL"); |
| 2695 | // from now on thread->mOutput is NULL |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2696 | delete out; |
| 2697 | } |
| 2698 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 2699 | void AudioFlinger::closeThreadInternal_l(const sp<PlaybackThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2700 | { |
| 2701 | mPlaybackThreads.removeItem(thread->mId); |
| 2702 | thread->exit(); |
| 2703 | closeOutputFinish(thread); |
| 2704 | } |
| 2705 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2706 | status_t AudioFlinger::suspendOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2707 | { |
| 2708 | Mutex::Autolock _l(mLock); |
| 2709 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 2710 | |
| 2711 | if (thread == NULL) { |
| 2712 | return BAD_VALUE; |
| 2713 | } |
| 2714 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2715 | ALOGV("suspendOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2716 | thread->suspend(); |
| 2717 | |
| 2718 | return NO_ERROR; |
| 2719 | } |
| 2720 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2721 | status_t AudioFlinger::restoreOutput(audio_io_handle_t output) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2722 | { |
| 2723 | Mutex::Autolock _l(mLock); |
| 2724 | PlaybackThread *thread = checkPlaybackThread_l(output); |
| 2725 | |
| 2726 | if (thread == NULL) { |
| 2727 | return BAD_VALUE; |
| 2728 | } |
| 2729 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 2730 | ALOGV("restoreOutput() %d", output); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2731 | |
| 2732 | thread->restore(); |
| 2733 | |
| 2734 | return NO_ERROR; |
| 2735 | } |
| 2736 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2737 | status_t AudioFlinger::openInput(audio_module_handle_t module, |
| 2738 | audio_io_handle_t *input, |
| 2739 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2740 | audio_devices_t *devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2741 | const String8& address, |
| 2742 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2743 | audio_input_flags_t flags) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2744 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2745 | Mutex::Autolock _l(mLock); |
| 2746 | |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2747 | if (*devices == AUDIO_DEVICE_NONE) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2748 | return BAD_VALUE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2751 | sp<ThreadBase> thread = openInput_l( |
| 2752 | module, input, config, *devices, address, source, flags, AUDIO_DEVICE_NONE, String8{}); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2753 | |
| 2754 | if (thread != 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2755 | // notify client processes of the new input creation |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2756 | thread->ioConfigChanged(AUDIO_INPUT_OPENED); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2757 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2758 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2759 | return NO_INIT; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2760 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2761 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2762 | sp<AudioFlinger::ThreadBase> AudioFlinger::openInput_l(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2763 | audio_io_handle_t *input, |
| 2764 | audio_config_t *config, |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2765 | audio_devices_t devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2766 | const String8& address, |
| 2767 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2768 | audio_input_flags_t flags, |
| 2769 | audio_devices_t outputDevice, |
| 2770 | const String8& outputDeviceAddress) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2771 | { |
Glenn Kasten | e7d6671 | 2015-03-05 13:46:52 -0800 | [diff] [blame] | 2772 | AudioHwDevice *inHwDev = findSuitableHwDev_l(module, devices); |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2773 | if (inHwDev == NULL) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2774 | *input = AUDIO_IO_HANDLE_NONE; |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2775 | return 0; |
Glenn Kasten | 6e2ebe9 | 2013-08-13 09:14:51 -0700 | [diff] [blame] | 2776 | } |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2777 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2778 | // Audio Policy can request a specific handle for hardware hotword. |
| 2779 | // The goal here is not to re-open an already opened input. |
| 2780 | // It is to use a pre-assigned I/O handle. |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2781 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2782 | *input = nextUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
| 2783 | } else if (audio_unique_id_get_use(*input) != AUDIO_UNIQUE_ID_USE_INPUT) { |
| 2784 | ALOGE("openInput_l() requested input handle %d is invalid", *input); |
| 2785 | return 0; |
| 2786 | } else if (mRecordThreads.indexOfKey(*input) >= 0) { |
| 2787 | // This should not happen in a transient state with current design. |
| 2788 | ALOGE("openInput_l() requested input handle %d is already assigned", *input); |
| 2789 | return 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2790 | } |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2791 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2792 | audio_config_t halconfig = *config; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 2793 | sp<DeviceHalInterface> inHwHal = inHwDev->hwDevice(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2794 | sp<StreamInHalInterface> inStream; |
| 2795 | status_t status = inHwHal->openInputStream( |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2796 | *input, devices, &halconfig, flags, address.string(), source, |
| 2797 | outputDevice, outputDeviceAddress, &inStream); |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2798 | ALOGV("openInput_l() openInputStream returned input %p, devices %#x, SamplingRate %d" |
| 2799 | ", Format %#x, Channels %#x, flags %#x, status %d addr %s", |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2800 | inStream.get(), |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 2801 | devices, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2802 | halconfig.sample_rate, |
| 2803 | halconfig.format, |
| 2804 | halconfig.channel_mask, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 2805 | flags, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2806 | status, address.string()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2807 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 2808 | // 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] | 2809 | // conversion internally, try to open again with the proposed parameters. |
Eric Laurent | f7ffb8b | 2012-04-14 09:06:57 -0700 | [diff] [blame] | 2810 | if (status == BAD_VALUE && |
Andy Hung | 6770c6f | 2015-04-07 13:43:36 -0700 | [diff] [blame] | 2811 | audio_is_linear_pcm(config->format) && |
| 2812 | audio_is_linear_pcm(halconfig.format) && |
| 2813 | (halconfig.sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
vivek mehta | 7534666 | 2016-05-04 18:45:46 -0700 | [diff] [blame] | 2814 | (audio_channel_count_from_in_mask(halconfig.channel_mask) <= FCC_8) && |
| 2815 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_8)) { |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2816 | // 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] | 2817 | ALOGV("openInput_l() reopening with proposed sampling rate and channel mask"); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2818 | inStream.clear(); |
| 2819 | status = inHwHal->openInputStream( |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 2820 | *input, devices, &halconfig, flags, address.string(), source, |
| 2821 | outputDevice, outputDeviceAddress, &inStream); |
Glenn Kasten | 8594843 | 2013-08-19 12:09:05 -0700 | [diff] [blame] | 2822 | // FIXME log this new status; HAL should not propose any further changes |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 2825 | if (status == NO_ERROR && inStream != 0) { |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2826 | AudioStreamIn *inputStream = new AudioStreamIn(inHwDev, inStream, flags); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2827 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) { |
| 2828 | sp<MmapCaptureThread> thread = |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2829 | new MmapCaptureThread(this, *input, inHwDev, inputStream, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2830 | mMmapThreads.add(*input, thread); |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2831 | ALOGV("openInput_l() created mmap capture thread: ID %d thread %p", *input, |
| 2832 | thread.get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2833 | return thread; |
| 2834 | } else { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2835 | // Start record thread |
| 2836 | // RecordThread requires both input and output device indication to forward to audio |
| 2837 | // pre processing modules |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 2838 | sp<RecordThread> thread = new RecordThread(this, inputStream, *input, mSystemReady); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2839 | mRecordThreads.add(*input, thread); |
| 2840 | ALOGV("openInput_l() created record thread: ID %d thread %p", *input, thread.get()); |
| 2841 | return thread; |
| 2842 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2845 | *input = AUDIO_IO_HANDLE_NONE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2846 | return 0; |
| 2847 | } |
| 2848 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 2849 | status_t AudioFlinger::closeInput(audio_io_handle_t input) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2850 | { |
Glenn Kasten | d96c572 | 2012-04-25 13:44:49 -0700 | [diff] [blame] | 2851 | return closeInput_nonvirtual(input); |
| 2852 | } |
| 2853 | |
| 2854 | status_t AudioFlinger::closeInput_nonvirtual(audio_io_handle_t input) |
| 2855 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2856 | // keep strong reference on the record thread so that |
| 2857 | // it is not destroyed while exit() is executed |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2858 | sp<RecordThread> recordThread; |
| 2859 | sp<MmapCaptureThread> mmapThread; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2860 | { |
| 2861 | Mutex::Autolock _l(mLock); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2862 | recordThread = checkRecordThread_l(input); |
| 2863 | if (recordThread != 0) { |
| 2864 | ALOGV("closeInput() %d", input); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2865 | |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2866 | dumpToThreadLog_l(recordThread); |
Andy Hung | 0264e7d | 2018-09-17 19:30:46 -0700 | [diff] [blame] | 2867 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2868 | // If we still have effect chains, it means that a client still holds a handle |
| 2869 | // on at least one effect. We must either move the chain to an existing thread with the |
| 2870 | // same session ID or put it aside in case a new record thread is opened for a |
| 2871 | // new capture on the same session |
| 2872 | sp<EffectChain> chain; |
| 2873 | { |
| 2874 | Mutex::Autolock _sl(recordThread->mLock); |
| 2875 | Vector< sp<EffectChain> > effectChains = recordThread->getEffectChains_l(); |
| 2876 | // Note: maximum one chain per record thread |
| 2877 | if (effectChains.size() != 0) { |
| 2878 | chain = effectChains[0]; |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 2879 | } |
| 2880 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2881 | if (chain != 0) { |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2882 | // 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] | 2883 | // This should only happen in case of overlap between one thread tear down and the |
| 2884 | // creation of its replacement |
| 2885 | size_t i; |
| 2886 | for (i = 0; i < mRecordThreads.size(); i++) { |
| 2887 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
| 2888 | if (t == recordThread) { |
| 2889 | continue; |
| 2890 | } |
| 2891 | if (t->hasAudioSession(chain->sessionId()) != 0) { |
| 2892 | Mutex::Autolock _l(t->mLock); |
| 2893 | ALOGV("closeInput() found thread %d for effect session %d", |
| 2894 | t->id(), chain->sessionId()); |
| 2895 | t->addEffectChain_l(chain); |
| 2896 | break; |
| 2897 | } |
| 2898 | } |
Glenn Kasten | d3bb645 | 2016-12-05 18:14:37 -0800 | [diff] [blame] | 2899 | // 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] | 2900 | if (i == mRecordThreads.size()) { |
| 2901 | putOrphanEffectChain_l(chain); |
| 2902 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2903 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2904 | mRecordThreads.removeItem(input); |
| 2905 | } else { |
| 2906 | mmapThread = (MmapCaptureThread *)checkMmapThread_l(input); |
| 2907 | if (mmapThread == 0) { |
| 2908 | return BAD_VALUE; |
| 2909 | } |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 2910 | dumpToThreadLog_l(mmapThread); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2911 | mMmapThreads.removeItem(input); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 2912 | } |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 2913 | const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor(); |
| 2914 | ioDesc->mIoHandle = input; |
| 2915 | ioConfigChanged(AUDIO_INPUT_CLOSED, ioDesc); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2916 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2917 | // FIXME: calling thread->exit() without mLock held should not be needed anymore now that |
| 2918 | // we have a different lock for notification client |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2919 | if (recordThread != 0) { |
| 2920 | closeInputFinish(recordThread); |
| 2921 | } else if (mmapThread != 0) { |
| 2922 | mmapThread->exit(); |
| 2923 | AudioStreamIn *in = mmapThread->clearInput(); |
| 2924 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
| 2925 | // from now on thread->mInput is NULL |
| 2926 | delete in; |
| 2927 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2928 | return NO_ERROR; |
| 2929 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2930 | |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2931 | void AudioFlinger::closeInputFinish(const sp<RecordThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2932 | { |
| 2933 | thread->exit(); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2934 | AudioStreamIn *in = thread->clearInput(); |
Glenn Kasten | 5798d4e | 2012-03-08 12:18:35 -0800 | [diff] [blame] | 2935 | ALOG_ASSERT(in != NULL, "in shouldn't be NULL"); |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 2936 | // from now on thread->mInput is NULL |
Dima Zavin | 799a70e | 2011-04-18 16:57:27 -0700 | [diff] [blame] | 2937 | delete in; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2938 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2939 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 2940 | void AudioFlinger::closeThreadInternal_l(const sp<RecordThread>& thread) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2941 | { |
| 2942 | mRecordThreads.removeItem(thread->mId); |
| 2943 | closeInputFinish(thread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2944 | } |
| 2945 | |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2946 | status_t AudioFlinger::invalidateStream(audio_stream_type_t stream) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2947 | { |
| 2948 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d2304db | 2014-02-03 07:40:31 -0800 | [diff] [blame] | 2949 | ALOGV("invalidateStream() stream %d", stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2950 | |
| 2951 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 2952 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | 2216785 | 2012-06-20 12:26:32 -0700 | [diff] [blame] | 2953 | thread->invalidateTracks(stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 2954 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2955 | for (size_t i = 0; i < mMmapThreads.size(); i++) { |
| 2956 | mMmapThreads[i]->invalidateTracks(stream); |
| 2957 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2958 | return NO_ERROR; |
| 2959 | } |
| 2960 | |
| 2961 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2962 | audio_unique_id_t AudioFlinger::newAudioUniqueId(audio_unique_id_use_t use) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2963 | { |
Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 2964 | // This is a binder API, so a malicious client could pass in a bad parameter. |
| 2965 | // Check for that before calling the internal API nextUniqueId(). |
| 2966 | if ((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX) { |
| 2967 | ALOGE("newAudioUniqueId invalid use %d", use); |
| 2968 | return AUDIO_UNIQUE_ID_ALLOCATE; |
| 2969 | } |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 2970 | return nextUniqueId(use); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 2973 | void AudioFlinger::acquireAudioSessionId( |
| 2974 | audio_session_t audioSession, pid_t pid, uid_t uid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 2975 | { |
| 2976 | Mutex::Autolock _l(mLock); |
Glenn Kasten | bb00192 | 2012-02-03 11:10:26 -0800 | [diff] [blame] | 2977 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2978 | ALOGV("acquiring %d from %d, for %d", audioSession, caller, pid); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2979 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 2980 | if (pid != (pid_t)-1 && isAudioServerOrMediaServerUid(callerUid)) { |
| 2981 | caller = pid; // check must match releaseAudioSessionId() |
| 2982 | } |
| 2983 | if (uid == (uid_t)-1 || !isAudioServerOrMediaServerUid(callerUid)) { |
| 2984 | uid = callerUid; |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 2985 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2986 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 2987 | { |
| 2988 | Mutex::Autolock _cl(mClientLock); |
| 2989 | // Ignore requests received from processes not known as notification client. The request |
| 2990 | // is likely proxied by mediaserver (e.g CameraService) and releaseAudioSessionId() can be |
| 2991 | // called from a different pid leaving a stale session reference. Also we don't know how |
| 2992 | // to clear this reference if the client process dies. |
| 2993 | if (mNotificationClients.indexOfKey(caller) < 0) { |
| 2994 | ALOGW("acquireAudioSessionId() unknown client %d for session %d", caller, audioSession); |
| 2995 | return; |
| 2996 | } |
Eric Laurent | d1b28d4 | 2013-09-18 18:47:13 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
Glenn Kasten | 8d6a244 | 2012-02-08 14:04:28 -0800 | [diff] [blame] | 2999 | size_t num = mAudioSessionRefs.size(); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3000 | for (size_t i = 0; i < num; i++) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3001 | AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 3002 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 3003 | ref->mCnt++; |
| 3004 | ALOGV(" incremented refcount to %d", ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3005 | return; |
| 3006 | } |
| 3007 | } |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 3008 | mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller, uid)); |
Glenn Kasten | 84afa3b | 2012-01-25 15:28:08 -0800 | [diff] [blame] | 3009 | ALOGV(" added new entry for %d", audioSession); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3012 | void AudioFlinger::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3013 | { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3014 | std::vector< sp<EffectModule> > removedEffects; |
| 3015 | { |
| 3016 | Mutex::Autolock _l(mLock); |
| 3017 | pid_t caller = IPCThreadState::self()->getCallingPid(); |
| 3018 | ALOGV("releasing %d from %d for %d", audioSession, caller, pid); |
| 3019 | const uid_t callerUid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 3020 | if (pid != (pid_t)-1 && isAudioServerOrMediaServerUid(callerUid)) { |
| 3021 | caller = pid; // check must match acquireAudioSessionId() |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3022 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3023 | size_t num = mAudioSessionRefs.size(); |
| 3024 | for (size_t i = 0; i < num; i++) { |
| 3025 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 3026 | if (ref->mSessionid == audioSession && ref->mPid == caller) { |
| 3027 | ref->mCnt--; |
| 3028 | ALOGV(" decremented refcount to %d", ref->mCnt); |
| 3029 | if (ref->mCnt == 0) { |
| 3030 | mAudioSessionRefs.removeAt(i); |
| 3031 | delete ref; |
| 3032 | std::vector< sp<EffectModule> > effects = purgeStaleEffects_l(); |
| 3033 | removedEffects.insert(removedEffects.end(), effects.begin(), effects.end()); |
| 3034 | } |
| 3035 | goto Exit; |
| 3036 | } |
| 3037 | } |
| 3038 | // If the caller is audioserver it is likely that the session being released was acquired |
| 3039 | // on behalf of a process not in notification clients and we ignore the warning. |
| 3040 | ALOGW_IF(!isAudioServerUid(callerUid), |
| 3041 | "session id %d not found for pid %d", audioSession, caller); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3042 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3043 | |
| 3044 | Exit: |
| 3045 | for (auto& effect : removedEffects) { |
| 3046 | effect->updatePolicyState(); |
| 3047 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3050 | bool AudioFlinger::isSessionAcquired_l(audio_session_t audioSession) |
| 3051 | { |
| 3052 | size_t num = mAudioSessionRefs.size(); |
| 3053 | for (size_t i = 0; i < num; i++) { |
| 3054 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(i); |
| 3055 | if (ref->mSessionid == audioSession) { |
| 3056 | return true; |
| 3057 | } |
| 3058 | } |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3062 | std::vector<sp<AudioFlinger::EffectModule>> AudioFlinger::purgeStaleEffects_l() { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3063 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3064 | ALOGV("purging stale effects"); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3065 | |
| 3066 | Vector< sp<EffectChain> > chains; |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3067 | std::vector< sp<EffectModule> > removedEffects; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3068 | |
| 3069 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3070 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
Mateusz Kaplon | 2a6e1b0 | 2017-03-30 16:50:50 +0200 | [diff] [blame] | 3071 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3072 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 3073 | sp<EffectChain> ec = t->mEffectChains[j]; |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3074 | if (!audio_is_global_session(ec->sessionId())) { |
Marco Nelissen | 0270b18 | 2011-08-12 14:14:39 -0700 | [diff] [blame] | 3075 | chains.push(ec); |
| 3076 | } |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3077 | } |
| 3078 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3079 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3080 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 3081 | sp<RecordThread> t = mRecordThreads.valueAt(i); |
Mateusz Kaplon | 2a6e1b0 | 2017-03-30 16:50:50 +0200 | [diff] [blame] | 3082 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3083 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 3084 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 3085 | chains.push(ec); |
| 3086 | } |
| 3087 | } |
| 3088 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3089 | for (size_t i = 0; i < mMmapThreads.size(); i++) { |
| 3090 | sp<MmapThread> t = mMmapThreads.valueAt(i); |
| 3091 | Mutex::Autolock _l(t->mLock); |
| 3092 | for (size_t j = 0; j < t->mEffectChains.size(); j++) { |
| 3093 | sp<EffectChain> ec = t->mEffectChains[j]; |
| 3094 | chains.push(ec); |
| 3095 | } |
| 3096 | } |
| 3097 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3098 | for (size_t i = 0; i < chains.size(); i++) { |
| 3099 | sp<EffectChain> ec = chains[i]; |
| 3100 | int sessionid = ec->sessionId(); |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3101 | sp<ThreadBase> t = ec->thread().promote(); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3102 | if (t == 0) { |
| 3103 | continue; |
| 3104 | } |
| 3105 | size_t numsessionrefs = mAudioSessionRefs.size(); |
| 3106 | bool found = false; |
| 3107 | for (size_t k = 0; k < numsessionrefs; k++) { |
| 3108 | AudioSessionRef *ref = mAudioSessionRefs.itemAt(k); |
Glenn Kasten | 012ca6b | 2012-03-06 11:22:01 -0800 | [diff] [blame] | 3109 | if (ref->mSessionid == sessionid) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3110 | ALOGV(" session %d still exists for %d with %d refs", |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 3111 | sessionid, ref->mPid, ref->mCnt); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3112 | found = true; |
| 3113 | break; |
| 3114 | } |
| 3115 | } |
| 3116 | if (!found) { |
Glenn Kasten | e198c36 | 2013-08-13 09:13:36 -0700 | [diff] [blame] | 3117 | Mutex::Autolock _l(t->mLock); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3118 | // remove all effects from the chain |
| 3119 | while (ec->mEffects.size()) { |
| 3120 | sp<EffectModule> effect = ec->mEffects[0]; |
| 3121 | effect->unPin(); |
Mikhail Naganov | 424c4f5 | 2017-07-19 17:54:29 -0700 | [diff] [blame] | 3122 | t->removeEffect_l(effect, /*release*/ true); |
Eric Laurent | a5f44eb | 2012-06-25 11:38:29 -0700 | [diff] [blame] | 3123 | if (effect->purgeHandles()) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3124 | effect->checkSuspendOnEffectEnabled(false, true /*threadLocked*/); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3125 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3126 | removedEffects.push_back(effect); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3130 | return removedEffects; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Andy Hung | dc099c2 | 2018-09-18 13:46:39 -0700 | [diff] [blame] | 3133 | // dumpToThreadLog_l() must be called with AudioFlinger::mLock held |
| 3134 | void AudioFlinger::dumpToThreadLog_l(const sp<ThreadBase> &thread) |
| 3135 | { |
| 3136 | audio_utils::FdToString fdToString; |
| 3137 | const int fd = fdToString.fd(); |
| 3138 | if (fd >= 0) { |
| 3139 | thread->dump(fd, {} /* args */); |
| 3140 | mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose()); |
| 3141 | } |
| 3142 | } |
| 3143 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3144 | // checkThread_l() must be called with AudioFlinger::mLock held |
| 3145 | AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const |
| 3146 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3147 | ThreadBase *thread = checkMmapThread_l(ioHandle); |
| 3148 | if (thread == 0) { |
| 3149 | switch (audio_unique_id_get_use(ioHandle)) { |
| 3150 | case AUDIO_UNIQUE_ID_USE_OUTPUT: |
| 3151 | thread = checkPlaybackThread_l(ioHandle); |
| 3152 | break; |
| 3153 | case AUDIO_UNIQUE_ID_USE_INPUT: |
| 3154 | thread = checkRecordThread_l(ioHandle); |
| 3155 | break; |
| 3156 | default: |
| 3157 | break; |
| 3158 | } |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3159 | } |
| 3160 | return thread; |
| 3161 | } |
| 3162 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3163 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3164 | AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3165 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3166 | return mPlaybackThreads.valueFor(output).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | // checkMixerThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3170 | AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3171 | { |
| 3172 | PlaybackThread *thread = checkPlaybackThread_l(output); |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3173 | return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | // checkRecordThread_l() must be called with AudioFlinger::mLock held |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3177 | AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3178 | { |
Glenn Kasten | a111792 | 2012-01-26 10:53:32 -0800 | [diff] [blame] | 3179 | return mRecordThreads.valueFor(input).get(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3180 | } |
| 3181 | |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3182 | // checkMmapThread_l() must be called with AudioFlinger::mLock held |
| 3183 | AudioFlinger::MmapThread *AudioFlinger::checkMmapThread_l(audio_io_handle_t io) const |
| 3184 | { |
| 3185 | return mMmapThreads.valueFor(io).get(); |
| 3186 | } |
| 3187 | |
| 3188 | |
| 3189 | // checkPlaybackThread_l() must be called with AudioFlinger::mLock held |
| 3190 | AudioFlinger::VolumeInterface *AudioFlinger::getVolumeInterface_l(audio_io_handle_t output) const |
| 3191 | { |
Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3192 | VolumeInterface *volumeInterface = mPlaybackThreads.valueFor(output).get(); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3193 | if (volumeInterface == nullptr) { |
| 3194 | MmapThread *mmapThread = mMmapThreads.valueFor(output).get(); |
| 3195 | if (mmapThread != nullptr) { |
| 3196 | if (mmapThread->isOutput()) { |
Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3197 | MmapPlaybackThread *mmapPlaybackThread = |
| 3198 | static_cast<MmapPlaybackThread *>(mmapThread); |
| 3199 | volumeInterface = mmapPlaybackThread; |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | return volumeInterface; |
| 3204 | } |
| 3205 | |
| 3206 | Vector <AudioFlinger::VolumeInterface *> AudioFlinger::getAllVolumeInterfaces_l() const |
| 3207 | { |
| 3208 | Vector <VolumeInterface *> volumeInterfaces; |
| 3209 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3210 | volumeInterfaces.add(mPlaybackThreads.valueAt(i).get()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3211 | } |
| 3212 | for (size_t i = 0; i < mMmapThreads.size(); i++) { |
| 3213 | if (mMmapThreads.valueAt(i)->isOutput()) { |
Eric Laurent | 7459b90 | 2017-04-19 18:10:41 -0700 | [diff] [blame] | 3214 | MmapPlaybackThread *mmapPlaybackThread = |
| 3215 | static_cast<MmapPlaybackThread *>(mMmapThreads.valueAt(i).get()); |
| 3216 | volumeInterfaces.add(mmapPlaybackThread); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | return volumeInterfaces; |
| 3220 | } |
| 3221 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3222 | audio_unique_id_t AudioFlinger::nextUniqueId(audio_unique_id_use_t use) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3223 | { |
Glenn Kasten | 9d00313 | 2016-04-06 14:38:09 -0700 | [diff] [blame] | 3224 | // 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] | 3225 | LOG_ALWAYS_FATAL_IF((unsigned) use >= (unsigned) AUDIO_UNIQUE_ID_USE_MAX); |
Glenn Kasten | d2e67e1 | 2016-04-11 08:26:37 -0700 | [diff] [blame] | 3226 | const int maxRetries = use == AUDIO_UNIQUE_ID_USE_SESSION ? 3 : 1; |
| 3227 | for (int retry = 0; retry < maxRetries; retry++) { |
| 3228 | // The cast allows wraparound from max positive to min negative instead of abort |
| 3229 | uint32_t base = (uint32_t) atomic_fetch_add_explicit(&mNextUniqueIds[use], |
| 3230 | (uint_fast32_t) AUDIO_UNIQUE_ID_USE_MAX, memory_order_acq_rel); |
| 3231 | ALOG_ASSERT(audio_unique_id_get_use(base) == AUDIO_UNIQUE_ID_USE_UNSPECIFIED); |
| 3232 | // allow wrap by skipping 0 and -1 for session ids |
| 3233 | if (!(base == 0 || base == (~0u & ~AUDIO_UNIQUE_ID_USE_MASK))) { |
| 3234 | ALOGW_IF(retry != 0, "unique ID overflow for use %d", use); |
| 3235 | return (audio_unique_id_t) (base | use); |
| 3236 | } |
| 3237 | } |
| 3238 | // We have no way of recovering from wraparound |
| 3239 | LOG_ALWAYS_FATAL("unique ID overflow for use %d", use); |
| 3240 | // TODO Use a floor after wraparound. This may need a mutex. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3241 | } |
| 3242 | |
Glenn Kasten | 02fe1bf | 2012-02-24 15:42:17 -0800 | [diff] [blame] | 3243 | AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3244 | { |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 3245 | AutoMutex lock(mHardwareLock); |
| 3246 | if (mPrimaryHardwareDev == nullptr) { |
| 3247 | return nullptr; |
| 3248 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3249 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3250 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
Eric Laurent | f6870ae | 2015-05-08 10:50:03 -0700 | [diff] [blame] | 3251 | if(thread->isDuplicating()) { |
| 3252 | continue; |
| 3253 | } |
Eric Laurent | b8ba0a9 | 2011-08-07 16:32:26 -0700 | [diff] [blame] | 3254 | AudioStreamOut *output = thread->getOutput(); |
John Grossman | ee578c0 | 2012-07-23 17:05:46 -0700 | [diff] [blame] | 3255 | if (output != NULL && output->audioHwDev == mPrimaryHardwareDev) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3256 | return thread; |
| 3257 | } |
| 3258 | } |
Eric Laurent | 00abf0d | 2020-04-22 19:28:22 -0700 | [diff] [blame] | 3259 | return nullptr; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3262 | DeviceTypeSet AudioFlinger::primaryOutputDevice_l() const |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3263 | { |
| 3264 | PlaybackThread *thread = primaryPlaybackThread_l(); |
| 3265 | |
| 3266 | if (thread == NULL) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3267 | return DeviceTypeSet(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3268 | } |
| 3269 | |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 3270 | return thread->outDeviceTypes(); |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
Glenn Kasten | a733563 | 2016-06-09 17:09:53 -0700 | [diff] [blame] | 3273 | AudioFlinger::PlaybackThread *AudioFlinger::fastPlaybackThread_l() const |
| 3274 | { |
| 3275 | size_t minFrameCount = 0; |
| 3276 | PlaybackThread *minThread = NULL; |
| 3277 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3278 | PlaybackThread *thread = mPlaybackThreads.valueAt(i).get(); |
| 3279 | if (!thread->isDuplicating()) { |
| 3280 | size_t frameCount = thread->frameCountHAL(); |
| 3281 | if (frameCount != 0 && (minFrameCount == 0 || frameCount < minFrameCount || |
| 3282 | (frameCount == minFrameCount && thread->hasFastMixer() && |
| 3283 | /*minThread != NULL &&*/ !minThread->hasFastMixer()))) { |
| 3284 | minFrameCount = frameCount; |
| 3285 | minThread = thread; |
| 3286 | } |
| 3287 | } |
| 3288 | } |
| 3289 | return minThread; |
| 3290 | } |
| 3291 | |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3292 | sp<AudioFlinger::SyncEvent> AudioFlinger::createSyncEvent(AudioSystem::sync_event_t type, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3293 | audio_session_t triggerSession, |
| 3294 | audio_session_t listenerSession, |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3295 | sync_event_callback_t callBack, |
Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3296 | const wp<RefBase>& cookie) |
Eric Laurent | a011e35 | 2012-03-29 15:51:43 -0700 | [diff] [blame] | 3297 | { |
| 3298 | Mutex::Autolock _l(mLock); |
| 3299 | |
| 3300 | sp<SyncEvent> event = new SyncEvent(type, triggerSession, listenerSession, callBack, cookie); |
| 3301 | status_t playStatus = NAME_NOT_FOUND; |
| 3302 | status_t recStatus = NAME_NOT_FOUND; |
| 3303 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3304 | playStatus = mPlaybackThreads.valueAt(i)->setSyncEvent(event); |
| 3305 | if (playStatus == NO_ERROR) { |
| 3306 | return event; |
| 3307 | } |
| 3308 | } |
| 3309 | for (size_t i = 0; i < mRecordThreads.size(); i++) { |
| 3310 | recStatus = mRecordThreads.valueAt(i)->setSyncEvent(event); |
| 3311 | if (recStatus == NO_ERROR) { |
| 3312 | return event; |
| 3313 | } |
| 3314 | } |
| 3315 | if (playStatus == NAME_NOT_FOUND || recStatus == NAME_NOT_FOUND) { |
| 3316 | mPendingSyncEvents.add(event); |
| 3317 | } else { |
| 3318 | ALOGV("createSyncEvent() invalid event %d", event->type()); |
| 3319 | event.clear(); |
| 3320 | } |
| 3321 | return event; |
| 3322 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3323 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3324 | // ---------------------------------------------------------------------------- |
| 3325 | // Effect management |
| 3326 | // ---------------------------------------------------------------------------- |
| 3327 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3328 | sp<EffectsFactoryHalInterface> AudioFlinger::getEffectsFactory() { |
| 3329 | return mEffectsFactoryHal; |
| 3330 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3331 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 3332 | status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3333 | { |
| 3334 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3335 | if (mEffectsFactoryHal.get()) { |
| 3336 | return mEffectsFactoryHal->queryNumberEffects(numEffects); |
| 3337 | } else { |
| 3338 | return -ENODEV; |
| 3339 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
Glenn Kasten | f587ba5 | 2012-01-26 16:25:10 -0800 | [diff] [blame] | 3342 | status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3343 | { |
| 3344 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3345 | if (mEffectsFactoryHal.get()) { |
| 3346 | return mEffectsFactoryHal->getDescriptor(index, descriptor); |
| 3347 | } else { |
| 3348 | return -ENODEV; |
| 3349 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3350 | } |
| 3351 | |
Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 3352 | status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid, |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3353 | const effect_uuid_t *pTypeUuid, |
| 3354 | uint32_t preferredTypeFlag, |
| 3355 | effect_descriptor_t *descriptor) const |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3356 | { |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3357 | if (pUuid == NULL || pTypeUuid == NULL || descriptor == NULL) { |
| 3358 | return BAD_VALUE; |
| 3359 | } |
| 3360 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3361 | Mutex::Autolock _l(mLock); |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3362 | |
| 3363 | if (!mEffectsFactoryHal.get()) { |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3364 | return -ENODEV; |
| 3365 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3366 | |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3367 | status_t status = NO_ERROR; |
| 3368 | if (!EffectsFactoryHalInterface::isNullUuid(pUuid)) { |
| 3369 | // If uuid is specified, request effect descriptor from that. |
| 3370 | status = mEffectsFactoryHal->getDescriptor(pUuid, descriptor); |
| 3371 | } else if (!EffectsFactoryHalInterface::isNullUuid(pTypeUuid)) { |
| 3372 | // If uuid is not specified, look for an available implementation |
| 3373 | // of the required type instead. |
| 3374 | |
| 3375 | // Use a temporary descriptor to avoid modifying |descriptor| in the failure case. |
| 3376 | effect_descriptor_t desc; |
| 3377 | desc.flags = 0; // prevent compiler warning |
| 3378 | |
| 3379 | uint32_t numEffects = 0; |
| 3380 | status = mEffectsFactoryHal->queryNumberEffects(&numEffects); |
| 3381 | if (status < 0) { |
| 3382 | ALOGW("getEffectDescriptor() error %d from FactoryHal queryNumberEffects", status); |
| 3383 | return status; |
| 3384 | } |
| 3385 | |
| 3386 | bool found = false; |
| 3387 | for (uint32_t i = 0; i < numEffects; i++) { |
| 3388 | status = mEffectsFactoryHal->getDescriptor(i, &desc); |
| 3389 | if (status < 0) { |
| 3390 | ALOGW("getEffectDescriptor() error %d from FactoryHal getDescriptor", status); |
| 3391 | continue; |
| 3392 | } |
| 3393 | if (memcmp(&desc.type, pTypeUuid, sizeof(effect_uuid_t)) == 0) { |
| 3394 | // If matching type found save effect descriptor. |
| 3395 | found = true; |
| 3396 | *descriptor = desc; |
| 3397 | |
| 3398 | // If there's no preferred flag or this descriptor matches the preferred |
| 3399 | // flag, success! If this descriptor doesn't match the preferred |
| 3400 | // flag, continue enumeration in case a better matching version of this |
| 3401 | // effect type is available. Note that this means if no effect with a |
| 3402 | // correct flag is found, the descriptor returned will correspond to the |
| 3403 | // last effect that at least had a matching type uuid (if any). |
| 3404 | if (preferredTypeFlag == EFFECT_FLAG_TYPE_MASK || |
| 3405 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == preferredTypeFlag) { |
| 3406 | break; |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | if (!found) { |
| 3412 | status = NAME_NOT_FOUND; |
| 3413 | ALOGW("getEffectDescriptor(): Effect not found by type."); |
| 3414 | } |
| 3415 | } else { |
| 3416 | status = BAD_VALUE; |
| 3417 | ALOGE("getEffectDescriptor(): Either uuid or type uuid must be non-null UUIDs."); |
| 3418 | } |
| 3419 | return status; |
| 3420 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3421 | |
Glenn Kasten | 8d6cc84 | 2012-02-03 11:06:53 -0800 | [diff] [blame] | 3422 | sp<IEffect> AudioFlinger::createEffect( |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3423 | effect_descriptor_t *pDesc, |
| 3424 | const sp<IEffectClient>& effectClient, |
| 3425 | int32_t priority, |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 3426 | audio_io_handle_t io, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3427 | audio_session_t sessionId, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3428 | const AudioDeviceTypeAddr& device, |
Svet Ganov | be71aa2 | 2015-04-28 12:06:02 -0700 | [diff] [blame] | 3429 | const String16& opPackageName, |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3430 | pid_t pid, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 3431 | bool probe, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3432 | status_t *status, |
| 3433 | int *id, |
| 3434 | int *enabled) |
| 3435 | { |
| 3436 | status_t lStatus = NO_ERROR; |
| 3437 | sp<EffectHandle> handle; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3438 | effect_descriptor_t desc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3439 | |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3440 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3441 | if (pid == -1 || !isAudioServerOrMediaServerUid(callingUid)) { |
Eric Laurent | b643627 | 2016-12-07 19:24:50 -0800 | [diff] [blame] | 3442 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 3443 | ALOGW_IF(pid != -1 && pid != callingPid, |
| 3444 | "%s uid %d pid %d tried to pass itself off as pid %d", |
| 3445 | __func__, callingUid, callingPid, pid); |
| 3446 | pid = callingPid; |
| 3447 | } |
| 3448 | |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3449 | ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d, factory %p", |
| 3450 | pid, effectClient.get(), priority, sessionId, io, mEffectsFactoryHal.get()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3451 | |
| 3452 | if (pDesc == NULL) { |
| 3453 | lStatus = BAD_VALUE; |
| 3454 | goto Exit; |
| 3455 | } |
| 3456 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 3457 | if (mEffectsFactoryHal == 0) { |
Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3458 | ALOGE("%s: no effects factory hal", __func__); |
Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 3459 | lStatus = NO_INIT; |
| 3460 | goto Exit; |
| 3461 | } |
| 3462 | |
Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3463 | // check audio settings permission for global effects |
| 3464 | if (sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
| 3465 | if (!settingsAllowed()) { |
| 3466 | ALOGE("%s: no permission for AUDIO_SESSION_OUTPUT_MIX", __func__); |
| 3467 | lStatus = PERMISSION_DENIED; |
| 3468 | goto Exit; |
| 3469 | } |
| 3470 | } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) { |
| 3471 | if (!isAudioServerUid(callingUid)) { |
| 3472 | ALOGE("%s: only APM can create using AUDIO_SESSION_OUTPUT_STAGE", __func__); |
| 3473 | lStatus = PERMISSION_DENIED; |
| 3474 | goto Exit; |
| 3475 | } |
| 3476 | |
| 3477 | if (io == AUDIO_IO_HANDLE_NONE) { |
| 3478 | ALOGE("%s: APM must specify output when using AUDIO_SESSION_OUTPUT_STAGE", __func__); |
| 3479 | lStatus = BAD_VALUE; |
| 3480 | goto Exit; |
| 3481 | } |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3482 | } else if (sessionId == AUDIO_SESSION_DEVICE) { |
| 3483 | if (!modifyDefaultAudioEffectsAllowed(pid, callingUid)) { |
| 3484 | ALOGE("%s: device effect permission denied for uid %d", __func__, callingUid); |
| 3485 | lStatus = PERMISSION_DENIED; |
| 3486 | goto Exit; |
| 3487 | } |
Eric Laurent | 9487603 | 2019-11-13 12:45:28 -0800 | [diff] [blame] | 3488 | if (io != AUDIO_IO_HANDLE_NONE) { |
| 3489 | ALOGE("%s: io handle should not be specified for device effect", __func__); |
| 3490 | lStatus = BAD_VALUE; |
| 3491 | goto Exit; |
| 3492 | } |
Andy Hung | 6096dcd | 2019-03-13 13:20:30 -0700 | [diff] [blame] | 3493 | } else { |
| 3494 | // general sessionId. |
| 3495 | |
| 3496 | if (audio_unique_id_get_use(sessionId) != AUDIO_UNIQUE_ID_USE_SESSION) { |
| 3497 | ALOGE("%s: invalid sessionId %d", __func__, sessionId); |
| 3498 | lStatus = BAD_VALUE; |
| 3499 | goto Exit; |
| 3500 | } |
| 3501 | |
| 3502 | // TODO: should we check if the callingUid (limited to pid) is in mAudioSessionRefs |
| 3503 | // to prevent creating an effect when one doesn't actually have track with that session? |
| 3504 | } |
| 3505 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3506 | { |
Ari Hausman-Cohen | 2046ec7 | 2018-04-24 14:00:55 -0700 | [diff] [blame] | 3507 | // Get the full effect descriptor from the uuid/type. |
| 3508 | // If the session is the output mix, prefer an auxiliary effect, |
| 3509 | // otherwise no preference. |
| 3510 | uint32_t preferredType = (sessionId == AUDIO_SESSION_OUTPUT_MIX ? |
| 3511 | EFFECT_FLAG_TYPE_AUXILIARY : EFFECT_FLAG_TYPE_MASK); |
| 3512 | lStatus = getEffectDescriptor(&pDesc->uuid, &pDesc->type, preferredType, &desc); |
| 3513 | if (lStatus < 0) { |
| 3514 | ALOGW("createEffect() error %d from getEffectDescriptor", lStatus); |
| 3515 | goto Exit; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | // 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] | 3519 | if (sessionId != AUDIO_SESSION_OUTPUT_MIX && |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3520 | (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) { |
| 3521 | lStatus = INVALID_OPERATION; |
| 3522 | goto Exit; |
| 3523 | } |
| 3524 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3525 | // check recording permission for visualizer |
| 3526 | if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) && |
Svet Ganov | 6e64137 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 3527 | // TODO: Do we need to start/stop op - i.e. is there recording being performed? |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3528 | !recordingAllowed(opPackageName, pid, callingUid)) { |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3529 | lStatus = PERMISSION_DENIED; |
| 3530 | goto Exit; |
| 3531 | } |
| 3532 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3533 | // return effect descriptor |
Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 3534 | *pDesc = desc; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 3535 | if (io == AUDIO_IO_HANDLE_NONE && sessionId == AUDIO_SESSION_OUTPUT_MIX) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 3536 | // if the output returned by getOutputForEffect() is removed before we lock the |
| 3537 | // mutex below, the call to checkPlaybackThread_l(io) below will detect it |
| 3538 | // and we will exit safely |
| 3539 | io = AudioSystem::getOutputForEffect(&desc); |
| 3540 | ALOGV("createEffect got output %d", io); |
| 3541 | } |
| 3542 | |
| 3543 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3544 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3545 | if (sessionId == AUDIO_SESSION_DEVICE) { |
| 3546 | sp<Client> client = registerPid(pid); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 3547 | ALOGV("%s device type %#x address %s", __func__, device.mType, device.getAddress()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3548 | handle = mDeviceEffectManager.createEffect_l( |
| 3549 | &desc, device, client, effectClient, mPatchPanel.patches_l(), |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 3550 | enabled, &lStatus, probe); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3551 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
| 3552 | // remove local strong reference to Client with mClientLock held |
| 3553 | Mutex::Autolock _cl(mClientLock); |
| 3554 | client.clear(); |
| 3555 | } else { |
| 3556 | // handle must be valid here, but check again to be safe. |
| 3557 | if (handle.get() != nullptr && id != nullptr) *id = handle->id(); |
| 3558 | } |
| 3559 | goto Register; |
| 3560 | } |
| 3561 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3562 | // If output is not specified try to find a matching audio session ID in one of the |
| 3563 | // output threads. |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3564 | // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX |
| 3565 | // because of code checking output when entering the function. |
Andy Hung | 444bb55 | 2019-03-14 17:06:39 -0700 | [diff] [blame] | 3566 | // Note: io is never AUDIO_IO_HANDLE_NONE when creating an effect on an input by APM. |
| 3567 | // 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] | 3568 | if (io == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | eb3c337 | 2013-09-25 12:25:29 -0700 | [diff] [blame] | 3569 | // look for the thread where the specified audio session is present |
Andy Hung | e778c42 | 2019-03-14 15:04:30 -0700 | [diff] [blame] | 3570 | io = findIoHandleBySessionId_l(sessionId, mPlaybackThreads); |
| 3571 | if (io == AUDIO_IO_HANDLE_NONE) { |
| 3572 | io = findIoHandleBySessionId_l(sessionId, mRecordThreads); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3573 | } |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3574 | if (io == AUDIO_IO_HANDLE_NONE) { |
Andy Hung | e778c42 | 2019-03-14 15:04:30 -0700 | [diff] [blame] | 3575 | io = findIoHandleBySessionId_l(sessionId, mMmapThreads); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3576 | } |
Andy Hung | 444bb55 | 2019-03-14 17:06:39 -0700 | [diff] [blame] | 3577 | |
| 3578 | // If you wish to create a Record preprocessing AudioEffect in Java, |
| 3579 | // you MUST create an AudioRecord first and keep it alive so it is picked up above. |
| 3580 | // Otherwise it will fail when created on a Playback thread by legacy |
| 3581 | // handling below. Ditto with Mmap, the associated Mmap track must be created |
| 3582 | // before creating the AudioEffect or the io handle must be specified. |
| 3583 | // |
| 3584 | // Detect if the effect is created after an AudioRecord is destroyed. |
| 3585 | if (getOrphanEffectChain_l(sessionId).get() != nullptr) { |
| 3586 | ALOGE("%s: effect %s with no specified io handle is denied because the AudioRecord" |
| 3587 | " for session %d no longer exists", |
| 3588 | __func__, desc.name, sessionId); |
| 3589 | lStatus = PERMISSION_DENIED; |
| 3590 | goto Exit; |
| 3591 | } |
| 3592 | |
| 3593 | // Legacy handling of creating an effect on an expired or made-up |
| 3594 | // session id. We think that it is a Playback effect. |
| 3595 | // |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3596 | // If no output thread contains the requested session ID, default to |
| 3597 | // first output. The effect chain will be moved to the correct output |
| 3598 | // thread when a track with the same session ID is created |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 3599 | if (io == AUDIO_IO_HANDLE_NONE && mPlaybackThreads.size() > 0) { |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3600 | io = mPlaybackThreads.keyAt(0); |
| 3601 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3602 | ALOGV("createEffect() got io %d for effect %s", io, desc.name); |
Andy Hung | 1631f06 | 2019-03-12 19:39:03 -0700 | [diff] [blame] | 3603 | } else if (checkPlaybackThread_l(io) != nullptr) { |
| 3604 | // allow only one effect chain per sessionId on mPlaybackThreads. |
| 3605 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3606 | const audio_io_handle_t checkIo = mPlaybackThreads.keyAt(i); |
| 3607 | if (io == checkIo) continue; |
| 3608 | const uint32_t sessionType = |
| 3609 | mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId); |
| 3610 | if ((sessionType & ThreadBase::EFFECT_SESSION) != 0) { |
| 3611 | ALOGE("%s: effect %s io %d denied because session %d effect exists on io %d", |
| 3612 | __func__, desc.name, (int)io, (int)sessionId, (int)checkIo); |
| 3613 | android_errorWriteLog(0x534e4554, "123237974"); |
| 3614 | lStatus = BAD_VALUE; |
| 3615 | goto Exit; |
| 3616 | } |
| 3617 | } |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 3618 | } |
| 3619 | ThreadBase *thread = checkRecordThread_l(io); |
| 3620 | if (thread == NULL) { |
| 3621 | thread = checkPlaybackThread_l(io); |
| 3622 | if (thread == NULL) { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3623 | thread = checkMmapThread_l(io); |
| 3624 | if (thread == NULL) { |
| 3625 | ALOGE("createEffect() unknown output thread"); |
| 3626 | lStatus = BAD_VALUE; |
| 3627 | goto Exit; |
| 3628 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3629 | } |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3630 | } else { |
| 3631 | // Check if one effect chain was awaiting for an effect to be created on this |
| 3632 | // session and used it instead of creating a new one. |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3633 | sp<EffectChain> chain = getOrphanEffectChain_l(sessionId); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3634 | if (chain != 0) { |
Eric Laurent | 1b92868 | 2014-10-02 19:41:47 -0700 | [diff] [blame] | 3635 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3636 | thread->addEffectChain_l(chain); |
| 3637 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3638 | } |
Eric Laurent | 84e9a10 | 2010-09-23 16:10:16 -0700 | [diff] [blame] | 3639 | |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 3640 | sp<Client> client = registerPid(pid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3641 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3642 | // create effect on selected output thread |
Eric Laurent | 3f75a5b | 2019-11-12 15:55:51 -0800 | [diff] [blame] | 3643 | bool pinned = !audio_is_global_session(sessionId) && isSessionAcquired_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3644 | handle = thread->createEffect_l(client, effectClient, priority, sessionId, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 3645 | &desc, enabled, &lStatus, pinned, probe); |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3646 | if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) { |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 3647 | // remove local strong reference to Client with mClientLock held |
| 3648 | Mutex::Autolock _cl(mClientLock); |
| 3649 | client.clear(); |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3650 | } else { |
| 3651 | // handle must be valid here, but check again to be safe. |
| 3652 | if (handle.get() != nullptr && id != nullptr) *id = handle->id(); |
Eric Laurent | fe1a94e | 2014-05-26 16:03:08 -0700 | [diff] [blame] | 3653 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3656 | Register: |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 3657 | if (!probe && (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS)) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3658 | // Check CPU and memory usage |
Eric Laurent | 4170955 | 2019-12-16 19:34:05 -0800 | [diff] [blame] | 3659 | sp<EffectBase> effect = handle->effect().promote(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3660 | if (effect != nullptr) { |
| 3661 | status_t rStatus = effect->updatePolicyState(); |
| 3662 | if (rStatus != NO_ERROR) { |
| 3663 | lStatus = rStatus; |
| 3664 | } |
| 3665 | } |
| 3666 | } else { |
haobo10173529 | 5ff7b0d | 2017-03-17 17:44:03 +0800 | [diff] [blame] | 3667 | handle.clear(); |
| 3668 | } |
| 3669 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3670 | Exit: |
Glenn Kasten | 9156ef3 | 2013-08-06 15:39:08 -0700 | [diff] [blame] | 3671 | *status = lStatus; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3672 | return handle; |
| 3673 | } |
| 3674 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3675 | 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] | 3676 | audio_io_handle_t dstOutput) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3677 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3678 | ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3679 | sessionId, srcOutput, dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3680 | Mutex::Autolock _l(mLock); |
| 3681 | if (srcOutput == dstOutput) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3682 | ALOGW("moveEffects() same dst and src outputs %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3683 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3684 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3685 | PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput); |
| 3686 | if (srcThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3687 | ALOGW("moveEffects() bad srcOutput %d", srcOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3688 | return BAD_VALUE; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3689 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3690 | PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput); |
| 3691 | if (dstThread == NULL) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3692 | ALOGW("moveEffects() bad dstOutput %d", dstOutput); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3693 | return BAD_VALUE; |
| 3694 | } |
| 3695 | |
| 3696 | Mutex::Autolock _dl(dstThread->mLock); |
| 3697 | Mutex::Autolock _sl(srcThread->mLock); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3698 | return moveEffectChain_l(sessionId, srcThread, dstThread); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3699 | } |
| 3700 | |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 3701 | |
| 3702 | void AudioFlinger::setEffectSuspended(int effectId, |
| 3703 | audio_session_t sessionId, |
| 3704 | bool suspended) |
| 3705 | { |
| 3706 | Mutex::Autolock _l(mLock); |
| 3707 | |
| 3708 | sp<ThreadBase> thread = getEffectThread_l(sessionId, effectId); |
| 3709 | if (thread == nullptr) { |
| 3710 | return; |
| 3711 | } |
| 3712 | Mutex::Autolock _sl(thread->mLock); |
| 3713 | sp<EffectModule> effect = thread->getEffect_l(sessionId, effectId); |
| 3714 | thread->setEffectSuspended_l(&effect->desc().type, suspended, sessionId); |
| 3715 | } |
| 3716 | |
| 3717 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3718 | // moveEffectChain_l must be called with both srcThread and dstThread mLocks held |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3719 | status_t AudioFlinger::moveEffectChain_l(audio_session_t sessionId, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3720 | AudioFlinger::PlaybackThread *srcThread, |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3721 | AudioFlinger::PlaybackThread *dstThread) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3722 | { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 3723 | ALOGV("moveEffectChain_l() session %d from thread %p to thread %p", |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3724 | sessionId, srcThread, dstThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3725 | |
Eric Laurent | 59255e4 | 2011-07-27 19:49:51 -0700 | [diff] [blame] | 3726 | sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3727 | if (chain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3728 | 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] | 3729 | sessionId, srcThread); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3730 | return INVALID_OPERATION; |
| 3731 | } |
| 3732 | |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 3733 | // Check whether the destination thread and all effects in the chain are compatible |
| 3734 | if (!chain->isCompatibleWithThread_l(dstThread)) { |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3735 | ALOGW("moveEffectChain_l() effect chain failed because" |
Eric Laurent | 4c41506 | 2016-06-17 16:14:16 -0700 | [diff] [blame] | 3736 | " destination thread %p is not compatible with effects in the chain", |
| 3737 | dstThread); |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 3738 | return INVALID_OPERATION; |
| 3739 | } |
| 3740 | |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3741 | // 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] | 3742 | // 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] | 3743 | // 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] | 3744 | // removed. |
| 3745 | srcThread->removeEffectChain_l(chain); |
| 3746 | |
| 3747 | // transfer all effects one by one so that new effect chain is created on new thread with |
| 3748 | // correct buffer sizes and audio parameters and effect engines reconfigured accordingly |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3749 | sp<EffectChain> dstChain; |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 3750 | uint32_t strategy = 0; // prevent compiler warning |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3751 | sp<EffectModule> effect = chain->getEffectFromId_l(0); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3752 | Vector< sp<EffectModule> > removed; |
| 3753 | status_t status = NO_ERROR; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3754 | while (effect != 0) { |
| 3755 | srcThread->removeEffect_l(effect); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3756 | removed.add(effect); |
| 3757 | status = dstThread->addEffect_l(effect); |
| 3758 | if (status != NO_ERROR) { |
| 3759 | break; |
| 3760 | } |
Eric Laurent | ec35a14 | 2011-10-05 17:42:25 -0700 | [diff] [blame] | 3761 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 3762 | if (effect->state() == EffectModule::ACTIVE || |
| 3763 | effect->state() == EffectModule::STOPPING) { |
| 3764 | effect->start(); |
| 3765 | } |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3766 | // if the move request is not received from audio policy manager, the effect must be |
| 3767 | // re-registered with the new strategy and output |
| 3768 | if (dstChain == 0) { |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3769 | dstChain = effect->callback()->chain().promote(); |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3770 | if (dstChain == 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3771 | ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get()); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3772 | status = NO_INIT; |
| 3773 | break; |
Eric Laurent | 39e94f8 | 2010-07-28 01:32:47 -0700 | [diff] [blame] | 3774 | } |
| 3775 | strategy = dstChain->strategy(); |
| 3776 | } |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 3777 | effect = chain->getEffectFromId_l(0); |
| 3778 | } |
| 3779 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3780 | if (status != NO_ERROR) { |
| 3781 | for (size_t i = 0; i < removed.size(); i++) { |
| 3782 | srcThread->addEffect_l(removed[i]); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3787 | } |
| 3788 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3789 | status_t AudioFlinger::moveAuxEffectToIo(int EffectId, |
| 3790 | const sp<PlaybackThread>& dstThread, |
| 3791 | sp<PlaybackThread> *srcThread) |
| 3792 | { |
| 3793 | status_t status = NO_ERROR; |
| 3794 | Mutex::Autolock _l(mLock); |
Eric Laurent | b20cf7d | 2019-04-05 19:37:34 -0700 | [diff] [blame] | 3795 | sp<PlaybackThread> thread = |
| 3796 | static_cast<PlaybackThread *>(getEffectThread_l(AUDIO_SESSION_OUTPUT_MIX, EffectId).get()); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3797 | |
| 3798 | if (EffectId != 0 && thread != 0 && dstThread != thread.get()) { |
| 3799 | Mutex::Autolock _dl(dstThread->mLock); |
| 3800 | Mutex::Autolock _sl(thread->mLock); |
| 3801 | sp<EffectChain> srcChain = thread->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
| 3802 | sp<EffectChain> dstChain; |
| 3803 | if (srcChain == 0) { |
| 3804 | return INVALID_OPERATION; |
| 3805 | } |
| 3806 | |
| 3807 | sp<EffectModule> effect = srcChain->getEffectFromId_l(EffectId); |
| 3808 | if (effect == 0) { |
| 3809 | return INVALID_OPERATION; |
| 3810 | } |
| 3811 | thread->removeEffect_l(effect); |
| 3812 | status = dstThread->addEffect_l(effect); |
| 3813 | if (status != NO_ERROR) { |
| 3814 | thread->addEffect_l(effect); |
| 3815 | status = INVALID_OPERATION; |
| 3816 | goto Exit; |
| 3817 | } |
| 3818 | |
Eric Laurent | 6b446ce | 2019-12-13 10:56:31 -0800 | [diff] [blame] | 3819 | dstChain = effect->callback()->chain().promote(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3820 | if (dstChain == 0) { |
| 3821 | thread->addEffect_l(effect); |
| 3822 | status = INVALID_OPERATION; |
| 3823 | } |
| 3824 | |
| 3825 | Exit: |
| 3826 | // removeEffect_l() has stopped the effect if it was active so it must be restarted |
| 3827 | if (effect->state() == EffectModule::ACTIVE || |
| 3828 | effect->state() == EffectModule::STOPPING) { |
| 3829 | effect->start(); |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | if (status == NO_ERROR && srcThread != nullptr) { |
| 3834 | *srcThread = thread; |
| 3835 | } |
| 3836 | return status; |
| 3837 | } |
| 3838 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3839 | bool AudioFlinger::isNonOffloadableGlobalEffectEnabled_l() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3840 | { |
| 3841 | if (mGlobalEffectEnableTime != 0 && |
| 3842 | ((systemTime() - mGlobalEffectEnableTime) < kMinGlobalEffectEnabletimeNs)) { |
| 3843 | return true; |
| 3844 | } |
| 3845 | |
| 3846 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3847 | sp<EffectChain> ec = |
| 3848 | mPlaybackThreads.valueAt(i)->getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3849 | if (ec != 0 && ec->isNonOffloadableEnabled()) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3850 | return true; |
| 3851 | } |
| 3852 | } |
| 3853 | return false; |
| 3854 | } |
| 3855 | |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 3856 | void AudioFlinger::onNonOffloadableGlobalEffectEnable() |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 3857 | { |
| 3858 | Mutex::Autolock _l(mLock); |
| 3859 | |
| 3860 | mGlobalEffectEnableTime = systemTime(); |
| 3861 | |
| 3862 | for (size_t i = 0; i < mPlaybackThreads.size(); i++) { |
| 3863 | sp<PlaybackThread> t = mPlaybackThreads.valueAt(i); |
| 3864 | if (t->mType == ThreadBase::OFFLOAD) { |
| 3865 | t->invalidateTracks(AUDIO_STREAM_MUSIC); |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | } |
| 3870 | |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3871 | status_t AudioFlinger::putOrphanEffectChain_l(const sp<AudioFlinger::EffectChain>& chain) |
| 3872 | { |
Eric Laurent | d8365c5 | 2017-07-16 15:27:05 -0700 | [diff] [blame] | 3873 | // clear possible suspended state before parking the chain so that it starts in default state |
| 3874 | // when attached to a new record thread |
| 3875 | chain->setEffectSuspended_l(FX_IID_AEC, false); |
| 3876 | chain->setEffectSuspended_l(FX_IID_NS, false); |
| 3877 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3878 | audio_session_t session = chain->sessionId(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3879 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3880 | ALOGV("putOrphanEffectChain_l session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3881 | if (index >= 0) { |
| 3882 | ALOGW("putOrphanEffectChain_l chain for session %d already present", session); |
| 3883 | return ALREADY_EXISTS; |
| 3884 | } |
| 3885 | mOrphanEffectChains.add(session, chain); |
| 3886 | return NO_ERROR; |
| 3887 | } |
| 3888 | |
| 3889 | sp<AudioFlinger::EffectChain> AudioFlinger::getOrphanEffectChain_l(audio_session_t session) |
| 3890 | { |
| 3891 | sp<EffectChain> chain; |
| 3892 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3893 | ALOGV("getOrphanEffectChain_l session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3894 | if (index >= 0) { |
| 3895 | chain = mOrphanEffectChains.valueAt(index); |
| 3896 | mOrphanEffectChains.removeItemsAt(index); |
| 3897 | } |
| 3898 | return chain; |
| 3899 | } |
| 3900 | |
| 3901 | bool AudioFlinger::updateOrphanEffectChains(const sp<AudioFlinger::EffectModule>& effect) |
| 3902 | { |
| 3903 | Mutex::Autolock _l(mLock); |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 3904 | audio_session_t session = effect->sessionId(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3905 | ssize_t index = mOrphanEffectChains.indexOfKey(session); |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3906 | ALOGV("updateOrphanEffectChains session %d index %zd", session, index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3907 | if (index >= 0) { |
| 3908 | sp<EffectChain> chain = mOrphanEffectChains.valueAt(index); |
Eric Laurent | 0d5a2ed | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 3909 | if (chain->removeEffect_l(effect, true) == 0) { |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 3910 | ALOGV("updateOrphanEffectChains removing effect chain at index %zd", index); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 3911 | mOrphanEffectChains.removeItemsAt(index); |
| 3912 | } |
| 3913 | return true; |
| 3914 | } |
| 3915 | return false; |
| 3916 | } |
| 3917 | |
| 3918 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 3919 | // ---------------------------------------------------------------------------- |
| 3920 | |
| 3921 | status_t AudioFlinger::onTransact( |
| 3922 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 3923 | { |
| 3924 | return BnAudioFlinger::onTransact(code, data, reply, flags); |
| 3925 | } |
| 3926 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3927 | } // namespace android |