blob: 081d9a9d0a133dd920a4b3dd9b9307fa302ba4d4 [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"
Andy Hungba8e52b2023-05-11 14:33:03 -070047#include "EffectConfiguration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080048
49// ----------------------------------------------------------------------------
50
51// Note: the following macro is used for extremely verbose logging message. In
52// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
53// 0; but one side effect of this is to turn all LOGV's as well. Some messages
54// are so verbose that we want to suppress them even when we have ALOG_ASSERT
55// turned on. Do not uncomment the #def below unless you really know what you
56// are doing and want to see all of the extremely verbose messages.
57//#define VERY_VERY_VERBOSE_LOGGING
58#ifdef VERY_VERY_VERBOSE_LOGGING
59#define ALOGVV ALOGV
60#else
61#define ALOGVV(a...) do { } while(0)
62#endif
63
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090064#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
65
Eric Laurentca7cc822012-11-19 14:55:58 -080066namespace android {
67
Andy Hung1131b6e2020-12-08 20:47:45 -080068using aidl_utils::statusTFromBinderStatus;
Andy Hungba8e52b2023-05-11 14:33:03 -070069using audioflinger::EffectConfiguration;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070070using binder::Status;
71
72namespace {
73
74// Append a POD value into a vector of bytes.
75template<typename T>
76void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
77 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
78 buffer->insert(buffer->end(), ar, ar + sizeof(T));
79}
80
81// Write a POD value into a vector of bytes (clears the previous buffer
82// content).
83template<typename T>
84void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
85 buffer->clear();
86 appendToBuffer(value, buffer);
87}
88
89} // namespace
90
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080092// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080093// ----------------------------------------------------------------------------
94
95#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080096#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080097
Eric Laurent41709552019-12-16 19:34:05 -080098AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080099 effect_descriptor_t *desc,
100 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800101 audio_session_t sessionId,
102 bool pinned)
103 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800104 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800105 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800106{
Eric Laurentca7cc822012-11-19 14:55:58 -0800107}
108
Eric Laurent41709552019-12-16 19:34:05 -0800109// must be called with EffectModule::mLock held
110status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800111{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800112
Eric Laurent41709552019-12-16 19:34:05 -0800113 ALOGV("setEnabled %p enabled %d", this, enabled);
114
115 if (enabled != isEnabled()) {
116 switch (mState) {
117 // going from disabled to enabled
118 case IDLE:
119 mState = STARTING;
120 break;
121 case STOPPED:
122 mState = RESTART;
123 break;
124 case STOPPING:
125 mState = ACTIVE;
126 break;
127
128 // going from enabled to disabled
129 case RESTART:
130 mState = STOPPED;
131 break;
132 case STARTING:
133 mState = IDLE;
134 break;
135 case ACTIVE:
136 mState = STOPPING;
137 break;
138 case DESTROYED:
139 return NO_ERROR; // simply ignore as we are being destroyed
140 }
141 for (size_t i = 1; i < mHandles.size(); i++) {
142 EffectHandle *h = mHandles[i];
143 if (h != NULL && !h->disconnected()) {
144 h->setEnabled(enabled);
145 }
146 }
147 }
148 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800149}
150
Eric Laurent41709552019-12-16 19:34:05 -0800151status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
152{
153 status_t status;
154 {
155 Mutex::Autolock _l(mLock);
156 status = setEnabled_l(enabled);
157 }
158 if (fromHandle) {
159 if (enabled) {
160 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800162 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700163 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800164 }
165 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700166 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800167 }
168 }
169 return status;
170}
171
172bool AudioFlinger::EffectBase::isEnabled() const
173{
174 switch (mState) {
175 case RESTART:
176 case STARTING:
177 case ACTIVE:
178 return true;
179 case IDLE:
180 case STOPPING:
181 case STOPPED:
182 case DESTROYED:
183 default:
184 return false;
185 }
186}
187
188void AudioFlinger::EffectBase::setSuspended(bool suspended)
189{
190 Mutex::Autolock _l(mLock);
191 mSuspended = suspended;
192}
193
194bool AudioFlinger::EffectBase::suspended() const
195{
196 Mutex::Autolock _l(mLock);
197 return mSuspended;
198}
199
200status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800201{
202 status_t status;
203
204 Mutex::Autolock _l(mLock);
205 int priority = handle->priority();
206 size_t size = mHandles.size();
207 EffectHandle *controlHandle = NULL;
208 size_t i;
209 for (i = 0; i < size; i++) {
210 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800211 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 continue;
213 }
214 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700217 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800218 if (h->priority() <= priority) {
219 break;
220 }
221 }
222 // if inserted in first place, move effect control from previous owner to this handle
223 if (i == 0) {
224 bool enabled = false;
225 if (controlHandle != NULL) {
226 enabled = controlHandle->enabled();
227 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
228 }
229 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
230 status = NO_ERROR;
231 } else {
232 status = ALREADY_EXISTS;
233 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700234 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800235 mHandles.insertAt(handle, i);
236 return status;
237}
238
Eric Laurent41709552019-12-16 19:34:05 -0800239status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700240{
241 status_t status = NO_ERROR;
242 bool doRegister = false;
243 bool registered = false;
244 bool doEnable = false;
245 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700246 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800247 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700248
249 {
250 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200251
252 if ((isInternal_l() && !mPolicyRegistered)
253 || !getCallback()->isAudioPolicyReady()) {
254 return NO_ERROR;
255 }
256
Eric Laurent6c796322019-04-09 14:13:17 -0700257 // register effect when first handle is attached and unregister when last handle is removed
258 if (mPolicyRegistered != mHandles.size() > 0) {
259 doRegister = true;
260 mPolicyRegistered = mHandles.size() > 0;
261 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700262 const auto callback = getCallback();
263 io = callback->io();
264 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700265 }
266 }
267 // enable effect when registered according to enable state requested by controlling handle
268 if (mHandles.size() > 0) {
269 EffectHandle *handle = controlHandle_l();
270 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
271 doEnable = true;
272 mPolicyEnabled = handle->enabled();
273 }
274 }
275 registered = mPolicyRegistered;
276 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100277 // The simultaneous release of two EffectHandles with the same EffectModule
278 // may cause us to call this method at the same time.
279 // This may deadlock under some circumstances (b/180941720). Avoid this.
280 if (!doRegister && !(registered && doEnable)) {
281 return NO_ERROR;
282 }
Eric Laurent6c796322019-04-09 14:13:17 -0700283 }
fengjnlan46407562022-09-07 16:20:01 +0800284 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700285 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
286 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
287 if (doRegister) {
288 if (registered) {
289 status = AudioSystem::registerEffect(
290 &mDescriptor,
291 io,
292 strategy,
293 mSessionId,
294 mId);
295 } else {
296 status = AudioSystem::unregisterEffect(mId);
297 }
298 }
299 if (registered && doEnable) {
300 status = AudioSystem::setEffectEnabled(mId, enabled);
301 }
302 mPolicyLock.unlock();
303
304 return status;
305}
306
307
Eric Laurent41709552019-12-16 19:34:05 -0800308ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800309{
310 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800311 return removeHandle_l(handle);
312}
313
Eric Laurent41709552019-12-16 19:34:05 -0800314ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800315{
Eric Laurentca7cc822012-11-19 14:55:58 -0800316 size_t size = mHandles.size();
317 size_t i;
318 for (i = 0; i < size; i++) {
319 if (mHandles[i] == handle) {
320 break;
321 }
322 }
323 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800324 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
325 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800326 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800327 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800328
329 mHandles.removeAt(i);
330 // if removed from first place, move effect control from this handle to next in line
331 if (i == 0) {
332 EffectHandle *h = controlHandle_l();
333 if (h != NULL) {
334 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
335 }
336 }
337
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530338 // Prevent calls to process() and other functions on effect interface from now on.
339 // The effect engine will be released by the destructor when the last strong reference on
340 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 if (mHandles.size() == 0 && !mPinned) {
342 mState = DESTROYED;
343 }
344
345 return mHandles.size();
346}
347
348// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800349AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800350{
351 // the first valid handle in the list has control over the module
352 for (size_t i = 0; i < mHandles.size(); i++) {
353 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800354 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800355 return h;
356 }
357 }
358
359 return NULL;
360}
361
Eric Laurentf10c7092016-12-06 17:09:56 -0800362// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800363ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800364{
Andy Hungfda44002021-06-03 17:23:16 -0700365 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800366 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700367 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800368 return mHandles.size();
369 }
370
Eric Laurentf10c7092016-12-06 17:09:56 -0800371 Mutex::Autolock _l(mLock);
372 ssize_t numHandles = removeHandle_l(handle);
373 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700375 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800376 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800377 }
378 return numHandles;
379}
380
Eric Laurent41709552019-12-16 19:34:05 -0800381bool AudioFlinger::EffectBase::purgeHandles()
382{
383 bool enabled = false;
384 Mutex::Autolock _l(mLock);
385 EffectHandle *handle = controlHandle_l();
386 if (handle != NULL) {
387 enabled = handle->enabled();
388 }
389 mHandles.clear();
390 return enabled;
391}
392
393void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700394 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800395}
396
397static String8 effectFlagsToString(uint32_t flags) {
398 String8 s;
399
400 s.append("conn. mode: ");
401 switch (flags & EFFECT_FLAG_TYPE_MASK) {
402 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
403 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
404 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
405 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
406 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
407 default: s.append("unknown/reserved"); break;
408 }
409 s.append(", ");
410
411 s.append("insert pref: ");
412 switch (flags & EFFECT_FLAG_INSERT_MASK) {
413 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
414 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
415 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
416 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
417 default: s.append("unknown/reserved"); break;
418 }
419 s.append(", ");
420
421 s.append("volume mgmt: ");
422 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
423 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
424 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
425 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
426 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
427 default: s.append("unknown/reserved"); break;
428 }
429 s.append(", ");
430
431 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
432 if (devind) {
433 s.append("device indication: ");
434 switch (devind) {
435 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
436 default: s.append("unknown/reserved"); break;
437 }
438 s.append(", ");
439 }
440
441 s.append("input mode: ");
442 switch (flags & EFFECT_FLAG_INPUT_MASK) {
443 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
444 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
445 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
446 default: s.append("not set"); break;
447 }
448 s.append(", ");
449
450 s.append("output mode: ");
451 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
452 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
453 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
454 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
455 default: s.append("not set"); break;
456 }
457 s.append(", ");
458
459 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
460 if (accel) {
461 s.append("hardware acceleration: ");
462 switch (accel) {
463 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
464 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
465 default: s.append("unknown/reserved"); break;
466 }
467 s.append(", ");
468 }
469
470 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
471 if (modeind) {
472 s.append("mode indication: ");
473 switch (modeind) {
474 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
475 default: s.append("unknown/reserved"); break;
476 }
477 s.append(", ");
478 }
479
480 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
481 if (srcind) {
482 s.append("source indication: ");
483 switch (srcind) {
484 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
485 default: s.append("unknown/reserved"); break;
486 }
487 s.append(", ");
488 }
489
490 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
491 s.append("offloadable, ");
492 }
493
494 int len = s.length();
495 if (s.length() > 2) {
496 (void) s.lockBuffer(len);
497 s.unlockBuffer(len - 2);
498 }
499 return s;
500}
501
502void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
Andy Hung71ba4b32022-10-06 12:09:49 -0700503NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800504{
505 String8 result;
506
507 result.appendFormat("\tEffect ID %d:\n", mId);
508
509 bool locked = AudioFlinger::dumpTryLock(mLock);
510 // failed to lock - AudioFlinger is probably deadlocked
511 if (!locked) {
512 result.append("\t\tCould not lock Fx mutex:\n");
513 }
514
515 result.append("\t\tSession State Registered Enabled Suspended:\n");
516 result.appendFormat("\t\t%05d %03d %s %s %s\n",
517 mSessionId, mState, mPolicyRegistered ? "y" : "n",
518 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
519
520 result.append("\t\tDescriptor:\n");
521 char uuidStr[64];
522 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
523 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
524 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
525 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
526 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
527 mDescriptor.apiVersion,
528 mDescriptor.flags,
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000529 effectFlagsToString(mDescriptor.flags).c_str());
Eric Laurent41709552019-12-16 19:34:05 -0800530 result.appendFormat("\t\t- name: %s\n",
531 mDescriptor.name);
532
533 result.appendFormat("\t\t- implementor: %s\n",
534 mDescriptor.implementor);
535
536 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
537 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
538 char buffer[256];
539 for (size_t i = 0; i < mHandles.size(); ++i) {
540 EffectHandle *handle = mHandles[i];
541 if (handle != NULL && !handle->disconnected()) {
542 handle->dumpToBuffer(buffer, sizeof(buffer));
543 result.append(buffer);
544 }
545 }
546 if (locked) {
547 mLock.unlock();
548 }
549
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +0000550 write(fd, result.c_str(), result.length());
Eric Laurent41709552019-12-16 19:34:05 -0800551}
552
553// ----------------------------------------------------------------------------
554// EffectModule implementation
555// ----------------------------------------------------------------------------
556
557#undef LOG_TAG
558#define LOG_TAG "AudioFlinger::EffectModule"
559
560AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
561 effect_descriptor_t *desc,
562 int id,
563 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800564 bool pinned,
565 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800566 : EffectBase(callback, desc, id, sessionId, pinned),
567 // clear mConfig to ensure consistent initial value of buffer framecount
568 // in case buffers are associated by setInBuffer() or setOutBuffer()
569 // prior to configure().
570 mConfig{{}, {}},
571 mStatus(NO_INIT),
572 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
573 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800574 mOffloaded(false),
Mikhail Naganov59984db2022-04-19 21:21:23 +0000575 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800576 , mSupportsFloat(false)
Eric Laurent41709552019-12-16 19:34:05 -0800577{
578 ALOGV("Constructor %p pinned %d", this, pinned);
579 int lStatus;
580
581 // create effect engine from effect factory
582 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800583 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800584 if (mStatus != NO_ERROR) {
585 return;
586 }
587 lStatus = init();
588 if (lStatus < 0) {
589 mStatus = lStatus;
590 goto Error;
591 }
592
593 setOffloaded(callback->isOffload(), callback->io());
594 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
595
596 return;
597Error:
598 mEffectInterface.clear();
599 ALOGV("Constructor Error %d", mStatus);
600}
601
602AudioFlinger::EffectModule::~EffectModule()
603{
604 ALOGV("Destructor %p", this);
605 if (mEffectInterface != 0) {
606 char uuidStr[64];
607 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
608 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
609 this, uuidStr);
610 release_l();
611 }
612
613}
614
Eric Laurentfa1e1232016-08-02 19:01:49 -0700615bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800616 Mutex::Autolock _l(mLock);
617
Eric Laurentfa1e1232016-08-02 19:01:49 -0700618 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 switch (mState) {
620 case RESTART:
621 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700622 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800623
624 case STARTING:
625 // clear auxiliary effect input buffer for next accumulation
626 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
627 memset(mConfig.inputCfg.buffer.raw,
628 0,
629 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
630 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 if (start_l() == NO_ERROR) {
632 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700633 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700634 } else {
635 mState = IDLE;
636 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800637 break;
638 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900639 // volume control for offload and direct threads must take effect immediately.
640 if (stop_l() == NO_ERROR
641 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700642 mDisableWaitCnt = mMaxDisableWaitCnt;
643 } else {
644 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
645 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 mState = STOPPED;
647 break;
648 case STOPPED:
649 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
650 // turn off sequence.
651 if (--mDisableWaitCnt == 0) {
652 reset_l();
653 mState = IDLE;
654 }
655 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200656 case ACTIVE:
657 for (size_t i = 0; i < mHandles.size(); i++) {
658 if (!mHandles[i]->disconnected()) {
659 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
660 }
661 }
662 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800663 default: //IDLE , ACTIVE, DESTROYED
664 break;
665 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700666
667 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800668}
669
670void AudioFlinger::EffectModule::process()
671{
672 Mutex::Autolock _l(mLock);
673
Mikhail Naganov022b9952017-01-04 16:36:51 -0800674 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 return;
676 }
677
rago94a1ee82017-07-21 15:11:02 -0700678 const uint32_t inChannelCount =
679 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
680 const uint32_t outChannelCount =
681 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
682 const bool auxType =
683 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
684
Andy Hungfa69ca32017-11-30 10:07:53 -0800685 // safeInputOutputSampleCount is 0 if the channel count between input and output
686 // buffers do not match. This prevents automatic accumulation or copying between the
687 // input and output effect buffers without an intermediary effect process.
688 // TODO: consider implementing channel conversion.
689 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700690 mInChannelCountRequested != mOutChannelCountRequested ? 0
691 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800692 mConfig.inputCfg.buffer.frameCount,
693 mConfig.outputCfg.buffer.frameCount);
694 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800695 accumulate_float(
696 mConfig.outputCfg.buffer.f32,
697 mConfig.inputCfg.buffer.f32,
698 safeInputOutputSampleCount);
Andy Hungfa69ca32017-11-30 10:07:53 -0800699 };
700 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
Andy Hungfa69ca32017-11-30 10:07:53 -0800701 memcpy(
702 mConfig.outputCfg.buffer.f32,
703 mConfig.inputCfg.buffer.f32,
704 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
Andy Hungfa69ca32017-11-30 10:07:53 -0800705 };
706
Eric Laurentca7cc822012-11-19 14:55:58 -0800707 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700708 int ret;
709 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700710 if (auxType) {
711 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800712 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700713
Andy Hung26836922023-05-22 17:31:57 -0700714 if (!mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800715 memcpy_to_i16_from_float(
716 mConfig.inputCfg.buffer.s16,
717 mConfig.inputCfg.buffer.f32,
718 mConfig.inputCfg.buffer.frameCount);
rago94a1ee82017-07-21 15:11:02 -0700719 }
rago94a1ee82017-07-21 15:11:02 -0700720 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800721 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
722 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
723
724 if (!auxType && mInChannelCountRequested != inChannelCount) {
725 adjust_channels(
726 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
727 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
728 sizeof(float),
729 sizeof(float)
730 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
731 inBuffer = mInConversionBuffer;
732 }
733 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
734 && mOutChannelCountRequested != outChannelCount) {
735 adjust_selected_channels(
736 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
737 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
738 sizeof(float),
739 sizeof(float)
740 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
741 outBuffer = mOutConversionBuffer;
742 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800743 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
744 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800745 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800746 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
747 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700748 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800749 memcpy_to_i16_from_float(
750 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800751 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800752 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800753 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700754 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800755 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800756 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800757 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
758 goto data_bypass;
759 }
760 memcpy_to_i16_from_float(
761 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800762 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800763 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800764 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700765 }
766 }
Mikhail Naganov022b9952017-01-04 16:36:51 -0800767 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800768 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800769 sp<EffectBufferHalInterface> target =
770 mOutChannelCountRequested != outChannelCount
771 ? mOutConversionBuffer : mOutBuffer;
772
Andy Hungfa69ca32017-11-30 10:07:53 -0800773 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800774 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800775 mOutConversionBuffer->audioBuffer()->s16,
776 outChannelCount * mConfig.outputCfg.buffer.frameCount);
777 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800778 if (mOutChannelCountRequested != outChannelCount) {
779 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
780 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
781 sizeof(float),
782 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
783 }
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700784 } else {
rago94a1ee82017-07-21 15:11:02 -0700785 data_bypass:
rago94a1ee82017-07-21 15:11:02 -0700786 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700788 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700790 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700792 }
793 }
794 ret = -ENODATA;
795 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800796
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 // force transition to IDLE state when engine is ready
798 if (mState == STOPPED && ret == -ENODATA) {
799 mDisableWaitCnt = 1;
800 }
801
802 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700803 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800804 const size_t size =
805 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
rago94a1ee82017-07-21 15:11:02 -0700806 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800807 }
808 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700809 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
811 // If an insert effect is idle and input buffer is different from output buffer,
812 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700813 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700814 // similar handling with data_bypass above.
815 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
816 accumulateInputToOutput();
817 } else { // EFFECT_BUFFER_ACCESS_WRITE
818 copyInputToOutput();
819 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800820 }
821 }
822}
823
824void AudioFlinger::EffectModule::reset_l()
825{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700826 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800827 return;
828 }
Shunkai Yaoc92f3df2023-09-20 23:10:22 +0000829
830 int reply = 0;
831 uint32_t replySize = sizeof(reply);
832 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, &replySize, &reply);
Eric Laurentca7cc822012-11-19 14:55:58 -0800833}
834
835status_t AudioFlinger::EffectModule::configure()
836{
rago94a1ee82017-07-21 15:11:02 -0700837 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700838 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700839 uint32_t size;
840 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700841 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700842
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700843 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700844 status = NO_INIT;
845 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 }
847
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800849 // TODO: handle configuration of input (record) SW effects above the HAL,
850 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
851 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700852 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200853 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200855 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800856
857 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800858 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
859 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
860 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
861 mConfig.inputCfg.channels);
862 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800863 }
jiabineb3bda02020-06-30 14:07:03 -0700864 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700865 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700866 mConfig.inputCfg.channels |= hapticChannelMask;
867 mConfig.outputCfg.channels |= hapticChannelMask;
868 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800869 mInChannelCountRequested =
870 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
871 mOutChannelCountRequested =
872 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700873
Andy Hung319587b2023-05-23 14:01:03 -0700874 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
875 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900876
877 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700878 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900879 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
880 ALOGV("Overriding effect input as 48kHz");
881 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700882 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900883 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
885 mConfig.inputCfg.bufferProvider.cookie = NULL;
886 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
887 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
888 mConfig.outputCfg.bufferProvider.cookie = NULL;
889 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
890 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
891 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
892 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800893 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 // always overwrites output buffer: input buffer == output buffer
895 // - in other sessions:
896 // last effect in the chain accumulates in output buffer: input buffer != output buffer
897 // other effect: overwrites output buffer: input buffer == output buffer
898 // Auxiliary effect:
899 // accumulates in output buffer: input buffer != output buffer
900 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700901 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800902 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
903 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700904 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800905 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov59984db2022-04-19 21:21:23 +0000906 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800907
Eric Laurent6b446ce2019-12-13 10:56:31 -0800908 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700909 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800910 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800911
912 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700913 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700914 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800915 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700916 &mConfig,
917 &size,
918 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700919 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800920 status = cmdStatus;
921 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800922
Andy Hung9aad48c2017-11-29 10:29:19 -0800923 if (status != NO_ERROR &&
Andy Hungba8e52b2023-05-11 14:33:03 -0700924 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
Mikhail Naganov59984db2022-04-19 21:21:23 +0000925 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800926 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
927 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
928 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700929 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
930 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
932 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
933 }
934 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
935 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
936 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
937 }
938 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700939 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800940 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700941 &mConfig,
942 &size,
943 &cmdStatus);
944 if (status == NO_ERROR) {
945 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800946 }
947 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800948
Andy Hung9aad48c2017-11-29 10:29:19 -0800949 if (status == NO_ERROR) {
950 mSupportsFloat = true;
951 }
952
Andy Hungba8e52b2023-05-11 14:33:03 -0700953 // only HIDL effects support integer conversion.
954 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800955 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
956 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
957 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
958 size = sizeof(int);
959 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
960 sizeof(mConfig),
961 &mConfig,
962 &size,
963 &cmdStatus);
964 if (status == NO_ERROR) {
965 status = cmdStatus;
966 }
967 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700968 mSupportsFloat = false;
969 ALOGVV("config worked with 16 bit");
970 } else {
971 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800972 }
rago94a1ee82017-07-21 15:11:02 -0700973 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800974
rago94a1ee82017-07-21 15:11:02 -0700975 if (status == NO_ERROR) {
976 // Establish Buffer strategy
977 setInBuffer(mInBuffer);
978 setOutBuffer(mOutBuffer);
979
980 // Update visualizer latency
981 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
982 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
983 effect_param_t *p = (effect_param_t *)buf32;
984
985 p->psize = sizeof(uint32_t);
986 p->vsize = sizeof(uint32_t);
987 size = sizeof(int);
988 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
989
Andy Hungfda44002021-06-03 17:23:16 -0700990 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -0700991
992 *((int32_t *)p->data + 1)= latency;
993 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
994 sizeof(effect_param_t) + 8,
995 &buf32,
996 &size,
997 &cmdStatus);
998 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800999 }
1000
Andy Hung05083ac2017-12-14 15:00:28 -08001001 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1002 mMaxDisableWaitCnt = (uint32_t)std::max(
1003 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1004 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1005 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001006
Eric Laurentd0ebb532013-04-02 16:41:41 -07001007exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001008 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001009 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001010 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001011 return status;
1012}
1013
1014status_t AudioFlinger::EffectModule::init()
1015{
1016 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001017 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 return NO_INIT;
1019 }
1020 status_t cmdStatus;
1021 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001022 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1023 0,
1024 NULL,
1025 &size,
1026 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 if (status == 0) {
1028 status = cmdStatus;
1029 }
1030 return status;
1031}
1032
Eric Laurent1b928682014-10-02 19:41:47 -07001033void AudioFlinger::EffectModule::addEffectToHal_l()
1034{
1035 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1036 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001037 if (mCurrentHalStream == getCallback()->io()) {
David Li6c8ac4b2021-06-22 22:17:52 +08001038 return;
1039 }
1040
Andy Hungfda44002021-06-03 17:23:16 -07001041 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001042 mCurrentHalStream = getCallback()->io();
Eric Laurent1b928682014-10-02 19:41:47 -07001043 }
1044}
1045
Eric Laurentfa1e1232016-08-02 19:01:49 -07001046// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001047status_t AudioFlinger::EffectModule::start()
1048{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001049 status_t status;
1050 {
1051 Mutex::Autolock _l(mLock);
1052 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001053 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001054 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001055 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001056 }
1057 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001058}
1059
1060status_t AudioFlinger::EffectModule::start_l()
1061{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001062 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001063 return NO_INIT;
1064 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065 if (mStatus != NO_ERROR) {
1066 return mStatus;
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 status_t cmdStatus;
1069 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001070 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1071 0,
1072 NULL,
1073 &size,
1074 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 if (status == 0) {
1076 status = cmdStatus;
1077 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001078 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001079 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 }
1081 return status;
1082}
1083
1084status_t AudioFlinger::EffectModule::stop()
1085{
1086 Mutex::Autolock _l(mLock);
1087 return stop_l();
1088}
1089
1090status_t AudioFlinger::EffectModule::stop_l()
1091{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001092 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 return NO_INIT;
1094 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001095 if (mStatus != NO_ERROR) {
1096 return mStatus;
1097 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001098 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001100
1101 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001102 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1103 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1104 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001105 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001106 mSetVolumeReentrantTid = INVALID_PID;
1107 }
1108
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001109 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1110 0,
1111 NULL,
1112 &size,
1113 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001114 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001115 status = cmdStatus;
1116 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001117 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001118 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001119 }
1120 return status;
1121}
1122
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001123// must be called with EffectChain::mLock held
1124void AudioFlinger::EffectModule::release_l()
1125{
1126 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001127 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001128 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001129 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001130 mEffectInterface.clear();
1131 }
1132}
1133
Eric Laurent6b446ce2019-12-13 10:56:31 -08001134status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001135{
1136 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1137 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001138 if (mCurrentHalStream != getCallback()->io()) {
1139 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
David Li6c8ac4b2021-06-22 22:17:52 +08001140 }
Andy Hungfda44002021-06-03 17:23:16 -07001141 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001142 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001143 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001144 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001145}
1146
Andy Hunge4a1d912016-08-17 14:11:13 -07001147// round up delta valid if value and divisor are positive.
1148template <typename T>
1149static T roundUpDelta(const T &value, const T &divisor) {
1150 T remainder = value % divisor;
1151 return remainder == 0 ? 0 : divisor - remainder;
1152}
1153
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001154status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1155 const std::vector<uint8_t>& cmdData,
1156 int32_t maxReplySize,
1157 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001158{
1159 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001160 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001161
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001162 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001163 return NO_INIT;
1164 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001165 if (mStatus != NO_ERROR) {
1166 return mStatus;
1167 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001168 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1169 return -EINVAL;
1170 }
1171 size_t cmdSize = cmdData.size();
1172 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1173 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1174 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001175 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001176 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001177 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001178 android_errorWriteLog(0x534e4554, "33003822");
1179 return -EINVAL;
1180 }
1181 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001182 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001183 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001184 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001185 return -EINVAL;
1186 }
ragoe2759072016-11-22 18:02:48 -08001187 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001188 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001189 || param->psize > maxReplySize - sizeof(effect_param_t)
1190 || param->vsize > maxReplySize - sizeof(effect_param_t)
1191 - param->psize
1192 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1193 maxReplySize
1194 - sizeof(effect_param_t)
1195 - param->psize
1196 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001197 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1198 android_errorWriteLog(0x534e4554, "32705438");
1199 return -EINVAL;
1200 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001201 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001202 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1203 && // DEFERRED not generally used
1204 (param == nullptr
1205 || param->psize > cmdSize - sizeof(effect_param_t)
1206 || param->vsize > cmdSize - sizeof(effect_param_t)
1207 - param->psize
1208 || roundUpDelta(param->psize,
1209 (uint32_t) sizeof(int)) >
1210 cmdSize
1211 - sizeof(effect_param_t)
1212 - param->psize
1213 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001214 android_errorWriteLog(0x534e4554, "30204301");
1215 return -EINVAL;
1216 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001217 uint32_t replySize = maxReplySize;
1218 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001219 status_t status = mEffectInterface->command(cmdCode,
1220 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001221 const_cast<uint8_t*>(cmdData.data()),
1222 &replySize,
1223 reply->data());
1224 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001225 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001226 for (size_t i = 1; i < mHandles.size(); i++) {
1227 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001228 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001229 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001230 }
1231 }
1232 }
1233 return status;
1234}
1235
Eric Laurentca7cc822012-11-19 14:55:58 -08001236bool AudioFlinger::EffectModule::isProcessEnabled() const
1237{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001238 if (mStatus != NO_ERROR) {
1239 return false;
1240 }
1241
Eric Laurentca7cc822012-11-19 14:55:58 -08001242 switch (mState) {
1243 case RESTART:
1244 case ACTIVE:
1245 case STOPPING:
1246 case STOPPED:
1247 return true;
1248 case IDLE:
1249 case STARTING:
1250 case DESTROYED:
1251 default:
1252 return false;
1253 }
1254}
1255
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001256bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1257{
Andy Hungfda44002021-06-03 17:23:16 -07001258 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001259}
1260
1261bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1262{
1263 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1264}
1265
Mikhail Naganov022b9952017-01-04 16:36:51 -08001266void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001267 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001268
1269 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001270 if (buffer != 0) {
1271 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1272 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1273 } else {
1274 mConfig.inputCfg.buffer.raw = NULL;
1275 }
1276 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001277 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001278
Andy Hungbded9c82017-11-30 18:47:35 -08001279 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001280 // Theoretically insert effects can also do in-place conversions (destroying
1281 // the original buffer) when the output buffer is identical to the input buffer,
1282 // but we don't optimize for it here.
1283 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001284 const uint32_t inChannelCount =
1285 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1286 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001287 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001288 // we need to translate - create hidl shared buffer and intercept
1289 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001290 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1291 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1292 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001293
1294 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1295 __func__, inChannels, inFrameCount, size);
1296
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001297 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001298 || size > mInConversionBuffer->getSize())) {
1299 mInConversionBuffer.clear();
1300 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001301 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001302 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001303 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001304 mInConversionBuffer->setFrameCount(inFrameCount);
1305 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001306 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001307 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001308 }
1309 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001310}
1311
1312void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001313 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001314
1315 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001316 if (buffer != 0) {
1317 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1318 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1319 } else {
1320 mConfig.outputCfg.buffer.raw = NULL;
1321 }
1322 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001323 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001324
Andy Hungbded9c82017-11-30 18:47:35 -08001325 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001326 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001327 const uint32_t outChannelCount =
1328 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1329 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001330 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001331 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001332 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1333 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1334 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001335
1336 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1337 __func__, outChannels, outFrameCount, size);
1338
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001339 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001340 || size > mOutConversionBuffer->getSize())) {
1341 mOutConversionBuffer.clear();
1342 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001343 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001344 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001345 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001346 mOutConversionBuffer->setFrameCount(outFrameCount);
1347 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001348 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001349 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001350 }
1351 }
Mikhail Naganov022b9952017-01-04 16:36:51 -08001352}
1353
Eric Laurentca7cc822012-11-19 14:55:58 -08001354status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1355{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001356 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001357 if (mStatus != NO_ERROR) {
1358 return mStatus;
1359 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001360 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001361 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1362 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1363 if (isProcessEnabled() &&
1364 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001365 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1366 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001367 status = setVolumeInternal(left, right, controller);
1368 }
1369 return status;
1370}
1371
1372status_t AudioFlinger::EffectModule::setVolumeInternal(
1373 uint32_t *left, uint32_t *right, bool controller) {
1374 uint32_t volume[2] = {*left, *right};
1375 uint32_t *pVolume = controller ? volume : nullptr;
1376 uint32_t size = sizeof(volume);
1377 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1378 size,
1379 volume,
1380 &size,
1381 pVolume);
1382 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1383 *left = volume[0];
1384 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001385 }
1386 return status;
1387}
1388
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001389void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1390{
Zhou Songd505c642020-02-20 16:35:37 +08001391 // for offload or direct thread, if the effect chain has non-offloadable
1392 // effect and any effect module within the chain has volume control, then
1393 // volume control is delegated to effect, otherwise, set volume to hal.
1394 if (mEffectCallback->isOffloadOrDirect() &&
1395 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001396 float vol_l = (float)left / (1 << 24);
1397 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001398 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001399 }
1400}
1401
jiabin8f278ee2019-11-11 12:16:27 -08001402status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1403 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001404{
jiabin8f278ee2019-11-11 12:16:27 -08001405 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1406 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 return NO_ERROR;
1408 }
1409
1410 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001411 if (mStatus != NO_ERROR) {
1412 return mStatus;
1413 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001415 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001416 status_t cmdStatus;
1417 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001418 // FIXME: use audio device types and addresses when the hal interface is ready.
1419 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001420 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001421 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001422 &size,
1423 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 }
1425 return status;
1426}
1427
jiabin8f278ee2019-11-11 12:16:27 -08001428status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1429{
1430 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1431}
1432
1433status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1434{
1435 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1436}
1437
Eric Laurentca7cc822012-11-19 14:55:58 -08001438status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1439{
1440 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001441 if (mStatus != NO_ERROR) {
1442 return mStatus;
1443 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 status_t status = NO_ERROR;
1445 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1446 status_t cmdStatus;
1447 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001448 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1449 sizeof(audio_mode_t),
1450 &mode,
1451 &size,
1452 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 if (status == NO_ERROR) {
1454 status = cmdStatus;
1455 }
1456 }
1457 return status;
1458}
1459
1460status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1461{
1462 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001463 if (mStatus != NO_ERROR) {
1464 return mStatus;
1465 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001466 status_t status = NO_ERROR;
1467 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1468 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001469 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1470 sizeof(audio_source_t),
1471 &source,
1472 &size,
1473 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 }
1475 return status;
1476}
1477
Eric Laurent5baf2af2013-09-12 17:37:00 -07001478status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1479{
1480 Mutex::Autolock _l(mLock);
1481 if (mStatus != NO_ERROR) {
1482 return mStatus;
1483 }
1484 status_t status = NO_ERROR;
1485 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1486 status_t cmdStatus;
1487 uint32_t size = sizeof(status_t);
1488 effect_offload_param_t cmd;
1489
1490 cmd.isOffload = offloaded;
1491 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001492 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1493 sizeof(effect_offload_param_t),
1494 &cmd,
1495 &size,
1496 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001497 if (status == NO_ERROR) {
1498 status = cmdStatus;
1499 }
1500 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1501 } else {
1502 if (offloaded) {
1503 status = INVALID_OPERATION;
1504 }
1505 mOffloaded = false;
1506 }
1507 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1508 return status;
1509}
1510
1511bool AudioFlinger::EffectModule::isOffloaded() const
1512{
1513 Mutex::Autolock _l(mLock);
1514 return mOffloaded;
1515}
1516
jiabineb3bda02020-06-30 14:07:03 -07001517/*static*/
1518bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1519 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1520}
1521
1522bool AudioFlinger::EffectModule::isHapticGenerator() const {
1523 return isHapticGenerator(&mDescriptor.type);
1524}
1525
Simon Bowden62823412022-10-17 14:52:26 +00001526status_t AudioFlinger::EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001527{
1528 if (mStatus != NO_ERROR) {
1529 return mStatus;
1530 }
1531 if (!isHapticGenerator()) {
1532 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1533 return INVALID_OPERATION;
1534 }
1535
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001536 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1537 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001538 param->psize = sizeof(int32_t);
1539 param->vsize = sizeof(int32_t) * 2;
1540 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1541 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001542 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001543 std::vector<uint8_t> response;
1544 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001545 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001546 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1547 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001548 }
1549 return status;
1550}
1551
Lais Andradebc3f37a2021-07-02 00:13:19 +01001552status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001553{
1554 if (mStatus != NO_ERROR) {
1555 return mStatus;
1556 }
1557 if (!isHapticGenerator()) {
1558 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1559 return INVALID_OPERATION;
1560 }
1561
Lais Andradebc3f37a2021-07-02 00:13:19 +01001562 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001563 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001564 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001565 effect_param_t *param = (effect_param_t*) request.data();
1566 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001567 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001568 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1569 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001570 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1571 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1572 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001573 std::vector<uint8_t> response;
1574 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1575 if (status == NO_ERROR) {
1576 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1577 status = *reinterpret_cast<const status_t*>(response.data());
1578 }
1579 return status;
1580}
1581
Mikhail Naganov59984db2022-04-19 21:21:23 +00001582status_t AudioFlinger::EffectModule::getConfigs(
1583 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1584 Mutex::Autolock _l(mLock);
1585 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1586 return NO_INIT;
1587 }
1588 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1589 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1590 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1591 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1592 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1593 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1594 *isOutput = mIsOutput;
1595 return NO_ERROR;
1596}
1597
Andy Hungbded9c82017-11-30 18:47:35 -08001598static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1599 std::stringstream ss;
1600
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001601 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001602 return "nullptr"; // make different than below
1603 } else if (buffer->externalData() != nullptr) {
1604 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1605 << " -> "
1606 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1607 } else {
1608 ss << buffer->audioBuffer()->raw;
1609 }
1610 return ss.str();
1611}
Marco Nelissenb2208842014-02-07 14:00:50 -08001612
Eric Laurent41709552019-12-16 19:34:05 -08001613void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07001614NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001615{
Eric Laurent41709552019-12-16 19:34:05 -08001616 EffectBase::dump(fd, args);
1617
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001620
Eric Laurent41709552019-12-16 19:34:05 -08001621 result.append("\t\tStatus Engine:\n");
1622 result.appendFormat("\t\t%03d %p\n",
1623 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001624
1625 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001626
1627 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001628 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1629 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1630 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 mConfig.inputCfg.buffer.frameCount,
1632 mConfig.inputCfg.samplingRate,
1633 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001634 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001635 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001636
1637 result.append("\t\t- Output configuration:\n");
1638 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001639 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001640 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 mConfig.outputCfg.buffer.frameCount,
1642 mConfig.outputCfg.samplingRate,
1643 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001644 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001645 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001646
Andy Hungbded9c82017-11-30 18:47:35 -08001647 result.appendFormat("\t\t- HAL buffers:\n"
1648 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1649 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1650 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1651 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1652 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001653
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00001654 write(fd, result.c_str(), result.length());
Eric Laurentca7cc822012-11-19 14:55:58 -08001655
Mikhail Naganov4d547672019-02-22 14:19:19 -08001656 if (mEffectInterface != 0) {
1657 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1658 (void)mEffectInterface->dump(fd);
1659 }
1660
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 if (locked) {
1662 mLock.unlock();
1663 }
1664}
1665
1666// ----------------------------------------------------------------------------
1667// EffectHandle implementation
1668// ----------------------------------------------------------------------------
1669
1670#undef LOG_TAG
1671#define LOG_TAG "AudioFlinger::EffectHandle"
1672
Eric Laurent41709552019-12-16 19:34:05 -08001673AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001674 const sp<AudioFlinger::Client>& client,
1675 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001676 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001677 : BnEffect(),
1678 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001679 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1680 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001681{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001682 ALOGV("constructor %p client %p", this, client.get());
Andy Hung393de3a2022-12-06 16:33:20 -08001683 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001684
1685 if (client == 0) {
1686 return;
1687 }
1688 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001689 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1690 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1691 std::string("Effect ID: ")
1692 .append(std::to_string(effect->id()))
1693 .append(" Session ID: ")
1694 .append(std::to_string(static_cast<int>(effect->sessionId())))
1695 .append(" \n")
1696 });
Glenn Kastene75da402013-11-20 13:54:52 -08001697 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001698 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001699 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001700 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001701 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 return;
1703 }
Glenn Kastene75da402013-11-20 13:54:52 -08001704 new(mCblk) effect_param_cblk_t();
1705 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001706}
1707
1708AudioFlinger::EffectHandle::~EffectHandle()
1709{
1710 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 disconnect(false);
1712}
1713
Andy Hungc747c532022-03-07 21:41:14 -08001714// Creates an association between Binder code to name for IEffect.
1715#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1716BINDER_METHOD_ENTRY(enable) \
1717BINDER_METHOD_ENTRY(disable) \
1718BINDER_METHOD_ENTRY(command) \
1719BINDER_METHOD_ENTRY(disconnect) \
1720BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001721BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001722
1723// singleton for Binder Method Statistics for IEffect
1724mediautils::MethodStatistics<int>& getIEffectStatistics() {
1725 using Code = int;
1726
1727#pragma push_macro("BINDER_METHOD_ENTRY")
1728#undef BINDER_METHOD_ENTRY
1729#define BINDER_METHOD_ENTRY(ENTRY) \
1730 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1731
1732 static mediautils::MethodStatistics<Code> methodStatistics{
1733 IEFFECT_BINDER_METHOD_MACRO_LIST
1734 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1735 };
1736#pragma pop_macro("BINDER_METHOD_ENTRY")
1737
1738 return methodStatistics;
1739}
1740
1741status_t AudioFlinger::EffectHandle::onTransact(
1742 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001743 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1744 mediautils::TimeCheck check(
1745 std::string("IEffect::").append(methodName),
1746 [code](bool timeout, float elapsedMs) {
1747 if (timeout) {
1748 ; // we don't timeout right now on the effect interface.
1749 } else {
1750 getIEffectStatistics().event(code, elapsedMs);
1751 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001752 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001753 return BnEffect::onTransact(code, data, reply, flags);
1754}
1755
Glenn Kastene75da402013-11-20 13:54:52 -08001756status_t AudioFlinger::EffectHandle::initCheck()
1757{
1758 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1759}
1760
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001761#define RETURN(code) \
1762 *_aidl_return = (code); \
1763 return Status::ok();
1764
Mikhail Naganov59984db2022-04-19 21:21:23 +00001765#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1766 ({ \
1767 auto _tmp = (exp); \
1768 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1769 std::move(_tmp.value()); \
1770 })
1771
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001773{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001776 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001777 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001778 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001779 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001781 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001783
1784 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001785 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 }
1787
1788 mEnabled = true;
1789
Eric Laurent6c796322019-04-09 14:13:17 -07001790 status_t status = effect->updatePolicyState();
1791 if (status != NO_ERROR) {
1792 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001793 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001794 }
1795
Eric Laurent6b446ce2019-12-13 10:56:31 -08001796 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797
1798 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001799 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001800 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001801 }
1802
Eric Laurent6b446ce2019-12-13 10:56:31 -08001803 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001804 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001805 mEnabled = false;
1806 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001807 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001808}
1809
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001810Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001811{
1812 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001813 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001814 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001815 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001816 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001817 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001819 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001821
1822 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001823 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001824 }
1825 mEnabled = false;
1826
Eric Laurent6c796322019-04-09 14:13:17 -07001827 effect->updatePolicyState();
1828
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001829 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 }
1832
Eric Laurent6b446ce2019-12-13 10:56:31 -08001833 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001834 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001835}
1836
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001837Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001838{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001839 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001842}
1843
1844void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1845{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001846 AutoMutex _l(mLock);
1847 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1848 if (mDisconnected) {
1849 if (unpinIfLast) {
1850 android_errorWriteLog(0x534e4554, "32707507");
1851 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 return;
1853 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 {
Eric Laurent41709552019-12-16 19:34:05 -08001856 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001858 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001859 ALOGW("%s Effect handle %p disconnected after thread destruction",
1860 __func__, this);
1861 }
1862 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001863 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001864 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001865
Eric Laurentca7cc822012-11-19 14:55:58 -08001866 if (mClient != 0) {
1867 if (mCblk != NULL) {
1868 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1869 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1870 }
1871 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001872 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung71ba4b32022-10-06 12:09:49 -07001873 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 mClient.clear();
1875 }
1876}
1877
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001878Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1879 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1880 return Status::ok();
1881}
1882
Mikhail Naganov59984db2022-04-19 21:21:23 +00001883Status AudioFlinger::EffectHandle::getConfig(
1884 media::EffectConfig* _config, int32_t* _aidl_return) {
1885 AutoMutex _l(mLock);
1886 sp<EffectBase> effect = mEffect.promote();
1887 if (effect == nullptr || mDisconnected) {
1888 RETURN(DEAD_OBJECT);
1889 }
1890 sp<EffectModule> effectModule = effect->asEffectModule();
1891 if (effectModule == nullptr) {
1892 RETURN(INVALID_OPERATION);
1893 }
1894 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1895 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1896 bool isOutput;
1897 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1898 if (status == NO_ERROR) {
1899 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1900 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1901 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1902 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1903 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1904 _config->isOnInputStream = !isOutput;
1905 }
1906 RETURN(status);
1907}
1908
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1910 const std::vector<uint8_t>& cmdData,
1911 int32_t maxResponseSize,
1912 std::vector<uint8_t>* response,
1913 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001914{
1915 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001916 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001917
Eric Laurentc7ab3092017-06-15 18:43:46 -07001918 // reject commands reserved for internal use by audio framework if coming from outside
1919 // of audioserver
1920 switch(cmdCode) {
1921 case EFFECT_CMD_ENABLE:
1922 case EFFECT_CMD_DISABLE:
1923 case EFFECT_CMD_SET_PARAM:
1924 case EFFECT_CMD_SET_PARAM_DEFERRED:
1925 case EFFECT_CMD_SET_PARAM_COMMIT:
1926 case EFFECT_CMD_GET_PARAM:
1927 break;
1928 default:
1929 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1930 break;
1931 }
1932 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001933 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001934 }
1935
Eric Laurent1ffc5852016-12-15 14:46:09 -08001936 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001937 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001938 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001939 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001940 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001941 writeToBuffer(NO_ERROR, response);
1942 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001943 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung71ba4b32022-10-06 12:09:49 -07001944 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001945 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001946 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001947 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001948 writeToBuffer(NO_ERROR, response);
1949 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001950 }
1951
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001952 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001953 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001954 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001955 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001956 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001957 // only get parameter command is permitted for applications not controlling the effect
1958 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001959 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001960 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001961
1962 // handle commands that are not forwarded transparently to effect engine
1963 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001964 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001965 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001966 }
1967
Andy Hung71ba4b32022-10-06 12:09:49 -07001968 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001969 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001970 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001971 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001972 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001973
Eric Laurentca7cc822012-11-19 14:55:58 -08001974 // No need to trylock() here as this function is executed in the binder thread serving a
1975 // particular client process: no risk to block the whole media server process or mixer
1976 // threads if we are stuck here
Andy Hung71ba4b32022-10-06 12:09:49 -07001977 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001978 // keep local copy of index in case of client corruption b/32220769
1979 const uint32_t clientIndex = mCblk->clientIndex;
1980 const uint32_t serverIndex = mCblk->serverIndex;
1981 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1982 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001983 mCblk->serverIndex = 0;
1984 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001985 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 }
1987 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001988 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001989 for (uint32_t index = serverIndex; index < clientIndex;) {
1990 int *p = (int *)(mBuffer + index);
1991 const int size = *p++;
1992 if (size < 0
1993 || size > EFFECT_PARAM_BUFFER_SIZE
1994 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001995 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001996 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001997 break;
1998 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001999
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002000 std::copy(reinterpret_cast<const uint8_t*>(p),
2001 reinterpret_cast<const uint8_t*>(p) + size,
2002 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002003
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002004 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002005 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002006 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002007 sizeof(int),
2008 &replyBuffer);
2009 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002010
2011 // verify shared memory: server index shouldn't change; client index can't go back.
2012 if (serverIndex != mCblk->serverIndex
2013 || clientIndex > mCblk->clientIndex) {
2014 android_errorWriteLog(0x534e4554, "32220769");
2015 status = BAD_VALUE;
2016 break;
2017 }
2018
Eric Laurentca7cc822012-11-19 14:55:58 -08002019 // stop at first error encountered
2020 if (ret != NO_ERROR) {
2021 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002022 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002023 break;
2024 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002025 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002026 break;
2027 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002028 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002029 }
2030 mCblk->serverIndex = 0;
2031 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002032 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002033 }
2034
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002035 status_t status = effect->command(cmdCode,
2036 cmdData,
2037 maxResponseSize,
2038 response);
2039 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002040}
2041
2042void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2043{
2044 ALOGV("setControl %p control %d", this, hasControl);
2045
2046 mHasControl = hasControl;
2047 mEnabled = enabled;
2048
2049 if (signal && mEffectClient != 0) {
2050 mEffectClient->controlStatusChanged(hasControl);
2051 }
2052}
2053
2054void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002055 const std::vector<uint8_t>& cmdData,
2056 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002057{
2058 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002059 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002060 }
2061}
2062
2063
2064
2065void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2066{
2067 if (mEffectClient != 0) {
2068 mEffectClient->enableStatusChanged(enabled);
2069 }
2070}
2071
Eric Laurentde8caf42021-08-11 17:19:25 +02002072void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2073{
2074 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2075 mEffectClient->framesProcessed(frames);
2076 }
2077}
2078
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002079void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung71ba4b32022-10-06 12:09:49 -07002080NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002081{
2082 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2083
Marco Nelissenb2208842014-02-07 14:00:50 -08002084 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002085 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002086 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002087 mHasControl ? "yes" : "no",
2088 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002089 mCblk ? mCblk->clientIndex : 0,
2090 mCblk ? mCblk->serverIndex : 0
2091 );
2092
2093 if (locked) {
2094 mCblk->lock.unlock();
2095 }
2096}
2097
2098#undef LOG_TAG
2099#define LOG_TAG "AudioFlinger::EffectChain"
2100
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002101AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2102 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002103 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
jiabineb5784e2023-08-24 21:10:44 +00002104 mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002105 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002106 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002107{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002108 sp<ThreadBase> p = thread.promote();
2109 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002110 return;
2111 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002112 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002113 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2114 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002115}
2116
2117AudioFlinger::EffectChain::~EffectChain()
2118{
Eric Laurentca7cc822012-11-19 14:55:58 -08002119}
2120
2121// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2122sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2123 effect_descriptor_t *descriptor)
2124{
2125 size_t size = mEffects.size();
2126
2127 for (size_t i = 0; i < size; i++) {
2128 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2129 return mEffects[i];
2130 }
2131 }
2132 return 0;
2133}
2134
2135// getEffectFromId_l() must be called with ThreadBase::mLock held
2136sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2137{
2138 size_t size = mEffects.size();
2139
2140 for (size_t i = 0; i < size; i++) {
2141 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2142 if (id == 0 || mEffects[i]->id() == id) {
2143 return mEffects[i];
2144 }
2145 }
2146 return 0;
2147}
2148
2149// getEffectFromType_l() must be called with ThreadBase::mLock held
2150sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2151 const effect_uuid_t *type)
2152{
2153 size_t size = mEffects.size();
2154
2155 for (size_t i = 0; i < size; i++) {
2156 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2157 return mEffects[i];
2158 }
2159 }
2160 return 0;
2161}
2162
Eric Laurent6c796322019-04-09 14:13:17 -07002163std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2164{
2165 std::vector<int> ids;
2166 Mutex::Autolock _l(mLock);
2167 for (size_t i = 0; i < mEffects.size(); i++) {
2168 ids.push_back(mEffects[i]->id());
2169 }
2170 return ids;
2171}
2172
Eric Laurentca7cc822012-11-19 14:55:58 -08002173void AudioFlinger::EffectChain::clearInputBuffer()
2174{
2175 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002176 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002177}
2178
2179// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002180void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002181{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002182 if (mInBuffer == NULL) {
2183 return;
2184 }
Andy Hung319587b2023-05-23 14:01:03 -07002185 const size_t frameSize = audio_bytes_per_sample(AUDIO_FORMAT_PCM_FLOAT)
Eric Laurentf1f22e72021-07-13 14:04:14 +02002186 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002187
Eric Laurent6b446ce2019-12-13 10:56:31 -08002188 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002189 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002190}
2191
2192// Must be called with EffectChain::mLock locked
2193void AudioFlinger::EffectChain::process_l()
2194{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002195 // never process effects when:
2196 // - on an OFFLOAD thread
2197 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002198 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002199 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002200 bool tracksOnSession = (trackCnt() != 0);
2201
2202 if (!tracksOnSession && mTailBufferCount == 0) {
2203 doProcess = false;
2204 }
2205
2206 if (activeTrackCnt() == 0) {
2207 // if no track is active and the effect tail has not been rendered,
2208 // the input buffer must be cleared here as the mixer process will not do it
2209 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002210 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002211 if (mTailBufferCount > 0) {
2212 mTailBufferCount--;
2213 }
2214 }
2215 }
2216 }
2217
2218 size_t size = mEffects.size();
2219 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002220 // Only the input and output buffers of the chain can be external,
2221 // and 'update' / 'commit' do nothing for allocated buffers, thus
2222 // it's not needed to consider any other buffers here.
2223 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002224 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2225 mOutBuffer->update();
2226 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002227 for (size_t i = 0; i < size; i++) {
2228 mEffects[i]->process();
2229 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002230 mInBuffer->commit();
2231 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2232 mOutBuffer->commit();
2233 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002234 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002235 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002236 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002237 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2238 }
2239 if (doResetVolume) {
2240 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002241 }
2242}
2243
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002244// createEffect_l() must be called with ThreadBase::mLock held
2245status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002246 effect_descriptor_t *desc,
2247 int id,
2248 audio_session_t sessionId,
2249 bool pinned)
2250{
2251 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002252 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002253 status_t lStatus = effect->status();
2254 if (lStatus == NO_ERROR) {
2255 lStatus = addEffect_ll(effect);
2256 }
2257 if (lStatus != NO_ERROR) {
2258 effect.clear();
2259 }
2260 return lStatus;
2261}
2262
2263// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002264status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2265{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002266 Mutex::Autolock _l(mLock);
2267 return addEffect_ll(effect);
2268}
2269// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2270status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2271{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002272 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002273
Eric Laurentb62d0362021-10-26 17:40:18 +02002274 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002275 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2276 // Auxiliary effects are inserted at the beginning of mEffects vector as
2277 // they are processed first and accumulated in chain input buffer
2278 mEffects.insertAt(effect, 0);
2279
2280 // the input buffer for auxiliary effect contains mono samples in
2281 // 32 bit format. This is to avoid saturation in AudoMixer
2282 // accumulation stage. Saturation is done in EffectModule::process() before
2283 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002284 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002285 sp<EffectBufferHalInterface> halBuffer;
Andy Hung26836922023-05-22 17:31:57 -07002286
Eric Laurent6b446ce2019-12-13 10:56:31 -08002287 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002288 numSamples * sizeof(float), &halBuffer);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002289 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002290
2291 effect->configure();
2292
Mikhail Naganov022b9952017-01-04 16:36:51 -08002293 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002294 // auxiliary effects output samples to chain input buffer for further processing
2295 // by insert effects
2296 effect->setOutBuffer(mInBuffer);
2297 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002298 ssize_t idx_insert = getInsertIndex(desc);
2299 if (idx_insert < 0) {
2300 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002301 }
2302
Eric Laurentb62d0362021-10-26 17:40:18 +02002303 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002304 mEffects.insertAt(effect, idx_insert);
2305
2306 effect->configure();
2307
Eric Laurentb62d0362021-10-26 17:40:18 +02002308 // - By default:
2309 // All effects read samples from chain input buffer.
2310 // The last effect in the chain, writes samples to chain output buffer,
2311 // otherwise to chain input buffer
2312 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2313 // The spatializer effect (first effect) reads samples from the input buffer
2314 // and writes samples to the output buffer.
2315 // All other effects read and writes samples to the output buffer
2316 if (mEffectCallback->isSpatializer()
2317 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002318 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002319 if (idx_insert == 0) {
2320 if (previousSize != 0) {
2321 mEffects[1]->configure();
2322 mEffects[1]->setInBuffer(mOutBuffer);
2323 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2324 }
2325 effect->setInBuffer(mInBuffer);
2326 } else {
2327 effect->setInBuffer(mOutBuffer);
2328 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002330 effect->setInBuffer(mInBuffer);
Andy Hung71ba4b32022-10-06 12:09:49 -07002331 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002332 if (idx_insert != 0) {
2333 mEffects[idx_insert-1]->configure();
2334 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2335 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2336 }
2337 effect->setOutBuffer(mOutBuffer);
2338 } else {
2339 effect->setOutBuffer(mInBuffer);
2340 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002341 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002342 ALOGV("%s effect %p, added in chain %p at rank %zu",
2343 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002344 }
2345 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002346
Eric Laurentca7cc822012-11-19 14:55:58 -08002347 return NO_ERROR;
2348}
2349
jiabineb5784e2023-08-24 21:10:44 +00002350std::optional<size_t> AudioFlinger::EffectChain::findVolumeControl_l(size_t from, size_t to) const {
2351 for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
2352 if (mEffects[i - 1]->isVolumeControlEnabled()) {
2353 return i - 1;
2354 }
2355 }
2356 return std::nullopt;
2357}
2358
Eric Laurentb62d0362021-10-26 17:40:18 +02002359ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2360 // Insert effects are inserted at the end of mEffects vector as they are processed
2361 // after track and auxiliary effects.
2362 // Insert effect order as a function of indicated preference:
2363 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2364 // another effect is present
2365 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2366 // last effect claiming first position
2367 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2368 // first effect claiming last position
2369 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2370 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2371 // already present
2372 // Spatializer or Downmixer effects are inserted in first position because
2373 // they adapt the channel count for all other effects in the chain
2374 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2375 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2376 return 0;
2377 }
2378
2379 size_t size = mEffects.size();
2380 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2381 ssize_t idx_insert;
2382 ssize_t idx_insert_first = -1;
2383 ssize_t idx_insert_last = -1;
2384
2385 idx_insert = size;
2386 for (size_t i = 0; i < size; i++) {
2387 effect_descriptor_t d = mEffects[i]->desc();
2388 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2389 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2390 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2391 // check invalid effect chaining combinations
2392 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2393 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2394 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2395 __func__, desc.name, d.name);
2396 return -1;
2397 }
2398 // remember position of first insert effect and by default
2399 // select this as insert position for new effect
Andy Hung71ba4b32022-10-06 12:09:49 -07002400 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002401 idx_insert = i;
2402 }
2403 // remember position of last insert effect claiming
2404 // first position
2405 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2406 idx_insert_first = i;
2407 }
2408 // remember position of first insert effect claiming
2409 // last position
2410 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2411 idx_insert_last == -1) {
2412 idx_insert_last = i;
2413 }
2414 }
2415 }
2416
2417 // modify idx_insert from first position if needed
2418 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2419 if (idx_insert_last != -1) {
2420 idx_insert = idx_insert_last;
2421 } else {
2422 idx_insert = size;
2423 }
2424 } else {
2425 if (idx_insert_first != -1) {
2426 idx_insert = idx_insert_first + 1;
2427 }
2428 }
2429 return idx_insert;
2430}
2431
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002432// removeEffect_l() must be called with ThreadBase::mLock held
2433size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2434 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002435{
2436 Mutex::Autolock _l(mLock);
2437 size_t size = mEffects.size();
2438 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2439
2440 for (size_t i = 0; i < size; i++) {
2441 if (effect == mEffects[i]) {
2442 // calling stop here will remove pre-processing effect from the audio HAL.
2443 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2444 // the middle of a read from audio HAL
2445 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2446 mEffects[i]->state() == EffectModule::STOPPING) {
2447 mEffects[i]->stop();
2448 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002449 if (release) {
2450 mEffects[i]->release_l();
2451 }
2452
Mikhail Naganov022b9952017-01-04 16:36:51 -08002453 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002454 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002455 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002456 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002457 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002458 }
2459 }
2460 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002461
2462 // make sure the input buffer configuration for the new first effect in the chain
2463 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
Nie Wei Feng4d2cb592023-05-26 15:18:04 -07002464 if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
2465 && i == 0 && size > 1) {
Eric Laurentf1f22e72021-07-13 14:04:14 +02002466 mEffects[0]->configure();
2467 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002468 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002469 }
2470
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002471 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002472 this, i);
2473 break;
2474 }
2475 }
2476
2477 return mEffects.size();
2478}
2479
jiabin8f278ee2019-11-11 12:16:27 -08002480// setDevices_l() must be called with ThreadBase::mLock held
2481void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002482{
2483 size_t size = mEffects.size();
2484 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002485 mEffects[i]->setDevices(devices);
2486 }
2487}
2488
2489// setInputDevice_l() must be called with ThreadBase::mLock held
2490void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2491{
2492 size_t size = mEffects.size();
2493 for (size_t i = 0; i < size; i++) {
2494 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002495 }
2496}
2497
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002498// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002499void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2500{
2501 size_t size = mEffects.size();
2502 for (size_t i = 0; i < size; i++) {
2503 mEffects[i]->setMode(mode);
2504 }
2505}
2506
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002507// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002508void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2509{
2510 size_t size = mEffects.size();
2511 for (size_t i = 0; i < size; i++) {
2512 mEffects[i]->setAudioSource(source);
2513 }
2514}
2515
Zhou Songd505c642020-02-20 16:35:37 +08002516bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2517 for (const auto &effect : mEffects) {
2518 if (effect->isVolumeControlEnabled()) return true;
2519 }
2520 return false;
2521}
2522
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002523// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002524bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002525{
2526 uint32_t newLeft = *left;
2527 uint32_t newRight = *right;
jiabineb5784e2023-08-24 21:10:44 +00002528 const size_t size = mEffects.size();
Eric Laurentca7cc822012-11-19 14:55:58 -08002529
2530 // first update volume controller
jiabineb5784e2023-08-24 21:10:44 +00002531 const auto volumeControlIndex = findVolumeControl_l(0, size);
2532 const int ctrlIdx = volumeControlIndex.value_or(-1);
2533 const sp<EffectModule> volumeControlEffect =
2534 volumeControlIndex.has_value() ? mEffects[ctrlIdx] : nullptr;
2535 const sp<EffectModule> cachedVolumeControlEffect = mVolumeControlEffect.promote();
Eric Laurentca7cc822012-11-19 14:55:58 -08002536
jiabineb5784e2023-08-24 21:10:44 +00002537 if (!force && volumeControlEffect == cachedVolumeControlEffect &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002538 *left == mLeftVolume && *right == mRightVolume) {
jiabineb5784e2023-08-24 21:10:44 +00002539 if (volumeControlIndex.has_value()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002540 *left = mNewLeftVolume;
2541 *right = mNewRightVolume;
2542 }
jiabineb5784e2023-08-24 21:10:44 +00002543 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002544 }
2545
jiabineb5784e2023-08-24 21:10:44 +00002546 if (volumeControlEffect != cachedVolumeControlEffect) {
2547 // The volume control effect is a new one. Set the old one as full volume. Set the new onw
2548 // as zero for safe ramping.
2549 if (cachedVolumeControlEffect != nullptr) {
2550 uint32_t leftMax = 1 << 24;
2551 uint32_t rightMax = 1 << 24;
2552 cachedVolumeControlEffect->setVolume(&leftMax, &rightMax, true /*controller*/);
2553 }
2554 if (volumeControlEffect != nullptr) {
2555 uint32_t leftZero = 0;
2556 uint32_t rightZero = 0;
2557 volumeControlEffect->setVolume(&leftZero, &rightZero, true /*controller*/);
2558 }
2559 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002560 mLeftVolume = newLeft;
2561 mRightVolume = newRight;
2562
2563 // second get volume update from volume controller
2564 if (ctrlIdx >= 0) {
2565 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2566 mNewLeftVolume = newLeft;
2567 mNewRightVolume = newRight;
2568 }
2569 // then indicate volume to all other effects in chain.
2570 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002571 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002572 uint32_t lVol = newLeft;
2573 uint32_t rVol = newRight;
2574
2575 for (size_t i = 0; i < size; i++) {
2576 if ((int)i == ctrlIdx) {
2577 continue;
2578 }
2579 // this also works for ctrlIdx == -1 when there is no volume controller
2580 if ((int)i > ctrlIdx) {
2581 lVol = *left;
2582 rVol = *right;
2583 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002584 // Pass requested volume directly if this is volume monitor module
2585 if (mEffects[i]->isVolumeMonitor()) {
2586 mEffects[i]->setVolume(left, right, false);
2587 } else {
2588 mEffects[i]->setVolume(&lVol, &rVol, false);
2589 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002590 }
2591 *left = newLeft;
2592 *right = newRight;
2593
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002594 setVolumeForOutput_l(*left, *right);
2595
jiabineb5784e2023-08-24 21:10:44 +00002596 return volumeControlIndex.has_value();
Eric Laurentca7cc822012-11-19 14:55:58 -08002597}
2598
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002599// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002600void AudioFlinger::EffectChain::resetVolume_l()
2601{
Eric Laurente7449bf2016-08-03 18:44:07 -07002602 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2603 uint32_t left = mLeftVolume;
2604 uint32_t right = mRightVolume;
2605 (void)setVolume_l(&left, &right, true);
2606 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002607}
2608
jiabineb3bda02020-06-30 14:07:03 -07002609// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2610bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2611{
2612 for (size_t i = 0; i < mEffects.size(); ++i) {
2613 if (mEffects[i]->isHapticGenerator()) {
2614 return true;
2615 }
2616 }
2617 return false;
2618}
2619
Simon Bowden62823412022-10-17 14:52:26 +00002620void AudioFlinger::EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002621{
2622 Mutex::Autolock _l(mLock);
2623 for (size_t i = 0; i < mEffects.size(); ++i) {
2624 mEffects[i]->setHapticIntensity(id, intensity);
2625 }
2626}
2627
Eric Laurent1b928682014-10-02 19:41:47 -07002628void AudioFlinger::EffectChain::syncHalEffectsState()
2629{
2630 Mutex::Autolock _l(mLock);
2631 for (size_t i = 0; i < mEffects.size(); i++) {
2632 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2633 mEffects[i]->state() == EffectModule::STOPPING) {
2634 mEffects[i]->addEffectToHal_l();
2635 }
2636 }
2637}
2638
Eric Laurentca7cc822012-11-19 14:55:58 -08002639void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung71ba4b32022-10-06 12:09:49 -07002640NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002641{
Eric Laurentca7cc822012-11-19 14:55:58 -08002642 String8 result;
2643
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002644 const size_t numEffects = mEffects.size();
2645 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002646
Marco Nelissenb2208842014-02-07 14:00:50 -08002647 if (numEffects) {
2648 bool locked = AudioFlinger::dumpTryLock(mLock);
2649 // failed to lock - AudioFlinger is probably deadlocked
2650 if (!locked) {
2651 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002652 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002653
Andy Hungbded9c82017-11-30 18:47:35 -08002654 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2655 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2656 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2657 (int)inBufferStr.size(), "In buffer ",
2658 (int)outBufferStr.size(), "Out buffer ");
2659 result.appendFormat("\t%s %s %d\n",
2660 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002661 write(fd, result.c_str(), result.size());
Marco Nelissenb2208842014-02-07 14:00:50 -08002662
2663 for (size_t i = 0; i < numEffects; ++i) {
2664 sp<EffectModule> effect = mEffects[i];
2665 if (effect != 0) {
2666 effect->dump(fd, args);
2667 }
2668 }
2669
2670 if (locked) {
2671 mLock.unlock();
2672 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002673 } else {
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00002674 write(fd, result.c_str(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002675 }
2676}
2677
2678// must be called with ThreadBase::mLock held
2679void AudioFlinger::EffectChain::setEffectSuspended_l(
2680 const effect_uuid_t *type, bool suspend)
2681{
2682 sp<SuspendedEffectDesc> desc;
2683 // use effect type UUID timelow as key as there is no real risk of identical
2684 // timeLow fields among effect type UUIDs.
2685 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2686 if (suspend) {
2687 if (index >= 0) {
2688 desc = mSuspendedEffects.valueAt(index);
2689 } else {
2690 desc = new SuspendedEffectDesc();
2691 desc->mType = *type;
2692 mSuspendedEffects.add(type->timeLow, desc);
2693 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2694 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002695
Eric Laurentca7cc822012-11-19 14:55:58 -08002696 if (desc->mRefCount++ == 0) {
2697 sp<EffectModule> effect = getEffectIfEnabled(type);
2698 if (effect != 0) {
2699 desc->mEffect = effect;
2700 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002701 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002702 }
2703 }
2704 } else {
2705 if (index < 0) {
2706 return;
2707 }
2708 desc = mSuspendedEffects.valueAt(index);
2709 if (desc->mRefCount <= 0) {
2710 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002711 desc->mRefCount = 0;
2712 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002713 }
2714 if (--desc->mRefCount == 0) {
2715 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2716 if (desc->mEffect != 0) {
2717 sp<EffectModule> effect = desc->mEffect.promote();
2718 if (effect != 0) {
2719 effect->setSuspended(false);
2720 effect->lock();
2721 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002722 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002723 effect->setEnabled_l(handle->enabled());
2724 }
2725 effect->unlock();
2726 }
2727 desc->mEffect.clear();
2728 }
2729 mSuspendedEffects.removeItemsAt(index);
2730 }
2731 }
2732}
2733
2734// must be called with ThreadBase::mLock held
2735void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2736{
2737 sp<SuspendedEffectDesc> desc;
2738
2739 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2740 if (suspend) {
2741 if (index >= 0) {
2742 desc = mSuspendedEffects.valueAt(index);
2743 } else {
2744 desc = new SuspendedEffectDesc();
2745 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2746 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2747 }
2748 if (desc->mRefCount++ == 0) {
2749 Vector< sp<EffectModule> > effects;
2750 getSuspendEligibleEffects(effects);
2751 for (size_t i = 0; i < effects.size(); i++) {
2752 setEffectSuspended_l(&effects[i]->desc().type, true);
2753 }
2754 }
2755 } else {
2756 if (index < 0) {
2757 return;
2758 }
2759 desc = mSuspendedEffects.valueAt(index);
2760 if (desc->mRefCount <= 0) {
2761 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2762 desc->mRefCount = 1;
2763 }
2764 if (--desc->mRefCount == 0) {
2765 Vector<const effect_uuid_t *> types;
2766 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2767 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2768 continue;
2769 }
2770 types.add(&mSuspendedEffects.valueAt(i)->mType);
2771 }
2772 for (size_t i = 0; i < types.size(); i++) {
2773 setEffectSuspended_l(types[i], false);
2774 }
2775 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2776 mSuspendedEffects.keyAt(index));
2777 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2778 }
2779 }
2780}
2781
2782
2783// The volume effect is used for automated tests only
2784#ifndef OPENSL_ES_H_
2785static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2786 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2787const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2788#endif //OPENSL_ES_H_
2789
Eric Laurentd8365c52017-07-16 15:27:05 -07002790/* static */
2791bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2792{
2793 // Only NS and AEC are suspended when BtNRec is off
2794 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2795 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2796 return true;
2797 }
2798 return false;
2799}
2800
Eric Laurentca7cc822012-11-19 14:55:58 -08002801bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2802{
2803 // auxiliary effects and visualizer are never suspended on output mix
2804 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2805 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2806 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002807 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2808 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002809 return false;
2810 }
2811 return true;
2812}
2813
2814void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2815 Vector< sp<AudioFlinger::EffectModule> > &effects)
2816{
2817 effects.clear();
2818 for (size_t i = 0; i < mEffects.size(); i++) {
2819 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2820 effects.add(mEffects[i]);
2821 }
2822 }
2823}
2824
2825sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2826 const effect_uuid_t *type)
2827{
2828 sp<EffectModule> effect = getEffectFromType_l(type);
2829 return effect != 0 && effect->isEnabled() ? effect : 0;
2830}
2831
2832void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2833 bool enabled)
2834{
2835 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2836 if (enabled) {
2837 if (index < 0) {
2838 // if the effect is not suspend check if all effects are suspended
2839 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2840 if (index < 0) {
2841 return;
2842 }
2843 if (!isEffectEligibleForSuspend(effect->desc())) {
2844 return;
2845 }
2846 setEffectSuspended_l(&effect->desc().type, enabled);
2847 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2848 if (index < 0) {
2849 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2850 return;
2851 }
2852 }
2853 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2854 effect->desc().type.timeLow);
2855 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002856 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002857 if (desc->mEffect == 0) {
2858 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002859 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002860 effect->setSuspended(true);
2861 }
2862 } else {
2863 if (index < 0) {
2864 return;
2865 }
2866 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2867 effect->desc().type.timeLow);
2868 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2869 desc->mEffect.clear();
2870 effect->setSuspended(false);
2871 }
2872}
2873
Eric Laurent5baf2af2013-09-12 17:37:00 -07002874bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002875{
2876 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002877 return isNonOffloadableEnabled_l();
2878}
2879
2880bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2881{
Eric Laurent813e2a72013-08-31 12:59:48 -07002882 size_t size = mEffects.size();
2883 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002884 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002885 return true;
2886 }
2887 }
2888 return false;
2889}
2890
Eric Laurentaaa44472014-09-12 17:41:50 -07002891void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2892{
2893 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002894 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002895}
2896
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002897void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2898{
2899 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2900 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2901 }
2902 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2903 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2904 }
jiabinc658e452022-10-21 20:52:21 +00002905 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2906 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2907 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002908}
2909
2910void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2911{
2912 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2913 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2914 }
2915 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2916 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2917 }
2918}
2919
2920bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002921{
2922 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002923 for (const auto &effect : mEffects) {
2924 if (effect->isProcessImplemented()) {
2925 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002926 }
2927 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002928 // Allow effects without processing.
2929 return true;
2930}
2931
2932bool AudioFlinger::EffectChain::isFastCompatible() const
2933{
2934 Mutex::Autolock _l(mLock);
2935 for (const auto &effect : mEffects) {
2936 if (effect->isProcessImplemented()
2937 && effect->isImplementationSoftware()) {
2938 return false;
2939 }
2940 }
2941 // Allow effects without processing or hw accelerated effects.
2942 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002943}
2944
jiabinc658e452022-10-21 20:52:21 +00002945bool AudioFlinger::EffectChain::isBitPerfectCompatible() const {
2946 Mutex::Autolock _l(mLock);
2947 for (const auto &effect : mEffects) {
2948 if (effect->isProcessImplemented()
2949 && effect->isImplementationSoftware()) {
2950 return false;
2951 }
2952 }
2953 // Allow effects without processing or hw accelerated effects.
2954 return true;
2955}
2956
Eric Laurent4c415062016-06-17 16:14:16 -07002957// isCompatibleWithThread_l() must be called with thread->mLock held
2958bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2959{
2960 Mutex::Autolock _l(mLock);
2961 for (size_t i = 0; i < mEffects.size(); i++) {
2962 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2963 return false;
2964 }
2965 }
2966 return true;
2967}
2968
Eric Laurent6b446ce2019-12-13 10:56:31 -08002969// EffectCallbackInterface implementation
2970status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2971 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2972 sp<EffectHalInterface> *effect) {
2973 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07002974 const sp<EffectsFactoryHalInterface> effectsFactory =
2975 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002976 if (effectsFactory != 0) {
2977 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2978 }
2979 return status;
2980}
2981
2982bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002983 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002984 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002985 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002986}
2987
2988status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2989 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002990 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002991}
2992
2993status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07002994 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002995 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002996 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997 if (t == nullptr) {
2998 return result;
2999 }
3000 sp <StreamHalInterface> st = t->stream();
3001 if (st == nullptr) {
3002 return result;
3003 }
3004 result = st->addEffect(effect);
3005 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3006 return result;
3007}
3008
3009status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003010 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003011 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003012 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003013 if (t == nullptr) {
3014 return result;
3015 }
3016 sp <StreamHalInterface> st = t->stream();
3017 if (st == nullptr) {
3018 return result;
3019 }
3020 result = st->removeEffect(effect);
3021 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3022 return result;
3023}
3024
3025audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003026 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003027 if (t == nullptr) {
3028 return AUDIO_IO_HANDLE_NONE;
3029 }
3030 return t->id();
3031}
3032
3033bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003034 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003035 if (t == nullptr) {
3036 return true;
3037 }
3038 return t->isOutput();
3039}
3040
3041bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003042 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003043}
3044
3045bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003046 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003047}
3048
3049bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003050 switch (mThreadType) {
3051 case ThreadBase::OFFLOAD:
3052 case ThreadBase::MMAP_PLAYBACK:
3053 case ThreadBase::MMAP_CAPTURE:
3054 return true;
3055 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003056 return false;
3057 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003058}
3059
3060bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3061 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062}
3063
3064uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003065 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003066 if (t == nullptr) {
3067 return 0;
3068 }
3069 return t->sampleRate();
3070}
3071
Eric Laurentf1f22e72021-07-13 14:04:14 +02003072audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3073 sp<ThreadBase> t = thread().promote();
3074 if (t == nullptr) {
3075 return AUDIO_CHANNEL_NONE;
3076 }
3077 sp<EffectChain> c = chain().promote();
3078 if (c == nullptr) {
3079 return AUDIO_CHANNEL_NONE;
3080 }
3081
Eric Laurentb62d0362021-10-26 17:40:18 +02003082 if (mThreadType == ThreadBase::SPATIALIZER) {
3083 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3084 if (c->isFirstEffect(id)) {
3085 return t->mixerChannelMask();
3086 } else {
3087 return t->channelMask();
3088 }
3089 } else if (!audio_is_global_session(c->sessionId())) {
3090 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3091 return t->mixerChannelMask();
3092 } else {
3093 return t->channelMask();
3094 }
3095 } else {
3096 return t->channelMask();
3097 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003098 } else {
3099 return t->channelMask();
3100 }
3101}
3102
3103uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003104 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003105}
3106
3107audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003108 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003109 if (t == nullptr) {
3110 return AUDIO_CHANNEL_NONE;
3111 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003112 sp<EffectChain> c = chain().promote();
3113 if (c == nullptr) {
3114 return AUDIO_CHANNEL_NONE;
3115 }
3116
3117 if (mThreadType == ThreadBase::SPATIALIZER) {
3118 if (!audio_is_global_session(c->sessionId())) {
3119 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3120 return t->mixerChannelMask();
3121 } else {
3122 return t->channelMask();
3123 }
3124 } else {
3125 return t->channelMask();
3126 }
3127 } else {
3128 return t->channelMask();
3129 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003130}
3131
Eric Laurentf1f22e72021-07-13 14:04:14 +02003132uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003133 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003134}
3135
jiabineb3bda02020-06-30 14:07:03 -07003136audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003137 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003138 if (t == nullptr) {
3139 return AUDIO_CHANNEL_NONE;
3140 }
3141 return t->hapticChannelMask();
3142}
3143
Eric Laurent6b446ce2019-12-13 10:56:31 -08003144size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003145 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003146 if (t == nullptr) {
3147 return 0;
3148 }
3149 return t->frameCount();
3150}
3151
Andy Hung71ba4b32022-10-06 12:09:49 -07003152uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3153NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3154{
Andy Hung328d6772021-01-12 12:32:21 -08003155 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003156 if (t == nullptr) {
3157 return 0;
3158 }
Andy Hung71ba4b32022-10-06 12:09:49 -07003159 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003160 return t->latency_l();
3161}
3162
Andy Hung71ba4b32022-10-06 12:09:49 -07003163void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3164NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3165{
Andy Hung328d6772021-01-12 12:32:21 -08003166 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003167 if (t == nullptr) {
3168 return;
3169 }
3170 t->setVolumeForOutput_l(left, right);
3171}
3172
3173void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003174 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003175 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003176 if (t == nullptr) {
3177 return;
3178 }
3179 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3180
Andy Hung328d6772021-01-12 12:32:21 -08003181 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003182 if (c == nullptr) {
3183 return;
3184 }
Eric Laurent41709552019-12-16 19:34:05 -08003185 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3186 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003187}
3188
Eric Laurent41709552019-12-16 19:34:05 -08003189void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003190 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003191 if (t == nullptr) {
3192 return;
3193 }
Eric Laurent41709552019-12-16 19:34:05 -08003194 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3195 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003196}
3197
Eric Laurent41709552019-12-16 19:34:05 -08003198void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003199 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3200
Andy Hung328d6772021-01-12 12:32:21 -08003201 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003202 if (t == nullptr) {
3203 return;
3204 }
3205 t->onEffectDisable();
3206}
3207
3208bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3209 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003210 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003211 if (t == nullptr) {
3212 return false;
3213 }
3214 t->disconnectEffectHandle(handle, unpinIfLast);
3215 return true;
3216}
3217
3218void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003219 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003220 if (c == nullptr) {
3221 return;
3222 }
3223 c->resetVolume_l();
3224
3225}
3226
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003227product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003228 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003229 if (c == nullptr) {
3230 return PRODUCT_STRATEGY_NONE;
3231 }
3232 return c->strategy();
3233}
3234
3235int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003236 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003237 if (c == nullptr) {
3238 return 0;
3239 }
3240 return c->activeTrackCnt();
3241}
3242
Eric Laurentb82e6b72019-11-22 17:25:04 -08003243
3244#undef LOG_TAG
3245#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3246
3247status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3248{
3249 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3250 Mutex::Autolock _l(mProxyLock);
3251 if (status == NO_ERROR) {
3252 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003253 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003254 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003255 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003256 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003257 bs = handle.second->disable(&status);
3258 }
3259 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003260 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003261 }
3262 }
3263 }
3264 ALOGV("%s enable %d status %d", __func__, enabled, status);
3265 return status;
3266}
3267
3268status_t AudioFlinger::DeviceEffectProxy::init(
3269 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3270//For all audio patches
3271//If src or sink device match
3272//If the effect is HW accelerated
3273// if no corresponding effect module
3274// Create EffectModule: mHalEffect
3275//Create and attach EffectHandle
3276//If the effect is not HW accelerated and the patch sink or src is a mixer port
3277// Create Effect on patch input or output thread on session -1
3278//Add EffectHandle to EffectHandle map of Effect Proxy:
3279 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3280 status_t status = NO_ERROR;
3281 for (auto &patch : patches) {
3282 status = onCreatePatch(patch.first, patch.second);
3283 ALOGV("%s onCreatePatch status %d", __func__, status);
3284 if (status == BAD_VALUE) {
3285 return status;
3286 }
3287 }
3288 return status;
3289}
3290
3291status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3292 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3293 status_t status = NAME_NOT_FOUND;
3294 sp<EffectHandle> handle;
3295 // only consider source[0] as this is the only "true" source of a patch
3296 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3297 ALOGV("%s source checkPort status %d", __func__, status);
3298 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3299 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3300 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3301 }
3302 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3303 Mutex::Autolock _l(mProxyLock);
3304 mEffectHandles.emplace(patchHandle, handle);
3305 }
3306 ALOGW_IF(status == BAD_VALUE,
3307 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3308
3309 return status;
3310}
3311
3312status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3313 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3314
3315 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3316 __func__, port->type, port->ext.device.type,
3317 port->ext.device.address, port->id, patch.isSoftware());
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003318 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType ||
3319 port->ext.device.address != mDevice.address()) {
3320 return NAME_NOT_FOUND;
3321 }
3322 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) &&
3323 (audio_port_config_has_input_direction(port))) {
3324 ALOGI("%s don't create postprocessing effect on record port", __func__);
3325 return NAME_NOT_FOUND;
3326 }
3327 if (((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) &&
3328 (!audio_port_config_has_input_direction(port))) {
3329 ALOGI("%s don't create preprocessing effect on playback port", __func__);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003330 return NAME_NOT_FOUND;
3331 }
3332 status_t status = NAME_NOT_FOUND;
3333
3334 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3335 Mutex::Autolock _l(mProxyLock);
3336 mDevicePort = *port;
3337 mHalEffect = new EffectModule(mMyCallback,
3338 const_cast<effect_descriptor_t *>(&mDescriptor),
3339 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3340 false /* pinned */, port->id);
3341 if (audio_is_input_device(mDevice.mType)) {
3342 mHalEffect->setInputDevice(mDevice);
3343 } else {
3344 mHalEffect->setDevices({mDevice});
3345 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003346 mHalEffect->configure();
3347
Eric Laurentde8caf42021-08-11 17:19:25 +02003348 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3349 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003350 status = (*handle)->initCheck();
3351 if (status == OK) {
3352 status = mHalEffect->addHandle((*handle).get());
3353 } else {
3354 mHalEffect.clear();
3355 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3356 }
3357 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3358 sp <ThreadBase> thread;
3359 if (audio_port_config_has_input_direction(port)) {
3360 if (patch.isSoftware()) {
3361 thread = patch.mRecord.thread();
3362 } else {
3363 thread = patch.thread().promote();
3364 }
3365 } else {
3366 if (patch.isSoftware()) {
3367 thread = patch.mPlayback.thread();
3368 } else {
3369 thread = patch.thread().promote();
3370 }
3371 }
3372 int enabled;
3373 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3374 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003375 &enabled, &status, false, false /*probe*/,
3376 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003377 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3378 } else {
3379 status = BAD_VALUE;
3380 }
Shunkai Yaoee1e8a22023-05-05 22:43:24 +00003381
Eric Laurentb82e6b72019-11-22 17:25:04 -08003382 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003383 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003384 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003385 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003386 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003387 bs = (*handle)->disable(&status);
3388 }
3389 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003390 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003391 }
3392 }
3393 return status;
3394}
3395
3396void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003397 sp<EffectHandle> effect;
3398 {
3399 Mutex::Autolock _l(mProxyLock);
3400 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3401 effect = mEffectHandles.at(patchHandle);
3402 mEffectHandles.erase(patchHandle);
3403 }
3404 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003405}
3406
3407
3408size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3409{
3410 Mutex::Autolock _l(mProxyLock);
3411 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003412 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003413 mHalEffect.clear();
3414 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3415 }
3416 return mHalEffect == nullptr ? 0 : 1;
3417}
3418
3419status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003420 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003421 if (mHalEffect == nullptr) {
3422 return NO_INIT;
3423 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003424 return mManagerCallback->addEffectToHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003425}
3426
3427status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003428 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003429 if (mHalEffect == nullptr) {
3430 return NO_INIT;
3431 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -07003432 return mManagerCallback->removeEffectFromHal(&mDevicePort, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003433}
3434
3435bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3436 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3437 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3438 }
3439 return true;
3440}
3441
3442uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3443 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3444 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3445 return mDevicePort.sample_rate;
3446 }
3447 return DEFAULT_OUTPUT_SAMPLE_RATE;
3448}
3449
3450audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3451 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3452 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3453 return mDevicePort.channel_mask;
3454 }
3455 return AUDIO_CHANNEL_OUT_STEREO;
3456}
3457
3458uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3459 if (isOutput()) {
3460 return audio_channel_count_from_out_mask(channelMask());
3461 }
3462 return audio_channel_count_from_in_mask(channelMask());
3463}
3464
Andy Hung71ba4b32022-10-06 12:09:49 -07003465void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3466NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3467{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003468 const Vector<String16> args;
3469 EffectBase::dump(fd, args);
3470
3471 const bool locked = dumpTryLock(mProxyLock);
3472 if (!locked) {
3473 String8 result("DeviceEffectProxy may be deadlocked\n");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003474 write(fd, result.c_str(), result.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003475 }
3476
3477 String8 outStr;
3478 if (mHalEffect != nullptr) {
3479 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3480 } else {
3481 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3482 }
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003483 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003484 outStr.clear();
3485
3486 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003487 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003488 outStr.clear();
3489
3490 for (const auto& iter : mEffectHandles) {
3491 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
Tomasz Wasilczyk8f36f6e2023-08-15 20:59:35 +00003492 write(fd, outStr.c_str(), outStr.size());
Eric Laurentb82e6b72019-11-22 17:25:04 -08003493 outStr.clear();
3494 sp<EffectBase> effect = iter.second->effect().promote();
3495 if (effect != nullptr) {
3496 effect->dump(fd, args);
3497 }
3498 }
3499
3500 if (locked) {
3501 mLock.unlock();
3502 }
3503}
3504
3505#undef LOG_TAG
3506#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3507
3508int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3509 return mManagerCallback->newEffectId();
3510}
3511
3512
3513bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3514 EffectHandle *handle, bool unpinIfLast) {
3515 sp<EffectBase> effectBase = handle->effect().promote();
3516 if (effectBase == nullptr) {
3517 return false;
3518 }
3519
3520 sp<EffectModule> effect = effectBase->asEffectModule();
3521 if (effect == nullptr) {
3522 return false;
3523 }
3524
3525 // restore suspended effects if the disconnected handle was enabled and the last one.
3526 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3527 if (remove) {
3528 sp<DeviceEffectProxy> proxy = mProxy.promote();
3529 if (proxy != nullptr) {
3530 proxy->removeEffect(effect);
3531 }
3532 if (handle->enabled()) {
3533 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3534 }
3535 }
3536 return true;
3537}
3538
3539status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3540 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3541 sp<EffectHalInterface> *effect) {
3542 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3543}
3544
3545status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003546 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003547 sp<DeviceEffectProxy> proxy = mProxy.promote();
3548 if (proxy == nullptr) {
3549 return NO_INIT;
3550 }
3551 return proxy->addEffectToHal(effect);
3552}
3553
3554status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung71ba4b32022-10-06 12:09:49 -07003555 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003556 sp<DeviceEffectProxy> proxy = mProxy.promote();
3557 if (proxy == nullptr) {
3558 return NO_INIT;
3559 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003560 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003561}
3562
3563bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3564 sp<DeviceEffectProxy> proxy = mProxy.promote();
3565 if (proxy == nullptr) {
3566 return true;
3567 }
3568 return proxy->isOutput();
3569}
3570
3571uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3572 sp<DeviceEffectProxy> proxy = mProxy.promote();
3573 if (proxy == nullptr) {
3574 return DEFAULT_OUTPUT_SAMPLE_RATE;
3575 }
3576 return proxy->sampleRate();
3577}
3578
Eric Laurentf1f22e72021-07-13 14:04:14 +02003579audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3580 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003581 sp<DeviceEffectProxy> proxy = mProxy.promote();
3582 if (proxy == nullptr) {
3583 return AUDIO_CHANNEL_OUT_STEREO;
3584 }
3585 return proxy->channelMask();
3586}
3587
Eric Laurentf1f22e72021-07-13 14:04:14 +02003588uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3589 sp<DeviceEffectProxy> proxy = mProxy.promote();
3590 if (proxy == nullptr) {
3591 return 2;
3592 }
3593 return proxy->channelCount();
3594}
3595
3596audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3597 sp<DeviceEffectProxy> proxy = mProxy.promote();
3598 if (proxy == nullptr) {
3599 return AUDIO_CHANNEL_OUT_STEREO;
3600 }
3601 return proxy->channelMask();
3602}
3603
3604uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003605 sp<DeviceEffectProxy> proxy = mProxy.promote();
3606 if (proxy == nullptr) {
3607 return 2;
3608 }
3609 return proxy->channelCount();
3610}
3611
Eric Laurent76c89f32021-12-03 17:13:23 +01003612void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3613 const sp<EffectBase>& effectBase) {
3614 sp<EffectModule> effect = effectBase->asEffectModule();
3615 if (effect == nullptr) {
3616 return;
3617 }
3618 effect->start();
3619}
3620
3621void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3622 const sp<EffectBase>& effectBase) {
3623 sp<EffectModule> effect = effectBase->asEffectModule();
3624 if (effect == nullptr) {
3625 return;
3626 }
3627 effect->stop();
3628}
3629
Glenn Kasten63238ef2015-03-02 15:50:29 -08003630} // namespace android