blob: 193e270e389d78a0d78a4b13e409f529619e8b9d [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);
1764 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001765 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001766 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001767 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001769 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 return;
1771 }
Glenn Kastene75da402013-11-20 13:54:52 -08001772 new(mCblk) effect_param_cblk_t();
1773 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001774}
1775
1776AudioFlinger::EffectHandle::~EffectHandle()
1777{
1778 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 disconnect(false);
1780}
1781
Andy Hungc747c532022-03-07 21:41:14 -08001782// Creates an association between Binder code to name for IEffect.
1783#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1784BINDER_METHOD_ENTRY(enable) \
1785BINDER_METHOD_ENTRY(disable) \
1786BINDER_METHOD_ENTRY(command) \
1787BINDER_METHOD_ENTRY(disconnect) \
1788BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001789BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001790
1791// singleton for Binder Method Statistics for IEffect
1792mediautils::MethodStatistics<int>& getIEffectStatistics() {
1793 using Code = int;
1794
1795#pragma push_macro("BINDER_METHOD_ENTRY")
1796#undef BINDER_METHOD_ENTRY
1797#define BINDER_METHOD_ENTRY(ENTRY) \
1798 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1799
1800 static mediautils::MethodStatistics<Code> methodStatistics{
1801 IEFFECT_BINDER_METHOD_MACRO_LIST
1802 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1803 };
1804#pragma pop_macro("BINDER_METHOD_ENTRY")
1805
1806 return methodStatistics;
1807}
1808
1809status_t AudioFlinger::EffectHandle::onTransact(
1810 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001811 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1812 mediautils::TimeCheck check(
1813 std::string("IEffect::").append(methodName),
1814 [code](bool timeout, float elapsedMs) {
1815 if (timeout) {
1816 ; // we don't timeout right now on the effect interface.
1817 } else {
1818 getIEffectStatistics().event(code, elapsedMs);
1819 }
1820 }, 0 /* timeoutMs */);
Andy Hungc747c532022-03-07 21:41:14 -08001821 return BnEffect::onTransact(code, data, reply, flags);
1822}
1823
Glenn Kastene75da402013-11-20 13:54:52 -08001824status_t AudioFlinger::EffectHandle::initCheck()
1825{
1826 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1827}
1828
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001829#define RETURN(code) \
1830 *_aidl_return = (code); \
1831 return Status::ok();
1832
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001833#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1834 ({ \
1835 auto _tmp = (exp); \
1836 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1837 std::move(_tmp.value()); \
1838 })
1839
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001840Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001841{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001842 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001844 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001846 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001847 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001848 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851
1852 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001854 }
1855
1856 mEnabled = true;
1857
Eric Laurent6c796322019-04-09 14:13:17 -07001858 status_t status = effect->updatePolicyState();
1859 if (status != NO_ERROR) {
1860 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001861 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001862 }
1863
Eric Laurent6b446ce2019-12-13 10:56:31 -08001864 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865
1866 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001867 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001868 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 }
1870
Eric Laurent6b446ce2019-12-13 10:56:31 -08001871 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 mEnabled = false;
1874 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001875 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001876}
1877
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001878Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001879{
1880 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001881 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001882 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001883 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001884 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001885 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001886 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001887 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001888 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001889
1890 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001891 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001892 }
1893 mEnabled = false;
1894
Eric Laurent6c796322019-04-09 14:13:17 -07001895 effect->updatePolicyState();
1896
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001897 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001898 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001899 }
1900
Eric Laurent6b446ce2019-12-13 10:56:31 -08001901 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001902 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001903}
1904
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001906{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001907 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001908 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001910}
1911
1912void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1913{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001914 AutoMutex _l(mLock);
1915 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1916 if (mDisconnected) {
1917 if (unpinIfLast) {
1918 android_errorWriteLog(0x534e4554, "32707507");
1919 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001920 return;
1921 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001922 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001923 {
Eric Laurent41709552019-12-16 19:34:05 -08001924 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001925 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001926 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001927 ALOGW("%s Effect handle %p disconnected after thread destruction",
1928 __func__, this);
1929 }
1930 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001931 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001932 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001933
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 if (mClient != 0) {
1935 if (mCblk != NULL) {
1936 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1937 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1938 }
1939 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001940 // Client destructor must run with AudioFlinger client mutex locked
1941 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 mClient.clear();
1943 }
1944}
1945
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001946Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1947 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1948 return Status::ok();
1949}
1950
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001951Status AudioFlinger::EffectHandle::getConfig(
1952 media::EffectConfig* _config, int32_t* _aidl_return) {
1953 AutoMutex _l(mLock);
1954 sp<EffectBase> effect = mEffect.promote();
1955 if (effect == nullptr || mDisconnected) {
1956 RETURN(DEAD_OBJECT);
1957 }
1958 sp<EffectModule> effectModule = effect->asEffectModule();
1959 if (effectModule == nullptr) {
1960 RETURN(INVALID_OPERATION);
1961 }
1962 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1963 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1964 bool isOutput;
1965 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1966 if (status == NO_ERROR) {
1967 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1968 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1969 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1970 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1971 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1972 _config->isOnInputStream = !isOutput;
1973 }
1974 RETURN(status);
1975}
1976
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001977Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1978 const std::vector<uint8_t>& cmdData,
1979 int32_t maxResponseSize,
1980 std::vector<uint8_t>* response,
1981 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001982{
1983 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001984 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001985
Eric Laurentc7ab3092017-06-15 18:43:46 -07001986 // reject commands reserved for internal use by audio framework if coming from outside
1987 // of audioserver
1988 switch(cmdCode) {
1989 case EFFECT_CMD_ENABLE:
1990 case EFFECT_CMD_DISABLE:
1991 case EFFECT_CMD_SET_PARAM:
1992 case EFFECT_CMD_SET_PARAM_DEFERRED:
1993 case EFFECT_CMD_SET_PARAM_COMMIT:
1994 case EFFECT_CMD_GET_PARAM:
1995 break;
1996 default:
1997 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1998 break;
1999 }
2000 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002001 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002002 }
2003
Eric Laurent1ffc5852016-12-15 14:46:09 -08002004 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002005 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002006 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002007 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002008 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002009 writeToBuffer(NO_ERROR, response);
2010 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002011 } else if (cmdCode == EFFECT_CMD_DISABLE) {
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 disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002018 }
2019
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002020 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002021 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002022 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002023 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002024 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002025 // only get parameter command is permitted for applications not controlling the effect
2026 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002027 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002028 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002029
2030 // handle commands that are not forwarded transparently to effect engine
2031 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002032 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002033 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002034 }
2035
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002036 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002037 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002038 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002039 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002040 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002041
Eric Laurentca7cc822012-11-19 14:55:58 -08002042 // No need to trylock() here as this function is executed in the binder thread serving a
2043 // particular client process: no risk to block the whole media server process or mixer
2044 // threads if we are stuck here
2045 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002046 // keep local copy of index in case of client corruption b/32220769
2047 const uint32_t clientIndex = mCblk->clientIndex;
2048 const uint32_t serverIndex = mCblk->serverIndex;
2049 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2050 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002051 mCblk->serverIndex = 0;
2052 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002053 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 }
2055 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002056 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002057 for (uint32_t index = serverIndex; index < clientIndex;) {
2058 int *p = (int *)(mBuffer + index);
2059 const int size = *p++;
2060 if (size < 0
2061 || size > EFFECT_PARAM_BUFFER_SIZE
2062 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002064 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002065 break;
2066 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002067
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002068 std::copy(reinterpret_cast<const uint8_t*>(p),
2069 reinterpret_cast<const uint8_t*>(p) + size,
2070 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002071
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002072 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002073 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002074 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002075 sizeof(int),
2076 &replyBuffer);
2077 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002078
2079 // verify shared memory: server index shouldn't change; client index can't go back.
2080 if (serverIndex != mCblk->serverIndex
2081 || clientIndex > mCblk->clientIndex) {
2082 android_errorWriteLog(0x534e4554, "32220769");
2083 status = BAD_VALUE;
2084 break;
2085 }
2086
Eric Laurentca7cc822012-11-19 14:55:58 -08002087 // stop at first error encountered
2088 if (ret != NO_ERROR) {
2089 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002090 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002091 break;
2092 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002093 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 break;
2095 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002096 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002097 }
2098 mCblk->serverIndex = 0;
2099 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002100 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002101 }
2102
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002103 status_t status = effect->command(cmdCode,
2104 cmdData,
2105 maxResponseSize,
2106 response);
2107 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002108}
2109
2110void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2111{
2112 ALOGV("setControl %p control %d", this, hasControl);
2113
2114 mHasControl = hasControl;
2115 mEnabled = enabled;
2116
2117 if (signal && mEffectClient != 0) {
2118 mEffectClient->controlStatusChanged(hasControl);
2119 }
2120}
2121
2122void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002123 const std::vector<uint8_t>& cmdData,
2124 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002125{
2126 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002127 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002128 }
2129}
2130
2131
2132
2133void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2134{
2135 if (mEffectClient != 0) {
2136 mEffectClient->enableStatusChanged(enabled);
2137 }
2138}
2139
Eric Laurentde8caf42021-08-11 17:19:25 +02002140void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2141{
2142 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2143 mEffectClient->framesProcessed(frames);
2144 }
2145}
2146
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002147void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002148{
2149 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2150
Marco Nelissenb2208842014-02-07 14:00:50 -08002151 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002152 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002153 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002154 mHasControl ? "yes" : "no",
2155 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002156 mCblk ? mCblk->clientIndex : 0,
2157 mCblk ? mCblk->serverIndex : 0
2158 );
2159
2160 if (locked) {
2161 mCblk->lock.unlock();
2162 }
2163}
2164
2165#undef LOG_TAG
2166#define LOG_TAG "AudioFlinger::EffectChain"
2167
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002168AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2169 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002170 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002171 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002172 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002173 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002174{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002175 sp<ThreadBase> p = thread.promote();
2176 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002177 return;
2178 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002179 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002180 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2181 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002182}
2183
2184AudioFlinger::EffectChain::~EffectChain()
2185{
Eric Laurentca7cc822012-11-19 14:55:58 -08002186}
2187
2188// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2189sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2190 effect_descriptor_t *descriptor)
2191{
2192 size_t size = mEffects.size();
2193
2194 for (size_t i = 0; i < size; i++) {
2195 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2196 return mEffects[i];
2197 }
2198 }
2199 return 0;
2200}
2201
2202// getEffectFromId_l() must be called with ThreadBase::mLock held
2203sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2204{
2205 size_t size = mEffects.size();
2206
2207 for (size_t i = 0; i < size; i++) {
2208 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2209 if (id == 0 || mEffects[i]->id() == id) {
2210 return mEffects[i];
2211 }
2212 }
2213 return 0;
2214}
2215
2216// getEffectFromType_l() must be called with ThreadBase::mLock held
2217sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2218 const effect_uuid_t *type)
2219{
2220 size_t size = mEffects.size();
2221
2222 for (size_t i = 0; i < size; i++) {
2223 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2224 return mEffects[i];
2225 }
2226 }
2227 return 0;
2228}
2229
Eric Laurent6c796322019-04-09 14:13:17 -07002230std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2231{
2232 std::vector<int> ids;
2233 Mutex::Autolock _l(mLock);
2234 for (size_t i = 0; i < mEffects.size(); i++) {
2235 ids.push_back(mEffects[i]->id());
2236 }
2237 return ids;
2238}
2239
Eric Laurentca7cc822012-11-19 14:55:58 -08002240void AudioFlinger::EffectChain::clearInputBuffer()
2241{
2242 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002243 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002244}
2245
2246// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002247void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002248{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002249 if (mInBuffer == NULL) {
2250 return;
2251 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002252 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2253 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002254
Eric Laurent6b446ce2019-12-13 10:56:31 -08002255 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002256 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002257}
2258
2259// Must be called with EffectChain::mLock locked
2260void AudioFlinger::EffectChain::process_l()
2261{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002262 // never process effects when:
2263 // - on an OFFLOAD thread
2264 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002265 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002266 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002267 bool tracksOnSession = (trackCnt() != 0);
2268
2269 if (!tracksOnSession && mTailBufferCount == 0) {
2270 doProcess = false;
2271 }
2272
2273 if (activeTrackCnt() == 0) {
2274 // if no track is active and the effect tail has not been rendered,
2275 // the input buffer must be cleared here as the mixer process will not do it
2276 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002277 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002278 if (mTailBufferCount > 0) {
2279 mTailBufferCount--;
2280 }
2281 }
2282 }
2283 }
2284
2285 size_t size = mEffects.size();
2286 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002287 // Only the input and output buffers of the chain can be external,
2288 // and 'update' / 'commit' do nothing for allocated buffers, thus
2289 // it's not needed to consider any other buffers here.
2290 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002291 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2292 mOutBuffer->update();
2293 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002294 for (size_t i = 0; i < size; i++) {
2295 mEffects[i]->process();
2296 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002297 mInBuffer->commit();
2298 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2299 mOutBuffer->commit();
2300 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002301 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002302 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002303 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002304 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2305 }
2306 if (doResetVolume) {
2307 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 }
2309}
2310
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002311// createEffect_l() must be called with ThreadBase::mLock held
2312status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002313 effect_descriptor_t *desc,
2314 int id,
2315 audio_session_t sessionId,
2316 bool pinned)
2317{
2318 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002319 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002320 status_t lStatus = effect->status();
2321 if (lStatus == NO_ERROR) {
2322 lStatus = addEffect_ll(effect);
2323 }
2324 if (lStatus != NO_ERROR) {
2325 effect.clear();
2326 }
2327 return lStatus;
2328}
2329
2330// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002331status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2332{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002333 Mutex::Autolock _l(mLock);
2334 return addEffect_ll(effect);
2335}
2336// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2337status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2338{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002339 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002340
Eric Laurentb62d0362021-10-26 17:40:18 +02002341 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002342 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2343 // Auxiliary effects are inserted at the beginning of mEffects vector as
2344 // they are processed first and accumulated in chain input buffer
2345 mEffects.insertAt(effect, 0);
2346
2347 // the input buffer for auxiliary effect contains mono samples in
2348 // 32 bit format. This is to avoid saturation in AudoMixer
2349 // accumulation stage. Saturation is done in EffectModule::process() before
2350 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002351 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002352 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002353#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002354 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002355 numSamples * sizeof(float), &halBuffer);
2356#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002357 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002358 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002359#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002360 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002361
2362 effect->configure();
2363
Mikhail Naganov022b9952017-01-04 16:36:51 -08002364 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002365 // auxiliary effects output samples to chain input buffer for further processing
2366 // by insert effects
2367 effect->setOutBuffer(mInBuffer);
2368 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002369 ssize_t idx_insert = getInsertIndex(desc);
2370 if (idx_insert < 0) {
2371 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002372 }
2373
Eric Laurentb62d0362021-10-26 17:40:18 +02002374 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002375 mEffects.insertAt(effect, idx_insert);
2376
2377 effect->configure();
2378
Eric Laurentb62d0362021-10-26 17:40:18 +02002379 // - By default:
2380 // All effects read samples from chain input buffer.
2381 // The last effect in the chain, writes samples to chain output buffer,
2382 // otherwise to chain input buffer
2383 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2384 // The spatializer effect (first effect) reads samples from the input buffer
2385 // and writes samples to the output buffer.
2386 // All other effects read and writes samples to the output buffer
2387 if (mEffectCallback->isSpatializer()
2388 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002389 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002390 if (idx_insert == 0) {
2391 if (previousSize != 0) {
2392 mEffects[1]->configure();
2393 mEffects[1]->setInBuffer(mOutBuffer);
2394 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2395 }
2396 effect->setInBuffer(mInBuffer);
2397 } else {
2398 effect->setInBuffer(mOutBuffer);
2399 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002400 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002401 effect->setInBuffer(mInBuffer);
2402 if (idx_insert == previousSize) {
2403 if (idx_insert != 0) {
2404 mEffects[idx_insert-1]->configure();
2405 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2406 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2407 }
2408 effect->setOutBuffer(mOutBuffer);
2409 } else {
2410 effect->setOutBuffer(mInBuffer);
2411 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002412 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002413 ALOGV("%s effect %p, added in chain %p at rank %zu",
2414 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002415 }
2416 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002417
Eric Laurentca7cc822012-11-19 14:55:58 -08002418 return NO_ERROR;
2419}
2420
Eric Laurentb62d0362021-10-26 17:40:18 +02002421ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2422 // Insert effects are inserted at the end of mEffects vector as they are processed
2423 // after track and auxiliary effects.
2424 // Insert effect order as a function of indicated preference:
2425 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2426 // another effect is present
2427 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2428 // last effect claiming first position
2429 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2430 // first effect claiming last position
2431 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2432 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2433 // already present
2434 // Spatializer or Downmixer effects are inserted in first position because
2435 // they adapt the channel count for all other effects in the chain
2436 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2437 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2438 return 0;
2439 }
2440
2441 size_t size = mEffects.size();
2442 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2443 ssize_t idx_insert;
2444 ssize_t idx_insert_first = -1;
2445 ssize_t idx_insert_last = -1;
2446
2447 idx_insert = size;
2448 for (size_t i = 0; i < size; i++) {
2449 effect_descriptor_t d = mEffects[i]->desc();
2450 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2451 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2452 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2453 // check invalid effect chaining combinations
2454 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2455 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2456 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2457 __func__, desc.name, d.name);
2458 return -1;
2459 }
2460 // remember position of first insert effect and by default
2461 // select this as insert position for new effect
2462 if (idx_insert == size) {
2463 idx_insert = i;
2464 }
2465 // remember position of last insert effect claiming
2466 // first position
2467 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2468 idx_insert_first = i;
2469 }
2470 // remember position of first insert effect claiming
2471 // last position
2472 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2473 idx_insert_last == -1) {
2474 idx_insert_last = i;
2475 }
2476 }
2477 }
2478
2479 // modify idx_insert from first position if needed
2480 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2481 if (idx_insert_last != -1) {
2482 idx_insert = idx_insert_last;
2483 } else {
2484 idx_insert = size;
2485 }
2486 } else {
2487 if (idx_insert_first != -1) {
2488 idx_insert = idx_insert_first + 1;
2489 }
2490 }
2491 return idx_insert;
2492}
2493
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002494// removeEffect_l() must be called with ThreadBase::mLock held
2495size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2496 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002497{
2498 Mutex::Autolock _l(mLock);
2499 size_t size = mEffects.size();
2500 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2501
2502 for (size_t i = 0; i < size; i++) {
2503 if (effect == mEffects[i]) {
2504 // calling stop here will remove pre-processing effect from the audio HAL.
2505 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2506 // the middle of a read from audio HAL
2507 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2508 mEffects[i]->state() == EffectModule::STOPPING) {
2509 mEffects[i]->stop();
2510 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002511 if (release) {
2512 mEffects[i]->release_l();
2513 }
2514
Mikhail Naganov022b9952017-01-04 16:36:51 -08002515 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002516 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002517 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002518 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002519 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002520 }
2521 }
2522 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002523
2524 // make sure the input buffer configuration for the new first effect in the chain
2525 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2526 if (i == 0 && size > 1) {
2527 mEffects[0]->configure();
2528 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002529 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002530 }
2531
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002532 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002533 this, i);
2534 break;
2535 }
2536 }
2537
2538 return mEffects.size();
2539}
2540
jiabin8f278ee2019-11-11 12:16:27 -08002541// setDevices_l() must be called with ThreadBase::mLock held
2542void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002543{
2544 size_t size = mEffects.size();
2545 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002546 mEffects[i]->setDevices(devices);
2547 }
2548}
2549
2550// setInputDevice_l() must be called with ThreadBase::mLock held
2551void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2552{
2553 size_t size = mEffects.size();
2554 for (size_t i = 0; i < size; i++) {
2555 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002556 }
2557}
2558
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002559// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002560void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2561{
2562 size_t size = mEffects.size();
2563 for (size_t i = 0; i < size; i++) {
2564 mEffects[i]->setMode(mode);
2565 }
2566}
2567
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002568// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002569void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2570{
2571 size_t size = mEffects.size();
2572 for (size_t i = 0; i < size; i++) {
2573 mEffects[i]->setAudioSource(source);
2574 }
2575}
2576
Zhou Songd505c642020-02-20 16:35:37 +08002577bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2578 for (const auto &effect : mEffects) {
2579 if (effect->isVolumeControlEnabled()) return true;
2580 }
2581 return false;
2582}
2583
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002584// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002585bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002586{
2587 uint32_t newLeft = *left;
2588 uint32_t newRight = *right;
2589 bool hasControl = false;
2590 int ctrlIdx = -1;
2591 size_t size = mEffects.size();
2592
2593 // first update volume controller
2594 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002595 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002596 ctrlIdx = i - 1;
2597 hasControl = true;
2598 break;
2599 }
2600 }
2601
Eric Laurentfa1e1232016-08-02 19:01:49 -07002602 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002603 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002604 if (hasControl) {
2605 *left = mNewLeftVolume;
2606 *right = mNewRightVolume;
2607 }
2608 return hasControl;
2609 }
2610
2611 mVolumeCtrlIdx = ctrlIdx;
2612 mLeftVolume = newLeft;
2613 mRightVolume = newRight;
2614
2615 // second get volume update from volume controller
2616 if (ctrlIdx >= 0) {
2617 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2618 mNewLeftVolume = newLeft;
2619 mNewRightVolume = newRight;
2620 }
2621 // then indicate volume to all other effects in chain.
2622 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002623 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002624 uint32_t lVol = newLeft;
2625 uint32_t rVol = newRight;
2626
2627 for (size_t i = 0; i < size; i++) {
2628 if ((int)i == ctrlIdx) {
2629 continue;
2630 }
2631 // this also works for ctrlIdx == -1 when there is no volume controller
2632 if ((int)i > ctrlIdx) {
2633 lVol = *left;
2634 rVol = *right;
2635 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002636 // Pass requested volume directly if this is volume monitor module
2637 if (mEffects[i]->isVolumeMonitor()) {
2638 mEffects[i]->setVolume(left, right, false);
2639 } else {
2640 mEffects[i]->setVolume(&lVol, &rVol, false);
2641 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002642 }
2643 *left = newLeft;
2644 *right = newRight;
2645
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002646 setVolumeForOutput_l(*left, *right);
2647
Eric Laurentca7cc822012-11-19 14:55:58 -08002648 return hasControl;
2649}
2650
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002651// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002652void AudioFlinger::EffectChain::resetVolume_l()
2653{
Eric Laurente7449bf2016-08-03 18:44:07 -07002654 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2655 uint32_t left = mLeftVolume;
2656 uint32_t right = mRightVolume;
2657 (void)setVolume_l(&left, &right, true);
2658 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002659}
2660
jiabineb3bda02020-06-30 14:07:03 -07002661// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2662bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2663{
2664 for (size_t i = 0; i < mEffects.size(); ++i) {
2665 if (mEffects[i]->isHapticGenerator()) {
2666 return true;
2667 }
2668 }
2669 return false;
2670}
2671
jiabine70bc7f2020-06-30 22:07:55 -07002672void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2673{
2674 Mutex::Autolock _l(mLock);
2675 for (size_t i = 0; i < mEffects.size(); ++i) {
2676 mEffects[i]->setHapticIntensity(id, intensity);
2677 }
2678}
2679
Eric Laurent1b928682014-10-02 19:41:47 -07002680void AudioFlinger::EffectChain::syncHalEffectsState()
2681{
2682 Mutex::Autolock _l(mLock);
2683 for (size_t i = 0; i < mEffects.size(); i++) {
2684 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2685 mEffects[i]->state() == EffectModule::STOPPING) {
2686 mEffects[i]->addEffectToHal_l();
2687 }
2688 }
2689}
2690
Eric Laurentca7cc822012-11-19 14:55:58 -08002691void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2692{
Eric Laurentca7cc822012-11-19 14:55:58 -08002693 String8 result;
2694
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002695 const size_t numEffects = mEffects.size();
2696 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002697
Marco Nelissenb2208842014-02-07 14:00:50 -08002698 if (numEffects) {
2699 bool locked = AudioFlinger::dumpTryLock(mLock);
2700 // failed to lock - AudioFlinger is probably deadlocked
2701 if (!locked) {
2702 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002703 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002704
Andy Hungbded9c82017-11-30 18:47:35 -08002705 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2706 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2707 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2708 (int)inBufferStr.size(), "In buffer ",
2709 (int)outBufferStr.size(), "Out buffer ");
2710 result.appendFormat("\t%s %s %d\n",
2711 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002712 write(fd, result.string(), result.size());
2713
2714 for (size_t i = 0; i < numEffects; ++i) {
2715 sp<EffectModule> effect = mEffects[i];
2716 if (effect != 0) {
2717 effect->dump(fd, args);
2718 }
2719 }
2720
2721 if (locked) {
2722 mLock.unlock();
2723 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002724 } else {
2725 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002726 }
2727}
2728
2729// must be called with ThreadBase::mLock held
2730void AudioFlinger::EffectChain::setEffectSuspended_l(
2731 const effect_uuid_t *type, bool suspend)
2732{
2733 sp<SuspendedEffectDesc> desc;
2734 // use effect type UUID timelow as key as there is no real risk of identical
2735 // timeLow fields among effect type UUIDs.
2736 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2737 if (suspend) {
2738 if (index >= 0) {
2739 desc = mSuspendedEffects.valueAt(index);
2740 } else {
2741 desc = new SuspendedEffectDesc();
2742 desc->mType = *type;
2743 mSuspendedEffects.add(type->timeLow, desc);
2744 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2745 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002746
Eric Laurentca7cc822012-11-19 14:55:58 -08002747 if (desc->mRefCount++ == 0) {
2748 sp<EffectModule> effect = getEffectIfEnabled(type);
2749 if (effect != 0) {
2750 desc->mEffect = effect;
2751 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002752 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002753 }
2754 }
2755 } else {
2756 if (index < 0) {
2757 return;
2758 }
2759 desc = mSuspendedEffects.valueAt(index);
2760 if (desc->mRefCount <= 0) {
2761 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002762 desc->mRefCount = 0;
2763 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002764 }
2765 if (--desc->mRefCount == 0) {
2766 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2767 if (desc->mEffect != 0) {
2768 sp<EffectModule> effect = desc->mEffect.promote();
2769 if (effect != 0) {
2770 effect->setSuspended(false);
2771 effect->lock();
2772 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002773 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002774 effect->setEnabled_l(handle->enabled());
2775 }
2776 effect->unlock();
2777 }
2778 desc->mEffect.clear();
2779 }
2780 mSuspendedEffects.removeItemsAt(index);
2781 }
2782 }
2783}
2784
2785// must be called with ThreadBase::mLock held
2786void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2787{
2788 sp<SuspendedEffectDesc> desc;
2789
2790 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2791 if (suspend) {
2792 if (index >= 0) {
2793 desc = mSuspendedEffects.valueAt(index);
2794 } else {
2795 desc = new SuspendedEffectDesc();
2796 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2797 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2798 }
2799 if (desc->mRefCount++ == 0) {
2800 Vector< sp<EffectModule> > effects;
2801 getSuspendEligibleEffects(effects);
2802 for (size_t i = 0; i < effects.size(); i++) {
2803 setEffectSuspended_l(&effects[i]->desc().type, true);
2804 }
2805 }
2806 } else {
2807 if (index < 0) {
2808 return;
2809 }
2810 desc = mSuspendedEffects.valueAt(index);
2811 if (desc->mRefCount <= 0) {
2812 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2813 desc->mRefCount = 1;
2814 }
2815 if (--desc->mRefCount == 0) {
2816 Vector<const effect_uuid_t *> types;
2817 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2818 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2819 continue;
2820 }
2821 types.add(&mSuspendedEffects.valueAt(i)->mType);
2822 }
2823 for (size_t i = 0; i < types.size(); i++) {
2824 setEffectSuspended_l(types[i], false);
2825 }
2826 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2827 mSuspendedEffects.keyAt(index));
2828 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2829 }
2830 }
2831}
2832
2833
2834// The volume effect is used for automated tests only
2835#ifndef OPENSL_ES_H_
2836static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2837 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2838const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2839#endif //OPENSL_ES_H_
2840
Eric Laurentd8365c52017-07-16 15:27:05 -07002841/* static */
2842bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2843{
2844 // Only NS and AEC are suspended when BtNRec is off
2845 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2846 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2847 return true;
2848 }
2849 return false;
2850}
2851
Eric Laurentca7cc822012-11-19 14:55:58 -08002852bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2853{
2854 // auxiliary effects and visualizer are never suspended on output mix
2855 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2856 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2857 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002858 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2859 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002860 return false;
2861 }
2862 return true;
2863}
2864
2865void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2866 Vector< sp<AudioFlinger::EffectModule> > &effects)
2867{
2868 effects.clear();
2869 for (size_t i = 0; i < mEffects.size(); i++) {
2870 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2871 effects.add(mEffects[i]);
2872 }
2873 }
2874}
2875
2876sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2877 const effect_uuid_t *type)
2878{
2879 sp<EffectModule> effect = getEffectFromType_l(type);
2880 return effect != 0 && effect->isEnabled() ? effect : 0;
2881}
2882
2883void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2884 bool enabled)
2885{
2886 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2887 if (enabled) {
2888 if (index < 0) {
2889 // if the effect is not suspend check if all effects are suspended
2890 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2891 if (index < 0) {
2892 return;
2893 }
2894 if (!isEffectEligibleForSuspend(effect->desc())) {
2895 return;
2896 }
2897 setEffectSuspended_l(&effect->desc().type, enabled);
2898 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2899 if (index < 0) {
2900 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2901 return;
2902 }
2903 }
2904 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2905 effect->desc().type.timeLow);
2906 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002907 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002908 if (desc->mEffect == 0) {
2909 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002910 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002911 effect->setSuspended(true);
2912 }
2913 } else {
2914 if (index < 0) {
2915 return;
2916 }
2917 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2918 effect->desc().type.timeLow);
2919 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2920 desc->mEffect.clear();
2921 effect->setSuspended(false);
2922 }
2923}
2924
Eric Laurent5baf2af2013-09-12 17:37:00 -07002925bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002926{
2927 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002928 return isNonOffloadableEnabled_l();
2929}
2930
2931bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2932{
Eric Laurent813e2a72013-08-31 12:59:48 -07002933 size_t size = mEffects.size();
2934 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002935 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002936 return true;
2937 }
2938 }
2939 return false;
2940}
2941
Eric Laurentaaa44472014-09-12 17:41:50 -07002942void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2943{
2944 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002945 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002946}
2947
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002948void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2949{
2950 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2951 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2952 }
2953 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2954 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2955 }
2956}
2957
2958void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2959{
2960 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2961 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2962 }
2963 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2964 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2965 }
2966}
2967
2968bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002969{
2970 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002971 for (const auto &effect : mEffects) {
2972 if (effect->isProcessImplemented()) {
2973 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002974 }
2975 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002976 // Allow effects without processing.
2977 return true;
2978}
2979
2980bool AudioFlinger::EffectChain::isFastCompatible() const
2981{
2982 Mutex::Autolock _l(mLock);
2983 for (const auto &effect : mEffects) {
2984 if (effect->isProcessImplemented()
2985 && effect->isImplementationSoftware()) {
2986 return false;
2987 }
2988 }
2989 // Allow effects without processing or hw accelerated effects.
2990 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002991}
2992
2993// isCompatibleWithThread_l() must be called with thread->mLock held
2994bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2995{
2996 Mutex::Autolock _l(mLock);
2997 for (size_t i = 0; i < mEffects.size(); i++) {
2998 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2999 return false;
3000 }
3001 }
3002 return true;
3003}
3004
Eric Laurent6b446ce2019-12-13 10:56:31 -08003005// EffectCallbackInterface implementation
3006status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3007 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3008 sp<EffectHalInterface> *effect) {
3009 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003010 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003011 if (effectsFactory != 0) {
3012 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3013 }
3014 return status;
3015}
3016
3017bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003018 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003019 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003020 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003021}
3022
3023status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3024 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003025 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003026}
3027
3028status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
3029 sp<EffectHalInterface> effect) {
3030 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003031 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003032 if (t == nullptr) {
3033 return result;
3034 }
3035 sp <StreamHalInterface> st = t->stream();
3036 if (st == nullptr) {
3037 return result;
3038 }
3039 result = st->addEffect(effect);
3040 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3041 return result;
3042}
3043
3044status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
3045 sp<EffectHalInterface> effect) {
3046 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003047 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003048 if (t == nullptr) {
3049 return result;
3050 }
3051 sp <StreamHalInterface> st = t->stream();
3052 if (st == nullptr) {
3053 return result;
3054 }
3055 result = st->removeEffect(effect);
3056 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3057 return result;
3058}
3059
3060audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003061 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062 if (t == nullptr) {
3063 return AUDIO_IO_HANDLE_NONE;
3064 }
3065 return t->id();
3066}
3067
3068bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003069 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003070 if (t == nullptr) {
3071 return true;
3072 }
3073 return t->isOutput();
3074}
3075
3076bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003077 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003078}
3079
3080bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003081 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003082}
3083
3084bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003085 switch (mThreadType) {
3086 case ThreadBase::OFFLOAD:
3087 case ThreadBase::MMAP_PLAYBACK:
3088 case ThreadBase::MMAP_CAPTURE:
3089 return true;
3090 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003091 return false;
3092 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003093}
3094
3095bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3096 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003097}
3098
3099uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003100 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003101 if (t == nullptr) {
3102 return 0;
3103 }
3104 return t->sampleRate();
3105}
3106
Eric Laurentf1f22e72021-07-13 14:04:14 +02003107audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3108 sp<ThreadBase> t = thread().promote();
3109 if (t == nullptr) {
3110 return AUDIO_CHANNEL_NONE;
3111 }
3112 sp<EffectChain> c = chain().promote();
3113 if (c == nullptr) {
3114 return AUDIO_CHANNEL_NONE;
3115 }
3116
Eric Laurentb62d0362021-10-26 17:40:18 +02003117 if (mThreadType == ThreadBase::SPATIALIZER) {
3118 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3119 if (c->isFirstEffect(id)) {
3120 return t->mixerChannelMask();
3121 } else {
3122 return t->channelMask();
3123 }
3124 } else if (!audio_is_global_session(c->sessionId())) {
3125 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3126 return t->mixerChannelMask();
3127 } else {
3128 return t->channelMask();
3129 }
3130 } else {
3131 return t->channelMask();
3132 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003133 } else {
3134 return t->channelMask();
3135 }
3136}
3137
3138uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003139 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003140}
3141
3142audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003143 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003144 if (t == nullptr) {
3145 return AUDIO_CHANNEL_NONE;
3146 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003147 sp<EffectChain> c = chain().promote();
3148 if (c == nullptr) {
3149 return AUDIO_CHANNEL_NONE;
3150 }
3151
3152 if (mThreadType == ThreadBase::SPATIALIZER) {
3153 if (!audio_is_global_session(c->sessionId())) {
3154 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3155 return t->mixerChannelMask();
3156 } else {
3157 return t->channelMask();
3158 }
3159 } else {
3160 return t->channelMask();
3161 }
3162 } else {
3163 return t->channelMask();
3164 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003165}
3166
Eric Laurentf1f22e72021-07-13 14:04:14 +02003167uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003168 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003169}
3170
jiabineb3bda02020-06-30 14:07:03 -07003171audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003172 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003173 if (t == nullptr) {
3174 return AUDIO_CHANNEL_NONE;
3175 }
3176 return t->hapticChannelMask();
3177}
3178
Eric Laurent6b446ce2019-12-13 10:56:31 -08003179size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003180 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003181 if (t == nullptr) {
3182 return 0;
3183 }
3184 return t->frameCount();
3185}
3186
3187uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003188 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003189 if (t == nullptr) {
3190 return 0;
3191 }
3192 return t->latency_l();
3193}
3194
3195void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003196 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003197 if (t == nullptr) {
3198 return;
3199 }
3200 t->setVolumeForOutput_l(left, right);
3201}
3202
3203void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003204 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003205 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003206 if (t == nullptr) {
3207 return;
3208 }
3209 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3210
Andy Hung328d6772021-01-12 12:32:21 -08003211 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003212 if (c == nullptr) {
3213 return;
3214 }
Eric Laurent41709552019-12-16 19:34:05 -08003215 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3216 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003217}
3218
Eric Laurent41709552019-12-16 19:34:05 -08003219void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003220 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003221 if (t == nullptr) {
3222 return;
3223 }
Eric Laurent41709552019-12-16 19:34:05 -08003224 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3225 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003226}
3227
Eric Laurent41709552019-12-16 19:34:05 -08003228void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003229 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3230
Andy Hung328d6772021-01-12 12:32:21 -08003231 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003232 if (t == nullptr) {
3233 return;
3234 }
3235 t->onEffectDisable();
3236}
3237
3238bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3239 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003240 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003241 if (t == nullptr) {
3242 return false;
3243 }
3244 t->disconnectEffectHandle(handle, unpinIfLast);
3245 return true;
3246}
3247
3248void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003249 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003250 if (c == nullptr) {
3251 return;
3252 }
3253 c->resetVolume_l();
3254
3255}
3256
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003257product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003258 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003259 if (c == nullptr) {
3260 return PRODUCT_STRATEGY_NONE;
3261 }
3262 return c->strategy();
3263}
3264
3265int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003266 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003267 if (c == nullptr) {
3268 return 0;
3269 }
3270 return c->activeTrackCnt();
3271}
3272
Eric Laurentb82e6b72019-11-22 17:25:04 -08003273
3274#undef LOG_TAG
3275#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3276
3277status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3278{
3279 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3280 Mutex::Autolock _l(mProxyLock);
3281 if (status == NO_ERROR) {
3282 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003283 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003284 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003285 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003286 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003287 bs = handle.second->disable(&status);
3288 }
3289 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003290 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003291 }
3292 }
3293 }
3294 ALOGV("%s enable %d status %d", __func__, enabled, status);
3295 return status;
3296}
3297
3298status_t AudioFlinger::DeviceEffectProxy::init(
3299 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3300//For all audio patches
3301//If src or sink device match
3302//If the effect is HW accelerated
3303// if no corresponding effect module
3304// Create EffectModule: mHalEffect
3305//Create and attach EffectHandle
3306//If the effect is not HW accelerated and the patch sink or src is a mixer port
3307// Create Effect on patch input or output thread on session -1
3308//Add EffectHandle to EffectHandle map of Effect Proxy:
3309 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3310 status_t status = NO_ERROR;
3311 for (auto &patch : patches) {
3312 status = onCreatePatch(patch.first, patch.second);
3313 ALOGV("%s onCreatePatch status %d", __func__, status);
3314 if (status == BAD_VALUE) {
3315 return status;
3316 }
3317 }
3318 return status;
3319}
3320
3321status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3322 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3323 status_t status = NAME_NOT_FOUND;
3324 sp<EffectHandle> handle;
3325 // only consider source[0] as this is the only "true" source of a patch
3326 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3327 ALOGV("%s source checkPort status %d", __func__, status);
3328 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3329 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3330 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3331 }
3332 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3333 Mutex::Autolock _l(mProxyLock);
3334 mEffectHandles.emplace(patchHandle, handle);
3335 }
3336 ALOGW_IF(status == BAD_VALUE,
3337 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3338
3339 return status;
3340}
3341
3342status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3343 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3344
3345 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3346 __func__, port->type, port->ext.device.type,
3347 port->ext.device.address, port->id, patch.isSoftware());
3348 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003349 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003350 return NAME_NOT_FOUND;
3351 }
3352 status_t status = NAME_NOT_FOUND;
3353
3354 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3355 Mutex::Autolock _l(mProxyLock);
3356 mDevicePort = *port;
3357 mHalEffect = new EffectModule(mMyCallback,
3358 const_cast<effect_descriptor_t *>(&mDescriptor),
3359 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3360 false /* pinned */, port->id);
3361 if (audio_is_input_device(mDevice.mType)) {
3362 mHalEffect->setInputDevice(mDevice);
3363 } else {
3364 mHalEffect->setDevices({mDevice});
3365 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003366 mHalEffect->configure();
3367
Eric Laurentde8caf42021-08-11 17:19:25 +02003368 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3369 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003370 status = (*handle)->initCheck();
3371 if (status == OK) {
3372 status = mHalEffect->addHandle((*handle).get());
3373 } else {
3374 mHalEffect.clear();
3375 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3376 }
3377 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3378 sp <ThreadBase> thread;
3379 if (audio_port_config_has_input_direction(port)) {
3380 if (patch.isSoftware()) {
3381 thread = patch.mRecord.thread();
3382 } else {
3383 thread = patch.thread().promote();
3384 }
3385 } else {
3386 if (patch.isSoftware()) {
3387 thread = patch.mPlayback.thread();
3388 } else {
3389 thread = patch.thread().promote();
3390 }
3391 }
3392 int enabled;
3393 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3394 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003395 &enabled, &status, false, false /*probe*/,
3396 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003397 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3398 } else {
3399 status = BAD_VALUE;
3400 }
3401 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003402 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003403 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003404 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003405 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003406 bs = (*handle)->disable(&status);
3407 }
3408 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003409 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003410 }
3411 }
3412 return status;
3413}
3414
3415void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003416 sp<EffectHandle> effect;
3417 {
3418 Mutex::Autolock _l(mProxyLock);
3419 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3420 effect = mEffectHandles.at(patchHandle);
3421 mEffectHandles.erase(patchHandle);
3422 }
3423 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003424}
3425
3426
3427size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3428{
3429 Mutex::Autolock _l(mProxyLock);
3430 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003431 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003432 mHalEffect.clear();
3433 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3434 }
3435 return mHalEffect == nullptr ? 0 : 1;
3436}
3437
3438status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3439 sp<EffectHalInterface> effect) {
3440 if (mHalEffect == nullptr) {
3441 return NO_INIT;
3442 }
3443 return mManagerCallback->addEffectToHal(
3444 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3445}
3446
3447status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3448 sp<EffectHalInterface> effect) {
3449 if (mHalEffect == nullptr) {
3450 return NO_INIT;
3451 }
3452 return mManagerCallback->removeEffectFromHal(
3453 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3454}
3455
3456bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3457 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3458 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3459 }
3460 return true;
3461}
3462
3463uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3464 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3465 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3466 return mDevicePort.sample_rate;
3467 }
3468 return DEFAULT_OUTPUT_SAMPLE_RATE;
3469}
3470
3471audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3472 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3473 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3474 return mDevicePort.channel_mask;
3475 }
3476 return AUDIO_CHANNEL_OUT_STEREO;
3477}
3478
3479uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3480 if (isOutput()) {
3481 return audio_channel_count_from_out_mask(channelMask());
3482 }
3483 return audio_channel_count_from_in_mask(channelMask());
3484}
3485
3486void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3487 const Vector<String16> args;
3488 EffectBase::dump(fd, args);
3489
3490 const bool locked = dumpTryLock(mProxyLock);
3491 if (!locked) {
3492 String8 result("DeviceEffectProxy may be deadlocked\n");
3493 write(fd, result.string(), result.size());
3494 }
3495
3496 String8 outStr;
3497 if (mHalEffect != nullptr) {
3498 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3499 } else {
3500 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3501 }
3502 write(fd, outStr.string(), outStr.size());
3503 outStr.clear();
3504
3505 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3506 write(fd, outStr.string(), outStr.size());
3507 outStr.clear();
3508
3509 for (const auto& iter : mEffectHandles) {
3510 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3511 write(fd, outStr.string(), outStr.size());
3512 outStr.clear();
3513 sp<EffectBase> effect = iter.second->effect().promote();
3514 if (effect != nullptr) {
3515 effect->dump(fd, args);
3516 }
3517 }
3518
3519 if (locked) {
3520 mLock.unlock();
3521 }
3522}
3523
3524#undef LOG_TAG
3525#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3526
3527int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3528 return mManagerCallback->newEffectId();
3529}
3530
3531
3532bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3533 EffectHandle *handle, bool unpinIfLast) {
3534 sp<EffectBase> effectBase = handle->effect().promote();
3535 if (effectBase == nullptr) {
3536 return false;
3537 }
3538
3539 sp<EffectModule> effect = effectBase->asEffectModule();
3540 if (effect == nullptr) {
3541 return false;
3542 }
3543
3544 // restore suspended effects if the disconnected handle was enabled and the last one.
3545 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3546 if (remove) {
3547 sp<DeviceEffectProxy> proxy = mProxy.promote();
3548 if (proxy != nullptr) {
3549 proxy->removeEffect(effect);
3550 }
3551 if (handle->enabled()) {
3552 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3553 }
3554 }
3555 return true;
3556}
3557
3558status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3559 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3560 sp<EffectHalInterface> *effect) {
3561 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3562}
3563
3564status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3565 sp<EffectHalInterface> effect) {
3566 sp<DeviceEffectProxy> proxy = mProxy.promote();
3567 if (proxy == nullptr) {
3568 return NO_INIT;
3569 }
3570 return proxy->addEffectToHal(effect);
3571}
3572
3573status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3574 sp<EffectHalInterface> effect) {
3575 sp<DeviceEffectProxy> proxy = mProxy.promote();
3576 if (proxy == nullptr) {
3577 return NO_INIT;
3578 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003579 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003580}
3581
3582bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3583 sp<DeviceEffectProxy> proxy = mProxy.promote();
3584 if (proxy == nullptr) {
3585 return true;
3586 }
3587 return proxy->isOutput();
3588}
3589
3590uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3591 sp<DeviceEffectProxy> proxy = mProxy.promote();
3592 if (proxy == nullptr) {
3593 return DEFAULT_OUTPUT_SAMPLE_RATE;
3594 }
3595 return proxy->sampleRate();
3596}
3597
Eric Laurentf1f22e72021-07-13 14:04:14 +02003598audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3599 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003600 sp<DeviceEffectProxy> proxy = mProxy.promote();
3601 if (proxy == nullptr) {
3602 return AUDIO_CHANNEL_OUT_STEREO;
3603 }
3604 return proxy->channelMask();
3605}
3606
Eric Laurentf1f22e72021-07-13 14:04:14 +02003607uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3608 sp<DeviceEffectProxy> proxy = mProxy.promote();
3609 if (proxy == nullptr) {
3610 return 2;
3611 }
3612 return proxy->channelCount();
3613}
3614
3615audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3616 sp<DeviceEffectProxy> proxy = mProxy.promote();
3617 if (proxy == nullptr) {
3618 return AUDIO_CHANNEL_OUT_STEREO;
3619 }
3620 return proxy->channelMask();
3621}
3622
3623uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003624 sp<DeviceEffectProxy> proxy = mProxy.promote();
3625 if (proxy == nullptr) {
3626 return 2;
3627 }
3628 return proxy->channelCount();
3629}
3630
Eric Laurent76c89f32021-12-03 17:13:23 +01003631void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3632 const sp<EffectBase>& effectBase) {
3633 sp<EffectModule> effect = effectBase->asEffectModule();
3634 if (effect == nullptr) {
3635 return;
3636 }
3637 effect->start();
3638}
3639
3640void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3641 const sp<EffectBase>& effectBase) {
3642 sp<EffectModule> effect = effectBase->asEffectModule();
3643 if (effect == nullptr) {
3644 return;
3645 }
3646 effect->stop();
3647}
3648
Glenn Kasten63238ef2015-03-02 15:50:29 -08003649} // namespace android