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