blob: d482d61034da35baf082c82c02065106aa181368 [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 Hung6ac17eb2023-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 Hung6ac17eb2023-06-20 18:56:17 -070097#define LOG_TAG "EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080098
Andy Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-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 Hung6ac17eb2023-06-20 18:56:17 -0700503void EffectBase::dump(int fd, const Vector<String16>& args __unused) const
Andy Hung920f6572022-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 }
François Gaffie163ce832023-06-28 11:36:35 +0200515 bool isInternal = isInternal_l();
516 result.append("\t\tSession State Registered Internal Enabled Suspended:\n");
517 result.appendFormat("\t\t%05d %03d %s %s %s %s\n",
518 mSessionId, mState, mPolicyRegistered ? "y" : "n", isInternal ? "y" : "n",
519 ((isInternal && isEnabled()) || (!isInternal && mPolicyEnabled)) ? "y" : "n",
520 mSuspended ? "y" : "n");
Eric Laurent41709552019-12-16 19:34:05 -0800521
522 result.append("\t\tDescriptor:\n");
523 char uuidStr[64];
524 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
525 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
526 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
527 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
528 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
529 mDescriptor.apiVersion,
530 mDescriptor.flags,
531 effectFlagsToString(mDescriptor.flags).string());
532 result.appendFormat("\t\t- name: %s\n",
533 mDescriptor.name);
534
535 result.appendFormat("\t\t- implementor: %s\n",
536 mDescriptor.implementor);
537
538 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
539 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
540 char buffer[256];
541 for (size_t i = 0; i < mHandles.size(); ++i) {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700542 IAfEffectHandle *handle = mHandles[i];
Eric Laurent41709552019-12-16 19:34:05 -0800543 if (handle != NULL && !handle->disconnected()) {
544 handle->dumpToBuffer(buffer, sizeof(buffer));
545 result.append(buffer);
546 }
547 }
548 if (locked) {
549 mLock.unlock();
550 }
551
552 write(fd, result.string(), result.length());
553}
554
555// ----------------------------------------------------------------------------
556// EffectModule implementation
557// ----------------------------------------------------------------------------
558
559#undef LOG_TAG
Andy Hung6ac17eb2023-06-20 18:56:17 -0700560#define LOG_TAG "EffectModule"
Eric Laurent41709552019-12-16 19:34:05 -0800561
Andy Hung6ac17eb2023-06-20 18:56:17 -0700562EffectModule::EffectModule(const sp<EffectCallbackInterface>& callback,
Eric Laurent41709552019-12-16 19:34:05 -0800563 effect_descriptor_t *desc,
564 int id,
565 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800566 bool pinned,
567 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800568 : EffectBase(callback, desc, id, sessionId, pinned),
569 // clear mConfig to ensure consistent initial value of buffer framecount
570 // in case buffers are associated by setInBuffer() or setOutBuffer()
571 // prior to configure().
572 mConfig{{}, {}},
573 mStatus(NO_INIT),
574 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
575 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800576 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000577 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800578 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800579{
580 ALOGV("Constructor %p pinned %d", this, pinned);
581 int lStatus;
582
583 // create effect engine from effect factory
584 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800585 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800586 if (mStatus != NO_ERROR) {
587 return;
588 }
589 lStatus = init();
590 if (lStatus < 0) {
591 mStatus = lStatus;
592 goto Error;
593 }
594
595 setOffloaded(callback->isOffload(), callback->io());
596 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
597
598 return;
599Error:
600 mEffectInterface.clear();
601 ALOGV("Constructor Error %d", mStatus);
602}
603
Andy Hung6ac17eb2023-06-20 18:56:17 -0700604EffectModule::~EffectModule()
Eric Laurent41709552019-12-16 19:34:05 -0800605{
606 ALOGV("Destructor %p", this);
607 if (mEffectInterface != 0) {
608 char uuidStr[64];
609 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
610 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
611 this, uuidStr);
612 release_l();
613 }
614
615}
616
Andy Hung6ac17eb2023-06-20 18:56:17 -0700617bool EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 Mutex::Autolock _l(mLock);
619
Eric Laurentfa1e1232016-08-02 19:01:49 -0700620 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800621 switch (mState) {
622 case RESTART:
623 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700624 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800625
626 case STARTING:
627 // clear auxiliary effect input buffer for next accumulation
628 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
629 memset(mConfig.inputCfg.buffer.raw,
630 0,
631 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
632 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700633 if (start_l() == NO_ERROR) {
634 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700635 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700636 } else {
637 mState = IDLE;
638 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800639 break;
640 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900641 // volume control for offload and direct threads must take effect immediately.
642 if (stop_l() == NO_ERROR
643 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700644 mDisableWaitCnt = mMaxDisableWaitCnt;
645 } else {
646 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
647 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800648 mState = STOPPED;
649 break;
650 case STOPPED:
651 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
652 // turn off sequence.
653 if (--mDisableWaitCnt == 0) {
654 reset_l();
655 mState = IDLE;
656 }
657 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200658 case ACTIVE:
659 for (size_t i = 0; i < mHandles.size(); i++) {
660 if (!mHandles[i]->disconnected()) {
661 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
662 }
663 }
664 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800665 default: //IDLE , ACTIVE, DESTROYED
666 break;
667 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700668
669 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800670}
671
Andy Hung6ac17eb2023-06-20 18:56:17 -0700672void EffectModule::process()
Eric Laurentca7cc822012-11-19 14:55:58 -0800673{
674 Mutex::Autolock _l(mLock);
675
Mikhail Naganov022b9952017-01-04 16:36:51 -0800676 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800677 return;
678 }
679
rago94a1ee82017-07-21 15:11:02 -0700680 const uint32_t inChannelCount =
681 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
682 const uint32_t outChannelCount =
683 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
684 const bool auxType =
685 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
686
Andy Hungfa69ca32017-11-30 10:07:53 -0800687 // safeInputOutputSampleCount is 0 if the channel count between input and output
688 // buffers do not match. This prevents automatic accumulation or copying between the
689 // input and output effect buffers without an intermediary effect process.
690 // TODO: consider implementing channel conversion.
691 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700692 mInChannelCountRequested != mOutChannelCountRequested ? 0
693 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 mConfig.inputCfg.buffer.frameCount,
695 mConfig.outputCfg.buffer.frameCount);
696 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800697 accumulate_float(
698 mConfig.outputCfg.buffer.f32,
699 mConfig.inputCfg.buffer.f32,
700 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800701 };
702 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800703 memcpy(
704 mConfig.outputCfg.buffer.f32,
705 mConfig.inputCfg.buffer.f32,
706 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800707 };
708
Eric Laurentca7cc822012-11-19 14:55:58 -0800709 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700710 int ret;
711 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700712 if (auxType) {
713 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800714 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700715
Andy Hung26836922023-05-22 17:31:57 -0700716 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800717 memcpy_to_i16_from_float(
718 mConfig.inputCfg.buffer.s16,
719 mConfig.inputCfg.buffer.f32,
720 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700721 }
rago94a1ee82017-07-21 15:11:02 -0700722 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800723 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
724 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
725
726 if (!auxType && mInChannelCountRequested != inChannelCount) {
727 adjust_channels(
728 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
729 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
730 sizeof(float),
731 sizeof(float)
732 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
733 inBuffer = mInConversionBuffer;
734 }
735 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
736 && mOutChannelCountRequested != outChannelCount) {
737 adjust_selected_channels(
738 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
739 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
740 sizeof(float),
741 sizeof(float)
742 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
743 outBuffer = mOutConversionBuffer;
744 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800745 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
746 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800747 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
749 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700750 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 memcpy_to_i16_from_float(
752 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800753 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800754 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800755 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700756 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800757 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800758 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800759 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
760 goto data_bypass;
761 }
762 memcpy_to_i16_from_float(
763 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800764 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800765 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800766 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700767 }
768 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800769 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 sp<EffectBufferHalInterface> target =
772 mOutChannelCountRequested != outChannelCount
773 ? mOutConversionBuffer : mOutBuffer;
774
Andy Hungfa69ca32017-11-30 10:07:53 -0800775 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800776 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800777 mOutConversionBuffer->audioBuffer()->s16,
778 outChannelCount * mConfig.outputCfg.buffer.frameCount);
779 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800780 if (mOutChannelCountRequested != outChannelCount) {
781 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
782 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
783 sizeof(float),
784 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
785 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700786 } else {
rago94a1ee82017-07-21 15:11:02 -0700787 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700788 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700790 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700792 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700794 }
795 }
796 ret = -ENODATA;
797 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800798
Eric Laurentca7cc822012-11-19 14:55:58 -0800799 // force transition to IDLE state when engine is ready
800 if (mState == STOPPED && ret == -ENODATA) {
801 mDisableWaitCnt = 1;
802 }
803
804 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700805 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800806 const size_t size =
807 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700808 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800809 }
810 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700811 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800812 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
813 // If an insert effect is idle and input buffer is different from output buffer,
814 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700815 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700816 // similar handling with data_bypass above.
817 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
818 accumulateInputToOutput();
819 } else { // EFFECT_BUFFER_ACCESS_WRITE
820 copyInputToOutput();
821 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800822 }
823 }
824}
825
Andy Hung6ac17eb2023-06-20 18:56:17 -0700826void EffectModule::reset_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800827{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700828 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800829 return;
830 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700831 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800832}
833
Andy Hung6ac17eb2023-06-20 18:56:17 -0700834status_t EffectModule::configure()
Eric Laurentca7cc822012-11-19 14:55:58 -0800835{
rago94a1ee82017-07-21 15:11:02 -0700836 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700837 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838 uint32_t size;
839 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700840 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700842 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700843 status = NO_INIT;
844 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800845 }
846
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800848 // TODO: handle configuration of input (record) SW effects above the HAL,
849 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
850 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700851 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200852 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800853 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200854 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800855
856 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800857 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
858 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
859 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
860 mConfig.inputCfg.channels);
861 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800862 }
jiabineb3bda02020-06-30 14:07:03 -0700863 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700864 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700865 mConfig.inputCfg.channels |= hapticChannelMask;
866 mConfig.outputCfg.channels |= hapticChannelMask;
867 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800868 mInChannelCountRequested =
869 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
870 mOutChannelCountRequested =
871 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700872
Andy Hung319587b2023-05-23 14:01:03 -0700873 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
874 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900875
876 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700877 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900878 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
879 ALOGV("Overriding effect input as 48kHz");
880 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700881 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900882 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800883 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
884 mConfig.inputCfg.bufferProvider.cookie = NULL;
885 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
886 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
887 mConfig.outputCfg.bufferProvider.cookie = NULL;
888 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
889 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
890 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
891 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800892 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800893 // always overwrites output buffer: input buffer == output buffer
894 // - in other sessions:
895 // last effect in the chain accumulates in output buffer: input buffer != output buffer
896 // other effect: overwrites output buffer: input buffer == output buffer
897 // Auxiliary effect:
898 // accumulates in output buffer: input buffer != output buffer
899 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700900 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800901 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
902 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700903 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800904 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000905 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800906
Eric Laurent6b446ce2019-12-13 10:56:31 -0800907 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700908 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800909 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800910
911 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700912 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700913 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800914 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700915 &mConfig,
916 &size,
917 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700918 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800919 status = cmdStatus;
920 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800921
Andy Hung9aad48c2017-11-29 10:29:19 -0800922 if (status != NO_ERROR &&
Andy Hungba8e52b2023-05-11 14:33:03 -0700923 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000924 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800925 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
926 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
927 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700928 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
929 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800930 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
931 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
932 }
933 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
934 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
935 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
936 }
937 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700938 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800939 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700940 &mConfig,
941 &size,
942 &cmdStatus);
943 if (status == NO_ERROR) {
944 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800945 }
946 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800947
Andy Hung9aad48c2017-11-29 10:29:19 -0800948 if (status == NO_ERROR) {
949 mSupportsFloat = true;
950 }
951
Andy Hungba8e52b2023-05-11 14:33:03 -0700952 // only HIDL effects support integer conversion.
953 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800954 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
955 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
956 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
957 size = sizeof(int);
958 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
959 sizeof(mConfig),
960 &mConfig,
961 &size,
962 &cmdStatus);
963 if (status == NO_ERROR) {
964 status = cmdStatus;
965 }
966 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700967 mSupportsFloat = false;
968 ALOGVV("config worked with 16 bit");
969 } else {
970 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800971 }
rago94a1ee82017-07-21 15:11:02 -0700972 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800973
rago94a1ee82017-07-21 15:11:02 -0700974 if (status == NO_ERROR) {
975 // Establish Buffer strategy
976 setInBuffer(mInBuffer);
977 setOutBuffer(mOutBuffer);
978
979 // Update visualizer latency
980 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
981 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
982 effect_param_t *p = (effect_param_t *)buf32;
983
984 p->psize = sizeof(uint32_t);
985 p->vsize = sizeof(uint32_t);
986 size = sizeof(int);
987 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
988
Andy Hungfda44002021-06-03 17:23:16 -0700989 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700990
991 *((int32_t *)p->data + 1)= latency;
992 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
993 sizeof(effect_param_t) + 8,
994 &buf32,
995 &size,
996 &cmdStatus);
997 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800998 }
999
Andy Hung05083ac2017-12-14 15:00:28 -08001000 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1001 mMaxDisableWaitCnt = (uint32_t)std::max(
1002 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
François Gaffieb8551f62023-02-15 11:36:35 +01001003 (uint64_t)mConfig.outputCfg.buffer.frameCount == 0 ? 1
1004 : (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1005 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount)));
Eric Laurentca7cc822012-11-19 14:55:58 -08001006
Eric Laurentd0ebb532013-04-02 16:41:41 -07001007exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001008 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001009 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001010 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001011 return status;
1012}
1013
Andy Hung6ac17eb2023-06-20 18:56:17 -07001014status_t EffectModule::init()
Eric Laurentca7cc822012-11-19 14:55:58 -08001015{
1016 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001017 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 return NO_INIT;
1019 }
1020 status_t cmdStatus;
1021 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001022 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1023 0,
1024 NULL,
1025 &size,
1026 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 if (status == 0) {
1028 status = cmdStatus;
1029 }
1030 return status;
1031}
1032
Andy Hung6ac17eb2023-06-20 18:56:17 -07001033void EffectModule::addEffectToHal_l()
Eric Laurent1b928682014-10-02 19:41:47 -07001034{
1035 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1036 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001037 if (mCurrentHalStream == getCallback()->io()) {
David Li6c8ac4b2021-06-22 22:17:52 +08001038 return;
1039 }
1040
Andy Hungfda44002021-06-03 17:23:16 -07001041 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001042 mCurrentHalStream = getCallback()->io();
Eric Laurent1b928682014-10-02 19:41:47 -07001043 }
1044}
1045
Eric Laurentfa1e1232016-08-02 19:01:49 -07001046// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -07001047status_t EffectModule::start()
Eric Laurentca7cc822012-11-19 14:55:58 -08001048{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001049 status_t status;
1050 {
1051 Mutex::Autolock _l(mLock);
1052 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001053 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001054 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001055 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001056 }
1057 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001058}
1059
Andy Hung6ac17eb2023-06-20 18:56:17 -07001060status_t EffectModule::start_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08001061{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001062 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001063 return NO_INIT;
1064 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065 if (mStatus != NO_ERROR) {
1066 return mStatus;
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 status_t cmdStatus;
1069 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001070 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1071 0,
1072 NULL,
1073 &size,
1074 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 if (status == 0) {
1076 status = cmdStatus;
1077 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001078 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001079 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 }
1081 return status;
1082}
1083
Andy Hung6ac17eb2023-06-20 18:56:17 -07001084status_t EffectModule::stop()
Eric Laurentca7cc822012-11-19 14:55:58 -08001085{
1086 Mutex::Autolock _l(mLock);
1087 return stop_l();
1088}
1089
Andy Hung6ac17eb2023-06-20 18:56:17 -07001090status_t EffectModule::stop_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08001091{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001092 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 return NO_INIT;
1094 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001095 if (mStatus != NO_ERROR) {
1096 return mStatus;
1097 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001098 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001100
1101 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001102 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1103 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1104 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001105 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001106 mSetVolumeReentrantTid = INVALID_PID;
1107 }
1108
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001109 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1110 0,
1111 NULL,
1112 &size,
1113 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001114 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001115 status = cmdStatus;
1116 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001117 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001118 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001119 }
1120 return status;
1121}
1122
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001123// must be called with EffectChain::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -07001124void EffectModule::release_l()
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001125{
1126 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001127 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001128 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001129 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001130 mEffectInterface.clear();
1131 }
1132}
1133
Andy Hung6ac17eb2023-06-20 18:56:17 -07001134status_t EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001135{
1136 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1137 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001138 if (mCurrentHalStream != getCallback()->io()) {
1139 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
David Li6c8ac4b2021-06-22 22:17:52 +08001140 }
Andy Hungfda44002021-06-03 17:23:16 -07001141 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001142 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001143 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001144 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001145}
1146
Andy Hunge4a1d912016-08-17 14:11:13 -07001147// round up delta valid if value and divisor are positive.
1148template <typename T>
1149static T roundUpDelta(const T &value, const T &divisor) {
1150 T remainder = value % divisor;
1151 return remainder == 0 ? 0 : divisor - remainder;
1152}
1153
Andy Hung6ac17eb2023-06-20 18:56:17 -07001154status_t EffectModule::command(int32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001155 const std::vector<uint8_t>& cmdData,
1156 int32_t maxReplySize,
1157 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001158{
1159 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001160 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001161
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001162 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001163 return NO_INIT;
1164 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001165 if (mStatus != NO_ERROR) {
1166 return mStatus;
1167 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001168 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1169 return -EINVAL;
1170 }
1171 size_t cmdSize = cmdData.size();
1172 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1173 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1174 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001175 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001176 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001177 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001178 android_errorWriteLog(0x534e4554, "33003822");
1179 return -EINVAL;
1180 }
1181 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001182 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001183 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001184 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001185 return -EINVAL;
1186 }
ragoe2759072016-11-22 18:02:48 -08001187 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001188 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001189 || param->psize > maxReplySize - sizeof(effect_param_t)
1190 || param->vsize > maxReplySize - sizeof(effect_param_t)
1191 - param->psize
1192 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1193 maxReplySize
1194 - sizeof(effect_param_t)
1195 - param->psize
1196 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001197 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1198 android_errorWriteLog(0x534e4554, "32705438");
1199 return -EINVAL;
1200 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001201 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001202 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1203 && // DEFERRED not generally used
1204 (param == nullptr
1205 || param->psize > cmdSize - sizeof(effect_param_t)
1206 || param->vsize > cmdSize - sizeof(effect_param_t)
1207 - param->psize
1208 || roundUpDelta(param->psize,
1209 (uint32_t) sizeof(int)) >
1210 cmdSize
1211 - sizeof(effect_param_t)
1212 - param->psize
1213 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001214 android_errorWriteLog(0x534e4554, "30204301");
1215 return -EINVAL;
1216 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001217 uint32_t replySize = maxReplySize;
1218 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001219 status_t status = mEffectInterface->command(cmdCode,
1220 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001221 const_cast<uint8_t*>(cmdData.data()),
1222 &replySize,
1223 reply->data());
1224 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001225 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001226 for (size_t i = 1; i < mHandles.size(); i++) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07001227 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001228 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001229 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001230 }
1231 }
1232 }
1233 return status;
1234}
1235
Andy Hung6ac17eb2023-06-20 18:56:17 -07001236bool EffectModule::isProcessEnabled() const
Eric Laurentca7cc822012-11-19 14:55:58 -08001237{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001238 if (mStatus != NO_ERROR) {
1239 return false;
1240 }
1241
Eric Laurentca7cc822012-11-19 14:55:58 -08001242 switch (mState) {
1243 case RESTART:
1244 case ACTIVE:
1245 case STOPPING:
1246 case STOPPED:
1247 return true;
1248 case IDLE:
1249 case STARTING:
1250 case DESTROYED:
1251 default:
1252 return false;
1253 }
1254}
1255
Andy Hung6ac17eb2023-06-20 18:56:17 -07001256bool EffectModule::isOffloadedOrDirect() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001257{
Andy Hungfda44002021-06-03 17:23:16 -07001258 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001259}
1260
Andy Hung6ac17eb2023-06-20 18:56:17 -07001261bool EffectModule::isVolumeControlEnabled() const
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001262{
1263 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1264}
1265
Andy Hung6ac17eb2023-06-20 18:56:17 -07001266void EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001267 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001268
1269 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001270 if (buffer != 0) {
1271 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1272 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1273 } else {
1274 mConfig.inputCfg.buffer.raw = NULL;
1275 }
1276 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001277 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001278
Andy Hungbded9c82017-11-30 18:47:35 -08001279 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001280 // Theoretically insert effects can also do in-place conversions (destroying
1281 // the original buffer) when the output buffer is identical to the input buffer,
1282 // but we don't optimize for it here.
1283 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001284 const uint32_t inChannelCount =
1285 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1286 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001287 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001288 // we need to translate - create hidl shared buffer and intercept
1289 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001290 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1291 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1292 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001293
1294 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1295 __func__, inChannels, inFrameCount, size);
1296
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001297 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001298 || size > mInConversionBuffer->getSize())) {
1299 mInConversionBuffer.clear();
1300 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001301 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001302 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001303 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001304 mInConversionBuffer->setFrameCount(inFrameCount);
1305 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001306 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001307 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001308 }
1309 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001310}
1311
Andy Hung6ac17eb2023-06-20 18:56:17 -07001312void EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001313 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001314
1315 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001316 if (buffer != 0) {
1317 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1318 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1319 } else {
1320 mConfig.outputCfg.buffer.raw = NULL;
1321 }
1322 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001323 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001324
Andy Hungbded9c82017-11-30 18:47:35 -08001325 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001326 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001327 const uint32_t outChannelCount =
1328 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1329 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001330 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001331 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001332 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1333 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1334 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001335
1336 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1337 __func__, outChannels, outFrameCount, size);
1338
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001339 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001340 || size > mOutConversionBuffer->getSize())) {
1341 mOutConversionBuffer.clear();
1342 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001343 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001344 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001345 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001346 mOutConversionBuffer->setFrameCount(outFrameCount);
1347 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001348 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001349 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001350 }
1351 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001352}
1353
Andy Hung6ac17eb2023-06-20 18:56:17 -07001354status_t EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
Eric Laurentca7cc822012-11-19 14:55:58 -08001355{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001356 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001357 if (mStatus != NO_ERROR) {
1358 return mStatus;
1359 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001360 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001361 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1362 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1363 if (isProcessEnabled() &&
1364 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001365 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1366 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin4e246532022-08-23 16:37:30 -07001367 status = setVolumeInternal(left, right, controller);
1368 }
1369 return status;
1370}
1371
Andy Hung6ac17eb2023-06-20 18:56:17 -07001372status_t EffectModule::setVolumeInternal(
jiabin4e246532022-08-23 16:37:30 -07001373 uint32_t *left, uint32_t *right, bool controller) {
1374 uint32_t volume[2] = {*left, *right};
1375 uint32_t *pVolume = controller ? volume : nullptr;
1376 uint32_t size = sizeof(volume);
1377 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1378 size,
1379 volume,
1380 &size,
1381 pVolume);
1382 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1383 *left = volume[0];
1384 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 }
1386 return status;
1387}
1388
Andy Hung6ac17eb2023-06-20 18:56:17 -07001389void EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001390{
Zhou Songd505c642020-02-20 16:35:37 +08001391 // for offload or direct thread, if the effect chain has non-offloadable
1392 // effect and any effect module within the chain has volume control, then
1393 // volume control is delegated to effect, otherwise, set volume to hal.
1394 if (mEffectCallback->isOffloadOrDirect() &&
1395 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001396 float vol_l = (float)left / (1 << 24);
1397 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001398 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001399 }
1400}
1401
Andy Hung6ac17eb2023-06-20 18:56:17 -07001402status_t EffectModule::sendSetAudioDevicesCommand(
jiabin8f278ee2019-11-11 12:16:27 -08001403 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001404{
jiabin8f278ee2019-11-11 12:16:27 -08001405 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1406 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 return NO_ERROR;
1408 }
1409
1410 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001411 if (mStatus != NO_ERROR) {
1412 return mStatus;
1413 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001415 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 status_t cmdStatus;
1417 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001418 // FIXME: use audio device types and addresses when the hal interface is ready.
1419 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001420 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001421 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001422 &size,
1423 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 }
1425 return status;
1426}
1427
Andy Hung6ac17eb2023-06-20 18:56:17 -07001428status_t EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
jiabin8f278ee2019-11-11 12:16:27 -08001429{
1430 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1431}
1432
Andy Hung6ac17eb2023-06-20 18:56:17 -07001433status_t EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08001434{
1435 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1436}
1437
Andy Hung6ac17eb2023-06-20 18:56:17 -07001438status_t EffectModule::setMode(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001439{
1440 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001441 if (mStatus != NO_ERROR) {
1442 return mStatus;
1443 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 status_t status = NO_ERROR;
1445 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1446 status_t cmdStatus;
1447 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001448 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1449 sizeof(audio_mode_t),
1450 &mode,
1451 &size,
1452 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 if (status == NO_ERROR) {
1454 status = cmdStatus;
1455 }
1456 }
1457 return status;
1458}
1459
Andy Hung6ac17eb2023-06-20 18:56:17 -07001460status_t EffectModule::setAudioSource(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08001461{
1462 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001463 if (mStatus != NO_ERROR) {
1464 return mStatus;
1465 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001466 status_t status = NO_ERROR;
1467 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1468 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001469 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1470 sizeof(audio_source_t),
1471 &source,
1472 &size,
1473 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 }
1475 return status;
1476}
1477
Andy Hung6ac17eb2023-06-20 18:56:17 -07001478status_t EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
Eric Laurent5baf2af2013-09-12 17:37:00 -07001479{
1480 Mutex::Autolock _l(mLock);
1481 if (mStatus != NO_ERROR) {
1482 return mStatus;
1483 }
1484 status_t status = NO_ERROR;
1485 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1486 status_t cmdStatus;
1487 uint32_t size = sizeof(status_t);
1488 effect_offload_param_t cmd;
1489
1490 cmd.isOffload = offloaded;
1491 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001492 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1493 sizeof(effect_offload_param_t),
1494 &cmd,
1495 &size,
1496 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001497 if (status == NO_ERROR) {
1498 status = cmdStatus;
1499 }
1500 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1501 } else {
1502 if (offloaded) {
1503 status = INVALID_OPERATION;
1504 }
1505 mOffloaded = false;
1506 }
1507 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1508 return status;
1509}
1510
Andy Hung6ac17eb2023-06-20 18:56:17 -07001511bool EffectModule::isOffloaded() const
Eric Laurent5baf2af2013-09-12 17:37:00 -07001512{
1513 Mutex::Autolock _l(mLock);
1514 return mOffloaded;
1515}
1516
jiabineb3bda02020-06-30 14:07:03 -07001517/*static*/
Andy Hung6ac17eb2023-06-20 18:56:17 -07001518bool IAfEffectModule::isHapticGenerator(const effect_uuid_t *type) {
jiabineb3bda02020-06-30 14:07:03 -07001519 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1520}
1521
Andy Hung6ac17eb2023-06-20 18:56:17 -07001522bool EffectModule::isHapticGenerator() const {
1523 return IAfEffectModule::isHapticGenerator(&mDescriptor.type);
jiabineb3bda02020-06-30 14:07:03 -07001524}
1525
Andy Hung6ac17eb2023-06-20 18:56:17 -07001526status_t EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001527{
1528 if (mStatus != NO_ERROR) {
1529 return mStatus;
1530 }
1531 if (!isHapticGenerator()) {
1532 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1533 return INVALID_OPERATION;
1534 }
1535
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001536 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1537 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001538 param->psize = sizeof(int32_t);
1539 param->vsize = sizeof(int32_t) * 2;
1540 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1541 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001542 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001543 std::vector<uint8_t> response;
1544 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001545 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001546 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1547 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001548 }
1549 return status;
1550}
1551
Andy Hung6ac17eb2023-06-20 18:56:17 -07001552status_t EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001553{
1554 if (mStatus != NO_ERROR) {
1555 return mStatus;
1556 }
1557 if (!isHapticGenerator()) {
1558 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1559 return INVALID_OPERATION;
1560 }
1561
Lais Andradebc3f37a2021-07-02 00:13:19 +01001562 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001563 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001564 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001565 effect_param_t *param = (effect_param_t*) request.data();
1566 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001567 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001568 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1569 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001570 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1571 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1572 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001573 std::vector<uint8_t> response;
1574 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1575 if (status == NO_ERROR) {
1576 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1577 status = *reinterpret_cast<const status_t*>(response.data());
1578 }
1579 return status;
1580}
1581
Andy Hung6ac17eb2023-06-20 18:56:17 -07001582status_t EffectModule::getConfigs(
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001583 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1584 Mutex::Autolock _l(mLock);
1585 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1586 return NO_INIT;
1587 }
1588 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1589 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1590 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1591 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1592 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1593 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1594 *isOutput = mIsOutput;
1595 return NO_ERROR;
1596}
1597
Andy Hungbded9c82017-11-30 18:47:35 -08001598static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1599 std::stringstream ss;
1600
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001601 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001602 return "nullptr"; // make different than below
1603 } else if (buffer->externalData() != nullptr) {
1604 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1605 << " -> "
1606 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1607 } else {
1608 ss << buffer->audioBuffer()->raw;
1609 }
1610 return ss.str();
1611}
Marco Nelissenb2208842014-02-07 14:00:50 -08001612
Andy Hung6ac17eb2023-06-20 18:56:17 -07001613void EffectModule::dump(int fd, const Vector<String16>& args) const
Andy Hung920f6572022-10-06 12:09:49 -07001614NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001615{
Eric Laurent41709552019-12-16 19:34:05 -08001616 EffectBase::dump(fd, args);
1617
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001620
Eric Laurent41709552019-12-16 19:34:05 -08001621 result.append("\t\tStatus Engine:\n");
1622 result.appendFormat("\t\t%03d %p\n",
1623 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001624
1625 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001626
1627 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001628 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1629 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1630 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 mConfig.inputCfg.buffer.frameCount,
1632 mConfig.inputCfg.samplingRate,
1633 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001634 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001635 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001636
1637 result.append("\t\t- Output configuration:\n");
1638 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001639 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001640 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 mConfig.outputCfg.buffer.frameCount,
1642 mConfig.outputCfg.samplingRate,
1643 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001644 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001645 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001646
Andy Hungbded9c82017-11-30 18:47:35 -08001647 result.appendFormat("\t\t- HAL buffers:\n"
1648 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1649 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1650 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1651 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1652 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001653
Eric Laurentca7cc822012-11-19 14:55:58 -08001654 write(fd, result.string(), result.length());
1655
Mikhail Naganov4d547672019-02-22 14:19:19 -08001656 if (mEffectInterface != 0) {
1657 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1658 (void)mEffectInterface->dump(fd);
1659 }
1660
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 if (locked) {
1662 mLock.unlock();
1663 }
1664}
1665
1666// ----------------------------------------------------------------------------
1667// EffectHandle implementation
1668// ----------------------------------------------------------------------------
1669
1670#undef LOG_TAG
Andy Hung6ac17eb2023-06-20 18:56:17 -07001671#define LOG_TAG "EffectHandle"
Eric Laurentca7cc822012-11-19 14:55:58 -08001672
Andy Hung6ac17eb2023-06-20 18:56:17 -07001673/* static */
1674sp<IAfEffectHandle> IAfEffectHandle::create(
1675 const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -07001676 const sp<Client>& client,
Andy Hung6ac17eb2023-06-20 18:56:17 -07001677 const sp<media::IEffectClient>& effectClient,
1678 int32_t priority, bool notifyFramesProcessed)
1679{
1680 return sp<EffectHandle>::make(
Andy Hung59867e42023-06-27 17:05:02 -07001681 effect, client, effectClient, priority, notifyFramesProcessed);
Andy Hung6ac17eb2023-06-20 18:56:17 -07001682}
1683
1684EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -07001685 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001686 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001687 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 : BnEffect(),
1689 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001690 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1691 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001692{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001693 ALOGV("constructor %p client %p", this, client.get());
Andy Hung225aef62022-12-06 16:33:20 -08001694 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001695
1696 if (client == 0) {
1697 return;
1698 }
1699 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001700 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1701 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1702 std::string("Effect ID: ")
1703 .append(std::to_string(effect->id()))
1704 .append(" Session ID: ")
1705 .append(std::to_string(static_cast<int>(effect->sessionId())))
1706 .append(" \n")
1707 });
Glenn Kastene75da402013-11-20 13:54:52 -08001708 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001709 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001710 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001712 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 return;
1714 }
Glenn Kastene75da402013-11-20 13:54:52 -08001715 new(mCblk) effect_param_cblk_t();
1716 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001717}
1718
Andy Hung6ac17eb2023-06-20 18:56:17 -07001719EffectHandle::~EffectHandle()
Eric Laurentca7cc822012-11-19 14:55:58 -08001720{
1721 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001722 disconnect(false);
1723}
1724
Andy Hungc747c532022-03-07 21:41:14 -08001725// Creates an association between Binder code to name for IEffect.
1726#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1727BINDER_METHOD_ENTRY(enable) \
1728BINDER_METHOD_ENTRY(disable) \
1729BINDER_METHOD_ENTRY(command) \
1730BINDER_METHOD_ENTRY(disconnect) \
1731BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001732BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001733
1734// singleton for Binder Method Statistics for IEffect
1735mediautils::MethodStatistics<int>& getIEffectStatistics() {
1736 using Code = int;
1737
1738#pragma push_macro("BINDER_METHOD_ENTRY")
1739#undef BINDER_METHOD_ENTRY
1740#define BINDER_METHOD_ENTRY(ENTRY) \
1741 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1742
1743 static mediautils::MethodStatistics<Code> methodStatistics{
1744 IEFFECT_BINDER_METHOD_MACRO_LIST
1745 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1746 };
1747#pragma pop_macro("BINDER_METHOD_ENTRY")
1748
1749 return methodStatistics;
1750}
1751
Andy Hung6ac17eb2023-06-20 18:56:17 -07001752status_t EffectHandle::onTransact(
Andy Hungc747c532022-03-07 21:41:14 -08001753 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001754 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1755 mediautils::TimeCheck check(
1756 std::string("IEffect::").append(methodName),
1757 [code](bool timeout, float elapsedMs) {
1758 if (timeout) {
1759 ; // we don't timeout right now on the effect interface.
1760 } else {
1761 getIEffectStatistics().event(code, elapsedMs);
1762 }
Andy Hungf8ab0932022-06-13 19:49:43 -07001763 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001764 return BnEffect::onTransact(code, data, reply, flags);
1765}
1766
Andy Hung6ac17eb2023-06-20 18:56:17 -07001767status_t EffectHandle::initCheck() const
Glenn Kastene75da402013-11-20 13:54:52 -08001768{
1769 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1770}
1771
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772#define RETURN(code) \
1773 *_aidl_return = (code); \
1774 return Status::ok();
1775
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001776#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1777 ({ \
1778 auto _tmp = (exp); \
1779 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1780 std::move(_tmp.value()); \
1781 })
1782
Andy Hung6ac17eb2023-06-20 18:56:17 -07001783Status EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001784{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001785 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 ALOGV("enable %p", this);
Andy Hung6ac17eb2023-06-20 18:56:17 -07001787 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001788 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001789 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001792 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001794
1795 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001796 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 }
1798
1799 mEnabled = true;
1800
Eric Laurent6c796322019-04-09 14:13:17 -07001801 status_t status = effect->updatePolicyState();
1802 if (status != NO_ERROR) {
1803 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001804 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001805 }
1806
Eric Laurent6b446ce2019-12-13 10:56:31 -08001807 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001808
1809 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001810 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001811 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 }
1813
Eric Laurent6b446ce2019-12-13 10:56:31 -08001814 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 mEnabled = false;
1817 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001818 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001819}
1820
Andy Hung6ac17eb2023-06-20 18:56:17 -07001821Status EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001822{
1823 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001824 AutoMutex _l(mLock);
Andy Hung6ac17eb2023-06-20 18:56:17 -07001825 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001826 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001827 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001828 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001832
1833 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001834 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 }
1836 mEnabled = false;
1837
Eric Laurent6c796322019-04-09 14:13:17 -07001838 effect->updatePolicyState();
1839
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001840 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001842 }
1843
Eric Laurent6b446ce2019-12-13 10:56:31 -08001844 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001845 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001846}
1847
Andy Hung6ac17eb2023-06-20 18:56:17 -07001848Status EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001849{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001852 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001853}
1854
Andy Hung6ac17eb2023-06-20 18:56:17 -07001855void EffectHandle::disconnect(bool unpinIfLast)
Eric Laurentca7cc822012-11-19 14:55:58 -08001856{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 AutoMutex _l(mLock);
1858 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1859 if (mDisconnected) {
1860 if (unpinIfLast) {
1861 android_errorWriteLog(0x534e4554, "32707507");
1862 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001863 return;
1864 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001865 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001866 {
Andy Hung6ac17eb2023-06-20 18:56:17 -07001867 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001868 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001869 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001870 ALOGW("%s Effect handle %p disconnected after thread destruction",
1871 __func__, this);
1872 }
1873 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001874 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001875 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001876
Eric Laurentca7cc822012-11-19 14:55:58 -08001877 if (mClient != 0) {
1878 if (mCblk != NULL) {
1879 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1880 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1881 }
1882 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001883 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung920f6572022-10-06 12:09:49 -07001884 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001885 mClient.clear();
1886 }
1887}
1888
Andy Hung6ac17eb2023-06-20 18:56:17 -07001889Status EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1891 return Status::ok();
1892}
1893
Andy Hung6ac17eb2023-06-20 18:56:17 -07001894Status EffectHandle::getConfig(
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001895 media::EffectConfig* _config, int32_t* _aidl_return) {
1896 AutoMutex _l(mLock);
Andy Hung6ac17eb2023-06-20 18:56:17 -07001897 sp<IAfEffectBase> effect = mEffect.promote();
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001898 if (effect == nullptr || mDisconnected) {
1899 RETURN(DEAD_OBJECT);
1900 }
Andy Hung6ac17eb2023-06-20 18:56:17 -07001901 sp<IAfEffectModule> effectModule = effect->asEffectModule();
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001902 if (effectModule == nullptr) {
1903 RETURN(INVALID_OPERATION);
1904 }
1905 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1906 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1907 bool isOutput;
1908 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1909 if (status == NO_ERROR) {
1910 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1911 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1912 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1913 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1914 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1915 _config->isOnInputStream = !isOutput;
1916 }
1917 RETURN(status);
1918}
1919
Andy Hung6ac17eb2023-06-20 18:56:17 -07001920Status EffectHandle::command(int32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 const std::vector<uint8_t>& cmdData,
1922 int32_t maxResponseSize,
1923 std::vector<uint8_t>* response,
1924 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001925{
1926 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001927 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001928
Eric Laurentc7ab3092017-06-15 18:43:46 -07001929 // reject commands reserved for internal use by audio framework if coming from outside
1930 // of audioserver
1931 switch(cmdCode) {
1932 case EFFECT_CMD_ENABLE:
1933 case EFFECT_CMD_DISABLE:
1934 case EFFECT_CMD_SET_PARAM:
1935 case EFFECT_CMD_SET_PARAM_DEFERRED:
1936 case EFFECT_CMD_SET_PARAM_COMMIT:
1937 case EFFECT_CMD_GET_PARAM:
1938 break;
1939 default:
1940 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1941 break;
1942 }
1943 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001944 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001945 }
1946
Eric Laurent1ffc5852016-12-15 14:46:09 -08001947 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07001948 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001949 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001950 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001951 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001952 writeToBuffer(NO_ERROR, response);
1953 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001954 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07001955 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001956 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001957 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001958 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001959 writeToBuffer(NO_ERROR, response);
1960 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001961 }
1962
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001963 AutoMutex _l(mLock);
Andy Hung6ac17eb2023-06-20 18:56:17 -07001964 sp<IAfEffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001965 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001966 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001967 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 // only get parameter command is permitted for applications not controlling the effect
1969 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001970 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001971 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001972
1973 // handle commands that are not forwarded transparently to effect engine
1974 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001975 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001976 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001977 }
1978
Andy Hung920f6572022-10-06 12:09:49 -07001979 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001980 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001981 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001982 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001983 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001984
Eric Laurentca7cc822012-11-19 14:55:58 -08001985 // No need to trylock() here as this function is executed in the binder thread serving a
1986 // particular client process: no risk to block the whole media server process or mixer
1987 // threads if we are stuck here
Andy Hung920f6572022-10-06 12:09:49 -07001988 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001989 // keep local copy of index in case of client corruption b/32220769
1990 const uint32_t clientIndex = mCblk->clientIndex;
1991 const uint32_t serverIndex = mCblk->serverIndex;
1992 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1993 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001994 mCblk->serverIndex = 0;
1995 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001996 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001997 }
1998 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001999 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002000 for (uint32_t index = serverIndex; index < clientIndex;) {
2001 int *p = (int *)(mBuffer + index);
2002 const int size = *p++;
2003 if (size < 0
2004 || size > EFFECT_PARAM_BUFFER_SIZE
2005 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002006 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002007 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002008 break;
2009 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002010
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002011 std::copy(reinterpret_cast<const uint8_t*>(p),
2012 reinterpret_cast<const uint8_t*>(p) + size,
2013 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002014
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002015 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002016 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002017 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002018 sizeof(int),
2019 &replyBuffer);
2020 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002021
2022 // verify shared memory: server index shouldn't change; client index can't go back.
2023 if (serverIndex != mCblk->serverIndex
2024 || clientIndex > mCblk->clientIndex) {
2025 android_errorWriteLog(0x534e4554, "32220769");
2026 status = BAD_VALUE;
2027 break;
2028 }
2029
Eric Laurentca7cc822012-11-19 14:55:58 -08002030 // stop at first error encountered
2031 if (ret != NO_ERROR) {
2032 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002033 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002034 break;
2035 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002036 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 break;
2038 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002039 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002040 }
2041 mCblk->serverIndex = 0;
2042 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002044 }
2045
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002046 status_t status = effect->command(cmdCode,
2047 cmdData,
2048 maxResponseSize,
2049 response);
2050 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002051}
2052
Andy Hung6ac17eb2023-06-20 18:56:17 -07002053void EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002054{
2055 ALOGV("setControl %p control %d", this, hasControl);
2056
2057 mHasControl = hasControl;
2058 mEnabled = enabled;
2059
2060 if (signal && mEffectClient != 0) {
2061 mEffectClient->controlStatusChanged(hasControl);
2062 }
2063}
2064
Andy Hung6ac17eb2023-06-20 18:56:17 -07002065void EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002066 const std::vector<uint8_t>& cmdData,
2067 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002068{
2069 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002070 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002071 }
2072}
2073
2074
2075
Andy Hung6ac17eb2023-06-20 18:56:17 -07002076void EffectHandle::setEnabled(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -08002077{
2078 if (mEffectClient != 0) {
2079 mEffectClient->enableStatusChanged(enabled);
2080 }
2081}
2082
Andy Hung6ac17eb2023-06-20 18:56:17 -07002083void EffectHandle::framesProcessed(int32_t frames) const
Eric Laurentde8caf42021-08-11 17:19:25 +02002084{
2085 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2086 mEffectClient->framesProcessed(frames);
2087 }
2088}
2089
Andy Hung6ac17eb2023-06-20 18:56:17 -07002090void EffectHandle::dumpToBuffer(char* buffer, size_t size) const
Andy Hung920f6572022-10-06 12:09:49 -07002091NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002092{
2093 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2094
Marco Nelissenb2208842014-02-07 14:00:50 -08002095 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002096 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002097 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002098 mHasControl ? "yes" : "no",
2099 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002100 mCblk ? mCblk->clientIndex : 0,
2101 mCblk ? mCblk->serverIndex : 0
2102 );
2103
2104 if (locked) {
2105 mCblk->lock.unlock();
2106 }
2107}
2108
2109#undef LOG_TAG
Andy Hung6ac17eb2023-06-20 18:56:17 -07002110#define LOG_TAG "EffectChain"
Eric Laurentca7cc822012-11-19 14:55:58 -08002111
Andy Hung6ac17eb2023-06-20 18:56:17 -07002112/* static */
2113sp<IAfEffectChain> IAfEffectChain::create(
Andy Hung87c693c2023-07-06 20:56:16 -07002114 const wp<IAfThreadBase>& wThread,
Andy Hung6ac17eb2023-06-20 18:56:17 -07002115 audio_session_t sessionId)
2116{
Andy Hung87c693c2023-07-06 20:56:16 -07002117 return sp<EffectChain>::make(wThread, sessionId);
Andy Hung6ac17eb2023-06-20 18:56:17 -07002118}
2119
Andy Hung87c693c2023-07-06 20:56:16 -07002120EffectChain::EffectChain(const wp<IAfThreadBase>& thread,
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002121 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002122 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabinbd18c8e2023-08-24 21:10:44 +00002123 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002124 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002125 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002126{
Andy Hung87c693c2023-07-06 20:56:16 -07002127 const sp<IAfThreadBase> p = thread.promote();
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002128 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002129 return;
2130 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002131 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002132 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2133 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002134}
2135
Andy Hung6ac17eb2023-06-20 18:56:17 -07002136EffectChain::~EffectChain()
Eric Laurentca7cc822012-11-19 14:55:58 -08002137{
Eric Laurentca7cc822012-11-19 14:55:58 -08002138}
2139
Andy Hung87c693c2023-07-06 20:56:16 -07002140// getEffectFromDesc_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002141sp<IAfEffectModule> EffectChain::getEffectFromDesc_l(
2142 effect_descriptor_t *descriptor) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002143{
2144 size_t size = mEffects.size();
2145
2146 for (size_t i = 0; i < size; i++) {
2147 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2148 return mEffects[i];
2149 }
2150 }
2151 return 0;
2152}
2153
Andy Hung87c693c2023-07-06 20:56:16 -07002154// getEffectFromId_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002155sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002156{
2157 size_t size = mEffects.size();
2158
2159 for (size_t i = 0; i < size; i++) {
2160 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2161 if (id == 0 || mEffects[i]->id() == id) {
2162 return mEffects[i];
2163 }
2164 }
2165 return 0;
2166}
2167
Andy Hung87c693c2023-07-06 20:56:16 -07002168// getEffectFromType_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002169sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2170 const effect_uuid_t *type) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002171{
2172 size_t size = mEffects.size();
2173
2174 for (size_t i = 0; i < size; i++) {
2175 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2176 return mEffects[i];
2177 }
2178 }
2179 return 0;
2180}
2181
Andy Hung6ac17eb2023-06-20 18:56:17 -07002182std::vector<int> EffectChain::getEffectIds() const
Eric Laurent6c796322019-04-09 14:13:17 -07002183{
2184 std::vector<int> ids;
2185 Mutex::Autolock _l(mLock);
2186 for (size_t i = 0; i < mEffects.size(); i++) {
2187 ids.push_back(mEffects[i]->id());
2188 }
2189 return ids;
2190}
2191
Andy Hung6ac17eb2023-06-20 18:56:17 -07002192void EffectChain::clearInputBuffer()
Eric Laurentca7cc822012-11-19 14:55:58 -08002193{
2194 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002195 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002196}
2197
2198// Must be called with EffectChain::mLock locked
Andy Hung6ac17eb2023-06-20 18:56:17 -07002199void EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002200{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002201 if (mInBuffer == NULL) {
2202 return;
2203 }
Andy Hung319587b2023-05-23 14:01:03 -07002204 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002205 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002206
Eric Laurent6b446ce2019-12-13 10:56:31 -08002207 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002208 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002209}
2210
2211// Must be called with EffectChain::mLock locked
Andy Hung6ac17eb2023-06-20 18:56:17 -07002212void EffectChain::process_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002213{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002214 // never process effects when:
2215 // - on an OFFLOAD thread
2216 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002217 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002218 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002219 bool tracksOnSession = (trackCnt() != 0);
2220
2221 if (!tracksOnSession && mTailBufferCount == 0) {
2222 doProcess = false;
2223 }
2224
2225 if (activeTrackCnt() == 0) {
2226 // if no track is active and the effect tail has not been rendered,
2227 // the input buffer must be cleared here as the mixer process will not do it
2228 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002229 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002230 if (mTailBufferCount > 0) {
2231 mTailBufferCount--;
2232 }
2233 }
2234 }
2235 }
2236
2237 size_t size = mEffects.size();
2238 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002239 // Only the input and output buffers of the chain can be external,
2240 // and 'update' / 'commit' do nothing for allocated buffers, thus
2241 // it's not needed to consider any other buffers here.
2242 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002243 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2244 mOutBuffer->update();
2245 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 for (size_t i = 0; i < size; i++) {
2247 mEffects[i]->process();
2248 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002249 mInBuffer->commit();
2250 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2251 mOutBuffer->commit();
2252 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002254 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002255 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002256 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2257 }
2258 if (doResetVolume) {
2259 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002260 }
2261}
2262
Andy Hung87c693c2023-07-06 20:56:16 -07002263// createEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002264status_t EffectChain::createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002265 effect_descriptor_t *desc,
2266 int id,
2267 audio_session_t sessionId,
2268 bool pinned)
2269{
2270 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002271 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002272 status_t lStatus = effect->status();
2273 if (lStatus == NO_ERROR) {
2274 lStatus = addEffect_ll(effect);
2275 }
2276 if (lStatus != NO_ERROR) {
2277 effect.clear();
2278 }
2279 return lStatus;
2280}
2281
Andy Hung87c693c2023-07-06 20:56:16 -07002282// addEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002283status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
Eric Laurentca7cc822012-11-19 14:55:58 -08002284{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002285 Mutex::Autolock _l(mLock);
2286 return addEffect_ll(effect);
2287}
Andy Hung87c693c2023-07-06 20:56:16 -07002288// addEffect_l() must be called with IAfThreadBase::mLock and EffectChain::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002289status_t EffectChain::addEffect_ll(const sp<IAfEffectModule>& effect)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002290{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002291 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002292
Eric Laurentb62d0362021-10-26 17:40:18 +02002293 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002294 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2295 // Auxiliary effects are inserted at the beginning of mEffects vector as
2296 // they are processed first and accumulated in chain input buffer
2297 mEffects.insertAt(effect, 0);
2298
2299 // the input buffer for auxiliary effect contains mono samples in
2300 // 32 bit format. This is to avoid saturation in AudoMixer
2301 // accumulation stage. Saturation is done in EffectModule::process() before
2302 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002303 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002304 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002305
Eric Laurent6b446ce2019-12-13 10:56:31 -08002306 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002307 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002308 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002309
2310 effect->configure();
2311
Mikhail Naganov022b9952017-01-04 16:36:51 -08002312 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002313 // auxiliary effects output samples to chain input buffer for further processing
2314 // by insert effects
2315 effect->setOutBuffer(mInBuffer);
2316 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002317 ssize_t idx_insert = getInsertIndex(desc);
2318 if (idx_insert < 0) {
2319 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002320 }
2321
Eric Laurentb62d0362021-10-26 17:40:18 +02002322 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002323 mEffects.insertAt(effect, idx_insert);
2324
2325 effect->configure();
2326
Eric Laurentb62d0362021-10-26 17:40:18 +02002327 // - By default:
2328 // All effects read samples from chain input buffer.
2329 // The last effect in the chain, writes samples to chain output buffer,
2330 // otherwise to chain input buffer
2331 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2332 // The spatializer effect (first effect) reads samples from the input buffer
2333 // and writes samples to the output buffer.
2334 // All other effects read and writes samples to the output buffer
2335 if (mEffectCallback->isSpatializer()
2336 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002337 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002338 if (idx_insert == 0) {
2339 if (previousSize != 0) {
2340 mEffects[1]->configure();
2341 mEffects[1]->setInBuffer(mOutBuffer);
2342 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2343 }
2344 effect->setInBuffer(mInBuffer);
2345 } else {
2346 effect->setInBuffer(mOutBuffer);
2347 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002349 effect->setInBuffer(mInBuffer);
Andy Hung920f6572022-10-06 12:09:49 -07002350 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002351 if (idx_insert != 0) {
2352 mEffects[idx_insert-1]->configure();
2353 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2354 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2355 }
2356 effect->setOutBuffer(mOutBuffer);
2357 } else {
2358 effect->setOutBuffer(mInBuffer);
2359 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002360 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002361 ALOGV("%s effect %p, added in chain %p at rank %zu",
2362 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002363 }
2364 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002365
Eric Laurentca7cc822012-11-19 14:55:58 -08002366 return NO_ERROR;
2367}
2368
jiabinbd18c8e2023-08-24 21:10:44 +00002369std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
2370 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2371 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2372 return i - 1;
2373 }
2374 }
2375 return std::nullopt;
2376}
2377
Andy Hung6ac17eb2023-06-20 18:56:17 -07002378ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002379 // Insert effects are inserted at the end of mEffects vector as they are processed
2380 // after track and auxiliary effects.
2381 // Insert effect order as a function of indicated preference:
2382 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2383 // another effect is present
2384 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2385 // last effect claiming first position
2386 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2387 // first effect claiming last position
2388 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2389 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2390 // already present
2391 // Spatializer or Downmixer effects are inserted in first position because
2392 // they adapt the channel count for all other effects in the chain
2393 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2394 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2395 return 0;
2396 }
2397
2398 size_t size = mEffects.size();
2399 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2400 ssize_t idx_insert;
2401 ssize_t idx_insert_first = -1;
2402 ssize_t idx_insert_last = -1;
2403
2404 idx_insert = size;
2405 for (size_t i = 0; i < size; i++) {
2406 effect_descriptor_t d = mEffects[i]->desc();
2407 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2408 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2409 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2410 // check invalid effect chaining combinations
2411 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2412 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2413 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2414 __func__, desc.name, d.name);
2415 return -1;
2416 }
2417 // remember position of first insert effect and by default
2418 // select this as insert position for new effect
Andy Hung920f6572022-10-06 12:09:49 -07002419 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002420 idx_insert = i;
2421 }
2422 // remember position of last insert effect claiming
2423 // first position
2424 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2425 idx_insert_first = i;
2426 }
2427 // remember position of first insert effect claiming
2428 // last position
2429 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2430 idx_insert_last == -1) {
2431 idx_insert_last = i;
2432 }
2433 }
2434 }
2435
2436 // modify idx_insert from first position if needed
2437 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2438 if (idx_insert_last != -1) {
2439 idx_insert = idx_insert_last;
2440 } else {
2441 idx_insert = size;
2442 }
2443 } else {
2444 if (idx_insert_first != -1) {
2445 idx_insert = idx_insert_first + 1;
2446 }
2447 }
2448 return idx_insert;
2449}
2450
Andy Hung87c693c2023-07-06 20:56:16 -07002451// removeEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002452size_t EffectChain::removeEffect_l(const sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002453 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002454{
2455 Mutex::Autolock _l(mLock);
2456 size_t size = mEffects.size();
2457 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2458
2459 for (size_t i = 0; i < size; i++) {
2460 if (effect == mEffects[i]) {
2461 // calling stop here will remove pre-processing effect from the audio HAL.
2462 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2463 // the middle of a read from audio HAL
2464 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2465 mEffects[i]->state() == EffectModule::STOPPING) {
2466 mEffects[i]->stop();
2467 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002468 if (release) {
2469 mEffects[i]->release_l();
2470 }
2471
Mikhail Naganov022b9952017-01-04 16:36:51 -08002472 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002473 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002474 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002475 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002476 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002477 }
2478 }
2479 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002480
2481 // make sure the input buffer configuration for the new first effect in the chain
2482 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002483 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2484 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002485 mEffects[0]->configure();
2486 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002487 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002488 }
2489
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002490 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002491 this, i);
2492 break;
2493 }
2494 }
2495
2496 return mEffects.size();
2497}
2498
Andy Hung87c693c2023-07-06 20:56:16 -07002499// setDevices_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002500void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002501{
2502 size_t size = mEffects.size();
2503 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002504 mEffects[i]->setDevices(devices);
2505 }
2506}
2507
Andy Hung87c693c2023-07-06 20:56:16 -07002508// setInputDevice_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002509void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08002510{
2511 size_t size = mEffects.size();
2512 for (size_t i = 0; i < size; i++) {
2513 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002514 }
2515}
2516
Andy Hung87c693c2023-07-06 20:56:16 -07002517// setMode_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002518void EffectChain::setMode_l(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08002519{
2520 size_t size = mEffects.size();
2521 for (size_t i = 0; i < size; i++) {
2522 mEffects[i]->setMode(mode);
2523 }
2524}
2525
Andy Hung87c693c2023-07-06 20:56:16 -07002526// setAudioSource_l() must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002527void EffectChain::setAudioSource_l(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08002528{
2529 size_t size = mEffects.size();
2530 for (size_t i = 0; i < size; i++) {
2531 mEffects[i]->setAudioSource(source);
2532 }
2533}
2534
Andy Hung6ac17eb2023-06-20 18:56:17 -07002535bool EffectChain::hasVolumeControlEnabled_l() const {
Zhou Songd505c642020-02-20 16:35:37 +08002536 for (const auto &effect : mEffects) {
2537 if (effect->isVolumeControlEnabled()) return true;
2538 }
2539 return false;
2540}
2541
Andy Hung87c693c2023-07-06 20:56:16 -07002542// setVolume_l() must be called with IAfThreadBase::mLock or EffectChain::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002543bool EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002544{
2545 uint32_t newLeft = *left;
2546 uint32_t newRight = *right;
jiabinbd18c8e2023-08-24 21:10:44 +00002547 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002548
2549 // first update volume controller
jiabinbd18c8e2023-08-24 21:10:44 +00002550 const auto volumeControlIndex = findVolumeControl_l(0, size);
2551 const int ctrlIdx = volumeControlIndex.value_or(-1);
2552 const sp<IAfEffectModule> volumeControlEffect =
2553 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
2554 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002555
jiabinbd18c8e2023-08-24 21:10:44 +00002556 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002557 *left == mLeftVolume && *right == mRightVolume) {
jiabinbd18c8e2023-08-24 21:10:44 +00002558 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002559 *left = mNewLeftVolume;
2560 *right = mNewRightVolume;
2561 }
jiabinbd18c8e2023-08-24 21:10:44 +00002562 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002563 }
2564
jiabinbd18c8e2023-08-24 21:10:44 +00002565 if (volumeControlEffect != cachedVolumeControlEffect) {
2566 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2567 // as zero for safe ramping.
2568 if (cachedVolumeControlEffect != nullptr) {
2569 uint32_t leftMax = 1 << 24;
2570 uint32_t rightMax = 1 << 24;
2571 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2572 }
2573 if (volumeControlEffect != nullptr) {
2574 uint32_t leftZero = 0;
2575 uint32_t rightZero = 0;
2576 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2577 }
2578 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002579 mLeftVolume = newLeft;
2580 mRightVolume = newRight;
2581
2582 // second get volume update from volume controller
2583 if (ctrlIdx >= 0) {
2584 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2585 mNewLeftVolume = newLeft;
2586 mNewRightVolume = newRight;
2587 }
2588 // then indicate volume to all other effects in chain.
2589 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002590 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002591 uint32_t lVol = newLeft;
2592 uint32_t rVol = newRight;
2593
2594 for (size_t i = 0; i < size; i++) {
2595 if ((int)i == ctrlIdx) {
2596 continue;
2597 }
2598 // this also works for ctrlIdx == -1 when there is no volume controller
2599 if ((int)i > ctrlIdx) {
2600 lVol = *left;
2601 rVol = *right;
2602 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002603 // Pass requested volume directly if this is volume monitor module
2604 if (mEffects[i]->isVolumeMonitor()) {
2605 mEffects[i]->setVolume(left, right, false);
2606 } else {
2607 mEffects[i]->setVolume(&lVol, &rVol, false);
2608 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002609 }
2610 *left = newLeft;
2611 *right = newRight;
2612
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002613 setVolumeForOutput_l(*left, *right);
2614
jiabinbd18c8e2023-08-24 21:10:44 +00002615 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002616}
2617
Andy Hung87c693c2023-07-06 20:56:16 -07002618// resetVolume_l() must be called with IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002619void EffectChain::resetVolume_l()
Eric Laurentfa1e1232016-08-02 19:01:49 -07002620{
Eric Laurente7449bf2016-08-03 18:44:07 -07002621 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2622 uint32_t left = mLeftVolume;
2623 uint32_t right = mRightVolume;
2624 (void)setVolume_l(&left, &right, true);
2625 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002626}
2627
Andy Hung6ac17eb2023-06-20 18:56:17 -07002628// containsHapticGeneratingEffect_l must be called with
Andy Hung87c693c2023-07-06 20:56:16 -07002629// IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002630bool EffectChain::containsHapticGeneratingEffect_l()
jiabineb3bda02020-06-30 14:07:03 -07002631{
2632 for (size_t i = 0; i < mEffects.size(); ++i) {
2633 if (mEffects[i]->isHapticGenerator()) {
2634 return true;
2635 }
2636 }
2637 return false;
2638}
2639
Andy Hung6ac17eb2023-06-20 18:56:17 -07002640void EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002641{
2642 Mutex::Autolock _l(mLock);
2643 for (size_t i = 0; i < mEffects.size(); ++i) {
2644 mEffects[i]->setHapticIntensity(id, intensity);
2645 }
2646}
2647
Andy Hung6ac17eb2023-06-20 18:56:17 -07002648void EffectChain::syncHalEffectsState()
Eric Laurent1b928682014-10-02 19:41:47 -07002649{
2650 Mutex::Autolock _l(mLock);
2651 for (size_t i = 0; i < mEffects.size(); i++) {
2652 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2653 mEffects[i]->state() == EffectModule::STOPPING) {
2654 mEffects[i]->addEffectToHal_l();
2655 }
2656 }
2657}
2658
Andy Hung6ac17eb2023-06-20 18:56:17 -07002659void EffectChain::dump(int fd, const Vector<String16>& args) const
Andy Hung920f6572022-10-06 12:09:49 -07002660NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002661{
Eric Laurentca7cc822012-11-19 14:55:58 -08002662 String8 result;
2663
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002664 const size_t numEffects = mEffects.size();
2665 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002666
Marco Nelissenb2208842014-02-07 14:00:50 -08002667 if (numEffects) {
2668 bool locked = AudioFlinger::dumpTryLock(mLock);
2669 // failed to lock - AudioFlinger is probably deadlocked
2670 if (!locked) {
2671 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002672 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002673
Andy Hungbded9c82017-11-30 18:47:35 -08002674 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2675 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2676 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2677 (int)inBufferStr.size(), "In buffer ",
2678 (int)outBufferStr.size(), "Out buffer ");
2679 result.appendFormat("\t%s %s %d\n",
2680 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002681 write(fd, result.string(), result.size());
2682
2683 for (size_t i = 0; i < numEffects; ++i) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07002684 sp<IAfEffectModule> effect = mEffects[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08002685 if (effect != 0) {
2686 effect->dump(fd, args);
2687 }
2688 }
2689
2690 if (locked) {
2691 mLock.unlock();
2692 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002693 } else {
2694 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002695 }
2696}
2697
Andy Hung87c693c2023-07-06 20:56:16 -07002698// must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002699void EffectChain::setEffectSuspended_l(
Eric Laurentca7cc822012-11-19 14:55:58 -08002700 const effect_uuid_t *type, bool suspend)
2701{
2702 sp<SuspendedEffectDesc> desc;
2703 // use effect type UUID timelow as key as there is no real risk of identical
2704 // timeLow fields among effect type UUIDs.
2705 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2706 if (suspend) {
2707 if (index >= 0) {
2708 desc = mSuspendedEffects.valueAt(index);
2709 } else {
2710 desc = new SuspendedEffectDesc();
2711 desc->mType = *type;
2712 mSuspendedEffects.add(type->timeLow, desc);
2713 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2714 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002715
Eric Laurentca7cc822012-11-19 14:55:58 -08002716 if (desc->mRefCount++ == 0) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07002717 sp<IAfEffectModule> effect = getEffectIfEnabled(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002718 if (effect != 0) {
2719 desc->mEffect = effect;
2720 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002721 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002722 }
2723 }
2724 } else {
2725 if (index < 0) {
2726 return;
2727 }
2728 desc = mSuspendedEffects.valueAt(index);
2729 if (desc->mRefCount <= 0) {
2730 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002731 desc->mRefCount = 0;
2732 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002733 }
2734 if (--desc->mRefCount == 0) {
2735 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2736 if (desc->mEffect != 0) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07002737 sp<IAfEffectModule> effect = desc->mEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002738 if (effect != 0) {
2739 effect->setSuspended(false);
2740 effect->lock();
Andy Hung6ac17eb2023-06-20 18:56:17 -07002741 IAfEffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002742 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002743 effect->setEnabled_l(handle->enabled());
2744 }
2745 effect->unlock();
2746 }
2747 desc->mEffect.clear();
2748 }
2749 mSuspendedEffects.removeItemsAt(index);
2750 }
2751 }
2752}
2753
Andy Hung87c693c2023-07-06 20:56:16 -07002754// must be called with IAfThreadBase::mutex() held
Andy Hung6ac17eb2023-06-20 18:56:17 -07002755void EffectChain::setEffectSuspendedAll_l(bool suspend)
Eric Laurentca7cc822012-11-19 14:55:58 -08002756{
2757 sp<SuspendedEffectDesc> desc;
2758
2759 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2760 if (suspend) {
2761 if (index >= 0) {
2762 desc = mSuspendedEffects.valueAt(index);
2763 } else {
2764 desc = new SuspendedEffectDesc();
2765 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2766 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2767 }
2768 if (desc->mRefCount++ == 0) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07002769 Vector< sp<IAfEffectModule> > effects;
Eric Laurentca7cc822012-11-19 14:55:58 -08002770 getSuspendEligibleEffects(effects);
2771 for (size_t i = 0; i < effects.size(); i++) {
2772 setEffectSuspended_l(&effects[i]->desc().type, true);
2773 }
2774 }
2775 } else {
2776 if (index < 0) {
2777 return;
2778 }
2779 desc = mSuspendedEffects.valueAt(index);
2780 if (desc->mRefCount <= 0) {
2781 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2782 desc->mRefCount = 1;
2783 }
2784 if (--desc->mRefCount == 0) {
2785 Vector<const effect_uuid_t *> types;
2786 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2787 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2788 continue;
2789 }
2790 types.add(&mSuspendedEffects.valueAt(i)->mType);
2791 }
2792 for (size_t i = 0; i < types.size(); i++) {
2793 setEffectSuspended_l(types[i], false);
2794 }
2795 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2796 mSuspendedEffects.keyAt(index));
2797 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2798 }
2799 }
2800}
2801
2802
2803// The volume effect is used for automated tests only
2804#ifndef OPENSL_ES_H_
2805static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2806 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2807const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2808#endif //OPENSL_ES_H_
2809
Eric Laurentd8365c52017-07-16 15:27:05 -07002810/* static */
Andy Hung6ac17eb2023-06-20 18:56:17 -07002811bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
Eric Laurentd8365c52017-07-16 15:27:05 -07002812{
2813 // Only NS and AEC are suspended when BtNRec is off
2814 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2815 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2816 return true;
2817 }
2818 return false;
2819}
2820
Andy Hung6ac17eb2023-06-20 18:56:17 -07002821bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
Eric Laurentca7cc822012-11-19 14:55:58 -08002822{
2823 // auxiliary effects and visualizer are never suspended on output mix
2824 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2825 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2826 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002827 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2828 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002829 return false;
2830 }
2831 return true;
2832}
2833
Andy Hung6ac17eb2023-06-20 18:56:17 -07002834void EffectChain::getSuspendEligibleEffects(
2835 Vector< sp<IAfEffectModule> > &effects)
Eric Laurentca7cc822012-11-19 14:55:58 -08002836{
2837 effects.clear();
2838 for (size_t i = 0; i < mEffects.size(); i++) {
2839 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2840 effects.add(mEffects[i]);
2841 }
2842 }
2843}
2844
Andy Hung6ac17eb2023-06-20 18:56:17 -07002845sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
Eric Laurentca7cc822012-11-19 14:55:58 -08002846{
Andy Hung6ac17eb2023-06-20 18:56:17 -07002847 sp<IAfEffectModule> effect = getEffectFromType_l(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002848 return effect != 0 && effect->isEnabled() ? effect : 0;
2849}
2850
Andy Hung6ac17eb2023-06-20 18:56:17 -07002851void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08002852 bool enabled)
2853{
2854 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2855 if (enabled) {
2856 if (index < 0) {
2857 // if the effect is not suspend check if all effects are suspended
2858 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2859 if (index < 0) {
2860 return;
2861 }
2862 if (!isEffectEligibleForSuspend(effect->desc())) {
2863 return;
2864 }
2865 setEffectSuspended_l(&effect->desc().type, enabled);
2866 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2867 if (index < 0) {
2868 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2869 return;
2870 }
2871 }
2872 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2873 effect->desc().type.timeLow);
2874 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002875 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002876 if (desc->mEffect == 0) {
2877 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002878 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002879 effect->setSuspended(true);
2880 }
2881 } else {
2882 if (index < 0) {
2883 return;
2884 }
2885 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2886 effect->desc().type.timeLow);
2887 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2888 desc->mEffect.clear();
2889 effect->setSuspended(false);
2890 }
2891}
2892
Andy Hung6ac17eb2023-06-20 18:56:17 -07002893bool EffectChain::isNonOffloadableEnabled() const
Eric Laurent813e2a72013-08-31 12:59:48 -07002894{
2895 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002896 return isNonOffloadableEnabled_l();
2897}
2898
Andy Hung6ac17eb2023-06-20 18:56:17 -07002899bool EffectChain::isNonOffloadableEnabled_l() const
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002900{
Eric Laurent813e2a72013-08-31 12:59:48 -07002901 size_t size = mEffects.size();
2902 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002903 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002904 return true;
2905 }
2906 }
2907 return false;
2908}
2909
Andy Hung87c693c2023-07-06 20:56:16 -07002910void EffectChain::setThread(const sp<IAfThreadBase>& thread)
Eric Laurentaaa44472014-09-12 17:41:50 -07002911{
2912 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002913 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002914}
2915
Andy Hung6ac17eb2023-06-20 18:56:17 -07002916void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002917{
2918 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2919 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2920 }
2921 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2922 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2923 }
jiabinc658e452022-10-21 20:52:21 +00002924 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2925 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2926 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002927}
2928
Andy Hung6ac17eb2023-06-20 18:56:17 -07002929void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002930{
2931 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2932 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2933 }
2934 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2935 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2936 }
2937}
2938
Andy Hung6ac17eb2023-06-20 18:56:17 -07002939bool EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002940{
2941 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002942 for (const auto &effect : mEffects) {
2943 if (effect->isProcessImplemented()) {
2944 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002945 }
2946 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002947 // Allow effects without processing.
2948 return true;
2949}
2950
Andy Hung6ac17eb2023-06-20 18:56:17 -07002951bool EffectChain::isFastCompatible() const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002952{
2953 Mutex::Autolock _l(mLock);
2954 for (const auto &effect : mEffects) {
2955 if (effect->isProcessImplemented()
2956 && effect->isImplementationSoftware()) {
2957 return false;
2958 }
2959 }
2960 // Allow effects without processing or hw accelerated effects.
2961 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002962}
2963
Andy Hung6ac17eb2023-06-20 18:56:17 -07002964bool EffectChain::isBitPerfectCompatible() const {
jiabinc658e452022-10-21 20:52:21 +00002965 Mutex::Autolock _l(mLock);
2966 for (const auto &effect : mEffects) {
2967 if (effect->isProcessImplemented()
2968 && effect->isImplementationSoftware()) {
2969 return false;
2970 }
2971 }
2972 // Allow effects without processing or hw accelerated effects.
2973 return true;
2974}
2975
Eric Laurent4c415062016-06-17 16:14:16 -07002976// isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung87c693c2023-07-06 20:56:16 -07002977bool EffectChain::isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const
Eric Laurent4c415062016-06-17 16:14:16 -07002978{
2979 Mutex::Autolock _l(mLock);
2980 for (size_t i = 0; i < mEffects.size(); i++) {
2981 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2982 return false;
2983 }
2984 }
2985 return true;
2986}
2987
Eric Laurent6b446ce2019-12-13 10:56:31 -08002988// EffectCallbackInterface implementation
Andy Hung6ac17eb2023-06-20 18:56:17 -07002989status_t EffectChain::EffectCallback::createEffectHal(
Eric Laurent6b446ce2019-12-13 10:56:31 -08002990 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2991 sp<EffectHalInterface> *effect) {
2992 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002993 const sp<EffectsFactoryHalInterface> effectsFactory =
2994 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002995 if (effectsFactory != 0) {
2996 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2997 }
2998 return status;
2999}
3000
Andy Hung6ac17eb2023-06-20 18:56:17 -07003001bool EffectChain::EffectCallback::updateOrphanEffectChains(
3002 const sp<IAfEffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003003 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003004 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003005}
3006
Andy Hung6ac17eb2023-06-20 18:56:17 -07003007status_t EffectChain::EffectCallback::allocateHalBuffer(
Eric Laurent6b446ce2019-12-13 10:56:31 -08003008 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003009 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003010}
3011
Andy Hung6ac17eb2023-06-20 18:56:17 -07003012status_t EffectChain::EffectCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003013 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003014 status_t result = NO_INIT;
Andy Hung87c693c2023-07-06 20:56:16 -07003015 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003016 if (t == nullptr) {
3017 return result;
3018 }
3019 sp <StreamHalInterface> st = t->stream();
3020 if (st == nullptr) {
3021 return result;
3022 }
3023 result = st->addEffect(effect);
3024 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3025 return result;
3026}
3027
Andy Hung6ac17eb2023-06-20 18:56:17 -07003028status_t EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003029 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003030 status_t result = NO_INIT;
Andy Hung87c693c2023-07-06 20:56:16 -07003031 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003032 if (t == nullptr) {
3033 return result;
3034 }
3035 sp <StreamHalInterface> st = t->stream();
3036 if (st == nullptr) {
3037 return result;
3038 }
3039 result = st->removeEffect(effect);
3040 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3041 return result;
3042}
3043
Andy Hung6ac17eb2023-06-20 18:56:17 -07003044audio_io_handle_t EffectChain::EffectCallback::io() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003045 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003046 if (t == nullptr) {
3047 return AUDIO_IO_HANDLE_NONE;
3048 }
3049 return t->id();
3050}
3051
Andy Hung6ac17eb2023-06-20 18:56:17 -07003052bool EffectChain::EffectCallback::isOutput() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003053 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003054 if (t == nullptr) {
3055 return true;
3056 }
3057 return t->isOutput();
3058}
3059
Andy Hung6ac17eb2023-06-20 18:56:17 -07003060bool EffectChain::EffectCallback::isOffload() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003061 return mThreadType == IAfThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062}
3063
Andy Hung6ac17eb2023-06-20 18:56:17 -07003064bool EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003065 return mThreadType == IAfThreadBase::OFFLOAD
3066 || mThreadType == IAfThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003067}
3068
Andy Hung6ac17eb2023-06-20 18:56:17 -07003069bool EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003070 switch (mThreadType) {
Andy Hung87c693c2023-07-06 20:56:16 -07003071 case IAfThreadBase::OFFLOAD:
3072 case IAfThreadBase::MMAP_PLAYBACK:
3073 case IAfThreadBase::MMAP_CAPTURE:
Eric Laurentb62d0362021-10-26 17:40:18 +02003074 return true;
3075 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003076 return false;
3077 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003078}
3079
Andy Hung6ac17eb2023-06-20 18:56:17 -07003080bool EffectChain::EffectCallback::isSpatializer() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003081 return mThreadType == IAfThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003082}
3083
Andy Hung6ac17eb2023-06-20 18:56:17 -07003084uint32_t EffectChain::EffectCallback::sampleRate() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003085 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003086 if (t == nullptr) {
3087 return 0;
3088 }
3089 return t->sampleRate();
3090}
3091
Andy Hung6ac17eb2023-06-20 18:56:17 -07003092audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const {
Andy Hung87c693c2023-07-06 20:56:16 -07003093 const sp<IAfThreadBase> t = thread().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003094 if (t == nullptr) {
3095 return AUDIO_CHANNEL_NONE;
3096 }
Andy Hung6ac17eb2023-06-20 18:56:17 -07003097 sp<IAfEffectChain> c = chain().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003098 if (c == nullptr) {
3099 return AUDIO_CHANNEL_NONE;
3100 }
3101
Andy Hung87c693c2023-07-06 20:56:16 -07003102 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003103 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3104 if (c->isFirstEffect(id)) {
3105 return t->mixerChannelMask();
3106 } else {
3107 return t->channelMask();
3108 }
3109 } else if (!audio_is_global_session(c->sessionId())) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003110 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung87c693c2023-07-06 20:56:16 -07003111 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003112 return t->mixerChannelMask();
3113 } else {
3114 return t->channelMask();
3115 }
3116 } else {
3117 return t->channelMask();
3118 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003119 } else {
3120 return t->channelMask();
3121 }
3122}
3123
Andy Hung6ac17eb2023-06-20 18:56:17 -07003124uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003125 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003126}
3127
Andy Hung6ac17eb2023-06-20 18:56:17 -07003128audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003129 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003130 if (t == nullptr) {
3131 return AUDIO_CHANNEL_NONE;
3132 }
Andy Hung6ac17eb2023-06-20 18:56:17 -07003133 sp<IAfEffectChain> c = chain().promote();
Eric Laurentb62d0362021-10-26 17:40:18 +02003134 if (c == nullptr) {
3135 return AUDIO_CHANNEL_NONE;
3136 }
3137
Andy Hung87c693c2023-07-06 20:56:16 -07003138 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003139 if (!audio_is_global_session(c->sessionId())) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003140 if ((t->hasAudioSession_l(c->sessionId())
Andy Hung87c693c2023-07-06 20:56:16 -07003141 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003142 return t->mixerChannelMask();
3143 } else {
3144 return t->channelMask();
3145 }
3146 } else {
3147 return t->channelMask();
3148 }
3149 } else {
3150 return t->channelMask();
3151 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003152}
3153
Andy Hung6ac17eb2023-06-20 18:56:17 -07003154uint32_t EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003155 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003156}
3157
Andy Hung6ac17eb2023-06-20 18:56:17 -07003158audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003159 const sp<IAfThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003160 if (t == nullptr) {
3161 return AUDIO_CHANNEL_NONE;
3162 }
3163 return t->hapticChannelMask();
3164}
3165
Andy Hung6ac17eb2023-06-20 18:56:17 -07003166size_t EffectChain::EffectCallback::frameCount() const {
Andy Hung87c693c2023-07-06 20:56:16 -07003167 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003168 if (t == nullptr) {
3169 return 0;
3170 }
3171 return t->frameCount();
3172}
3173
Andy Hung6ac17eb2023-06-20 18:56:17 -07003174uint32_t EffectChain::EffectCallback::latency() const
Andy Hung920f6572022-10-06 12:09:49 -07003175NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3176{
Andy Hung87c693c2023-07-06 20:56:16 -07003177 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003178 if (t == nullptr) {
3179 return 0;
3180 }
Andy Hung920f6572022-10-06 12:09:49 -07003181 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003182 return t->latency_l();
3183}
3184
Andy Hung6ac17eb2023-06-20 18:56:17 -07003185void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
Andy Hung920f6572022-10-06 12:09:49 -07003186NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3187{
Andy Hung87c693c2023-07-06 20:56:16 -07003188 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003189 if (t == nullptr) {
3190 return;
3191 }
3192 t->setVolumeForOutput_l(left, right);
3193}
3194
Andy Hung6ac17eb2023-06-20 18:56:17 -07003195void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
3196 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung87c693c2023-07-06 20:56:16 -07003197 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003198 if (t == nullptr) {
3199 return;
3200 }
3201 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3202
Andy Hung6ac17eb2023-06-20 18:56:17 -07003203 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003204 if (c == nullptr) {
3205 return;
3206 }
Eric Laurent41709552019-12-16 19:34:05 -08003207 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3208 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003209}
3210
Andy Hung6ac17eb2023-06-20 18:56:17 -07003211void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
Andy Hung87c693c2023-07-06 20:56:16 -07003212 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003213 if (t == nullptr) {
3214 return;
3215 }
Eric Laurent41709552019-12-16 19:34:05 -08003216 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3217 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003218}
3219
Andy Hung6ac17eb2023-06-20 18:56:17 -07003220void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003221 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3222
Andy Hung87c693c2023-07-06 20:56:16 -07003223 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003224 if (t == nullptr) {
3225 return;
3226 }
3227 t->onEffectDisable();
3228}
3229
Andy Hung6ac17eb2023-06-20 18:56:17 -07003230bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
Eric Laurent6b446ce2019-12-13 10:56:31 -08003231 bool unpinIfLast) {
Andy Hung87c693c2023-07-06 20:56:16 -07003232 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003233 if (t == nullptr) {
3234 return false;
3235 }
3236 t->disconnectEffectHandle(handle, unpinIfLast);
3237 return true;
3238}
3239
Andy Hung6ac17eb2023-06-20 18:56:17 -07003240void EffectChain::EffectCallback::resetVolume() {
3241 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003242 if (c == nullptr) {
3243 return;
3244 }
3245 c->resetVolume_l();
3246
3247}
3248
Andy Hung6ac17eb2023-06-20 18:56:17 -07003249product_strategy_t EffectChain::EffectCallback::strategy() const {
3250 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003251 if (c == nullptr) {
3252 return PRODUCT_STRATEGY_NONE;
3253 }
3254 return c->strategy();
3255}
3256
Andy Hung6ac17eb2023-06-20 18:56:17 -07003257int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3258 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003259 if (c == nullptr) {
3260 return 0;
3261 }
3262 return c->activeTrackCnt();
3263}
3264
Eric Laurentb82e6b72019-11-22 17:25:04 -08003265
3266#undef LOG_TAG
Andy Hung6ac17eb2023-06-20 18:56:17 -07003267#define LOG_TAG "DeviceEffectProxy"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003268
Andy Hung6ac17eb2023-06-20 18:56:17 -07003269/* static */
3270sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3271 const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -07003272 const sp<DeviceEffectManagerCallback>& callback,
Andy Hung6ac17eb2023-06-20 18:56:17 -07003273 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3274{
3275 return sp<DeviceEffectProxy>::make(device,
Andy Hung55a74fd2023-07-13 19:54:47 -07003276 callback,
Andy Hung6ac17eb2023-06-20 18:56:17 -07003277 desc, id, notifyFramesProcessed);
3278}
3279
3280status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003281{
3282 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3283 Mutex::Autolock _l(mProxyLock);
3284 if (status == NO_ERROR) {
3285 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003286 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003287 if (enabled) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003288 bs = handle.second->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003289 } else {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003290 bs = handle.second->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003291 }
3292 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003293 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003294 }
3295 }
3296 }
3297 ALOGV("%s enable %d status %d", __func__, enabled, status);
3298 return status;
3299}
3300
Andy Hung6ac17eb2023-06-20 18:56:17 -07003301status_t DeviceEffectProxy::init(
Andy Hung8e6b62a2023-07-13 18:11:33 -07003302 const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003303//For all audio patches
3304//If src or sink device match
3305//If the effect is HW accelerated
3306// if no corresponding effect module
3307// Create EffectModule: mHalEffect
3308//Create and attach EffectHandle
3309//If the effect is not HW accelerated and the patch sink or src is a mixer port
3310// Create Effect on patch input or output thread on session -1
3311//Add EffectHandle to EffectHandle map of Effect Proxy:
3312 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3313 status_t status = NO_ERROR;
3314 for (auto &patch : patches) {
3315 status = onCreatePatch(patch.first, patch.second);
3316 ALOGV("%s onCreatePatch status %d", __func__, status);
3317 if (status == BAD_VALUE) {
3318 return status;
3319 }
3320 }
3321 return status;
3322}
3323
François Gaffie58e73af2023-02-15 11:47:24 +01003324status_t DeviceEffectProxy::onUpdatePatch(audio_patch_handle_t oldPatchHandle,
3325 audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -07003326 const IAfPatchPanel::Patch& /* patch */) {
François Gaffie58e73af2023-02-15 11:47:24 +01003327 status_t status = NAME_NOT_FOUND;
3328 ALOGV("%s", __func__);
3329 Mutex::Autolock _l(mProxyLock);
3330 if (mEffectHandles.find(oldPatchHandle) != mEffectHandles.end()) {
3331 ALOGV("%s replacing effect from handle %d to handle %d", __func__, oldPatchHandle,
3332 newPatchHandle);
3333 sp<IAfEffectHandle> effect = mEffectHandles.at(oldPatchHandle);
3334 mEffectHandles.erase(oldPatchHandle);
3335 mEffectHandles.emplace(newPatchHandle, effect);
3336 status = NO_ERROR;
3337 }
3338 return status;
3339}
3340
Andy Hung6ac17eb2023-06-20 18:56:17 -07003341status_t DeviceEffectProxy::onCreatePatch(
Andy Hung8e6b62a2023-07-13 18:11:33 -07003342 audio_patch_handle_t patchHandle, const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003343 status_t status = NAME_NOT_FOUND;
Andy Hung6ac17eb2023-06-20 18:56:17 -07003344 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003345 // only consider source[0] as this is the only "true" source of a patch
3346 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3347 ALOGV("%s source checkPort status %d", __func__, status);
3348 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3349 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3350 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3351 }
3352 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3353 Mutex::Autolock _l(mProxyLock);
François Gaffie70c6c722023-06-22 12:23:32 +02003354 size_t erasedHandle = mEffectHandles.erase(patchHandle);
3355 ALOGV("%s %s effecthandle %p for patch %d",
3356 __func__, (erasedHandle == 0 ? "adding" : "replacing"), handle.get(), patchHandle);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003357 mEffectHandles.emplace(patchHandle, handle);
3358 }
3359 ALOGW_IF(status == BAD_VALUE,
3360 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3361
3362 return status;
3363}
3364
Andy Hung8e6b62a2023-07-13 18:11:33 -07003365status_t DeviceEffectProxy::checkPort(const IAfPatchPanel::Patch& patch,
Andy Hung6ac17eb2023-06-20 18:56:17 -07003366 const struct audio_port_config *port, sp<IAfEffectHandle> *handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003367
3368 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3369 __func__, port->type, port->ext.device.type,
3370 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaob009ad52023-05-05 22:43:24 +00003371 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3372 port->ext.device.address != mDevice.address()) {
3373 return NAME_NOT_FOUND;
3374 }
3375 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3376 (audio_port_config_has_input_direction(port))) {
3377 ALOGI("%s don't create postprocessing effect on record port", __func__);
3378 return NAME_NOT_FOUND;
3379 }
3380 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3381 (!audio_port_config_has_input_direction(port))) {
3382 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003383 return NAME_NOT_FOUND;
3384 }
3385 status_t status = NAME_NOT_FOUND;
3386
3387 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3388 Mutex::Autolock _l(mProxyLock);
François Gaffie70c6c722023-06-22 12:23:32 +02003389 if (mHalEffect != nullptr && mDevicePort.id == port->id) {
3390 ALOGV("%s reusing HAL effect", __func__);
3391 } else {
3392 mDevicePort = *port;
3393 mHalEffect = new EffectModule(mMyCallback,
Eric Laurentb82e6b72019-11-22 17:25:04 -08003394 const_cast<effect_descriptor_t *>(&mDescriptor),
3395 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3396 false /* pinned */, port->id);
François Gaffie70c6c722023-06-22 12:23:32 +02003397 if (audio_is_input_device(mDevice.mType)) {
3398 mHalEffect->setInputDevice(mDevice);
3399 } else {
3400 mHalEffect->setDevices({mDevice});
3401 }
3402 mHalEffect->configure();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003403 }
Eric Laurentde8caf42021-08-11 17:19:25 +02003404 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3405 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003406 status = (*handle)->initCheck();
3407 if (status == OK) {
3408 status = mHalEffect->addHandle((*handle).get());
3409 } else {
3410 mHalEffect.clear();
3411 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3412 }
3413 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07003414 sp<IAfThreadBase> thread;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003415 if (audio_port_config_has_input_direction(port)) {
3416 if (patch.isSoftware()) {
3417 thread = patch.mRecord.thread();
3418 } else {
3419 thread = patch.thread().promote();
3420 }
3421 } else {
3422 if (patch.isSoftware()) {
3423 thread = patch.mPlayback.thread();
3424 } else {
3425 thread = patch.thread().promote();
3426 }
3427 }
3428 int enabled;
3429 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3430 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003431 &enabled, &status, false, false /*probe*/,
3432 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003433 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3434 } else {
3435 status = BAD_VALUE;
3436 }
Shunkai Yaob009ad52023-05-05 22:43:24 +00003437
Eric Laurentb82e6b72019-11-22 17:25:04 -08003438 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003439 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003440 if (isEnabled()) {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003441 bs = (*handle)->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003442 } else {
Andy Hung6ac17eb2023-06-20 18:56:17 -07003443 bs = (*handle)->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003444 }
3445 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003446 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003447 }
3448 }
3449 return status;
3450}
3451
Andy Hung6ac17eb2023-06-20 18:56:17 -07003452void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3453 sp<IAfEffectHandle> effect;
Eric Laurent76c89f32021-12-03 17:13:23 +01003454 {
3455 Mutex::Autolock _l(mProxyLock);
3456 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3457 effect = mEffectHandles.at(patchHandle);
3458 mEffectHandles.erase(patchHandle);
3459 }
3460 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003461}
3462
3463
Andy Hung6ac17eb2023-06-20 18:56:17 -07003464size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003465{
3466 Mutex::Autolock _l(mProxyLock);
3467 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003468 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003469 mHalEffect.clear();
3470 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3471 }
3472 return mHalEffect == nullptr ? 0 : 1;
3473}
3474
Andy Hung6ac17eb2023-06-20 18:56:17 -07003475status_t DeviceEffectProxy::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003476 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003477 if (mHalEffect == nullptr) {
3478 return NO_INIT;
3479 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003480 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003481}
3482
Andy Hung6ac17eb2023-06-20 18:56:17 -07003483status_t DeviceEffectProxy::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003484 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003485 if (mHalEffect == nullptr) {
3486 return NO_INIT;
3487 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003488 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003489}
3490
François Gaffiea2e985b2023-06-09 14:37:56 +02003491status_t DeviceEffectProxy::command(
3492 int32_t cmdCode, const std::vector<uint8_t>& cmdData, int32_t maxReplySize,
3493 std::vector<uint8_t>* reply) {
3494 Mutex::Autolock _l(mProxyLock);
3495 status_t status = EffectBase::command(cmdCode, cmdData, maxReplySize, reply);
3496 if (status == NO_ERROR) {
3497 for (auto& handle : mEffectHandles) {
3498 sp<IAfEffectBase> effect = handle.second->effect().promote();
3499 if (effect != nullptr) {
3500 status = effect->command(cmdCode, cmdData, maxReplySize, reply);
3501 }
3502 }
3503 }
3504 ALOGV("%s status %d", __func__, status);
3505 return status;
3506}
3507
Andy Hung6ac17eb2023-06-20 18:56:17 -07003508bool DeviceEffectProxy::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003509 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3510 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3511 }
3512 return true;
3513}
3514
Andy Hung6ac17eb2023-06-20 18:56:17 -07003515uint32_t DeviceEffectProxy::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003516 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3517 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3518 return mDevicePort.sample_rate;
3519 }
3520 return DEFAULT_OUTPUT_SAMPLE_RATE;
3521}
3522
Andy Hung6ac17eb2023-06-20 18:56:17 -07003523audio_channel_mask_t DeviceEffectProxy::channelMask() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003524 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3525 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3526 return mDevicePort.channel_mask;
3527 }
3528 return AUDIO_CHANNEL_OUT_STEREO;
3529}
3530
Andy Hung6ac17eb2023-06-20 18:56:17 -07003531uint32_t DeviceEffectProxy::channelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003532 if (isOutput()) {
3533 return audio_channel_count_from_out_mask(channelMask());
3534 }
3535 return audio_channel_count_from_in_mask(channelMask());
3536}
3537
Andy Hung6ac17eb2023-06-20 18:56:17 -07003538void DeviceEffectProxy::dump2(int fd, int spaces) const
Andy Hung920f6572022-10-06 12:09:49 -07003539NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3540{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003541 const Vector<String16> args;
3542 EffectBase::dump(fd, args);
3543
Andy Hung6ac17eb2023-06-20 18:56:17 -07003544 const bool locked = AudioFlinger::dumpTryLock(mProxyLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003545 if (!locked) {
3546 String8 result("DeviceEffectProxy may be deadlocked\n");
3547 write(fd, result.string(), result.size());
3548 }
3549
3550 String8 outStr;
3551 if (mHalEffect != nullptr) {
3552 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3553 } else {
3554 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3555 }
3556 write(fd, outStr.string(), outStr.size());
3557 outStr.clear();
3558
3559 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3560 write(fd, outStr.string(), outStr.size());
3561 outStr.clear();
3562
3563 for (const auto& iter : mEffectHandles) {
3564 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3565 write(fd, outStr.string(), outStr.size());
3566 outStr.clear();
Andy Hung6ac17eb2023-06-20 18:56:17 -07003567 sp<IAfEffectBase> effect = iter.second->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003568 if (effect != nullptr) {
3569 effect->dump(fd, args);
3570 }
3571 }
3572
3573 if (locked) {
François Gaffiefcf15bf2023-02-15 11:37:43 +01003574 mProxyLock.unlock();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003575 }
3576}
3577
3578#undef LOG_TAG
Andy Hung6ac17eb2023-06-20 18:56:17 -07003579#define LOG_TAG "DeviceEffectProxy::ProxyCallback"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003580
Andy Hung6ac17eb2023-06-20 18:56:17 -07003581int DeviceEffectProxy::ProxyCallback::newEffectId() {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003582 return mManagerCallback->newEffectId();
3583}
3584
3585
Andy Hung6ac17eb2023-06-20 18:56:17 -07003586bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3587 IAfEffectHandle *handle, bool unpinIfLast) {
3588 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003589 if (effectBase == nullptr) {
3590 return false;
3591 }
3592
Andy Hung6ac17eb2023-06-20 18:56:17 -07003593 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003594 if (effect == nullptr) {
3595 return false;
3596 }
3597
3598 // restore suspended effects if the disconnected handle was enabled and the last one.
3599 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3600 if (remove) {
3601 sp<DeviceEffectProxy> proxy = mProxy.promote();
3602 if (proxy != nullptr) {
3603 proxy->removeEffect(effect);
3604 }
3605 if (handle->enabled()) {
3606 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3607 }
3608 }
3609 return true;
3610}
3611
Andy Hung6ac17eb2023-06-20 18:56:17 -07003612status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003613 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3614 sp<EffectHalInterface> *effect) {
3615 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3616}
3617
Andy Hung6ac17eb2023-06-20 18:56:17 -07003618status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003619 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003620 sp<DeviceEffectProxy> proxy = mProxy.promote();
3621 if (proxy == nullptr) {
3622 return NO_INIT;
3623 }
3624 return proxy->addEffectToHal(effect);
3625}
3626
Andy Hung6ac17eb2023-06-20 18:56:17 -07003627status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003628 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003629 sp<DeviceEffectProxy> proxy = mProxy.promote();
3630 if (proxy == nullptr) {
3631 return NO_INIT;
3632 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003633 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003634}
3635
Andy Hung6ac17eb2023-06-20 18:56:17 -07003636bool DeviceEffectProxy::ProxyCallback::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003637 sp<DeviceEffectProxy> proxy = mProxy.promote();
3638 if (proxy == nullptr) {
3639 return true;
3640 }
3641 return proxy->isOutput();
3642}
3643
Andy Hung6ac17eb2023-06-20 18:56:17 -07003644uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003645 sp<DeviceEffectProxy> proxy = mProxy.promote();
3646 if (proxy == nullptr) {
3647 return DEFAULT_OUTPUT_SAMPLE_RATE;
3648 }
3649 return proxy->sampleRate();
3650}
3651
Andy Hung6ac17eb2023-06-20 18:56:17 -07003652audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
Eric Laurentf1f22e72021-07-13 14:04:14 +02003653 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003654 sp<DeviceEffectProxy> proxy = mProxy.promote();
3655 if (proxy == nullptr) {
3656 return AUDIO_CHANNEL_OUT_STEREO;
3657 }
3658 return proxy->channelMask();
3659}
3660
Andy Hung6ac17eb2023-06-20 18:56:17 -07003661uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003662 sp<DeviceEffectProxy> proxy = mProxy.promote();
3663 if (proxy == nullptr) {
3664 return 2;
3665 }
3666 return proxy->channelCount();
3667}
3668
Andy Hung6ac17eb2023-06-20 18:56:17 -07003669audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003670 sp<DeviceEffectProxy> proxy = mProxy.promote();
3671 if (proxy == nullptr) {
3672 return AUDIO_CHANNEL_OUT_STEREO;
3673 }
3674 return proxy->channelMask();
3675}
3676
Andy Hung6ac17eb2023-06-20 18:56:17 -07003677uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003678 sp<DeviceEffectProxy> proxy = mProxy.promote();
3679 if (proxy == nullptr) {
3680 return 2;
3681 }
3682 return proxy->channelCount();
3683}
3684
Andy Hung6ac17eb2023-06-20 18:56:17 -07003685void DeviceEffectProxy::ProxyCallback::onEffectEnable(
3686 const sp<IAfEffectBase>& effectBase) {
3687 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003688 if (effect == nullptr) {
3689 return;
3690 }
3691 effect->start();
3692}
3693
Andy Hung6ac17eb2023-06-20 18:56:17 -07003694void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3695 const sp<IAfEffectBase>& effectBase) {
3696 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003697 if (effect == nullptr) {
3698 return;
3699 }
3700 effect->stop();
3701}
3702
Glenn Kasten63238ef2015-03-02 15:50:29 -08003703} // namespace android