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