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