blob: 0a7be757c1695246e2938d397e3f29e1c5d49bde [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 Hungbf766942023-07-13 19:54:47 -070018#define LOG_TAG "DeviceEffectManager"
Eric Laurentb82e6b72019-11-22 17:25:04 -080019//#define LOG_NDEBUG 0
20
Andy Hung21ff9672023-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 Hung21ff9672023-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 Hung21ff9672023-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 Hunge9a1c7a2023-07-17 13:45:55 -070038DeviceEffectManager::DeviceEffectManager(
39 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback)
40 : mAfDeviceEffectManagerCallback(afDeviceEffectManagerCallback),
Andy Hunga2e69002023-09-06 17:37:34 -070041 mMyCallback(sp<DeviceEffectManagerCallback>::make(*this)) {}
Andy Hungbf766942023-07-13 19:54:47 -070042
43void DeviceEffectManager::onFirstRef() {
Andy Hunge9a1c7a2023-07-17 13:45:55 -070044 mAfDeviceEffectManagerCallback->getPatchCommandThread()->addListener(this);
Andy Hungbf766942023-07-13 19:54:47 -070045}
46
47status_t DeviceEffectManager::addEffectToHal(const struct audio_port_config* device,
48 const sp<EffectHalInterface>& effect) {
Andy Hunge9a1c7a2023-07-17 13:45:55 -070049 return mAfDeviceEffectManagerCallback->addEffectToHal(device, effect);
Andy Hungbf766942023-07-13 19:54:47 -070050};
51
52status_t DeviceEffectManager::removeEffectFromHal(const struct audio_port_config* device,
53 const sp<EffectHalInterface>& effect) {
Andy Hunge9a1c7a2023-07-17 13:45:55 -070054 return mAfDeviceEffectManagerCallback->removeEffectFromHal(device, effect);
Andy Hungbf766942023-07-13 19:54:47 -070055};
56
57void DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hung07434ef2023-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 Hung60a6c3d2023-08-29 12:19:17 -070062 audio_utils::lock_guard _l(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -080063 for (auto& effect : mDeviceEffects) {
Andy Hung07434ef2023-07-13 18:11:33 -070064 status_t status = effect.second->onCreatePatch(handle, patch);
Eric Laurentb82e6b72019-11-22 17:25:04 -080065 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
66 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
67 }
68}
69
Andy Hungbf766942023-07-13 19:54:47 -070070void DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080071 ALOGV("%s", __func__);
Andy Hung60a6c3d2023-08-29 12:19:17 -070072 audio_utils::lock_guard _l(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -080073 for (auto& effect : mDeviceEffects) {
74 effect.second->onReleasePatch(handle);
75 }
76}
77
Andy Hung60a6c3d2023-08-29 12:19:17 -070078// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mutex() held
Andy Hungbf766942023-07-13 19:54:47 -070079sp<IAfEffectHandle> DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080080 effect_descriptor_t *descriptor,
81 const AudioDeviceTypeAddr& device,
Andy Hungd65869f2023-06-27 17:05:02 -070082 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -080083 const sp<IEffectClient>& effectClient,
Andy Hung07434ef2023-07-13 18:11:33 -070084 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -080085 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 Hungbd72c542023-06-20 18:56:17 -070089 sp<IAfDeviceEffectProxy> effect;
90 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 {
Andy Hung60a6c3d2023-08-29 12:19:17 -0700100 audio_utils::lock_guard _l(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800101 auto iter = mDeviceEffects.find(device);
102 if (iter != mDeviceEffects.end()) {
103 effect = iter->second;
104 } else {
Andy Hungbd72c542023-06-20 18:56:17 -0700105 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Andy Hunge9a1c7a2023-07-17 13:45:55 -0700106 descriptor,
107 mAfDeviceEffectManagerCallback->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
Eric Laurentde8caf42021-08-11 17:19:25 +0200108 notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800109 }
110 // create effect handle and connect it to effect module
Andy Hungbd72c542023-06-20 18:56:17 -0700111 handle = IAfEffectHandle::create(
112 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800113 lStatus = handle->initCheck();
114 if (lStatus == NO_ERROR) {
115 lStatus = effect->addHandle(handle.get());
116 if (lStatus == NO_ERROR) {
Andy Hung07434ef2023-07-13 18:11:33 -0700117 lStatus = effect->init(patches);
Ram Mohan M2a05df22022-08-28 11:46:23 +0530118 if (lStatus == NAME_NOT_FOUND) {
119 lStatus = NO_ERROR;
120 }
121 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
122 mDeviceEffects.emplace(device, effect);
123 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800124 }
125 }
126 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200127 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800128 *enabled = (int)effect->isEnabled();
129 }
130 *status = lStatus;
131 return handle;
132}
133
Andy Hunga2e69002023-09-06 17:37:34 -0700134/* static */
Andy Hungbf766942023-07-13 19:54:47 -0700135status_t DeviceEffectManager::checkEffectCompatibility(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800136 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700137 const sp<EffectsFactoryHalInterface> effectsFactory =
138 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700139 if (effectsFactory == nullptr) {
140 return BAD_VALUE;
141 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800142
Andy Hungba8e52b2023-05-11 14:33:03 -0700143 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000144 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700145 static const AudioHalVersionInfo halVersion =
146 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700147
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000148 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
149 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700150 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
151 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
152 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000153 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
154 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800155 return BAD_VALUE;
156 }
157
158 return NO_ERROR;
159}
160
Andy Hunga2e69002023-09-06 17:37:34 -0700161/* static */
Andy Hungbf766942023-07-13 19:54:47 -0700162status_t DeviceEffectManager::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800163 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
164 sp<EffectHalInterface> *effect) {
165 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700166 const sp<EffectsFactoryHalInterface> effectsFactory =
167 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800168 if (effectsFactory != 0) {
169 status = effectsFactory->createEffect(
170 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
171 }
172 return status;
173}
174
Andy Hungbf766942023-07-13 19:54:47 -0700175void DeviceEffectManager::dump(int fd)
Andy Hung71ba4b32022-10-06 12:09:49 -0700176NO_THREAD_SAFETY_ANALYSIS // conditional try lock
177{
Andy Hung60a6c3d2023-08-29 12:19:17 -0700178 const bool locked = afutils::dumpTryLock(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800179 if (!locked) {
180 String8 result("DeviceEffectManager may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000181 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800182 }
183
Phil Burk651d0a52020-05-08 14:00:58 -0700184 String8 heading("\nDevice Effects:\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000185 write(fd, heading.c_str(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800186 for (const auto& iter : mDeviceEffects) {
187 String8 outStr;
188 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
189 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000190 write(fd, outStr.c_str(), outStr.size());
Andy Hungbd72c542023-06-20 18:56:17 -0700191 iter.second->dump2(fd, 4);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800192 }
193
194 if (locked) {
Andy Hung60a6c3d2023-08-29 12:19:17 -0700195 mutex().unlock();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800196 }
197}
198
Andy Hungbf766942023-07-13 19:54:47 -0700199size_t DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800200{
Andy Hung60a6c3d2023-08-29 12:19:17 -0700201 audio_utils::lock_guard _l(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800202 mDeviceEffects.erase(effect->device());
203 return mDeviceEffects.size();
204}
205
Andy Hungbf766942023-07-13 19:54:47 -0700206bool DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hungbd72c542023-06-20 18:56:17 -0700207 IAfEffectHandle *handle, bool unpinIfLast) {
208 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800209 if (effectBase == nullptr) {
210 return false;
211 }
212
Andy Hungbd72c542023-06-20 18:56:17 -0700213 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800214 if (effect == nullptr) {
215 return false;
216 }
217 // restore suspended effects if the disconnected handle was enabled and the last one.
218 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
219 if (remove) {
220 mManager.removeEffect(effect);
221 if (handle->enabled()) {
222 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
223 }
224 }
225 return true;
226}
227
Andy Hungbf766942023-07-13 19:54:47 -0700228bool DeviceEffectManagerCallback::isAudioPolicyReady() const {
Andy Hunge9a1c7a2023-07-17 13:45:55 -0700229 return mManager.afDeviceEffectManagerCallback()->isAudioPolicyReady();
Andy Hungbf766942023-07-13 19:54:47 -0700230}
231
232int DeviceEffectManagerCallback::newEffectId() const {
Andy Hunge9a1c7a2023-07-17 13:45:55 -0700233 return mManager.afDeviceEffectManagerCallback()->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
Andy Hungbf766942023-07-13 19:54:47 -0700234}
235
Eric Laurentb82e6b72019-11-22 17:25:04 -0800236} // namespace android