blob: e6d7cf7751cb7100e982a7273eb5cb7d1a6c82cc [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),
572 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800573#ifdef FLOAT_EFFECT_CHAIN
574 , mSupportsFloat(false)
575#endif
576{
577 ALOGV("Constructor %p pinned %d", this, pinned);
578 int lStatus;
579
580 // create effect engine from effect factory
581 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800582 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800583 if (mStatus != NO_ERROR) {
584 return;
585 }
586 lStatus = init();
587 if (lStatus < 0) {
588 mStatus = lStatus;
589 goto Error;
590 }
591
592 setOffloaded(callback->isOffload(), callback->io());
593 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
594
595 return;
596Error:
597 mEffectInterface.clear();
598 ALOGV("Constructor Error %d", mStatus);
599}
600
601AudioFlinger::EffectModule::~EffectModule()
602{
603 ALOGV("Destructor %p", this);
604 if (mEffectInterface != 0) {
605 char uuidStr[64];
606 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
607 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
608 this, uuidStr);
609 release_l();
610 }
611
612}
613
Eric Laurentfa1e1232016-08-02 19:01:49 -0700614bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800615 Mutex::Autolock _l(mLock);
616
Eric Laurentfa1e1232016-08-02 19:01:49 -0700617 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 switch (mState) {
619 case RESTART:
620 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700621 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800622
623 case STARTING:
624 // clear auxiliary effect input buffer for next accumulation
625 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
626 memset(mConfig.inputCfg.buffer.raw,
627 0,
628 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
629 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700630 if (start_l() == NO_ERROR) {
631 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700632 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700633 } else {
634 mState = IDLE;
635 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 break;
637 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900638 // volume control for offload and direct threads must take effect immediately.
639 if (stop_l() == NO_ERROR
640 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700641 mDisableWaitCnt = mMaxDisableWaitCnt;
642 } else {
643 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
644 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800645 mState = STOPPED;
646 break;
647 case STOPPED:
648 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
649 // turn off sequence.
650 if (--mDisableWaitCnt == 0) {
651 reset_l();
652 mState = IDLE;
653 }
654 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200655 case ACTIVE:
656 for (size_t i = 0; i < mHandles.size(); i++) {
657 if (!mHandles[i]->disconnected()) {
658 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
659 }
660 }
661 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800662 default: //IDLE , ACTIVE, DESTROYED
663 break;
664 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700665
666 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800667}
668
669void AudioFlinger::EffectModule::process()
670{
671 Mutex::Autolock _l(mLock);
672
Mikhail Naganov022b9952017-01-04 16:36:51 -0800673 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800674 return;
675 }
676
rago94a1ee82017-07-21 15:11:02 -0700677 const uint32_t inChannelCount =
678 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
679 const uint32_t outChannelCount =
680 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
681 const bool auxType =
682 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
683
Andy Hungfa69ca32017-11-30 10:07:53 -0800684 // safeInputOutputSampleCount is 0 if the channel count between input and output
685 // buffers do not match. This prevents automatic accumulation or copying between the
686 // input and output effect buffers without an intermediary effect process.
687 // TODO: consider implementing channel conversion.
688 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700689 mInChannelCountRequested != mOutChannelCountRequested ? 0
690 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800691 mConfig.inputCfg.buffer.frameCount,
692 mConfig.outputCfg.buffer.frameCount);
693 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
694#ifdef FLOAT_EFFECT_CHAIN
695 accumulate_float(
696 mConfig.outputCfg.buffer.f32,
697 mConfig.inputCfg.buffer.f32,
698 safeInputOutputSampleCount);
699#else
700 accumulate_i16(
701 mConfig.outputCfg.buffer.s16,
702 mConfig.inputCfg.buffer.s16,
703 safeInputOutputSampleCount);
704#endif
705 };
706 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
707#ifdef FLOAT_EFFECT_CHAIN
708 memcpy(
709 mConfig.outputCfg.buffer.f32,
710 mConfig.inputCfg.buffer.f32,
711 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
712
713#else
714 memcpy(
715 mConfig.outputCfg.buffer.s16,
716 mConfig.inputCfg.buffer.s16,
717 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
718#endif
719 };
720
Eric Laurentca7cc822012-11-19 14:55:58 -0800721 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700722 int ret;
723 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700724 if (auxType) {
725 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800726 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700727#ifdef FLOAT_EFFECT_CHAIN
728 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800729#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700730 // Do in-place float conversion for auxiliary effect input buffer.
731 static_assert(sizeof(float) <= sizeof(int32_t),
732 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
733
Andy Hungfa69ca32017-11-30 10:07:53 -0800734 memcpy_to_float_from_q4_27(
735 mConfig.inputCfg.buffer.f32,
736 mConfig.inputCfg.buffer.s32,
737 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800738#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800739 } else
Andy Hung116a4982017-11-30 10:15:08 -0800740#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800741 {
Andy Hung116a4982017-11-30 10:15:08 -0800742#ifdef FLOAT_AUX
743 memcpy_to_i16_from_float(
744 mConfig.inputCfg.buffer.s16,
745 mConfig.inputCfg.buffer.f32,
746 mConfig.inputCfg.buffer.frameCount);
747#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 memcpy_to_i16_from_q4_27(
749 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700750 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800751 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800752#endif
rago94a1ee82017-07-21 15:11:02 -0700753 }
rago94a1ee82017-07-21 15:11:02 -0700754 }
755#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800756 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
757 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
758
759 if (!auxType && mInChannelCountRequested != inChannelCount) {
760 adjust_channels(
761 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
762 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
763 sizeof(float),
764 sizeof(float)
765 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
766 inBuffer = mInConversionBuffer;
767 }
768 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
769 && mOutChannelCountRequested != outChannelCount) {
770 adjust_selected_channels(
771 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
772 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
773 sizeof(float),
774 sizeof(float)
775 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
776 outBuffer = mOutConversionBuffer;
777 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800778 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
779 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800780 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
782 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700783 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800784 memcpy_to_i16_from_float(
785 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800786 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700789 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800791 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
793 goto data_bypass;
794 }
795 memcpy_to_i16_from_float(
796 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800797 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800798 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800799 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700800 }
801 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800802#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800803 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800804#ifdef FLOAT_EFFECT_CHAIN
805 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800806 sp<EffectBufferHalInterface> target =
807 mOutChannelCountRequested != outChannelCount
808 ? mOutConversionBuffer : mOutBuffer;
809
Andy Hungfa69ca32017-11-30 10:07:53 -0800810 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800811 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800812 mOutConversionBuffer->audioBuffer()->s16,
813 outChannelCount * mConfig.outputCfg.buffer.frameCount);
814 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800815 if (mOutChannelCountRequested != outChannelCount) {
816 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
817 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
818 sizeof(float),
819 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
820 }
rago94a1ee82017-07-21 15:11:02 -0700821#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700822 } else {
rago94a1ee82017-07-21 15:11:02 -0700823#ifdef FLOAT_EFFECT_CHAIN
824 data_bypass:
825#endif
826 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800827 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700828 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800829 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700830 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800831 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700832 }
833 }
834 ret = -ENODATA;
835 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800836
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 // force transition to IDLE state when engine is ready
838 if (mState == STOPPED && ret == -ENODATA) {
839 mDisableWaitCnt = 1;
840 }
841
842 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700843 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800844#ifdef FLOAT_AUX
845 const size_t size =
846 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
847#else
rago94a1ee82017-07-21 15:11:02 -0700848 const size_t size =
849 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800850#endif
rago94a1ee82017-07-21 15:11:02 -0700851 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800852 }
853 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700854 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800855 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
856 // If an insert effect is idle and input buffer is different from output buffer,
857 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700858 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700859 // similar handling with data_bypass above.
860 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
861 accumulateInputToOutput();
862 } else { // EFFECT_BUFFER_ACCESS_WRITE
863 copyInputToOutput();
864 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800865 }
866 }
867}
868
869void AudioFlinger::EffectModule::reset_l()
870{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700871 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800872 return;
873 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700874 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800875}
876
877status_t AudioFlinger::EffectModule::configure()
878{
rago94a1ee82017-07-21 15:11:02 -0700879 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700880 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700881 uint32_t size;
882 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700883 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700884
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700885 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700886 status = NO_INIT;
887 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800888 }
889
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800891 // TODO: handle configuration of input (record) SW effects above the HAL,
892 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
893 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700894 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200895 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800896 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200897 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800898
899 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800900 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
901 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
902 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
903 mConfig.inputCfg.channels);
904 }
905#ifndef MULTICHANNEL_EFFECT_CHAIN
906 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
907 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
908 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
909 mConfig.outputCfg.channels);
910 }
911#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800912 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800913#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700914 // TODO: Update this logic when multichannel effects are implemented.
915 // For offloaded tracks consider mono output as stereo for proper effect initialization
916 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
917 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
918 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
919 ALOGV("Overriding effect input and output as STEREO");
920 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800921#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800922 }
jiabineb3bda02020-06-30 14:07:03 -0700923 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700924 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700925 mConfig.inputCfg.channels |= hapticChannelMask;
926 mConfig.outputCfg.channels |= hapticChannelMask;
927 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800928 mInChannelCountRequested =
929 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
930 mOutChannelCountRequested =
931 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700932
rago94a1ee82017-07-21 15:11:02 -0700933 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
934 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900935
936 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700937 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900938 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
939 ALOGV("Overriding effect input as 48kHz");
940 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700941 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900942 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800943 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
944 mConfig.inputCfg.bufferProvider.cookie = NULL;
945 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
946 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
947 mConfig.outputCfg.bufferProvider.cookie = NULL;
948 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
949 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
950 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
951 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800952 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800953 // always overwrites output buffer: input buffer == output buffer
954 // - in other sessions:
955 // last effect in the chain accumulates in output buffer: input buffer != output buffer
956 // other effect: overwrites output buffer: input buffer == output buffer
957 // Auxiliary effect:
958 // accumulates in output buffer: input buffer != output buffer
959 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700960 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800961 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
962 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700963 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
965
Eric Laurent6b446ce2019-12-13 10:56:31 -0800966 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700967 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800968 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800969
970 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700971 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700972 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800973 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700974 &mConfig,
975 &size,
976 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700977 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800978 status = cmdStatus;
979 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800980
981#ifdef MULTICHANNEL_EFFECT_CHAIN
982 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700983 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800984 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
985 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
986 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700987 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
988 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800989 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
990 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
991 }
992 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
993 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
994 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
995 }
996 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700997 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800998 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700999 &mConfig,
1000 &size,
1001 &cmdStatus);
1002 if (status == NO_ERROR) {
1003 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001004 }
1005 }
1006#endif
1007
1008#ifdef FLOAT_EFFECT_CHAIN
1009 if (status == NO_ERROR) {
1010 mSupportsFloat = true;
1011 }
1012
1013 if (status != NO_ERROR) {
1014 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1015 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1016 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1017 size = sizeof(int);
1018 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1019 sizeof(mConfig),
1020 &mConfig,
1021 &size,
1022 &cmdStatus);
1023 if (status == NO_ERROR) {
1024 status = cmdStatus;
1025 }
1026 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001027 mSupportsFloat = false;
1028 ALOGVV("config worked with 16 bit");
1029 } else {
1030 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001031 }
rago94a1ee82017-07-21 15:11:02 -07001032 }
1033#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001034
rago94a1ee82017-07-21 15:11:02 -07001035 if (status == NO_ERROR) {
1036 // Establish Buffer strategy
1037 setInBuffer(mInBuffer);
1038 setOutBuffer(mOutBuffer);
1039
1040 // Update visualizer latency
1041 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1042 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1043 effect_param_t *p = (effect_param_t *)buf32;
1044
1045 p->psize = sizeof(uint32_t);
1046 p->vsize = sizeof(uint32_t);
1047 size = sizeof(int);
1048 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1049
Andy Hungfda44002021-06-03 17:23:16 -07001050 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001051
1052 *((int32_t *)p->data + 1)= latency;
1053 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1054 sizeof(effect_param_t) + 8,
1055 &buf32,
1056 &size,
1057 &cmdStatus);
1058 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001059 }
1060
Andy Hung05083ac2017-12-14 15:00:28 -08001061 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1062 mMaxDisableWaitCnt = (uint32_t)std::max(
1063 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1064 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1065 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001066
Eric Laurentd0ebb532013-04-02 16:41:41 -07001067exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001068 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001069 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001070 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001071 return status;
1072}
1073
1074status_t AudioFlinger::EffectModule::init()
1075{
1076 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001077 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001078 return NO_INIT;
1079 }
1080 status_t cmdStatus;
1081 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001082 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1083 0,
1084 NULL,
1085 &size,
1086 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 if (status == 0) {
1088 status = cmdStatus;
1089 }
1090 return status;
1091}
1092
Eric Laurent1b928682014-10-02 19:41:47 -07001093void AudioFlinger::EffectModule::addEffectToHal_l()
1094{
1095 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1096 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001097 if (mAddedToHal) {
1098 return;
1099 }
1100
Andy Hungfda44002021-06-03 17:23:16 -07001101 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001102 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001103 }
1104}
1105
Eric Laurentfa1e1232016-08-02 19:01:49 -07001106// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001107status_t AudioFlinger::EffectModule::start()
1108{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001109 status_t status;
1110 {
1111 Mutex::Autolock _l(mLock);
1112 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001113 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001114 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001115 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001116 }
1117 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001118}
1119
1120status_t AudioFlinger::EffectModule::start_l()
1121{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001122 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001123 return NO_INIT;
1124 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001125 if (mStatus != NO_ERROR) {
1126 return mStatus;
1127 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 status_t cmdStatus;
1129 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001130 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1131 0,
1132 NULL,
1133 &size,
1134 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 if (status == 0) {
1136 status = cmdStatus;
1137 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001138 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001139 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 }
1141 return status;
1142}
1143
1144status_t AudioFlinger::EffectModule::stop()
1145{
1146 Mutex::Autolock _l(mLock);
1147 return stop_l();
1148}
1149
1150status_t AudioFlinger::EffectModule::stop_l()
1151{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001152 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001153 return NO_INIT;
1154 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001155 if (mStatus != NO_ERROR) {
1156 return mStatus;
1157 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001158 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001159 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001160
1161 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001162 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1163 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1164 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001165 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001166 mSetVolumeReentrantTid = INVALID_PID;
1167 }
1168
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001169 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1170 0,
1171 NULL,
1172 &size,
1173 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001174 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001175 status = cmdStatus;
1176 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001177 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001178 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001179 }
1180 return status;
1181}
1182
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001183// must be called with EffectChain::mLock held
1184void AudioFlinger::EffectModule::release_l()
1185{
1186 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001187 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001188 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001189 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001190 mEffectInterface.clear();
1191 }
1192}
1193
Eric Laurent6b446ce2019-12-13 10:56:31 -08001194status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001195{
1196 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1197 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001198 if (!mAddedToHal) {
1199 return NO_ERROR;
1200 }
1201
Andy Hungfda44002021-06-03 17:23:16 -07001202 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001203 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001204 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001205 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001206}
1207
Andy Hunge4a1d912016-08-17 14:11:13 -07001208// round up delta valid if value and divisor are positive.
1209template <typename T>
1210static T roundUpDelta(const T &value, const T &divisor) {
1211 T remainder = value % divisor;
1212 return remainder == 0 ? 0 : divisor - remainder;
1213}
1214
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001215status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1216 const std::vector<uint8_t>& cmdData,
1217 int32_t maxReplySize,
1218 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001219{
1220 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001221 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001222
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001223 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001224 return NO_INIT;
1225 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001226 if (mStatus != NO_ERROR) {
1227 return mStatus;
1228 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001229 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1230 return -EINVAL;
1231 }
1232 size_t cmdSize = cmdData.size();
1233 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1234 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1235 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001236 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001237 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001238 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001239 android_errorWriteLog(0x534e4554, "33003822");
1240 return -EINVAL;
1241 }
1242 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001243 (maxReplySize < sizeof(effect_param_t) ||
1244 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001245 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001246 return -EINVAL;
1247 }
ragoe2759072016-11-22 18:02:48 -08001248 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001249 (sizeof(effect_param_t) > maxReplySize
1250 || param->psize > maxReplySize - sizeof(effect_param_t)
1251 || param->vsize > maxReplySize - sizeof(effect_param_t)
1252 - param->psize
1253 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1254 maxReplySize
1255 - sizeof(effect_param_t)
1256 - param->psize
1257 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001258 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1259 android_errorWriteLog(0x534e4554, "32705438");
1260 return -EINVAL;
1261 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001262 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001263 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1264 && // DEFERRED not generally used
1265 (param == nullptr
1266 || param->psize > cmdSize - sizeof(effect_param_t)
1267 || param->vsize > cmdSize - sizeof(effect_param_t)
1268 - param->psize
1269 || roundUpDelta(param->psize,
1270 (uint32_t) sizeof(int)) >
1271 cmdSize
1272 - sizeof(effect_param_t)
1273 - param->psize
1274 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001275 android_errorWriteLog(0x534e4554, "30204301");
1276 return -EINVAL;
1277 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001278 uint32_t replySize = maxReplySize;
1279 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001280 status_t status = mEffectInterface->command(cmdCode,
1281 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001282 const_cast<uint8_t*>(cmdData.data()),
1283 &replySize,
1284 reply->data());
1285 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001286 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001287 for (size_t i = 1; i < mHandles.size(); i++) {
1288 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001289 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001290 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 }
1292 }
1293 }
1294 return status;
1295}
1296
Eric Laurentca7cc822012-11-19 14:55:58 -08001297bool AudioFlinger::EffectModule::isProcessEnabled() const
1298{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001299 if (mStatus != NO_ERROR) {
1300 return false;
1301 }
1302
Eric Laurentca7cc822012-11-19 14:55:58 -08001303 switch (mState) {
1304 case RESTART:
1305 case ACTIVE:
1306 case STOPPING:
1307 case STOPPED:
1308 return true;
1309 case IDLE:
1310 case STARTING:
1311 case DESTROYED:
1312 default:
1313 return false;
1314 }
1315}
1316
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001317bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1318{
Andy Hungfda44002021-06-03 17:23:16 -07001319 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001320}
1321
1322bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1323{
1324 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1325}
1326
Mikhail Naganov022b9952017-01-04 16:36:51 -08001327void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001328 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001329
1330 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001331 if (buffer != 0) {
1332 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1333 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1334 } else {
1335 mConfig.inputCfg.buffer.raw = NULL;
1336 }
1337 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001338 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001339
1340#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001341 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001342 // Theoretically insert effects can also do in-place conversions (destroying
1343 // the original buffer) when the output buffer is identical to the input buffer,
1344 // but we don't optimize for it here.
1345 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001346 const uint32_t inChannelCount =
1347 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1348 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001349 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001350 // we need to translate - create hidl shared buffer and intercept
1351 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001352 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1353 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1354 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001355
1356 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1357 __func__, inChannels, inFrameCount, size);
1358
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001359 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001360 || size > mInConversionBuffer->getSize())) {
1361 mInConversionBuffer.clear();
1362 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001363 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001364 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001365 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001366 mInConversionBuffer->setFrameCount(inFrameCount);
1367 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001368 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001369 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001370 }
1371 }
1372#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001373}
1374
1375void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001376 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001377
1378 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001379 if (buffer != 0) {
1380 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1381 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1382 } else {
1383 mConfig.outputCfg.buffer.raw = NULL;
1384 }
1385 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001386 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001387
1388#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001389 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001390 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001391 const uint32_t outChannelCount =
1392 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1393 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001394 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001395 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001396 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1397 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1398 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001399
1400 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1401 __func__, outChannels, outFrameCount, size);
1402
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001403 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001404 || size > mOutConversionBuffer->getSize())) {
1405 mOutConversionBuffer.clear();
1406 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001407 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001408 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001409 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001410 mOutConversionBuffer->setFrameCount(outFrameCount);
1411 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001412 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001413 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001414 }
1415 }
1416#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001417}
1418
Eric Laurentca7cc822012-11-19 14:55:58 -08001419status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1420{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001421 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001422 if (mStatus != NO_ERROR) {
1423 return mStatus;
1424 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1427 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1428 if (isProcessEnabled() &&
1429 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001430 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1431 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 uint32_t volume[2];
1433 uint32_t *pVolume = NULL;
1434 uint32_t size = sizeof(volume);
1435 volume[0] = *left;
1436 volume[1] = *right;
1437 if (controller) {
1438 pVolume = volume;
1439 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001440 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1441 size,
1442 volume,
1443 &size,
1444 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1446 *left = volume[0];
1447 *right = volume[1];
1448 }
1449 }
1450 return status;
1451}
1452
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001453void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1454{
Zhou Songd505c642020-02-20 16:35:37 +08001455 // for offload or direct thread, if the effect chain has non-offloadable
1456 // effect and any effect module within the chain has volume control, then
1457 // volume control is delegated to effect, otherwise, set volume to hal.
1458 if (mEffectCallback->isOffloadOrDirect() &&
1459 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001460 float vol_l = (float)left / (1 << 24);
1461 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001462 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001463 }
1464}
1465
jiabin8f278ee2019-11-11 12:16:27 -08001466status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1467 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001468{
jiabin8f278ee2019-11-11 12:16:27 -08001469 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1470 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 return NO_ERROR;
1472 }
1473
1474 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001475 if (mStatus != NO_ERROR) {
1476 return mStatus;
1477 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001479 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 status_t cmdStatus;
1481 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001482 // FIXME: use audio device types and addresses when the hal interface is ready.
1483 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001484 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001485 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001486 &size,
1487 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001488 }
1489 return status;
1490}
1491
jiabin8f278ee2019-11-11 12:16:27 -08001492status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1493{
1494 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1495}
1496
1497status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1498{
1499 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1500}
1501
Eric Laurentca7cc822012-11-19 14:55:58 -08001502status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1503{
1504 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001505 if (mStatus != NO_ERROR) {
1506 return mStatus;
1507 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 status_t status = NO_ERROR;
1509 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1510 status_t cmdStatus;
1511 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001512 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1513 sizeof(audio_mode_t),
1514 &mode,
1515 &size,
1516 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 if (status == NO_ERROR) {
1518 status = cmdStatus;
1519 }
1520 }
1521 return status;
1522}
1523
1524status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1525{
1526 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001527 if (mStatus != NO_ERROR) {
1528 return mStatus;
1529 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 status_t status = NO_ERROR;
1531 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1532 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001533 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1534 sizeof(audio_source_t),
1535 &source,
1536 &size,
1537 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001538 }
1539 return status;
1540}
1541
Eric Laurent5baf2af2013-09-12 17:37:00 -07001542status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1543{
1544 Mutex::Autolock _l(mLock);
1545 if (mStatus != NO_ERROR) {
1546 return mStatus;
1547 }
1548 status_t status = NO_ERROR;
1549 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1550 status_t cmdStatus;
1551 uint32_t size = sizeof(status_t);
1552 effect_offload_param_t cmd;
1553
1554 cmd.isOffload = offloaded;
1555 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001556 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1557 sizeof(effect_offload_param_t),
1558 &cmd,
1559 &size,
1560 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001561 if (status == NO_ERROR) {
1562 status = cmdStatus;
1563 }
1564 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1565 } else {
1566 if (offloaded) {
1567 status = INVALID_OPERATION;
1568 }
1569 mOffloaded = false;
1570 }
1571 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1572 return status;
1573}
1574
1575bool AudioFlinger::EffectModule::isOffloaded() const
1576{
1577 Mutex::Autolock _l(mLock);
1578 return mOffloaded;
1579}
1580
jiabineb3bda02020-06-30 14:07:03 -07001581/*static*/
1582bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1583 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1584}
1585
1586bool AudioFlinger::EffectModule::isHapticGenerator() const {
1587 return isHapticGenerator(&mDescriptor.type);
1588}
1589
jiabine70bc7f2020-06-30 22:07:55 -07001590status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1591{
1592 if (mStatus != NO_ERROR) {
1593 return mStatus;
1594 }
1595 if (!isHapticGenerator()) {
1596 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1597 return INVALID_OPERATION;
1598 }
1599
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001600 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1601 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001602 param->psize = sizeof(int32_t);
1603 param->vsize = sizeof(int32_t) * 2;
1604 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1605 *((int32_t*)param->data + 1) = id;
1606 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001607 std::vector<uint8_t> response;
1608 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001609 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001610 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1611 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001612 }
1613 return status;
1614}
1615
Lais Andradebc3f37a2021-07-02 00:13:19 +01001616status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001617{
1618 if (mStatus != NO_ERROR) {
1619 return mStatus;
1620 }
1621 if (!isHapticGenerator()) {
1622 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1623 return INVALID_OPERATION;
1624 }
1625
Lais Andradebc3f37a2021-07-02 00:13:19 +01001626 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001627 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001628 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001629 effect_param_t *param = (effect_param_t*) request.data();
1630 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001631 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001632 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1633 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001634 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1635 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1636 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001637 std::vector<uint8_t> response;
1638 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1639 if (status == NO_ERROR) {
1640 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1641 status = *reinterpret_cast<const status_t*>(response.data());
1642 }
1643 return status;
1644}
1645
Andy Hungbded9c82017-11-30 18:47:35 -08001646static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1647 std::stringstream ss;
1648
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001649 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001650 return "nullptr"; // make different than below
1651 } else if (buffer->externalData() != nullptr) {
1652 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1653 << " -> "
1654 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1655 } else {
1656 ss << buffer->audioBuffer()->raw;
1657 }
1658 return ss.str();
1659}
Marco Nelissenb2208842014-02-07 14:00:50 -08001660
Eric Laurent41709552019-12-16 19:34:05 -08001661void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001662{
Eric Laurent41709552019-12-16 19:34:05 -08001663 EffectBase::dump(fd, args);
1664
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001667
Eric Laurent41709552019-12-16 19:34:05 -08001668 result.append("\t\tStatus Engine:\n");
1669 result.appendFormat("\t\t%03d %p\n",
1670 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001671
1672 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001673
1674 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001675 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1676 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1677 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001678 mConfig.inputCfg.buffer.frameCount,
1679 mConfig.inputCfg.samplingRate,
1680 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001681 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001682 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001683
1684 result.append("\t\t- Output configuration:\n");
1685 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001686 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001687 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 mConfig.outputCfg.buffer.frameCount,
1689 mConfig.outputCfg.samplingRate,
1690 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001691 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001692 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001693
rago94a1ee82017-07-21 15:11:02 -07001694#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001695
Andy Hungbded9c82017-11-30 18:47:35 -08001696 result.appendFormat("\t\t- HAL buffers:\n"
1697 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1698 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1699 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1700 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1701 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001702#endif
1703
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 write(fd, result.string(), result.length());
1705
Mikhail Naganov4d547672019-02-22 14:19:19 -08001706 if (mEffectInterface != 0) {
1707 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1708 (void)mEffectInterface->dump(fd);
1709 }
1710
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 if (locked) {
1712 mLock.unlock();
1713 }
1714}
1715
1716// ----------------------------------------------------------------------------
1717// EffectHandle implementation
1718// ----------------------------------------------------------------------------
1719
1720#undef LOG_TAG
1721#define LOG_TAG "AudioFlinger::EffectHandle"
1722
Eric Laurent41709552019-12-16 19:34:05 -08001723AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001724 const sp<AudioFlinger::Client>& client,
1725 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001726 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001727 : BnEffect(),
1728 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001729 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1730 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001731{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001732 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001733
1734 if (client == 0) {
1735 return;
1736 }
1737 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1738 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001739 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001740 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001741 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001743 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001744 return;
1745 }
Glenn Kastene75da402013-11-20 13:54:52 -08001746 new(mCblk) effect_param_cblk_t();
1747 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001748}
1749
1750AudioFlinger::EffectHandle::~EffectHandle()
1751{
1752 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 disconnect(false);
1754}
1755
Andy Hungc747c532022-03-07 21:41:14 -08001756// Creates an association between Binder code to name for IEffect.
1757#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1758BINDER_METHOD_ENTRY(enable) \
1759BINDER_METHOD_ENTRY(disable) \
1760BINDER_METHOD_ENTRY(command) \
1761BINDER_METHOD_ENTRY(disconnect) \
1762BINDER_METHOD_ENTRY(getCblk) \
1763
1764// singleton for Binder Method Statistics for IEffect
1765mediautils::MethodStatistics<int>& getIEffectStatistics() {
1766 using Code = int;
1767
1768#pragma push_macro("BINDER_METHOD_ENTRY")
1769#undef BINDER_METHOD_ENTRY
1770#define BINDER_METHOD_ENTRY(ENTRY) \
1771 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1772
1773 static mediautils::MethodStatistics<Code> methodStatistics{
1774 IEFFECT_BINDER_METHOD_MACRO_LIST
1775 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1776 };
1777#pragma pop_macro("BINDER_METHOD_ENTRY")
1778
1779 return methodStatistics;
1780}
1781
1782status_t AudioFlinger::EffectHandle::onTransact(
1783 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001784 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1785 mediautils::TimeCheck check(
1786 std::string("IEffect::").append(methodName),
1787 [code](bool timeout, float elapsedMs) {
1788 if (timeout) {
1789 ; // we don't timeout right now on the effect interface.
1790 } else {
1791 getIEffectStatistics().event(code, elapsedMs);
1792 }
1793 }, 0 /* timeoutMs */);
Andy Hungc747c532022-03-07 21:41:14 -08001794 return BnEffect::onTransact(code, data, reply, flags);
1795}
1796
Glenn Kastene75da402013-11-20 13:54:52 -08001797status_t AudioFlinger::EffectHandle::initCheck()
1798{
1799 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1800}
1801
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001802#define RETURN(code) \
1803 *_aidl_return = (code); \
1804 return Status::ok();
1805
1806Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001807{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001810 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001811 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001812 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001813 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001814 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001815 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001817
1818 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001819 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 }
1821
1822 mEnabled = true;
1823
Eric Laurent6c796322019-04-09 14:13:17 -07001824 status_t status = effect->updatePolicyState();
1825 if (status != NO_ERROR) {
1826 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001827 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001828 }
1829
Eric Laurent6b446ce2019-12-13 10:56:31 -08001830 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001831
1832 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001833 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001834 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 }
1836
Eric Laurent6b446ce2019-12-13 10:56:31 -08001837 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001838 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001839 mEnabled = false;
1840 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001842}
1843
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001844Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001845{
1846 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001847 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001848 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001849 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001850 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001851 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001854 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001855
1856 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001857 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 }
1859 mEnabled = false;
1860
Eric Laurent6c796322019-04-09 14:13:17 -07001861 effect->updatePolicyState();
1862
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001863 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001864 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 }
1866
Eric Laurent6b446ce2019-12-13 10:56:31 -08001867 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001868 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001869}
1870
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001872{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001873 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001875 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001876}
1877
1878void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1879{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001880 AutoMutex _l(mLock);
1881 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1882 if (mDisconnected) {
1883 if (unpinIfLast) {
1884 android_errorWriteLog(0x534e4554, "32707507");
1885 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001886 return;
1887 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001888 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001889 {
Eric Laurent41709552019-12-16 19:34:05 -08001890 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001891 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001892 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001893 ALOGW("%s Effect handle %p disconnected after thread destruction",
1894 __func__, this);
1895 }
1896 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001897 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001898 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001899
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 if (mClient != 0) {
1901 if (mCblk != NULL) {
1902 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1903 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1904 }
1905 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001906 // Client destructor must run with AudioFlinger client mutex locked
1907 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001908 mClient.clear();
1909 }
1910}
1911
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1913 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1914 return Status::ok();
1915}
1916
1917Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1918 const std::vector<uint8_t>& cmdData,
1919 int32_t maxResponseSize,
1920 std::vector<uint8_t>* response,
1921 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001922{
1923 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001924 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001925
Eric Laurentc7ab3092017-06-15 18:43:46 -07001926 // reject commands reserved for internal use by audio framework if coming from outside
1927 // of audioserver
1928 switch(cmdCode) {
1929 case EFFECT_CMD_ENABLE:
1930 case EFFECT_CMD_DISABLE:
1931 case EFFECT_CMD_SET_PARAM:
1932 case EFFECT_CMD_SET_PARAM_DEFERRED:
1933 case EFFECT_CMD_SET_PARAM_COMMIT:
1934 case EFFECT_CMD_GET_PARAM:
1935 break;
1936 default:
1937 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1938 break;
1939 }
1940 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001941 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001942 }
1943
Eric Laurent1ffc5852016-12-15 14:46:09 -08001944 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001945 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001946 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001948 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001949 writeToBuffer(NO_ERROR, response);
1950 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001951 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001952 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001953 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001954 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001955 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001956 writeToBuffer(NO_ERROR, response);
1957 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001958 }
1959
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001960 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001961 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001962 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001963 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001964 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001965 // only get parameter command is permitted for applications not controlling the effect
1966 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001969
1970 // handle commands that are not forwarded transparently to effect engine
1971 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001972 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001973 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001974 }
1975
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001976 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001977 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001978 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001979 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001980 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001981
Eric Laurentca7cc822012-11-19 14:55:58 -08001982 // No need to trylock() here as this function is executed in the binder thread serving a
1983 // particular client process: no risk to block the whole media server process or mixer
1984 // threads if we are stuck here
1985 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001986 // keep local copy of index in case of client corruption b/32220769
1987 const uint32_t clientIndex = mCblk->clientIndex;
1988 const uint32_t serverIndex = mCblk->serverIndex;
1989 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1990 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001991 mCblk->serverIndex = 0;
1992 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001993 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001994 }
1995 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001996 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001997 for (uint32_t index = serverIndex; index < clientIndex;) {
1998 int *p = (int *)(mBuffer + index);
1999 const int size = *p++;
2000 if (size < 0
2001 || size > EFFECT_PARAM_BUFFER_SIZE
2002 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002003 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002004 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002005 break;
2006 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002007
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 std::copy(reinterpret_cast<const uint8_t*>(p),
2009 reinterpret_cast<const uint8_t*>(p) + size,
2010 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002011
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002012 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002013 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002014 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002015 sizeof(int),
2016 &replyBuffer);
2017 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002018
2019 // verify shared memory: server index shouldn't change; client index can't go back.
2020 if (serverIndex != mCblk->serverIndex
2021 || clientIndex > mCblk->clientIndex) {
2022 android_errorWriteLog(0x534e4554, "32220769");
2023 status = BAD_VALUE;
2024 break;
2025 }
2026
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 // stop at first error encountered
2028 if (ret != NO_ERROR) {
2029 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002030 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 break;
2032 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002033 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002034 break;
2035 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002036 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 }
2038 mCblk->serverIndex = 0;
2039 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002040 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002041 }
2042
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002043 status_t status = effect->command(cmdCode,
2044 cmdData,
2045 maxResponseSize,
2046 response);
2047 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002048}
2049
2050void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2051{
2052 ALOGV("setControl %p control %d", this, hasControl);
2053
2054 mHasControl = hasControl;
2055 mEnabled = enabled;
2056
2057 if (signal && mEffectClient != 0) {
2058 mEffectClient->controlStatusChanged(hasControl);
2059 }
2060}
2061
2062void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002063 const std::vector<uint8_t>& cmdData,
2064 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002065{
2066 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002067 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002068 }
2069}
2070
2071
2072
2073void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2074{
2075 if (mEffectClient != 0) {
2076 mEffectClient->enableStatusChanged(enabled);
2077 }
2078}
2079
Eric Laurentde8caf42021-08-11 17:19:25 +02002080void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2081{
2082 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2083 mEffectClient->framesProcessed(frames);
2084 }
2085}
2086
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002087void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002088{
2089 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2090
Marco Nelissenb2208842014-02-07 14:00:50 -08002091 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002092 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002093 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002094 mHasControl ? "yes" : "no",
2095 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002096 mCblk ? mCblk->clientIndex : 0,
2097 mCblk ? mCblk->serverIndex : 0
2098 );
2099
2100 if (locked) {
2101 mCblk->lock.unlock();
2102 }
2103}
2104
2105#undef LOG_TAG
2106#define LOG_TAG "AudioFlinger::EffectChain"
2107
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002108AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2109 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002110 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002111 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002112 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002113 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002114{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002115 sp<ThreadBase> p = thread.promote();
2116 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002117 return;
2118 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002119 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002120 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2121 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002122}
2123
2124AudioFlinger::EffectChain::~EffectChain()
2125{
Eric Laurentca7cc822012-11-19 14:55:58 -08002126}
2127
2128// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2129sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2130 effect_descriptor_t *descriptor)
2131{
2132 size_t size = mEffects.size();
2133
2134 for (size_t i = 0; i < size; i++) {
2135 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2136 return mEffects[i];
2137 }
2138 }
2139 return 0;
2140}
2141
2142// getEffectFromId_l() must be called with ThreadBase::mLock held
2143sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2144{
2145 size_t size = mEffects.size();
2146
2147 for (size_t i = 0; i < size; i++) {
2148 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2149 if (id == 0 || mEffects[i]->id() == id) {
2150 return mEffects[i];
2151 }
2152 }
2153 return 0;
2154}
2155
2156// getEffectFromType_l() must be called with ThreadBase::mLock held
2157sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2158 const effect_uuid_t *type)
2159{
2160 size_t size = mEffects.size();
2161
2162 for (size_t i = 0; i < size; i++) {
2163 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2164 return mEffects[i];
2165 }
2166 }
2167 return 0;
2168}
2169
Eric Laurent6c796322019-04-09 14:13:17 -07002170std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2171{
2172 std::vector<int> ids;
2173 Mutex::Autolock _l(mLock);
2174 for (size_t i = 0; i < mEffects.size(); i++) {
2175 ids.push_back(mEffects[i]->id());
2176 }
2177 return ids;
2178}
2179
Eric Laurentca7cc822012-11-19 14:55:58 -08002180void AudioFlinger::EffectChain::clearInputBuffer()
2181{
2182 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002184}
2185
2186// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002187void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002188{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002189 if (mInBuffer == NULL) {
2190 return;
2191 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002192 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2193 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002194
Eric Laurent6b446ce2019-12-13 10:56:31 -08002195 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002196 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002197}
2198
2199// Must be called with EffectChain::mLock locked
2200void AudioFlinger::EffectChain::process_l()
2201{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002202 // never process effects when:
2203 // - on an OFFLOAD thread
2204 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002205 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002206 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002207 bool tracksOnSession = (trackCnt() != 0);
2208
2209 if (!tracksOnSession && mTailBufferCount == 0) {
2210 doProcess = false;
2211 }
2212
2213 if (activeTrackCnt() == 0) {
2214 // if no track is active and the effect tail has not been rendered,
2215 // the input buffer must be cleared here as the mixer process will not do it
2216 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002217 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002218 if (mTailBufferCount > 0) {
2219 mTailBufferCount--;
2220 }
2221 }
2222 }
2223 }
2224
2225 size_t size = mEffects.size();
2226 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002227 // Only the input and output buffers of the chain can be external,
2228 // and 'update' / 'commit' do nothing for allocated buffers, thus
2229 // it's not needed to consider any other buffers here.
2230 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002231 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2232 mOutBuffer->update();
2233 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002234 for (size_t i = 0; i < size; i++) {
2235 mEffects[i]->process();
2236 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002237 mInBuffer->commit();
2238 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2239 mOutBuffer->commit();
2240 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002241 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002242 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002243 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002244 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2245 }
2246 if (doResetVolume) {
2247 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 }
2249}
2250
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002251// createEffect_l() must be called with ThreadBase::mLock held
2252status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002253 effect_descriptor_t *desc,
2254 int id,
2255 audio_session_t sessionId,
2256 bool pinned)
2257{
2258 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002259 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002260 status_t lStatus = effect->status();
2261 if (lStatus == NO_ERROR) {
2262 lStatus = addEffect_ll(effect);
2263 }
2264 if (lStatus != NO_ERROR) {
2265 effect.clear();
2266 }
2267 return lStatus;
2268}
2269
2270// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002271status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2272{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002273 Mutex::Autolock _l(mLock);
2274 return addEffect_ll(effect);
2275}
2276// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2277status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2278{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002279 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002280
Eric Laurentb62d0362021-10-26 17:40:18 +02002281 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2283 // Auxiliary effects are inserted at the beginning of mEffects vector as
2284 // they are processed first and accumulated in chain input buffer
2285 mEffects.insertAt(effect, 0);
2286
2287 // the input buffer for auxiliary effect contains mono samples in
2288 // 32 bit format. This is to avoid saturation in AudoMixer
2289 // accumulation stage. Saturation is done in EffectModule::process() before
2290 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002291 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002292 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002293#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002294 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002295 numSamples * sizeof(float), &halBuffer);
2296#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002297 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002298 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002299#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002300 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002301
2302 effect->configure();
2303
Mikhail Naganov022b9952017-01-04 16:36:51 -08002304 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 // auxiliary effects output samples to chain input buffer for further processing
2306 // by insert effects
2307 effect->setOutBuffer(mInBuffer);
2308 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002309 ssize_t idx_insert = getInsertIndex(desc);
2310 if (idx_insert < 0) {
2311 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 }
2313
Eric Laurentb62d0362021-10-26 17:40:18 +02002314 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002315 mEffects.insertAt(effect, idx_insert);
2316
2317 effect->configure();
2318
Eric Laurentb62d0362021-10-26 17:40:18 +02002319 // - By default:
2320 // All effects read samples from chain input buffer.
2321 // The last effect in the chain, writes samples to chain output buffer,
2322 // otherwise to chain input buffer
2323 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2324 // The spatializer effect (first effect) reads samples from the input buffer
2325 // and writes samples to the output buffer.
2326 // All other effects read and writes samples to the output buffer
2327 if (mEffectCallback->isSpatializer()
2328 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002330 if (idx_insert == 0) {
2331 if (previousSize != 0) {
2332 mEffects[1]->configure();
2333 mEffects[1]->setInBuffer(mOutBuffer);
2334 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2335 }
2336 effect->setInBuffer(mInBuffer);
2337 } else {
2338 effect->setInBuffer(mOutBuffer);
2339 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002340 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002341 effect->setInBuffer(mInBuffer);
2342 if (idx_insert == previousSize) {
2343 if (idx_insert != 0) {
2344 mEffects[idx_insert-1]->configure();
2345 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2346 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2347 }
2348 effect->setOutBuffer(mOutBuffer);
2349 } else {
2350 effect->setOutBuffer(mInBuffer);
2351 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002353 ALOGV("%s effect %p, added in chain %p at rank %zu",
2354 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002355 }
2356 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002357
Eric Laurentca7cc822012-11-19 14:55:58 -08002358 return NO_ERROR;
2359}
2360
Eric Laurentb62d0362021-10-26 17:40:18 +02002361ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2362 // Insert effects are inserted at the end of mEffects vector as they are processed
2363 // after track and auxiliary effects.
2364 // Insert effect order as a function of indicated preference:
2365 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2366 // another effect is present
2367 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2368 // last effect claiming first position
2369 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2370 // first effect claiming last position
2371 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2372 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2373 // already present
2374 // Spatializer or Downmixer effects are inserted in first position because
2375 // they adapt the channel count for all other effects in the chain
2376 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2377 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2378 return 0;
2379 }
2380
2381 size_t size = mEffects.size();
2382 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2383 ssize_t idx_insert;
2384 ssize_t idx_insert_first = -1;
2385 ssize_t idx_insert_last = -1;
2386
2387 idx_insert = size;
2388 for (size_t i = 0; i < size; i++) {
2389 effect_descriptor_t d = mEffects[i]->desc();
2390 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2391 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2392 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2393 // check invalid effect chaining combinations
2394 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2395 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2396 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2397 __func__, desc.name, d.name);
2398 return -1;
2399 }
2400 // remember position of first insert effect and by default
2401 // select this as insert position for new effect
2402 if (idx_insert == size) {
2403 idx_insert = i;
2404 }
2405 // remember position of last insert effect claiming
2406 // first position
2407 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2408 idx_insert_first = i;
2409 }
2410 // remember position of first insert effect claiming
2411 // last position
2412 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2413 idx_insert_last == -1) {
2414 idx_insert_last = i;
2415 }
2416 }
2417 }
2418
2419 // modify idx_insert from first position if needed
2420 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2421 if (idx_insert_last != -1) {
2422 idx_insert = idx_insert_last;
2423 } else {
2424 idx_insert = size;
2425 }
2426 } else {
2427 if (idx_insert_first != -1) {
2428 idx_insert = idx_insert_first + 1;
2429 }
2430 }
2431 return idx_insert;
2432}
2433
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002434// removeEffect_l() must be called with ThreadBase::mLock held
2435size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2436 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002437{
2438 Mutex::Autolock _l(mLock);
2439 size_t size = mEffects.size();
2440 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2441
2442 for (size_t i = 0; i < size; i++) {
2443 if (effect == mEffects[i]) {
2444 // calling stop here will remove pre-processing effect from the audio HAL.
2445 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2446 // the middle of a read from audio HAL
2447 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2448 mEffects[i]->state() == EffectModule::STOPPING) {
2449 mEffects[i]->stop();
2450 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002451 if (release) {
2452 mEffects[i]->release_l();
2453 }
2454
Mikhail Naganov022b9952017-01-04 16:36:51 -08002455 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002456 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002457 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002458 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002459 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 }
2461 }
2462 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002463
2464 // make sure the input buffer configuration for the new first effect in the chain
2465 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2466 if (i == 0 && size > 1) {
2467 mEffects[0]->configure();
2468 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002469 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002470 }
2471
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002472 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002473 this, i);
2474 break;
2475 }
2476 }
2477
2478 return mEffects.size();
2479}
2480
jiabin8f278ee2019-11-11 12:16:27 -08002481// setDevices_l() must be called with ThreadBase::mLock held
2482void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002483{
2484 size_t size = mEffects.size();
2485 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002486 mEffects[i]->setDevices(devices);
2487 }
2488}
2489
2490// setInputDevice_l() must be called with ThreadBase::mLock held
2491void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2492{
2493 size_t size = mEffects.size();
2494 for (size_t i = 0; i < size; i++) {
2495 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002496 }
2497}
2498
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002499// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002500void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2501{
2502 size_t size = mEffects.size();
2503 for (size_t i = 0; i < size; i++) {
2504 mEffects[i]->setMode(mode);
2505 }
2506}
2507
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002508// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002509void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2510{
2511 size_t size = mEffects.size();
2512 for (size_t i = 0; i < size; i++) {
2513 mEffects[i]->setAudioSource(source);
2514 }
2515}
2516
Zhou Songd505c642020-02-20 16:35:37 +08002517bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2518 for (const auto &effect : mEffects) {
2519 if (effect->isVolumeControlEnabled()) return true;
2520 }
2521 return false;
2522}
2523
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002524// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002525bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002526{
2527 uint32_t newLeft = *left;
2528 uint32_t newRight = *right;
2529 bool hasControl = false;
2530 int ctrlIdx = -1;
2531 size_t size = mEffects.size();
2532
2533 // first update volume controller
2534 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002535 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002536 ctrlIdx = i - 1;
2537 hasControl = true;
2538 break;
2539 }
2540 }
2541
Eric Laurentfa1e1232016-08-02 19:01:49 -07002542 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002543 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002544 if (hasControl) {
2545 *left = mNewLeftVolume;
2546 *right = mNewRightVolume;
2547 }
2548 return hasControl;
2549 }
2550
2551 mVolumeCtrlIdx = ctrlIdx;
2552 mLeftVolume = newLeft;
2553 mRightVolume = newRight;
2554
2555 // second get volume update from volume controller
2556 if (ctrlIdx >= 0) {
2557 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2558 mNewLeftVolume = newLeft;
2559 mNewRightVolume = newRight;
2560 }
2561 // then indicate volume to all other effects in chain.
2562 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002563 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002564 uint32_t lVol = newLeft;
2565 uint32_t rVol = newRight;
2566
2567 for (size_t i = 0; i < size; i++) {
2568 if ((int)i == ctrlIdx) {
2569 continue;
2570 }
2571 // this also works for ctrlIdx == -1 when there is no volume controller
2572 if ((int)i > ctrlIdx) {
2573 lVol = *left;
2574 rVol = *right;
2575 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002576 // Pass requested volume directly if this is volume monitor module
2577 if (mEffects[i]->isVolumeMonitor()) {
2578 mEffects[i]->setVolume(left, right, false);
2579 } else {
2580 mEffects[i]->setVolume(&lVol, &rVol, false);
2581 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002582 }
2583 *left = newLeft;
2584 *right = newRight;
2585
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002586 setVolumeForOutput_l(*left, *right);
2587
Eric Laurentca7cc822012-11-19 14:55:58 -08002588 return hasControl;
2589}
2590
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002591// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002592void AudioFlinger::EffectChain::resetVolume_l()
2593{
Eric Laurente7449bf2016-08-03 18:44:07 -07002594 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2595 uint32_t left = mLeftVolume;
2596 uint32_t right = mRightVolume;
2597 (void)setVolume_l(&left, &right, true);
2598 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002599}
2600
jiabineb3bda02020-06-30 14:07:03 -07002601// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2602bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2603{
2604 for (size_t i = 0; i < mEffects.size(); ++i) {
2605 if (mEffects[i]->isHapticGenerator()) {
2606 return true;
2607 }
2608 }
2609 return false;
2610}
2611
jiabine70bc7f2020-06-30 22:07:55 -07002612void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2613{
2614 Mutex::Autolock _l(mLock);
2615 for (size_t i = 0; i < mEffects.size(); ++i) {
2616 mEffects[i]->setHapticIntensity(id, intensity);
2617 }
2618}
2619
Eric Laurent1b928682014-10-02 19:41:47 -07002620void AudioFlinger::EffectChain::syncHalEffectsState()
2621{
2622 Mutex::Autolock _l(mLock);
2623 for (size_t i = 0; i < mEffects.size(); i++) {
2624 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2625 mEffects[i]->state() == EffectModule::STOPPING) {
2626 mEffects[i]->addEffectToHal_l();
2627 }
2628 }
2629}
2630
Eric Laurentca7cc822012-11-19 14:55:58 -08002631void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2632{
Eric Laurentca7cc822012-11-19 14:55:58 -08002633 String8 result;
2634
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002635 const size_t numEffects = mEffects.size();
2636 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002637
Marco Nelissenb2208842014-02-07 14:00:50 -08002638 if (numEffects) {
2639 bool locked = AudioFlinger::dumpTryLock(mLock);
2640 // failed to lock - AudioFlinger is probably deadlocked
2641 if (!locked) {
2642 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002643 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002644
Andy Hungbded9c82017-11-30 18:47:35 -08002645 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2646 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2647 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2648 (int)inBufferStr.size(), "In buffer ",
2649 (int)outBufferStr.size(), "Out buffer ");
2650 result.appendFormat("\t%s %s %d\n",
2651 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002652 write(fd, result.string(), result.size());
2653
2654 for (size_t i = 0; i < numEffects; ++i) {
2655 sp<EffectModule> effect = mEffects[i];
2656 if (effect != 0) {
2657 effect->dump(fd, args);
2658 }
2659 }
2660
2661 if (locked) {
2662 mLock.unlock();
2663 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002664 } else {
2665 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002666 }
2667}
2668
2669// must be called with ThreadBase::mLock held
2670void AudioFlinger::EffectChain::setEffectSuspended_l(
2671 const effect_uuid_t *type, bool suspend)
2672{
2673 sp<SuspendedEffectDesc> desc;
2674 // use effect type UUID timelow as key as there is no real risk of identical
2675 // timeLow fields among effect type UUIDs.
2676 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2677 if (suspend) {
2678 if (index >= 0) {
2679 desc = mSuspendedEffects.valueAt(index);
2680 } else {
2681 desc = new SuspendedEffectDesc();
2682 desc->mType = *type;
2683 mSuspendedEffects.add(type->timeLow, desc);
2684 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2685 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002686
Eric Laurentca7cc822012-11-19 14:55:58 -08002687 if (desc->mRefCount++ == 0) {
2688 sp<EffectModule> effect = getEffectIfEnabled(type);
2689 if (effect != 0) {
2690 desc->mEffect = effect;
2691 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002692 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002693 }
2694 }
2695 } else {
2696 if (index < 0) {
2697 return;
2698 }
2699 desc = mSuspendedEffects.valueAt(index);
2700 if (desc->mRefCount <= 0) {
2701 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002702 desc->mRefCount = 0;
2703 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002704 }
2705 if (--desc->mRefCount == 0) {
2706 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2707 if (desc->mEffect != 0) {
2708 sp<EffectModule> effect = desc->mEffect.promote();
2709 if (effect != 0) {
2710 effect->setSuspended(false);
2711 effect->lock();
2712 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002713 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002714 effect->setEnabled_l(handle->enabled());
2715 }
2716 effect->unlock();
2717 }
2718 desc->mEffect.clear();
2719 }
2720 mSuspendedEffects.removeItemsAt(index);
2721 }
2722 }
2723}
2724
2725// must be called with ThreadBase::mLock held
2726void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2727{
2728 sp<SuspendedEffectDesc> desc;
2729
2730 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2731 if (suspend) {
2732 if (index >= 0) {
2733 desc = mSuspendedEffects.valueAt(index);
2734 } else {
2735 desc = new SuspendedEffectDesc();
2736 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2737 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2738 }
2739 if (desc->mRefCount++ == 0) {
2740 Vector< sp<EffectModule> > effects;
2741 getSuspendEligibleEffects(effects);
2742 for (size_t i = 0; i < effects.size(); i++) {
2743 setEffectSuspended_l(&effects[i]->desc().type, true);
2744 }
2745 }
2746 } else {
2747 if (index < 0) {
2748 return;
2749 }
2750 desc = mSuspendedEffects.valueAt(index);
2751 if (desc->mRefCount <= 0) {
2752 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2753 desc->mRefCount = 1;
2754 }
2755 if (--desc->mRefCount == 0) {
2756 Vector<const effect_uuid_t *> types;
2757 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2758 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2759 continue;
2760 }
2761 types.add(&mSuspendedEffects.valueAt(i)->mType);
2762 }
2763 for (size_t i = 0; i < types.size(); i++) {
2764 setEffectSuspended_l(types[i], false);
2765 }
2766 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2767 mSuspendedEffects.keyAt(index));
2768 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2769 }
2770 }
2771}
2772
2773
2774// The volume effect is used for automated tests only
2775#ifndef OPENSL_ES_H_
2776static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2777 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2778const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2779#endif //OPENSL_ES_H_
2780
Eric Laurentd8365c52017-07-16 15:27:05 -07002781/* static */
2782bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2783{
2784 // Only NS and AEC are suspended when BtNRec is off
2785 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2786 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2787 return true;
2788 }
2789 return false;
2790}
2791
Eric Laurentca7cc822012-11-19 14:55:58 -08002792bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2793{
2794 // auxiliary effects and visualizer are never suspended on output mix
2795 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2796 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2797 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002798 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2799 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002800 return false;
2801 }
2802 return true;
2803}
2804
2805void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2806 Vector< sp<AudioFlinger::EffectModule> > &effects)
2807{
2808 effects.clear();
2809 for (size_t i = 0; i < mEffects.size(); i++) {
2810 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2811 effects.add(mEffects[i]);
2812 }
2813 }
2814}
2815
2816sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2817 const effect_uuid_t *type)
2818{
2819 sp<EffectModule> effect = getEffectFromType_l(type);
2820 return effect != 0 && effect->isEnabled() ? effect : 0;
2821}
2822
2823void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2824 bool enabled)
2825{
2826 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2827 if (enabled) {
2828 if (index < 0) {
2829 // if the effect is not suspend check if all effects are suspended
2830 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2831 if (index < 0) {
2832 return;
2833 }
2834 if (!isEffectEligibleForSuspend(effect->desc())) {
2835 return;
2836 }
2837 setEffectSuspended_l(&effect->desc().type, enabled);
2838 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2839 if (index < 0) {
2840 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2841 return;
2842 }
2843 }
2844 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2845 effect->desc().type.timeLow);
2846 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002847 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002848 if (desc->mEffect == 0) {
2849 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002850 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002851 effect->setSuspended(true);
2852 }
2853 } else {
2854 if (index < 0) {
2855 return;
2856 }
2857 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2858 effect->desc().type.timeLow);
2859 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2860 desc->mEffect.clear();
2861 effect->setSuspended(false);
2862 }
2863}
2864
Eric Laurent5baf2af2013-09-12 17:37:00 -07002865bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002866{
2867 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002868 return isNonOffloadableEnabled_l();
2869}
2870
2871bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2872{
Eric Laurent813e2a72013-08-31 12:59:48 -07002873 size_t size = mEffects.size();
2874 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002875 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002876 return true;
2877 }
2878 }
2879 return false;
2880}
2881
Eric Laurentaaa44472014-09-12 17:41:50 -07002882void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2883{
2884 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002885 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002886}
2887
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002888void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2889{
2890 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2891 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2892 }
2893 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2894 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2895 }
2896}
2897
2898void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2899{
2900 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2901 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2902 }
2903 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2904 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2905 }
2906}
2907
2908bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002909{
2910 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002911 for (const auto &effect : mEffects) {
2912 if (effect->isProcessImplemented()) {
2913 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002914 }
2915 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002916 // Allow effects without processing.
2917 return true;
2918}
2919
2920bool AudioFlinger::EffectChain::isFastCompatible() const
2921{
2922 Mutex::Autolock _l(mLock);
2923 for (const auto &effect : mEffects) {
2924 if (effect->isProcessImplemented()
2925 && effect->isImplementationSoftware()) {
2926 return false;
2927 }
2928 }
2929 // Allow effects without processing or hw accelerated effects.
2930 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002931}
2932
2933// isCompatibleWithThread_l() must be called with thread->mLock held
2934bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2935{
2936 Mutex::Autolock _l(mLock);
2937 for (size_t i = 0; i < mEffects.size(); i++) {
2938 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2939 return false;
2940 }
2941 }
2942 return true;
2943}
2944
Eric Laurent6b446ce2019-12-13 10:56:31 -08002945// EffectCallbackInterface implementation
2946status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2947 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2948 sp<EffectHalInterface> *effect) {
2949 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002950 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002951 if (effectsFactory != 0) {
2952 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2953 }
2954 return status;
2955}
2956
2957bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002958 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002959 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002960 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002961}
2962
2963status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2964 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002965 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002966}
2967
2968status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2969 sp<EffectHalInterface> effect) {
2970 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002971 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002972 if (t == nullptr) {
2973 return result;
2974 }
2975 sp <StreamHalInterface> st = t->stream();
2976 if (st == nullptr) {
2977 return result;
2978 }
2979 result = st->addEffect(effect);
2980 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2981 return result;
2982}
2983
2984status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2985 sp<EffectHalInterface> effect) {
2986 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002987 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002988 if (t == nullptr) {
2989 return result;
2990 }
2991 sp <StreamHalInterface> st = t->stream();
2992 if (st == nullptr) {
2993 return result;
2994 }
2995 result = st->removeEffect(effect);
2996 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2997 return result;
2998}
2999
3000audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003001 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003002 if (t == nullptr) {
3003 return AUDIO_IO_HANDLE_NONE;
3004 }
3005 return t->id();
3006}
3007
3008bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003009 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003010 if (t == nullptr) {
3011 return true;
3012 }
3013 return t->isOutput();
3014}
3015
3016bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003017 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018}
3019
3020bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003021 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003022}
3023
3024bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003025 switch (mThreadType) {
3026 case ThreadBase::OFFLOAD:
3027 case ThreadBase::MMAP_PLAYBACK:
3028 case ThreadBase::MMAP_CAPTURE:
3029 return true;
3030 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003031 return false;
3032 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003033}
3034
3035bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3036 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003037}
3038
3039uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003040 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003041 if (t == nullptr) {
3042 return 0;
3043 }
3044 return t->sampleRate();
3045}
3046
Eric Laurentf1f22e72021-07-13 14:04:14 +02003047audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3048 sp<ThreadBase> t = thread().promote();
3049 if (t == nullptr) {
3050 return AUDIO_CHANNEL_NONE;
3051 }
3052 sp<EffectChain> c = chain().promote();
3053 if (c == nullptr) {
3054 return AUDIO_CHANNEL_NONE;
3055 }
3056
Eric Laurentb62d0362021-10-26 17:40:18 +02003057 if (mThreadType == ThreadBase::SPATIALIZER) {
3058 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3059 if (c->isFirstEffect(id)) {
3060 return t->mixerChannelMask();
3061 } else {
3062 return t->channelMask();
3063 }
3064 } else if (!audio_is_global_session(c->sessionId())) {
3065 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3066 return t->mixerChannelMask();
3067 } else {
3068 return t->channelMask();
3069 }
3070 } else {
3071 return t->channelMask();
3072 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003073 } else {
3074 return t->channelMask();
3075 }
3076}
3077
3078uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003079 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003080}
3081
3082audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003083 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003084 if (t == nullptr) {
3085 return AUDIO_CHANNEL_NONE;
3086 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003087 sp<EffectChain> c = chain().promote();
3088 if (c == nullptr) {
3089 return AUDIO_CHANNEL_NONE;
3090 }
3091
3092 if (mThreadType == ThreadBase::SPATIALIZER) {
3093 if (!audio_is_global_session(c->sessionId())) {
3094 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3095 return t->mixerChannelMask();
3096 } else {
3097 return t->channelMask();
3098 }
3099 } else {
3100 return t->channelMask();
3101 }
3102 } else {
3103 return t->channelMask();
3104 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003105}
3106
Eric Laurentf1f22e72021-07-13 14:04:14 +02003107uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003108 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003109}
3110
jiabineb3bda02020-06-30 14:07:03 -07003111audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003112 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003113 if (t == nullptr) {
3114 return AUDIO_CHANNEL_NONE;
3115 }
3116 return t->hapticChannelMask();
3117}
3118
Eric Laurent6b446ce2019-12-13 10:56:31 -08003119size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003120 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003121 if (t == nullptr) {
3122 return 0;
3123 }
3124 return t->frameCount();
3125}
3126
3127uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003128 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003129 if (t == nullptr) {
3130 return 0;
3131 }
3132 return t->latency_l();
3133}
3134
3135void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003136 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003137 if (t == nullptr) {
3138 return;
3139 }
3140 t->setVolumeForOutput_l(left, right);
3141}
3142
3143void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003144 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003145 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003146 if (t == nullptr) {
3147 return;
3148 }
3149 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3150
Andy Hung328d6772021-01-12 12:32:21 -08003151 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003152 if (c == nullptr) {
3153 return;
3154 }
Eric Laurent41709552019-12-16 19:34:05 -08003155 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3156 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003157}
3158
Eric Laurent41709552019-12-16 19:34:05 -08003159void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003160 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003161 if (t == nullptr) {
3162 return;
3163 }
Eric Laurent41709552019-12-16 19:34:05 -08003164 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3165 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003166}
3167
Eric Laurent41709552019-12-16 19:34:05 -08003168void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003169 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3170
Andy Hung328d6772021-01-12 12:32:21 -08003171 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003172 if (t == nullptr) {
3173 return;
3174 }
3175 t->onEffectDisable();
3176}
3177
3178bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3179 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003180 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003181 if (t == nullptr) {
3182 return false;
3183 }
3184 t->disconnectEffectHandle(handle, unpinIfLast);
3185 return true;
3186}
3187
3188void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003189 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003190 if (c == nullptr) {
3191 return;
3192 }
3193 c->resetVolume_l();
3194
3195}
3196
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003197product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003198 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003199 if (c == nullptr) {
3200 return PRODUCT_STRATEGY_NONE;
3201 }
3202 return c->strategy();
3203}
3204
3205int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003206 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003207 if (c == nullptr) {
3208 return 0;
3209 }
3210 return c->activeTrackCnt();
3211}
3212
Eric Laurentb82e6b72019-11-22 17:25:04 -08003213
3214#undef LOG_TAG
3215#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3216
3217status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3218{
3219 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3220 Mutex::Autolock _l(mProxyLock);
3221 if (status == NO_ERROR) {
3222 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003223 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003224 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003225 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003226 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003227 bs = handle.second->disable(&status);
3228 }
3229 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003230 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003231 }
3232 }
3233 }
3234 ALOGV("%s enable %d status %d", __func__, enabled, status);
3235 return status;
3236}
3237
3238status_t AudioFlinger::DeviceEffectProxy::init(
3239 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3240//For all audio patches
3241//If src or sink device match
3242//If the effect is HW accelerated
3243// if no corresponding effect module
3244// Create EffectModule: mHalEffect
3245//Create and attach EffectHandle
3246//If the effect is not HW accelerated and the patch sink or src is a mixer port
3247// Create Effect on patch input or output thread on session -1
3248//Add EffectHandle to EffectHandle map of Effect Proxy:
3249 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3250 status_t status = NO_ERROR;
3251 for (auto &patch : patches) {
3252 status = onCreatePatch(patch.first, patch.second);
3253 ALOGV("%s onCreatePatch status %d", __func__, status);
3254 if (status == BAD_VALUE) {
3255 return status;
3256 }
3257 }
3258 return status;
3259}
3260
3261status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3262 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3263 status_t status = NAME_NOT_FOUND;
3264 sp<EffectHandle> handle;
3265 // only consider source[0] as this is the only "true" source of a patch
3266 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3267 ALOGV("%s source checkPort status %d", __func__, status);
3268 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3269 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3270 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3271 }
3272 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3273 Mutex::Autolock _l(mProxyLock);
3274 mEffectHandles.emplace(patchHandle, handle);
3275 }
3276 ALOGW_IF(status == BAD_VALUE,
3277 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3278
3279 return status;
3280}
3281
3282status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3283 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3284
3285 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3286 __func__, port->type, port->ext.device.type,
3287 port->ext.device.address, port->id, patch.isSoftware());
3288 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003289 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003290 return NAME_NOT_FOUND;
3291 }
3292 status_t status = NAME_NOT_FOUND;
3293
3294 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3295 Mutex::Autolock _l(mProxyLock);
3296 mDevicePort = *port;
3297 mHalEffect = new EffectModule(mMyCallback,
3298 const_cast<effect_descriptor_t *>(&mDescriptor),
3299 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3300 false /* pinned */, port->id);
3301 if (audio_is_input_device(mDevice.mType)) {
3302 mHalEffect->setInputDevice(mDevice);
3303 } else {
3304 mHalEffect->setDevices({mDevice});
3305 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003306 mHalEffect->configure();
3307
Eric Laurentde8caf42021-08-11 17:19:25 +02003308 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3309 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003310 status = (*handle)->initCheck();
3311 if (status == OK) {
3312 status = mHalEffect->addHandle((*handle).get());
3313 } else {
3314 mHalEffect.clear();
3315 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3316 }
3317 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3318 sp <ThreadBase> thread;
3319 if (audio_port_config_has_input_direction(port)) {
3320 if (patch.isSoftware()) {
3321 thread = patch.mRecord.thread();
3322 } else {
3323 thread = patch.thread().promote();
3324 }
3325 } else {
3326 if (patch.isSoftware()) {
3327 thread = patch.mPlayback.thread();
3328 } else {
3329 thread = patch.thread().promote();
3330 }
3331 }
3332 int enabled;
3333 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3334 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003335 &enabled, &status, false, false /*probe*/,
3336 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003337 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3338 } else {
3339 status = BAD_VALUE;
3340 }
3341 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003342 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003343 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003344 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003345 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003346 bs = (*handle)->disable(&status);
3347 }
3348 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003349 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003350 }
3351 }
3352 return status;
3353}
3354
3355void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003356 sp<EffectHandle> effect;
3357 {
3358 Mutex::Autolock _l(mProxyLock);
3359 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3360 effect = mEffectHandles.at(patchHandle);
3361 mEffectHandles.erase(patchHandle);
3362 }
3363 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003364}
3365
3366
3367size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3368{
3369 Mutex::Autolock _l(mProxyLock);
3370 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003371 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003372 mHalEffect.clear();
3373 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3374 }
3375 return mHalEffect == nullptr ? 0 : 1;
3376}
3377
3378status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3379 sp<EffectHalInterface> effect) {
3380 if (mHalEffect == nullptr) {
3381 return NO_INIT;
3382 }
3383 return mManagerCallback->addEffectToHal(
3384 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3385}
3386
3387status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3388 sp<EffectHalInterface> effect) {
3389 if (mHalEffect == nullptr) {
3390 return NO_INIT;
3391 }
3392 return mManagerCallback->removeEffectFromHal(
3393 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3394}
3395
3396bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3397 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3398 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3399 }
3400 return true;
3401}
3402
3403uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3404 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3405 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3406 return mDevicePort.sample_rate;
3407 }
3408 return DEFAULT_OUTPUT_SAMPLE_RATE;
3409}
3410
3411audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3412 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3413 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3414 return mDevicePort.channel_mask;
3415 }
3416 return AUDIO_CHANNEL_OUT_STEREO;
3417}
3418
3419uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3420 if (isOutput()) {
3421 return audio_channel_count_from_out_mask(channelMask());
3422 }
3423 return audio_channel_count_from_in_mask(channelMask());
3424}
3425
3426void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3427 const Vector<String16> args;
3428 EffectBase::dump(fd, args);
3429
3430 const bool locked = dumpTryLock(mProxyLock);
3431 if (!locked) {
3432 String8 result("DeviceEffectProxy may be deadlocked\n");
3433 write(fd, result.string(), result.size());
3434 }
3435
3436 String8 outStr;
3437 if (mHalEffect != nullptr) {
3438 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3439 } else {
3440 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3441 }
3442 write(fd, outStr.string(), outStr.size());
3443 outStr.clear();
3444
3445 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3446 write(fd, outStr.string(), outStr.size());
3447 outStr.clear();
3448
3449 for (const auto& iter : mEffectHandles) {
3450 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3451 write(fd, outStr.string(), outStr.size());
3452 outStr.clear();
3453 sp<EffectBase> effect = iter.second->effect().promote();
3454 if (effect != nullptr) {
3455 effect->dump(fd, args);
3456 }
3457 }
3458
3459 if (locked) {
3460 mLock.unlock();
3461 }
3462}
3463
3464#undef LOG_TAG
3465#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3466
3467int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3468 return mManagerCallback->newEffectId();
3469}
3470
3471
3472bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3473 EffectHandle *handle, bool unpinIfLast) {
3474 sp<EffectBase> effectBase = handle->effect().promote();
3475 if (effectBase == nullptr) {
3476 return false;
3477 }
3478
3479 sp<EffectModule> effect = effectBase->asEffectModule();
3480 if (effect == nullptr) {
3481 return false;
3482 }
3483
3484 // restore suspended effects if the disconnected handle was enabled and the last one.
3485 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3486 if (remove) {
3487 sp<DeviceEffectProxy> proxy = mProxy.promote();
3488 if (proxy != nullptr) {
3489 proxy->removeEffect(effect);
3490 }
3491 if (handle->enabled()) {
3492 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3493 }
3494 }
3495 return true;
3496}
3497
3498status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3499 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3500 sp<EffectHalInterface> *effect) {
3501 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3502}
3503
3504status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3505 sp<EffectHalInterface> effect) {
3506 sp<DeviceEffectProxy> proxy = mProxy.promote();
3507 if (proxy == nullptr) {
3508 return NO_INIT;
3509 }
3510 return proxy->addEffectToHal(effect);
3511}
3512
3513status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3514 sp<EffectHalInterface> effect) {
3515 sp<DeviceEffectProxy> proxy = mProxy.promote();
3516 if (proxy == nullptr) {
3517 return NO_INIT;
3518 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003519 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003520}
3521
3522bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3523 sp<DeviceEffectProxy> proxy = mProxy.promote();
3524 if (proxy == nullptr) {
3525 return true;
3526 }
3527 return proxy->isOutput();
3528}
3529
3530uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3531 sp<DeviceEffectProxy> proxy = mProxy.promote();
3532 if (proxy == nullptr) {
3533 return DEFAULT_OUTPUT_SAMPLE_RATE;
3534 }
3535 return proxy->sampleRate();
3536}
3537
Eric Laurentf1f22e72021-07-13 14:04:14 +02003538audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3539 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003540 sp<DeviceEffectProxy> proxy = mProxy.promote();
3541 if (proxy == nullptr) {
3542 return AUDIO_CHANNEL_OUT_STEREO;
3543 }
3544 return proxy->channelMask();
3545}
3546
Eric Laurentf1f22e72021-07-13 14:04:14 +02003547uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3548 sp<DeviceEffectProxy> proxy = mProxy.promote();
3549 if (proxy == nullptr) {
3550 return 2;
3551 }
3552 return proxy->channelCount();
3553}
3554
3555audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3556 sp<DeviceEffectProxy> proxy = mProxy.promote();
3557 if (proxy == nullptr) {
3558 return AUDIO_CHANNEL_OUT_STEREO;
3559 }
3560 return proxy->channelMask();
3561}
3562
3563uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003564 sp<DeviceEffectProxy> proxy = mProxy.promote();
3565 if (proxy == nullptr) {
3566 return 2;
3567 }
3568 return proxy->channelCount();
3569}
3570
Eric Laurent76c89f32021-12-03 17:13:23 +01003571void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3572 const sp<EffectBase>& effectBase) {
3573 sp<EffectModule> effect = effectBase->asEffectModule();
3574 if (effect == nullptr) {
3575 return;
3576 }
3577 effect->start();
3578}
3579
3580void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3581 const sp<EffectBase>& effectBase) {
3582 sp<EffectModule> effect = effectBase->asEffectModule();
3583 if (effect == nullptr) {
3584 return;
3585 }
3586 effect->stop();
3587}
3588
Glenn Kasten63238ef2015-03-02 15:50:29 -08003589} // namespace android