blob: 480d16d7ce977d955094dac2ac5c1e04adadb011 [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,
Andy Hung8e6b62a2023-07-13 18:11:33 -070038 const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080039 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) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070045 const status_t status = effect->onCreatePatch(handle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020046 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,
Andy Hung8e6b62a2023-07-13 18:11:33 -070063 audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) {
François Gaffie58e73af2023-02-15 11:47:24 +010064 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) {
Andy Hung8e6b62a2023-07-13 18:11:33 -070070 const status_t status = effect->onUpdatePatch(oldHandle, newHandle, patch);
François Gaffie0f660582023-06-27 15:15:26 +020071 ALOGV("%s Effect onUpdatePatch status %d", __func__, status);
72 ALOGW_IF(status != NO_ERROR, "%s onUpdatePatch error %d", __func__, status);
73 }
François Gaffie58e73af2023-02-15 11:47:24 +010074 }
75}
76
Eric Laurentb82e6b72019-11-22 17:25:04 -080077// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -070078sp<IAfEffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080079 effect_descriptor_t *descriptor,
80 const AudioDeviceTypeAddr& device,
Andy Hung59867e42023-06-27 17:05:02 -070081 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -080082 const sp<IEffectClient>& effectClient,
Andy Hung8e6b62a2023-07-13 18:11:33 -070083 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -080084 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070085 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020086 bool probe,
87 bool notifyFramesProcessed) {
Andy Hung6ac17eb2023-06-20 18:56:17 -070088 sp<IAfDeviceEffectProxy> effect;
François Gaffie0f660582023-06-27 15:15:26 +020089 std::vector<sp<IAfDeviceEffectProxy>> effectsForDevice = {};
Andy Hung6ac17eb2023-06-20 18:56:17 -070090 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -080091 status_t lStatus;
92
93 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070094 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080095 *status = lStatus;
96 return handle;
97 }
98
99 {
100 Mutex::Autolock _l(mLock);
101 auto iter = mDeviceEffects.find(device);
102 if (iter != mDeviceEffects.end()) {
François Gaffie0f660582023-06-27 15:15:26 +0200103 effectsForDevice = iter->second;
104 for (const auto& iterEffect : effectsForDevice) {
105 if (memcmp(&iterEffect->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) ==
106 0) {
107 effect = iterEffect;
108 break;
109 }
110 }
111 }
112 if (effect == nullptr) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700113 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200114 descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
115 notifyFramesProcessed);
François Gaffie0f660582023-06-27 15:15:26 +0200116 effectsForDevice.push_back(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800117 }
118 // create effect handle and connect it to effect module
Andy Hung6ac17eb2023-06-20 18:56:17 -0700119 handle = IAfEffectHandle::create(
120 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800121 lStatus = handle->initCheck();
122 if (lStatus == NO_ERROR) {
123 lStatus = effect->addHandle(handle.get());
124 if (lStatus == NO_ERROR) {
Andy Hung8e6b62a2023-07-13 18:11:33 -0700125 lStatus = effect->init(patches);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530126 if (lStatus == NAME_NOT_FOUND) {
127 lStatus = NO_ERROR;
128 }
129 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
François Gaffie0f660582023-06-27 15:15:26 +0200130 mDeviceEffects.erase(device);
131 mDeviceEffects.emplace(device, effectsForDevice);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530132 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800133 }
134 }
135 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200136 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800137 *enabled = (int)effect->isEnabled();
138 }
139 *status = lStatus;
140 return handle;
141}
142
143status_t AudioFlinger::DeviceEffectManager::checkEffectCompatibility(
144 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700145 const sp<EffectsFactoryHalInterface> effectsFactory =
146 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700147 if (effectsFactory == nullptr) {
148 return BAD_VALUE;
149 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800150
Andy Hungba8e52b2023-05-11 14:33:03 -0700151 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000152 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700153 static const AudioHalVersionInfo halVersion =
154 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700155
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000156 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
157 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700158 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
159 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
160 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yao489c5a92022-12-02 05:35:41 +0000161 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
162 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800163 return BAD_VALUE;
164 }
165
166 return NO_ERROR;
167}
168
169status_t AudioFlinger::DeviceEffectManager::createEffectHal(
170 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
171 sp<EffectHalInterface> *effect) {
172 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700173 const sp<EffectsFactoryHalInterface> effectsFactory =
174 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800175 if (effectsFactory != 0) {
176 status = effectsFactory->createEffect(
177 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
178 }
179 return status;
180}
181
Andy Hung920f6572022-10-06 12:09:49 -0700182void AudioFlinger::DeviceEffectManager::dump(int fd)
183NO_THREAD_SAFETY_ANALYSIS // conditional try lock
184{
Eric Laurentb82e6b72019-11-22 17:25:04 -0800185 const bool locked = dumpTryLock(mLock);
186 if (!locked) {
187 String8 result("DeviceEffectManager may be deadlocked\n");
188 write(fd, result.string(), result.size());
189 }
190
Phil Burk651d0a52020-05-08 14:00:58 -0700191 String8 heading("\nDevice Effects:\n");
192 write(fd, heading.string(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800193 for (const auto& iter : mDeviceEffects) {
194 String8 outStr;
195 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
196 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
François Gaffie0f660582023-06-27 15:15:26 +0200197 for (const auto& effect : iter.second) {
198 write(fd, outStr.string(), outStr.size());
199 effect->dump2(fd, 4);
200 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800201 }
202
203 if (locked) {
204 mLock.unlock();
205 }
206}
207
Andy Hung6ac17eb2023-06-20 18:56:17 -0700208size_t AudioFlinger::DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800209{
210 Mutex::Autolock _l(mLock);
François Gaffie0f660582023-06-27 15:15:26 +0200211 const auto& iter = mDeviceEffects.find(effect->device());
212 if (iter != mDeviceEffects.end()) {
213 const auto& iterEffect = std::find_if(
214 iter->second.begin(), iter->second.end(), [&effect](const auto& effectProxy) {
215 return memcmp(&effectProxy->desc().uuid, &effect->desc().uuid,
216 sizeof(effect_uuid_t)) == 0;
217 });
218 if (iterEffect != iter->second.end()) {
219 iter->second.erase(iterEffect);
220 if (iter->second.empty()) {
221 mDeviceEffects.erase(effect->device());
222 }
223 }
224 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800225 return mDeviceEffects.size();
226}
227
228bool AudioFlinger::DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700229 IAfEffectHandle *handle, bool unpinIfLast) {
230 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800231 if (effectBase == nullptr) {
232 return false;
233 }
234
Andy Hung6ac17eb2023-06-20 18:56:17 -0700235 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800236 if (effect == nullptr) {
237 return false;
238 }
239 // restore suspended effects if the disconnected handle was enabled and the last one.
240 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
241 if (remove) {
242 mManager.removeEffect(effect);
243 if (handle->enabled()) {
244 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
245 }
246 }
247 return true;
248}
249
Eric Laurentb82e6b72019-11-22 17:25:04 -0800250} // namespace android