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