blob: d891a32a4f11bc79db245e0cf93e70d7d9aa07a0 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, 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"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Andy Hungba8e52b2023-05-11 14:33:03 -070047#include "EffectConfiguration.h"
Andy Hungbd72c542023-06-20 18:56:17 -070048#include "Effects.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080049
50// ----------------------------------------------------------------------------
51
52// Note: the following macro is used for extremely verbose logging message. In
53// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
54// 0; but one side effect of this is to turn all LOGV's as well. Some messages
55// are so verbose that we want to suppress them even when we have ALOG_ASSERT
56// turned on. Do not uncomment the #def below unless you really know what you
57// are doing and want to see all of the extremely verbose messages.
58//#define VERY_VERY_VERBOSE_LOGGING
59#ifdef VERY_VERY_VERBOSE_LOGGING
60#define ALOGVV ALOGV
61#else
62#define ALOGVV(a...) do { } while(0)
63#endif
64
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090065#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
66
Eric Laurentca7cc822012-11-19 14:55:58 -080067namespace android {
68
Andy Hung1131b6e2020-12-08 20:47:45 -080069using aidl_utils::statusTFromBinderStatus;
Andy Hungba8e52b2023-05-11 14:33:03 -070070using audioflinger::EffectConfiguration;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070071using binder::Status;
72
73namespace {
74
75// Append a POD value into a vector of bytes.
76template<typename T>
77void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
78 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
79 buffer->insert(buffer->end(), ar, ar + sizeof(T));
80}
81
82// Write a POD value into a vector of bytes (clears the previous buffer
83// content).
84template<typename T>
85void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
86 buffer->clear();
87 appendToBuffer(value, buffer);
88}
89
90} // namespace
91
Eric Laurentca7cc822012-11-19 14:55:58 -080092// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080093// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080094// ----------------------------------------------------------------------------
95
96#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -070097#define LOG_TAG "EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080098
Andy Hungbd72c542023-06-20 18:56:17 -070099EffectBase::EffectBase(const sp<EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -0800100 effect_descriptor_t *desc,
101 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800102 audio_session_t sessionId,
103 bool pinned)
104 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800105 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800106 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800107{
Eric Laurentca7cc822012-11-19 14:55:58 -0800108}
109
Eric Laurent41709552019-12-16 19:34:05 -0800110// must be called with EffectModule::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -0700111status_t EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800112{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800113
Eric Laurent41709552019-12-16 19:34:05 -0800114 ALOGV("setEnabled %p enabled %d", this, enabled);
115
116 if (enabled != isEnabled()) {
117 switch (mState) {
118 // going from disabled to enabled
119 case IDLE:
120 mState = STARTING;
121 break;
122 case STOPPED:
123 mState = RESTART;
124 break;
125 case STOPPING:
126 mState = ACTIVE;
127 break;
128
129 // going from enabled to disabled
130 case RESTART:
131 mState = STOPPED;
132 break;
133 case STARTING:
134 mState = IDLE;
135 break;
136 case ACTIVE:
137 mState = STOPPING;
138 break;
139 case DESTROYED:
140 return NO_ERROR; // simply ignore as we are being destroyed
141 }
142 for (size_t i = 1; i < mHandles.size(); i++) {
Andy Hungbd72c542023-06-20 18:56:17 -0700143 IAfEffectHandle *h = mHandles[i];
Eric Laurent41709552019-12-16 19:34:05 -0800144 if (h != NULL && !h->disconnected()) {
145 h->setEnabled(enabled);
146 }
147 }
148 }
149 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800150}
151
Andy Hungbd72c542023-06-20 18:56:17 -0700152status_t EffectBase::setEnabled(bool enabled, bool fromHandle)
Eric Laurent41709552019-12-16 19:34:05 -0800153{
154 status_t status;
155 {
156 Mutex::Autolock _l(mLock);
157 status = setEnabled_l(enabled);
158 }
159 if (fromHandle) {
160 if (enabled) {
161 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700162 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800163 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800165 }
166 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700167 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800168 }
169 }
170 return status;
171}
172
Andy Hungbd72c542023-06-20 18:56:17 -0700173bool EffectBase::isEnabled() const
Eric Laurent41709552019-12-16 19:34:05 -0800174{
175 switch (mState) {
176 case RESTART:
177 case STARTING:
178 case ACTIVE:
179 return true;
180 case IDLE:
181 case STOPPING:
182 case STOPPED:
183 case DESTROYED:
184 default:
185 return false;
186 }
187}
188
Andy Hungbd72c542023-06-20 18:56:17 -0700189void EffectBase::setSuspended(bool suspended)
Eric Laurent41709552019-12-16 19:34:05 -0800190{
191 Mutex::Autolock _l(mLock);
192 mSuspended = suspended;
193}
194
Andy Hungbd72c542023-06-20 18:56:17 -0700195bool EffectBase::suspended() const
Eric Laurent41709552019-12-16 19:34:05 -0800196{
197 Mutex::Autolock _l(mLock);
198 return mSuspended;
199}
200
Andy Hungbd72c542023-06-20 18:56:17 -0700201status_t EffectBase::addHandle(IAfEffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800202{
203 status_t status;
204
205 Mutex::Autolock _l(mLock);
206 int priority = handle->priority();
207 size_t size = mHandles.size();
Andy Hungbd72c542023-06-20 18:56:17 -0700208 IAfEffectHandle *controlHandle = nullptr;
Eric Laurentca7cc822012-11-19 14:55:58 -0800209 size_t i;
210 for (i = 0; i < size; i++) {
Andy Hungbd72c542023-06-20 18:56:17 -0700211 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800212 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800213 continue;
214 }
215 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700216 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800217 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700218 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800219 if (h->priority() <= priority) {
220 break;
221 }
222 }
223 // if inserted in first place, move effect control from previous owner to this handle
224 if (i == 0) {
225 bool enabled = false;
226 if (controlHandle != NULL) {
227 enabled = controlHandle->enabled();
228 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
229 }
230 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
231 status = NO_ERROR;
232 } else {
233 status = ALREADY_EXISTS;
234 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700235 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800236 mHandles.insertAt(handle, i);
237 return status;
238}
239
Andy Hungbd72c542023-06-20 18:56:17 -0700240status_t EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700241{
242 status_t status = NO_ERROR;
243 bool doRegister = false;
244 bool registered = false;
245 bool doEnable = false;
246 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700247 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800248 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700249
250 {
251 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200252
253 if ((isInternal_l() && !mPolicyRegistered)
254 || !getCallback()->isAudioPolicyReady()) {
255 return NO_ERROR;
256 }
257
Eric Laurent6c796322019-04-09 14:13:17 -0700258 // register effect when first handle is attached and unregister when last handle is removed
259 if (mPolicyRegistered != mHandles.size() > 0) {
260 doRegister = true;
261 mPolicyRegistered = mHandles.size() > 0;
262 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700263 const auto callback = getCallback();
264 io = callback->io();
265 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700266 }
267 }
268 // enable effect when registered according to enable state requested by controlling handle
269 if (mHandles.size() > 0) {
Andy Hungbd72c542023-06-20 18:56:17 -0700270 IAfEffectHandle *handle = controlHandle_l();
Eric Laurent6c796322019-04-09 14:13:17 -0700271 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
272 doEnable = true;
273 mPolicyEnabled = handle->enabled();
274 }
275 }
276 registered = mPolicyRegistered;
277 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100278 // The simultaneous release of two EffectHandles with the same EffectModule
279 // may cause us to call this method at the same time.
280 // This may deadlock under some circumstances (b/180941720). Avoid this.
281 if (!doRegister && !(registered && doEnable)) {
282 return NO_ERROR;
283 }
Eric Laurent6c796322019-04-09 14:13:17 -0700284 }
fengjnlan46407562022-09-07 16:20:01 +0800285 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700286 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
287 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
288 if (doRegister) {
289 if (registered) {
290 status = AudioSystem::registerEffect(
291 &mDescriptor,
292 io,
293 strategy,
294 mSessionId,
295 mId);
296 } else {
297 status = AudioSystem::unregisterEffect(mId);
298 }
299 }
300 if (registered && doEnable) {
301 status = AudioSystem::setEffectEnabled(mId, enabled);
302 }
303 mPolicyLock.unlock();
304
305 return status;
306}
307
308
Andy Hungbd72c542023-06-20 18:56:17 -0700309ssize_t EffectBase::removeHandle(IAfEffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800310{
311 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800312 return removeHandle_l(handle);
313}
314
Andy Hungbd72c542023-06-20 18:56:17 -0700315ssize_t EffectBase::removeHandle_l(IAfEffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800316{
Eric Laurentca7cc822012-11-19 14:55:58 -0800317 size_t size = mHandles.size();
318 size_t i;
319 for (i = 0; i < size; i++) {
320 if (mHandles[i] == handle) {
321 break;
322 }
323 }
324 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800325 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
326 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800327 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800328 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800329
330 mHandles.removeAt(i);
331 // if removed from first place, move effect control from this handle to next in line
332 if (i == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -0700333 IAfEffectHandle *h = controlHandle_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800334 if (h != NULL) {
335 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
336 }
337 }
338
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530339 // Prevent calls to process() and other functions on effect interface from now on.
340 // The effect engine will be released by the destructor when the last strong reference on
341 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800342 if (mHandles.size() == 0 && !mPinned) {
343 mState = DESTROYED;
344 }
345
346 return mHandles.size();
347}
348
349// must be called with EffectModule::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -0700350IAfEffectHandle *EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800351{
352 // the first valid handle in the list has control over the module
353 for (size_t i = 0; i < mHandles.size(); i++) {
Andy Hungbd72c542023-06-20 18:56:17 -0700354 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800355 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800356 return h;
357 }
358 }
359
360 return NULL;
361}
362
Eric Laurentf10c7092016-12-06 17:09:56 -0800363// unsafe method called when the effect parent thread has been destroyed
Andy Hungbd72c542023-06-20 18:56:17 -0700364ssize_t EffectBase::disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800365{
Andy Hungfda44002021-06-03 17:23:16 -0700366 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800367 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700368 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800369 return mHandles.size();
370 }
371
Eric Laurentf10c7092016-12-06 17:09:56 -0800372 Mutex::Autolock _l(mLock);
373 ssize_t numHandles = removeHandle_l(handle);
374 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800375 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700376 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800377 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800378 }
379 return numHandles;
380}
381
Andy Hungbd72c542023-06-20 18:56:17 -0700382bool EffectBase::purgeHandles()
Eric Laurent41709552019-12-16 19:34:05 -0800383{
384 bool enabled = false;
385 Mutex::Autolock _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -0700386 IAfEffectHandle *handle = controlHandle_l();
Eric Laurent41709552019-12-16 19:34:05 -0800387 if (handle != NULL) {
388 enabled = handle->enabled();
389 }
390 mHandles.clear();
391 return enabled;
392}
393
Andy Hungbd72c542023-06-20 18:56:17 -0700394void EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700395 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800396}
397
398static String8 effectFlagsToString(uint32_t flags) {
399 String8 s;
400
401 s.append("conn. mode: ");
402 switch (flags & EFFECT_FLAG_TYPE_MASK) {
403 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
404 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
405 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
406 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
407 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
408 default: s.append("unknown/reserved"); break;
409 }
410 s.append(", ");
411
412 s.append("insert pref: ");
413 switch (flags & EFFECT_FLAG_INSERT_MASK) {
414 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
415 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
416 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
417 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
418 default: s.append("unknown/reserved"); break;
419 }
420 s.append(", ");
421
422 s.append("volume mgmt: ");
423 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
424 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
425 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
426 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
427 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
428 default: s.append("unknown/reserved"); break;
429 }
430 s.append(", ");
431
432 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
433 if (devind) {
434 s.append("device indication: ");
435 switch (devind) {
436 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
437 default: s.append("unknown/reserved"); break;
438 }
439 s.append(", ");
440 }
441
442 s.append("input mode: ");
443 switch (flags & EFFECT_FLAG_INPUT_MASK) {
444 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
445 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
446 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
447 default: s.append("not set"); break;
448 }
449 s.append(", ");
450
451 s.append("output mode: ");
452 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
453 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
454 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
455 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
456 default: s.append("not set"); break;
457 }
458 s.append(", ");
459
460 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
461 if (accel) {
462 s.append("hardware acceleration: ");
463 switch (accel) {
464 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
465 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
466 default: s.append("unknown/reserved"); break;
467 }
468 s.append(", ");
469 }
470
471 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
472 if (modeind) {
473 s.append("mode indication: ");
474 switch (modeind) {
475 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
476 default: s.append("unknown/reserved"); break;
477 }
478 s.append(", ");
479 }
480
481 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
482 if (srcind) {
483 s.append("source indication: ");
484 switch (srcind) {
485 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
486 default: s.append("unknown/reserved"); break;
487 }
488 s.append(", ");
489 }
490
491 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
492 s.append("offloadable, ");
493 }
494
495 int len = s.length();
496 if (s.length() > 2) {
497 (void) s.lockBuffer(len);
498 s.unlockBuffer(len - 2);
499 }
500 return s;
501}
502
Andy Hungbd72c542023-06-20 18:56:17 -0700503void EffectBase::dump(int fd, const Vector<String16>& args __unused) const
Andy Hung71ba4b32022-10-06 12:09:49 -0700504NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800505{
506 String8 result;
507
508 result.appendFormat("\tEffect ID %d:\n", mId);
509
510 bool locked = AudioFlinger::dumpTryLock(mLock);
511 // failed to lock - AudioFlinger is probably deadlocked
512 if (!locked) {
513 result.append("\t\tCould not lock Fx mutex:\n");
514 }
515
516 result.append("\t\tSession State Registered Enabled Suspended:\n");
517 result.appendFormat("\t\t%05d %03d %s %s %s\n",
518 mSessionId, mState, mPolicyRegistered ? "y" : "n",
519 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
520
521 result.append("\t\tDescriptor:\n");
522 char uuidStr[64];
523 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
524 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
525 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
526 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
527 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
528 mDescriptor.apiVersion,
529 mDescriptor.flags,
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000530 effectFlagsToString(mDescriptor.flags).c_str());
Eric Laurent41709552019-12-16 19:34:05 -0800531 result.appendFormat("\t\t- name: %s\n",
532 mDescriptor.name);
533
534 result.appendFormat("\t\t- implementor: %s\n",
535 mDescriptor.implementor);
536
537 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
538 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
539 char buffer[256];
540 for (size_t i = 0; i < mHandles.size(); ++i) {
Andy Hungbd72c542023-06-20 18:56:17 -0700541 IAfEffectHandle *handle = mHandles[i];
Eric Laurent41709552019-12-16 19:34:05 -0800542 if (handle != NULL && !handle->disconnected()) {
543 handle->dumpToBuffer(buffer, sizeof(buffer));
544 result.append(buffer);
545 }
546 }
547 if (locked) {
548 mLock.unlock();
549 }
550
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000551 write(fd, result.c_str(), result.length());
Eric Laurent41709552019-12-16 19:34:05 -0800552}
553
554// ----------------------------------------------------------------------------
555// EffectModule implementation
556// ----------------------------------------------------------------------------
557
558#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -0700559#define LOG_TAG "EffectModule"
Eric Laurent41709552019-12-16 19:34:05 -0800560
Andy Hungbd72c542023-06-20 18:56:17 -0700561EffectModule::EffectModule(const sp<EffectCallbackInterface>& callback,
Eric Laurent41709552019-12-16 19:34:05 -0800562 effect_descriptor_t *desc,
563 int id,
564 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800565 bool pinned,
566 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800567 : EffectBase(callback, desc, id, sessionId, pinned),
568 // clear mConfig to ensure consistent initial value of buffer framecount
569 // in case buffers are associated by setInBuffer() or setOutBuffer()
570 // prior to configure().
571 mConfig{{}, {}},
572 mStatus(NO_INIT),
573 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
574 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800575 mOffloaded(false),
Mikhail Naganov59984db2022-04-19 21:21:23 +0000576 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800577 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800578{
579 ALOGV("Constructor %p pinned %d", this, pinned);
580 int lStatus;
581
582 // create effect engine from effect factory
583 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800584 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800585 if (mStatus != NO_ERROR) {
586 return;
587 }
588 lStatus = init();
589 if (lStatus < 0) {
590 mStatus = lStatus;
591 goto Error;
592 }
593
594 setOffloaded(callback->isOffload(), callback->io());
595 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
596
597 return;
598Error:
599 mEffectInterface.clear();
600 ALOGV("Constructor Error %d", mStatus);
601}
602
Andy Hungbd72c542023-06-20 18:56:17 -0700603EffectModule::~EffectModule()
Eric Laurent41709552019-12-16 19:34:05 -0800604{
605 ALOGV("Destructor %p", this);
606 if (mEffectInterface != 0) {
607 char uuidStr[64];
608 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
609 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
610 this, uuidStr);
611 release_l();
612 }
613
614}
615
Andy Hungbd72c542023-06-20 18:56:17 -0700616bool EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 Mutex::Autolock _l(mLock);
618
Eric Laurentfa1e1232016-08-02 19:01:49 -0700619 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800620 switch (mState) {
621 case RESTART:
622 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700623 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800624
625 case STARTING:
626 // clear auxiliary effect input buffer for next accumulation
627 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
628 memset(mConfig.inputCfg.buffer.raw,
629 0,
630 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
631 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 if (start_l() == NO_ERROR) {
633 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700634 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700635 } else {
636 mState = IDLE;
637 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800638 break;
639 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900640 // volume control for offload and direct threads must take effect immediately.
641 if (stop_l() == NO_ERROR
642 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700643 mDisableWaitCnt = mMaxDisableWaitCnt;
644 } else {
645 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
646 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 mState = STOPPED;
648 break;
649 case STOPPED:
650 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
651 // turn off sequence.
652 if (--mDisableWaitCnt == 0) {
653 reset_l();
654 mState = IDLE;
655 }
656 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200657 case ACTIVE:
658 for (size_t i = 0; i < mHandles.size(); i++) {
659 if (!mHandles[i]->disconnected()) {
660 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
661 }
662 }
663 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 default: //IDLE , ACTIVE, DESTROYED
665 break;
666 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700667
668 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800669}
670
Andy Hungbd72c542023-06-20 18:56:17 -0700671void EffectModule::process()
Eric Laurentca7cc822012-11-19 14:55:58 -0800672{
673 Mutex::Autolock _l(mLock);
674
Mikhail Naganov022b9952017-01-04 16:36:51 -0800675 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 return;
677 }
678
rago94a1ee82017-07-21 15:11:02 -0700679 const uint32_t inChannelCount =
680 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
681 const uint32_t outChannelCount =
682 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
683 const bool auxType =
684 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
685
Andy Hungfa69ca32017-11-30 10:07:53 -0800686 // safeInputOutputSampleCount is 0 if the channel count between input and output
687 // buffers do not match. This prevents automatic accumulation or copying between the
688 // input and output effect buffers without an intermediary effect process.
689 // TODO: consider implementing channel conversion.
690 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700691 mInChannelCountRequested != mOutChannelCountRequested ? 0
692 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800693 mConfig.inputCfg.buffer.frameCount,
694 mConfig.outputCfg.buffer.frameCount);
695 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800696 accumulate_float(
697 mConfig.outputCfg.buffer.f32,
698 mConfig.inputCfg.buffer.f32,
699 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800700 };
701 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800702 memcpy(
703 mConfig.outputCfg.buffer.f32,
704 mConfig.inputCfg.buffer.f32,
705 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800706 };
707
Eric Laurentca7cc822012-11-19 14:55:58 -0800708 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700709 int ret;
710 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700711 if (auxType) {
712 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800713 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700714
Andy Hung26836922023-05-22 17:31:57 -0700715 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800716 memcpy_to_i16_from_float(
717 mConfig.inputCfg.buffer.s16,
718 mConfig.inputCfg.buffer.f32,
719 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700720 }
rago94a1ee82017-07-21 15:11:02 -0700721 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800722 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
723 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
724
725 if (!auxType && mInChannelCountRequested != inChannelCount) {
726 adjust_channels(
727 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
728 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
729 sizeof(float),
730 sizeof(float)
731 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
732 inBuffer = mInConversionBuffer;
733 }
734 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
735 && mOutChannelCountRequested != outChannelCount) {
736 adjust_selected_channels(
737 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
738 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
739 sizeof(float),
740 sizeof(float)
741 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
742 outBuffer = mOutConversionBuffer;
743 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
745 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800746 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
748 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700749 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 memcpy_to_i16_from_float(
751 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800752 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800753 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800754 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700755 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800756 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800757 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
759 goto data_bypass;
760 }
761 memcpy_to_i16_from_float(
762 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800763 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800764 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800765 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700766 }
767 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800768 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800769 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800770 sp<EffectBufferHalInterface> target =
771 mOutChannelCountRequested != outChannelCount
772 ? mOutConversionBuffer : mOutBuffer;
773
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800775 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 mOutConversionBuffer->audioBuffer()->s16,
777 outChannelCount * mConfig.outputCfg.buffer.frameCount);
778 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800779 if (mOutChannelCountRequested != outChannelCount) {
780 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
781 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
782 sizeof(float),
783 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
784 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700785 } else {
rago94a1ee82017-07-21 15:11:02 -0700786 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700787 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700789 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700793 }
794 }
795 ret = -ENODATA;
796 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800797
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 // force transition to IDLE state when engine is ready
799 if (mState == STOPPED && ret == -ENODATA) {
800 mDisableWaitCnt = 1;
801 }
802
803 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700804 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800805 const size_t size =
806 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700807 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800808 }
809 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700810 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800811 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
812 // If an insert effect is idle and input buffer is different from output buffer,
813 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700814 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700815 // similar handling with data_bypass above.
816 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
817 accumulateInputToOutput();
818 } else { // EFFECT_BUFFER_ACCESS_WRITE
819 copyInputToOutput();
820 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800821 }
822 }
823}
824
Andy Hungbd72c542023-06-20 18:56:17 -0700825void EffectModule::reset_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800826{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700827 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800828 return;
829 }
Shunkai Yaoc92f3df2023-09-20 23:10:22 +0000830
831 int reply = 0;
832 uint32_t replySize = sizeof(reply);
833 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, &replySize, &reply);
Eric Laurentca7cc822012-11-19 14:55:58 -0800834}
835
Andy Hungbd72c542023-06-20 18:56:17 -0700836status_t EffectModule::configure()
Eric Laurentca7cc822012-11-19 14:55:58 -0800837{
rago94a1ee82017-07-21 15:11:02 -0700838 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700839 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 uint32_t size;
841 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700842 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700843
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700844 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700845 status = NO_INIT;
846 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848
Eric Laurentca7cc822012-11-19 14:55:58 -0800849 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 // TODO: handle configuration of input (record) SW effects above the HAL,
851 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
852 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700853 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200854 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800855 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200856 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800857
858 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800859 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
860 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
861 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
862 mConfig.inputCfg.channels);
863 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800864 }
jiabineb3bda02020-06-30 14:07:03 -0700865 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700866 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700867 mConfig.inputCfg.channels |= hapticChannelMask;
868 mConfig.outputCfg.channels |= hapticChannelMask;
869 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800870 mInChannelCountRequested =
871 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
872 mOutChannelCountRequested =
873 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700874
Andy Hung319587b2023-05-23 14:01:03 -0700875 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
876 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900877
878 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700879 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900880 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
881 ALOGV("Overriding effect input as 48kHz");
882 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700883 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900884 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800885 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
886 mConfig.inputCfg.bufferProvider.cookie = NULL;
887 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
888 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
889 mConfig.outputCfg.bufferProvider.cookie = NULL;
890 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
891 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
892 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
893 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800894 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 // always overwrites output buffer: input buffer == output buffer
896 // - in other sessions:
897 // last effect in the chain accumulates in output buffer: input buffer != output buffer
898 // other effect: overwrites output buffer: input buffer == output buffer
899 // Auxiliary effect:
900 // accumulates in output buffer: input buffer != output buffer
901 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700902 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800903 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
904 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700905 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800906 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov59984db2022-04-19 21:21:23 +0000907 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800908
Eric Laurent6b446ce2019-12-13 10:56:31 -0800909 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700910 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800911 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800912
913 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700914 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700915 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800916 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700917 &mConfig,
918 &size,
919 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700920 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 status = cmdStatus;
922 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800923
Andy Hung9aad48c2017-11-29 10:29:19 -0800924 if (status != NO_ERROR &&
Andy Hungba8e52b2023-05-11 14:33:03 -0700925 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
Mikhail Naganov59984db2022-04-19 21:21:23 +0000926 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800927 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
928 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
929 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700930 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
931 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800932 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
933 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
934 }
935 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
936 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
937 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
938 }
939 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700940 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800941 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700942 &mConfig,
943 &size,
944 &cmdStatus);
945 if (status == NO_ERROR) {
946 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800947 }
948 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800949
Andy Hung9aad48c2017-11-29 10:29:19 -0800950 if (status == NO_ERROR) {
951 mSupportsFloat = true;
952 }
953
Andy Hungba8e52b2023-05-11 14:33:03 -0700954 // only HIDL effects support integer conversion.
955 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800956 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
957 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
958 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
959 size = sizeof(int);
960 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
961 sizeof(mConfig),
962 &mConfig,
963 &size,
964 &cmdStatus);
965 if (status == NO_ERROR) {
966 status = cmdStatus;
967 }
968 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700969 mSupportsFloat = false;
970 ALOGVV("config worked with 16 bit");
971 } else {
972 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800973 }
rago94a1ee82017-07-21 15:11:02 -0700974 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800975
rago94a1ee82017-07-21 15:11:02 -0700976 if (status == NO_ERROR) {
977 // Establish Buffer strategy
978 setInBuffer(mInBuffer);
979 setOutBuffer(mOutBuffer);
980
981 // Update visualizer latency
982 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
983 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
984 effect_param_t *p = (effect_param_t *)buf32;
985
986 p->psize = sizeof(uint32_t);
987 p->vsize = sizeof(uint32_t);
988 size = sizeof(int);
989 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
990
Andy Hungfda44002021-06-03 17:23:16 -0700991 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700992
993 *((int32_t *)p->data + 1)= latency;
994 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
995 sizeof(effect_param_t) + 8,
996 &buf32,
997 &size,
998 &cmdStatus);
999 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001000 }
1001
Andy Hung05083ac2017-12-14 15:00:28 -08001002 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1003 mMaxDisableWaitCnt = (uint32_t)std::max(
1004 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1005 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1006 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001007
Eric Laurentd0ebb532013-04-02 16:41:41 -07001008exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001009 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001010 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001011 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001012 return status;
1013}
1014
Andy Hungbd72c542023-06-20 18:56:17 -07001015status_t EffectModule::init()
Eric Laurentca7cc822012-11-19 14:55:58 -08001016{
1017 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001018 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001019 return NO_INIT;
1020 }
1021 status_t cmdStatus;
1022 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001023 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1024 0,
1025 NULL,
1026 &size,
1027 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 if (status == 0) {
1029 status = cmdStatus;
1030 }
1031 return status;
1032}
1033
Andy Hungbd72c542023-06-20 18:56:17 -07001034void EffectModule::addEffectToHal_l()
Eric Laurent1b928682014-10-02 19:41:47 -07001035{
1036 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1037 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001038 if (mCurrentHalStream == getCallback()->io()) {
David Li6c8ac4b2021-06-22 22:17:52 +08001039 return;
1040 }
1041
Andy Hungfda44002021-06-03 17:23:16 -07001042 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001043 mCurrentHalStream = getCallback()->io();
Eric Laurent1b928682014-10-02 19:41:47 -07001044 }
1045}
1046
Eric Laurentfa1e1232016-08-02 19:01:49 -07001047// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07001048status_t EffectModule::start()
Eric Laurentca7cc822012-11-19 14:55:58 -08001049{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001050 status_t status;
1051 {
1052 Mutex::Autolock _l(mLock);
1053 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001054 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001055 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001056 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001057 }
1058 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001059}
1060
Andy Hungbd72c542023-06-20 18:56:17 -07001061status_t EffectModule::start_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08001062{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001063 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return NO_INIT;
1065 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001066 if (mStatus != NO_ERROR) {
1067 return mStatus;
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 status_t cmdStatus;
1070 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001071 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1072 0,
1073 NULL,
1074 &size,
1075 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 if (status == 0) {
1077 status = cmdStatus;
1078 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001079 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001080 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 }
1082 return status;
1083}
1084
Andy Hungbd72c542023-06-20 18:56:17 -07001085status_t EffectModule::stop()
Eric Laurentca7cc822012-11-19 14:55:58 -08001086{
1087 Mutex::Autolock _l(mLock);
1088 return stop_l();
1089}
1090
Andy Hungbd72c542023-06-20 18:56:17 -07001091status_t EffectModule::stop_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08001092{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001093 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001094 return NO_INIT;
1095 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001096 if (mStatus != NO_ERROR) {
1097 return mStatus;
1098 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001099 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001101
1102 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001103 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1104 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1105 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001106 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001107 mSetVolumeReentrantTid = INVALID_PID;
1108 }
1109
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001110 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1111 0,
1112 NULL,
1113 &size,
1114 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001115 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 status = cmdStatus;
1117 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001118 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001119 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001120 }
1121 return status;
1122}
1123
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001124// must be called with EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07001125void EffectModule::release_l()
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001126{
1127 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001128 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001129 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001130 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001131 mEffectInterface.clear();
1132 }
1133}
1134
Andy Hungbd72c542023-06-20 18:56:17 -07001135status_t EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001136{
1137 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1138 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001139 if (mCurrentHalStream != getCallback()->io()) {
1140 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
David Li6c8ac4b2021-06-22 22:17:52 +08001141 }
Andy Hungfda44002021-06-03 17:23:16 -07001142 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001143 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001145 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001146}
1147
Andy Hunge4a1d912016-08-17 14:11:13 -07001148// round up delta valid if value and divisor are positive.
1149template <typename T>
1150static T roundUpDelta(const T &value, const T &divisor) {
1151 T remainder = value % divisor;
1152 return remainder == 0 ? 0 : divisor - remainder;
1153}
1154
Andy Hungbd72c542023-06-20 18:56:17 -07001155status_t EffectModule::command(int32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001156 const std::vector<uint8_t>& cmdData,
1157 int32_t maxReplySize,
1158 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001159{
1160 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001161 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001162
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001163 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001164 return NO_INIT;
1165 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001166 if (mStatus != NO_ERROR) {
1167 return mStatus;
1168 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001169 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1170 return -EINVAL;
1171 }
1172 size_t cmdSize = cmdData.size();
1173 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1174 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1175 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001176 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001177 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001178 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001179 android_errorWriteLog(0x534e4554, "33003822");
1180 return -EINVAL;
1181 }
1182 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001183 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001184 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001185 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001186 return -EINVAL;
1187 }
ragoe2759072016-11-22 18:02:48 -08001188 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001189 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001190 || param->psize > maxReplySize - sizeof(effect_param_t)
1191 || param->vsize > maxReplySize - sizeof(effect_param_t)
1192 - param->psize
1193 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1194 maxReplySize
1195 - sizeof(effect_param_t)
1196 - param->psize
1197 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001198 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1199 android_errorWriteLog(0x534e4554, "32705438");
1200 return -EINVAL;
1201 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001202 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001203 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1204 && // DEFERRED not generally used
1205 (param == nullptr
1206 || param->psize > cmdSize - sizeof(effect_param_t)
1207 || param->vsize > cmdSize - sizeof(effect_param_t)
1208 - param->psize
1209 || roundUpDelta(param->psize,
1210 (uint32_t) sizeof(int)) >
1211 cmdSize
1212 - sizeof(effect_param_t)
1213 - param->psize
1214 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001215 android_errorWriteLog(0x534e4554, "30204301");
1216 return -EINVAL;
1217 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001218 uint32_t replySize = maxReplySize;
1219 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001220 status_t status = mEffectInterface->command(cmdCode,
1221 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001222 const_cast<uint8_t*>(cmdData.data()),
1223 &replySize,
1224 reply->data());
1225 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001226 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001227 for (size_t i = 1; i < mHandles.size(); i++) {
Andy Hungbd72c542023-06-20 18:56:17 -07001228 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001229 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001230 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001231 }
1232 }
1233 }
1234 return status;
1235}
1236
Andy Hungbd72c542023-06-20 18:56:17 -07001237bool EffectModule::isProcessEnabled() const
Eric Laurentca7cc822012-11-19 14:55:58 -08001238{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001239 if (mStatus != NO_ERROR) {
1240 return false;
1241 }
1242
Eric Laurentca7cc822012-11-19 14:55:58 -08001243 switch (mState) {
1244 case RESTART:
1245 case ACTIVE:
1246 case STOPPING:
1247 case STOPPED:
1248 return true;
1249 case IDLE:
1250 case STARTING:
1251 case DESTROYED:
1252 default:
1253 return false;
1254 }
1255}
1256
Andy Hungbd72c542023-06-20 18:56:17 -07001257bool EffectModule::isOffloadedOrDirect() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001258{
Andy Hungfda44002021-06-03 17:23:16 -07001259 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001260}
1261
Andy Hungbd72c542023-06-20 18:56:17 -07001262bool EffectModule::isVolumeControlEnabled() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001263{
1264 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1265}
1266
Andy Hungbd72c542023-06-20 18:56:17 -07001267void EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001268 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001269
1270 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001271 if (buffer != 0) {
1272 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1273 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1274 } else {
1275 mConfig.inputCfg.buffer.raw = NULL;
1276 }
1277 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001278 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001279
Andy Hungbded9c82017-11-30 18:47:35 -08001280 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001281 // Theoretically insert effects can also do in-place conversions (destroying
1282 // the original buffer) when the output buffer is identical to the input buffer,
1283 // but we don't optimize for it here.
1284 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001285 const uint32_t inChannelCount =
1286 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1287 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001288 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001289 // we need to translate - create hidl shared buffer and intercept
1290 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001291 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1292 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1293 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001294
1295 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1296 __func__, inChannels, inFrameCount, size);
1297
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001298 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001299 || size > mInConversionBuffer->getSize())) {
1300 mInConversionBuffer.clear();
1301 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001302 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001303 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001304 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001305 mInConversionBuffer->setFrameCount(inFrameCount);
1306 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001307 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001308 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001309 }
1310 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001311}
1312
Andy Hungbd72c542023-06-20 18:56:17 -07001313void EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001314 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001315
1316 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001317 if (buffer != 0) {
1318 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1319 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1320 } else {
1321 mConfig.outputCfg.buffer.raw = NULL;
1322 }
1323 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001324 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001325
Andy Hungbded9c82017-11-30 18:47:35 -08001326 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001327 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001328 const uint32_t outChannelCount =
1329 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1330 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001331 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001332 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001333 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1334 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1335 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001336
1337 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1338 __func__, outChannels, outFrameCount, size);
1339
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001340 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001341 || size > mOutConversionBuffer->getSize())) {
1342 mOutConversionBuffer.clear();
1343 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001344 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001345 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001346 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001347 mOutConversionBuffer->setFrameCount(outFrameCount);
1348 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001349 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001350 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001351 }
1352 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001353}
1354
Andy Hungbd72c542023-06-20 18:56:17 -07001355status_t EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
Eric Laurentca7cc822012-11-19 14:55:58 -08001356{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001357 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001358 if (mStatus != NO_ERROR) {
1359 return mStatus;
1360 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001361 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001362 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1363 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1364 if (isProcessEnabled() &&
1365 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001366 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1367 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001368 status = setVolumeInternal(left, right, controller);
1369 }
1370 return status;
1371}
1372
Andy Hungbd72c542023-06-20 18:56:17 -07001373status_t EffectModule::setVolumeInternal(
jiabin229f94d2022-08-23 16:37:30 -07001374 uint32_t *left, uint32_t *right, bool controller) {
1375 uint32_t volume[2] = {*left, *right};
1376 uint32_t *pVolume = controller ? volume : nullptr;
1377 uint32_t size = sizeof(volume);
1378 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1379 size,
1380 volume,
1381 &size,
1382 pVolume);
1383 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1384 *left = volume[0];
1385 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001386 }
1387 return status;
1388}
1389
Andy Hungbd72c542023-06-20 18:56:17 -07001390void EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001391{
Zhou Songd505c642020-02-20 16:35:37 +08001392 // for offload or direct thread, if the effect chain has non-offloadable
1393 // effect and any effect module within the chain has volume control, then
1394 // volume control is delegated to effect, otherwise, set volume to hal.
1395 if (mEffectCallback->isOffloadOrDirect() &&
1396 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001397 float vol_l = (float)left / (1 << 24);
1398 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001399 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001400 }
1401}
1402
Andy Hungbd72c542023-06-20 18:56:17 -07001403status_t EffectModule::sendSetAudioDevicesCommand(
jiabin8f278ee2019-11-11 12:16:27 -08001404 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001405{
jiabin8f278ee2019-11-11 12:16:27 -08001406 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1407 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 return NO_ERROR;
1409 }
1410
1411 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001412 if (mStatus != NO_ERROR) {
1413 return mStatus;
1414 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001416 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 status_t cmdStatus;
1418 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001419 // FIXME: use audio device types and addresses when the hal interface is ready.
1420 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001421 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001422 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001423 &size,
1424 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 }
1426 return status;
1427}
1428
Andy Hungbd72c542023-06-20 18:56:17 -07001429status_t EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
jiabin8f278ee2019-11-11 12:16:27 -08001430{
1431 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1432}
1433
Andy Hungbd72c542023-06-20 18:56:17 -07001434status_t EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08001435{
1436 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1437}
1438
Andy Hungbd72c542023-06-20 18:56:17 -07001439status_t EffectModule::setMode(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001440{
1441 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001442 if (mStatus != NO_ERROR) {
1443 return mStatus;
1444 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 status_t status = NO_ERROR;
1446 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1447 status_t cmdStatus;
1448 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001449 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1450 sizeof(audio_mode_t),
1451 &mode,
1452 &size,
1453 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001454 if (status == NO_ERROR) {
1455 status = cmdStatus;
1456 }
1457 }
1458 return status;
1459}
1460
Andy Hungbd72c542023-06-20 18:56:17 -07001461status_t EffectModule::setAudioSource(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08001462{
1463 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001464 if (mStatus != NO_ERROR) {
1465 return mStatus;
1466 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 status_t status = NO_ERROR;
1468 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1469 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001470 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1471 sizeof(audio_source_t),
1472 &source,
1473 &size,
1474 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001475 }
1476 return status;
1477}
1478
Andy Hungbd72c542023-06-20 18:56:17 -07001479status_t EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
Eric Laurent5baf2af2013-09-12 17:37:00 -07001480{
1481 Mutex::Autolock _l(mLock);
1482 if (mStatus != NO_ERROR) {
1483 return mStatus;
1484 }
1485 status_t status = NO_ERROR;
1486 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1487 status_t cmdStatus;
1488 uint32_t size = sizeof(status_t);
1489 effect_offload_param_t cmd;
1490
1491 cmd.isOffload = offloaded;
1492 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001493 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1494 sizeof(effect_offload_param_t),
1495 &cmd,
1496 &size,
1497 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001498 if (status == NO_ERROR) {
1499 status = cmdStatus;
1500 }
1501 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1502 } else {
1503 if (offloaded) {
1504 status = INVALID_OPERATION;
1505 }
1506 mOffloaded = false;
1507 }
1508 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1509 return status;
1510}
1511
Andy Hungbd72c542023-06-20 18:56:17 -07001512bool EffectModule::isOffloaded() const
Eric Laurent5baf2af2013-09-12 17:37:00 -07001513{
1514 Mutex::Autolock _l(mLock);
1515 return mOffloaded;
1516}
1517
jiabineb3bda02020-06-30 14:07:03 -07001518/*static*/
Andy Hungbd72c542023-06-20 18:56:17 -07001519bool IAfEffectModule::isHapticGenerator(const effect_uuid_t *type) {
jiabineb3bda02020-06-30 14:07:03 -07001520 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1521}
1522
Andy Hungbd72c542023-06-20 18:56:17 -07001523bool EffectModule::isHapticGenerator() const {
1524 return IAfEffectModule::isHapticGenerator(&mDescriptor.type);
jiabineb3bda02020-06-30 14:07:03 -07001525}
1526
Andy Hungbd72c542023-06-20 18:56:17 -07001527status_t EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001528{
1529 if (mStatus != NO_ERROR) {
1530 return mStatus;
1531 }
1532 if (!isHapticGenerator()) {
1533 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1534 return INVALID_OPERATION;
1535 }
1536
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001537 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1538 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001539 param->psize = sizeof(int32_t);
1540 param->vsize = sizeof(int32_t) * 2;
1541 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1542 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001543 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001544 std::vector<uint8_t> response;
1545 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001546 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001547 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1548 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001549 }
1550 return status;
1551}
1552
Andy Hungbd72c542023-06-20 18:56:17 -07001553status_t EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001554{
1555 if (mStatus != NO_ERROR) {
1556 return mStatus;
1557 }
1558 if (!isHapticGenerator()) {
1559 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1560 return INVALID_OPERATION;
1561 }
1562
Lais Andradebc3f37a2021-07-02 00:13:19 +01001563 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001564 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001565 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001566 effect_param_t *param = (effect_param_t*) request.data();
1567 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001568 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001569 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1570 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001571 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1572 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1573 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001574 std::vector<uint8_t> response;
1575 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1576 if (status == NO_ERROR) {
1577 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1578 status = *reinterpret_cast<const status_t*>(response.data());
1579 }
1580 return status;
1581}
1582
Andy Hungbd72c542023-06-20 18:56:17 -07001583status_t EffectModule::getConfigs(
Mikhail Naganov59984db2022-04-19 21:21:23 +00001584 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1585 Mutex::Autolock _l(mLock);
1586 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1587 return NO_INIT;
1588 }
1589 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1590 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1591 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1592 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1593 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1594 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1595 *isOutput = mIsOutput;
1596 return NO_ERROR;
1597}
1598
Andy Hungbded9c82017-11-30 18:47:35 -08001599static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1600 std::stringstream ss;
1601
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001602 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001603 return "nullptr"; // make different than below
1604 } else if (buffer->externalData() != nullptr) {
1605 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1606 << " -> "
1607 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1608 } else {
1609 ss << buffer->audioBuffer()->raw;
1610 }
1611 return ss.str();
1612}
Marco Nelissenb2208842014-02-07 14:00:50 -08001613
Andy Hungbd72c542023-06-20 18:56:17 -07001614void EffectModule::dump(int fd, const Vector<String16>& args) const
Andy Hung71ba4b32022-10-06 12:09:49 -07001615NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001616{
Eric Laurent41709552019-12-16 19:34:05 -08001617 EffectBase::dump(fd, args);
1618
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001621
Eric Laurent41709552019-12-16 19:34:05 -08001622 result.append("\t\tStatus Engine:\n");
1623 result.appendFormat("\t\t%03d %p\n",
1624 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001625
1626 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001627
1628 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001629 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1630 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1631 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 mConfig.inputCfg.buffer.frameCount,
1633 mConfig.inputCfg.samplingRate,
1634 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001635 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001636 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001637
1638 result.append("\t\t- Output configuration:\n");
1639 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001640 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001641 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 mConfig.outputCfg.buffer.frameCount,
1643 mConfig.outputCfg.samplingRate,
1644 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001645 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001646 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001647
Andy Hungbded9c82017-11-30 18:47:35 -08001648 result.appendFormat("\t\t- HAL buffers:\n"
1649 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1650 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1651 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1652 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1653 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001654
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00001655 write(fd, result.c_str(), result.length());
Eric Laurentca7cc822012-11-19 14:55:58 -08001656
Mikhail Naganov4d547672019-02-22 14:19:19 -08001657 if (mEffectInterface != 0) {
1658 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1659 (void)mEffectInterface->dump(fd);
1660 }
1661
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 if (locked) {
1663 mLock.unlock();
1664 }
1665}
1666
1667// ----------------------------------------------------------------------------
1668// EffectHandle implementation
1669// ----------------------------------------------------------------------------
1670
1671#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07001672#define LOG_TAG "EffectHandle"
Eric Laurentca7cc822012-11-19 14:55:58 -08001673
Andy Hungbd72c542023-06-20 18:56:17 -07001674/* static */
1675sp<IAfEffectHandle> IAfEffectHandle::create(
1676 const sp<IAfEffectBase>& effect,
1677 const sp<RefBase /*AudioFlinger::Client */>& client, // TODO(b/288339104) update type
1678 const sp<media::IEffectClient>& effectClient,
1679 int32_t priority, bool notifyFramesProcessed)
1680{
1681 return sp<EffectHandle>::make(
1682 effect, sp<AudioFlinger::Client>::cast(client),
1683 effectClient, priority, notifyFramesProcessed);
1684}
1685
1686EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001687 const sp<AudioFlinger::Client>& client,
1688 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001689 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001690 : BnEffect(),
1691 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001692 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1693 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001694{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001695 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001696 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001697
1698 if (client == 0) {
1699 return;
1700 }
1701 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001702 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1703 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1704 std::string("Effect ID: ")
1705 .append(std::to_string(effect->id()))
1706 .append(" Session ID: ")
1707 .append(std::to_string(static_cast<int>(effect->sessionId())))
1708 .append(" \n")
1709 });
Glenn Kastene75da402013-11-20 13:54:52 -08001710 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001711 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001712 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001714 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001715 return;
1716 }
Glenn Kastene75da402013-11-20 13:54:52 -08001717 new(mCblk) effect_param_cblk_t();
1718 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001719}
1720
Andy Hungbd72c542023-06-20 18:56:17 -07001721EffectHandle::~EffectHandle()
Eric Laurentca7cc822012-11-19 14:55:58 -08001722{
1723 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 disconnect(false);
1725}
1726
Andy Hungc747c532022-03-07 21:41:14 -08001727// Creates an association between Binder code to name for IEffect.
1728#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1729BINDER_METHOD_ENTRY(enable) \
1730BINDER_METHOD_ENTRY(disable) \
1731BINDER_METHOD_ENTRY(command) \
1732BINDER_METHOD_ENTRY(disconnect) \
1733BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001734BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001735
1736// singleton for Binder Method Statistics for IEffect
1737mediautils::MethodStatistics<int>& getIEffectStatistics() {
1738 using Code = int;
1739
1740#pragma push_macro("BINDER_METHOD_ENTRY")
1741#undef BINDER_METHOD_ENTRY
1742#define BINDER_METHOD_ENTRY(ENTRY) \
1743 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1744
1745 static mediautils::MethodStatistics<Code> methodStatistics{
1746 IEFFECT_BINDER_METHOD_MACRO_LIST
1747 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1748 };
1749#pragma pop_macro("BINDER_METHOD_ENTRY")
1750
1751 return methodStatistics;
1752}
1753
Andy Hungbd72c542023-06-20 18:56:17 -07001754status_t EffectHandle::onTransact(
Andy Hungc747c532022-03-07 21:41:14 -08001755 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001756 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1757 mediautils::TimeCheck check(
1758 std::string("IEffect::").append(methodName),
1759 [code](bool timeout, float elapsedMs) {
1760 if (timeout) {
1761 ; // we don't timeout right now on the effect interface.
1762 } else {
1763 getIEffectStatistics().event(code, elapsedMs);
1764 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001765 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001766 return BnEffect::onTransact(code, data, reply, flags);
1767}
1768
Andy Hungbd72c542023-06-20 18:56:17 -07001769status_t EffectHandle::initCheck() const
Glenn Kastene75da402013-11-20 13:54:52 -08001770{
1771 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1772}
1773
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001774#define RETURN(code) \
1775 *_aidl_return = (code); \
1776 return Status::ok();
1777
Mikhail Naganov59984db2022-04-19 21:21:23 +00001778#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1779 ({ \
1780 auto _tmp = (exp); \
1781 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1782 std::move(_tmp.value()); \
1783 })
1784
Andy Hungbd72c542023-06-20 18:56:17 -07001785Status EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001786{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001787 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001788 ALOGV("enable %p", this);
Andy Hungbd72c542023-06-20 18:56:17 -07001789 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001791 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001794 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001795 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001796
1797 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001798 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001799 }
1800
1801 mEnabled = true;
1802
Eric Laurent6c796322019-04-09 14:13:17 -07001803 status_t status = effect->updatePolicyState();
1804 if (status != NO_ERROR) {
1805 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001806 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001807 }
1808
Eric Laurent6b446ce2019-12-13 10:56:31 -08001809 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001810
1811 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001812 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001813 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001814 }
1815
Eric Laurent6b446ce2019-12-13 10:56:31 -08001816 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 mEnabled = false;
1819 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001820 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001821}
1822
Andy Hungbd72c542023-06-20 18:56:17 -07001823Status EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001824{
1825 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001826 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001827 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001828 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001829 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001830 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001832 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001833 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001834
1835 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001836 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001837 }
1838 mEnabled = false;
1839
Eric Laurent6c796322019-04-09 14:13:17 -07001840 effect->updatePolicyState();
1841
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001842 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001843 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 }
1845
Eric Laurent6b446ce2019-12-13 10:56:31 -08001846 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001847 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001848}
1849
Andy Hungbd72c542023-06-20 18:56:17 -07001850Status EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001851{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001854 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001855}
1856
Andy Hungbd72c542023-06-20 18:56:17 -07001857void EffectHandle::disconnect(bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -08001858{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001859 AutoMutex _l(mLock);
1860 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1861 if (mDisconnected) {
1862 if (unpinIfLast) {
1863 android_errorWriteLog(0x534e4554, "32707507");
1864 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 return;
1866 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001867 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001868 {
Andy Hungbd72c542023-06-20 18:56:17 -07001869 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001870 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001871 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001872 ALOGW("%s Effect handle %p disconnected after thread destruction",
1873 __func__, this);
1874 }
1875 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001876 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001877 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001878
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 if (mClient != 0) {
1880 if (mCblk != NULL) {
1881 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1882 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1883 }
1884 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001885 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung71ba4b32022-10-06 12:09:49 -07001886 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001887 mClient.clear();
1888 }
1889}
1890
Andy Hungbd72c542023-06-20 18:56:17 -07001891Status EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1893 return Status::ok();
1894}
1895
Andy Hungbd72c542023-06-20 18:56:17 -07001896Status EffectHandle::getConfig(
Mikhail Naganov59984db2022-04-19 21:21:23 +00001897 media::EffectConfig* _config, int32_t* _aidl_return) {
1898 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001899 sp<IAfEffectBase> effect = mEffect.promote();
Mikhail Naganov59984db2022-04-19 21:21:23 +00001900 if (effect == nullptr || mDisconnected) {
1901 RETURN(DEAD_OBJECT);
1902 }
Andy Hungbd72c542023-06-20 18:56:17 -07001903 sp<IAfEffectModule> effectModule = effect->asEffectModule();
Mikhail Naganov59984db2022-04-19 21:21:23 +00001904 if (effectModule == nullptr) {
1905 RETURN(INVALID_OPERATION);
1906 }
1907 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1908 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1909 bool isOutput;
1910 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1911 if (status == NO_ERROR) {
1912 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1913 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1914 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1915 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1916 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1917 _config->isOnInputStream = !isOutput;
1918 }
1919 RETURN(status);
1920}
1921
Andy Hungbd72c542023-06-20 18:56:17 -07001922Status EffectHandle::command(int32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001923 const std::vector<uint8_t>& cmdData,
1924 int32_t maxResponseSize,
1925 std::vector<uint8_t>* response,
1926 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001927{
1928 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001929 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001930
Eric Laurentc7ab3092017-06-15 18:43:46 -07001931 // reject commands reserved for internal use by audio framework if coming from outside
1932 // of audioserver
1933 switch(cmdCode) {
1934 case EFFECT_CMD_ENABLE:
1935 case EFFECT_CMD_DISABLE:
1936 case EFFECT_CMD_SET_PARAM:
1937 case EFFECT_CMD_SET_PARAM_DEFERRED:
1938 case EFFECT_CMD_SET_PARAM_COMMIT:
1939 case EFFECT_CMD_GET_PARAM:
1940 break;
1941 default:
1942 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1943 break;
1944 }
1945 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001946 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001947 }
1948
Eric Laurent1ffc5852016-12-15 14:46:09 -08001949 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001950 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001951 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001952 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001953 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001954 writeToBuffer(NO_ERROR, response);
1955 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001956 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001957 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001958 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001959 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001960 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001961 writeToBuffer(NO_ERROR, response);
1962 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001963 }
1964
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001965 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001966 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001967 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001968 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001969 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001970 // only get parameter command is permitted for applications not controlling the effect
1971 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001972 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001973 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001974
1975 // handle commands that are not forwarded transparently to effect engine
1976 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001977 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001978 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001979 }
1980
Andy Hung71ba4b32022-10-06 12:09:49 -07001981 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001982 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001983 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001984 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001985 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001986
Eric Laurentca7cc822012-11-19 14:55:58 -08001987 // No need to trylock() here as this function is executed in the binder thread serving a
1988 // particular client process: no risk to block the whole media server process or mixer
1989 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07001990 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001991 // keep local copy of index in case of client corruption b/32220769
1992 const uint32_t clientIndex = mCblk->clientIndex;
1993 const uint32_t serverIndex = mCblk->serverIndex;
1994 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1995 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001996 mCblk->serverIndex = 0;
1997 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001998 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001999 }
2000 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002001 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002002 for (uint32_t index = serverIndex; index < clientIndex;) {
2003 int *p = (int *)(mBuffer + index);
2004 const int size = *p++;
2005 if (size < 0
2006 || size > EFFECT_PARAM_BUFFER_SIZE
2007 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002008 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002009 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002010 break;
2011 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002012
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002013 std::copy(reinterpret_cast<const uint8_t*>(p),
2014 reinterpret_cast<const uint8_t*>(p) + size,
2015 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002016
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002018 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002019 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002020 sizeof(int),
2021 &replyBuffer);
2022 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002023
2024 // verify shared memory: server index shouldn't change; client index can't go back.
2025 if (serverIndex != mCblk->serverIndex
2026 || clientIndex > mCblk->clientIndex) {
2027 android_errorWriteLog(0x534e4554, "32220769");
2028 status = BAD_VALUE;
2029 break;
2030 }
2031
Eric Laurentca7cc822012-11-19 14:55:58 -08002032 // stop at first error encountered
2033 if (ret != NO_ERROR) {
2034 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002035 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002036 break;
2037 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002038 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002039 break;
2040 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002041 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002042 }
2043 mCblk->serverIndex = 0;
2044 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002045 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002046 }
2047
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002048 status_t status = effect->command(cmdCode,
2049 cmdData,
2050 maxResponseSize,
2051 response);
2052 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002053}
2054
Andy Hungbd72c542023-06-20 18:56:17 -07002055void EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002056{
2057 ALOGV("setControl %p control %d", this, hasControl);
2058
2059 mHasControl = hasControl;
2060 mEnabled = enabled;
2061
2062 if (signal && mEffectClient != 0) {
2063 mEffectClient->controlStatusChanged(hasControl);
2064 }
2065}
2066
Andy Hungbd72c542023-06-20 18:56:17 -07002067void EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002068 const std::vector<uint8_t>& cmdData,
2069 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002070{
2071 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002072 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 }
2074}
2075
2076
2077
Andy Hungbd72c542023-06-20 18:56:17 -07002078void EffectHandle::setEnabled(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002079{
2080 if (mEffectClient != 0) {
2081 mEffectClient->enableStatusChanged(enabled);
2082 }
2083}
2084
Andy Hungbd72c542023-06-20 18:56:17 -07002085void EffectHandle::framesProcessed(int32_t frames) const
Eric Laurentde8caf42021-08-11 17:19:25 +02002086{
2087 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2088 mEffectClient->framesProcessed(frames);
2089 }
2090}
2091
Andy Hungbd72c542023-06-20 18:56:17 -07002092void EffectHandle::dumpToBuffer(char* buffer, size_t size) const
Andy Hung71ba4b32022-10-06 12:09:49 -07002093NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002094{
2095 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2096
Marco Nelissenb2208842014-02-07 14:00:50 -08002097 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002098 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002099 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002100 mHasControl ? "yes" : "no",
2101 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002102 mCblk ? mCblk->clientIndex : 0,
2103 mCblk ? mCblk->serverIndex : 0
2104 );
2105
2106 if (locked) {
2107 mCblk->lock.unlock();
2108 }
2109}
2110
2111#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07002112#define LOG_TAG "EffectChain"
Eric Laurentca7cc822012-11-19 14:55:58 -08002113
Andy Hungbd72c542023-06-20 18:56:17 -07002114/* static */
2115sp<IAfEffectChain> IAfEffectChain::create(
2116 const wp<Thread /*ThreadBase*/>& wThread, // TODO(b/288339104) update type
2117 audio_session_t sessionId)
2118{
2119 // TODO(b/288339104) no weak pointer cast.
2120 return sp<EffectChain>::make(sp<AudioFlinger::ThreadBase>::cast(wThread.promote()), sessionId);
2121}
2122
2123EffectChain::EffectChain(const wp<AudioFlinger::ThreadBase>& thread,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002124 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002125 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabineb5784e2023-08-24 21:10:44 +00002126 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002127 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002128 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002129{
Andy Hungbd72c542023-06-20 18:56:17 -07002130 sp<AudioFlinger::ThreadBase> p = thread.promote();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002131 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002132 return;
2133 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002134 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002135 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2136 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002137}
2138
Andy Hungbd72c542023-06-20 18:56:17 -07002139EffectChain::~EffectChain()
Eric Laurentca7cc822012-11-19 14:55:58 -08002140{
Eric Laurentca7cc822012-11-19 14:55:58 -08002141}
2142
Andy Hungbd72c542023-06-20 18:56:17 -07002143// getEffectFromDesc_l() must be called with AudioFlinger::ThreadBase::mLock held
2144sp<IAfEffectModule> EffectChain::getEffectFromDesc_l(
2145 effect_descriptor_t *descriptor) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002146{
2147 size_t size = mEffects.size();
2148
2149 for (size_t i = 0; i < size; i++) {
2150 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2151 return mEffects[i];
2152 }
2153 }
2154 return 0;
2155}
2156
Andy Hungbd72c542023-06-20 18:56:17 -07002157// getEffectFromId_l() must be called with AudioFlinger::ThreadBase::mLock held
2158sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002159{
2160 size_t size = mEffects.size();
2161
2162 for (size_t i = 0; i < size; i++) {
2163 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2164 if (id == 0 || mEffects[i]->id() == id) {
2165 return mEffects[i];
2166 }
2167 }
2168 return 0;
2169}
2170
Andy Hungbd72c542023-06-20 18:56:17 -07002171// getEffectFromType_l() must be called with AudioFlinger::ThreadBase::mLock held
2172sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2173 const effect_uuid_t *type) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002174{
2175 size_t size = mEffects.size();
2176
2177 for (size_t i = 0; i < size; i++) {
2178 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2179 return mEffects[i];
2180 }
2181 }
2182 return 0;
2183}
2184
Andy Hungbd72c542023-06-20 18:56:17 -07002185std::vector<int> EffectChain::getEffectIds() const
Eric Laurent6c796322019-04-09 14:13:17 -07002186{
2187 std::vector<int> ids;
2188 Mutex::Autolock _l(mLock);
2189 for (size_t i = 0; i < mEffects.size(); i++) {
2190 ids.push_back(mEffects[i]->id());
2191 }
2192 return ids;
2193}
2194
Andy Hungbd72c542023-06-20 18:56:17 -07002195void EffectChain::clearInputBuffer()
Eric Laurentca7cc822012-11-19 14:55:58 -08002196{
2197 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002198 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002199}
2200
2201// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002202void EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002203{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002204 if (mInBuffer == NULL) {
2205 return;
2206 }
Andy Hung319587b2023-05-23 14:01:03 -07002207 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002208 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002209
Eric Laurent6b446ce2019-12-13 10:56:31 -08002210 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002211 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002212}
2213
2214// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002215void EffectChain::process_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002216{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002217 // never process effects when:
2218 // - on an OFFLOAD thread
2219 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002220 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002221 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 bool tracksOnSession = (trackCnt() != 0);
2223
2224 if (!tracksOnSession && mTailBufferCount == 0) {
2225 doProcess = false;
2226 }
2227
2228 if (activeTrackCnt() == 0) {
2229 // if no track is active and the effect tail has not been rendered,
2230 // the input buffer must be cleared here as the mixer process will not do it
2231 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002232 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002233 if (mTailBufferCount > 0) {
2234 mTailBufferCount--;
2235 }
2236 }
2237 }
2238 }
2239
2240 size_t size = mEffects.size();
2241 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002242 // Only the input and output buffers of the chain can be external,
2243 // and 'update' / 'commit' do nothing for allocated buffers, thus
2244 // it's not needed to consider any other buffers here.
2245 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002246 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2247 mOutBuffer->update();
2248 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002249 for (size_t i = 0; i < size; i++) {
2250 mEffects[i]->process();
2251 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002252 mInBuffer->commit();
2253 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2254 mOutBuffer->commit();
2255 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002256 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002257 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002258 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002259 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2260 }
2261 if (doResetVolume) {
2262 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002263 }
2264}
2265
Andy Hungbd72c542023-06-20 18:56:17 -07002266// createEffect_l() must be called with AudioFlinger::ThreadBase::mLock held
2267status_t EffectChain::createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002268 effect_descriptor_t *desc,
2269 int id,
2270 audio_session_t sessionId,
2271 bool pinned)
2272{
2273 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002274 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002275 status_t lStatus = effect->status();
2276 if (lStatus == NO_ERROR) {
2277 lStatus = addEffect_ll(effect);
2278 }
2279 if (lStatus != NO_ERROR) {
2280 effect.clear();
2281 }
2282 return lStatus;
2283}
2284
Andy Hungbd72c542023-06-20 18:56:17 -07002285// addEffect_l() must be called with AudioFlinger::ThreadBase::mLock held
2286status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
Eric Laurentca7cc822012-11-19 14:55:58 -08002287{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002288 Mutex::Autolock _l(mLock);
2289 return addEffect_ll(effect);
2290}
Andy Hungbd72c542023-06-20 18:56:17 -07002291// addEffect_l() must be called with AudioFlinger::ThreadBase::mLock and EffectChain::mLock held
2292status_t EffectChain::addEffect_ll(const sp<IAfEffectModule>& effect)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002293{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002294 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002295
Eric Laurentb62d0362021-10-26 17:40:18 +02002296 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002297 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2298 // Auxiliary effects are inserted at the beginning of mEffects vector as
2299 // they are processed first and accumulated in chain input buffer
2300 mEffects.insertAt(effect, 0);
2301
2302 // the input buffer for auxiliary effect contains mono samples in
2303 // 32 bit format. This is to avoid saturation in AudoMixer
2304 // accumulation stage. Saturation is done in EffectModule::process() before
2305 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002306 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002307 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002308
Eric Laurent6b446ce2019-12-13 10:56:31 -08002309 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002310 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002311 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002312
2313 effect->configure();
2314
Mikhail Naganov022b9952017-01-04 16:36:51 -08002315 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002316 // auxiliary effects output samples to chain input buffer for further processing
2317 // by insert effects
2318 effect->setOutBuffer(mInBuffer);
2319 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002320 ssize_t idx_insert = getInsertIndex(desc);
2321 if (idx_insert < 0) {
2322 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002323 }
2324
Eric Laurentb62d0362021-10-26 17:40:18 +02002325 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002326 mEffects.insertAt(effect, idx_insert);
2327
2328 effect->configure();
2329
Eric Laurentb62d0362021-10-26 17:40:18 +02002330 // - By default:
2331 // All effects read samples from chain input buffer.
2332 // The last effect in the chain, writes samples to chain output buffer,
2333 // otherwise to chain input buffer
2334 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2335 // The spatializer effect (first effect) reads samples from the input buffer
2336 // and writes samples to the output buffer.
2337 // All other effects read and writes samples to the output buffer
2338 if (mEffectCallback->isSpatializer()
2339 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002340 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002341 if (idx_insert == 0) {
2342 if (previousSize != 0) {
2343 mEffects[1]->configure();
2344 mEffects[1]->setInBuffer(mOutBuffer);
2345 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2346 }
2347 effect->setInBuffer(mInBuffer);
2348 } else {
2349 effect->setInBuffer(mOutBuffer);
2350 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002351 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002352 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002353 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002354 if (idx_insert != 0) {
2355 mEffects[idx_insert-1]->configure();
2356 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2357 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2358 }
2359 effect->setOutBuffer(mOutBuffer);
2360 } else {
2361 effect->setOutBuffer(mInBuffer);
2362 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002363 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002364 ALOGV("%s effect %p, added in chain %p at rank %zu",
2365 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002366 }
2367 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002368
Eric Laurentca7cc822012-11-19 14:55:58 -08002369 return NO_ERROR;
2370}
2371
Andy Hungbd72c542023-06-20 18:56:17 -07002372std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
jiabineb5784e2023-08-24 21:10:44 +00002373 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2374 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2375 return i - 1;
2376 }
2377 }
2378 return std::nullopt;
2379}
2380
Andy Hungbd72c542023-06-20 18:56:17 -07002381ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002382 // Insert effects are inserted at the end of mEffects vector as they are processed
2383 // after track and auxiliary effects.
2384 // Insert effect order as a function of indicated preference:
2385 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2386 // another effect is present
2387 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2388 // last effect claiming first position
2389 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2390 // first effect claiming last position
2391 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2392 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2393 // already present
2394 // Spatializer or Downmixer effects are inserted in first position because
2395 // they adapt the channel count for all other effects in the chain
2396 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2397 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2398 return 0;
2399 }
2400
2401 size_t size = mEffects.size();
2402 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2403 ssize_t idx_insert;
2404 ssize_t idx_insert_first = -1;
2405 ssize_t idx_insert_last = -1;
2406
2407 idx_insert = size;
2408 for (size_t i = 0; i < size; i++) {
2409 effect_descriptor_t d = mEffects[i]->desc();
2410 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2411 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2412 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2413 // check invalid effect chaining combinations
2414 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2415 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2416 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2417 __func__, desc.name, d.name);
2418 return -1;
2419 }
2420 // remember position of first insert effect and by default
2421 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002422 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002423 idx_insert = i;
2424 }
2425 // remember position of last insert effect claiming
2426 // first position
2427 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2428 idx_insert_first = i;
2429 }
2430 // remember position of first insert effect claiming
2431 // last position
2432 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2433 idx_insert_last == -1) {
2434 idx_insert_last = i;
2435 }
2436 }
2437 }
2438
2439 // modify idx_insert from first position if needed
2440 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2441 if (idx_insert_last != -1) {
2442 idx_insert = idx_insert_last;
2443 } else {
2444 idx_insert = size;
2445 }
2446 } else {
2447 if (idx_insert_first != -1) {
2448 idx_insert = idx_insert_first + 1;
2449 }
2450 }
2451 return idx_insert;
2452}
2453
Andy Hungbd72c542023-06-20 18:56:17 -07002454// removeEffect_l() must be called with AudioFlinger::ThreadBase::mLock held
2455size_t EffectChain::removeEffect_l(const sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002456 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002457{
2458 Mutex::Autolock _l(mLock);
2459 size_t size = mEffects.size();
2460 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2461
2462 for (size_t i = 0; i < size; i++) {
2463 if (effect == mEffects[i]) {
2464 // calling stop here will remove pre-processing effect from the audio HAL.
2465 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2466 // the middle of a read from audio HAL
2467 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2468 mEffects[i]->state() == EffectModule::STOPPING) {
2469 mEffects[i]->stop();
2470 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002471 if (release) {
2472 mEffects[i]->release_l();
2473 }
2474
Mikhail Naganov022b9952017-01-04 16:36:51 -08002475 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002476 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002477 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002478 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002479 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002480 }
2481 }
2482 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002483
2484 // make sure the input buffer configuration for the new first effect in the chain
2485 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002486 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2487 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002488 mEffects[0]->configure();
2489 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002490 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002491 }
2492
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002493 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002494 this, i);
2495 break;
2496 }
2497 }
2498
2499 return mEffects.size();
2500}
2501
Andy Hungbd72c542023-06-20 18:56:17 -07002502// setDevices_l() must be called with AudioFlinger::ThreadBase::mLock held
2503void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002504{
2505 size_t size = mEffects.size();
2506 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002507 mEffects[i]->setDevices(devices);
2508 }
2509}
2510
Andy Hungbd72c542023-06-20 18:56:17 -07002511// setInputDevice_l() must be called with AudioFlinger::ThreadBase::mLock held
2512void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08002513{
2514 size_t size = mEffects.size();
2515 for (size_t i = 0; i < size; i++) {
2516 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002517 }
2518}
2519
Andy Hungbd72c542023-06-20 18:56:17 -07002520// setMode_l() must be called with AudioFlinger::ThreadBase::mLock held
2521void EffectChain::setMode_l(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08002522{
2523 size_t size = mEffects.size();
2524 for (size_t i = 0; i < size; i++) {
2525 mEffects[i]->setMode(mode);
2526 }
2527}
2528
Andy Hungbd72c542023-06-20 18:56:17 -07002529// setAudioSource_l() must be called with AudioFlinger::ThreadBase::mLock held
2530void EffectChain::setAudioSource_l(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08002531{
2532 size_t size = mEffects.size();
2533 for (size_t i = 0; i < size; i++) {
2534 mEffects[i]->setAudioSource(source);
2535 }
2536}
2537
Andy Hungbd72c542023-06-20 18:56:17 -07002538bool EffectChain::hasVolumeControlEnabled_l() const {
Zhou Songd505c642020-02-20 16:35:37 +08002539 for (const auto &effect : mEffects) {
2540 if (effect->isVolumeControlEnabled()) return true;
2541 }
2542 return false;
2543}
2544
Andy Hungbd72c542023-06-20 18:56:17 -07002545// setVolume_l() must be called with AudioFlinger::ThreadBase::mLock or EffectChain::mLock held
2546bool EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002547{
2548 uint32_t newLeft = *left;
2549 uint32_t newRight = *right;
jiabineb5784e2023-08-24 21:10:44 +00002550 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002551
2552 // first update volume controller
jiabineb5784e2023-08-24 21:10:44 +00002553 const auto volumeControlIndex = findVolumeControl_l(0, size);
2554 const int ctrlIdx = volumeControlIndex.value_or(-1);
Andy Hungbd72c542023-06-20 18:56:17 -07002555 const sp<IAfEffectModule> volumeControlEffect =
jiabineb5784e2023-08-24 21:10:44 +00002556 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
Andy Hungbd72c542023-06-20 18:56:17 -07002557 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002558
jiabineb5784e2023-08-24 21:10:44 +00002559 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002560 *left == mLeftVolume && *right == mRightVolume) {
jiabineb5784e2023-08-24 21:10:44 +00002561 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002562 *left = mNewLeftVolume;
2563 *right = mNewRightVolume;
2564 }
jiabineb5784e2023-08-24 21:10:44 +00002565 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002566 }
2567
jiabineb5784e2023-08-24 21:10:44 +00002568 if (volumeControlEffect != cachedVolumeControlEffect) {
2569 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2570 // as zero for safe ramping.
2571 if (cachedVolumeControlEffect != nullptr) {
2572 uint32_t leftMax = 1 << 24;
2573 uint32_t rightMax = 1 << 24;
2574 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2575 }
2576 if (volumeControlEffect != nullptr) {
2577 uint32_t leftZero = 0;
2578 uint32_t rightZero = 0;
2579 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2580 }
2581 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002582 mLeftVolume = newLeft;
2583 mRightVolume = newRight;
2584
2585 // second get volume update from volume controller
2586 if (ctrlIdx >= 0) {
2587 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2588 mNewLeftVolume = newLeft;
2589 mNewRightVolume = newRight;
2590 }
2591 // then indicate volume to all other effects in chain.
2592 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002593 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002594 uint32_t lVol = newLeft;
2595 uint32_t rVol = newRight;
2596
2597 for (size_t i = 0; i < size; i++) {
2598 if ((int)i == ctrlIdx) {
2599 continue;
2600 }
2601 // this also works for ctrlIdx == -1 when there is no volume controller
2602 if ((int)i > ctrlIdx) {
2603 lVol = *left;
2604 rVol = *right;
2605 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002606 // Pass requested volume directly if this is volume monitor module
2607 if (mEffects[i]->isVolumeMonitor()) {
2608 mEffects[i]->setVolume(left, right, false);
2609 } else {
2610 mEffects[i]->setVolume(&lVol, &rVol, false);
2611 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002612 }
2613 *left = newLeft;
2614 *right = newRight;
2615
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002616 setVolumeForOutput_l(*left, *right);
2617
jiabineb5784e2023-08-24 21:10:44 +00002618 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002619}
2620
Andy Hungbd72c542023-06-20 18:56:17 -07002621// resetVolume_l() must be called with AudioFlinger::ThreadBase::mLock or EffectChain::mLock held
2622void EffectChain::resetVolume_l()
Eric Laurentfa1e1232016-08-02 19:01:49 -07002623{
Eric Laurente7449bf2016-08-03 18:44:07 -07002624 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2625 uint32_t left = mLeftVolume;
2626 uint32_t right = mRightVolume;
2627 (void)setVolume_l(&left, &right, true);
2628 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002629}
2630
Andy Hungbd72c542023-06-20 18:56:17 -07002631// containsHapticGeneratingEffect_l must be called with
2632// AudioFlinger::ThreadBase::mLock or EffectChain::mLock held
2633bool EffectChain::containsHapticGeneratingEffect_l()
jiabineb3bda02020-06-30 14:07:03 -07002634{
2635 for (size_t i = 0; i < mEffects.size(); ++i) {
2636 if (mEffects[i]->isHapticGenerator()) {
2637 return true;
2638 }
2639 }
2640 return false;
2641}
2642
Andy Hungbd72c542023-06-20 18:56:17 -07002643void EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002644{
2645 Mutex::Autolock _l(mLock);
2646 for (size_t i = 0; i < mEffects.size(); ++i) {
2647 mEffects[i]->setHapticIntensity(id, intensity);
2648 }
2649}
2650
Andy Hungbd72c542023-06-20 18:56:17 -07002651void EffectChain::syncHalEffectsState()
Eric Laurent1b928682014-10-02 19:41:47 -07002652{
2653 Mutex::Autolock _l(mLock);
2654 for (size_t i = 0; i < mEffects.size(); i++) {
2655 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2656 mEffects[i]->state() == EffectModule::STOPPING) {
2657 mEffects[i]->addEffectToHal_l();
2658 }
2659 }
2660}
2661
Andy Hungbd72c542023-06-20 18:56:17 -07002662void EffectChain::dump(int fd, const Vector<String16>& args) const
Andy Hung71ba4b32022-10-06 12:09:49 -07002663NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002664{
Eric Laurentca7cc822012-11-19 14:55:58 -08002665 String8 result;
2666
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002667 const size_t numEffects = mEffects.size();
2668 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002669
Marco Nelissenb2208842014-02-07 14:00:50 -08002670 if (numEffects) {
2671 bool locked = AudioFlinger::dumpTryLock(mLock);
2672 // failed to lock - AudioFlinger is probably deadlocked
2673 if (!locked) {
2674 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002675 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002676
Andy Hungbded9c82017-11-30 18:47:35 -08002677 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2678 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2679 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2680 (int)inBufferStr.size(), "In buffer ",
2681 (int)outBufferStr.size(), "Out buffer ");
2682 result.appendFormat("\t%s %s %d\n",
2683 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002684 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002685
2686 for (size_t i = 0; i < numEffects; ++i) {
Andy Hungbd72c542023-06-20 18:56:17 -07002687 sp<IAfEffectModule> effect = mEffects[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08002688 if (effect != 0) {
2689 effect->dump(fd, args);
2690 }
2691 }
2692
2693 if (locked) {
2694 mLock.unlock();
2695 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002696 } else {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002697 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002698 }
2699}
2700
Andy Hungbd72c542023-06-20 18:56:17 -07002701// must be called with AudioFlinger::ThreadBase::mLock held
2702void EffectChain::setEffectSuspended_l(
Eric Laurentca7cc822012-11-19 14:55:58 -08002703 const effect_uuid_t *type, bool suspend)
2704{
2705 sp<SuspendedEffectDesc> desc;
2706 // use effect type UUID timelow as key as there is no real risk of identical
2707 // timeLow fields among effect type UUIDs.
2708 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2709 if (suspend) {
2710 if (index >= 0) {
2711 desc = mSuspendedEffects.valueAt(index);
2712 } else {
2713 desc = new SuspendedEffectDesc();
2714 desc->mType = *type;
2715 mSuspendedEffects.add(type->timeLow, desc);
2716 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2717 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002718
Eric Laurentca7cc822012-11-19 14:55:58 -08002719 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002720 sp<IAfEffectModule> effect = getEffectIfEnabled(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002721 if (effect != 0) {
2722 desc->mEffect = effect;
2723 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002724 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002725 }
2726 }
2727 } else {
2728 if (index < 0) {
2729 return;
2730 }
2731 desc = mSuspendedEffects.valueAt(index);
2732 if (desc->mRefCount <= 0) {
2733 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002734 desc->mRefCount = 0;
2735 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002736 }
2737 if (--desc->mRefCount == 0) {
2738 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2739 if (desc->mEffect != 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002740 sp<IAfEffectModule> effect = desc->mEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002741 if (effect != 0) {
2742 effect->setSuspended(false);
2743 effect->lock();
Andy Hungbd72c542023-06-20 18:56:17 -07002744 IAfEffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002745 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002746 effect->setEnabled_l(handle->enabled());
2747 }
2748 effect->unlock();
2749 }
2750 desc->mEffect.clear();
2751 }
2752 mSuspendedEffects.removeItemsAt(index);
2753 }
2754 }
2755}
2756
Andy Hungbd72c542023-06-20 18:56:17 -07002757// must be called with AudioFlinger::ThreadBase::mLock held
2758void EffectChain::setEffectSuspendedAll_l(bool suspend)
Eric Laurentca7cc822012-11-19 14:55:58 -08002759{
2760 sp<SuspendedEffectDesc> desc;
2761
2762 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2763 if (suspend) {
2764 if (index >= 0) {
2765 desc = mSuspendedEffects.valueAt(index);
2766 } else {
2767 desc = new SuspendedEffectDesc();
2768 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2769 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2770 }
2771 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002772 Vector< sp<IAfEffectModule> > effects;
Eric Laurentca7cc822012-11-19 14:55:58 -08002773 getSuspendEligibleEffects(effects);
2774 for (size_t i = 0; i < effects.size(); i++) {
2775 setEffectSuspended_l(&effects[i]->desc().type, true);
2776 }
2777 }
2778 } else {
2779 if (index < 0) {
2780 return;
2781 }
2782 desc = mSuspendedEffects.valueAt(index);
2783 if (desc->mRefCount <= 0) {
2784 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2785 desc->mRefCount = 1;
2786 }
2787 if (--desc->mRefCount == 0) {
2788 Vector<const effect_uuid_t *> types;
2789 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2790 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2791 continue;
2792 }
2793 types.add(&mSuspendedEffects.valueAt(i)->mType);
2794 }
2795 for (size_t i = 0; i < types.size(); i++) {
2796 setEffectSuspended_l(types[i], false);
2797 }
2798 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2799 mSuspendedEffects.keyAt(index));
2800 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2801 }
2802 }
2803}
2804
2805
2806// The volume effect is used for automated tests only
2807#ifndef OPENSL_ES_H_
2808static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2809 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2810const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2811#endif //OPENSL_ES_H_
2812
Eric Laurentd8365c52017-07-16 15:27:05 -07002813/* static */
Andy Hungbd72c542023-06-20 18:56:17 -07002814bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
Eric Laurentd8365c52017-07-16 15:27:05 -07002815{
2816 // Only NS and AEC are suspended when BtNRec is off
2817 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2818 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2819 return true;
2820 }
2821 return false;
2822}
2823
Andy Hungbd72c542023-06-20 18:56:17 -07002824bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
Eric Laurentca7cc822012-11-19 14:55:58 -08002825{
2826 // auxiliary effects and visualizer are never suspended on output mix
2827 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2828 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2829 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002830 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2831 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002832 return false;
2833 }
2834 return true;
2835}
2836
Andy Hungbd72c542023-06-20 18:56:17 -07002837void EffectChain::getSuspendEligibleEffects(
2838 Vector< sp<IAfEffectModule> > &effects)
Eric Laurentca7cc822012-11-19 14:55:58 -08002839{
2840 effects.clear();
2841 for (size_t i = 0; i < mEffects.size(); i++) {
2842 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2843 effects.add(mEffects[i]);
2844 }
2845 }
2846}
2847
Andy Hungbd72c542023-06-20 18:56:17 -07002848sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
Eric Laurentca7cc822012-11-19 14:55:58 -08002849{
Andy Hungbd72c542023-06-20 18:56:17 -07002850 sp<IAfEffectModule> effect = getEffectFromType_l(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002851 return effect != 0 && effect->isEnabled() ? effect : 0;
2852}
2853
Andy Hungbd72c542023-06-20 18:56:17 -07002854void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08002855 bool enabled)
2856{
2857 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2858 if (enabled) {
2859 if (index < 0) {
2860 // if the effect is not suspend check if all effects are suspended
2861 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2862 if (index < 0) {
2863 return;
2864 }
2865 if (!isEffectEligibleForSuspend(effect->desc())) {
2866 return;
2867 }
2868 setEffectSuspended_l(&effect->desc().type, enabled);
2869 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2870 if (index < 0) {
2871 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2872 return;
2873 }
2874 }
2875 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2876 effect->desc().type.timeLow);
2877 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002878 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002879 if (desc->mEffect == 0) {
2880 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002881 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002882 effect->setSuspended(true);
2883 }
2884 } else {
2885 if (index < 0) {
2886 return;
2887 }
2888 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2889 effect->desc().type.timeLow);
2890 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2891 desc->mEffect.clear();
2892 effect->setSuspended(false);
2893 }
2894}
2895
Andy Hungbd72c542023-06-20 18:56:17 -07002896bool EffectChain::isNonOffloadableEnabled() const
Eric Laurent813e2a72013-08-31 12:59:48 -07002897{
2898 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002899 return isNonOffloadableEnabled_l();
2900}
2901
Andy Hungbd72c542023-06-20 18:56:17 -07002902bool EffectChain::isNonOffloadableEnabled_l() const
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002903{
Eric Laurent813e2a72013-08-31 12:59:48 -07002904 size_t size = mEffects.size();
2905 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002906 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002907 return true;
2908 }
2909 }
2910 return false;
2911}
2912
Andy Hungbd72c542023-06-20 18:56:17 -07002913void EffectChain::setThread(const sp<AudioFlinger::ThreadBase>& thread)
Eric Laurentaaa44472014-09-12 17:41:50 -07002914{
2915 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002916 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002917}
2918
Andy Hungbd72c542023-06-20 18:56:17 -07002919void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002920{
2921 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2922 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2923 }
2924 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2925 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2926 }
jiabinc658e452022-10-21 20:52:21 +00002927 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2928 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2929 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002930}
2931
Andy Hungbd72c542023-06-20 18:56:17 -07002932void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002933{
2934 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2935 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2936 }
2937 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2938 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2939 }
2940}
2941
Andy Hungbd72c542023-06-20 18:56:17 -07002942bool EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002943{
2944 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002945 for (const auto &effect : mEffects) {
2946 if (effect->isProcessImplemented()) {
2947 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002948 }
2949 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002950 // Allow effects without processing.
2951 return true;
2952}
2953
Andy Hungbd72c542023-06-20 18:56:17 -07002954bool EffectChain::isFastCompatible() const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002955{
2956 Mutex::Autolock _l(mLock);
2957 for (const auto &effect : mEffects) {
2958 if (effect->isProcessImplemented()
2959 && effect->isImplementationSoftware()) {
2960 return false;
2961 }
2962 }
2963 // Allow effects without processing or hw accelerated effects.
2964 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002965}
2966
Andy Hungbd72c542023-06-20 18:56:17 -07002967bool EffectChain::isBitPerfectCompatible() const {
jiabinc658e452022-10-21 20:52:21 +00002968 Mutex::Autolock _l(mLock);
2969 for (const auto &effect : mEffects) {
2970 if (effect->isProcessImplemented()
2971 && effect->isImplementationSoftware()) {
2972 return false;
2973 }
2974 }
2975 // Allow effects without processing or hw accelerated effects.
2976 return true;
2977}
2978
Eric Laurent4c415062016-06-17 16:14:16 -07002979// isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002980bool EffectChain::isCompatibleWithThread_l(const sp<AudioFlinger::ThreadBase>& thread) const
Eric Laurent4c415062016-06-17 16:14:16 -07002981{
2982 Mutex::Autolock _l(mLock);
2983 for (size_t i = 0; i < mEffects.size(); i++) {
2984 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2985 return false;
2986 }
2987 }
2988 return true;
2989}
2990
Eric Laurent6b446ce2019-12-13 10:56:31 -08002991// EffectCallbackInterface implementation
Andy Hungbd72c542023-06-20 18:56:17 -07002992status_t EffectChain::EffectCallback::createEffectHal(
Eric Laurent6b446ce2019-12-13 10:56:31 -08002993 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2994 sp<EffectHalInterface> *effect) {
2995 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002996 const sp<EffectsFactoryHalInterface> effectsFactory =
2997 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002998 if (effectsFactory != 0) {
2999 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3000 }
3001 return status;
3002}
3003
Andy Hungbd72c542023-06-20 18:56:17 -07003004bool EffectChain::EffectCallback::updateOrphanEffectChains(
3005 const sp<IAfEffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003006 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003007 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003008}
3009
Andy Hungbd72c542023-06-20 18:56:17 -07003010status_t EffectChain::EffectCallback::allocateHalBuffer(
Eric Laurent6b446ce2019-12-13 10:56:31 -08003011 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003012 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003013}
3014
Andy Hungbd72c542023-06-20 18:56:17 -07003015status_t EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003016 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003017 status_t result = NO_INIT;
Andy Hungbd72c542023-06-20 18:56:17 -07003018 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003019 if (t == nullptr) {
3020 return result;
3021 }
3022 sp <StreamHalInterface> st = t->stream();
3023 if (st == nullptr) {
3024 return result;
3025 }
3026 result = st->addEffect(effect);
3027 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3028 return result;
3029}
3030
Andy Hungbd72c542023-06-20 18:56:17 -07003031status_t EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003032 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003033 status_t result = NO_INIT;
Andy Hungbd72c542023-06-20 18:56:17 -07003034 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003035 if (t == nullptr) {
3036 return result;
3037 }
3038 sp <StreamHalInterface> st = t->stream();
3039 if (st == nullptr) {
3040 return result;
3041 }
3042 result = st->removeEffect(effect);
3043 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3044 return result;
3045}
3046
Andy Hungbd72c542023-06-20 18:56:17 -07003047audio_io_handle_t EffectChain::EffectCallback::io() const {
3048 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (t == nullptr) {
3050 return AUDIO_IO_HANDLE_NONE;
3051 }
3052 return t->id();
3053}
3054
Andy Hungbd72c542023-06-20 18:56:17 -07003055bool EffectChain::EffectCallback::isOutput() const {
3056 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003057 if (t == nullptr) {
3058 return true;
3059 }
3060 return t->isOutput();
3061}
3062
Andy Hungbd72c542023-06-20 18:56:17 -07003063bool EffectChain::EffectCallback::isOffload() const {
3064 return mThreadType == AudioFlinger::ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003065}
3066
Andy Hungbd72c542023-06-20 18:56:17 -07003067bool EffectChain::EffectCallback::isOffloadOrDirect() const {
3068 return mThreadType == AudioFlinger::ThreadBase::OFFLOAD
3069 || mThreadType == AudioFlinger::ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003070}
3071
Andy Hungbd72c542023-06-20 18:56:17 -07003072bool EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003073 switch (mThreadType) {
Andy Hungbd72c542023-06-20 18:56:17 -07003074 case AudioFlinger::ThreadBase::OFFLOAD:
3075 case AudioFlinger::ThreadBase::MMAP_PLAYBACK:
3076 case AudioFlinger::ThreadBase::MMAP_CAPTURE:
Eric Laurentb62d0362021-10-26 17:40:18 +02003077 return true;
3078 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003079 return false;
3080 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003081}
3082
Andy Hungbd72c542023-06-20 18:56:17 -07003083bool EffectChain::EffectCallback::isSpatializer() const {
3084 return mThreadType == AudioFlinger::ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003085}
3086
Andy Hungbd72c542023-06-20 18:56:17 -07003087uint32_t EffectChain::EffectCallback::sampleRate() const {
3088 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003089 if (t == nullptr) {
3090 return 0;
3091 }
3092 return t->sampleRate();
3093}
3094
Andy Hungbd72c542023-06-20 18:56:17 -07003095audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const {
3096 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003097 if (t == nullptr) {
3098 return AUDIO_CHANNEL_NONE;
3099 }
Andy Hungbd72c542023-06-20 18:56:17 -07003100 sp<IAfEffectChain> c = chain().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003101 if (c == nullptr) {
3102 return AUDIO_CHANNEL_NONE;
3103 }
3104
Andy Hungbd72c542023-06-20 18:56:17 -07003105 if (mThreadType == AudioFlinger::ThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003106 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3107 if (c->isFirstEffect(id)) {
3108 return t->mixerChannelMask();
3109 } else {
3110 return t->channelMask();
3111 }
3112 } else if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003113 if ((t->hasAudioSession_l(c->sessionId())
3114 & AudioFlinger::ThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003115 return t->mixerChannelMask();
3116 } else {
3117 return t->channelMask();
3118 }
3119 } else {
3120 return t->channelMask();
3121 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003122 } else {
3123 return t->channelMask();
3124 }
3125}
3126
Andy Hungbd72c542023-06-20 18:56:17 -07003127uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003128 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003129}
3130
Andy Hungbd72c542023-06-20 18:56:17 -07003131audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const {
3132 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003133 if (t == nullptr) {
3134 return AUDIO_CHANNEL_NONE;
3135 }
Andy Hungbd72c542023-06-20 18:56:17 -07003136 sp<IAfEffectChain> c = chain().promote();
Eric Laurentb62d0362021-10-26 17:40:18 +02003137 if (c == nullptr) {
3138 return AUDIO_CHANNEL_NONE;
3139 }
3140
Andy Hungbd72c542023-06-20 18:56:17 -07003141 if (mThreadType == AudioFlinger::ThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003142 if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003143 if ((t->hasAudioSession_l(c->sessionId())
3144 & AudioFlinger::ThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003145 return t->mixerChannelMask();
3146 } else {
3147 return t->channelMask();
3148 }
3149 } else {
3150 return t->channelMask();
3151 }
3152 } else {
3153 return t->channelMask();
3154 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003155}
3156
Andy Hungbd72c542023-06-20 18:56:17 -07003157uint32_t EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003158 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003159}
3160
Andy Hungbd72c542023-06-20 18:56:17 -07003161audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
3162 sp<AudioFlinger::ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003163 if (t == nullptr) {
3164 return AUDIO_CHANNEL_NONE;
3165 }
3166 return t->hapticChannelMask();
3167}
3168
Andy Hungbd72c542023-06-20 18:56:17 -07003169size_t EffectChain::EffectCallback::frameCount() const {
3170 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171 if (t == nullptr) {
3172 return 0;
3173 }
3174 return t->frameCount();
3175}
3176
Andy Hungbd72c542023-06-20 18:56:17 -07003177uint32_t EffectChain::EffectCallback::latency() const
Andy Hung71ba4b32022-10-06 12:09:49 -07003178NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3179{
Andy Hungbd72c542023-06-20 18:56:17 -07003180 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003181 if (t == nullptr) {
3182 return 0;
3183 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003184 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003185 return t->latency_l();
3186}
3187
Andy Hungbd72c542023-06-20 18:56:17 -07003188void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003189NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3190{
Andy Hungbd72c542023-06-20 18:56:17 -07003191 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003192 if (t == nullptr) {
3193 return;
3194 }
3195 t->setVolumeForOutput_l(left, right);
3196}
3197
Andy Hungbd72c542023-06-20 18:56:17 -07003198void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
3199 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
3200 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003201 if (t == nullptr) {
3202 return;
3203 }
3204 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3205
Andy Hungbd72c542023-06-20 18:56:17 -07003206 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003207 if (c == nullptr) {
3208 return;
3209 }
Eric Laurent41709552019-12-16 19:34:05 -08003210 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3211 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003212}
3213
Andy Hungbd72c542023-06-20 18:56:17 -07003214void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
3215 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003216 if (t == nullptr) {
3217 return;
3218 }
Eric Laurent41709552019-12-16 19:34:05 -08003219 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3220 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003221}
3222
Andy Hungbd72c542023-06-20 18:56:17 -07003223void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003224 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3225
Andy Hungbd72c542023-06-20 18:56:17 -07003226 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003227 if (t == nullptr) {
3228 return;
3229 }
3230 t->onEffectDisable();
3231}
3232
Andy Hungbd72c542023-06-20 18:56:17 -07003233bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
Eric Laurent6b446ce2019-12-13 10:56:31 -08003234 bool unpinIfLast) {
Andy Hungbd72c542023-06-20 18:56:17 -07003235 sp<AudioFlinger::ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003236 if (t == nullptr) {
3237 return false;
3238 }
3239 t->disconnectEffectHandle(handle, unpinIfLast);
3240 return true;
3241}
3242
Andy Hungbd72c542023-06-20 18:56:17 -07003243void EffectChain::EffectCallback::resetVolume() {
3244 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003245 if (c == nullptr) {
3246 return;
3247 }
3248 c->resetVolume_l();
3249
3250}
3251
Andy Hungbd72c542023-06-20 18:56:17 -07003252product_strategy_t EffectChain::EffectCallback::strategy() const {
3253 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003254 if (c == nullptr) {
3255 return PRODUCT_STRATEGY_NONE;
3256 }
3257 return c->strategy();
3258}
3259
Andy Hungbd72c542023-06-20 18:56:17 -07003260int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3261 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003262 if (c == nullptr) {
3263 return 0;
3264 }
3265 return c->activeTrackCnt();
3266}
3267
Eric Laurentb82e6b72019-11-22 17:25:04 -08003268
3269#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003270#define LOG_TAG "DeviceEffectProxy"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003271
Andy Hungbd72c542023-06-20 18:56:17 -07003272/* static */
3273sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3274 const AudioDeviceTypeAddr& device,
3275 const sp</* DeviceEffectManagerCallback */ RefBase>& callback, // TODO(b/288339104) type
3276 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3277{
3278 return sp<DeviceEffectProxy>::make(device,
3279 sp<AudioFlinger::DeviceEffectManagerCallback>::cast(callback),
3280 desc, id, notifyFramesProcessed);
3281}
3282
3283status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003284{
3285 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3286 Mutex::Autolock _l(mProxyLock);
3287 if (status == NO_ERROR) {
3288 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003289 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003290 if (enabled) {
Andy Hungbd72c542023-06-20 18:56:17 -07003291 bs = handle.second->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003292 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003293 bs = handle.second->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003294 }
3295 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003296 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003297 }
3298 }
3299 }
3300 ALOGV("%s enable %d status %d", __func__, enabled, status);
3301 return status;
3302}
3303
Andy Hungbd72c542023-06-20 18:56:17 -07003304status_t DeviceEffectProxy::init(
3305 const std::map <audio_patch_handle_t, AudioFlinger::PatchPanel::Patch>& patches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003306//For all audio patches
3307//If src or sink device match
3308//If the effect is HW accelerated
3309// if no corresponding effect module
3310// Create EffectModule: mHalEffect
3311//Create and attach EffectHandle
3312//If the effect is not HW accelerated and the patch sink or src is a mixer port
3313// Create Effect on patch input or output thread on session -1
3314//Add EffectHandle to EffectHandle map of Effect Proxy:
3315 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3316 status_t status = NO_ERROR;
3317 for (auto &patch : patches) {
3318 status = onCreatePatch(patch.first, patch.second);
3319 ALOGV("%s onCreatePatch status %d", __func__, status);
3320 if (status == BAD_VALUE) {
3321 return status;
3322 }
3323 }
3324 return status;
3325}
3326
Andy Hungbd72c542023-06-20 18:56:17 -07003327status_t DeviceEffectProxy::onCreatePatch(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003328 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3329 status_t status = NAME_NOT_FOUND;
Andy Hungbd72c542023-06-20 18:56:17 -07003330 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003331 // only consider source[0] as this is the only "true" source of a patch
3332 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3333 ALOGV("%s source checkPort status %d", __func__, status);
3334 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3335 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3336 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3337 }
3338 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3339 Mutex::Autolock _l(mProxyLock);
3340 mEffectHandles.emplace(patchHandle, handle);
3341 }
3342 ALOGW_IF(status == BAD_VALUE,
3343 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3344
3345 return status;
3346}
3347
Andy Hungbd72c542023-06-20 18:56:17 -07003348status_t DeviceEffectProxy::checkPort(const AudioFlinger::PatchPanel::Patch& patch,
3349 const struct audio_port_config *port, sp<IAfEffectHandle> *handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003350
3351 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3352 __func__, port->type, port->ext.device.type,
3353 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003354 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3355 port->ext.device.address != mDevice.address()) {
3356 return NAME_NOT_FOUND;
3357 }
3358 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3359 (audio_port_config_has_input_direction(port))) {
3360 ALOGI("%s don't create postprocessing effect on record port", __func__);
3361 return NAME_NOT_FOUND;
3362 }
3363 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3364 (!audio_port_config_has_input_direction(port))) {
3365 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003366 return NAME_NOT_FOUND;
3367 }
3368 status_t status = NAME_NOT_FOUND;
3369
3370 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3371 Mutex::Autolock _l(mProxyLock);
3372 mDevicePort = *port;
3373 mHalEffect = new EffectModule(mMyCallback,
3374 const_cast<effect_descriptor_t *>(&mDescriptor),
3375 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3376 false /* pinned */, port->id);
3377 if (audio_is_input_device(mDevice.mType)) {
3378 mHalEffect->setInputDevice(mDevice);
3379 } else {
3380 mHalEffect->setDevices({mDevice});
3381 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003382 mHalEffect->configure();
3383
Eric Laurentde8caf42021-08-11 17:19:25 +02003384 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3385 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003386 status = (*handle)->initCheck();
3387 if (status == OK) {
3388 status = mHalEffect->addHandle((*handle).get());
3389 } else {
3390 mHalEffect.clear();
3391 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3392 }
3393 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
Andy Hungbd72c542023-06-20 18:56:17 -07003394 sp <AudioFlinger::ThreadBase> thread;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003395 if (audio_port_config_has_input_direction(port)) {
3396 if (patch.isSoftware()) {
3397 thread = patch.mRecord.thread();
3398 } else {
3399 thread = patch.thread().promote();
3400 }
3401 } else {
3402 if (patch.isSoftware()) {
3403 thread = patch.mPlayback.thread();
3404 } else {
3405 thread = patch.thread().promote();
3406 }
3407 }
3408 int enabled;
3409 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3410 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003411 &enabled, &status, false, false /*probe*/,
3412 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003413 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3414 } else {
3415 status = BAD_VALUE;
3416 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003417
Eric Laurentb82e6b72019-11-22 17:25:04 -08003418 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003419 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003420 if (isEnabled()) {
Andy Hungbd72c542023-06-20 18:56:17 -07003421 bs = (*handle)->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003422 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003423 bs = (*handle)->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003424 }
3425 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003426 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003427 }
3428 }
3429 return status;
3430}
3431
Andy Hungbd72c542023-06-20 18:56:17 -07003432void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3433 sp<IAfEffectHandle> effect;
Eric Laurent76c89f32021-12-03 17:13:23 +01003434 {
3435 Mutex::Autolock _l(mProxyLock);
3436 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3437 effect = mEffectHandles.at(patchHandle);
3438 mEffectHandles.erase(patchHandle);
3439 }
3440 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003441}
3442
3443
Andy Hungbd72c542023-06-20 18:56:17 -07003444size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003445{
3446 Mutex::Autolock _l(mProxyLock);
3447 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003448 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003449 mHalEffect.clear();
3450 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3451 }
3452 return mHalEffect == nullptr ? 0 : 1;
3453}
3454
Andy Hungbd72c542023-06-20 18:56:17 -07003455status_t DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003456 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003457 if (mHalEffect == nullptr) {
3458 return NO_INIT;
3459 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003460 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003461}
3462
Andy Hungbd72c542023-06-20 18:56:17 -07003463status_t DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003464 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003465 if (mHalEffect == nullptr) {
3466 return NO_INIT;
3467 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003468 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003469}
3470
Andy Hungbd72c542023-06-20 18:56:17 -07003471bool DeviceEffectProxy::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003472 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3473 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3474 }
3475 return true;
3476}
3477
Andy Hungbd72c542023-06-20 18:56:17 -07003478uint32_t DeviceEffectProxy::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003479 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3480 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3481 return mDevicePort.sample_rate;
3482 }
3483 return DEFAULT_OUTPUT_SAMPLE_RATE;
3484}
3485
Andy Hungbd72c542023-06-20 18:56:17 -07003486audio_channel_mask_t DeviceEffectProxy::channelMask() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003487 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3488 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3489 return mDevicePort.channel_mask;
3490 }
3491 return AUDIO_CHANNEL_OUT_STEREO;
3492}
3493
Andy Hungbd72c542023-06-20 18:56:17 -07003494uint32_t DeviceEffectProxy::channelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003495 if (isOutput()) {
3496 return audio_channel_count_from_out_mask(channelMask());
3497 }
3498 return audio_channel_count_from_in_mask(channelMask());
3499}
3500
Andy Hungbd72c542023-06-20 18:56:17 -07003501void DeviceEffectProxy::dump2(int fd, int spaces) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003502NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3503{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003504 const Vector<String16> args;
3505 EffectBase::dump(fd, args);
3506
Andy Hungbd72c542023-06-20 18:56:17 -07003507 const bool locked = AudioFlinger::dumpTryLock(mProxyLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003508 if (!locked) {
3509 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003510 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003511 }
3512
3513 String8 outStr;
3514 if (mHalEffect != nullptr) {
3515 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3516 } else {
3517 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3518 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003519 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003520 outStr.clear();
3521
3522 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003523 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003524 outStr.clear();
3525
3526 for (const auto& iter : mEffectHandles) {
3527 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003528 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003529 outStr.clear();
Andy Hungbd72c542023-06-20 18:56:17 -07003530 sp<IAfEffectBase> effect = iter.second->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003531 if (effect != nullptr) {
3532 effect->dump(fd, args);
3533 }
3534 }
3535
3536 if (locked) {
3537 mLock.unlock();
3538 }
3539}
3540
3541#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003542#define LOG_TAG "DeviceEffectProxy::ProxyCallback"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003543
Andy Hungbd72c542023-06-20 18:56:17 -07003544int DeviceEffectProxy::ProxyCallback::newEffectId() {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003545 return mManagerCallback->newEffectId();
3546}
3547
3548
Andy Hungbd72c542023-06-20 18:56:17 -07003549bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3550 IAfEffectHandle *handle, bool unpinIfLast) {
3551 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003552 if (effectBase == nullptr) {
3553 return false;
3554 }
3555
Andy Hungbd72c542023-06-20 18:56:17 -07003556 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003557 if (effect == nullptr) {
3558 return false;
3559 }
3560
3561 // restore suspended effects if the disconnected handle was enabled and the last one.
3562 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3563 if (remove) {
3564 sp<DeviceEffectProxy> proxy = mProxy.promote();
3565 if (proxy != nullptr) {
3566 proxy->removeEffect(effect);
3567 }
3568 if (handle->enabled()) {
3569 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3570 }
3571 }
3572 return true;
3573}
3574
Andy Hungbd72c542023-06-20 18:56:17 -07003575status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003576 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3577 sp<EffectHalInterface> *effect) {
3578 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3579}
3580
Andy Hungbd72c542023-06-20 18:56:17 -07003581status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003582 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003583 sp<DeviceEffectProxy> proxy = mProxy.promote();
3584 if (proxy == nullptr) {
3585 return NO_INIT;
3586 }
3587 return proxy->addEffectToHal(effect);
3588}
3589
Andy Hungbd72c542023-06-20 18:56:17 -07003590status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003591 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003592 sp<DeviceEffectProxy> proxy = mProxy.promote();
3593 if (proxy == nullptr) {
3594 return NO_INIT;
3595 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003596 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003597}
3598
Andy Hungbd72c542023-06-20 18:56:17 -07003599bool DeviceEffectProxy::ProxyCallback::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003600 sp<DeviceEffectProxy> proxy = mProxy.promote();
3601 if (proxy == nullptr) {
3602 return true;
3603 }
3604 return proxy->isOutput();
3605}
3606
Andy Hungbd72c542023-06-20 18:56:17 -07003607uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003608 sp<DeviceEffectProxy> proxy = mProxy.promote();
3609 if (proxy == nullptr) {
3610 return DEFAULT_OUTPUT_SAMPLE_RATE;
3611 }
3612 return proxy->sampleRate();
3613}
3614
Andy Hungbd72c542023-06-20 18:56:17 -07003615audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
Eric Laurentf1f22e72021-07-13 14:04:14 +02003616 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003617 sp<DeviceEffectProxy> proxy = mProxy.promote();
3618 if (proxy == nullptr) {
3619 return AUDIO_CHANNEL_OUT_STEREO;
3620 }
3621 return proxy->channelMask();
3622}
3623
Andy Hungbd72c542023-06-20 18:56:17 -07003624uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003625 sp<DeviceEffectProxy> proxy = mProxy.promote();
3626 if (proxy == nullptr) {
3627 return 2;
3628 }
3629 return proxy->channelCount();
3630}
3631
Andy Hungbd72c542023-06-20 18:56:17 -07003632audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003633 sp<DeviceEffectProxy> proxy = mProxy.promote();
3634 if (proxy == nullptr) {
3635 return AUDIO_CHANNEL_OUT_STEREO;
3636 }
3637 return proxy->channelMask();
3638}
3639
Andy Hungbd72c542023-06-20 18:56:17 -07003640uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003641 sp<DeviceEffectProxy> proxy = mProxy.promote();
3642 if (proxy == nullptr) {
3643 return 2;
3644 }
3645 return proxy->channelCount();
3646}
3647
Andy Hungbd72c542023-06-20 18:56:17 -07003648void DeviceEffectProxy::ProxyCallback::onEffectEnable(
3649 const sp<IAfEffectBase>& effectBase) {
3650 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003651 if (effect == nullptr) {
3652 return;
3653 }
3654 effect->start();
3655}
3656
Andy Hungbd72c542023-06-20 18:56:17 -07003657void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3658 const sp<IAfEffectBase>& effectBase) {
3659 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003660 if (effect == nullptr) {
3661 return;
3662 }
3663 effect->stop();
3664}
3665
Glenn Kasten63238ef2015-03-02 15:50:29 -08003666} // namespace android