blob: ff608591a062cd140d1a1da9adddb30cfa210fcb [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 }
jiabin4e246532022-08-23 16:37:30 -07001061
1062 if (isVolumeControl()) {
1063 // Force initializing the volume as 0 for volume control effect for safer ramping
1064 uint32_t left = 0;
1065 uint32_t right = 0;
1066 setVolumeInternal(&left, &right, true /*controller*/);
1067 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001068 }
1069
Andy Hung05083ac2017-12-14 15:00:28 -08001070 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1071 mMaxDisableWaitCnt = (uint32_t)std::max(
1072 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1073 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1074 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001075
Eric Laurentd0ebb532013-04-02 16:41:41 -07001076exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001077 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001078 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001079 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 return status;
1081}
1082
1083status_t AudioFlinger::EffectModule::init()
1084{
1085 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001086 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 return NO_INIT;
1088 }
1089 status_t cmdStatus;
1090 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001091 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1092 0,
1093 NULL,
1094 &size,
1095 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001096 if (status == 0) {
1097 status = cmdStatus;
1098 }
1099 return status;
1100}
1101
Eric Laurent1b928682014-10-02 19:41:47 -07001102void AudioFlinger::EffectModule::addEffectToHal_l()
1103{
1104 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1105 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001106 if (mAddedToHal) {
1107 return;
1108 }
1109
Andy Hungfda44002021-06-03 17:23:16 -07001110 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001111 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001112 }
1113}
1114
Eric Laurentfa1e1232016-08-02 19:01:49 -07001115// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001116status_t AudioFlinger::EffectModule::start()
1117{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001118 status_t status;
1119 {
1120 Mutex::Autolock _l(mLock);
1121 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001122 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001123 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001124 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001125 }
1126 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001127}
1128
1129status_t AudioFlinger::EffectModule::start_l()
1130{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001131 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001132 return NO_INIT;
1133 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001134 if (mStatus != NO_ERROR) {
1135 return mStatus;
1136 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001137 status_t cmdStatus;
1138 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001139 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1140 0,
1141 NULL,
1142 &size,
1143 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001144 if (status == 0) {
1145 status = cmdStatus;
1146 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001147 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001148 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001149 }
1150 return status;
1151}
1152
1153status_t AudioFlinger::EffectModule::stop()
1154{
1155 Mutex::Autolock _l(mLock);
1156 return stop_l();
1157}
1158
1159status_t AudioFlinger::EffectModule::stop_l()
1160{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001161 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 return NO_INIT;
1163 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001164 if (mStatus != NO_ERROR) {
1165 return mStatus;
1166 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001167 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001168 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001169
1170 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001171 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1172 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1173 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001174 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001175 mSetVolumeReentrantTid = INVALID_PID;
1176 }
1177
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001178 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1179 0,
1180 NULL,
1181 &size,
1182 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001183 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001184 status = cmdStatus;
1185 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001186 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001187 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001188 }
1189 return status;
1190}
1191
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001192// must be called with EffectChain::mLock held
1193void AudioFlinger::EffectModule::release_l()
1194{
1195 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001196 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001197 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001198 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001199 mEffectInterface.clear();
1200 }
1201}
1202
Eric Laurent6b446ce2019-12-13 10:56:31 -08001203status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001204{
1205 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1206 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001207 if (!mAddedToHal) {
1208 return NO_ERROR;
1209 }
1210
Andy Hungfda44002021-06-03 17:23:16 -07001211 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001212 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001213 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001214 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001215}
1216
Andy Hunge4a1d912016-08-17 14:11:13 -07001217// round up delta valid if value and divisor are positive.
1218template <typename T>
1219static T roundUpDelta(const T &value, const T &divisor) {
1220 T remainder = value % divisor;
1221 return remainder == 0 ? 0 : divisor - remainder;
1222}
1223
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001224status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1225 const std::vector<uint8_t>& cmdData,
1226 int32_t maxReplySize,
1227 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001228{
1229 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001230 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001231
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001232 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001233 return NO_INIT;
1234 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001235 if (mStatus != NO_ERROR) {
1236 return mStatus;
1237 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001238 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1239 return -EINVAL;
1240 }
1241 size_t cmdSize = cmdData.size();
1242 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1243 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1244 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001245 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001246 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001247 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001248 android_errorWriteLog(0x534e4554, "33003822");
1249 return -EINVAL;
1250 }
1251 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001252 (maxReplySize < sizeof(effect_param_t) ||
1253 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001254 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001255 return -EINVAL;
1256 }
ragoe2759072016-11-22 18:02:48 -08001257 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001258 (sizeof(effect_param_t) > maxReplySize
1259 || param->psize > maxReplySize - sizeof(effect_param_t)
1260 || param->vsize > maxReplySize - sizeof(effect_param_t)
1261 - param->psize
1262 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1263 maxReplySize
1264 - sizeof(effect_param_t)
1265 - param->psize
1266 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001267 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1268 android_errorWriteLog(0x534e4554, "32705438");
1269 return -EINVAL;
1270 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001271 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001272 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1273 && // DEFERRED not generally used
1274 (param == nullptr
1275 || param->psize > cmdSize - sizeof(effect_param_t)
1276 || param->vsize > cmdSize - sizeof(effect_param_t)
1277 - param->psize
1278 || roundUpDelta(param->psize,
1279 (uint32_t) sizeof(int)) >
1280 cmdSize
1281 - sizeof(effect_param_t)
1282 - param->psize
1283 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001284 android_errorWriteLog(0x534e4554, "30204301");
1285 return -EINVAL;
1286 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001287 uint32_t replySize = maxReplySize;
1288 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001289 status_t status = mEffectInterface->command(cmdCode,
1290 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001291 const_cast<uint8_t*>(cmdData.data()),
1292 &replySize,
1293 reply->data());
1294 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001295 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 for (size_t i = 1; i < mHandles.size(); i++) {
1297 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001298 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001299 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001300 }
1301 }
1302 }
1303 return status;
1304}
1305
Eric Laurentca7cc822012-11-19 14:55:58 -08001306bool AudioFlinger::EffectModule::isProcessEnabled() const
1307{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001308 if (mStatus != NO_ERROR) {
1309 return false;
1310 }
1311
Eric Laurentca7cc822012-11-19 14:55:58 -08001312 switch (mState) {
1313 case RESTART:
1314 case ACTIVE:
1315 case STOPPING:
1316 case STOPPED:
1317 return true;
1318 case IDLE:
1319 case STARTING:
1320 case DESTROYED:
1321 default:
1322 return false;
1323 }
1324}
1325
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001326bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1327{
Andy Hungfda44002021-06-03 17:23:16 -07001328 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001329}
1330
1331bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1332{
1333 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1334}
1335
Mikhail Naganov022b9952017-01-04 16:36:51 -08001336void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001337 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001338
1339 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001340 if (buffer != 0) {
1341 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1342 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1343 } else {
1344 mConfig.inputCfg.buffer.raw = NULL;
1345 }
1346 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001347 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001348
1349#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001350 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001351 // Theoretically insert effects can also do in-place conversions (destroying
1352 // the original buffer) when the output buffer is identical to the input buffer,
1353 // but we don't optimize for it here.
1354 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001355 const uint32_t inChannelCount =
1356 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1357 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001358 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001359 // we need to translate - create hidl shared buffer and intercept
1360 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001361 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1362 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1363 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001364
1365 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1366 __func__, inChannels, inFrameCount, size);
1367
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001368 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001369 || size > mInConversionBuffer->getSize())) {
1370 mInConversionBuffer.clear();
1371 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001372 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001373 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001374 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001375 mInConversionBuffer->setFrameCount(inFrameCount);
1376 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001377 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001378 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001379 }
1380 }
1381#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001382}
1383
1384void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001385 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001386
1387 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001388 if (buffer != 0) {
1389 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1390 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1391 } else {
1392 mConfig.outputCfg.buffer.raw = NULL;
1393 }
1394 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001395 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001396
1397#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001398 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001399 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001400 const uint32_t outChannelCount =
1401 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1402 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001403 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001404 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001405 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1406 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1407 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001408
1409 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1410 __func__, outChannels, outFrameCount, size);
1411
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001412 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001413 || size > mOutConversionBuffer->getSize())) {
1414 mOutConversionBuffer.clear();
1415 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001416 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001417 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001418 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001419 mOutConversionBuffer->setFrameCount(outFrameCount);
1420 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001421 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001422 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001423 }
1424 }
1425#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001426}
1427
Eric Laurentca7cc822012-11-19 14:55:58 -08001428status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1429{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001430 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001431 if (mStatus != NO_ERROR) {
1432 return mStatus;
1433 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001434 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001435 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1436 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1437 if (isProcessEnabled() &&
1438 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001439 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1440 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin4e246532022-08-23 16:37:30 -07001441 status = setVolumeInternal(left, right, controller);
1442 }
1443 return status;
1444}
1445
1446status_t AudioFlinger::EffectModule::setVolumeInternal(
1447 uint32_t *left, uint32_t *right, bool controller) {
1448 uint32_t volume[2] = {*left, *right};
1449 uint32_t *pVolume = controller ? volume : nullptr;
1450 uint32_t size = sizeof(volume);
1451 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1452 size,
1453 volume,
1454 &size,
1455 pVolume);
1456 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1457 *left = volume[0];
1458 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001459 }
1460 return status;
1461}
1462
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001463void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1464{
Zhou Songd505c642020-02-20 16:35:37 +08001465 // for offload or direct thread, if the effect chain has non-offloadable
1466 // effect and any effect module within the chain has volume control, then
1467 // volume control is delegated to effect, otherwise, set volume to hal.
1468 if (mEffectCallback->isOffloadOrDirect() &&
1469 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001470 float vol_l = (float)left / (1 << 24);
1471 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001472 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001473 }
1474}
1475
jiabin8f278ee2019-11-11 12:16:27 -08001476status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1477 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001478{
jiabin8f278ee2019-11-11 12:16:27 -08001479 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1480 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 return NO_ERROR;
1482 }
1483
1484 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001485 if (mStatus != NO_ERROR) {
1486 return mStatus;
1487 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001488 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001489 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001490 status_t cmdStatus;
1491 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001492 // FIXME: use audio device types and addresses when the hal interface is ready.
1493 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001494 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001495 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001496 &size,
1497 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001498 }
1499 return status;
1500}
1501
jiabin8f278ee2019-11-11 12:16:27 -08001502status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1503{
1504 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1505}
1506
1507status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1508{
1509 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1510}
1511
Eric Laurentca7cc822012-11-19 14:55:58 -08001512status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1513{
1514 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001515 if (mStatus != NO_ERROR) {
1516 return mStatus;
1517 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001518 status_t status = NO_ERROR;
1519 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1520 status_t cmdStatus;
1521 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001522 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1523 sizeof(audio_mode_t),
1524 &mode,
1525 &size,
1526 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001527 if (status == NO_ERROR) {
1528 status = cmdStatus;
1529 }
1530 }
1531 return status;
1532}
1533
1534status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1535{
1536 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001537 if (mStatus != NO_ERROR) {
1538 return mStatus;
1539 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 status_t status = NO_ERROR;
1541 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1542 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001543 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1544 sizeof(audio_source_t),
1545 &source,
1546 &size,
1547 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001548 }
1549 return status;
1550}
1551
Eric Laurent5baf2af2013-09-12 17:37:00 -07001552status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1553{
1554 Mutex::Autolock _l(mLock);
1555 if (mStatus != NO_ERROR) {
1556 return mStatus;
1557 }
1558 status_t status = NO_ERROR;
1559 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1560 status_t cmdStatus;
1561 uint32_t size = sizeof(status_t);
1562 effect_offload_param_t cmd;
1563
1564 cmd.isOffload = offloaded;
1565 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001566 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1567 sizeof(effect_offload_param_t),
1568 &cmd,
1569 &size,
1570 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001571 if (status == NO_ERROR) {
1572 status = cmdStatus;
1573 }
1574 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1575 } else {
1576 if (offloaded) {
1577 status = INVALID_OPERATION;
1578 }
1579 mOffloaded = false;
1580 }
1581 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1582 return status;
1583}
1584
1585bool AudioFlinger::EffectModule::isOffloaded() const
1586{
1587 Mutex::Autolock _l(mLock);
1588 return mOffloaded;
1589}
1590
jiabineb3bda02020-06-30 14:07:03 -07001591/*static*/
1592bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1593 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1594}
1595
1596bool AudioFlinger::EffectModule::isHapticGenerator() const {
1597 return isHapticGenerator(&mDescriptor.type);
1598}
1599
jiabine70bc7f2020-06-30 22:07:55 -07001600status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1601{
1602 if (mStatus != NO_ERROR) {
1603 return mStatus;
1604 }
1605 if (!isHapticGenerator()) {
1606 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1607 return INVALID_OPERATION;
1608 }
1609
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001610 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1611 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001612 param->psize = sizeof(int32_t);
1613 param->vsize = sizeof(int32_t) * 2;
1614 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1615 *((int32_t*)param->data + 1) = id;
1616 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001617 std::vector<uint8_t> response;
1618 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001619 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001620 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1621 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001622 }
1623 return status;
1624}
1625
Lais Andradebc3f37a2021-07-02 00:13:19 +01001626status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001627{
1628 if (mStatus != NO_ERROR) {
1629 return mStatus;
1630 }
1631 if (!isHapticGenerator()) {
1632 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1633 return INVALID_OPERATION;
1634 }
1635
Lais Andradebc3f37a2021-07-02 00:13:19 +01001636 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001637 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001638 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001639 effect_param_t *param = (effect_param_t*) request.data();
1640 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001641 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001642 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1643 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001644 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1645 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1646 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001647 std::vector<uint8_t> response;
1648 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1649 if (status == NO_ERROR) {
1650 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1651 status = *reinterpret_cast<const status_t*>(response.data());
1652 }
1653 return status;
1654}
1655
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001656status_t AudioFlinger::EffectModule::getConfigs(
1657 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1658 Mutex::Autolock _l(mLock);
1659 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1660 return NO_INIT;
1661 }
1662 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1663 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1664 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1665 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1666 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1667 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1668 *isOutput = mIsOutput;
1669 return NO_ERROR;
1670}
1671
Andy Hungbded9c82017-11-30 18:47:35 -08001672static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1673 std::stringstream ss;
1674
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001675 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001676 return "nullptr"; // make different than below
1677 } else if (buffer->externalData() != nullptr) {
1678 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1679 << " -> "
1680 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1681 } else {
1682 ss << buffer->audioBuffer()->raw;
1683 }
1684 return ss.str();
1685}
Marco Nelissenb2208842014-02-07 14:00:50 -08001686
Eric Laurent41709552019-12-16 19:34:05 -08001687void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001688{
Eric Laurent41709552019-12-16 19:34:05 -08001689 EffectBase::dump(fd, args);
1690
Eric Laurentca7cc822012-11-19 14:55:58 -08001691 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001693
Eric Laurent41709552019-12-16 19:34:05 -08001694 result.append("\t\tStatus Engine:\n");
1695 result.appendFormat("\t\t%03d %p\n",
1696 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001697
1698 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001699
1700 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001701 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1702 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1703 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 mConfig.inputCfg.buffer.frameCount,
1705 mConfig.inputCfg.samplingRate,
1706 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001707 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001708 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001709
1710 result.append("\t\t- Output configuration:\n");
1711 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001712 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001713 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 mConfig.outputCfg.buffer.frameCount,
1715 mConfig.outputCfg.samplingRate,
1716 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001717 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001718 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001719
rago94a1ee82017-07-21 15:11:02 -07001720#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001721
Andy Hungbded9c82017-11-30 18:47:35 -08001722 result.appendFormat("\t\t- HAL buffers:\n"
1723 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1724 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1725 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1726 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1727 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001728#endif
1729
Eric Laurentca7cc822012-11-19 14:55:58 -08001730 write(fd, result.string(), result.length());
1731
Mikhail Naganov4d547672019-02-22 14:19:19 -08001732 if (mEffectInterface != 0) {
1733 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1734 (void)mEffectInterface->dump(fd);
1735 }
1736
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 if (locked) {
1738 mLock.unlock();
1739 }
1740}
1741
1742// ----------------------------------------------------------------------------
1743// EffectHandle implementation
1744// ----------------------------------------------------------------------------
1745
1746#undef LOG_TAG
1747#define LOG_TAG "AudioFlinger::EffectHandle"
1748
Eric Laurent41709552019-12-16 19:34:05 -08001749AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001750 const sp<AudioFlinger::Client>& client,
1751 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001752 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 : BnEffect(),
1754 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001755 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1756 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001757{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001758 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001759
1760 if (client == 0) {
1761 return;
1762 }
1763 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001764 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1765 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1766 std::string("Effect ID: ")
1767 .append(std::to_string(effect->id()))
1768 .append(" Session ID: ")
1769 .append(std::to_string(static_cast<int>(effect->sessionId())))
1770 .append(" \n")
1771 });
Glenn Kastene75da402013-11-20 13:54:52 -08001772 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001773 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001774 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001776 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001777 return;
1778 }
Glenn Kastene75da402013-11-20 13:54:52 -08001779 new(mCblk) effect_param_cblk_t();
1780 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001781}
1782
1783AudioFlinger::EffectHandle::~EffectHandle()
1784{
1785 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 disconnect(false);
1787}
1788
Andy Hungc747c532022-03-07 21:41:14 -08001789// Creates an association between Binder code to name for IEffect.
1790#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1791BINDER_METHOD_ENTRY(enable) \
1792BINDER_METHOD_ENTRY(disable) \
1793BINDER_METHOD_ENTRY(command) \
1794BINDER_METHOD_ENTRY(disconnect) \
1795BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001796BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001797
1798// singleton for Binder Method Statistics for IEffect
1799mediautils::MethodStatistics<int>& getIEffectStatistics() {
1800 using Code = int;
1801
1802#pragma push_macro("BINDER_METHOD_ENTRY")
1803#undef BINDER_METHOD_ENTRY
1804#define BINDER_METHOD_ENTRY(ENTRY) \
1805 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1806
1807 static mediautils::MethodStatistics<Code> methodStatistics{
1808 IEFFECT_BINDER_METHOD_MACRO_LIST
1809 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1810 };
1811#pragma pop_macro("BINDER_METHOD_ENTRY")
1812
1813 return methodStatistics;
1814}
1815
1816status_t AudioFlinger::EffectHandle::onTransact(
1817 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001818 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1819 mediautils::TimeCheck check(
1820 std::string("IEffect::").append(methodName),
1821 [code](bool timeout, float elapsedMs) {
1822 if (timeout) {
1823 ; // we don't timeout right now on the effect interface.
1824 } else {
1825 getIEffectStatistics().event(code, elapsedMs);
1826 }
1827 }, 0 /* timeoutMs */);
Andy Hungc747c532022-03-07 21:41:14 -08001828 return BnEffect::onTransact(code, data, reply, flags);
1829}
1830
Glenn Kastene75da402013-11-20 13:54:52 -08001831status_t AudioFlinger::EffectHandle::initCheck()
1832{
1833 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1834}
1835
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001836#define RETURN(code) \
1837 *_aidl_return = (code); \
1838 return Status::ok();
1839
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001840#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1841 ({ \
1842 auto _tmp = (exp); \
1843 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1844 std::move(_tmp.value()); \
1845 })
1846
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001847Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001848{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001849 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001850 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001851 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001857 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001858
1859 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001860 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 }
1862
1863 mEnabled = true;
1864
Eric Laurent6c796322019-04-09 14:13:17 -07001865 status_t status = effect->updatePolicyState();
1866 if (status != NO_ERROR) {
1867 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001868 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001869 }
1870
Eric Laurent6b446ce2019-12-13 10:56:31 -08001871 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001872
1873 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001874 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001875 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001876 }
1877
Eric Laurent6b446ce2019-12-13 10:56:31 -08001878 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001880 mEnabled = false;
1881 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001882 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001883}
1884
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001885Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001886{
1887 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001888 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001889 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001890 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001891 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001892 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001893 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001894 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001895 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001896
1897 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001898 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001899 }
1900 mEnabled = false;
1901
Eric Laurent6c796322019-04-09 14:13:17 -07001902 effect->updatePolicyState();
1903
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001904 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001906 }
1907
Eric Laurent6b446ce2019-12-13 10:56:31 -08001908 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001910}
1911
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001913{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001914 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001915 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001916 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001917}
1918
1919void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1920{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001921 AutoMutex _l(mLock);
1922 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1923 if (mDisconnected) {
1924 if (unpinIfLast) {
1925 android_errorWriteLog(0x534e4554, "32707507");
1926 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 return;
1928 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001929 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001930 {
Eric Laurent41709552019-12-16 19:34:05 -08001931 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001932 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001933 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001934 ALOGW("%s Effect handle %p disconnected after thread destruction",
1935 __func__, this);
1936 }
1937 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001938 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001939 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001940
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 if (mClient != 0) {
1942 if (mCblk != NULL) {
1943 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1944 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1945 }
1946 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001947 // Client destructor must run with AudioFlinger client mutex locked
1948 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001949 mClient.clear();
1950 }
1951}
1952
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001953Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1954 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1955 return Status::ok();
1956}
1957
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001958Status AudioFlinger::EffectHandle::getConfig(
1959 media::EffectConfig* _config, int32_t* _aidl_return) {
1960 AutoMutex _l(mLock);
1961 sp<EffectBase> effect = mEffect.promote();
1962 if (effect == nullptr || mDisconnected) {
1963 RETURN(DEAD_OBJECT);
1964 }
1965 sp<EffectModule> effectModule = effect->asEffectModule();
1966 if (effectModule == nullptr) {
1967 RETURN(INVALID_OPERATION);
1968 }
1969 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1970 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1971 bool isOutput;
1972 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1973 if (status == NO_ERROR) {
1974 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1975 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1976 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1977 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1978 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1979 _config->isOnInputStream = !isOutput;
1980 }
1981 RETURN(status);
1982}
1983
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001984Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1985 const std::vector<uint8_t>& cmdData,
1986 int32_t maxResponseSize,
1987 std::vector<uint8_t>* response,
1988 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001989{
1990 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001991 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001992
Eric Laurentc7ab3092017-06-15 18:43:46 -07001993 // reject commands reserved for internal use by audio framework if coming from outside
1994 // of audioserver
1995 switch(cmdCode) {
1996 case EFFECT_CMD_ENABLE:
1997 case EFFECT_CMD_DISABLE:
1998 case EFFECT_CMD_SET_PARAM:
1999 case EFFECT_CMD_SET_PARAM_DEFERRED:
2000 case EFFECT_CMD_SET_PARAM_COMMIT:
2001 case EFFECT_CMD_GET_PARAM:
2002 break;
2003 default:
2004 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2005 break;
2006 }
2007 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002009 }
2010
Eric Laurent1ffc5852016-12-15 14:46:09 -08002011 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002012 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002013 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002014 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002015 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002016 writeToBuffer(NO_ERROR, response);
2017 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002018 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002020 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002021 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002022 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002023 writeToBuffer(NO_ERROR, response);
2024 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002025 }
2026
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002027 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002028 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002029 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002030 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002031 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002032 // only get parameter command is permitted for applications not controlling the effect
2033 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002034 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002035 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002036
2037 // handle commands that are not forwarded transparently to effect engine
2038 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002039 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002040 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002041 }
2042
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002044 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002045 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002046 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002047 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002048
Eric Laurentca7cc822012-11-19 14:55:58 -08002049 // No need to trylock() here as this function is executed in the binder thread serving a
2050 // particular client process: no risk to block the whole media server process or mixer
2051 // threads if we are stuck here
2052 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002053 // keep local copy of index in case of client corruption b/32220769
2054 const uint32_t clientIndex = mCblk->clientIndex;
2055 const uint32_t serverIndex = mCblk->serverIndex;
2056 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2057 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002058 mCblk->serverIndex = 0;
2059 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002060 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 }
2062 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002063 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002064 for (uint32_t index = serverIndex; index < clientIndex;) {
2065 int *p = (int *)(mBuffer + index);
2066 const int size = *p++;
2067 if (size < 0
2068 || size > EFFECT_PARAM_BUFFER_SIZE
2069 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002070 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002071 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002072 break;
2073 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002074
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002075 std::copy(reinterpret_cast<const uint8_t*>(p),
2076 reinterpret_cast<const uint8_t*>(p) + size,
2077 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002078
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002079 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002080 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002081 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002082 sizeof(int),
2083 &replyBuffer);
2084 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002085
2086 // verify shared memory: server index shouldn't change; client index can't go back.
2087 if (serverIndex != mCblk->serverIndex
2088 || clientIndex > mCblk->clientIndex) {
2089 android_errorWriteLog(0x534e4554, "32220769");
2090 status = BAD_VALUE;
2091 break;
2092 }
2093
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 // stop at first error encountered
2095 if (ret != NO_ERROR) {
2096 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002097 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002098 break;
2099 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002100 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 break;
2102 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002103 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 }
2105 mCblk->serverIndex = 0;
2106 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002107 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002108 }
2109
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002110 status_t status = effect->command(cmdCode,
2111 cmdData,
2112 maxResponseSize,
2113 response);
2114 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002115}
2116
2117void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2118{
2119 ALOGV("setControl %p control %d", this, hasControl);
2120
2121 mHasControl = hasControl;
2122 mEnabled = enabled;
2123
2124 if (signal && mEffectClient != 0) {
2125 mEffectClient->controlStatusChanged(hasControl);
2126 }
2127}
2128
2129void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002130 const std::vector<uint8_t>& cmdData,
2131 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002132{
2133 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002134 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002135 }
2136}
2137
2138
2139
2140void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2141{
2142 if (mEffectClient != 0) {
2143 mEffectClient->enableStatusChanged(enabled);
2144 }
2145}
2146
Eric Laurentde8caf42021-08-11 17:19:25 +02002147void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2148{
2149 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2150 mEffectClient->framesProcessed(frames);
2151 }
2152}
2153
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002154void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002155{
2156 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2157
Marco Nelissenb2208842014-02-07 14:00:50 -08002158 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002159 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002160 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002161 mHasControl ? "yes" : "no",
2162 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002163 mCblk ? mCblk->clientIndex : 0,
2164 mCblk ? mCblk->serverIndex : 0
2165 );
2166
2167 if (locked) {
2168 mCblk->lock.unlock();
2169 }
2170}
2171
2172#undef LOG_TAG
2173#define LOG_TAG "AudioFlinger::EffectChain"
2174
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002175AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2176 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002177 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002178 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002179 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002180 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002181{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002182 sp<ThreadBase> p = thread.promote();
2183 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002184 return;
2185 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002186 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002187 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2188 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002189}
2190
2191AudioFlinger::EffectChain::~EffectChain()
2192{
Eric Laurentca7cc822012-11-19 14:55:58 -08002193}
2194
2195// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2196sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2197 effect_descriptor_t *descriptor)
2198{
2199 size_t size = mEffects.size();
2200
2201 for (size_t i = 0; i < size; i++) {
2202 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2203 return mEffects[i];
2204 }
2205 }
2206 return 0;
2207}
2208
2209// getEffectFromId_l() must be called with ThreadBase::mLock held
2210sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2211{
2212 size_t size = mEffects.size();
2213
2214 for (size_t i = 0; i < size; i++) {
2215 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2216 if (id == 0 || mEffects[i]->id() == id) {
2217 return mEffects[i];
2218 }
2219 }
2220 return 0;
2221}
2222
2223// getEffectFromType_l() must be called with ThreadBase::mLock held
2224sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2225 const effect_uuid_t *type)
2226{
2227 size_t size = mEffects.size();
2228
2229 for (size_t i = 0; i < size; i++) {
2230 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2231 return mEffects[i];
2232 }
2233 }
2234 return 0;
2235}
2236
Eric Laurent6c796322019-04-09 14:13:17 -07002237std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2238{
2239 std::vector<int> ids;
2240 Mutex::Autolock _l(mLock);
2241 for (size_t i = 0; i < mEffects.size(); i++) {
2242 ids.push_back(mEffects[i]->id());
2243 }
2244 return ids;
2245}
2246
Eric Laurentca7cc822012-11-19 14:55:58 -08002247void AudioFlinger::EffectChain::clearInputBuffer()
2248{
2249 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002250 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002251}
2252
2253// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002254void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002255{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002256 if (mInBuffer == NULL) {
2257 return;
2258 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002259 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2260 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002261
Eric Laurent6b446ce2019-12-13 10:56:31 -08002262 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002263 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002264}
2265
2266// Must be called with EffectChain::mLock locked
2267void AudioFlinger::EffectChain::process_l()
2268{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002269 // never process effects when:
2270 // - on an OFFLOAD thread
2271 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002272 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002273 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002274 bool tracksOnSession = (trackCnt() != 0);
2275
2276 if (!tracksOnSession && mTailBufferCount == 0) {
2277 doProcess = false;
2278 }
2279
2280 if (activeTrackCnt() == 0) {
2281 // if no track is active and the effect tail has not been rendered,
2282 // the input buffer must be cleared here as the mixer process will not do it
2283 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002284 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002285 if (mTailBufferCount > 0) {
2286 mTailBufferCount--;
2287 }
2288 }
2289 }
2290 }
2291
2292 size_t size = mEffects.size();
2293 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002294 // Only the input and output buffers of the chain can be external,
2295 // and 'update' / 'commit' do nothing for allocated buffers, thus
2296 // it's not needed to consider any other buffers here.
2297 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002298 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2299 mOutBuffer->update();
2300 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002301 for (size_t i = 0; i < size; i++) {
2302 mEffects[i]->process();
2303 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002304 mInBuffer->commit();
2305 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2306 mOutBuffer->commit();
2307 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002309 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002310 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002311 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2312 }
2313 if (doResetVolume) {
2314 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002315 }
2316}
2317
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002318// createEffect_l() must be called with ThreadBase::mLock held
2319status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002320 effect_descriptor_t *desc,
2321 int id,
2322 audio_session_t sessionId,
2323 bool pinned)
2324{
2325 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002326 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002327 status_t lStatus = effect->status();
2328 if (lStatus == NO_ERROR) {
2329 lStatus = addEffect_ll(effect);
2330 }
2331 if (lStatus != NO_ERROR) {
2332 effect.clear();
2333 }
2334 return lStatus;
2335}
2336
2337// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002338status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2339{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002340 Mutex::Autolock _l(mLock);
2341 return addEffect_ll(effect);
2342}
2343// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2344status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2345{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002346 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002347
Eric Laurentb62d0362021-10-26 17:40:18 +02002348 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002349 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2350 // Auxiliary effects are inserted at the beginning of mEffects vector as
2351 // they are processed first and accumulated in chain input buffer
2352 mEffects.insertAt(effect, 0);
2353
2354 // the input buffer for auxiliary effect contains mono samples in
2355 // 32 bit format. This is to avoid saturation in AudoMixer
2356 // accumulation stage. Saturation is done in EffectModule::process() before
2357 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002358 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002359 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002360#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002361 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002362 numSamples * sizeof(float), &halBuffer);
2363#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002364 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002365 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002366#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002367 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002368
2369 effect->configure();
2370
Mikhail Naganov022b9952017-01-04 16:36:51 -08002371 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002372 // auxiliary effects output samples to chain input buffer for further processing
2373 // by insert effects
2374 effect->setOutBuffer(mInBuffer);
2375 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002376 ssize_t idx_insert = getInsertIndex(desc);
2377 if (idx_insert < 0) {
2378 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002379 }
2380
Eric Laurentb62d0362021-10-26 17:40:18 +02002381 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002382 mEffects.insertAt(effect, idx_insert);
2383
2384 effect->configure();
2385
Eric Laurentb62d0362021-10-26 17:40:18 +02002386 // - By default:
2387 // All effects read samples from chain input buffer.
2388 // The last effect in the chain, writes samples to chain output buffer,
2389 // otherwise to chain input buffer
2390 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2391 // The spatializer effect (first effect) reads samples from the input buffer
2392 // and writes samples to the output buffer.
2393 // All other effects read and writes samples to the output buffer
2394 if (mEffectCallback->isSpatializer()
2395 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002396 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002397 if (idx_insert == 0) {
2398 if (previousSize != 0) {
2399 mEffects[1]->configure();
2400 mEffects[1]->setInBuffer(mOutBuffer);
2401 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2402 }
2403 effect->setInBuffer(mInBuffer);
2404 } else {
2405 effect->setInBuffer(mOutBuffer);
2406 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002407 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002408 effect->setInBuffer(mInBuffer);
2409 if (idx_insert == previousSize) {
2410 if (idx_insert != 0) {
2411 mEffects[idx_insert-1]->configure();
2412 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2413 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2414 }
2415 effect->setOutBuffer(mOutBuffer);
2416 } else {
2417 effect->setOutBuffer(mInBuffer);
2418 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002420 ALOGV("%s effect %p, added in chain %p at rank %zu",
2421 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002422 }
2423 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002424
Eric Laurentca7cc822012-11-19 14:55:58 -08002425 return NO_ERROR;
2426}
2427
Eric Laurentb62d0362021-10-26 17:40:18 +02002428ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2429 // Insert effects are inserted at the end of mEffects vector as they are processed
2430 // after track and auxiliary effects.
2431 // Insert effect order as a function of indicated preference:
2432 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2433 // another effect is present
2434 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2435 // last effect claiming first position
2436 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2437 // first effect claiming last position
2438 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2439 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2440 // already present
2441 // Spatializer or Downmixer effects are inserted in first position because
2442 // they adapt the channel count for all other effects in the chain
2443 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2444 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2445 return 0;
2446 }
2447
2448 size_t size = mEffects.size();
2449 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2450 ssize_t idx_insert;
2451 ssize_t idx_insert_first = -1;
2452 ssize_t idx_insert_last = -1;
2453
2454 idx_insert = size;
2455 for (size_t i = 0; i < size; i++) {
2456 effect_descriptor_t d = mEffects[i]->desc();
2457 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2458 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2459 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2460 // check invalid effect chaining combinations
2461 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2462 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2463 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2464 __func__, desc.name, d.name);
2465 return -1;
2466 }
2467 // remember position of first insert effect and by default
2468 // select this as insert position for new effect
2469 if (idx_insert == size) {
2470 idx_insert = i;
2471 }
2472 // remember position of last insert effect claiming
2473 // first position
2474 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2475 idx_insert_first = i;
2476 }
2477 // remember position of first insert effect claiming
2478 // last position
2479 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2480 idx_insert_last == -1) {
2481 idx_insert_last = i;
2482 }
2483 }
2484 }
2485
2486 // modify idx_insert from first position if needed
2487 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2488 if (idx_insert_last != -1) {
2489 idx_insert = idx_insert_last;
2490 } else {
2491 idx_insert = size;
2492 }
2493 } else {
2494 if (idx_insert_first != -1) {
2495 idx_insert = idx_insert_first + 1;
2496 }
2497 }
2498 return idx_insert;
2499}
2500
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002501// removeEffect_l() must be called with ThreadBase::mLock held
2502size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2503 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002504{
2505 Mutex::Autolock _l(mLock);
2506 size_t size = mEffects.size();
2507 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2508
2509 for (size_t i = 0; i < size; i++) {
2510 if (effect == mEffects[i]) {
2511 // calling stop here will remove pre-processing effect from the audio HAL.
2512 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2513 // the middle of a read from audio HAL
2514 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2515 mEffects[i]->state() == EffectModule::STOPPING) {
2516 mEffects[i]->stop();
2517 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002518 if (release) {
2519 mEffects[i]->release_l();
2520 }
2521
Mikhail Naganov022b9952017-01-04 16:36:51 -08002522 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002523 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002524 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002525 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002526 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002527 }
2528 }
2529 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002530
2531 // make sure the input buffer configuration for the new first effect in the chain
2532 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2533 if (i == 0 && size > 1) {
2534 mEffects[0]->configure();
2535 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002536 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002537 }
2538
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002539 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002540 this, i);
2541 break;
2542 }
2543 }
2544
2545 return mEffects.size();
2546}
2547
jiabin8f278ee2019-11-11 12:16:27 -08002548// setDevices_l() must be called with ThreadBase::mLock held
2549void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002550{
2551 size_t size = mEffects.size();
2552 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002553 mEffects[i]->setDevices(devices);
2554 }
2555}
2556
2557// setInputDevice_l() must be called with ThreadBase::mLock held
2558void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2559{
2560 size_t size = mEffects.size();
2561 for (size_t i = 0; i < size; i++) {
2562 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002563 }
2564}
2565
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002566// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002567void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2568{
2569 size_t size = mEffects.size();
2570 for (size_t i = 0; i < size; i++) {
2571 mEffects[i]->setMode(mode);
2572 }
2573}
2574
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002575// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002576void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2577{
2578 size_t size = mEffects.size();
2579 for (size_t i = 0; i < size; i++) {
2580 mEffects[i]->setAudioSource(source);
2581 }
2582}
2583
Zhou Songd505c642020-02-20 16:35:37 +08002584bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2585 for (const auto &effect : mEffects) {
2586 if (effect->isVolumeControlEnabled()) return true;
2587 }
2588 return false;
2589}
2590
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002591// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002592bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002593{
2594 uint32_t newLeft = *left;
2595 uint32_t newRight = *right;
2596 bool hasControl = false;
2597 int ctrlIdx = -1;
2598 size_t size = mEffects.size();
2599
2600 // first update volume controller
2601 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002602 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002603 ctrlIdx = i - 1;
2604 hasControl = true;
2605 break;
2606 }
2607 }
2608
Eric Laurentfa1e1232016-08-02 19:01:49 -07002609 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002610 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002611 if (hasControl) {
2612 *left = mNewLeftVolume;
2613 *right = mNewRightVolume;
2614 }
2615 return hasControl;
2616 }
2617
2618 mVolumeCtrlIdx = ctrlIdx;
2619 mLeftVolume = newLeft;
2620 mRightVolume = newRight;
2621
2622 // second get volume update from volume controller
2623 if (ctrlIdx >= 0) {
2624 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2625 mNewLeftVolume = newLeft;
2626 mNewRightVolume = newRight;
2627 }
2628 // then indicate volume to all other effects in chain.
2629 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002630 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002631 uint32_t lVol = newLeft;
2632 uint32_t rVol = newRight;
2633
2634 for (size_t i = 0; i < size; i++) {
2635 if ((int)i == ctrlIdx) {
2636 continue;
2637 }
2638 // this also works for ctrlIdx == -1 when there is no volume controller
2639 if ((int)i > ctrlIdx) {
2640 lVol = *left;
2641 rVol = *right;
2642 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002643 // Pass requested volume directly if this is volume monitor module
2644 if (mEffects[i]->isVolumeMonitor()) {
2645 mEffects[i]->setVolume(left, right, false);
2646 } else {
2647 mEffects[i]->setVolume(&lVol, &rVol, false);
2648 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002649 }
2650 *left = newLeft;
2651 *right = newRight;
2652
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002653 setVolumeForOutput_l(*left, *right);
2654
Eric Laurentca7cc822012-11-19 14:55:58 -08002655 return hasControl;
2656}
2657
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002658// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002659void AudioFlinger::EffectChain::resetVolume_l()
2660{
Eric Laurente7449bf2016-08-03 18:44:07 -07002661 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2662 uint32_t left = mLeftVolume;
2663 uint32_t right = mRightVolume;
2664 (void)setVolume_l(&left, &right, true);
2665 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002666}
2667
jiabineb3bda02020-06-30 14:07:03 -07002668// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2669bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2670{
2671 for (size_t i = 0; i < mEffects.size(); ++i) {
2672 if (mEffects[i]->isHapticGenerator()) {
2673 return true;
2674 }
2675 }
2676 return false;
2677}
2678
jiabine70bc7f2020-06-30 22:07:55 -07002679void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2680{
2681 Mutex::Autolock _l(mLock);
2682 for (size_t i = 0; i < mEffects.size(); ++i) {
2683 mEffects[i]->setHapticIntensity(id, intensity);
2684 }
2685}
2686
Eric Laurent1b928682014-10-02 19:41:47 -07002687void AudioFlinger::EffectChain::syncHalEffectsState()
2688{
2689 Mutex::Autolock _l(mLock);
2690 for (size_t i = 0; i < mEffects.size(); i++) {
2691 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2692 mEffects[i]->state() == EffectModule::STOPPING) {
2693 mEffects[i]->addEffectToHal_l();
2694 }
2695 }
2696}
2697
Eric Laurentca7cc822012-11-19 14:55:58 -08002698void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2699{
Eric Laurentca7cc822012-11-19 14:55:58 -08002700 String8 result;
2701
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002702 const size_t numEffects = mEffects.size();
2703 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002704
Marco Nelissenb2208842014-02-07 14:00:50 -08002705 if (numEffects) {
2706 bool locked = AudioFlinger::dumpTryLock(mLock);
2707 // failed to lock - AudioFlinger is probably deadlocked
2708 if (!locked) {
2709 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002710 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002711
Andy Hungbded9c82017-11-30 18:47:35 -08002712 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2713 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2714 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2715 (int)inBufferStr.size(), "In buffer ",
2716 (int)outBufferStr.size(), "Out buffer ");
2717 result.appendFormat("\t%s %s %d\n",
2718 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002719 write(fd, result.string(), result.size());
2720
2721 for (size_t i = 0; i < numEffects; ++i) {
2722 sp<EffectModule> effect = mEffects[i];
2723 if (effect != 0) {
2724 effect->dump(fd, args);
2725 }
2726 }
2727
2728 if (locked) {
2729 mLock.unlock();
2730 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002731 } else {
2732 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002733 }
2734}
2735
2736// must be called with ThreadBase::mLock held
2737void AudioFlinger::EffectChain::setEffectSuspended_l(
2738 const effect_uuid_t *type, bool suspend)
2739{
2740 sp<SuspendedEffectDesc> desc;
2741 // use effect type UUID timelow as key as there is no real risk of identical
2742 // timeLow fields among effect type UUIDs.
2743 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2744 if (suspend) {
2745 if (index >= 0) {
2746 desc = mSuspendedEffects.valueAt(index);
2747 } else {
2748 desc = new SuspendedEffectDesc();
2749 desc->mType = *type;
2750 mSuspendedEffects.add(type->timeLow, desc);
2751 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2752 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002753
Eric Laurentca7cc822012-11-19 14:55:58 -08002754 if (desc->mRefCount++ == 0) {
2755 sp<EffectModule> effect = getEffectIfEnabled(type);
2756 if (effect != 0) {
2757 desc->mEffect = effect;
2758 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002759 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002760 }
2761 }
2762 } else {
2763 if (index < 0) {
2764 return;
2765 }
2766 desc = mSuspendedEffects.valueAt(index);
2767 if (desc->mRefCount <= 0) {
2768 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002769 desc->mRefCount = 0;
2770 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002771 }
2772 if (--desc->mRefCount == 0) {
2773 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2774 if (desc->mEffect != 0) {
2775 sp<EffectModule> effect = desc->mEffect.promote();
2776 if (effect != 0) {
2777 effect->setSuspended(false);
2778 effect->lock();
2779 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002780 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002781 effect->setEnabled_l(handle->enabled());
2782 }
2783 effect->unlock();
2784 }
2785 desc->mEffect.clear();
2786 }
2787 mSuspendedEffects.removeItemsAt(index);
2788 }
2789 }
2790}
2791
2792// must be called with ThreadBase::mLock held
2793void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2794{
2795 sp<SuspendedEffectDesc> desc;
2796
2797 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2798 if (suspend) {
2799 if (index >= 0) {
2800 desc = mSuspendedEffects.valueAt(index);
2801 } else {
2802 desc = new SuspendedEffectDesc();
2803 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2804 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2805 }
2806 if (desc->mRefCount++ == 0) {
2807 Vector< sp<EffectModule> > effects;
2808 getSuspendEligibleEffects(effects);
2809 for (size_t i = 0; i < effects.size(); i++) {
2810 setEffectSuspended_l(&effects[i]->desc().type, true);
2811 }
2812 }
2813 } else {
2814 if (index < 0) {
2815 return;
2816 }
2817 desc = mSuspendedEffects.valueAt(index);
2818 if (desc->mRefCount <= 0) {
2819 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2820 desc->mRefCount = 1;
2821 }
2822 if (--desc->mRefCount == 0) {
2823 Vector<const effect_uuid_t *> types;
2824 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2825 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2826 continue;
2827 }
2828 types.add(&mSuspendedEffects.valueAt(i)->mType);
2829 }
2830 for (size_t i = 0; i < types.size(); i++) {
2831 setEffectSuspended_l(types[i], false);
2832 }
2833 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2834 mSuspendedEffects.keyAt(index));
2835 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2836 }
2837 }
2838}
2839
2840
2841// The volume effect is used for automated tests only
2842#ifndef OPENSL_ES_H_
2843static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2844 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2845const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2846#endif //OPENSL_ES_H_
2847
Eric Laurentd8365c52017-07-16 15:27:05 -07002848/* static */
2849bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2850{
2851 // Only NS and AEC are suspended when BtNRec is off
2852 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2853 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2854 return true;
2855 }
2856 return false;
2857}
2858
Eric Laurentca7cc822012-11-19 14:55:58 -08002859bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2860{
2861 // auxiliary effects and visualizer are never suspended on output mix
2862 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2863 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2864 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002865 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2866 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002867 return false;
2868 }
2869 return true;
2870}
2871
2872void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2873 Vector< sp<AudioFlinger::EffectModule> > &effects)
2874{
2875 effects.clear();
2876 for (size_t i = 0; i < mEffects.size(); i++) {
2877 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2878 effects.add(mEffects[i]);
2879 }
2880 }
2881}
2882
2883sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2884 const effect_uuid_t *type)
2885{
2886 sp<EffectModule> effect = getEffectFromType_l(type);
2887 return effect != 0 && effect->isEnabled() ? effect : 0;
2888}
2889
2890void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2891 bool enabled)
2892{
2893 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2894 if (enabled) {
2895 if (index < 0) {
2896 // if the effect is not suspend check if all effects are suspended
2897 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2898 if (index < 0) {
2899 return;
2900 }
2901 if (!isEffectEligibleForSuspend(effect->desc())) {
2902 return;
2903 }
2904 setEffectSuspended_l(&effect->desc().type, enabled);
2905 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2906 if (index < 0) {
2907 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2908 return;
2909 }
2910 }
2911 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2912 effect->desc().type.timeLow);
2913 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002914 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002915 if (desc->mEffect == 0) {
2916 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002917 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002918 effect->setSuspended(true);
2919 }
2920 } else {
2921 if (index < 0) {
2922 return;
2923 }
2924 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2925 effect->desc().type.timeLow);
2926 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2927 desc->mEffect.clear();
2928 effect->setSuspended(false);
2929 }
2930}
2931
Eric Laurent5baf2af2013-09-12 17:37:00 -07002932bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002933{
2934 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002935 return isNonOffloadableEnabled_l();
2936}
2937
2938bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2939{
Eric Laurent813e2a72013-08-31 12:59:48 -07002940 size_t size = mEffects.size();
2941 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002942 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002943 return true;
2944 }
2945 }
2946 return false;
2947}
2948
Eric Laurentaaa44472014-09-12 17:41:50 -07002949void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2950{
2951 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002952 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002953}
2954
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002955void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2956{
2957 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2958 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2959 }
2960 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2961 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2962 }
2963}
2964
2965void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2966{
2967 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2968 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2969 }
2970 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2971 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2972 }
2973}
2974
2975bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002976{
2977 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002978 for (const auto &effect : mEffects) {
2979 if (effect->isProcessImplemented()) {
2980 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002981 }
2982 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002983 // Allow effects without processing.
2984 return true;
2985}
2986
2987bool AudioFlinger::EffectChain::isFastCompatible() const
2988{
2989 Mutex::Autolock _l(mLock);
2990 for (const auto &effect : mEffects) {
2991 if (effect->isProcessImplemented()
2992 && effect->isImplementationSoftware()) {
2993 return false;
2994 }
2995 }
2996 // Allow effects without processing or hw accelerated effects.
2997 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002998}
2999
3000// isCompatibleWithThread_l() must be called with thread->mLock held
3001bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
3002{
3003 Mutex::Autolock _l(mLock);
3004 for (size_t i = 0; i < mEffects.size(); i++) {
3005 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3006 return false;
3007 }
3008 }
3009 return true;
3010}
3011
Eric Laurent6b446ce2019-12-13 10:56:31 -08003012// EffectCallbackInterface implementation
3013status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3014 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3015 sp<EffectHalInterface> *effect) {
3016 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003017 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018 if (effectsFactory != 0) {
3019 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3020 }
3021 return status;
3022}
3023
3024bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003025 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003026 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003027 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003028}
3029
3030status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3031 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003032 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003033}
3034
3035status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
3036 sp<EffectHalInterface> effect) {
3037 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003038 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003039 if (t == nullptr) {
3040 return result;
3041 }
3042 sp <StreamHalInterface> st = t->stream();
3043 if (st == nullptr) {
3044 return result;
3045 }
3046 result = st->addEffect(effect);
3047 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3048 return result;
3049}
3050
3051status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
3052 sp<EffectHalInterface> effect) {
3053 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003054 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003055 if (t == nullptr) {
3056 return result;
3057 }
3058 sp <StreamHalInterface> st = t->stream();
3059 if (st == nullptr) {
3060 return result;
3061 }
3062 result = st->removeEffect(effect);
3063 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3064 return result;
3065}
3066
3067audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003068 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069 if (t == nullptr) {
3070 return AUDIO_IO_HANDLE_NONE;
3071 }
3072 return t->id();
3073}
3074
3075bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003076 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003077 if (t == nullptr) {
3078 return true;
3079 }
3080 return t->isOutput();
3081}
3082
3083bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003084 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003085}
3086
3087bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003088 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003089}
3090
3091bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003092 switch (mThreadType) {
3093 case ThreadBase::OFFLOAD:
3094 case ThreadBase::MMAP_PLAYBACK:
3095 case ThreadBase::MMAP_CAPTURE:
3096 return true;
3097 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003098 return false;
3099 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003100}
3101
3102bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3103 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003104}
3105
3106uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003107 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003108 if (t == nullptr) {
3109 return 0;
3110 }
3111 return t->sampleRate();
3112}
3113
Eric Laurentf1f22e72021-07-13 14:04:14 +02003114audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3115 sp<ThreadBase> t = thread().promote();
3116 if (t == nullptr) {
3117 return AUDIO_CHANNEL_NONE;
3118 }
3119 sp<EffectChain> c = chain().promote();
3120 if (c == nullptr) {
3121 return AUDIO_CHANNEL_NONE;
3122 }
3123
Eric Laurentb62d0362021-10-26 17:40:18 +02003124 if (mThreadType == ThreadBase::SPATIALIZER) {
3125 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3126 if (c->isFirstEffect(id)) {
3127 return t->mixerChannelMask();
3128 } else {
3129 return t->channelMask();
3130 }
3131 } else if (!audio_is_global_session(c->sessionId())) {
3132 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3133 return t->mixerChannelMask();
3134 } else {
3135 return t->channelMask();
3136 }
3137 } else {
3138 return t->channelMask();
3139 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003140 } else {
3141 return t->channelMask();
3142 }
3143}
3144
3145uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003146 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003147}
3148
3149audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003150 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003151 if (t == nullptr) {
3152 return AUDIO_CHANNEL_NONE;
3153 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003154 sp<EffectChain> c = chain().promote();
3155 if (c == nullptr) {
3156 return AUDIO_CHANNEL_NONE;
3157 }
3158
3159 if (mThreadType == ThreadBase::SPATIALIZER) {
3160 if (!audio_is_global_session(c->sessionId())) {
3161 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3162 return t->mixerChannelMask();
3163 } else {
3164 return t->channelMask();
3165 }
3166 } else {
3167 return t->channelMask();
3168 }
3169 } else {
3170 return t->channelMask();
3171 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003172}
3173
Eric Laurentf1f22e72021-07-13 14:04:14 +02003174uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003175 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003176}
3177
jiabineb3bda02020-06-30 14:07:03 -07003178audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003179 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003180 if (t == nullptr) {
3181 return AUDIO_CHANNEL_NONE;
3182 }
3183 return t->hapticChannelMask();
3184}
3185
Eric Laurent6b446ce2019-12-13 10:56:31 -08003186size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003187 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003188 if (t == nullptr) {
3189 return 0;
3190 }
3191 return t->frameCount();
3192}
3193
3194uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003195 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003196 if (t == nullptr) {
3197 return 0;
3198 }
3199 return t->latency_l();
3200}
3201
3202void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003203 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003204 if (t == nullptr) {
3205 return;
3206 }
3207 t->setVolumeForOutput_l(left, right);
3208}
3209
3210void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003211 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
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 }
3216 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3217
Andy Hung328d6772021-01-12 12:32:21 -08003218 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003219 if (c == nullptr) {
3220 return;
3221 }
Eric Laurent41709552019-12-16 19:34:05 -08003222 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3223 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003224}
3225
Eric Laurent41709552019-12-16 19:34:05 -08003226void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003227 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003228 if (t == nullptr) {
3229 return;
3230 }
Eric Laurent41709552019-12-16 19:34:05 -08003231 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3232 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003233}
3234
Eric Laurent41709552019-12-16 19:34:05 -08003235void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003236 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3237
Andy Hung328d6772021-01-12 12:32:21 -08003238 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003239 if (t == nullptr) {
3240 return;
3241 }
3242 t->onEffectDisable();
3243}
3244
3245bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3246 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003247 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003248 if (t == nullptr) {
3249 return false;
3250 }
3251 t->disconnectEffectHandle(handle, unpinIfLast);
3252 return true;
3253}
3254
3255void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003256 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003257 if (c == nullptr) {
3258 return;
3259 }
3260 c->resetVolume_l();
3261
3262}
3263
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003264product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003265 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003266 if (c == nullptr) {
3267 return PRODUCT_STRATEGY_NONE;
3268 }
3269 return c->strategy();
3270}
3271
3272int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003273 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003274 if (c == nullptr) {
3275 return 0;
3276 }
3277 return c->activeTrackCnt();
3278}
3279
Eric Laurentb82e6b72019-11-22 17:25:04 -08003280
3281#undef LOG_TAG
3282#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3283
3284status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3285{
3286 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3287 Mutex::Autolock _l(mProxyLock);
3288 if (status == NO_ERROR) {
3289 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003290 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003291 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003292 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003293 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003294 bs = handle.second->disable(&status);
3295 }
3296 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003297 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003298 }
3299 }
3300 }
3301 ALOGV("%s enable %d status %d", __func__, enabled, status);
3302 return status;
3303}
3304
3305status_t AudioFlinger::DeviceEffectProxy::init(
3306 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3307//For all audio patches
3308//If src or sink device match
3309//If the effect is HW accelerated
3310// if no corresponding effect module
3311// Create EffectModule: mHalEffect
3312//Create and attach EffectHandle
3313//If the effect is not HW accelerated and the patch sink or src is a mixer port
3314// Create Effect on patch input or output thread on session -1
3315//Add EffectHandle to EffectHandle map of Effect Proxy:
3316 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3317 status_t status = NO_ERROR;
3318 for (auto &patch : patches) {
3319 status = onCreatePatch(patch.first, patch.second);
3320 ALOGV("%s onCreatePatch status %d", __func__, status);
3321 if (status == BAD_VALUE) {
3322 return status;
3323 }
3324 }
3325 return status;
3326}
3327
3328status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3329 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3330 status_t status = NAME_NOT_FOUND;
3331 sp<EffectHandle> handle;
3332 // only consider source[0] as this is the only "true" source of a patch
3333 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3334 ALOGV("%s source checkPort status %d", __func__, status);
3335 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3336 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3337 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3338 }
3339 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3340 Mutex::Autolock _l(mProxyLock);
3341 mEffectHandles.emplace(patchHandle, handle);
3342 }
3343 ALOGW_IF(status == BAD_VALUE,
3344 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3345
3346 return status;
3347}
3348
3349status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3350 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3351
3352 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3353 __func__, port->type, port->ext.device.type,
3354 port->ext.device.address, port->id, patch.isSoftware());
3355 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003356 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003357 return NAME_NOT_FOUND;
3358 }
3359 status_t status = NAME_NOT_FOUND;
3360
3361 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3362 Mutex::Autolock _l(mProxyLock);
3363 mDevicePort = *port;
3364 mHalEffect = new EffectModule(mMyCallback,
3365 const_cast<effect_descriptor_t *>(&mDescriptor),
3366 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3367 false /* pinned */, port->id);
3368 if (audio_is_input_device(mDevice.mType)) {
3369 mHalEffect->setInputDevice(mDevice);
3370 } else {
3371 mHalEffect->setDevices({mDevice});
3372 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003373 mHalEffect->configure();
3374
Eric Laurentde8caf42021-08-11 17:19:25 +02003375 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3376 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003377 status = (*handle)->initCheck();
3378 if (status == OK) {
3379 status = mHalEffect->addHandle((*handle).get());
3380 } else {
3381 mHalEffect.clear();
3382 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3383 }
3384 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3385 sp <ThreadBase> thread;
3386 if (audio_port_config_has_input_direction(port)) {
3387 if (patch.isSoftware()) {
3388 thread = patch.mRecord.thread();
3389 } else {
3390 thread = patch.thread().promote();
3391 }
3392 } else {
3393 if (patch.isSoftware()) {
3394 thread = patch.mPlayback.thread();
3395 } else {
3396 thread = patch.thread().promote();
3397 }
3398 }
3399 int enabled;
3400 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3401 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003402 &enabled, &status, false, false /*probe*/,
3403 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003404 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3405 } else {
3406 status = BAD_VALUE;
3407 }
3408 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003409 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003410 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003411 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003412 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003413 bs = (*handle)->disable(&status);
3414 }
3415 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003416 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003417 }
3418 }
3419 return status;
3420}
3421
3422void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003423 sp<EffectHandle> effect;
3424 {
3425 Mutex::Autolock _l(mProxyLock);
3426 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3427 effect = mEffectHandles.at(patchHandle);
3428 mEffectHandles.erase(patchHandle);
3429 }
3430 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003431}
3432
3433
3434size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3435{
3436 Mutex::Autolock _l(mProxyLock);
3437 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003438 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003439 mHalEffect.clear();
3440 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3441 }
3442 return mHalEffect == nullptr ? 0 : 1;
3443}
3444
3445status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3446 sp<EffectHalInterface> effect) {
3447 if (mHalEffect == nullptr) {
3448 return NO_INIT;
3449 }
3450 return mManagerCallback->addEffectToHal(
3451 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3452}
3453
3454status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3455 sp<EffectHalInterface> effect) {
3456 if (mHalEffect == nullptr) {
3457 return NO_INIT;
3458 }
3459 return mManagerCallback->removeEffectFromHal(
3460 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3461}
3462
3463bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3464 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3465 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3466 }
3467 return true;
3468}
3469
3470uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3471 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3472 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3473 return mDevicePort.sample_rate;
3474 }
3475 return DEFAULT_OUTPUT_SAMPLE_RATE;
3476}
3477
3478audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3479 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3480 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3481 return mDevicePort.channel_mask;
3482 }
3483 return AUDIO_CHANNEL_OUT_STEREO;
3484}
3485
3486uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3487 if (isOutput()) {
3488 return audio_channel_count_from_out_mask(channelMask());
3489 }
3490 return audio_channel_count_from_in_mask(channelMask());
3491}
3492
3493void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3494 const Vector<String16> args;
3495 EffectBase::dump(fd, args);
3496
3497 const bool locked = dumpTryLock(mProxyLock);
3498 if (!locked) {
3499 String8 result("DeviceEffectProxy may be deadlocked\n");
3500 write(fd, result.string(), result.size());
3501 }
3502
3503 String8 outStr;
3504 if (mHalEffect != nullptr) {
3505 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3506 } else {
3507 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3508 }
3509 write(fd, outStr.string(), outStr.size());
3510 outStr.clear();
3511
3512 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3513 write(fd, outStr.string(), outStr.size());
3514 outStr.clear();
3515
3516 for (const auto& iter : mEffectHandles) {
3517 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3518 write(fd, outStr.string(), outStr.size());
3519 outStr.clear();
3520 sp<EffectBase> effect = iter.second->effect().promote();
3521 if (effect != nullptr) {
3522 effect->dump(fd, args);
3523 }
3524 }
3525
3526 if (locked) {
3527 mLock.unlock();
3528 }
3529}
3530
3531#undef LOG_TAG
3532#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3533
3534int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3535 return mManagerCallback->newEffectId();
3536}
3537
3538
3539bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3540 EffectHandle *handle, bool unpinIfLast) {
3541 sp<EffectBase> effectBase = handle->effect().promote();
3542 if (effectBase == nullptr) {
3543 return false;
3544 }
3545
3546 sp<EffectModule> effect = effectBase->asEffectModule();
3547 if (effect == nullptr) {
3548 return false;
3549 }
3550
3551 // restore suspended effects if the disconnected handle was enabled and the last one.
3552 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3553 if (remove) {
3554 sp<DeviceEffectProxy> proxy = mProxy.promote();
3555 if (proxy != nullptr) {
3556 proxy->removeEffect(effect);
3557 }
3558 if (handle->enabled()) {
3559 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3560 }
3561 }
3562 return true;
3563}
3564
3565status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3566 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3567 sp<EffectHalInterface> *effect) {
3568 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3569}
3570
3571status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3572 sp<EffectHalInterface> effect) {
3573 sp<DeviceEffectProxy> proxy = mProxy.promote();
3574 if (proxy == nullptr) {
3575 return NO_INIT;
3576 }
3577 return proxy->addEffectToHal(effect);
3578}
3579
3580status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3581 sp<EffectHalInterface> effect) {
3582 sp<DeviceEffectProxy> proxy = mProxy.promote();
3583 if (proxy == nullptr) {
3584 return NO_INIT;
3585 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003586 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003587}
3588
3589bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3590 sp<DeviceEffectProxy> proxy = mProxy.promote();
3591 if (proxy == nullptr) {
3592 return true;
3593 }
3594 return proxy->isOutput();
3595}
3596
3597uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3598 sp<DeviceEffectProxy> proxy = mProxy.promote();
3599 if (proxy == nullptr) {
3600 return DEFAULT_OUTPUT_SAMPLE_RATE;
3601 }
3602 return proxy->sampleRate();
3603}
3604
Eric Laurentf1f22e72021-07-13 14:04:14 +02003605audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3606 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003607 sp<DeviceEffectProxy> proxy = mProxy.promote();
3608 if (proxy == nullptr) {
3609 return AUDIO_CHANNEL_OUT_STEREO;
3610 }
3611 return proxy->channelMask();
3612}
3613
Eric Laurentf1f22e72021-07-13 14:04:14 +02003614uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3615 sp<DeviceEffectProxy> proxy = mProxy.promote();
3616 if (proxy == nullptr) {
3617 return 2;
3618 }
3619 return proxy->channelCount();
3620}
3621
3622audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3623 sp<DeviceEffectProxy> proxy = mProxy.promote();
3624 if (proxy == nullptr) {
3625 return AUDIO_CHANNEL_OUT_STEREO;
3626 }
3627 return proxy->channelMask();
3628}
3629
3630uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003631 sp<DeviceEffectProxy> proxy = mProxy.promote();
3632 if (proxy == nullptr) {
3633 return 2;
3634 }
3635 return proxy->channelCount();
3636}
3637
Eric Laurent76c89f32021-12-03 17:13:23 +01003638void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3639 const sp<EffectBase>& effectBase) {
3640 sp<EffectModule> effect = effectBase->asEffectModule();
3641 if (effect == nullptr) {
3642 return;
3643 }
3644 effect->start();
3645}
3646
3647void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3648 const sp<EffectBase>& effectBase) {
3649 sp<EffectModule> effect = effectBase->asEffectModule();
3650 if (effect == nullptr) {
3651 return;
3652 }
3653 effect->stop();
3654}
3655
Glenn Kasten63238ef2015-03-02 15:50:29 -08003656} // namespace android