blob: c088c90664b8dcb9ba60996546d0b1eb4bef963f [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 Hungbf766942023-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 Hungbf766942023-07-13 19:54:47 -070037DeviceEffectManager::DeviceEffectManager(AudioFlinger& audioFlinger)
38 : mAudioFlinger(audioFlinger),
39 mMyCallback(new DeviceEffectManagerCallback(*this)) {}
40
41void DeviceEffectManager::onFirstRef() {
42 mAudioFlinger.mPatchCommandThread->addListener(this);
43}
44
45status_t DeviceEffectManager::addEffectToHal(const struct audio_port_config* device,
46 const sp<EffectHalInterface>& effect) {
47 return mAudioFlinger.addEffectToHal(device, effect);
48};
49
50status_t DeviceEffectManager::removeEffectFromHal(const struct audio_port_config* device,
51 const sp<EffectHalInterface>& effect) {
52 return mAudioFlinger.removeEffectFromHal(device, effect);
53};
54
55void DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung07434ef2023-07-13 18:11:33 -070056 const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080057 ALOGV("%s handle %d mHalHandle %d device sink %08x",
58 __func__, handle, patch.mHalHandle,
59 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
60 Mutex::Autolock _l(mLock);
61 for (auto& effect : mDeviceEffects) {
Andy Hung07434ef2023-07-13 18:11:33 -070062 status_t status = effect.second->onCreatePatch(handle, patch);
Eric Laurentb82e6b72019-11-22 17:25:04 -080063 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
64 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
65 }
66}
67
Andy Hungbf766942023-07-13 19:54:47 -070068void DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080069 ALOGV("%s", __func__);
70 Mutex::Autolock _l(mLock);
71 for (auto& effect : mDeviceEffects) {
72 effect.second->onReleasePatch(handle);
73 }
74}
75
76// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
Andy Hungbf766942023-07-13 19:54:47 -070077sp<IAfEffectHandle> DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080078 effect_descriptor_t *descriptor,
79 const AudioDeviceTypeAddr& device,
Andy Hungd65869f2023-06-27 17:05:02 -070080 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -080081 const sp<IEffectClient>& effectClient,
Andy Hung07434ef2023-07-13 18:11:33 -070082 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -080083 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070084 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020085 bool probe,
86 bool notifyFramesProcessed) {
Andy Hungbd72c542023-06-20 18:56:17 -070087 sp<IAfDeviceEffectProxy> effect;
88 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -080089 status_t lStatus;
90
91 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070092 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080093 *status = lStatus;
94 return handle;
95 }
96
97 {
98 Mutex::Autolock _l(mLock);
99 auto iter = mDeviceEffects.find(device);
100 if (iter != mDeviceEffects.end()) {
101 effect = iter->second;
102 } else {
Andy Hungbd72c542023-06-20 18:56:17 -0700103 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Eric Laurentde8caf42021-08-11 17:19:25 +0200104 descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
105 notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800106 }
107 // create effect handle and connect it to effect module
Andy Hungbd72c542023-06-20 18:56:17 -0700108 handle = IAfEffectHandle::create(
109 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800110 lStatus = handle->initCheck();
111 if (lStatus == NO_ERROR) {
112 lStatus = effect->addHandle(handle.get());
113 if (lStatus == NO_ERROR) {
Andy Hung07434ef2023-07-13 18:11:33 -0700114 lStatus = effect->init(patches);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530115 if (lStatus == NAME_NOT_FOUND) {
116 lStatus = NO_ERROR;
117 }
118 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
119 mDeviceEffects.emplace(device, effect);
120 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800121 }
122 }
123 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200124 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800125 *enabled = (int)effect->isEnabled();
126 }
127 *status = lStatus;
128 return handle;
129}
130
Andy Hungbf766942023-07-13 19:54:47 -0700131status_t DeviceEffectManager::checkEffectCompatibility(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800132 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700133 const sp<EffectsFactoryHalInterface> effectsFactory =
134 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700135 if (effectsFactory == nullptr) {
136 return BAD_VALUE;
137 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138
Andy Hungba8e52b2023-05-11 14:33:03 -0700139 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000140 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700141 static const AudioHalVersionInfo halVersion =
142 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700143
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000144 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
145 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700146 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
147 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
148 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000149 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
150 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800151 return BAD_VALUE;
152 }
153
154 return NO_ERROR;
155}
156
Andy Hungbf766942023-07-13 19:54:47 -0700157status_t DeviceEffectManager::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800158 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
159 sp<EffectHalInterface> *effect) {
160 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700161 const sp<EffectsFactoryHalInterface> effectsFactory =
162 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800163 if (effectsFactory != 0) {
164 status = effectsFactory->createEffect(
165 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
166 }
167 return status;
168}
169
Andy Hungbf766942023-07-13 19:54:47 -0700170void DeviceEffectManager::dump(int fd)
Andy Hung71ba4b32022-10-06 12:09:49 -0700171NO_THREAD_SAFETY_ANALYSIS // conditional try lock
172{
Andy Hungbf766942023-07-13 19:54:47 -0700173 const bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800174 if (!locked) {
175 String8 result("DeviceEffectManager may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000176 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800177 }
178
Phil Burk651d0a52020-05-08 14:00:58 -0700179 String8 heading("\nDevice Effects:\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000180 write(fd, heading.c_str(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800181 for (const auto& iter : mDeviceEffects) {
182 String8 outStr;
183 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
184 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000185 write(fd, outStr.c_str(), outStr.size());
Andy Hungbd72c542023-06-20 18:56:17 -0700186 iter.second->dump2(fd, 4);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800187 }
188
189 if (locked) {
190 mLock.unlock();
191 }
192}
193
Andy Hungbf766942023-07-13 19:54:47 -0700194size_t DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800195{
196 Mutex::Autolock _l(mLock);
197 mDeviceEffects.erase(effect->device());
198 return mDeviceEffects.size();
199}
200
Andy Hungbf766942023-07-13 19:54:47 -0700201bool DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hungbd72c542023-06-20 18:56:17 -0700202 IAfEffectHandle *handle, bool unpinIfLast) {
203 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800204 if (effectBase == nullptr) {
205 return false;
206 }
207
Andy Hungbd72c542023-06-20 18:56:17 -0700208 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800209 if (effect == nullptr) {
210 return false;
211 }
212 // restore suspended effects if the disconnected handle was enabled and the last one.
213 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
214 if (remove) {
215 mManager.removeEffect(effect);
216 if (handle->enabled()) {
217 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
218 }
219 }
220 return true;
221}
222
Andy Hungbf766942023-07-13 19:54:47 -0700223bool DeviceEffectManagerCallback::isAudioPolicyReady() const {
224 return mManager.audioFlinger().isAudioPolicyReady();
225}
226
227int DeviceEffectManagerCallback::newEffectId() const {
228 return mManager.audioFlinger().nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
229}
230
Eric Laurentb82e6b72019-11-22 17:25:04 -0800231} // namespace android