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