blob: d78e26fcb860202c71b8f55c0f6a88f9c8842710 [file] [log] [blame]
Eric Laurentb82e6b72019-11-22 17:25:04 -08001/*
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 Hung55a74fd2023-07-13 19:54:47 -070019#define LOG_TAG "DeviceEffectManager"
Eric Laurentb82e6b72019-11-22 17:25:04 -080020//#define LOG_NDEBUG 0
21
22#include <utils/Log.h>
23#include <audio_utils/primitives.h>
24
25#include "AudioFlinger.h"
Andy Hungba8e52b2023-05-11 14:33:03 -070026#include "EffectConfiguration.h"
Eric Laurentb82e6b72019-11-22 17:25:04 -080027#include <media/audiohal/EffectsFactoryHalInterface.h>
28
29// ----------------------------------------------------------------------------
30
31
32namespace android {
33
Shunkai Yaod7ea4092022-12-12 00:44:33 +000034using detail::AudioHalVersionInfo;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070035using media::IEffectClient;
36
Andy Hung692f0452023-07-17 13:45:55 -070037DeviceEffectManager::DeviceEffectManager(
38 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback)
39 : mAfDeviceEffectManagerCallback(afDeviceEffectManagerCallback),
Andy Hung55a74fd2023-07-13 19:54:47 -070040 mMyCallback(new DeviceEffectManagerCallback(*this)) {}
41
42void DeviceEffectManager::onFirstRef() {
Andy Hung692f0452023-07-17 13:45:55 -070043 mAfDeviceEffectManagerCallback->getPatchCommandThread()->addListener(this);
Andy Hung55a74fd2023-07-13 19:54:47 -070044}
45
46status_t DeviceEffectManager::addEffectToHal(const struct audio_port_config* device,
47 const sp<EffectHalInterface>& effect) {
Andy Hung692f0452023-07-17 13:45:55 -070048 return mAfDeviceEffectManagerCallback->addEffectToHal(device, effect);
Andy Hung55a74fd2023-07-13 19:54:47 -070049};
50
51status_t DeviceEffectManager::removeEffectFromHal(const struct audio_port_config* device,
52 const sp<EffectHalInterface>& effect) {
Andy Hung692f0452023-07-17 13:45:55 -070053 return mAfDeviceEffectManagerCallback->removeEffectFromHal(device, effect);
Andy Hung55a74fd2023-07-13 19:54:47 -070054};
55
56void DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070057 const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080058 ALOGV("%s handle %d mHalHandle %d device sink %08x",
59 __func__, handle, patch.mHalHandle,
60 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
61 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +020062 for (auto& effectProxies : mDeviceEffects) {
63 for (auto& effect : effectProxies.second) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070064 const status_t status = effect->onCreatePatch(handle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020065 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
66 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
67 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080068 }
69}
70
Andy Hung55a74fd2023-07-13 19:54:47 -070071void DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080072 ALOGV("%s", __func__);
73 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +020074 for (auto& effectProxies : mDeviceEffects) {
75 for (auto& effect : effectProxies.second) {
76 effect->onReleasePatch(handle);
77 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080078 }
79}
80
Andy Hung55a74fd2023-07-13 19:54:47 -070081void DeviceEffectManager::onUpdateAudioPatch(audio_patch_handle_t oldHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070082 audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) {
François Gaffie58e73af2023-02-15 11:47:24 +010083 ALOGV("%s oldhandle %d newHandle %d mHalHandle %d device sink %08x",
84 __func__, oldHandle, newHandle, patch.mHalHandle,
85 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
86 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +020087 for (auto& effectProxies : mDeviceEffects) {
88 for (auto& effect : effectProxies.second) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070089 const status_t status = effect->onUpdatePatch(oldHandle, newHandle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020090 ALOGV("%s Effect onUpdatePatch status %d", __func__, status);
91 ALOGW_IF(status != NO_ERROR, "%s onUpdatePatch error %d", __func__, status);
92 }
François Gaffie58e73af2023-02-15 11:47:24 +010093 }
94}
95
Eric Laurentb82e6b72019-11-22 17:25:04 -080096// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
Andy Hung55a74fd2023-07-13 19:54:47 -070097sp<IAfEffectHandle> DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080098 effect_descriptor_t *descriptor,
99 const AudioDeviceTypeAddr& device,
Andy Hung59867e42023-06-27 17:05:02 -0700100 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800101 const sp<IEffectClient>& effectClient,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700102 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800103 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700104 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +0200105 bool probe,
106 bool notifyFramesProcessed) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700107 sp<IAfDeviceEffectProxy> effect;
François Gaffie0f660582023-06-27 15:15:26 +0200108 std::vector<sp<IAfDeviceEffectProxy>> effectsForDevice = {};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700109 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800110 status_t lStatus;
111
112 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700113 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800114 *status = lStatus;
115 return handle;
116 }
117
118 {
119 Mutex::Autolock _l(mLock);
120 auto iter = mDeviceEffects.find(device);
121 if (iter != mDeviceEffects.end()) {
François Gaffie0f660582023-06-27 15:15:26 +0200122 effectsForDevice = iter->second;
123 for (const auto& iterEffect : effectsForDevice) {
124 if (memcmp(&iterEffect->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) ==
125 0) {
126 effect = iterEffect;
127 break;
128 }
129 }
130 }
131 if (effect == nullptr) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700132 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Andy Hung692f0452023-07-17 13:45:55 -0700133 descriptor,
134 mAfDeviceEffectManagerCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
Eric Laurentde8caf42021-08-11 17:19:25 +0200135 notifyFramesProcessed);
François Gaffie0f660582023-06-27 15:15:26 +0200136 effectsForDevice.push_back(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800137 }
138 // create effect handle and connect it to effect module
Andy Hung6ac17eb2023-06-20 18:56:17 -0700139 handle = IAfEffectHandle::create(
140 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800141 lStatus = handle->initCheck();
142 if (lStatus == NO_ERROR) {
143 lStatus = effect->addHandle(handle.get());
144 if (lStatus == NO_ERROR) {
Andy Hung8e6b62a2023-07-13 18:11:33 -0700145 lStatus = effect->init(patches);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530146 if (lStatus == NAME_NOT_FOUND) {
147 lStatus = NO_ERROR;
148 }
149 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
François Gaffie0f660582023-06-27 15:15:26 +0200150 mDeviceEffects.erase(device);
151 mDeviceEffects.emplace(device, effectsForDevice);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530152 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800153 }
154 }
155 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200156 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800157 *enabled = (int)effect->isEnabled();
158 }
159 *status = lStatus;
160 return handle;
161}
162
Andy Hung55a74fd2023-07-13 19:54:47 -0700163status_t DeviceEffectManager::checkEffectCompatibility(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800164 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700165 const sp<EffectsFactoryHalInterface> effectsFactory =
166 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700167 if (effectsFactory == nullptr) {
168 return BAD_VALUE;
169 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800170
Andy Hungba8e52b2023-05-11 14:33:03 -0700171 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000172 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700173 static const AudioHalVersionInfo halVersion =
174 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700175
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000176 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
177 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700178 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
179 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
180 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yao489c5a92022-12-02 05:35:41 +0000181 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
182 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800183 return BAD_VALUE;
184 }
185
186 return NO_ERROR;
187}
188
Andy Hung55a74fd2023-07-13 19:54:47 -0700189status_t DeviceEffectManager::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800190 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
191 sp<EffectHalInterface> *effect) {
192 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700193 const sp<EffectsFactoryHalInterface> effectsFactory =
194 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800195 if (effectsFactory != 0) {
196 status = effectsFactory->createEffect(
197 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
198 }
199 return status;
200}
201
Andy Hung55a74fd2023-07-13 19:54:47 -0700202void DeviceEffectManager::dump(int fd)
Andy Hung920f6572022-10-06 12:09:49 -0700203NO_THREAD_SAFETY_ANALYSIS // conditional try lock
204{
Andy Hung55a74fd2023-07-13 19:54:47 -0700205 const bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800206 if (!locked) {
207 String8 result("DeviceEffectManager may be deadlocked\n");
208 write(fd, result.string(), result.size());
209 }
210
Phil Burk651d0a52020-05-08 14:00:58 -0700211 String8 heading("\nDevice Effects:\n");
212 write(fd, heading.string(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800213 for (const auto& iter : mDeviceEffects) {
214 String8 outStr;
215 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
216 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
François Gaffie0f660582023-06-27 15:15:26 +0200217 for (const auto& effect : iter.second) {
218 write(fd, outStr.string(), outStr.size());
219 effect->dump2(fd, 4);
220 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800221 }
222
223 if (locked) {
224 mLock.unlock();
225 }
226}
227
Andy Hung55a74fd2023-07-13 19:54:47 -0700228size_t DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800229{
230 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +0200231 const auto& iter = mDeviceEffects.find(effect->device());
232 if (iter != mDeviceEffects.end()) {
233 const auto& iterEffect = std::find_if(
234 iter->second.begin(), iter->second.end(), [&effect](const auto& effectProxy) {
235 return memcmp(&effectProxy->desc().uuid, &effect->desc().uuid,
236 sizeof(effect_uuid_t)) == 0;
237 });
238 if (iterEffect != iter->second.end()) {
239 iter->second.erase(iterEffect);
240 if (iter->second.empty()) {
241 mDeviceEffects.erase(effect->device());
242 }
243 }
244 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800245 return mDeviceEffects.size();
246}
247
Andy Hung55a74fd2023-07-13 19:54:47 -0700248bool DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700249 IAfEffectHandle *handle, bool unpinIfLast) {
250 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800251 if (effectBase == nullptr) {
252 return false;
253 }
254
Andy Hung6ac17eb2023-06-20 18:56:17 -0700255 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800256 if (effect == nullptr) {
257 return false;
258 }
259 // restore suspended effects if the disconnected handle was enabled and the last one.
260 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
261 if (remove) {
262 mManager.removeEffect(effect);
263 if (handle->enabled()) {
264 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
265 }
266 }
267 return true;
268}
269
Andy Hung55a74fd2023-07-13 19:54:47 -0700270bool DeviceEffectManagerCallback::isAudioPolicyReady() const {
Andy Hung692f0452023-07-17 13:45:55 -0700271 return mManager.afDeviceEffectManagerCallback()->isAudioPolicyReady();
Andy Hung55a74fd2023-07-13 19:54:47 -0700272}
273
274int DeviceEffectManagerCallback::newEffectId() const {
Andy Hung692f0452023-07-17 13:45:55 -0700275 return mManager.afDeviceEffectManagerCallback()->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
Andy Hung55a74fd2023-07-13 19:54:47 -0700276}
277
Eric Laurentb82e6b72019-11-22 17:25:04 -0800278} // namespace android