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