blob: 1c46ddd29ae025be36c42bda43f74eb00e7da2ff [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 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800576{
577 ALOGV("Constructor %p pinned %d", this, pinned);
578 int lStatus;
579
580 // create effect engine from effect factory
581 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800582 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800583 if (mStatus != NO_ERROR) {
584 return;
585 }
586 lStatus = init();
587 if (lStatus < 0) {
588 mStatus = lStatus;
589 goto Error;
590 }
591
592 setOffloaded(callback->isOffload(), callback->io());
593 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
594
595 return;
596Error:
597 mEffectInterface.clear();
598 ALOGV("Constructor Error %d", mStatus);
599}
600
601AudioFlinger::EffectModule::~EffectModule()
602{
603 ALOGV("Destructor %p", this);
604 if (mEffectInterface != 0) {
605 char uuidStr[64];
606 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
607 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
608 this, uuidStr);
609 release_l();
610 }
611
612}
613
Eric Laurentfa1e1232016-08-02 19:01:49 -0700614bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800615 Mutex::Autolock _l(mLock);
616
Eric Laurentfa1e1232016-08-02 19:01:49 -0700617 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 switch (mState) {
619 case RESTART:
620 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700621 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800622
623 case STARTING:
624 // clear auxiliary effect input buffer for next accumulation
625 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
626 memset(mConfig.inputCfg.buffer.raw,
627 0,
628 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
629 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700630 if (start_l() == NO_ERROR) {
631 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700632 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700633 } else {
634 mState = IDLE;
635 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 break;
637 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900638 // volume control for offload and direct threads must take effect immediately.
639 if (stop_l() == NO_ERROR
640 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700641 mDisableWaitCnt = mMaxDisableWaitCnt;
642 } else {
643 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
644 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800645 mState = STOPPED;
646 break;
647 case STOPPED:
648 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
649 // turn off sequence.
650 if (--mDisableWaitCnt == 0) {
651 reset_l();
652 mState = IDLE;
653 }
654 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200655 case ACTIVE:
656 for (size_t i = 0; i < mHandles.size(); i++) {
657 if (!mHandles[i]->disconnected()) {
658 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
659 }
660 }
661 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 default: //IDLE , ACTIVE, DESTROYED
663 break;
664 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700665
666 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800667}
668
669void AudioFlinger::EffectModule::process()
670{
671 Mutex::Autolock _l(mLock);
672
Mikhail Naganov022b9952017-01-04 16:36:51 -0800673 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 return;
675 }
676
rago94a1ee82017-07-21 15:11:02 -0700677 const uint32_t inChannelCount =
678 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
679 const uint32_t outChannelCount =
680 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
681 const bool auxType =
682 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
683
Andy Hungfa69ca32017-11-30 10:07:53 -0800684 // safeInputOutputSampleCount is 0 if the channel count between input and output
685 // buffers do not match. This prevents automatic accumulation or copying between the
686 // input and output effect buffers without an intermediary effect process.
687 // TODO: consider implementing channel conversion.
688 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700689 mInChannelCountRequested != mOutChannelCountRequested ? 0
690 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800691 mConfig.inputCfg.buffer.frameCount,
692 mConfig.outputCfg.buffer.frameCount);
693 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 accumulate_float(
695 mConfig.outputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.f32,
697 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800698 };
699 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800700 memcpy(
701 mConfig.outputCfg.buffer.f32,
702 mConfig.inputCfg.buffer.f32,
703 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800704 };
705
Eric Laurentca7cc822012-11-19 14:55:58 -0800706 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700707 int ret;
708 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700709 if (auxType) {
710 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800711 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700712
Andy Hung26836922023-05-22 17:31:57 -0700713 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800714 memcpy_to_i16_from_float(
715 mConfig.inputCfg.buffer.s16,
716 mConfig.inputCfg.buffer.f32,
717 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700718 }
rago94a1ee82017-07-21 15:11:02 -0700719 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800720 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
721 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
722
723 if (!auxType && mInChannelCountRequested != inChannelCount) {
724 adjust_channels(
725 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
726 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
727 sizeof(float),
728 sizeof(float)
729 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
730 inBuffer = mInConversionBuffer;
731 }
732 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
733 && mOutChannelCountRequested != outChannelCount) {
734 adjust_selected_channels(
735 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
736 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
737 sizeof(float),
738 sizeof(float)
739 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
740 outBuffer = mOutConversionBuffer;
741 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
743 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800744 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800745 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
746 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700747 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 memcpy_to_i16_from_float(
749 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800750 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800752 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700753 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800754 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800755 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800756 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
757 goto data_bypass;
758 }
759 memcpy_to_i16_from_float(
760 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800761 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800762 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800763 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700764 }
765 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800766 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800768 sp<EffectBufferHalInterface> target =
769 mOutChannelCountRequested != outChannelCount
770 ? mOutConversionBuffer : mOutBuffer;
771
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800773 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 mOutConversionBuffer->audioBuffer()->s16,
775 outChannelCount * mConfig.outputCfg.buffer.frameCount);
776 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800777 if (mOutChannelCountRequested != outChannelCount) {
778 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
779 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
780 sizeof(float),
781 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
782 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700783 } else {
rago94a1ee82017-07-21 15:11:02 -0700784 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700785 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700787 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700789 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 }
792 }
793 ret = -ENODATA;
794 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800795
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 // force transition to IDLE state when engine is ready
797 if (mState == STOPPED && ret == -ENODATA) {
798 mDisableWaitCnt = 1;
799 }
800
801 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700802 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800803 const size_t size =
804 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700805 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800806 }
807 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700808 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800809 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
810 // If an insert effect is idle and input buffer is different from output buffer,
811 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700812 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700813 // similar handling with data_bypass above.
814 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
815 accumulateInputToOutput();
816 } else { // EFFECT_BUFFER_ACCESS_WRITE
817 copyInputToOutput();
818 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800819 }
820 }
821}
822
823void AudioFlinger::EffectModule::reset_l()
824{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700825 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 return;
827 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700828 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800829}
830
831status_t AudioFlinger::EffectModule::configure()
832{
rago94a1ee82017-07-21 15:11:02 -0700833 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700834 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700835 uint32_t size;
836 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700837 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700839 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 status = NO_INIT;
841 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800842 }
843
Eric Laurentca7cc822012-11-19 14:55:58 -0800844 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800845 // TODO: handle configuration of input (record) SW effects above the HAL,
846 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
847 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700848 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200849 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200851 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800852
853 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
855 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
856 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
857 mConfig.inputCfg.channels);
858 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800859 }
jiabineb3bda02020-06-30 14:07:03 -0700860 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700861 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700862 mConfig.inputCfg.channels |= hapticChannelMask;
863 mConfig.outputCfg.channels |= hapticChannelMask;
864 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800865 mInChannelCountRequested =
866 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
867 mOutChannelCountRequested =
868 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700869
Andy Hung319587b2023-05-23 14:01:03 -0700870 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
871 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900872
873 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700874 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900875 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
876 ALOGV("Overriding effect input as 48kHz");
877 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700878 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900879 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
881 mConfig.inputCfg.bufferProvider.cookie = NULL;
882 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
883 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
884 mConfig.outputCfg.bufferProvider.cookie = NULL;
885 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
886 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
887 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
888 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800889 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 // always overwrites output buffer: input buffer == output buffer
891 // - in other sessions:
892 // last effect in the chain accumulates in output buffer: input buffer != output buffer
893 // other effect: overwrites output buffer: input buffer == output buffer
894 // Auxiliary effect:
895 // accumulates in output buffer: input buffer != output buffer
896 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700897 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800898 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
899 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700900 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800901 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov59984db2022-04-19 21:21:23 +0000902 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800903
Eric Laurent6b446ce2019-12-13 10:56:31 -0800904 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700905 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800906 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800907
908 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700909 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700910 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800911 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700912 &mConfig,
913 &size,
914 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700915 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800916 status = cmdStatus;
917 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800918
Andy Hung9aad48c2017-11-29 10:29:19 -0800919 if (status != NO_ERROR &&
Mikhail Naganov59984db2022-04-19 21:21:23 +0000920 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800921 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
922 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
923 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700924 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
925 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800926 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
927 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
928 }
929 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
930 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
931 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
932 }
933 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700934 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800935 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700936 &mConfig,
937 &size,
938 &cmdStatus);
939 if (status == NO_ERROR) {
940 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800941 }
942 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800943
Andy Hung9aad48c2017-11-29 10:29:19 -0800944 if (status == NO_ERROR) {
945 mSupportsFloat = true;
946 }
947
948 if (status != NO_ERROR) {
949 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
950 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
951 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
952 size = sizeof(int);
953 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
954 sizeof(mConfig),
955 &mConfig,
956 &size,
957 &cmdStatus);
958 if (status == NO_ERROR) {
959 status = cmdStatus;
960 }
961 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700962 mSupportsFloat = false;
963 ALOGVV("config worked with 16 bit");
964 } else {
965 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800966 }
rago94a1ee82017-07-21 15:11:02 -0700967 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800968
rago94a1ee82017-07-21 15:11:02 -0700969 if (status == NO_ERROR) {
970 // Establish Buffer strategy
971 setInBuffer(mInBuffer);
972 setOutBuffer(mOutBuffer);
973
974 // Update visualizer latency
975 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
976 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
977 effect_param_t *p = (effect_param_t *)buf32;
978
979 p->psize = sizeof(uint32_t);
980 p->vsize = sizeof(uint32_t);
981 size = sizeof(int);
982 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
983
Andy Hungfda44002021-06-03 17:23:16 -0700984 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700985
986 *((int32_t *)p->data + 1)= latency;
987 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
988 sizeof(effect_param_t) + 8,
989 &buf32,
990 &size,
991 &cmdStatus);
992 }
jiabin229f94d2022-08-23 16:37:30 -0700993
994 if (isVolumeControl()) {
995 // Force initializing the volume as 0 for volume control effect for safer ramping
996 uint32_t left = 0;
997 uint32_t right = 0;
998 setVolumeInternal(&left, &right, true /*controller*/);
999 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001000 }
1001
Andy Hung05083ac2017-12-14 15:00:28 -08001002 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1003 mMaxDisableWaitCnt = (uint32_t)std::max(
1004 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1005 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1006 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001007
Eric Laurentd0ebb532013-04-02 16:41:41 -07001008exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001009 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001010 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001011 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001012 return status;
1013}
1014
1015status_t AudioFlinger::EffectModule::init()
1016{
1017 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001018 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001019 return NO_INIT;
1020 }
1021 status_t cmdStatus;
1022 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001023 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1024 0,
1025 NULL,
1026 &size,
1027 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 if (status == 0) {
1029 status = cmdStatus;
1030 }
1031 return status;
1032}
1033
Eric Laurent1b928682014-10-02 19:41:47 -07001034void AudioFlinger::EffectModule::addEffectToHal_l()
1035{
1036 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1037 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001038 if (mAddedToHal) {
1039 return;
1040 }
1041
Andy Hungfda44002021-06-03 17:23:16 -07001042 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001043 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001044 }
1045}
1046
Eric Laurentfa1e1232016-08-02 19:01:49 -07001047// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001048status_t AudioFlinger::EffectModule::start()
1049{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001050 status_t status;
1051 {
1052 Mutex::Autolock _l(mLock);
1053 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001054 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001055 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001056 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001057 }
1058 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001059}
1060
1061status_t AudioFlinger::EffectModule::start_l()
1062{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001063 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return NO_INIT;
1065 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001066 if (mStatus != NO_ERROR) {
1067 return mStatus;
1068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 status_t cmdStatus;
1070 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001071 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1072 0,
1073 NULL,
1074 &size,
1075 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 if (status == 0) {
1077 status = cmdStatus;
1078 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001079 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001080 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 }
1082 return status;
1083}
1084
1085status_t AudioFlinger::EffectModule::stop()
1086{
1087 Mutex::Autolock _l(mLock);
1088 return stop_l();
1089}
1090
1091status_t AudioFlinger::EffectModule::stop_l()
1092{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001093 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001094 return NO_INIT;
1095 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001096 if (mStatus != NO_ERROR) {
1097 return mStatus;
1098 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001099 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001101
1102 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001103 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1104 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1105 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001106 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001107 mSetVolumeReentrantTid = INVALID_PID;
1108 }
1109
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001110 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1111 0,
1112 NULL,
1113 &size,
1114 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001115 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 status = cmdStatus;
1117 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001118 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001119 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001120 }
1121 return status;
1122}
1123
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001124// must be called with EffectChain::mLock held
1125void AudioFlinger::EffectModule::release_l()
1126{
1127 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001128 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001129 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001130 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001131 mEffectInterface.clear();
1132 }
1133}
1134
Eric Laurent6b446ce2019-12-13 10:56:31 -08001135status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001136{
1137 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1138 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001139 if (!mAddedToHal) {
1140 return NO_ERROR;
1141 }
1142
Andy Hungfda44002021-06-03 17:23:16 -07001143 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001144 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001146 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001147}
1148
Andy Hunge4a1d912016-08-17 14:11:13 -07001149// round up delta valid if value and divisor are positive.
1150template <typename T>
1151static T roundUpDelta(const T &value, const T &divisor) {
1152 T remainder = value % divisor;
1153 return remainder == 0 ? 0 : divisor - remainder;
1154}
1155
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001156status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1157 const std::vector<uint8_t>& cmdData,
1158 int32_t maxReplySize,
1159 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001160{
1161 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001162 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001163
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001164 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001165 return NO_INIT;
1166 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001167 if (mStatus != NO_ERROR) {
1168 return mStatus;
1169 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001170 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1171 return -EINVAL;
1172 }
1173 size_t cmdSize = cmdData.size();
1174 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1175 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1176 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001177 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001178 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001179 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001180 android_errorWriteLog(0x534e4554, "33003822");
1181 return -EINVAL;
1182 }
1183 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001184 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001185 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001186 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001187 return -EINVAL;
1188 }
ragoe2759072016-11-22 18:02:48 -08001189 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001190 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001191 || param->psize > maxReplySize - sizeof(effect_param_t)
1192 || param->vsize > maxReplySize - sizeof(effect_param_t)
1193 - param->psize
1194 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1195 maxReplySize
1196 - sizeof(effect_param_t)
1197 - param->psize
1198 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001199 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1200 android_errorWriteLog(0x534e4554, "32705438");
1201 return -EINVAL;
1202 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001203 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001204 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1205 && // DEFERRED not generally used
1206 (param == nullptr
1207 || param->psize > cmdSize - sizeof(effect_param_t)
1208 || param->vsize > cmdSize - sizeof(effect_param_t)
1209 - param->psize
1210 || roundUpDelta(param->psize,
1211 (uint32_t) sizeof(int)) >
1212 cmdSize
1213 - sizeof(effect_param_t)
1214 - param->psize
1215 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001216 android_errorWriteLog(0x534e4554, "30204301");
1217 return -EINVAL;
1218 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001219 uint32_t replySize = maxReplySize;
1220 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001221 status_t status = mEffectInterface->command(cmdCode,
1222 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001223 const_cast<uint8_t*>(cmdData.data()),
1224 &replySize,
1225 reply->data());
1226 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001227 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 for (size_t i = 1; i < mHandles.size(); i++) {
1229 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001230 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001231 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001232 }
1233 }
1234 }
1235 return status;
1236}
1237
Eric Laurentca7cc822012-11-19 14:55:58 -08001238bool AudioFlinger::EffectModule::isProcessEnabled() const
1239{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001240 if (mStatus != NO_ERROR) {
1241 return false;
1242 }
1243
Eric Laurentca7cc822012-11-19 14:55:58 -08001244 switch (mState) {
1245 case RESTART:
1246 case ACTIVE:
1247 case STOPPING:
1248 case STOPPED:
1249 return true;
1250 case IDLE:
1251 case STARTING:
1252 case DESTROYED:
1253 default:
1254 return false;
1255 }
1256}
1257
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001258bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1259{
Andy Hungfda44002021-06-03 17:23:16 -07001260 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001261}
1262
1263bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1264{
1265 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1266}
1267
Mikhail Naganov022b9952017-01-04 16:36:51 -08001268void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001269 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001270
1271 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001272 if (buffer != 0) {
1273 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1274 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1275 } else {
1276 mConfig.inputCfg.buffer.raw = NULL;
1277 }
1278 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001279 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001280
Andy Hungbded9c82017-11-30 18:47:35 -08001281 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001282 // Theoretically insert effects can also do in-place conversions (destroying
1283 // the original buffer) when the output buffer is identical to the input buffer,
1284 // but we don't optimize for it here.
1285 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001286 const uint32_t inChannelCount =
1287 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1288 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001289 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001290 // we need to translate - create hidl shared buffer and intercept
1291 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001292 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1293 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1294 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001295
1296 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1297 __func__, inChannels, inFrameCount, size);
1298
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001299 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001300 || size > mInConversionBuffer->getSize())) {
1301 mInConversionBuffer.clear();
1302 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001303 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001304 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001305 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001306 mInConversionBuffer->setFrameCount(inFrameCount);
1307 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001308 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001309 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001310 }
1311 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001312}
1313
1314void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001315 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001316
1317 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001318 if (buffer != 0) {
1319 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1320 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1321 } else {
1322 mConfig.outputCfg.buffer.raw = NULL;
1323 }
1324 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001325 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001326
Andy Hungbded9c82017-11-30 18:47:35 -08001327 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001328 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001329 const uint32_t outChannelCount =
1330 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1331 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001332 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001333 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001334 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1335 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1336 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001337
1338 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1339 __func__, outChannels, outFrameCount, size);
1340
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001341 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001342 || size > mOutConversionBuffer->getSize())) {
1343 mOutConversionBuffer.clear();
1344 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001345 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001346 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001347 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001348 mOutConversionBuffer->setFrameCount(outFrameCount);
1349 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001350 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001351 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001352 }
1353 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001354}
1355
Eric Laurentca7cc822012-11-19 14:55:58 -08001356status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1357{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001358 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001359 if (mStatus != NO_ERROR) {
1360 return mStatus;
1361 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001362 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001363 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1364 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1365 if (isProcessEnabled() &&
1366 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001367 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1368 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001369 status = setVolumeInternal(left, right, controller);
1370 }
1371 return status;
1372}
1373
1374status_t AudioFlinger::EffectModule::setVolumeInternal(
1375 uint32_t *left, uint32_t *right, bool controller) {
1376 uint32_t volume[2] = {*left, *right};
1377 uint32_t *pVolume = controller ? volume : nullptr;
1378 uint32_t size = sizeof(volume);
1379 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1380 size,
1381 volume,
1382 &size,
1383 pVolume);
1384 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1385 *left = volume[0];
1386 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001387 }
1388 return status;
1389}
1390
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001391void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1392{
Zhou Songd505c642020-02-20 16:35:37 +08001393 // for offload or direct thread, if the effect chain has non-offloadable
1394 // effect and any effect module within the chain has volume control, then
1395 // volume control is delegated to effect, otherwise, set volume to hal.
1396 if (mEffectCallback->isOffloadOrDirect() &&
1397 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001398 float vol_l = (float)left / (1 << 24);
1399 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001400 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001401 }
1402}
1403
jiabin8f278ee2019-11-11 12:16:27 -08001404status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1405 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001406{
jiabin8f278ee2019-11-11 12:16:27 -08001407 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1408 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 return NO_ERROR;
1410 }
1411
1412 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001413 if (mStatus != NO_ERROR) {
1414 return mStatus;
1415 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001417 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 status_t cmdStatus;
1419 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001420 // FIXME: use audio device types and addresses when the hal interface is ready.
1421 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001422 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001423 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001424 &size,
1425 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 }
1427 return status;
1428}
1429
jiabin8f278ee2019-11-11 12:16:27 -08001430status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1431{
1432 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1433}
1434
1435status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1436{
1437 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1438}
1439
Eric Laurentca7cc822012-11-19 14:55:58 -08001440status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1441{
1442 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001443 if (mStatus != NO_ERROR) {
1444 return mStatus;
1445 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001446 status_t status = NO_ERROR;
1447 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1448 status_t cmdStatus;
1449 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001450 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1451 sizeof(audio_mode_t),
1452 &mode,
1453 &size,
1454 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001455 if (status == NO_ERROR) {
1456 status = cmdStatus;
1457 }
1458 }
1459 return status;
1460}
1461
1462status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1463{
1464 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001465 if (mStatus != NO_ERROR) {
1466 return mStatus;
1467 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001468 status_t status = NO_ERROR;
1469 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1470 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001471 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1472 sizeof(audio_source_t),
1473 &source,
1474 &size,
1475 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 }
1477 return status;
1478}
1479
Eric Laurent5baf2af2013-09-12 17:37:00 -07001480status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1481{
1482 Mutex::Autolock _l(mLock);
1483 if (mStatus != NO_ERROR) {
1484 return mStatus;
1485 }
1486 status_t status = NO_ERROR;
1487 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1488 status_t cmdStatus;
1489 uint32_t size = sizeof(status_t);
1490 effect_offload_param_t cmd;
1491
1492 cmd.isOffload = offloaded;
1493 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001494 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1495 sizeof(effect_offload_param_t),
1496 &cmd,
1497 &size,
1498 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001499 if (status == NO_ERROR) {
1500 status = cmdStatus;
1501 }
1502 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1503 } else {
1504 if (offloaded) {
1505 status = INVALID_OPERATION;
1506 }
1507 mOffloaded = false;
1508 }
1509 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1510 return status;
1511}
1512
1513bool AudioFlinger::EffectModule::isOffloaded() const
1514{
1515 Mutex::Autolock _l(mLock);
1516 return mOffloaded;
1517}
1518
jiabineb3bda02020-06-30 14:07:03 -07001519/*static*/
1520bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1521 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1522}
1523
1524bool AudioFlinger::EffectModule::isHapticGenerator() const {
1525 return isHapticGenerator(&mDescriptor.type);
1526}
1527
jiabine70bc7f2020-06-30 22:07:55 -07001528status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1529{
1530 if (mStatus != NO_ERROR) {
1531 return mStatus;
1532 }
1533 if (!isHapticGenerator()) {
1534 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1535 return INVALID_OPERATION;
1536 }
1537
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001538 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1539 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001540 param->psize = sizeof(int32_t);
1541 param->vsize = sizeof(int32_t) * 2;
1542 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1543 *((int32_t*)param->data + 1) = id;
1544 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001545 std::vector<uint8_t> response;
1546 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001547 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001548 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1549 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001550 }
1551 return status;
1552}
1553
Lais Andradebc3f37a2021-07-02 00:13:19 +01001554status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001555{
1556 if (mStatus != NO_ERROR) {
1557 return mStatus;
1558 }
1559 if (!isHapticGenerator()) {
1560 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1561 return INVALID_OPERATION;
1562 }
1563
Lais Andradebc3f37a2021-07-02 00:13:19 +01001564 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001565 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001566 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001567 effect_param_t *param = (effect_param_t*) request.data();
1568 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001569 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001570 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1571 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001572 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1573 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1574 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001575 std::vector<uint8_t> response;
1576 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1577 if (status == NO_ERROR) {
1578 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1579 status = *reinterpret_cast<const status_t*>(response.data());
1580 }
1581 return status;
1582}
1583
Mikhail Naganov59984db2022-04-19 21:21:23 +00001584status_t AudioFlinger::EffectModule::getConfigs(
1585 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1586 Mutex::Autolock _l(mLock);
1587 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1588 return NO_INIT;
1589 }
1590 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1591 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1592 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1593 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1594 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1595 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1596 *isOutput = mIsOutput;
1597 return NO_ERROR;
1598}
1599
Andy Hungbded9c82017-11-30 18:47:35 -08001600static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1601 std::stringstream ss;
1602
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001603 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001604 return "nullptr"; // make different than below
1605 } else if (buffer->externalData() != nullptr) {
1606 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1607 << " -> "
1608 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1609 } else {
1610 ss << buffer->audioBuffer()->raw;
1611 }
1612 return ss.str();
1613}
Marco Nelissenb2208842014-02-07 14:00:50 -08001614
Eric Laurent41709552019-12-16 19:34:05 -08001615void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07001616NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001617{
Eric Laurent41709552019-12-16 19:34:05 -08001618 EffectBase::dump(fd, args);
1619
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001621 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001622
Eric Laurent41709552019-12-16 19:34:05 -08001623 result.append("\t\tStatus Engine:\n");
1624 result.appendFormat("\t\t%03d %p\n",
1625 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001626
1627 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001628
1629 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001630 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1631 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1632 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001633 mConfig.inputCfg.buffer.frameCount,
1634 mConfig.inputCfg.samplingRate,
1635 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001636 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001637 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001638
1639 result.append("\t\t- Output configuration:\n");
1640 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001641 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001642 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001643 mConfig.outputCfg.buffer.frameCount,
1644 mConfig.outputCfg.samplingRate,
1645 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001646 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001647 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001648
Andy Hungbded9c82017-11-30 18:47:35 -08001649 result.appendFormat("\t\t- HAL buffers:\n"
1650 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1651 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1652 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1653 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1654 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001655
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 write(fd, result.string(), result.length());
1657
Mikhail Naganov4d547672019-02-22 14:19:19 -08001658 if (mEffectInterface != 0) {
1659 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1660 (void)mEffectInterface->dump(fd);
1661 }
1662
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 if (locked) {
1664 mLock.unlock();
1665 }
1666}
1667
1668// ----------------------------------------------------------------------------
1669// EffectHandle implementation
1670// ----------------------------------------------------------------------------
1671
1672#undef LOG_TAG
1673#define LOG_TAG "AudioFlinger::EffectHandle"
1674
Eric Laurent41709552019-12-16 19:34:05 -08001675AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001676 const sp<AudioFlinger::Client>& client,
1677 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001678 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001679 : BnEffect(),
1680 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001681 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1682 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001683{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001684 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001685 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001686
1687 if (client == 0) {
1688 return;
1689 }
1690 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1691 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001692 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001693 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001694 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001695 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001696 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 return;
1698 }
Glenn Kastene75da402013-11-20 13:54:52 -08001699 new(mCblk) effect_param_cblk_t();
1700 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001701}
1702
1703AudioFlinger::EffectHandle::~EffectHandle()
1704{
1705 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 disconnect(false);
1707}
1708
Andy Hungc747c532022-03-07 21:41:14 -08001709// Creates an association between Binder code to name for IEffect.
1710#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1711BINDER_METHOD_ENTRY(enable) \
1712BINDER_METHOD_ENTRY(disable) \
1713BINDER_METHOD_ENTRY(command) \
1714BINDER_METHOD_ENTRY(disconnect) \
1715BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001716BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001717
1718// singleton for Binder Method Statistics for IEffect
1719mediautils::MethodStatistics<int>& getIEffectStatistics() {
1720 using Code = int;
1721
1722#pragma push_macro("BINDER_METHOD_ENTRY")
1723#undef BINDER_METHOD_ENTRY
1724#define BINDER_METHOD_ENTRY(ENTRY) \
1725 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1726
1727 static mediautils::MethodStatistics<Code> methodStatistics{
1728 IEFFECT_BINDER_METHOD_MACRO_LIST
1729 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1730 };
1731#pragma pop_macro("BINDER_METHOD_ENTRY")
1732
1733 return methodStatistics;
1734}
1735
1736status_t AudioFlinger::EffectHandle::onTransact(
1737 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001738 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1739 mediautils::TimeCheck check(
1740 std::string("IEffect::").append(methodName),
1741 [code](bool timeout, float elapsedMs) {
1742 if (timeout) {
1743 ; // we don't timeout right now on the effect interface.
1744 } else {
1745 getIEffectStatistics().event(code, elapsedMs);
1746 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001747 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001748 return BnEffect::onTransact(code, data, reply, flags);
1749}
1750
Glenn Kastene75da402013-11-20 13:54:52 -08001751status_t AudioFlinger::EffectHandle::initCheck()
1752{
1753 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1754}
1755
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001756#define RETURN(code) \
1757 *_aidl_return = (code); \
1758 return Status::ok();
1759
Mikhail Naganov59984db2022-04-19 21:21:23 +00001760#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1761 ({ \
1762 auto _tmp = (exp); \
1763 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1764 std::move(_tmp.value()); \
1765 })
1766
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001767Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001768{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001769 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001771 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001772 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001773 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001776 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001777 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001778
1779 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001780 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001781 }
1782
1783 mEnabled = true;
1784
Eric Laurent6c796322019-04-09 14:13:17 -07001785 status_t status = effect->updatePolicyState();
1786 if (status != NO_ERROR) {
1787 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001788 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001789 }
1790
Eric Laurent6b446ce2019-12-13 10:56:31 -08001791 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001792
1793 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001794 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001795 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001796 }
1797
Eric Laurent6b446ce2019-12-13 10:56:31 -08001798 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001799 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001800 mEnabled = false;
1801 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001802 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001803}
1804
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001805Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001806{
1807 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001809 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001810 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001811 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001812 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001813 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001814 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001816
1817 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001818 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 }
1820 mEnabled = false;
1821
Eric Laurent6c796322019-04-09 14:13:17 -07001822 effect->updatePolicyState();
1823
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001824 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001825 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001826 }
1827
Eric Laurent6b446ce2019-12-13 10:56:31 -08001828 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001829 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001830}
1831
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001832Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001833{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001834 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001836 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001837}
1838
1839void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1840{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001841 AutoMutex _l(mLock);
1842 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1843 if (mDisconnected) {
1844 if (unpinIfLast) {
1845 android_errorWriteLog(0x534e4554, "32707507");
1846 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001847 return;
1848 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001849 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 {
Eric Laurent41709552019-12-16 19:34:05 -08001851 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001853 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001854 ALOGW("%s Effect handle %p disconnected after thread destruction",
1855 __func__, this);
1856 }
1857 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001858 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001859 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001860
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 if (mClient != 0) {
1862 if (mCblk != NULL) {
1863 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1864 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1865 }
1866 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001867 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung71ba4b32022-10-06 12:09:49 -07001868 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 mClient.clear();
1870 }
1871}
1872
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1874 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1875 return Status::ok();
1876}
1877
Mikhail Naganov59984db2022-04-19 21:21:23 +00001878Status AudioFlinger::EffectHandle::getConfig(
1879 media::EffectConfig* _config, int32_t* _aidl_return) {
1880 AutoMutex _l(mLock);
1881 sp<EffectBase> effect = mEffect.promote();
1882 if (effect == nullptr || mDisconnected) {
1883 RETURN(DEAD_OBJECT);
1884 }
1885 sp<EffectModule> effectModule = effect->asEffectModule();
1886 if (effectModule == nullptr) {
1887 RETURN(INVALID_OPERATION);
1888 }
1889 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1890 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1891 bool isOutput;
1892 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1893 if (status == NO_ERROR) {
1894 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1895 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1896 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1897 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1898 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1899 _config->isOnInputStream = !isOutput;
1900 }
1901 RETURN(status);
1902}
1903
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001904Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1905 const std::vector<uint8_t>& cmdData,
1906 int32_t maxResponseSize,
1907 std::vector<uint8_t>* response,
1908 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001909{
1910 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001911 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001912
Eric Laurentc7ab3092017-06-15 18:43:46 -07001913 // reject commands reserved for internal use by audio framework if coming from outside
1914 // of audioserver
1915 switch(cmdCode) {
1916 case EFFECT_CMD_ENABLE:
1917 case EFFECT_CMD_DISABLE:
1918 case EFFECT_CMD_SET_PARAM:
1919 case EFFECT_CMD_SET_PARAM_DEFERRED:
1920 case EFFECT_CMD_SET_PARAM_COMMIT:
1921 case EFFECT_CMD_GET_PARAM:
1922 break;
1923 default:
1924 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1925 break;
1926 }
1927 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001928 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001929 }
1930
Eric Laurent1ffc5852016-12-15 14:46:09 -08001931 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001932 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001933 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001934 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001935 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001936 writeToBuffer(NO_ERROR, response);
1937 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001938 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001939 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001940 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001941 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001942 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001943 writeToBuffer(NO_ERROR, response);
1944 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001945 }
1946
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001947 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001948 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001949 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001950 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001951 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001952 // only get parameter command is permitted for applications not controlling the effect
1953 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001954 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001955 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001956
1957 // handle commands that are not forwarded transparently to effect engine
1958 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001959 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001960 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001961 }
1962
Andy Hung71ba4b32022-10-06 12:09:49 -07001963 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001964 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001965 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001966 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001968
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 // No need to trylock() here as this function is executed in the binder thread serving a
1970 // particular client process: no risk to block the whole media server process or mixer
1971 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07001972 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001973 // keep local copy of index in case of client corruption b/32220769
1974 const uint32_t clientIndex = mCblk->clientIndex;
1975 const uint32_t serverIndex = mCblk->serverIndex;
1976 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1977 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001978 mCblk->serverIndex = 0;
1979 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001980 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001981 }
1982 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001983 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001984 for (uint32_t index = serverIndex; index < clientIndex;) {
1985 int *p = (int *)(mBuffer + index);
1986 const int size = *p++;
1987 if (size < 0
1988 || size > EFFECT_PARAM_BUFFER_SIZE
1989 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001991 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001992 break;
1993 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001994
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 std::copy(reinterpret_cast<const uint8_t*>(p),
1996 reinterpret_cast<const uint8_t*>(p) + size,
1997 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001998
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001999 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002000 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002001 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002002 sizeof(int),
2003 &replyBuffer);
2004 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002005
2006 // verify shared memory: server index shouldn't change; client index can't go back.
2007 if (serverIndex != mCblk->serverIndex
2008 || clientIndex > mCblk->clientIndex) {
2009 android_errorWriteLog(0x534e4554, "32220769");
2010 status = BAD_VALUE;
2011 break;
2012 }
2013
Eric Laurentca7cc822012-11-19 14:55:58 -08002014 // stop at first error encountered
2015 if (ret != NO_ERROR) {
2016 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002018 break;
2019 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002020 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002021 break;
2022 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002023 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002024 }
2025 mCblk->serverIndex = 0;
2026 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002027 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002028 }
2029
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002030 status_t status = effect->command(cmdCode,
2031 cmdData,
2032 maxResponseSize,
2033 response);
2034 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002035}
2036
2037void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2038{
2039 ALOGV("setControl %p control %d", this, hasControl);
2040
2041 mHasControl = hasControl;
2042 mEnabled = enabled;
2043
2044 if (signal && mEffectClient != 0) {
2045 mEffectClient->controlStatusChanged(hasControl);
2046 }
2047}
2048
2049void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002050 const std::vector<uint8_t>& cmdData,
2051 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002052{
2053 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002054 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002055 }
2056}
2057
2058
2059
2060void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2061{
2062 if (mEffectClient != 0) {
2063 mEffectClient->enableStatusChanged(enabled);
2064 }
2065}
2066
Eric Laurentde8caf42021-08-11 17:19:25 +02002067void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2068{
2069 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2070 mEffectClient->framesProcessed(frames);
2071 }
2072}
2073
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002074void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung71ba4b32022-10-06 12:09:49 -07002075NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002076{
2077 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2078
Marco Nelissenb2208842014-02-07 14:00:50 -08002079 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002080 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002081 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002082 mHasControl ? "yes" : "no",
2083 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002084 mCblk ? mCblk->clientIndex : 0,
2085 mCblk ? mCblk->serverIndex : 0
2086 );
2087
2088 if (locked) {
2089 mCblk->lock.unlock();
2090 }
2091}
2092
2093#undef LOG_TAG
2094#define LOG_TAG "AudioFlinger::EffectChain"
2095
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002096AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2097 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002098 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002099 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002100 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002101 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002102{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002103 sp<ThreadBase> p = thread.promote();
2104 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002105 return;
2106 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002107 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002108 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2109 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002110}
2111
2112AudioFlinger::EffectChain::~EffectChain()
2113{
Eric Laurentca7cc822012-11-19 14:55:58 -08002114}
2115
2116// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2117sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2118 effect_descriptor_t *descriptor)
2119{
2120 size_t size = mEffects.size();
2121
2122 for (size_t i = 0; i < size; i++) {
2123 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2124 return mEffects[i];
2125 }
2126 }
2127 return 0;
2128}
2129
2130// getEffectFromId_l() must be called with ThreadBase::mLock held
2131sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2132{
2133 size_t size = mEffects.size();
2134
2135 for (size_t i = 0; i < size; i++) {
2136 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2137 if (id == 0 || mEffects[i]->id() == id) {
2138 return mEffects[i];
2139 }
2140 }
2141 return 0;
2142}
2143
2144// getEffectFromType_l() must be called with ThreadBase::mLock held
2145sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2146 const effect_uuid_t *type)
2147{
2148 size_t size = mEffects.size();
2149
2150 for (size_t i = 0; i < size; i++) {
2151 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2152 return mEffects[i];
2153 }
2154 }
2155 return 0;
2156}
2157
Eric Laurent6c796322019-04-09 14:13:17 -07002158std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2159{
2160 std::vector<int> ids;
2161 Mutex::Autolock _l(mLock);
2162 for (size_t i = 0; i < mEffects.size(); i++) {
2163 ids.push_back(mEffects[i]->id());
2164 }
2165 return ids;
2166}
2167
Eric Laurentca7cc822012-11-19 14:55:58 -08002168void AudioFlinger::EffectChain::clearInputBuffer()
2169{
2170 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002171 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002172}
2173
2174// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002175void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002176{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002177 if (mInBuffer == NULL) {
2178 return;
2179 }
Andy Hung319587b2023-05-23 14:01:03 -07002180 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002181 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002182
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002184 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002185}
2186
2187// Must be called with EffectChain::mLock locked
2188void AudioFlinger::EffectChain::process_l()
2189{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002190 // never process effects when:
2191 // - on an OFFLOAD thread
2192 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002193 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002194 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002195 bool tracksOnSession = (trackCnt() != 0);
2196
2197 if (!tracksOnSession && mTailBufferCount == 0) {
2198 doProcess = false;
2199 }
2200
2201 if (activeTrackCnt() == 0) {
2202 // if no track is active and the effect tail has not been rendered,
2203 // the input buffer must be cleared here as the mixer process will not do it
2204 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002205 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002206 if (mTailBufferCount > 0) {
2207 mTailBufferCount--;
2208 }
2209 }
2210 }
2211 }
2212
2213 size_t size = mEffects.size();
2214 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002215 // Only the input and output buffers of the chain can be external,
2216 // and 'update' / 'commit' do nothing for allocated buffers, thus
2217 // it's not needed to consider any other buffers here.
2218 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002219 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2220 mOutBuffer->update();
2221 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 for (size_t i = 0; i < size; i++) {
2223 mEffects[i]->process();
2224 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002225 mInBuffer->commit();
2226 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2227 mOutBuffer->commit();
2228 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002229 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002230 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002231 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002232 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2233 }
2234 if (doResetVolume) {
2235 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002236 }
2237}
2238
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002239// createEffect_l() must be called with ThreadBase::mLock held
2240status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002241 effect_descriptor_t *desc,
2242 int id,
2243 audio_session_t sessionId,
2244 bool pinned)
2245{
2246 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002247 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002248 status_t lStatus = effect->status();
2249 if (lStatus == NO_ERROR) {
2250 lStatus = addEffect_ll(effect);
2251 }
2252 if (lStatus != NO_ERROR) {
2253 effect.clear();
2254 }
2255 return lStatus;
2256}
2257
2258// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002259status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2260{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002261 Mutex::Autolock _l(mLock);
2262 return addEffect_ll(effect);
2263}
2264// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2265status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2266{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002267 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002268
Eric Laurentb62d0362021-10-26 17:40:18 +02002269 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002270 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2271 // Auxiliary effects are inserted at the beginning of mEffects vector as
2272 // they are processed first and accumulated in chain input buffer
2273 mEffects.insertAt(effect, 0);
2274
2275 // the input buffer for auxiliary effect contains mono samples in
2276 // 32 bit format. This is to avoid saturation in AudoMixer
2277 // accumulation stage. Saturation is done in EffectModule::process() before
2278 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002279 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002280 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002281
Eric Laurent6b446ce2019-12-13 10:56:31 -08002282 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002283 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002284 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002285
2286 effect->configure();
2287
Mikhail Naganov022b9952017-01-04 16:36:51 -08002288 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002289 // auxiliary effects output samples to chain input buffer for further processing
2290 // by insert effects
2291 effect->setOutBuffer(mInBuffer);
2292 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002293 ssize_t idx_insert = getInsertIndex(desc);
2294 if (idx_insert < 0) {
2295 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002296 }
2297
Eric Laurentb62d0362021-10-26 17:40:18 +02002298 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002299 mEffects.insertAt(effect, idx_insert);
2300
2301 effect->configure();
2302
Eric Laurentb62d0362021-10-26 17:40:18 +02002303 // - By default:
2304 // All effects read samples from chain input buffer.
2305 // The last effect in the chain, writes samples to chain output buffer,
2306 // otherwise to chain input buffer
2307 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2308 // The spatializer effect (first effect) reads samples from the input buffer
2309 // and writes samples to the output buffer.
2310 // All other effects read and writes samples to the output buffer
2311 if (mEffectCallback->isSpatializer()
2312 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002313 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002314 if (idx_insert == 0) {
2315 if (previousSize != 0) {
2316 mEffects[1]->configure();
2317 mEffects[1]->setInBuffer(mOutBuffer);
2318 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2319 }
2320 effect->setInBuffer(mInBuffer);
2321 } else {
2322 effect->setInBuffer(mOutBuffer);
2323 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002324 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002325 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002326 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002327 if (idx_insert != 0) {
2328 mEffects[idx_insert-1]->configure();
2329 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2330 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2331 }
2332 effect->setOutBuffer(mOutBuffer);
2333 } else {
2334 effect->setOutBuffer(mInBuffer);
2335 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002336 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002337 ALOGV("%s effect %p, added in chain %p at rank %zu",
2338 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002339 }
2340 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002341
Eric Laurentca7cc822012-11-19 14:55:58 -08002342 return NO_ERROR;
2343}
2344
Eric Laurentb62d0362021-10-26 17:40:18 +02002345ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2346 // Insert effects are inserted at the end of mEffects vector as they are processed
2347 // after track and auxiliary effects.
2348 // Insert effect order as a function of indicated preference:
2349 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2350 // another effect is present
2351 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2352 // last effect claiming first position
2353 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2354 // first effect claiming last position
2355 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2356 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2357 // already present
2358 // Spatializer or Downmixer effects are inserted in first position because
2359 // they adapt the channel count for all other effects in the chain
2360 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2361 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2362 return 0;
2363 }
2364
2365 size_t size = mEffects.size();
2366 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2367 ssize_t idx_insert;
2368 ssize_t idx_insert_first = -1;
2369 ssize_t idx_insert_last = -1;
2370
2371 idx_insert = size;
2372 for (size_t i = 0; i < size; i++) {
2373 effect_descriptor_t d = mEffects[i]->desc();
2374 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2375 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2376 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2377 // check invalid effect chaining combinations
2378 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2379 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2380 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2381 __func__, desc.name, d.name);
2382 return -1;
2383 }
2384 // remember position of first insert effect and by default
2385 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002386 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002387 idx_insert = i;
2388 }
2389 // remember position of last insert effect claiming
2390 // first position
2391 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2392 idx_insert_first = i;
2393 }
2394 // remember position of first insert effect claiming
2395 // last position
2396 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2397 idx_insert_last == -1) {
2398 idx_insert_last = i;
2399 }
2400 }
2401 }
2402
2403 // modify idx_insert from first position if needed
2404 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2405 if (idx_insert_last != -1) {
2406 idx_insert = idx_insert_last;
2407 } else {
2408 idx_insert = size;
2409 }
2410 } else {
2411 if (idx_insert_first != -1) {
2412 idx_insert = idx_insert_first + 1;
2413 }
2414 }
2415 return idx_insert;
2416}
2417
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002418// removeEffect_l() must be called with ThreadBase::mLock held
2419size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2420 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002421{
2422 Mutex::Autolock _l(mLock);
2423 size_t size = mEffects.size();
2424 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2425
2426 for (size_t i = 0; i < size; i++) {
2427 if (effect == mEffects[i]) {
2428 // calling stop here will remove pre-processing effect from the audio HAL.
2429 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2430 // the middle of a read from audio HAL
2431 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2432 mEffects[i]->state() == EffectModule::STOPPING) {
2433 mEffects[i]->stop();
2434 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002435 if (release) {
2436 mEffects[i]->release_l();
2437 }
2438
Mikhail Naganov022b9952017-01-04 16:36:51 -08002439 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002440 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002441 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002442 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002443 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002444 }
2445 }
2446 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002447
2448 // make sure the input buffer configuration for the new first effect in the chain
2449 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2450 if (i == 0 && size > 1) {
2451 mEffects[0]->configure();
2452 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002453 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002454 }
2455
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002456 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002457 this, i);
2458 break;
2459 }
2460 }
2461
2462 return mEffects.size();
2463}
2464
jiabin8f278ee2019-11-11 12:16:27 -08002465// setDevices_l() must be called with ThreadBase::mLock held
2466void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002467{
2468 size_t size = mEffects.size();
2469 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002470 mEffects[i]->setDevices(devices);
2471 }
2472}
2473
2474// setInputDevice_l() must be called with ThreadBase::mLock held
2475void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2476{
2477 size_t size = mEffects.size();
2478 for (size_t i = 0; i < size; i++) {
2479 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002480 }
2481}
2482
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002483// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002484void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2485{
2486 size_t size = mEffects.size();
2487 for (size_t i = 0; i < size; i++) {
2488 mEffects[i]->setMode(mode);
2489 }
2490}
2491
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002492// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002493void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2494{
2495 size_t size = mEffects.size();
2496 for (size_t i = 0; i < size; i++) {
2497 mEffects[i]->setAudioSource(source);
2498 }
2499}
2500
Zhou Songd505c642020-02-20 16:35:37 +08002501bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2502 for (const auto &effect : mEffects) {
2503 if (effect->isVolumeControlEnabled()) return true;
2504 }
2505 return false;
2506}
2507
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002508// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002509bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002510{
2511 uint32_t newLeft = *left;
2512 uint32_t newRight = *right;
2513 bool hasControl = false;
2514 int ctrlIdx = -1;
2515 size_t size = mEffects.size();
2516
2517 // first update volume controller
2518 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002519 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002520 ctrlIdx = i - 1;
2521 hasControl = true;
2522 break;
2523 }
2524 }
2525
Eric Laurentfa1e1232016-08-02 19:01:49 -07002526 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002527 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002528 if (hasControl) {
2529 *left = mNewLeftVolume;
2530 *right = mNewRightVolume;
2531 }
2532 return hasControl;
2533 }
2534
2535 mVolumeCtrlIdx = ctrlIdx;
2536 mLeftVolume = newLeft;
2537 mRightVolume = newRight;
2538
2539 // second get volume update from volume controller
2540 if (ctrlIdx >= 0) {
2541 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2542 mNewLeftVolume = newLeft;
2543 mNewRightVolume = newRight;
2544 }
2545 // then indicate volume to all other effects in chain.
2546 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002547 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002548 uint32_t lVol = newLeft;
2549 uint32_t rVol = newRight;
2550
2551 for (size_t i = 0; i < size; i++) {
2552 if ((int)i == ctrlIdx) {
2553 continue;
2554 }
2555 // this also works for ctrlIdx == -1 when there is no volume controller
2556 if ((int)i > ctrlIdx) {
2557 lVol = *left;
2558 rVol = *right;
2559 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002560 // Pass requested volume directly if this is volume monitor module
2561 if (mEffects[i]->isVolumeMonitor()) {
2562 mEffects[i]->setVolume(left, right, false);
2563 } else {
2564 mEffects[i]->setVolume(&lVol, &rVol, false);
2565 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002566 }
2567 *left = newLeft;
2568 *right = newRight;
2569
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002570 setVolumeForOutput_l(*left, *right);
2571
Eric Laurentca7cc822012-11-19 14:55:58 -08002572 return hasControl;
2573}
2574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002575// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002576void AudioFlinger::EffectChain::resetVolume_l()
2577{
Eric Laurente7449bf2016-08-03 18:44:07 -07002578 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2579 uint32_t left = mLeftVolume;
2580 uint32_t right = mRightVolume;
2581 (void)setVolume_l(&left, &right, true);
2582 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002583}
2584
jiabineb3bda02020-06-30 14:07:03 -07002585// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2586bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2587{
2588 for (size_t i = 0; i < mEffects.size(); ++i) {
2589 if (mEffects[i]->isHapticGenerator()) {
2590 return true;
2591 }
2592 }
2593 return false;
2594}
2595
jiabine70bc7f2020-06-30 22:07:55 -07002596void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2597{
2598 Mutex::Autolock _l(mLock);
2599 for (size_t i = 0; i < mEffects.size(); ++i) {
2600 mEffects[i]->setHapticIntensity(id, intensity);
2601 }
2602}
2603
Eric Laurent1b928682014-10-02 19:41:47 -07002604void AudioFlinger::EffectChain::syncHalEffectsState()
2605{
2606 Mutex::Autolock _l(mLock);
2607 for (size_t i = 0; i < mEffects.size(); i++) {
2608 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2609 mEffects[i]->state() == EffectModule::STOPPING) {
2610 mEffects[i]->addEffectToHal_l();
2611 }
2612 }
2613}
2614
Eric Laurentca7cc822012-11-19 14:55:58 -08002615void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07002616NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002617{
Eric Laurentca7cc822012-11-19 14:55:58 -08002618 String8 result;
2619
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002620 const size_t numEffects = mEffects.size();
2621 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002622
Marco Nelissenb2208842014-02-07 14:00:50 -08002623 if (numEffects) {
2624 bool locked = AudioFlinger::dumpTryLock(mLock);
2625 // failed to lock - AudioFlinger is probably deadlocked
2626 if (!locked) {
2627 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002628 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002629
Andy Hungbded9c82017-11-30 18:47:35 -08002630 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2631 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2632 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2633 (int)inBufferStr.size(), "In buffer ",
2634 (int)outBufferStr.size(), "Out buffer ");
2635 result.appendFormat("\t%s %s %d\n",
2636 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002637 write(fd, result.string(), result.size());
2638
2639 for (size_t i = 0; i < numEffects; ++i) {
2640 sp<EffectModule> effect = mEffects[i];
2641 if (effect != 0) {
2642 effect->dump(fd, args);
2643 }
2644 }
2645
2646 if (locked) {
2647 mLock.unlock();
2648 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002649 } else {
2650 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002651 }
2652}
2653
2654// must be called with ThreadBase::mLock held
2655void AudioFlinger::EffectChain::setEffectSuspended_l(
2656 const effect_uuid_t *type, bool suspend)
2657{
2658 sp<SuspendedEffectDesc> desc;
2659 // use effect type UUID timelow as key as there is no real risk of identical
2660 // timeLow fields among effect type UUIDs.
2661 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2662 if (suspend) {
2663 if (index >= 0) {
2664 desc = mSuspendedEffects.valueAt(index);
2665 } else {
2666 desc = new SuspendedEffectDesc();
2667 desc->mType = *type;
2668 mSuspendedEffects.add(type->timeLow, desc);
2669 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2670 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002671
Eric Laurentca7cc822012-11-19 14:55:58 -08002672 if (desc->mRefCount++ == 0) {
2673 sp<EffectModule> effect = getEffectIfEnabled(type);
2674 if (effect != 0) {
2675 desc->mEffect = effect;
2676 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002677 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002678 }
2679 }
2680 } else {
2681 if (index < 0) {
2682 return;
2683 }
2684 desc = mSuspendedEffects.valueAt(index);
2685 if (desc->mRefCount <= 0) {
2686 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002687 desc->mRefCount = 0;
2688 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002689 }
2690 if (--desc->mRefCount == 0) {
2691 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2692 if (desc->mEffect != 0) {
2693 sp<EffectModule> effect = desc->mEffect.promote();
2694 if (effect != 0) {
2695 effect->setSuspended(false);
2696 effect->lock();
2697 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002698 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002699 effect->setEnabled_l(handle->enabled());
2700 }
2701 effect->unlock();
2702 }
2703 desc->mEffect.clear();
2704 }
2705 mSuspendedEffects.removeItemsAt(index);
2706 }
2707 }
2708}
2709
2710// must be called with ThreadBase::mLock held
2711void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2712{
2713 sp<SuspendedEffectDesc> desc;
2714
2715 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2716 if (suspend) {
2717 if (index >= 0) {
2718 desc = mSuspendedEffects.valueAt(index);
2719 } else {
2720 desc = new SuspendedEffectDesc();
2721 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2722 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2723 }
2724 if (desc->mRefCount++ == 0) {
2725 Vector< sp<EffectModule> > effects;
2726 getSuspendEligibleEffects(effects);
2727 for (size_t i = 0; i < effects.size(); i++) {
2728 setEffectSuspended_l(&effects[i]->desc().type, true);
2729 }
2730 }
2731 } else {
2732 if (index < 0) {
2733 return;
2734 }
2735 desc = mSuspendedEffects.valueAt(index);
2736 if (desc->mRefCount <= 0) {
2737 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2738 desc->mRefCount = 1;
2739 }
2740 if (--desc->mRefCount == 0) {
2741 Vector<const effect_uuid_t *> types;
2742 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2743 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2744 continue;
2745 }
2746 types.add(&mSuspendedEffects.valueAt(i)->mType);
2747 }
2748 for (size_t i = 0; i < types.size(); i++) {
2749 setEffectSuspended_l(types[i], false);
2750 }
2751 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2752 mSuspendedEffects.keyAt(index));
2753 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2754 }
2755 }
2756}
2757
2758
2759// The volume effect is used for automated tests only
2760#ifndef OPENSL_ES_H_
2761static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2762 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2763const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2764#endif //OPENSL_ES_H_
2765
Eric Laurentd8365c52017-07-16 15:27:05 -07002766/* static */
2767bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2768{
2769 // Only NS and AEC are suspended when BtNRec is off
2770 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2771 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2772 return true;
2773 }
2774 return false;
2775}
2776
Eric Laurentca7cc822012-11-19 14:55:58 -08002777bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2778{
2779 // auxiliary effects and visualizer are never suspended on output mix
2780 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2781 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2782 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002783 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2784 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002785 return false;
2786 }
2787 return true;
2788}
2789
2790void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2791 Vector< sp<AudioFlinger::EffectModule> > &effects)
2792{
2793 effects.clear();
2794 for (size_t i = 0; i < mEffects.size(); i++) {
2795 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2796 effects.add(mEffects[i]);
2797 }
2798 }
2799}
2800
2801sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2802 const effect_uuid_t *type)
2803{
2804 sp<EffectModule> effect = getEffectFromType_l(type);
2805 return effect != 0 && effect->isEnabled() ? effect : 0;
2806}
2807
2808void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2809 bool enabled)
2810{
2811 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2812 if (enabled) {
2813 if (index < 0) {
2814 // if the effect is not suspend check if all effects are suspended
2815 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2816 if (index < 0) {
2817 return;
2818 }
2819 if (!isEffectEligibleForSuspend(effect->desc())) {
2820 return;
2821 }
2822 setEffectSuspended_l(&effect->desc().type, enabled);
2823 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2824 if (index < 0) {
2825 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2826 return;
2827 }
2828 }
2829 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2830 effect->desc().type.timeLow);
2831 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002832 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002833 if (desc->mEffect == 0) {
2834 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002835 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002836 effect->setSuspended(true);
2837 }
2838 } else {
2839 if (index < 0) {
2840 return;
2841 }
2842 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2843 effect->desc().type.timeLow);
2844 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2845 desc->mEffect.clear();
2846 effect->setSuspended(false);
2847 }
2848}
2849
Eric Laurent5baf2af2013-09-12 17:37:00 -07002850bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002851{
2852 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002853 return isNonOffloadableEnabled_l();
2854}
2855
2856bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2857{
Eric Laurent813e2a72013-08-31 12:59:48 -07002858 size_t size = mEffects.size();
2859 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002860 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002861 return true;
2862 }
2863 }
2864 return false;
2865}
2866
Eric Laurentaaa44472014-09-12 17:41:50 -07002867void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2868{
2869 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002870 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002871}
2872
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002873void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2874{
2875 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2876 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2877 }
2878 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2879 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2880 }
2881}
2882
2883void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2884{
2885 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2886 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2887 }
2888 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2889 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2890 }
2891}
2892
2893bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002894{
2895 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002896 for (const auto &effect : mEffects) {
2897 if (effect->isProcessImplemented()) {
2898 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002899 }
2900 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002901 // Allow effects without processing.
2902 return true;
2903}
2904
2905bool AudioFlinger::EffectChain::isFastCompatible() const
2906{
2907 Mutex::Autolock _l(mLock);
2908 for (const auto &effect : mEffects) {
2909 if (effect->isProcessImplemented()
2910 && effect->isImplementationSoftware()) {
2911 return false;
2912 }
2913 }
2914 // Allow effects without processing or hw accelerated effects.
2915 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002916}
2917
2918// isCompatibleWithThread_l() must be called with thread->mLock held
2919bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2920{
2921 Mutex::Autolock _l(mLock);
2922 for (size_t i = 0; i < mEffects.size(); i++) {
2923 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2924 return false;
2925 }
2926 }
2927 return true;
2928}
2929
Eric Laurent6b446ce2019-12-13 10:56:31 -08002930// EffectCallbackInterface implementation
2931status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2932 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2933 sp<EffectHalInterface> *effect) {
2934 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002935 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002936 if (effectsFactory != 0) {
2937 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2938 }
2939 return status;
2940}
2941
2942bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002943 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002944 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002945 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002946}
2947
2948status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2949 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002950 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002951}
2952
2953status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07002954 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002955 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002956 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002957 if (t == nullptr) {
2958 return result;
2959 }
2960 sp <StreamHalInterface> st = t->stream();
2961 if (st == nullptr) {
2962 return result;
2963 }
2964 result = st->addEffect(effect);
2965 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2966 return result;
2967}
2968
2969status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07002970 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002971 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002972 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002973 if (t == nullptr) {
2974 return result;
2975 }
2976 sp <StreamHalInterface> st = t->stream();
2977 if (st == nullptr) {
2978 return result;
2979 }
2980 result = st->removeEffect(effect);
2981 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2982 return result;
2983}
2984
2985audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002986 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002987 if (t == nullptr) {
2988 return AUDIO_IO_HANDLE_NONE;
2989 }
2990 return t->id();
2991}
2992
2993bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002994 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002995 if (t == nullptr) {
2996 return true;
2997 }
2998 return t->isOutput();
2999}
3000
3001bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003002 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003003}
3004
3005bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003006 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003007}
3008
3009bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003010 switch (mThreadType) {
3011 case ThreadBase::OFFLOAD:
3012 case ThreadBase::MMAP_PLAYBACK:
3013 case ThreadBase::MMAP_CAPTURE:
3014 return true;
3015 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003016 return false;
3017 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003018}
3019
3020bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3021 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003022}
3023
3024uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003025 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003026 if (t == nullptr) {
3027 return 0;
3028 }
3029 return t->sampleRate();
3030}
3031
Eric Laurentf1f22e72021-07-13 14:04:14 +02003032audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3033 sp<ThreadBase> t = thread().promote();
3034 if (t == nullptr) {
3035 return AUDIO_CHANNEL_NONE;
3036 }
3037 sp<EffectChain> c = chain().promote();
3038 if (c == nullptr) {
3039 return AUDIO_CHANNEL_NONE;
3040 }
3041
Eric Laurentb62d0362021-10-26 17:40:18 +02003042 if (mThreadType == ThreadBase::SPATIALIZER) {
3043 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3044 if (c->isFirstEffect(id)) {
3045 return t->mixerChannelMask();
3046 } else {
3047 return t->channelMask();
3048 }
3049 } else if (!audio_is_global_session(c->sessionId())) {
3050 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3051 return t->mixerChannelMask();
3052 } else {
3053 return t->channelMask();
3054 }
3055 } else {
3056 return t->channelMask();
3057 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003058 } else {
3059 return t->channelMask();
3060 }
3061}
3062
3063uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003064 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003065}
3066
3067audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003068 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069 if (t == nullptr) {
3070 return AUDIO_CHANNEL_NONE;
3071 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003072 sp<EffectChain> c = chain().promote();
3073 if (c == nullptr) {
3074 return AUDIO_CHANNEL_NONE;
3075 }
3076
3077 if (mThreadType == ThreadBase::SPATIALIZER) {
3078 if (!audio_is_global_session(c->sessionId())) {
3079 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3080 return t->mixerChannelMask();
3081 } else {
3082 return t->channelMask();
3083 }
3084 } else {
3085 return t->channelMask();
3086 }
3087 } else {
3088 return t->channelMask();
3089 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003090}
3091
Eric Laurentf1f22e72021-07-13 14:04:14 +02003092uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003093 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003094}
3095
jiabineb3bda02020-06-30 14:07:03 -07003096audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003097 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003098 if (t == nullptr) {
3099 return AUDIO_CHANNEL_NONE;
3100 }
3101 return t->hapticChannelMask();
3102}
3103
Eric Laurent6b446ce2019-12-13 10:56:31 -08003104size_t AudioFlinger::EffectChain::EffectCallback::frameCount() 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->frameCount();
3110}
3111
Andy Hung71ba4b32022-10-06 12:09:49 -07003112uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3113NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3114{
Andy Hung328d6772021-01-12 12:32:21 -08003115 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003116 if (t == nullptr) {
3117 return 0;
3118 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003119 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003120 return t->latency_l();
3121}
3122
Andy Hung71ba4b32022-10-06 12:09:49 -07003123void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3124NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3125{
Andy Hung328d6772021-01-12 12:32:21 -08003126 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003127 if (t == nullptr) {
3128 return;
3129 }
3130 t->setVolumeForOutput_l(left, right);
3131}
3132
3133void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003134 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003135 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003136 if (t == nullptr) {
3137 return;
3138 }
3139 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3140
Andy Hung328d6772021-01-12 12:32:21 -08003141 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003142 if (c == nullptr) {
3143 return;
3144 }
Eric Laurent41709552019-12-16 19:34:05 -08003145 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3146 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003147}
3148
Eric Laurent41709552019-12-16 19:34:05 -08003149void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003150 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003151 if (t == nullptr) {
3152 return;
3153 }
Eric Laurent41709552019-12-16 19:34:05 -08003154 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3155 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003156}
3157
Eric Laurent41709552019-12-16 19:34:05 -08003158void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003159 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3160
Andy Hung328d6772021-01-12 12:32:21 -08003161 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003162 if (t == nullptr) {
3163 return;
3164 }
3165 t->onEffectDisable();
3166}
3167
3168bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3169 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003170 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171 if (t == nullptr) {
3172 return false;
3173 }
3174 t->disconnectEffectHandle(handle, unpinIfLast);
3175 return true;
3176}
3177
3178void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003179 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003180 if (c == nullptr) {
3181 return;
3182 }
3183 c->resetVolume_l();
3184
3185}
3186
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003187product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003188 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003189 if (c == nullptr) {
3190 return PRODUCT_STRATEGY_NONE;
3191 }
3192 return c->strategy();
3193}
3194
3195int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003196 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003197 if (c == nullptr) {
3198 return 0;
3199 }
3200 return c->activeTrackCnt();
3201}
3202
Eric Laurentb82e6b72019-11-22 17:25:04 -08003203
3204#undef LOG_TAG
3205#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3206
3207status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3208{
3209 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3210 Mutex::Autolock _l(mProxyLock);
3211 if (status == NO_ERROR) {
3212 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003213 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003214 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003215 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003216 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003217 bs = handle.second->disable(&status);
3218 }
3219 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003220 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003221 }
3222 }
3223 }
3224 ALOGV("%s enable %d status %d", __func__, enabled, status);
3225 return status;
3226}
3227
3228status_t AudioFlinger::DeviceEffectProxy::init(
3229 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3230//For all audio patches
3231//If src or sink device match
3232//If the effect is HW accelerated
3233// if no corresponding effect module
3234// Create EffectModule: mHalEffect
3235//Create and attach EffectHandle
3236//If the effect is not HW accelerated and the patch sink or src is a mixer port
3237// Create Effect on patch input or output thread on session -1
3238//Add EffectHandle to EffectHandle map of Effect Proxy:
3239 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3240 status_t status = NO_ERROR;
3241 for (auto &patch : patches) {
3242 status = onCreatePatch(patch.first, patch.second);
3243 ALOGV("%s onCreatePatch status %d", __func__, status);
3244 if (status == BAD_VALUE) {
3245 return status;
3246 }
3247 }
3248 return status;
3249}
3250
3251status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3252 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3253 status_t status = NAME_NOT_FOUND;
3254 sp<EffectHandle> handle;
3255 // only consider source[0] as this is the only "true" source of a patch
3256 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3257 ALOGV("%s source checkPort status %d", __func__, status);
3258 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3259 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3260 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3261 }
3262 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3263 Mutex::Autolock _l(mProxyLock);
3264 mEffectHandles.emplace(patchHandle, handle);
3265 }
3266 ALOGW_IF(status == BAD_VALUE,
3267 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3268
3269 return status;
3270}
3271
3272status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3273 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3274
3275 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3276 __func__, port->type, port->ext.device.type,
3277 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003278 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3279 port->ext.device.address != mDevice.address()) {
3280 return NAME_NOT_FOUND;
3281 }
3282 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3283 (audio_port_config_has_input_direction(port))) {
3284 ALOGI("%s don't create postprocessing effect on record port", __func__);
3285 return NAME_NOT_FOUND;
3286 }
3287 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3288 (!audio_port_config_has_input_direction(port))) {
3289 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003290 return NAME_NOT_FOUND;
3291 }
3292 status_t status = NAME_NOT_FOUND;
3293
3294 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3295 Mutex::Autolock _l(mProxyLock);
3296 mDevicePort = *port;
3297 mHalEffect = new EffectModule(mMyCallback,
3298 const_cast<effect_descriptor_t *>(&mDescriptor),
3299 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3300 false /* pinned */, port->id);
3301 if (audio_is_input_device(mDevice.mType)) {
3302 mHalEffect->setInputDevice(mDevice);
3303 } else {
3304 mHalEffect->setDevices({mDevice});
3305 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003306 mHalEffect->configure();
3307
Eric Laurentde8caf42021-08-11 17:19:25 +02003308 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3309 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003310 status = (*handle)->initCheck();
3311 if (status == OK) {
3312 status = mHalEffect->addHandle((*handle).get());
3313 } else {
3314 mHalEffect.clear();
3315 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3316 }
3317 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3318 sp <ThreadBase> thread;
3319 if (audio_port_config_has_input_direction(port)) {
3320 if (patch.isSoftware()) {
3321 thread = patch.mRecord.thread();
3322 } else {
3323 thread = patch.thread().promote();
3324 }
3325 } else {
3326 if (patch.isSoftware()) {
3327 thread = patch.mPlayback.thread();
3328 } else {
3329 thread = patch.thread().promote();
3330 }
3331 }
3332 int enabled;
3333 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3334 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003335 &enabled, &status, false, false /*probe*/,
3336 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003337 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3338 } else {
3339 status = BAD_VALUE;
3340 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003341
Eric Laurentb82e6b72019-11-22 17:25:04 -08003342 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003343 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003344 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003345 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003346 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003347 bs = (*handle)->disable(&status);
3348 }
3349 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003350 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003351 }
3352 }
3353 return status;
3354}
3355
3356void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003357 sp<EffectHandle> effect;
3358 {
3359 Mutex::Autolock _l(mProxyLock);
3360 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3361 effect = mEffectHandles.at(patchHandle);
3362 mEffectHandles.erase(patchHandle);
3363 }
3364 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003365}
3366
3367
3368size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3369{
3370 Mutex::Autolock _l(mProxyLock);
3371 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003372 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003373 mHalEffect.clear();
3374 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3375 }
3376 return mHalEffect == nullptr ? 0 : 1;
3377}
3378
3379status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003380 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003381 if (mHalEffect == nullptr) {
3382 return NO_INIT;
3383 }
3384 return mManagerCallback->addEffectToHal(
3385 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3386}
3387
3388status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003389 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003390 if (mHalEffect == nullptr) {
3391 return NO_INIT;
3392 }
3393 return mManagerCallback->removeEffectFromHal(
3394 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3395}
3396
3397bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3398 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3399 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3400 }
3401 return true;
3402}
3403
3404uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3405 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3406 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3407 return mDevicePort.sample_rate;
3408 }
3409 return DEFAULT_OUTPUT_SAMPLE_RATE;
3410}
3411
3412audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3413 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3414 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3415 return mDevicePort.channel_mask;
3416 }
3417 return AUDIO_CHANNEL_OUT_STEREO;
3418}
3419
3420uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3421 if (isOutput()) {
3422 return audio_channel_count_from_out_mask(channelMask());
3423 }
3424 return audio_channel_count_from_in_mask(channelMask());
3425}
3426
Andy Hung71ba4b32022-10-06 12:09:49 -07003427void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3428NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3429{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003430 const Vector<String16> args;
3431 EffectBase::dump(fd, args);
3432
3433 const bool locked = dumpTryLock(mProxyLock);
3434 if (!locked) {
3435 String8 result("DeviceEffectProxy may be deadlocked\n");
3436 write(fd, result.string(), result.size());
3437 }
3438
3439 String8 outStr;
3440 if (mHalEffect != nullptr) {
3441 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3442 } else {
3443 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3444 }
3445 write(fd, outStr.string(), outStr.size());
3446 outStr.clear();
3447
3448 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3449 write(fd, outStr.string(), outStr.size());
3450 outStr.clear();
3451
3452 for (const auto& iter : mEffectHandles) {
3453 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3454 write(fd, outStr.string(), outStr.size());
3455 outStr.clear();
3456 sp<EffectBase> effect = iter.second->effect().promote();
3457 if (effect != nullptr) {
3458 effect->dump(fd, args);
3459 }
3460 }
3461
3462 if (locked) {
3463 mLock.unlock();
3464 }
3465}
3466
3467#undef LOG_TAG
3468#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3469
3470int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3471 return mManagerCallback->newEffectId();
3472}
3473
3474
3475bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3476 EffectHandle *handle, bool unpinIfLast) {
3477 sp<EffectBase> effectBase = handle->effect().promote();
3478 if (effectBase == nullptr) {
3479 return false;
3480 }
3481
3482 sp<EffectModule> effect = effectBase->asEffectModule();
3483 if (effect == nullptr) {
3484 return false;
3485 }
3486
3487 // restore suspended effects if the disconnected handle was enabled and the last one.
3488 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3489 if (remove) {
3490 sp<DeviceEffectProxy> proxy = mProxy.promote();
3491 if (proxy != nullptr) {
3492 proxy->removeEffect(effect);
3493 }
3494 if (handle->enabled()) {
3495 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3496 }
3497 }
3498 return true;
3499}
3500
3501status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3502 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3503 sp<EffectHalInterface> *effect) {
3504 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3505}
3506
3507status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003508 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003509 sp<DeviceEffectProxy> proxy = mProxy.promote();
3510 if (proxy == nullptr) {
3511 return NO_INIT;
3512 }
3513 return proxy->addEffectToHal(effect);
3514}
3515
3516status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003517 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003518 sp<DeviceEffectProxy> proxy = mProxy.promote();
3519 if (proxy == nullptr) {
3520 return NO_INIT;
3521 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003522 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003523}
3524
3525bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3526 sp<DeviceEffectProxy> proxy = mProxy.promote();
3527 if (proxy == nullptr) {
3528 return true;
3529 }
3530 return proxy->isOutput();
3531}
3532
3533uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3534 sp<DeviceEffectProxy> proxy = mProxy.promote();
3535 if (proxy == nullptr) {
3536 return DEFAULT_OUTPUT_SAMPLE_RATE;
3537 }
3538 return proxy->sampleRate();
3539}
3540
Eric Laurentf1f22e72021-07-13 14:04:14 +02003541audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3542 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003543 sp<DeviceEffectProxy> proxy = mProxy.promote();
3544 if (proxy == nullptr) {
3545 return AUDIO_CHANNEL_OUT_STEREO;
3546 }
3547 return proxy->channelMask();
3548}
3549
Eric Laurentf1f22e72021-07-13 14:04:14 +02003550uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3551 sp<DeviceEffectProxy> proxy = mProxy.promote();
3552 if (proxy == nullptr) {
3553 return 2;
3554 }
3555 return proxy->channelCount();
3556}
3557
3558audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3559 sp<DeviceEffectProxy> proxy = mProxy.promote();
3560 if (proxy == nullptr) {
3561 return AUDIO_CHANNEL_OUT_STEREO;
3562 }
3563 return proxy->channelMask();
3564}
3565
3566uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003567 sp<DeviceEffectProxy> proxy = mProxy.promote();
3568 if (proxy == nullptr) {
3569 return 2;
3570 }
3571 return proxy->channelCount();
3572}
3573
Eric Laurent76c89f32021-12-03 17:13:23 +01003574void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3575 const sp<EffectBase>& effectBase) {
3576 sp<EffectModule> effect = effectBase->asEffectModule();
3577 if (effect == nullptr) {
3578 return;
3579 }
3580 effect->start();
3581}
3582
3583void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3584 const sp<EffectBase>& effectBase) {
3585 sp<EffectModule> effect = effectBase->asEffectModule();
3586 if (effect == nullptr) {
3587 return;
3588 }
3589 effect->stop();
3590}
3591
Glenn Kasten63238ef2015-03-02 15:50:29 -08003592} // namespace android