blob: 23b3bd3554b886cf6179f999bb13a04e68a02890 [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 Hung71ba4b32022-10-06 12:09:49 -07001885 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
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 Hung44f27182023-07-06 20:56:16 -07002115 const wp<IAfThreadBase>& wThread,
Andy Hungbd72c542023-06-20 18:56:17 -07002116 audio_session_t sessionId)
2117{
2118 // TODO(b/288339104) no weak pointer cast.
Andy Hung44f27182023-07-06 20:56:16 -07002119 return sp<EffectChain>::make(wThread, sessionId);
Andy Hungbd72c542023-06-20 18:56:17 -07002120}
2121
Andy Hung44f27182023-07-06 20:56:16 -07002122EffectChain::EffectChain(const wp<IAfThreadBase>& thread,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002123 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002124 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabineb5784e2023-08-24 21:10:44 +00002125 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002126 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002127 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002128{
Andy Hung44f27182023-07-06 20:56:16 -07002129 const sp<IAfThreadBase> p = thread.promote();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002130 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002131 return;
2132 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002133 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002134 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2135 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002136}
2137
Andy Hungbd72c542023-06-20 18:56:17 -07002138EffectChain::~EffectChain()
Eric Laurentca7cc822012-11-19 14:55:58 -08002139{
Eric Laurentca7cc822012-11-19 14:55:58 -08002140}
2141
Andy Hung44f27182023-07-06 20:56:16 -07002142// getEffectFromDesc_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002143sp<IAfEffectModule> EffectChain::getEffectFromDesc_l(
2144 effect_descriptor_t *descriptor) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002145{
2146 size_t size = mEffects.size();
2147
2148 for (size_t i = 0; i < size; i++) {
2149 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2150 return mEffects[i];
2151 }
2152 }
2153 return 0;
2154}
2155
Andy Hung44f27182023-07-06 20:56:16 -07002156// getEffectFromId_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002157sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002158{
2159 size_t size = mEffects.size();
2160
2161 for (size_t i = 0; i < size; i++) {
2162 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2163 if (id == 0 || mEffects[i]->id() == id) {
2164 return mEffects[i];
2165 }
2166 }
2167 return 0;
2168}
2169
Andy Hung44f27182023-07-06 20:56:16 -07002170// getEffectFromType_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002171sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2172 const effect_uuid_t *type) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002173{
2174 size_t size = mEffects.size();
2175
2176 for (size_t i = 0; i < size; i++) {
2177 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2178 return mEffects[i];
2179 }
2180 }
2181 return 0;
2182}
2183
Andy Hungbd72c542023-06-20 18:56:17 -07002184std::vector<int> EffectChain::getEffectIds() const
Eric Laurent6c796322019-04-09 14:13:17 -07002185{
2186 std::vector<int> ids;
2187 Mutex::Autolock _l(mLock);
2188 for (size_t i = 0; i < mEffects.size(); i++) {
2189 ids.push_back(mEffects[i]->id());
2190 }
2191 return ids;
2192}
2193
Andy Hungbd72c542023-06-20 18:56:17 -07002194void EffectChain::clearInputBuffer()
Eric Laurentca7cc822012-11-19 14:55:58 -08002195{
2196 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002197 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002198}
2199
2200// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002201void EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002202{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002203 if (mInBuffer == NULL) {
2204 return;
2205 }
Andy Hung319587b2023-05-23 14:01:03 -07002206 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002207 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002208
Eric Laurent6b446ce2019-12-13 10:56:31 -08002209 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002210 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002211}
2212
2213// Must be called with EffectChain::mLock locked
Andy Hungbd72c542023-06-20 18:56:17 -07002214void EffectChain::process_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002215{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002216 // never process effects when:
2217 // - on an OFFLOAD thread
2218 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002219 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002220 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002221 bool tracksOnSession = (trackCnt() != 0);
2222
2223 if (!tracksOnSession && mTailBufferCount == 0) {
2224 doProcess = false;
2225 }
2226
2227 if (activeTrackCnt() == 0) {
2228 // if no track is active and the effect tail has not been rendered,
2229 // the input buffer must be cleared here as the mixer process will not do it
2230 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002231 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002232 if (mTailBufferCount > 0) {
2233 mTailBufferCount--;
2234 }
2235 }
2236 }
2237 }
2238
2239 size_t size = mEffects.size();
2240 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002241 // Only the input and output buffers of the chain can be external,
2242 // and 'update' / 'commit' do nothing for allocated buffers, thus
2243 // it's not needed to consider any other buffers here.
2244 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002245 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2246 mOutBuffer->update();
2247 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 for (size_t i = 0; i < size; i++) {
2249 mEffects[i]->process();
2250 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002251 mInBuffer->commit();
2252 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2253 mOutBuffer->commit();
2254 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002255 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002256 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002257 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002258 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2259 }
2260 if (doResetVolume) {
2261 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002262 }
2263}
2264
Andy Hung44f27182023-07-06 20:56:16 -07002265// createEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002266status_t EffectChain::createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002267 effect_descriptor_t *desc,
2268 int id,
2269 audio_session_t sessionId,
2270 bool pinned)
2271{
2272 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002273 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002274 status_t lStatus = effect->status();
2275 if (lStatus == NO_ERROR) {
2276 lStatus = addEffect_ll(effect);
2277 }
2278 if (lStatus != NO_ERROR) {
2279 effect.clear();
2280 }
2281 return lStatus;
2282}
2283
Andy Hung44f27182023-07-06 20:56:16 -07002284// addEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002285status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
Eric Laurentca7cc822012-11-19 14:55:58 -08002286{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002287 Mutex::Autolock _l(mLock);
2288 return addEffect_ll(effect);
2289}
Andy Hung44f27182023-07-06 20:56:16 -07002290// addEffect_l() must be called with IAfThreadBase::mLock and EffectChain::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002291status_t EffectChain::addEffect_ll(const sp<IAfEffectModule>& effect)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002292{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002293 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002294
Eric Laurentb62d0362021-10-26 17:40:18 +02002295 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002296 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2297 // Auxiliary effects are inserted at the beginning of mEffects vector as
2298 // they are processed first and accumulated in chain input buffer
2299 mEffects.insertAt(effect, 0);
2300
2301 // the input buffer for auxiliary effect contains mono samples in
2302 // 32 bit format. This is to avoid saturation in AudoMixer
2303 // accumulation stage. Saturation is done in EffectModule::process() before
2304 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002305 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002306 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002307
Eric Laurent6b446ce2019-12-13 10:56:31 -08002308 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002309 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002310 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002311
2312 effect->configure();
2313
Mikhail Naganov022b9952017-01-04 16:36:51 -08002314 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002315 // auxiliary effects output samples to chain input buffer for further processing
2316 // by insert effects
2317 effect->setOutBuffer(mInBuffer);
2318 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002319 ssize_t idx_insert = getInsertIndex(desc);
2320 if (idx_insert < 0) {
2321 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002322 }
2323
Eric Laurentb62d0362021-10-26 17:40:18 +02002324 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002325 mEffects.insertAt(effect, idx_insert);
2326
2327 effect->configure();
2328
Eric Laurentb62d0362021-10-26 17:40:18 +02002329 // - By default:
2330 // All effects read samples from chain input buffer.
2331 // The last effect in the chain, writes samples to chain output buffer,
2332 // otherwise to chain input buffer
2333 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2334 // The spatializer effect (first effect) reads samples from the input buffer
2335 // and writes samples to the output buffer.
2336 // All other effects read and writes samples to the output buffer
2337 if (mEffectCallback->isSpatializer()
2338 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002339 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002340 if (idx_insert == 0) {
2341 if (previousSize != 0) {
2342 mEffects[1]->configure();
2343 mEffects[1]->setInBuffer(mOutBuffer);
2344 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2345 }
2346 effect->setInBuffer(mInBuffer);
2347 } else {
2348 effect->setInBuffer(mOutBuffer);
2349 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002350 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002351 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002352 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002353 if (idx_insert != 0) {
2354 mEffects[idx_insert-1]->configure();
2355 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2356 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2357 }
2358 effect->setOutBuffer(mOutBuffer);
2359 } else {
2360 effect->setOutBuffer(mInBuffer);
2361 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002362 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002363 ALOGV("%s effect %p, added in chain %p at rank %zu",
2364 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002365 }
2366 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002367
Eric Laurentca7cc822012-11-19 14:55:58 -08002368 return NO_ERROR;
2369}
2370
Andy Hungbd72c542023-06-20 18:56:17 -07002371std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
jiabineb5784e2023-08-24 21:10:44 +00002372 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2373 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2374 return i - 1;
2375 }
2376 }
2377 return std::nullopt;
2378}
2379
Andy Hungbd72c542023-06-20 18:56:17 -07002380ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002381 // Insert effects are inserted at the end of mEffects vector as they are processed
2382 // after track and auxiliary effects.
2383 // Insert effect order as a function of indicated preference:
2384 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2385 // another effect is present
2386 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2387 // last effect claiming first position
2388 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2389 // first effect claiming last position
2390 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2391 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2392 // already present
2393 // Spatializer or Downmixer effects are inserted in first position because
2394 // they adapt the channel count for all other effects in the chain
2395 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2396 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2397 return 0;
2398 }
2399
2400 size_t size = mEffects.size();
2401 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2402 ssize_t idx_insert;
2403 ssize_t idx_insert_first = -1;
2404 ssize_t idx_insert_last = -1;
2405
2406 idx_insert = size;
2407 for (size_t i = 0; i < size; i++) {
2408 effect_descriptor_t d = mEffects[i]->desc();
2409 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2410 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2411 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2412 // check invalid effect chaining combinations
2413 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2414 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2415 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2416 __func__, desc.name, d.name);
2417 return -1;
2418 }
2419 // remember position of first insert effect and by default
2420 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002421 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002422 idx_insert = i;
2423 }
2424 // remember position of last insert effect claiming
2425 // first position
2426 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2427 idx_insert_first = i;
2428 }
2429 // remember position of first insert effect claiming
2430 // last position
2431 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2432 idx_insert_last == -1) {
2433 idx_insert_last = i;
2434 }
2435 }
2436 }
2437
2438 // modify idx_insert from first position if needed
2439 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2440 if (idx_insert_last != -1) {
2441 idx_insert = idx_insert_last;
2442 } else {
2443 idx_insert = size;
2444 }
2445 } else {
2446 if (idx_insert_first != -1) {
2447 idx_insert = idx_insert_first + 1;
2448 }
2449 }
2450 return idx_insert;
2451}
2452
Andy Hung44f27182023-07-06 20:56:16 -07002453// removeEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002454size_t EffectChain::removeEffect_l(const sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002455 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002456{
2457 Mutex::Autolock _l(mLock);
2458 size_t size = mEffects.size();
2459 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2460
2461 for (size_t i = 0; i < size; i++) {
2462 if (effect == mEffects[i]) {
2463 // calling stop here will remove pre-processing effect from the audio HAL.
2464 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2465 // the middle of a read from audio HAL
2466 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2467 mEffects[i]->state() == EffectModule::STOPPING) {
2468 mEffects[i]->stop();
2469 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002470 if (release) {
2471 mEffects[i]->release_l();
2472 }
2473
Mikhail Naganov022b9952017-01-04 16:36:51 -08002474 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002475 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002476 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002477 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002478 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002479 }
2480 }
2481 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002482
2483 // make sure the input buffer configuration for the new first effect in the chain
2484 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002485 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2486 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002487 mEffects[0]->configure();
2488 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002489 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002490 }
2491
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002492 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002493 this, i);
2494 break;
2495 }
2496 }
2497
2498 return mEffects.size();
2499}
2500
Andy Hung44f27182023-07-06 20:56:16 -07002501// setDevices_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002502void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002503{
2504 size_t size = mEffects.size();
2505 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002506 mEffects[i]->setDevices(devices);
2507 }
2508}
2509
Andy Hung44f27182023-07-06 20:56:16 -07002510// setInputDevice_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002511void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08002512{
2513 size_t size = mEffects.size();
2514 for (size_t i = 0; i < size; i++) {
2515 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002516 }
2517}
2518
Andy Hung44f27182023-07-06 20:56:16 -07002519// setMode_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002520void EffectChain::setMode_l(audio_mode_t mode)
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]->setMode(mode);
2525 }
2526}
2527
Andy Hung44f27182023-07-06 20:56:16 -07002528// setAudioSource_l() must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002529void EffectChain::setAudioSource_l(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08002530{
2531 size_t size = mEffects.size();
2532 for (size_t i = 0; i < size; i++) {
2533 mEffects[i]->setAudioSource(source);
2534 }
2535}
2536
Andy Hungbd72c542023-06-20 18:56:17 -07002537bool EffectChain::hasVolumeControlEnabled_l() const {
Zhou Songd505c642020-02-20 16:35:37 +08002538 for (const auto &effect : mEffects) {
2539 if (effect->isVolumeControlEnabled()) return true;
2540 }
2541 return false;
2542}
2543
Andy Hung44f27182023-07-06 20:56:16 -07002544// setVolume_l() must be called with IAfThreadBase::mLock or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002545bool EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002546{
2547 uint32_t newLeft = *left;
2548 uint32_t newRight = *right;
jiabineb5784e2023-08-24 21:10:44 +00002549 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002550
2551 // first update volume controller
jiabineb5784e2023-08-24 21:10:44 +00002552 const auto volumeControlIndex = findVolumeControl_l(0, size);
2553 const int ctrlIdx = volumeControlIndex.value_or(-1);
Andy Hungbd72c542023-06-20 18:56:17 -07002554 const sp<IAfEffectModule> volumeControlEffect =
jiabineb5784e2023-08-24 21:10:44 +00002555 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
Andy Hungbd72c542023-06-20 18:56:17 -07002556 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002557
jiabineb5784e2023-08-24 21:10:44 +00002558 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002559 *left == mLeftVolume && *right == mRightVolume) {
jiabineb5784e2023-08-24 21:10:44 +00002560 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002561 *left = mNewLeftVolume;
2562 *right = mNewRightVolume;
2563 }
jiabineb5784e2023-08-24 21:10:44 +00002564 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002565 }
2566
jiabineb5784e2023-08-24 21:10:44 +00002567 if (volumeControlEffect != cachedVolumeControlEffect) {
2568 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2569 // as zero for safe ramping.
2570 if (cachedVolumeControlEffect != nullptr) {
2571 uint32_t leftMax = 1 << 24;
2572 uint32_t rightMax = 1 << 24;
2573 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2574 }
2575 if (volumeControlEffect != nullptr) {
2576 uint32_t leftZero = 0;
2577 uint32_t rightZero = 0;
2578 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2579 }
2580 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002581 mLeftVolume = newLeft;
2582 mRightVolume = newRight;
2583
2584 // second get volume update from volume controller
2585 if (ctrlIdx >= 0) {
2586 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2587 mNewLeftVolume = newLeft;
2588 mNewRightVolume = newRight;
2589 }
2590 // then indicate volume to all other effects in chain.
2591 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002592 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002593 uint32_t lVol = newLeft;
2594 uint32_t rVol = newRight;
2595
2596 for (size_t i = 0; i < size; i++) {
2597 if ((int)i == ctrlIdx) {
2598 continue;
2599 }
2600 // this also works for ctrlIdx == -1 when there is no volume controller
2601 if ((int)i > ctrlIdx) {
2602 lVol = *left;
2603 rVol = *right;
2604 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002605 // Pass requested volume directly if this is volume monitor module
2606 if (mEffects[i]->isVolumeMonitor()) {
2607 mEffects[i]->setVolume(left, right, false);
2608 } else {
2609 mEffects[i]->setVolume(&lVol, &rVol, false);
2610 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002611 }
2612 *left = newLeft;
2613 *right = newRight;
2614
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002615 setVolumeForOutput_l(*left, *right);
2616
jiabineb5784e2023-08-24 21:10:44 +00002617 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002618}
2619
Andy Hung44f27182023-07-06 20:56:16 -07002620// resetVolume_l() must be called with IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002621void EffectChain::resetVolume_l()
Eric Laurentfa1e1232016-08-02 19:01:49 -07002622{
Eric Laurente7449bf2016-08-03 18:44:07 -07002623 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2624 uint32_t left = mLeftVolume;
2625 uint32_t right = mRightVolume;
2626 (void)setVolume_l(&left, &right, true);
2627 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002628}
2629
Andy Hungbd72c542023-06-20 18:56:17 -07002630// containsHapticGeneratingEffect_l must be called with
Andy Hung44f27182023-07-06 20:56:16 -07002631// IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hungbd72c542023-06-20 18:56:17 -07002632bool EffectChain::containsHapticGeneratingEffect_l()
jiabineb3bda02020-06-30 14:07:03 -07002633{
2634 for (size_t i = 0; i < mEffects.size(); ++i) {
2635 if (mEffects[i]->isHapticGenerator()) {
2636 return true;
2637 }
2638 }
2639 return false;
2640}
2641
Andy Hungbd72c542023-06-20 18:56:17 -07002642void EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002643{
2644 Mutex::Autolock _l(mLock);
2645 for (size_t i = 0; i < mEffects.size(); ++i) {
2646 mEffects[i]->setHapticIntensity(id, intensity);
2647 }
2648}
2649
Andy Hungbd72c542023-06-20 18:56:17 -07002650void EffectChain::syncHalEffectsState()
Eric Laurent1b928682014-10-02 19:41:47 -07002651{
2652 Mutex::Autolock _l(mLock);
2653 for (size_t i = 0; i < mEffects.size(); i++) {
2654 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2655 mEffects[i]->state() == EffectModule::STOPPING) {
2656 mEffects[i]->addEffectToHal_l();
2657 }
2658 }
2659}
2660
Andy Hungbd72c542023-06-20 18:56:17 -07002661void EffectChain::dump(int fd, const Vector<String16>& args) const
Andy Hung71ba4b32022-10-06 12:09:49 -07002662NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002663{
Eric Laurentca7cc822012-11-19 14:55:58 -08002664 String8 result;
2665
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002666 const size_t numEffects = mEffects.size();
2667 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002668
Marco Nelissenb2208842014-02-07 14:00:50 -08002669 if (numEffects) {
2670 bool locked = AudioFlinger::dumpTryLock(mLock);
2671 // failed to lock - AudioFlinger is probably deadlocked
2672 if (!locked) {
2673 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002674 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002675
Andy Hungbded9c82017-11-30 18:47:35 -08002676 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2677 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2678 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2679 (int)inBufferStr.size(), "In buffer ",
2680 (int)outBufferStr.size(), "Out buffer ");
2681 result.appendFormat("\t%s %s %d\n",
2682 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002683 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002684
2685 for (size_t i = 0; i < numEffects; ++i) {
Andy Hungbd72c542023-06-20 18:56:17 -07002686 sp<IAfEffectModule> effect = mEffects[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08002687 if (effect != 0) {
2688 effect->dump(fd, args);
2689 }
2690 }
2691
2692 if (locked) {
2693 mLock.unlock();
2694 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002695 } else {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002696 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002697 }
2698}
2699
Andy Hung44f27182023-07-06 20:56:16 -07002700// must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002701void EffectChain::setEffectSuspended_l(
Eric Laurentca7cc822012-11-19 14:55:58 -08002702 const effect_uuid_t *type, bool suspend)
2703{
2704 sp<SuspendedEffectDesc> desc;
2705 // use effect type UUID timelow as key as there is no real risk of identical
2706 // timeLow fields among effect type UUIDs.
2707 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2708 if (suspend) {
2709 if (index >= 0) {
2710 desc = mSuspendedEffects.valueAt(index);
2711 } else {
2712 desc = new SuspendedEffectDesc();
2713 desc->mType = *type;
2714 mSuspendedEffects.add(type->timeLow, desc);
2715 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2716 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002717
Eric Laurentca7cc822012-11-19 14:55:58 -08002718 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002719 sp<IAfEffectModule> effect = getEffectIfEnabled(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002720 if (effect != 0) {
2721 desc->mEffect = effect;
2722 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002723 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002724 }
2725 }
2726 } else {
2727 if (index < 0) {
2728 return;
2729 }
2730 desc = mSuspendedEffects.valueAt(index);
2731 if (desc->mRefCount <= 0) {
2732 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002733 desc->mRefCount = 0;
2734 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002735 }
2736 if (--desc->mRefCount == 0) {
2737 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2738 if (desc->mEffect != 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002739 sp<IAfEffectModule> effect = desc->mEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002740 if (effect != 0) {
2741 effect->setSuspended(false);
2742 effect->lock();
Andy Hungbd72c542023-06-20 18:56:17 -07002743 IAfEffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002744 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002745 effect->setEnabled_l(handle->enabled());
2746 }
2747 effect->unlock();
2748 }
2749 desc->mEffect.clear();
2750 }
2751 mSuspendedEffects.removeItemsAt(index);
2752 }
2753 }
2754}
2755
Andy Hung44f27182023-07-06 20:56:16 -07002756// must be called with IAfThreadBase::mutex() held
Andy Hungbd72c542023-06-20 18:56:17 -07002757void EffectChain::setEffectSuspendedAll_l(bool suspend)
Eric Laurentca7cc822012-11-19 14:55:58 -08002758{
2759 sp<SuspendedEffectDesc> desc;
2760
2761 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2762 if (suspend) {
2763 if (index >= 0) {
2764 desc = mSuspendedEffects.valueAt(index);
2765 } else {
2766 desc = new SuspendedEffectDesc();
2767 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2768 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2769 }
2770 if (desc->mRefCount++ == 0) {
Andy Hungbd72c542023-06-20 18:56:17 -07002771 Vector< sp<IAfEffectModule> > effects;
Eric Laurentca7cc822012-11-19 14:55:58 -08002772 getSuspendEligibleEffects(effects);
2773 for (size_t i = 0; i < effects.size(); i++) {
2774 setEffectSuspended_l(&effects[i]->desc().type, true);
2775 }
2776 }
2777 } else {
2778 if (index < 0) {
2779 return;
2780 }
2781 desc = mSuspendedEffects.valueAt(index);
2782 if (desc->mRefCount <= 0) {
2783 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2784 desc->mRefCount = 1;
2785 }
2786 if (--desc->mRefCount == 0) {
2787 Vector<const effect_uuid_t *> types;
2788 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2789 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2790 continue;
2791 }
2792 types.add(&mSuspendedEffects.valueAt(i)->mType);
2793 }
2794 for (size_t i = 0; i < types.size(); i++) {
2795 setEffectSuspended_l(types[i], false);
2796 }
2797 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2798 mSuspendedEffects.keyAt(index));
2799 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2800 }
2801 }
2802}
2803
2804
2805// The volume effect is used for automated tests only
2806#ifndef OPENSL_ES_H_
2807static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2808 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2809const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2810#endif //OPENSL_ES_H_
2811
Eric Laurentd8365c52017-07-16 15:27:05 -07002812/* static */
Andy Hungbd72c542023-06-20 18:56:17 -07002813bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
Eric Laurentd8365c52017-07-16 15:27:05 -07002814{
2815 // Only NS and AEC are suspended when BtNRec is off
2816 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2817 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2818 return true;
2819 }
2820 return false;
2821}
2822
Andy Hungbd72c542023-06-20 18:56:17 -07002823bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
Eric Laurentca7cc822012-11-19 14:55:58 -08002824{
2825 // auxiliary effects and visualizer are never suspended on output mix
2826 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2827 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2828 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002829 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2830 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002831 return false;
2832 }
2833 return true;
2834}
2835
Andy Hungbd72c542023-06-20 18:56:17 -07002836void EffectChain::getSuspendEligibleEffects(
2837 Vector< sp<IAfEffectModule> > &effects)
Eric Laurentca7cc822012-11-19 14:55:58 -08002838{
2839 effects.clear();
2840 for (size_t i = 0; i < mEffects.size(); i++) {
2841 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2842 effects.add(mEffects[i]);
2843 }
2844 }
2845}
2846
Andy Hungbd72c542023-06-20 18:56:17 -07002847sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
Eric Laurentca7cc822012-11-19 14:55:58 -08002848{
Andy Hungbd72c542023-06-20 18:56:17 -07002849 sp<IAfEffectModule> effect = getEffectFromType_l(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002850 return effect != 0 && effect->isEnabled() ? effect : 0;
2851}
2852
Andy Hungbd72c542023-06-20 18:56:17 -07002853void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08002854 bool enabled)
2855{
2856 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2857 if (enabled) {
2858 if (index < 0) {
2859 // if the effect is not suspend check if all effects are suspended
2860 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2861 if (index < 0) {
2862 return;
2863 }
2864 if (!isEffectEligibleForSuspend(effect->desc())) {
2865 return;
2866 }
2867 setEffectSuspended_l(&effect->desc().type, enabled);
2868 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2869 if (index < 0) {
2870 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2871 return;
2872 }
2873 }
2874 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2875 effect->desc().type.timeLow);
2876 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002877 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002878 if (desc->mEffect == 0) {
2879 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002880 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002881 effect->setSuspended(true);
2882 }
2883 } else {
2884 if (index < 0) {
2885 return;
2886 }
2887 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2888 effect->desc().type.timeLow);
2889 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2890 desc->mEffect.clear();
2891 effect->setSuspended(false);
2892 }
2893}
2894
Andy Hungbd72c542023-06-20 18:56:17 -07002895bool EffectChain::isNonOffloadableEnabled() const
Eric Laurent813e2a72013-08-31 12:59:48 -07002896{
2897 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002898 return isNonOffloadableEnabled_l();
2899}
2900
Andy Hungbd72c542023-06-20 18:56:17 -07002901bool EffectChain::isNonOffloadableEnabled_l() const
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002902{
Eric Laurent813e2a72013-08-31 12:59:48 -07002903 size_t size = mEffects.size();
2904 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002905 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002906 return true;
2907 }
2908 }
2909 return false;
2910}
2911
Andy Hung44f27182023-07-06 20:56:16 -07002912void EffectChain::setThread(const sp<IAfThreadBase>& thread)
Eric Laurentaaa44472014-09-12 17:41:50 -07002913{
2914 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002915 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002916}
2917
Andy Hungbd72c542023-06-20 18:56:17 -07002918void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002919{
2920 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2921 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2922 }
2923 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2924 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2925 }
jiabinc658e452022-10-21 20:52:21 +00002926 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2927 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2928 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002929}
2930
Andy Hungbd72c542023-06-20 18:56:17 -07002931void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002932{
2933 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2934 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2935 }
2936 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2937 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2938 }
2939}
2940
Andy Hungbd72c542023-06-20 18:56:17 -07002941bool EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002942{
2943 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002944 for (const auto &effect : mEffects) {
2945 if (effect->isProcessImplemented()) {
2946 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002947 }
2948 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002949 // Allow effects without processing.
2950 return true;
2951}
2952
Andy Hungbd72c542023-06-20 18:56:17 -07002953bool EffectChain::isFastCompatible() const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002954{
2955 Mutex::Autolock _l(mLock);
2956 for (const auto &effect : mEffects) {
2957 if (effect->isProcessImplemented()
2958 && effect->isImplementationSoftware()) {
2959 return false;
2960 }
2961 }
2962 // Allow effects without processing or hw accelerated effects.
2963 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002964}
2965
Andy Hungbd72c542023-06-20 18:56:17 -07002966bool EffectChain::isBitPerfectCompatible() const {
jiabinc658e452022-10-21 20:52:21 +00002967 Mutex::Autolock _l(mLock);
2968 for (const auto &effect : mEffects) {
2969 if (effect->isProcessImplemented()
2970 && effect->isImplementationSoftware()) {
2971 return false;
2972 }
2973 }
2974 // Allow effects without processing or hw accelerated effects.
2975 return true;
2976}
2977
Eric Laurent4c415062016-06-17 16:14:16 -07002978// isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung44f27182023-07-06 20:56:16 -07002979bool EffectChain::isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const
Eric Laurent4c415062016-06-17 16:14:16 -07002980{
2981 Mutex::Autolock _l(mLock);
2982 for (size_t i = 0; i < mEffects.size(); i++) {
2983 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2984 return false;
2985 }
2986 }
2987 return true;
2988}
2989
Eric Laurent6b446ce2019-12-13 10:56:31 -08002990// EffectCallbackInterface implementation
Andy Hungbd72c542023-06-20 18:56:17 -07002991status_t EffectChain::EffectCallback::createEffectHal(
Eric Laurent6b446ce2019-12-13 10:56:31 -08002992 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2993 sp<EffectHalInterface> *effect) {
2994 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002995 const sp<EffectsFactoryHalInterface> effectsFactory =
2996 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997 if (effectsFactory != 0) {
2998 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2999 }
3000 return status;
3001}
3002
Andy Hungbd72c542023-06-20 18:56:17 -07003003bool EffectChain::EffectCallback::updateOrphanEffectChains(
3004 const sp<IAfEffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003005 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003006 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003007}
3008
Andy Hungbd72c542023-06-20 18:56:17 -07003009status_t EffectChain::EffectCallback::allocateHalBuffer(
Eric Laurent6b446ce2019-12-13 10:56:31 -08003010 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003011 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003012}
3013
Andy Hungbd72c542023-06-20 18:56:17 -07003014status_t EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003015 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003016 status_t result = NO_INIT;
Andy Hung44f27182023-07-06 20:56:16 -07003017 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018 if (t == nullptr) {
3019 return result;
3020 }
3021 sp <StreamHalInterface> st = t->stream();
3022 if (st == nullptr) {
3023 return result;
3024 }
3025 result = st->addEffect(effect);
3026 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3027 return result;
3028}
3029
Andy Hungbd72c542023-06-20 18:56:17 -07003030status_t EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003031 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003032 status_t result = NO_INIT;
Andy Hung44f27182023-07-06 20:56:16 -07003033 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003034 if (t == nullptr) {
3035 return result;
3036 }
3037 sp <StreamHalInterface> st = t->stream();
3038 if (st == nullptr) {
3039 return result;
3040 }
3041 result = st->removeEffect(effect);
3042 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3043 return result;
3044}
3045
Andy Hungbd72c542023-06-20 18:56:17 -07003046audio_io_handle_t EffectChain::EffectCallback::io() const {
Andy Hung44f27182023-07-06 20:56:16 -07003047 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003048 if (t == nullptr) {
3049 return AUDIO_IO_HANDLE_NONE;
3050 }
3051 return t->id();
3052}
3053
Andy Hungbd72c542023-06-20 18:56:17 -07003054bool EffectChain::EffectCallback::isOutput() const {
Andy Hung44f27182023-07-06 20:56:16 -07003055 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003056 if (t == nullptr) {
3057 return true;
3058 }
3059 return t->isOutput();
3060}
3061
Andy Hungbd72c542023-06-20 18:56:17 -07003062bool EffectChain::EffectCallback::isOffload() const {
Andy Hung44f27182023-07-06 20:56:16 -07003063 return mThreadType == IAfThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003064}
3065
Andy Hungbd72c542023-06-20 18:56:17 -07003066bool EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung44f27182023-07-06 20:56:16 -07003067 return mThreadType == IAfThreadBase::OFFLOAD
3068 || mThreadType == IAfThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069}
3070
Andy Hungbd72c542023-06-20 18:56:17 -07003071bool EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003072 switch (mThreadType) {
Andy Hung44f27182023-07-06 20:56:16 -07003073 case IAfThreadBase::OFFLOAD:
3074 case IAfThreadBase::MMAP_PLAYBACK:
3075 case IAfThreadBase::MMAP_CAPTURE:
Eric Laurentb62d0362021-10-26 17:40:18 +02003076 return true;
3077 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003078 return false;
3079 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003080}
3081
Andy Hungbd72c542023-06-20 18:56:17 -07003082bool EffectChain::EffectCallback::isSpatializer() const {
Andy Hung44f27182023-07-06 20:56:16 -07003083 return mThreadType == IAfThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003084}
3085
Andy Hungbd72c542023-06-20 18:56:17 -07003086uint32_t EffectChain::EffectCallback::sampleRate() const {
Andy Hung44f27182023-07-06 20:56:16 -07003087 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003088 if (t == nullptr) {
3089 return 0;
3090 }
3091 return t->sampleRate();
3092}
3093
Andy Hungbd72c542023-06-20 18:56:17 -07003094audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const {
Andy Hung44f27182023-07-06 20:56:16 -07003095 const sp<IAfThreadBase> t = thread().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003096 if (t == nullptr) {
3097 return AUDIO_CHANNEL_NONE;
3098 }
Andy Hungbd72c542023-06-20 18:56:17 -07003099 sp<IAfEffectChain> c = chain().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003100 if (c == nullptr) {
3101 return AUDIO_CHANNEL_NONE;
3102 }
3103
Andy Hung44f27182023-07-06 20:56:16 -07003104 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003105 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3106 if (c->isFirstEffect(id)) {
3107 return t->mixerChannelMask();
3108 } else {
3109 return t->channelMask();
3110 }
3111 } else if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003112 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung44f27182023-07-06 20:56:16 -07003113 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003114 return t->mixerChannelMask();
3115 } else {
3116 return t->channelMask();
3117 }
3118 } else {
3119 return t->channelMask();
3120 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003121 } else {
3122 return t->channelMask();
3123 }
3124}
3125
Andy Hungbd72c542023-06-20 18:56:17 -07003126uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003127 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003128}
3129
Andy Hungbd72c542023-06-20 18:56:17 -07003130audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const {
Andy Hung44f27182023-07-06 20:56:16 -07003131 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003132 if (t == nullptr) {
3133 return AUDIO_CHANNEL_NONE;
3134 }
Andy Hungbd72c542023-06-20 18:56:17 -07003135 sp<IAfEffectChain> c = chain().promote();
Eric Laurentb62d0362021-10-26 17:40:18 +02003136 if (c == nullptr) {
3137 return AUDIO_CHANNEL_NONE;
3138 }
3139
Andy Hung44f27182023-07-06 20:56:16 -07003140 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003141 if (!audio_is_global_session(c->sessionId())) {
Andy Hungbd72c542023-06-20 18:56:17 -07003142 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung44f27182023-07-06 20:56:16 -07003143 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003144 return t->mixerChannelMask();
3145 } else {
3146 return t->channelMask();
3147 }
3148 } else {
3149 return t->channelMask();
3150 }
3151 } else {
3152 return t->channelMask();
3153 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003154}
3155
Andy Hungbd72c542023-06-20 18:56:17 -07003156uint32_t EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003157 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003158}
3159
Andy Hungbd72c542023-06-20 18:56:17 -07003160audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung44f27182023-07-06 20:56:16 -07003161 const sp<IAfThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003162 if (t == nullptr) {
3163 return AUDIO_CHANNEL_NONE;
3164 }
3165 return t->hapticChannelMask();
3166}
3167
Andy Hungbd72c542023-06-20 18:56:17 -07003168size_t EffectChain::EffectCallback::frameCount() const {
Andy Hung44f27182023-07-06 20:56:16 -07003169 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003170 if (t == nullptr) {
3171 return 0;
3172 }
3173 return t->frameCount();
3174}
3175
Andy Hungbd72c542023-06-20 18:56:17 -07003176uint32_t EffectChain::EffectCallback::latency() const
Andy Hung71ba4b32022-10-06 12:09:49 -07003177NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3178{
Andy Hung44f27182023-07-06 20:56:16 -07003179 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003180 if (t == nullptr) {
3181 return 0;
3182 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003183 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003184 return t->latency_l();
3185}
3186
Andy Hungbd72c542023-06-20 18:56:17 -07003187void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003188NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3189{
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->setVolumeForOutput_l(left, right);
3195}
3196
Andy Hungbd72c542023-06-20 18:56:17 -07003197void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
3198 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung44f27182023-07-06 20:56:16 -07003199 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003200 if (t == nullptr) {
3201 return;
3202 }
3203 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3204
Andy Hungbd72c542023-06-20 18:56:17 -07003205 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003206 if (c == 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 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003211}
3212
Andy Hungbd72c542023-06-20 18:56:17 -07003213void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
Andy Hung44f27182023-07-06 20:56:16 -07003214 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003215 if (t == nullptr) {
3216 return;
3217 }
Eric Laurent41709552019-12-16 19:34:05 -08003218 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3219 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003220}
3221
Andy Hungbd72c542023-06-20 18:56:17 -07003222void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003223 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3224
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;
3228 }
3229 t->onEffectDisable();
3230}
3231
Andy Hungbd72c542023-06-20 18:56:17 -07003232bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
Eric Laurent6b446ce2019-12-13 10:56:31 -08003233 bool unpinIfLast) {
Andy Hung44f27182023-07-06 20:56:16 -07003234 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003235 if (t == nullptr) {
3236 return false;
3237 }
3238 t->disconnectEffectHandle(handle, unpinIfLast);
3239 return true;
3240}
3241
Andy Hungbd72c542023-06-20 18:56:17 -07003242void EffectChain::EffectCallback::resetVolume() {
3243 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003244 if (c == nullptr) {
3245 return;
3246 }
3247 c->resetVolume_l();
3248
3249}
3250
Andy Hungbd72c542023-06-20 18:56:17 -07003251product_strategy_t EffectChain::EffectCallback::strategy() const {
3252 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003253 if (c == nullptr) {
3254 return PRODUCT_STRATEGY_NONE;
3255 }
3256 return c->strategy();
3257}
3258
Andy Hungbd72c542023-06-20 18:56:17 -07003259int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3260 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003261 if (c == nullptr) {
3262 return 0;
3263 }
3264 return c->activeTrackCnt();
3265}
3266
Eric Laurentb82e6b72019-11-22 17:25:04 -08003267
3268#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003269#define LOG_TAG "DeviceEffectProxy"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003270
Andy Hungbd72c542023-06-20 18:56:17 -07003271/* static */
3272sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3273 const AudioDeviceTypeAddr& device,
3274 const sp</* DeviceEffectManagerCallback */ RefBase>& callback, // TODO(b/288339104) type
3275 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3276{
3277 return sp<DeviceEffectProxy>::make(device,
3278 sp<AudioFlinger::DeviceEffectManagerCallback>::cast(callback),
3279 desc, id, notifyFramesProcessed);
3280}
3281
3282status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003283{
3284 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3285 Mutex::Autolock _l(mProxyLock);
3286 if (status == NO_ERROR) {
3287 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003288 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003289 if (enabled) {
Andy Hungbd72c542023-06-20 18:56:17 -07003290 bs = handle.second->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003291 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003292 bs = handle.second->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003293 }
3294 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003295 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003296 }
3297 }
3298 }
3299 ALOGV("%s enable %d status %d", __func__, enabled, status);
3300 return status;
3301}
3302
Andy Hungbd72c542023-06-20 18:56:17 -07003303status_t DeviceEffectProxy::init(
Andy Hung07434ef2023-07-13 18:11:33 -07003304 const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003305//For all audio patches
3306//If src or sink device match
3307//If the effect is HW accelerated
3308// if no corresponding effect module
3309// Create EffectModule: mHalEffect
3310//Create and attach EffectHandle
3311//If the effect is not HW accelerated and the patch sink or src is a mixer port
3312// Create Effect on patch input or output thread on session -1
3313//Add EffectHandle to EffectHandle map of Effect Proxy:
3314 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3315 status_t status = NO_ERROR;
3316 for (auto &patch : patches) {
3317 status = onCreatePatch(patch.first, patch.second);
3318 ALOGV("%s onCreatePatch status %d", __func__, status);
3319 if (status == BAD_VALUE) {
3320 return status;
3321 }
3322 }
3323 return status;
3324}
3325
Andy Hungbd72c542023-06-20 18:56:17 -07003326status_t DeviceEffectProxy::onCreatePatch(
Andy Hung07434ef2023-07-13 18:11:33 -07003327 audio_patch_handle_t patchHandle, const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003328 status_t status = NAME_NOT_FOUND;
Andy Hungbd72c542023-06-20 18:56:17 -07003329 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003330 // only consider source[0] as this is the only "true" source of a patch
3331 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3332 ALOGV("%s source checkPort status %d", __func__, status);
3333 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3334 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3335 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3336 }
3337 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3338 Mutex::Autolock _l(mProxyLock);
3339 mEffectHandles.emplace(patchHandle, handle);
3340 }
3341 ALOGW_IF(status == BAD_VALUE,
3342 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3343
3344 return status;
3345}
3346
Andy Hung07434ef2023-07-13 18:11:33 -07003347status_t DeviceEffectProxy::checkPort(const IAfPatchPanel::Patch& patch,
Andy Hungbd72c542023-06-20 18:56:17 -07003348 const struct audio_port_config *port, sp<IAfEffectHandle> *handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003349
3350 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3351 __func__, port->type, port->ext.device.type,
3352 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003353 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3354 port->ext.device.address != mDevice.address()) {
3355 return NAME_NOT_FOUND;
3356 }
3357 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3358 (audio_port_config_has_input_direction(port))) {
3359 ALOGI("%s don't create postprocessing effect on record port", __func__);
3360 return NAME_NOT_FOUND;
3361 }
3362 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3363 (!audio_port_config_has_input_direction(port))) {
3364 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003365 return NAME_NOT_FOUND;
3366 }
3367 status_t status = NAME_NOT_FOUND;
3368
3369 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3370 Mutex::Autolock _l(mProxyLock);
3371 mDevicePort = *port;
3372 mHalEffect = new EffectModule(mMyCallback,
3373 const_cast<effect_descriptor_t *>(&mDescriptor),
3374 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3375 false /* pinned */, port->id);
3376 if (audio_is_input_device(mDevice.mType)) {
3377 mHalEffect->setInputDevice(mDevice);
3378 } else {
3379 mHalEffect->setDevices({mDevice});
3380 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003381 mHalEffect->configure();
3382
Eric Laurentde8caf42021-08-11 17:19:25 +02003383 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3384 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003385 status = (*handle)->initCheck();
3386 if (status == OK) {
3387 status = mHalEffect->addHandle((*handle).get());
3388 } else {
3389 mHalEffect.clear();
3390 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3391 }
3392 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07003393 sp<IAfThreadBase> thread;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003394 if (audio_port_config_has_input_direction(port)) {
3395 if (patch.isSoftware()) {
3396 thread = patch.mRecord.thread();
3397 } else {
3398 thread = patch.thread().promote();
3399 }
3400 } else {
3401 if (patch.isSoftware()) {
3402 thread = patch.mPlayback.thread();
3403 } else {
3404 thread = patch.thread().promote();
3405 }
3406 }
3407 int enabled;
3408 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3409 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003410 &enabled, &status, false, false /*probe*/,
3411 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003412 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3413 } else {
3414 status = BAD_VALUE;
3415 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003416
Eric Laurentb82e6b72019-11-22 17:25:04 -08003417 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003418 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003419 if (isEnabled()) {
Andy Hungbd72c542023-06-20 18:56:17 -07003420 bs = (*handle)->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003421 } else {
Andy Hungbd72c542023-06-20 18:56:17 -07003422 bs = (*handle)->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003423 }
3424 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003425 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003426 }
3427 }
3428 return status;
3429}
3430
Andy Hungbd72c542023-06-20 18:56:17 -07003431void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3432 sp<IAfEffectHandle> effect;
Eric Laurent76c89f32021-12-03 17:13:23 +01003433 {
3434 Mutex::Autolock _l(mProxyLock);
3435 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3436 effect = mEffectHandles.at(patchHandle);
3437 mEffectHandles.erase(patchHandle);
3438 }
3439 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003440}
3441
3442
Andy Hungbd72c542023-06-20 18:56:17 -07003443size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003444{
3445 Mutex::Autolock _l(mProxyLock);
3446 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003447 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003448 mHalEffect.clear();
3449 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3450 }
3451 return mHalEffect == nullptr ? 0 : 1;
3452}
3453
Andy Hungbd72c542023-06-20 18:56:17 -07003454status_t DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003455 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003456 if (mHalEffect == nullptr) {
3457 return NO_INIT;
3458 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003459 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003460}
3461
Andy Hungbd72c542023-06-20 18:56:17 -07003462status_t DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003463 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003464 if (mHalEffect == nullptr) {
3465 return NO_INIT;
3466 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003467 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003468}
3469
Andy Hungbd72c542023-06-20 18:56:17 -07003470bool DeviceEffectProxy::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003471 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3472 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3473 }
3474 return true;
3475}
3476
Andy Hungbd72c542023-06-20 18:56:17 -07003477uint32_t DeviceEffectProxy::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003478 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3479 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3480 return mDevicePort.sample_rate;
3481 }
3482 return DEFAULT_OUTPUT_SAMPLE_RATE;
3483}
3484
Andy Hungbd72c542023-06-20 18:56:17 -07003485audio_channel_mask_t DeviceEffectProxy::channelMask() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003486 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3487 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3488 return mDevicePort.channel_mask;
3489 }
3490 return AUDIO_CHANNEL_OUT_STEREO;
3491}
3492
Andy Hungbd72c542023-06-20 18:56:17 -07003493uint32_t DeviceEffectProxy::channelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003494 if (isOutput()) {
3495 return audio_channel_count_from_out_mask(channelMask());
3496 }
3497 return audio_channel_count_from_in_mask(channelMask());
3498}
3499
Andy Hungbd72c542023-06-20 18:56:17 -07003500void DeviceEffectProxy::dump2(int fd, int spaces) const
Andy Hung71ba4b32022-10-06 12:09:49 -07003501NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3502{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003503 const Vector<String16> args;
3504 EffectBase::dump(fd, args);
3505
Andy Hungbd72c542023-06-20 18:56:17 -07003506 const bool locked = AudioFlinger::dumpTryLock(mProxyLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003507 if (!locked) {
3508 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003509 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003510 }
3511
3512 String8 outStr;
3513 if (mHalEffect != nullptr) {
3514 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3515 } else {
3516 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3517 }
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();
3520
3521 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003522 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003523 outStr.clear();
3524
3525 for (const auto& iter : mEffectHandles) {
3526 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003527 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003528 outStr.clear();
Andy Hungbd72c542023-06-20 18:56:17 -07003529 sp<IAfEffectBase> effect = iter.second->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003530 if (effect != nullptr) {
3531 effect->dump(fd, args);
3532 }
3533 }
3534
3535 if (locked) {
3536 mLock.unlock();
3537 }
3538}
3539
3540#undef LOG_TAG
Andy Hungbd72c542023-06-20 18:56:17 -07003541#define LOG_TAG "DeviceEffectProxy::ProxyCallback"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003542
Andy Hungbd72c542023-06-20 18:56:17 -07003543int DeviceEffectProxy::ProxyCallback::newEffectId() {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003544 return mManagerCallback->newEffectId();
3545}
3546
3547
Andy Hungbd72c542023-06-20 18:56:17 -07003548bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3549 IAfEffectHandle *handle, bool unpinIfLast) {
3550 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003551 if (effectBase == nullptr) {
3552 return false;
3553 }
3554
Andy Hungbd72c542023-06-20 18:56:17 -07003555 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003556 if (effect == nullptr) {
3557 return false;
3558 }
3559
3560 // restore suspended effects if the disconnected handle was enabled and the last one.
3561 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3562 if (remove) {
3563 sp<DeviceEffectProxy> proxy = mProxy.promote();
3564 if (proxy != nullptr) {
3565 proxy->removeEffect(effect);
3566 }
3567 if (handle->enabled()) {
3568 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3569 }
3570 }
3571 return true;
3572}
3573
Andy Hungbd72c542023-06-20 18:56:17 -07003574status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003575 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3576 sp<EffectHalInterface> *effect) {
3577 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3578}
3579
Andy Hungbd72c542023-06-20 18:56:17 -07003580status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
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 }
3586 return proxy->addEffectToHal(effect);
3587}
3588
Andy Hungbd72c542023-06-20 18:56:17 -07003589status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003590 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003591 sp<DeviceEffectProxy> proxy = mProxy.promote();
3592 if (proxy == nullptr) {
3593 return NO_INIT;
3594 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003595 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003596}
3597
Andy Hungbd72c542023-06-20 18:56:17 -07003598bool DeviceEffectProxy::ProxyCallback::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003599 sp<DeviceEffectProxy> proxy = mProxy.promote();
3600 if (proxy == nullptr) {
3601 return true;
3602 }
3603 return proxy->isOutput();
3604}
3605
Andy Hungbd72c542023-06-20 18:56:17 -07003606uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003607 sp<DeviceEffectProxy> proxy = mProxy.promote();
3608 if (proxy == nullptr) {
3609 return DEFAULT_OUTPUT_SAMPLE_RATE;
3610 }
3611 return proxy->sampleRate();
3612}
3613
Andy Hungbd72c542023-06-20 18:56:17 -07003614audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
Eric Laurentf1f22e72021-07-13 14:04:14 +02003615 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003616 sp<DeviceEffectProxy> proxy = mProxy.promote();
3617 if (proxy == nullptr) {
3618 return AUDIO_CHANNEL_OUT_STEREO;
3619 }
3620 return proxy->channelMask();
3621}
3622
Andy Hungbd72c542023-06-20 18:56:17 -07003623uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003624 sp<DeviceEffectProxy> proxy = mProxy.promote();
3625 if (proxy == nullptr) {
3626 return 2;
3627 }
3628 return proxy->channelCount();
3629}
3630
Andy Hungbd72c542023-06-20 18:56:17 -07003631audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003632 sp<DeviceEffectProxy> proxy = mProxy.promote();
3633 if (proxy == nullptr) {
3634 return AUDIO_CHANNEL_OUT_STEREO;
3635 }
3636 return proxy->channelMask();
3637}
3638
Andy Hungbd72c542023-06-20 18:56:17 -07003639uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003640 sp<DeviceEffectProxy> proxy = mProxy.promote();
3641 if (proxy == nullptr) {
3642 return 2;
3643 }
3644 return proxy->channelCount();
3645}
3646
Andy Hungbd72c542023-06-20 18:56:17 -07003647void DeviceEffectProxy::ProxyCallback::onEffectEnable(
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->start();
3654}
3655
Andy Hungbd72c542023-06-20 18:56:17 -07003656void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3657 const sp<IAfEffectBase>& effectBase) {
3658 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003659 if (effect == nullptr) {
3660 return;
3661 }
3662 effect->stop();
3663}
3664
Glenn Kasten63238ef2015-03-02 15:50:29 -08003665} // namespace android