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