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