blob: e318e2fe05bdcf4473e2231adbb932946e08a1c8 [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,
Andy Hungd65869f2023-06-27 17:05:02 -07001677 const sp<Client>& client,
Andy Hungbd72c542023-06-20 18:56:17 -07001678 const sp<media::IEffectClient>& effectClient,
1679 int32_t priority, bool notifyFramesProcessed)
1680{
1681 return sp<EffectHandle>::make(
Andy Hungd65869f2023-06-27 17:05:02 -07001682 effect, client, effectClient, priority, notifyFramesProcessed);
Andy Hungbd72c542023-06-20 18:56:17 -07001683}
1684
1685EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hungd65869f2023-06-27 17:05:02 -07001686 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001687 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001688 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001689 : BnEffect(),
1690 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001691 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1692 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001693{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001694 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001695 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001696
1697 if (client == 0) {
1698 return;
1699 }
1700 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001701 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1702 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1703 std::string("Effect ID: ")
1704 .append(std::to_string(effect->id()))
1705 .append(" Session ID: ")
1706 .append(std::to_string(static_cast<int>(effect->sessionId())))
1707 .append(" \n")
1708 });
Glenn Kastene75da402013-11-20 13:54:52 -08001709 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001710 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001711 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001713 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 return;
1715 }
Glenn Kastene75da402013-11-20 13:54:52 -08001716 new(mCblk) effect_param_cblk_t();
1717 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001718}
1719
Andy Hungbd72c542023-06-20 18:56:17 -07001720EffectHandle::~EffectHandle()
Eric Laurentca7cc822012-11-19 14:55:58 -08001721{
1722 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 disconnect(false);
1724}
1725
Andy Hungc747c532022-03-07 21:41:14 -08001726// Creates an association between Binder code to name for IEffect.
1727#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1728BINDER_METHOD_ENTRY(enable) \
1729BINDER_METHOD_ENTRY(disable) \
1730BINDER_METHOD_ENTRY(command) \
1731BINDER_METHOD_ENTRY(disconnect) \
1732BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001733BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001734
1735// singleton for Binder Method Statistics for IEffect
1736mediautils::MethodStatistics<int>& getIEffectStatistics() {
1737 using Code = int;
1738
1739#pragma push_macro("BINDER_METHOD_ENTRY")
1740#undef BINDER_METHOD_ENTRY
1741#define BINDER_METHOD_ENTRY(ENTRY) \
1742 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1743
1744 static mediautils::MethodStatistics<Code> methodStatistics{
1745 IEFFECT_BINDER_METHOD_MACRO_LIST
1746 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1747 };
1748#pragma pop_macro("BINDER_METHOD_ENTRY")
1749
1750 return methodStatistics;
1751}
1752
Andy Hungbd72c542023-06-20 18:56:17 -07001753status_t EffectHandle::onTransact(
Andy Hungc747c532022-03-07 21:41:14 -08001754 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001755 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1756 mediautils::TimeCheck check(
1757 std::string("IEffect::").append(methodName),
1758 [code](bool timeout, float elapsedMs) {
1759 if (timeout) {
1760 ; // we don't timeout right now on the effect interface.
1761 } else {
1762 getIEffectStatistics().event(code, elapsedMs);
1763 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001764 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001765 return BnEffect::onTransact(code, data, reply, flags);
1766}
1767
Andy Hungbd72c542023-06-20 18:56:17 -07001768status_t EffectHandle::initCheck() const
Glenn Kastene75da402013-11-20 13:54:52 -08001769{
1770 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1771}
1772
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001773#define RETURN(code) \
1774 *_aidl_return = (code); \
1775 return Status::ok();
1776
Mikhail Naganov59984db2022-04-19 21:21:23 +00001777#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1778 ({ \
1779 auto _tmp = (exp); \
1780 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1781 std::move(_tmp.value()); \
1782 })
1783
Andy Hungbd72c542023-06-20 18:56:17 -07001784Status EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001785{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001786 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001787 ALOGV("enable %p", this);
Andy Hungbd72c542023-06-20 18:56:17 -07001788 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001789 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001790 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001791 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001792 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001793 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001794 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001795
1796 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001797 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001798 }
1799
1800 mEnabled = true;
1801
Eric Laurent6c796322019-04-09 14:13:17 -07001802 status_t status = effect->updatePolicyState();
1803 if (status != NO_ERROR) {
1804 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001805 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001806 }
1807
Eric Laurent6b446ce2019-12-13 10:56:31 -08001808 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001809
1810 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001811 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001812 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001813 }
1814
Eric Laurent6b446ce2019-12-13 10:56:31 -08001815 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 mEnabled = false;
1818 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001819 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001820}
1821
Andy Hungbd72c542023-06-20 18:56:17 -07001822Status EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001823{
1824 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001825 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001826 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001827 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001828 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001829 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001830 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001831 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001832 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001833
1834 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001835 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001836 }
1837 mEnabled = false;
1838
Eric Laurent6c796322019-04-09 14:13:17 -07001839 effect->updatePolicyState();
1840
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001841 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001842 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 }
1844
Eric Laurent6b446ce2019-12-13 10:56:31 -08001845 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001846 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001847}
1848
Andy Hungbd72c542023-06-20 18:56:17 -07001849Status EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001850{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001851 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001854}
1855
Andy Hungbd72c542023-06-20 18:56:17 -07001856void EffectHandle::disconnect(bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -08001857{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001858 AutoMutex _l(mLock);
1859 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1860 if (mDisconnected) {
1861 if (unpinIfLast) {
1862 android_errorWriteLog(0x534e4554, "32707507");
1863 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 return;
1865 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001866 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001867 {
Andy Hungbd72c542023-06-20 18:56:17 -07001868 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001869 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001870 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001871 ALOGW("%s Effect handle %p disconnected after thread destruction",
1872 __func__, this);
1873 }
1874 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001875 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001876 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001877
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 if (mClient != 0) {
1879 if (mCblk != NULL) {
1880 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1881 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1882 }
1883 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001884 // Client destructor must run with AudioFlinger client mutex locked
Andy Hungfa2f4f32023-07-17 12:40:43 -07001885 Mutex::Autolock _l2(mClient->afClientCallback()->clientMutex());
Eric Laurentca7cc822012-11-19 14:55:58 -08001886 mClient.clear();
1887 }
1888}
1889
Andy Hungbd72c542023-06-20 18:56:17 -07001890Status EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001891 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1892 return Status::ok();
1893}
1894
Andy Hungbd72c542023-06-20 18:56:17 -07001895Status EffectHandle::getConfig(
Mikhail Naganov59984db2022-04-19 21:21:23 +00001896 media::EffectConfig* _config, int32_t* _aidl_return) {
1897 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001898 sp<IAfEffectBase> effect = mEffect.promote();
Mikhail Naganov59984db2022-04-19 21:21:23 +00001899 if (effect == nullptr || mDisconnected) {
1900 RETURN(DEAD_OBJECT);
1901 }
Andy Hungbd72c542023-06-20 18:56:17 -07001902 sp<IAfEffectModule> effectModule = effect->asEffectModule();
Mikhail Naganov59984db2022-04-19 21:21:23 +00001903 if (effectModule == nullptr) {
1904 RETURN(INVALID_OPERATION);
1905 }
1906 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1907 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1908 bool isOutput;
1909 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1910 if (status == NO_ERROR) {
1911 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1912 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1913 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1914 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1915 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1916 _config->isOnInputStream = !isOutput;
1917 }
1918 RETURN(status);
1919}
1920
Andy Hungbd72c542023-06-20 18:56:17 -07001921Status EffectHandle::command(int32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001922 const std::vector<uint8_t>& cmdData,
1923 int32_t maxResponseSize,
1924 std::vector<uint8_t>* response,
1925 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001926{
1927 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001928 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001929
Eric Laurentc7ab3092017-06-15 18:43:46 -07001930 // reject commands reserved for internal use by audio framework if coming from outside
1931 // of audioserver
1932 switch(cmdCode) {
1933 case EFFECT_CMD_ENABLE:
1934 case EFFECT_CMD_DISABLE:
1935 case EFFECT_CMD_SET_PARAM:
1936 case EFFECT_CMD_SET_PARAM_DEFERRED:
1937 case EFFECT_CMD_SET_PARAM_COMMIT:
1938 case EFFECT_CMD_GET_PARAM:
1939 break;
1940 default:
1941 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1942 break;
1943 }
1944 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001945 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001946 }
1947
Eric Laurent1ffc5852016-12-15 14:46:09 -08001948 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001949 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001950 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001951 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001952 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001953 writeToBuffer(NO_ERROR, response);
1954 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001955 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001956 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001957 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001959 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001960 writeToBuffer(NO_ERROR, response);
1961 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001962 }
1963
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001964 AutoMutex _l(mLock);
Andy Hungbd72c542023-06-20 18:56:17 -07001965 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001966 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001968 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 // only get parameter command is permitted for applications not controlling the effect
1970 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001971 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001972 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001973
1974 // handle commands that are not forwarded transparently to effect engine
1975 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001976 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001977 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001978 }
1979
Andy Hung71ba4b32022-10-06 12:09:49 -07001980 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001981 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001982 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001983 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001984 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001985
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 // No need to trylock() here as this function is executed in the binder thread serving a
1987 // particular client process: no risk to block the whole media server process or mixer
1988 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07001989 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001990 // keep local copy of index in case of client corruption b/32220769
1991 const uint32_t clientIndex = mCblk->clientIndex;
1992 const uint32_t serverIndex = mCblk->serverIndex;
1993 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1994 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001995 mCblk->serverIndex = 0;
1996 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001997 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001998 }
1999 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002000 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002001 for (uint32_t index = serverIndex; index < clientIndex;) {
2002 int *p = (int *)(mBuffer + index);
2003 const int size = *p++;
2004 if (size < 0
2005 || size > EFFECT_PARAM_BUFFER_SIZE
2006 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002007 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002008 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002009 break;
2010 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002011
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002012 std::copy(reinterpret_cast<const uint8_t*>(p),
2013 reinterpret_cast<const uint8_t*>(p) + size,
2014 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002015
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002016 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002017 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002018 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 sizeof(int),
2020 &replyBuffer);
2021 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002022
2023 // verify shared memory: server index shouldn't change; client index can't go back.
2024 if (serverIndex != mCblk->serverIndex
2025 || clientIndex > mCblk->clientIndex) {
2026 android_errorWriteLog(0x534e4554, "32220769");
2027 status = BAD_VALUE;
2028 break;
2029 }
2030
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 // stop at first error encountered
2032 if (ret != NO_ERROR) {
2033 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002034 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 break;
2036 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002037 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002038 break;
2039 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002040 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002041 }
2042 mCblk->serverIndex = 0;
2043 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002044 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002045 }
2046
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002047 status_t status = effect->command(cmdCode,
2048 cmdData,
2049 maxResponseSize,
2050 response);
2051 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002052}
2053
Andy Hungbd72c542023-06-20 18:56:17 -07002054void EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002055{
2056 ALOGV("setControl %p control %d", this, hasControl);
2057
2058 mHasControl = hasControl;
2059 mEnabled = enabled;
2060
2061 if (signal && mEffectClient != 0) {
2062 mEffectClient->controlStatusChanged(hasControl);
2063 }
2064}
2065
Andy Hungbd72c542023-06-20 18:56:17 -07002066void EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002067 const std::vector<uint8_t>& cmdData,
2068 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002069{
2070 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002071 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002072 }
2073}
2074
2075
2076
Andy Hungbd72c542023-06-20 18:56:17 -07002077void EffectHandle::setEnabled(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002078{
2079 if (mEffectClient != 0) {
2080 mEffectClient->enableStatusChanged(enabled);
2081 }
2082}
2083
Andy Hungbd72c542023-06-20 18:56:17 -07002084void EffectHandle::framesProcessed(int32_t frames) const
Eric Laurentde8caf42021-08-11 17:19:25 +02002085{
2086 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2087 mEffectClient->framesProcessed(frames);
2088 }
2089}
2090
Andy Hungbd72c542023-06-20 18:56:17 -07002091void EffectHandle::dumpToBuffer(char* buffer, size_t size) const
Andy Hung71ba4b32022-10-06 12:09:49 -07002092NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002093{
2094 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2095
Marco Nelissenb2208842014-02-07 14:00:50 -08002096 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002097 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002098 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002099 mHasControl ? "yes" : "no",
2100 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 mCblk ? mCblk->clientIndex : 0,
2102 mCblk ? mCblk->serverIndex : 0
2103 );
2104
2105 if (locked) {
2106 mCblk->lock.unlock();
2107 }
2108}
2109
2110#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07002111#define LOG_TAG "EffectChain"
Eric Laurentca7cc822012-11-19 14:55:58 -08002112
Andy Hungbd72c542023-06-20 18:56:17 -07002113/* static */
2114sp<IAfEffectChain> IAfEffectChain::create(
Andy Hung2cbc2722023-07-17 17:05:00 -07002115 const sp<IAfThreadBase>& thread,
Andy Hungbd72c542023-06-20 18:56:17 -07002116 audio_session_t sessionId)
2117{
Andy Hung2cbc2722023-07-17 17:05:00 -07002118 return sp<EffectChain>::make(thread, sessionId);
Andy Hungbd72c542023-06-20 18:56:17 -07002119}
2120
Andy Hung2cbc2722023-07-17 17:05:00 -07002121EffectChain::EffectChain(const sp<IAfThreadBase>& thread,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002122 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002123 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabineb5784e2023-08-24 21:10:44 +00002124 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002125 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002126 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002127{
Andy Hung2cbc2722023-07-17 17:05:00 -07002128 mStrategy = thread->getStrategyForStream(AUDIO_STREAM_MUSIC);
2129 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
2130 thread->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002131}
2132
Andy Hung44f27182023-07-06 20:56:16 -07002133// getEffectFromDesc_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002134sp<IAfEffectModule> EffectChain::getEffectFromDesc_l(
2135 effect_descriptor_t *descriptor) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002136{
2137 size_t size = mEffects.size();
2138
2139 for (size_t i = 0; i < size; i++) {
2140 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2141 return mEffects[i];
2142 }
2143 }
2144 return 0;
2145}
2146
Andy Hung44f27182023-07-06 20:56:16 -07002147// getEffectFromId_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002148sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002149{
2150 size_t size = mEffects.size();
2151
2152 for (size_t i = 0; i < size; i++) {
2153 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2154 if (id == 0 || mEffects[i]->id() == id) {
2155 return mEffects[i];
2156 }
2157 }
2158 return 0;
2159}
2160
Andy Hung44f27182023-07-06 20:56:16 -07002161// getEffectFromType_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002162sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2163 const effect_uuid_t *type) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002164{
2165 size_t size = mEffects.size();
2166
2167 for (size_t i = 0; i < size; i++) {
2168 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2169 return mEffects[i];
2170 }
2171 }
2172 return 0;
2173}
2174
Andy Hungbd72c542023-06-20 18:56:17 -07002175std::vector<int> EffectChain::getEffectIds() const
Eric Laurent6c796322019-04-09 14:13:17 -07002176{
2177 std::vector<int> ids;
2178 Mutex::Autolock _l(mLock);
2179 for (size_t i = 0; i < mEffects.size(); i++) {
2180 ids.push_back(mEffects[i]->id());
2181 }
2182 return ids;
2183}
2184
Andy Hungbd72c542023-06-20 18:56:17 -07002185void EffectChain::clearInputBuffer()
Eric Laurentca7cc822012-11-19 14:55:58 -08002186{
2187 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002188 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002189}
2190
2191// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002192void EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002193{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002194 if (mInBuffer == NULL) {
2195 return;
2196 }
Andy Hung319587b2023-05-23 14:01:03 -07002197 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002198 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002199
Eric Laurent6b446ce2019-12-13 10:56:31 -08002200 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002201 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002202}
2203
2204// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002205void EffectChain::process_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002206{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002207 // never process effects when:
2208 // - on an OFFLOAD thread
2209 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002210 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002211 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002212 bool tracksOnSession = (trackCnt() != 0);
2213
2214 if (!tracksOnSession && mTailBufferCount == 0) {
2215 doProcess = false;
2216 }
2217
2218 if (activeTrackCnt() == 0) {
2219 // if no track is active and the effect tail has not been rendered,
2220 // the input buffer must be cleared here as the mixer process will not do it
2221 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002222 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002223 if (mTailBufferCount > 0) {
2224 mTailBufferCount--;
2225 }
2226 }
2227 }
2228 }
2229
2230 size_t size = mEffects.size();
2231 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002232 // Only the input and output buffers of the chain can be external,
2233 // and 'update' / 'commit' do nothing for allocated buffers, thus
2234 // it's not needed to consider any other buffers here.
2235 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002236 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2237 mOutBuffer->update();
2238 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002239 for (size_t i = 0; i < size; i++) {
2240 mEffects[i]->process();
2241 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002242 mInBuffer->commit();
2243 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2244 mOutBuffer->commit();
2245 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002247 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002249 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2250 }
2251 if (doResetVolume) {
2252 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 }
2254}
2255
Andy Hung44f27182023-07-06 20:56:16 -07002256// createEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002257status_t EffectChain::createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002258 effect_descriptor_t *desc,
2259 int id,
2260 audio_session_t sessionId,
2261 bool pinned)
2262{
2263 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002264 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002265 status_t lStatus = effect->status();
2266 if (lStatus == NO_ERROR) {
2267 lStatus = addEffect_ll(effect);
2268 }
2269 if (lStatus != NO_ERROR) {
2270 effect.clear();
2271 }
2272 return lStatus;
2273}
2274
Andy Hung44f27182023-07-06 20:56:16 -07002275// addEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002276status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
Eric Laurentca7cc822012-11-19 14:55:58 -08002277{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002278 Mutex::Autolock _l(mLock);
2279 return addEffect_ll(effect);
2280}
Andy Hung44f27182023-07-06 20:56:16 -07002281// addEffect_l() must be called with IAfThreadBase::mLock and EffectChain::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002282status_t EffectChain::addEffect_ll(const sp<IAfEffectModule>& effect)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002283{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002284 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002285
Eric Laurentb62d0362021-10-26 17:40:18 +02002286 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002287 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2288 // Auxiliary effects are inserted at the beginning of mEffects vector as
2289 // they are processed first and accumulated in chain input buffer
2290 mEffects.insertAt(effect, 0);
2291
2292 // the input buffer for auxiliary effect contains mono samples in
2293 // 32 bit format. This is to avoid saturation in AudoMixer
2294 // accumulation stage. Saturation is done in EffectModule::process() before
2295 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002296 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002297 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002298
Eric Laurent6b446ce2019-12-13 10:56:31 -08002299 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002300 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002301 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002302
2303 effect->configure();
2304
Mikhail Naganov022b9952017-01-04 16:36:51 -08002305 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002306 // auxiliary effects output samples to chain input buffer for further processing
2307 // by insert effects
2308 effect->setOutBuffer(mInBuffer);
2309 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002310 ssize_t idx_insert = getInsertIndex(desc);
2311 if (idx_insert < 0) {
2312 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002313 }
2314
Eric Laurentb62d0362021-10-26 17:40:18 +02002315 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002316 mEffects.insertAt(effect, idx_insert);
2317
2318 effect->configure();
2319
Eric Laurentb62d0362021-10-26 17:40:18 +02002320 // - By default:
2321 // All effects read samples from chain input buffer.
2322 // The last effect in the chain, writes samples to chain output buffer,
2323 // otherwise to chain input buffer
2324 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2325 // The spatializer effect (first effect) reads samples from the input buffer
2326 // and writes samples to the output buffer.
2327 // All other effects read and writes samples to the output buffer
2328 if (mEffectCallback->isSpatializer()
2329 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002330 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002331 if (idx_insert == 0) {
2332 if (previousSize != 0) {
2333 mEffects[1]->configure();
2334 mEffects[1]->setInBuffer(mOutBuffer);
2335 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2336 }
2337 effect->setInBuffer(mInBuffer);
2338 } else {
2339 effect->setInBuffer(mOutBuffer);
2340 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002341 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002342 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002343 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002344 if (idx_insert != 0) {
2345 mEffects[idx_insert-1]->configure();
2346 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2347 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2348 }
2349 effect->setOutBuffer(mOutBuffer);
2350 } else {
2351 effect->setOutBuffer(mInBuffer);
2352 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002353 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002354 ALOGV("%s effect %p, added in chain %p at rank %zu",
2355 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002356 }
2357 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002358
Eric Laurentca7cc822012-11-19 14:55:58 -08002359 return NO_ERROR;
2360}
2361
Andy Hungbd72c542023-06-20 18:56:17 -07002362std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
jiabineb5784e2023-08-24 21:10:44 +00002363 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2364 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2365 return i - 1;
2366 }
2367 }
2368 return std::nullopt;
2369}
2370
Andy Hungbd72c542023-06-20 18:56:17 -07002371ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002372 // Insert effects are inserted at the end of mEffects vector as they are processed
2373 // after track and auxiliary effects.
2374 // Insert effect order as a function of indicated preference:
2375 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2376 // another effect is present
2377 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2378 // last effect claiming first position
2379 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2380 // first effect claiming last position
2381 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2382 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2383 // already present
2384 // Spatializer or Downmixer effects are inserted in first position because
2385 // they adapt the channel count for all other effects in the chain
2386 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2387 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2388 return 0;
2389 }
2390
2391 size_t size = mEffects.size();
2392 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2393 ssize_t idx_insert;
2394 ssize_t idx_insert_first = -1;
2395 ssize_t idx_insert_last = -1;
2396
2397 idx_insert = size;
2398 for (size_t i = 0; i < size; i++) {
2399 effect_descriptor_t d = mEffects[i]->desc();
2400 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2401 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2402 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2403 // check invalid effect chaining combinations
2404 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2405 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2406 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2407 __func__, desc.name, d.name);
2408 return -1;
2409 }
2410 // remember position of first insert effect and by default
2411 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002412 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002413 idx_insert = i;
2414 }
2415 // remember position of last insert effect claiming
2416 // first position
2417 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2418 idx_insert_first = i;
2419 }
2420 // remember position of first insert effect claiming
2421 // last position
2422 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2423 idx_insert_last == -1) {
2424 idx_insert_last = i;
2425 }
2426 }
2427 }
2428
2429 // modify idx_insert from first position if needed
2430 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2431 if (idx_insert_last != -1) {
2432 idx_insert = idx_insert_last;
2433 } else {
2434 idx_insert = size;
2435 }
2436 } else {
2437 if (idx_insert_first != -1) {
2438 idx_insert = idx_insert_first + 1;
2439 }
2440 }
2441 return idx_insert;
2442}
2443
Andy Hung44f27182023-07-06 20:56:16 -07002444// removeEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002445size_t EffectChain::removeEffect_l(const sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002446 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002447{
2448 Mutex::Autolock _l(mLock);
2449 size_t size = mEffects.size();
2450 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2451
2452 for (size_t i = 0; i < size; i++) {
2453 if (effect == mEffects[i]) {
2454 // calling stop here will remove pre-processing effect from the audio HAL.
2455 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2456 // the middle of a read from audio HAL
2457 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2458 mEffects[i]->state() == EffectModule::STOPPING) {
2459 mEffects[i]->stop();
2460 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002461 if (release) {
2462 mEffects[i]->release_l();
2463 }
2464
Mikhail Naganov022b9952017-01-04 16:36:51 -08002465 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002466 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002467 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002468 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002469 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002470 }
2471 }
2472 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002473
2474 // make sure the input buffer configuration for the new first effect in the chain
2475 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002476 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2477 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002478 mEffects[0]->configure();
2479 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002480 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002481 }
2482
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002483 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002484 this, i);
2485 break;
2486 }
2487 }
2488
2489 return mEffects.size();
2490}
2491
Andy Hung44f27182023-07-06 20:56:16 -07002492// setDevices_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002493void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002494{
2495 size_t size = mEffects.size();
2496 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002497 mEffects[i]->setDevices(devices);
2498 }
2499}
2500
Andy Hung44f27182023-07-06 20:56:16 -07002501// setInputDevice_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002502void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08002503{
2504 size_t size = mEffects.size();
2505 for (size_t i = 0; i < size; i++) {
2506 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002507 }
2508}
2509
Andy Hung44f27182023-07-06 20:56:16 -07002510// setMode_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002511void EffectChain::setMode_l(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08002512{
2513 size_t size = mEffects.size();
2514 for (size_t i = 0; i < size; i++) {
2515 mEffects[i]->setMode(mode);
2516 }
2517}
2518
Andy Hung44f27182023-07-06 20:56:16 -07002519// setAudioSource_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002520void EffectChain::setAudioSource_l(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08002521{
2522 size_t size = mEffects.size();
2523 for (size_t i = 0; i < size; i++) {
2524 mEffects[i]->setAudioSource(source);
2525 }
2526}
2527
Andy Hungbd72c542023-06-20 18:56:17 -07002528bool EffectChain::hasVolumeControlEnabled_l() const {
Zhou Songd505c642020-02-20 16:35:37 +08002529 for (const auto &effect : mEffects) {
2530 if (effect->isVolumeControlEnabled()) return true;
2531 }
2532 return false;
2533}
2534
Andy Hung44f27182023-07-06 20:56:16 -07002535// setVolume_l() must be called with IAfThreadBase::mLock or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002536bool EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002537{
2538 uint32_t newLeft = *left;
2539 uint32_t newRight = *right;
jiabineb5784e2023-08-24 21:10:44 +00002540 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002541
2542 // first update volume controller
jiabineb5784e2023-08-24 21:10:44 +00002543 const auto volumeControlIndex = findVolumeControl_l(0, size);
2544 const int ctrlIdx = volumeControlIndex.value_or(-1);
Andy Hungbd72c542023-06-20 18:56:17 -07002545 const sp<IAfEffectModule> volumeControlEffect =
jiabineb5784e2023-08-24 21:10:44 +00002546 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
Andy Hungbd72c542023-06-20 18:56:17 -07002547 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002548
jiabineb5784e2023-08-24 21:10:44 +00002549 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002550 *left == mLeftVolume && *right == mRightVolume) {
jiabineb5784e2023-08-24 21:10:44 +00002551 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002552 *left = mNewLeftVolume;
2553 *right = mNewRightVolume;
2554 }
jiabineb5784e2023-08-24 21:10:44 +00002555 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002556 }
2557
jiabineb5784e2023-08-24 21:10:44 +00002558 if (volumeControlEffect != cachedVolumeControlEffect) {
2559 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2560 // as zero for safe ramping.
2561 if (cachedVolumeControlEffect != nullptr) {
2562 uint32_t leftMax = 1 << 24;
2563 uint32_t rightMax = 1 << 24;
2564 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2565 }
2566 if (volumeControlEffect != nullptr) {
2567 uint32_t leftZero = 0;
2568 uint32_t rightZero = 0;
2569 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2570 }
2571 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002572 mLeftVolume = newLeft;
2573 mRightVolume = newRight;
2574
2575 // second get volume update from volume controller
2576 if (ctrlIdx >= 0) {
2577 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2578 mNewLeftVolume = newLeft;
2579 mNewRightVolume = newRight;
2580 }
2581 // then indicate volume to all other effects in chain.
2582 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002583 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002584 uint32_t lVol = newLeft;
2585 uint32_t rVol = newRight;
2586
2587 for (size_t i = 0; i < size; i++) {
2588 if ((int)i == ctrlIdx) {
2589 continue;
2590 }
2591 // this also works for ctrlIdx == -1 when there is no volume controller
2592 if ((int)i > ctrlIdx) {
2593 lVol = *left;
2594 rVol = *right;
2595 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002596 // Pass requested volume directly if this is volume monitor module
2597 if (mEffects[i]->isVolumeMonitor()) {
2598 mEffects[i]->setVolume(left, right, false);
2599 } else {
2600 mEffects[i]->setVolume(&lVol, &rVol, false);
2601 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002602 }
2603 *left = newLeft;
2604 *right = newRight;
2605
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002606 setVolumeForOutput_l(*left, *right);
2607
jiabineb5784e2023-08-24 21:10:44 +00002608 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002609}
2610
Andy Hung44f27182023-07-06 20:56:16 -07002611// resetVolume_l() must be called with IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002612void EffectChain::resetVolume_l()
Eric Laurentfa1e1232016-08-02 19:01:49 -07002613{
Eric Laurente7449bf2016-08-03 18:44:07 -07002614 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2615 uint32_t left = mLeftVolume;
2616 uint32_t right = mRightVolume;
2617 (void)setVolume_l(&left, &right, true);
2618 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002619}
2620
Andy Hungbd72c542023-06-20 18:56:17 -07002621// containsHapticGeneratingEffect_l must be called with
Andy Hung44f27182023-07-06 20:56:16 -07002622// IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002623bool EffectChain::containsHapticGeneratingEffect_l()
jiabineb3bda02020-06-30 14:07:03 -07002624{
2625 for (size_t i = 0; i < mEffects.size(); ++i) {
2626 if (mEffects[i]->isHapticGenerator()) {
2627 return true;
2628 }
2629 }
2630 return false;
2631}
2632
Andy Hungbd72c542023-06-20 18:56:17 -07002633void EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002634{
2635 Mutex::Autolock _l(mLock);
2636 for (size_t i = 0; i < mEffects.size(); ++i) {
2637 mEffects[i]->setHapticIntensity(id, intensity);
2638 }
2639}
2640
Andy Hungbd72c542023-06-20 18:56:17 -07002641void EffectChain::syncHalEffectsState()
Eric Laurent1b928682014-10-02 19:41:47 -07002642{
2643 Mutex::Autolock _l(mLock);
2644 for (size_t i = 0; i < mEffects.size(); i++) {
2645 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2646 mEffects[i]->state() == EffectModule::STOPPING) {
2647 mEffects[i]->addEffectToHal_l();
2648 }
2649 }
2650}
2651
Andy Hungbd72c542023-06-20 18:56:17 -07002652void EffectChain::dump(int fd, const Vector<String16>& args) const
Andy Hung71ba4b32022-10-06 12:09:49 -07002653NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002654{
Eric Laurentca7cc822012-11-19 14:55:58 -08002655 String8 result;
2656
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002657 const size_t numEffects = mEffects.size();
2658 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002659
Marco Nelissenb2208842014-02-07 14:00:50 -08002660 if (numEffects) {
2661 bool locked = AudioFlinger::dumpTryLock(mLock);
2662 // failed to lock - AudioFlinger is probably deadlocked
2663 if (!locked) {
2664 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002665 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002666
Andy Hungbded9c82017-11-30 18:47:35 -08002667 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2668 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2669 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2670 (int)inBufferStr.size(), "In buffer ",
2671 (int)outBufferStr.size(), "Out buffer ");
2672 result.appendFormat("\t%s %s %d\n",
2673 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002674 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002675
2676 for (size_t i = 0; i < numEffects; ++i) {
Andy Hungbd72c542023-06-20 18:56:17 -07002677 sp<IAfEffectModule> effect = mEffects[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08002678 if (effect != 0) {
2679 effect->dump(fd, args);
2680 }
2681 }
2682
2683 if (locked) {
2684 mLock.unlock();
2685 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002686 } else {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002687 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002688 }
2689}
2690
Andy Hung44f27182023-07-06 20:56:16 -07002691// must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002692void EffectChain::setEffectSuspended_l(
Eric Laurentca7cc822012-11-19 14:55:58 -08002693 const effect_uuid_t *type, bool suspend)
2694{
2695 sp<SuspendedEffectDesc> desc;
2696 // use effect type UUID timelow as key as there is no real risk of identical
2697 // timeLow fields among effect type UUIDs.
2698 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2699 if (suspend) {
2700 if (index >= 0) {
2701 desc = mSuspendedEffects.valueAt(index);
2702 } else {
2703 desc = new SuspendedEffectDesc();
2704 desc->mType = *type;
2705 mSuspendedEffects.add(type->timeLow, desc);
2706 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2707 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002708
Eric Laurentca7cc822012-11-19 14:55:58 -08002709 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002710 sp<IAfEffectModule> effect = getEffectIfEnabled(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002711 if (effect != 0) {
2712 desc->mEffect = effect;
2713 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002714 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002715 }
2716 }
2717 } else {
2718 if (index < 0) {
2719 return;
2720 }
2721 desc = mSuspendedEffects.valueAt(index);
2722 if (desc->mRefCount <= 0) {
2723 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002724 desc->mRefCount = 0;
2725 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002726 }
2727 if (--desc->mRefCount == 0) {
2728 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2729 if (desc->mEffect != 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002730 sp<IAfEffectModule> effect = desc->mEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002731 if (effect != 0) {
2732 effect->setSuspended(false);
2733 effect->lock();
Andy Hungbd72c542023-06-20 18:56:17 -07002734 IAfEffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002735 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002736 effect->setEnabled_l(handle->enabled());
2737 }
2738 effect->unlock();
2739 }
2740 desc->mEffect.clear();
2741 }
2742 mSuspendedEffects.removeItemsAt(index);
2743 }
2744 }
2745}
2746
Andy Hung44f27182023-07-06 20:56:16 -07002747// must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002748void EffectChain::setEffectSuspendedAll_l(bool suspend)
Eric Laurentca7cc822012-11-19 14:55:58 -08002749{
2750 sp<SuspendedEffectDesc> desc;
2751
2752 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2753 if (suspend) {
2754 if (index >= 0) {
2755 desc = mSuspendedEffects.valueAt(index);
2756 } else {
2757 desc = new SuspendedEffectDesc();
2758 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2759 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2760 }
2761 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002762 Vector< sp<IAfEffectModule> > effects;
Eric Laurentca7cc822012-11-19 14:55:58 -08002763 getSuspendEligibleEffects(effects);
2764 for (size_t i = 0; i < effects.size(); i++) {
2765 setEffectSuspended_l(&effects[i]->desc().type, true);
2766 }
2767 }
2768 } else {
2769 if (index < 0) {
2770 return;
2771 }
2772 desc = mSuspendedEffects.valueAt(index);
2773 if (desc->mRefCount <= 0) {
2774 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2775 desc->mRefCount = 1;
2776 }
2777 if (--desc->mRefCount == 0) {
2778 Vector<const effect_uuid_t *> types;
2779 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2780 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2781 continue;
2782 }
2783 types.add(&mSuspendedEffects.valueAt(i)->mType);
2784 }
2785 for (size_t i = 0; i < types.size(); i++) {
2786 setEffectSuspended_l(types[i], false);
2787 }
2788 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2789 mSuspendedEffects.keyAt(index));
2790 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2791 }
2792 }
2793}
2794
2795
2796// The volume effect is used for automated tests only
2797#ifndef OPENSL_ES_H_
2798static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2799 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2800const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2801#endif //OPENSL_ES_H_
2802
Eric Laurentd8365c52017-07-16 15:27:05 -07002803/* static */
Andy Hungbd72c542023-06-20 18:56:17 -07002804bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
Eric Laurentd8365c52017-07-16 15:27:05 -07002805{
2806 // Only NS and AEC are suspended when BtNRec is off
2807 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2808 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2809 return true;
2810 }
2811 return false;
2812}
2813
Andy Hungbd72c542023-06-20 18:56:17 -07002814bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
Eric Laurentca7cc822012-11-19 14:55:58 -08002815{
2816 // auxiliary effects and visualizer are never suspended on output mix
2817 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2818 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2819 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002820 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2821 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002822 return false;
2823 }
2824 return true;
2825}
2826
Andy Hungbd72c542023-06-20 18:56:17 -07002827void EffectChain::getSuspendEligibleEffects(
2828 Vector< sp<IAfEffectModule> > &effects)
Eric Laurentca7cc822012-11-19 14:55:58 -08002829{
2830 effects.clear();
2831 for (size_t i = 0; i < mEffects.size(); i++) {
2832 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2833 effects.add(mEffects[i]);
2834 }
2835 }
2836}
2837
Andy Hungbd72c542023-06-20 18:56:17 -07002838sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
Eric Laurentca7cc822012-11-19 14:55:58 -08002839{
Andy Hungbd72c542023-06-20 18:56:17 -07002840 sp<IAfEffectModule> effect = getEffectFromType_l(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002841 return effect != 0 && effect->isEnabled() ? effect : 0;
2842}
2843
Andy Hungbd72c542023-06-20 18:56:17 -07002844void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08002845 bool enabled)
2846{
2847 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2848 if (enabled) {
2849 if (index < 0) {
2850 // if the effect is not suspend check if all effects are suspended
2851 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2852 if (index < 0) {
2853 return;
2854 }
2855 if (!isEffectEligibleForSuspend(effect->desc())) {
2856 return;
2857 }
2858 setEffectSuspended_l(&effect->desc().type, enabled);
2859 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2860 if (index < 0) {
2861 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2862 return;
2863 }
2864 }
2865 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2866 effect->desc().type.timeLow);
2867 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002868 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002869 if (desc->mEffect == 0) {
2870 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002871 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002872 effect->setSuspended(true);
2873 }
2874 } else {
2875 if (index < 0) {
2876 return;
2877 }
2878 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2879 effect->desc().type.timeLow);
2880 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2881 desc->mEffect.clear();
2882 effect->setSuspended(false);
2883 }
2884}
2885
Andy Hungbd72c542023-06-20 18:56:17 -07002886bool EffectChain::isNonOffloadableEnabled() const
Eric Laurent813e2a72013-08-31 12:59:48 -07002887{
2888 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002889 return isNonOffloadableEnabled_l();
2890}
2891
Andy Hungbd72c542023-06-20 18:56:17 -07002892bool EffectChain::isNonOffloadableEnabled_l() const
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002893{
Eric Laurent813e2a72013-08-31 12:59:48 -07002894 size_t size = mEffects.size();
2895 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002896 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002897 return true;
2898 }
2899 }
2900 return false;
2901}
2902
Andy Hung44f27182023-07-06 20:56:16 -07002903void EffectChain::setThread(const sp<IAfThreadBase>& thread)
Eric Laurentaaa44472014-09-12 17:41:50 -07002904{
2905 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002906 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002907}
2908
Andy Hungbd72c542023-06-20 18:56:17 -07002909void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002910{
2911 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2912 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2913 }
2914 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2915 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2916 }
jiabinc658e452022-10-21 20:52:21 +00002917 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2918 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2919 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002920}
2921
Andy Hungbd72c542023-06-20 18:56:17 -07002922void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002923{
2924 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2925 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2926 }
2927 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2928 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2929 }
2930}
2931
Andy Hungbd72c542023-06-20 18:56:17 -07002932bool EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002933{
2934 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002935 for (const auto &effect : mEffects) {
2936 if (effect->isProcessImplemented()) {
2937 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002938 }
2939 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002940 // Allow effects without processing.
2941 return true;
2942}
2943
Andy Hungbd72c542023-06-20 18:56:17 -07002944bool EffectChain::isFastCompatible() const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002945{
2946 Mutex::Autolock _l(mLock);
2947 for (const auto &effect : mEffects) {
2948 if (effect->isProcessImplemented()
2949 && effect->isImplementationSoftware()) {
2950 return false;
2951 }
2952 }
2953 // Allow effects without processing or hw accelerated effects.
2954 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002955}
2956
Andy Hungbd72c542023-06-20 18:56:17 -07002957bool EffectChain::isBitPerfectCompatible() const {
jiabinc658e452022-10-21 20:52:21 +00002958 Mutex::Autolock _l(mLock);
2959 for (const auto &effect : mEffects) {
2960 if (effect->isProcessImplemented()
2961 && effect->isImplementationSoftware()) {
2962 return false;
2963 }
2964 }
2965 // Allow effects without processing or hw accelerated effects.
2966 return true;
2967}
2968
Eric Laurent4c415062016-06-17 16:14:16 -07002969// isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung44f27182023-07-06 20:56:16 -07002970bool EffectChain::isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const
Eric Laurent4c415062016-06-17 16:14:16 -07002971{
2972 Mutex::Autolock _l(mLock);
2973 for (size_t i = 0; i < mEffects.size(); i++) {
2974 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2975 return false;
2976 }
2977 }
2978 return true;
2979}
2980
Eric Laurent6b446ce2019-12-13 10:56:31 -08002981// EffectCallbackInterface implementation
Andy Hungbd72c542023-06-20 18:56:17 -07002982status_t EffectChain::EffectCallback::createEffectHal(
Eric Laurent6b446ce2019-12-13 10:56:31 -08002983 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2984 sp<EffectHalInterface> *effect) {
2985 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002986 const sp<EffectsFactoryHalInterface> effectsFactory =
2987 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002988 if (effectsFactory != 0) {
2989 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2990 }
2991 return status;
2992}
2993
Andy Hungbd72c542023-06-20 18:56:17 -07002994bool EffectChain::EffectCallback::updateOrphanEffectChains(
2995 const sp<IAfEffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002996 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung2cbc2722023-07-17 17:05:00 -07002997 return mAfThreadCallback->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002998}
2999
Andy Hungbd72c542023-06-20 18:56:17 -07003000status_t EffectChain::EffectCallback::allocateHalBuffer(
Eric Laurent6b446ce2019-12-13 10:56:31 -08003001 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung2cbc2722023-07-17 17:05:00 -07003002 return mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003003}
3004
Andy Hungbd72c542023-06-20 18:56:17 -07003005status_t EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003006 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003007 status_t result = NO_INIT;
Andy Hung44f27182023-07-06 20:56:16 -07003008 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003009 if (t == nullptr) {
3010 return result;
3011 }
3012 sp <StreamHalInterface> st = t->stream();
3013 if (st == nullptr) {
3014 return result;
3015 }
3016 result = st->addEffect(effect);
3017 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3018 return result;
3019}
3020
Andy Hungbd72c542023-06-20 18:56:17 -07003021status_t EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003022 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003023 status_t result = NO_INIT;
Andy Hung44f27182023-07-06 20:56:16 -07003024 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003025 if (t == nullptr) {
3026 return result;
3027 }
3028 sp <StreamHalInterface> st = t->stream();
3029 if (st == nullptr) {
3030 return result;
3031 }
3032 result = st->removeEffect(effect);
3033 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3034 return result;
3035}
3036
Andy Hungbd72c542023-06-20 18:56:17 -07003037audio_io_handle_t EffectChain::EffectCallback::io() const {
Andy Hung44f27182023-07-06 20:56:16 -07003038 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003039 if (t == nullptr) {
3040 return AUDIO_IO_HANDLE_NONE;
3041 }
3042 return t->id();
3043}
3044
Andy Hungbd72c542023-06-20 18:56:17 -07003045bool EffectChain::EffectCallback::isOutput() const {
Andy Hung44f27182023-07-06 20:56:16 -07003046 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003047 if (t == nullptr) {
3048 return true;
3049 }
3050 return t->isOutput();
3051}
3052
Andy Hungbd72c542023-06-20 18:56:17 -07003053bool EffectChain::EffectCallback::isOffload() const {
Andy Hung44f27182023-07-06 20:56:16 -07003054 return mThreadType == IAfThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003055}
3056
Andy Hungbd72c542023-06-20 18:56:17 -07003057bool EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung44f27182023-07-06 20:56:16 -07003058 return mThreadType == IAfThreadBase::OFFLOAD
3059 || mThreadType == IAfThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003060}
3061
Andy Hungbd72c542023-06-20 18:56:17 -07003062bool EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003063 switch (mThreadType) {
Andy Hung44f27182023-07-06 20:56:16 -07003064 case IAfThreadBase::OFFLOAD:
3065 case IAfThreadBase::MMAP_PLAYBACK:
3066 case IAfThreadBase::MMAP_CAPTURE:
Eric Laurentb62d0362021-10-26 17:40:18 +02003067 return true;
3068 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069 return false;
3070 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003071}
3072
Andy Hungbd72c542023-06-20 18:56:17 -07003073bool EffectChain::EffectCallback::isSpatializer() const {
Andy Hung44f27182023-07-06 20:56:16 -07003074 return mThreadType == IAfThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003075}
3076
Andy Hungbd72c542023-06-20 18:56:17 -07003077uint32_t EffectChain::EffectCallback::sampleRate() const {
Andy Hung44f27182023-07-06 20:56:16 -07003078 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003079 if (t == nullptr) {
3080 return 0;
3081 }
3082 return t->sampleRate();
3083}
3084
Andy Hungbd72c542023-06-20 18:56:17 -07003085audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const {
Andy Hung44f27182023-07-06 20:56:16 -07003086 const sp<IAfThreadBase> t = thread().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003087 if (t == nullptr) {
3088 return AUDIO_CHANNEL_NONE;
3089 }
Andy Hungbd72c542023-06-20 18:56:17 -07003090 sp<IAfEffectChain> c = chain().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003091 if (c == nullptr) {
3092 return AUDIO_CHANNEL_NONE;
3093 }
3094
Andy Hung44f27182023-07-06 20:56:16 -07003095 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003096 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3097 if (c->isFirstEffect(id)) {
3098 return t->mixerChannelMask();
3099 } else {
3100 return t->channelMask();
3101 }
3102 } else if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003103 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung44f27182023-07-06 20:56:16 -07003104 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003105 return t->mixerChannelMask();
3106 } else {
3107 return t->channelMask();
3108 }
3109 } else {
3110 return t->channelMask();
3111 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003112 } else {
3113 return t->channelMask();
3114 }
3115}
3116
Andy Hungbd72c542023-06-20 18:56:17 -07003117uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003118 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003119}
3120
Andy Hungbd72c542023-06-20 18:56:17 -07003121audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const {
Andy Hung44f27182023-07-06 20:56:16 -07003122 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003123 if (t == nullptr) {
3124 return AUDIO_CHANNEL_NONE;
3125 }
Andy Hungbd72c542023-06-20 18:56:17 -07003126 sp<IAfEffectChain> c = chain().promote();
Eric Laurentb62d0362021-10-26 17:40:18 +02003127 if (c == nullptr) {
3128 return AUDIO_CHANNEL_NONE;
3129 }
3130
Andy Hung44f27182023-07-06 20:56:16 -07003131 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003132 if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003133 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung44f27182023-07-06 20:56:16 -07003134 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003135 return t->mixerChannelMask();
3136 } else {
3137 return t->channelMask();
3138 }
3139 } else {
3140 return t->channelMask();
3141 }
3142 } else {
3143 return t->channelMask();
3144 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003145}
3146
Andy Hungbd72c542023-06-20 18:56:17 -07003147uint32_t EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003148 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003149}
3150
Andy Hungbd72c542023-06-20 18:56:17 -07003151audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung44f27182023-07-06 20:56:16 -07003152 const sp<IAfThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003153 if (t == nullptr) {
3154 return AUDIO_CHANNEL_NONE;
3155 }
3156 return t->hapticChannelMask();
3157}
3158
Andy Hungbd72c542023-06-20 18:56:17 -07003159size_t EffectChain::EffectCallback::frameCount() const {
Andy Hung44f27182023-07-06 20:56:16 -07003160 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003161 if (t == nullptr) {
3162 return 0;
3163 }
3164 return t->frameCount();
3165}
3166
Andy Hungbd72c542023-06-20 18:56:17 -07003167uint32_t EffectChain::EffectCallback::latency() const
Andy Hung71ba4b32022-10-06 12:09:49 -07003168NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3169{
Andy Hung44f27182023-07-06 20:56:16 -07003170 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171 if (t == nullptr) {
3172 return 0;
3173 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003174 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003175 return t->latency_l();
3176}
3177
Andy Hungbd72c542023-06-20 18:56:17 -07003178void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003179NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3180{
Andy Hung44f27182023-07-06 20:56:16 -07003181 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003182 if (t == nullptr) {
3183 return;
3184 }
3185 t->setVolumeForOutput_l(left, right);
3186}
3187
Andy Hungbd72c542023-06-20 18:56:17 -07003188void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
3189 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung44f27182023-07-06 20:56:16 -07003190 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003191 if (t == nullptr) {
3192 return;
3193 }
3194 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3195
Andy Hungbd72c542023-06-20 18:56:17 -07003196 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003197 if (c == nullptr) {
3198 return;
3199 }
Eric Laurent41709552019-12-16 19:34:05 -08003200 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3201 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003202}
3203
Andy Hungbd72c542023-06-20 18:56:17 -07003204void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
Andy Hung44f27182023-07-06 20:56:16 -07003205 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003206 if (t == nullptr) {
3207 return;
3208 }
Eric Laurent41709552019-12-16 19:34:05 -08003209 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3210 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003211}
3212
Andy Hungbd72c542023-06-20 18:56:17 -07003213void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003214 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3215
Andy Hung44f27182023-07-06 20:56:16 -07003216 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003217 if (t == nullptr) {
3218 return;
3219 }
3220 t->onEffectDisable();
3221}
3222
Andy Hungbd72c542023-06-20 18:56:17 -07003223bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
Eric Laurent6b446ce2019-12-13 10:56:31 -08003224 bool unpinIfLast) {
Andy Hung44f27182023-07-06 20:56:16 -07003225 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003226 if (t == nullptr) {
3227 return false;
3228 }
3229 t->disconnectEffectHandle(handle, unpinIfLast);
3230 return true;
3231}
3232
Andy Hungbd72c542023-06-20 18:56:17 -07003233void EffectChain::EffectCallback::resetVolume() {
3234 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003235 if (c == nullptr) {
3236 return;
3237 }
3238 c->resetVolume_l();
3239
3240}
3241
Andy Hungbd72c542023-06-20 18:56:17 -07003242product_strategy_t EffectChain::EffectCallback::strategy() const {
3243 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003244 if (c == nullptr) {
3245 return PRODUCT_STRATEGY_NONE;
3246 }
3247 return c->strategy();
3248}
3249
Andy Hungbd72c542023-06-20 18:56:17 -07003250int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3251 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003252 if (c == nullptr) {
3253 return 0;
3254 }
3255 return c->activeTrackCnt();
3256}
3257
Eric Laurentb82e6b72019-11-22 17:25:04 -08003258
3259#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003260#define LOG_TAG "DeviceEffectProxy"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003261
Andy Hungbd72c542023-06-20 18:56:17 -07003262/* static */
3263sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3264 const AudioDeviceTypeAddr& device,
Andy Hungbf766942023-07-13 19:54:47 -07003265 const sp<DeviceEffectManagerCallback>& callback,
Andy Hungbd72c542023-06-20 18:56:17 -07003266 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3267{
3268 return sp<DeviceEffectProxy>::make(device,
Andy Hungbf766942023-07-13 19:54:47 -07003269 callback,
Andy Hungbd72c542023-06-20 18:56:17 -07003270 desc, id, notifyFramesProcessed);
3271}
3272
3273status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003274{
3275 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3276 Mutex::Autolock _l(mProxyLock);
3277 if (status == NO_ERROR) {
3278 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003279 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003280 if (enabled) {
Andy Hungbd72c542023-06-20 18:56:17 -07003281 bs = handle.second->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003282 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003283 bs = handle.second->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003284 }
3285 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003286 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003287 }
3288 }
3289 }
3290 ALOGV("%s enable %d status %d", __func__, enabled, status);
3291 return status;
3292}
3293
Andy Hungbd72c542023-06-20 18:56:17 -07003294status_t DeviceEffectProxy::init(
Andy Hung07434ef2023-07-13 18:11:33 -07003295 const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003296//For all audio patches
3297//If src or sink device match
3298//If the effect is HW accelerated
3299// if no corresponding effect module
3300// Create EffectModule: mHalEffect
3301//Create and attach EffectHandle
3302//If the effect is not HW accelerated and the patch sink or src is a mixer port
3303// Create Effect on patch input or output thread on session -1
3304//Add EffectHandle to EffectHandle map of Effect Proxy:
3305 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3306 status_t status = NO_ERROR;
3307 for (auto &patch : patches) {
3308 status = onCreatePatch(patch.first, patch.second);
3309 ALOGV("%s onCreatePatch status %d", __func__, status);
3310 if (status == BAD_VALUE) {
3311 return status;
3312 }
3313 }
3314 return status;
3315}
3316
Andy Hungbd72c542023-06-20 18:56:17 -07003317status_t DeviceEffectProxy::onCreatePatch(
Andy Hung07434ef2023-07-13 18:11:33 -07003318 audio_patch_handle_t patchHandle, const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003319 status_t status = NAME_NOT_FOUND;
Andy Hungbd72c542023-06-20 18:56:17 -07003320 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003321 // only consider source[0] as this is the only "true" source of a patch
3322 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3323 ALOGV("%s source checkPort status %d", __func__, status);
3324 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3325 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3326 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3327 }
3328 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3329 Mutex::Autolock _l(mProxyLock);
3330 mEffectHandles.emplace(patchHandle, handle);
3331 }
3332 ALOGW_IF(status == BAD_VALUE,
3333 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3334
3335 return status;
3336}
3337
Andy Hung07434ef2023-07-13 18:11:33 -07003338status_t DeviceEffectProxy::checkPort(const IAfPatchPanel::Patch& patch,
Andy Hungbd72c542023-06-20 18:56:17 -07003339 const struct audio_port_config *port, sp<IAfEffectHandle> *handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003340
3341 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3342 __func__, port->type, port->ext.device.type,
3343 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003344 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3345 port->ext.device.address != mDevice.address()) {
3346 return NAME_NOT_FOUND;
3347 }
3348 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3349 (audio_port_config_has_input_direction(port))) {
3350 ALOGI("%s don't create postprocessing effect on record port", __func__);
3351 return NAME_NOT_FOUND;
3352 }
3353 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3354 (!audio_port_config_has_input_direction(port))) {
3355 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003356 return NAME_NOT_FOUND;
3357 }
3358 status_t status = NAME_NOT_FOUND;
3359
3360 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3361 Mutex::Autolock _l(mProxyLock);
3362 mDevicePort = *port;
3363 mHalEffect = new EffectModule(mMyCallback,
3364 const_cast<effect_descriptor_t *>(&mDescriptor),
3365 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3366 false /* pinned */, port->id);
3367 if (audio_is_input_device(mDevice.mType)) {
3368 mHalEffect->setInputDevice(mDevice);
3369 } else {
3370 mHalEffect->setDevices({mDevice});
3371 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003372 mHalEffect->configure();
3373
Eric Laurentde8caf42021-08-11 17:19:25 +02003374 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3375 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003376 status = (*handle)->initCheck();
3377 if (status == OK) {
3378 status = mHalEffect->addHandle((*handle).get());
3379 } else {
3380 mHalEffect.clear();
3381 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3382 }
3383 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07003384 sp<IAfThreadBase> thread;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003385 if (audio_port_config_has_input_direction(port)) {
3386 if (patch.isSoftware()) {
3387 thread = patch.mRecord.thread();
3388 } else {
3389 thread = patch.thread().promote();
3390 }
3391 } else {
3392 if (patch.isSoftware()) {
3393 thread = patch.mPlayback.thread();
3394 } else {
3395 thread = patch.thread().promote();
3396 }
3397 }
3398 int enabled;
3399 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3400 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003401 &enabled, &status, false, false /*probe*/,
3402 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003403 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3404 } else {
3405 status = BAD_VALUE;
3406 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003407
Eric Laurentb82e6b72019-11-22 17:25:04 -08003408 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003409 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003410 if (isEnabled()) {
Andy Hungbd72c542023-06-20 18:56:17 -07003411 bs = (*handle)->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003412 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003413 bs = (*handle)->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003414 }
3415 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003416 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003417 }
3418 }
3419 return status;
3420}
3421
Andy Hungbd72c542023-06-20 18:56:17 -07003422void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3423 sp<IAfEffectHandle> effect;
Eric Laurent76c89f32021-12-03 17:13:23 +01003424 {
3425 Mutex::Autolock _l(mProxyLock);
3426 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3427 effect = mEffectHandles.at(patchHandle);
3428 mEffectHandles.erase(patchHandle);
3429 }
3430 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003431}
3432
3433
Andy Hungbd72c542023-06-20 18:56:17 -07003434size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003435{
3436 Mutex::Autolock _l(mProxyLock);
3437 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003438 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003439 mHalEffect.clear();
3440 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3441 }
3442 return mHalEffect == nullptr ? 0 : 1;
3443}
3444
Andy Hungbd72c542023-06-20 18:56:17 -07003445status_t DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003446 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003447 if (mHalEffect == nullptr) {
3448 return NO_INIT;
3449 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003450 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003451}
3452
Andy Hungbd72c542023-06-20 18:56:17 -07003453status_t DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003454 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003455 if (mHalEffect == nullptr) {
3456 return NO_INIT;
3457 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003458 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003459}
3460
Andy Hungbd72c542023-06-20 18:56:17 -07003461bool DeviceEffectProxy::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003462 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3463 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3464 }
3465 return true;
3466}
3467
Andy Hungbd72c542023-06-20 18:56:17 -07003468uint32_t DeviceEffectProxy::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003469 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3470 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3471 return mDevicePort.sample_rate;
3472 }
3473 return DEFAULT_OUTPUT_SAMPLE_RATE;
3474}
3475
Andy Hungbd72c542023-06-20 18:56:17 -07003476audio_channel_mask_t DeviceEffectProxy::channelMask() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003477 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3478 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3479 return mDevicePort.channel_mask;
3480 }
3481 return AUDIO_CHANNEL_OUT_STEREO;
3482}
3483
Andy Hungbd72c542023-06-20 18:56:17 -07003484uint32_t DeviceEffectProxy::channelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003485 if (isOutput()) {
3486 return audio_channel_count_from_out_mask(channelMask());
3487 }
3488 return audio_channel_count_from_in_mask(channelMask());
3489}
3490
Andy Hungbd72c542023-06-20 18:56:17 -07003491void DeviceEffectProxy::dump2(int fd, int spaces) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003492NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3493{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003494 const Vector<String16> args;
3495 EffectBase::dump(fd, args);
3496
Andy Hungbd72c542023-06-20 18:56:17 -07003497 const bool locked = AudioFlinger::dumpTryLock(mProxyLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003498 if (!locked) {
3499 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003500 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003501 }
3502
3503 String8 outStr;
3504 if (mHalEffect != nullptr) {
3505 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3506 } else {
3507 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3508 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003509 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003510 outStr.clear();
3511
3512 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003513 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003514 outStr.clear();
3515
3516 for (const auto& iter : mEffectHandles) {
3517 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003518 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003519 outStr.clear();
Andy Hungbd72c542023-06-20 18:56:17 -07003520 sp<IAfEffectBase> effect = iter.second->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003521 if (effect != nullptr) {
3522 effect->dump(fd, args);
3523 }
3524 }
3525
3526 if (locked) {
3527 mLock.unlock();
3528 }
3529}
3530
3531#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003532#define LOG_TAG "DeviceEffectProxy::ProxyCallback"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003533
Andy Hungbd72c542023-06-20 18:56:17 -07003534int DeviceEffectProxy::ProxyCallback::newEffectId() {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003535 return mManagerCallback->newEffectId();
3536}
3537
3538
Andy Hungbd72c542023-06-20 18:56:17 -07003539bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3540 IAfEffectHandle *handle, bool unpinIfLast) {
3541 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003542 if (effectBase == nullptr) {
3543 return false;
3544 }
3545
Andy Hungbd72c542023-06-20 18:56:17 -07003546 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003547 if (effect == nullptr) {
3548 return false;
3549 }
3550
3551 // restore suspended effects if the disconnected handle was enabled and the last one.
3552 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3553 if (remove) {
3554 sp<DeviceEffectProxy> proxy = mProxy.promote();
3555 if (proxy != nullptr) {
3556 proxy->removeEffect(effect);
3557 }
3558 if (handle->enabled()) {
3559 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3560 }
3561 }
3562 return true;
3563}
3564
Andy Hungbd72c542023-06-20 18:56:17 -07003565status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003566 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3567 sp<EffectHalInterface> *effect) {
3568 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3569}
3570
Andy Hungbd72c542023-06-20 18:56:17 -07003571status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003572 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003573 sp<DeviceEffectProxy> proxy = mProxy.promote();
3574 if (proxy == nullptr) {
3575 return NO_INIT;
3576 }
3577 return proxy->addEffectToHal(effect);
3578}
3579
Andy Hungbd72c542023-06-20 18:56:17 -07003580status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003581 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003582 sp<DeviceEffectProxy> proxy = mProxy.promote();
3583 if (proxy == nullptr) {
3584 return NO_INIT;
3585 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003586 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003587}
3588
Andy Hungbd72c542023-06-20 18:56:17 -07003589bool DeviceEffectProxy::ProxyCallback::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003590 sp<DeviceEffectProxy> proxy = mProxy.promote();
3591 if (proxy == nullptr) {
3592 return true;
3593 }
3594 return proxy->isOutput();
3595}
3596
Andy Hungbd72c542023-06-20 18:56:17 -07003597uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003598 sp<DeviceEffectProxy> proxy = mProxy.promote();
3599 if (proxy == nullptr) {
3600 return DEFAULT_OUTPUT_SAMPLE_RATE;
3601 }
3602 return proxy->sampleRate();
3603}
3604
Andy Hungbd72c542023-06-20 18:56:17 -07003605audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
Eric Laurentf1f22e72021-07-13 14:04:14 +02003606 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003607 sp<DeviceEffectProxy> proxy = mProxy.promote();
3608 if (proxy == nullptr) {
3609 return AUDIO_CHANNEL_OUT_STEREO;
3610 }
3611 return proxy->channelMask();
3612}
3613
Andy Hungbd72c542023-06-20 18:56:17 -07003614uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003615 sp<DeviceEffectProxy> proxy = mProxy.promote();
3616 if (proxy == nullptr) {
3617 return 2;
3618 }
3619 return proxy->channelCount();
3620}
3621
Andy Hungbd72c542023-06-20 18:56:17 -07003622audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003623 sp<DeviceEffectProxy> proxy = mProxy.promote();
3624 if (proxy == nullptr) {
3625 return AUDIO_CHANNEL_OUT_STEREO;
3626 }
3627 return proxy->channelMask();
3628}
3629
Andy Hungbd72c542023-06-20 18:56:17 -07003630uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003631 sp<DeviceEffectProxy> proxy = mProxy.promote();
3632 if (proxy == nullptr) {
3633 return 2;
3634 }
3635 return proxy->channelCount();
3636}
3637
Andy Hungbd72c542023-06-20 18:56:17 -07003638void DeviceEffectProxy::ProxyCallback::onEffectEnable(
3639 const sp<IAfEffectBase>& effectBase) {
3640 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003641 if (effect == nullptr) {
3642 return;
3643 }
3644 effect->start();
3645}
3646
Andy Hungbd72c542023-06-20 18:56:17 -07003647void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3648 const sp<IAfEffectBase>& effectBase) {
3649 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003650 if (effect == nullptr) {
3651 return;
3652 }
3653 effect->stop();
3654}
3655
Glenn Kasten63238ef2015-03-02 15:50:29 -08003656} // namespace android