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