blob: feae97e37d571f188ae8b67748d321c21068ab8e [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
Andy Hung55a74fd2023-07-13 19:54:47 -070018#define LOG_TAG "DeviceEffectManager"
Eric Laurentb82e6b72019-11-22 17:25:04 -080019//#define LOG_NDEBUG 0
20
Andy Hung0a51b5c2023-07-18 20:54:44 -070021#include "DeviceEffectManager.h"
Eric Laurentb82e6b72019-11-22 17:25:04 -080022
Andy Hungba8e52b2023-05-11 14:33:03 -070023#include "EffectConfiguration.h"
Andy Hung0a51b5c2023-07-18 20:54:44 -070024
25#include <afutils/DumpTryLock.h>
26#include <audio_utils/primitives.h>
Eric Laurentb82e6b72019-11-22 17:25:04 -080027#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hung0a51b5c2023-07-18 20:54:44 -070028#include <utils/Log.h>
Eric Laurentb82e6b72019-11-22 17:25:04 -080029
30// ----------------------------------------------------------------------------
31
32
33namespace android {
34
Shunkai Yaod7ea4092022-12-12 00:44:33 +000035using detail::AudioHalVersionInfo;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070036using media::IEffectClient;
37
Andy Hung692f0452023-07-17 13:45:55 -070038DeviceEffectManager::DeviceEffectManager(
39 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback)
40 : mAfDeviceEffectManagerCallback(afDeviceEffectManagerCallback),
Andy Hung3e07ef02023-09-06 17:37:34 -070041 mMyCallback(sp<DeviceEffectManagerCallback>::make(*this)) {}
Andy Hung55a74fd2023-07-13 19:54:47 -070042
43void DeviceEffectManager::onFirstRef() {
Andy Hung692f0452023-07-17 13:45:55 -070044 mAfDeviceEffectManagerCallback->getPatchCommandThread()->addListener(this);
Andy Hung55a74fd2023-07-13 19:54:47 -070045}
46
47status_t DeviceEffectManager::addEffectToHal(const struct audio_port_config* device,
48 const sp<EffectHalInterface>& effect) {
Andy Hung692f0452023-07-17 13:45:55 -070049 return mAfDeviceEffectManagerCallback->addEffectToHal(device, effect);
Andy Hung55a74fd2023-07-13 19:54:47 -070050};
51
52status_t DeviceEffectManager::removeEffectFromHal(const struct audio_port_config* device,
53 const sp<EffectHalInterface>& effect) {
Andy Hung692f0452023-07-17 13:45:55 -070054 return mAfDeviceEffectManagerCallback->removeEffectFromHal(device, effect);
Andy Hung55a74fd2023-07-13 19:54:47 -070055};
56
57void DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070058 const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080059 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 Hungf65f5a72023-08-29 12:19:17 -070062 audio_utils::lock_guard _l(mutex());
François Gaffie0f660582023-06-27 15:15:26 +020063 for (auto& effectProxies : mDeviceEffects) {
64 for (auto& effect : effectProxies.second) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070065 const status_t status = effect->onCreatePatch(handle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020066 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
67 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
68 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080069 }
70}
71
Andy Hung55a74fd2023-07-13 19:54:47 -070072void DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080073 ALOGV("%s", __func__);
Andy Hungf65f5a72023-08-29 12:19:17 -070074 audio_utils::lock_guard _l(mutex());
François Gaffie0f660582023-06-27 15:15:26 +020075 for (auto& effectProxies : mDeviceEffects) {
76 for (auto& effect : effectProxies.second) {
77 effect->onReleasePatch(handle);
78 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080079 }
80}
81
Andy Hung55a74fd2023-07-13 19:54:47 -070082void DeviceEffectManager::onUpdateAudioPatch(audio_patch_handle_t oldHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -070083 audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) {
François Gaffie58e73af2023-02-15 11:47:24 +010084 ALOGV("%s oldhandle %d newHandle %d mHalHandle %d device sink %08x",
85 __func__, oldHandle, newHandle, patch.mHalHandle,
86 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
Andy Hungf65f5a72023-08-29 12:19:17 -070087 audio_utils::lock_guard _l(mutex());
François Gaffie0f660582023-06-27 15:15:26 +020088 for (auto& effectProxies : mDeviceEffects) {
89 for (auto& effect : effectProxies.second) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070090 const status_t status = effect->onUpdatePatch(oldHandle, newHandle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020091 ALOGV("%s Effect onUpdatePatch status %d", __func__, status);
92 ALOGW_IF(status != NO_ERROR, "%s onUpdatePatch error %d", __func__, status);
93 }
François Gaffie58e73af2023-02-15 11:47:24 +010094 }
95}
96
Andy Hungf65f5a72023-08-29 12:19:17 -070097// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mutex() held
Andy Hung55a74fd2023-07-13 19:54:47 -070098sp<IAfEffectHandle> DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080099 effect_descriptor_t *descriptor,
100 const AudioDeviceTypeAddr& device,
Andy Hung59867e42023-06-27 17:05:02 -0700101 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800102 const sp<IEffectClient>& effectClient,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700103 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800104 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700105 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +0200106 bool probe,
107 bool notifyFramesProcessed) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700108 sp<IAfDeviceEffectProxy> effect;
François Gaffie0f660582023-06-27 15:15:26 +0200109 std::vector<sp<IAfDeviceEffectProxy>> effectsForDevice = {};
Andy Hung6ac17eb2023-06-20 18:56:17 -0700110 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800111 status_t lStatus;
112
113 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -0700114 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800115 *status = lStatus;
116 return handle;
117 }
118
119 {
Andy Hungf65f5a72023-08-29 12:19:17 -0700120 audio_utils::lock_guard _l(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800121 auto iter = mDeviceEffects.find(device);
122 if (iter != mDeviceEffects.end()) {
François Gaffie0f660582023-06-27 15:15:26 +0200123 effectsForDevice = iter->second;
124 for (const auto& iterEffect : effectsForDevice) {
125 if (memcmp(&iterEffect->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) ==
126 0) {
127 effect = iterEffect;
128 break;
129 }
130 }
131 }
132 if (effect == nullptr) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700133 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Andy Hung692f0452023-07-17 13:45:55 -0700134 descriptor,
135 mAfDeviceEffectManagerCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
Eric Laurentde8caf42021-08-11 17:19:25 +0200136 notifyFramesProcessed);
François Gaffie0f660582023-06-27 15:15:26 +0200137 effectsForDevice.push_back(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138 }
139 // create effect handle and connect it to effect module
Andy Hung6ac17eb2023-06-20 18:56:17 -0700140 handle = IAfEffectHandle::create(
141 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800142 lStatus = handle->initCheck();
143 if (lStatus == NO_ERROR) {
144 lStatus = effect->addHandle(handle.get());
145 if (lStatus == NO_ERROR) {
Shunkai Yaod125e402024-01-20 03:19:06 +0000146 lStatus = effect->init_l(patches);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530147 if (lStatus == NAME_NOT_FOUND) {
148 lStatus = NO_ERROR;
149 }
150 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
François Gaffie0f660582023-06-27 15:15:26 +0200151 mDeviceEffects.erase(device);
152 mDeviceEffects.emplace(device, effectsForDevice);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530153 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800154 }
155 }
156 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200157 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800158 *enabled = (int)effect->isEnabled();
159 }
160 *status = lStatus;
161 return handle;
162}
163
Andy Hung3e07ef02023-09-06 17:37:34 -0700164/* static */
Andy Hung55a74fd2023-07-13 19:54:47 -0700165status_t DeviceEffectManager::checkEffectCompatibility(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800166 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700167 const sp<EffectsFactoryHalInterface> effectsFactory =
168 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700169 if (effectsFactory == nullptr) {
170 return BAD_VALUE;
171 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800172
Andy Hungba8e52b2023-05-11 14:33:03 -0700173 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000174 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700175 static const AudioHalVersionInfo halVersion =
176 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700177
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000178 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
179 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700180 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
181 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
182 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yao489c5a92022-12-02 05:35:41 +0000183 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
184 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800185 return BAD_VALUE;
186 }
187
188 return NO_ERROR;
189}
190
Andy Hung3e07ef02023-09-06 17:37:34 -0700191/* static */
Andy Hung55a74fd2023-07-13 19:54:47 -0700192status_t DeviceEffectManager::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800193 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
194 sp<EffectHalInterface> *effect) {
195 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700196 const sp<EffectsFactoryHalInterface> effectsFactory =
197 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800198 if (effectsFactory != 0) {
199 status = effectsFactory->createEffect(
200 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
201 }
202 return status;
203}
204
Andy Hung55a74fd2023-07-13 19:54:47 -0700205void DeviceEffectManager::dump(int fd)
Andy Hung920f6572022-10-06 12:09:49 -0700206NO_THREAD_SAFETY_ANALYSIS // conditional try lock
207{
Andy Hungf65f5a72023-08-29 12:19:17 -0700208 const bool locked = afutils::dumpTryLock(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800209 if (!locked) {
210 String8 result("DeviceEffectManager may be deadlocked\n");
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000211 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800212 }
213
Phil Burk651d0a52020-05-08 14:00:58 -0700214 String8 heading("\nDevice Effects:\n");
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000215 write(fd, heading.c_str(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800216 for (const auto& iter : mDeviceEffects) {
217 String8 outStr;
218 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
219 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
François Gaffie0f660582023-06-27 15:15:26 +0200220 for (const auto& effect : iter.second) {
Tomasz Wasilczyk833345b2023-08-15 20:59:35 +0000221 write(fd, outStr.c_str(), outStr.size());
François Gaffie0f660582023-06-27 15:15:26 +0200222 effect->dump2(fd, 4);
223 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800224 }
225
226 if (locked) {
Andy Hungf65f5a72023-08-29 12:19:17 -0700227 mutex().unlock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800228 }
229}
230
Andy Hung55a74fd2023-07-13 19:54:47 -0700231size_t DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800232{
Andy Hungf65f5a72023-08-29 12:19:17 -0700233 audio_utils::lock_guard _l(mutex());
François Gaffie0f660582023-06-27 15:15:26 +0200234 const auto& iter = mDeviceEffects.find(effect->device());
235 if (iter != mDeviceEffects.end()) {
236 const auto& iterEffect = std::find_if(
237 iter->second.begin(), iter->second.end(), [&effect](const auto& effectProxy) {
238 return memcmp(&effectProxy->desc().uuid, &effect->desc().uuid,
239 sizeof(effect_uuid_t)) == 0;
240 });
241 if (iterEffect != iter->second.end()) {
242 iter->second.erase(iterEffect);
243 if (iter->second.empty()) {
244 mDeviceEffects.erase(effect->device());
245 }
246 }
247 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800248 return mDeviceEffects.size();
249}
250
Andy Hung55a74fd2023-07-13 19:54:47 -0700251bool DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700252 IAfEffectHandle *handle, bool unpinIfLast) {
253 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800254 if (effectBase == nullptr) {
255 return false;
256 }
257
Andy Hung6ac17eb2023-06-20 18:56:17 -0700258 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800259 if (effect == nullptr) {
260 return false;
261 }
262 // restore suspended effects if the disconnected handle was enabled and the last one.
263 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
264 if (remove) {
265 mManager.removeEffect(effect);
266 if (handle->enabled()) {
267 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
268 }
269 }
270 return true;
271}
272
Andy Hung55a74fd2023-07-13 19:54:47 -0700273bool DeviceEffectManagerCallback::isAudioPolicyReady() const {
Andy Hung692f0452023-07-17 13:45:55 -0700274 return mManager.afDeviceEffectManagerCallback()->isAudioPolicyReady();
Andy Hung55a74fd2023-07-13 19:54:47 -0700275}
276
277int DeviceEffectManagerCallback::newEffectId() const {
Andy Hung692f0452023-07-17 13:45:55 -0700278 return mManager.afDeviceEffectManagerCallback()->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
Andy Hung55a74fd2023-07-13 19:54:47 -0700279}
280
Eric Laurentb82e6b72019-11-22 17:25:04 -0800281} // namespace android