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