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