blob: bbfe76374c48bb60a6e54cfd9948ff0ff85e9188 [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 mPolicyLock.lock();
282 }
283 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 Hung71ba4b32022-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,
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000527 effectFlagsToString(mDescriptor.flags).c_str());
Eric Laurent41709552019-12-16 19:34:05 -0800528 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
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000548 write(fd, result.c_str(), result.length());
Eric Laurent41709552019-12-16 19:34:05 -0800549}
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 Naganov59984db2022-04-19 21:21:23 +0000573 mAddedToHal(false),
574 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800575 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800576{
577 ALOGV("Constructor %p pinned %d", this, pinned);
578 int lStatus;
579
580 // create effect engine from effect factory
581 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800582 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800583 if (mStatus != NO_ERROR) {
584 return;
585 }
586 lStatus = init();
587 if (lStatus < 0) {
588 mStatus = lStatus;
589 goto Error;
590 }
591
592 setOffloaded(callback->isOffload(), callback->io());
593 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
594
595 return;
596Error:
597 mEffectInterface.clear();
598 ALOGV("Constructor Error %d", mStatus);
599}
600
601AudioFlinger::EffectModule::~EffectModule()
602{
603 ALOGV("Destructor %p", this);
604 if (mEffectInterface != 0) {
605 char uuidStr[64];
606 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
607 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
608 this, uuidStr);
609 release_l();
610 }
611
612}
613
Eric Laurentfa1e1232016-08-02 19:01:49 -0700614bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800615 Mutex::Autolock _l(mLock);
616
Eric Laurentfa1e1232016-08-02 19:01:49 -0700617 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 switch (mState) {
619 case RESTART:
620 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700621 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800622
623 case STARTING:
624 // clear auxiliary effect input buffer for next accumulation
625 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
626 memset(mConfig.inputCfg.buffer.raw,
627 0,
628 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
629 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700630 if (start_l() == NO_ERROR) {
631 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700632 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700633 } else {
634 mState = IDLE;
635 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 break;
637 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900638 // volume control for offload and direct threads must take effect immediately.
639 if (stop_l() == NO_ERROR
640 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700641 mDisableWaitCnt = mMaxDisableWaitCnt;
642 } else {
643 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
644 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800645 mState = STOPPED;
646 break;
647 case STOPPED:
648 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
649 // turn off sequence.
650 if (--mDisableWaitCnt == 0) {
651 reset_l();
652 mState = IDLE;
653 }
654 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200655 case ACTIVE:
656 for (size_t i = 0; i < mHandles.size(); i++) {
657 if (!mHandles[i]->disconnected()) {
658 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
659 }
660 }
661 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 default: //IDLE , ACTIVE, DESTROYED
663 break;
664 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700665
666 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800667}
668
669void AudioFlinger::EffectModule::process()
670{
671 Mutex::Autolock _l(mLock);
672
Mikhail Naganov022b9952017-01-04 16:36:51 -0800673 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 return;
675 }
676
rago94a1ee82017-07-21 15:11:02 -0700677 const uint32_t inChannelCount =
678 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
679 const uint32_t outChannelCount =
680 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
681 const bool auxType =
682 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
683
Andy Hungfa69ca32017-11-30 10:07:53 -0800684 // safeInputOutputSampleCount is 0 if the channel count between input and output
685 // buffers do not match. This prevents automatic accumulation or copying between the
686 // input and output effect buffers without an intermediary effect process.
687 // TODO: consider implementing channel conversion.
688 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700689 mInChannelCountRequested != mOutChannelCountRequested ? 0
690 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800691 mConfig.inputCfg.buffer.frameCount,
692 mConfig.outputCfg.buffer.frameCount);
693 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 accumulate_float(
695 mConfig.outputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.f32,
697 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800698 };
699 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800700 memcpy(
701 mConfig.outputCfg.buffer.f32,
702 mConfig.inputCfg.buffer.f32,
703 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800704 };
705
Eric Laurentca7cc822012-11-19 14:55:58 -0800706 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700707 int ret;
708 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700709 if (auxType) {
710 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800711 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700712
Andy Hung26836922023-05-22 17:31:57 -0700713 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800714 memcpy_to_i16_from_float(
715 mConfig.inputCfg.buffer.s16,
716 mConfig.inputCfg.buffer.f32,
717 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700718 }
rago94a1ee82017-07-21 15:11:02 -0700719 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800720 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
721 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
722
723 if (!auxType && mInChannelCountRequested != inChannelCount) {
724 adjust_channels(
725 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
726 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
727 sizeof(float),
728 sizeof(float)
729 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
730 inBuffer = mInConversionBuffer;
731 }
732 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
733 && mOutChannelCountRequested != outChannelCount) {
734 adjust_selected_channels(
735 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
736 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
737 sizeof(float),
738 sizeof(float)
739 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
740 outBuffer = mOutConversionBuffer;
741 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
743 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800744 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800745 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
746 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700747 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 memcpy_to_i16_from_float(
749 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800750 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800752 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700753 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800754 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800755 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800756 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
757 goto data_bypass;
758 }
759 memcpy_to_i16_from_float(
760 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800761 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800762 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800763 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700764 }
765 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800766 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800768 sp<EffectBufferHalInterface> target =
769 mOutChannelCountRequested != outChannelCount
770 ? mOutConversionBuffer : mOutBuffer;
771
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800773 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 mOutConversionBuffer->audioBuffer()->s16,
775 outChannelCount * mConfig.outputCfg.buffer.frameCount);
776 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800777 if (mOutChannelCountRequested != outChannelCount) {
778 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
779 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
780 sizeof(float),
781 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
782 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700783 } else {
rago94a1ee82017-07-21 15:11:02 -0700784 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700785 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700787 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700789 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 }
792 }
793 ret = -ENODATA;
794 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800795
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 // force transition to IDLE state when engine is ready
797 if (mState == STOPPED && ret == -ENODATA) {
798 mDisableWaitCnt = 1;
799 }
800
801 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700802 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800803 const size_t size =
804 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700805 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800806 }
807 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700808 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800809 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
810 // If an insert effect is idle and input buffer is different from output buffer,
811 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700812 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700813 // similar handling with data_bypass above.
814 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
815 accumulateInputToOutput();
816 } else { // EFFECT_BUFFER_ACCESS_WRITE
817 copyInputToOutput();
818 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800819 }
820 }
821}
822
823void AudioFlinger::EffectModule::reset_l()
824{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700825 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 return;
827 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700828 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800829}
830
831status_t AudioFlinger::EffectModule::configure()
832{
rago94a1ee82017-07-21 15:11:02 -0700833 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700834 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700835 uint32_t size;
836 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700837 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700839 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 status = NO_INIT;
841 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800842 }
843
Eric Laurentca7cc822012-11-19 14:55:58 -0800844 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800845 // TODO: handle configuration of input (record) SW effects above the HAL,
846 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
847 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700848 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200849 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200851 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800852
853 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
855 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
856 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
857 mConfig.inputCfg.channels);
858 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800859 }
jiabineb3bda02020-06-30 14:07:03 -0700860 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700861 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700862 mConfig.inputCfg.channels |= hapticChannelMask;
863 mConfig.outputCfg.channels |= hapticChannelMask;
864 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800865 mInChannelCountRequested =
866 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
867 mOutChannelCountRequested =
868 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700869
Andy Hung319587b2023-05-23 14:01:03 -0700870 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
871 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900872
873 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700874 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900875 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
876 ALOGV("Overriding effect input as 48kHz");
877 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700878 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900879 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
881 mConfig.inputCfg.bufferProvider.cookie = NULL;
882 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
883 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
884 mConfig.outputCfg.bufferProvider.cookie = NULL;
885 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
886 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
887 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
888 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800889 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 // always overwrites output buffer: input buffer == output buffer
891 // - in other sessions:
892 // last effect in the chain accumulates in output buffer: input buffer != output buffer
893 // other effect: overwrites output buffer: input buffer == output buffer
894 // Auxiliary effect:
895 // accumulates in output buffer: input buffer != output buffer
896 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700897 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800898 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
899 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700900 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800901 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov59984db2022-04-19 21:21:23 +0000902 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800903
Eric Laurent6b446ce2019-12-13 10:56:31 -0800904 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700905 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800906 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800907
908 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700909 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700910 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800911 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700912 &mConfig,
913 &size,
914 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700915 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800916 status = cmdStatus;
917 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800918
Andy Hung9aad48c2017-11-29 10:29:19 -0800919 if (status != NO_ERROR &&
Mikhail Naganov59984db2022-04-19 21:21:23 +0000920 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800921 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
922 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
923 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700924 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
925 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800926 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
927 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
928 }
929 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
930 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
931 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
932 }
933 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700934 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800935 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700936 &mConfig,
937 &size,
938 &cmdStatus);
939 if (status == NO_ERROR) {
940 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800941 }
942 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800943
Andy Hung9aad48c2017-11-29 10:29:19 -0800944 if (status == NO_ERROR) {
945 mSupportsFloat = true;
946 }
947
948 if (status != NO_ERROR) {
949 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
950 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
951 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
952 size = sizeof(int);
953 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
954 sizeof(mConfig),
955 &mConfig,
956 &size,
957 &cmdStatus);
958 if (status == NO_ERROR) {
959 status = cmdStatus;
960 }
961 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700962 mSupportsFloat = false;
963 ALOGVV("config worked with 16 bit");
964 } else {
965 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800966 }
rago94a1ee82017-07-21 15:11:02 -0700967 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800968
rago94a1ee82017-07-21 15:11:02 -0700969 if (status == NO_ERROR) {
970 // Establish Buffer strategy
971 setInBuffer(mInBuffer);
972 setOutBuffer(mOutBuffer);
973
974 // Update visualizer latency
975 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
976 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
977 effect_param_t *p = (effect_param_t *)buf32;
978
979 p->psize = sizeof(uint32_t);
980 p->vsize = sizeof(uint32_t);
981 size = sizeof(int);
982 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
983
Andy Hungfda44002021-06-03 17:23:16 -0700984 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700985
986 *((int32_t *)p->data + 1)= latency;
987 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
988 sizeof(effect_param_t) + 8,
989 &buf32,
990 &size,
991 &cmdStatus);
992 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800993 }
994
Andy Hung05083ac2017-12-14 15:00:28 -0800995 // mConfig.outputCfg.buffer.frameCount cannot be zero.
996 mMaxDisableWaitCnt = (uint32_t)std::max(
997 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
998 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
999 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001000
Eric Laurentd0ebb532013-04-02 16:41:41 -07001001exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001002 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001003 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001004 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001005 return status;
1006}
1007
1008status_t AudioFlinger::EffectModule::init()
1009{
1010 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001011 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001012 return NO_INIT;
1013 }
1014 status_t cmdStatus;
1015 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001016 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1017 0,
1018 NULL,
1019 &size,
1020 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001021 if (status == 0) {
1022 status = cmdStatus;
1023 }
1024 return status;
1025}
1026
Eric Laurent1b928682014-10-02 19:41:47 -07001027void AudioFlinger::EffectModule::addEffectToHal_l()
1028{
1029 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1030 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001031 if (mAddedToHal) {
1032 return;
1033 }
1034
Andy Hungfda44002021-06-03 17:23:16 -07001035 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001036 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001037 }
1038}
1039
Eric Laurentfa1e1232016-08-02 19:01:49 -07001040// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001041status_t AudioFlinger::EffectModule::start()
1042{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001043 status_t status;
1044 {
1045 Mutex::Autolock _l(mLock);
1046 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001047 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001048 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001049 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001050 }
1051 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001052}
1053
1054status_t AudioFlinger::EffectModule::start_l()
1055{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001056 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001057 return NO_INIT;
1058 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001059 if (mStatus != NO_ERROR) {
1060 return mStatus;
1061 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 status_t cmdStatus;
1063 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001064 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1065 0,
1066 NULL,
1067 &size,
1068 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 if (status == 0) {
1070 status = cmdStatus;
1071 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001072 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001073 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 }
1075 return status;
1076}
1077
1078status_t AudioFlinger::EffectModule::stop()
1079{
1080 Mutex::Autolock _l(mLock);
1081 return stop_l();
1082}
1083
1084status_t AudioFlinger::EffectModule::stop_l()
1085{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001086 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 return NO_INIT;
1088 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001089 if (mStatus != NO_ERROR) {
1090 return mStatus;
1091 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001092 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001094
1095 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001096 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1097 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1098 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001099 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001100 mSetVolumeReentrantTid = INVALID_PID;
1101 }
1102
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001103 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1104 0,
1105 NULL,
1106 &size,
1107 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001108 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001109 status = cmdStatus;
1110 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001111 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001112 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001113 }
1114 return status;
1115}
1116
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001117// must be called with EffectChain::mLock held
1118void AudioFlinger::EffectModule::release_l()
1119{
1120 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001121 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001122 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001123 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001124 mEffectInterface.clear();
1125 }
1126}
1127
Eric Laurent6b446ce2019-12-13 10:56:31 -08001128status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001129{
1130 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1131 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001132 if (!mAddedToHal) {
1133 return NO_ERROR;
1134 }
1135
Andy Hungfda44002021-06-03 17:23:16 -07001136 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001137 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001139 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001140}
1141
Andy Hunge4a1d912016-08-17 14:11:13 -07001142// round up delta valid if value and divisor are positive.
1143template <typename T>
1144static T roundUpDelta(const T &value, const T &divisor) {
1145 T remainder = value % divisor;
1146 return remainder == 0 ? 0 : divisor - remainder;
1147}
1148
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001149status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1150 const std::vector<uint8_t>& cmdData,
1151 int32_t maxReplySize,
1152 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001153{
1154 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001155 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001156
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001157 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001158 return NO_INIT;
1159 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001160 if (mStatus != NO_ERROR) {
1161 return mStatus;
1162 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001163 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1164 return -EINVAL;
1165 }
1166 size_t cmdSize = cmdData.size();
1167 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1168 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1169 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001170 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001171 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001172 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001173 android_errorWriteLog(0x534e4554, "33003822");
1174 return -EINVAL;
1175 }
1176 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001177 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001178 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001179 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001180 return -EINVAL;
1181 }
ragoe2759072016-11-22 18:02:48 -08001182 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001183 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001184 || param->psize > maxReplySize - sizeof(effect_param_t)
1185 || param->vsize > maxReplySize - sizeof(effect_param_t)
1186 - param->psize
1187 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1188 maxReplySize
1189 - sizeof(effect_param_t)
1190 - param->psize
1191 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001192 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1193 android_errorWriteLog(0x534e4554, "32705438");
1194 return -EINVAL;
1195 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001196 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001197 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1198 && // DEFERRED not generally used
1199 (param == nullptr
1200 || param->psize > cmdSize - sizeof(effect_param_t)
1201 || param->vsize > cmdSize - sizeof(effect_param_t)
1202 - param->psize
1203 || roundUpDelta(param->psize,
1204 (uint32_t) sizeof(int)) >
1205 cmdSize
1206 - sizeof(effect_param_t)
1207 - param->psize
1208 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001209 android_errorWriteLog(0x534e4554, "30204301");
1210 return -EINVAL;
1211 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001212 uint32_t replySize = maxReplySize;
1213 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001214 status_t status = mEffectInterface->command(cmdCode,
1215 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001216 const_cast<uint8_t*>(cmdData.data()),
1217 &replySize,
1218 reply->data());
1219 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001220 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001221 for (size_t i = 1; i < mHandles.size(); i++) {
1222 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001223 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001224 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001225 }
1226 }
1227 }
1228 return status;
1229}
1230
Eric Laurentca7cc822012-11-19 14:55:58 -08001231bool AudioFlinger::EffectModule::isProcessEnabled() const
1232{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001233 if (mStatus != NO_ERROR) {
1234 return false;
1235 }
1236
Eric Laurentca7cc822012-11-19 14:55:58 -08001237 switch (mState) {
1238 case RESTART:
1239 case ACTIVE:
1240 case STOPPING:
1241 case STOPPED:
1242 return true;
1243 case IDLE:
1244 case STARTING:
1245 case DESTROYED:
1246 default:
1247 return false;
1248 }
1249}
1250
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001251bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1252{
Andy Hungfda44002021-06-03 17:23:16 -07001253 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001254}
1255
1256bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1257{
1258 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1259}
1260
Mikhail Naganov022b9952017-01-04 16:36:51 -08001261void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001262 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001263
1264 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001265 if (buffer != 0) {
1266 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1267 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1268 } else {
1269 mConfig.inputCfg.buffer.raw = NULL;
1270 }
1271 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001272 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001273
Andy Hungbded9c82017-11-30 18:47:35 -08001274 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001275 // Theoretically insert effects can also do in-place conversions (destroying
1276 // the original buffer) when the output buffer is identical to the input buffer,
1277 // but we don't optimize for it here.
1278 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001279 const uint32_t inChannelCount =
1280 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1281 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001282 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001283 // we need to translate - create hidl shared buffer and intercept
1284 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001285 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1286 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1287 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001288
1289 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1290 __func__, inChannels, inFrameCount, size);
1291
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001292 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001293 || size > mInConversionBuffer->getSize())) {
1294 mInConversionBuffer.clear();
1295 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001296 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001297 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001298 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001299 mInConversionBuffer->setFrameCount(inFrameCount);
1300 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001301 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001302 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001303 }
1304 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001305}
1306
1307void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001308 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001309
1310 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001311 if (buffer != 0) {
1312 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1313 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1314 } else {
1315 mConfig.outputCfg.buffer.raw = NULL;
1316 }
1317 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001318 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001319
Andy Hungbded9c82017-11-30 18:47:35 -08001320 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001321 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001322 const uint32_t outChannelCount =
1323 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1324 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001325 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001326 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001327 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1328 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1329 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001330
1331 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1332 __func__, outChannels, outFrameCount, size);
1333
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001334 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001335 || size > mOutConversionBuffer->getSize())) {
1336 mOutConversionBuffer.clear();
1337 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001338 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001339 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001340 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001341 mOutConversionBuffer->setFrameCount(outFrameCount);
1342 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001343 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001344 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001345 }
1346 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001347}
1348
Eric Laurentca7cc822012-11-19 14:55:58 -08001349status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1350{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001351 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001352 if (mStatus != NO_ERROR) {
1353 return mStatus;
1354 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001355 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001356 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1357 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1358 if (isProcessEnabled() &&
1359 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001360 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1361 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001362 status = setVolumeInternal(left, right, controller);
1363 }
1364 return status;
1365}
1366
1367status_t AudioFlinger::EffectModule::setVolumeInternal(
1368 uint32_t *left, uint32_t *right, bool controller) {
1369 uint32_t volume[2] = {*left, *right};
1370 uint32_t *pVolume = controller ? volume : nullptr;
1371 uint32_t size = sizeof(volume);
1372 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1373 size,
1374 volume,
1375 &size,
1376 pVolume);
1377 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1378 *left = volume[0];
1379 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001380 }
1381 return status;
1382}
1383
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001384void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1385{
Zhou Songd505c642020-02-20 16:35:37 +08001386 // for offload or direct thread, if the effect chain has non-offloadable
1387 // effect and any effect module within the chain has volume control, then
1388 // volume control is delegated to effect, otherwise, set volume to hal.
1389 if (mEffectCallback->isOffloadOrDirect() &&
1390 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001391 float vol_l = (float)left / (1 << 24);
1392 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001393 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001394 }
1395}
1396
jiabin8f278ee2019-11-11 12:16:27 -08001397status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1398 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001399{
jiabin8f278ee2019-11-11 12:16:27 -08001400 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1401 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 return NO_ERROR;
1403 }
1404
1405 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001406 if (mStatus != NO_ERROR) {
1407 return mStatus;
1408 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001410 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 status_t cmdStatus;
1412 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001413 // FIXME: use audio device types and addresses when the hal interface is ready.
1414 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001415 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001416 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001417 &size,
1418 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 }
1420 return status;
1421}
1422
jiabin8f278ee2019-11-11 12:16:27 -08001423status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1424{
1425 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1426}
1427
1428status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1429{
1430 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1431}
1432
Eric Laurentca7cc822012-11-19 14:55:58 -08001433status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1434{
1435 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001436 if (mStatus != NO_ERROR) {
1437 return mStatus;
1438 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001439 status_t status = NO_ERROR;
1440 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1441 status_t cmdStatus;
1442 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001443 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1444 sizeof(audio_mode_t),
1445 &mode,
1446 &size,
1447 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001448 if (status == NO_ERROR) {
1449 status = cmdStatus;
1450 }
1451 }
1452 return status;
1453}
1454
1455status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1456{
1457 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001458 if (mStatus != NO_ERROR) {
1459 return mStatus;
1460 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001461 status_t status = NO_ERROR;
1462 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1463 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001464 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1465 sizeof(audio_source_t),
1466 &source,
1467 &size,
1468 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 }
1470 return status;
1471}
1472
Eric Laurent5baf2af2013-09-12 17:37:00 -07001473status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1474{
1475 Mutex::Autolock _l(mLock);
1476 if (mStatus != NO_ERROR) {
1477 return mStatus;
1478 }
1479 status_t status = NO_ERROR;
1480 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1481 status_t cmdStatus;
1482 uint32_t size = sizeof(status_t);
1483 effect_offload_param_t cmd;
1484
1485 cmd.isOffload = offloaded;
1486 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001487 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1488 sizeof(effect_offload_param_t),
1489 &cmd,
1490 &size,
1491 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001492 if (status == NO_ERROR) {
1493 status = cmdStatus;
1494 }
1495 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1496 } else {
1497 if (offloaded) {
1498 status = INVALID_OPERATION;
1499 }
1500 mOffloaded = false;
1501 }
1502 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1503 return status;
1504}
1505
1506bool AudioFlinger::EffectModule::isOffloaded() const
1507{
1508 Mutex::Autolock _l(mLock);
1509 return mOffloaded;
1510}
1511
jiabineb3bda02020-06-30 14:07:03 -07001512/*static*/
1513bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1514 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1515}
1516
1517bool AudioFlinger::EffectModule::isHapticGenerator() const {
1518 return isHapticGenerator(&mDescriptor.type);
1519}
1520
jiabine70bc7f2020-06-30 22:07:55 -07001521status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1522{
1523 if (mStatus != NO_ERROR) {
1524 return mStatus;
1525 }
1526 if (!isHapticGenerator()) {
1527 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1528 return INVALID_OPERATION;
1529 }
1530
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001531 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1532 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001533 param->psize = sizeof(int32_t);
1534 param->vsize = sizeof(int32_t) * 2;
1535 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1536 *((int32_t*)param->data + 1) = id;
1537 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001538 std::vector<uint8_t> response;
1539 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001540 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001541 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1542 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001543 }
1544 return status;
1545}
1546
Lais Andradebc3f37a2021-07-02 00:13:19 +01001547status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001548{
1549 if (mStatus != NO_ERROR) {
1550 return mStatus;
1551 }
1552 if (!isHapticGenerator()) {
1553 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1554 return INVALID_OPERATION;
1555 }
1556
Lais Andradebc3f37a2021-07-02 00:13:19 +01001557 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001558 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001559 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001560 effect_param_t *param = (effect_param_t*) request.data();
1561 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001562 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001563 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1564 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001565 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1566 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1567 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001568 std::vector<uint8_t> response;
1569 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1570 if (status == NO_ERROR) {
1571 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1572 status = *reinterpret_cast<const status_t*>(response.data());
1573 }
1574 return status;
1575}
1576
Mikhail Naganov59984db2022-04-19 21:21:23 +00001577status_t AudioFlinger::EffectModule::getConfigs(
1578 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1579 Mutex::Autolock _l(mLock);
1580 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1581 return NO_INIT;
1582 }
1583 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1584 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1585 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1586 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1587 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1588 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1589 *isOutput = mIsOutput;
1590 return NO_ERROR;
1591}
1592
Andy Hungbded9c82017-11-30 18:47:35 -08001593static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1594 std::stringstream ss;
1595
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001596 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001597 return "nullptr"; // make different than below
1598 } else if (buffer->externalData() != nullptr) {
1599 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1600 << " -> "
1601 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1602 } else {
1603 ss << buffer->audioBuffer()->raw;
1604 }
1605 return ss.str();
1606}
Marco Nelissenb2208842014-02-07 14:00:50 -08001607
Eric Laurent41709552019-12-16 19:34:05 -08001608void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07001609NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001610{
Eric Laurent41709552019-12-16 19:34:05 -08001611 EffectBase::dump(fd, args);
1612
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001615
Eric Laurent41709552019-12-16 19:34:05 -08001616 result.append("\t\tStatus Engine:\n");
1617 result.appendFormat("\t\t%03d %p\n",
1618 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001619
1620 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001621
1622 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001623 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1624 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1625 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001626 mConfig.inputCfg.buffer.frameCount,
1627 mConfig.inputCfg.samplingRate,
1628 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001629 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001630 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001631
1632 result.append("\t\t- Output configuration:\n");
1633 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001634 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001635 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001636 mConfig.outputCfg.buffer.frameCount,
1637 mConfig.outputCfg.samplingRate,
1638 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001639 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001640 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001641
Andy Hungbded9c82017-11-30 18:47:35 -08001642 result.appendFormat("\t\t- HAL buffers:\n"
1643 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1644 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1645 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1646 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1647 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001648
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00001649 write(fd, result.c_str(), result.length());
Eric Laurentca7cc822012-11-19 14:55:58 -08001650
Mikhail Naganov4d547672019-02-22 14:19:19 -08001651 if (mEffectInterface != 0) {
1652 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1653 (void)mEffectInterface->dump(fd);
1654 }
1655
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 if (locked) {
1657 mLock.unlock();
1658 }
1659}
1660
1661// ----------------------------------------------------------------------------
1662// EffectHandle implementation
1663// ----------------------------------------------------------------------------
1664
1665#undef LOG_TAG
1666#define LOG_TAG "AudioFlinger::EffectHandle"
1667
Eric Laurent41709552019-12-16 19:34:05 -08001668AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001669 const sp<AudioFlinger::Client>& client,
1670 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001671 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001672 : BnEffect(),
1673 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001674 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1675 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001676{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001677 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001678 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001679
1680 if (client == 0) {
1681 return;
1682 }
1683 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1684 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001685 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001686 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001687 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001689 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001690 return;
1691 }
Glenn Kastene75da402013-11-20 13:54:52 -08001692 new(mCblk) effect_param_cblk_t();
1693 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001694}
1695
1696AudioFlinger::EffectHandle::~EffectHandle()
1697{
1698 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001699 disconnect(false);
1700}
1701
Andy Hungc747c532022-03-07 21:41:14 -08001702// Creates an association between Binder code to name for IEffect.
1703#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1704BINDER_METHOD_ENTRY(enable) \
1705BINDER_METHOD_ENTRY(disable) \
1706BINDER_METHOD_ENTRY(command) \
1707BINDER_METHOD_ENTRY(disconnect) \
1708BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001709BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001710
1711// singleton for Binder Method Statistics for IEffect
1712mediautils::MethodStatistics<int>& getIEffectStatistics() {
1713 using Code = int;
1714
1715#pragma push_macro("BINDER_METHOD_ENTRY")
1716#undef BINDER_METHOD_ENTRY
1717#define BINDER_METHOD_ENTRY(ENTRY) \
1718 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1719
1720 static mediautils::MethodStatistics<Code> methodStatistics{
1721 IEFFECT_BINDER_METHOD_MACRO_LIST
1722 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1723 };
1724#pragma pop_macro("BINDER_METHOD_ENTRY")
1725
1726 return methodStatistics;
1727}
1728
1729status_t AudioFlinger::EffectHandle::onTransact(
1730 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001731 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1732 mediautils::TimeCheck check(
1733 std::string("IEffect::").append(methodName),
1734 [code](bool timeout, float elapsedMs) {
1735 if (timeout) {
1736 ; // we don't timeout right now on the effect interface.
1737 } else {
1738 getIEffectStatistics().event(code, elapsedMs);
1739 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001740 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001741 return BnEffect::onTransact(code, data, reply, flags);
1742}
1743
Glenn Kastene75da402013-11-20 13:54:52 -08001744status_t AudioFlinger::EffectHandle::initCheck()
1745{
1746 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1747}
1748
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001749#define RETURN(code) \
1750 *_aidl_return = (code); \
1751 return Status::ok();
1752
Mikhail Naganov59984db2022-04-19 21:21:23 +00001753#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1754 ({ \
1755 auto _tmp = (exp); \
1756 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1757 std::move(_tmp.value()); \
1758 })
1759
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001760Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001761{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001762 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001763 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001764 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001765 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001766 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001767 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001769 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001771
1772 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001773 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001774 }
1775
1776 mEnabled = true;
1777
Eric Laurent6c796322019-04-09 14:13:17 -07001778 status_t status = effect->updatePolicyState();
1779 if (status != NO_ERROR) {
1780 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001781 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001782 }
1783
Eric Laurent6b446ce2019-12-13 10:56:31 -08001784 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001785
1786 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001787 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001788 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 }
1790
Eric Laurent6b446ce2019-12-13 10:56:31 -08001791 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001792 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 mEnabled = false;
1794 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001795 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001796}
1797
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001798Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001799{
1800 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001801 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001802 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001803 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001804 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001805 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001807 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001809
1810 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001811 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 }
1813 mEnabled = false;
1814
Eric Laurent6c796322019-04-09 14:13:17 -07001815 effect->updatePolicyState();
1816
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001817 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001818 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 }
1820
Eric Laurent6b446ce2019-12-13 10:56:31 -08001821 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001822 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001823}
1824
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001825Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001826{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001827 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001829 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001830}
1831
1832void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1833{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001834 AutoMutex _l(mLock);
1835 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1836 if (mDisconnected) {
1837 if (unpinIfLast) {
1838 android_errorWriteLog(0x534e4554, "32707507");
1839 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 return;
1841 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001842 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001843 {
Eric Laurent41709552019-12-16 19:34:05 -08001844 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001846 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001847 ALOGW("%s Effect handle %p disconnected after thread destruction",
1848 __func__, this);
1849 }
1850 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001851 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001853
Eric Laurentca7cc822012-11-19 14:55:58 -08001854 if (mClient != 0) {
1855 if (mCblk != NULL) {
1856 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1857 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1858 }
1859 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001860 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung71ba4b32022-10-06 12:09:49 -07001861 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001862 mClient.clear();
1863 }
1864}
1865
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001866Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1867 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1868 return Status::ok();
1869}
1870
Mikhail Naganov59984db2022-04-19 21:21:23 +00001871Status AudioFlinger::EffectHandle::getConfig(
1872 media::EffectConfig* _config, int32_t* _aidl_return) {
1873 AutoMutex _l(mLock);
1874 sp<EffectBase> effect = mEffect.promote();
1875 if (effect == nullptr || mDisconnected) {
1876 RETURN(DEAD_OBJECT);
1877 }
1878 sp<EffectModule> effectModule = effect->asEffectModule();
1879 if (effectModule == nullptr) {
1880 RETURN(INVALID_OPERATION);
1881 }
1882 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1883 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1884 bool isOutput;
1885 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1886 if (status == NO_ERROR) {
1887 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1888 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1889 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1890 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1891 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1892 _config->isOnInputStream = !isOutput;
1893 }
1894 RETURN(status);
1895}
1896
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1898 const std::vector<uint8_t>& cmdData,
1899 int32_t maxResponseSize,
1900 std::vector<uint8_t>* response,
1901 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001902{
1903 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001904 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001905
Eric Laurentc7ab3092017-06-15 18:43:46 -07001906 // reject commands reserved for internal use by audio framework if coming from outside
1907 // of audioserver
1908 switch(cmdCode) {
1909 case EFFECT_CMD_ENABLE:
1910 case EFFECT_CMD_DISABLE:
1911 case EFFECT_CMD_SET_PARAM:
1912 case EFFECT_CMD_SET_PARAM_DEFERRED:
1913 case EFFECT_CMD_SET_PARAM_COMMIT:
1914 case EFFECT_CMD_GET_PARAM:
1915 break;
1916 default:
1917 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1918 break;
1919 }
1920 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001922 }
1923
Eric Laurent1ffc5852016-12-15 14:46:09 -08001924 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001925 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001926 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001927 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001928 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001929 writeToBuffer(NO_ERROR, response);
1930 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001931 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001932 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001933 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001934 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001935 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001936 writeToBuffer(NO_ERROR, response);
1937 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001938 }
1939
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001940 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001941 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001942 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001943 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001944 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001945 // only get parameter command is permitted for applications not controlling the effect
1946 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001949
1950 // handle commands that are not forwarded transparently to effect engine
1951 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001952 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001953 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001954 }
1955
Andy Hung71ba4b32022-10-06 12:09:49 -07001956 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001957 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001959 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001960 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001961
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 // No need to trylock() here as this function is executed in the binder thread serving a
1963 // particular client process: no risk to block the whole media server process or mixer
1964 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07001965 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001966 // keep local copy of index in case of client corruption b/32220769
1967 const uint32_t clientIndex = mCblk->clientIndex;
1968 const uint32_t serverIndex = mCblk->serverIndex;
1969 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1970 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001971 mCblk->serverIndex = 0;
1972 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001973 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001974 }
1975 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001976 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001977 for (uint32_t index = serverIndex; index < clientIndex;) {
1978 int *p = (int *)(mBuffer + index);
1979 const int size = *p++;
1980 if (size < 0
1981 || size > EFFECT_PARAM_BUFFER_SIZE
1982 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001983 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001984 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001985 break;
1986 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001987
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001988 std::copy(reinterpret_cast<const uint8_t*>(p),
1989 reinterpret_cast<const uint8_t*>(p) + size,
1990 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001991
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001992 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001993 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001994 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 sizeof(int),
1996 &replyBuffer);
1997 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001998
1999 // verify shared memory: server index shouldn't change; client index can't go back.
2000 if (serverIndex != mCblk->serverIndex
2001 || clientIndex > mCblk->clientIndex) {
2002 android_errorWriteLog(0x534e4554, "32220769");
2003 status = BAD_VALUE;
2004 break;
2005 }
2006
Eric Laurentca7cc822012-11-19 14:55:58 -08002007 // stop at first error encountered
2008 if (ret != NO_ERROR) {
2009 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002010 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002011 break;
2012 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002013 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002014 break;
2015 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002016 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002017 }
2018 mCblk->serverIndex = 0;
2019 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002020 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002021 }
2022
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002023 status_t status = effect->command(cmdCode,
2024 cmdData,
2025 maxResponseSize,
2026 response);
2027 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002028}
2029
2030void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2031{
2032 ALOGV("setControl %p control %d", this, hasControl);
2033
2034 mHasControl = hasControl;
2035 mEnabled = enabled;
2036
2037 if (signal && mEffectClient != 0) {
2038 mEffectClient->controlStatusChanged(hasControl);
2039 }
2040}
2041
2042void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 const std::vector<uint8_t>& cmdData,
2044 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002045{
2046 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002047 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002048 }
2049}
2050
2051
2052
2053void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2054{
2055 if (mEffectClient != 0) {
2056 mEffectClient->enableStatusChanged(enabled);
2057 }
2058}
2059
Eric Laurentde8caf42021-08-11 17:19:25 +02002060void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2061{
2062 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2063 mEffectClient->framesProcessed(frames);
2064 }
2065}
2066
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002067void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung71ba4b32022-10-06 12:09:49 -07002068NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002069{
2070 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2071
Marco Nelissenb2208842014-02-07 14:00:50 -08002072 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002073 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002074 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002075 mHasControl ? "yes" : "no",
2076 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002077 mCblk ? mCblk->clientIndex : 0,
2078 mCblk ? mCblk->serverIndex : 0
2079 );
2080
2081 if (locked) {
2082 mCblk->lock.unlock();
2083 }
2084}
2085
2086#undef LOG_TAG
2087#define LOG_TAG "AudioFlinger::EffectChain"
2088
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002089AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2090 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002091 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabineb5784e2023-08-24 21:10:44 +00002092 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002093 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002094 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002095{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002096 sp<ThreadBase> p = thread.promote();
2097 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002098 return;
2099 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002100 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002101 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2102 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002103}
2104
2105AudioFlinger::EffectChain::~EffectChain()
2106{
Eric Laurentca7cc822012-11-19 14:55:58 -08002107}
2108
2109// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2110sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2111 effect_descriptor_t *descriptor)
2112{
2113 size_t size = mEffects.size();
2114
2115 for (size_t i = 0; i < size; i++) {
2116 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2117 return mEffects[i];
2118 }
2119 }
2120 return 0;
2121}
2122
2123// getEffectFromId_l() must be called with ThreadBase::mLock held
2124sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2125{
2126 size_t size = mEffects.size();
2127
2128 for (size_t i = 0; i < size; i++) {
2129 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2130 if (id == 0 || mEffects[i]->id() == id) {
2131 return mEffects[i];
2132 }
2133 }
2134 return 0;
2135}
2136
2137// getEffectFromType_l() must be called with ThreadBase::mLock held
2138sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2139 const effect_uuid_t *type)
2140{
2141 size_t size = mEffects.size();
2142
2143 for (size_t i = 0; i < size; i++) {
2144 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2145 return mEffects[i];
2146 }
2147 }
2148 return 0;
2149}
2150
Eric Laurent6c796322019-04-09 14:13:17 -07002151std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2152{
2153 std::vector<int> ids;
2154 Mutex::Autolock _l(mLock);
2155 for (size_t i = 0; i < mEffects.size(); i++) {
2156 ids.push_back(mEffects[i]->id());
2157 }
2158 return ids;
2159}
2160
Eric Laurentca7cc822012-11-19 14:55:58 -08002161void AudioFlinger::EffectChain::clearInputBuffer()
2162{
2163 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002164 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002165}
2166
2167// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002168void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002169{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002170 if (mInBuffer == NULL) {
2171 return;
2172 }
Andy Hung319587b2023-05-23 14:01:03 -07002173 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002174 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002175
Eric Laurent6b446ce2019-12-13 10:56:31 -08002176 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002177 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002178}
2179
2180// Must be called with EffectChain::mLock locked
2181void AudioFlinger::EffectChain::process_l()
2182{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002183 // never process effects when:
2184 // - on an OFFLOAD thread
2185 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002186 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002187 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002188 bool tracksOnSession = (trackCnt() != 0);
2189
2190 if (!tracksOnSession && mTailBufferCount == 0) {
2191 doProcess = false;
2192 }
2193
2194 if (activeTrackCnt() == 0) {
2195 // if no track is active and the effect tail has not been rendered,
2196 // the input buffer must be cleared here as the mixer process will not do it
2197 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002198 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002199 if (mTailBufferCount > 0) {
2200 mTailBufferCount--;
2201 }
2202 }
2203 }
2204 }
2205
2206 size_t size = mEffects.size();
2207 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002208 // Only the input and output buffers of the chain can be external,
2209 // and 'update' / 'commit' do nothing for allocated buffers, thus
2210 // it's not needed to consider any other buffers here.
2211 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002212 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2213 mOutBuffer->update();
2214 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002215 for (size_t i = 0; i < size; i++) {
2216 mEffects[i]->process();
2217 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002218 mInBuffer->commit();
2219 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2220 mOutBuffer->commit();
2221 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002223 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002224 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002225 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2226 }
2227 if (doResetVolume) {
2228 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002229 }
2230}
2231
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002232// createEffect_l() must be called with ThreadBase::mLock held
2233status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002234 effect_descriptor_t *desc,
2235 int id,
2236 audio_session_t sessionId,
2237 bool pinned)
2238{
2239 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002240 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002241 status_t lStatus = effect->status();
2242 if (lStatus == NO_ERROR) {
2243 lStatus = addEffect_ll(effect);
2244 }
2245 if (lStatus != NO_ERROR) {
2246 effect.clear();
2247 }
2248 return lStatus;
2249}
2250
2251// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002252status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2253{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002254 Mutex::Autolock _l(mLock);
2255 return addEffect_ll(effect);
2256}
2257// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2258status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2259{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002260 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002261
Eric Laurentb62d0362021-10-26 17:40:18 +02002262 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002263 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2264 // Auxiliary effects are inserted at the beginning of mEffects vector as
2265 // they are processed first and accumulated in chain input buffer
2266 mEffects.insertAt(effect, 0);
2267
2268 // the input buffer for auxiliary effect contains mono samples in
2269 // 32 bit format. This is to avoid saturation in AudoMixer
2270 // accumulation stage. Saturation is done in EffectModule::process() before
2271 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002272 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002273 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002274
Eric Laurent6b446ce2019-12-13 10:56:31 -08002275 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002276 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002277 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002278
2279 effect->configure();
2280
Mikhail Naganov022b9952017-01-04 16:36:51 -08002281 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 // auxiliary effects output samples to chain input buffer for further processing
2283 // by insert effects
2284 effect->setOutBuffer(mInBuffer);
2285 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002286 ssize_t idx_insert = getInsertIndex(desc);
2287 if (idx_insert < 0) {
2288 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002289 }
2290
Eric Laurentb62d0362021-10-26 17:40:18 +02002291 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002292 mEffects.insertAt(effect, idx_insert);
2293
2294 effect->configure();
2295
Eric Laurentb62d0362021-10-26 17:40:18 +02002296 // - By default:
2297 // All effects read samples from chain input buffer.
2298 // The last effect in the chain, writes samples to chain output buffer,
2299 // otherwise to chain input buffer
2300 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2301 // The spatializer effect (first effect) reads samples from the input buffer
2302 // and writes samples to the output buffer.
2303 // All other effects read and writes samples to the output buffer
2304 if (mEffectCallback->isSpatializer()
2305 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002306 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002307 if (idx_insert == 0) {
2308 if (previousSize != 0) {
2309 mEffects[1]->configure();
2310 mEffects[1]->setInBuffer(mOutBuffer);
2311 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2312 }
2313 effect->setInBuffer(mInBuffer);
2314 } else {
2315 effect->setInBuffer(mOutBuffer);
2316 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002317 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002318 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002319 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002320 if (idx_insert != 0) {
2321 mEffects[idx_insert-1]->configure();
2322 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2323 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2324 }
2325 effect->setOutBuffer(mOutBuffer);
2326 } else {
2327 effect->setOutBuffer(mInBuffer);
2328 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002330 ALOGV("%s effect %p, added in chain %p at rank %zu",
2331 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002332 }
2333 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002334
Eric Laurentca7cc822012-11-19 14:55:58 -08002335 return NO_ERROR;
2336}
2337
jiabineb5784e2023-08-24 21:10:44 +00002338std::optional<size_t> AudioFlinger::EffectChain::findVolumeControl_l(size_t from, size_t to) const {
2339 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2340 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2341 return i - 1;
2342 }
2343 }
2344 return std::nullopt;
2345}
2346
Eric Laurentb62d0362021-10-26 17:40:18 +02002347ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2348 // Insert effects are inserted at the end of mEffects vector as they are processed
2349 // after track and auxiliary effects.
2350 // Insert effect order as a function of indicated preference:
2351 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2352 // another effect is present
2353 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2354 // last effect claiming first position
2355 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2356 // first effect claiming last position
2357 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2358 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2359 // already present
2360 // Spatializer or Downmixer effects are inserted in first position because
2361 // they adapt the channel count for all other effects in the chain
2362 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2363 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2364 return 0;
2365 }
2366
2367 size_t size = mEffects.size();
2368 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2369 ssize_t idx_insert;
2370 ssize_t idx_insert_first = -1;
2371 ssize_t idx_insert_last = -1;
2372
2373 idx_insert = size;
2374 for (size_t i = 0; i < size; i++) {
2375 effect_descriptor_t d = mEffects[i]->desc();
2376 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2377 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2378 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2379 // check invalid effect chaining combinations
2380 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2381 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2382 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2383 __func__, desc.name, d.name);
2384 return -1;
2385 }
2386 // remember position of first insert effect and by default
2387 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002388 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002389 idx_insert = i;
2390 }
2391 // remember position of last insert effect claiming
2392 // first position
2393 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2394 idx_insert_first = i;
2395 }
2396 // remember position of first insert effect claiming
2397 // last position
2398 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2399 idx_insert_last == -1) {
2400 idx_insert_last = i;
2401 }
2402 }
2403 }
2404
2405 // modify idx_insert from first position if needed
2406 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2407 if (idx_insert_last != -1) {
2408 idx_insert = idx_insert_last;
2409 } else {
2410 idx_insert = size;
2411 }
2412 } else {
2413 if (idx_insert_first != -1) {
2414 idx_insert = idx_insert_first + 1;
2415 }
2416 }
2417 return idx_insert;
2418}
2419
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002420// removeEffect_l() must be called with ThreadBase::mLock held
2421size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2422 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002423{
2424 Mutex::Autolock _l(mLock);
2425 size_t size = mEffects.size();
2426 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2427
2428 for (size_t i = 0; i < size; i++) {
2429 if (effect == mEffects[i]) {
2430 // calling stop here will remove pre-processing effect from the audio HAL.
2431 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2432 // the middle of a read from audio HAL
2433 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2434 mEffects[i]->state() == EffectModule::STOPPING) {
2435 mEffects[i]->stop();
2436 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002437 if (release) {
2438 mEffects[i]->release_l();
2439 }
2440
Mikhail Naganov022b9952017-01-04 16:36:51 -08002441 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002442 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002443 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002444 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002445 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002446 }
2447 }
2448 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002449
2450 // make sure the input buffer configuration for the new first effect in the chain
2451 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002452 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2453 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002454 mEffects[0]->configure();
2455 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002456 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002457 }
2458
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002459 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 this, i);
2461 break;
2462 }
2463 }
2464
2465 return mEffects.size();
2466}
2467
jiabin8f278ee2019-11-11 12:16:27 -08002468// setDevices_l() must be called with ThreadBase::mLock held
2469void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002470{
2471 size_t size = mEffects.size();
2472 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002473 mEffects[i]->setDevices(devices);
2474 }
2475}
2476
2477// setInputDevice_l() must be called with ThreadBase::mLock held
2478void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2479{
2480 size_t size = mEffects.size();
2481 for (size_t i = 0; i < size; i++) {
2482 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002483 }
2484}
2485
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002486// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002487void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2488{
2489 size_t size = mEffects.size();
2490 for (size_t i = 0; i < size; i++) {
2491 mEffects[i]->setMode(mode);
2492 }
2493}
2494
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002495// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002496void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2497{
2498 size_t size = mEffects.size();
2499 for (size_t i = 0; i < size; i++) {
2500 mEffects[i]->setAudioSource(source);
2501 }
2502}
2503
Zhou Songd505c642020-02-20 16:35:37 +08002504bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2505 for (const auto &effect : mEffects) {
2506 if (effect->isVolumeControlEnabled()) return true;
2507 }
2508 return false;
2509}
2510
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002511// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002512bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002513{
2514 uint32_t newLeft = *left;
2515 uint32_t newRight = *right;
jiabineb5784e2023-08-24 21:10:44 +00002516 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002517
2518 // first update volume controller
jiabineb5784e2023-08-24 21:10:44 +00002519 const auto volumeControlIndex = findVolumeControl_l(0, size);
2520 const int ctrlIdx = volumeControlIndex.value_or(-1);
2521 const sp<EffectModule> volumeControlEffect =
2522 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
2523 const sp<EffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002524
jiabineb5784e2023-08-24 21:10:44 +00002525 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002526 *left == mLeftVolume && *right == mRightVolume) {
jiabineb5784e2023-08-24 21:10:44 +00002527 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002528 *left = mNewLeftVolume;
2529 *right = mNewRightVolume;
2530 }
jiabineb5784e2023-08-24 21:10:44 +00002531 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002532 }
2533
jiabineb5784e2023-08-24 21:10:44 +00002534 if (volumeControlEffect != cachedVolumeControlEffect) {
2535 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2536 // as zero for safe ramping.
2537 if (cachedVolumeControlEffect != nullptr) {
2538 uint32_t leftMax = 1 << 24;
2539 uint32_t rightMax = 1 << 24;
2540 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2541 }
2542 if (volumeControlEffect != nullptr) {
2543 uint32_t leftZero = 0;
2544 uint32_t rightZero = 0;
2545 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2546 }
2547 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002548 mLeftVolume = newLeft;
2549 mRightVolume = newRight;
2550
2551 // second get volume update from volume controller
2552 if (ctrlIdx >= 0) {
2553 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2554 mNewLeftVolume = newLeft;
2555 mNewRightVolume = newRight;
2556 }
2557 // then indicate volume to all other effects in chain.
2558 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002559 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002560 uint32_t lVol = newLeft;
2561 uint32_t rVol = newRight;
2562
2563 for (size_t i = 0; i < size; i++) {
2564 if ((int)i == ctrlIdx) {
2565 continue;
2566 }
2567 // this also works for ctrlIdx == -1 when there is no volume controller
2568 if ((int)i > ctrlIdx) {
2569 lVol = *left;
2570 rVol = *right;
2571 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002572 // Pass requested volume directly if this is volume monitor module
2573 if (mEffects[i]->isVolumeMonitor()) {
2574 mEffects[i]->setVolume(left, right, false);
2575 } else {
2576 mEffects[i]->setVolume(&lVol, &rVol, false);
2577 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002578 }
2579 *left = newLeft;
2580 *right = newRight;
2581
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002582 setVolumeForOutput_l(*left, *right);
2583
jiabineb5784e2023-08-24 21:10:44 +00002584 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002585}
2586
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002587// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002588void AudioFlinger::EffectChain::resetVolume_l()
2589{
Eric Laurente7449bf2016-08-03 18:44:07 -07002590 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2591 uint32_t left = mLeftVolume;
2592 uint32_t right = mRightVolume;
2593 (void)setVolume_l(&left, &right, true);
2594 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002595}
2596
jiabineb3bda02020-06-30 14:07:03 -07002597// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2598bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2599{
2600 for (size_t i = 0; i < mEffects.size(); ++i) {
2601 if (mEffects[i]->isHapticGenerator()) {
2602 return true;
2603 }
2604 }
2605 return false;
2606}
2607
jiabine70bc7f2020-06-30 22:07:55 -07002608void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2609{
2610 Mutex::Autolock _l(mLock);
2611 for (size_t i = 0; i < mEffects.size(); ++i) {
2612 mEffects[i]->setHapticIntensity(id, intensity);
2613 }
2614}
2615
Eric Laurent1b928682014-10-02 19:41:47 -07002616void AudioFlinger::EffectChain::syncHalEffectsState()
2617{
2618 Mutex::Autolock _l(mLock);
2619 for (size_t i = 0; i < mEffects.size(); i++) {
2620 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2621 mEffects[i]->state() == EffectModule::STOPPING) {
2622 mEffects[i]->addEffectToHal_l();
2623 }
2624 }
2625}
2626
Eric Laurentca7cc822012-11-19 14:55:58 -08002627void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07002628NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002629{
Eric Laurentca7cc822012-11-19 14:55:58 -08002630 String8 result;
2631
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002632 const size_t numEffects = mEffects.size();
2633 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002634
Marco Nelissenb2208842014-02-07 14:00:50 -08002635 if (numEffects) {
2636 bool locked = AudioFlinger::dumpTryLock(mLock);
2637 // failed to lock - AudioFlinger is probably deadlocked
2638 if (!locked) {
2639 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002640 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002641
Andy Hungbded9c82017-11-30 18:47:35 -08002642 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2643 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2644 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2645 (int)inBufferStr.size(), "In buffer ",
2646 (int)outBufferStr.size(), "Out buffer ");
2647 result.appendFormat("\t%s %s %d\n",
2648 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002649 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002650
2651 for (size_t i = 0; i < numEffects; ++i) {
2652 sp<EffectModule> effect = mEffects[i];
2653 if (effect != 0) {
2654 effect->dump(fd, args);
2655 }
2656 }
2657
2658 if (locked) {
2659 mLock.unlock();
2660 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002661 } else {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002662 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002663 }
2664}
2665
2666// must be called with ThreadBase::mLock held
2667void AudioFlinger::EffectChain::setEffectSuspended_l(
2668 const effect_uuid_t *type, bool suspend)
2669{
2670 sp<SuspendedEffectDesc> desc;
2671 // use effect type UUID timelow as key as there is no real risk of identical
2672 // timeLow fields among effect type UUIDs.
2673 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2674 if (suspend) {
2675 if (index >= 0) {
2676 desc = mSuspendedEffects.valueAt(index);
2677 } else {
2678 desc = new SuspendedEffectDesc();
2679 desc->mType = *type;
2680 mSuspendedEffects.add(type->timeLow, desc);
2681 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2682 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002683
Eric Laurentca7cc822012-11-19 14:55:58 -08002684 if (desc->mRefCount++ == 0) {
2685 sp<EffectModule> effect = getEffectIfEnabled(type);
2686 if (effect != 0) {
2687 desc->mEffect = effect;
2688 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002689 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002690 }
2691 }
2692 } else {
2693 if (index < 0) {
2694 return;
2695 }
2696 desc = mSuspendedEffects.valueAt(index);
2697 if (desc->mRefCount <= 0) {
2698 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002699 desc->mRefCount = 0;
2700 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002701 }
2702 if (--desc->mRefCount == 0) {
2703 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2704 if (desc->mEffect != 0) {
2705 sp<EffectModule> effect = desc->mEffect.promote();
2706 if (effect != 0) {
2707 effect->setSuspended(false);
2708 effect->lock();
2709 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002710 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002711 effect->setEnabled_l(handle->enabled());
2712 }
2713 effect->unlock();
2714 }
2715 desc->mEffect.clear();
2716 }
2717 mSuspendedEffects.removeItemsAt(index);
2718 }
2719 }
2720}
2721
2722// must be called with ThreadBase::mLock held
2723void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2724{
2725 sp<SuspendedEffectDesc> desc;
2726
2727 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2728 if (suspend) {
2729 if (index >= 0) {
2730 desc = mSuspendedEffects.valueAt(index);
2731 } else {
2732 desc = new SuspendedEffectDesc();
2733 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2734 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2735 }
2736 if (desc->mRefCount++ == 0) {
2737 Vector< sp<EffectModule> > effects;
2738 getSuspendEligibleEffects(effects);
2739 for (size_t i = 0; i < effects.size(); i++) {
2740 setEffectSuspended_l(&effects[i]->desc().type, true);
2741 }
2742 }
2743 } else {
2744 if (index < 0) {
2745 return;
2746 }
2747 desc = mSuspendedEffects.valueAt(index);
2748 if (desc->mRefCount <= 0) {
2749 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2750 desc->mRefCount = 1;
2751 }
2752 if (--desc->mRefCount == 0) {
2753 Vector<const effect_uuid_t *> types;
2754 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2755 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2756 continue;
2757 }
2758 types.add(&mSuspendedEffects.valueAt(i)->mType);
2759 }
2760 for (size_t i = 0; i < types.size(); i++) {
2761 setEffectSuspended_l(types[i], false);
2762 }
2763 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2764 mSuspendedEffects.keyAt(index));
2765 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2766 }
2767 }
2768}
2769
2770
2771// The volume effect is used for automated tests only
2772#ifndef OPENSL_ES_H_
2773static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2774 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2775const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2776#endif //OPENSL_ES_H_
2777
Eric Laurentd8365c52017-07-16 15:27:05 -07002778/* static */
2779bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2780{
2781 // Only NS and AEC are suspended when BtNRec is off
2782 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2783 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2784 return true;
2785 }
2786 return false;
2787}
2788
Eric Laurentca7cc822012-11-19 14:55:58 -08002789bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2790{
2791 // auxiliary effects and visualizer are never suspended on output mix
2792 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2793 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2794 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002795 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2796 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002797 return false;
2798 }
2799 return true;
2800}
2801
2802void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2803 Vector< sp<AudioFlinger::EffectModule> > &effects)
2804{
2805 effects.clear();
2806 for (size_t i = 0; i < mEffects.size(); i++) {
2807 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2808 effects.add(mEffects[i]);
2809 }
2810 }
2811}
2812
2813sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2814 const effect_uuid_t *type)
2815{
2816 sp<EffectModule> effect = getEffectFromType_l(type);
2817 return effect != 0 && effect->isEnabled() ? effect : 0;
2818}
2819
2820void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2821 bool enabled)
2822{
2823 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2824 if (enabled) {
2825 if (index < 0) {
2826 // if the effect is not suspend check if all effects are suspended
2827 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2828 if (index < 0) {
2829 return;
2830 }
2831 if (!isEffectEligibleForSuspend(effect->desc())) {
2832 return;
2833 }
2834 setEffectSuspended_l(&effect->desc().type, enabled);
2835 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2836 if (index < 0) {
2837 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2838 return;
2839 }
2840 }
2841 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2842 effect->desc().type.timeLow);
2843 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002844 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002845 if (desc->mEffect == 0) {
2846 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002847 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002848 effect->setSuspended(true);
2849 }
2850 } else {
2851 if (index < 0) {
2852 return;
2853 }
2854 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2855 effect->desc().type.timeLow);
2856 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2857 desc->mEffect.clear();
2858 effect->setSuspended(false);
2859 }
2860}
2861
Eric Laurent5baf2af2013-09-12 17:37:00 -07002862bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002863{
2864 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002865 return isNonOffloadableEnabled_l();
2866}
2867
2868bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2869{
Eric Laurent813e2a72013-08-31 12:59:48 -07002870 size_t size = mEffects.size();
2871 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002872 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002873 return true;
2874 }
2875 }
2876 return false;
2877}
2878
Eric Laurentaaa44472014-09-12 17:41:50 -07002879void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2880{
2881 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002882 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002883}
2884
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002885void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2886{
2887 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2888 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2889 }
2890 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2891 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2892 }
2893}
2894
2895void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2896{
2897 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2898 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2899 }
2900 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2901 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2902 }
2903}
2904
2905bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002906{
2907 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002908 for (const auto &effect : mEffects) {
2909 if (effect->isProcessImplemented()) {
2910 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002911 }
2912 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002913 // Allow effects without processing.
2914 return true;
2915}
2916
2917bool AudioFlinger::EffectChain::isFastCompatible() const
2918{
2919 Mutex::Autolock _l(mLock);
2920 for (const auto &effect : mEffects) {
2921 if (effect->isProcessImplemented()
2922 && effect->isImplementationSoftware()) {
2923 return false;
2924 }
2925 }
2926 // Allow effects without processing or hw accelerated effects.
2927 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002928}
2929
2930// isCompatibleWithThread_l() must be called with thread->mLock held
2931bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2932{
2933 Mutex::Autolock _l(mLock);
2934 for (size_t i = 0; i < mEffects.size(); i++) {
2935 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2936 return false;
2937 }
2938 }
2939 return true;
2940}
2941
Eric Laurent6b446ce2019-12-13 10:56:31 -08002942// EffectCallbackInterface implementation
2943status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2944 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2945 sp<EffectHalInterface> *effect) {
2946 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002947 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002948 if (effectsFactory != 0) {
2949 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2950 }
2951 return status;
2952}
2953
2954bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002955 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002956 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002957 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002958}
2959
2960status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2961 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002962 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002963}
2964
2965status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07002966 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002967 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002968 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002969 if (t == nullptr) {
2970 return result;
2971 }
2972 sp <StreamHalInterface> st = t->stream();
2973 if (st == nullptr) {
2974 return result;
2975 }
2976 result = st->addEffect(effect);
2977 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2978 return result;
2979}
2980
2981status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07002982 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002983 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002984 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002985 if (t == nullptr) {
2986 return result;
2987 }
2988 sp <StreamHalInterface> st = t->stream();
2989 if (st == nullptr) {
2990 return result;
2991 }
2992 result = st->removeEffect(effect);
2993 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2994 return result;
2995}
2996
2997audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002998 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002999 if (t == nullptr) {
3000 return AUDIO_IO_HANDLE_NONE;
3001 }
3002 return t->id();
3003}
3004
3005bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003006 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003007 if (t == nullptr) {
3008 return true;
3009 }
3010 return t->isOutput();
3011}
3012
3013bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003014 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003015}
3016
3017bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003018 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003019}
3020
3021bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003022 switch (mThreadType) {
3023 case ThreadBase::OFFLOAD:
3024 case ThreadBase::MMAP_PLAYBACK:
3025 case ThreadBase::MMAP_CAPTURE:
3026 return true;
3027 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003028 return false;
3029 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003030}
3031
3032bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3033 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003034}
3035
3036uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003037 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003038 if (t == nullptr) {
3039 return 0;
3040 }
3041 return t->sampleRate();
3042}
3043
Eric Laurentf1f22e72021-07-13 14:04:14 +02003044audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3045 sp<ThreadBase> t = thread().promote();
3046 if (t == nullptr) {
3047 return AUDIO_CHANNEL_NONE;
3048 }
3049 sp<EffectChain> c = chain().promote();
3050 if (c == nullptr) {
3051 return AUDIO_CHANNEL_NONE;
3052 }
3053
Eric Laurentb62d0362021-10-26 17:40:18 +02003054 if (mThreadType == ThreadBase::SPATIALIZER) {
3055 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3056 if (c->isFirstEffect(id)) {
3057 return t->mixerChannelMask();
3058 } else {
3059 return t->channelMask();
3060 }
3061 } else if (!audio_is_global_session(c->sessionId())) {
3062 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3063 return t->mixerChannelMask();
3064 } else {
3065 return t->channelMask();
3066 }
3067 } else {
3068 return t->channelMask();
3069 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003070 } else {
3071 return t->channelMask();
3072 }
3073}
3074
3075uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003076 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003077}
3078
3079audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003080 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003081 if (t == nullptr) {
3082 return AUDIO_CHANNEL_NONE;
3083 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003084 sp<EffectChain> c = chain().promote();
3085 if (c == nullptr) {
3086 return AUDIO_CHANNEL_NONE;
3087 }
3088
3089 if (mThreadType == ThreadBase::SPATIALIZER) {
3090 if (!audio_is_global_session(c->sessionId())) {
3091 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3092 return t->mixerChannelMask();
3093 } else {
3094 return t->channelMask();
3095 }
3096 } else {
3097 return t->channelMask();
3098 }
3099 } else {
3100 return t->channelMask();
3101 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003102}
3103
Eric Laurentf1f22e72021-07-13 14:04:14 +02003104uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003105 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003106}
3107
jiabineb3bda02020-06-30 14:07:03 -07003108audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003109 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003110 if (t == nullptr) {
3111 return AUDIO_CHANNEL_NONE;
3112 }
3113 return t->hapticChannelMask();
3114}
3115
Eric Laurent6b446ce2019-12-13 10:56:31 -08003116size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003117 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003118 if (t == nullptr) {
3119 return 0;
3120 }
3121 return t->frameCount();
3122}
3123
Andy Hung71ba4b32022-10-06 12:09:49 -07003124uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3125NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3126{
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 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003131 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003132 return t->latency_l();
3133}
3134
Andy Hung71ba4b32022-10-06 12:09:49 -07003135void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3136NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3137{
Andy Hung328d6772021-01-12 12:32:21 -08003138 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003139 if (t == nullptr) {
3140 return;
3141 }
3142 t->setVolumeForOutput_l(left, right);
3143}
3144
3145void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003146 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003147 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003148 if (t == nullptr) {
3149 return;
3150 }
3151 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3152
Andy Hung328d6772021-01-12 12:32:21 -08003153 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003154 if (c == nullptr) {
3155 return;
3156 }
Eric Laurent41709552019-12-16 19:34:05 -08003157 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3158 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003159}
3160
Eric Laurent41709552019-12-16 19:34:05 -08003161void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003162 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003163 if (t == nullptr) {
3164 return;
3165 }
Eric Laurent41709552019-12-16 19:34:05 -08003166 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3167 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003168}
3169
Eric Laurent41709552019-12-16 19:34:05 -08003170void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3172
Andy Hung328d6772021-01-12 12:32:21 -08003173 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003174 if (t == nullptr) {
3175 return;
3176 }
3177 t->onEffectDisable();
3178}
3179
3180bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3181 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003182 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003183 if (t == nullptr) {
3184 return false;
3185 }
3186 t->disconnectEffectHandle(handle, unpinIfLast);
3187 return true;
3188}
3189
3190void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003191 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003192 if (c == nullptr) {
3193 return;
3194 }
3195 c->resetVolume_l();
3196
3197}
3198
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003199product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003200 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003201 if (c == nullptr) {
3202 return PRODUCT_STRATEGY_NONE;
3203 }
3204 return c->strategy();
3205}
3206
3207int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003208 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003209 if (c == nullptr) {
3210 return 0;
3211 }
3212 return c->activeTrackCnt();
3213}
3214
Eric Laurentb82e6b72019-11-22 17:25:04 -08003215
3216#undef LOG_TAG
3217#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3218
3219status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3220{
3221 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3222 Mutex::Autolock _l(mProxyLock);
3223 if (status == NO_ERROR) {
3224 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003225 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003226 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003227 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003228 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003229 bs = handle.second->disable(&status);
3230 }
3231 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003232 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003233 }
3234 }
3235 }
3236 ALOGV("%s enable %d status %d", __func__, enabled, status);
3237 return status;
3238}
3239
3240status_t AudioFlinger::DeviceEffectProxy::init(
3241 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3242//For all audio patches
3243//If src or sink device match
3244//If the effect is HW accelerated
3245// if no corresponding effect module
3246// Create EffectModule: mHalEffect
3247//Create and attach EffectHandle
3248//If the effect is not HW accelerated and the patch sink or src is a mixer port
3249// Create Effect on patch input or output thread on session -1
3250//Add EffectHandle to EffectHandle map of Effect Proxy:
3251 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3252 status_t status = NO_ERROR;
3253 for (auto &patch : patches) {
3254 status = onCreatePatch(patch.first, patch.second);
3255 ALOGV("%s onCreatePatch status %d", __func__, status);
3256 if (status == BAD_VALUE) {
3257 return status;
3258 }
3259 }
3260 return status;
3261}
3262
3263status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3264 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3265 status_t status = NAME_NOT_FOUND;
3266 sp<EffectHandle> handle;
3267 // only consider source[0] as this is the only "true" source of a patch
3268 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3269 ALOGV("%s source checkPort status %d", __func__, status);
3270 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3271 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3272 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3273 }
3274 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3275 Mutex::Autolock _l(mProxyLock);
3276 mEffectHandles.emplace(patchHandle, handle);
3277 }
3278 ALOGW_IF(status == BAD_VALUE,
3279 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3280
3281 return status;
3282}
3283
3284status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3285 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3286
3287 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3288 __func__, port->type, port->ext.device.type,
3289 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003290 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3291 port->ext.device.address != mDevice.address()) {
3292 return NAME_NOT_FOUND;
3293 }
3294 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3295 (audio_port_config_has_input_direction(port))) {
3296 ALOGI("%s don't create postprocessing effect on record port", __func__);
3297 return NAME_NOT_FOUND;
3298 }
3299 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3300 (!audio_port_config_has_input_direction(port))) {
3301 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003302 return NAME_NOT_FOUND;
3303 }
3304 status_t status = NAME_NOT_FOUND;
3305
3306 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3307 Mutex::Autolock _l(mProxyLock);
3308 mDevicePort = *port;
3309 mHalEffect = new EffectModule(mMyCallback,
3310 const_cast<effect_descriptor_t *>(&mDescriptor),
3311 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3312 false /* pinned */, port->id);
3313 if (audio_is_input_device(mDevice.mType)) {
3314 mHalEffect->setInputDevice(mDevice);
3315 } else {
3316 mHalEffect->setDevices({mDevice});
3317 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003318 mHalEffect->configure();
3319
Eric Laurentde8caf42021-08-11 17:19:25 +02003320 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3321 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003322 status = (*handle)->initCheck();
3323 if (status == OK) {
3324 status = mHalEffect->addHandle((*handle).get());
3325 } else {
3326 mHalEffect.clear();
3327 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3328 }
3329 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3330 sp <ThreadBase> thread;
3331 if (audio_port_config_has_input_direction(port)) {
3332 if (patch.isSoftware()) {
3333 thread = patch.mRecord.thread();
3334 } else {
3335 thread = patch.thread().promote();
3336 }
3337 } else {
3338 if (patch.isSoftware()) {
3339 thread = patch.mPlayback.thread();
3340 } else {
3341 thread = patch.thread().promote();
3342 }
3343 }
3344 int enabled;
3345 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3346 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003347 &enabled, &status, false, false /*probe*/,
3348 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003349 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3350 } else {
3351 status = BAD_VALUE;
3352 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003353
Eric Laurentb82e6b72019-11-22 17:25:04 -08003354 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003355 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003356 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003357 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003358 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003359 bs = (*handle)->disable(&status);
3360 }
3361 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003362 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003363 }
3364 }
3365 return status;
3366}
3367
3368void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003369 sp<EffectHandle> effect;
3370 {
3371 Mutex::Autolock _l(mProxyLock);
3372 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3373 effect = mEffectHandles.at(patchHandle);
3374 mEffectHandles.erase(patchHandle);
3375 }
3376 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003377}
3378
3379
3380size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3381{
3382 Mutex::Autolock _l(mProxyLock);
3383 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003384 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003385 mHalEffect.clear();
3386 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3387 }
3388 return mHalEffect == nullptr ? 0 : 1;
3389}
3390
3391status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003392 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003393 if (mHalEffect == nullptr) {
3394 return NO_INIT;
3395 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003396 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003397}
3398
3399status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003400 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003401 if (mHalEffect == nullptr) {
3402 return NO_INIT;
3403 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003404 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003405}
3406
3407bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3408 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3409 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3410 }
3411 return true;
3412}
3413
3414uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3415 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3416 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3417 return mDevicePort.sample_rate;
3418 }
3419 return DEFAULT_OUTPUT_SAMPLE_RATE;
3420}
3421
3422audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3423 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3424 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3425 return mDevicePort.channel_mask;
3426 }
3427 return AUDIO_CHANNEL_OUT_STEREO;
3428}
3429
3430uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3431 if (isOutput()) {
3432 return audio_channel_count_from_out_mask(channelMask());
3433 }
3434 return audio_channel_count_from_in_mask(channelMask());
3435}
3436
Andy Hung71ba4b32022-10-06 12:09:49 -07003437void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3438NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3439{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003440 const Vector<String16> args;
3441 EffectBase::dump(fd, args);
3442
3443 const bool locked = dumpTryLock(mProxyLock);
3444 if (!locked) {
3445 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003446 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003447 }
3448
3449 String8 outStr;
3450 if (mHalEffect != nullptr) {
3451 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3452 } else {
3453 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3454 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003455 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003456 outStr.clear();
3457
3458 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003459 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003460 outStr.clear();
3461
3462 for (const auto& iter : mEffectHandles) {
3463 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003464 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003465 outStr.clear();
3466 sp<EffectBase> effect = iter.second->effect().promote();
3467 if (effect != nullptr) {
3468 effect->dump(fd, args);
3469 }
3470 }
3471
3472 if (locked) {
3473 mLock.unlock();
3474 }
3475}
3476
3477#undef LOG_TAG
3478#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3479
3480int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3481 return mManagerCallback->newEffectId();
3482}
3483
3484
3485bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3486 EffectHandle *handle, bool unpinIfLast) {
3487 sp<EffectBase> effectBase = handle->effect().promote();
3488 if (effectBase == nullptr) {
3489 return false;
3490 }
3491
3492 sp<EffectModule> effect = effectBase->asEffectModule();
3493 if (effect == nullptr) {
3494 return false;
3495 }
3496
3497 // restore suspended effects if the disconnected handle was enabled and the last one.
3498 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3499 if (remove) {
3500 sp<DeviceEffectProxy> proxy = mProxy.promote();
3501 if (proxy != nullptr) {
3502 proxy->removeEffect(effect);
3503 }
3504 if (handle->enabled()) {
3505 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3506 }
3507 }
3508 return true;
3509}
3510
3511status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3512 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3513 sp<EffectHalInterface> *effect) {
3514 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3515}
3516
3517status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003518 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003519 sp<DeviceEffectProxy> proxy = mProxy.promote();
3520 if (proxy == nullptr) {
3521 return NO_INIT;
3522 }
3523 return proxy->addEffectToHal(effect);
3524}
3525
3526status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003527 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003528 sp<DeviceEffectProxy> proxy = mProxy.promote();
3529 if (proxy == nullptr) {
3530 return NO_INIT;
3531 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003532 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003533}
3534
3535bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3536 sp<DeviceEffectProxy> proxy = mProxy.promote();
3537 if (proxy == nullptr) {
3538 return true;
3539 }
3540 return proxy->isOutput();
3541}
3542
3543uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3544 sp<DeviceEffectProxy> proxy = mProxy.promote();
3545 if (proxy == nullptr) {
3546 return DEFAULT_OUTPUT_SAMPLE_RATE;
3547 }
3548 return proxy->sampleRate();
3549}
3550
Eric Laurentf1f22e72021-07-13 14:04:14 +02003551audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3552 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003553 sp<DeviceEffectProxy> proxy = mProxy.promote();
3554 if (proxy == nullptr) {
3555 return AUDIO_CHANNEL_OUT_STEREO;
3556 }
3557 return proxy->channelMask();
3558}
3559
Eric Laurentf1f22e72021-07-13 14:04:14 +02003560uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3561 sp<DeviceEffectProxy> proxy = mProxy.promote();
3562 if (proxy == nullptr) {
3563 return 2;
3564 }
3565 return proxy->channelCount();
3566}
3567
3568audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3569 sp<DeviceEffectProxy> proxy = mProxy.promote();
3570 if (proxy == nullptr) {
3571 return AUDIO_CHANNEL_OUT_STEREO;
3572 }
3573 return proxy->channelMask();
3574}
3575
3576uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003577 sp<DeviceEffectProxy> proxy = mProxy.promote();
3578 if (proxy == nullptr) {
3579 return 2;
3580 }
3581 return proxy->channelCount();
3582}
3583
Eric Laurent76c89f32021-12-03 17:13:23 +01003584void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3585 const sp<EffectBase>& effectBase) {
3586 sp<EffectModule> effect = effectBase->asEffectModule();
3587 if (effect == nullptr) {
3588 return;
3589 }
3590 effect->start();
3591}
3592
3593void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3594 const sp<EffectBase>& effectBase) {
3595 sp<EffectModule> effect = effectBase->asEffectModule();
3596 if (effect == nullptr) {
3597 return;
3598 }
3599 effect->stop();
3600}
3601
Glenn Kasten63238ef2015-03-02 15:50:29 -08003602} // namespace android