blob: a3c3b84797700c02fa4a1bdcc580406174152787 [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"
26#include <media/audiohal/EffectsFactoryHalInterface.h>
27
28// ----------------------------------------------------------------------------
29
30
31namespace android {
32
33void AudioFlinger::DeviceEffectManager::createAudioPatch(audio_patch_handle_t handle,
34 const PatchPanel::Patch& patch) {
35 ALOGV("%s handle %d mHalHandle %d num sinks %d device sink %08x",
36 __func__, handle, patch.mHalHandle,
37 patch.mAudioPatch.num_sinks,
38 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
39
40 mCommandThread->createAudioPatchCommand(handle, patch);
41}
42
43void AudioFlinger::DeviceEffectManager::onCreateAudioPatch(audio_patch_handle_t handle,
44 const PatchPanel::Patch& patch) {
45 ALOGV("%s handle %d mHalHandle %d device sink %08x",
46 __func__, handle, patch.mHalHandle,
47 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
48 Mutex::Autolock _l(mLock);
49 for (auto& effect : mDeviceEffects) {
50 status_t status = effect.second->onCreatePatch(handle, patch);
51 ALOGV("%s Effect onCreatePatch status %d", __func__, status);
52 ALOGW_IF(status == BAD_VALUE, "%s onCreatePatch error %d", __func__, status);
53 }
54}
55
56void AudioFlinger::DeviceEffectManager::releaseAudioPatch(audio_patch_handle_t handle) {
57 ALOGV("%s", __func__);
58 mCommandThread->releaseAudioPatchCommand(handle);
59}
60
61void AudioFlinger::DeviceEffectManager::onReleaseAudioPatch(audio_patch_handle_t handle) {
62 ALOGV("%s", __func__);
63 Mutex::Autolock _l(mLock);
64 for (auto& effect : mDeviceEffects) {
65 effect.second->onReleasePatch(handle);
66 }
67}
68
69// DeviceEffectManager::createEffect_l() must be called with AudioFlinger::mLock held
70sp<AudioFlinger::EffectHandle> AudioFlinger::DeviceEffectManager::createEffect_l(
71 effect_descriptor_t *descriptor,
72 const AudioDeviceTypeAddr& device,
73 const sp<AudioFlinger::Client>& client,
74 const sp<IEffectClient>& effectClient,
75 const std::map<audio_patch_handle_t, PatchPanel::Patch>& patches,
76 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070077 status_t *status,
78 bool probe) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080079 sp<DeviceEffectProxy> effect;
80 sp<EffectHandle> handle;
81 status_t lStatus;
82
83 lStatus = checkEffectCompatibility(descriptor);
Eric Laurent2fe0acd2020-03-13 14:30:46 -070084 if (probe || lStatus != NO_ERROR) {
Eric Laurentb82e6b72019-11-22 17:25:04 -080085 *status = lStatus;
86 return handle;
87 }
88
89 {
90 Mutex::Autolock _l(mLock);
91 auto iter = mDeviceEffects.find(device);
92 if (iter != mDeviceEffects.end()) {
93 effect = iter->second;
94 } else {
95 effect = new DeviceEffectProxy(device, mMyCallback,
96 descriptor, mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT));
97 }
98 // create effect handle and connect it to effect module
99 handle = new EffectHandle(effect, client, effectClient, 0 /*priority*/);
100 lStatus = handle->initCheck();
101 if (lStatus == NO_ERROR) {
102 lStatus = effect->addHandle(handle.get());
103 if (lStatus == NO_ERROR) {
104 effect->init(patches);
105 mDeviceEffects.emplace(device, effect);
106 }
107 }
108 }
109 if (enabled != NULL) {
110 *enabled = (int)effect->isEnabled();
111 }
112 *status = lStatus;
113 return handle;
114}
115
116status_t AudioFlinger::DeviceEffectManager::checkEffectCompatibility(
117 const effect_descriptor_t *desc) {
118
119 if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC
120 && (desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
121 ALOGW("%s() non pre/post processing device effect %s", __func__, desc->name);
122 return BAD_VALUE;
123 }
124
125 return NO_ERROR;
126}
127
128status_t AudioFlinger::DeviceEffectManager::createEffectHal(
129 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
130 sp<EffectHalInterface> *effect) {
131 status_t status = NO_INIT;
132 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
133 if (effectsFactory != 0) {
134 status = effectsFactory->createEffect(
135 pEffectUuid, sessionId, AUDIO_IO_HANDLE_NONE, deviceId, effect);
136 }
137 return status;
138}
139
140void AudioFlinger::DeviceEffectManager::dump(int fd) {
141 const bool locked = dumpTryLock(mLock);
142 if (!locked) {
143 String8 result("DeviceEffectManager may be deadlocked\n");
144 write(fd, result.string(), result.size());
145 }
146
147 write(fd, "\nDevice Effects:\n", sizeof("\nDevice Effects:\n"));
148 for (const auto& iter : mDeviceEffects) {
149 String8 outStr;
150 outStr.appendFormat("%*sEffect for device %s address %s:\n", 2, "",
151 ::android::toString(iter.first.mType).c_str(), iter.first.getAddress());
152 write(fd, outStr.string(), outStr.size());
153 iter.second->dump(fd, 4);
154 }
155
156 if (locked) {
157 mLock.unlock();
158 }
159}
160
161
162size_t AudioFlinger::DeviceEffectManager::removeEffect(const sp<DeviceEffectProxy>& effect)
163{
164 Mutex::Autolock _l(mLock);
165 mDeviceEffects.erase(effect->device());
166 return mDeviceEffects.size();
167}
168
169bool AudioFlinger::DeviceEffectManagerCallback::disconnectEffectHandle(
170 EffectHandle *handle, bool unpinIfLast) {
171 sp<EffectBase> effectBase = handle->effect().promote();
172 if (effectBase == nullptr) {
173 return false;
174 }
175
176 sp<DeviceEffectProxy> effect = effectBase->asDeviceEffectProxy();
177 if (effect == nullptr) {
178 return false;
179 }
180 // restore suspended effects if the disconnected handle was enabled and the last one.
181 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
182 if (remove) {
183 mManager.removeEffect(effect);
184 if (handle->enabled()) {
185 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
186 }
187 }
188 return true;
189}
190
191// ----------- DeviceEffectManager::CommandThread implementation ----------
192
193
194AudioFlinger::DeviceEffectManager::CommandThread::~CommandThread()
195{
196 Mutex::Autolock _l(mLock);
197 mCommands.clear();
198}
199
200void AudioFlinger::DeviceEffectManager::CommandThread::onFirstRef()
201{
202 run("DeviceEffectManage_CommandThread", ANDROID_PRIORITY_AUDIO);
203}
204
205bool AudioFlinger::DeviceEffectManager::CommandThread::threadLoop()
206{
207 mLock.lock();
208 while (!exitPending())
209 {
210 while (!mCommands.empty() && !exitPending()) {
211 sp<Command> command = mCommands.front();
212 mCommands.pop_front();
213 mLock.unlock();
214
215 switch (command->mCommand) {
216 case CREATE_AUDIO_PATCH: {
217 CreateAudioPatchData *data = (CreateAudioPatchData *)command->mData.get();
218 ALOGV("CommandThread() processing create audio patch handle %d", data->mHandle);
219 mManager.onCreateAudioPatch(data->mHandle, data->mPatch);
220 } break;
221 case RELEASE_AUDIO_PATCH: {
222 ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mData.get();
223 ALOGV("CommandThread() processing release audio patch handle %d", data->mHandle);
224 mManager.onReleaseAudioPatch(data->mHandle);
225 } break;
226 default:
227 ALOGW("CommandThread() unknown command %d", command->mCommand);
228 }
229 mLock.lock();
230 }
231
232 // At this stage we have either an empty command queue or the first command in the queue
233 // has a finite delay. So unless we are exiting it is safe to wait.
234 if (!exitPending()) {
235 ALOGV("CommandThread() going to sleep");
236 mWaitWorkCV.wait(mLock);
237 }
238 }
239 mLock.unlock();
240 return false;
241}
242
243void AudioFlinger::DeviceEffectManager::CommandThread::sendCommand(sp<Command> command) {
244 Mutex::Autolock _l(mLock);
245 mCommands.push_back(command);
246 mWaitWorkCV.signal();
247}
248
249void AudioFlinger::DeviceEffectManager::CommandThread::createAudioPatchCommand(
250 audio_patch_handle_t handle, const PatchPanel::Patch& patch)
251{
252 sp<Command> command = new Command(CREATE_AUDIO_PATCH, new CreateAudioPatchData(handle, patch));
253 ALOGV("CommandThread() adding create patch handle %d mHalHandle %d.", handle, patch.mHalHandle);
254 sendCommand(command);
255}
256
257void AudioFlinger::DeviceEffectManager::CommandThread::releaseAudioPatchCommand(
258 audio_patch_handle_t handle)
259{
260 sp<Command> command = new Command(RELEASE_AUDIO_PATCH, new ReleaseAudioPatchData(handle));
261 ALOGV("CommandThread() adding release patch");
262 sendCommand(command);
263}
264
265void AudioFlinger::DeviceEffectManager::CommandThread::exit()
266{
267 ALOGV("CommandThread::exit");
268 {
269 AutoMutex _l(mLock);
270 requestExit();
271 mWaitWorkCV.signal();
272 }
273 // Note that we can call it from the thread loop if all other references have been released
274 // but it will safely return WOULD_BLOCK in this case
275 requestExitAndWait();
276}
277
278} // namespace android