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