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