blob: 72c378d504620d3c704255e8058925dd758700eb [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080047
48// ----------------------------------------------------------------------------
49
50// Note: the following macro is used for extremely verbose logging message. In
51// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
52// 0; but one side effect of this is to turn all LOGV's as well. Some messages
53// are so verbose that we want to suppress them even when we have ALOG_ASSERT
54// turned on. Do not uncomment the #def below unless you really know what you
55// are doing and want to see all of the extremely verbose messages.
56//#define VERY_VERY_VERBOSE_LOGGING
57#ifdef VERY_VERY_VERBOSE_LOGGING
58#define ALOGVV ALOGV
59#else
60#define ALOGVV(a...) do { } while(0)
61#endif
62
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090063#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
64
Eric Laurentca7cc822012-11-19 14:55:58 -080065namespace android {
66
Andy Hung1131b6e2020-12-08 20:47:45 -080067using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070068using binder::Status;
69
70namespace {
71
72// Append a POD value into a vector of bytes.
73template<typename T>
74void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
75 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
76 buffer->insert(buffer->end(), ar, ar + sizeof(T));
77}
78
79// Write a POD value into a vector of bytes (clears the previous buffer
80// content).
81template<typename T>
82void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
83 buffer->clear();
84 appendToBuffer(value, buffer);
85}
86
87} // namespace
88
Eric Laurentca7cc822012-11-19 14:55:58 -080089// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080090// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
92
93#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080094#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080095
Eric Laurent41709552019-12-16 19:34:05 -080096AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080097 effect_descriptor_t *desc,
98 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080099 audio_session_t sessionId,
100 bool pinned)
101 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800102 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800103 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurentca7cc822012-11-19 14:55:58 -0800105}
106
Eric Laurent41709552019-12-16 19:34:05 -0800107// must be called with EffectModule::mLock held
108status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800109{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800110
Eric Laurent41709552019-12-16 19:34:05 -0800111 ALOGV("setEnabled %p enabled %d", this, enabled);
112
113 if (enabled != isEnabled()) {
114 switch (mState) {
115 // going from disabled to enabled
116 case IDLE:
117 mState = STARTING;
118 break;
119 case STOPPED:
120 mState = RESTART;
121 break;
122 case STOPPING:
123 mState = ACTIVE;
124 break;
125
126 // going from enabled to disabled
127 case RESTART:
128 mState = STOPPED;
129 break;
130 case STARTING:
131 mState = IDLE;
132 break;
133 case ACTIVE:
134 mState = STOPPING;
135 break;
136 case DESTROYED:
137 return NO_ERROR; // simply ignore as we are being destroyed
138 }
139 for (size_t i = 1; i < mHandles.size(); i++) {
140 EffectHandle *h = mHandles[i];
141 if (h != NULL && !h->disconnected()) {
142 h->setEnabled(enabled);
143 }
144 }
145 }
146 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800147}
148
Eric Laurent41709552019-12-16 19:34:05 -0800149status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
150{
151 status_t status;
152 {
153 Mutex::Autolock _l(mLock);
154 status = setEnabled_l(enabled);
155 }
156 if (fromHandle) {
157 if (enabled) {
158 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700159 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800160 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800162 }
163 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700164 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800165 }
166 }
167 return status;
168}
169
170bool AudioFlinger::EffectBase::isEnabled() const
171{
172 switch (mState) {
173 case RESTART:
174 case STARTING:
175 case ACTIVE:
176 return true;
177 case IDLE:
178 case STOPPING:
179 case STOPPED:
180 case DESTROYED:
181 default:
182 return false;
183 }
184}
185
186void AudioFlinger::EffectBase::setSuspended(bool suspended)
187{
188 Mutex::Autolock _l(mLock);
189 mSuspended = suspended;
190}
191
192bool AudioFlinger::EffectBase::suspended() const
193{
194 Mutex::Autolock _l(mLock);
195 return mSuspended;
196}
197
198status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800199{
200 status_t status;
201
202 Mutex::Autolock _l(mLock);
203 int priority = handle->priority();
204 size_t size = mHandles.size();
205 EffectHandle *controlHandle = NULL;
206 size_t i;
207 for (i = 0; i < size; i++) {
208 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800209 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 continue;
211 }
212 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 if (h->priority() <= priority) {
217 break;
218 }
219 }
220 // if inserted in first place, move effect control from previous owner to this handle
221 if (i == 0) {
222 bool enabled = false;
223 if (controlHandle != NULL) {
224 enabled = controlHandle->enabled();
225 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
226 }
227 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
228 status = NO_ERROR;
229 } else {
230 status = ALREADY_EXISTS;
231 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700232 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800233 mHandles.insertAt(handle, i);
234 return status;
235}
236
Eric Laurent41709552019-12-16 19:34:05 -0800237status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700238{
239 status_t status = NO_ERROR;
240 bool doRegister = false;
241 bool registered = false;
242 bool doEnable = false;
243 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700244 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800245 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700246
247 {
248 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200249
250 if ((isInternal_l() && !mPolicyRegistered)
251 || !getCallback()->isAudioPolicyReady()) {
252 return NO_ERROR;
253 }
254
Eric Laurent6c796322019-04-09 14:13:17 -0700255 // register effect when first handle is attached and unregister when last handle is removed
256 if (mPolicyRegistered != mHandles.size() > 0) {
257 doRegister = true;
258 mPolicyRegistered = mHandles.size() > 0;
259 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700260 const auto callback = getCallback();
261 io = callback->io();
262 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700263 }
264 }
265 // enable effect when registered according to enable state requested by controlling handle
266 if (mHandles.size() > 0) {
267 EffectHandle *handle = controlHandle_l();
268 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
269 doEnable = true;
270 mPolicyEnabled = handle->enabled();
271 }
272 }
273 registered = mPolicyRegistered;
274 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100275 // The simultaneous release of two EffectHandles with the same EffectModule
276 // may cause us to call this method at the same time.
277 // This may deadlock under some circumstances (b/180941720). Avoid this.
278 if (!doRegister && !(registered && doEnable)) {
279 return NO_ERROR;
280 }
Eric Laurent6c796322019-04-09 14:13:17 -0700281 mPolicyLock.lock();
282 }
283 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
284 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
285 if (doRegister) {
286 if (registered) {
287 status = AudioSystem::registerEffect(
288 &mDescriptor,
289 io,
290 strategy,
291 mSessionId,
292 mId);
293 } else {
294 status = AudioSystem::unregisterEffect(mId);
295 }
296 }
297 if (registered && doEnable) {
298 status = AudioSystem::setEffectEnabled(mId, enabled);
299 }
300 mPolicyLock.unlock();
301
302 return status;
303}
304
305
Eric Laurent41709552019-12-16 19:34:05 -0800306ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800307{
308 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309 return removeHandle_l(handle);
310}
311
Eric Laurent41709552019-12-16 19:34:05 -0800312ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800313{
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 size_t size = mHandles.size();
315 size_t i;
316 for (i = 0; i < size; i++) {
317 if (mHandles[i] == handle) {
318 break;
319 }
320 }
321 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800322 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
323 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800324 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800325 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800326
327 mHandles.removeAt(i);
328 // if removed from first place, move effect control from this handle to next in line
329 if (i == 0) {
330 EffectHandle *h = controlHandle_l();
331 if (h != NULL) {
332 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
333 }
334 }
335
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530336 // Prevent calls to process() and other functions on effect interface from now on.
337 // The effect engine will be released by the destructor when the last strong reference on
338 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800339 if (mHandles.size() == 0 && !mPinned) {
340 mState = DESTROYED;
341 }
342
343 return mHandles.size();
344}
345
346// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800347AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800348{
349 // the first valid handle in the list has control over the module
350 for (size_t i = 0; i < mHandles.size(); i++) {
351 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800352 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800353 return h;
354 }
355 }
356
357 return NULL;
358}
359
Eric Laurentf10c7092016-12-06 17:09:56 -0800360// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800361ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800362{
Andy Hungfda44002021-06-03 17:23:16 -0700363 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800364 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700365 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800366 return mHandles.size();
367 }
368
Eric Laurentf10c7092016-12-06 17:09:56 -0800369 Mutex::Autolock _l(mLock);
370 ssize_t numHandles = removeHandle_l(handle);
371 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800372 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700373 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800375 }
376 return numHandles;
377}
378
Eric Laurent41709552019-12-16 19:34:05 -0800379bool AudioFlinger::EffectBase::purgeHandles()
380{
381 bool enabled = false;
382 Mutex::Autolock _l(mLock);
383 EffectHandle *handle = controlHandle_l();
384 if (handle != NULL) {
385 enabled = handle->enabled();
386 }
387 mHandles.clear();
388 return enabled;
389}
390
391void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700392 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800393}
394
395static String8 effectFlagsToString(uint32_t flags) {
396 String8 s;
397
398 s.append("conn. mode: ");
399 switch (flags & EFFECT_FLAG_TYPE_MASK) {
400 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
401 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
402 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
403 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
404 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("insert pref: ");
410 switch (flags & EFFECT_FLAG_INSERT_MASK) {
411 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
412 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
413 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
414 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 s.append("volume mgmt: ");
420 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
421 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
422 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
423 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
424 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
425 default: s.append("unknown/reserved"); break;
426 }
427 s.append(", ");
428
429 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
430 if (devind) {
431 s.append("device indication: ");
432 switch (devind) {
433 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
434 default: s.append("unknown/reserved"); break;
435 }
436 s.append(", ");
437 }
438
439 s.append("input mode: ");
440 switch (flags & EFFECT_FLAG_INPUT_MASK) {
441 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
442 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
443 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
444 default: s.append("not set"); break;
445 }
446 s.append(", ");
447
448 s.append("output mode: ");
449 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
450 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
451 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
452 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
453 default: s.append("not set"); break;
454 }
455 s.append(", ");
456
457 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
458 if (accel) {
459 s.append("hardware acceleration: ");
460 switch (accel) {
461 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
462 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
469 if (modeind) {
470 s.append("mode indication: ");
471 switch (modeind) {
472 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
479 if (srcind) {
480 s.append("source indication: ");
481 switch (srcind) {
482 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
483 default: s.append("unknown/reserved"); break;
484 }
485 s.append(", ");
486 }
487
488 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
489 s.append("offloadable, ");
490 }
491
492 int len = s.length();
493 if (s.length() > 2) {
494 (void) s.lockBuffer(len);
495 s.unlockBuffer(len - 2);
496 }
497 return s;
498}
499
500void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
501{
502 String8 result;
503
504 result.appendFormat("\tEffect ID %d:\n", mId);
505
506 bool locked = AudioFlinger::dumpTryLock(mLock);
507 // failed to lock - AudioFlinger is probably deadlocked
508 if (!locked) {
509 result.append("\t\tCould not lock Fx mutex:\n");
510 }
511
512 result.append("\t\tSession State Registered Enabled Suspended:\n");
513 result.appendFormat("\t\t%05d %03d %s %s %s\n",
514 mSessionId, mState, mPolicyRegistered ? "y" : "n",
515 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
516
517 result.append("\t\tDescriptor:\n");
518 char uuidStr[64];
519 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
520 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
521 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
522 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
523 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
524 mDescriptor.apiVersion,
525 mDescriptor.flags,
526 effectFlagsToString(mDescriptor.flags).string());
527 result.appendFormat("\t\t- name: %s\n",
528 mDescriptor.name);
529
530 result.appendFormat("\t\t- implementor: %s\n",
531 mDescriptor.implementor);
532
533 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
534 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
535 char buffer[256];
536 for (size_t i = 0; i < mHandles.size(); ++i) {
537 EffectHandle *handle = mHandles[i];
538 if (handle != NULL && !handle->disconnected()) {
539 handle->dumpToBuffer(buffer, sizeof(buffer));
540 result.append(buffer);
541 }
542 }
543 if (locked) {
544 mLock.unlock();
545 }
546
547 write(fd, result.string(), result.length());
548}
549
550// ----------------------------------------------------------------------------
551// EffectModule implementation
552// ----------------------------------------------------------------------------
553
554#undef LOG_TAG
555#define LOG_TAG "AudioFlinger::EffectModule"
556
557AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
558 effect_descriptor_t *desc,
559 int id,
560 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800561 bool pinned,
562 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800563 : EffectBase(callback, desc, id, sessionId, pinned),
564 // clear mConfig to ensure consistent initial value of buffer framecount
565 // in case buffers are associated by setInBuffer() or setOutBuffer()
566 // prior to configure().
567 mConfig{{}, {}},
568 mStatus(NO_INIT),
569 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
570 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800571 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000572 mAddedToHal(false),
573 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800574#ifdef FLOAT_EFFECT_CHAIN
575 , mSupportsFloat(false)
576#endif
577{
578 ALOGV("Constructor %p pinned %d", this, pinned);
579 int lStatus;
580
581 // create effect engine from effect factory
582 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800583 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800584 if (mStatus != NO_ERROR) {
585 return;
586 }
587 lStatus = init();
588 if (lStatus < 0) {
589 mStatus = lStatus;
590 goto Error;
591 }
592
593 setOffloaded(callback->isOffload(), callback->io());
594 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
595
596 return;
597Error:
598 mEffectInterface.clear();
599 ALOGV("Constructor Error %d", mStatus);
600}
601
602AudioFlinger::EffectModule::~EffectModule()
603{
604 ALOGV("Destructor %p", this);
605 if (mEffectInterface != 0) {
606 char uuidStr[64];
607 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
608 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
609 this, uuidStr);
610 release_l();
611 }
612
613}
614
Eric Laurentfa1e1232016-08-02 19:01:49 -0700615bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800616 Mutex::Autolock _l(mLock);
617
Eric Laurentfa1e1232016-08-02 19:01:49 -0700618 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800619 switch (mState) {
620 case RESTART:
621 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700622 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800623
624 case STARTING:
625 // clear auxiliary effect input buffer for next accumulation
626 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
627 memset(mConfig.inputCfg.buffer.raw,
628 0,
629 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
630 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 if (start_l() == NO_ERROR) {
632 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700633 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700634 } else {
635 mState = IDLE;
636 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800637 break;
638 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900639 // volume control for offload and direct threads must take effect immediately.
640 if (stop_l() == NO_ERROR
641 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700642 mDisableWaitCnt = mMaxDisableWaitCnt;
643 } else {
644 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
645 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800646 mState = STOPPED;
647 break;
648 case STOPPED:
649 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
650 // turn off sequence.
651 if (--mDisableWaitCnt == 0) {
652 reset_l();
653 mState = IDLE;
654 }
655 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200656 case ACTIVE:
657 for (size_t i = 0; i < mHandles.size(); i++) {
658 if (!mHandles[i]->disconnected()) {
659 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
660 }
661 }
662 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800663 default: //IDLE , ACTIVE, DESTROYED
664 break;
665 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700666
667 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800668}
669
670void AudioFlinger::EffectModule::process()
671{
672 Mutex::Autolock _l(mLock);
673
Mikhail Naganov022b9952017-01-04 16:36:51 -0800674 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800675 return;
676 }
677
rago94a1ee82017-07-21 15:11:02 -0700678 const uint32_t inChannelCount =
679 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
680 const uint32_t outChannelCount =
681 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
682 const bool auxType =
683 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
684
Andy Hungfa69ca32017-11-30 10:07:53 -0800685 // safeInputOutputSampleCount is 0 if the channel count between input and output
686 // buffers do not match. This prevents automatic accumulation or copying between the
687 // input and output effect buffers without an intermediary effect process.
688 // TODO: consider implementing channel conversion.
689 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700690 mInChannelCountRequested != mOutChannelCountRequested ? 0
691 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800692 mConfig.inputCfg.buffer.frameCount,
693 mConfig.outputCfg.buffer.frameCount);
694 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
695#ifdef FLOAT_EFFECT_CHAIN
696 accumulate_float(
697 mConfig.outputCfg.buffer.f32,
698 mConfig.inputCfg.buffer.f32,
699 safeInputOutputSampleCount);
700#else
701 accumulate_i16(
702 mConfig.outputCfg.buffer.s16,
703 mConfig.inputCfg.buffer.s16,
704 safeInputOutputSampleCount);
705#endif
706 };
707 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
708#ifdef FLOAT_EFFECT_CHAIN
709 memcpy(
710 mConfig.outputCfg.buffer.f32,
711 mConfig.inputCfg.buffer.f32,
712 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
713
714#else
715 memcpy(
716 mConfig.outputCfg.buffer.s16,
717 mConfig.inputCfg.buffer.s16,
718 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
719#endif
720 };
721
Eric Laurentca7cc822012-11-19 14:55:58 -0800722 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700723 int ret;
724 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700725 if (auxType) {
726 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800727 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700728#ifdef FLOAT_EFFECT_CHAIN
729 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800730#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700731 // Do in-place float conversion for auxiliary effect input buffer.
732 static_assert(sizeof(float) <= sizeof(int32_t),
733 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
734
Andy Hungfa69ca32017-11-30 10:07:53 -0800735 memcpy_to_float_from_q4_27(
736 mConfig.inputCfg.buffer.f32,
737 mConfig.inputCfg.buffer.s32,
738 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800739#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800740 } else
Andy Hung116a4982017-11-30 10:15:08 -0800741#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 {
Andy Hung116a4982017-11-30 10:15:08 -0800743#ifdef FLOAT_AUX
744 memcpy_to_i16_from_float(
745 mConfig.inputCfg.buffer.s16,
746 mConfig.inputCfg.buffer.f32,
747 mConfig.inputCfg.buffer.frameCount);
748#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800749 memcpy_to_i16_from_q4_27(
750 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700751 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800752 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800753#endif
rago94a1ee82017-07-21 15:11:02 -0700754 }
rago94a1ee82017-07-21 15:11:02 -0700755 }
756#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800757 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
758 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
759
760 if (!auxType && mInChannelCountRequested != inChannelCount) {
761 adjust_channels(
762 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
763 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
764 sizeof(float),
765 sizeof(float)
766 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
767 inBuffer = mInConversionBuffer;
768 }
769 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
770 && mOutChannelCountRequested != outChannelCount) {
771 adjust_selected_channels(
772 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
773 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
774 sizeof(float),
775 sizeof(float)
776 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
777 outBuffer = mOutConversionBuffer;
778 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800779 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
780 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800781 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800782 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
783 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700784 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800785 memcpy_to_i16_from_float(
786 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800787 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700790 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800792 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
794 goto data_bypass;
795 }
796 memcpy_to_i16_from_float(
797 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800798 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800799 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800800 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700801 }
802 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800803#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800804 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800805#ifdef FLOAT_EFFECT_CHAIN
806 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800807 sp<EffectBufferHalInterface> target =
808 mOutChannelCountRequested != outChannelCount
809 ? mOutConversionBuffer : mOutBuffer;
810
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800812 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800813 mOutConversionBuffer->audioBuffer()->s16,
814 outChannelCount * mConfig.outputCfg.buffer.frameCount);
815 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800816 if (mOutChannelCountRequested != outChannelCount) {
817 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
818 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
819 sizeof(float),
820 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
821 }
rago94a1ee82017-07-21 15:11:02 -0700822#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700823 } else {
rago94a1ee82017-07-21 15:11:02 -0700824#ifdef FLOAT_EFFECT_CHAIN
825 data_bypass:
826#endif
827 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800828 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700829 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800830 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700831 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800832 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700833 }
834 }
835 ret = -ENODATA;
836 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800837
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 // force transition to IDLE state when engine is ready
839 if (mState == STOPPED && ret == -ENODATA) {
840 mDisableWaitCnt = 1;
841 }
842
843 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700844 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800845#ifdef FLOAT_AUX
846 const size_t size =
847 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
848#else
rago94a1ee82017-07-21 15:11:02 -0700849 const size_t size =
850 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800851#endif
rago94a1ee82017-07-21 15:11:02 -0700852 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800853 }
854 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700855 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
857 // If an insert effect is idle and input buffer is different from output buffer,
858 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700859 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700860 // similar handling with data_bypass above.
861 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
862 accumulateInputToOutput();
863 } else { // EFFECT_BUFFER_ACCESS_WRITE
864 copyInputToOutput();
865 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800866 }
867 }
868}
869
870void AudioFlinger::EffectModule::reset_l()
871{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700872 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800873 return;
874 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700875 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800876}
877
878status_t AudioFlinger::EffectModule::configure()
879{
rago94a1ee82017-07-21 15:11:02 -0700880 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882 uint32_t size;
883 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700884 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700885
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700886 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700887 status = NO_INIT;
888 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800889 }
890
Eric Laurentca7cc822012-11-19 14:55:58 -0800891 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800892 // TODO: handle configuration of input (record) SW effects above the HAL,
893 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
894 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700895 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200896 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800897 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200898 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800899
900 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800901 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
902 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
903 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
904 mConfig.inputCfg.channels);
905 }
906#ifndef MULTICHANNEL_EFFECT_CHAIN
907 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
908 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
909 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
910 mConfig.outputCfg.channels);
911 }
912#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800913 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800914#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700915 // TODO: Update this logic when multichannel effects are implemented.
916 // For offloaded tracks consider mono output as stereo for proper effect initialization
917 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
918 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
919 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
920 ALOGV("Overriding effect input and output as STEREO");
921 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800922#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800923 }
jiabineb3bda02020-06-30 14:07:03 -0700924 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700925 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700926 mConfig.inputCfg.channels |= hapticChannelMask;
927 mConfig.outputCfg.channels |= hapticChannelMask;
928 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800929 mInChannelCountRequested =
930 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
931 mOutChannelCountRequested =
932 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700933
rago94a1ee82017-07-21 15:11:02 -0700934 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
935 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900936
937 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700938 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900939 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
940 ALOGV("Overriding effect input as 48kHz");
941 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700942 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900943 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800944 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
945 mConfig.inputCfg.bufferProvider.cookie = NULL;
946 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
947 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
948 mConfig.outputCfg.bufferProvider.cookie = NULL;
949 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
950 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
951 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
952 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800953 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800954 // always overwrites output buffer: input buffer == output buffer
955 // - in other sessions:
956 // last effect in the chain accumulates in output buffer: input buffer != output buffer
957 // other effect: overwrites output buffer: input buffer == output buffer
958 // Auxiliary effect:
959 // accumulates in output buffer: input buffer != output buffer
960 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700961 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800962 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
963 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700964 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800965 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000966 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800967
Eric Laurent6b446ce2019-12-13 10:56:31 -0800968 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700969 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800970 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800971
972 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700973 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700974 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800975 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700976 &mConfig,
977 &size,
978 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700979 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800980 status = cmdStatus;
981 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800982
983#ifdef MULTICHANNEL_EFFECT_CHAIN
984 if (status != NO_ERROR &&
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000985 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800986 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
987 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
988 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700989 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
990 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800991 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
992 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
993 }
994 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
995 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
996 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
997 }
998 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700999 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -08001000 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -07001001 &mConfig,
1002 &size,
1003 &cmdStatus);
1004 if (status == NO_ERROR) {
1005 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001006 }
1007 }
1008#endif
1009
1010#ifdef FLOAT_EFFECT_CHAIN
1011 if (status == NO_ERROR) {
1012 mSupportsFloat = true;
1013 }
1014
1015 if (status != NO_ERROR) {
1016 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1017 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1018 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1019 size = sizeof(int);
1020 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1021 sizeof(mConfig),
1022 &mConfig,
1023 &size,
1024 &cmdStatus);
1025 if (status == NO_ERROR) {
1026 status = cmdStatus;
1027 }
1028 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001029 mSupportsFloat = false;
1030 ALOGVV("config worked with 16 bit");
1031 } else {
1032 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001033 }
rago94a1ee82017-07-21 15:11:02 -07001034 }
1035#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001036
rago94a1ee82017-07-21 15:11:02 -07001037 if (status == NO_ERROR) {
1038 // Establish Buffer strategy
1039 setInBuffer(mInBuffer);
1040 setOutBuffer(mOutBuffer);
1041
1042 // Update visualizer latency
1043 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1044 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1045 effect_param_t *p = (effect_param_t *)buf32;
1046
1047 p->psize = sizeof(uint32_t);
1048 p->vsize = sizeof(uint32_t);
1049 size = sizeof(int);
1050 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1051
Andy Hungfda44002021-06-03 17:23:16 -07001052 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001053
1054 *((int32_t *)p->data + 1)= latency;
1055 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1056 sizeof(effect_param_t) + 8,
1057 &buf32,
1058 &size,
1059 &cmdStatus);
1060 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001061 }
1062
Andy Hung05083ac2017-12-14 15:00:28 -08001063 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1064 mMaxDisableWaitCnt = (uint32_t)std::max(
1065 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1066 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1067 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001068
Eric Laurentd0ebb532013-04-02 16:41:41 -07001069exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001070 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001071 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001072 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001073 return status;
1074}
1075
1076status_t AudioFlinger::EffectModule::init()
1077{
1078 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001079 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 return NO_INIT;
1081 }
1082 status_t cmdStatus;
1083 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001084 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1085 0,
1086 NULL,
1087 &size,
1088 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001089 if (status == 0) {
1090 status = cmdStatus;
1091 }
1092 return status;
1093}
1094
Eric Laurent1b928682014-10-02 19:41:47 -07001095void AudioFlinger::EffectModule::addEffectToHal_l()
1096{
1097 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1098 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001099 if (mAddedToHal) {
1100 return;
1101 }
1102
Andy Hungfda44002021-06-03 17:23:16 -07001103 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001104 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001105 }
1106}
1107
Eric Laurentfa1e1232016-08-02 19:01:49 -07001108// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001109status_t AudioFlinger::EffectModule::start()
1110{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001111 status_t status;
1112 {
1113 Mutex::Autolock _l(mLock);
1114 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001115 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001116 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001117 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001118 }
1119 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001120}
1121
1122status_t AudioFlinger::EffectModule::start_l()
1123{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001124 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001125 return NO_INIT;
1126 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001127 if (mStatus != NO_ERROR) {
1128 return mStatus;
1129 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 status_t cmdStatus;
1131 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001132 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1133 0,
1134 NULL,
1135 &size,
1136 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 if (status == 0) {
1138 status = cmdStatus;
1139 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001140 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001141 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001142 }
1143 return status;
1144}
1145
1146status_t AudioFlinger::EffectModule::stop()
1147{
1148 Mutex::Autolock _l(mLock);
1149 return stop_l();
1150}
1151
1152status_t AudioFlinger::EffectModule::stop_l()
1153{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001154 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001155 return NO_INIT;
1156 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001157 if (mStatus != NO_ERROR) {
1158 return mStatus;
1159 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001160 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001161 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001162
1163 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001164 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1165 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1166 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001167 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001168 mSetVolumeReentrantTid = INVALID_PID;
1169 }
1170
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001171 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1172 0,
1173 NULL,
1174 &size,
1175 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001177 status = cmdStatus;
1178 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001179 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001180 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001181 }
1182 return status;
1183}
1184
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001185// must be called with EffectChain::mLock held
1186void AudioFlinger::EffectModule::release_l()
1187{
1188 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001189 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001190 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001191 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001192 mEffectInterface.clear();
1193 }
1194}
1195
Eric Laurent6b446ce2019-12-13 10:56:31 -08001196status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001197{
1198 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1199 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001200 if (!mAddedToHal) {
1201 return NO_ERROR;
1202 }
1203
Andy Hungfda44002021-06-03 17:23:16 -07001204 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001205 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001206 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001207 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001208}
1209
Andy Hunge4a1d912016-08-17 14:11:13 -07001210// round up delta valid if value and divisor are positive.
1211template <typename T>
1212static T roundUpDelta(const T &value, const T &divisor) {
1213 T remainder = value % divisor;
1214 return remainder == 0 ? 0 : divisor - remainder;
1215}
1216
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001217status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1218 const std::vector<uint8_t>& cmdData,
1219 int32_t maxReplySize,
1220 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001221{
1222 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001223 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001224
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001225 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001226 return NO_INIT;
1227 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001228 if (mStatus != NO_ERROR) {
1229 return mStatus;
1230 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001231 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1232 return -EINVAL;
1233 }
1234 size_t cmdSize = cmdData.size();
1235 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1236 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1237 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001238 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001240 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001241 android_errorWriteLog(0x534e4554, "33003822");
1242 return -EINVAL;
1243 }
1244 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001245 (maxReplySize < sizeof(effect_param_t) ||
1246 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001247 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001248 return -EINVAL;
1249 }
ragoe2759072016-11-22 18:02:48 -08001250 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001251 (sizeof(effect_param_t) > maxReplySize
1252 || param->psize > maxReplySize - sizeof(effect_param_t)
1253 || param->vsize > maxReplySize - sizeof(effect_param_t)
1254 - param->psize
1255 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1256 maxReplySize
1257 - sizeof(effect_param_t)
1258 - param->psize
1259 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001260 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1261 android_errorWriteLog(0x534e4554, "32705438");
1262 return -EINVAL;
1263 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001264 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001265 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1266 && // DEFERRED not generally used
1267 (param == nullptr
1268 || param->psize > cmdSize - sizeof(effect_param_t)
1269 || param->vsize > cmdSize - sizeof(effect_param_t)
1270 - param->psize
1271 || roundUpDelta(param->psize,
1272 (uint32_t) sizeof(int)) >
1273 cmdSize
1274 - sizeof(effect_param_t)
1275 - param->psize
1276 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001277 android_errorWriteLog(0x534e4554, "30204301");
1278 return -EINVAL;
1279 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001280 uint32_t replySize = maxReplySize;
1281 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001282 status_t status = mEffectInterface->command(cmdCode,
1283 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001284 const_cast<uint8_t*>(cmdData.data()),
1285 &replySize,
1286 reply->data());
1287 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001288 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001289 for (size_t i = 1; i < mHandles.size(); i++) {
1290 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001291 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001292 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001293 }
1294 }
1295 }
1296 return status;
1297}
1298
Eric Laurentca7cc822012-11-19 14:55:58 -08001299bool AudioFlinger::EffectModule::isProcessEnabled() const
1300{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001301 if (mStatus != NO_ERROR) {
1302 return false;
1303 }
1304
Eric Laurentca7cc822012-11-19 14:55:58 -08001305 switch (mState) {
1306 case RESTART:
1307 case ACTIVE:
1308 case STOPPING:
1309 case STOPPED:
1310 return true;
1311 case IDLE:
1312 case STARTING:
1313 case DESTROYED:
1314 default:
1315 return false;
1316 }
1317}
1318
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001319bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1320{
Andy Hungfda44002021-06-03 17:23:16 -07001321 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001322}
1323
1324bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1325{
1326 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1327}
1328
Mikhail Naganov022b9952017-01-04 16:36:51 -08001329void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001330 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001331
1332 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001333 if (buffer != 0) {
1334 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1335 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1336 } else {
1337 mConfig.inputCfg.buffer.raw = NULL;
1338 }
1339 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001340 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001341
1342#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001343 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001344 // Theoretically insert effects can also do in-place conversions (destroying
1345 // the original buffer) when the output buffer is identical to the input buffer,
1346 // but we don't optimize for it here.
1347 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001348 const uint32_t inChannelCount =
1349 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1350 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001351 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001352 // we need to translate - create hidl shared buffer and intercept
1353 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001354 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1355 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1356 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001357
1358 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1359 __func__, inChannels, inFrameCount, size);
1360
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001361 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001362 || size > mInConversionBuffer->getSize())) {
1363 mInConversionBuffer.clear();
1364 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001365 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001366 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001367 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001368 mInConversionBuffer->setFrameCount(inFrameCount);
1369 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001370 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001371 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001372 }
1373 }
1374#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001375}
1376
1377void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001378 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001379
1380 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001381 if (buffer != 0) {
1382 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1383 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1384 } else {
1385 mConfig.outputCfg.buffer.raw = NULL;
1386 }
1387 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001388 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001389
1390#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001391 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001392 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001393 const uint32_t outChannelCount =
1394 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1395 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001396 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001397 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001398 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1399 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1400 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001401
1402 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1403 __func__, outChannels, outFrameCount, size);
1404
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001405 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001406 || size > mOutConversionBuffer->getSize())) {
1407 mOutConversionBuffer.clear();
1408 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001409 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001410 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001411 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001412 mOutConversionBuffer->setFrameCount(outFrameCount);
1413 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001414 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001415 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001416 }
1417 }
1418#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001419}
1420
Eric Laurentca7cc822012-11-19 14:55:58 -08001421status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1422{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001423 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001424 if (mStatus != NO_ERROR) {
1425 return mStatus;
1426 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1429 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1430 if (isProcessEnabled() &&
1431 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001432 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1433 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001434 uint32_t volume[2];
1435 uint32_t *pVolume = NULL;
1436 uint32_t size = sizeof(volume);
1437 volume[0] = *left;
1438 volume[1] = *right;
1439 if (controller) {
1440 pVolume = volume;
1441 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001442 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1443 size,
1444 volume,
1445 &size,
1446 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1448 *left = volume[0];
1449 *right = volume[1];
1450 }
1451 }
1452 return status;
1453}
1454
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001455void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1456{
Zhou Songd505c642020-02-20 16:35:37 +08001457 // for offload or direct thread, if the effect chain has non-offloadable
1458 // effect and any effect module within the chain has volume control, then
1459 // volume control is delegated to effect, otherwise, set volume to hal.
1460 if (mEffectCallback->isOffloadOrDirect() &&
1461 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001462 float vol_l = (float)left / (1 << 24);
1463 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001464 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001465 }
1466}
1467
jiabin8f278ee2019-11-11 12:16:27 -08001468status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1469 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001470{
jiabin8f278ee2019-11-11 12:16:27 -08001471 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1472 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 return NO_ERROR;
1474 }
1475
1476 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001477 if (mStatus != NO_ERROR) {
1478 return mStatus;
1479 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001481 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 status_t cmdStatus;
1483 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001484 // FIXME: use audio device types and addresses when the hal interface is ready.
1485 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001486 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001487 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001488 &size,
1489 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001490 }
1491 return status;
1492}
1493
jiabin8f278ee2019-11-11 12:16:27 -08001494status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1495{
1496 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1497}
1498
1499status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1500{
1501 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1502}
1503
Eric Laurentca7cc822012-11-19 14:55:58 -08001504status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1505{
1506 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001507 if (mStatus != NO_ERROR) {
1508 return mStatus;
1509 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 status_t status = NO_ERROR;
1511 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1512 status_t cmdStatus;
1513 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001514 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1515 sizeof(audio_mode_t),
1516 &mode,
1517 &size,
1518 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001519 if (status == NO_ERROR) {
1520 status = cmdStatus;
1521 }
1522 }
1523 return status;
1524}
1525
1526status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1527{
1528 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001529 if (mStatus != NO_ERROR) {
1530 return mStatus;
1531 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001532 status_t status = NO_ERROR;
1533 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1534 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001535 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1536 sizeof(audio_source_t),
1537 &source,
1538 &size,
1539 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 }
1541 return status;
1542}
1543
Eric Laurent5baf2af2013-09-12 17:37:00 -07001544status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1545{
1546 Mutex::Autolock _l(mLock);
1547 if (mStatus != NO_ERROR) {
1548 return mStatus;
1549 }
1550 status_t status = NO_ERROR;
1551 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1552 status_t cmdStatus;
1553 uint32_t size = sizeof(status_t);
1554 effect_offload_param_t cmd;
1555
1556 cmd.isOffload = offloaded;
1557 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001558 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1559 sizeof(effect_offload_param_t),
1560 &cmd,
1561 &size,
1562 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001563 if (status == NO_ERROR) {
1564 status = cmdStatus;
1565 }
1566 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1567 } else {
1568 if (offloaded) {
1569 status = INVALID_OPERATION;
1570 }
1571 mOffloaded = false;
1572 }
1573 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1574 return status;
1575}
1576
1577bool AudioFlinger::EffectModule::isOffloaded() const
1578{
1579 Mutex::Autolock _l(mLock);
1580 return mOffloaded;
1581}
1582
jiabineb3bda02020-06-30 14:07:03 -07001583/*static*/
1584bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1585 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1586}
1587
1588bool AudioFlinger::EffectModule::isHapticGenerator() const {
1589 return isHapticGenerator(&mDescriptor.type);
1590}
1591
jiabine70bc7f2020-06-30 22:07:55 -07001592status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1593{
1594 if (mStatus != NO_ERROR) {
1595 return mStatus;
1596 }
1597 if (!isHapticGenerator()) {
1598 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1599 return INVALID_OPERATION;
1600 }
1601
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001602 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1603 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001604 param->psize = sizeof(int32_t);
1605 param->vsize = sizeof(int32_t) * 2;
1606 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1607 *((int32_t*)param->data + 1) = id;
1608 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001609 std::vector<uint8_t> response;
1610 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001611 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001612 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1613 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001614 }
1615 return status;
1616}
1617
Lais Andradebc3f37a2021-07-02 00:13:19 +01001618status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001619{
1620 if (mStatus != NO_ERROR) {
1621 return mStatus;
1622 }
1623 if (!isHapticGenerator()) {
1624 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1625 return INVALID_OPERATION;
1626 }
1627
Lais Andradebc3f37a2021-07-02 00:13:19 +01001628 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001629 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001630 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001631 effect_param_t *param = (effect_param_t*) request.data();
1632 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001633 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001634 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1635 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001636 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1637 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1638 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001639 std::vector<uint8_t> response;
1640 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1641 if (status == NO_ERROR) {
1642 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1643 status = *reinterpret_cast<const status_t*>(response.data());
1644 }
1645 return status;
1646}
1647
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001648status_t AudioFlinger::EffectModule::getConfigs(
1649 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1650 Mutex::Autolock _l(mLock);
1651 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1652 return NO_INIT;
1653 }
1654 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1655 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1656 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1657 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1658 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1659 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1660 *isOutput = mIsOutput;
1661 return NO_ERROR;
1662}
1663
Andy Hungbded9c82017-11-30 18:47:35 -08001664static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1665 std::stringstream ss;
1666
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001667 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001668 return "nullptr"; // make different than below
1669 } else if (buffer->externalData() != nullptr) {
1670 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1671 << " -> "
1672 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1673 } else {
1674 ss << buffer->audioBuffer()->raw;
1675 }
1676 return ss.str();
1677}
Marco Nelissenb2208842014-02-07 14:00:50 -08001678
Eric Laurent41709552019-12-16 19:34:05 -08001679void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001680{
Eric Laurent41709552019-12-16 19:34:05 -08001681 EffectBase::dump(fd, args);
1682
Eric Laurentca7cc822012-11-19 14:55:58 -08001683 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001685
Eric Laurent41709552019-12-16 19:34:05 -08001686 result.append("\t\tStatus Engine:\n");
1687 result.appendFormat("\t\t%03d %p\n",
1688 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001689
1690 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001691
1692 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001693 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1694 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1695 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 mConfig.inputCfg.buffer.frameCount,
1697 mConfig.inputCfg.samplingRate,
1698 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001699 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001700 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001701
1702 result.append("\t\t- Output configuration:\n");
1703 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001704 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001705 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 mConfig.outputCfg.buffer.frameCount,
1707 mConfig.outputCfg.samplingRate,
1708 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001709 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001710 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001711
rago94a1ee82017-07-21 15:11:02 -07001712#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001713
Andy Hungbded9c82017-11-30 18:47:35 -08001714 result.appendFormat("\t\t- HAL buffers:\n"
1715 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1716 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1717 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1718 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1719 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001720#endif
1721
Eric Laurentca7cc822012-11-19 14:55:58 -08001722 write(fd, result.string(), result.length());
1723
Mikhail Naganov4d547672019-02-22 14:19:19 -08001724 if (mEffectInterface != 0) {
1725 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1726 (void)mEffectInterface->dump(fd);
1727 }
1728
Eric Laurentca7cc822012-11-19 14:55:58 -08001729 if (locked) {
1730 mLock.unlock();
1731 }
1732}
1733
1734// ----------------------------------------------------------------------------
1735// EffectHandle implementation
1736// ----------------------------------------------------------------------------
1737
1738#undef LOG_TAG
1739#define LOG_TAG "AudioFlinger::EffectHandle"
1740
Eric Laurent41709552019-12-16 19:34:05 -08001741AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001742 const sp<AudioFlinger::Client>& client,
1743 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001744 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001745 : BnEffect(),
1746 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001747 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1748 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001749{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001750 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001751
1752 if (client == 0) {
1753 return;
1754 }
1755 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1756 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001757 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001758 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001759 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001761 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001762 return;
1763 }
Glenn Kastene75da402013-11-20 13:54:52 -08001764 new(mCblk) effect_param_cblk_t();
1765 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001766}
1767
1768AudioFlinger::EffectHandle::~EffectHandle()
1769{
1770 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 disconnect(false);
1772}
1773
Andy Hungc747c532022-03-07 21:41:14 -08001774// Creates an association between Binder code to name for IEffect.
1775#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1776BINDER_METHOD_ENTRY(enable) \
1777BINDER_METHOD_ENTRY(disable) \
1778BINDER_METHOD_ENTRY(command) \
1779BINDER_METHOD_ENTRY(disconnect) \
1780BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001781BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001782
1783// singleton for Binder Method Statistics for IEffect
1784mediautils::MethodStatistics<int>& getIEffectStatistics() {
1785 using Code = int;
1786
1787#pragma push_macro("BINDER_METHOD_ENTRY")
1788#undef BINDER_METHOD_ENTRY
1789#define BINDER_METHOD_ENTRY(ENTRY) \
1790 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1791
1792 static mediautils::MethodStatistics<Code> methodStatistics{
1793 IEFFECT_BINDER_METHOD_MACRO_LIST
1794 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1795 };
1796#pragma pop_macro("BINDER_METHOD_ENTRY")
1797
1798 return methodStatistics;
1799}
1800
1801status_t AudioFlinger::EffectHandle::onTransact(
1802 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001803 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1804 mediautils::TimeCheck check(
1805 std::string("IEffect::").append(methodName),
1806 [code](bool timeout, float elapsedMs) {
1807 if (timeout) {
1808 ; // we don't timeout right now on the effect interface.
1809 } else {
1810 getIEffectStatistics().event(code, elapsedMs);
1811 }
1812 }, 0 /* timeoutMs */);
Andy Hungc747c532022-03-07 21:41:14 -08001813 return BnEffect::onTransact(code, data, reply, flags);
1814}
1815
Glenn Kastene75da402013-11-20 13:54:52 -08001816status_t AudioFlinger::EffectHandle::initCheck()
1817{
1818 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1819}
1820
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001821#define RETURN(code) \
1822 *_aidl_return = (code); \
1823 return Status::ok();
1824
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001825#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1826 ({ \
1827 auto _tmp = (exp); \
1828 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1829 std::move(_tmp.value()); \
1830 })
1831
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001832Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001833{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001834 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001836 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001837 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001838 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001839 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001842 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001843
1844 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001845 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001846 }
1847
1848 mEnabled = true;
1849
Eric Laurent6c796322019-04-09 14:13:17 -07001850 status_t status = effect->updatePolicyState();
1851 if (status != NO_ERROR) {
1852 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001854 }
1855
Eric Laurent6b446ce2019-12-13 10:56:31 -08001856 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001857
1858 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001859 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001860 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 }
1862
Eric Laurent6b446ce2019-12-13 10:56:31 -08001863 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 mEnabled = false;
1866 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001868}
1869
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001870Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001871{
1872 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001873 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001874 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001875 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001876 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001877 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001879 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001881
1882 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001883 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 }
1885 mEnabled = false;
1886
Eric Laurent6c796322019-04-09 14:13:17 -07001887 effect->updatePolicyState();
1888
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001889 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001891 }
1892
Eric Laurent6b446ce2019-12-13 10:56:31 -08001893 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001894 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001895}
1896
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001898{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001899 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001902}
1903
1904void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1905{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001906 AutoMutex _l(mLock);
1907 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1908 if (mDisconnected) {
1909 if (unpinIfLast) {
1910 android_errorWriteLog(0x534e4554, "32707507");
1911 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001912 return;
1913 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001914 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001915 {
Eric Laurent41709552019-12-16 19:34:05 -08001916 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001918 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001919 ALOGW("%s Effect handle %p disconnected after thread destruction",
1920 __func__, this);
1921 }
1922 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001923 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001924 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001925
Eric Laurentca7cc822012-11-19 14:55:58 -08001926 if (mClient != 0) {
1927 if (mCblk != NULL) {
1928 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1929 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1930 }
1931 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001932 // Client destructor must run with AudioFlinger client mutex locked
1933 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 mClient.clear();
1935 }
1936}
1937
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001938Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1939 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1940 return Status::ok();
1941}
1942
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001943Status AudioFlinger::EffectHandle::getConfig(
1944 media::EffectConfig* _config, int32_t* _aidl_return) {
1945 AutoMutex _l(mLock);
1946 sp<EffectBase> effect = mEffect.promote();
1947 if (effect == nullptr || mDisconnected) {
1948 RETURN(DEAD_OBJECT);
1949 }
1950 sp<EffectModule> effectModule = effect->asEffectModule();
1951 if (effectModule == nullptr) {
1952 RETURN(INVALID_OPERATION);
1953 }
1954 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1955 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1956 bool isOutput;
1957 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1958 if (status == NO_ERROR) {
1959 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1960 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1961 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1962 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1963 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1964 _config->isOnInputStream = !isOutput;
1965 }
1966 RETURN(status);
1967}
1968
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001969Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1970 const std::vector<uint8_t>& cmdData,
1971 int32_t maxResponseSize,
1972 std::vector<uint8_t>* response,
1973 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001974{
1975 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001976 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001977
Eric Laurentc7ab3092017-06-15 18:43:46 -07001978 // reject commands reserved for internal use by audio framework if coming from outside
1979 // of audioserver
1980 switch(cmdCode) {
1981 case EFFECT_CMD_ENABLE:
1982 case EFFECT_CMD_DISABLE:
1983 case EFFECT_CMD_SET_PARAM:
1984 case EFFECT_CMD_SET_PARAM_DEFERRED:
1985 case EFFECT_CMD_SET_PARAM_COMMIT:
1986 case EFFECT_CMD_GET_PARAM:
1987 break;
1988 default:
1989 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1990 break;
1991 }
1992 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001993 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001994 }
1995
Eric Laurent1ffc5852016-12-15 14:46:09 -08001996 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001997 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001998 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001999 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002000 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002001 writeToBuffer(NO_ERROR, response);
2002 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002003 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002004 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002005 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002006 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002007 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 writeToBuffer(NO_ERROR, response);
2009 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002010 }
2011
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002012 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002013 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002014 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002015 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002016 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002017 // only get parameter command is permitted for applications not controlling the effect
2018 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002020 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002021
2022 // handle commands that are not forwarded transparently to effect engine
2023 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002024 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002025 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002026 }
2027
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002028 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002029 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002030 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002031 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002032 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002033
Eric Laurentca7cc822012-11-19 14:55:58 -08002034 // No need to trylock() here as this function is executed in the binder thread serving a
2035 // particular client process: no risk to block the whole media server process or mixer
2036 // threads if we are stuck here
2037 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002038 // keep local copy of index in case of client corruption b/32220769
2039 const uint32_t clientIndex = mCblk->clientIndex;
2040 const uint32_t serverIndex = mCblk->serverIndex;
2041 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2042 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 mCblk->serverIndex = 0;
2044 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002045 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002046 }
2047 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002048 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002049 for (uint32_t index = serverIndex; index < clientIndex;) {
2050 int *p = (int *)(mBuffer + index);
2051 const int size = *p++;
2052 if (size < 0
2053 || size > EFFECT_PARAM_BUFFER_SIZE
2054 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002055 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002056 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002057 break;
2058 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002059
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002060 std::copy(reinterpret_cast<const uint8_t*>(p),
2061 reinterpret_cast<const uint8_t*>(p) + size,
2062 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002063
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002064 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002065 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002066 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002067 sizeof(int),
2068 &replyBuffer);
2069 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002070
2071 // verify shared memory: server index shouldn't change; client index can't go back.
2072 if (serverIndex != mCblk->serverIndex
2073 || clientIndex > mCblk->clientIndex) {
2074 android_errorWriteLog(0x534e4554, "32220769");
2075 status = BAD_VALUE;
2076 break;
2077 }
2078
Eric Laurentca7cc822012-11-19 14:55:58 -08002079 // stop at first error encountered
2080 if (ret != NO_ERROR) {
2081 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002082 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002083 break;
2084 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002085 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002086 break;
2087 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002088 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002089 }
2090 mCblk->serverIndex = 0;
2091 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002092 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002093 }
2094
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002095 status_t status = effect->command(cmdCode,
2096 cmdData,
2097 maxResponseSize,
2098 response);
2099 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002100}
2101
2102void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2103{
2104 ALOGV("setControl %p control %d", this, hasControl);
2105
2106 mHasControl = hasControl;
2107 mEnabled = enabled;
2108
2109 if (signal && mEffectClient != 0) {
2110 mEffectClient->controlStatusChanged(hasControl);
2111 }
2112}
2113
2114void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002115 const std::vector<uint8_t>& cmdData,
2116 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002117{
2118 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002119 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002120 }
2121}
2122
2123
2124
2125void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2126{
2127 if (mEffectClient != 0) {
2128 mEffectClient->enableStatusChanged(enabled);
2129 }
2130}
2131
Eric Laurentde8caf42021-08-11 17:19:25 +02002132void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2133{
2134 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2135 mEffectClient->framesProcessed(frames);
2136 }
2137}
2138
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002139void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002140{
2141 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2142
Marco Nelissenb2208842014-02-07 14:00:50 -08002143 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002144 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002145 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002146 mHasControl ? "yes" : "no",
2147 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002148 mCblk ? mCblk->clientIndex : 0,
2149 mCblk ? mCblk->serverIndex : 0
2150 );
2151
2152 if (locked) {
2153 mCblk->lock.unlock();
2154 }
2155}
2156
2157#undef LOG_TAG
2158#define LOG_TAG "AudioFlinger::EffectChain"
2159
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002160AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2161 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002162 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002163 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002164 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002165 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002166{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002167 sp<ThreadBase> p = thread.promote();
2168 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002169 return;
2170 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002171 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002172 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2173 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002174}
2175
2176AudioFlinger::EffectChain::~EffectChain()
2177{
Eric Laurentca7cc822012-11-19 14:55:58 -08002178}
2179
2180// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2181sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2182 effect_descriptor_t *descriptor)
2183{
2184 size_t size = mEffects.size();
2185
2186 for (size_t i = 0; i < size; i++) {
2187 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2188 return mEffects[i];
2189 }
2190 }
2191 return 0;
2192}
2193
2194// getEffectFromId_l() must be called with ThreadBase::mLock held
2195sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2196{
2197 size_t size = mEffects.size();
2198
2199 for (size_t i = 0; i < size; i++) {
2200 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2201 if (id == 0 || mEffects[i]->id() == id) {
2202 return mEffects[i];
2203 }
2204 }
2205 return 0;
2206}
2207
2208// getEffectFromType_l() must be called with ThreadBase::mLock held
2209sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2210 const effect_uuid_t *type)
2211{
2212 size_t size = mEffects.size();
2213
2214 for (size_t i = 0; i < size; i++) {
2215 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2216 return mEffects[i];
2217 }
2218 }
2219 return 0;
2220}
2221
Eric Laurent6c796322019-04-09 14:13:17 -07002222std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2223{
2224 std::vector<int> ids;
2225 Mutex::Autolock _l(mLock);
2226 for (size_t i = 0; i < mEffects.size(); i++) {
2227 ids.push_back(mEffects[i]->id());
2228 }
2229 return ids;
2230}
2231
Eric Laurentca7cc822012-11-19 14:55:58 -08002232void AudioFlinger::EffectChain::clearInputBuffer()
2233{
2234 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002235 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002236}
2237
2238// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002239void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002240{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002241 if (mInBuffer == NULL) {
2242 return;
2243 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002244 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2245 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002246
Eric Laurent6b446ce2019-12-13 10:56:31 -08002247 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002248 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002249}
2250
2251// Must be called with EffectChain::mLock locked
2252void AudioFlinger::EffectChain::process_l()
2253{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002254 // never process effects when:
2255 // - on an OFFLOAD thread
2256 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002257 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002258 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002259 bool tracksOnSession = (trackCnt() != 0);
2260
2261 if (!tracksOnSession && mTailBufferCount == 0) {
2262 doProcess = false;
2263 }
2264
2265 if (activeTrackCnt() == 0) {
2266 // if no track is active and the effect tail has not been rendered,
2267 // the input buffer must be cleared here as the mixer process will not do it
2268 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002269 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002270 if (mTailBufferCount > 0) {
2271 mTailBufferCount--;
2272 }
2273 }
2274 }
2275 }
2276
2277 size_t size = mEffects.size();
2278 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002279 // Only the input and output buffers of the chain can be external,
2280 // and 'update' / 'commit' do nothing for allocated buffers, thus
2281 // it's not needed to consider any other buffers here.
2282 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002283 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2284 mOutBuffer->update();
2285 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002286 for (size_t i = 0; i < size; i++) {
2287 mEffects[i]->process();
2288 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002289 mInBuffer->commit();
2290 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2291 mOutBuffer->commit();
2292 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002293 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002294 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002295 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002296 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2297 }
2298 if (doResetVolume) {
2299 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002300 }
2301}
2302
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002303// createEffect_l() must be called with ThreadBase::mLock held
2304status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002305 effect_descriptor_t *desc,
2306 int id,
2307 audio_session_t sessionId,
2308 bool pinned)
2309{
2310 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002311 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002312 status_t lStatus = effect->status();
2313 if (lStatus == NO_ERROR) {
2314 lStatus = addEffect_ll(effect);
2315 }
2316 if (lStatus != NO_ERROR) {
2317 effect.clear();
2318 }
2319 return lStatus;
2320}
2321
2322// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002323status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2324{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002325 Mutex::Autolock _l(mLock);
2326 return addEffect_ll(effect);
2327}
2328// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2329status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2330{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002331 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002332
Eric Laurentb62d0362021-10-26 17:40:18 +02002333 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002334 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2335 // Auxiliary effects are inserted at the beginning of mEffects vector as
2336 // they are processed first and accumulated in chain input buffer
2337 mEffects.insertAt(effect, 0);
2338
2339 // the input buffer for auxiliary effect contains mono samples in
2340 // 32 bit format. This is to avoid saturation in AudoMixer
2341 // accumulation stage. Saturation is done in EffectModule::process() before
2342 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002343 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002344 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002345#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002346 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002347 numSamples * sizeof(float), &halBuffer);
2348#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002349 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002350 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002351#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002352 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002353
2354 effect->configure();
2355
Mikhail Naganov022b9952017-01-04 16:36:51 -08002356 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002357 // auxiliary effects output samples to chain input buffer for further processing
2358 // by insert effects
2359 effect->setOutBuffer(mInBuffer);
2360 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002361 ssize_t idx_insert = getInsertIndex(desc);
2362 if (idx_insert < 0) {
2363 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002364 }
2365
Eric Laurentb62d0362021-10-26 17:40:18 +02002366 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002367 mEffects.insertAt(effect, idx_insert);
2368
2369 effect->configure();
2370
Eric Laurentb62d0362021-10-26 17:40:18 +02002371 // - By default:
2372 // All effects read samples from chain input buffer.
2373 // The last effect in the chain, writes samples to chain output buffer,
2374 // otherwise to chain input buffer
2375 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2376 // The spatializer effect (first effect) reads samples from the input buffer
2377 // and writes samples to the output buffer.
2378 // All other effects read and writes samples to the output buffer
2379 if (mEffectCallback->isSpatializer()
2380 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002382 if (idx_insert == 0) {
2383 if (previousSize != 0) {
2384 mEffects[1]->configure();
2385 mEffects[1]->setInBuffer(mOutBuffer);
2386 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2387 }
2388 effect->setInBuffer(mInBuffer);
2389 } else {
2390 effect->setInBuffer(mOutBuffer);
2391 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002392 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002393 effect->setInBuffer(mInBuffer);
2394 if (idx_insert == previousSize) {
2395 if (idx_insert != 0) {
2396 mEffects[idx_insert-1]->configure();
2397 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2398 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2399 }
2400 effect->setOutBuffer(mOutBuffer);
2401 } else {
2402 effect->setOutBuffer(mInBuffer);
2403 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002404 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002405 ALOGV("%s effect %p, added in chain %p at rank %zu",
2406 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002407 }
2408 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002409
Eric Laurentca7cc822012-11-19 14:55:58 -08002410 return NO_ERROR;
2411}
2412
Eric Laurentb62d0362021-10-26 17:40:18 +02002413ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2414 // Insert effects are inserted at the end of mEffects vector as they are processed
2415 // after track and auxiliary effects.
2416 // Insert effect order as a function of indicated preference:
2417 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2418 // another effect is present
2419 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2420 // last effect claiming first position
2421 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2422 // first effect claiming last position
2423 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2424 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2425 // already present
2426 // Spatializer or Downmixer effects are inserted in first position because
2427 // they adapt the channel count for all other effects in the chain
2428 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2429 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2430 return 0;
2431 }
2432
2433 size_t size = mEffects.size();
2434 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2435 ssize_t idx_insert;
2436 ssize_t idx_insert_first = -1;
2437 ssize_t idx_insert_last = -1;
2438
2439 idx_insert = size;
2440 for (size_t i = 0; i < size; i++) {
2441 effect_descriptor_t d = mEffects[i]->desc();
2442 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2443 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2444 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2445 // check invalid effect chaining combinations
2446 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2447 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2448 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2449 __func__, desc.name, d.name);
2450 return -1;
2451 }
2452 // remember position of first insert effect and by default
2453 // select this as insert position for new effect
2454 if (idx_insert == size) {
2455 idx_insert = i;
2456 }
2457 // remember position of last insert effect claiming
2458 // first position
2459 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2460 idx_insert_first = i;
2461 }
2462 // remember position of first insert effect claiming
2463 // last position
2464 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2465 idx_insert_last == -1) {
2466 idx_insert_last = i;
2467 }
2468 }
2469 }
2470
2471 // modify idx_insert from first position if needed
2472 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2473 if (idx_insert_last != -1) {
2474 idx_insert = idx_insert_last;
2475 } else {
2476 idx_insert = size;
2477 }
2478 } else {
2479 if (idx_insert_first != -1) {
2480 idx_insert = idx_insert_first + 1;
2481 }
2482 }
2483 return idx_insert;
2484}
2485
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002486// removeEffect_l() must be called with ThreadBase::mLock held
2487size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2488 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002489{
2490 Mutex::Autolock _l(mLock);
2491 size_t size = mEffects.size();
2492 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2493
2494 for (size_t i = 0; i < size; i++) {
2495 if (effect == mEffects[i]) {
2496 // calling stop here will remove pre-processing effect from the audio HAL.
2497 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2498 // the middle of a read from audio HAL
2499 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2500 mEffects[i]->state() == EffectModule::STOPPING) {
2501 mEffects[i]->stop();
2502 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002503 if (release) {
2504 mEffects[i]->release_l();
2505 }
2506
Mikhail Naganov022b9952017-01-04 16:36:51 -08002507 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002508 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002509 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002510 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002511 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002512 }
2513 }
2514 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002515
2516 // make sure the input buffer configuration for the new first effect in the chain
2517 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2518 if (i == 0 && size > 1) {
2519 mEffects[0]->configure();
2520 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002521 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002522 }
2523
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002524 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002525 this, i);
2526 break;
2527 }
2528 }
2529
2530 return mEffects.size();
2531}
2532
jiabin8f278ee2019-11-11 12:16:27 -08002533// setDevices_l() must be called with ThreadBase::mLock held
2534void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002535{
2536 size_t size = mEffects.size();
2537 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002538 mEffects[i]->setDevices(devices);
2539 }
2540}
2541
2542// setInputDevice_l() must be called with ThreadBase::mLock held
2543void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2544{
2545 size_t size = mEffects.size();
2546 for (size_t i = 0; i < size; i++) {
2547 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002548 }
2549}
2550
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002551// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002552void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2553{
2554 size_t size = mEffects.size();
2555 for (size_t i = 0; i < size; i++) {
2556 mEffects[i]->setMode(mode);
2557 }
2558}
2559
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002560// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002561void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2562{
2563 size_t size = mEffects.size();
2564 for (size_t i = 0; i < size; i++) {
2565 mEffects[i]->setAudioSource(source);
2566 }
2567}
2568
Zhou Songd505c642020-02-20 16:35:37 +08002569bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2570 for (const auto &effect : mEffects) {
2571 if (effect->isVolumeControlEnabled()) return true;
2572 }
2573 return false;
2574}
2575
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002576// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002577bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002578{
2579 uint32_t newLeft = *left;
2580 uint32_t newRight = *right;
2581 bool hasControl = false;
2582 int ctrlIdx = -1;
2583 size_t size = mEffects.size();
2584
2585 // first update volume controller
2586 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002587 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002588 ctrlIdx = i - 1;
2589 hasControl = true;
2590 break;
2591 }
2592 }
2593
Eric Laurentfa1e1232016-08-02 19:01:49 -07002594 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002595 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002596 if (hasControl) {
2597 *left = mNewLeftVolume;
2598 *right = mNewRightVolume;
2599 }
2600 return hasControl;
2601 }
2602
2603 mVolumeCtrlIdx = ctrlIdx;
2604 mLeftVolume = newLeft;
2605 mRightVolume = newRight;
2606
2607 // second get volume update from volume controller
2608 if (ctrlIdx >= 0) {
2609 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2610 mNewLeftVolume = newLeft;
2611 mNewRightVolume = newRight;
2612 }
2613 // then indicate volume to all other effects in chain.
2614 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002615 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002616 uint32_t lVol = newLeft;
2617 uint32_t rVol = newRight;
2618
2619 for (size_t i = 0; i < size; i++) {
2620 if ((int)i == ctrlIdx) {
2621 continue;
2622 }
2623 // this also works for ctrlIdx == -1 when there is no volume controller
2624 if ((int)i > ctrlIdx) {
2625 lVol = *left;
2626 rVol = *right;
2627 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002628 // Pass requested volume directly if this is volume monitor module
2629 if (mEffects[i]->isVolumeMonitor()) {
2630 mEffects[i]->setVolume(left, right, false);
2631 } else {
2632 mEffects[i]->setVolume(&lVol, &rVol, false);
2633 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002634 }
2635 *left = newLeft;
2636 *right = newRight;
2637
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002638 setVolumeForOutput_l(*left, *right);
2639
Eric Laurentca7cc822012-11-19 14:55:58 -08002640 return hasControl;
2641}
2642
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002643// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002644void AudioFlinger::EffectChain::resetVolume_l()
2645{
Eric Laurente7449bf2016-08-03 18:44:07 -07002646 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2647 uint32_t left = mLeftVolume;
2648 uint32_t right = mRightVolume;
2649 (void)setVolume_l(&left, &right, true);
2650 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002651}
2652
jiabineb3bda02020-06-30 14:07:03 -07002653// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2654bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2655{
2656 for (size_t i = 0; i < mEffects.size(); ++i) {
2657 if (mEffects[i]->isHapticGenerator()) {
2658 return true;
2659 }
2660 }
2661 return false;
2662}
2663
jiabine70bc7f2020-06-30 22:07:55 -07002664void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2665{
2666 Mutex::Autolock _l(mLock);
2667 for (size_t i = 0; i < mEffects.size(); ++i) {
2668 mEffects[i]->setHapticIntensity(id, intensity);
2669 }
2670}
2671
Eric Laurent1b928682014-10-02 19:41:47 -07002672void AudioFlinger::EffectChain::syncHalEffectsState()
2673{
2674 Mutex::Autolock _l(mLock);
2675 for (size_t i = 0; i < mEffects.size(); i++) {
2676 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2677 mEffects[i]->state() == EffectModule::STOPPING) {
2678 mEffects[i]->addEffectToHal_l();
2679 }
2680 }
2681}
2682
Eric Laurentca7cc822012-11-19 14:55:58 -08002683void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2684{
Eric Laurentca7cc822012-11-19 14:55:58 -08002685 String8 result;
2686
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002687 const size_t numEffects = mEffects.size();
2688 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002689
Marco Nelissenb2208842014-02-07 14:00:50 -08002690 if (numEffects) {
2691 bool locked = AudioFlinger::dumpTryLock(mLock);
2692 // failed to lock - AudioFlinger is probably deadlocked
2693 if (!locked) {
2694 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002695 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002696
Andy Hungbded9c82017-11-30 18:47:35 -08002697 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2698 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2699 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2700 (int)inBufferStr.size(), "In buffer ",
2701 (int)outBufferStr.size(), "Out buffer ");
2702 result.appendFormat("\t%s %s %d\n",
2703 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002704 write(fd, result.string(), result.size());
2705
2706 for (size_t i = 0; i < numEffects; ++i) {
2707 sp<EffectModule> effect = mEffects[i];
2708 if (effect != 0) {
2709 effect->dump(fd, args);
2710 }
2711 }
2712
2713 if (locked) {
2714 mLock.unlock();
2715 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002716 } else {
2717 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002718 }
2719}
2720
2721// must be called with ThreadBase::mLock held
2722void AudioFlinger::EffectChain::setEffectSuspended_l(
2723 const effect_uuid_t *type, bool suspend)
2724{
2725 sp<SuspendedEffectDesc> desc;
2726 // use effect type UUID timelow as key as there is no real risk of identical
2727 // timeLow fields among effect type UUIDs.
2728 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2729 if (suspend) {
2730 if (index >= 0) {
2731 desc = mSuspendedEffects.valueAt(index);
2732 } else {
2733 desc = new SuspendedEffectDesc();
2734 desc->mType = *type;
2735 mSuspendedEffects.add(type->timeLow, desc);
2736 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2737 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002738
Eric Laurentca7cc822012-11-19 14:55:58 -08002739 if (desc->mRefCount++ == 0) {
2740 sp<EffectModule> effect = getEffectIfEnabled(type);
2741 if (effect != 0) {
2742 desc->mEffect = effect;
2743 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002744 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002745 }
2746 }
2747 } else {
2748 if (index < 0) {
2749 return;
2750 }
2751 desc = mSuspendedEffects.valueAt(index);
2752 if (desc->mRefCount <= 0) {
2753 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002754 desc->mRefCount = 0;
2755 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002756 }
2757 if (--desc->mRefCount == 0) {
2758 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2759 if (desc->mEffect != 0) {
2760 sp<EffectModule> effect = desc->mEffect.promote();
2761 if (effect != 0) {
2762 effect->setSuspended(false);
2763 effect->lock();
2764 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002765 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002766 effect->setEnabled_l(handle->enabled());
2767 }
2768 effect->unlock();
2769 }
2770 desc->mEffect.clear();
2771 }
2772 mSuspendedEffects.removeItemsAt(index);
2773 }
2774 }
2775}
2776
2777// must be called with ThreadBase::mLock held
2778void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2779{
2780 sp<SuspendedEffectDesc> desc;
2781
2782 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2783 if (suspend) {
2784 if (index >= 0) {
2785 desc = mSuspendedEffects.valueAt(index);
2786 } else {
2787 desc = new SuspendedEffectDesc();
2788 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2789 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2790 }
2791 if (desc->mRefCount++ == 0) {
2792 Vector< sp<EffectModule> > effects;
2793 getSuspendEligibleEffects(effects);
2794 for (size_t i = 0; i < effects.size(); i++) {
2795 setEffectSuspended_l(&effects[i]->desc().type, true);
2796 }
2797 }
2798 } else {
2799 if (index < 0) {
2800 return;
2801 }
2802 desc = mSuspendedEffects.valueAt(index);
2803 if (desc->mRefCount <= 0) {
2804 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2805 desc->mRefCount = 1;
2806 }
2807 if (--desc->mRefCount == 0) {
2808 Vector<const effect_uuid_t *> types;
2809 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2810 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2811 continue;
2812 }
2813 types.add(&mSuspendedEffects.valueAt(i)->mType);
2814 }
2815 for (size_t i = 0; i < types.size(); i++) {
2816 setEffectSuspended_l(types[i], false);
2817 }
2818 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2819 mSuspendedEffects.keyAt(index));
2820 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2821 }
2822 }
2823}
2824
2825
2826// The volume effect is used for automated tests only
2827#ifndef OPENSL_ES_H_
2828static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2829 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2830const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2831#endif //OPENSL_ES_H_
2832
Eric Laurentd8365c52017-07-16 15:27:05 -07002833/* static */
2834bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2835{
2836 // Only NS and AEC are suspended when BtNRec is off
2837 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2838 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2839 return true;
2840 }
2841 return false;
2842}
2843
Eric Laurentca7cc822012-11-19 14:55:58 -08002844bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2845{
2846 // auxiliary effects and visualizer are never suspended on output mix
2847 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2848 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2849 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002850 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2851 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002852 return false;
2853 }
2854 return true;
2855}
2856
2857void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2858 Vector< sp<AudioFlinger::EffectModule> > &effects)
2859{
2860 effects.clear();
2861 for (size_t i = 0; i < mEffects.size(); i++) {
2862 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2863 effects.add(mEffects[i]);
2864 }
2865 }
2866}
2867
2868sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2869 const effect_uuid_t *type)
2870{
2871 sp<EffectModule> effect = getEffectFromType_l(type);
2872 return effect != 0 && effect->isEnabled() ? effect : 0;
2873}
2874
2875void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2876 bool enabled)
2877{
2878 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2879 if (enabled) {
2880 if (index < 0) {
2881 // if the effect is not suspend check if all effects are suspended
2882 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2883 if (index < 0) {
2884 return;
2885 }
2886 if (!isEffectEligibleForSuspend(effect->desc())) {
2887 return;
2888 }
2889 setEffectSuspended_l(&effect->desc().type, enabled);
2890 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2891 if (index < 0) {
2892 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2893 return;
2894 }
2895 }
2896 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2897 effect->desc().type.timeLow);
2898 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002899 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002900 if (desc->mEffect == 0) {
2901 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002902 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002903 effect->setSuspended(true);
2904 }
2905 } else {
2906 if (index < 0) {
2907 return;
2908 }
2909 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2910 effect->desc().type.timeLow);
2911 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2912 desc->mEffect.clear();
2913 effect->setSuspended(false);
2914 }
2915}
2916
Eric Laurent5baf2af2013-09-12 17:37:00 -07002917bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002918{
2919 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002920 return isNonOffloadableEnabled_l();
2921}
2922
2923bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2924{
Eric Laurent813e2a72013-08-31 12:59:48 -07002925 size_t size = mEffects.size();
2926 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002927 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002928 return true;
2929 }
2930 }
2931 return false;
2932}
2933
Eric Laurentaaa44472014-09-12 17:41:50 -07002934void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2935{
2936 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002937 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002938}
2939
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002940void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2941{
2942 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2943 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2944 }
2945 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2946 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2947 }
2948}
2949
2950void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2951{
2952 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2953 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2954 }
2955 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2956 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2957 }
2958}
2959
2960bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002961{
2962 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002963 for (const auto &effect : mEffects) {
2964 if (effect->isProcessImplemented()) {
2965 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002966 }
2967 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002968 // Allow effects without processing.
2969 return true;
2970}
2971
2972bool AudioFlinger::EffectChain::isFastCompatible() const
2973{
2974 Mutex::Autolock _l(mLock);
2975 for (const auto &effect : mEffects) {
2976 if (effect->isProcessImplemented()
2977 && effect->isImplementationSoftware()) {
2978 return false;
2979 }
2980 }
2981 // Allow effects without processing or hw accelerated effects.
2982 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002983}
2984
2985// isCompatibleWithThread_l() must be called with thread->mLock held
2986bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2987{
2988 Mutex::Autolock _l(mLock);
2989 for (size_t i = 0; i < mEffects.size(); i++) {
2990 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2991 return false;
2992 }
2993 }
2994 return true;
2995}
2996
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997// EffectCallbackInterface implementation
2998status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2999 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3000 sp<EffectHalInterface> *effect) {
3001 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003002 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003003 if (effectsFactory != 0) {
3004 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3005 }
3006 return status;
3007}
3008
3009bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003010 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003011 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003012 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003013}
3014
3015status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3016 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003017 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018}
3019
3020status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
3021 sp<EffectHalInterface> effect) {
3022 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003023 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003024 if (t == nullptr) {
3025 return result;
3026 }
3027 sp <StreamHalInterface> st = t->stream();
3028 if (st == nullptr) {
3029 return result;
3030 }
3031 result = st->addEffect(effect);
3032 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3033 return result;
3034}
3035
3036status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
3037 sp<EffectHalInterface> effect) {
3038 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003039 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003040 if (t == nullptr) {
3041 return result;
3042 }
3043 sp <StreamHalInterface> st = t->stream();
3044 if (st == nullptr) {
3045 return result;
3046 }
3047 result = st->removeEffect(effect);
3048 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3049 return result;
3050}
3051
3052audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003053 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003054 if (t == nullptr) {
3055 return AUDIO_IO_HANDLE_NONE;
3056 }
3057 return t->id();
3058}
3059
3060bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003061 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062 if (t == nullptr) {
3063 return true;
3064 }
3065 return t->isOutput();
3066}
3067
3068bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003069 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003070}
3071
3072bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003073 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003074}
3075
3076bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003077 switch (mThreadType) {
3078 case ThreadBase::OFFLOAD:
3079 case ThreadBase::MMAP_PLAYBACK:
3080 case ThreadBase::MMAP_CAPTURE:
3081 return true;
3082 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003083 return false;
3084 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003085}
3086
3087bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3088 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003089}
3090
3091uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003092 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003093 if (t == nullptr) {
3094 return 0;
3095 }
3096 return t->sampleRate();
3097}
3098
Eric Laurentf1f22e72021-07-13 14:04:14 +02003099audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3100 sp<ThreadBase> t = thread().promote();
3101 if (t == nullptr) {
3102 return AUDIO_CHANNEL_NONE;
3103 }
3104 sp<EffectChain> c = chain().promote();
3105 if (c == nullptr) {
3106 return AUDIO_CHANNEL_NONE;
3107 }
3108
Eric Laurentb62d0362021-10-26 17:40:18 +02003109 if (mThreadType == ThreadBase::SPATIALIZER) {
3110 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3111 if (c->isFirstEffect(id)) {
3112 return t->mixerChannelMask();
3113 } else {
3114 return t->channelMask();
3115 }
3116 } else if (!audio_is_global_session(c->sessionId())) {
3117 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3118 return t->mixerChannelMask();
3119 } else {
3120 return t->channelMask();
3121 }
3122 } else {
3123 return t->channelMask();
3124 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003125 } else {
3126 return t->channelMask();
3127 }
3128}
3129
3130uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003131 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003132}
3133
3134audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003135 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003136 if (t == nullptr) {
3137 return AUDIO_CHANNEL_NONE;
3138 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003139 sp<EffectChain> c = chain().promote();
3140 if (c == nullptr) {
3141 return AUDIO_CHANNEL_NONE;
3142 }
3143
3144 if (mThreadType == ThreadBase::SPATIALIZER) {
3145 if (!audio_is_global_session(c->sessionId())) {
3146 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3147 return t->mixerChannelMask();
3148 } else {
3149 return t->channelMask();
3150 }
3151 } else {
3152 return t->channelMask();
3153 }
3154 } else {
3155 return t->channelMask();
3156 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003157}
3158
Eric Laurentf1f22e72021-07-13 14:04:14 +02003159uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003160 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003161}
3162
jiabineb3bda02020-06-30 14:07:03 -07003163audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003164 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003165 if (t == nullptr) {
3166 return AUDIO_CHANNEL_NONE;
3167 }
3168 return t->hapticChannelMask();
3169}
3170
Eric Laurent6b446ce2019-12-13 10:56:31 -08003171size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003172 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003173 if (t == nullptr) {
3174 return 0;
3175 }
3176 return t->frameCount();
3177}
3178
3179uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003180 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003181 if (t == nullptr) {
3182 return 0;
3183 }
3184 return t->latency_l();
3185}
3186
3187void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003188 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003189 if (t == nullptr) {
3190 return;
3191 }
3192 t->setVolumeForOutput_l(left, right);
3193}
3194
3195void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003196 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003197 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003198 if (t == nullptr) {
3199 return;
3200 }
3201 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3202
Andy Hung328d6772021-01-12 12:32:21 -08003203 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003204 if (c == nullptr) {
3205 return;
3206 }
Eric Laurent41709552019-12-16 19:34:05 -08003207 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3208 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003209}
3210
Eric Laurent41709552019-12-16 19:34:05 -08003211void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003212 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003213 if (t == nullptr) {
3214 return;
3215 }
Eric Laurent41709552019-12-16 19:34:05 -08003216 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3217 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003218}
3219
Eric Laurent41709552019-12-16 19:34:05 -08003220void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003221 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3222
Andy Hung328d6772021-01-12 12:32:21 -08003223 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003224 if (t == nullptr) {
3225 return;
3226 }
3227 t->onEffectDisable();
3228}
3229
3230bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3231 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003232 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003233 if (t == nullptr) {
3234 return false;
3235 }
3236 t->disconnectEffectHandle(handle, unpinIfLast);
3237 return true;
3238}
3239
3240void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003241 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003242 if (c == nullptr) {
3243 return;
3244 }
3245 c->resetVolume_l();
3246
3247}
3248
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003249product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003250 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003251 if (c == nullptr) {
3252 return PRODUCT_STRATEGY_NONE;
3253 }
3254 return c->strategy();
3255}
3256
3257int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003258 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003259 if (c == nullptr) {
3260 return 0;
3261 }
3262 return c->activeTrackCnt();
3263}
3264
Eric Laurentb82e6b72019-11-22 17:25:04 -08003265
3266#undef LOG_TAG
3267#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3268
3269status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3270{
3271 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3272 Mutex::Autolock _l(mProxyLock);
3273 if (status == NO_ERROR) {
3274 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003275 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003276 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003277 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003278 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003279 bs = handle.second->disable(&status);
3280 }
3281 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003282 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003283 }
3284 }
3285 }
3286 ALOGV("%s enable %d status %d", __func__, enabled, status);
3287 return status;
3288}
3289
3290status_t AudioFlinger::DeviceEffectProxy::init(
3291 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3292//For all audio patches
3293//If src or sink device match
3294//If the effect is HW accelerated
3295// if no corresponding effect module
3296// Create EffectModule: mHalEffect
3297//Create and attach EffectHandle
3298//If the effect is not HW accelerated and the patch sink or src is a mixer port
3299// Create Effect on patch input or output thread on session -1
3300//Add EffectHandle to EffectHandle map of Effect Proxy:
3301 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3302 status_t status = NO_ERROR;
3303 for (auto &patch : patches) {
3304 status = onCreatePatch(patch.first, patch.second);
3305 ALOGV("%s onCreatePatch status %d", __func__, status);
3306 if (status == BAD_VALUE) {
3307 return status;
3308 }
3309 }
3310 return status;
3311}
3312
3313status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3314 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3315 status_t status = NAME_NOT_FOUND;
3316 sp<EffectHandle> handle;
3317 // only consider source[0] as this is the only "true" source of a patch
3318 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3319 ALOGV("%s source checkPort status %d", __func__, status);
3320 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3321 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3322 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3323 }
3324 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3325 Mutex::Autolock _l(mProxyLock);
3326 mEffectHandles.emplace(patchHandle, handle);
3327 }
3328 ALOGW_IF(status == BAD_VALUE,
3329 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3330
3331 return status;
3332}
3333
3334status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3335 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3336
3337 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3338 __func__, port->type, port->ext.device.type,
3339 port->ext.device.address, port->id, patch.isSoftware());
3340 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003341 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003342 return NAME_NOT_FOUND;
3343 }
3344 status_t status = NAME_NOT_FOUND;
3345
3346 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3347 Mutex::Autolock _l(mProxyLock);
3348 mDevicePort = *port;
3349 mHalEffect = new EffectModule(mMyCallback,
3350 const_cast<effect_descriptor_t *>(&mDescriptor),
3351 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3352 false /* pinned */, port->id);
3353 if (audio_is_input_device(mDevice.mType)) {
3354 mHalEffect->setInputDevice(mDevice);
3355 } else {
3356 mHalEffect->setDevices({mDevice});
3357 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003358 mHalEffect->configure();
3359
Eric Laurentde8caf42021-08-11 17:19:25 +02003360 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3361 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003362 status = (*handle)->initCheck();
3363 if (status == OK) {
3364 status = mHalEffect->addHandle((*handle).get());
3365 } else {
3366 mHalEffect.clear();
3367 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3368 }
3369 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3370 sp <ThreadBase> thread;
3371 if (audio_port_config_has_input_direction(port)) {
3372 if (patch.isSoftware()) {
3373 thread = patch.mRecord.thread();
3374 } else {
3375 thread = patch.thread().promote();
3376 }
3377 } else {
3378 if (patch.isSoftware()) {
3379 thread = patch.mPlayback.thread();
3380 } else {
3381 thread = patch.thread().promote();
3382 }
3383 }
3384 int enabled;
3385 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3386 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003387 &enabled, &status, false, false /*probe*/,
3388 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003389 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3390 } else {
3391 status = BAD_VALUE;
3392 }
3393 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003394 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003395 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003396 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003397 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003398 bs = (*handle)->disable(&status);
3399 }
3400 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003401 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003402 }
3403 }
3404 return status;
3405}
3406
3407void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003408 sp<EffectHandle> effect;
3409 {
3410 Mutex::Autolock _l(mProxyLock);
3411 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3412 effect = mEffectHandles.at(patchHandle);
3413 mEffectHandles.erase(patchHandle);
3414 }
3415 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003416}
3417
3418
3419size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3420{
3421 Mutex::Autolock _l(mProxyLock);
3422 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003423 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003424 mHalEffect.clear();
3425 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3426 }
3427 return mHalEffect == nullptr ? 0 : 1;
3428}
3429
3430status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3431 sp<EffectHalInterface> effect) {
3432 if (mHalEffect == nullptr) {
3433 return NO_INIT;
3434 }
3435 return mManagerCallback->addEffectToHal(
3436 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3437}
3438
3439status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3440 sp<EffectHalInterface> effect) {
3441 if (mHalEffect == nullptr) {
3442 return NO_INIT;
3443 }
3444 return mManagerCallback->removeEffectFromHal(
3445 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3446}
3447
3448bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3449 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3450 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3451 }
3452 return true;
3453}
3454
3455uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3456 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3457 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3458 return mDevicePort.sample_rate;
3459 }
3460 return DEFAULT_OUTPUT_SAMPLE_RATE;
3461}
3462
3463audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3464 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3465 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3466 return mDevicePort.channel_mask;
3467 }
3468 return AUDIO_CHANNEL_OUT_STEREO;
3469}
3470
3471uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3472 if (isOutput()) {
3473 return audio_channel_count_from_out_mask(channelMask());
3474 }
3475 return audio_channel_count_from_in_mask(channelMask());
3476}
3477
3478void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3479 const Vector<String16> args;
3480 EffectBase::dump(fd, args);
3481
3482 const bool locked = dumpTryLock(mProxyLock);
3483 if (!locked) {
3484 String8 result("DeviceEffectProxy may be deadlocked\n");
3485 write(fd, result.string(), result.size());
3486 }
3487
3488 String8 outStr;
3489 if (mHalEffect != nullptr) {
3490 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3491 } else {
3492 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3493 }
3494 write(fd, outStr.string(), outStr.size());
3495 outStr.clear();
3496
3497 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3498 write(fd, outStr.string(), outStr.size());
3499 outStr.clear();
3500
3501 for (const auto& iter : mEffectHandles) {
3502 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3503 write(fd, outStr.string(), outStr.size());
3504 outStr.clear();
3505 sp<EffectBase> effect = iter.second->effect().promote();
3506 if (effect != nullptr) {
3507 effect->dump(fd, args);
3508 }
3509 }
3510
3511 if (locked) {
3512 mLock.unlock();
3513 }
3514}
3515
3516#undef LOG_TAG
3517#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3518
3519int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3520 return mManagerCallback->newEffectId();
3521}
3522
3523
3524bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3525 EffectHandle *handle, bool unpinIfLast) {
3526 sp<EffectBase> effectBase = handle->effect().promote();
3527 if (effectBase == nullptr) {
3528 return false;
3529 }
3530
3531 sp<EffectModule> effect = effectBase->asEffectModule();
3532 if (effect == nullptr) {
3533 return false;
3534 }
3535
3536 // restore suspended effects if the disconnected handle was enabled and the last one.
3537 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3538 if (remove) {
3539 sp<DeviceEffectProxy> proxy = mProxy.promote();
3540 if (proxy != nullptr) {
3541 proxy->removeEffect(effect);
3542 }
3543 if (handle->enabled()) {
3544 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3545 }
3546 }
3547 return true;
3548}
3549
3550status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3551 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3552 sp<EffectHalInterface> *effect) {
3553 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3554}
3555
3556status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3557 sp<EffectHalInterface> effect) {
3558 sp<DeviceEffectProxy> proxy = mProxy.promote();
3559 if (proxy == nullptr) {
3560 return NO_INIT;
3561 }
3562 return proxy->addEffectToHal(effect);
3563}
3564
3565status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3566 sp<EffectHalInterface> effect) {
3567 sp<DeviceEffectProxy> proxy = mProxy.promote();
3568 if (proxy == nullptr) {
3569 return NO_INIT;
3570 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003571 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003572}
3573
3574bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3575 sp<DeviceEffectProxy> proxy = mProxy.promote();
3576 if (proxy == nullptr) {
3577 return true;
3578 }
3579 return proxy->isOutput();
3580}
3581
3582uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3583 sp<DeviceEffectProxy> proxy = mProxy.promote();
3584 if (proxy == nullptr) {
3585 return DEFAULT_OUTPUT_SAMPLE_RATE;
3586 }
3587 return proxy->sampleRate();
3588}
3589
Eric Laurentf1f22e72021-07-13 14:04:14 +02003590audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3591 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003592 sp<DeviceEffectProxy> proxy = mProxy.promote();
3593 if (proxy == nullptr) {
3594 return AUDIO_CHANNEL_OUT_STEREO;
3595 }
3596 return proxy->channelMask();
3597}
3598
Eric Laurentf1f22e72021-07-13 14:04:14 +02003599uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3600 sp<DeviceEffectProxy> proxy = mProxy.promote();
3601 if (proxy == nullptr) {
3602 return 2;
3603 }
3604 return proxy->channelCount();
3605}
3606
3607audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3608 sp<DeviceEffectProxy> proxy = mProxy.promote();
3609 if (proxy == nullptr) {
3610 return AUDIO_CHANNEL_OUT_STEREO;
3611 }
3612 return proxy->channelMask();
3613}
3614
3615uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003616 sp<DeviceEffectProxy> proxy = mProxy.promote();
3617 if (proxy == nullptr) {
3618 return 2;
3619 }
3620 return proxy->channelCount();
3621}
3622
Eric Laurent76c89f32021-12-03 17:13:23 +01003623void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3624 const sp<EffectBase>& effectBase) {
3625 sp<EffectModule> effect = effectBase->asEffectModule();
3626 if (effect == nullptr) {
3627 return;
3628 }
3629 effect->start();
3630}
3631
3632void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3633 const sp<EffectBase>& effectBase) {
3634 sp<EffectModule> effect = effectBase->asEffectModule();
3635 if (effect == nullptr) {
3636 return;
3637 }
3638 effect->stop();
3639}
3640
Glenn Kasten63238ef2015-03-02 15:50:29 -08003641} // namespace android