blob: 19e4151bb7225d26f227da28e7b8f67cb86ed597 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080047
48// ----------------------------------------------------------------------------
49
50// Note: the following macro is used for extremely verbose logging message. In
51// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
52// 0; but one side effect of this is to turn all LOGV's as well. Some messages
53// are so verbose that we want to suppress them even when we have ALOG_ASSERT
54// turned on. Do not uncomment the #def below unless you really know what you
55// are doing and want to see all of the extremely verbose messages.
56//#define VERY_VERY_VERBOSE_LOGGING
57#ifdef VERY_VERY_VERBOSE_LOGGING
58#define ALOGVV ALOGV
59#else
60#define ALOGVV(a...) do { } while(0)
61#endif
62
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090063#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
64
Eric Laurentca7cc822012-11-19 14:55:58 -080065namespace android {
66
Andy Hung1131b6e2020-12-08 20:47:45 -080067using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070068using binder::Status;
69
70namespace {
71
72// Append a POD value into a vector of bytes.
73template<typename T>
74void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
75 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
76 buffer->insert(buffer->end(), ar, ar + sizeof(T));
77}
78
79// Write a POD value into a vector of bytes (clears the previous buffer
80// content).
81template<typename T>
82void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
83 buffer->clear();
84 appendToBuffer(value, buffer);
85}
86
87} // namespace
88
Eric Laurentca7cc822012-11-19 14:55:58 -080089// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080090// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
92
93#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080094#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080095
Eric Laurent41709552019-12-16 19:34:05 -080096AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080097 effect_descriptor_t *desc,
98 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 audio_session_t sessionId,
100 bool pinned)
101 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800102 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800103 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurentca7cc822012-11-19 14:55:58 -0800105}
106
Eric Laurent41709552019-12-16 19:34:05 -0800107// must be called with EffectModule::mLock held
108status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800109{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurent41709552019-12-16 19:34:05 -0800111 ALOGV("setEnabled %p enabled %d", this, enabled);
112
113 if (enabled != isEnabled()) {
114 switch (mState) {
115 // going from disabled to enabled
116 case IDLE:
117 mState = STARTING;
118 break;
119 case STOPPED:
120 mState = RESTART;
121 break;
122 case STOPPING:
123 mState = ACTIVE;
124 break;
125
126 // going from enabled to disabled
127 case RESTART:
128 mState = STOPPED;
129 break;
130 case STARTING:
131 mState = IDLE;
132 break;
133 case ACTIVE:
134 mState = STOPPING;
135 break;
136 case DESTROYED:
137 return NO_ERROR; // simply ignore as we are being destroyed
138 }
139 for (size_t i = 1; i < mHandles.size(); i++) {
140 EffectHandle *h = mHandles[i];
141 if (h != NULL && !h->disconnected()) {
142 h->setEnabled(enabled);
143 }
144 }
145 }
146 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800147}
148
Eric Laurent41709552019-12-16 19:34:05 -0800149status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
150{
151 status_t status;
152 {
153 Mutex::Autolock _l(mLock);
154 status = setEnabled_l(enabled);
155 }
156 if (fromHandle) {
157 if (enabled) {
158 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700159 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800160 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800162 }
163 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800165 }
166 }
167 return status;
168}
169
170bool AudioFlinger::EffectBase::isEnabled() const
171{
172 switch (mState) {
173 case RESTART:
174 case STARTING:
175 case ACTIVE:
176 return true;
177 case IDLE:
178 case STOPPING:
179 case STOPPED:
180 case DESTROYED:
181 default:
182 return false;
183 }
184}
185
186void AudioFlinger::EffectBase::setSuspended(bool suspended)
187{
188 Mutex::Autolock _l(mLock);
189 mSuspended = suspended;
190}
191
192bool AudioFlinger::EffectBase::suspended() const
193{
194 Mutex::Autolock _l(mLock);
195 return mSuspended;
196}
197
198status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800199{
200 status_t status;
201
202 Mutex::Autolock _l(mLock);
203 int priority = handle->priority();
204 size_t size = mHandles.size();
205 EffectHandle *controlHandle = NULL;
206 size_t i;
207 for (i = 0; i < size; i++) {
208 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800209 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 continue;
211 }
212 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 if (h->priority() <= priority) {
217 break;
218 }
219 }
220 // if inserted in first place, move effect control from previous owner to this handle
221 if (i == 0) {
222 bool enabled = false;
223 if (controlHandle != NULL) {
224 enabled = controlHandle->enabled();
225 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
226 }
227 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
228 status = NO_ERROR;
229 } else {
230 status = ALREADY_EXISTS;
231 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700232 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800233 mHandles.insertAt(handle, i);
234 return status;
235}
236
Eric Laurent41709552019-12-16 19:34:05 -0800237status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700238{
239 status_t status = NO_ERROR;
240 bool doRegister = false;
241 bool registered = false;
242 bool doEnable = false;
243 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700244 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800245 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700246
247 {
248 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200249
250 if ((isInternal_l() && !mPolicyRegistered)
251 || !getCallback()->isAudioPolicyReady()) {
252 return NO_ERROR;
253 }
254
Eric Laurent6c796322019-04-09 14:13:17 -0700255 // register effect when first handle is attached and unregister when last handle is removed
256 if (mPolicyRegistered != mHandles.size() > 0) {
257 doRegister = true;
258 mPolicyRegistered = mHandles.size() > 0;
259 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700260 const auto callback = getCallback();
261 io = callback->io();
262 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700263 }
264 }
265 // enable effect when registered according to enable state requested by controlling handle
266 if (mHandles.size() > 0) {
267 EffectHandle *handle = controlHandle_l();
268 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
269 doEnable = true;
270 mPolicyEnabled = handle->enabled();
271 }
272 }
273 registered = mPolicyRegistered;
274 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100275 // The simultaneous release of two EffectHandles with the same EffectModule
276 // may cause us to call this method at the same time.
277 // This may deadlock under some circumstances (b/180941720). Avoid this.
278 if (!doRegister && !(registered && doEnable)) {
279 return NO_ERROR;
280 }
Eric Laurent6c796322019-04-09 14:13:17 -0700281 }
fengjnlan46407562022-09-07 16:20:01 +0800282 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700283 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
284 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
285 if (doRegister) {
286 if (registered) {
287 status = AudioSystem::registerEffect(
288 &mDescriptor,
289 io,
290 strategy,
291 mSessionId,
292 mId);
293 } else {
294 status = AudioSystem::unregisterEffect(mId);
295 }
296 }
297 if (registered && doEnable) {
298 status = AudioSystem::setEffectEnabled(mId, enabled);
299 }
300 mPolicyLock.unlock();
301
302 return status;
303}
304
305
Eric Laurent41709552019-12-16 19:34:05 -0800306ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800307{
308 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309 return removeHandle_l(handle);
310}
311
Eric Laurent41709552019-12-16 19:34:05 -0800312ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800313{
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 size_t size = mHandles.size();
315 size_t i;
316 for (i = 0; i < size; i++) {
317 if (mHandles[i] == handle) {
318 break;
319 }
320 }
321 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800322 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
323 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800324 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800325 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800326
327 mHandles.removeAt(i);
328 // if removed from first place, move effect control from this handle to next in line
329 if (i == 0) {
330 EffectHandle *h = controlHandle_l();
331 if (h != NULL) {
332 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
333 }
334 }
335
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530336 // Prevent calls to process() and other functions on effect interface from now on.
337 // The effect engine will be released by the destructor when the last strong reference on
338 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 if (mHandles.size() == 0 && !mPinned) {
340 mState = DESTROYED;
341 }
342
343 return mHandles.size();
344}
345
346// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800347AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800348{
349 // the first valid handle in the list has control over the module
350 for (size_t i = 0; i < mHandles.size(); i++) {
351 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800352 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 return h;
354 }
355 }
356
357 return NULL;
358}
359
Eric Laurentf10c7092016-12-06 17:09:56 -0800360// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800361ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800362{
Andy Hungfda44002021-06-03 17:23:16 -0700363 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800364 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700365 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800366 return mHandles.size();
367 }
368
Eric Laurentf10c7092016-12-06 17:09:56 -0800369 Mutex::Autolock _l(mLock);
370 ssize_t numHandles = removeHandle_l(handle);
371 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800372 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700373 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800375 }
376 return numHandles;
377}
378
Eric Laurent41709552019-12-16 19:34:05 -0800379bool AudioFlinger::EffectBase::purgeHandles()
380{
381 bool enabled = false;
382 Mutex::Autolock _l(mLock);
383 EffectHandle *handle = controlHandle_l();
384 if (handle != NULL) {
385 enabled = handle->enabled();
386 }
387 mHandles.clear();
388 return enabled;
389}
390
391void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700392 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800393}
394
395static String8 effectFlagsToString(uint32_t flags) {
396 String8 s;
397
398 s.append("conn. mode: ");
399 switch (flags & EFFECT_FLAG_TYPE_MASK) {
400 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
401 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
402 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
403 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
404 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("insert pref: ");
410 switch (flags & EFFECT_FLAG_INSERT_MASK) {
411 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
412 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
413 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
414 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 s.append("volume mgmt: ");
420 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
421 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
422 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
423 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
424 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
425 default: s.append("unknown/reserved"); break;
426 }
427 s.append(", ");
428
429 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
430 if (devind) {
431 s.append("device indication: ");
432 switch (devind) {
433 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
434 default: s.append("unknown/reserved"); break;
435 }
436 s.append(", ");
437 }
438
439 s.append("input mode: ");
440 switch (flags & EFFECT_FLAG_INPUT_MASK) {
441 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
442 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
443 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
444 default: s.append("not set"); break;
445 }
446 s.append(", ");
447
448 s.append("output mode: ");
449 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
450 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
451 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
452 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
453 default: s.append("not set"); break;
454 }
455 s.append(", ");
456
457 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
458 if (accel) {
459 s.append("hardware acceleration: ");
460 switch (accel) {
461 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
462 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
469 if (modeind) {
470 s.append("mode indication: ");
471 switch (modeind) {
472 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
479 if (srcind) {
480 s.append("source indication: ");
481 switch (srcind) {
482 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
483 default: s.append("unknown/reserved"); break;
484 }
485 s.append(", ");
486 }
487
488 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
489 s.append("offloadable, ");
490 }
491
492 int len = s.length();
493 if (s.length() > 2) {
494 (void) s.lockBuffer(len);
495 s.unlockBuffer(len - 2);
496 }
497 return s;
498}
499
500void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
Andy Hung920f6572022-10-06 12:09:49 -0700501NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800502{
503 String8 result;
504
505 result.appendFormat("\tEffect ID %d:\n", mId);
506
507 bool locked = AudioFlinger::dumpTryLock(mLock);
508 // failed to lock - AudioFlinger is probably deadlocked
509 if (!locked) {
510 result.append("\t\tCould not lock Fx mutex:\n");
511 }
512
513 result.append("\t\tSession State Registered Enabled Suspended:\n");
514 result.appendFormat("\t\t%05d %03d %s %s %s\n",
515 mSessionId, mState, mPolicyRegistered ? "y" : "n",
516 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
517
518 result.append("\t\tDescriptor:\n");
519 char uuidStr[64];
520 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
521 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
522 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
523 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
524 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
525 mDescriptor.apiVersion,
526 mDescriptor.flags,
527 effectFlagsToString(mDescriptor.flags).string());
528 result.appendFormat("\t\t- name: %s\n",
529 mDescriptor.name);
530
531 result.appendFormat("\t\t- implementor: %s\n",
532 mDescriptor.implementor);
533
534 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
535 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
536 char buffer[256];
537 for (size_t i = 0; i < mHandles.size(); ++i) {
538 EffectHandle *handle = mHandles[i];
539 if (handle != NULL && !handle->disconnected()) {
540 handle->dumpToBuffer(buffer, sizeof(buffer));
541 result.append(buffer);
542 }
543 }
544 if (locked) {
545 mLock.unlock();
546 }
547
548 write(fd, result.string(), result.length());
549}
550
551// ----------------------------------------------------------------------------
552// EffectModule implementation
553// ----------------------------------------------------------------------------
554
555#undef LOG_TAG
556#define LOG_TAG "AudioFlinger::EffectModule"
557
558AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
559 effect_descriptor_t *desc,
560 int id,
561 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800562 bool pinned,
563 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800564 : EffectBase(callback, desc, id, sessionId, pinned),
565 // clear mConfig to ensure consistent initial value of buffer framecount
566 // in case buffers are associated by setInBuffer() or setOutBuffer()
567 // prior to configure().
568 mConfig{{}, {}},
569 mStatus(NO_INIT),
570 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
571 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800572 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000573 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800574#ifdef FLOAT_EFFECT_CHAIN
575 , mSupportsFloat(false)
576#endif
577{
578 ALOGV("Constructor %p pinned %d", this, pinned);
579 int lStatus;
580
581 // create effect engine from effect factory
582 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800583 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800584 if (mStatus != NO_ERROR) {
585 return;
586 }
587 lStatus = init();
588 if (lStatus < 0) {
589 mStatus = lStatus;
590 goto Error;
591 }
592
593 setOffloaded(callback->isOffload(), callback->io());
594 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
595
596 return;
597Error:
598 mEffectInterface.clear();
599 ALOGV("Constructor Error %d", mStatus);
600}
601
602AudioFlinger::EffectModule::~EffectModule()
603{
604 ALOGV("Destructor %p", this);
605 if (mEffectInterface != 0) {
606 char uuidStr[64];
607 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
608 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
609 this, uuidStr);
610 release_l();
611 }
612
613}
614
Eric Laurentfa1e1232016-08-02 19:01:49 -0700615bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800616 Mutex::Autolock _l(mLock);
617
Eric Laurentfa1e1232016-08-02 19:01:49 -0700618 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 switch (mState) {
620 case RESTART:
621 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700622 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800623
624 case STARTING:
625 // clear auxiliary effect input buffer for next accumulation
626 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
627 memset(mConfig.inputCfg.buffer.raw,
628 0,
629 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
630 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 if (start_l() == NO_ERROR) {
632 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700633 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700634 } else {
635 mState = IDLE;
636 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800637 break;
638 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900639 // volume control for offload and direct threads must take effect immediately.
640 if (stop_l() == NO_ERROR
641 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700642 mDisableWaitCnt = mMaxDisableWaitCnt;
643 } else {
644 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
645 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 mState = STOPPED;
647 break;
648 case STOPPED:
649 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
650 // turn off sequence.
651 if (--mDisableWaitCnt == 0) {
652 reset_l();
653 mState = IDLE;
654 }
655 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200656 case ACTIVE:
657 for (size_t i = 0; i < mHandles.size(); i++) {
658 if (!mHandles[i]->disconnected()) {
659 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
660 }
661 }
662 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800663 default: //IDLE , ACTIVE, DESTROYED
664 break;
665 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700666
667 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800668}
669
670void AudioFlinger::EffectModule::process()
671{
672 Mutex::Autolock _l(mLock);
673
Mikhail Naganov022b9952017-01-04 16:36:51 -0800674 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 return;
676 }
677
rago94a1ee82017-07-21 15:11:02 -0700678 const uint32_t inChannelCount =
679 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
680 const uint32_t outChannelCount =
681 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
682 const bool auxType =
683 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
684
Andy Hungfa69ca32017-11-30 10:07:53 -0800685 // safeInputOutputSampleCount is 0 if the channel count between input and output
686 // buffers do not match. This prevents automatic accumulation or copying between the
687 // input and output effect buffers without an intermediary effect process.
688 // TODO: consider implementing channel conversion.
689 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700690 mInChannelCountRequested != mOutChannelCountRequested ? 0
691 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800692 mConfig.inputCfg.buffer.frameCount,
693 mConfig.outputCfg.buffer.frameCount);
694 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
695#ifdef FLOAT_EFFECT_CHAIN
696 accumulate_float(
697 mConfig.outputCfg.buffer.f32,
698 mConfig.inputCfg.buffer.f32,
699 safeInputOutputSampleCount);
700#else
701 accumulate_i16(
702 mConfig.outputCfg.buffer.s16,
703 mConfig.inputCfg.buffer.s16,
704 safeInputOutputSampleCount);
705#endif
706 };
707 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
708#ifdef FLOAT_EFFECT_CHAIN
709 memcpy(
710 mConfig.outputCfg.buffer.f32,
711 mConfig.inputCfg.buffer.f32,
712 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
713
714#else
715 memcpy(
716 mConfig.outputCfg.buffer.s16,
717 mConfig.inputCfg.buffer.s16,
718 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
719#endif
720 };
721
Eric Laurentca7cc822012-11-19 14:55:58 -0800722 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700723 int ret;
724 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700725 if (auxType) {
726 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800727 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700728#ifdef FLOAT_EFFECT_CHAIN
729 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800730#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700731 // Do in-place float conversion for auxiliary effect input buffer.
732 static_assert(sizeof(float) <= sizeof(int32_t),
733 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
734
Andy Hungfa69ca32017-11-30 10:07:53 -0800735 memcpy_to_float_from_q4_27(
736 mConfig.inputCfg.buffer.f32,
737 mConfig.inputCfg.buffer.s32,
738 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800739#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800740 } else
Andy Hung116a4982017-11-30 10:15:08 -0800741#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 {
Andy Hung116a4982017-11-30 10:15:08 -0800743#ifdef FLOAT_AUX
744 memcpy_to_i16_from_float(
745 mConfig.inputCfg.buffer.s16,
746 mConfig.inputCfg.buffer.f32,
747 mConfig.inputCfg.buffer.frameCount);
748#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800749 memcpy_to_i16_from_q4_27(
750 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700751 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800752 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800753#endif
rago94a1ee82017-07-21 15:11:02 -0700754 }
rago94a1ee82017-07-21 15:11:02 -0700755 }
756#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800757 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
758 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
759
760 if (!auxType && mInChannelCountRequested != inChannelCount) {
761 adjust_channels(
762 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
763 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
764 sizeof(float),
765 sizeof(float)
766 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
767 inBuffer = mInConversionBuffer;
768 }
769 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
770 && mOutChannelCountRequested != outChannelCount) {
771 adjust_selected_channels(
772 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
773 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
774 sizeof(float),
775 sizeof(float)
776 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
777 outBuffer = mOutConversionBuffer;
778 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800779 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
780 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800781 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800782 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
783 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700784 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800785 memcpy_to_i16_from_float(
786 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800787 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700790 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800792 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
794 goto data_bypass;
795 }
796 memcpy_to_i16_from_float(
797 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800798 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800799 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800800 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700801 }
802 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800803#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800804 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800805#ifdef FLOAT_EFFECT_CHAIN
806 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800807 sp<EffectBufferHalInterface> target =
808 mOutChannelCountRequested != outChannelCount
809 ? mOutConversionBuffer : mOutBuffer;
810
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800812 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800813 mOutConversionBuffer->audioBuffer()->s16,
814 outChannelCount * mConfig.outputCfg.buffer.frameCount);
815 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800816 if (mOutChannelCountRequested != outChannelCount) {
817 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
818 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
819 sizeof(float),
820 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
821 }
rago94a1ee82017-07-21 15:11:02 -0700822#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700823 } else {
rago94a1ee82017-07-21 15:11:02 -0700824#ifdef FLOAT_EFFECT_CHAIN
825 data_bypass:
826#endif
827 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800828 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700829 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800830 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700831 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800832 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700833 }
834 }
835 ret = -ENODATA;
836 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800837
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 // force transition to IDLE state when engine is ready
839 if (mState == STOPPED && ret == -ENODATA) {
840 mDisableWaitCnt = 1;
841 }
842
843 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700844 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800845#ifdef FLOAT_AUX
846 const size_t size =
847 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
848#else
rago94a1ee82017-07-21 15:11:02 -0700849 const size_t size =
850 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800851#endif
rago94a1ee82017-07-21 15:11:02 -0700852 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800853 }
854 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700855 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
857 // If an insert effect is idle and input buffer is different from output buffer,
858 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700859 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700860 // similar handling with data_bypass above.
861 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
862 accumulateInputToOutput();
863 } else { // EFFECT_BUFFER_ACCESS_WRITE
864 copyInputToOutput();
865 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800866 }
867 }
868}
869
870void AudioFlinger::EffectModule::reset_l()
871{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700872 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800873 return;
874 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700875 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800876}
877
878status_t AudioFlinger::EffectModule::configure()
879{
rago94a1ee82017-07-21 15:11:02 -0700880 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882 uint32_t size;
883 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700884 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700885
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700886 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700887 status = NO_INIT;
888 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800889 }
890
Eric Laurentca7cc822012-11-19 14:55:58 -0800891 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800892 // TODO: handle configuration of input (record) SW effects above the HAL,
893 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
894 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700895 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200896 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800897 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200898 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800899
900 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800901 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
902 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
903 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
904 mConfig.inputCfg.channels);
905 }
906#ifndef MULTICHANNEL_EFFECT_CHAIN
907 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
908 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
909 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
910 mConfig.outputCfg.channels);
911 }
912#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800913 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800914#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700915 // TODO: Update this logic when multichannel effects are implemented.
916 // For offloaded tracks consider mono output as stereo for proper effect initialization
917 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
918 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
919 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
920 ALOGV("Overriding effect input and output as STEREO");
921 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800922#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800923 }
jiabineb3bda02020-06-30 14:07:03 -0700924 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700925 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700926 mConfig.inputCfg.channels |= hapticChannelMask;
927 mConfig.outputCfg.channels |= hapticChannelMask;
928 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800929 mInChannelCountRequested =
930 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
931 mOutChannelCountRequested =
932 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700933
rago94a1ee82017-07-21 15:11:02 -0700934 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
935 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900936
937 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700938 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900939 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
940 ALOGV("Overriding effect input as 48kHz");
941 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700942 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900943 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800944 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
945 mConfig.inputCfg.bufferProvider.cookie = NULL;
946 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
947 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
948 mConfig.outputCfg.bufferProvider.cookie = NULL;
949 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
950 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
951 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
952 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800953 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800954 // always overwrites output buffer: input buffer == output buffer
955 // - in other sessions:
956 // last effect in the chain accumulates in output buffer: input buffer != output buffer
957 // other effect: overwrites output buffer: input buffer == output buffer
958 // Auxiliary effect:
959 // accumulates in output buffer: input buffer != output buffer
960 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700961 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800962 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
963 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700964 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800965 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000966 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800967
Eric Laurent6b446ce2019-12-13 10:56:31 -0800968 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700969 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800970 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800971
972 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700973 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700974 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800975 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700976 &mConfig,
977 &size,
978 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700979 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800980 status = cmdStatus;
981 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800982
983#ifdef MULTICHANNEL_EFFECT_CHAIN
984 if (status != NO_ERROR &&
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000985 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800986 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
987 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
988 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700989 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
990 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800991 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
992 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
993 }
994 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
995 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
996 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
997 }
998 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700999 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -08001000 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -07001001 &mConfig,
1002 &size,
1003 &cmdStatus);
1004 if (status == NO_ERROR) {
1005 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001006 }
1007 }
1008#endif
1009
1010#ifdef FLOAT_EFFECT_CHAIN
1011 if (status == NO_ERROR) {
1012 mSupportsFloat = true;
1013 }
1014
1015 if (status != NO_ERROR) {
1016 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1017 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1018 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1019 size = sizeof(int);
1020 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1021 sizeof(mConfig),
1022 &mConfig,
1023 &size,
1024 &cmdStatus);
1025 if (status == NO_ERROR) {
1026 status = cmdStatus;
1027 }
1028 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001029 mSupportsFloat = false;
1030 ALOGVV("config worked with 16 bit");
1031 } else {
1032 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001033 }
rago94a1ee82017-07-21 15:11:02 -07001034 }
1035#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001036
rago94a1ee82017-07-21 15:11:02 -07001037 if (status == NO_ERROR) {
1038 // Establish Buffer strategy
1039 setInBuffer(mInBuffer);
1040 setOutBuffer(mOutBuffer);
1041
1042 // Update visualizer latency
1043 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1044 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1045 effect_param_t *p = (effect_param_t *)buf32;
1046
1047 p->psize = sizeof(uint32_t);
1048 p->vsize = sizeof(uint32_t);
1049 size = sizeof(int);
1050 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1051
Andy Hungfda44002021-06-03 17:23:16 -07001052 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001053
1054 *((int32_t *)p->data + 1)= latency;
1055 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1056 sizeof(effect_param_t) + 8,
1057 &buf32,
1058 &size,
1059 &cmdStatus);
1060 }
jiabin4e246532022-08-23 16:37:30 -07001061
1062 if (isVolumeControl()) {
1063 // Force initializing the volume as 0 for volume control effect for safer ramping
1064 uint32_t left = 0;
1065 uint32_t right = 0;
1066 setVolumeInternal(&left, &right, true /*controller*/);
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 }
1069
Andy Hung05083ac2017-12-14 15:00:28 -08001070 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1071 mMaxDisableWaitCnt = (uint32_t)std::max(
1072 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1073 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1074 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001075
Eric Laurentd0ebb532013-04-02 16:41:41 -07001076exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001077 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001078 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001079 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 return status;
1081}
1082
1083status_t AudioFlinger::EffectModule::init()
1084{
1085 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001086 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 return NO_INIT;
1088 }
1089 status_t cmdStatus;
1090 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001091 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1092 0,
1093 NULL,
1094 &size,
1095 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001096 if (status == 0) {
1097 status = cmdStatus;
1098 }
1099 return status;
1100}
1101
Eric Laurent1b928682014-10-02 19:41:47 -07001102void AudioFlinger::EffectModule::addEffectToHal_l()
1103{
1104 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1105 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001106 if (mCurrentHalStream == getCallback()->io()) {
David Li6c8ac4b2021-06-22 22:17:52 +08001107 return;
1108 }
1109
Andy Hungfda44002021-06-03 17:23:16 -07001110 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001111 mCurrentHalStream = getCallback()->io();
Eric Laurent1b928682014-10-02 19:41:47 -07001112 }
1113}
1114
Eric Laurentfa1e1232016-08-02 19:01:49 -07001115// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001116status_t AudioFlinger::EffectModule::start()
1117{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001118 status_t status;
1119 {
1120 Mutex::Autolock _l(mLock);
1121 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001122 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001123 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001124 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001125 }
1126 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001127}
1128
1129status_t AudioFlinger::EffectModule::start_l()
1130{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001131 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001132 return NO_INIT;
1133 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001134 if (mStatus != NO_ERROR) {
1135 return mStatus;
1136 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 status_t cmdStatus;
1138 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001139 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1140 0,
1141 NULL,
1142 &size,
1143 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 if (status == 0) {
1145 status = cmdStatus;
1146 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001147 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001148 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001149 }
1150 return status;
1151}
1152
1153status_t AudioFlinger::EffectModule::stop()
1154{
1155 Mutex::Autolock _l(mLock);
1156 return stop_l();
1157}
1158
1159status_t AudioFlinger::EffectModule::stop_l()
1160{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001161 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 return NO_INIT;
1163 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001164 if (mStatus != NO_ERROR) {
1165 return mStatus;
1166 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001167 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001168 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001169
1170 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001171 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1172 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1173 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001174 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001175 mSetVolumeReentrantTid = INVALID_PID;
1176 }
1177
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001178 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1179 0,
1180 NULL,
1181 &size,
1182 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001183 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 status = cmdStatus;
1185 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001186 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001187 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001188 }
1189 return status;
1190}
1191
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001192// must be called with EffectChain::mLock held
1193void AudioFlinger::EffectModule::release_l()
1194{
1195 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001196 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001197 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001198 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001199 mEffectInterface.clear();
1200 }
1201}
1202
Eric Laurent6b446ce2019-12-13 10:56:31 -08001203status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001204{
1205 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1206 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001207 if (mCurrentHalStream != getCallback()->io()) {
1208 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
David Li6c8ac4b2021-06-22 22:17:52 +08001209 }
Andy Hungfda44002021-06-03 17:23:16 -07001210 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001211 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001212 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001213 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001214}
1215
Andy Hunge4a1d912016-08-17 14:11:13 -07001216// round up delta valid if value and divisor are positive.
1217template <typename T>
1218static T roundUpDelta(const T &value, const T &divisor) {
1219 T remainder = value % divisor;
1220 return remainder == 0 ? 0 : divisor - remainder;
1221}
1222
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001223status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1224 const std::vector<uint8_t>& cmdData,
1225 int32_t maxReplySize,
1226 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001227{
1228 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001229 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001230
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001231 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001232 return NO_INIT;
1233 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001234 if (mStatus != NO_ERROR) {
1235 return mStatus;
1236 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001237 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1238 return -EINVAL;
1239 }
1240 size_t cmdSize = cmdData.size();
1241 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1242 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1243 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001244 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001245 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001246 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001247 android_errorWriteLog(0x534e4554, "33003822");
1248 return -EINVAL;
1249 }
1250 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001251 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001252 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001253 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001254 return -EINVAL;
1255 }
ragoe2759072016-11-22 18:02:48 -08001256 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001257 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001258 || param->psize > maxReplySize - sizeof(effect_param_t)
1259 || param->vsize > maxReplySize - sizeof(effect_param_t)
1260 - param->psize
1261 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1262 maxReplySize
1263 - sizeof(effect_param_t)
1264 - param->psize
1265 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001266 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1267 android_errorWriteLog(0x534e4554, "32705438");
1268 return -EINVAL;
1269 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001270 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001271 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1272 && // DEFERRED not generally used
1273 (param == nullptr
1274 || param->psize > cmdSize - sizeof(effect_param_t)
1275 || param->vsize > cmdSize - sizeof(effect_param_t)
1276 - param->psize
1277 || roundUpDelta(param->psize,
1278 (uint32_t) sizeof(int)) >
1279 cmdSize
1280 - sizeof(effect_param_t)
1281 - param->psize
1282 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001283 android_errorWriteLog(0x534e4554, "30204301");
1284 return -EINVAL;
1285 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001286 uint32_t replySize = maxReplySize;
1287 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001288 status_t status = mEffectInterface->command(cmdCode,
1289 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001290 const_cast<uint8_t*>(cmdData.data()),
1291 &replySize,
1292 reply->data());
1293 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001294 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001295 for (size_t i = 1; i < mHandles.size(); i++) {
1296 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001297 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001298 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 }
1300 }
1301 }
1302 return status;
1303}
1304
Eric Laurentca7cc822012-11-19 14:55:58 -08001305bool AudioFlinger::EffectModule::isProcessEnabled() const
1306{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001307 if (mStatus != NO_ERROR) {
1308 return false;
1309 }
1310
Eric Laurentca7cc822012-11-19 14:55:58 -08001311 switch (mState) {
1312 case RESTART:
1313 case ACTIVE:
1314 case STOPPING:
1315 case STOPPED:
1316 return true;
1317 case IDLE:
1318 case STARTING:
1319 case DESTROYED:
1320 default:
1321 return false;
1322 }
1323}
1324
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001325bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1326{
Andy Hungfda44002021-06-03 17:23:16 -07001327 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001328}
1329
1330bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1331{
1332 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1333}
1334
Mikhail Naganov022b9952017-01-04 16:36:51 -08001335void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001336 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001337
1338 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001339 if (buffer != 0) {
1340 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1341 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1342 } else {
1343 mConfig.inputCfg.buffer.raw = NULL;
1344 }
1345 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001346 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001347
1348#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001349 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001350 // Theoretically insert effects can also do in-place conversions (destroying
1351 // the original buffer) when the output buffer is identical to the input buffer,
1352 // but we don't optimize for it here.
1353 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001354 const uint32_t inChannelCount =
1355 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1356 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001357 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001358 // we need to translate - create hidl shared buffer and intercept
1359 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001360 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1361 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1362 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001363
1364 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1365 __func__, inChannels, inFrameCount, size);
1366
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001367 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001368 || size > mInConversionBuffer->getSize())) {
1369 mInConversionBuffer.clear();
1370 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001371 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001372 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001373 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001374 mInConversionBuffer->setFrameCount(inFrameCount);
1375 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001376 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001377 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001378 }
1379 }
1380#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001381}
1382
1383void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001384 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001385
1386 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001387 if (buffer != 0) {
1388 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1389 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1390 } else {
1391 mConfig.outputCfg.buffer.raw = NULL;
1392 }
1393 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001394 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001395
1396#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001397 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001398 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001399 const uint32_t outChannelCount =
1400 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1401 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001402 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001403 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001404 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1405 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1406 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001407
1408 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1409 __func__, outChannels, outFrameCount, size);
1410
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001411 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001412 || size > mOutConversionBuffer->getSize())) {
1413 mOutConversionBuffer.clear();
1414 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001415 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001416 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001417 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001418 mOutConversionBuffer->setFrameCount(outFrameCount);
1419 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001420 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001421 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001422 }
1423 }
1424#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001425}
1426
Eric Laurentca7cc822012-11-19 14:55:58 -08001427status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1428{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001429 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001430 if (mStatus != NO_ERROR) {
1431 return mStatus;
1432 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001433 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001434 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1435 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1436 if (isProcessEnabled() &&
1437 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001438 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1439 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin4e246532022-08-23 16:37:30 -07001440 status = setVolumeInternal(left, right, controller);
1441 }
1442 return status;
1443}
1444
1445status_t AudioFlinger::EffectModule::setVolumeInternal(
1446 uint32_t *left, uint32_t *right, bool controller) {
1447 uint32_t volume[2] = {*left, *right};
1448 uint32_t *pVolume = controller ? volume : nullptr;
1449 uint32_t size = sizeof(volume);
1450 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1451 size,
1452 volume,
1453 &size,
1454 pVolume);
1455 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1456 *left = volume[0];
1457 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001458 }
1459 return status;
1460}
1461
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001462void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1463{
Zhou Songd505c642020-02-20 16:35:37 +08001464 // for offload or direct thread, if the effect chain has non-offloadable
1465 // effect and any effect module within the chain has volume control, then
1466 // volume control is delegated to effect, otherwise, set volume to hal.
1467 if (mEffectCallback->isOffloadOrDirect() &&
1468 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001469 float vol_l = (float)left / (1 << 24);
1470 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001471 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001472 }
1473}
1474
jiabin8f278ee2019-11-11 12:16:27 -08001475status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1476 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001477{
jiabin8f278ee2019-11-11 12:16:27 -08001478 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1479 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 return NO_ERROR;
1481 }
1482
1483 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001484 if (mStatus != NO_ERROR) {
1485 return mStatus;
1486 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001488 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001489 status_t cmdStatus;
1490 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001491 // FIXME: use audio device types and addresses when the hal interface is ready.
1492 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001493 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001494 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001495 &size,
1496 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001497 }
1498 return status;
1499}
1500
jiabin8f278ee2019-11-11 12:16:27 -08001501status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1502{
1503 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1504}
1505
1506status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1507{
1508 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1509}
1510
Eric Laurentca7cc822012-11-19 14:55:58 -08001511status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1512{
1513 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001514 if (mStatus != NO_ERROR) {
1515 return mStatus;
1516 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 status_t status = NO_ERROR;
1518 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1519 status_t cmdStatus;
1520 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001521 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1522 sizeof(audio_mode_t),
1523 &mode,
1524 &size,
1525 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 if (status == NO_ERROR) {
1527 status = cmdStatus;
1528 }
1529 }
1530 return status;
1531}
1532
1533status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1534{
1535 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001536 if (mStatus != NO_ERROR) {
1537 return mStatus;
1538 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001539 status_t status = NO_ERROR;
1540 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1541 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001542 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1543 sizeof(audio_source_t),
1544 &source,
1545 &size,
1546 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001547 }
1548 return status;
1549}
1550
Eric Laurent5baf2af2013-09-12 17:37:00 -07001551status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1552{
1553 Mutex::Autolock _l(mLock);
1554 if (mStatus != NO_ERROR) {
1555 return mStatus;
1556 }
1557 status_t status = NO_ERROR;
1558 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1559 status_t cmdStatus;
1560 uint32_t size = sizeof(status_t);
1561 effect_offload_param_t cmd;
1562
1563 cmd.isOffload = offloaded;
1564 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001565 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1566 sizeof(effect_offload_param_t),
1567 &cmd,
1568 &size,
1569 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001570 if (status == NO_ERROR) {
1571 status = cmdStatus;
1572 }
1573 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1574 } else {
1575 if (offloaded) {
1576 status = INVALID_OPERATION;
1577 }
1578 mOffloaded = false;
1579 }
1580 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1581 return status;
1582}
1583
1584bool AudioFlinger::EffectModule::isOffloaded() const
1585{
1586 Mutex::Autolock _l(mLock);
1587 return mOffloaded;
1588}
1589
jiabineb3bda02020-06-30 14:07:03 -07001590/*static*/
1591bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1592 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1593}
1594
1595bool AudioFlinger::EffectModule::isHapticGenerator() const {
1596 return isHapticGenerator(&mDescriptor.type);
1597}
1598
Simon Bowden62823412022-10-17 14:52:26 +00001599status_t AudioFlinger::EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001600{
1601 if (mStatus != NO_ERROR) {
1602 return mStatus;
1603 }
1604 if (!isHapticGenerator()) {
1605 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1606 return INVALID_OPERATION;
1607 }
1608
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001609 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1610 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001611 param->psize = sizeof(int32_t);
1612 param->vsize = sizeof(int32_t) * 2;
1613 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1614 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001615 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001616 std::vector<uint8_t> response;
1617 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001618 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001619 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1620 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001621 }
1622 return status;
1623}
1624
Lais Andradebc3f37a2021-07-02 00:13:19 +01001625status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001626{
1627 if (mStatus != NO_ERROR) {
1628 return mStatus;
1629 }
1630 if (!isHapticGenerator()) {
1631 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1632 return INVALID_OPERATION;
1633 }
1634
Lais Andradebc3f37a2021-07-02 00:13:19 +01001635 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001636 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001637 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001638 effect_param_t *param = (effect_param_t*) request.data();
1639 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001640 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001641 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1642 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001643 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1644 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1645 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001646 std::vector<uint8_t> response;
1647 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1648 if (status == NO_ERROR) {
1649 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1650 status = *reinterpret_cast<const status_t*>(response.data());
1651 }
1652 return status;
1653}
1654
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001655status_t AudioFlinger::EffectModule::getConfigs(
1656 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1657 Mutex::Autolock _l(mLock);
1658 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1659 return NO_INIT;
1660 }
1661 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1662 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1663 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1664 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1665 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1666 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1667 *isOutput = mIsOutput;
1668 return NO_ERROR;
1669}
1670
Andy Hungbded9c82017-11-30 18:47:35 -08001671static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1672 std::stringstream ss;
1673
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001674 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001675 return "nullptr"; // make different than below
1676 } else if (buffer->externalData() != nullptr) {
1677 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1678 << " -> "
1679 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1680 } else {
1681 ss << buffer->audioBuffer()->raw;
1682 }
1683 return ss.str();
1684}
Marco Nelissenb2208842014-02-07 14:00:50 -08001685
Eric Laurent41709552019-12-16 19:34:05 -08001686void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07001687NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001688{
Eric Laurent41709552019-12-16 19:34:05 -08001689 EffectBase::dump(fd, args);
1690
Eric Laurentca7cc822012-11-19 14:55:58 -08001691 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001693
Eric Laurent41709552019-12-16 19:34:05 -08001694 result.append("\t\tStatus Engine:\n");
1695 result.appendFormat("\t\t%03d %p\n",
1696 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001697
1698 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001699
1700 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001701 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1702 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1703 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 mConfig.inputCfg.buffer.frameCount,
1705 mConfig.inputCfg.samplingRate,
1706 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001707 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001708 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001709
1710 result.append("\t\t- Output configuration:\n");
1711 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001712 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001713 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 mConfig.outputCfg.buffer.frameCount,
1715 mConfig.outputCfg.samplingRate,
1716 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001717 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001718 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001719
rago94a1ee82017-07-21 15:11:02 -07001720#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001721
Andy Hungbded9c82017-11-30 18:47:35 -08001722 result.appendFormat("\t\t- HAL buffers:\n"
1723 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1724 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1725 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1726 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1727 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001728#endif
1729
Eric Laurentca7cc822012-11-19 14:55:58 -08001730 write(fd, result.string(), result.length());
1731
Mikhail Naganov4d547672019-02-22 14:19:19 -08001732 if (mEffectInterface != 0) {
1733 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1734 (void)mEffectInterface->dump(fd);
1735 }
1736
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 if (locked) {
1738 mLock.unlock();
1739 }
1740}
1741
1742// ----------------------------------------------------------------------------
1743// EffectHandle implementation
1744// ----------------------------------------------------------------------------
1745
1746#undef LOG_TAG
1747#define LOG_TAG "AudioFlinger::EffectHandle"
1748
Eric Laurent41709552019-12-16 19:34:05 -08001749AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001750 const sp<AudioFlinger::Client>& client,
1751 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001752 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 : BnEffect(),
1754 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001755 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1756 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001757{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001758 ALOGV("constructor %p client %p", this, client.get());
Andy Hung225aef62022-12-06 16:33:20 -08001759 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001760
1761 if (client == 0) {
1762 return;
1763 }
1764 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001765 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1766 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1767 std::string("Effect ID: ")
1768 .append(std::to_string(effect->id()))
1769 .append(" Session ID: ")
1770 .append(std::to_string(static_cast<int>(effect->sessionId())))
1771 .append(" \n")
1772 });
Glenn Kastene75da402013-11-20 13:54:52 -08001773 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001774 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001775 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001776 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001777 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001778 return;
1779 }
Glenn Kastene75da402013-11-20 13:54:52 -08001780 new(mCblk) effect_param_cblk_t();
1781 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001782}
1783
1784AudioFlinger::EffectHandle::~EffectHandle()
1785{
1786 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001787 disconnect(false);
1788}
1789
Andy Hungc747c532022-03-07 21:41:14 -08001790// Creates an association between Binder code to name for IEffect.
1791#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1792BINDER_METHOD_ENTRY(enable) \
1793BINDER_METHOD_ENTRY(disable) \
1794BINDER_METHOD_ENTRY(command) \
1795BINDER_METHOD_ENTRY(disconnect) \
1796BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001797BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001798
1799// singleton for Binder Method Statistics for IEffect
1800mediautils::MethodStatistics<int>& getIEffectStatistics() {
1801 using Code = int;
1802
1803#pragma push_macro("BINDER_METHOD_ENTRY")
1804#undef BINDER_METHOD_ENTRY
1805#define BINDER_METHOD_ENTRY(ENTRY) \
1806 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1807
1808 static mediautils::MethodStatistics<Code> methodStatistics{
1809 IEFFECT_BINDER_METHOD_MACRO_LIST
1810 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1811 };
1812#pragma pop_macro("BINDER_METHOD_ENTRY")
1813
1814 return methodStatistics;
1815}
1816
1817status_t AudioFlinger::EffectHandle::onTransact(
1818 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001819 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1820 mediautils::TimeCheck check(
1821 std::string("IEffect::").append(methodName),
1822 [code](bool timeout, float elapsedMs) {
1823 if (timeout) {
1824 ; // we don't timeout right now on the effect interface.
1825 } else {
1826 getIEffectStatistics().event(code, elapsedMs);
1827 }
Andy Hungf8ab0932022-06-13 19:49:43 -07001828 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001829 return BnEffect::onTransact(code, data, reply, flags);
1830}
1831
Glenn Kastene75da402013-11-20 13:54:52 -08001832status_t AudioFlinger::EffectHandle::initCheck()
1833{
1834 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1835}
1836
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001837#define RETURN(code) \
1838 *_aidl_return = (code); \
1839 return Status::ok();
1840
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001841#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1842 ({ \
1843 auto _tmp = (exp); \
1844 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1845 std::move(_tmp.value()); \
1846 })
1847
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001848Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001849{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001852 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001854 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001856 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001857 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001859
1860 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001861 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001862 }
1863
1864 mEnabled = true;
1865
Eric Laurent6c796322019-04-09 14:13:17 -07001866 status_t status = effect->updatePolicyState();
1867 if (status != NO_ERROR) {
1868 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001869 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001870 }
1871
Eric Laurent6b446ce2019-12-13 10:56:31 -08001872 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001873
1874 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001875 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001876 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001877 }
1878
Eric Laurent6b446ce2019-12-13 10:56:31 -08001879 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001881 mEnabled = false;
1882 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001883 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001884}
1885
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001886Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001887{
1888 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001889 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001890 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001891 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001893 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001894 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001895 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001897
1898 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 }
1901 mEnabled = false;
1902
Eric Laurent6c796322019-04-09 14:13:17 -07001903 effect->updatePolicyState();
1904
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001905 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001907 }
1908
Eric Laurent6b446ce2019-12-13 10:56:31 -08001909 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001911}
1912
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001913Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001914{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001915 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001916 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001917 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001918}
1919
1920void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1921{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001922 AutoMutex _l(mLock);
1923 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1924 if (mDisconnected) {
1925 if (unpinIfLast) {
1926 android_errorWriteLog(0x534e4554, "32707507");
1927 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001928 return;
1929 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001930 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001931 {
Eric Laurent41709552019-12-16 19:34:05 -08001932 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001933 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001934 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001935 ALOGW("%s Effect handle %p disconnected after thread destruction",
1936 __func__, this);
1937 }
1938 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001939 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001940 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001941
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 if (mClient != 0) {
1943 if (mCblk != NULL) {
1944 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1945 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1946 }
1947 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001948 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung920f6572022-10-06 12:09:49 -07001949 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001950 mClient.clear();
1951 }
1952}
1953
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001954Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1955 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1956 return Status::ok();
1957}
1958
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001959Status AudioFlinger::EffectHandle::getConfig(
1960 media::EffectConfig* _config, int32_t* _aidl_return) {
1961 AutoMutex _l(mLock);
1962 sp<EffectBase> effect = mEffect.promote();
1963 if (effect == nullptr || mDisconnected) {
1964 RETURN(DEAD_OBJECT);
1965 }
1966 sp<EffectModule> effectModule = effect->asEffectModule();
1967 if (effectModule == nullptr) {
1968 RETURN(INVALID_OPERATION);
1969 }
1970 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1971 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1972 bool isOutput;
1973 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1974 if (status == NO_ERROR) {
1975 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1976 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1977 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1978 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1979 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1980 _config->isOnInputStream = !isOutput;
1981 }
1982 RETURN(status);
1983}
1984
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001985Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1986 const std::vector<uint8_t>& cmdData,
1987 int32_t maxResponseSize,
1988 std::vector<uint8_t>* response,
1989 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001990{
1991 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001992 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001993
Eric Laurentc7ab3092017-06-15 18:43:46 -07001994 // reject commands reserved for internal use by audio framework if coming from outside
1995 // of audioserver
1996 switch(cmdCode) {
1997 case EFFECT_CMD_ENABLE:
1998 case EFFECT_CMD_DISABLE:
1999 case EFFECT_CMD_SET_PARAM:
2000 case EFFECT_CMD_SET_PARAM_DEFERRED:
2001 case EFFECT_CMD_SET_PARAM_COMMIT:
2002 case EFFECT_CMD_GET_PARAM:
2003 break;
2004 default:
2005 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2006 break;
2007 }
2008 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002009 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002010 }
2011
Eric Laurent1ffc5852016-12-15 14:46:09 -08002012 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002013 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002014 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002015 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002016 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 writeToBuffer(NO_ERROR, response);
2018 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002019 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002020 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002021 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002022 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002023 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002024 writeToBuffer(NO_ERROR, response);
2025 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002026 }
2027
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002028 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002029 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002030 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002031 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002032 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002033 // only get parameter command is permitted for applications not controlling the effect
2034 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002035 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002036 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002037
2038 // handle commands that are not forwarded transparently to effect engine
2039 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002040 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002041 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002042 }
2043
Andy Hung920f6572022-10-06 12:09:49 -07002044 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002045 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002046 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002047 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002048 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002049
Eric Laurentca7cc822012-11-19 14:55:58 -08002050 // No need to trylock() here as this function is executed in the binder thread serving a
2051 // particular client process: no risk to block the whole media server process or mixer
2052 // threads if we are stuck here
Andy Hung920f6572022-10-06 12:09:49 -07002053 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002054 // keep local copy of index in case of client corruption b/32220769
2055 const uint32_t clientIndex = mCblk->clientIndex;
2056 const uint32_t serverIndex = mCblk->serverIndex;
2057 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2058 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002059 mCblk->serverIndex = 0;
2060 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002061 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002062 }
2063 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002064 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002065 for (uint32_t index = serverIndex; index < clientIndex;) {
2066 int *p = (int *)(mBuffer + index);
2067 const int size = *p++;
2068 if (size < 0
2069 || size > EFFECT_PARAM_BUFFER_SIZE
2070 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002071 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002072 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 break;
2074 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002075
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002076 std::copy(reinterpret_cast<const uint8_t*>(p),
2077 reinterpret_cast<const uint8_t*>(p) + size,
2078 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002079
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002080 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002081 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002082 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002083 sizeof(int),
2084 &replyBuffer);
2085 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002086
2087 // verify shared memory: server index shouldn't change; client index can't go back.
2088 if (serverIndex != mCblk->serverIndex
2089 || clientIndex > mCblk->clientIndex) {
2090 android_errorWriteLog(0x534e4554, "32220769");
2091 status = BAD_VALUE;
2092 break;
2093 }
2094
Eric Laurentca7cc822012-11-19 14:55:58 -08002095 // stop at first error encountered
2096 if (ret != NO_ERROR) {
2097 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002098 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002099 break;
2100 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002101 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002102 break;
2103 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002104 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002105 }
2106 mCblk->serverIndex = 0;
2107 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002108 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002109 }
2110
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002111 status_t status = effect->command(cmdCode,
2112 cmdData,
2113 maxResponseSize,
2114 response);
2115 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002116}
2117
2118void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2119{
2120 ALOGV("setControl %p control %d", this, hasControl);
2121
2122 mHasControl = hasControl;
2123 mEnabled = enabled;
2124
2125 if (signal && mEffectClient != 0) {
2126 mEffectClient->controlStatusChanged(hasControl);
2127 }
2128}
2129
2130void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002131 const std::vector<uint8_t>& cmdData,
2132 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002133{
2134 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002135 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002136 }
2137}
2138
2139
2140
2141void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2142{
2143 if (mEffectClient != 0) {
2144 mEffectClient->enableStatusChanged(enabled);
2145 }
2146}
2147
Eric Laurentde8caf42021-08-11 17:19:25 +02002148void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2149{
2150 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2151 mEffectClient->framesProcessed(frames);
2152 }
2153}
2154
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002155void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung920f6572022-10-06 12:09:49 -07002156NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002157{
2158 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2159
Marco Nelissenb2208842014-02-07 14:00:50 -08002160 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002161 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002162 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002163 mHasControl ? "yes" : "no",
2164 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002165 mCblk ? mCblk->clientIndex : 0,
2166 mCblk ? mCblk->serverIndex : 0
2167 );
2168
2169 if (locked) {
2170 mCblk->lock.unlock();
2171 }
2172}
2173
2174#undef LOG_TAG
2175#define LOG_TAG "AudioFlinger::EffectChain"
2176
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002177AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2178 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002179 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002180 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002181 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002182 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002183{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002184 sp<ThreadBase> p = thread.promote();
2185 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002186 return;
2187 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002188 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002189 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2190 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002191}
2192
2193AudioFlinger::EffectChain::~EffectChain()
2194{
Eric Laurentca7cc822012-11-19 14:55:58 -08002195}
2196
2197// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2198sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2199 effect_descriptor_t *descriptor)
2200{
2201 size_t size = mEffects.size();
2202
2203 for (size_t i = 0; i < size; i++) {
2204 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2205 return mEffects[i];
2206 }
2207 }
2208 return 0;
2209}
2210
2211// getEffectFromId_l() must be called with ThreadBase::mLock held
2212sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2213{
2214 size_t size = mEffects.size();
2215
2216 for (size_t i = 0; i < size; i++) {
2217 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2218 if (id == 0 || mEffects[i]->id() == id) {
2219 return mEffects[i];
2220 }
2221 }
2222 return 0;
2223}
2224
2225// getEffectFromType_l() must be called with ThreadBase::mLock held
2226sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2227 const effect_uuid_t *type)
2228{
2229 size_t size = mEffects.size();
2230
2231 for (size_t i = 0; i < size; i++) {
2232 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2233 return mEffects[i];
2234 }
2235 }
2236 return 0;
2237}
2238
Eric Laurent6c796322019-04-09 14:13:17 -07002239std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2240{
2241 std::vector<int> ids;
2242 Mutex::Autolock _l(mLock);
2243 for (size_t i = 0; i < mEffects.size(); i++) {
2244 ids.push_back(mEffects[i]->id());
2245 }
2246 return ids;
2247}
2248
Eric Laurentca7cc822012-11-19 14:55:58 -08002249void AudioFlinger::EffectChain::clearInputBuffer()
2250{
2251 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002252 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002253}
2254
2255// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002256void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002257{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002258 if (mInBuffer == NULL) {
2259 return;
2260 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002261 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2262 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002263
Eric Laurent6b446ce2019-12-13 10:56:31 -08002264 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002265 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002266}
2267
2268// Must be called with EffectChain::mLock locked
2269void AudioFlinger::EffectChain::process_l()
2270{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002271 // never process effects when:
2272 // - on an OFFLOAD thread
2273 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002274 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002275 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002276 bool tracksOnSession = (trackCnt() != 0);
2277
2278 if (!tracksOnSession && mTailBufferCount == 0) {
2279 doProcess = false;
2280 }
2281
2282 if (activeTrackCnt() == 0) {
2283 // if no track is active and the effect tail has not been rendered,
2284 // the input buffer must be cleared here as the mixer process will not do it
2285 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002286 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002287 if (mTailBufferCount > 0) {
2288 mTailBufferCount--;
2289 }
2290 }
2291 }
2292 }
2293
2294 size_t size = mEffects.size();
2295 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002296 // Only the input and output buffers of the chain can be external,
2297 // and 'update' / 'commit' do nothing for allocated buffers, thus
2298 // it's not needed to consider any other buffers here.
2299 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002300 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2301 mOutBuffer->update();
2302 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002303 for (size_t i = 0; i < size; i++) {
2304 mEffects[i]->process();
2305 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002306 mInBuffer->commit();
2307 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2308 mOutBuffer->commit();
2309 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002310 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002311 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002313 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2314 }
2315 if (doResetVolume) {
2316 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002317 }
2318}
2319
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002320// createEffect_l() must be called with ThreadBase::mLock held
2321status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002322 effect_descriptor_t *desc,
2323 int id,
2324 audio_session_t sessionId,
2325 bool pinned)
2326{
2327 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002328 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002329 status_t lStatus = effect->status();
2330 if (lStatus == NO_ERROR) {
2331 lStatus = addEffect_ll(effect);
2332 }
2333 if (lStatus != NO_ERROR) {
2334 effect.clear();
2335 }
2336 return lStatus;
2337}
2338
2339// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002340status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2341{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002342 Mutex::Autolock _l(mLock);
2343 return addEffect_ll(effect);
2344}
2345// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2346status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2347{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002348 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002349
Eric Laurentb62d0362021-10-26 17:40:18 +02002350 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002351 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2352 // Auxiliary effects are inserted at the beginning of mEffects vector as
2353 // they are processed first and accumulated in chain input buffer
2354 mEffects.insertAt(effect, 0);
2355
2356 // the input buffer for auxiliary effect contains mono samples in
2357 // 32 bit format. This is to avoid saturation in AudoMixer
2358 // accumulation stage. Saturation is done in EffectModule::process() before
2359 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002360 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002361 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002362#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002363 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002364 numSamples * sizeof(float), &halBuffer);
2365#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002366 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002367 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002368#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002369 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002370
2371 effect->configure();
2372
Mikhail Naganov022b9952017-01-04 16:36:51 -08002373 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002374 // auxiliary effects output samples to chain input buffer for further processing
2375 // by insert effects
2376 effect->setOutBuffer(mInBuffer);
2377 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002378 ssize_t idx_insert = getInsertIndex(desc);
2379 if (idx_insert < 0) {
2380 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 }
2382
Eric Laurentb62d0362021-10-26 17:40:18 +02002383 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002384 mEffects.insertAt(effect, idx_insert);
2385
2386 effect->configure();
2387
Eric Laurentb62d0362021-10-26 17:40:18 +02002388 // - By default:
2389 // All effects read samples from chain input buffer.
2390 // The last effect in the chain, writes samples to chain output buffer,
2391 // otherwise to chain input buffer
2392 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2393 // The spatializer effect (first effect) reads samples from the input buffer
2394 // and writes samples to the output buffer.
2395 // All other effects read and writes samples to the output buffer
2396 if (mEffectCallback->isSpatializer()
2397 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002398 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002399 if (idx_insert == 0) {
2400 if (previousSize != 0) {
2401 mEffects[1]->configure();
2402 mEffects[1]->setInBuffer(mOutBuffer);
2403 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2404 }
2405 effect->setInBuffer(mInBuffer);
2406 } else {
2407 effect->setInBuffer(mOutBuffer);
2408 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002409 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002410 effect->setInBuffer(mInBuffer);
Andy Hung920f6572022-10-06 12:09:49 -07002411 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002412 if (idx_insert != 0) {
2413 mEffects[idx_insert-1]->configure();
2414 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2415 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2416 }
2417 effect->setOutBuffer(mOutBuffer);
2418 } else {
2419 effect->setOutBuffer(mInBuffer);
2420 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002421 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002422 ALOGV("%s effect %p, added in chain %p at rank %zu",
2423 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002424 }
2425 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002426
Eric Laurentca7cc822012-11-19 14:55:58 -08002427 return NO_ERROR;
2428}
2429
Eric Laurentb62d0362021-10-26 17:40:18 +02002430ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2431 // Insert effects are inserted at the end of mEffects vector as they are processed
2432 // after track and auxiliary effects.
2433 // Insert effect order as a function of indicated preference:
2434 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2435 // another effect is present
2436 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2437 // last effect claiming first position
2438 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2439 // first effect claiming last position
2440 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2441 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2442 // already present
2443 // Spatializer or Downmixer effects are inserted in first position because
2444 // they adapt the channel count for all other effects in the chain
2445 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2446 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2447 return 0;
2448 }
2449
2450 size_t size = mEffects.size();
2451 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2452 ssize_t idx_insert;
2453 ssize_t idx_insert_first = -1;
2454 ssize_t idx_insert_last = -1;
2455
2456 idx_insert = size;
2457 for (size_t i = 0; i < size; i++) {
2458 effect_descriptor_t d = mEffects[i]->desc();
2459 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2460 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2461 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2462 // check invalid effect chaining combinations
2463 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2464 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2465 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2466 __func__, desc.name, d.name);
2467 return -1;
2468 }
2469 // remember position of first insert effect and by default
2470 // select this as insert position for new effect
Andy Hung920f6572022-10-06 12:09:49 -07002471 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002472 idx_insert = i;
2473 }
2474 // remember position of last insert effect claiming
2475 // first position
2476 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2477 idx_insert_first = i;
2478 }
2479 // remember position of first insert effect claiming
2480 // last position
2481 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2482 idx_insert_last == -1) {
2483 idx_insert_last = i;
2484 }
2485 }
2486 }
2487
2488 // modify idx_insert from first position if needed
2489 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2490 if (idx_insert_last != -1) {
2491 idx_insert = idx_insert_last;
2492 } else {
2493 idx_insert = size;
2494 }
2495 } else {
2496 if (idx_insert_first != -1) {
2497 idx_insert = idx_insert_first + 1;
2498 }
2499 }
2500 return idx_insert;
2501}
2502
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002503// removeEffect_l() must be called with ThreadBase::mLock held
2504size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2505 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002506{
2507 Mutex::Autolock _l(mLock);
2508 size_t size = mEffects.size();
2509 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2510
2511 for (size_t i = 0; i < size; i++) {
2512 if (effect == mEffects[i]) {
2513 // calling stop here will remove pre-processing effect from the audio HAL.
2514 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2515 // the middle of a read from audio HAL
2516 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2517 mEffects[i]->state() == EffectModule::STOPPING) {
2518 mEffects[i]->stop();
2519 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002520 if (release) {
2521 mEffects[i]->release_l();
2522 }
2523
Mikhail Naganov022b9952017-01-04 16:36:51 -08002524 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002525 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002526 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002527 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002528 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002529 }
2530 }
2531 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002532
2533 // make sure the input buffer configuration for the new first effect in the chain
2534 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2535 if (i == 0 && size > 1) {
2536 mEffects[0]->configure();
2537 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002538 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002539 }
2540
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002541 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002542 this, i);
2543 break;
2544 }
2545 }
2546
2547 return mEffects.size();
2548}
2549
jiabin8f278ee2019-11-11 12:16:27 -08002550// setDevices_l() must be called with ThreadBase::mLock held
2551void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002552{
2553 size_t size = mEffects.size();
2554 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002555 mEffects[i]->setDevices(devices);
2556 }
2557}
2558
2559// setInputDevice_l() must be called with ThreadBase::mLock held
2560void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2561{
2562 size_t size = mEffects.size();
2563 for (size_t i = 0; i < size; i++) {
2564 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002565 }
2566}
2567
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002568// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002569void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2570{
2571 size_t size = mEffects.size();
2572 for (size_t i = 0; i < size; i++) {
2573 mEffects[i]->setMode(mode);
2574 }
2575}
2576
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002577// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002578void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2579{
2580 size_t size = mEffects.size();
2581 for (size_t i = 0; i < size; i++) {
2582 mEffects[i]->setAudioSource(source);
2583 }
2584}
2585
Zhou Songd505c642020-02-20 16:35:37 +08002586bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2587 for (const auto &effect : mEffects) {
2588 if (effect->isVolumeControlEnabled()) return true;
2589 }
2590 return false;
2591}
2592
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002593// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002594bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002595{
2596 uint32_t newLeft = *left;
2597 uint32_t newRight = *right;
2598 bool hasControl = false;
2599 int ctrlIdx = -1;
2600 size_t size = mEffects.size();
2601
2602 // first update volume controller
2603 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002604 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002605 ctrlIdx = i - 1;
2606 hasControl = true;
2607 break;
2608 }
2609 }
2610
Eric Laurentfa1e1232016-08-02 19:01:49 -07002611 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002612 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002613 if (hasControl) {
2614 *left = mNewLeftVolume;
2615 *right = mNewRightVolume;
2616 }
2617 return hasControl;
2618 }
2619
2620 mVolumeCtrlIdx = ctrlIdx;
2621 mLeftVolume = newLeft;
2622 mRightVolume = newRight;
2623
2624 // second get volume update from volume controller
2625 if (ctrlIdx >= 0) {
2626 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2627 mNewLeftVolume = newLeft;
2628 mNewRightVolume = newRight;
2629 }
2630 // then indicate volume to all other effects in chain.
2631 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002632 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002633 uint32_t lVol = newLeft;
2634 uint32_t rVol = newRight;
2635
2636 for (size_t i = 0; i < size; i++) {
2637 if ((int)i == ctrlIdx) {
2638 continue;
2639 }
2640 // this also works for ctrlIdx == -1 when there is no volume controller
2641 if ((int)i > ctrlIdx) {
2642 lVol = *left;
2643 rVol = *right;
2644 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002645 // Pass requested volume directly if this is volume monitor module
2646 if (mEffects[i]->isVolumeMonitor()) {
2647 mEffects[i]->setVolume(left, right, false);
2648 } else {
2649 mEffects[i]->setVolume(&lVol, &rVol, false);
2650 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002651 }
2652 *left = newLeft;
2653 *right = newRight;
2654
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002655 setVolumeForOutput_l(*left, *right);
2656
Eric Laurentca7cc822012-11-19 14:55:58 -08002657 return hasControl;
2658}
2659
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002660// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002661void AudioFlinger::EffectChain::resetVolume_l()
2662{
Eric Laurente7449bf2016-08-03 18:44:07 -07002663 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2664 uint32_t left = mLeftVolume;
2665 uint32_t right = mRightVolume;
2666 (void)setVolume_l(&left, &right, true);
2667 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002668}
2669
jiabineb3bda02020-06-30 14:07:03 -07002670// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2671bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2672{
2673 for (size_t i = 0; i < mEffects.size(); ++i) {
2674 if (mEffects[i]->isHapticGenerator()) {
2675 return true;
2676 }
2677 }
2678 return false;
2679}
2680
Simon Bowden62823412022-10-17 14:52:26 +00002681void AudioFlinger::EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002682{
2683 Mutex::Autolock _l(mLock);
2684 for (size_t i = 0; i < mEffects.size(); ++i) {
2685 mEffects[i]->setHapticIntensity(id, intensity);
2686 }
2687}
2688
Eric Laurent1b928682014-10-02 19:41:47 -07002689void AudioFlinger::EffectChain::syncHalEffectsState()
2690{
2691 Mutex::Autolock _l(mLock);
2692 for (size_t i = 0; i < mEffects.size(); i++) {
2693 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2694 mEffects[i]->state() == EffectModule::STOPPING) {
2695 mEffects[i]->addEffectToHal_l();
2696 }
2697 }
2698}
2699
Eric Laurentca7cc822012-11-19 14:55:58 -08002700void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07002701NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002702{
Eric Laurentca7cc822012-11-19 14:55:58 -08002703 String8 result;
2704
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002705 const size_t numEffects = mEffects.size();
2706 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002707
Marco Nelissenb2208842014-02-07 14:00:50 -08002708 if (numEffects) {
2709 bool locked = AudioFlinger::dumpTryLock(mLock);
2710 // failed to lock - AudioFlinger is probably deadlocked
2711 if (!locked) {
2712 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002713 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002714
Andy Hungbded9c82017-11-30 18:47:35 -08002715 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2716 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2717 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2718 (int)inBufferStr.size(), "In buffer ",
2719 (int)outBufferStr.size(), "Out buffer ");
2720 result.appendFormat("\t%s %s %d\n",
2721 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002722 write(fd, result.string(), result.size());
2723
2724 for (size_t i = 0; i < numEffects; ++i) {
2725 sp<EffectModule> effect = mEffects[i];
2726 if (effect != 0) {
2727 effect->dump(fd, args);
2728 }
2729 }
2730
2731 if (locked) {
2732 mLock.unlock();
2733 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002734 } else {
2735 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002736 }
2737}
2738
2739// must be called with ThreadBase::mLock held
2740void AudioFlinger::EffectChain::setEffectSuspended_l(
2741 const effect_uuid_t *type, bool suspend)
2742{
2743 sp<SuspendedEffectDesc> desc;
2744 // use effect type UUID timelow as key as there is no real risk of identical
2745 // timeLow fields among effect type UUIDs.
2746 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2747 if (suspend) {
2748 if (index >= 0) {
2749 desc = mSuspendedEffects.valueAt(index);
2750 } else {
2751 desc = new SuspendedEffectDesc();
2752 desc->mType = *type;
2753 mSuspendedEffects.add(type->timeLow, desc);
2754 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2755 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002756
Eric Laurentca7cc822012-11-19 14:55:58 -08002757 if (desc->mRefCount++ == 0) {
2758 sp<EffectModule> effect = getEffectIfEnabled(type);
2759 if (effect != 0) {
2760 desc->mEffect = effect;
2761 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002762 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002763 }
2764 }
2765 } else {
2766 if (index < 0) {
2767 return;
2768 }
2769 desc = mSuspendedEffects.valueAt(index);
2770 if (desc->mRefCount <= 0) {
2771 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002772 desc->mRefCount = 0;
2773 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002774 }
2775 if (--desc->mRefCount == 0) {
2776 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2777 if (desc->mEffect != 0) {
2778 sp<EffectModule> effect = desc->mEffect.promote();
2779 if (effect != 0) {
2780 effect->setSuspended(false);
2781 effect->lock();
2782 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002783 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002784 effect->setEnabled_l(handle->enabled());
2785 }
2786 effect->unlock();
2787 }
2788 desc->mEffect.clear();
2789 }
2790 mSuspendedEffects.removeItemsAt(index);
2791 }
2792 }
2793}
2794
2795// must be called with ThreadBase::mLock held
2796void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2797{
2798 sp<SuspendedEffectDesc> desc;
2799
2800 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2801 if (suspend) {
2802 if (index >= 0) {
2803 desc = mSuspendedEffects.valueAt(index);
2804 } else {
2805 desc = new SuspendedEffectDesc();
2806 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2807 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2808 }
2809 if (desc->mRefCount++ == 0) {
2810 Vector< sp<EffectModule> > effects;
2811 getSuspendEligibleEffects(effects);
2812 for (size_t i = 0; i < effects.size(); i++) {
2813 setEffectSuspended_l(&effects[i]->desc().type, true);
2814 }
2815 }
2816 } else {
2817 if (index < 0) {
2818 return;
2819 }
2820 desc = mSuspendedEffects.valueAt(index);
2821 if (desc->mRefCount <= 0) {
2822 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2823 desc->mRefCount = 1;
2824 }
2825 if (--desc->mRefCount == 0) {
2826 Vector<const effect_uuid_t *> types;
2827 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2828 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2829 continue;
2830 }
2831 types.add(&mSuspendedEffects.valueAt(i)->mType);
2832 }
2833 for (size_t i = 0; i < types.size(); i++) {
2834 setEffectSuspended_l(types[i], false);
2835 }
2836 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2837 mSuspendedEffects.keyAt(index));
2838 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2839 }
2840 }
2841}
2842
2843
2844// The volume effect is used for automated tests only
2845#ifndef OPENSL_ES_H_
2846static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2847 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2848const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2849#endif //OPENSL_ES_H_
2850
Eric Laurentd8365c52017-07-16 15:27:05 -07002851/* static */
2852bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2853{
2854 // Only NS and AEC are suspended when BtNRec is off
2855 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2856 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2857 return true;
2858 }
2859 return false;
2860}
2861
Eric Laurentca7cc822012-11-19 14:55:58 -08002862bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2863{
2864 // auxiliary effects and visualizer are never suspended on output mix
2865 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2866 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2867 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002868 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2869 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002870 return false;
2871 }
2872 return true;
2873}
2874
2875void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2876 Vector< sp<AudioFlinger::EffectModule> > &effects)
2877{
2878 effects.clear();
2879 for (size_t i = 0; i < mEffects.size(); i++) {
2880 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2881 effects.add(mEffects[i]);
2882 }
2883 }
2884}
2885
2886sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2887 const effect_uuid_t *type)
2888{
2889 sp<EffectModule> effect = getEffectFromType_l(type);
2890 return effect != 0 && effect->isEnabled() ? effect : 0;
2891}
2892
2893void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2894 bool enabled)
2895{
2896 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2897 if (enabled) {
2898 if (index < 0) {
2899 // if the effect is not suspend check if all effects are suspended
2900 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2901 if (index < 0) {
2902 return;
2903 }
2904 if (!isEffectEligibleForSuspend(effect->desc())) {
2905 return;
2906 }
2907 setEffectSuspended_l(&effect->desc().type, enabled);
2908 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2909 if (index < 0) {
2910 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2911 return;
2912 }
2913 }
2914 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2915 effect->desc().type.timeLow);
2916 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002917 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002918 if (desc->mEffect == 0) {
2919 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002920 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002921 effect->setSuspended(true);
2922 }
2923 } else {
2924 if (index < 0) {
2925 return;
2926 }
2927 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2928 effect->desc().type.timeLow);
2929 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2930 desc->mEffect.clear();
2931 effect->setSuspended(false);
2932 }
2933}
2934
Eric Laurent5baf2af2013-09-12 17:37:00 -07002935bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002936{
2937 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002938 return isNonOffloadableEnabled_l();
2939}
2940
2941bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2942{
Eric Laurent813e2a72013-08-31 12:59:48 -07002943 size_t size = mEffects.size();
2944 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002945 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002946 return true;
2947 }
2948 }
2949 return false;
2950}
2951
Eric Laurentaaa44472014-09-12 17:41:50 -07002952void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2953{
2954 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002955 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002956}
2957
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002958void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2959{
2960 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2961 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2962 }
2963 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2964 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2965 }
jiabinc658e452022-10-21 20:52:21 +00002966 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2967 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2968 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002969}
2970
2971void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2972{
2973 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2974 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2975 }
2976 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2977 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2978 }
2979}
2980
2981bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002982{
2983 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002984 for (const auto &effect : mEffects) {
2985 if (effect->isProcessImplemented()) {
2986 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002987 }
2988 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002989 // Allow effects without processing.
2990 return true;
2991}
2992
2993bool AudioFlinger::EffectChain::isFastCompatible() const
2994{
2995 Mutex::Autolock _l(mLock);
2996 for (const auto &effect : mEffects) {
2997 if (effect->isProcessImplemented()
2998 && effect->isImplementationSoftware()) {
2999 return false;
3000 }
3001 }
3002 // Allow effects without processing or hw accelerated effects.
3003 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07003004}
3005
jiabinc658e452022-10-21 20:52:21 +00003006bool AudioFlinger::EffectChain::isBitPerfectCompatible() const {
3007 Mutex::Autolock _l(mLock);
3008 for (const auto &effect : mEffects) {
3009 if (effect->isProcessImplemented()
3010 && effect->isImplementationSoftware()) {
3011 return false;
3012 }
3013 }
3014 // Allow effects without processing or hw accelerated effects.
3015 return true;
3016}
3017
Eric Laurent4c415062016-06-17 16:14:16 -07003018// isCompatibleWithThread_l() must be called with thread->mLock held
3019bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
3020{
3021 Mutex::Autolock _l(mLock);
3022 for (size_t i = 0; i < mEffects.size(); i++) {
3023 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3024 return false;
3025 }
3026 }
3027 return true;
3028}
3029
Eric Laurent6b446ce2019-12-13 10:56:31 -08003030// EffectCallbackInterface implementation
3031status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3032 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3033 sp<EffectHalInterface> *effect) {
3034 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003035 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003036 if (effectsFactory != 0) {
3037 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3038 }
3039 return status;
3040}
3041
3042bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003043 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003044 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003045 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003046}
3047
3048status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3049 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003050 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003051}
3052
3053status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003054 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003055 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003056 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003057 if (t == nullptr) {
3058 return result;
3059 }
3060 sp <StreamHalInterface> st = t->stream();
3061 if (st == nullptr) {
3062 return result;
3063 }
3064 result = st->addEffect(effect);
3065 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3066 return result;
3067}
3068
3069status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003070 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003071 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003072 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003073 if (t == nullptr) {
3074 return result;
3075 }
3076 sp <StreamHalInterface> st = t->stream();
3077 if (st == nullptr) {
3078 return result;
3079 }
3080 result = st->removeEffect(effect);
3081 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3082 return result;
3083}
3084
3085audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003086 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003087 if (t == nullptr) {
3088 return AUDIO_IO_HANDLE_NONE;
3089 }
3090 return t->id();
3091}
3092
3093bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003094 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003095 if (t == nullptr) {
3096 return true;
3097 }
3098 return t->isOutput();
3099}
3100
3101bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003102 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003103}
3104
3105bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003106 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003107}
3108
3109bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003110 switch (mThreadType) {
3111 case ThreadBase::OFFLOAD:
3112 case ThreadBase::MMAP_PLAYBACK:
3113 case ThreadBase::MMAP_CAPTURE:
3114 return true;
3115 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003116 return false;
3117 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003118}
3119
3120bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3121 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003122}
3123
3124uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003125 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003126 if (t == nullptr) {
3127 return 0;
3128 }
3129 return t->sampleRate();
3130}
3131
Eric Laurentf1f22e72021-07-13 14:04:14 +02003132audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3133 sp<ThreadBase> t = thread().promote();
3134 if (t == nullptr) {
3135 return AUDIO_CHANNEL_NONE;
3136 }
3137 sp<EffectChain> c = chain().promote();
3138 if (c == nullptr) {
3139 return AUDIO_CHANNEL_NONE;
3140 }
3141
Eric Laurentb62d0362021-10-26 17:40:18 +02003142 if (mThreadType == ThreadBase::SPATIALIZER) {
3143 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3144 if (c->isFirstEffect(id)) {
3145 return t->mixerChannelMask();
3146 } else {
3147 return t->channelMask();
3148 }
3149 } else if (!audio_is_global_session(c->sessionId())) {
3150 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3151 return t->mixerChannelMask();
3152 } else {
3153 return t->channelMask();
3154 }
3155 } else {
3156 return t->channelMask();
3157 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003158 } else {
3159 return t->channelMask();
3160 }
3161}
3162
3163uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003164 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003165}
3166
3167audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003168 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003169 if (t == nullptr) {
3170 return AUDIO_CHANNEL_NONE;
3171 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003172 sp<EffectChain> c = chain().promote();
3173 if (c == nullptr) {
3174 return AUDIO_CHANNEL_NONE;
3175 }
3176
3177 if (mThreadType == ThreadBase::SPATIALIZER) {
3178 if (!audio_is_global_session(c->sessionId())) {
3179 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3180 return t->mixerChannelMask();
3181 } else {
3182 return t->channelMask();
3183 }
3184 } else {
3185 return t->channelMask();
3186 }
3187 } else {
3188 return t->channelMask();
3189 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003190}
3191
Eric Laurentf1f22e72021-07-13 14:04:14 +02003192uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003193 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003194}
3195
jiabineb3bda02020-06-30 14:07:03 -07003196audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003197 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003198 if (t == nullptr) {
3199 return AUDIO_CHANNEL_NONE;
3200 }
3201 return t->hapticChannelMask();
3202}
3203
Eric Laurent6b446ce2019-12-13 10:56:31 -08003204size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003205 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003206 if (t == nullptr) {
3207 return 0;
3208 }
3209 return t->frameCount();
3210}
3211
Andy Hung920f6572022-10-06 12:09:49 -07003212uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3213NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3214{
Andy Hung328d6772021-01-12 12:32:21 -08003215 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003216 if (t == nullptr) {
3217 return 0;
3218 }
Andy Hung920f6572022-10-06 12:09:49 -07003219 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003220 return t->latency_l();
3221}
3222
Andy Hung920f6572022-10-06 12:09:49 -07003223void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3224NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3225{
Andy Hung328d6772021-01-12 12:32:21 -08003226 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003227 if (t == nullptr) {
3228 return;
3229 }
3230 t->setVolumeForOutput_l(left, right);
3231}
3232
3233void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003234 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003235 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003236 if (t == nullptr) {
3237 return;
3238 }
3239 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3240
Andy Hung328d6772021-01-12 12:32:21 -08003241 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003242 if (c == nullptr) {
3243 return;
3244 }
Eric Laurent41709552019-12-16 19:34:05 -08003245 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3246 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003247}
3248
Eric Laurent41709552019-12-16 19:34:05 -08003249void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003250 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003251 if (t == nullptr) {
3252 return;
3253 }
Eric Laurent41709552019-12-16 19:34:05 -08003254 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3255 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003256}
3257
Eric Laurent41709552019-12-16 19:34:05 -08003258void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003259 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3260
Andy Hung328d6772021-01-12 12:32:21 -08003261 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003262 if (t == nullptr) {
3263 return;
3264 }
3265 t->onEffectDisable();
3266}
3267
3268bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3269 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003270 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003271 if (t == nullptr) {
3272 return false;
3273 }
3274 t->disconnectEffectHandle(handle, unpinIfLast);
3275 return true;
3276}
3277
3278void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003279 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003280 if (c == nullptr) {
3281 return;
3282 }
3283 c->resetVolume_l();
3284
3285}
3286
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003287product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003288 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003289 if (c == nullptr) {
3290 return PRODUCT_STRATEGY_NONE;
3291 }
3292 return c->strategy();
3293}
3294
3295int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003296 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003297 if (c == nullptr) {
3298 return 0;
3299 }
3300 return c->activeTrackCnt();
3301}
3302
Eric Laurentb82e6b72019-11-22 17:25:04 -08003303
3304#undef LOG_TAG
3305#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3306
3307status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3308{
3309 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3310 Mutex::Autolock _l(mProxyLock);
3311 if (status == NO_ERROR) {
3312 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003313 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003314 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003315 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003316 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003317 bs = handle.second->disable(&status);
3318 }
3319 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003320 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003321 }
3322 }
3323 }
3324 ALOGV("%s enable %d status %d", __func__, enabled, status);
3325 return status;
3326}
3327
3328status_t AudioFlinger::DeviceEffectProxy::init(
3329 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3330//For all audio patches
3331//If src or sink device match
3332//If the effect is HW accelerated
3333// if no corresponding effect module
3334// Create EffectModule: mHalEffect
3335//Create and attach EffectHandle
3336//If the effect is not HW accelerated and the patch sink or src is a mixer port
3337// Create Effect on patch input or output thread on session -1
3338//Add EffectHandle to EffectHandle map of Effect Proxy:
3339 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3340 status_t status = NO_ERROR;
3341 for (auto &patch : patches) {
3342 status = onCreatePatch(patch.first, patch.second);
3343 ALOGV("%s onCreatePatch status %d", __func__, status);
3344 if (status == BAD_VALUE) {
3345 return status;
3346 }
3347 }
3348 return status;
3349}
3350
3351status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3352 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3353 status_t status = NAME_NOT_FOUND;
3354 sp<EffectHandle> handle;
3355 // only consider source[0] as this is the only "true" source of a patch
3356 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3357 ALOGV("%s source checkPort status %d", __func__, status);
3358 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3359 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3360 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3361 }
3362 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3363 Mutex::Autolock _l(mProxyLock);
3364 mEffectHandles.emplace(patchHandle, handle);
3365 }
3366 ALOGW_IF(status == BAD_VALUE,
3367 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3368
3369 return status;
3370}
3371
3372status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3373 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3374
3375 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3376 __func__, port->type, port->ext.device.type,
3377 port->ext.device.address, port->id, patch.isSoftware());
3378 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003379 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003380 return NAME_NOT_FOUND;
3381 }
3382 status_t status = NAME_NOT_FOUND;
3383
3384 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3385 Mutex::Autolock _l(mProxyLock);
3386 mDevicePort = *port;
3387 mHalEffect = new EffectModule(mMyCallback,
3388 const_cast<effect_descriptor_t *>(&mDescriptor),
3389 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3390 false /* pinned */, port->id);
3391 if (audio_is_input_device(mDevice.mType)) {
3392 mHalEffect->setInputDevice(mDevice);
3393 } else {
3394 mHalEffect->setDevices({mDevice});
3395 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003396 mHalEffect->configure();
3397
Eric Laurentde8caf42021-08-11 17:19:25 +02003398 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3399 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003400 status = (*handle)->initCheck();
3401 if (status == OK) {
3402 status = mHalEffect->addHandle((*handle).get());
3403 } else {
3404 mHalEffect.clear();
3405 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3406 }
3407 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3408 sp <ThreadBase> thread;
3409 if (audio_port_config_has_input_direction(port)) {
3410 if (patch.isSoftware()) {
3411 thread = patch.mRecord.thread();
3412 } else {
3413 thread = patch.thread().promote();
3414 }
3415 } else {
3416 if (patch.isSoftware()) {
3417 thread = patch.mPlayback.thread();
3418 } else {
3419 thread = patch.thread().promote();
3420 }
3421 }
3422 int enabled;
3423 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3424 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003425 &enabled, &status, false, false /*probe*/,
3426 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003427 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3428 } else {
3429 status = BAD_VALUE;
3430 }
3431 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003432 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003433 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003434 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003435 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003436 bs = (*handle)->disable(&status);
3437 }
3438 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003439 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003440 }
3441 }
3442 return status;
3443}
3444
3445void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003446 sp<EffectHandle> effect;
3447 {
3448 Mutex::Autolock _l(mProxyLock);
3449 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3450 effect = mEffectHandles.at(patchHandle);
3451 mEffectHandles.erase(patchHandle);
3452 }
3453 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003454}
3455
3456
3457size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3458{
3459 Mutex::Autolock _l(mProxyLock);
3460 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003461 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003462 mHalEffect.clear();
3463 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3464 }
3465 return mHalEffect == nullptr ? 0 : 1;
3466}
3467
3468status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003469 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003470 if (mHalEffect == nullptr) {
3471 return NO_INIT;
3472 }
3473 return mManagerCallback->addEffectToHal(
3474 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3475}
3476
3477status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003478 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003479 if (mHalEffect == nullptr) {
3480 return NO_INIT;
3481 }
3482 return mManagerCallback->removeEffectFromHal(
3483 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3484}
3485
3486bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3487 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3488 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3489 }
3490 return true;
3491}
3492
3493uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3494 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3495 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3496 return mDevicePort.sample_rate;
3497 }
3498 return DEFAULT_OUTPUT_SAMPLE_RATE;
3499}
3500
3501audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3502 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3503 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3504 return mDevicePort.channel_mask;
3505 }
3506 return AUDIO_CHANNEL_OUT_STEREO;
3507}
3508
3509uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3510 if (isOutput()) {
3511 return audio_channel_count_from_out_mask(channelMask());
3512 }
3513 return audio_channel_count_from_in_mask(channelMask());
3514}
3515
Andy Hung920f6572022-10-06 12:09:49 -07003516void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3517NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3518{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003519 const Vector<String16> args;
3520 EffectBase::dump(fd, args);
3521
3522 const bool locked = dumpTryLock(mProxyLock);
3523 if (!locked) {
3524 String8 result("DeviceEffectProxy may be deadlocked\n");
3525 write(fd, result.string(), result.size());
3526 }
3527
3528 String8 outStr;
3529 if (mHalEffect != nullptr) {
3530 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3531 } else {
3532 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3533 }
3534 write(fd, outStr.string(), outStr.size());
3535 outStr.clear();
3536
3537 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3538 write(fd, outStr.string(), outStr.size());
3539 outStr.clear();
3540
3541 for (const auto& iter : mEffectHandles) {
3542 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3543 write(fd, outStr.string(), outStr.size());
3544 outStr.clear();
3545 sp<EffectBase> effect = iter.second->effect().promote();
3546 if (effect != nullptr) {
3547 effect->dump(fd, args);
3548 }
3549 }
3550
3551 if (locked) {
3552 mLock.unlock();
3553 }
3554}
3555
3556#undef LOG_TAG
3557#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3558
3559int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3560 return mManagerCallback->newEffectId();
3561}
3562
3563
3564bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3565 EffectHandle *handle, bool unpinIfLast) {
3566 sp<EffectBase> effectBase = handle->effect().promote();
3567 if (effectBase == nullptr) {
3568 return false;
3569 }
3570
3571 sp<EffectModule> effect = effectBase->asEffectModule();
3572 if (effect == nullptr) {
3573 return false;
3574 }
3575
3576 // restore suspended effects if the disconnected handle was enabled and the last one.
3577 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3578 if (remove) {
3579 sp<DeviceEffectProxy> proxy = mProxy.promote();
3580 if (proxy != nullptr) {
3581 proxy->removeEffect(effect);
3582 }
3583 if (handle->enabled()) {
3584 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3585 }
3586 }
3587 return true;
3588}
3589
3590status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3591 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3592 sp<EffectHalInterface> *effect) {
3593 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3594}
3595
3596status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003597 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003598 sp<DeviceEffectProxy> proxy = mProxy.promote();
3599 if (proxy == nullptr) {
3600 return NO_INIT;
3601 }
3602 return proxy->addEffectToHal(effect);
3603}
3604
3605status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003606 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003607 sp<DeviceEffectProxy> proxy = mProxy.promote();
3608 if (proxy == nullptr) {
3609 return NO_INIT;
3610 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003611 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003612}
3613
3614bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3615 sp<DeviceEffectProxy> proxy = mProxy.promote();
3616 if (proxy == nullptr) {
3617 return true;
3618 }
3619 return proxy->isOutput();
3620}
3621
3622uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3623 sp<DeviceEffectProxy> proxy = mProxy.promote();
3624 if (proxy == nullptr) {
3625 return DEFAULT_OUTPUT_SAMPLE_RATE;
3626 }
3627 return proxy->sampleRate();
3628}
3629
Eric Laurentf1f22e72021-07-13 14:04:14 +02003630audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3631 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003632 sp<DeviceEffectProxy> proxy = mProxy.promote();
3633 if (proxy == nullptr) {
3634 return AUDIO_CHANNEL_OUT_STEREO;
3635 }
3636 return proxy->channelMask();
3637}
3638
Eric Laurentf1f22e72021-07-13 14:04:14 +02003639uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3640 sp<DeviceEffectProxy> proxy = mProxy.promote();
3641 if (proxy == nullptr) {
3642 return 2;
3643 }
3644 return proxy->channelCount();
3645}
3646
3647audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3648 sp<DeviceEffectProxy> proxy = mProxy.promote();
3649 if (proxy == nullptr) {
3650 return AUDIO_CHANNEL_OUT_STEREO;
3651 }
3652 return proxy->channelMask();
3653}
3654
3655uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003656 sp<DeviceEffectProxy> proxy = mProxy.promote();
3657 if (proxy == nullptr) {
3658 return 2;
3659 }
3660 return proxy->channelCount();
3661}
3662
Eric Laurent76c89f32021-12-03 17:13:23 +01003663void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3664 const sp<EffectBase>& effectBase) {
3665 sp<EffectModule> effect = effectBase->asEffectModule();
3666 if (effect == nullptr) {
3667 return;
3668 }
3669 effect->start();
3670}
3671
3672void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3673 const sp<EffectBase>& effectBase) {
3674 sp<EffectModule> effect = effectBase->asEffectModule();
3675 if (effect == nullptr) {
3676 return;
3677 }
3678 effect->stop();
3679}
3680
Glenn Kasten63238ef2015-03-02 15:50:29 -08003681} // namespace android