Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2019, 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 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 19 | #define LOG_TAG "DeviceEffectManager" |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <utils/Log.h> |
| 23 | #include <audio_utils/primitives.h> |
| 24 | |
| 25 | #include "AudioFlinger.h" |
Andy Hung | ba8e52b | 2023-05-11 14:33:03 -0700 | [diff] [blame] | 26 | #include "EffectConfiguration.h" |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 27 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame] | 34 | using detail::AudioHalVersionInfo; |
Ytai Ben-Tsvi | 9cd8981 | 2020-07-01 17:12:06 -0700 | [diff] [blame] | 35 | using media::IEffectClient; |
| 36 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 37 | DeviceEffectManager::DeviceEffectManager(AudioFlinger& audioFlinger) |
| 38 | : mAudioFlinger(audioFlinger), |
| 39 | mMyCallback(new DeviceEffectManagerCallback(*this)) {} |
| 40 | |
| 41 | void DeviceEffectManager::onFirstRef() { |
| 42 | mAudioFlinger.mPatchCommandThread->addListener(this); |
| 43 | } |
| 44 | |
| 45 | status_t DeviceEffectManager::addEffectToHal(const struct audio_port_config* device, |
| 46 | const sp<EffectHalInterface>& effect) { |
| 47 | return mAudioFlinger.addEffectToHal(device, effect); |
| 48 | }; |
| 49 | |
| 50 | status_t DeviceEffectManager::removeEffectFromHal(const struct audio_port_config* device, |
| 51 | const sp<EffectHalInterface>& effect) { |
| 52 | return mAudioFlinger.removeEffectFromHal(device, effect); |
| 53 | }; |
| 54 | |
| 55 | void DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 56 | const IAfPatchPanel::Patch& patch) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 57 | ALOGV("%s handle %d mHalHandle %d device sink %08x", |
| 58 | __func__, handle, patch.mHalHandle, |
| 59 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 60 | Mutex::Autolock _l(mLock); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 61 | for (auto& effectProxies : mDeviceEffects) { |
| 62 | for (auto& effect : effectProxies.second) { |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 63 | const status_t status = effect->onCreatePatch(handle, patch); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 64 | ALOGV("%s Effect onCreatePatch status %d", __func__, status); |
| 65 | ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status); |
| 66 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 70 | void DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 71 | ALOGV("%s", __func__); |
| 72 | Mutex::Autolock _l(mLock); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 73 | for (auto& effectProxies : mDeviceEffects) { |
| 74 | for (auto& effect : effectProxies.second) { |
| 75 | effect->onReleasePatch(handle); |
| 76 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 80 | void DeviceEffectManager::onUpdateAudioPatch(audio_patch_handle_t oldHandle, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 81 | audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) { |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 82 | ALOGV("%s oldhandle %d newHandle %d mHalHandle %d device sink %08x", |
| 83 | __func__, oldHandle, newHandle, patch.mHalHandle, |
| 84 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 85 | Mutex::Autolock _l(mLock); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 86 | for (auto& effectProxies : mDeviceEffects) { |
| 87 | for (auto& effect : effectProxies.second) { |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 88 | const status_t status = effect->onUpdatePatch(oldHandle, newHandle, patch); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 89 | ALOGV("%s Effect onUpdatePatch status %d", __func__, status); |
| 90 | ALOGW_IF(status != NO_ERROR, "%s onUpdatePatch error %d", __func__, status); |
| 91 | } |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 95 | // DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 96 | sp<IAfEffectHandle> DeviceEffectManager::createEffect_l( |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 97 | effect_descriptor_t *descriptor, |
| 98 | const AudioDeviceTypeAddr& device, |
Andy Hung | 59867e4 | 2023-06-27 17:05:02 -0700 | [diff] [blame] | 99 | const sp<Client>& client, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 100 | const sp<IEffectClient>& effectClient, |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 101 | const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches, |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 102 | int *enabled, |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 103 | status_t *status, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 104 | bool probe, |
| 105 | bool notifyFramesProcessed) { |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 106 | sp<IAfDeviceEffectProxy> effect; |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 107 | std::vector<sp<IAfDeviceEffectProxy>> effectsForDevice = {}; |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 108 | sp<IAfEffectHandle> handle; |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 109 | status_t lStatus; |
| 110 | |
| 111 | lStatus = checkEffectCompatibility(descriptor); |
Eric Laurent | 2fe0acd | 2020-03-13 14:30:46 -0700 | [diff] [blame] | 112 | if (probe || lStatus != NO_ERROR) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 113 | *status = lStatus; |
| 114 | return handle; |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | Mutex::Autolock _l(mLock); |
| 119 | auto iter = mDeviceEffects.find(device); |
| 120 | if (iter != mDeviceEffects.end()) { |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 121 | effectsForDevice = iter->second; |
| 122 | for (const auto& iterEffect : effectsForDevice) { |
| 123 | if (memcmp(&iterEffect->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == |
| 124 | 0) { |
| 125 | effect = iterEffect; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | if (effect == nullptr) { |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 131 | effect = IAfDeviceEffectProxy::create(device, mMyCallback, |
Eric Laurent | de8caf4 | 2021-08-11 17:19:25 +0200 | [diff] [blame] | 132 | descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT), |
| 133 | notifyFramesProcessed); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 134 | effectsForDevice.push_back(effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 135 | } |
| 136 | // create effect handle and connect it to effect module |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 137 | handle = IAfEffectHandle::create( |
| 138 | effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 139 | lStatus = handle->initCheck(); |
| 140 | if (lStatus == NO_ERROR) { |
| 141 | lStatus = effect->addHandle(handle.get()); |
| 142 | if (lStatus == NO_ERROR) { |
Andy Hung | 8e6b62a | 2023-07-13 18:11:33 -0700 | [diff] [blame] | 143 | lStatus = effect->init(patches); |
Ram Mohan M | 2a05df2 | 2022-08-28 11:46:23 +0530 | [diff] [blame] | 144 | if (lStatus == NAME_NOT_FOUND) { |
| 145 | lStatus = NO_ERROR; |
| 146 | } |
| 147 | if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) { |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 148 | mDeviceEffects.erase(device); |
| 149 | mDeviceEffects.emplace(device, effectsForDevice); |
Ram Mohan M | 2a05df2 | 2022-08-28 11:46:23 +0530 | [diff] [blame] | 150 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
Vlad Popa | 5161f8a | 2022-10-10 16:17:20 +0200 | [diff] [blame] | 154 | if (enabled != nullptr) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 155 | *enabled = (int)effect->isEnabled(); |
| 156 | } |
| 157 | *status = lStatus; |
| 158 | return handle; |
| 159 | } |
| 160 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 161 | status_t DeviceEffectManager::checkEffectCompatibility( |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 162 | const effect_descriptor_t *desc) { |
Andy Hung | ba8e52b | 2023-05-11 14:33:03 -0700 | [diff] [blame] | 163 | const sp<EffectsFactoryHalInterface> effectsFactory = |
| 164 | audioflinger::EffectConfiguration::getEffectsFactoryHal(); |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 165 | if (effectsFactory == nullptr) { |
| 166 | return BAD_VALUE; |
| 167 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 168 | |
Andy Hung | ba8e52b | 2023-05-11 14:33:03 -0700 | [diff] [blame] | 169 | static const AudioHalVersionInfo sMinDeviceEffectHalVersion = |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame] | 170 | AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0); |
Andy Hung | ba8e52b | 2023-05-11 14:33:03 -0700 | [diff] [blame] | 171 | static const AudioHalVersionInfo halVersion = |
| 172 | audioflinger::EffectConfiguration::getAudioHalVersionInfo(); |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 173 | |
Shunkai Yao | d7ea409 | 2022-12-12 00:44:33 +0000 | [diff] [blame] | 174 | // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as |
| 175 | // long as the type, major and minor sequence doesn't change in the definition. |
Eric Laurent | 9289bde | 2020-08-18 12:49:17 -0700 | [diff] [blame] | 176 | if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC |
| 177 | && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) |
| 178 | || halVersion < sMinDeviceEffectHalVersion) { |
Shunkai Yao | 489c5a9 | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 179 | ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s", |
| 180 | __func__, desc->name, halVersion.toString().c_str()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 181 | return BAD_VALUE; |
| 182 | } |
| 183 | |
| 184 | return NO_ERROR; |
| 185 | } |
| 186 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 187 | status_t DeviceEffectManager::createEffectHal( |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 188 | const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId, |
| 189 | sp<EffectHalInterface> *effect) { |
| 190 | status_t status = NO_INIT; |
Andy Hung | ba8e52b | 2023-05-11 14:33:03 -0700 | [diff] [blame] | 191 | const sp<EffectsFactoryHalInterface> effectsFactory = |
| 192 | audioflinger::EffectConfiguration::getEffectsFactoryHal(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 193 | if (effectsFactory != 0) { |
| 194 | status = effectsFactory->createEffect( |
| 195 | pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect); |
| 196 | } |
| 197 | return status; |
| 198 | } |
| 199 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 200 | void DeviceEffectManager::dump(int fd) |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 201 | NO_THREAD_SAFETY_ANALYSIS // conditional try lock |
| 202 | { |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 203 | const bool locked = AudioFlinger::dumpTryLock(mLock); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 204 | if (!locked) { |
| 205 | String8 result("DeviceEffectManager may be deadlocked\n"); |
| 206 | write(fd, result.string(), result.size()); |
| 207 | } |
| 208 | |
Phil Burk | 651d0a5 | 2020-05-08 14:00:58 -0700 | [diff] [blame] | 209 | String8 heading("\nDevice Effects:\n"); |
| 210 | write(fd, heading.string(), heading.size()); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 211 | for (const auto& iter : mDeviceEffects) { |
| 212 | String8 outStr; |
| 213 | outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "", |
| 214 | ::android::toString(iter.first.mType).c_str(), iter.first.getAddress()); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 215 | for (const auto& effect : iter.second) { |
| 216 | write(fd, outStr.string(), outStr.size()); |
| 217 | effect->dump2(fd, 4); |
| 218 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (locked) { |
| 222 | mLock.unlock(); |
| 223 | } |
| 224 | } |
| 225 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 226 | size_t DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect) |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 227 | { |
| 228 | Mutex::Autolock _l(mLock); |
François Gaffie | 0f66058 | 2023-06-27 15:15:26 +0200 | [diff] [blame] | 229 | const auto& iter = mDeviceEffects.find(effect->device()); |
| 230 | if (iter != mDeviceEffects.end()) { |
| 231 | const auto& iterEffect = std::find_if( |
| 232 | iter->second.begin(), iter->second.end(), [&effect](const auto& effectProxy) { |
| 233 | return memcmp(&effectProxy->desc().uuid, &effect->desc().uuid, |
| 234 | sizeof(effect_uuid_t)) == 0; |
| 235 | }); |
| 236 | if (iterEffect != iter->second.end()) { |
| 237 | iter->second.erase(iterEffect); |
| 238 | if (iter->second.empty()) { |
| 239 | mDeviceEffects.erase(effect->device()); |
| 240 | } |
| 241 | } |
| 242 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 243 | return mDeviceEffects.size(); |
| 244 | } |
| 245 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 246 | bool DeviceEffectManagerCallback::disconnectEffectHandle( |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 247 | IAfEffectHandle *handle, bool unpinIfLast) { |
| 248 | sp<IAfEffectBase> effectBase = handle->effect().promote(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 249 | if (effectBase == nullptr) { |
| 250 | return false; |
| 251 | } |
| 252 | |
Andy Hung | 6ac17eb | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 253 | sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 254 | if (effect == nullptr) { |
| 255 | return false; |
| 256 | } |
| 257 | // restore suspended effects if the disconnected handle was enabled and the last one. |
| 258 | bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast); |
| 259 | if (remove) { |
| 260 | mManager.removeEffect(effect); |
| 261 | if (handle->enabled()) { |
| 262 | effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/); |
| 263 | } |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
Andy Hung | 55a74fd | 2023-07-13 19:54:47 -0700 | [diff] [blame] | 268 | bool DeviceEffectManagerCallback::isAudioPolicyReady() const { |
| 269 | return mManager.audioFlinger().isAudioPolicyReady(); |
| 270 | } |
| 271 | |
| 272 | int DeviceEffectManagerCallback::newEffectId() const { |
| 273 | return mManager.audioFlinger().nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT); |
| 274 | } |
| 275 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 276 | } // namespace android |