blob: 366a7ab9558fecffd89426b2e68346fe3ee682dc [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);
43 for (auto& effect : mDeviceEffects) {
Andy Hung6ac17eb2023-06-20 18:56:17 -070044 status_t status = effect.second->onCreatePatch(handle, &patch); // TODO(b/288339104) void*
Eric Laurentb82e6b72019-11-22 17:25:04 -080045 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
46 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
47 }
48}
49
Eric Laurentb82e6b72019-11-22 17:25:04 -080050void AudioFlinger::DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
51 ALOGV("%s", __func__);
52 Mutex::Autolock _l(mLock);
53 for (auto& effect : mDeviceEffects) {
54 effect.second->onReleasePatch(handle);
55 }
56}
57
58// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -070059sp<IAfEffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l(
Eric Laurentb82e6b72019-11-22 17:25:04 -080060 effect_descriptor_t *descriptor,
61 const AudioDeviceTypeAddr& device,
Andy Hung59867e42023-06-27 17:05:02 -070062 const sp<Client>& client,
Eric Laurentb82e6b72019-11-22 17:25:04 -080063 const sp<IEffectClient>& effectClient,
64 const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches,
65 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070066 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020067 bool probe,
68 bool notifyFramesProcessed) {
Andy Hung6ac17eb2023-06-20 18:56:17 -070069 sp<IAfDeviceEffectProxy> effect;
70 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -080071 status_t lStatus;
72
73 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070074 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080075 *status = lStatus;
76 return handle;
77 }
78
79 {
80 Mutex::Autolock _l(mLock);
81 auto iter = mDeviceEffects.find(device);
82 if (iter != mDeviceEffects.end()) {
83 effect = iter->second;
84 } else {
Andy Hung6ac17eb2023-06-20 18:56:17 -070085 effect = IAfDeviceEffectProxy::create(device, mMyCallback,
Eric Laurentde8caf42021-08-11 17:19:25 +020086 descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT),
87 notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -080088 }
89 // create effect handle and connect it to effect module
Andy Hung6ac17eb2023-06-20 18:56:17 -070090 handle = IAfEffectHandle::create(
91 effect, client, effectClient, 0 /*priority*/, notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -080092 lStatus = handle->initCheck();
93 if (lStatus == NO_ERROR) {
94 lStatus = effect->addHandle(handle.get());
95 if (lStatus == NO_ERROR) {
Andy Hung6ac17eb2023-06-20 18:56:17 -070096 lStatus = effect->init(&patches); // TODO(b/288339104) void*
Ram Mohan M2a05df22022-08-28 11:46:23 +053097 if (lStatus == NAME_NOT_FOUND) {
98 lStatus = NO_ERROR;
99 }
100 if (lStatus == NO_ERROR || lStatus == ALREADY_EXISTS) {
101 mDeviceEffects.emplace(device, effect);
102 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800103 }
104 }
105 }
Vlad Popa5161f8a2022-10-10 16:17:20 +0200106 if (enabled != nullptr) {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800107 *enabled = (int)effect->isEnabled();
108 }
109 *status = lStatus;
110 return handle;
111}
112
113status_t AudioFlinger::DeviceEffectManager::checkEffectCompatibility(
114 const effect_descriptor_t *desc) {
Andy Hungba8e52b2023-05-11 14:33:03 -0700115 const sp<EffectsFactoryHalInterface> effectsFactory =
116 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurent9289bde2020-08-18 12:49:17 -0700117 if (effectsFactory == nullptr) {
118 return BAD_VALUE;
119 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800120
Andy Hungba8e52b2023-05-11 14:33:03 -0700121 static const AudioHalVersionInfo sMinDeviceEffectHalVersion =
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000122 AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, 6, 0);
Andy Hungba8e52b2023-05-11 14:33:03 -0700123 static const AudioHalVersionInfo halVersion =
124 audioflinger::EffectConfiguration::getAudioHalVersionInfo();
Eric Laurent9289bde2020-08-18 12:49:17 -0700125
Shunkai Yaod7ea4092022-12-12 00:44:33 +0000126 // We can trust AIDL generated AudioHalVersionInfo comparison operator (based on std::tie) as
127 // long as the type, major and minor sequence doesn't change in the definition.
Eric Laurent9289bde2020-08-18 12:49:17 -0700128 if (((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
129 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC)
130 || halVersion < sMinDeviceEffectHalVersion) {
Shunkai Yao489c5a92022-12-02 05:35:41 +0000131 ALOGW("%s() non pre/post processing device effect %s or incompatible API version %s",
132 __func__, desc->name, halVersion.toString().c_str());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800133 return BAD_VALUE;
134 }
135
136 return NO_ERROR;
137}
138
139status_t AudioFlinger::DeviceEffectManager::createEffectHal(
140 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
141 sp<EffectHalInterface> *effect) {
142 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -0700143 const sp<EffectsFactoryHalInterface> effectsFactory =
144 audioflinger::EffectConfiguration::getEffectsFactoryHal();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800145 if (effectsFactory != 0) {
146 status = effectsFactory->createEffect(
147 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
148 }
149 return status;
150}
151
Andy Hung920f6572022-10-06 12:09:49 -0700152void AudioFlinger::DeviceEffectManager::dump(int fd)
153NO_THREAD_SAFETY_ANALYSIS // conditional try lock
154{
Eric Laurentb82e6b72019-11-22 17:25:04 -0800155 const bool locked = dumpTryLock(mLock);
156 if (!locked) {
157 String8 result("DeviceEffectManager may be deadlocked\n");
158 write(fd, result.string(), result.size());
159 }
160
Phil Burk651d0a52020-05-08 14:00:58 -0700161 String8 heading("\nDevice Effects:\n");
162 write(fd, heading.string(), heading.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -0800163 for (const auto& iter : mDeviceEffects) {
164 String8 outStr;
165 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
166 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
167 write(fd, outStr.string(), outStr.size());
Andy Hung6ac17eb2023-06-20 18:56:17 -0700168 iter.second->dump2(fd, 4);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800169 }
170
171 if (locked) {
172 mLock.unlock();
173 }
174}
175
176
Andy Hung6ac17eb2023-06-20 18:56:17 -0700177size_t AudioFlinger::DeviceEffectManager::removeEffect(const sp<IAfDeviceEffectProxy>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -0800178{
179 Mutex::Autolock _l(mLock);
180 mDeviceEffects.erase(effect->device());
181 return mDeviceEffects.size();
182}
183
184bool AudioFlinger::DeviceEffectManagerCallback::disconnectEffectHandle(
Andy Hung6ac17eb2023-06-20 18:56:17 -0700185 IAfEffectHandle *handle, bool unpinIfLast) {
186 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800187 if (effectBase == nullptr) {
188 return false;
189 }
190
Andy Hung6ac17eb2023-06-20 18:56:17 -0700191 sp<IAfDeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
Eric Laurentb82e6b72019-11-22 17:25:04 -0800192 if (effect == nullptr) {
193 return false;
194 }
195 // restore suspended effects if the disconnected handle was enabled and the last one.
196 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
197 if (remove) {
198 mManager.removeEffect(effect);
199 if (handle->enabled()) {
200 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
201 }
202 }
203 return true;
204}
205
Eric Laurentb82e6b72019-11-22 17:25:04 -0800206} // namespace android