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