blob: 8e78e4a4ca098d141f9d607a995d89e707aa515b [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
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 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
Eric Laurentb82e6b72019-11-22 17:25:04 -080037void 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);
François Gaffie0f660582023-06-27 15:15:26 +020043 for (auto& effectProxies : mDeviceEffects) {
44 for (auto& effect : effectProxies.second) {
45 status_t status = effect->onCreatePatch(handle, &patch); // TODO(b/288339104) void*
46 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
47 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
48 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080049 }
50}
51
Eric Laurentb82e6b72019-11-22 17:25:04 -080052void AudioFlinger::DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
53 ALOGV("%s", __func__);
54 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +020055 for (auto& effectProxies : mDeviceEffects) {
56 for (auto& effect : effectProxies.second) {
57 effect->onReleasePatch(handle);
58 }
Eric Laurentb82e6b72019-11-22 17:25:04 -080059 }
60}
61
François Gaffie58e73af2023-02-15 11:47:24 +010062void AudioFlinger::DeviceEffectManager::onUpdateAudioPatch(audio_patch_handle_t oldHandle,
63 audio_patch_handle_t newHandle, const PatchPanel::Patch& patch) {
64 ALOGV("%s oldhandle %d newHandle %d mHalHandle %d device sink %08x",
65 __func__, oldHandle, newHandle, patch.mHalHandle,
66 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
67 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +020068 for (auto& effectProxies : mDeviceEffects) {
69 for (auto& effect : effectProxies.second) {
70 // TODO(b/288339104) void*
71 status_t status = effect->onUpdatePatch(oldHandle, newHandle, &patch);
72 ALOGV("%s Effect onUpdatePatch status %d", __func__, status);
73 ALOGW_IF(status != NO_ERROR, "%s onUpdatePatch error %d", __func__, status);
74 }
François Gaffie58e73af2023-02-15 11:47:24 +010075 }
76}
77
Eric Laurentb82e6b72019-11-22 17:25:04 -080078// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -070079sp<IAfEffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080080 effect_descriptor_t *descriptor,
81 const AudioDeviceTypeAddr& device,
Andy Hung59867e42023-06-27 17:05:02 -070082 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -080083 const sp<IEffectClient>& effectClient,
84 const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches,
85 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070086 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020087 bool probe,
88 bool notifyFramesProcessed) {
Andy Hung6ac17eb2023-06-20 18:56:17 -070089 sp<IAfDeviceEffectProxy> effect;
François Gaffie0f660582023-06-27 15:15:26 +020090 std::vector<sp<IAfDeviceEffectProxy>> effectsForDevice = {};
Andy Hung6ac17eb2023-06-20 18:56:17 -070091 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -080092 status_t lStatus;
93
94 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070095 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080096 *status = lStatus;
97 return handle;
98 }
99
100 {
101 Mutex::Autolock _l(mLock);
102 auto iter = mDeviceEffects.find(device);
103 if (iter != mDeviceEffects.end()) {
François Gaffie0f660582023-06-27 15:15:26 +0200104 effectsForDevice = iter->second;
105 for (const auto& iterEffect : effectsForDevice) {
106 if (memcmp(&iterEffect->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) ==
107 0) {
108 effect = iterEffect;
109 break;
110 }
111 }
112 }
113 if (effect == nullptr) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700114 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200115 descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
116 notifyFramesProcessed);
François Gaffie0f660582023-06-27 15:15:26 +0200117 effectsForDevice.push_back(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800118 }
119 // create effect handle and connect it to effect module
Andy Hung6ac17eb2023-06-20 18:56:17 -0700120 handle = IAfEffectHandle::create(
121 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800122 lStatus = handle->initCheck();
123 if (lStatus == NO_ERROR) {
124 lStatus = effect->addHandle(handle.get());
125 if (lStatus == NO_ERROR) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700126 lStatus = effect->init(&patches); // TODO(b/288339104) void*
Ram Mohan M2a05df22022-08-28 11:46:23 +0530127 if (lStatus == NAME_NOT_FOUND) {
128 lStatus = NO_ERROR;
129 }
130 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
François Gaffie0f660582023-06-27 15:15:26 +0200131 mDeviceEffects.erase(device);
132 mDeviceEffects.emplace(device, effectsForDevice);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530133 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800134 }
135 }
136 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200137 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138 *enabled = (int)effect->isEnabled();
139 }
140 *status = lStatus;
141 return handle;
142}
143
144status_t AudioFlinger::DeviceEffectManager::checkEffectCompatibility(
145 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700146 const sp<EffectsFactoryHalInterface> effectsFactory =
147 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700148 if (effectsFactory == nullptr) {
149 return BAD_VALUE;
150 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800151
Andy Hungba8e52b2023-05-11 14:33:03 -0700152 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000153 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700154 static const AudioHalVersionInfo halVersion =
155 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700156
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000157 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
158 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700159 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
160 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
161 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yao489c5a92022-12-02 05:35:41 +0000162 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
163 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800164 return BAD_VALUE;
165 }
166
167 return NO_ERROR;
168}
169
170status_t AudioFlinger::DeviceEffectManager::createEffectHal(
171 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
172 sp<EffectHalInterface> *effect) {
173 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700174 const sp<EffectsFactoryHalInterface> effectsFactory =
175 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800176 if (effectsFactory != 0) {
177 status = effectsFactory->createEffect(
178 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
179 }
180 return status;
181}
182
Andy Hung920f6572022-10-06 12:09:49 -0700183void AudioFlinger::DeviceEffectManager::dump(int fd)
184NO_THREAD_SAFETY_ANALYSIS // conditional try lock
185{
Eric Laurentb82e6b72019-11-22 17:25:04 -0800186 const bool locked = dumpTryLock(mLock);
187 if (!locked) {
188 String8 result("DeviceEffectManager may be deadlocked\n");
189 write(fd, result.string(), result.size());
190 }
191
Phil Burk651d0a52020-05-08 14:00:58 -0700192 String8 heading("\nDevice Effects:\n");
193 write(fd, heading.string(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800194 for (const auto& iter : mDeviceEffects) {
195 String8 outStr;
196 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
197 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
François Gaffie0f660582023-06-27 15:15:26 +0200198 for (const auto& effect : iter.second) {
199 write(fd, outStr.string(), outStr.size());
200 effect->dump2(fd, 4);
201 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800202 }
203
204 if (locked) {
205 mLock.unlock();
206 }
207}
208
Andy Hung6ac17eb2023-06-20 18:56:17 -0700209size_t AudioFlinger::DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800210{
211 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +0200212 const auto& iter = mDeviceEffects.find(effect->device());
213 if (iter != mDeviceEffects.end()) {
214 const auto& iterEffect = std::find_if(
215 iter->second.begin(), iter->second.end(), [&effect](const auto& effectProxy) {
216 return memcmp(&effectProxy->desc().uuid, &effect->desc().uuid,
217 sizeof(effect_uuid_t)) == 0;
218 });
219 if (iterEffect != iter->second.end()) {
220 iter->second.erase(iterEffect);
221 if (iter->second.empty()) {
222 mDeviceEffects.erase(effect->device());
223 }
224 }
225 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800226 return mDeviceEffects.size();
227}
228
229bool AudioFlinger::DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700230 IAfEffectHandle *handle, bool unpinIfLast) {
231 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800232 if (effectBase == nullptr) {
233 return false;
234 }
235
Andy Hung6ac17eb2023-06-20 18:56:17 -0700236 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800237 if (effect == nullptr) {
238 return false;
239 }
240 // restore suspended effects if the disconnected handle was enabled and the last one.
241 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
242 if (remove) {
243 mManager.removeEffect(effect);
244 if (handle->enabled()) {
245 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
246 }
247 }
248 return true;
249}
250
Eric Laurentb82e6b72019-11-22 17:25:04 -0800251} // namespace android