blob: 49046387bf022b9c53699560b82312c960454066 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080047
48// ----------------------------------------------------------------------------
49
50// Note: the following macro is used for extremely verbose logging message. In
51// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
52// 0; but one side effect of this is to turn all LOGV's as well. Some messages
53// are so verbose that we want to suppress them even when we have ALOG_ASSERT
54// turned on. Do not uncomment the #def below unless you really know what you
55// are doing and want to see all of the extremely verbose messages.
56//#define VERY_VERY_VERBOSE_LOGGING
57#ifdef VERY_VERY_VERBOSE_LOGGING
58#define ALOGVV ALOGV
59#else
60#define ALOGVV(a...) do { } while(0)
61#endif
62
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090063#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
64
Eric Laurentca7cc822012-11-19 14:55:58 -080065namespace android {
66
Andy Hung1131b6e2020-12-08 20:47:45 -080067using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070068using binder::Status;
69
70namespace {
71
72// Append a POD value into a vector of bytes.
73template<typename T>
74void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
75 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
76 buffer->insert(buffer->end(), ar, ar + sizeof(T));
77}
78
79// Write a POD value into a vector of bytes (clears the previous buffer
80// content).
81template<typename T>
82void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
83 buffer->clear();
84 appendToBuffer(value, buffer);
85}
86
87} // namespace
88
Eric Laurentca7cc822012-11-19 14:55:58 -080089// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080090// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
92
93#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080094#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080095
Eric Laurent41709552019-12-16 19:34:05 -080096AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080097 effect_descriptor_t *desc,
98 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 audio_session_t sessionId,
100 bool pinned)
101 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800102 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800103 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurentca7cc822012-11-19 14:55:58 -0800105}
106
Eric Laurent41709552019-12-16 19:34:05 -0800107// must be called with EffectModule::mLock held
108status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800109{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurent41709552019-12-16 19:34:05 -0800111 ALOGV("setEnabled %p enabled %d", this, enabled);
112
113 if (enabled != isEnabled()) {
114 switch (mState) {
115 // going from disabled to enabled
116 case IDLE:
117 mState = STARTING;
118 break;
119 case STOPPED:
120 mState = RESTART;
121 break;
122 case STOPPING:
123 mState = ACTIVE;
124 break;
125
126 // going from enabled to disabled
127 case RESTART:
128 mState = STOPPED;
129 break;
130 case STARTING:
131 mState = IDLE;
132 break;
133 case ACTIVE:
134 mState = STOPPING;
135 break;
136 case DESTROYED:
137 return NO_ERROR; // simply ignore as we are being destroyed
138 }
139 for (size_t i = 1; i < mHandles.size(); i++) {
140 EffectHandle *h = mHandles[i];
141 if (h != NULL && !h->disconnected()) {
142 h->setEnabled(enabled);
143 }
144 }
145 }
146 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800147}
148
Eric Laurent41709552019-12-16 19:34:05 -0800149status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
150{
151 status_t status;
152 {
153 Mutex::Autolock _l(mLock);
154 status = setEnabled_l(enabled);
155 }
156 if (fromHandle) {
157 if (enabled) {
158 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700159 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800160 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800162 }
163 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800165 }
166 }
167 return status;
168}
169
170bool AudioFlinger::EffectBase::isEnabled() const
171{
172 switch (mState) {
173 case RESTART:
174 case STARTING:
175 case ACTIVE:
176 return true;
177 case IDLE:
178 case STOPPING:
179 case STOPPED:
180 case DESTROYED:
181 default:
182 return false;
183 }
184}
185
186void AudioFlinger::EffectBase::setSuspended(bool suspended)
187{
188 Mutex::Autolock _l(mLock);
189 mSuspended = suspended;
190}
191
192bool AudioFlinger::EffectBase::suspended() const
193{
194 Mutex::Autolock _l(mLock);
195 return mSuspended;
196}
197
198status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800199{
200 status_t status;
201
202 Mutex::Autolock _l(mLock);
203 int priority = handle->priority();
204 size_t size = mHandles.size();
205 EffectHandle *controlHandle = NULL;
206 size_t i;
207 for (i = 0; i < size; i++) {
208 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800209 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 continue;
211 }
212 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 if (h->priority() <= priority) {
217 break;
218 }
219 }
220 // if inserted in first place, move effect control from previous owner to this handle
221 if (i == 0) {
222 bool enabled = false;
223 if (controlHandle != NULL) {
224 enabled = controlHandle->enabled();
225 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
226 }
227 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
228 status = NO_ERROR;
229 } else {
230 status = ALREADY_EXISTS;
231 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700232 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800233 mHandles.insertAt(handle, i);
234 return status;
235}
236
Eric Laurent41709552019-12-16 19:34:05 -0800237status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700238{
239 status_t status = NO_ERROR;
240 bool doRegister = false;
241 bool registered = false;
242 bool doEnable = false;
243 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700244 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800245 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700246
247 {
248 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200249
250 if ((isInternal_l() && !mPolicyRegistered)
251 || !getCallback()->isAudioPolicyReady()) {
252 return NO_ERROR;
253 }
254
Eric Laurent6c796322019-04-09 14:13:17 -0700255 // register effect when first handle is attached and unregister when last handle is removed
256 if (mPolicyRegistered != mHandles.size() > 0) {
257 doRegister = true;
258 mPolicyRegistered = mHandles.size() > 0;
259 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700260 const auto callback = getCallback();
261 io = callback->io();
262 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700263 }
264 }
265 // enable effect when registered according to enable state requested by controlling handle
266 if (mHandles.size() > 0) {
267 EffectHandle *handle = controlHandle_l();
268 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
269 doEnable = true;
270 mPolicyEnabled = handle->enabled();
271 }
272 }
273 registered = mPolicyRegistered;
274 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100275 // The simultaneous release of two EffectHandles with the same EffectModule
276 // may cause us to call this method at the same time.
277 // This may deadlock under some circumstances (b/180941720). Avoid this.
278 if (!doRegister && !(registered && doEnable)) {
279 return NO_ERROR;
280 }
Eric Laurent6c796322019-04-09 14:13:17 -0700281 }
fengjnlan46407562022-09-07 16:20:01 +0800282 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700283 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
284 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
285 if (doRegister) {
286 if (registered) {
287 status = AudioSystem::registerEffect(
288 &mDescriptor,
289 io,
290 strategy,
291 mSessionId,
292 mId);
293 } else {
294 status = AudioSystem::unregisterEffect(mId);
295 }
296 }
297 if (registered && doEnable) {
298 status = AudioSystem::setEffectEnabled(mId, enabled);
299 }
300 mPolicyLock.unlock();
301
302 return status;
303}
304
305
Eric Laurent41709552019-12-16 19:34:05 -0800306ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800307{
308 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309 return removeHandle_l(handle);
310}
311
Eric Laurent41709552019-12-16 19:34:05 -0800312ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800313{
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 size_t size = mHandles.size();
315 size_t i;
316 for (i = 0; i < size; i++) {
317 if (mHandles[i] == handle) {
318 break;
319 }
320 }
321 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800322 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
323 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800324 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800325 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800326
327 mHandles.removeAt(i);
328 // if removed from first place, move effect control from this handle to next in line
329 if (i == 0) {
330 EffectHandle *h = controlHandle_l();
331 if (h != NULL) {
332 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
333 }
334 }
335
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530336 // Prevent calls to process() and other functions on effect interface from now on.
337 // The effect engine will be released by the destructor when the last strong reference on
338 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 if (mHandles.size() == 0 && !mPinned) {
340 mState = DESTROYED;
341 }
342
343 return mHandles.size();
344}
345
346// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800347AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800348{
349 // the first valid handle in the list has control over the module
350 for (size_t i = 0; i < mHandles.size(); i++) {
351 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800352 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 return h;
354 }
355 }
356
357 return NULL;
358}
359
Eric Laurentf10c7092016-12-06 17:09:56 -0800360// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800361ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800362{
Andy Hungfda44002021-06-03 17:23:16 -0700363 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800364 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700365 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800366 return mHandles.size();
367 }
368
Eric Laurentf10c7092016-12-06 17:09:56 -0800369 Mutex::Autolock _l(mLock);
370 ssize_t numHandles = removeHandle_l(handle);
371 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800372 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700373 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800375 }
376 return numHandles;
377}
378
Eric Laurent41709552019-12-16 19:34:05 -0800379bool AudioFlinger::EffectBase::purgeHandles()
380{
381 bool enabled = false;
382 Mutex::Autolock _l(mLock);
383 EffectHandle *handle = controlHandle_l();
384 if (handle != NULL) {
385 enabled = handle->enabled();
386 }
387 mHandles.clear();
388 return enabled;
389}
390
391void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700392 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800393}
394
395static String8 effectFlagsToString(uint32_t flags) {
396 String8 s;
397
398 s.append("conn. mode: ");
399 switch (flags & EFFECT_FLAG_TYPE_MASK) {
400 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
401 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
402 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
403 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
404 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("insert pref: ");
410 switch (flags & EFFECT_FLAG_INSERT_MASK) {
411 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
412 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
413 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
414 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 s.append("volume mgmt: ");
420 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
421 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
422 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
423 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
424 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
425 default: s.append("unknown/reserved"); break;
426 }
427 s.append(", ");
428
429 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
430 if (devind) {
431 s.append("device indication: ");
432 switch (devind) {
433 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
434 default: s.append("unknown/reserved"); break;
435 }
436 s.append(", ");
437 }
438
439 s.append("input mode: ");
440 switch (flags & EFFECT_FLAG_INPUT_MASK) {
441 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
442 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
443 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
444 default: s.append("not set"); break;
445 }
446 s.append(", ");
447
448 s.append("output mode: ");
449 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
450 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
451 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
452 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
453 default: s.append("not set"); break;
454 }
455 s.append(", ");
456
457 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
458 if (accel) {
459 s.append("hardware acceleration: ");
460 switch (accel) {
461 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
462 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
469 if (modeind) {
470 s.append("mode indication: ");
471 switch (modeind) {
472 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
479 if (srcind) {
480 s.append("source indication: ");
481 switch (srcind) {
482 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
483 default: s.append("unknown/reserved"); break;
484 }
485 s.append(", ");
486 }
487
488 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
489 s.append("offloadable, ");
490 }
491
492 int len = s.length();
493 if (s.length() > 2) {
494 (void) s.lockBuffer(len);
495 s.unlockBuffer(len - 2);
496 }
497 return s;
498}
499
500void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
Andy Hung920f6572022-10-06 12:09:49 -0700501NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800502{
503 String8 result;
504
505 result.appendFormat("\tEffect ID %d:\n", mId);
506
507 bool locked = AudioFlinger::dumpTryLock(mLock);
508 // failed to lock - AudioFlinger is probably deadlocked
509 if (!locked) {
510 result.append("\t\tCould not lock Fx mutex:\n");
511 }
512
513 result.append("\t\tSession State Registered Enabled Suspended:\n");
514 result.appendFormat("\t\t%05d %03d %s %s %s\n",
515 mSessionId, mState, mPolicyRegistered ? "y" : "n",
516 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
517
518 result.append("\t\tDescriptor:\n");
519 char uuidStr[64];
520 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
521 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
522 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
523 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
524 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
525 mDescriptor.apiVersion,
526 mDescriptor.flags,
527 effectFlagsToString(mDescriptor.flags).string());
528 result.appendFormat("\t\t- name: %s\n",
529 mDescriptor.name);
530
531 result.appendFormat("\t\t- implementor: %s\n",
532 mDescriptor.implementor);
533
534 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
535 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
536 char buffer[256];
537 for (size_t i = 0; i < mHandles.size(); ++i) {
538 EffectHandle *handle = mHandles[i];
539 if (handle != NULL && !handle->disconnected()) {
540 handle->dumpToBuffer(buffer, sizeof(buffer));
541 result.append(buffer);
542 }
543 }
544 if (locked) {
545 mLock.unlock();
546 }
547
548 write(fd, result.string(), result.length());
549}
550
551// ----------------------------------------------------------------------------
552// EffectModule implementation
553// ----------------------------------------------------------------------------
554
555#undef LOG_TAG
556#define LOG_TAG "AudioFlinger::EffectModule"
557
558AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
559 effect_descriptor_t *desc,
560 int id,
561 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800562 bool pinned,
563 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800564 : EffectBase(callback, desc, id, sessionId, pinned),
565 // clear mConfig to ensure consistent initial value of buffer framecount
566 // in case buffers are associated by setInBuffer() or setOutBuffer()
567 // prior to configure().
568 mConfig{{}, {}},
569 mStatus(NO_INIT),
570 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
571 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800572 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000573 mAddedToHal(false),
574 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800575#ifdef FLOAT_EFFECT_CHAIN
576 , mSupportsFloat(false)
577#endif
578{
579 ALOGV("Constructor %p pinned %d", this, pinned);
580 int lStatus;
581
582 // create effect engine from effect factory
583 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800584 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800585 if (mStatus != NO_ERROR) {
586 return;
587 }
588 lStatus = init();
589 if (lStatus < 0) {
590 mStatus = lStatus;
591 goto Error;
592 }
593
594 setOffloaded(callback->isOffload(), callback->io());
595 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
596
597 return;
598Error:
599 mEffectInterface.clear();
600 ALOGV("Constructor Error %d", mStatus);
601}
602
603AudioFlinger::EffectModule::~EffectModule()
604{
605 ALOGV("Destructor %p", this);
606 if (mEffectInterface != 0) {
607 char uuidStr[64];
608 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
609 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
610 this, uuidStr);
611 release_l();
612 }
613
614}
615
Eric Laurentfa1e1232016-08-02 19:01:49 -0700616bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 Mutex::Autolock _l(mLock);
618
Eric Laurentfa1e1232016-08-02 19:01:49 -0700619 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800620 switch (mState) {
621 case RESTART:
622 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700623 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800624
625 case STARTING:
626 // clear auxiliary effect input buffer for next accumulation
627 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
628 memset(mConfig.inputCfg.buffer.raw,
629 0,
630 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
631 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 if (start_l() == NO_ERROR) {
633 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700634 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700635 } else {
636 mState = IDLE;
637 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800638 break;
639 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900640 // volume control for offload and direct threads must take effect immediately.
641 if (stop_l() == NO_ERROR
642 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700643 mDisableWaitCnt = mMaxDisableWaitCnt;
644 } else {
645 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
646 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 mState = STOPPED;
648 break;
649 case STOPPED:
650 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
651 // turn off sequence.
652 if (--mDisableWaitCnt == 0) {
653 reset_l();
654 mState = IDLE;
655 }
656 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200657 case ACTIVE:
658 for (size_t i = 0; i < mHandles.size(); i++) {
659 if (!mHandles[i]->disconnected()) {
660 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
661 }
662 }
663 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 default: //IDLE , ACTIVE, DESTROYED
665 break;
666 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700667
668 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800669}
670
671void AudioFlinger::EffectModule::process()
672{
673 Mutex::Autolock _l(mLock);
674
Mikhail Naganov022b9952017-01-04 16:36:51 -0800675 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 return;
677 }
678
rago94a1ee82017-07-21 15:11:02 -0700679 const uint32_t inChannelCount =
680 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
681 const uint32_t outChannelCount =
682 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
683 const bool auxType =
684 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
685
Andy Hungfa69ca32017-11-30 10:07:53 -0800686 // safeInputOutputSampleCount is 0 if the channel count between input and output
687 // buffers do not match. This prevents automatic accumulation or copying between the
688 // input and output effect buffers without an intermediary effect process.
689 // TODO: consider implementing channel conversion.
690 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700691 mInChannelCountRequested != mOutChannelCountRequested ? 0
692 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800693 mConfig.inputCfg.buffer.frameCount,
694 mConfig.outputCfg.buffer.frameCount);
695 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
696#ifdef FLOAT_EFFECT_CHAIN
697 accumulate_float(
698 mConfig.outputCfg.buffer.f32,
699 mConfig.inputCfg.buffer.f32,
700 safeInputOutputSampleCount);
701#else
702 accumulate_i16(
703 mConfig.outputCfg.buffer.s16,
704 mConfig.inputCfg.buffer.s16,
705 safeInputOutputSampleCount);
706#endif
707 };
708 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
709#ifdef FLOAT_EFFECT_CHAIN
710 memcpy(
711 mConfig.outputCfg.buffer.f32,
712 mConfig.inputCfg.buffer.f32,
713 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
714
715#else
716 memcpy(
717 mConfig.outputCfg.buffer.s16,
718 mConfig.inputCfg.buffer.s16,
719 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
720#endif
721 };
722
Eric Laurentca7cc822012-11-19 14:55:58 -0800723 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700724 int ret;
725 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700726 if (auxType) {
727 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800728 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700729#ifdef FLOAT_EFFECT_CHAIN
730 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800731#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700732 // Do in-place float conversion for auxiliary effect input buffer.
733 static_assert(sizeof(float) <= sizeof(int32_t),
734 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
735
Andy Hungfa69ca32017-11-30 10:07:53 -0800736 memcpy_to_float_from_q4_27(
737 mConfig.inputCfg.buffer.f32,
738 mConfig.inputCfg.buffer.s32,
739 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800740#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800741 } else
Andy Hung116a4982017-11-30 10:15:08 -0800742#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800743 {
Andy Hung116a4982017-11-30 10:15:08 -0800744#ifdef FLOAT_AUX
745 memcpy_to_i16_from_float(
746 mConfig.inputCfg.buffer.s16,
747 mConfig.inputCfg.buffer.f32,
748 mConfig.inputCfg.buffer.frameCount);
749#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 memcpy_to_i16_from_q4_27(
751 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700752 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800753 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800754#endif
rago94a1ee82017-07-21 15:11:02 -0700755 }
rago94a1ee82017-07-21 15:11:02 -0700756 }
757#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800758 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
759 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
760
761 if (!auxType && mInChannelCountRequested != inChannelCount) {
762 adjust_channels(
763 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
764 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
765 sizeof(float),
766 sizeof(float)
767 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
768 inBuffer = mInConversionBuffer;
769 }
770 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
771 && mOutChannelCountRequested != outChannelCount) {
772 adjust_selected_channels(
773 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
774 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
775 sizeof(float),
776 sizeof(float)
777 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
778 outBuffer = mOutConversionBuffer;
779 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
781 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800782 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800783 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
784 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700785 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 memcpy_to_i16_from_float(
787 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800790 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700791 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800793 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
795 goto data_bypass;
796 }
797 memcpy_to_i16_from_float(
798 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800799 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800800 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800801 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700802 }
803 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800804#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800805 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800806#ifdef FLOAT_EFFECT_CHAIN
807 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800808 sp<EffectBufferHalInterface> target =
809 mOutChannelCountRequested != outChannelCount
810 ? mOutConversionBuffer : mOutBuffer;
811
Andy Hungfa69ca32017-11-30 10:07:53 -0800812 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800813 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800814 mOutConversionBuffer->audioBuffer()->s16,
815 outChannelCount * mConfig.outputCfg.buffer.frameCount);
816 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800817 if (mOutChannelCountRequested != outChannelCount) {
818 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
819 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
820 sizeof(float),
821 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
822 }
rago94a1ee82017-07-21 15:11:02 -0700823#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700824 } else {
rago94a1ee82017-07-21 15:11:02 -0700825#ifdef FLOAT_EFFECT_CHAIN
826 data_bypass:
827#endif
828 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800829 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700830 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800831 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700832 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800833 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700834 }
835 }
836 ret = -ENODATA;
837 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800838
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 // force transition to IDLE state when engine is ready
840 if (mState == STOPPED && ret == -ENODATA) {
841 mDisableWaitCnt = 1;
842 }
843
844 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700845 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800846#ifdef FLOAT_AUX
847 const size_t size =
848 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
849#else
rago94a1ee82017-07-21 15:11:02 -0700850 const size_t size =
851 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800852#endif
rago94a1ee82017-07-21 15:11:02 -0700853 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 }
855 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700856 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800857 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
858 // If an insert effect is idle and input buffer is different from output buffer,
859 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700860 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700861 // similar handling with data_bypass above.
862 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
863 accumulateInputToOutput();
864 } else { // EFFECT_BUFFER_ACCESS_WRITE
865 copyInputToOutput();
866 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800867 }
868 }
869}
870
871void AudioFlinger::EffectModule::reset_l()
872{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700873 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800874 return;
875 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700876 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800877}
878
879status_t AudioFlinger::EffectModule::configure()
880{
rago94a1ee82017-07-21 15:11:02 -0700881 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700883 uint32_t size;
884 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700885 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700886
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700887 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700888 status = NO_INIT;
889 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 }
891
Eric Laurentca7cc822012-11-19 14:55:58 -0800892 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800893 // TODO: handle configuration of input (record) SW effects above the HAL,
894 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
895 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700896 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200897 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800898 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200899 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800900
901 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800902 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
903 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
904 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
905 mConfig.inputCfg.channels);
906 }
907#ifndef MULTICHANNEL_EFFECT_CHAIN
908 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
909 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
910 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
911 mConfig.outputCfg.channels);
912 }
913#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800914 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800915#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700916 // TODO: Update this logic when multichannel effects are implemented.
917 // For offloaded tracks consider mono output as stereo for proper effect initialization
918 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
919 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
920 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
921 ALOGV("Overriding effect input and output as STEREO");
922 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800923#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800924 }
jiabineb3bda02020-06-30 14:07:03 -0700925 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700926 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700927 mConfig.inputCfg.channels |= hapticChannelMask;
928 mConfig.outputCfg.channels |= hapticChannelMask;
929 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800930 mInChannelCountRequested =
931 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
932 mOutChannelCountRequested =
933 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700934
rago94a1ee82017-07-21 15:11:02 -0700935 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
936 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900937
938 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700939 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900940 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
941 ALOGV("Overriding effect input as 48kHz");
942 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700943 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900944 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800945 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
946 mConfig.inputCfg.bufferProvider.cookie = NULL;
947 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
948 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
949 mConfig.outputCfg.bufferProvider.cookie = NULL;
950 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
951 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
952 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
953 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800954 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800955 // always overwrites output buffer: input buffer == output buffer
956 // - in other sessions:
957 // last effect in the chain accumulates in output buffer: input buffer != output buffer
958 // other effect: overwrites output buffer: input buffer == output buffer
959 // Auxiliary effect:
960 // accumulates in output buffer: input buffer != output buffer
961 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700962 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800963 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
964 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700965 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800966 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000967 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800968
Eric Laurent6b446ce2019-12-13 10:56:31 -0800969 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700970 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800971 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800972
973 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700974 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700975 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800976 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700977 &mConfig,
978 &size,
979 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700980 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800981 status = cmdStatus;
982 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800983
984#ifdef MULTICHANNEL_EFFECT_CHAIN
985 if (status != NO_ERROR &&
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000986 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800987 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
988 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
989 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700990 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
991 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800992 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
993 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
994 }
995 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
996 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
997 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
998 }
999 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -07001000 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -08001001 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -07001002 &mConfig,
1003 &size,
1004 &cmdStatus);
1005 if (status == NO_ERROR) {
1006 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001007 }
1008 }
1009#endif
1010
1011#ifdef FLOAT_EFFECT_CHAIN
1012 if (status == NO_ERROR) {
1013 mSupportsFloat = true;
1014 }
1015
1016 if (status != NO_ERROR) {
1017 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1018 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1019 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1020 size = sizeof(int);
1021 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1022 sizeof(mConfig),
1023 &mConfig,
1024 &size,
1025 &cmdStatus);
1026 if (status == NO_ERROR) {
1027 status = cmdStatus;
1028 }
1029 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001030 mSupportsFloat = false;
1031 ALOGVV("config worked with 16 bit");
1032 } else {
1033 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 }
rago94a1ee82017-07-21 15:11:02 -07001035 }
1036#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001037
rago94a1ee82017-07-21 15:11:02 -07001038 if (status == NO_ERROR) {
1039 // Establish Buffer strategy
1040 setInBuffer(mInBuffer);
1041 setOutBuffer(mOutBuffer);
1042
1043 // Update visualizer latency
1044 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1045 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1046 effect_param_t *p = (effect_param_t *)buf32;
1047
1048 p->psize = sizeof(uint32_t);
1049 p->vsize = sizeof(uint32_t);
1050 size = sizeof(int);
1051 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1052
Andy Hungfda44002021-06-03 17:23:16 -07001053 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001054
1055 *((int32_t *)p->data + 1)= latency;
1056 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1057 sizeof(effect_param_t) + 8,
1058 &buf32,
1059 &size,
1060 &cmdStatus);
1061 }
jiabin4e246532022-08-23 16:37:30 -07001062
1063 if (isVolumeControl()) {
1064 // Force initializing the volume as 0 for volume control effect for safer ramping
1065 uint32_t left = 0;
1066 uint32_t right = 0;
1067 setVolumeInternal(&left, &right, true /*controller*/);
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 }
1070
Andy Hung05083ac2017-12-14 15:00:28 -08001071 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1072 mMaxDisableWaitCnt = (uint32_t)std::max(
1073 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1074 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1075 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001076
Eric Laurentd0ebb532013-04-02 16:41:41 -07001077exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001078 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001079 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001080 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 return status;
1082}
1083
1084status_t AudioFlinger::EffectModule::init()
1085{
1086 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001087 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001088 return NO_INIT;
1089 }
1090 status_t cmdStatus;
1091 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001092 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1093 0,
1094 NULL,
1095 &size,
1096 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 if (status == 0) {
1098 status = cmdStatus;
1099 }
1100 return status;
1101}
1102
Eric Laurent1b928682014-10-02 19:41:47 -07001103void AudioFlinger::EffectModule::addEffectToHal_l()
1104{
1105 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1106 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001107 if (mAddedToHal) {
1108 return;
1109 }
1110
Andy Hungfda44002021-06-03 17:23:16 -07001111 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001112 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001113 }
1114}
1115
Eric Laurentfa1e1232016-08-02 19:01:49 -07001116// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001117status_t AudioFlinger::EffectModule::start()
1118{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001119 status_t status;
1120 {
1121 Mutex::Autolock _l(mLock);
1122 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001123 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001124 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001125 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001126 }
1127 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001128}
1129
1130status_t AudioFlinger::EffectModule::start_l()
1131{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001132 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 return NO_INIT;
1134 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001135 if (mStatus != NO_ERROR) {
1136 return mStatus;
1137 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 status_t cmdStatus;
1139 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001140 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1141 0,
1142 NULL,
1143 &size,
1144 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 if (status == 0) {
1146 status = cmdStatus;
1147 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001148 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001149 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001150 }
1151 return status;
1152}
1153
1154status_t AudioFlinger::EffectModule::stop()
1155{
1156 Mutex::Autolock _l(mLock);
1157 return stop_l();
1158}
1159
1160status_t AudioFlinger::EffectModule::stop_l()
1161{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001162 if (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 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001168 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001169 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001170
1171 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001172 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1173 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1174 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001175 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001176 mSetVolumeReentrantTid = INVALID_PID;
1177 }
1178
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001179 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1180 0,
1181 NULL,
1182 &size,
1183 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001184 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 status = cmdStatus;
1186 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001187 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001188 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001189 }
1190 return status;
1191}
1192
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001193// must be called with EffectChain::mLock held
1194void AudioFlinger::EffectModule::release_l()
1195{
1196 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001197 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001198 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001199 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001200 mEffectInterface.clear();
1201 }
1202}
1203
Eric Laurent6b446ce2019-12-13 10:56:31 -08001204status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001205{
1206 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1207 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001208 if (!mAddedToHal) {
1209 return NO_ERROR;
1210 }
1211
Andy Hungfda44002021-06-03 17:23:16 -07001212 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001213 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001214 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001215 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001216}
1217
Andy Hunge4a1d912016-08-17 14:11:13 -07001218// round up delta valid if value and divisor are positive.
1219template <typename T>
1220static T roundUpDelta(const T &value, const T &divisor) {
1221 T remainder = value % divisor;
1222 return remainder == 0 ? 0 : divisor - remainder;
1223}
1224
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001225status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1226 const std::vector<uint8_t>& cmdData,
1227 int32_t maxReplySize,
1228 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001229{
1230 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001231 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001232
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001233 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001234 return NO_INIT;
1235 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001236 if (mStatus != NO_ERROR) {
1237 return mStatus;
1238 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1240 return -EINVAL;
1241 }
1242 size_t cmdSize = cmdData.size();
1243 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1244 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1245 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001246 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001247 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001248 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001249 android_errorWriteLog(0x534e4554, "33003822");
1250 return -EINVAL;
1251 }
1252 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001253 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001255 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001256 return -EINVAL;
1257 }
ragoe2759072016-11-22 18:02:48 -08001258 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001259 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001260 || param->psize > maxReplySize - sizeof(effect_param_t)
1261 || param->vsize > maxReplySize - sizeof(effect_param_t)
1262 - param->psize
1263 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1264 maxReplySize
1265 - sizeof(effect_param_t)
1266 - param->psize
1267 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001268 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1269 android_errorWriteLog(0x534e4554, "32705438");
1270 return -EINVAL;
1271 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001272 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001273 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1274 && // DEFERRED not generally used
1275 (param == nullptr
1276 || param->psize > cmdSize - sizeof(effect_param_t)
1277 || param->vsize > cmdSize - sizeof(effect_param_t)
1278 - param->psize
1279 || roundUpDelta(param->psize,
1280 (uint32_t) sizeof(int)) >
1281 cmdSize
1282 - sizeof(effect_param_t)
1283 - param->psize
1284 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001285 android_errorWriteLog(0x534e4554, "30204301");
1286 return -EINVAL;
1287 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001288 uint32_t replySize = maxReplySize;
1289 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001290 status_t status = mEffectInterface->command(cmdCode,
1291 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001292 const_cast<uint8_t*>(cmdData.data()),
1293 &replySize,
1294 reply->data());
1295 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001297 for (size_t i = 1; i < mHandles.size(); i++) {
1298 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001299 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001300 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001301 }
1302 }
1303 }
1304 return status;
1305}
1306
Eric Laurentca7cc822012-11-19 14:55:58 -08001307bool AudioFlinger::EffectModule::isProcessEnabled() const
1308{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001309 if (mStatus != NO_ERROR) {
1310 return false;
1311 }
1312
Eric Laurentca7cc822012-11-19 14:55:58 -08001313 switch (mState) {
1314 case RESTART:
1315 case ACTIVE:
1316 case STOPPING:
1317 case STOPPED:
1318 return true;
1319 case IDLE:
1320 case STARTING:
1321 case DESTROYED:
1322 default:
1323 return false;
1324 }
1325}
1326
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001327bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1328{
Andy Hungfda44002021-06-03 17:23:16 -07001329 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001330}
1331
1332bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1333{
1334 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1335}
1336
Mikhail Naganov022b9952017-01-04 16:36:51 -08001337void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001338 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001339
1340 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001341 if (buffer != 0) {
1342 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1343 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1344 } else {
1345 mConfig.inputCfg.buffer.raw = NULL;
1346 }
1347 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001348 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001349
1350#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001351 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001352 // Theoretically insert effects can also do in-place conversions (destroying
1353 // the original buffer) when the output buffer is identical to the input buffer,
1354 // but we don't optimize for it here.
1355 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001356 const uint32_t inChannelCount =
1357 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1358 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001359 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001360 // we need to translate - create hidl shared buffer and intercept
1361 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001362 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1363 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1364 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001365
1366 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1367 __func__, inChannels, inFrameCount, size);
1368
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001369 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001370 || size > mInConversionBuffer->getSize())) {
1371 mInConversionBuffer.clear();
1372 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001373 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001374 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001375 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001376 mInConversionBuffer->setFrameCount(inFrameCount);
1377 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001378 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001379 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001380 }
1381 }
1382#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001383}
1384
1385void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001386 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001387
1388 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001389 if (buffer != 0) {
1390 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1391 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1392 } else {
1393 mConfig.outputCfg.buffer.raw = NULL;
1394 }
1395 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001396 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001397
1398#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001399 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001400 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001401 const uint32_t outChannelCount =
1402 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1403 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001404 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001405 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001406 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1407 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1408 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001409
1410 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1411 __func__, outChannels, outFrameCount, size);
1412
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001413 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001414 || size > mOutConversionBuffer->getSize())) {
1415 mOutConversionBuffer.clear();
1416 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001417 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001418 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001419 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001420 mOutConversionBuffer->setFrameCount(outFrameCount);
1421 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001422 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001423 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001424 }
1425 }
1426#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001427}
1428
Eric Laurentca7cc822012-11-19 14:55:58 -08001429status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1430{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001431 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001432 if (mStatus != NO_ERROR) {
1433 return mStatus;
1434 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1437 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1438 if (isProcessEnabled() &&
1439 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001440 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1441 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin4e246532022-08-23 16:37:30 -07001442 status = setVolumeInternal(left, right, controller);
1443 }
1444 return status;
1445}
1446
1447status_t AudioFlinger::EffectModule::setVolumeInternal(
1448 uint32_t *left, uint32_t *right, bool controller) {
1449 uint32_t volume[2] = {*left, *right};
1450 uint32_t *pVolume = controller ? volume : nullptr;
1451 uint32_t size = sizeof(volume);
1452 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1453 size,
1454 volume,
1455 &size,
1456 pVolume);
1457 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1458 *left = volume[0];
1459 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 }
1461 return status;
1462}
1463
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001464void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1465{
Zhou Songd505c642020-02-20 16:35:37 +08001466 // for offload or direct thread, if the effect chain has non-offloadable
1467 // effect and any effect module within the chain has volume control, then
1468 // volume control is delegated to effect, otherwise, set volume to hal.
1469 if (mEffectCallback->isOffloadOrDirect() &&
1470 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001471 float vol_l = (float)left / (1 << 24);
1472 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001473 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001474 }
1475}
1476
jiabin8f278ee2019-11-11 12:16:27 -08001477status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1478 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001479{
jiabin8f278ee2019-11-11 12:16:27 -08001480 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1481 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 return NO_ERROR;
1483 }
1484
1485 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001486 if (mStatus != NO_ERROR) {
1487 return mStatus;
1488 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001489 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001490 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 status_t cmdStatus;
1492 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001493 // FIXME: use audio device types and addresses when the hal interface is ready.
1494 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001495 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001496 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001497 &size,
1498 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001499 }
1500 return status;
1501}
1502
jiabin8f278ee2019-11-11 12:16:27 -08001503status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1504{
1505 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1506}
1507
1508status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1509{
1510 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1511}
1512
Eric Laurentca7cc822012-11-19 14:55:58 -08001513status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1514{
1515 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001516 if (mStatus != NO_ERROR) {
1517 return mStatus;
1518 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001519 status_t status = NO_ERROR;
1520 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1521 status_t cmdStatus;
1522 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001523 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1524 sizeof(audio_mode_t),
1525 &mode,
1526 &size,
1527 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 if (status == NO_ERROR) {
1529 status = cmdStatus;
1530 }
1531 }
1532 return status;
1533}
1534
1535status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1536{
1537 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001538 if (mStatus != NO_ERROR) {
1539 return mStatus;
1540 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001541 status_t status = NO_ERROR;
1542 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1543 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001544 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1545 sizeof(audio_source_t),
1546 &source,
1547 &size,
1548 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001549 }
1550 return status;
1551}
1552
Eric Laurent5baf2af2013-09-12 17:37:00 -07001553status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1554{
1555 Mutex::Autolock _l(mLock);
1556 if (mStatus != NO_ERROR) {
1557 return mStatus;
1558 }
1559 status_t status = NO_ERROR;
1560 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1561 status_t cmdStatus;
1562 uint32_t size = sizeof(status_t);
1563 effect_offload_param_t cmd;
1564
1565 cmd.isOffload = offloaded;
1566 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001567 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1568 sizeof(effect_offload_param_t),
1569 &cmd,
1570 &size,
1571 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001572 if (status == NO_ERROR) {
1573 status = cmdStatus;
1574 }
1575 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1576 } else {
1577 if (offloaded) {
1578 status = INVALID_OPERATION;
1579 }
1580 mOffloaded = false;
1581 }
1582 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1583 return status;
1584}
1585
1586bool AudioFlinger::EffectModule::isOffloaded() const
1587{
1588 Mutex::Autolock _l(mLock);
1589 return mOffloaded;
1590}
1591
jiabineb3bda02020-06-30 14:07:03 -07001592/*static*/
1593bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1594 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1595}
1596
1597bool AudioFlinger::EffectModule::isHapticGenerator() const {
1598 return isHapticGenerator(&mDescriptor.type);
1599}
1600
Simon Bowden62823412022-10-17 14:52:26 +00001601status_t AudioFlinger::EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001602{
1603 if (mStatus != NO_ERROR) {
1604 return mStatus;
1605 }
1606 if (!isHapticGenerator()) {
1607 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1608 return INVALID_OPERATION;
1609 }
1610
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001611 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1612 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001613 param->psize = sizeof(int32_t);
1614 param->vsize = sizeof(int32_t) * 2;
1615 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1616 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001617 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001618 std::vector<uint8_t> response;
1619 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001620 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001621 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1622 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001623 }
1624 return status;
1625}
1626
Lais Andradebc3f37a2021-07-02 00:13:19 +01001627status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001628{
1629 if (mStatus != NO_ERROR) {
1630 return mStatus;
1631 }
1632 if (!isHapticGenerator()) {
1633 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1634 return INVALID_OPERATION;
1635 }
1636
Lais Andradebc3f37a2021-07-02 00:13:19 +01001637 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001638 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001639 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001640 effect_param_t *param = (effect_param_t*) request.data();
1641 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001642 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001643 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1644 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001645 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1646 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1647 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001648 std::vector<uint8_t> response;
1649 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1650 if (status == NO_ERROR) {
1651 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1652 status = *reinterpret_cast<const status_t*>(response.data());
1653 }
1654 return status;
1655}
1656
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001657status_t AudioFlinger::EffectModule::getConfigs(
1658 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1659 Mutex::Autolock _l(mLock);
1660 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1661 return NO_INIT;
1662 }
1663 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1664 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1665 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1666 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1667 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1668 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1669 *isOutput = mIsOutput;
1670 return NO_ERROR;
1671}
1672
Andy Hungbded9c82017-11-30 18:47:35 -08001673static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1674 std::stringstream ss;
1675
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001676 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001677 return "nullptr"; // make different than below
1678 } else if (buffer->externalData() != nullptr) {
1679 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1680 << " -> "
1681 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1682 } else {
1683 ss << buffer->audioBuffer()->raw;
1684 }
1685 return ss.str();
1686}
Marco Nelissenb2208842014-02-07 14:00:50 -08001687
Eric Laurent41709552019-12-16 19:34:05 -08001688void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07001689NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001690{
Eric Laurent41709552019-12-16 19:34:05 -08001691 EffectBase::dump(fd, args);
1692
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001694 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001695
Eric Laurent41709552019-12-16 19:34:05 -08001696 result.append("\t\tStatus Engine:\n");
1697 result.appendFormat("\t\t%03d %p\n",
1698 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001699
1700 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001701
1702 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001703 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1704 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1705 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 mConfig.inputCfg.buffer.frameCount,
1707 mConfig.inputCfg.samplingRate,
1708 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001709 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001710 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001711
1712 result.append("\t\t- Output configuration:\n");
1713 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001714 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001715 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 mConfig.outputCfg.buffer.frameCount,
1717 mConfig.outputCfg.samplingRate,
1718 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001719 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001720 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001721
rago94a1ee82017-07-21 15:11:02 -07001722#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001723
Andy Hungbded9c82017-11-30 18:47:35 -08001724 result.appendFormat("\t\t- HAL buffers:\n"
1725 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1726 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1727 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1728 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1729 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001730#endif
1731
Eric Laurentca7cc822012-11-19 14:55:58 -08001732 write(fd, result.string(), result.length());
1733
Mikhail Naganov4d547672019-02-22 14:19:19 -08001734 if (mEffectInterface != 0) {
1735 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1736 (void)mEffectInterface->dump(fd);
1737 }
1738
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 if (locked) {
1740 mLock.unlock();
1741 }
1742}
1743
1744// ----------------------------------------------------------------------------
1745// EffectHandle implementation
1746// ----------------------------------------------------------------------------
1747
1748#undef LOG_TAG
1749#define LOG_TAG "AudioFlinger::EffectHandle"
1750
Eric Laurent41709552019-12-16 19:34:05 -08001751AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001752 const sp<AudioFlinger::Client>& client,
1753 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001754 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001755 : BnEffect(),
1756 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001757 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1758 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001759{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001760 ALOGV("constructor %p client %p", this, client.get());
Andy Hung225aef62022-12-06 16:33:20 -08001761 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001762
1763 if (client == 0) {
1764 return;
1765 }
1766 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001767 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1768 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1769 std::string("Effect ID: ")
1770 .append(std::to_string(effect->id()))
1771 .append(" Session ID: ")
1772 .append(std::to_string(static_cast<int>(effect->sessionId())))
1773 .append(" \n")
1774 });
Glenn Kastene75da402013-11-20 13:54:52 -08001775 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001776 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001777 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001778 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001779 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 return;
1781 }
Glenn Kastene75da402013-11-20 13:54:52 -08001782 new(mCblk) effect_param_cblk_t();
1783 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001784}
1785
1786AudioFlinger::EffectHandle::~EffectHandle()
1787{
1788 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 disconnect(false);
1790}
1791
Andy Hungc747c532022-03-07 21:41:14 -08001792// Creates an association between Binder code to name for IEffect.
1793#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1794BINDER_METHOD_ENTRY(enable) \
1795BINDER_METHOD_ENTRY(disable) \
1796BINDER_METHOD_ENTRY(command) \
1797BINDER_METHOD_ENTRY(disconnect) \
1798BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001799BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001800
1801// singleton for Binder Method Statistics for IEffect
1802mediautils::MethodStatistics<int>& getIEffectStatistics() {
1803 using Code = int;
1804
1805#pragma push_macro("BINDER_METHOD_ENTRY")
1806#undef BINDER_METHOD_ENTRY
1807#define BINDER_METHOD_ENTRY(ENTRY) \
1808 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1809
1810 static mediautils::MethodStatistics<Code> methodStatistics{
1811 IEFFECT_BINDER_METHOD_MACRO_LIST
1812 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1813 };
1814#pragma pop_macro("BINDER_METHOD_ENTRY")
1815
1816 return methodStatistics;
1817}
1818
1819status_t AudioFlinger::EffectHandle::onTransact(
1820 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001821 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1822 mediautils::TimeCheck check(
1823 std::string("IEffect::").append(methodName),
1824 [code](bool timeout, float elapsedMs) {
1825 if (timeout) {
1826 ; // we don't timeout right now on the effect interface.
1827 } else {
1828 getIEffectStatistics().event(code, elapsedMs);
1829 }
Andy Hungf8ab0932022-06-13 19:49:43 -07001830 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001831 return BnEffect::onTransact(code, data, reply, flags);
1832}
1833
Glenn Kastene75da402013-11-20 13:54:52 -08001834status_t AudioFlinger::EffectHandle::initCheck()
1835{
1836 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1837}
1838
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001839#define RETURN(code) \
1840 *_aidl_return = (code); \
1841 return Status::ok();
1842
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001843#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1844 ({ \
1845 auto _tmp = (exp); \
1846 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1847 std::move(_tmp.value()); \
1848 })
1849
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001850Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001851{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001854 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001859 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001860 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001861
1862 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001863 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 }
1865
1866 mEnabled = true;
1867
Eric Laurent6c796322019-04-09 14:13:17 -07001868 status_t status = effect->updatePolicyState();
1869 if (status != NO_ERROR) {
1870 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001872 }
1873
Eric Laurent6b446ce2019-12-13 10:56:31 -08001874 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001875
1876 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001877 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001878 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 }
1880
Eric Laurent6b446ce2019-12-13 10:56:31 -08001881 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001882 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001883 mEnabled = false;
1884 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001885 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001886}
1887
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001888Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001889{
1890 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001891 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001892 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001893 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001894 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001895 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001898 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001899
1900 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001902 }
1903 mEnabled = false;
1904
Eric Laurent6c796322019-04-09 14:13:17 -07001905 effect->updatePolicyState();
1906
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001907 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001909 }
1910
Eric Laurent6b446ce2019-12-13 10:56:31 -08001911 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001913}
1914
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001915Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001916{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001918 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001919 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001920}
1921
1922void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1923{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001924 AutoMutex _l(mLock);
1925 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1926 if (mDisconnected) {
1927 if (unpinIfLast) {
1928 android_errorWriteLog(0x534e4554, "32707507");
1929 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001930 return;
1931 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001932 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001933 {
Eric Laurent41709552019-12-16 19:34:05 -08001934 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001935 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001936 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001937 ALOGW("%s Effect handle %p disconnected after thread destruction",
1938 __func__, this);
1939 }
1940 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001941 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001942 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001943
Eric Laurentca7cc822012-11-19 14:55:58 -08001944 if (mClient != 0) {
1945 if (mCblk != NULL) {
1946 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1947 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1948 }
1949 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001950 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung920f6572022-10-06 12:09:49 -07001951 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001952 mClient.clear();
1953 }
1954}
1955
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001956Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1957 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1958 return Status::ok();
1959}
1960
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001961Status AudioFlinger::EffectHandle::getConfig(
1962 media::EffectConfig* _config, int32_t* _aidl_return) {
1963 AutoMutex _l(mLock);
1964 sp<EffectBase> effect = mEffect.promote();
1965 if (effect == nullptr || mDisconnected) {
1966 RETURN(DEAD_OBJECT);
1967 }
1968 sp<EffectModule> effectModule = effect->asEffectModule();
1969 if (effectModule == nullptr) {
1970 RETURN(INVALID_OPERATION);
1971 }
1972 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1973 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1974 bool isOutput;
1975 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1976 if (status == NO_ERROR) {
1977 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1978 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1979 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1980 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1981 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1982 _config->isOnInputStream = !isOutput;
1983 }
1984 RETURN(status);
1985}
1986
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001987Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1988 const std::vector<uint8_t>& cmdData,
1989 int32_t maxResponseSize,
1990 std::vector<uint8_t>* response,
1991 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001992{
1993 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001994 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001995
Eric Laurentc7ab3092017-06-15 18:43:46 -07001996 // reject commands reserved for internal use by audio framework if coming from outside
1997 // of audioserver
1998 switch(cmdCode) {
1999 case EFFECT_CMD_ENABLE:
2000 case EFFECT_CMD_DISABLE:
2001 case EFFECT_CMD_SET_PARAM:
2002 case EFFECT_CMD_SET_PARAM_DEFERRED:
2003 case EFFECT_CMD_SET_PARAM_COMMIT:
2004 case EFFECT_CMD_GET_PARAM:
2005 break;
2006 default:
2007 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2008 break;
2009 }
2010 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002011 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002012 }
2013
Eric Laurent1ffc5852016-12-15 14:46:09 -08002014 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002015 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002016 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002018 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 writeToBuffer(NO_ERROR, response);
2020 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002021 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002022 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002023 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002024 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002025 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002026 writeToBuffer(NO_ERROR, response);
2027 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002028 }
2029
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002030 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002031 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002032 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002033 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002034 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 // only get parameter command is permitted for applications not controlling the effect
2036 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002037 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002038 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002039
2040 // handle commands that are not forwarded transparently to effect engine
2041 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002042 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002044 }
2045
Andy Hung920f6572022-10-06 12:09:49 -07002046 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002047 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002048 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002049 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002050 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002051
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 // No need to trylock() here as this function is executed in the binder thread serving a
2053 // particular client process: no risk to block the whole media server process or mixer
2054 // threads if we are stuck here
Andy Hung920f6572022-10-06 12:09:49 -07002055 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002056 // keep local copy of index in case of client corruption b/32220769
2057 const uint32_t clientIndex = mCblk->clientIndex;
2058 const uint32_t serverIndex = mCblk->serverIndex;
2059 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2060 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 mCblk->serverIndex = 0;
2062 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002063 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002064 }
2065 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002066 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002067 for (uint32_t index = serverIndex; index < clientIndex;) {
2068 int *p = (int *)(mBuffer + index);
2069 const int size = *p++;
2070 if (size < 0
2071 || size > EFFECT_PARAM_BUFFER_SIZE
2072 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002074 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002075 break;
2076 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002077
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002078 std::copy(reinterpret_cast<const uint8_t*>(p),
2079 reinterpret_cast<const uint8_t*>(p) + size,
2080 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002081
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002082 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002083 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002084 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002085 sizeof(int),
2086 &replyBuffer);
2087 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002088
2089 // verify shared memory: server index shouldn't change; client index can't go back.
2090 if (serverIndex != mCblk->serverIndex
2091 || clientIndex > mCblk->clientIndex) {
2092 android_errorWriteLog(0x534e4554, "32220769");
2093 status = BAD_VALUE;
2094 break;
2095 }
2096
Eric Laurentca7cc822012-11-19 14:55:58 -08002097 // stop at first error encountered
2098 if (ret != NO_ERROR) {
2099 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002100 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 break;
2102 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002103 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 break;
2105 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002106 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002107 }
2108 mCblk->serverIndex = 0;
2109 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002110 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002111 }
2112
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002113 status_t status = effect->command(cmdCode,
2114 cmdData,
2115 maxResponseSize,
2116 response);
2117 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002118}
2119
2120void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2121{
2122 ALOGV("setControl %p control %d", this, hasControl);
2123
2124 mHasControl = hasControl;
2125 mEnabled = enabled;
2126
2127 if (signal && mEffectClient != 0) {
2128 mEffectClient->controlStatusChanged(hasControl);
2129 }
2130}
2131
2132void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002133 const std::vector<uint8_t>& cmdData,
2134 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002135{
2136 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002137 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002138 }
2139}
2140
2141
2142
2143void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2144{
2145 if (mEffectClient != 0) {
2146 mEffectClient->enableStatusChanged(enabled);
2147 }
2148}
2149
Eric Laurentde8caf42021-08-11 17:19:25 +02002150void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2151{
2152 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2153 mEffectClient->framesProcessed(frames);
2154 }
2155}
2156
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002157void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung920f6572022-10-06 12:09:49 -07002158NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002159{
2160 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2161
Marco Nelissenb2208842014-02-07 14:00:50 -08002162 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002163 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002164 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002165 mHasControl ? "yes" : "no",
2166 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002167 mCblk ? mCblk->clientIndex : 0,
2168 mCblk ? mCblk->serverIndex : 0
2169 );
2170
2171 if (locked) {
2172 mCblk->lock.unlock();
2173 }
2174}
2175
2176#undef LOG_TAG
2177#define LOG_TAG "AudioFlinger::EffectChain"
2178
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002179AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2180 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002181 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002182 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002184 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002185{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002186 sp<ThreadBase> p = thread.promote();
2187 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002188 return;
2189 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002190 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002191 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2192 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002193}
2194
2195AudioFlinger::EffectChain::~EffectChain()
2196{
Eric Laurentca7cc822012-11-19 14:55:58 -08002197}
2198
2199// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2200sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2201 effect_descriptor_t *descriptor)
2202{
2203 size_t size = mEffects.size();
2204
2205 for (size_t i = 0; i < size; i++) {
2206 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2207 return mEffects[i];
2208 }
2209 }
2210 return 0;
2211}
2212
2213// getEffectFromId_l() must be called with ThreadBase::mLock held
2214sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2215{
2216 size_t size = mEffects.size();
2217
2218 for (size_t i = 0; i < size; i++) {
2219 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2220 if (id == 0 || mEffects[i]->id() == id) {
2221 return mEffects[i];
2222 }
2223 }
2224 return 0;
2225}
2226
2227// getEffectFromType_l() must be called with ThreadBase::mLock held
2228sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2229 const effect_uuid_t *type)
2230{
2231 size_t size = mEffects.size();
2232
2233 for (size_t i = 0; i < size; i++) {
2234 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2235 return mEffects[i];
2236 }
2237 }
2238 return 0;
2239}
2240
Eric Laurent6c796322019-04-09 14:13:17 -07002241std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2242{
2243 std::vector<int> ids;
2244 Mutex::Autolock _l(mLock);
2245 for (size_t i = 0; i < mEffects.size(); i++) {
2246 ids.push_back(mEffects[i]->id());
2247 }
2248 return ids;
2249}
2250
Eric Laurentca7cc822012-11-19 14:55:58 -08002251void AudioFlinger::EffectChain::clearInputBuffer()
2252{
2253 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002254 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002255}
2256
2257// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002258void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002259{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002260 if (mInBuffer == NULL) {
2261 return;
2262 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002263 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2264 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002265
Eric Laurent6b446ce2019-12-13 10:56:31 -08002266 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002267 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002268}
2269
2270// Must be called with EffectChain::mLock locked
2271void AudioFlinger::EffectChain::process_l()
2272{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002273 // never process effects when:
2274 // - on an OFFLOAD thread
2275 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002276 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002277 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002278 bool tracksOnSession = (trackCnt() != 0);
2279
2280 if (!tracksOnSession && mTailBufferCount == 0) {
2281 doProcess = false;
2282 }
2283
2284 if (activeTrackCnt() == 0) {
2285 // if no track is active and the effect tail has not been rendered,
2286 // the input buffer must be cleared here as the mixer process will not do it
2287 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002288 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002289 if (mTailBufferCount > 0) {
2290 mTailBufferCount--;
2291 }
2292 }
2293 }
2294 }
2295
2296 size_t size = mEffects.size();
2297 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002298 // Only the input and output buffers of the chain can be external,
2299 // and 'update' / 'commit' do nothing for allocated buffers, thus
2300 // it's not needed to consider any other buffers here.
2301 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002302 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2303 mOutBuffer->update();
2304 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 for (size_t i = 0; i < size; i++) {
2306 mEffects[i]->process();
2307 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002308 mInBuffer->commit();
2309 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2310 mOutBuffer->commit();
2311 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002313 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002314 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002315 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2316 }
2317 if (doResetVolume) {
2318 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002319 }
2320}
2321
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002322// createEffect_l() must be called with ThreadBase::mLock held
2323status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002324 effect_descriptor_t *desc,
2325 int id,
2326 audio_session_t sessionId,
2327 bool pinned)
2328{
2329 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002330 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002331 status_t lStatus = effect->status();
2332 if (lStatus == NO_ERROR) {
2333 lStatus = addEffect_ll(effect);
2334 }
2335 if (lStatus != NO_ERROR) {
2336 effect.clear();
2337 }
2338 return lStatus;
2339}
2340
2341// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002342status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2343{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002344 Mutex::Autolock _l(mLock);
2345 return addEffect_ll(effect);
2346}
2347// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2348status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2349{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002350 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002351
Eric Laurentb62d0362021-10-26 17:40:18 +02002352 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002353 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2354 // Auxiliary effects are inserted at the beginning of mEffects vector as
2355 // they are processed first and accumulated in chain input buffer
2356 mEffects.insertAt(effect, 0);
2357
2358 // the input buffer for auxiliary effect contains mono samples in
2359 // 32 bit format. This is to avoid saturation in AudoMixer
2360 // accumulation stage. Saturation is done in EffectModule::process() before
2361 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002362 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002363 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002364#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002365 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002366 numSamples * sizeof(float), &halBuffer);
2367#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002368 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002369 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002370#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002371 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002372
2373 effect->configure();
2374
Mikhail Naganov022b9952017-01-04 16:36:51 -08002375 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002376 // auxiliary effects output samples to chain input buffer for further processing
2377 // by insert effects
2378 effect->setOutBuffer(mInBuffer);
2379 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002380 ssize_t idx_insert = getInsertIndex(desc);
2381 if (idx_insert < 0) {
2382 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002383 }
2384
Eric Laurentb62d0362021-10-26 17:40:18 +02002385 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002386 mEffects.insertAt(effect, idx_insert);
2387
2388 effect->configure();
2389
Eric Laurentb62d0362021-10-26 17:40:18 +02002390 // - By default:
2391 // All effects read samples from chain input buffer.
2392 // The last effect in the chain, writes samples to chain output buffer,
2393 // otherwise to chain input buffer
2394 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2395 // The spatializer effect (first effect) reads samples from the input buffer
2396 // and writes samples to the output buffer.
2397 // All other effects read and writes samples to the output buffer
2398 if (mEffectCallback->isSpatializer()
2399 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002400 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002401 if (idx_insert == 0) {
2402 if (previousSize != 0) {
2403 mEffects[1]->configure();
2404 mEffects[1]->setInBuffer(mOutBuffer);
2405 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2406 }
2407 effect->setInBuffer(mInBuffer);
2408 } else {
2409 effect->setInBuffer(mOutBuffer);
2410 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002411 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002412 effect->setInBuffer(mInBuffer);
Andy Hung920f6572022-10-06 12:09:49 -07002413 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002414 if (idx_insert != 0) {
2415 mEffects[idx_insert-1]->configure();
2416 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2417 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2418 }
2419 effect->setOutBuffer(mOutBuffer);
2420 } else {
2421 effect->setOutBuffer(mInBuffer);
2422 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002423 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002424 ALOGV("%s effect %p, added in chain %p at rank %zu",
2425 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002426 }
2427 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002428
Eric Laurentca7cc822012-11-19 14:55:58 -08002429 return NO_ERROR;
2430}
2431
Eric Laurentb62d0362021-10-26 17:40:18 +02002432ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2433 // Insert effects are inserted at the end of mEffects vector as they are processed
2434 // after track and auxiliary effects.
2435 // Insert effect order as a function of indicated preference:
2436 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2437 // another effect is present
2438 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2439 // last effect claiming first position
2440 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2441 // first effect claiming last position
2442 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2443 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2444 // already present
2445 // Spatializer or Downmixer effects are inserted in first position because
2446 // they adapt the channel count for all other effects in the chain
2447 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2448 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2449 return 0;
2450 }
2451
2452 size_t size = mEffects.size();
2453 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2454 ssize_t idx_insert;
2455 ssize_t idx_insert_first = -1;
2456 ssize_t idx_insert_last = -1;
2457
2458 idx_insert = size;
2459 for (size_t i = 0; i < size; i++) {
2460 effect_descriptor_t d = mEffects[i]->desc();
2461 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2462 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2463 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2464 // check invalid effect chaining combinations
2465 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2466 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2467 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2468 __func__, desc.name, d.name);
2469 return -1;
2470 }
2471 // remember position of first insert effect and by default
2472 // select this as insert position for new effect
Andy Hung920f6572022-10-06 12:09:49 -07002473 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002474 idx_insert = i;
2475 }
2476 // remember position of last insert effect claiming
2477 // first position
2478 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2479 idx_insert_first = i;
2480 }
2481 // remember position of first insert effect claiming
2482 // last position
2483 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2484 idx_insert_last == -1) {
2485 idx_insert_last = i;
2486 }
2487 }
2488 }
2489
2490 // modify idx_insert from first position if needed
2491 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2492 if (idx_insert_last != -1) {
2493 idx_insert = idx_insert_last;
2494 } else {
2495 idx_insert = size;
2496 }
2497 } else {
2498 if (idx_insert_first != -1) {
2499 idx_insert = idx_insert_first + 1;
2500 }
2501 }
2502 return idx_insert;
2503}
2504
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002505// removeEffect_l() must be called with ThreadBase::mLock held
2506size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2507 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002508{
2509 Mutex::Autolock _l(mLock);
2510 size_t size = mEffects.size();
2511 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2512
2513 for (size_t i = 0; i < size; i++) {
2514 if (effect == mEffects[i]) {
2515 // calling stop here will remove pre-processing effect from the audio HAL.
2516 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2517 // the middle of a read from audio HAL
2518 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2519 mEffects[i]->state() == EffectModule::STOPPING) {
2520 mEffects[i]->stop();
2521 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002522 if (release) {
2523 mEffects[i]->release_l();
2524 }
2525
Mikhail Naganov022b9952017-01-04 16:36:51 -08002526 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002527 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002528 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002529 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002530 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002531 }
2532 }
2533 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002534
2535 // make sure the input buffer configuration for the new first effect in the chain
2536 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2537 if (i == 0 && size > 1) {
2538 mEffects[0]->configure();
2539 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002540 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002541 }
2542
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002543 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002544 this, i);
2545 break;
2546 }
2547 }
2548
2549 return mEffects.size();
2550}
2551
jiabin8f278ee2019-11-11 12:16:27 -08002552// setDevices_l() must be called with ThreadBase::mLock held
2553void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002554{
2555 size_t size = mEffects.size();
2556 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002557 mEffects[i]->setDevices(devices);
2558 }
2559}
2560
2561// setInputDevice_l() must be called with ThreadBase::mLock held
2562void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2563{
2564 size_t size = mEffects.size();
2565 for (size_t i = 0; i < size; i++) {
2566 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002567 }
2568}
2569
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002570// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002571void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2572{
2573 size_t size = mEffects.size();
2574 for (size_t i = 0; i < size; i++) {
2575 mEffects[i]->setMode(mode);
2576 }
2577}
2578
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002579// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002580void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2581{
2582 size_t size = mEffects.size();
2583 for (size_t i = 0; i < size; i++) {
2584 mEffects[i]->setAudioSource(source);
2585 }
2586}
2587
Zhou Songd505c642020-02-20 16:35:37 +08002588bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2589 for (const auto &effect : mEffects) {
2590 if (effect->isVolumeControlEnabled()) return true;
2591 }
2592 return false;
2593}
2594
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002595// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002596bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002597{
2598 uint32_t newLeft = *left;
2599 uint32_t newRight = *right;
2600 bool hasControl = false;
2601 int ctrlIdx = -1;
2602 size_t size = mEffects.size();
2603
2604 // first update volume controller
2605 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002606 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002607 ctrlIdx = i - 1;
2608 hasControl = true;
2609 break;
2610 }
2611 }
2612
Eric Laurentfa1e1232016-08-02 19:01:49 -07002613 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002614 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002615 if (hasControl) {
2616 *left = mNewLeftVolume;
2617 *right = mNewRightVolume;
2618 }
2619 return hasControl;
2620 }
2621
2622 mVolumeCtrlIdx = ctrlIdx;
2623 mLeftVolume = newLeft;
2624 mRightVolume = newRight;
2625
2626 // second get volume update from volume controller
2627 if (ctrlIdx >= 0) {
2628 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2629 mNewLeftVolume = newLeft;
2630 mNewRightVolume = newRight;
2631 }
2632 // then indicate volume to all other effects in chain.
2633 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002634 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002635 uint32_t lVol = newLeft;
2636 uint32_t rVol = newRight;
2637
2638 for (size_t i = 0; i < size; i++) {
2639 if ((int)i == ctrlIdx) {
2640 continue;
2641 }
2642 // this also works for ctrlIdx == -1 when there is no volume controller
2643 if ((int)i > ctrlIdx) {
2644 lVol = *left;
2645 rVol = *right;
2646 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002647 // Pass requested volume directly if this is volume monitor module
2648 if (mEffects[i]->isVolumeMonitor()) {
2649 mEffects[i]->setVolume(left, right, false);
2650 } else {
2651 mEffects[i]->setVolume(&lVol, &rVol, false);
2652 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002653 }
2654 *left = newLeft;
2655 *right = newRight;
2656
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002657 setVolumeForOutput_l(*left, *right);
2658
Eric Laurentca7cc822012-11-19 14:55:58 -08002659 return hasControl;
2660}
2661
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002662// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002663void AudioFlinger::EffectChain::resetVolume_l()
2664{
Eric Laurente7449bf2016-08-03 18:44:07 -07002665 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2666 uint32_t left = mLeftVolume;
2667 uint32_t right = mRightVolume;
2668 (void)setVolume_l(&left, &right, true);
2669 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002670}
2671
jiabineb3bda02020-06-30 14:07:03 -07002672// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2673bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2674{
2675 for (size_t i = 0; i < mEffects.size(); ++i) {
2676 if (mEffects[i]->isHapticGenerator()) {
2677 return true;
2678 }
2679 }
2680 return false;
2681}
2682
Simon Bowden62823412022-10-17 14:52:26 +00002683void AudioFlinger::EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002684{
2685 Mutex::Autolock _l(mLock);
2686 for (size_t i = 0; i < mEffects.size(); ++i) {
2687 mEffects[i]->setHapticIntensity(id, intensity);
2688 }
2689}
2690
Eric Laurent1b928682014-10-02 19:41:47 -07002691void AudioFlinger::EffectChain::syncHalEffectsState()
2692{
2693 Mutex::Autolock _l(mLock);
2694 for (size_t i = 0; i < mEffects.size(); i++) {
2695 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2696 mEffects[i]->state() == EffectModule::STOPPING) {
2697 mEffects[i]->addEffectToHal_l();
2698 }
2699 }
2700}
2701
Eric Laurentca7cc822012-11-19 14:55:58 -08002702void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07002703NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002704{
Eric Laurentca7cc822012-11-19 14:55:58 -08002705 String8 result;
2706
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002707 const size_t numEffects = mEffects.size();
2708 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002709
Marco Nelissenb2208842014-02-07 14:00:50 -08002710 if (numEffects) {
2711 bool locked = AudioFlinger::dumpTryLock(mLock);
2712 // failed to lock - AudioFlinger is probably deadlocked
2713 if (!locked) {
2714 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002715 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002716
Andy Hungbded9c82017-11-30 18:47:35 -08002717 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2718 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2719 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2720 (int)inBufferStr.size(), "In buffer ",
2721 (int)outBufferStr.size(), "Out buffer ");
2722 result.appendFormat("\t%s %s %d\n",
2723 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002724 write(fd, result.string(), result.size());
2725
2726 for (size_t i = 0; i < numEffects; ++i) {
2727 sp<EffectModule> effect = mEffects[i];
2728 if (effect != 0) {
2729 effect->dump(fd, args);
2730 }
2731 }
2732
2733 if (locked) {
2734 mLock.unlock();
2735 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002736 } else {
2737 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002738 }
2739}
2740
2741// must be called with ThreadBase::mLock held
2742void AudioFlinger::EffectChain::setEffectSuspended_l(
2743 const effect_uuid_t *type, bool suspend)
2744{
2745 sp<SuspendedEffectDesc> desc;
2746 // use effect type UUID timelow as key as there is no real risk of identical
2747 // timeLow fields among effect type UUIDs.
2748 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2749 if (suspend) {
2750 if (index >= 0) {
2751 desc = mSuspendedEffects.valueAt(index);
2752 } else {
2753 desc = new SuspendedEffectDesc();
2754 desc->mType = *type;
2755 mSuspendedEffects.add(type->timeLow, desc);
2756 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2757 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002758
Eric Laurentca7cc822012-11-19 14:55:58 -08002759 if (desc->mRefCount++ == 0) {
2760 sp<EffectModule> effect = getEffectIfEnabled(type);
2761 if (effect != 0) {
2762 desc->mEffect = effect;
2763 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002764 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002765 }
2766 }
2767 } else {
2768 if (index < 0) {
2769 return;
2770 }
2771 desc = mSuspendedEffects.valueAt(index);
2772 if (desc->mRefCount <= 0) {
2773 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002774 desc->mRefCount = 0;
2775 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002776 }
2777 if (--desc->mRefCount == 0) {
2778 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2779 if (desc->mEffect != 0) {
2780 sp<EffectModule> effect = desc->mEffect.promote();
2781 if (effect != 0) {
2782 effect->setSuspended(false);
2783 effect->lock();
2784 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002785 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002786 effect->setEnabled_l(handle->enabled());
2787 }
2788 effect->unlock();
2789 }
2790 desc->mEffect.clear();
2791 }
2792 mSuspendedEffects.removeItemsAt(index);
2793 }
2794 }
2795}
2796
2797// must be called with ThreadBase::mLock held
2798void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2799{
2800 sp<SuspendedEffectDesc> desc;
2801
2802 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2803 if (suspend) {
2804 if (index >= 0) {
2805 desc = mSuspendedEffects.valueAt(index);
2806 } else {
2807 desc = new SuspendedEffectDesc();
2808 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2809 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2810 }
2811 if (desc->mRefCount++ == 0) {
2812 Vector< sp<EffectModule> > effects;
2813 getSuspendEligibleEffects(effects);
2814 for (size_t i = 0; i < effects.size(); i++) {
2815 setEffectSuspended_l(&effects[i]->desc().type, true);
2816 }
2817 }
2818 } else {
2819 if (index < 0) {
2820 return;
2821 }
2822 desc = mSuspendedEffects.valueAt(index);
2823 if (desc->mRefCount <= 0) {
2824 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2825 desc->mRefCount = 1;
2826 }
2827 if (--desc->mRefCount == 0) {
2828 Vector<const effect_uuid_t *> types;
2829 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2830 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2831 continue;
2832 }
2833 types.add(&mSuspendedEffects.valueAt(i)->mType);
2834 }
2835 for (size_t i = 0; i < types.size(); i++) {
2836 setEffectSuspended_l(types[i], false);
2837 }
2838 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2839 mSuspendedEffects.keyAt(index));
2840 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2841 }
2842 }
2843}
2844
2845
2846// The volume effect is used for automated tests only
2847#ifndef OPENSL_ES_H_
2848static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2849 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2850const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2851#endif //OPENSL_ES_H_
2852
Eric Laurentd8365c52017-07-16 15:27:05 -07002853/* static */
2854bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2855{
2856 // Only NS and AEC are suspended when BtNRec is off
2857 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2858 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2859 return true;
2860 }
2861 return false;
2862}
2863
Eric Laurentca7cc822012-11-19 14:55:58 -08002864bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2865{
2866 // auxiliary effects and visualizer are never suspended on output mix
2867 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2868 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2869 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002870 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2871 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002872 return false;
2873 }
2874 return true;
2875}
2876
2877void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2878 Vector< sp<AudioFlinger::EffectModule> > &effects)
2879{
2880 effects.clear();
2881 for (size_t i = 0; i < mEffects.size(); i++) {
2882 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2883 effects.add(mEffects[i]);
2884 }
2885 }
2886}
2887
2888sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2889 const effect_uuid_t *type)
2890{
2891 sp<EffectModule> effect = getEffectFromType_l(type);
2892 return effect != 0 && effect->isEnabled() ? effect : 0;
2893}
2894
2895void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2896 bool enabled)
2897{
2898 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2899 if (enabled) {
2900 if (index < 0) {
2901 // if the effect is not suspend check if all effects are suspended
2902 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2903 if (index < 0) {
2904 return;
2905 }
2906 if (!isEffectEligibleForSuspend(effect->desc())) {
2907 return;
2908 }
2909 setEffectSuspended_l(&effect->desc().type, enabled);
2910 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2911 if (index < 0) {
2912 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2913 return;
2914 }
2915 }
2916 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2917 effect->desc().type.timeLow);
2918 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002919 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002920 if (desc->mEffect == 0) {
2921 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002922 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002923 effect->setSuspended(true);
2924 }
2925 } else {
2926 if (index < 0) {
2927 return;
2928 }
2929 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2930 effect->desc().type.timeLow);
2931 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2932 desc->mEffect.clear();
2933 effect->setSuspended(false);
2934 }
2935}
2936
Eric Laurent5baf2af2013-09-12 17:37:00 -07002937bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002938{
2939 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002940 return isNonOffloadableEnabled_l();
2941}
2942
2943bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2944{
Eric Laurent813e2a72013-08-31 12:59:48 -07002945 size_t size = mEffects.size();
2946 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002947 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002948 return true;
2949 }
2950 }
2951 return false;
2952}
2953
Eric Laurentaaa44472014-09-12 17:41:50 -07002954void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2955{
2956 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002957 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002958}
2959
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002960void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2961{
2962 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2963 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2964 }
2965 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2966 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2967 }
jiabinc658e452022-10-21 20:52:21 +00002968 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2969 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2970 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002971}
2972
2973void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2974{
2975 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2976 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2977 }
2978 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2979 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2980 }
2981}
2982
2983bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002984{
2985 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002986 for (const auto &effect : mEffects) {
2987 if (effect->isProcessImplemented()) {
2988 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002989 }
2990 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002991 // Allow effects without processing.
2992 return true;
2993}
2994
2995bool AudioFlinger::EffectChain::isFastCompatible() const
2996{
2997 Mutex::Autolock _l(mLock);
2998 for (const auto &effect : mEffects) {
2999 if (effect->isProcessImplemented()
3000 && effect->isImplementationSoftware()) {
3001 return false;
3002 }
3003 }
3004 // Allow effects without processing or hw accelerated effects.
3005 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07003006}
3007
jiabinc658e452022-10-21 20:52:21 +00003008bool AudioFlinger::EffectChain::isBitPerfectCompatible() const {
3009 Mutex::Autolock _l(mLock);
3010 for (const auto &effect : mEffects) {
3011 if (effect->isProcessImplemented()
3012 && effect->isImplementationSoftware()) {
3013 return false;
3014 }
3015 }
3016 // Allow effects without processing or hw accelerated effects.
3017 return true;
3018}
3019
Eric Laurent4c415062016-06-17 16:14:16 -07003020// isCompatibleWithThread_l() must be called with thread->mLock held
3021bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
3022{
3023 Mutex::Autolock _l(mLock);
3024 for (size_t i = 0; i < mEffects.size(); i++) {
3025 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3026 return false;
3027 }
3028 }
3029 return true;
3030}
3031
Eric Laurent6b446ce2019-12-13 10:56:31 -08003032// EffectCallbackInterface implementation
3033status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3034 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3035 sp<EffectHalInterface> *effect) {
3036 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003037 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003038 if (effectsFactory != 0) {
3039 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3040 }
3041 return status;
3042}
3043
3044bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003045 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003046 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003047 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003048}
3049
3050status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3051 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003052 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003053}
3054
3055status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003056 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003057 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003058 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003059 if (t == nullptr) {
3060 return result;
3061 }
3062 sp <StreamHalInterface> st = t->stream();
3063 if (st == nullptr) {
3064 return result;
3065 }
3066 result = st->addEffect(effect);
3067 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3068 return result;
3069}
3070
3071status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003072 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003073 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003074 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003075 if (t == nullptr) {
3076 return result;
3077 }
3078 sp <StreamHalInterface> st = t->stream();
3079 if (st == nullptr) {
3080 return result;
3081 }
3082 result = st->removeEffect(effect);
3083 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3084 return result;
3085}
3086
3087audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003088 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003089 if (t == nullptr) {
3090 return AUDIO_IO_HANDLE_NONE;
3091 }
3092 return t->id();
3093}
3094
3095bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003096 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003097 if (t == nullptr) {
3098 return true;
3099 }
3100 return t->isOutput();
3101}
3102
3103bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003104 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003105}
3106
3107bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003108 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003109}
3110
3111bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003112 switch (mThreadType) {
3113 case ThreadBase::OFFLOAD:
3114 case ThreadBase::MMAP_PLAYBACK:
3115 case ThreadBase::MMAP_CAPTURE:
3116 return true;
3117 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003118 return false;
3119 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003120}
3121
3122bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3123 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003124}
3125
3126uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003127 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003128 if (t == nullptr) {
3129 return 0;
3130 }
3131 return t->sampleRate();
3132}
3133
Eric Laurentf1f22e72021-07-13 14:04:14 +02003134audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3135 sp<ThreadBase> t = thread().promote();
3136 if (t == nullptr) {
3137 return AUDIO_CHANNEL_NONE;
3138 }
3139 sp<EffectChain> c = chain().promote();
3140 if (c == nullptr) {
3141 return AUDIO_CHANNEL_NONE;
3142 }
3143
Eric Laurentb62d0362021-10-26 17:40:18 +02003144 if (mThreadType == ThreadBase::SPATIALIZER) {
3145 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3146 if (c->isFirstEffect(id)) {
3147 return t->mixerChannelMask();
3148 } else {
3149 return t->channelMask();
3150 }
3151 } else if (!audio_is_global_session(c->sessionId())) {
3152 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3153 return t->mixerChannelMask();
3154 } else {
3155 return t->channelMask();
3156 }
3157 } else {
3158 return t->channelMask();
3159 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003160 } else {
3161 return t->channelMask();
3162 }
3163}
3164
3165uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003166 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003167}
3168
3169audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003170 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171 if (t == nullptr) {
3172 return AUDIO_CHANNEL_NONE;
3173 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003174 sp<EffectChain> c = chain().promote();
3175 if (c == nullptr) {
3176 return AUDIO_CHANNEL_NONE;
3177 }
3178
3179 if (mThreadType == ThreadBase::SPATIALIZER) {
3180 if (!audio_is_global_session(c->sessionId())) {
3181 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3182 return t->mixerChannelMask();
3183 } else {
3184 return t->channelMask();
3185 }
3186 } else {
3187 return t->channelMask();
3188 }
3189 } else {
3190 return t->channelMask();
3191 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003192}
3193
Eric Laurentf1f22e72021-07-13 14:04:14 +02003194uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003195 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003196}
3197
jiabineb3bda02020-06-30 14:07:03 -07003198audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003199 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003200 if (t == nullptr) {
3201 return AUDIO_CHANNEL_NONE;
3202 }
3203 return t->hapticChannelMask();
3204}
3205
Eric Laurent6b446ce2019-12-13 10:56:31 -08003206size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003207 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003208 if (t == nullptr) {
3209 return 0;
3210 }
3211 return t->frameCount();
3212}
3213
Andy Hung920f6572022-10-06 12:09:49 -07003214uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3215NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3216{
Andy Hung328d6772021-01-12 12:32:21 -08003217 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003218 if (t == nullptr) {
3219 return 0;
3220 }
Andy Hung920f6572022-10-06 12:09:49 -07003221 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003222 return t->latency_l();
3223}
3224
Andy Hung920f6572022-10-06 12:09:49 -07003225void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3226NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3227{
Andy Hung328d6772021-01-12 12:32:21 -08003228 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003229 if (t == nullptr) {
3230 return;
3231 }
3232 t->setVolumeForOutput_l(left, right);
3233}
3234
3235void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003236 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003237 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003238 if (t == nullptr) {
3239 return;
3240 }
3241 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3242
Andy Hung328d6772021-01-12 12:32:21 -08003243 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003244 if (c == nullptr) {
3245 return;
3246 }
Eric Laurent41709552019-12-16 19:34:05 -08003247 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3248 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003249}
3250
Eric Laurent41709552019-12-16 19:34:05 -08003251void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003252 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003253 if (t == nullptr) {
3254 return;
3255 }
Eric Laurent41709552019-12-16 19:34:05 -08003256 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3257 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003258}
3259
Eric Laurent41709552019-12-16 19:34:05 -08003260void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003261 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3262
Andy Hung328d6772021-01-12 12:32:21 -08003263 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003264 if (t == nullptr) {
3265 return;
3266 }
3267 t->onEffectDisable();
3268}
3269
3270bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3271 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003272 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003273 if (t == nullptr) {
3274 return false;
3275 }
3276 t->disconnectEffectHandle(handle, unpinIfLast);
3277 return true;
3278}
3279
3280void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003281 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003282 if (c == nullptr) {
3283 return;
3284 }
3285 c->resetVolume_l();
3286
3287}
3288
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003289product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003290 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003291 if (c == nullptr) {
3292 return PRODUCT_STRATEGY_NONE;
3293 }
3294 return c->strategy();
3295}
3296
3297int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003298 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003299 if (c == nullptr) {
3300 return 0;
3301 }
3302 return c->activeTrackCnt();
3303}
3304
Eric Laurentb82e6b72019-11-22 17:25:04 -08003305
3306#undef LOG_TAG
3307#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3308
3309status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3310{
3311 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3312 Mutex::Autolock _l(mProxyLock);
3313 if (status == NO_ERROR) {
3314 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003315 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003316 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003317 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003318 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003319 bs = handle.second->disable(&status);
3320 }
3321 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003322 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003323 }
3324 }
3325 }
3326 ALOGV("%s enable %d status %d", __func__, enabled, status);
3327 return status;
3328}
3329
3330status_t AudioFlinger::DeviceEffectProxy::init(
3331 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3332//For all audio patches
3333//If src or sink device match
3334//If the effect is HW accelerated
3335// if no corresponding effect module
3336// Create EffectModule: mHalEffect
3337//Create and attach EffectHandle
3338//If the effect is not HW accelerated and the patch sink or src is a mixer port
3339// Create Effect on patch input or output thread on session -1
3340//Add EffectHandle to EffectHandle map of Effect Proxy:
3341 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3342 status_t status = NO_ERROR;
3343 for (auto &patch : patches) {
3344 status = onCreatePatch(patch.first, patch.second);
3345 ALOGV("%s onCreatePatch status %d", __func__, status);
3346 if (status == BAD_VALUE) {
3347 return status;
3348 }
3349 }
3350 return status;
3351}
3352
3353status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3354 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3355 status_t status = NAME_NOT_FOUND;
3356 sp<EffectHandle> handle;
3357 // only consider source[0] as this is the only "true" source of a patch
3358 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3359 ALOGV("%s source checkPort status %d", __func__, status);
3360 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3361 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3362 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3363 }
3364 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3365 Mutex::Autolock _l(mProxyLock);
3366 mEffectHandles.emplace(patchHandle, handle);
3367 }
3368 ALOGW_IF(status == BAD_VALUE,
3369 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3370
3371 return status;
3372}
3373
3374status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3375 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3376
3377 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3378 __func__, port->type, port->ext.device.type,
3379 port->ext.device.address, port->id, patch.isSoftware());
3380 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003381 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003382 return NAME_NOT_FOUND;
3383 }
3384 status_t status = NAME_NOT_FOUND;
3385
3386 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3387 Mutex::Autolock _l(mProxyLock);
3388 mDevicePort = *port;
3389 mHalEffect = new EffectModule(mMyCallback,
3390 const_cast<effect_descriptor_t *>(&mDescriptor),
3391 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3392 false /* pinned */, port->id);
3393 if (audio_is_input_device(mDevice.mType)) {
3394 mHalEffect->setInputDevice(mDevice);
3395 } else {
3396 mHalEffect->setDevices({mDevice});
3397 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003398 mHalEffect->configure();
3399
Eric Laurentde8caf42021-08-11 17:19:25 +02003400 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3401 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003402 status = (*handle)->initCheck();
3403 if (status == OK) {
3404 status = mHalEffect->addHandle((*handle).get());
3405 } else {
3406 mHalEffect.clear();
3407 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3408 }
3409 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3410 sp <ThreadBase> thread;
3411 if (audio_port_config_has_input_direction(port)) {
3412 if (patch.isSoftware()) {
3413 thread = patch.mRecord.thread();
3414 } else {
3415 thread = patch.thread().promote();
3416 }
3417 } else {
3418 if (patch.isSoftware()) {
3419 thread = patch.mPlayback.thread();
3420 } else {
3421 thread = patch.thread().promote();
3422 }
3423 }
3424 int enabled;
3425 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3426 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003427 &enabled, &status, false, false /*probe*/,
3428 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003429 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3430 } else {
3431 status = BAD_VALUE;
3432 }
3433 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003434 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003435 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003436 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003437 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003438 bs = (*handle)->disable(&status);
3439 }
3440 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003441 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003442 }
3443 }
3444 return status;
3445}
3446
3447void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003448 sp<EffectHandle> effect;
3449 {
3450 Mutex::Autolock _l(mProxyLock);
3451 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3452 effect = mEffectHandles.at(patchHandle);
3453 mEffectHandles.erase(patchHandle);
3454 }
3455 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003456}
3457
3458
3459size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3460{
3461 Mutex::Autolock _l(mProxyLock);
3462 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003463 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003464 mHalEffect.clear();
3465 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3466 }
3467 return mHalEffect == nullptr ? 0 : 1;
3468}
3469
3470status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003471 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003472 if (mHalEffect == nullptr) {
3473 return NO_INIT;
3474 }
3475 return mManagerCallback->addEffectToHal(
3476 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3477}
3478
3479status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003480 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003481 if (mHalEffect == nullptr) {
3482 return NO_INIT;
3483 }
3484 return mManagerCallback->removeEffectFromHal(
3485 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3486}
3487
3488bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3489 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3490 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3491 }
3492 return true;
3493}
3494
3495uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3496 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3497 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3498 return mDevicePort.sample_rate;
3499 }
3500 return DEFAULT_OUTPUT_SAMPLE_RATE;
3501}
3502
3503audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3504 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3505 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3506 return mDevicePort.channel_mask;
3507 }
3508 return AUDIO_CHANNEL_OUT_STEREO;
3509}
3510
3511uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3512 if (isOutput()) {
3513 return audio_channel_count_from_out_mask(channelMask());
3514 }
3515 return audio_channel_count_from_in_mask(channelMask());
3516}
3517
Andy Hung920f6572022-10-06 12:09:49 -07003518void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3519NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3520{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003521 const Vector<String16> args;
3522 EffectBase::dump(fd, args);
3523
3524 const bool locked = dumpTryLock(mProxyLock);
3525 if (!locked) {
3526 String8 result("DeviceEffectProxy may be deadlocked\n");
3527 write(fd, result.string(), result.size());
3528 }
3529
3530 String8 outStr;
3531 if (mHalEffect != nullptr) {
3532 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3533 } else {
3534 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3535 }
3536 write(fd, outStr.string(), outStr.size());
3537 outStr.clear();
3538
3539 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3540 write(fd, outStr.string(), outStr.size());
3541 outStr.clear();
3542
3543 for (const auto& iter : mEffectHandles) {
3544 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3545 write(fd, outStr.string(), outStr.size());
3546 outStr.clear();
3547 sp<EffectBase> effect = iter.second->effect().promote();
3548 if (effect != nullptr) {
3549 effect->dump(fd, args);
3550 }
3551 }
3552
3553 if (locked) {
3554 mLock.unlock();
3555 }
3556}
3557
3558#undef LOG_TAG
3559#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3560
3561int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3562 return mManagerCallback->newEffectId();
3563}
3564
3565
3566bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3567 EffectHandle *handle, bool unpinIfLast) {
3568 sp<EffectBase> effectBase = handle->effect().promote();
3569 if (effectBase == nullptr) {
3570 return false;
3571 }
3572
3573 sp<EffectModule> effect = effectBase->asEffectModule();
3574 if (effect == nullptr) {
3575 return false;
3576 }
3577
3578 // restore suspended effects if the disconnected handle was enabled and the last one.
3579 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3580 if (remove) {
3581 sp<DeviceEffectProxy> proxy = mProxy.promote();
3582 if (proxy != nullptr) {
3583 proxy->removeEffect(effect);
3584 }
3585 if (handle->enabled()) {
3586 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3587 }
3588 }
3589 return true;
3590}
3591
3592status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3593 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3594 sp<EffectHalInterface> *effect) {
3595 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3596}
3597
3598status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003599 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003600 sp<DeviceEffectProxy> proxy = mProxy.promote();
3601 if (proxy == nullptr) {
3602 return NO_INIT;
3603 }
3604 return proxy->addEffectToHal(effect);
3605}
3606
3607status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003608 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003609 sp<DeviceEffectProxy> proxy = mProxy.promote();
3610 if (proxy == nullptr) {
3611 return NO_INIT;
3612 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003613 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003614}
3615
3616bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3617 sp<DeviceEffectProxy> proxy = mProxy.promote();
3618 if (proxy == nullptr) {
3619 return true;
3620 }
3621 return proxy->isOutput();
3622}
3623
3624uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3625 sp<DeviceEffectProxy> proxy = mProxy.promote();
3626 if (proxy == nullptr) {
3627 return DEFAULT_OUTPUT_SAMPLE_RATE;
3628 }
3629 return proxy->sampleRate();
3630}
3631
Eric Laurentf1f22e72021-07-13 14:04:14 +02003632audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3633 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003634 sp<DeviceEffectProxy> proxy = mProxy.promote();
3635 if (proxy == nullptr) {
3636 return AUDIO_CHANNEL_OUT_STEREO;
3637 }
3638 return proxy->channelMask();
3639}
3640
Eric Laurentf1f22e72021-07-13 14:04:14 +02003641uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3642 sp<DeviceEffectProxy> proxy = mProxy.promote();
3643 if (proxy == nullptr) {
3644 return 2;
3645 }
3646 return proxy->channelCount();
3647}
3648
3649audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3650 sp<DeviceEffectProxy> proxy = mProxy.promote();
3651 if (proxy == nullptr) {
3652 return AUDIO_CHANNEL_OUT_STEREO;
3653 }
3654 return proxy->channelMask();
3655}
3656
3657uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003658 sp<DeviceEffectProxy> proxy = mProxy.promote();
3659 if (proxy == nullptr) {
3660 return 2;
3661 }
3662 return proxy->channelCount();
3663}
3664
Eric Laurent76c89f32021-12-03 17:13:23 +01003665void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3666 const sp<EffectBase>& effectBase) {
3667 sp<EffectModule> effect = effectBase->asEffectModule();
3668 if (effect == nullptr) {
3669 return;
3670 }
3671 effect->start();
3672}
3673
3674void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3675 const sp<EffectBase>& effectBase) {
3676 sp<EffectModule> effect = effectBase->asEffectModule();
3677 if (effect == nullptr) {
3678 return;
3679 }
3680 effect->stop();
3681}
3682
Glenn Kasten63238ef2015-03-02 15:50:29 -08003683} // namespace android