blob: 43e3b393e46b95525af84a450a430aee480e755b [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
Andy Hung699435e2023-07-19 11:31:15 -070022#include "Effects.h"
rago94a1ee82017-07-21 15:11:02 -070023
Andy Hung699435e2023-07-19 11:31:15 -070024#include "Client.h"
25#include "EffectConfiguration.h"
26
27#include <afutils/DumpTryLock.h>
28#include <audio_utils/channels.h>
29#include <audio_utils/primitives.h>
30#include <media/AudioCommonTypes.h>
31#include <media/AudioContainers.h>
32#include <media/AudioDeviceTypeAddr.h>
33#include <media/AudioEffect.h>
34#include <media/ShmemCompat.h>
35#include <media/TypeConverter.h>
36#include <media/audiohal/EffectHalInterface.h>
37#include <media/audiohal/EffectsFactoryHalInterface.h>
38#include <mediautils/MethodStatistics.h>
39#include <mediautils/ServiceUtilities.h>
40#include <mediautils/TimeCheck.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070041#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020042#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070043#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070044#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070045#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020046#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070047#include <system/audio_effects/effect_visualizer.h>
Andy Hung699435e2023-07-19 11:31:15 -070048#include <utils/Log.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080049
Andy Hung699435e2023-07-19 11:31:15 -070050#include <algorithm>
Eric Laurentca7cc822012-11-19 14:55:58 -080051
52// ----------------------------------------------------------------------------
53
54// Note: the following macro is used for extremely verbose logging message. In
55// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
56// 0; but one side effect of this is to turn all LOGV's as well. Some messages
57// are so verbose that we want to suppress them even when we have ALOG_ASSERT
58// turned on. Do not uncomment the #def below unless you really know what you
59// are doing and want to see all of the extremely verbose messages.
60//#define VERY_VERY_VERBOSE_LOGGING
61#ifdef VERY_VERY_VERBOSE_LOGGING
62#define ALOGVV ALOGV
63#else
64#define ALOGVV(a...) do { } while(0)
65#endif
66
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090067#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
68
Eric Laurentca7cc822012-11-19 14:55:58 -080069namespace android {
70
Andy Hung1131b6e2020-12-08 20:47:45 -080071using aidl_utils::statusTFromBinderStatus;
Andy Hungba8e52b2023-05-11 14:33:03 -070072using audioflinger::EffectConfiguration;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070073using binder::Status;
74
75namespace {
76
77// Append a POD value into a vector of bytes.
78template<typename T>
79void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
80 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
81 buffer->insert(buffer->end(), ar, ar + sizeof(T));
82}
83
84// Write a POD value into a vector of bytes (clears the previous buffer
85// content).
86template<typename T>
87void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
88 buffer->clear();
89 appendToBuffer(value, buffer);
90}
91
92} // namespace
93
Eric Laurentca7cc822012-11-19 14:55:58 -080094// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080095// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080096// ----------------------------------------------------------------------------
97
98#undef LOG_TAG
Andy Hung116bc262023-06-20 18:56:17 -070099#define LOG_TAG "EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -0800100
Andy Hung116bc262023-06-20 18:56:17 -0700101EffectBase::EffectBase(const sp<EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -0800102 effect_descriptor_t *desc,
103 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800104 audio_session_t sessionId,
105 bool pinned)
106 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800107 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800108 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800109{
Eric Laurentca7cc822012-11-19 14:55:58 -0800110}
111
Eric Laurent41709552019-12-16 19:34:05 -0800112// must be called with EffectModule::mLock held
Andy Hung116bc262023-06-20 18:56:17 -0700113status_t EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800114{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800115
Eric Laurent41709552019-12-16 19:34:05 -0800116 ALOGV("setEnabled %p enabled %d", this, enabled);
117
118 if (enabled != isEnabled()) {
119 switch (mState) {
120 // going from disabled to enabled
121 case IDLE:
122 mState = STARTING;
123 break;
124 case STOPPED:
125 mState = RESTART;
126 break;
127 case STOPPING:
128 mState = ACTIVE;
129 break;
130
131 // going from enabled to disabled
132 case RESTART:
133 mState = STOPPED;
134 break;
135 case STARTING:
136 mState = IDLE;
137 break;
138 case ACTIVE:
139 mState = STOPPING;
140 break;
141 case DESTROYED:
142 return NO_ERROR; // simply ignore as we are being destroyed
143 }
144 for (size_t i = 1; i < mHandles.size(); i++) {
Andy Hung116bc262023-06-20 18:56:17 -0700145 IAfEffectHandle *h = mHandles[i];
Eric Laurent41709552019-12-16 19:34:05 -0800146 if (h != NULL && !h->disconnected()) {
147 h->setEnabled(enabled);
148 }
149 }
150 }
151 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800152}
153
Andy Hung116bc262023-06-20 18:56:17 -0700154status_t EffectBase::setEnabled(bool enabled, bool fromHandle)
Eric Laurent41709552019-12-16 19:34:05 -0800155{
156 status_t status;
157 {
158 Mutex::Autolock _l(mLock);
159 status = setEnabled_l(enabled);
160 }
161 if (fromHandle) {
162 if (enabled) {
163 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800165 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700166 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800167 }
168 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700169 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800170 }
171 }
172 return status;
173}
174
Andy Hung116bc262023-06-20 18:56:17 -0700175bool EffectBase::isEnabled() const
Eric Laurent41709552019-12-16 19:34:05 -0800176{
177 switch (mState) {
178 case RESTART:
179 case STARTING:
180 case ACTIVE:
181 return true;
182 case IDLE:
183 case STOPPING:
184 case STOPPED:
185 case DESTROYED:
186 default:
187 return false;
188 }
189}
190
Andy Hung116bc262023-06-20 18:56:17 -0700191void EffectBase::setSuspended(bool suspended)
Eric Laurent41709552019-12-16 19:34:05 -0800192{
193 Mutex::Autolock _l(mLock);
194 mSuspended = suspended;
195}
196
Andy Hung116bc262023-06-20 18:56:17 -0700197bool EffectBase::suspended() const
Eric Laurent41709552019-12-16 19:34:05 -0800198{
199 Mutex::Autolock _l(mLock);
200 return mSuspended;
201}
202
Andy Hung116bc262023-06-20 18:56:17 -0700203status_t EffectBase::addHandle(IAfEffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800204{
205 status_t status;
206
207 Mutex::Autolock _l(mLock);
208 int priority = handle->priority();
209 size_t size = mHandles.size();
Andy Hung116bc262023-06-20 18:56:17 -0700210 IAfEffectHandle *controlHandle = nullptr;
Eric Laurentca7cc822012-11-19 14:55:58 -0800211 size_t i;
212 for (i = 0; i < size; i++) {
Andy Hung116bc262023-06-20 18:56:17 -0700213 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800214 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800215 continue;
216 }
217 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700218 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800219 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700220 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800221 if (h->priority() <= priority) {
222 break;
223 }
224 }
225 // if inserted in first place, move effect control from previous owner to this handle
226 if (i == 0) {
227 bool enabled = false;
228 if (controlHandle != NULL) {
229 enabled = controlHandle->enabled();
230 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
231 }
232 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
233 status = NO_ERROR;
234 } else {
235 status = ALREADY_EXISTS;
236 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700237 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800238 mHandles.insertAt(handle, i);
239 return status;
240}
241
Andy Hung116bc262023-06-20 18:56:17 -0700242status_t EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700243{
244 status_t status = NO_ERROR;
245 bool doRegister = false;
246 bool registered = false;
247 bool doEnable = false;
248 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700249 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800250 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700251
252 {
253 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200254
255 if ((isInternal_l() && !mPolicyRegistered)
256 || !getCallback()->isAudioPolicyReady()) {
257 return NO_ERROR;
258 }
259
Eric Laurent6c796322019-04-09 14:13:17 -0700260 // register effect when first handle is attached and unregister when last handle is removed
261 if (mPolicyRegistered != mHandles.size() > 0) {
262 doRegister = true;
263 mPolicyRegistered = mHandles.size() > 0;
264 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700265 const auto callback = getCallback();
266 io = callback->io();
267 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700268 }
269 }
270 // enable effect when registered according to enable state requested by controlling handle
271 if (mHandles.size() > 0) {
Andy Hung116bc262023-06-20 18:56:17 -0700272 IAfEffectHandle *handle = controlHandle_l();
Eric Laurent6c796322019-04-09 14:13:17 -0700273 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
274 doEnable = true;
275 mPolicyEnabled = handle->enabled();
276 }
277 }
278 registered = mPolicyRegistered;
279 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100280 // The simultaneous release of two EffectHandles with the same EffectModule
281 // may cause us to call this method at the same time.
282 // This may deadlock under some circumstances (b/180941720). Avoid this.
283 if (!doRegister && !(registered && doEnable)) {
284 return NO_ERROR;
285 }
Eric Laurent6c796322019-04-09 14:13:17 -0700286 }
fengjnlan46407562022-09-07 16:20:01 +0800287 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700288 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
289 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
290 if (doRegister) {
291 if (registered) {
292 status = AudioSystem::registerEffect(
293 &mDescriptor,
294 io,
295 strategy,
296 mSessionId,
297 mId);
298 } else {
299 status = AudioSystem::unregisterEffect(mId);
300 }
301 }
302 if (registered && doEnable) {
303 status = AudioSystem::setEffectEnabled(mId, enabled);
304 }
305 mPolicyLock.unlock();
306
307 return status;
308}
309
310
Andy Hung116bc262023-06-20 18:56:17 -0700311ssize_t EffectBase::removeHandle(IAfEffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800312{
313 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800314 return removeHandle_l(handle);
315}
316
Andy Hung116bc262023-06-20 18:56:17 -0700317ssize_t EffectBase::removeHandle_l(IAfEffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800318{
Eric Laurentca7cc822012-11-19 14:55:58 -0800319 size_t size = mHandles.size();
320 size_t i;
321 for (i = 0; i < size; i++) {
322 if (mHandles[i] == handle) {
323 break;
324 }
325 }
326 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800327 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
328 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800330 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800331
332 mHandles.removeAt(i);
333 // if removed from first place, move effect control from this handle to next in line
334 if (i == 0) {
Andy Hung116bc262023-06-20 18:56:17 -0700335 IAfEffectHandle *h = controlHandle_l();
Eric Laurentca7cc822012-11-19 14:55:58 -0800336 if (h != NULL) {
337 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
338 }
339 }
340
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530341 // Prevent calls to process() and other functions on effect interface from now on.
342 // The effect engine will be released by the destructor when the last strong reference on
343 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800344 if (mHandles.size() == 0 && !mPinned) {
345 mState = DESTROYED;
346 }
347
348 return mHandles.size();
349}
350
351// must be called with EffectModule::mLock held
Andy Hung116bc262023-06-20 18:56:17 -0700352IAfEffectHandle *EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800353{
354 // the first valid handle in the list has control over the module
355 for (size_t i = 0; i < mHandles.size(); i++) {
Andy Hung116bc262023-06-20 18:56:17 -0700356 IAfEffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800357 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800358 return h;
359 }
360 }
361
362 return NULL;
363}
364
Eric Laurentf10c7092016-12-06 17:09:56 -0800365// unsafe method called when the effect parent thread has been destroyed
Andy Hung116bc262023-06-20 18:56:17 -0700366ssize_t EffectBase::disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800367{
Andy Hungfda44002021-06-03 17:23:16 -0700368 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800369 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700370 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800371 return mHandles.size();
372 }
373
Eric Laurentf10c7092016-12-06 17:09:56 -0800374 Mutex::Autolock _l(mLock);
375 ssize_t numHandles = removeHandle_l(handle);
376 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800377 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700378 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800379 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800380 }
381 return numHandles;
382}
383
Andy Hung116bc262023-06-20 18:56:17 -0700384bool EffectBase::purgeHandles()
Eric Laurent41709552019-12-16 19:34:05 -0800385{
386 bool enabled = false;
387 Mutex::Autolock _l(mLock);
Andy Hung116bc262023-06-20 18:56:17 -0700388 IAfEffectHandle *handle = controlHandle_l();
Eric Laurent41709552019-12-16 19:34:05 -0800389 if (handle != NULL) {
390 enabled = handle->enabled();
391 }
392 mHandles.clear();
393 return enabled;
394}
395
Andy Hung116bc262023-06-20 18:56:17 -0700396void EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700397 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800398}
399
400static String8 effectFlagsToString(uint32_t flags) {
401 String8 s;
402
403 s.append("conn. mode: ");
404 switch (flags & EFFECT_FLAG_TYPE_MASK) {
405 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
406 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
407 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
408 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
409 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
410 default: s.append("unknown/reserved"); break;
411 }
412 s.append(", ");
413
414 s.append("insert pref: ");
415 switch (flags & EFFECT_FLAG_INSERT_MASK) {
416 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
417 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
418 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
419 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
420 default: s.append("unknown/reserved"); break;
421 }
422 s.append(", ");
423
424 s.append("volume mgmt: ");
425 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
426 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
427 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
428 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
429 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
430 default: s.append("unknown/reserved"); break;
431 }
432 s.append(", ");
433
434 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
435 if (devind) {
436 s.append("device indication: ");
437 switch (devind) {
438 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
439 default: s.append("unknown/reserved"); break;
440 }
441 s.append(", ");
442 }
443
444 s.append("input mode: ");
445 switch (flags & EFFECT_FLAG_INPUT_MASK) {
446 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
447 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
448 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
449 default: s.append("not set"); break;
450 }
451 s.append(", ");
452
453 s.append("output mode: ");
454 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
455 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
456 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
457 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
458 default: s.append("not set"); break;
459 }
460 s.append(", ");
461
462 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
463 if (accel) {
464 s.append("hardware acceleration: ");
465 switch (accel) {
466 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
467 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
468 default: s.append("unknown/reserved"); break;
469 }
470 s.append(", ");
471 }
472
473 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
474 if (modeind) {
475 s.append("mode indication: ");
476 switch (modeind) {
477 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
478 default: s.append("unknown/reserved"); break;
479 }
480 s.append(", ");
481 }
482
483 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
484 if (srcind) {
485 s.append("source indication: ");
486 switch (srcind) {
487 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
488 default: s.append("unknown/reserved"); break;
489 }
490 s.append(", ");
491 }
492
493 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
494 s.append("offloadable, ");
495 }
496
497 int len = s.length();
498 if (s.length() > 2) {
499 (void) s.lockBuffer(len);
500 s.unlockBuffer(len - 2);
501 }
502 return s;
503}
504
Andy Hung116bc262023-06-20 18:56:17 -0700505void EffectBase::dump(int fd, const Vector<String16>& args __unused) const
Andy Hung920f6572022-10-06 12:09:49 -0700506NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800507{
508 String8 result;
509
510 result.appendFormat("\tEffect ID %d:\n", mId);
511
Andy Hung9a3f8182023-07-18 20:54:44 -0700512 const bool locked = afutils::dumpTryLock(mLock);
Eric Laurent41709552019-12-16 19:34:05 -0800513 // failed to lock - AudioFlinger is probably deadlocked
514 if (!locked) {
515 result.append("\t\tCould not lock Fx mutex:\n");
516 }
517
518 result.append("\t\tSession State Registered Enabled Suspended:\n");
519 result.appendFormat("\t\t%05d %03d %s %s %s\n",
520 mSessionId, mState, mPolicyRegistered ? "y" : "n",
521 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
522
523 result.append("\t\tDescriptor:\n");
524 char uuidStr[64];
525 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
526 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
527 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
528 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
529 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
530 mDescriptor.apiVersion,
531 mDescriptor.flags,
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +0000532 effectFlagsToString(mDescriptor.flags).c_str());
Eric Laurent41709552019-12-16 19:34:05 -0800533 result.appendFormat("\t\t- name: %s\n",
534 mDescriptor.name);
535
536 result.appendFormat("\t\t- implementor: %s\n",
537 mDescriptor.implementor);
538
539 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
540 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
541 char buffer[256];
542 for (size_t i = 0; i < mHandles.size(); ++i) {
Andy Hung116bc262023-06-20 18:56:17 -0700543 IAfEffectHandle *handle = mHandles[i];
Eric Laurent41709552019-12-16 19:34:05 -0800544 if (handle != NULL && !handle->disconnected()) {
545 handle->dumpToBuffer(buffer, sizeof(buffer));
546 result.append(buffer);
547 }
548 }
549 if (locked) {
550 mLock.unlock();
551 }
552
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +0000553 write(fd, result.c_str(), result.length());
Eric Laurent41709552019-12-16 19:34:05 -0800554}
555
556// ----------------------------------------------------------------------------
557// EffectModule implementation
558// ----------------------------------------------------------------------------
559
560#undef LOG_TAG
Andy Hung116bc262023-06-20 18:56:17 -0700561#define LOG_TAG "EffectModule"
Eric Laurent41709552019-12-16 19:34:05 -0800562
Andy Hung116bc262023-06-20 18:56:17 -0700563EffectModule::EffectModule(const sp<EffectCallbackInterface>& callback,
Eric Laurent41709552019-12-16 19:34:05 -0800564 effect_descriptor_t *desc,
565 int id,
566 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800567 bool pinned,
568 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800569 : EffectBase(callback, desc, id, sessionId, pinned),
570 // clear mConfig to ensure consistent initial value of buffer framecount
571 // in case buffers are associated by setInBuffer() or setOutBuffer()
572 // prior to configure().
573 mConfig{{}, {}},
574 mStatus(NO_INIT),
575 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
576 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800577 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000578 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800579 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800580{
581 ALOGV("Constructor %p pinned %d", this, pinned);
582 int lStatus;
583
584 // create effect engine from effect factory
585 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800586 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800587 if (mStatus != NO_ERROR) {
588 return;
589 }
590 lStatus = init();
591 if (lStatus < 0) {
592 mStatus = lStatus;
593 goto Error;
594 }
595
596 setOffloaded(callback->isOffload(), callback->io());
597 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
598
599 return;
600Error:
601 mEffectInterface.clear();
602 ALOGV("Constructor Error %d", mStatus);
603}
604
Andy Hung116bc262023-06-20 18:56:17 -0700605EffectModule::~EffectModule()
Eric Laurent41709552019-12-16 19:34:05 -0800606{
607 ALOGV("Destructor %p", this);
608 if (mEffectInterface != 0) {
609 char uuidStr[64];
610 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
611 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
612 this, uuidStr);
613 release_l();
614 }
615
616}
617
Andy Hung116bc262023-06-20 18:56:17 -0700618bool EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 Mutex::Autolock _l(mLock);
620
Eric Laurentfa1e1232016-08-02 19:01:49 -0700621 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800622 switch (mState) {
623 case RESTART:
624 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700625 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800626
627 case STARTING:
628 // clear auxiliary effect input buffer for next accumulation
629 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
630 memset(mConfig.inputCfg.buffer.raw,
631 0,
632 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
633 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700634 if (start_l() == NO_ERROR) {
635 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700636 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700637 } else {
638 mState = IDLE;
639 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800640 break;
641 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900642 // volume control for offload and direct threads must take effect immediately.
643 if (stop_l() == NO_ERROR
644 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700645 mDisableWaitCnt = mMaxDisableWaitCnt;
646 } else {
647 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
648 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800649 mState = STOPPED;
650 break;
651 case STOPPED:
652 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
653 // turn off sequence.
654 if (--mDisableWaitCnt == 0) {
655 reset_l();
656 mState = IDLE;
657 }
658 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200659 case ACTIVE:
660 for (size_t i = 0; i < mHandles.size(); i++) {
661 if (!mHandles[i]->disconnected()) {
662 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
663 }
664 }
665 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800666 default: //IDLE , ACTIVE, DESTROYED
667 break;
668 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700669
670 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800671}
672
Andy Hung116bc262023-06-20 18:56:17 -0700673void EffectModule::process()
Eric Laurentca7cc822012-11-19 14:55:58 -0800674{
675 Mutex::Autolock _l(mLock);
676
Mikhail Naganov022b9952017-01-04 16:36:51 -0800677 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800678 return;
679 }
680
rago94a1ee82017-07-21 15:11:02 -0700681 const uint32_t inChannelCount =
682 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
683 const uint32_t outChannelCount =
684 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
685 const bool auxType =
686 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
687
Andy Hungfa69ca32017-11-30 10:07:53 -0800688 // safeInputOutputSampleCount is 0 if the channel count between input and output
689 // buffers do not match. This prevents automatic accumulation or copying between the
690 // input and output effect buffers without an intermediary effect process.
691 // TODO: consider implementing channel conversion.
692 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700693 mInChannelCountRequested != mOutChannelCountRequested ? 0
694 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800695 mConfig.inputCfg.buffer.frameCount,
696 mConfig.outputCfg.buffer.frameCount);
697 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800698 accumulate_float(
699 mConfig.outputCfg.buffer.f32,
700 mConfig.inputCfg.buffer.f32,
701 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800702 };
703 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800704 memcpy(
705 mConfig.outputCfg.buffer.f32,
706 mConfig.inputCfg.buffer.f32,
707 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800708 };
709
Eric Laurentca7cc822012-11-19 14:55:58 -0800710 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700711 int ret;
712 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700713 if (auxType) {
714 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800715 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700716
Andy Hung26836922023-05-22 17:31:57 -0700717 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800718 memcpy_to_i16_from_float(
719 mConfig.inputCfg.buffer.s16,
720 mConfig.inputCfg.buffer.f32,
721 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700722 }
rago94a1ee82017-07-21 15:11:02 -0700723 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800724 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
725 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
726
727 if (!auxType && mInChannelCountRequested != inChannelCount) {
728 adjust_channels(
729 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
730 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
731 sizeof(float),
732 sizeof(float)
733 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
734 inBuffer = mInConversionBuffer;
735 }
736 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
737 && mOutChannelCountRequested != outChannelCount) {
738 adjust_selected_channels(
739 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
740 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
741 sizeof(float),
742 sizeof(float)
743 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
744 outBuffer = mOutConversionBuffer;
745 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800746 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
747 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800748 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800749 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
750 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700751 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800752 memcpy_to_i16_from_float(
753 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800754 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800755 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800756 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700757 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800759 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800760 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
761 goto data_bypass;
762 }
763 memcpy_to_i16_from_float(
764 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800765 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800766 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800767 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700768 }
769 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800770 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800771 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800772 sp<EffectBufferHalInterface> target =
773 mOutChannelCountRequested != outChannelCount
774 ? mOutConversionBuffer : mOutBuffer;
775
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800777 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800778 mOutConversionBuffer->audioBuffer()->s16,
779 outChannelCount * mConfig.outputCfg.buffer.frameCount);
780 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800781 if (mOutChannelCountRequested != outChannelCount) {
782 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
783 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
784 sizeof(float),
785 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
786 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700787 } else {
rago94a1ee82017-07-21 15:11:02 -0700788 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700789 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700793 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700795 }
796 }
797 ret = -ENODATA;
798 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800799
Eric Laurentca7cc822012-11-19 14:55:58 -0800800 // force transition to IDLE state when engine is ready
801 if (mState == STOPPED && ret == -ENODATA) {
802 mDisableWaitCnt = 1;
803 }
804
805 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700806 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800807 const size_t size =
808 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700809 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 }
811 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700812 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800813 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
814 // If an insert effect is idle and input buffer is different from output buffer,
815 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700816 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700817 // similar handling with data_bypass above.
818 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
819 accumulateInputToOutput();
820 } else { // EFFECT_BUFFER_ACCESS_WRITE
821 copyInputToOutput();
822 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800823 }
824 }
825}
826
Andy Hung116bc262023-06-20 18:56:17 -0700827void EffectModule::reset_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800828{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700829 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800830 return;
831 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700832 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800833}
834
Andy Hung116bc262023-06-20 18:56:17 -0700835status_t EffectModule::configure()
Eric Laurentca7cc822012-11-19 14:55:58 -0800836{
rago94a1ee82017-07-21 15:11:02 -0700837 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700839 uint32_t size;
840 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700841 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700842
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700843 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700844 status = NO_INIT;
845 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 }
847
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800849 // TODO: handle configuration of input (record) SW effects above the HAL,
850 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
851 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700852 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200853 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200855 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800856
857 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800858 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
859 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
860 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
861 mConfig.inputCfg.channels);
862 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800863 }
jiabineb3bda02020-06-30 14:07:03 -0700864 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700865 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700866 mConfig.inputCfg.channels |= hapticChannelMask;
867 mConfig.outputCfg.channels |= hapticChannelMask;
868 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800869 mInChannelCountRequested =
870 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
871 mOutChannelCountRequested =
872 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700873
Andy Hung319587b2023-05-23 14:01:03 -0700874 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
875 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900876
877 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700878 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900879 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
880 ALOGV("Overriding effect input as 48kHz");
881 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700882 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900883 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
885 mConfig.inputCfg.bufferProvider.cookie = NULL;
886 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
887 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
888 mConfig.outputCfg.bufferProvider.cookie = NULL;
889 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
890 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
891 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
892 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800893 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 // always overwrites output buffer: input buffer == output buffer
895 // - in other sessions:
896 // last effect in the chain accumulates in output buffer: input buffer != output buffer
897 // other effect: overwrites output buffer: input buffer == output buffer
898 // Auxiliary effect:
899 // accumulates in output buffer: input buffer != output buffer
900 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700901 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800902 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
903 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700904 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800905 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000906 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800907
Eric Laurent6b446ce2019-12-13 10:56:31 -0800908 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700909 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800910 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800911
912 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700913 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700914 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800915 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700916 &mConfig,
917 &size,
918 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700919 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800920 status = cmdStatus;
921 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800922
Andy Hung9aad48c2017-11-29 10:29:19 -0800923 if (status != NO_ERROR &&
Andy Hungba8e52b2023-05-11 14:33:03 -0700924 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000925 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800926 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
927 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
928 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700929 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
930 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
932 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
933 }
934 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
935 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
936 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
937 }
938 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700939 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800940 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700941 &mConfig,
942 &size,
943 &cmdStatus);
944 if (status == NO_ERROR) {
945 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800946 }
947 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800948
Andy Hung9aad48c2017-11-29 10:29:19 -0800949 if (status == NO_ERROR) {
950 mSupportsFloat = true;
951 }
952
Andy Hungba8e52b2023-05-11 14:33:03 -0700953 // only HIDL effects support integer conversion.
954 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800955 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
956 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
957 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
958 size = sizeof(int);
959 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
960 sizeof(mConfig),
961 &mConfig,
962 &size,
963 &cmdStatus);
964 if (status == NO_ERROR) {
965 status = cmdStatus;
966 }
967 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700968 mSupportsFloat = false;
969 ALOGVV("config worked with 16 bit");
970 } else {
971 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800972 }
rago94a1ee82017-07-21 15:11:02 -0700973 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800974
rago94a1ee82017-07-21 15:11:02 -0700975 if (status == NO_ERROR) {
976 // Establish Buffer strategy
977 setInBuffer(mInBuffer);
978 setOutBuffer(mOutBuffer);
979
980 // Update visualizer latency
981 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
982 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
983 effect_param_t *p = (effect_param_t *)buf32;
984
985 p->psize = sizeof(uint32_t);
986 p->vsize = sizeof(uint32_t);
987 size = sizeof(int);
988 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
989
Andy Hungfda44002021-06-03 17:23:16 -0700990 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700991
992 *((int32_t *)p->data + 1)= latency;
993 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
994 sizeof(effect_param_t) + 8,
995 &buf32,
996 &size,
997 &cmdStatus);
998 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800999 }
1000
Andy Hung05083ac2017-12-14 15:00:28 -08001001 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1002 mMaxDisableWaitCnt = (uint32_t)std::max(
1003 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1004 (uint64_t)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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-06-20 18:56:17 -07001522bool EffectModule::isHapticGenerator() const {
1523 return IAfEffectModule::isHapticGenerator(&mDescriptor.type);
jiabineb3bda02020-06-30 14:07:03 -07001524}
1525
Andy Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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;
Andy Hung9a3f8182023-07-18 20:54:44 -07001619 const bool locked = afutils::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 Hung699435e2023-07-19 11:31:15 -07001635 toString(static_cast<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,
Andy Hung699435e2023-07-19 11:31:15 -07001645 toString(static_cast<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
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00001654 write(fd, result.c_str(), result.length());
Eric Laurentca7cc822012-11-19 14:55:58 -08001655
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 Hung116bc262023-06-20 18:56:17 -07001671#define LOG_TAG "EffectHandle"
Eric Laurentca7cc822012-11-19 14:55:58 -08001672
Andy Hung116bc262023-06-20 18:56:17 -07001673/* static */
1674sp<IAfEffectHandle> IAfEffectHandle::create(
1675 const sp<IAfEffectBase>& effect,
Andy Hung88035ac2023-06-27 17:05:02 -07001676 const sp<Client>& client,
Andy Hung116bc262023-06-20 18:56:17 -07001677 const sp<media::IEffectClient>& effectClient,
1678 int32_t priority, bool notifyFramesProcessed)
1679{
1680 return sp<EffectHandle>::make(
Andy Hung88035ac2023-06-27 17:05:02 -07001681 effect, client, effectClient, priority, notifyFramesProcessed);
Andy Hung116bc262023-06-20 18:56:17 -07001682}
1683
1684EffectHandle::EffectHandle(const sp<IAfEffectBase>& effect,
Andy Hung88035ac2023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung85a07452023-08-28 18:36:53 -07001884 audio_utils::lock_guard _l2(mClient->afClientCallback()->clientMutex());
Eric Laurentca7cc822012-11-19 14:55:58 -08001885 mClient.clear();
1886 }
1887}
1888
Andy Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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 Hung116bc262023-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{
Andy Hung9a3f8182023-07-18 20:54:44 -07002093 const bool locked = mCblk != nullptr && afutils::dumpTryLock(mCblk->lock);
Eric Laurentca7cc822012-11-19 14:55:58 -08002094
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 Hung116bc262023-06-20 18:56:17 -07002110#define LOG_TAG "EffectChain"
Eric Laurentca7cc822012-11-19 14:55:58 -08002111
Andy Hung116bc262023-06-20 18:56:17 -07002112/* static */
2113sp<IAfEffectChain> IAfEffectChain::create(
Andy Hungb1dac1a2023-07-17 17:05:00 -07002114 const sp<IAfThreadBase>& thread,
Andy Hung116bc262023-06-20 18:56:17 -07002115 audio_session_t sessionId)
2116{
Andy Hungb1dac1a2023-07-17 17:05:00 -07002117 return sp<EffectChain>::make(thread, sessionId);
Andy Hung116bc262023-06-20 18:56:17 -07002118}
2119
Andy Hungb1dac1a2023-07-17 17:05:00 -07002120EffectChain::EffectChain(const sp<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),
jiabin75935aa2023-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 Hungb1dac1a2023-07-17 17:05:00 -07002127 mStrategy = thread->getStrategyForStream(AUDIO_STREAM_MUSIC);
2128 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
2129 thread->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002130}
2131
Andy Hungd3888022023-07-06 20:56:16 -07002132// getEffectFromDesc_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002133sp<IAfEffectModule> EffectChain::getEffectFromDesc_l(
2134 effect_descriptor_t *descriptor) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002135{
2136 size_t size = mEffects.size();
2137
2138 for (size_t i = 0; i < size; i++) {
2139 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2140 return mEffects[i];
2141 }
2142 }
2143 return 0;
2144}
2145
Andy Hungd3888022023-07-06 20:56:16 -07002146// getEffectFromId_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002147sp<IAfEffectModule> EffectChain::getEffectFromId_l(int id) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002148{
2149 size_t size = mEffects.size();
2150
2151 for (size_t i = 0; i < size; i++) {
2152 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2153 if (id == 0 || mEffects[i]->id() == id) {
2154 return mEffects[i];
2155 }
2156 }
2157 return 0;
2158}
2159
Andy Hungd3888022023-07-06 20:56:16 -07002160// getEffectFromType_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002161sp<IAfEffectModule> EffectChain::getEffectFromType_l(
2162 const effect_uuid_t *type) const
Eric Laurentca7cc822012-11-19 14:55:58 -08002163{
2164 size_t size = mEffects.size();
2165
2166 for (size_t i = 0; i < size; i++) {
2167 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2168 return mEffects[i];
2169 }
2170 }
2171 return 0;
2172}
2173
Andy Hung116bc262023-06-20 18:56:17 -07002174std::vector<int> EffectChain::getEffectIds() const
Eric Laurent6c796322019-04-09 14:13:17 -07002175{
2176 std::vector<int> ids;
2177 Mutex::Autolock _l(mLock);
2178 for (size_t i = 0; i < mEffects.size(); i++) {
2179 ids.push_back(mEffects[i]->id());
2180 }
2181 return ids;
2182}
2183
Andy Hung116bc262023-06-20 18:56:17 -07002184void EffectChain::clearInputBuffer()
Eric Laurentca7cc822012-11-19 14:55:58 -08002185{
2186 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002187 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002188}
2189
2190// Must be called with EffectChain::mLock locked
Andy Hung116bc262023-06-20 18:56:17 -07002191void EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002192{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002193 if (mInBuffer == NULL) {
2194 return;
2195 }
Andy Hung319587b2023-05-23 14:01:03 -07002196 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002197 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002198
Eric Laurent6b446ce2019-12-13 10:56:31 -08002199 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002200 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002201}
2202
2203// Must be called with EffectChain::mLock locked
Andy Hung116bc262023-06-20 18:56:17 -07002204void EffectChain::process_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002205{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002206 // never process effects when:
2207 // - on an OFFLOAD thread
2208 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002209 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002210 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002211 bool tracksOnSession = (trackCnt() != 0);
2212
2213 if (!tracksOnSession && mTailBufferCount == 0) {
2214 doProcess = false;
2215 }
2216
2217 if (activeTrackCnt() == 0) {
2218 // if no track is active and the effect tail has not been rendered,
2219 // the input buffer must be cleared here as the mixer process will not do it
2220 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002221 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 if (mTailBufferCount > 0) {
2223 mTailBufferCount--;
2224 }
2225 }
2226 }
2227 }
2228
2229 size_t size = mEffects.size();
2230 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002231 // Only the input and output buffers of the chain can be external,
2232 // and 'update' / 'commit' do nothing for allocated buffers, thus
2233 // it's not needed to consider any other buffers here.
2234 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002235 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2236 mOutBuffer->update();
2237 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002238 for (size_t i = 0; i < size; i++) {
2239 mEffects[i]->process();
2240 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002241 mInBuffer->commit();
2242 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2243 mOutBuffer->commit();
2244 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002245 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002246 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002247 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002248 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2249 }
2250 if (doResetVolume) {
2251 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002252 }
2253}
2254
Andy Hungd3888022023-07-06 20:56:16 -07002255// createEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002256status_t EffectChain::createEffect_l(sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002257 effect_descriptor_t *desc,
2258 int id,
2259 audio_session_t sessionId,
2260 bool pinned)
2261{
2262 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002263 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002264 status_t lStatus = effect->status();
2265 if (lStatus == NO_ERROR) {
2266 lStatus = addEffect_ll(effect);
2267 }
2268 if (lStatus != NO_ERROR) {
2269 effect.clear();
2270 }
2271 return lStatus;
2272}
2273
Andy Hungd3888022023-07-06 20:56:16 -07002274// addEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002275status_t EffectChain::addEffect_l(const sp<IAfEffectModule>& effect)
Eric Laurentca7cc822012-11-19 14:55:58 -08002276{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002277 Mutex::Autolock _l(mLock);
2278 return addEffect_ll(effect);
2279}
Andy Hungd3888022023-07-06 20:56:16 -07002280// addEffect_l() must be called with IAfThreadBase::mLock and EffectChain::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002281status_t EffectChain::addEffect_ll(const sp<IAfEffectModule>& effect)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002282{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002283 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002284
Eric Laurentb62d0362021-10-26 17:40:18 +02002285 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002286 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2287 // Auxiliary effects are inserted at the beginning of mEffects vector as
2288 // they are processed first and accumulated in chain input buffer
2289 mEffects.insertAt(effect, 0);
2290
2291 // the input buffer for auxiliary effect contains mono samples in
2292 // 32 bit format. This is to avoid saturation in AudoMixer
2293 // accumulation stage. Saturation is done in EffectModule::process() before
2294 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002295 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002296 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002297
Eric Laurent6b446ce2019-12-13 10:56:31 -08002298 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002299 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002300 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002301
2302 effect->configure();
2303
Mikhail Naganov022b9952017-01-04 16:36:51 -08002304 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 // auxiliary effects output samples to chain input buffer for further processing
2306 // by insert effects
2307 effect->setOutBuffer(mInBuffer);
2308 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002309 ssize_t idx_insert = getInsertIndex(desc);
2310 if (idx_insert < 0) {
2311 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 }
2313
Eric Laurentb62d0362021-10-26 17:40:18 +02002314 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002315 mEffects.insertAt(effect, idx_insert);
2316
2317 effect->configure();
2318
Eric Laurentb62d0362021-10-26 17:40:18 +02002319 // - By default:
2320 // All effects read samples from chain input buffer.
2321 // The last effect in the chain, writes samples to chain output buffer,
2322 // otherwise to chain input buffer
2323 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2324 // The spatializer effect (first effect) reads samples from the input buffer
2325 // and writes samples to the output buffer.
2326 // All other effects read and writes samples to the output buffer
2327 if (mEffectCallback->isSpatializer()
2328 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002330 if (idx_insert == 0) {
2331 if (previousSize != 0) {
2332 mEffects[1]->configure();
2333 mEffects[1]->setInBuffer(mOutBuffer);
2334 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2335 }
2336 effect->setInBuffer(mInBuffer);
2337 } else {
2338 effect->setInBuffer(mOutBuffer);
2339 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002340 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002341 effect->setInBuffer(mInBuffer);
Andy Hung920f6572022-10-06 12:09:49 -07002342 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002343 if (idx_insert != 0) {
2344 mEffects[idx_insert-1]->configure();
2345 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2346 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2347 }
2348 effect->setOutBuffer(mOutBuffer);
2349 } else {
2350 effect->setOutBuffer(mInBuffer);
2351 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002353 ALOGV("%s effect %p, added in chain %p at rank %zu",
2354 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002355 }
2356 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002357
Eric Laurentca7cc822012-11-19 14:55:58 -08002358 return NO_ERROR;
2359}
2360
jiabin75935aa2023-08-24 21:10:44 +00002361std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
2362 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2363 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2364 return i - 1;
2365 }
2366 }
2367 return std::nullopt;
2368}
2369
Andy Hung116bc262023-06-20 18:56:17 -07002370ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002371 // Insert effects are inserted at the end of mEffects vector as they are processed
2372 // after track and auxiliary effects.
2373 // Insert effect order as a function of indicated preference:
2374 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2375 // another effect is present
2376 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2377 // last effect claiming first position
2378 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2379 // first effect claiming last position
2380 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2381 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2382 // already present
2383 // Spatializer or Downmixer effects are inserted in first position because
2384 // they adapt the channel count for all other effects in the chain
2385 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2386 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2387 return 0;
2388 }
2389
2390 size_t size = mEffects.size();
2391 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2392 ssize_t idx_insert;
2393 ssize_t idx_insert_first = -1;
2394 ssize_t idx_insert_last = -1;
2395
2396 idx_insert = size;
2397 for (size_t i = 0; i < size; i++) {
2398 effect_descriptor_t d = mEffects[i]->desc();
2399 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2400 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2401 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2402 // check invalid effect chaining combinations
2403 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2404 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2405 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2406 __func__, desc.name, d.name);
2407 return -1;
2408 }
2409 // remember position of first insert effect and by default
2410 // select this as insert position for new effect
Andy Hung920f6572022-10-06 12:09:49 -07002411 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002412 idx_insert = i;
2413 }
2414 // remember position of last insert effect claiming
2415 // first position
2416 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2417 idx_insert_first = i;
2418 }
2419 // remember position of first insert effect claiming
2420 // last position
2421 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2422 idx_insert_last == -1) {
2423 idx_insert_last = i;
2424 }
2425 }
2426 }
2427
2428 // modify idx_insert from first position if needed
2429 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2430 if (idx_insert_last != -1) {
2431 idx_insert = idx_insert_last;
2432 } else {
2433 idx_insert = size;
2434 }
2435 } else {
2436 if (idx_insert_first != -1) {
2437 idx_insert = idx_insert_first + 1;
2438 }
2439 }
2440 return idx_insert;
2441}
2442
Andy Hungd3888022023-07-06 20:56:16 -07002443// removeEffect_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002444size_t EffectChain::removeEffect_l(const sp<IAfEffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002445 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002446{
2447 Mutex::Autolock _l(mLock);
2448 size_t size = mEffects.size();
2449 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2450
2451 for (size_t i = 0; i < size; i++) {
2452 if (effect == mEffects[i]) {
2453 // calling stop here will remove pre-processing effect from the audio HAL.
2454 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2455 // the middle of a read from audio HAL
2456 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2457 mEffects[i]->state() == EffectModule::STOPPING) {
2458 mEffects[i]->stop();
2459 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002460 if (release) {
2461 mEffects[i]->release_l();
2462 }
2463
Mikhail Naganov022b9952017-01-04 16:36:51 -08002464 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002465 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002466 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002467 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002468 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002469 }
2470 }
2471 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002472
2473 // make sure the input buffer configuration for the new first effect in the chain
2474 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002475 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2476 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002477 mEffects[0]->configure();
2478 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002479 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002480 }
2481
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002482 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002483 this, i);
2484 break;
2485 }
2486 }
2487
2488 return mEffects.size();
2489}
2490
Andy Hungd3888022023-07-06 20:56:16 -07002491// setDevices_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002492void EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002493{
2494 size_t size = mEffects.size();
2495 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002496 mEffects[i]->setDevices(devices);
2497 }
2498}
2499
Andy Hungd3888022023-07-06 20:56:16 -07002500// setInputDevice_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002501void EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
jiabin8f278ee2019-11-11 12:16:27 -08002502{
2503 size_t size = mEffects.size();
2504 for (size_t i = 0; i < size; i++) {
2505 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002506 }
2507}
2508
Andy Hungd3888022023-07-06 20:56:16 -07002509// setMode_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002510void EffectChain::setMode_l(audio_mode_t mode)
Eric Laurentca7cc822012-11-19 14:55:58 -08002511{
2512 size_t size = mEffects.size();
2513 for (size_t i = 0; i < size; i++) {
2514 mEffects[i]->setMode(mode);
2515 }
2516}
2517
Andy Hungd3888022023-07-06 20:56:16 -07002518// setAudioSource_l() must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002519void EffectChain::setAudioSource_l(audio_source_t source)
Eric Laurentca7cc822012-11-19 14:55:58 -08002520{
2521 size_t size = mEffects.size();
2522 for (size_t i = 0; i < size; i++) {
2523 mEffects[i]->setAudioSource(source);
2524 }
2525}
2526
Andy Hung116bc262023-06-20 18:56:17 -07002527bool EffectChain::hasVolumeControlEnabled_l() const {
Zhou Songd505c642020-02-20 16:35:37 +08002528 for (const auto &effect : mEffects) {
2529 if (effect->isVolumeControlEnabled()) return true;
2530 }
2531 return false;
2532}
2533
Andy Hungd3888022023-07-06 20:56:16 -07002534// setVolume_l() must be called with IAfThreadBase::mLock or EffectChain::mLock held
Andy Hung116bc262023-06-20 18:56:17 -07002535bool EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002536{
2537 uint32_t newLeft = *left;
2538 uint32_t newRight = *right;
jiabin75935aa2023-08-24 21:10:44 +00002539 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002540
2541 // first update volume controller
jiabin75935aa2023-08-24 21:10:44 +00002542 const auto volumeControlIndex = findVolumeControl_l(0, size);
2543 const int ctrlIdx = volumeControlIndex.value_or(-1);
2544 const sp<IAfEffectModule> volumeControlEffect =
2545 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
2546 const sp<IAfEffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002547
jiabin75935aa2023-08-24 21:10:44 +00002548 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002549 *left == mLeftVolume && *right == mRightVolume) {
jiabin75935aa2023-08-24 21:10:44 +00002550 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002551 *left = mNewLeftVolume;
2552 *right = mNewRightVolume;
2553 }
jiabin75935aa2023-08-24 21:10:44 +00002554 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002555 }
2556
jiabin75935aa2023-08-24 21:10:44 +00002557 if (volumeControlEffect != cachedVolumeControlEffect) {
2558 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2559 // as zero for safe ramping.
2560 if (cachedVolumeControlEffect != nullptr) {
2561 uint32_t leftMax = 1 << 24;
2562 uint32_t rightMax = 1 << 24;
2563 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2564 }
2565 if (volumeControlEffect != nullptr) {
2566 uint32_t leftZero = 0;
2567 uint32_t rightZero = 0;
2568 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2569 }
2570 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002571 mLeftVolume = newLeft;
2572 mRightVolume = newRight;
2573
2574 // second get volume update from volume controller
2575 if (ctrlIdx >= 0) {
2576 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2577 mNewLeftVolume = newLeft;
2578 mNewRightVolume = newRight;
2579 }
2580 // then indicate volume to all other effects in chain.
2581 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002582 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002583 uint32_t lVol = newLeft;
2584 uint32_t rVol = newRight;
2585
2586 for (size_t i = 0; i < size; i++) {
2587 if ((int)i == ctrlIdx) {
2588 continue;
2589 }
2590 // this also works for ctrlIdx == -1 when there is no volume controller
2591 if ((int)i > ctrlIdx) {
2592 lVol = *left;
2593 rVol = *right;
2594 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002595 // Pass requested volume directly if this is volume monitor module
2596 if (mEffects[i]->isVolumeMonitor()) {
2597 mEffects[i]->setVolume(left, right, false);
2598 } else {
2599 mEffects[i]->setVolume(&lVol, &rVol, false);
2600 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002601 }
2602 *left = newLeft;
2603 *right = newRight;
2604
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002605 setVolumeForOutput_l(*left, *right);
2606
jiabin75935aa2023-08-24 21:10:44 +00002607 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002608}
2609
Andy Hungd3888022023-07-06 20:56:16 -07002610// resetVolume_l() must be called with IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hung116bc262023-06-20 18:56:17 -07002611void EffectChain::resetVolume_l()
Eric Laurentfa1e1232016-08-02 19:01:49 -07002612{
Eric Laurente7449bf2016-08-03 18:44:07 -07002613 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2614 uint32_t left = mLeftVolume;
2615 uint32_t right = mRightVolume;
2616 (void)setVolume_l(&left, &right, true);
2617 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002618}
2619
Andy Hung116bc262023-06-20 18:56:17 -07002620// containsHapticGeneratingEffect_l must be called with
Andy Hungd3888022023-07-06 20:56:16 -07002621// IAfThreadBase::mutex() or EffectChain::mLock held
Andy Hung116bc262023-06-20 18:56:17 -07002622bool EffectChain::containsHapticGeneratingEffect_l()
jiabineb3bda02020-06-30 14:07:03 -07002623{
2624 for (size_t i = 0; i < mEffects.size(); ++i) {
2625 if (mEffects[i]->isHapticGenerator()) {
2626 return true;
2627 }
2628 }
2629 return false;
2630}
2631
Andy Hung116bc262023-06-20 18:56:17 -07002632void EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002633{
2634 Mutex::Autolock _l(mLock);
2635 for (size_t i = 0; i < mEffects.size(); ++i) {
2636 mEffects[i]->setHapticIntensity(id, intensity);
2637 }
2638}
2639
Andy Hung116bc262023-06-20 18:56:17 -07002640void EffectChain::syncHalEffectsState()
Eric Laurent1b928682014-10-02 19:41:47 -07002641{
2642 Mutex::Autolock _l(mLock);
2643 for (size_t i = 0; i < mEffects.size(); i++) {
2644 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2645 mEffects[i]->state() == EffectModule::STOPPING) {
2646 mEffects[i]->addEffectToHal_l();
2647 }
2648 }
2649}
2650
Andy Hung116bc262023-06-20 18:56:17 -07002651void EffectChain::dump(int fd, const Vector<String16>& args) const
Andy Hung920f6572022-10-06 12:09:49 -07002652NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002653{
Eric Laurentca7cc822012-11-19 14:55:58 -08002654 String8 result;
2655
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002656 const size_t numEffects = mEffects.size();
2657 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002658
Marco Nelissenb2208842014-02-07 14:00:50 -08002659 if (numEffects) {
Andy Hung9a3f8182023-07-18 20:54:44 -07002660 const bool locked = afutils::dumpTryLock(mLock);
Marco Nelissenb2208842014-02-07 14:00:50 -08002661 // failed to lock - AudioFlinger is probably deadlocked
2662 if (!locked) {
2663 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002664 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002665
Andy Hungbded9c82017-11-30 18:47:35 -08002666 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2667 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2668 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2669 (int)inBufferStr.size(), "In buffer ",
2670 (int)outBufferStr.size(), "Out buffer ");
2671 result.appendFormat("\t%s %s %d\n",
2672 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00002673 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002674
2675 for (size_t i = 0; i < numEffects; ++i) {
Andy Hung116bc262023-06-20 18:56:17 -07002676 sp<IAfEffectModule> effect = mEffects[i];
Marco Nelissenb2208842014-02-07 14:00:50 -08002677 if (effect != 0) {
2678 effect->dump(fd, args);
2679 }
2680 }
2681
2682 if (locked) {
2683 mLock.unlock();
2684 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002685 } else {
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00002686 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002687 }
2688}
2689
Andy Hungd3888022023-07-06 20:56:16 -07002690// must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002691void EffectChain::setEffectSuspended_l(
Eric Laurentca7cc822012-11-19 14:55:58 -08002692 const effect_uuid_t *type, bool suspend)
2693{
2694 sp<SuspendedEffectDesc> desc;
2695 // use effect type UUID timelow as key as there is no real risk of identical
2696 // timeLow fields among effect type UUIDs.
2697 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2698 if (suspend) {
2699 if (index >= 0) {
2700 desc = mSuspendedEffects.valueAt(index);
2701 } else {
2702 desc = new SuspendedEffectDesc();
2703 desc->mType = *type;
2704 mSuspendedEffects.add(type->timeLow, desc);
2705 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2706 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002707
Eric Laurentca7cc822012-11-19 14:55:58 -08002708 if (desc->mRefCount++ == 0) {
Andy Hung116bc262023-06-20 18:56:17 -07002709 sp<IAfEffectModule> effect = getEffectIfEnabled(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002710 if (effect != 0) {
2711 desc->mEffect = effect;
2712 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002713 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002714 }
2715 }
2716 } else {
2717 if (index < 0) {
2718 return;
2719 }
2720 desc = mSuspendedEffects.valueAt(index);
2721 if (desc->mRefCount <= 0) {
2722 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002723 desc->mRefCount = 0;
2724 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002725 }
2726 if (--desc->mRefCount == 0) {
2727 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2728 if (desc->mEffect != 0) {
Andy Hung116bc262023-06-20 18:56:17 -07002729 sp<IAfEffectModule> effect = desc->mEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002730 if (effect != 0) {
2731 effect->setSuspended(false);
2732 effect->lock();
Andy Hung116bc262023-06-20 18:56:17 -07002733 IAfEffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002734 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002735 effect->setEnabled_l(handle->enabled());
2736 }
2737 effect->unlock();
2738 }
2739 desc->mEffect.clear();
2740 }
2741 mSuspendedEffects.removeItemsAt(index);
2742 }
2743 }
2744}
2745
Andy Hungd3888022023-07-06 20:56:16 -07002746// must be called with IAfThreadBase::mutex() held
Andy Hung116bc262023-06-20 18:56:17 -07002747void EffectChain::setEffectSuspendedAll_l(bool suspend)
Eric Laurentca7cc822012-11-19 14:55:58 -08002748{
2749 sp<SuspendedEffectDesc> desc;
2750
2751 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2752 if (suspend) {
2753 if (index >= 0) {
2754 desc = mSuspendedEffects.valueAt(index);
2755 } else {
2756 desc = new SuspendedEffectDesc();
2757 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2758 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2759 }
2760 if (desc->mRefCount++ == 0) {
Andy Hung116bc262023-06-20 18:56:17 -07002761 Vector< sp<IAfEffectModule> > effects;
Eric Laurentca7cc822012-11-19 14:55:58 -08002762 getSuspendEligibleEffects(effects);
2763 for (size_t i = 0; i < effects.size(); i++) {
2764 setEffectSuspended_l(&effects[i]->desc().type, true);
2765 }
2766 }
2767 } else {
2768 if (index < 0) {
2769 return;
2770 }
2771 desc = mSuspendedEffects.valueAt(index);
2772 if (desc->mRefCount <= 0) {
2773 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2774 desc->mRefCount = 1;
2775 }
2776 if (--desc->mRefCount == 0) {
2777 Vector<const effect_uuid_t *> types;
2778 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2779 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2780 continue;
2781 }
2782 types.add(&mSuspendedEffects.valueAt(i)->mType);
2783 }
2784 for (size_t i = 0; i < types.size(); i++) {
2785 setEffectSuspended_l(types[i], false);
2786 }
2787 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2788 mSuspendedEffects.keyAt(index));
2789 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2790 }
2791 }
2792}
2793
2794
2795// The volume effect is used for automated tests only
2796#ifndef OPENSL_ES_H_
2797static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2798 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2799const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2800#endif //OPENSL_ES_H_
2801
Eric Laurentd8365c52017-07-16 15:27:05 -07002802/* static */
Andy Hung116bc262023-06-20 18:56:17 -07002803bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
Eric Laurentd8365c52017-07-16 15:27:05 -07002804{
2805 // Only NS and AEC are suspended when BtNRec is off
2806 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2807 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2808 return true;
2809 }
2810 return false;
2811}
2812
Andy Hung116bc262023-06-20 18:56:17 -07002813bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
Eric Laurentca7cc822012-11-19 14:55:58 -08002814{
2815 // auxiliary effects and visualizer are never suspended on output mix
2816 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2817 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2818 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002819 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2820 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002821 return false;
2822 }
2823 return true;
2824}
2825
Andy Hung116bc262023-06-20 18:56:17 -07002826void EffectChain::getSuspendEligibleEffects(
2827 Vector< sp<IAfEffectModule> > &effects)
Eric Laurentca7cc822012-11-19 14:55:58 -08002828{
2829 effects.clear();
2830 for (size_t i = 0; i < mEffects.size(); i++) {
2831 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2832 effects.add(mEffects[i]);
2833 }
2834 }
2835}
2836
Andy Hung116bc262023-06-20 18:56:17 -07002837sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
Eric Laurentca7cc822012-11-19 14:55:58 -08002838{
Andy Hung116bc262023-06-20 18:56:17 -07002839 sp<IAfEffectModule> effect = getEffectFromType_l(type);
Eric Laurentca7cc822012-11-19 14:55:58 -08002840 return effect != 0 && effect->isEnabled() ? effect : 0;
2841}
2842
Andy Hung116bc262023-06-20 18:56:17 -07002843void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08002844 bool enabled)
2845{
2846 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2847 if (enabled) {
2848 if (index < 0) {
2849 // if the effect is not suspend check if all effects are suspended
2850 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2851 if (index < 0) {
2852 return;
2853 }
2854 if (!isEffectEligibleForSuspend(effect->desc())) {
2855 return;
2856 }
2857 setEffectSuspended_l(&effect->desc().type, enabled);
2858 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2859 if (index < 0) {
2860 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2861 return;
2862 }
2863 }
2864 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2865 effect->desc().type.timeLow);
2866 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002867 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002868 if (desc->mEffect == 0) {
2869 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002870 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002871 effect->setSuspended(true);
2872 }
2873 } else {
2874 if (index < 0) {
2875 return;
2876 }
2877 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2878 effect->desc().type.timeLow);
2879 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2880 desc->mEffect.clear();
2881 effect->setSuspended(false);
2882 }
2883}
2884
Andy Hung116bc262023-06-20 18:56:17 -07002885bool EffectChain::isNonOffloadableEnabled() const
Eric Laurent813e2a72013-08-31 12:59:48 -07002886{
2887 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002888 return isNonOffloadableEnabled_l();
2889}
2890
Andy Hung116bc262023-06-20 18:56:17 -07002891bool EffectChain::isNonOffloadableEnabled_l() const
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002892{
Eric Laurent813e2a72013-08-31 12:59:48 -07002893 size_t size = mEffects.size();
2894 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002895 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002896 return true;
2897 }
2898 }
2899 return false;
2900}
2901
Andy Hungd3888022023-07-06 20:56:16 -07002902void EffectChain::setThread(const sp<IAfThreadBase>& thread)
Eric Laurentaaa44472014-09-12 17:41:50 -07002903{
2904 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002905 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002906}
2907
Andy Hung116bc262023-06-20 18:56:17 -07002908void EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002909{
2910 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2911 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2912 }
2913 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2914 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2915 }
jiabinc658e452022-10-21 20:52:21 +00002916 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2917 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2918 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002919}
2920
Andy Hung116bc262023-06-20 18:56:17 -07002921void EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002922{
2923 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2924 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2925 }
2926 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2927 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2928 }
2929}
2930
Andy Hung116bc262023-06-20 18:56:17 -07002931bool EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002932{
2933 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002934 for (const auto &effect : mEffects) {
2935 if (effect->isProcessImplemented()) {
2936 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002937 }
2938 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002939 // Allow effects without processing.
2940 return true;
2941}
2942
Andy Hung116bc262023-06-20 18:56:17 -07002943bool EffectChain::isFastCompatible() const
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002944{
2945 Mutex::Autolock _l(mLock);
2946 for (const auto &effect : mEffects) {
2947 if (effect->isProcessImplemented()
2948 && effect->isImplementationSoftware()) {
2949 return false;
2950 }
2951 }
2952 // Allow effects without processing or hw accelerated effects.
2953 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002954}
2955
Andy Hung116bc262023-06-20 18:56:17 -07002956bool EffectChain::isBitPerfectCompatible() const {
jiabinc658e452022-10-21 20:52:21 +00002957 Mutex::Autolock _l(mLock);
2958 for (const auto &effect : mEffects) {
2959 if (effect->isProcessImplemented()
2960 && effect->isImplementationSoftware()) {
2961 return false;
2962 }
2963 }
2964 // Allow effects without processing or hw accelerated effects.
2965 return true;
2966}
2967
Eric Laurent4c415062016-06-17 16:14:16 -07002968// isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hungd3888022023-07-06 20:56:16 -07002969bool EffectChain::isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const
Eric Laurent4c415062016-06-17 16:14:16 -07002970{
2971 Mutex::Autolock _l(mLock);
2972 for (size_t i = 0; i < mEffects.size(); i++) {
2973 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2974 return false;
2975 }
2976 }
2977 return true;
2978}
2979
Eric Laurent6b446ce2019-12-13 10:56:31 -08002980// EffectCallbackInterface implementation
Andy Hung116bc262023-06-20 18:56:17 -07002981status_t EffectChain::EffectCallback::createEffectHal(
Eric Laurent6b446ce2019-12-13 10:56:31 -08002982 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2983 sp<EffectHalInterface> *effect) {
2984 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002985 const sp<EffectsFactoryHalInterface> effectsFactory =
2986 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002987 if (effectsFactory != 0) {
2988 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2989 }
2990 return status;
2991}
2992
Andy Hung116bc262023-06-20 18:56:17 -07002993bool EffectChain::EffectCallback::updateOrphanEffectChains(
2994 const sp<IAfEffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002995 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hungb1dac1a2023-07-17 17:05:00 -07002996 return mAfThreadCallback->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997}
2998
Andy Hung116bc262023-06-20 18:56:17 -07002999status_t EffectChain::EffectCallback::allocateHalBuffer(
Eric Laurent6b446ce2019-12-13 10:56:31 -08003000 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hungb1dac1a2023-07-17 17:05:00 -07003001 return mAfThreadCallback->getEffectsFactoryHal()->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003002}
3003
Andy Hung116bc262023-06-20 18:56:17 -07003004status_t EffectChain::EffectCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003005 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003006 status_t result = NO_INIT;
Andy Hungd3888022023-07-06 20:56:16 -07003007 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003008 if (t == nullptr) {
3009 return result;
3010 }
3011 sp <StreamHalInterface> st = t->stream();
3012 if (st == nullptr) {
3013 return result;
3014 }
3015 result = st->addEffect(effect);
3016 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3017 return result;
3018}
3019
Andy Hung116bc262023-06-20 18:56:17 -07003020status_t EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003021 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003022 status_t result = NO_INIT;
Andy Hungd3888022023-07-06 20:56:16 -07003023 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003024 if (t == nullptr) {
3025 return result;
3026 }
3027 sp <StreamHalInterface> st = t->stream();
3028 if (st == nullptr) {
3029 return result;
3030 }
3031 result = st->removeEffect(effect);
3032 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3033 return result;
3034}
3035
Andy Hung116bc262023-06-20 18:56:17 -07003036audio_io_handle_t EffectChain::EffectCallback::io() const {
Andy Hungd3888022023-07-06 20:56:16 -07003037 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003038 if (t == nullptr) {
3039 return AUDIO_IO_HANDLE_NONE;
3040 }
3041 return t->id();
3042}
3043
Andy Hung116bc262023-06-20 18:56:17 -07003044bool EffectChain::EffectCallback::isOutput() const {
Andy Hungd3888022023-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 true;
3048 }
3049 return t->isOutput();
3050}
3051
Andy Hung116bc262023-06-20 18:56:17 -07003052bool EffectChain::EffectCallback::isOffload() const {
Andy Hungd3888022023-07-06 20:56:16 -07003053 return mThreadType == IAfThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003054}
3055
Andy Hung116bc262023-06-20 18:56:17 -07003056bool EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hungd3888022023-07-06 20:56:16 -07003057 return mThreadType == IAfThreadBase::OFFLOAD
3058 || mThreadType == IAfThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003059}
3060
Andy Hung116bc262023-06-20 18:56:17 -07003061bool EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003062 switch (mThreadType) {
Andy Hungd3888022023-07-06 20:56:16 -07003063 case IAfThreadBase::OFFLOAD:
3064 case IAfThreadBase::MMAP_PLAYBACK:
3065 case IAfThreadBase::MMAP_CAPTURE:
Eric Laurentb62d0362021-10-26 17:40:18 +02003066 return true;
3067 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003068 return false;
3069 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003070}
3071
Andy Hung116bc262023-06-20 18:56:17 -07003072bool EffectChain::EffectCallback::isSpatializer() const {
Andy Hungd3888022023-07-06 20:56:16 -07003073 return mThreadType == IAfThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003074}
3075
Andy Hung116bc262023-06-20 18:56:17 -07003076uint32_t EffectChain::EffectCallback::sampleRate() const {
Andy Hungd3888022023-07-06 20:56:16 -07003077 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003078 if (t == nullptr) {
3079 return 0;
3080 }
3081 return t->sampleRate();
3082}
3083
Andy Hung116bc262023-06-20 18:56:17 -07003084audio_channel_mask_t EffectChain::EffectCallback::inChannelMask(int id) const {
Andy Hungd3888022023-07-06 20:56:16 -07003085 const sp<IAfThreadBase> t = thread().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003086 if (t == nullptr) {
3087 return AUDIO_CHANNEL_NONE;
3088 }
Andy Hung116bc262023-06-20 18:56:17 -07003089 sp<IAfEffectChain> c = chain().promote();
Eric Laurentf1f22e72021-07-13 14:04:14 +02003090 if (c == nullptr) {
3091 return AUDIO_CHANNEL_NONE;
3092 }
3093
Andy Hungd3888022023-07-06 20:56:16 -07003094 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003095 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3096 if (c->isFirstEffect(id)) {
3097 return t->mixerChannelMask();
3098 } else {
3099 return t->channelMask();
3100 }
3101 } else if (!audio_is_global_session(c->sessionId())) {
Andy Hung116bc262023-06-20 18:56:17 -07003102 if ((t->hasAudioSession_l(c->sessionId())
Andy Hungd3888022023-07-06 20:56:16 -07003103 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003104 return t->mixerChannelMask();
3105 } else {
3106 return t->channelMask();
3107 }
3108 } else {
3109 return t->channelMask();
3110 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003111 } else {
3112 return t->channelMask();
3113 }
3114}
3115
Andy Hung116bc262023-06-20 18:56:17 -07003116uint32_t EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003117 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003118}
3119
Andy Hung116bc262023-06-20 18:56:17 -07003120audio_channel_mask_t EffectChain::EffectCallback::outChannelMask() const {
Andy Hungd3888022023-07-06 20:56:16 -07003121 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003122 if (t == nullptr) {
3123 return AUDIO_CHANNEL_NONE;
3124 }
Andy Hung116bc262023-06-20 18:56:17 -07003125 sp<IAfEffectChain> c = chain().promote();
Eric Laurentb62d0362021-10-26 17:40:18 +02003126 if (c == nullptr) {
3127 return AUDIO_CHANNEL_NONE;
3128 }
3129
Andy Hungd3888022023-07-06 20:56:16 -07003130 if (mThreadType == IAfThreadBase::SPATIALIZER) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003131 if (!audio_is_global_session(c->sessionId())) {
Andy Hung116bc262023-06-20 18:56:17 -07003132 if ((t->hasAudioSession_l(c->sessionId())
Andy Hungd3888022023-07-06 20:56:16 -07003133 & IAfThreadBase::SPATIALIZED_SESSION) != 0) {
Eric Laurentb62d0362021-10-26 17:40:18 +02003134 return t->mixerChannelMask();
3135 } else {
3136 return t->channelMask();
3137 }
3138 } else {
3139 return t->channelMask();
3140 }
3141 } else {
3142 return t->channelMask();
3143 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003144}
3145
Andy Hung116bc262023-06-20 18:56:17 -07003146uint32_t EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003147 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003148}
3149
Andy Hung116bc262023-06-20 18:56:17 -07003150audio_channel_mask_t EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hungd3888022023-07-06 20:56:16 -07003151 const sp<IAfThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003152 if (t == nullptr) {
3153 return AUDIO_CHANNEL_NONE;
3154 }
3155 return t->hapticChannelMask();
3156}
3157
Andy Hung116bc262023-06-20 18:56:17 -07003158size_t EffectChain::EffectCallback::frameCount() const {
Andy Hungd3888022023-07-06 20:56:16 -07003159 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003160 if (t == nullptr) {
3161 return 0;
3162 }
3163 return t->frameCount();
3164}
3165
Andy Hung116bc262023-06-20 18:56:17 -07003166uint32_t EffectChain::EffectCallback::latency() const
Andy Hung920f6572022-10-06 12:09:49 -07003167NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3168{
Andy Hungd3888022023-07-06 20:56:16 -07003169 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003170 if (t == nullptr) {
3171 return 0;
3172 }
Andy Hung920f6572022-10-06 12:09:49 -07003173 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003174 return t->latency_l();
3175}
3176
Andy Hung116bc262023-06-20 18:56:17 -07003177void EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
Andy Hung920f6572022-10-06 12:09:49 -07003178NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3179{
Andy Hungd3888022023-07-06 20:56:16 -07003180 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003181 if (t == nullptr) {
3182 return;
3183 }
3184 t->setVolumeForOutput_l(left, right);
3185}
3186
Andy Hung116bc262023-06-20 18:56:17 -07003187void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
3188 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hungd3888022023-07-06 20:56:16 -07003189 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003190 if (t == nullptr) {
3191 return;
3192 }
3193 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3194
Andy Hung116bc262023-06-20 18:56:17 -07003195 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003196 if (c == nullptr) {
3197 return;
3198 }
Eric Laurent41709552019-12-16 19:34:05 -08003199 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3200 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003201}
3202
Andy Hung116bc262023-06-20 18:56:17 -07003203void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
Andy Hungd3888022023-07-06 20:56:16 -07003204 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003205 if (t == nullptr) {
3206 return;
3207 }
Eric Laurent41709552019-12-16 19:34:05 -08003208 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3209 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003210}
3211
Andy Hung116bc262023-06-20 18:56:17 -07003212void EffectChain::EffectCallback::onEffectDisable(const sp<IAfEffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003213 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3214
Andy Hungd3888022023-07-06 20:56:16 -07003215 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003216 if (t == nullptr) {
3217 return;
3218 }
3219 t->onEffectDisable();
3220}
3221
Andy Hung116bc262023-06-20 18:56:17 -07003222bool EffectChain::EffectCallback::disconnectEffectHandle(IAfEffectHandle *handle,
Eric Laurent6b446ce2019-12-13 10:56:31 -08003223 bool unpinIfLast) {
Andy Hungd3888022023-07-06 20:56:16 -07003224 const sp<IAfThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003225 if (t == nullptr) {
3226 return false;
3227 }
3228 t->disconnectEffectHandle(handle, unpinIfLast);
3229 return true;
3230}
3231
Andy Hung116bc262023-06-20 18:56:17 -07003232void EffectChain::EffectCallback::resetVolume() {
3233 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003234 if (c == nullptr) {
3235 return;
3236 }
3237 c->resetVolume_l();
3238
3239}
3240
Andy Hung116bc262023-06-20 18:56:17 -07003241product_strategy_t EffectChain::EffectCallback::strategy() const {
3242 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003243 if (c == nullptr) {
3244 return PRODUCT_STRATEGY_NONE;
3245 }
3246 return c->strategy();
3247}
3248
Andy Hung116bc262023-06-20 18:56:17 -07003249int32_t EffectChain::EffectCallback::activeTrackCnt() const {
3250 sp<IAfEffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003251 if (c == nullptr) {
3252 return 0;
3253 }
3254 return c->activeTrackCnt();
3255}
3256
Eric Laurentb82e6b72019-11-22 17:25:04 -08003257
3258#undef LOG_TAG
Andy Hung116bc262023-06-20 18:56:17 -07003259#define LOG_TAG "DeviceEffectProxy"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003260
Andy Hung116bc262023-06-20 18:56:17 -07003261/* static */
3262sp<IAfDeviceEffectProxy> IAfDeviceEffectProxy::create(
3263 const AudioDeviceTypeAddr& device,
Andy Hungca2ebe52023-07-13 19:54:47 -07003264 const sp<DeviceEffectManagerCallback>& callback,
Andy Hung116bc262023-06-20 18:56:17 -07003265 effect_descriptor_t *desc, int id, bool notifyFramesProcessed)
3266{
3267 return sp<DeviceEffectProxy>::make(device,
Andy Hungca2ebe52023-07-13 19:54:47 -07003268 callback,
Andy Hung116bc262023-06-20 18:56:17 -07003269 desc, id, notifyFramesProcessed);
3270}
3271
3272status_t DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003273{
3274 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3275 Mutex::Autolock _l(mProxyLock);
3276 if (status == NO_ERROR) {
3277 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003278 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003279 if (enabled) {
Andy Hung116bc262023-06-20 18:56:17 -07003280 bs = handle.second->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003281 } else {
Andy Hung116bc262023-06-20 18:56:17 -07003282 bs = handle.second->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003283 }
3284 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003285 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003286 }
3287 }
3288 }
3289 ALOGV("%s enable %d status %d", __func__, enabled, status);
3290 return status;
3291}
3292
Andy Hung116bc262023-06-20 18:56:17 -07003293status_t DeviceEffectProxy::init(
Andy Hunga289990f2023-07-13 18:11:33 -07003294 const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003295//For all audio patches
3296//If src or sink device match
3297//If the effect is HW accelerated
3298// if no corresponding effect module
3299// Create EffectModule: mHalEffect
3300//Create and attach EffectHandle
3301//If the effect is not HW accelerated and the patch sink or src is a mixer port
3302// Create Effect on patch input or output thread on session -1
3303//Add EffectHandle to EffectHandle map of Effect Proxy:
3304 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3305 status_t status = NO_ERROR;
3306 for (auto &patch : patches) {
3307 status = onCreatePatch(patch.first, patch.second);
3308 ALOGV("%s onCreatePatch status %d", __func__, status);
3309 if (status == BAD_VALUE) {
3310 return status;
3311 }
3312 }
3313 return status;
3314}
3315
Andy Hung116bc262023-06-20 18:56:17 -07003316status_t DeviceEffectProxy::onCreatePatch(
Andy Hunga289990f2023-07-13 18:11:33 -07003317 audio_patch_handle_t patchHandle, const IAfPatchPanel::Patch& patch) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003318 status_t status = NAME_NOT_FOUND;
Andy Hung116bc262023-06-20 18:56:17 -07003319 sp<IAfEffectHandle> handle;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003320 // only consider source[0] as this is the only "true" source of a patch
3321 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3322 ALOGV("%s source checkPort status %d", __func__, status);
3323 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3324 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3325 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3326 }
3327 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3328 Mutex::Autolock _l(mProxyLock);
3329 mEffectHandles.emplace(patchHandle, handle);
3330 }
3331 ALOGW_IF(status == BAD_VALUE,
3332 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3333
3334 return status;
3335}
3336
Andy Hunga289990f2023-07-13 18:11:33 -07003337status_t DeviceEffectProxy::checkPort(const IAfPatchPanel::Patch& patch,
Andy Hung116bc262023-06-20 18:56:17 -07003338 const struct audio_port_config *port, sp<IAfEffectHandle> *handle) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003339
3340 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3341 __func__, port->type, port->ext.device.type,
3342 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaob009ad52023-05-05 22:43:24 +00003343 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3344 port->ext.device.address != mDevice.address()) {
3345 return NAME_NOT_FOUND;
3346 }
3347 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3348 (audio_port_config_has_input_direction(port))) {
3349 ALOGI("%s don't create postprocessing effect on record port", __func__);
3350 return NAME_NOT_FOUND;
3351 }
3352 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3353 (!audio_port_config_has_input_direction(port))) {
3354 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003355 return NAME_NOT_FOUND;
3356 }
3357 status_t status = NAME_NOT_FOUND;
3358
3359 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3360 Mutex::Autolock _l(mProxyLock);
3361 mDevicePort = *port;
3362 mHalEffect = new EffectModule(mMyCallback,
3363 const_cast<effect_descriptor_t *>(&mDescriptor),
3364 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3365 false /* pinned */, port->id);
3366 if (audio_is_input_device(mDevice.mType)) {
3367 mHalEffect->setInputDevice(mDevice);
3368 } else {
3369 mHalEffect->setDevices({mDevice});
3370 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003371 mHalEffect->configure();
3372
Eric Laurentde8caf42021-08-11 17:19:25 +02003373 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3374 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003375 status = (*handle)->initCheck();
3376 if (status == OK) {
3377 status = mHalEffect->addHandle((*handle).get());
3378 } else {
3379 mHalEffect.clear();
3380 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3381 }
3382 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
Andy Hungd3888022023-07-06 20:56:16 -07003383 sp<IAfThreadBase> thread;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003384 if (audio_port_config_has_input_direction(port)) {
3385 if (patch.isSoftware()) {
3386 thread = patch.mRecord.thread();
3387 } else {
3388 thread = patch.thread().promote();
3389 }
3390 } else {
3391 if (patch.isSoftware()) {
3392 thread = patch.mPlayback.thread();
3393 } else {
3394 thread = patch.thread().promote();
3395 }
3396 }
3397 int enabled;
3398 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3399 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003400 &enabled, &status, false, false /*probe*/,
3401 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003402 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3403 } else {
3404 status = BAD_VALUE;
3405 }
Shunkai Yaob009ad52023-05-05 22:43:24 +00003406
Eric Laurentb82e6b72019-11-22 17:25:04 -08003407 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003408 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003409 if (isEnabled()) {
Andy Hung116bc262023-06-20 18:56:17 -07003410 bs = (*handle)->asIEffect()->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003411 } else {
Andy Hung116bc262023-06-20 18:56:17 -07003412 bs = (*handle)->asIEffect()->disable(&status);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003413 }
3414 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003415 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003416 }
3417 }
3418 return status;
3419}
3420
Andy Hung116bc262023-06-20 18:56:17 -07003421void DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3422 sp<IAfEffectHandle> effect;
Eric Laurent76c89f32021-12-03 17:13:23 +01003423 {
3424 Mutex::Autolock _l(mProxyLock);
3425 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3426 effect = mEffectHandles.at(patchHandle);
3427 mEffectHandles.erase(patchHandle);
3428 }
3429 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003430}
3431
3432
Andy Hung116bc262023-06-20 18:56:17 -07003433size_t DeviceEffectProxy::removeEffect(const sp<IAfEffectModule>& effect)
Eric Laurentb82e6b72019-11-22 17:25:04 -08003434{
3435 Mutex::Autolock _l(mProxyLock);
3436 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003437 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003438 mHalEffect.clear();
3439 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3440 }
3441 return mHalEffect == nullptr ? 0 : 1;
3442}
3443
Andy Hung116bc262023-06-20 18:56:17 -07003444status_t DeviceEffectProxy::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003445 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003446 if (mHalEffect == nullptr) {
3447 return NO_INIT;
3448 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003449 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003450}
3451
Andy Hung116bc262023-06-20 18:56:17 -07003452status_t DeviceEffectProxy::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003453 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003454 if (mHalEffect == nullptr) {
3455 return NO_INIT;
3456 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003457 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003458}
3459
Andy Hung116bc262023-06-20 18:56:17 -07003460bool DeviceEffectProxy::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003461 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3462 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3463 }
3464 return true;
3465}
3466
Andy Hung116bc262023-06-20 18:56:17 -07003467uint32_t DeviceEffectProxy::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003468 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3469 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3470 return mDevicePort.sample_rate;
3471 }
3472 return DEFAULT_OUTPUT_SAMPLE_RATE;
3473}
3474
Andy Hung116bc262023-06-20 18:56:17 -07003475audio_channel_mask_t DeviceEffectProxy::channelMask() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003476 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3477 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3478 return mDevicePort.channel_mask;
3479 }
3480 return AUDIO_CHANNEL_OUT_STEREO;
3481}
3482
Andy Hung116bc262023-06-20 18:56:17 -07003483uint32_t DeviceEffectProxy::channelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003484 if (isOutput()) {
3485 return audio_channel_count_from_out_mask(channelMask());
3486 }
3487 return audio_channel_count_from_in_mask(channelMask());
3488}
3489
Andy Hung116bc262023-06-20 18:56:17 -07003490void DeviceEffectProxy::dump2(int fd, int spaces) const
Andy Hung920f6572022-10-06 12:09:49 -07003491NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3492{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003493 const Vector<String16> args;
3494 EffectBase::dump(fd, args);
3495
Andy Hung9a3f8182023-07-18 20:54:44 -07003496 const bool locked = afutils::dumpTryLock(mProxyLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003497 if (!locked) {
3498 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00003499 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003500 }
3501
3502 String8 outStr;
3503 if (mHalEffect != nullptr) {
3504 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3505 } else {
3506 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3507 }
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00003508 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003509 outStr.clear();
3510
3511 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00003512 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003513 outStr.clear();
3514
3515 for (const auto& iter : mEffectHandles) {
3516 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk90623d12023-08-15 20:59:35 +00003517 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003518 outStr.clear();
Andy Hung116bc262023-06-20 18:56:17 -07003519 sp<IAfEffectBase> effect = iter.second->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003520 if (effect != nullptr) {
3521 effect->dump(fd, args);
3522 }
3523 }
3524
3525 if (locked) {
3526 mLock.unlock();
3527 }
3528}
3529
3530#undef LOG_TAG
Andy Hung116bc262023-06-20 18:56:17 -07003531#define LOG_TAG "DeviceEffectProxy::ProxyCallback"
Eric Laurentb82e6b72019-11-22 17:25:04 -08003532
Andy Hung116bc262023-06-20 18:56:17 -07003533int DeviceEffectProxy::ProxyCallback::newEffectId() {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003534 return mManagerCallback->newEffectId();
3535}
3536
3537
Andy Hung116bc262023-06-20 18:56:17 -07003538bool DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3539 IAfEffectHandle *handle, bool unpinIfLast) {
3540 sp<IAfEffectBase> effectBase = handle->effect().promote();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003541 if (effectBase == nullptr) {
3542 return false;
3543 }
3544
Andy Hung116bc262023-06-20 18:56:17 -07003545 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003546 if (effect == nullptr) {
3547 return false;
3548 }
3549
3550 // restore suspended effects if the disconnected handle was enabled and the last one.
3551 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3552 if (remove) {
3553 sp<DeviceEffectProxy> proxy = mProxy.promote();
3554 if (proxy != nullptr) {
3555 proxy->removeEffect(effect);
3556 }
3557 if (handle->enabled()) {
3558 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3559 }
3560 }
3561 return true;
3562}
3563
Andy Hung116bc262023-06-20 18:56:17 -07003564status_t DeviceEffectProxy::ProxyCallback::createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -08003565 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3566 sp<EffectHalInterface> *effect) {
3567 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3568}
3569
Andy Hung116bc262023-06-20 18:56:17 -07003570status_t DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003571 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003572 sp<DeviceEffectProxy> proxy = mProxy.promote();
3573 if (proxy == nullptr) {
3574 return NO_INIT;
3575 }
3576 return proxy->addEffectToHal(effect);
3577}
3578
Andy Hung116bc262023-06-20 18:56:17 -07003579status_t DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003580 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003581 sp<DeviceEffectProxy> proxy = mProxy.promote();
3582 if (proxy == nullptr) {
3583 return NO_INIT;
3584 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003585 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003586}
3587
Andy Hung116bc262023-06-20 18:56:17 -07003588bool DeviceEffectProxy::ProxyCallback::isOutput() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003589 sp<DeviceEffectProxy> proxy = mProxy.promote();
3590 if (proxy == nullptr) {
3591 return true;
3592 }
3593 return proxy->isOutput();
3594}
3595
Andy Hung116bc262023-06-20 18:56:17 -07003596uint32_t DeviceEffectProxy::ProxyCallback::sampleRate() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003597 sp<DeviceEffectProxy> proxy = mProxy.promote();
3598 if (proxy == nullptr) {
3599 return DEFAULT_OUTPUT_SAMPLE_RATE;
3600 }
3601 return proxy->sampleRate();
3602}
3603
Andy Hung116bc262023-06-20 18:56:17 -07003604audio_channel_mask_t DeviceEffectProxy::ProxyCallback::inChannelMask(
Eric Laurentf1f22e72021-07-13 14:04:14 +02003605 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003606 sp<DeviceEffectProxy> proxy = mProxy.promote();
3607 if (proxy == nullptr) {
3608 return AUDIO_CHANNEL_OUT_STEREO;
3609 }
3610 return proxy->channelMask();
3611}
3612
Andy Hung116bc262023-06-20 18:56:17 -07003613uint32_t DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003614 sp<DeviceEffectProxy> proxy = mProxy.promote();
3615 if (proxy == nullptr) {
3616 return 2;
3617 }
3618 return proxy->channelCount();
3619}
3620
Andy Hung116bc262023-06-20 18:56:17 -07003621audio_channel_mask_t DeviceEffectProxy::ProxyCallback::outChannelMask() const {
Eric Laurentf1f22e72021-07-13 14:04:14 +02003622 sp<DeviceEffectProxy> proxy = mProxy.promote();
3623 if (proxy == nullptr) {
3624 return AUDIO_CHANNEL_OUT_STEREO;
3625 }
3626 return proxy->channelMask();
3627}
3628
Andy Hung116bc262023-06-20 18:56:17 -07003629uint32_t DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003630 sp<DeviceEffectProxy> proxy = mProxy.promote();
3631 if (proxy == nullptr) {
3632 return 2;
3633 }
3634 return proxy->channelCount();
3635}
3636
Andy Hung116bc262023-06-20 18:56:17 -07003637void DeviceEffectProxy::ProxyCallback::onEffectEnable(
3638 const sp<IAfEffectBase>& effectBase) {
3639 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003640 if (effect == nullptr) {
3641 return;
3642 }
3643 effect->start();
3644}
3645
Andy Hung116bc262023-06-20 18:56:17 -07003646void DeviceEffectProxy::ProxyCallback::onEffectDisable(
3647 const sp<IAfEffectBase>& effectBase) {
3648 sp<IAfEffectModule> effect = effectBase->asEffectModule();
Eric Laurent76c89f32021-12-03 17:13:23 +01003649 if (effect == nullptr) {
3650 return;
3651 }
3652 effect->stop();
3653}
3654
Glenn Kasten63238ef2015-03-02 15:50:29 -08003655} // namespace android