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