blob: 42e5a116b9616dd010002f9e6cc270bd0f4f2be9 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080047
48// ----------------------------------------------------------------------------
49
50// Note: the following macro is used for extremely verbose logging message. In
51// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
52// 0; but one side effect of this is to turn all LOGV's as well. Some messages
53// are so verbose that we want to suppress them even when we have ALOG_ASSERT
54// turned on. Do not uncomment the #def below unless you really know what you
55// are doing and want to see all of the extremely verbose messages.
56//#define VERY_VERY_VERBOSE_LOGGING
57#ifdef VERY_VERY_VERBOSE_LOGGING
58#define ALOGVV ALOGV
59#else
60#define ALOGVV(a...) do { } while(0)
61#endif
62
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090063#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
64
Eric Laurentca7cc822012-11-19 14:55:58 -080065namespace android {
66
Andy Hung1131b6e2020-12-08 20:47:45 -080067using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070068using binder::Status;
69
70namespace {
71
72// Append a POD value into a vector of bytes.
73template<typename T>
74void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
75 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
76 buffer->insert(buffer->end(), ar, ar + sizeof(T));
77}
78
79// Write a POD value into a vector of bytes (clears the previous buffer
80// content).
81template<typename T>
82void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
83 buffer->clear();
84 appendToBuffer(value, buffer);
85}
86
87} // namespace
88
Eric Laurentca7cc822012-11-19 14:55:58 -080089// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080090// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
92
93#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080094#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080095
Eric Laurent41709552019-12-16 19:34:05 -080096AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080097 effect_descriptor_t *desc,
98 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 audio_session_t sessionId,
100 bool pinned)
101 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800102 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800103 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurentca7cc822012-11-19 14:55:58 -0800105}
106
Eric Laurent41709552019-12-16 19:34:05 -0800107// must be called with EffectModule::mLock held
108status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800109{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurent41709552019-12-16 19:34:05 -0800111 ALOGV("setEnabled %p enabled %d", this, enabled);
112
113 if (enabled != isEnabled()) {
114 switch (mState) {
115 // going from disabled to enabled
116 case IDLE:
117 mState = STARTING;
118 break;
119 case STOPPED:
120 mState = RESTART;
121 break;
122 case STOPPING:
123 mState = ACTIVE;
124 break;
125
126 // going from enabled to disabled
127 case RESTART:
128 mState = STOPPED;
129 break;
130 case STARTING:
131 mState = IDLE;
132 break;
133 case ACTIVE:
134 mState = STOPPING;
135 break;
136 case DESTROYED:
137 return NO_ERROR; // simply ignore as we are being destroyed
138 }
139 for (size_t i = 1; i < mHandles.size(); i++) {
140 EffectHandle *h = mHandles[i];
141 if (h != NULL && !h->disconnected()) {
142 h->setEnabled(enabled);
143 }
144 }
145 }
146 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800147}
148
Eric Laurent41709552019-12-16 19:34:05 -0800149status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
150{
151 status_t status;
152 {
153 Mutex::Autolock _l(mLock);
154 status = setEnabled_l(enabled);
155 }
156 if (fromHandle) {
157 if (enabled) {
158 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700159 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800160 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800162 }
163 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800165 }
166 }
167 return status;
168}
169
170bool AudioFlinger::EffectBase::isEnabled() const
171{
172 switch (mState) {
173 case RESTART:
174 case STARTING:
175 case ACTIVE:
176 return true;
177 case IDLE:
178 case STOPPING:
179 case STOPPED:
180 case DESTROYED:
181 default:
182 return false;
183 }
184}
185
186void AudioFlinger::EffectBase::setSuspended(bool suspended)
187{
188 Mutex::Autolock _l(mLock);
189 mSuspended = suspended;
190}
191
192bool AudioFlinger::EffectBase::suspended() const
193{
194 Mutex::Autolock _l(mLock);
195 return mSuspended;
196}
197
198status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800199{
200 status_t status;
201
202 Mutex::Autolock _l(mLock);
203 int priority = handle->priority();
204 size_t size = mHandles.size();
205 EffectHandle *controlHandle = NULL;
206 size_t i;
207 for (i = 0; i < size; i++) {
208 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800209 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 continue;
211 }
212 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 if (h->priority() <= priority) {
217 break;
218 }
219 }
220 // if inserted in first place, move effect control from previous owner to this handle
221 if (i == 0) {
222 bool enabled = false;
223 if (controlHandle != NULL) {
224 enabled = controlHandle->enabled();
225 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
226 }
227 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
228 status = NO_ERROR;
229 } else {
230 status = ALREADY_EXISTS;
231 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700232 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800233 mHandles.insertAt(handle, i);
234 return status;
235}
236
Eric Laurent41709552019-12-16 19:34:05 -0800237status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700238{
239 status_t status = NO_ERROR;
240 bool doRegister = false;
241 bool registered = false;
242 bool doEnable = false;
243 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700244 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800245 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700246
247 {
248 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200249
250 if ((isInternal_l() && !mPolicyRegistered)
251 || !getCallback()->isAudioPolicyReady()) {
252 return NO_ERROR;
253 }
254
Eric Laurent6c796322019-04-09 14:13:17 -0700255 // register effect when first handle is attached and unregister when last handle is removed
256 if (mPolicyRegistered != mHandles.size() > 0) {
257 doRegister = true;
258 mPolicyRegistered = mHandles.size() > 0;
259 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700260 const auto callback = getCallback();
261 io = callback->io();
262 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700263 }
264 }
265 // enable effect when registered according to enable state requested by controlling handle
266 if (mHandles.size() > 0) {
267 EffectHandle *handle = controlHandle_l();
268 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
269 doEnable = true;
270 mPolicyEnabled = handle->enabled();
271 }
272 }
273 registered = mPolicyRegistered;
274 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100275 // The simultaneous release of two EffectHandles with the same EffectModule
276 // may cause us to call this method at the same time.
277 // This may deadlock under some circumstances (b/180941720). Avoid this.
278 if (!doRegister && !(registered && doEnable)) {
279 return NO_ERROR;
280 }
Eric Laurent6c796322019-04-09 14:13:17 -0700281 mPolicyLock.lock();
282 }
283 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
284 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
285 if (doRegister) {
286 if (registered) {
287 status = AudioSystem::registerEffect(
288 &mDescriptor,
289 io,
290 strategy,
291 mSessionId,
292 mId);
293 } else {
294 status = AudioSystem::unregisterEffect(mId);
295 }
296 }
297 if (registered && doEnable) {
298 status = AudioSystem::setEffectEnabled(mId, enabled);
299 }
300 mPolicyLock.unlock();
301
302 return status;
303}
304
305
Eric Laurent41709552019-12-16 19:34:05 -0800306ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800307{
308 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309 return removeHandle_l(handle);
310}
311
Eric Laurent41709552019-12-16 19:34:05 -0800312ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800313{
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 size_t size = mHandles.size();
315 size_t i;
316 for (i = 0; i < size; i++) {
317 if (mHandles[i] == handle) {
318 break;
319 }
320 }
321 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800322 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
323 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800324 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800325 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800326
327 mHandles.removeAt(i);
328 // if removed from first place, move effect control from this handle to next in line
329 if (i == 0) {
330 EffectHandle *h = controlHandle_l();
331 if (h != NULL) {
332 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
333 }
334 }
335
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530336 // Prevent calls to process() and other functions on effect interface from now on.
337 // The effect engine will be released by the destructor when the last strong reference on
338 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 if (mHandles.size() == 0 && !mPinned) {
340 mState = DESTROYED;
341 }
342
343 return mHandles.size();
344}
345
346// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800347AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800348{
349 // the first valid handle in the list has control over the module
350 for (size_t i = 0; i < mHandles.size(); i++) {
351 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800352 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 return h;
354 }
355 }
356
357 return NULL;
358}
359
Eric Laurentf10c7092016-12-06 17:09:56 -0800360// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800361ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800362{
Andy Hungfda44002021-06-03 17:23:16 -0700363 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800364 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700365 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800366 return mHandles.size();
367 }
368
Eric Laurentf10c7092016-12-06 17:09:56 -0800369 Mutex::Autolock _l(mLock);
370 ssize_t numHandles = removeHandle_l(handle);
371 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800372 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700373 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800375 }
376 return numHandles;
377}
378
Eric Laurent41709552019-12-16 19:34:05 -0800379bool AudioFlinger::EffectBase::purgeHandles()
380{
381 bool enabled = false;
382 Mutex::Autolock _l(mLock);
383 EffectHandle *handle = controlHandle_l();
384 if (handle != NULL) {
385 enabled = handle->enabled();
386 }
387 mHandles.clear();
388 return enabled;
389}
390
391void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700392 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800393}
394
395static String8 effectFlagsToString(uint32_t flags) {
396 String8 s;
397
398 s.append("conn. mode: ");
399 switch (flags & EFFECT_FLAG_TYPE_MASK) {
400 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
401 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
402 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
403 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
404 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("insert pref: ");
410 switch (flags & EFFECT_FLAG_INSERT_MASK) {
411 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
412 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
413 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
414 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 s.append("volume mgmt: ");
420 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
421 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
422 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
423 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
424 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
425 default: s.append("unknown/reserved"); break;
426 }
427 s.append(", ");
428
429 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
430 if (devind) {
431 s.append("device indication: ");
432 switch (devind) {
433 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
434 default: s.append("unknown/reserved"); break;
435 }
436 s.append(", ");
437 }
438
439 s.append("input mode: ");
440 switch (flags & EFFECT_FLAG_INPUT_MASK) {
441 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
442 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
443 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
444 default: s.append("not set"); break;
445 }
446 s.append(", ");
447
448 s.append("output mode: ");
449 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
450 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
451 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
452 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
453 default: s.append("not set"); break;
454 }
455 s.append(", ");
456
457 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
458 if (accel) {
459 s.append("hardware acceleration: ");
460 switch (accel) {
461 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
462 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
469 if (modeind) {
470 s.append("mode indication: ");
471 switch (modeind) {
472 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
479 if (srcind) {
480 s.append("source indication: ");
481 switch (srcind) {
482 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
483 default: s.append("unknown/reserved"); break;
484 }
485 s.append(", ");
486 }
487
488 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
489 s.append("offloadable, ");
490 }
491
492 int len = s.length();
493 if (s.length() > 2) {
494 (void) s.lockBuffer(len);
495 s.unlockBuffer(len - 2);
496 }
497 return s;
498}
499
500void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
Andy Hung71ba4b32022-10-06 12:09:49 -0700501NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800502{
503 String8 result;
504
505 result.appendFormat("\tEffect ID %d:\n", mId);
506
507 bool locked = AudioFlinger::dumpTryLock(mLock);
508 // failed to lock - AudioFlinger is probably deadlocked
509 if (!locked) {
510 result.append("\t\tCould not lock Fx mutex:\n");
511 }
512
513 result.append("\t\tSession State Registered Enabled Suspended:\n");
514 result.appendFormat("\t\t%05d %03d %s %s %s\n",
515 mSessionId, mState, mPolicyRegistered ? "y" : "n",
516 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
517
518 result.append("\t\tDescriptor:\n");
519 char uuidStr[64];
520 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
521 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
522 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
523 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
524 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
525 mDescriptor.apiVersion,
526 mDescriptor.flags,
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 Naganov59984db2022-04-19 21:21:23 +0000573 mAddedToHal(false),
574 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800575#ifdef FLOAT_EFFECT_CHAIN
576 , mSupportsFloat(false)
577#endif
578{
579 ALOGV("Constructor %p pinned %d", this, pinned);
580 int lStatus;
581
582 // create effect engine from effect factory
583 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800584 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800585 if (mStatus != NO_ERROR) {
586 return;
587 }
588 lStatus = init();
589 if (lStatus < 0) {
590 mStatus = lStatus;
591 goto Error;
592 }
593
594 setOffloaded(callback->isOffload(), callback->io());
595 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
596
597 return;
598Error:
599 mEffectInterface.clear();
600 ALOGV("Constructor Error %d", mStatus);
601}
602
603AudioFlinger::EffectModule::~EffectModule()
604{
605 ALOGV("Destructor %p", this);
606 if (mEffectInterface != 0) {
607 char uuidStr[64];
608 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
609 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
610 this, uuidStr);
611 release_l();
612 }
613
614}
615
Eric Laurentfa1e1232016-08-02 19:01:49 -0700616bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 Mutex::Autolock _l(mLock);
618
Eric Laurentfa1e1232016-08-02 19:01:49 -0700619 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800620 switch (mState) {
621 case RESTART:
622 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700623 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800624
625 case STARTING:
626 // clear auxiliary effect input buffer for next accumulation
627 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
628 memset(mConfig.inputCfg.buffer.raw,
629 0,
630 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
631 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 if (start_l() == NO_ERROR) {
633 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700634 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700635 } else {
636 mState = IDLE;
637 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800638 break;
639 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900640 // volume control for offload and direct threads must take effect immediately.
641 if (stop_l() == NO_ERROR
642 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700643 mDisableWaitCnt = mMaxDisableWaitCnt;
644 } else {
645 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
646 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 mState = STOPPED;
648 break;
649 case STOPPED:
650 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
651 // turn off sequence.
652 if (--mDisableWaitCnt == 0) {
653 reset_l();
654 mState = IDLE;
655 }
656 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200657 case ACTIVE:
658 for (size_t i = 0; i < mHandles.size(); i++) {
659 if (!mHandles[i]->disconnected()) {
660 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
661 }
662 }
663 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800664 default: //IDLE , ACTIVE, DESTROYED
665 break;
666 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700667
668 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800669}
670
671void AudioFlinger::EffectModule::process()
672{
673 Mutex::Autolock _l(mLock);
674
Mikhail Naganov022b9952017-01-04 16:36:51 -0800675 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800676 return;
677 }
678
rago94a1ee82017-07-21 15:11:02 -0700679 const uint32_t inChannelCount =
680 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
681 const uint32_t outChannelCount =
682 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
683 const bool auxType =
684 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
685
Andy Hungfa69ca32017-11-30 10:07:53 -0800686 // safeInputOutputSampleCount is 0 if the channel count between input and output
687 // buffers do not match. This prevents automatic accumulation or copying between the
688 // input and output effect buffers without an intermediary effect process.
689 // TODO: consider implementing channel conversion.
690 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700691 mInChannelCountRequested != mOutChannelCountRequested ? 0
692 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800693 mConfig.inputCfg.buffer.frameCount,
694 mConfig.outputCfg.buffer.frameCount);
695 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
696#ifdef FLOAT_EFFECT_CHAIN
697 accumulate_float(
698 mConfig.outputCfg.buffer.f32,
699 mConfig.inputCfg.buffer.f32,
700 safeInputOutputSampleCount);
701#else
702 accumulate_i16(
703 mConfig.outputCfg.buffer.s16,
704 mConfig.inputCfg.buffer.s16,
705 safeInputOutputSampleCount);
706#endif
707 };
708 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
709#ifdef FLOAT_EFFECT_CHAIN
710 memcpy(
711 mConfig.outputCfg.buffer.f32,
712 mConfig.inputCfg.buffer.f32,
713 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
714
715#else
716 memcpy(
717 mConfig.outputCfg.buffer.s16,
718 mConfig.inputCfg.buffer.s16,
719 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
720#endif
721 };
722
Eric Laurentca7cc822012-11-19 14:55:58 -0800723 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700724 int ret;
725 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700726 if (auxType) {
727 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800728 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700729#ifdef FLOAT_EFFECT_CHAIN
730 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800731#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700732 // Do in-place float conversion for auxiliary effect input buffer.
733 static_assert(sizeof(float) <= sizeof(int32_t),
734 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
735
Andy Hungfa69ca32017-11-30 10:07:53 -0800736 memcpy_to_float_from_q4_27(
737 mConfig.inputCfg.buffer.f32,
738 mConfig.inputCfg.buffer.s32,
739 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800740#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800741 } else
Andy Hung116a4982017-11-30 10:15:08 -0800742#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800743 {
Andy Hung116a4982017-11-30 10:15:08 -0800744#ifdef FLOAT_AUX
745 memcpy_to_i16_from_float(
746 mConfig.inputCfg.buffer.s16,
747 mConfig.inputCfg.buffer.f32,
748 mConfig.inputCfg.buffer.frameCount);
749#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 memcpy_to_i16_from_q4_27(
751 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700752 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800753 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800754#endif
rago94a1ee82017-07-21 15:11:02 -0700755 }
rago94a1ee82017-07-21 15:11:02 -0700756 }
757#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800758 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
759 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
760
761 if (!auxType && mInChannelCountRequested != inChannelCount) {
762 adjust_channels(
763 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
764 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
765 sizeof(float),
766 sizeof(float)
767 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
768 inBuffer = mInConversionBuffer;
769 }
770 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
771 && mOutChannelCountRequested != outChannelCount) {
772 adjust_selected_channels(
773 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
774 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
775 sizeof(float),
776 sizeof(float)
777 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
778 outBuffer = mOutConversionBuffer;
779 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
781 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800782 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800783 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
784 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700785 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 memcpy_to_i16_from_float(
787 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800790 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700791 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800793 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
795 goto data_bypass;
796 }
797 memcpy_to_i16_from_float(
798 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800799 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800800 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800801 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700802 }
803 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800804#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800805 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800806#ifdef FLOAT_EFFECT_CHAIN
807 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800808 sp<EffectBufferHalInterface> target =
809 mOutChannelCountRequested != outChannelCount
810 ? mOutConversionBuffer : mOutBuffer;
811
Andy Hungfa69ca32017-11-30 10:07:53 -0800812 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800813 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800814 mOutConversionBuffer->audioBuffer()->s16,
815 outChannelCount * mConfig.outputCfg.buffer.frameCount);
816 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800817 if (mOutChannelCountRequested != outChannelCount) {
818 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
819 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
820 sizeof(float),
821 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
822 }
rago94a1ee82017-07-21 15:11:02 -0700823#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700824 } else {
rago94a1ee82017-07-21 15:11:02 -0700825#ifdef FLOAT_EFFECT_CHAIN
826 data_bypass:
827#endif
828 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800829 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700830 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800831 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700832 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800833 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700834 }
835 }
836 ret = -ENODATA;
837 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800838
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 // force transition to IDLE state when engine is ready
840 if (mState == STOPPED && ret == -ENODATA) {
841 mDisableWaitCnt = 1;
842 }
843
844 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700845 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800846#ifdef FLOAT_AUX
847 const size_t size =
848 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
849#else
rago94a1ee82017-07-21 15:11:02 -0700850 const size_t size =
851 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800852#endif
rago94a1ee82017-07-21 15:11:02 -0700853 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 }
855 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700856 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800857 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
858 // If an insert effect is idle and input buffer is different from output buffer,
859 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700860 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700861 // similar handling with data_bypass above.
862 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
863 accumulateInputToOutput();
864 } else { // EFFECT_BUFFER_ACCESS_WRITE
865 copyInputToOutput();
866 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800867 }
868 }
869}
870
871void AudioFlinger::EffectModule::reset_l()
872{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700873 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800874 return;
875 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700876 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800877}
878
879status_t AudioFlinger::EffectModule::configure()
880{
rago94a1ee82017-07-21 15:11:02 -0700881 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700883 uint32_t size;
884 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700885 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700886
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700887 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700888 status = NO_INIT;
889 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 }
891
Eric Laurentca7cc822012-11-19 14:55:58 -0800892 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800893 // TODO: handle configuration of input (record) SW effects above the HAL,
894 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
895 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700896 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200897 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800898 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200899 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800900
901 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800902 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
903 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
904 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
905 mConfig.inputCfg.channels);
906 }
907#ifndef MULTICHANNEL_EFFECT_CHAIN
908 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
909 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
910 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
911 mConfig.outputCfg.channels);
912 }
913#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800914 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800915#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700916 // TODO: Update this logic when multichannel effects are implemented.
917 // For offloaded tracks consider mono output as stereo for proper effect initialization
918 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
919 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
920 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
921 ALOGV("Overriding effect input and output as STEREO");
922 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800923#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800924 }
jiabineb3bda02020-06-30 14:07:03 -0700925 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700926 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700927 mConfig.inputCfg.channels |= hapticChannelMask;
928 mConfig.outputCfg.channels |= hapticChannelMask;
929 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800930 mInChannelCountRequested =
931 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
932 mOutChannelCountRequested =
933 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700934
rago94a1ee82017-07-21 15:11:02 -0700935 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
936 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900937
938 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700939 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900940 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
941 ALOGV("Overriding effect input as 48kHz");
942 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700943 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900944 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800945 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
946 mConfig.inputCfg.bufferProvider.cookie = NULL;
947 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
948 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
949 mConfig.outputCfg.bufferProvider.cookie = NULL;
950 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
951 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
952 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
953 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800954 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800955 // always overwrites output buffer: input buffer == output buffer
956 // - in other sessions:
957 // last effect in the chain accumulates in output buffer: input buffer != output buffer
958 // other effect: overwrites output buffer: input buffer == output buffer
959 // Auxiliary effect:
960 // accumulates in output buffer: input buffer != output buffer
961 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700962 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800963 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
964 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700965 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800966 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov59984db2022-04-19 21:21:23 +0000967 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800968
Eric Laurent6b446ce2019-12-13 10:56:31 -0800969 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700970 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800971 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800972
973 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700974 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700975 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800976 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700977 &mConfig,
978 &size,
979 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700980 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800981 status = cmdStatus;
982 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800983
984#ifdef MULTICHANNEL_EFFECT_CHAIN
985 if (status != NO_ERROR &&
Mikhail Naganov59984db2022-04-19 21:21:23 +0000986 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800987 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
988 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
989 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700990 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
991 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800992 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
993 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
994 }
995 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
996 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
997 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
998 }
999 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -07001000 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -08001001 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -07001002 &mConfig,
1003 &size,
1004 &cmdStatus);
1005 if (status == NO_ERROR) {
1006 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001007 }
1008 }
1009#endif
1010
1011#ifdef FLOAT_EFFECT_CHAIN
1012 if (status == NO_ERROR) {
1013 mSupportsFloat = true;
1014 }
1015
1016 if (status != NO_ERROR) {
1017 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1018 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1019 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1020 size = sizeof(int);
1021 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1022 sizeof(mConfig),
1023 &mConfig,
1024 &size,
1025 &cmdStatus);
1026 if (status == NO_ERROR) {
1027 status = cmdStatus;
1028 }
1029 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001030 mSupportsFloat = false;
1031 ALOGVV("config worked with 16 bit");
1032 } else {
1033 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 }
rago94a1ee82017-07-21 15:11:02 -07001035 }
1036#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001037
rago94a1ee82017-07-21 15:11:02 -07001038 if (status == NO_ERROR) {
1039 // Establish Buffer strategy
1040 setInBuffer(mInBuffer);
1041 setOutBuffer(mOutBuffer);
1042
1043 // Update visualizer latency
1044 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1045 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1046 effect_param_t *p = (effect_param_t *)buf32;
1047
1048 p->psize = sizeof(uint32_t);
1049 p->vsize = sizeof(uint32_t);
1050 size = sizeof(int);
1051 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1052
Andy Hungfda44002021-06-03 17:23:16 -07001053 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001054
1055 *((int32_t *)p->data + 1)= latency;
1056 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1057 sizeof(effect_param_t) + 8,
1058 &buf32,
1059 &size,
1060 &cmdStatus);
1061 }
jiabin229f94d2022-08-23 16:37:30 -07001062
1063 if (isVolumeControl()) {
1064 // Force initializing the volume as 0 for volume control effect for safer ramping
1065 uint32_t left = 0;
1066 uint32_t right = 0;
1067 setVolumeInternal(&left, &right, true /*controller*/);
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 }
1070
Andy Hung05083ac2017-12-14 15:00:28 -08001071 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1072 mMaxDisableWaitCnt = (uint32_t)std::max(
1073 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1074 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1075 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001076
Eric Laurentd0ebb532013-04-02 16:41:41 -07001077exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001078 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001079 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001080 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 return status;
1082}
1083
1084status_t AudioFlinger::EffectModule::init()
1085{
1086 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001087 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001088 return NO_INIT;
1089 }
1090 status_t cmdStatus;
1091 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001092 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1093 0,
1094 NULL,
1095 &size,
1096 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001097 if (status == 0) {
1098 status = cmdStatus;
1099 }
1100 return status;
1101}
1102
Eric Laurent1b928682014-10-02 19:41:47 -07001103void AudioFlinger::EffectModule::addEffectToHal_l()
1104{
1105 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1106 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001107 if (mAddedToHal) {
1108 return;
1109 }
1110
Andy Hungfda44002021-06-03 17:23:16 -07001111 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001112 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001113 }
1114}
1115
Eric Laurentfa1e1232016-08-02 19:01:49 -07001116// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001117status_t AudioFlinger::EffectModule::start()
1118{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001119 status_t status;
1120 {
1121 Mutex::Autolock _l(mLock);
1122 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001123 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001124 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001125 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001126 }
1127 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001128}
1129
1130status_t AudioFlinger::EffectModule::start_l()
1131{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001132 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 return NO_INIT;
1134 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001135 if (mStatus != NO_ERROR) {
1136 return mStatus;
1137 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 status_t cmdStatus;
1139 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001140 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1141 0,
1142 NULL,
1143 &size,
1144 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 if (status == 0) {
1146 status = cmdStatus;
1147 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001148 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001149 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001150 }
1151 return status;
1152}
1153
1154status_t AudioFlinger::EffectModule::stop()
1155{
1156 Mutex::Autolock _l(mLock);
1157 return stop_l();
1158}
1159
1160status_t AudioFlinger::EffectModule::stop_l()
1161{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001162 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001163 return NO_INIT;
1164 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001165 if (mStatus != NO_ERROR) {
1166 return mStatus;
1167 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001168 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001169 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001170
1171 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001172 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1173 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1174 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001175 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001176 mSetVolumeReentrantTid = INVALID_PID;
1177 }
1178
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001179 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1180 0,
1181 NULL,
1182 &size,
1183 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001184 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001185 status = cmdStatus;
1186 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001187 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001188 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001189 }
1190 return status;
1191}
1192
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001193// must be called with EffectChain::mLock held
1194void AudioFlinger::EffectModule::release_l()
1195{
1196 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001197 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001198 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001199 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001200 mEffectInterface.clear();
1201 }
1202}
1203
Eric Laurent6b446ce2019-12-13 10:56:31 -08001204status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001205{
1206 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1207 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001208 if (!mAddedToHal) {
1209 return NO_ERROR;
1210 }
1211
Andy Hungfda44002021-06-03 17:23:16 -07001212 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001213 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001214 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001215 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001216}
1217
Andy Hunge4a1d912016-08-17 14:11:13 -07001218// round up delta valid if value and divisor are positive.
1219template <typename T>
1220static T roundUpDelta(const T &value, const T &divisor) {
1221 T remainder = value % divisor;
1222 return remainder == 0 ? 0 : divisor - remainder;
1223}
1224
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001225status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1226 const std::vector<uint8_t>& cmdData,
1227 int32_t maxReplySize,
1228 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001229{
1230 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001231 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001232
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001233 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001234 return NO_INIT;
1235 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001236 if (mStatus != NO_ERROR) {
1237 return mStatus;
1238 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1240 return -EINVAL;
1241 }
1242 size_t cmdSize = cmdData.size();
1243 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1244 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1245 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001246 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001247 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001248 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001249 android_errorWriteLog(0x534e4554, "33003822");
1250 return -EINVAL;
1251 }
1252 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001253 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001255 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001256 return -EINVAL;
1257 }
ragoe2759072016-11-22 18:02:48 -08001258 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001259 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001260 || param->psize > maxReplySize - sizeof(effect_param_t)
1261 || param->vsize > maxReplySize - sizeof(effect_param_t)
1262 - param->psize
1263 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1264 maxReplySize
1265 - sizeof(effect_param_t)
1266 - param->psize
1267 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001268 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1269 android_errorWriteLog(0x534e4554, "32705438");
1270 return -EINVAL;
1271 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001272 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001273 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1274 && // DEFERRED not generally used
1275 (param == nullptr
1276 || param->psize > cmdSize - sizeof(effect_param_t)
1277 || param->vsize > cmdSize - sizeof(effect_param_t)
1278 - param->psize
1279 || roundUpDelta(param->psize,
1280 (uint32_t) sizeof(int)) >
1281 cmdSize
1282 - sizeof(effect_param_t)
1283 - param->psize
1284 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001285 android_errorWriteLog(0x534e4554, "30204301");
1286 return -EINVAL;
1287 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001288 uint32_t replySize = maxReplySize;
1289 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001290 status_t status = mEffectInterface->command(cmdCode,
1291 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001292 const_cast<uint8_t*>(cmdData.data()),
1293 &replySize,
1294 reply->data());
1295 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001297 for (size_t i = 1; i < mHandles.size(); i++) {
1298 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001299 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001300 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001301 }
1302 }
1303 }
1304 return status;
1305}
1306
Eric Laurentca7cc822012-11-19 14:55:58 -08001307bool AudioFlinger::EffectModule::isProcessEnabled() const
1308{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001309 if (mStatus != NO_ERROR) {
1310 return false;
1311 }
1312
Eric Laurentca7cc822012-11-19 14:55:58 -08001313 switch (mState) {
1314 case RESTART:
1315 case ACTIVE:
1316 case STOPPING:
1317 case STOPPED:
1318 return true;
1319 case IDLE:
1320 case STARTING:
1321 case DESTROYED:
1322 default:
1323 return false;
1324 }
1325}
1326
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001327bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1328{
Andy Hungfda44002021-06-03 17:23:16 -07001329 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001330}
1331
1332bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1333{
1334 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1335}
1336
Mikhail Naganov022b9952017-01-04 16:36:51 -08001337void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001338 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001339
1340 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001341 if (buffer != 0) {
1342 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1343 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1344 } else {
1345 mConfig.inputCfg.buffer.raw = NULL;
1346 }
1347 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001348 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001349
1350#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001351 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001352 // Theoretically insert effects can also do in-place conversions (destroying
1353 // the original buffer) when the output buffer is identical to the input buffer,
1354 // but we don't optimize for it here.
1355 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001356 const uint32_t inChannelCount =
1357 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1358 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001359 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001360 // we need to translate - create hidl shared buffer and intercept
1361 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001362 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1363 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1364 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001365
1366 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1367 __func__, inChannels, inFrameCount, size);
1368
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001369 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001370 || size > mInConversionBuffer->getSize())) {
1371 mInConversionBuffer.clear();
1372 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001373 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001374 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001375 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001376 mInConversionBuffer->setFrameCount(inFrameCount);
1377 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001378 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001379 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001380 }
1381 }
1382#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001383}
1384
1385void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001386 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001387
1388 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001389 if (buffer != 0) {
1390 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1391 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1392 } else {
1393 mConfig.outputCfg.buffer.raw = NULL;
1394 }
1395 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001396 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001397
1398#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001399 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001400 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001401 const uint32_t outChannelCount =
1402 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1403 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001404 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001405 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001406 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1407 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1408 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001409
1410 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1411 __func__, outChannels, outFrameCount, size);
1412
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001413 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001414 || size > mOutConversionBuffer->getSize())) {
1415 mOutConversionBuffer.clear();
1416 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001417 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001418 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001419 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001420 mOutConversionBuffer->setFrameCount(outFrameCount);
1421 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001422 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001423 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001424 }
1425 }
1426#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001427}
1428
Eric Laurentca7cc822012-11-19 14:55:58 -08001429status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1430{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001431 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001432 if (mStatus != NO_ERROR) {
1433 return mStatus;
1434 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1437 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1438 if (isProcessEnabled() &&
1439 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001440 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1441 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001442 status = setVolumeInternal(left, right, controller);
1443 }
1444 return status;
1445}
1446
1447status_t AudioFlinger::EffectModule::setVolumeInternal(
1448 uint32_t *left, uint32_t *right, bool controller) {
1449 uint32_t volume[2] = {*left, *right};
1450 uint32_t *pVolume = controller ? volume : nullptr;
1451 uint32_t size = sizeof(volume);
1452 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1453 size,
1454 volume,
1455 &size,
1456 pVolume);
1457 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1458 *left = volume[0];
1459 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 }
1461 return status;
1462}
1463
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001464void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1465{
Zhou Songd505c642020-02-20 16:35:37 +08001466 // for offload or direct thread, if the effect chain has non-offloadable
1467 // effect and any effect module within the chain has volume control, then
1468 // volume control is delegated to effect, otherwise, set volume to hal.
1469 if (mEffectCallback->isOffloadOrDirect() &&
1470 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001471 float vol_l = (float)left / (1 << 24);
1472 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001473 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001474 }
1475}
1476
jiabin8f278ee2019-11-11 12:16:27 -08001477status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1478 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001479{
jiabin8f278ee2019-11-11 12:16:27 -08001480 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1481 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 return NO_ERROR;
1483 }
1484
1485 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001486 if (mStatus != NO_ERROR) {
1487 return mStatus;
1488 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001489 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001490 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 status_t cmdStatus;
1492 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001493 // FIXME: use audio device types and addresses when the hal interface is ready.
1494 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001495 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001496 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001497 &size,
1498 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001499 }
1500 return status;
1501}
1502
jiabin8f278ee2019-11-11 12:16:27 -08001503status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1504{
1505 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1506}
1507
1508status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1509{
1510 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1511}
1512
Eric Laurentca7cc822012-11-19 14:55:58 -08001513status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1514{
1515 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001516 if (mStatus != NO_ERROR) {
1517 return mStatus;
1518 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001519 status_t status = NO_ERROR;
1520 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1521 status_t cmdStatus;
1522 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001523 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1524 sizeof(audio_mode_t),
1525 &mode,
1526 &size,
1527 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 if (status == NO_ERROR) {
1529 status = cmdStatus;
1530 }
1531 }
1532 return status;
1533}
1534
1535status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1536{
1537 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001538 if (mStatus != NO_ERROR) {
1539 return mStatus;
1540 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001541 status_t status = NO_ERROR;
1542 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1543 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001544 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1545 sizeof(audio_source_t),
1546 &source,
1547 &size,
1548 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001549 }
1550 return status;
1551}
1552
Eric Laurent5baf2af2013-09-12 17:37:00 -07001553status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1554{
1555 Mutex::Autolock _l(mLock);
1556 if (mStatus != NO_ERROR) {
1557 return mStatus;
1558 }
1559 status_t status = NO_ERROR;
1560 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1561 status_t cmdStatus;
1562 uint32_t size = sizeof(status_t);
1563 effect_offload_param_t cmd;
1564
1565 cmd.isOffload = offloaded;
1566 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001567 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1568 sizeof(effect_offload_param_t),
1569 &cmd,
1570 &size,
1571 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001572 if (status == NO_ERROR) {
1573 status = cmdStatus;
1574 }
1575 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1576 } else {
1577 if (offloaded) {
1578 status = INVALID_OPERATION;
1579 }
1580 mOffloaded = false;
1581 }
1582 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1583 return status;
1584}
1585
1586bool AudioFlinger::EffectModule::isOffloaded() const
1587{
1588 Mutex::Autolock _l(mLock);
1589 return mOffloaded;
1590}
1591
jiabineb3bda02020-06-30 14:07:03 -07001592/*static*/
1593bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1594 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1595}
1596
1597bool AudioFlinger::EffectModule::isHapticGenerator() const {
1598 return isHapticGenerator(&mDescriptor.type);
1599}
1600
jiabine70bc7f2020-06-30 22:07:55 -07001601status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1602{
1603 if (mStatus != NO_ERROR) {
1604 return mStatus;
1605 }
1606 if (!isHapticGenerator()) {
1607 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1608 return INVALID_OPERATION;
1609 }
1610
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001611 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1612 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001613 param->psize = sizeof(int32_t);
1614 param->vsize = sizeof(int32_t) * 2;
1615 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1616 *((int32_t*)param->data + 1) = id;
1617 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001618 std::vector<uint8_t> response;
1619 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001620 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001621 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1622 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001623 }
1624 return status;
1625}
1626
Lais Andradebc3f37a2021-07-02 00:13:19 +01001627status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001628{
1629 if (mStatus != NO_ERROR) {
1630 return mStatus;
1631 }
1632 if (!isHapticGenerator()) {
1633 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1634 return INVALID_OPERATION;
1635 }
1636
Lais Andradebc3f37a2021-07-02 00:13:19 +01001637 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001638 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001639 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001640 effect_param_t *param = (effect_param_t*) request.data();
1641 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001642 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001643 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1644 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001645 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1646 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1647 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001648 std::vector<uint8_t> response;
1649 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1650 if (status == NO_ERROR) {
1651 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1652 status = *reinterpret_cast<const status_t*>(response.data());
1653 }
1654 return status;
1655}
1656
Mikhail Naganov59984db2022-04-19 21:21:23 +00001657status_t AudioFlinger::EffectModule::getConfigs(
1658 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1659 Mutex::Autolock _l(mLock);
1660 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1661 return NO_INIT;
1662 }
1663 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1664 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1665 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1666 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1667 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1668 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1669 *isOutput = mIsOutput;
1670 return NO_ERROR;
1671}
1672
Andy Hungbded9c82017-11-30 18:47:35 -08001673static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1674 std::stringstream ss;
1675
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001676 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001677 return "nullptr"; // make different than below
1678 } else if (buffer->externalData() != nullptr) {
1679 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1680 << " -> "
1681 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1682 } else {
1683 ss << buffer->audioBuffer()->raw;
1684 }
1685 return ss.str();
1686}
Marco Nelissenb2208842014-02-07 14:00:50 -08001687
Eric Laurent41709552019-12-16 19:34:05 -08001688void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07001689NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001690{
Eric Laurent41709552019-12-16 19:34:05 -08001691 EffectBase::dump(fd, args);
1692
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001694 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001695
Eric Laurent41709552019-12-16 19:34:05 -08001696 result.append("\t\tStatus Engine:\n");
1697 result.appendFormat("\t\t%03d %p\n",
1698 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001699
1700 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001701
1702 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001703 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1704 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1705 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 mConfig.inputCfg.buffer.frameCount,
1707 mConfig.inputCfg.samplingRate,
1708 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001709 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001710 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001711
1712 result.append("\t\t- Output configuration:\n");
1713 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001714 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001715 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 mConfig.outputCfg.buffer.frameCount,
1717 mConfig.outputCfg.samplingRate,
1718 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001719 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001720 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001721
rago94a1ee82017-07-21 15:11:02 -07001722#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001723
Andy Hungbded9c82017-11-30 18:47:35 -08001724 result.appendFormat("\t\t- HAL buffers:\n"
1725 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1726 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1727 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1728 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1729 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001730#endif
1731
Eric Laurentca7cc822012-11-19 14:55:58 -08001732 write(fd, result.string(), result.length());
1733
Mikhail Naganov4d547672019-02-22 14:19:19 -08001734 if (mEffectInterface != 0) {
1735 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1736 (void)mEffectInterface->dump(fd);
1737 }
1738
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 if (locked) {
1740 mLock.unlock();
1741 }
1742}
1743
1744// ----------------------------------------------------------------------------
1745// EffectHandle implementation
1746// ----------------------------------------------------------------------------
1747
1748#undef LOG_TAG
1749#define LOG_TAG "AudioFlinger::EffectHandle"
1750
Eric Laurent41709552019-12-16 19:34:05 -08001751AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001752 const sp<AudioFlinger::Client>& client,
1753 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001754 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001755 : BnEffect(),
1756 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001757 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1758 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001759{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001760 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001761 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001762
1763 if (client == 0) {
1764 return;
1765 }
1766 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1767 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001768 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001769 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001770 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001772 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001773 return;
1774 }
Glenn Kastene75da402013-11-20 13:54:52 -08001775 new(mCblk) effect_param_cblk_t();
1776 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001777}
1778
1779AudioFlinger::EffectHandle::~EffectHandle()
1780{
1781 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 disconnect(false);
1783}
1784
Andy Hungc747c532022-03-07 21:41:14 -08001785// Creates an association between Binder code to name for IEffect.
1786#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1787BINDER_METHOD_ENTRY(enable) \
1788BINDER_METHOD_ENTRY(disable) \
1789BINDER_METHOD_ENTRY(command) \
1790BINDER_METHOD_ENTRY(disconnect) \
1791BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001792BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001793
1794// singleton for Binder Method Statistics for IEffect
1795mediautils::MethodStatistics<int>& getIEffectStatistics() {
1796 using Code = int;
1797
1798#pragma push_macro("BINDER_METHOD_ENTRY")
1799#undef BINDER_METHOD_ENTRY
1800#define BINDER_METHOD_ENTRY(ENTRY) \
1801 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1802
1803 static mediautils::MethodStatistics<Code> methodStatistics{
1804 IEFFECT_BINDER_METHOD_MACRO_LIST
1805 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1806 };
1807#pragma pop_macro("BINDER_METHOD_ENTRY")
1808
1809 return methodStatistics;
1810}
1811
1812status_t AudioFlinger::EffectHandle::onTransact(
1813 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001814 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1815 mediautils::TimeCheck check(
1816 std::string("IEffect::").append(methodName),
1817 [code](bool timeout, float elapsedMs) {
1818 if (timeout) {
1819 ; // we don't timeout right now on the effect interface.
1820 } else {
1821 getIEffectStatistics().event(code, elapsedMs);
1822 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001823 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001824 return BnEffect::onTransact(code, data, reply, flags);
1825}
1826
Glenn Kastene75da402013-11-20 13:54:52 -08001827status_t AudioFlinger::EffectHandle::initCheck()
1828{
1829 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1830}
1831
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001832#define RETURN(code) \
1833 *_aidl_return = (code); \
1834 return Status::ok();
1835
Mikhail Naganov59984db2022-04-19 21:21:23 +00001836#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1837 ({ \
1838 auto _tmp = (exp); \
1839 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1840 std::move(_tmp.value()); \
1841 })
1842
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001843Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001844{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001846 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001847 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001848 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001852 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001854
1855 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001857 }
1858
1859 mEnabled = true;
1860
Eric Laurent6c796322019-04-09 14:13:17 -07001861 status_t status = effect->updatePolicyState();
1862 if (status != NO_ERROR) {
1863 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001864 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001865 }
1866
Eric Laurent6b446ce2019-12-13 10:56:31 -08001867 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001868
1869 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001870 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 }
1873
Eric Laurent6b446ce2019-12-13 10:56:31 -08001874 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001876 mEnabled = false;
1877 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001878 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001879}
1880
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001881Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001882{
1883 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001884 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001885 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001886 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001887 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001888 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001889 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001891 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001892
1893 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001894 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001895 }
1896 mEnabled = false;
1897
Eric Laurent6c796322019-04-09 14:13:17 -07001898 effect->updatePolicyState();
1899
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001900 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001902 }
1903
Eric Laurent6b446ce2019-12-13 10:56:31 -08001904 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001906}
1907
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001909{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001910 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001911 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001913}
1914
1915void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1916{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 AutoMutex _l(mLock);
1918 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1919 if (mDisconnected) {
1920 if (unpinIfLast) {
1921 android_errorWriteLog(0x534e4554, "32707507");
1922 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001923 return;
1924 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001925 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001926 {
Eric Laurent41709552019-12-16 19:34:05 -08001927 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001928 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001929 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001930 ALOGW("%s Effect handle %p disconnected after thread destruction",
1931 __func__, this);
1932 }
1933 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001934 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001935 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001936
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 if (mClient != 0) {
1938 if (mCblk != NULL) {
1939 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1940 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1941 }
1942 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001943 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung71ba4b32022-10-06 12:09:49 -07001944 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001945 mClient.clear();
1946 }
1947}
1948
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001949Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1950 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1951 return Status::ok();
1952}
1953
Mikhail Naganov59984db2022-04-19 21:21:23 +00001954Status AudioFlinger::EffectHandle::getConfig(
1955 media::EffectConfig* _config, int32_t* _aidl_return) {
1956 AutoMutex _l(mLock);
1957 sp<EffectBase> effect = mEffect.promote();
1958 if (effect == nullptr || mDisconnected) {
1959 RETURN(DEAD_OBJECT);
1960 }
1961 sp<EffectModule> effectModule = effect->asEffectModule();
1962 if (effectModule == nullptr) {
1963 RETURN(INVALID_OPERATION);
1964 }
1965 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1966 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1967 bool isOutput;
1968 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1969 if (status == NO_ERROR) {
1970 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1971 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1972 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1973 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1974 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1975 _config->isOnInputStream = !isOutput;
1976 }
1977 RETURN(status);
1978}
1979
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001980Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1981 const std::vector<uint8_t>& cmdData,
1982 int32_t maxResponseSize,
1983 std::vector<uint8_t>* response,
1984 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001985{
1986 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001987 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001988
Eric Laurentc7ab3092017-06-15 18:43:46 -07001989 // reject commands reserved for internal use by audio framework if coming from outside
1990 // of audioserver
1991 switch(cmdCode) {
1992 case EFFECT_CMD_ENABLE:
1993 case EFFECT_CMD_DISABLE:
1994 case EFFECT_CMD_SET_PARAM:
1995 case EFFECT_CMD_SET_PARAM_DEFERRED:
1996 case EFFECT_CMD_SET_PARAM_COMMIT:
1997 case EFFECT_CMD_GET_PARAM:
1998 break;
1999 default:
2000 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2001 break;
2002 }
2003 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002004 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002005 }
2006
Eric Laurent1ffc5852016-12-15 14:46:09 -08002007 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07002008 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002009 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002010 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002011 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002012 writeToBuffer(NO_ERROR, response);
2013 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002014 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07002015 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002016 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002018 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 writeToBuffer(NO_ERROR, response);
2020 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002021 }
2022
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002023 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002024 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002025 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002026 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002027 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002028 // only get parameter command is permitted for applications not controlling the effect
2029 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002030 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002032
2033 // handle commands that are not forwarded transparently to effect engine
2034 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002035 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002036 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002037 }
2038
Andy Hung71ba4b32022-10-06 12:09:49 -07002039 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002040 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002041 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002042 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002044
Eric Laurentca7cc822012-11-19 14:55:58 -08002045 // No need to trylock() here as this function is executed in the binder thread serving a
2046 // particular client process: no risk to block the whole media server process or mixer
2047 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07002048 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002049 // keep local copy of index in case of client corruption b/32220769
2050 const uint32_t clientIndex = mCblk->clientIndex;
2051 const uint32_t serverIndex = mCblk->serverIndex;
2052 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2053 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 mCblk->serverIndex = 0;
2055 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002056 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002057 }
2058 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002059 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002060 for (uint32_t index = serverIndex; index < clientIndex;) {
2061 int *p = (int *)(mBuffer + index);
2062 const int size = *p++;
2063 if (size < 0
2064 || size > EFFECT_PARAM_BUFFER_SIZE
2065 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002067 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002068 break;
2069 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002070
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002071 std::copy(reinterpret_cast<const uint8_t*>(p),
2072 reinterpret_cast<const uint8_t*>(p) + size,
2073 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002074
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002075 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002076 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002077 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002078 sizeof(int),
2079 &replyBuffer);
2080 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002081
2082 // verify shared memory: server index shouldn't change; client index can't go back.
2083 if (serverIndex != mCblk->serverIndex
2084 || clientIndex > mCblk->clientIndex) {
2085 android_errorWriteLog(0x534e4554, "32220769");
2086 status = BAD_VALUE;
2087 break;
2088 }
2089
Eric Laurentca7cc822012-11-19 14:55:58 -08002090 // stop at first error encountered
2091 if (ret != NO_ERROR) {
2092 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002093 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 break;
2095 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002096 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002097 break;
2098 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002099 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002100 }
2101 mCblk->serverIndex = 0;
2102 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002103 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 }
2105
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002106 status_t status = effect->command(cmdCode,
2107 cmdData,
2108 maxResponseSize,
2109 response);
2110 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002111}
2112
2113void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2114{
2115 ALOGV("setControl %p control %d", this, hasControl);
2116
2117 mHasControl = hasControl;
2118 mEnabled = enabled;
2119
2120 if (signal && mEffectClient != 0) {
2121 mEffectClient->controlStatusChanged(hasControl);
2122 }
2123}
2124
2125void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002126 const std::vector<uint8_t>& cmdData,
2127 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002128{
2129 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002130 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002131 }
2132}
2133
2134
2135
2136void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2137{
2138 if (mEffectClient != 0) {
2139 mEffectClient->enableStatusChanged(enabled);
2140 }
2141}
2142
Eric Laurentde8caf42021-08-11 17:19:25 +02002143void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2144{
2145 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2146 mEffectClient->framesProcessed(frames);
2147 }
2148}
2149
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002150void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung71ba4b32022-10-06 12:09:49 -07002151NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002152{
2153 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2154
Marco Nelissenb2208842014-02-07 14:00:50 -08002155 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002156 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002157 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002158 mHasControl ? "yes" : "no",
2159 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002160 mCblk ? mCblk->clientIndex : 0,
2161 mCblk ? mCblk->serverIndex : 0
2162 );
2163
2164 if (locked) {
2165 mCblk->lock.unlock();
2166 }
2167}
2168
2169#undef LOG_TAG
2170#define LOG_TAG "AudioFlinger::EffectChain"
2171
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002172AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2173 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002174 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002175 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002176 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002177 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002178{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002179 sp<ThreadBase> p = thread.promote();
2180 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002181 return;
2182 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002183 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002184 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2185 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002186}
2187
2188AudioFlinger::EffectChain::~EffectChain()
2189{
Eric Laurentca7cc822012-11-19 14:55:58 -08002190}
2191
2192// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2193sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2194 effect_descriptor_t *descriptor)
2195{
2196 size_t size = mEffects.size();
2197
2198 for (size_t i = 0; i < size; i++) {
2199 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2200 return mEffects[i];
2201 }
2202 }
2203 return 0;
2204}
2205
2206// getEffectFromId_l() must be called with ThreadBase::mLock held
2207sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2208{
2209 size_t size = mEffects.size();
2210
2211 for (size_t i = 0; i < size; i++) {
2212 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2213 if (id == 0 || mEffects[i]->id() == id) {
2214 return mEffects[i];
2215 }
2216 }
2217 return 0;
2218}
2219
2220// getEffectFromType_l() must be called with ThreadBase::mLock held
2221sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2222 const effect_uuid_t *type)
2223{
2224 size_t size = mEffects.size();
2225
2226 for (size_t i = 0; i < size; i++) {
2227 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2228 return mEffects[i];
2229 }
2230 }
2231 return 0;
2232}
2233
Eric Laurent6c796322019-04-09 14:13:17 -07002234std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2235{
2236 std::vector<int> ids;
2237 Mutex::Autolock _l(mLock);
2238 for (size_t i = 0; i < mEffects.size(); i++) {
2239 ids.push_back(mEffects[i]->id());
2240 }
2241 return ids;
2242}
2243
Eric Laurentca7cc822012-11-19 14:55:58 -08002244void AudioFlinger::EffectChain::clearInputBuffer()
2245{
2246 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002247 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002248}
2249
2250// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002251void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002252{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002253 if (mInBuffer == NULL) {
2254 return;
2255 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002256 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2257 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002258
Eric Laurent6b446ce2019-12-13 10:56:31 -08002259 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002260 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002261}
2262
2263// Must be called with EffectChain::mLock locked
2264void AudioFlinger::EffectChain::process_l()
2265{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002266 // never process effects when:
2267 // - on an OFFLOAD thread
2268 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002269 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002270 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002271 bool tracksOnSession = (trackCnt() != 0);
2272
2273 if (!tracksOnSession && mTailBufferCount == 0) {
2274 doProcess = false;
2275 }
2276
2277 if (activeTrackCnt() == 0) {
2278 // if no track is active and the effect tail has not been rendered,
2279 // the input buffer must be cleared here as the mixer process will not do it
2280 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002281 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 if (mTailBufferCount > 0) {
2283 mTailBufferCount--;
2284 }
2285 }
2286 }
2287 }
2288
2289 size_t size = mEffects.size();
2290 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002291 // Only the input and output buffers of the chain can be external,
2292 // and 'update' / 'commit' do nothing for allocated buffers, thus
2293 // it's not needed to consider any other buffers here.
2294 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002295 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2296 mOutBuffer->update();
2297 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002298 for (size_t i = 0; i < size; i++) {
2299 mEffects[i]->process();
2300 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002301 mInBuffer->commit();
2302 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2303 mOutBuffer->commit();
2304 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002306 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002307 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002308 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2309 }
2310 if (doResetVolume) {
2311 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 }
2313}
2314
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002315// createEffect_l() must be called with ThreadBase::mLock held
2316status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002317 effect_descriptor_t *desc,
2318 int id,
2319 audio_session_t sessionId,
2320 bool pinned)
2321{
2322 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002323 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002324 status_t lStatus = effect->status();
2325 if (lStatus == NO_ERROR) {
2326 lStatus = addEffect_ll(effect);
2327 }
2328 if (lStatus != NO_ERROR) {
2329 effect.clear();
2330 }
2331 return lStatus;
2332}
2333
2334// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002335status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2336{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002337 Mutex::Autolock _l(mLock);
2338 return addEffect_ll(effect);
2339}
2340// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2341status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2342{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002343 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002344
Eric Laurentb62d0362021-10-26 17:40:18 +02002345 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002346 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2347 // Auxiliary effects are inserted at the beginning of mEffects vector as
2348 // they are processed first and accumulated in chain input buffer
2349 mEffects.insertAt(effect, 0);
2350
2351 // the input buffer for auxiliary effect contains mono samples in
2352 // 32 bit format. This is to avoid saturation in AudoMixer
2353 // accumulation stage. Saturation is done in EffectModule::process() before
2354 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002355 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002356 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002357#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002358 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002359 numSamples * sizeof(float), &halBuffer);
2360#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002361 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002362 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002363#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002364 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002365
2366 effect->configure();
2367
Mikhail Naganov022b9952017-01-04 16:36:51 -08002368 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002369 // auxiliary effects output samples to chain input buffer for further processing
2370 // by insert effects
2371 effect->setOutBuffer(mInBuffer);
2372 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002373 ssize_t idx_insert = getInsertIndex(desc);
2374 if (idx_insert < 0) {
2375 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002376 }
2377
Eric Laurentb62d0362021-10-26 17:40:18 +02002378 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002379 mEffects.insertAt(effect, idx_insert);
2380
2381 effect->configure();
2382
Eric Laurentb62d0362021-10-26 17:40:18 +02002383 // - By default:
2384 // All effects read samples from chain input buffer.
2385 // The last effect in the chain, writes samples to chain output buffer,
2386 // otherwise to chain input buffer
2387 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2388 // The spatializer effect (first effect) reads samples from the input buffer
2389 // and writes samples to the output buffer.
2390 // All other effects read and writes samples to the output buffer
2391 if (mEffectCallback->isSpatializer()
2392 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002393 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002394 if (idx_insert == 0) {
2395 if (previousSize != 0) {
2396 mEffects[1]->configure();
2397 mEffects[1]->setInBuffer(mOutBuffer);
2398 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2399 }
2400 effect->setInBuffer(mInBuffer);
2401 } else {
2402 effect->setInBuffer(mOutBuffer);
2403 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002404 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002405 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002406 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002407 if (idx_insert != 0) {
2408 mEffects[idx_insert-1]->configure();
2409 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2410 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2411 }
2412 effect->setOutBuffer(mOutBuffer);
2413 } else {
2414 effect->setOutBuffer(mInBuffer);
2415 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002417 ALOGV("%s effect %p, added in chain %p at rank %zu",
2418 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 }
2420 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002421
Eric Laurentca7cc822012-11-19 14:55:58 -08002422 return NO_ERROR;
2423}
2424
Eric Laurentb62d0362021-10-26 17:40:18 +02002425ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2426 // Insert effects are inserted at the end of mEffects vector as they are processed
2427 // after track and auxiliary effects.
2428 // Insert effect order as a function of indicated preference:
2429 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2430 // another effect is present
2431 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2432 // last effect claiming first position
2433 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2434 // first effect claiming last position
2435 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2436 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2437 // already present
2438 // Spatializer or Downmixer effects are inserted in first position because
2439 // they adapt the channel count for all other effects in the chain
2440 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2441 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2442 return 0;
2443 }
2444
2445 size_t size = mEffects.size();
2446 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2447 ssize_t idx_insert;
2448 ssize_t idx_insert_first = -1;
2449 ssize_t idx_insert_last = -1;
2450
2451 idx_insert = size;
2452 for (size_t i = 0; i < size; i++) {
2453 effect_descriptor_t d = mEffects[i]->desc();
2454 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2455 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2456 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2457 // check invalid effect chaining combinations
2458 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2459 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2460 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2461 __func__, desc.name, d.name);
2462 return -1;
2463 }
2464 // remember position of first insert effect and by default
2465 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002466 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002467 idx_insert = i;
2468 }
2469 // remember position of last insert effect claiming
2470 // first position
2471 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2472 idx_insert_first = i;
2473 }
2474 // remember position of first insert effect claiming
2475 // last position
2476 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2477 idx_insert_last == -1) {
2478 idx_insert_last = i;
2479 }
2480 }
2481 }
2482
2483 // modify idx_insert from first position if needed
2484 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2485 if (idx_insert_last != -1) {
2486 idx_insert = idx_insert_last;
2487 } else {
2488 idx_insert = size;
2489 }
2490 } else {
2491 if (idx_insert_first != -1) {
2492 idx_insert = idx_insert_first + 1;
2493 }
2494 }
2495 return idx_insert;
2496}
2497
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002498// removeEffect_l() must be called with ThreadBase::mLock held
2499size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2500 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002501{
2502 Mutex::Autolock _l(mLock);
2503 size_t size = mEffects.size();
2504 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2505
2506 for (size_t i = 0; i < size; i++) {
2507 if (effect == mEffects[i]) {
2508 // calling stop here will remove pre-processing effect from the audio HAL.
2509 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2510 // the middle of a read from audio HAL
2511 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2512 mEffects[i]->state() == EffectModule::STOPPING) {
2513 mEffects[i]->stop();
2514 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002515 if (release) {
2516 mEffects[i]->release_l();
2517 }
2518
Mikhail Naganov022b9952017-01-04 16:36:51 -08002519 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002520 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002521 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002522 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002523 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002524 }
2525 }
2526 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002527
2528 // make sure the input buffer configuration for the new first effect in the chain
2529 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2530 if (i == 0 && size > 1) {
2531 mEffects[0]->configure();
2532 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002533 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002534 }
2535
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002536 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002537 this, i);
2538 break;
2539 }
2540 }
2541
2542 return mEffects.size();
2543}
2544
jiabin8f278ee2019-11-11 12:16:27 -08002545// setDevices_l() must be called with ThreadBase::mLock held
2546void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002547{
2548 size_t size = mEffects.size();
2549 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002550 mEffects[i]->setDevices(devices);
2551 }
2552}
2553
2554// setInputDevice_l() must be called with ThreadBase::mLock held
2555void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2556{
2557 size_t size = mEffects.size();
2558 for (size_t i = 0; i < size; i++) {
2559 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002560 }
2561}
2562
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002563// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002564void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2565{
2566 size_t size = mEffects.size();
2567 for (size_t i = 0; i < size; i++) {
2568 mEffects[i]->setMode(mode);
2569 }
2570}
2571
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002572// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002573void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2574{
2575 size_t size = mEffects.size();
2576 for (size_t i = 0; i < size; i++) {
2577 mEffects[i]->setAudioSource(source);
2578 }
2579}
2580
Zhou Songd505c642020-02-20 16:35:37 +08002581bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2582 for (const auto &effect : mEffects) {
2583 if (effect->isVolumeControlEnabled()) return true;
2584 }
2585 return false;
2586}
2587
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002588// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002589bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002590{
2591 uint32_t newLeft = *left;
2592 uint32_t newRight = *right;
2593 bool hasControl = false;
2594 int ctrlIdx = -1;
2595 size_t size = mEffects.size();
2596
2597 // first update volume controller
2598 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002599 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002600 ctrlIdx = i - 1;
2601 hasControl = true;
2602 break;
2603 }
2604 }
2605
Eric Laurentfa1e1232016-08-02 19:01:49 -07002606 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002607 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002608 if (hasControl) {
2609 *left = mNewLeftVolume;
2610 *right = mNewRightVolume;
2611 }
2612 return hasControl;
2613 }
2614
2615 mVolumeCtrlIdx = ctrlIdx;
2616 mLeftVolume = newLeft;
2617 mRightVolume = newRight;
2618
2619 // second get volume update from volume controller
2620 if (ctrlIdx >= 0) {
2621 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2622 mNewLeftVolume = newLeft;
2623 mNewRightVolume = newRight;
2624 }
2625 // then indicate volume to all other effects in chain.
2626 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002627 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002628 uint32_t lVol = newLeft;
2629 uint32_t rVol = newRight;
2630
2631 for (size_t i = 0; i < size; i++) {
2632 if ((int)i == ctrlIdx) {
2633 continue;
2634 }
2635 // this also works for ctrlIdx == -1 when there is no volume controller
2636 if ((int)i > ctrlIdx) {
2637 lVol = *left;
2638 rVol = *right;
2639 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002640 // Pass requested volume directly if this is volume monitor module
2641 if (mEffects[i]->isVolumeMonitor()) {
2642 mEffects[i]->setVolume(left, right, false);
2643 } else {
2644 mEffects[i]->setVolume(&lVol, &rVol, false);
2645 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002646 }
2647 *left = newLeft;
2648 *right = newRight;
2649
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002650 setVolumeForOutput_l(*left, *right);
2651
Eric Laurentca7cc822012-11-19 14:55:58 -08002652 return hasControl;
2653}
2654
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002655// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002656void AudioFlinger::EffectChain::resetVolume_l()
2657{
Eric Laurente7449bf2016-08-03 18:44:07 -07002658 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2659 uint32_t left = mLeftVolume;
2660 uint32_t right = mRightVolume;
2661 (void)setVolume_l(&left, &right, true);
2662 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002663}
2664
jiabineb3bda02020-06-30 14:07:03 -07002665// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2666bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2667{
2668 for (size_t i = 0; i < mEffects.size(); ++i) {
2669 if (mEffects[i]->isHapticGenerator()) {
2670 return true;
2671 }
2672 }
2673 return false;
2674}
2675
jiabine70bc7f2020-06-30 22:07:55 -07002676void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2677{
2678 Mutex::Autolock _l(mLock);
2679 for (size_t i = 0; i < mEffects.size(); ++i) {
2680 mEffects[i]->setHapticIntensity(id, intensity);
2681 }
2682}
2683
Eric Laurent1b928682014-10-02 19:41:47 -07002684void AudioFlinger::EffectChain::syncHalEffectsState()
2685{
2686 Mutex::Autolock _l(mLock);
2687 for (size_t i = 0; i < mEffects.size(); i++) {
2688 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2689 mEffects[i]->state() == EffectModule::STOPPING) {
2690 mEffects[i]->addEffectToHal_l();
2691 }
2692 }
2693}
2694
Eric Laurentca7cc822012-11-19 14:55:58 -08002695void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07002696NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002697{
Eric Laurentca7cc822012-11-19 14:55:58 -08002698 String8 result;
2699
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002700 const size_t numEffects = mEffects.size();
2701 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002702
Marco Nelissenb2208842014-02-07 14:00:50 -08002703 if (numEffects) {
2704 bool locked = AudioFlinger::dumpTryLock(mLock);
2705 // failed to lock - AudioFlinger is probably deadlocked
2706 if (!locked) {
2707 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002708 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002709
Andy Hungbded9c82017-11-30 18:47:35 -08002710 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2711 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2712 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2713 (int)inBufferStr.size(), "In buffer ",
2714 (int)outBufferStr.size(), "Out buffer ");
2715 result.appendFormat("\t%s %s %d\n",
2716 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002717 write(fd, result.string(), result.size());
2718
2719 for (size_t i = 0; i < numEffects; ++i) {
2720 sp<EffectModule> effect = mEffects[i];
2721 if (effect != 0) {
2722 effect->dump(fd, args);
2723 }
2724 }
2725
2726 if (locked) {
2727 mLock.unlock();
2728 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002729 } else {
2730 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002731 }
2732}
2733
2734// must be called with ThreadBase::mLock held
2735void AudioFlinger::EffectChain::setEffectSuspended_l(
2736 const effect_uuid_t *type, bool suspend)
2737{
2738 sp<SuspendedEffectDesc> desc;
2739 // use effect type UUID timelow as key as there is no real risk of identical
2740 // timeLow fields among effect type UUIDs.
2741 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2742 if (suspend) {
2743 if (index >= 0) {
2744 desc = mSuspendedEffects.valueAt(index);
2745 } else {
2746 desc = new SuspendedEffectDesc();
2747 desc->mType = *type;
2748 mSuspendedEffects.add(type->timeLow, desc);
2749 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2750 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002751
Eric Laurentca7cc822012-11-19 14:55:58 -08002752 if (desc->mRefCount++ == 0) {
2753 sp<EffectModule> effect = getEffectIfEnabled(type);
2754 if (effect != 0) {
2755 desc->mEffect = effect;
2756 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002757 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002758 }
2759 }
2760 } else {
2761 if (index < 0) {
2762 return;
2763 }
2764 desc = mSuspendedEffects.valueAt(index);
2765 if (desc->mRefCount <= 0) {
2766 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002767 desc->mRefCount = 0;
2768 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002769 }
2770 if (--desc->mRefCount == 0) {
2771 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2772 if (desc->mEffect != 0) {
2773 sp<EffectModule> effect = desc->mEffect.promote();
2774 if (effect != 0) {
2775 effect->setSuspended(false);
2776 effect->lock();
2777 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002778 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002779 effect->setEnabled_l(handle->enabled());
2780 }
2781 effect->unlock();
2782 }
2783 desc->mEffect.clear();
2784 }
2785 mSuspendedEffects.removeItemsAt(index);
2786 }
2787 }
2788}
2789
2790// must be called with ThreadBase::mLock held
2791void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2792{
2793 sp<SuspendedEffectDesc> desc;
2794
2795 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2796 if (suspend) {
2797 if (index >= 0) {
2798 desc = mSuspendedEffects.valueAt(index);
2799 } else {
2800 desc = new SuspendedEffectDesc();
2801 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2802 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2803 }
2804 if (desc->mRefCount++ == 0) {
2805 Vector< sp<EffectModule> > effects;
2806 getSuspendEligibleEffects(effects);
2807 for (size_t i = 0; i < effects.size(); i++) {
2808 setEffectSuspended_l(&effects[i]->desc().type, true);
2809 }
2810 }
2811 } else {
2812 if (index < 0) {
2813 return;
2814 }
2815 desc = mSuspendedEffects.valueAt(index);
2816 if (desc->mRefCount <= 0) {
2817 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2818 desc->mRefCount = 1;
2819 }
2820 if (--desc->mRefCount == 0) {
2821 Vector<const effect_uuid_t *> types;
2822 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2823 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2824 continue;
2825 }
2826 types.add(&mSuspendedEffects.valueAt(i)->mType);
2827 }
2828 for (size_t i = 0; i < types.size(); i++) {
2829 setEffectSuspended_l(types[i], false);
2830 }
2831 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2832 mSuspendedEffects.keyAt(index));
2833 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2834 }
2835 }
2836}
2837
2838
2839// The volume effect is used for automated tests only
2840#ifndef OPENSL_ES_H_
2841static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2842 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2843const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2844#endif //OPENSL_ES_H_
2845
Eric Laurentd8365c52017-07-16 15:27:05 -07002846/* static */
2847bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2848{
2849 // Only NS and AEC are suspended when BtNRec is off
2850 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2851 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2852 return true;
2853 }
2854 return false;
2855}
2856
Eric Laurentca7cc822012-11-19 14:55:58 -08002857bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2858{
2859 // auxiliary effects and visualizer are never suspended on output mix
2860 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2861 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2862 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002863 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2864 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002865 return false;
2866 }
2867 return true;
2868}
2869
2870void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2871 Vector< sp<AudioFlinger::EffectModule> > &effects)
2872{
2873 effects.clear();
2874 for (size_t i = 0; i < mEffects.size(); i++) {
2875 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2876 effects.add(mEffects[i]);
2877 }
2878 }
2879}
2880
2881sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2882 const effect_uuid_t *type)
2883{
2884 sp<EffectModule> effect = getEffectFromType_l(type);
2885 return effect != 0 && effect->isEnabled() ? effect : 0;
2886}
2887
2888void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2889 bool enabled)
2890{
2891 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2892 if (enabled) {
2893 if (index < 0) {
2894 // if the effect is not suspend check if all effects are suspended
2895 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2896 if (index < 0) {
2897 return;
2898 }
2899 if (!isEffectEligibleForSuspend(effect->desc())) {
2900 return;
2901 }
2902 setEffectSuspended_l(&effect->desc().type, enabled);
2903 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2904 if (index < 0) {
2905 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2906 return;
2907 }
2908 }
2909 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2910 effect->desc().type.timeLow);
2911 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002912 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002913 if (desc->mEffect == 0) {
2914 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002915 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002916 effect->setSuspended(true);
2917 }
2918 } else {
2919 if (index < 0) {
2920 return;
2921 }
2922 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2923 effect->desc().type.timeLow);
2924 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2925 desc->mEffect.clear();
2926 effect->setSuspended(false);
2927 }
2928}
2929
Eric Laurent5baf2af2013-09-12 17:37:00 -07002930bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002931{
2932 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002933 return isNonOffloadableEnabled_l();
2934}
2935
2936bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2937{
Eric Laurent813e2a72013-08-31 12:59:48 -07002938 size_t size = mEffects.size();
2939 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002940 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002941 return true;
2942 }
2943 }
2944 return false;
2945}
2946
Eric Laurentaaa44472014-09-12 17:41:50 -07002947void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2948{
2949 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002950 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002951}
2952
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002953void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2954{
2955 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2956 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2957 }
2958 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2959 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2960 }
2961}
2962
2963void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2964{
2965 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2966 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2967 }
2968 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2969 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2970 }
2971}
2972
2973bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002974{
2975 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002976 for (const auto &effect : mEffects) {
2977 if (effect->isProcessImplemented()) {
2978 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002979 }
2980 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002981 // Allow effects without processing.
2982 return true;
2983}
2984
2985bool AudioFlinger::EffectChain::isFastCompatible() const
2986{
2987 Mutex::Autolock _l(mLock);
2988 for (const auto &effect : mEffects) {
2989 if (effect->isProcessImplemented()
2990 && effect->isImplementationSoftware()) {
2991 return false;
2992 }
2993 }
2994 // Allow effects without processing or hw accelerated effects.
2995 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002996}
2997
2998// isCompatibleWithThread_l() must be called with thread->mLock held
2999bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
3000{
3001 Mutex::Autolock _l(mLock);
3002 for (size_t i = 0; i < mEffects.size(); i++) {
3003 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3004 return false;
3005 }
3006 }
3007 return true;
3008}
3009
Eric Laurent6b446ce2019-12-13 10:56:31 -08003010// EffectCallbackInterface implementation
3011status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3012 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3013 sp<EffectHalInterface> *effect) {
3014 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003015 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003016 if (effectsFactory != 0) {
3017 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3018 }
3019 return status;
3020}
3021
3022bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003023 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003024 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003025 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003026}
3027
3028status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3029 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003030 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003031}
3032
3033status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003034 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003035 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003036 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003037 if (t == nullptr) {
3038 return result;
3039 }
3040 sp <StreamHalInterface> st = t->stream();
3041 if (st == nullptr) {
3042 return result;
3043 }
3044 result = st->addEffect(effect);
3045 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3046 return result;
3047}
3048
3049status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003050 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003051 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003052 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003053 if (t == nullptr) {
3054 return result;
3055 }
3056 sp <StreamHalInterface> st = t->stream();
3057 if (st == nullptr) {
3058 return result;
3059 }
3060 result = st->removeEffect(effect);
3061 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3062 return result;
3063}
3064
3065audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003066 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003067 if (t == nullptr) {
3068 return AUDIO_IO_HANDLE_NONE;
3069 }
3070 return t->id();
3071}
3072
3073bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003074 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003075 if (t == nullptr) {
3076 return true;
3077 }
3078 return t->isOutput();
3079}
3080
3081bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003082 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003083}
3084
3085bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003086 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003087}
3088
3089bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003090 switch (mThreadType) {
3091 case ThreadBase::OFFLOAD:
3092 case ThreadBase::MMAP_PLAYBACK:
3093 case ThreadBase::MMAP_CAPTURE:
3094 return true;
3095 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003096 return false;
3097 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003098}
3099
3100bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3101 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003102}
3103
3104uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003105 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003106 if (t == nullptr) {
3107 return 0;
3108 }
3109 return t->sampleRate();
3110}
3111
Eric Laurentf1f22e72021-07-13 14:04:14 +02003112audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3113 sp<ThreadBase> t = thread().promote();
3114 if (t == nullptr) {
3115 return AUDIO_CHANNEL_NONE;
3116 }
3117 sp<EffectChain> c = chain().promote();
3118 if (c == nullptr) {
3119 return AUDIO_CHANNEL_NONE;
3120 }
3121
Eric Laurentb62d0362021-10-26 17:40:18 +02003122 if (mThreadType == ThreadBase::SPATIALIZER) {
3123 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3124 if (c->isFirstEffect(id)) {
3125 return t->mixerChannelMask();
3126 } else {
3127 return t->channelMask();
3128 }
3129 } else if (!audio_is_global_session(c->sessionId())) {
3130 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3131 return t->mixerChannelMask();
3132 } else {
3133 return t->channelMask();
3134 }
3135 } else {
3136 return t->channelMask();
3137 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003138 } else {
3139 return t->channelMask();
3140 }
3141}
3142
3143uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003144 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003145}
3146
3147audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003148 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003149 if (t == nullptr) {
3150 return AUDIO_CHANNEL_NONE;
3151 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003152 sp<EffectChain> c = chain().promote();
3153 if (c == nullptr) {
3154 return AUDIO_CHANNEL_NONE;
3155 }
3156
3157 if (mThreadType == ThreadBase::SPATIALIZER) {
3158 if (!audio_is_global_session(c->sessionId())) {
3159 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3160 return t->mixerChannelMask();
3161 } else {
3162 return t->channelMask();
3163 }
3164 } else {
3165 return t->channelMask();
3166 }
3167 } else {
3168 return t->channelMask();
3169 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003170}
3171
Eric Laurentf1f22e72021-07-13 14:04:14 +02003172uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003173 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003174}
3175
jiabineb3bda02020-06-30 14:07:03 -07003176audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003177 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003178 if (t == nullptr) {
3179 return AUDIO_CHANNEL_NONE;
3180 }
3181 return t->hapticChannelMask();
3182}
3183
Eric Laurent6b446ce2019-12-13 10:56:31 -08003184size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003185 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003186 if (t == nullptr) {
3187 return 0;
3188 }
3189 return t->frameCount();
3190}
3191
Andy Hung71ba4b32022-10-06 12:09:49 -07003192uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3193NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3194{
Andy Hung328d6772021-01-12 12:32:21 -08003195 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003196 if (t == nullptr) {
3197 return 0;
3198 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003199 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003200 return t->latency_l();
3201}
3202
Andy Hung71ba4b32022-10-06 12:09:49 -07003203void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3204NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3205{
Andy Hung328d6772021-01-12 12:32:21 -08003206 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003207 if (t == nullptr) {
3208 return;
3209 }
3210 t->setVolumeForOutput_l(left, right);
3211}
3212
3213void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003214 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
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;
3218 }
3219 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3220
Andy Hung328d6772021-01-12 12:32:21 -08003221 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003222 if (c == nullptr) {
3223 return;
3224 }
Eric Laurent41709552019-12-16 19:34:05 -08003225 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3226 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003227}
3228
Eric Laurent41709552019-12-16 19:34:05 -08003229void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003230 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003231 if (t == nullptr) {
3232 return;
3233 }
Eric Laurent41709552019-12-16 19:34:05 -08003234 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3235 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003236}
3237
Eric Laurent41709552019-12-16 19:34:05 -08003238void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003239 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3240
Andy Hung328d6772021-01-12 12:32:21 -08003241 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003242 if (t == nullptr) {
3243 return;
3244 }
3245 t->onEffectDisable();
3246}
3247
3248bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3249 bool unpinIfLast) {
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 false;
3253 }
3254 t->disconnectEffectHandle(handle, unpinIfLast);
3255 return true;
3256}
3257
3258void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003259 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003260 if (c == nullptr) {
3261 return;
3262 }
3263 c->resetVolume_l();
3264
3265}
3266
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003267product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003268 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003269 if (c == nullptr) {
3270 return PRODUCT_STRATEGY_NONE;
3271 }
3272 return c->strategy();
3273}
3274
3275int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003276 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003277 if (c == nullptr) {
3278 return 0;
3279 }
3280 return c->activeTrackCnt();
3281}
3282
Eric Laurentb82e6b72019-11-22 17:25:04 -08003283
3284#undef LOG_TAG
3285#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3286
3287status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3288{
3289 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3290 Mutex::Autolock _l(mProxyLock);
3291 if (status == NO_ERROR) {
3292 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003293 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003294 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003295 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003296 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003297 bs = handle.second->disable(&status);
3298 }
3299 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003300 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003301 }
3302 }
3303 }
3304 ALOGV("%s enable %d status %d", __func__, enabled, status);
3305 return status;
3306}
3307
3308status_t AudioFlinger::DeviceEffectProxy::init(
3309 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3310//For all audio patches
3311//If src or sink device match
3312//If the effect is HW accelerated
3313// if no corresponding effect module
3314// Create EffectModule: mHalEffect
3315//Create and attach EffectHandle
3316//If the effect is not HW accelerated and the patch sink or src is a mixer port
3317// Create Effect on patch input or output thread on session -1
3318//Add EffectHandle to EffectHandle map of Effect Proxy:
3319 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3320 status_t status = NO_ERROR;
3321 for (auto &patch : patches) {
3322 status = onCreatePatch(patch.first, patch.second);
3323 ALOGV("%s onCreatePatch status %d", __func__, status);
3324 if (status == BAD_VALUE) {
3325 return status;
3326 }
3327 }
3328 return status;
3329}
3330
3331status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3332 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3333 status_t status = NAME_NOT_FOUND;
3334 sp<EffectHandle> handle;
3335 // only consider source[0] as this is the only "true" source of a patch
3336 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3337 ALOGV("%s source checkPort status %d", __func__, status);
3338 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3339 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3340 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3341 }
3342 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3343 Mutex::Autolock _l(mProxyLock);
3344 mEffectHandles.emplace(patchHandle, handle);
3345 }
3346 ALOGW_IF(status == BAD_VALUE,
3347 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3348
3349 return status;
3350}
3351
3352status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3353 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3354
3355 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3356 __func__, port->type, port->ext.device.type,
3357 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003358 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3359 port->ext.device.address != mDevice.address()) {
3360 return NAME_NOT_FOUND;
3361 }
3362 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3363 (audio_port_config_has_input_direction(port))) {
3364 ALOGI("%s don't create postprocessing effect on record port", __func__);
3365 return NAME_NOT_FOUND;
3366 }
3367 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3368 (!audio_port_config_has_input_direction(port))) {
3369 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003370 return NAME_NOT_FOUND;
3371 }
3372 status_t status = NAME_NOT_FOUND;
3373
3374 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3375 Mutex::Autolock _l(mProxyLock);
3376 mDevicePort = *port;
3377 mHalEffect = new EffectModule(mMyCallback,
3378 const_cast<effect_descriptor_t *>(&mDescriptor),
3379 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3380 false /* pinned */, port->id);
3381 if (audio_is_input_device(mDevice.mType)) {
3382 mHalEffect->setInputDevice(mDevice);
3383 } else {
3384 mHalEffect->setDevices({mDevice});
3385 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003386 mHalEffect->configure();
3387
Eric Laurentde8caf42021-08-11 17:19:25 +02003388 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3389 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003390 status = (*handle)->initCheck();
3391 if (status == OK) {
3392 status = mHalEffect->addHandle((*handle).get());
3393 } else {
3394 mHalEffect.clear();
3395 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3396 }
3397 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3398 sp <ThreadBase> thread;
3399 if (audio_port_config_has_input_direction(port)) {
3400 if (patch.isSoftware()) {
3401 thread = patch.mRecord.thread();
3402 } else {
3403 thread = patch.thread().promote();
3404 }
3405 } else {
3406 if (patch.isSoftware()) {
3407 thread = patch.mPlayback.thread();
3408 } else {
3409 thread = patch.thread().promote();
3410 }
3411 }
3412 int enabled;
3413 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3414 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003415 &enabled, &status, false, false /*probe*/,
3416 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003417 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3418 } else {
3419 status = BAD_VALUE;
3420 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003421
Eric Laurentb82e6b72019-11-22 17:25:04 -08003422 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003423 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003424 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003425 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003426 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003427 bs = (*handle)->disable(&status);
3428 }
3429 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003430 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003431 }
3432 }
3433 return status;
3434}
3435
3436void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003437 sp<EffectHandle> effect;
3438 {
3439 Mutex::Autolock _l(mProxyLock);
3440 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3441 effect = mEffectHandles.at(patchHandle);
3442 mEffectHandles.erase(patchHandle);
3443 }
3444 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003445}
3446
3447
3448size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3449{
3450 Mutex::Autolock _l(mProxyLock);
3451 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003452 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003453 mHalEffect.clear();
3454 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3455 }
3456 return mHalEffect == nullptr ? 0 : 1;
3457}
3458
3459status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003460 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003461 if (mHalEffect == nullptr) {
3462 return NO_INIT;
3463 }
3464 return mManagerCallback->addEffectToHal(
3465 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3466}
3467
3468status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-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->removeEffectFromHal(
3474 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3475}
3476
3477bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3478 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3479 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3480 }
3481 return true;
3482}
3483
3484uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3485 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3486 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3487 return mDevicePort.sample_rate;
3488 }
3489 return DEFAULT_OUTPUT_SAMPLE_RATE;
3490}
3491
3492audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3493 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3494 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3495 return mDevicePort.channel_mask;
3496 }
3497 return AUDIO_CHANNEL_OUT_STEREO;
3498}
3499
3500uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3501 if (isOutput()) {
3502 return audio_channel_count_from_out_mask(channelMask());
3503 }
3504 return audio_channel_count_from_in_mask(channelMask());
3505}
3506
Andy Hung71ba4b32022-10-06 12:09:49 -07003507void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3508NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3509{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003510 const Vector<String16> args;
3511 EffectBase::dump(fd, args);
3512
3513 const bool locked = dumpTryLock(mProxyLock);
3514 if (!locked) {
3515 String8 result("DeviceEffectProxy may be deadlocked\n");
3516 write(fd, result.string(), result.size());
3517 }
3518
3519 String8 outStr;
3520 if (mHalEffect != nullptr) {
3521 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3522 } else {
3523 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3524 }
3525 write(fd, outStr.string(), outStr.size());
3526 outStr.clear();
3527
3528 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3529 write(fd, outStr.string(), outStr.size());
3530 outStr.clear();
3531
3532 for (const auto& iter : mEffectHandles) {
3533 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3534 write(fd, outStr.string(), outStr.size());
3535 outStr.clear();
3536 sp<EffectBase> effect = iter.second->effect().promote();
3537 if (effect != nullptr) {
3538 effect->dump(fd, args);
3539 }
3540 }
3541
3542 if (locked) {
3543 mLock.unlock();
3544 }
3545}
3546
3547#undef LOG_TAG
3548#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3549
3550int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3551 return mManagerCallback->newEffectId();
3552}
3553
3554
3555bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3556 EffectHandle *handle, bool unpinIfLast) {
3557 sp<EffectBase> effectBase = handle->effect().promote();
3558 if (effectBase == nullptr) {
3559 return false;
3560 }
3561
3562 sp<EffectModule> effect = effectBase->asEffectModule();
3563 if (effect == nullptr) {
3564 return false;
3565 }
3566
3567 // restore suspended effects if the disconnected handle was enabled and the last one.
3568 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3569 if (remove) {
3570 sp<DeviceEffectProxy> proxy = mProxy.promote();
3571 if (proxy != nullptr) {
3572 proxy->removeEffect(effect);
3573 }
3574 if (handle->enabled()) {
3575 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3576 }
3577 }
3578 return true;
3579}
3580
3581status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3582 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3583 sp<EffectHalInterface> *effect) {
3584 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3585}
3586
3587status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003588 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003589 sp<DeviceEffectProxy> proxy = mProxy.promote();
3590 if (proxy == nullptr) {
3591 return NO_INIT;
3592 }
3593 return proxy->addEffectToHal(effect);
3594}
3595
3596status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-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 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003602 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003603}
3604
3605bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3606 sp<DeviceEffectProxy> proxy = mProxy.promote();
3607 if (proxy == nullptr) {
3608 return true;
3609 }
3610 return proxy->isOutput();
3611}
3612
3613uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3614 sp<DeviceEffectProxy> proxy = mProxy.promote();
3615 if (proxy == nullptr) {
3616 return DEFAULT_OUTPUT_SAMPLE_RATE;
3617 }
3618 return proxy->sampleRate();
3619}
3620
Eric Laurentf1f22e72021-07-13 14:04:14 +02003621audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3622 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003623 sp<DeviceEffectProxy> proxy = mProxy.promote();
3624 if (proxy == nullptr) {
3625 return AUDIO_CHANNEL_OUT_STEREO;
3626 }
3627 return proxy->channelMask();
3628}
3629
Eric Laurentf1f22e72021-07-13 14:04:14 +02003630uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3631 sp<DeviceEffectProxy> proxy = mProxy.promote();
3632 if (proxy == nullptr) {
3633 return 2;
3634 }
3635 return proxy->channelCount();
3636}
3637
3638audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3639 sp<DeviceEffectProxy> proxy = mProxy.promote();
3640 if (proxy == nullptr) {
3641 return AUDIO_CHANNEL_OUT_STEREO;
3642 }
3643 return proxy->channelMask();
3644}
3645
3646uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003647 sp<DeviceEffectProxy> proxy = mProxy.promote();
3648 if (proxy == nullptr) {
3649 return 2;
3650 }
3651 return proxy->channelCount();
3652}
3653
Eric Laurent76c89f32021-12-03 17:13:23 +01003654void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3655 const sp<EffectBase>& effectBase) {
3656 sp<EffectModule> effect = effectBase->asEffectModule();
3657 if (effect == nullptr) {
3658 return;
3659 }
3660 effect->start();
3661}
3662
3663void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3664 const sp<EffectBase>& effectBase) {
3665 sp<EffectModule> effect = effectBase->asEffectModule();
3666 if (effect == nullptr) {
3667 return;
3668 }
3669 effect->stop();
3670}
3671
Glenn Kasten63238ef2015-03-02 15:50:29 -08003672} // namespace android