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