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