blob: 98829d0bb7610c9e273e779bc82135adcfd7ee02 [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 Naganov59984db2022-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 Naganov59984db2022-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 Naganov59984db2022-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 }
jiabin229f94d2022-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)) {
jiabin229f94d2022-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 Naganov59984db2022-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());
Andy Hung393de3a2022-12-06 16:33:20 -08001759 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001760
1761 if (client == 0) {
1762 return;
1763 }
1764 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1765 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001766 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001767 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001768 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001770 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 return;
1772 }
Glenn Kastene75da402013-11-20 13:54:52 -08001773 new(mCblk) effect_param_cblk_t();
1774 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001775}
1776
1777AudioFlinger::EffectHandle::~EffectHandle()
1778{
1779 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 disconnect(false);
1781}
1782
Andy Hungc747c532022-03-07 21:41:14 -08001783// Creates an association between Binder code to name for IEffect.
1784#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1785BINDER_METHOD_ENTRY(enable) \
1786BINDER_METHOD_ENTRY(disable) \
1787BINDER_METHOD_ENTRY(command) \
1788BINDER_METHOD_ENTRY(disconnect) \
1789BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov59984db2022-04-19 21:21:23 +00001790BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001791
1792// singleton for Binder Method Statistics for IEffect
1793mediautils::MethodStatistics<int>& getIEffectStatistics() {
1794 using Code = int;
1795
1796#pragma push_macro("BINDER_METHOD_ENTRY")
1797#undef BINDER_METHOD_ENTRY
1798#define BINDER_METHOD_ENTRY(ENTRY) \
1799 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1800
1801 static mediautils::MethodStatistics<Code> methodStatistics{
1802 IEFFECT_BINDER_METHOD_MACRO_LIST
1803 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1804 };
1805#pragma pop_macro("BINDER_METHOD_ENTRY")
1806
1807 return methodStatistics;
1808}
1809
1810status_t AudioFlinger::EffectHandle::onTransact(
1811 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001812 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1813 mediautils::TimeCheck check(
1814 std::string("IEffect::").append(methodName),
1815 [code](bool timeout, float elapsedMs) {
1816 if (timeout) {
1817 ; // we don't timeout right now on the effect interface.
1818 } else {
1819 getIEffectStatistics().event(code, elapsedMs);
1820 }
Andy Hung741b3dd2022-06-13 19:49:43 -07001821 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001822 return BnEffect::onTransact(code, data, reply, flags);
1823}
1824
Glenn Kastene75da402013-11-20 13:54:52 -08001825status_t AudioFlinger::EffectHandle::initCheck()
1826{
1827 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1828}
1829
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830#define RETURN(code) \
1831 *_aidl_return = (code); \
1832 return Status::ok();
1833
Mikhail Naganov59984db2022-04-19 21:21:23 +00001834#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1835 ({ \
1836 auto _tmp = (exp); \
1837 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1838 std::move(_tmp.value()); \
1839 })
1840
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001842{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001843 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001845 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001846 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001847 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001848 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001849 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001850 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001852
1853 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001854 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 }
1856
1857 mEnabled = true;
1858
Eric Laurent6c796322019-04-09 14:13:17 -07001859 status_t status = effect->updatePolicyState();
1860 if (status != NO_ERROR) {
1861 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001862 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001863 }
1864
Eric Laurent6b446ce2019-12-13 10:56:31 -08001865 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001866
1867 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001868 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001869 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 }
1871
Eric Laurent6b446ce2019-12-13 10:56:31 -08001872 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 mEnabled = false;
1875 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001876 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001877}
1878
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001879Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001880{
1881 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001882 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001883 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001884 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001885 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001886 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001887 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001888 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001889 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001890
1891 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001893 }
1894 mEnabled = false;
1895
Eric Laurent6c796322019-04-09 14:13:17 -07001896 effect->updatePolicyState();
1897
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001898 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 }
1901
Eric Laurent6b446ce2019-12-13 10:56:31 -08001902 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001903 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001904}
1905
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001907{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001908 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001909 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001911}
1912
1913void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1914{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001915 AutoMutex _l(mLock);
1916 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1917 if (mDisconnected) {
1918 if (unpinIfLast) {
1919 android_errorWriteLog(0x534e4554, "32707507");
1920 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001921 return;
1922 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001923 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001924 {
Eric Laurent41709552019-12-16 19:34:05 -08001925 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001926 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001927 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001928 ALOGW("%s Effect handle %p disconnected after thread destruction",
1929 __func__, this);
1930 }
1931 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001932 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001933 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001934
Eric Laurentca7cc822012-11-19 14:55:58 -08001935 if (mClient != 0) {
1936 if (mCblk != NULL) {
1937 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1938 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1939 }
1940 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001941 // Client destructor must run with AudioFlinger client mutex locked
1942 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001943 mClient.clear();
1944 }
1945}
1946
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1948 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1949 return Status::ok();
1950}
1951
Mikhail Naganov59984db2022-04-19 21:21:23 +00001952Status AudioFlinger::EffectHandle::getConfig(
1953 media::EffectConfig* _config, int32_t* _aidl_return) {
1954 AutoMutex _l(mLock);
1955 sp<EffectBase> effect = mEffect.promote();
1956 if (effect == nullptr || mDisconnected) {
1957 RETURN(DEAD_OBJECT);
1958 }
1959 sp<EffectModule> effectModule = effect->asEffectModule();
1960 if (effectModule == nullptr) {
1961 RETURN(INVALID_OPERATION);
1962 }
1963 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1964 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1965 bool isOutput;
1966 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1967 if (status == NO_ERROR) {
1968 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1969 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1970 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1971 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1972 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1973 _config->isOnInputStream = !isOutput;
1974 }
1975 RETURN(status);
1976}
1977
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001978Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1979 const std::vector<uint8_t>& cmdData,
1980 int32_t maxResponseSize,
1981 std::vector<uint8_t>* response,
1982 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001983{
1984 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001985 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001986
Eric Laurentc7ab3092017-06-15 18:43:46 -07001987 // reject commands reserved for internal use by audio framework if coming from outside
1988 // of audioserver
1989 switch(cmdCode) {
1990 case EFFECT_CMD_ENABLE:
1991 case EFFECT_CMD_DISABLE:
1992 case EFFECT_CMD_SET_PARAM:
1993 case EFFECT_CMD_SET_PARAM_DEFERRED:
1994 case EFFECT_CMD_SET_PARAM_COMMIT:
1995 case EFFECT_CMD_GET_PARAM:
1996 break;
1997 default:
1998 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1999 break;
2000 }
2001 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002002 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002003 }
2004
Eric Laurent1ffc5852016-12-15 14:46:09 -08002005 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002006 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002007 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002009 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002010 writeToBuffer(NO_ERROR, response);
2011 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002012 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002013 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002014 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002015 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002016 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002017 writeToBuffer(NO_ERROR, response);
2018 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002019 }
2020
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002021 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002022 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002023 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002024 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002025 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002026 // only get parameter command is permitted for applications not controlling the effect
2027 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002028 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002029 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002030
2031 // handle commands that are not forwarded transparently to effect engine
2032 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002033 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002034 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002035 }
2036
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002037 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002038 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002039 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002040 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002041 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002042
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 // No need to trylock() here as this function is executed in the binder thread serving a
2044 // particular client process: no risk to block the whole media server process or mixer
2045 // threads if we are stuck here
2046 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002047 // keep local copy of index in case of client corruption b/32220769
2048 const uint32_t clientIndex = mCblk->clientIndex;
2049 const uint32_t serverIndex = mCblk->serverIndex;
2050 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2051 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 mCblk->serverIndex = 0;
2053 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002054 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002055 }
2056 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002057 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002058 for (uint32_t index = serverIndex; index < clientIndex;) {
2059 int *p = (int *)(mBuffer + index);
2060 const int size = *p++;
2061 if (size < 0
2062 || size > EFFECT_PARAM_BUFFER_SIZE
2063 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002064 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002065 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 break;
2067 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002068
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002069 std::copy(reinterpret_cast<const uint8_t*>(p),
2070 reinterpret_cast<const uint8_t*>(p) + size,
2071 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002072
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002073 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002074 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002075 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002076 sizeof(int),
2077 &replyBuffer);
2078 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002079
2080 // verify shared memory: server index shouldn't change; client index can't go back.
2081 if (serverIndex != mCblk->serverIndex
2082 || clientIndex > mCblk->clientIndex) {
2083 android_errorWriteLog(0x534e4554, "32220769");
2084 status = BAD_VALUE;
2085 break;
2086 }
2087
Eric Laurentca7cc822012-11-19 14:55:58 -08002088 // stop at first error encountered
2089 if (ret != NO_ERROR) {
2090 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002091 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002092 break;
2093 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002094 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002095 break;
2096 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002097 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002098 }
2099 mCblk->serverIndex = 0;
2100 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002101 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002102 }
2103
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002104 status_t status = effect->command(cmdCode,
2105 cmdData,
2106 maxResponseSize,
2107 response);
2108 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002109}
2110
2111void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2112{
2113 ALOGV("setControl %p control %d", this, hasControl);
2114
2115 mHasControl = hasControl;
2116 mEnabled = enabled;
2117
2118 if (signal && mEffectClient != 0) {
2119 mEffectClient->controlStatusChanged(hasControl);
2120 }
2121}
2122
2123void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002124 const std::vector<uint8_t>& cmdData,
2125 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002126{
2127 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002128 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002129 }
2130}
2131
2132
2133
2134void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2135{
2136 if (mEffectClient != 0) {
2137 mEffectClient->enableStatusChanged(enabled);
2138 }
2139}
2140
Eric Laurentde8caf42021-08-11 17:19:25 +02002141void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2142{
2143 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2144 mEffectClient->framesProcessed(frames);
2145 }
2146}
2147
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002148void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002149{
2150 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2151
Marco Nelissenb2208842014-02-07 14:00:50 -08002152 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002153 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002154 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002155 mHasControl ? "yes" : "no",
2156 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002157 mCblk ? mCblk->clientIndex : 0,
2158 mCblk ? mCblk->serverIndex : 0
2159 );
2160
2161 if (locked) {
2162 mCblk->lock.unlock();
2163 }
2164}
2165
2166#undef LOG_TAG
2167#define LOG_TAG "AudioFlinger::EffectChain"
2168
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002169AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2170 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002171 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002172 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002173 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002174 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002175{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002176 sp<ThreadBase> p = thread.promote();
2177 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002178 return;
2179 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002180 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002181 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2182 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002183}
2184
2185AudioFlinger::EffectChain::~EffectChain()
2186{
Eric Laurentca7cc822012-11-19 14:55:58 -08002187}
2188
2189// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2190sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2191 effect_descriptor_t *descriptor)
2192{
2193 size_t size = mEffects.size();
2194
2195 for (size_t i = 0; i < size; i++) {
2196 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2197 return mEffects[i];
2198 }
2199 }
2200 return 0;
2201}
2202
2203// getEffectFromId_l() must be called with ThreadBase::mLock held
2204sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2205{
2206 size_t size = mEffects.size();
2207
2208 for (size_t i = 0; i < size; i++) {
2209 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2210 if (id == 0 || mEffects[i]->id() == id) {
2211 return mEffects[i];
2212 }
2213 }
2214 return 0;
2215}
2216
2217// getEffectFromType_l() must be called with ThreadBase::mLock held
2218sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2219 const effect_uuid_t *type)
2220{
2221 size_t size = mEffects.size();
2222
2223 for (size_t i = 0; i < size; i++) {
2224 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2225 return mEffects[i];
2226 }
2227 }
2228 return 0;
2229}
2230
Eric Laurent6c796322019-04-09 14:13:17 -07002231std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2232{
2233 std::vector<int> ids;
2234 Mutex::Autolock _l(mLock);
2235 for (size_t i = 0; i < mEffects.size(); i++) {
2236 ids.push_back(mEffects[i]->id());
2237 }
2238 return ids;
2239}
2240
Eric Laurentca7cc822012-11-19 14:55:58 -08002241void AudioFlinger::EffectChain::clearInputBuffer()
2242{
2243 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002244 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002245}
2246
2247// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002248void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002249{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002250 if (mInBuffer == NULL) {
2251 return;
2252 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002253 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2254 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002255
Eric Laurent6b446ce2019-12-13 10:56:31 -08002256 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002257 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002258}
2259
2260// Must be called with EffectChain::mLock locked
2261void AudioFlinger::EffectChain::process_l()
2262{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002263 // never process effects when:
2264 // - on an OFFLOAD thread
2265 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002266 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002267 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002268 bool tracksOnSession = (trackCnt() != 0);
2269
2270 if (!tracksOnSession && mTailBufferCount == 0) {
2271 doProcess = false;
2272 }
2273
2274 if (activeTrackCnt() == 0) {
2275 // if no track is active and the effect tail has not been rendered,
2276 // the input buffer must be cleared here as the mixer process will not do it
2277 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002278 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002279 if (mTailBufferCount > 0) {
2280 mTailBufferCount--;
2281 }
2282 }
2283 }
2284 }
2285
2286 size_t size = mEffects.size();
2287 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002288 // Only the input and output buffers of the chain can be external,
2289 // and 'update' / 'commit' do nothing for allocated buffers, thus
2290 // it's not needed to consider any other buffers here.
2291 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002292 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2293 mOutBuffer->update();
2294 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002295 for (size_t i = 0; i < size; i++) {
2296 mEffects[i]->process();
2297 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002298 mInBuffer->commit();
2299 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2300 mOutBuffer->commit();
2301 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002302 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002303 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002304 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002305 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2306 }
2307 if (doResetVolume) {
2308 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 }
2310}
2311
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002312// createEffect_l() must be called with ThreadBase::mLock held
2313status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002314 effect_descriptor_t *desc,
2315 int id,
2316 audio_session_t sessionId,
2317 bool pinned)
2318{
2319 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002320 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002321 status_t lStatus = effect->status();
2322 if (lStatus == NO_ERROR) {
2323 lStatus = addEffect_ll(effect);
2324 }
2325 if (lStatus != NO_ERROR) {
2326 effect.clear();
2327 }
2328 return lStatus;
2329}
2330
2331// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002332status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2333{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002334 Mutex::Autolock _l(mLock);
2335 return addEffect_ll(effect);
2336}
2337// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2338status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2339{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002340 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002341
Eric Laurentb62d0362021-10-26 17:40:18 +02002342 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002343 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2344 // Auxiliary effects are inserted at the beginning of mEffects vector as
2345 // they are processed first and accumulated in chain input buffer
2346 mEffects.insertAt(effect, 0);
2347
2348 // the input buffer for auxiliary effect contains mono samples in
2349 // 32 bit format. This is to avoid saturation in AudoMixer
2350 // accumulation stage. Saturation is done in EffectModule::process() before
2351 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002352 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002353 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002354#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002355 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002356 numSamples * sizeof(float), &halBuffer);
2357#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002358 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002359 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002360#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002361 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002362
2363 effect->configure();
2364
Mikhail Naganov022b9952017-01-04 16:36:51 -08002365 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002366 // auxiliary effects output samples to chain input buffer for further processing
2367 // by insert effects
2368 effect->setOutBuffer(mInBuffer);
2369 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002370 ssize_t idx_insert = getInsertIndex(desc);
2371 if (idx_insert < 0) {
2372 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002373 }
2374
Eric Laurentb62d0362021-10-26 17:40:18 +02002375 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002376 mEffects.insertAt(effect, idx_insert);
2377
2378 effect->configure();
2379
Eric Laurentb62d0362021-10-26 17:40:18 +02002380 // - By default:
2381 // All effects read samples from chain input buffer.
2382 // The last effect in the chain, writes samples to chain output buffer,
2383 // otherwise to chain input buffer
2384 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2385 // The spatializer effect (first effect) reads samples from the input buffer
2386 // and writes samples to the output buffer.
2387 // All other effects read and writes samples to the output buffer
2388 if (mEffectCallback->isSpatializer()
2389 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002390 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002391 if (idx_insert == 0) {
2392 if (previousSize != 0) {
2393 mEffects[1]->configure();
2394 mEffects[1]->setInBuffer(mOutBuffer);
2395 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2396 }
2397 effect->setInBuffer(mInBuffer);
2398 } else {
2399 effect->setInBuffer(mOutBuffer);
2400 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002401 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002402 effect->setInBuffer(mInBuffer);
2403 if (idx_insert == previousSize) {
2404 if (idx_insert != 0) {
2405 mEffects[idx_insert-1]->configure();
2406 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2407 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2408 }
2409 effect->setOutBuffer(mOutBuffer);
2410 } else {
2411 effect->setOutBuffer(mInBuffer);
2412 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002413 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002414 ALOGV("%s effect %p, added in chain %p at rank %zu",
2415 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 }
2417 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002418
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 return NO_ERROR;
2420}
2421
Eric Laurentb62d0362021-10-26 17:40:18 +02002422ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2423 // Insert effects are inserted at the end of mEffects vector as they are processed
2424 // after track and auxiliary effects.
2425 // Insert effect order as a function of indicated preference:
2426 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2427 // another effect is present
2428 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2429 // last effect claiming first position
2430 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2431 // first effect claiming last position
2432 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2433 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2434 // already present
2435 // Spatializer or Downmixer effects are inserted in first position because
2436 // they adapt the channel count for all other effects in the chain
2437 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2438 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2439 return 0;
2440 }
2441
2442 size_t size = mEffects.size();
2443 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2444 ssize_t idx_insert;
2445 ssize_t idx_insert_first = -1;
2446 ssize_t idx_insert_last = -1;
2447
2448 idx_insert = size;
2449 for (size_t i = 0; i < size; i++) {
2450 effect_descriptor_t d = mEffects[i]->desc();
2451 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2452 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2453 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2454 // check invalid effect chaining combinations
2455 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2456 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2457 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2458 __func__, desc.name, d.name);
2459 return -1;
2460 }
2461 // remember position of first insert effect and by default
2462 // select this as insert position for new effect
2463 if (idx_insert == size) {
2464 idx_insert = i;
2465 }
2466 // remember position of last insert effect claiming
2467 // first position
2468 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2469 idx_insert_first = i;
2470 }
2471 // remember position of first insert effect claiming
2472 // last position
2473 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2474 idx_insert_last == -1) {
2475 idx_insert_last = i;
2476 }
2477 }
2478 }
2479
2480 // modify idx_insert from first position if needed
2481 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2482 if (idx_insert_last != -1) {
2483 idx_insert = idx_insert_last;
2484 } else {
2485 idx_insert = size;
2486 }
2487 } else {
2488 if (idx_insert_first != -1) {
2489 idx_insert = idx_insert_first + 1;
2490 }
2491 }
2492 return idx_insert;
2493}
2494
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002495// removeEffect_l() must be called with ThreadBase::mLock held
2496size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2497 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002498{
2499 Mutex::Autolock _l(mLock);
2500 size_t size = mEffects.size();
2501 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2502
2503 for (size_t i = 0; i < size; i++) {
2504 if (effect == mEffects[i]) {
2505 // calling stop here will remove pre-processing effect from the audio HAL.
2506 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2507 // the middle of a read from audio HAL
2508 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2509 mEffects[i]->state() == EffectModule::STOPPING) {
2510 mEffects[i]->stop();
2511 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002512 if (release) {
2513 mEffects[i]->release_l();
2514 }
2515
Mikhail Naganov022b9952017-01-04 16:36:51 -08002516 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002517 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002518 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002519 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002520 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002521 }
2522 }
2523 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002524
2525 // make sure the input buffer configuration for the new first effect in the chain
2526 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2527 if (i == 0 && size > 1) {
2528 mEffects[0]->configure();
2529 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002530 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002531 }
2532
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002533 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002534 this, i);
2535 break;
2536 }
2537 }
2538
2539 return mEffects.size();
2540}
2541
jiabin8f278ee2019-11-11 12:16:27 -08002542// setDevices_l() must be called with ThreadBase::mLock held
2543void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002544{
2545 size_t size = mEffects.size();
2546 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002547 mEffects[i]->setDevices(devices);
2548 }
2549}
2550
2551// setInputDevice_l() must be called with ThreadBase::mLock held
2552void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2553{
2554 size_t size = mEffects.size();
2555 for (size_t i = 0; i < size; i++) {
2556 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002557 }
2558}
2559
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002560// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002561void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2562{
2563 size_t size = mEffects.size();
2564 for (size_t i = 0; i < size; i++) {
2565 mEffects[i]->setMode(mode);
2566 }
2567}
2568
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002569// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002570void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2571{
2572 size_t size = mEffects.size();
2573 for (size_t i = 0; i < size; i++) {
2574 mEffects[i]->setAudioSource(source);
2575 }
2576}
2577
Zhou Songd505c642020-02-20 16:35:37 +08002578bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2579 for (const auto &effect : mEffects) {
2580 if (effect->isVolumeControlEnabled()) return true;
2581 }
2582 return false;
2583}
2584
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002585// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002586bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002587{
2588 uint32_t newLeft = *left;
2589 uint32_t newRight = *right;
2590 bool hasControl = false;
2591 int ctrlIdx = -1;
2592 size_t size = mEffects.size();
2593
2594 // first update volume controller
2595 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002596 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002597 ctrlIdx = i - 1;
2598 hasControl = true;
2599 break;
2600 }
2601 }
2602
Eric Laurentfa1e1232016-08-02 19:01:49 -07002603 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002604 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002605 if (hasControl) {
2606 *left = mNewLeftVolume;
2607 *right = mNewRightVolume;
2608 }
2609 return hasControl;
2610 }
2611
2612 mVolumeCtrlIdx = ctrlIdx;
2613 mLeftVolume = newLeft;
2614 mRightVolume = newRight;
2615
2616 // second get volume update from volume controller
2617 if (ctrlIdx >= 0) {
2618 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2619 mNewLeftVolume = newLeft;
2620 mNewRightVolume = newRight;
2621 }
2622 // then indicate volume to all other effects in chain.
2623 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002624 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002625 uint32_t lVol = newLeft;
2626 uint32_t rVol = newRight;
2627
2628 for (size_t i = 0; i < size; i++) {
2629 if ((int)i == ctrlIdx) {
2630 continue;
2631 }
2632 // this also works for ctrlIdx == -1 when there is no volume controller
2633 if ((int)i > ctrlIdx) {
2634 lVol = *left;
2635 rVol = *right;
2636 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002637 // Pass requested volume directly if this is volume monitor module
2638 if (mEffects[i]->isVolumeMonitor()) {
2639 mEffects[i]->setVolume(left, right, false);
2640 } else {
2641 mEffects[i]->setVolume(&lVol, &rVol, false);
2642 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002643 }
2644 *left = newLeft;
2645 *right = newRight;
2646
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002647 setVolumeForOutput_l(*left, *right);
2648
Eric Laurentca7cc822012-11-19 14:55:58 -08002649 return hasControl;
2650}
2651
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002652// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002653void AudioFlinger::EffectChain::resetVolume_l()
2654{
Eric Laurente7449bf2016-08-03 18:44:07 -07002655 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2656 uint32_t left = mLeftVolume;
2657 uint32_t right = mRightVolume;
2658 (void)setVolume_l(&left, &right, true);
2659 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002660}
2661
jiabineb3bda02020-06-30 14:07:03 -07002662// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2663bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2664{
2665 for (size_t i = 0; i < mEffects.size(); ++i) {
2666 if (mEffects[i]->isHapticGenerator()) {
2667 return true;
2668 }
2669 }
2670 return false;
2671}
2672
jiabine70bc7f2020-06-30 22:07:55 -07002673void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2674{
2675 Mutex::Autolock _l(mLock);
2676 for (size_t i = 0; i < mEffects.size(); ++i) {
2677 mEffects[i]->setHapticIntensity(id, intensity);
2678 }
2679}
2680
Eric Laurent1b928682014-10-02 19:41:47 -07002681void AudioFlinger::EffectChain::syncHalEffectsState()
2682{
2683 Mutex::Autolock _l(mLock);
2684 for (size_t i = 0; i < mEffects.size(); i++) {
2685 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2686 mEffects[i]->state() == EffectModule::STOPPING) {
2687 mEffects[i]->addEffectToHal_l();
2688 }
2689 }
2690}
2691
Eric Laurentca7cc822012-11-19 14:55:58 -08002692void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2693{
Eric Laurentca7cc822012-11-19 14:55:58 -08002694 String8 result;
2695
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002696 const size_t numEffects = mEffects.size();
2697 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002698
Marco Nelissenb2208842014-02-07 14:00:50 -08002699 if (numEffects) {
2700 bool locked = AudioFlinger::dumpTryLock(mLock);
2701 // failed to lock - AudioFlinger is probably deadlocked
2702 if (!locked) {
2703 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002704 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002705
Andy Hungbded9c82017-11-30 18:47:35 -08002706 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2707 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2708 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2709 (int)inBufferStr.size(), "In buffer ",
2710 (int)outBufferStr.size(), "Out buffer ");
2711 result.appendFormat("\t%s %s %d\n",
2712 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002713 write(fd, result.string(), result.size());
2714
2715 for (size_t i = 0; i < numEffects; ++i) {
2716 sp<EffectModule> effect = mEffects[i];
2717 if (effect != 0) {
2718 effect->dump(fd, args);
2719 }
2720 }
2721
2722 if (locked) {
2723 mLock.unlock();
2724 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002725 } else {
2726 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002727 }
2728}
2729
2730// must be called with ThreadBase::mLock held
2731void AudioFlinger::EffectChain::setEffectSuspended_l(
2732 const effect_uuid_t *type, bool suspend)
2733{
2734 sp<SuspendedEffectDesc> desc;
2735 // use effect type UUID timelow as key as there is no real risk of identical
2736 // timeLow fields among effect type UUIDs.
2737 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2738 if (suspend) {
2739 if (index >= 0) {
2740 desc = mSuspendedEffects.valueAt(index);
2741 } else {
2742 desc = new SuspendedEffectDesc();
2743 desc->mType = *type;
2744 mSuspendedEffects.add(type->timeLow, desc);
2745 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2746 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002747
Eric Laurentca7cc822012-11-19 14:55:58 -08002748 if (desc->mRefCount++ == 0) {
2749 sp<EffectModule> effect = getEffectIfEnabled(type);
2750 if (effect != 0) {
2751 desc->mEffect = effect;
2752 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002753 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002754 }
2755 }
2756 } else {
2757 if (index < 0) {
2758 return;
2759 }
2760 desc = mSuspendedEffects.valueAt(index);
2761 if (desc->mRefCount <= 0) {
2762 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002763 desc->mRefCount = 0;
2764 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002765 }
2766 if (--desc->mRefCount == 0) {
2767 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2768 if (desc->mEffect != 0) {
2769 sp<EffectModule> effect = desc->mEffect.promote();
2770 if (effect != 0) {
2771 effect->setSuspended(false);
2772 effect->lock();
2773 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002774 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002775 effect->setEnabled_l(handle->enabled());
2776 }
2777 effect->unlock();
2778 }
2779 desc->mEffect.clear();
2780 }
2781 mSuspendedEffects.removeItemsAt(index);
2782 }
2783 }
2784}
2785
2786// must be called with ThreadBase::mLock held
2787void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2788{
2789 sp<SuspendedEffectDesc> desc;
2790
2791 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2792 if (suspend) {
2793 if (index >= 0) {
2794 desc = mSuspendedEffects.valueAt(index);
2795 } else {
2796 desc = new SuspendedEffectDesc();
2797 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2798 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2799 }
2800 if (desc->mRefCount++ == 0) {
2801 Vector< sp<EffectModule> > effects;
2802 getSuspendEligibleEffects(effects);
2803 for (size_t i = 0; i < effects.size(); i++) {
2804 setEffectSuspended_l(&effects[i]->desc().type, true);
2805 }
2806 }
2807 } else {
2808 if (index < 0) {
2809 return;
2810 }
2811 desc = mSuspendedEffects.valueAt(index);
2812 if (desc->mRefCount <= 0) {
2813 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2814 desc->mRefCount = 1;
2815 }
2816 if (--desc->mRefCount == 0) {
2817 Vector<const effect_uuid_t *> types;
2818 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2819 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2820 continue;
2821 }
2822 types.add(&mSuspendedEffects.valueAt(i)->mType);
2823 }
2824 for (size_t i = 0; i < types.size(); i++) {
2825 setEffectSuspended_l(types[i], false);
2826 }
2827 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2828 mSuspendedEffects.keyAt(index));
2829 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2830 }
2831 }
2832}
2833
2834
2835// The volume effect is used for automated tests only
2836#ifndef OPENSL_ES_H_
2837static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2838 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2839const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2840#endif //OPENSL_ES_H_
2841
Eric Laurentd8365c52017-07-16 15:27:05 -07002842/* static */
2843bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2844{
2845 // Only NS and AEC are suspended when BtNRec is off
2846 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2847 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2848 return true;
2849 }
2850 return false;
2851}
2852
Eric Laurentca7cc822012-11-19 14:55:58 -08002853bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2854{
2855 // auxiliary effects and visualizer are never suspended on output mix
2856 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2857 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2858 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002859 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2860 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002861 return false;
2862 }
2863 return true;
2864}
2865
2866void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2867 Vector< sp<AudioFlinger::EffectModule> > &effects)
2868{
2869 effects.clear();
2870 for (size_t i = 0; i < mEffects.size(); i++) {
2871 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2872 effects.add(mEffects[i]);
2873 }
2874 }
2875}
2876
2877sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2878 const effect_uuid_t *type)
2879{
2880 sp<EffectModule> effect = getEffectFromType_l(type);
2881 return effect != 0 && effect->isEnabled() ? effect : 0;
2882}
2883
2884void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2885 bool enabled)
2886{
2887 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2888 if (enabled) {
2889 if (index < 0) {
2890 // if the effect is not suspend check if all effects are suspended
2891 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2892 if (index < 0) {
2893 return;
2894 }
2895 if (!isEffectEligibleForSuspend(effect->desc())) {
2896 return;
2897 }
2898 setEffectSuspended_l(&effect->desc().type, enabled);
2899 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2900 if (index < 0) {
2901 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2902 return;
2903 }
2904 }
2905 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2906 effect->desc().type.timeLow);
2907 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002908 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002909 if (desc->mEffect == 0) {
2910 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002911 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002912 effect->setSuspended(true);
2913 }
2914 } else {
2915 if (index < 0) {
2916 return;
2917 }
2918 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2919 effect->desc().type.timeLow);
2920 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2921 desc->mEffect.clear();
2922 effect->setSuspended(false);
2923 }
2924}
2925
Eric Laurent5baf2af2013-09-12 17:37:00 -07002926bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002927{
2928 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002929 return isNonOffloadableEnabled_l();
2930}
2931
2932bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2933{
Eric Laurent813e2a72013-08-31 12:59:48 -07002934 size_t size = mEffects.size();
2935 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002936 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002937 return true;
2938 }
2939 }
2940 return false;
2941}
2942
Eric Laurentaaa44472014-09-12 17:41:50 -07002943void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2944{
2945 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002946 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002947}
2948
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002949void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2950{
2951 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2952 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2953 }
2954 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2955 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2956 }
2957}
2958
2959void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2960{
2961 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2962 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2963 }
2964 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2965 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2966 }
2967}
2968
2969bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002970{
2971 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002972 for (const auto &effect : mEffects) {
2973 if (effect->isProcessImplemented()) {
2974 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002975 }
2976 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002977 // Allow effects without processing.
2978 return true;
2979}
2980
2981bool AudioFlinger::EffectChain::isFastCompatible() const
2982{
2983 Mutex::Autolock _l(mLock);
2984 for (const auto &effect : mEffects) {
2985 if (effect->isProcessImplemented()
2986 && effect->isImplementationSoftware()) {
2987 return false;
2988 }
2989 }
2990 // Allow effects without processing or hw accelerated effects.
2991 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002992}
2993
2994// isCompatibleWithThread_l() must be called with thread->mLock held
2995bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2996{
2997 Mutex::Autolock _l(mLock);
2998 for (size_t i = 0; i < mEffects.size(); i++) {
2999 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3000 return false;
3001 }
3002 }
3003 return true;
3004}
3005
Eric Laurent6b446ce2019-12-13 10:56:31 -08003006// EffectCallbackInterface implementation
3007status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3008 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3009 sp<EffectHalInterface> *effect) {
3010 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08003011 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003012 if (effectsFactory != 0) {
3013 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3014 }
3015 return status;
3016}
3017
3018bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003019 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003020 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003021 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003022}
3023
3024status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3025 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003026 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003027}
3028
3029status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
3030 sp<EffectHalInterface> effect) {
3031 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003032 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003033 if (t == nullptr) {
3034 return result;
3035 }
3036 sp <StreamHalInterface> st = t->stream();
3037 if (st == nullptr) {
3038 return result;
3039 }
3040 result = st->addEffect(effect);
3041 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3042 return result;
3043}
3044
3045status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
3046 sp<EffectHalInterface> effect) {
3047 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003048 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (t == nullptr) {
3050 return result;
3051 }
3052 sp <StreamHalInterface> st = t->stream();
3053 if (st == nullptr) {
3054 return result;
3055 }
3056 result = st->removeEffect(effect);
3057 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3058 return result;
3059}
3060
3061audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003062 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003063 if (t == nullptr) {
3064 return AUDIO_IO_HANDLE_NONE;
3065 }
3066 return t->id();
3067}
3068
3069bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003070 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003071 if (t == nullptr) {
3072 return true;
3073 }
3074 return t->isOutput();
3075}
3076
3077bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003078 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003079}
3080
3081bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003082 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003083}
3084
3085bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003086 switch (mThreadType) {
3087 case ThreadBase::OFFLOAD:
3088 case ThreadBase::MMAP_PLAYBACK:
3089 case ThreadBase::MMAP_CAPTURE:
3090 return true;
3091 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003092 return false;
3093 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003094}
3095
3096bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3097 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003098}
3099
3100uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003101 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003102 if (t == nullptr) {
3103 return 0;
3104 }
3105 return t->sampleRate();
3106}
3107
Eric Laurentf1f22e72021-07-13 14:04:14 +02003108audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3109 sp<ThreadBase> t = thread().promote();
3110 if (t == nullptr) {
3111 return AUDIO_CHANNEL_NONE;
3112 }
3113 sp<EffectChain> c = chain().promote();
3114 if (c == nullptr) {
3115 return AUDIO_CHANNEL_NONE;
3116 }
3117
Eric Laurentb62d0362021-10-26 17:40:18 +02003118 if (mThreadType == ThreadBase::SPATIALIZER) {
3119 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3120 if (c->isFirstEffect(id)) {
3121 return t->mixerChannelMask();
3122 } else {
3123 return t->channelMask();
3124 }
3125 } else if (!audio_is_global_session(c->sessionId())) {
3126 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3127 return t->mixerChannelMask();
3128 } else {
3129 return t->channelMask();
3130 }
3131 } else {
3132 return t->channelMask();
3133 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003134 } else {
3135 return t->channelMask();
3136 }
3137}
3138
3139uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003140 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003141}
3142
3143audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003144 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003145 if (t == nullptr) {
3146 return AUDIO_CHANNEL_NONE;
3147 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003148 sp<EffectChain> c = chain().promote();
3149 if (c == nullptr) {
3150 return AUDIO_CHANNEL_NONE;
3151 }
3152
3153 if (mThreadType == ThreadBase::SPATIALIZER) {
3154 if (!audio_is_global_session(c->sessionId())) {
3155 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3156 return t->mixerChannelMask();
3157 } else {
3158 return t->channelMask();
3159 }
3160 } else {
3161 return t->channelMask();
3162 }
3163 } else {
3164 return t->channelMask();
3165 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003166}
3167
Eric Laurentf1f22e72021-07-13 14:04:14 +02003168uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003169 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003170}
3171
jiabineb3bda02020-06-30 14:07:03 -07003172audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003173 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003174 if (t == nullptr) {
3175 return AUDIO_CHANNEL_NONE;
3176 }
3177 return t->hapticChannelMask();
3178}
3179
Eric Laurent6b446ce2019-12-13 10:56:31 -08003180size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003181 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003182 if (t == nullptr) {
3183 return 0;
3184 }
3185 return t->frameCount();
3186}
3187
3188uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003189 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003190 if (t == nullptr) {
3191 return 0;
3192 }
3193 return t->latency_l();
3194}
3195
3196void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003197 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003198 if (t == nullptr) {
3199 return;
3200 }
3201 t->setVolumeForOutput_l(left, right);
3202}
3203
3204void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003205 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003206 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003207 if (t == nullptr) {
3208 return;
3209 }
3210 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3211
Andy Hung328d6772021-01-12 12:32:21 -08003212 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003213 if (c == nullptr) {
3214 return;
3215 }
Eric Laurent41709552019-12-16 19:34:05 -08003216 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3217 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003218}
3219
Eric Laurent41709552019-12-16 19:34:05 -08003220void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003221 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003222 if (t == nullptr) {
3223 return;
3224 }
Eric Laurent41709552019-12-16 19:34:05 -08003225 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3226 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003227}
3228
Eric Laurent41709552019-12-16 19:34:05 -08003229void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003230 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3231
Andy Hung328d6772021-01-12 12:32:21 -08003232 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003233 if (t == nullptr) {
3234 return;
3235 }
3236 t->onEffectDisable();
3237}
3238
3239bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3240 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003241 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003242 if (t == nullptr) {
3243 return false;
3244 }
3245 t->disconnectEffectHandle(handle, unpinIfLast);
3246 return true;
3247}
3248
3249void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003250 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003251 if (c == nullptr) {
3252 return;
3253 }
3254 c->resetVolume_l();
3255
3256}
3257
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003258product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003259 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003260 if (c == nullptr) {
3261 return PRODUCT_STRATEGY_NONE;
3262 }
3263 return c->strategy();
3264}
3265
3266int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003267 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003268 if (c == nullptr) {
3269 return 0;
3270 }
3271 return c->activeTrackCnt();
3272}
3273
Eric Laurentb82e6b72019-11-22 17:25:04 -08003274
3275#undef LOG_TAG
3276#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3277
3278status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3279{
3280 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3281 Mutex::Autolock _l(mProxyLock);
3282 if (status == NO_ERROR) {
3283 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003284 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003285 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003286 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003287 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003288 bs = handle.second->disable(&status);
3289 }
3290 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003291 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003292 }
3293 }
3294 }
3295 ALOGV("%s enable %d status %d", __func__, enabled, status);
3296 return status;
3297}
3298
3299status_t AudioFlinger::DeviceEffectProxy::init(
3300 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3301//For all audio patches
3302//If src or sink device match
3303//If the effect is HW accelerated
3304// if no corresponding effect module
3305// Create EffectModule: mHalEffect
3306//Create and attach EffectHandle
3307//If the effect is not HW accelerated and the patch sink or src is a mixer port
3308// Create Effect on patch input or output thread on session -1
3309//Add EffectHandle to EffectHandle map of Effect Proxy:
3310 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3311 status_t status = NO_ERROR;
3312 for (auto &patch : patches) {
3313 status = onCreatePatch(patch.first, patch.second);
3314 ALOGV("%s onCreatePatch status %d", __func__, status);
3315 if (status == BAD_VALUE) {
3316 return status;
3317 }
3318 }
3319 return status;
3320}
3321
3322status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3323 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3324 status_t status = NAME_NOT_FOUND;
3325 sp<EffectHandle> handle;
3326 // only consider source[0] as this is the only "true" source of a patch
3327 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3328 ALOGV("%s source checkPort status %d", __func__, status);
3329 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3330 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3331 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3332 }
3333 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3334 Mutex::Autolock _l(mProxyLock);
3335 mEffectHandles.emplace(patchHandle, handle);
3336 }
3337 ALOGW_IF(status == BAD_VALUE,
3338 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3339
3340 return status;
3341}
3342
3343status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3344 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3345
3346 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3347 __func__, port->type, port->ext.device.type,
3348 port->ext.device.address, port->id, patch.isSoftware());
3349 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003350 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003351 return NAME_NOT_FOUND;
3352 }
3353 status_t status = NAME_NOT_FOUND;
3354
3355 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3356 Mutex::Autolock _l(mProxyLock);
3357 mDevicePort = *port;
3358 mHalEffect = new EffectModule(mMyCallback,
3359 const_cast<effect_descriptor_t *>(&mDescriptor),
3360 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3361 false /* pinned */, port->id);
3362 if (audio_is_input_device(mDevice.mType)) {
3363 mHalEffect->setInputDevice(mDevice);
3364 } else {
3365 mHalEffect->setDevices({mDevice});
3366 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003367 mHalEffect->configure();
3368
Eric Laurentde8caf42021-08-11 17:19:25 +02003369 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3370 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003371 status = (*handle)->initCheck();
3372 if (status == OK) {
3373 status = mHalEffect->addHandle((*handle).get());
3374 } else {
3375 mHalEffect.clear();
3376 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3377 }
3378 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3379 sp <ThreadBase> thread;
3380 if (audio_port_config_has_input_direction(port)) {
3381 if (patch.isSoftware()) {
3382 thread = patch.mRecord.thread();
3383 } else {
3384 thread = patch.thread().promote();
3385 }
3386 } else {
3387 if (patch.isSoftware()) {
3388 thread = patch.mPlayback.thread();
3389 } else {
3390 thread = patch.thread().promote();
3391 }
3392 }
3393 int enabled;
3394 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3395 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003396 &enabled, &status, false, false /*probe*/,
3397 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003398 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3399 } else {
3400 status = BAD_VALUE;
3401 }
3402 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003403 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003404 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003405 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003406 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003407 bs = (*handle)->disable(&status);
3408 }
3409 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003410 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003411 }
3412 }
3413 return status;
3414}
3415
3416void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003417 sp<EffectHandle> effect;
3418 {
3419 Mutex::Autolock _l(mProxyLock);
3420 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3421 effect = mEffectHandles.at(patchHandle);
3422 mEffectHandles.erase(patchHandle);
3423 }
3424 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003425}
3426
3427
3428size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3429{
3430 Mutex::Autolock _l(mProxyLock);
3431 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003432 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003433 mHalEffect.clear();
3434 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3435 }
3436 return mHalEffect == nullptr ? 0 : 1;
3437}
3438
3439status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3440 sp<EffectHalInterface> effect) {
3441 if (mHalEffect == nullptr) {
3442 return NO_INIT;
3443 }
3444 return mManagerCallback->addEffectToHal(
3445 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3446}
3447
3448status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3449 sp<EffectHalInterface> effect) {
3450 if (mHalEffect == nullptr) {
3451 return NO_INIT;
3452 }
3453 return mManagerCallback->removeEffectFromHal(
3454 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3455}
3456
3457bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3458 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3459 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3460 }
3461 return true;
3462}
3463
3464uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3465 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3466 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3467 return mDevicePort.sample_rate;
3468 }
3469 return DEFAULT_OUTPUT_SAMPLE_RATE;
3470}
3471
3472audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3473 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3474 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3475 return mDevicePort.channel_mask;
3476 }
3477 return AUDIO_CHANNEL_OUT_STEREO;
3478}
3479
3480uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3481 if (isOutput()) {
3482 return audio_channel_count_from_out_mask(channelMask());
3483 }
3484 return audio_channel_count_from_in_mask(channelMask());
3485}
3486
3487void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3488 const Vector<String16> args;
3489 EffectBase::dump(fd, args);
3490
3491 const bool locked = dumpTryLock(mProxyLock);
3492 if (!locked) {
3493 String8 result("DeviceEffectProxy may be deadlocked\n");
3494 write(fd, result.string(), result.size());
3495 }
3496
3497 String8 outStr;
3498 if (mHalEffect != nullptr) {
3499 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3500 } else {
3501 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3502 }
3503 write(fd, outStr.string(), outStr.size());
3504 outStr.clear();
3505
3506 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3507 write(fd, outStr.string(), outStr.size());
3508 outStr.clear();
3509
3510 for (const auto& iter : mEffectHandles) {
3511 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3512 write(fd, outStr.string(), outStr.size());
3513 outStr.clear();
3514 sp<EffectBase> effect = iter.second->effect().promote();
3515 if (effect != nullptr) {
3516 effect->dump(fd, args);
3517 }
3518 }
3519
3520 if (locked) {
3521 mLock.unlock();
3522 }
3523}
3524
3525#undef LOG_TAG
3526#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3527
3528int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3529 return mManagerCallback->newEffectId();
3530}
3531
3532
3533bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3534 EffectHandle *handle, bool unpinIfLast) {
3535 sp<EffectBase> effectBase = handle->effect().promote();
3536 if (effectBase == nullptr) {
3537 return false;
3538 }
3539
3540 sp<EffectModule> effect = effectBase->asEffectModule();
3541 if (effect == nullptr) {
3542 return false;
3543 }
3544
3545 // restore suspended effects if the disconnected handle was enabled and the last one.
3546 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3547 if (remove) {
3548 sp<DeviceEffectProxy> proxy = mProxy.promote();
3549 if (proxy != nullptr) {
3550 proxy->removeEffect(effect);
3551 }
3552 if (handle->enabled()) {
3553 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3554 }
3555 }
3556 return true;
3557}
3558
3559status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3560 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3561 sp<EffectHalInterface> *effect) {
3562 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3563}
3564
3565status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3566 sp<EffectHalInterface> effect) {
3567 sp<DeviceEffectProxy> proxy = mProxy.promote();
3568 if (proxy == nullptr) {
3569 return NO_INIT;
3570 }
3571 return proxy->addEffectToHal(effect);
3572}
3573
3574status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3575 sp<EffectHalInterface> effect) {
3576 sp<DeviceEffectProxy> proxy = mProxy.promote();
3577 if (proxy == nullptr) {
3578 return NO_INIT;
3579 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003580 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003581}
3582
3583bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3584 sp<DeviceEffectProxy> proxy = mProxy.promote();
3585 if (proxy == nullptr) {
3586 return true;
3587 }
3588 return proxy->isOutput();
3589}
3590
3591uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3592 sp<DeviceEffectProxy> proxy = mProxy.promote();
3593 if (proxy == nullptr) {
3594 return DEFAULT_OUTPUT_SAMPLE_RATE;
3595 }
3596 return proxy->sampleRate();
3597}
3598
Eric Laurentf1f22e72021-07-13 14:04:14 +02003599audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3600 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003601 sp<DeviceEffectProxy> proxy = mProxy.promote();
3602 if (proxy == nullptr) {
3603 return AUDIO_CHANNEL_OUT_STEREO;
3604 }
3605 return proxy->channelMask();
3606}
3607
Eric Laurentf1f22e72021-07-13 14:04:14 +02003608uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3609 sp<DeviceEffectProxy> proxy = mProxy.promote();
3610 if (proxy == nullptr) {
3611 return 2;
3612 }
3613 return proxy->channelCount();
3614}
3615
3616audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3617 sp<DeviceEffectProxy> proxy = mProxy.promote();
3618 if (proxy == nullptr) {
3619 return AUDIO_CHANNEL_OUT_STEREO;
3620 }
3621 return proxy->channelMask();
3622}
3623
3624uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003625 sp<DeviceEffectProxy> proxy = mProxy.promote();
3626 if (proxy == nullptr) {
3627 return 2;
3628 }
3629 return proxy->channelCount();
3630}
3631
Eric Laurent76c89f32021-12-03 17:13:23 +01003632void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3633 const sp<EffectBase>& effectBase) {
3634 sp<EffectModule> effect = effectBase->asEffectModule();
3635 if (effect == nullptr) {
3636 return;
3637 }
3638 effect->start();
3639}
3640
3641void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3642 const sp<EffectBase>& effectBase) {
3643 sp<EffectModule> effect = effectBase->asEffectModule();
3644 if (effect == nullptr) {
3645 return;
3646 }
3647 effect->stop();
3648}
3649
Glenn Kasten63238ef2015-03-02 15:50:29 -08003650} // namespace android