blob: 2e9ecb169ce5be8aa9a65f99d2d99843251fd802 [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 Laurent0dccd2e2021-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 Laurent0dccd2e2021-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 Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46// ----------------------------------------------------------------------------
47
48// Note: the following macro is used for extremely verbose logging message. In
49// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
50// 0; but one side effect of this is to turn all LOGV's as well. Some messages
51// are so verbose that we want to suppress them even when we have ALOG_ASSERT
52// turned on. Do not uncomment the #def below unless you really know what you
53// are doing and want to see all of the extremely verbose messages.
54//#define VERY_VERY_VERBOSE_LOGGING
55#ifdef VERY_VERY_VERBOSE_LOGGING
56#define ALOGVV ALOGV
57#else
58#define ALOGVV(a...) do { } while(0)
59#endif
60
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090061#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
62
Eric Laurentca7cc822012-11-19 14:55:58 -080063namespace android {
64
Andy Hung1131b6e2020-12-08 20:47:45 -080065using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070066using binder::Status;
67
68namespace {
69
70// Append a POD value into a vector of bytes.
71template<typename T>
72void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
73 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
74 buffer->insert(buffer->end(), ar, ar + sizeof(T));
75}
76
77// Write a POD value into a vector of bytes (clears the previous buffer
78// content).
79template<typename T>
80void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
81 buffer->clear();
82 appendToBuffer(value, buffer);
83}
84
85} // namespace
86
Eric Laurentca7cc822012-11-19 14:55:58 -080087// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080088// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080089// ----------------------------------------------------------------------------
90
91#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080092#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080093
Eric Laurent41709552019-12-16 19:34:05 -080094AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080095 effect_descriptor_t *desc,
96 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080097 audio_session_t sessionId,
98 bool pinned)
99 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800100 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800101 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800102{
Eric Laurentca7cc822012-11-19 14:55:58 -0800103}
104
Eric Laurent41709552019-12-16 19:34:05 -0800105// must be called with EffectModule::mLock held
106status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800107{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800108
Eric Laurent41709552019-12-16 19:34:05 -0800109 ALOGV("setEnabled %p enabled %d", this, enabled);
110
111 if (enabled != isEnabled()) {
112 switch (mState) {
113 // going from disabled to enabled
114 case IDLE:
115 mState = STARTING;
116 break;
117 case STOPPED:
118 mState = RESTART;
119 break;
120 case STOPPING:
121 mState = ACTIVE;
122 break;
123
124 // going from enabled to disabled
125 case RESTART:
126 mState = STOPPED;
127 break;
128 case STARTING:
129 mState = IDLE;
130 break;
131 case ACTIVE:
132 mState = STOPPING;
133 break;
134 case DESTROYED:
135 return NO_ERROR; // simply ignore as we are being destroyed
136 }
137 for (size_t i = 1; i < mHandles.size(); i++) {
138 EffectHandle *h = mHandles[i];
139 if (h != NULL && !h->disconnected()) {
140 h->setEnabled(enabled);
141 }
142 }
143 }
144 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800145}
146
Eric Laurent41709552019-12-16 19:34:05 -0800147status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
148{
149 status_t status;
150 {
151 Mutex::Autolock _l(mLock);
152 status = setEnabled_l(enabled);
153 }
154 if (fromHandle) {
155 if (enabled) {
156 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700157 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800158 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700159 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800160 }
161 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700162 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800163 }
164 }
165 return status;
166}
167
168bool AudioFlinger::EffectBase::isEnabled() const
169{
170 switch (mState) {
171 case RESTART:
172 case STARTING:
173 case ACTIVE:
174 return true;
175 case IDLE:
176 case STOPPING:
177 case STOPPED:
178 case DESTROYED:
179 default:
180 return false;
181 }
182}
183
184void AudioFlinger::EffectBase::setSuspended(bool suspended)
185{
186 Mutex::Autolock _l(mLock);
187 mSuspended = suspended;
188}
189
190bool AudioFlinger::EffectBase::suspended() const
191{
192 Mutex::Autolock _l(mLock);
193 return mSuspended;
194}
195
196status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800197{
198 status_t status;
199
200 Mutex::Autolock _l(mLock);
201 int priority = handle->priority();
202 size_t size = mHandles.size();
203 EffectHandle *controlHandle = NULL;
204 size_t i;
205 for (i = 0; i < size; i++) {
206 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800207 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800208 continue;
209 }
210 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700213 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800214 if (h->priority() <= priority) {
215 break;
216 }
217 }
218 // if inserted in first place, move effect control from previous owner to this handle
219 if (i == 0) {
220 bool enabled = false;
221 if (controlHandle != NULL) {
222 enabled = controlHandle->enabled();
223 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
224 }
225 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
226 status = NO_ERROR;
227 } else {
228 status = ALREADY_EXISTS;
229 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700230 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800231 mHandles.insertAt(handle, i);
232 return status;
233}
234
Eric Laurent41709552019-12-16 19:34:05 -0800235status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700236{
237 status_t status = NO_ERROR;
238 bool doRegister = false;
239 bool registered = false;
240 bool doEnable = false;
241 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700242 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800243 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700244
245 {
246 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200247
248 if ((isInternal_l() && !mPolicyRegistered)
249 || !getCallback()->isAudioPolicyReady()) {
250 return NO_ERROR;
251 }
252
Eric Laurent6c796322019-04-09 14:13:17 -0700253 // register effect when first handle is attached and unregister when last handle is removed
254 if (mPolicyRegistered != mHandles.size() > 0) {
255 doRegister = true;
256 mPolicyRegistered = mHandles.size() > 0;
257 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700258 const auto callback = getCallback();
259 io = callback->io();
260 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700261 }
262 }
263 // enable effect when registered according to enable state requested by controlling handle
264 if (mHandles.size() > 0) {
265 EffectHandle *handle = controlHandle_l();
266 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
267 doEnable = true;
268 mPolicyEnabled = handle->enabled();
269 }
270 }
271 registered = mPolicyRegistered;
272 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100273 // The simultaneous release of two EffectHandles with the same EffectModule
274 // may cause us to call this method at the same time.
275 // This may deadlock under some circumstances (b/180941720). Avoid this.
276 if (!doRegister && !(registered && doEnable)) {
277 return NO_ERROR;
278 }
Eric Laurent6c796322019-04-09 14:13:17 -0700279 mPolicyLock.lock();
280 }
281 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
282 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
283 if (doRegister) {
284 if (registered) {
285 status = AudioSystem::registerEffect(
286 &mDescriptor,
287 io,
288 strategy,
289 mSessionId,
290 mId);
291 } else {
292 status = AudioSystem::unregisterEffect(mId);
293 }
294 }
295 if (registered && doEnable) {
296 status = AudioSystem::setEffectEnabled(mId, enabled);
297 }
298 mPolicyLock.unlock();
299
300 return status;
301}
302
303
Eric Laurent41709552019-12-16 19:34:05 -0800304ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800305{
306 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 return removeHandle_l(handle);
308}
309
Eric Laurent41709552019-12-16 19:34:05 -0800310ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800311{
Eric Laurentca7cc822012-11-19 14:55:58 -0800312 size_t size = mHandles.size();
313 size_t i;
314 for (i = 0; i < size; i++) {
315 if (mHandles[i] == handle) {
316 break;
317 }
318 }
319 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800320 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
321 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800322 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800323 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800324
325 mHandles.removeAt(i);
326 // if removed from first place, move effect control from this handle to next in line
327 if (i == 0) {
328 EffectHandle *h = controlHandle_l();
329 if (h != NULL) {
330 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
331 }
332 }
333
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530334 // Prevent calls to process() and other functions on effect interface from now on.
335 // The effect engine will be released by the destructor when the last strong reference on
336 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800337 if (mHandles.size() == 0 && !mPinned) {
338 mState = DESTROYED;
339 }
340
341 return mHandles.size();
342}
343
344// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800345AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800346{
347 // the first valid handle in the list has control over the module
348 for (size_t i = 0; i < mHandles.size(); i++) {
349 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800350 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800351 return h;
352 }
353 }
354
355 return NULL;
356}
357
Eric Laurentf10c7092016-12-06 17:09:56 -0800358// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800359ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800360{
Andy Hungfda44002021-06-03 17:23:16 -0700361 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800362 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700363 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800364 return mHandles.size();
365 }
366
Eric Laurentf10c7092016-12-06 17:09:56 -0800367 Mutex::Autolock _l(mLock);
368 ssize_t numHandles = removeHandle_l(handle);
369 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800370 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700371 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800372 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800373 }
374 return numHandles;
375}
376
Eric Laurent41709552019-12-16 19:34:05 -0800377bool AudioFlinger::EffectBase::purgeHandles()
378{
379 bool enabled = false;
380 Mutex::Autolock _l(mLock);
381 EffectHandle *handle = controlHandle_l();
382 if (handle != NULL) {
383 enabled = handle->enabled();
384 }
385 mHandles.clear();
386 return enabled;
387}
388
389void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700390 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800391}
392
393static String8 effectFlagsToString(uint32_t flags) {
394 String8 s;
395
396 s.append("conn. mode: ");
397 switch (flags & EFFECT_FLAG_TYPE_MASK) {
398 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
399 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
400 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
401 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
402 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
403 default: s.append("unknown/reserved"); break;
404 }
405 s.append(", ");
406
407 s.append("insert pref: ");
408 switch (flags & EFFECT_FLAG_INSERT_MASK) {
409 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
410 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
411 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
412 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
413 default: s.append("unknown/reserved"); break;
414 }
415 s.append(", ");
416
417 s.append("volume mgmt: ");
418 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
419 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
420 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
421 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
422 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
423 default: s.append("unknown/reserved"); break;
424 }
425 s.append(", ");
426
427 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
428 if (devind) {
429 s.append("device indication: ");
430 switch (devind) {
431 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
432 default: s.append("unknown/reserved"); break;
433 }
434 s.append(", ");
435 }
436
437 s.append("input mode: ");
438 switch (flags & EFFECT_FLAG_INPUT_MASK) {
439 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
440 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
441 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
442 default: s.append("not set"); break;
443 }
444 s.append(", ");
445
446 s.append("output mode: ");
447 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
448 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
449 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
450 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
451 default: s.append("not set"); break;
452 }
453 s.append(", ");
454
455 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
456 if (accel) {
457 s.append("hardware acceleration: ");
458 switch (accel) {
459 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
460 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
461 default: s.append("unknown/reserved"); break;
462 }
463 s.append(", ");
464 }
465
466 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
467 if (modeind) {
468 s.append("mode indication: ");
469 switch (modeind) {
470 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
471 default: s.append("unknown/reserved"); break;
472 }
473 s.append(", ");
474 }
475
476 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
477 if (srcind) {
478 s.append("source indication: ");
479 switch (srcind) {
480 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
481 default: s.append("unknown/reserved"); break;
482 }
483 s.append(", ");
484 }
485
486 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
487 s.append("offloadable, ");
488 }
489
490 int len = s.length();
491 if (s.length() > 2) {
492 (void) s.lockBuffer(len);
493 s.unlockBuffer(len - 2);
494 }
495 return s;
496}
497
498void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
499{
500 String8 result;
501
502 result.appendFormat("\tEffect ID %d:\n", mId);
503
504 bool locked = AudioFlinger::dumpTryLock(mLock);
505 // failed to lock - AudioFlinger is probably deadlocked
506 if (!locked) {
507 result.append("\t\tCould not lock Fx mutex:\n");
508 }
509
510 result.append("\t\tSession State Registered Enabled Suspended:\n");
511 result.appendFormat("\t\t%05d %03d %s %s %s\n",
512 mSessionId, mState, mPolicyRegistered ? "y" : "n",
513 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
514
515 result.append("\t\tDescriptor:\n");
516 char uuidStr[64];
517 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
518 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
519 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
520 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
521 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
522 mDescriptor.apiVersion,
523 mDescriptor.flags,
524 effectFlagsToString(mDescriptor.flags).string());
525 result.appendFormat("\t\t- name: %s\n",
526 mDescriptor.name);
527
528 result.appendFormat("\t\t- implementor: %s\n",
529 mDescriptor.implementor);
530
531 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
532 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
533 char buffer[256];
534 for (size_t i = 0; i < mHandles.size(); ++i) {
535 EffectHandle *handle = mHandles[i];
536 if (handle != NULL && !handle->disconnected()) {
537 handle->dumpToBuffer(buffer, sizeof(buffer));
538 result.append(buffer);
539 }
540 }
541 if (locked) {
542 mLock.unlock();
543 }
544
545 write(fd, result.string(), result.length());
546}
547
548// ----------------------------------------------------------------------------
549// EffectModule implementation
550// ----------------------------------------------------------------------------
551
552#undef LOG_TAG
553#define LOG_TAG "AudioFlinger::EffectModule"
554
555AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
556 effect_descriptor_t *desc,
557 int id,
558 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800559 bool pinned,
560 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800561 : EffectBase(callback, desc, id, sessionId, pinned),
562 // clear mConfig to ensure consistent initial value of buffer framecount
563 // in case buffers are associated by setInBuffer() or setOutBuffer()
564 // prior to configure().
565 mConfig{{}, {}},
566 mStatus(NO_INIT),
567 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
568 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800569 mOffloaded(false),
570 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800571#ifdef FLOAT_EFFECT_CHAIN
572 , mSupportsFloat(false)
573#endif
574{
575 ALOGV("Constructor %p pinned %d", this, pinned);
576 int lStatus;
577
578 // create effect engine from effect factory
579 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800580 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800581 if (mStatus != NO_ERROR) {
582 return;
583 }
584 lStatus = init();
585 if (lStatus < 0) {
586 mStatus = lStatus;
587 goto Error;
588 }
589
590 setOffloaded(callback->isOffload(), callback->io());
591 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
592
593 return;
594Error:
595 mEffectInterface.clear();
596 ALOGV("Constructor Error %d", mStatus);
597}
598
599AudioFlinger::EffectModule::~EffectModule()
600{
601 ALOGV("Destructor %p", this);
602 if (mEffectInterface != 0) {
603 char uuidStr[64];
604 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
605 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
606 this, uuidStr);
607 release_l();
608 }
609
610}
611
Eric Laurentfa1e1232016-08-02 19:01:49 -0700612bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800613 Mutex::Autolock _l(mLock);
614
Eric Laurentfa1e1232016-08-02 19:01:49 -0700615 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800616 switch (mState) {
617 case RESTART:
618 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700619 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800620
621 case STARTING:
622 // clear auxiliary effect input buffer for next accumulation
623 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
624 memset(mConfig.inputCfg.buffer.raw,
625 0,
626 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
627 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700628 if (start_l() == NO_ERROR) {
629 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700630 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 } else {
632 mState = IDLE;
633 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800634 break;
635 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900636 // volume control for offload and direct threads must take effect immediately.
637 if (stop_l() == NO_ERROR
638 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700639 mDisableWaitCnt = mMaxDisableWaitCnt;
640 } else {
641 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
642 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800643 mState = STOPPED;
644 break;
645 case STOPPED:
646 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
647 // turn off sequence.
648 if (--mDisableWaitCnt == 0) {
649 reset_l();
650 mState = IDLE;
651 }
652 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200653 case ACTIVE:
654 for (size_t i = 0; i < mHandles.size(); i++) {
655 if (!mHandles[i]->disconnected()) {
656 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
657 }
658 }
659 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800660 default: //IDLE , ACTIVE, DESTROYED
661 break;
662 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700663
664 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800665}
666
667void AudioFlinger::EffectModule::process()
668{
669 Mutex::Autolock _l(mLock);
670
Mikhail Naganov022b9952017-01-04 16:36:51 -0800671 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800672 return;
673 }
674
rago94a1ee82017-07-21 15:11:02 -0700675 const uint32_t inChannelCount =
676 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
677 const uint32_t outChannelCount =
678 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
679 const bool auxType =
680 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
681
Andy Hungfa69ca32017-11-30 10:07:53 -0800682 // safeInputOutputSampleCount is 0 if the channel count between input and output
683 // buffers do not match. This prevents automatic accumulation or copying between the
684 // input and output effect buffers without an intermediary effect process.
685 // TODO: consider implementing channel conversion.
686 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700687 mInChannelCountRequested != mOutChannelCountRequested ? 0
688 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800689 mConfig.inputCfg.buffer.frameCount,
690 mConfig.outputCfg.buffer.frameCount);
691 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
692#ifdef FLOAT_EFFECT_CHAIN
693 accumulate_float(
694 mConfig.outputCfg.buffer.f32,
695 mConfig.inputCfg.buffer.f32,
696 safeInputOutputSampleCount);
697#else
698 accumulate_i16(
699 mConfig.outputCfg.buffer.s16,
700 mConfig.inputCfg.buffer.s16,
701 safeInputOutputSampleCount);
702#endif
703 };
704 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
705#ifdef FLOAT_EFFECT_CHAIN
706 memcpy(
707 mConfig.outputCfg.buffer.f32,
708 mConfig.inputCfg.buffer.f32,
709 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
710
711#else
712 memcpy(
713 mConfig.outputCfg.buffer.s16,
714 mConfig.inputCfg.buffer.s16,
715 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
716#endif
717 };
718
Eric Laurentca7cc822012-11-19 14:55:58 -0800719 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700720 int ret;
721 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700722 if (auxType) {
723 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800724 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700725#ifdef FLOAT_EFFECT_CHAIN
726 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800727#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700728 // Do in-place float conversion for auxiliary effect input buffer.
729 static_assert(sizeof(float) <= sizeof(int32_t),
730 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
731
Andy Hungfa69ca32017-11-30 10:07:53 -0800732 memcpy_to_float_from_q4_27(
733 mConfig.inputCfg.buffer.f32,
734 mConfig.inputCfg.buffer.s32,
735 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800736#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 } else
Andy Hung116a4982017-11-30 10:15:08 -0800738#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800739 {
Andy Hung116a4982017-11-30 10:15:08 -0800740#ifdef FLOAT_AUX
741 memcpy_to_i16_from_float(
742 mConfig.inputCfg.buffer.s16,
743 mConfig.inputCfg.buffer.f32,
744 mConfig.inputCfg.buffer.frameCount);
745#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800746 memcpy_to_i16_from_q4_27(
747 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700748 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800749 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800750#endif
rago94a1ee82017-07-21 15:11:02 -0700751 }
rago94a1ee82017-07-21 15:11:02 -0700752 }
753#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800754 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
755 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
756
757 if (!auxType && mInChannelCountRequested != inChannelCount) {
758 adjust_channels(
759 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
760 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
761 sizeof(float),
762 sizeof(float)
763 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
764 inBuffer = mInConversionBuffer;
765 }
766 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
767 && mOutChannelCountRequested != outChannelCount) {
768 adjust_selected_channels(
769 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
770 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
771 sizeof(float),
772 sizeof(float)
773 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
774 outBuffer = mOutConversionBuffer;
775 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
777 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800778 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800779 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
780 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700781 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800782 memcpy_to_i16_from_float(
783 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800784 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800785 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800786 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700787 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800789 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
791 goto data_bypass;
792 }
793 memcpy_to_i16_from_float(
794 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800795 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800796 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800797 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700798 }
799 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800800#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800801 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800802#ifdef FLOAT_EFFECT_CHAIN
803 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800804 sp<EffectBufferHalInterface> target =
805 mOutChannelCountRequested != outChannelCount
806 ? mOutConversionBuffer : mOutBuffer;
807
Andy Hungfa69ca32017-11-30 10:07:53 -0800808 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800809 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800810 mOutConversionBuffer->audioBuffer()->s16,
811 outChannelCount * mConfig.outputCfg.buffer.frameCount);
812 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800813 if (mOutChannelCountRequested != outChannelCount) {
814 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
815 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
816 sizeof(float),
817 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
818 }
rago94a1ee82017-07-21 15:11:02 -0700819#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700820 } else {
rago94a1ee82017-07-21 15:11:02 -0700821#ifdef FLOAT_EFFECT_CHAIN
822 data_bypass:
823#endif
824 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800825 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700826 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800827 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700828 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800829 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700830 }
831 }
832 ret = -ENODATA;
833 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800834
Eric Laurentca7cc822012-11-19 14:55:58 -0800835 // force transition to IDLE state when engine is ready
836 if (mState == STOPPED && ret == -ENODATA) {
837 mDisableWaitCnt = 1;
838 }
839
840 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700841 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800842#ifdef FLOAT_AUX
843 const size_t size =
844 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
845#else
rago94a1ee82017-07-21 15:11:02 -0700846 const size_t size =
847 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800848#endif
rago94a1ee82017-07-21 15:11:02 -0700849 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800850 }
851 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700852 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800853 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
854 // If an insert effect is idle and input buffer is different from output buffer,
855 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700856 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700857 // similar handling with data_bypass above.
858 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
859 accumulateInputToOutput();
860 } else { // EFFECT_BUFFER_ACCESS_WRITE
861 copyInputToOutput();
862 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800863 }
864 }
865}
866
867void AudioFlinger::EffectModule::reset_l()
868{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700869 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 return;
871 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700872 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800873}
874
875status_t AudioFlinger::EffectModule::configure()
876{
rago94a1ee82017-07-21 15:11:02 -0700877 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700878 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700879 uint32_t size;
880 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700881 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700883 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700884 status = NO_INIT;
885 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800886 }
887
Eric Laurentca7cc822012-11-19 14:55:58 -0800888 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800889 // TODO: handle configuration of input (record) SW effects above the HAL,
890 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
891 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700892 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200893 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800894 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200895 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800896
897 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800898 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
899 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
900 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
901 mConfig.inputCfg.channels);
902 }
903#ifndef MULTICHANNEL_EFFECT_CHAIN
904 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
905 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
906 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
907 mConfig.outputCfg.channels);
908 }
909#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800910 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800911#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700912 // TODO: Update this logic when multichannel effects are implemented.
913 // For offloaded tracks consider mono output as stereo for proper effect initialization
914 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
915 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
916 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
917 ALOGV("Overriding effect input and output as STEREO");
918 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800919#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800920 }
jiabineb3bda02020-06-30 14:07:03 -0700921 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700922 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700923 mConfig.inputCfg.channels |= hapticChannelMask;
924 mConfig.outputCfg.channels |= hapticChannelMask;
925 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800926 mInChannelCountRequested =
927 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
928 mOutChannelCountRequested =
929 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700930
rago94a1ee82017-07-21 15:11:02 -0700931 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
932 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900933
934 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700935 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900936 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
937 ALOGV("Overriding effect input as 48kHz");
938 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700939 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900940 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800941 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
942 mConfig.inputCfg.bufferProvider.cookie = NULL;
943 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
944 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
945 mConfig.outputCfg.bufferProvider.cookie = NULL;
946 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
947 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
948 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
949 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800950 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800951 // always overwrites output buffer: input buffer == output buffer
952 // - in other sessions:
953 // last effect in the chain accumulates in output buffer: input buffer != output buffer
954 // other effect: overwrites output buffer: input buffer == output buffer
955 // Auxiliary effect:
956 // accumulates in output buffer: input buffer != output buffer
957 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700958 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800959 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
960 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700961 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800962 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
963
Eric Laurent6b446ce2019-12-13 10:56:31 -0800964 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700965 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800966 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800967
968 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700969 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700970 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800971 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700972 &mConfig,
973 &size,
974 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700975 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800976 status = cmdStatus;
977 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800978
979#ifdef MULTICHANNEL_EFFECT_CHAIN
980 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700981 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800982 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
983 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
984 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700985 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
986 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800987 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
988 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
989 }
990 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
991 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
992 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
993 }
994 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700995 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800996 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700997 &mConfig,
998 &size,
999 &cmdStatus);
1000 if (status == NO_ERROR) {
1001 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001002 }
1003 }
1004#endif
1005
1006#ifdef FLOAT_EFFECT_CHAIN
1007 if (status == NO_ERROR) {
1008 mSupportsFloat = true;
1009 }
1010
1011 if (status != NO_ERROR) {
1012 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1013 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1014 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1015 size = sizeof(int);
1016 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1017 sizeof(mConfig),
1018 &mConfig,
1019 &size,
1020 &cmdStatus);
1021 if (status == NO_ERROR) {
1022 status = cmdStatus;
1023 }
1024 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001025 mSupportsFloat = false;
1026 ALOGVV("config worked with 16 bit");
1027 } else {
1028 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001029 }
rago94a1ee82017-07-21 15:11:02 -07001030 }
1031#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001032
rago94a1ee82017-07-21 15:11:02 -07001033 if (status == NO_ERROR) {
1034 // Establish Buffer strategy
1035 setInBuffer(mInBuffer);
1036 setOutBuffer(mOutBuffer);
1037
1038 // Update visualizer latency
1039 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1040 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1041 effect_param_t *p = (effect_param_t *)buf32;
1042
1043 p->psize = sizeof(uint32_t);
1044 p->vsize = sizeof(uint32_t);
1045 size = sizeof(int);
1046 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1047
Andy Hungfda44002021-06-03 17:23:16 -07001048 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001049
1050 *((int32_t *)p->data + 1)= latency;
1051 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1052 sizeof(effect_param_t) + 8,
1053 &buf32,
1054 &size,
1055 &cmdStatus);
1056 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001057 }
1058
Andy Hung05083ac2017-12-14 15:00:28 -08001059 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1060 mMaxDisableWaitCnt = (uint32_t)std::max(
1061 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1062 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1063 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001064
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001066 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001067 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001068 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 return status;
1070}
1071
1072status_t AudioFlinger::EffectModule::init()
1073{
1074 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001075 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 return NO_INIT;
1077 }
1078 status_t cmdStatus;
1079 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001080 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1081 0,
1082 NULL,
1083 &size,
1084 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001085 if (status == 0) {
1086 status = cmdStatus;
1087 }
1088 return status;
1089}
1090
Eric Laurent1b928682014-10-02 19:41:47 -07001091void AudioFlinger::EffectModule::addEffectToHal_l()
1092{
1093 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1094 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001095 if (mAddedToHal) {
1096 return;
1097 }
1098
Andy Hungfda44002021-06-03 17:23:16 -07001099 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001100 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001101 }
1102}
1103
Eric Laurentfa1e1232016-08-02 19:01:49 -07001104// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001105status_t AudioFlinger::EffectModule::start()
1106{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001107 status_t status;
1108 {
1109 Mutex::Autolock _l(mLock);
1110 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001111 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001112 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001113 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001114 }
1115 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001116}
1117
1118status_t AudioFlinger::EffectModule::start_l()
1119{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001120 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 return NO_INIT;
1122 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001123 if (mStatus != NO_ERROR) {
1124 return mStatus;
1125 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001126 status_t cmdStatus;
1127 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001128 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1129 0,
1130 NULL,
1131 &size,
1132 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 if (status == 0) {
1134 status = cmdStatus;
1135 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001136 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001137 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001138 }
1139 return status;
1140}
1141
1142status_t AudioFlinger::EffectModule::stop()
1143{
1144 Mutex::Autolock _l(mLock);
1145 return stop_l();
1146}
1147
1148status_t AudioFlinger::EffectModule::stop_l()
1149{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001150 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001151 return NO_INIT;
1152 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001153 if (mStatus != NO_ERROR) {
1154 return mStatus;
1155 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001156 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001157 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001158
1159 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001160 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1161 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1162 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001163 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001164 mSetVolumeReentrantTid = INVALID_PID;
1165 }
1166
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001167 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1168 0,
1169 NULL,
1170 &size,
1171 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001172 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001173 status = cmdStatus;
1174 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001175 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001176 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001177 }
1178 return status;
1179}
1180
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001181// must be called with EffectChain::mLock held
1182void AudioFlinger::EffectModule::release_l()
1183{
1184 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001185 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001186 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001187 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001188 mEffectInterface.clear();
1189 }
1190}
1191
Eric Laurent6b446ce2019-12-13 10:56:31 -08001192status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001193{
1194 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1195 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001196 if (!mAddedToHal) {
1197 return NO_ERROR;
1198 }
1199
Andy Hungfda44002021-06-03 17:23:16 -07001200 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001201 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001202 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001203 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001204}
1205
Andy Hunge4a1d912016-08-17 14:11:13 -07001206// round up delta valid if value and divisor are positive.
1207template <typename T>
1208static T roundUpDelta(const T &value, const T &divisor) {
1209 T remainder = value % divisor;
1210 return remainder == 0 ? 0 : divisor - remainder;
1211}
1212
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001213status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1214 const std::vector<uint8_t>& cmdData,
1215 int32_t maxReplySize,
1216 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001217{
1218 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001219 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001220
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001221 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001222 return NO_INIT;
1223 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001224 if (mStatus != NO_ERROR) {
1225 return mStatus;
1226 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001227 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1228 return -EINVAL;
1229 }
1230 size_t cmdSize = cmdData.size();
1231 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1232 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1233 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001234 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001235 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001236 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001237 android_errorWriteLog(0x534e4554, "33003822");
1238 return -EINVAL;
1239 }
1240 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001241 (maxReplySize < sizeof(effect_param_t) ||
1242 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001243 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001244 return -EINVAL;
1245 }
ragoe2759072016-11-22 18:02:48 -08001246 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001247 (sizeof(effect_param_t) > maxReplySize
1248 || param->psize > maxReplySize - sizeof(effect_param_t)
1249 || param->vsize > maxReplySize - sizeof(effect_param_t)
1250 - param->psize
1251 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1252 maxReplySize
1253 - sizeof(effect_param_t)
1254 - param->psize
1255 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001256 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1257 android_errorWriteLog(0x534e4554, "32705438");
1258 return -EINVAL;
1259 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001260 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001261 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1262 && // DEFERRED not generally used
1263 (param == nullptr
1264 || param->psize > cmdSize - sizeof(effect_param_t)
1265 || param->vsize > cmdSize - sizeof(effect_param_t)
1266 - param->psize
1267 || roundUpDelta(param->psize,
1268 (uint32_t) sizeof(int)) >
1269 cmdSize
1270 - sizeof(effect_param_t)
1271 - param->psize
1272 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001273 android_errorWriteLog(0x534e4554, "30204301");
1274 return -EINVAL;
1275 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001276 uint32_t replySize = maxReplySize;
1277 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001278 status_t status = mEffectInterface->command(cmdCode,
1279 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001280 const_cast<uint8_t*>(cmdData.data()),
1281 &replySize,
1282 reply->data());
1283 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001284 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 for (size_t i = 1; i < mHandles.size(); i++) {
1286 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001287 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001288 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001289 }
1290 }
1291 }
1292 return status;
1293}
1294
Eric Laurentca7cc822012-11-19 14:55:58 -08001295bool AudioFlinger::EffectModule::isProcessEnabled() const
1296{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001297 if (mStatus != NO_ERROR) {
1298 return false;
1299 }
1300
Eric Laurentca7cc822012-11-19 14:55:58 -08001301 switch (mState) {
1302 case RESTART:
1303 case ACTIVE:
1304 case STOPPING:
1305 case STOPPED:
1306 return true;
1307 case IDLE:
1308 case STARTING:
1309 case DESTROYED:
1310 default:
1311 return false;
1312 }
1313}
1314
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001315bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1316{
Andy Hungfda44002021-06-03 17:23:16 -07001317 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001318}
1319
1320bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1321{
1322 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1323}
1324
Mikhail Naganov022b9952017-01-04 16:36:51 -08001325void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001326 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001327
1328 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001329 if (buffer != 0) {
1330 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1331 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1332 } else {
1333 mConfig.inputCfg.buffer.raw = NULL;
1334 }
1335 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001336 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001337
1338#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001339 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001340 // Theoretically insert effects can also do in-place conversions (destroying
1341 // the original buffer) when the output buffer is identical to the input buffer,
1342 // but we don't optimize for it here.
1343 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001344 const uint32_t inChannelCount =
1345 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1346 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001347 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001348 // we need to translate - create hidl shared buffer and intercept
1349 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001350 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1351 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1352 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001353
1354 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1355 __func__, inChannels, inFrameCount, size);
1356
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001357 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001358 || size > mInConversionBuffer->getSize())) {
1359 mInConversionBuffer.clear();
1360 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001361 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001362 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001363 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001364 mInConversionBuffer->setFrameCount(inFrameCount);
1365 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001366 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001367 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001368 }
1369 }
1370#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001371}
1372
1373void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001374 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001375
1376 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001377 if (buffer != 0) {
1378 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1379 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1380 } else {
1381 mConfig.outputCfg.buffer.raw = NULL;
1382 }
1383 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001384 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001385
1386#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001387 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001388 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001389 const uint32_t outChannelCount =
1390 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1391 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001392 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001393 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001394 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1395 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1396 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001397
1398 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1399 __func__, outChannels, outFrameCount, size);
1400
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001401 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001402 || size > mOutConversionBuffer->getSize())) {
1403 mOutConversionBuffer.clear();
1404 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001405 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001406 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001407 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001408 mOutConversionBuffer->setFrameCount(outFrameCount);
1409 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001410 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001411 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001412 }
1413 }
1414#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001415}
1416
Eric Laurentca7cc822012-11-19 14:55:58 -08001417status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1418{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001419 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001420 if (mStatus != NO_ERROR) {
1421 return mStatus;
1422 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1425 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1426 if (isProcessEnabled() &&
1427 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001428 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1429 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001430 uint32_t volume[2];
1431 uint32_t *pVolume = NULL;
1432 uint32_t size = sizeof(volume);
1433 volume[0] = *left;
1434 volume[1] = *right;
1435 if (controller) {
1436 pVolume = volume;
1437 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001438 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1439 size,
1440 volume,
1441 &size,
1442 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1444 *left = volume[0];
1445 *right = volume[1];
1446 }
1447 }
1448 return status;
1449}
1450
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001451void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1452{
Zhou Songd505c642020-02-20 16:35:37 +08001453 // for offload or direct thread, if the effect chain has non-offloadable
1454 // effect and any effect module within the chain has volume control, then
1455 // volume control is delegated to effect, otherwise, set volume to hal.
1456 if (mEffectCallback->isOffloadOrDirect() &&
1457 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001458 float vol_l = (float)left / (1 << 24);
1459 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001460 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001461 }
1462}
1463
jiabin8f278ee2019-11-11 12:16:27 -08001464status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1465 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001466{
jiabin8f278ee2019-11-11 12:16:27 -08001467 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1468 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 return NO_ERROR;
1470 }
1471
1472 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001473 if (mStatus != NO_ERROR) {
1474 return mStatus;
1475 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001477 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 status_t cmdStatus;
1479 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001480 // FIXME: use audio device types and addresses when the hal interface is ready.
1481 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001482 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001483 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001484 &size,
1485 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001486 }
1487 return status;
1488}
1489
jiabin8f278ee2019-11-11 12:16:27 -08001490status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1491{
1492 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1493}
1494
1495status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1496{
1497 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1498}
1499
Eric Laurentca7cc822012-11-19 14:55:58 -08001500status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1501{
1502 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001503 if (mStatus != NO_ERROR) {
1504 return mStatus;
1505 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 status_t status = NO_ERROR;
1507 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1508 status_t cmdStatus;
1509 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001510 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1511 sizeof(audio_mode_t),
1512 &mode,
1513 &size,
1514 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001515 if (status == NO_ERROR) {
1516 status = cmdStatus;
1517 }
1518 }
1519 return status;
1520}
1521
1522status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1523{
1524 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001525 if (mStatus != NO_ERROR) {
1526 return mStatus;
1527 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001528 status_t status = NO_ERROR;
1529 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1530 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001531 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1532 sizeof(audio_source_t),
1533 &source,
1534 &size,
1535 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001536 }
1537 return status;
1538}
1539
Eric Laurent5baf2af2013-09-12 17:37:00 -07001540status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1541{
1542 Mutex::Autolock _l(mLock);
1543 if (mStatus != NO_ERROR) {
1544 return mStatus;
1545 }
1546 status_t status = NO_ERROR;
1547 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1548 status_t cmdStatus;
1549 uint32_t size = sizeof(status_t);
1550 effect_offload_param_t cmd;
1551
1552 cmd.isOffload = offloaded;
1553 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001554 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1555 sizeof(effect_offload_param_t),
1556 &cmd,
1557 &size,
1558 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001559 if (status == NO_ERROR) {
1560 status = cmdStatus;
1561 }
1562 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1563 } else {
1564 if (offloaded) {
1565 status = INVALID_OPERATION;
1566 }
1567 mOffloaded = false;
1568 }
1569 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1570 return status;
1571}
1572
1573bool AudioFlinger::EffectModule::isOffloaded() const
1574{
1575 Mutex::Autolock _l(mLock);
1576 return mOffloaded;
1577}
1578
jiabineb3bda02020-06-30 14:07:03 -07001579/*static*/
1580bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1581 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1582}
1583
1584bool AudioFlinger::EffectModule::isHapticGenerator() const {
1585 return isHapticGenerator(&mDescriptor.type);
1586}
1587
jiabine70bc7f2020-06-30 22:07:55 -07001588status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1589{
1590 if (mStatus != NO_ERROR) {
1591 return mStatus;
1592 }
1593 if (!isHapticGenerator()) {
1594 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1595 return INVALID_OPERATION;
1596 }
1597
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001598 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1599 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001600 param->psize = sizeof(int32_t);
1601 param->vsize = sizeof(int32_t) * 2;
1602 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1603 *((int32_t*)param->data + 1) = id;
1604 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001605 std::vector<uint8_t> response;
1606 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001607 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001608 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1609 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001610 }
1611 return status;
1612}
1613
Lais Andradebc3f37a2021-07-02 00:13:19 +01001614status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001615{
1616 if (mStatus != NO_ERROR) {
1617 return mStatus;
1618 }
1619 if (!isHapticGenerator()) {
1620 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1621 return INVALID_OPERATION;
1622 }
1623
Lais Andradebc3f37a2021-07-02 00:13:19 +01001624 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001625 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001626 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001627 effect_param_t *param = (effect_param_t*) request.data();
1628 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001629 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001630 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1631 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001632 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1633 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1634 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001635 std::vector<uint8_t> response;
1636 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1637 if (status == NO_ERROR) {
1638 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1639 status = *reinterpret_cast<const status_t*>(response.data());
1640 }
1641 return status;
1642}
1643
Andy Hungbded9c82017-11-30 18:47:35 -08001644static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1645 std::stringstream ss;
1646
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001647 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001648 return "nullptr"; // make different than below
1649 } else if (buffer->externalData() != nullptr) {
1650 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1651 << " -> "
1652 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1653 } else {
1654 ss << buffer->audioBuffer()->raw;
1655 }
1656 return ss.str();
1657}
Marco Nelissenb2208842014-02-07 14:00:50 -08001658
Eric Laurent41709552019-12-16 19:34:05 -08001659void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001660{
Eric Laurent41709552019-12-16 19:34:05 -08001661 EffectBase::dump(fd, args);
1662
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001665
Eric Laurent41709552019-12-16 19:34:05 -08001666 result.append("\t\tStatus Engine:\n");
1667 result.appendFormat("\t\t%03d %p\n",
1668 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001669
1670 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001671
1672 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001673 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1674 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1675 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001676 mConfig.inputCfg.buffer.frameCount,
1677 mConfig.inputCfg.samplingRate,
1678 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001679 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001680 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001681
1682 result.append("\t\t- Output configuration:\n");
1683 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001684 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001685 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 mConfig.outputCfg.buffer.frameCount,
1687 mConfig.outputCfg.samplingRate,
1688 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001689 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001690 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001691
rago94a1ee82017-07-21 15:11:02 -07001692#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001693
Andy Hungbded9c82017-11-30 18:47:35 -08001694 result.appendFormat("\t\t- HAL buffers:\n"
1695 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1696 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1697 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1698 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1699 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001700#endif
1701
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 write(fd, result.string(), result.length());
1703
Mikhail Naganov4d547672019-02-22 14:19:19 -08001704 if (mEffectInterface != 0) {
1705 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1706 (void)mEffectInterface->dump(fd);
1707 }
1708
Eric Laurentca7cc822012-11-19 14:55:58 -08001709 if (locked) {
1710 mLock.unlock();
1711 }
1712}
1713
1714// ----------------------------------------------------------------------------
1715// EffectHandle implementation
1716// ----------------------------------------------------------------------------
1717
1718#undef LOG_TAG
1719#define LOG_TAG "AudioFlinger::EffectHandle"
1720
Eric Laurent41709552019-12-16 19:34:05 -08001721AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001722 const sp<AudioFlinger::Client>& client,
1723 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001724 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 : BnEffect(),
1726 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001727 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1728 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001729{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001730 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001731
1732 if (client == 0) {
1733 return;
1734 }
1735 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1736 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001737 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001738 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001739 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001740 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001741 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 return;
1743 }
Glenn Kastene75da402013-11-20 13:54:52 -08001744 new(mCblk) effect_param_cblk_t();
1745 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001746}
1747
1748AudioFlinger::EffectHandle::~EffectHandle()
1749{
1750 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001751 disconnect(false);
1752}
1753
Glenn Kastene75da402013-11-20 13:54:52 -08001754status_t AudioFlinger::EffectHandle::initCheck()
1755{
1756 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1757}
1758
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001759#define RETURN(code) \
1760 *_aidl_return = (code); \
1761 return Status::ok();
1762
1763Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001764{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001765 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001766 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001767 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001768 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001769 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001770 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001773 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001774
1775 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001776 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001777 }
1778
1779 mEnabled = true;
1780
Eric Laurent6c796322019-04-09 14:13:17 -07001781 status_t status = effect->updatePolicyState();
1782 if (status != NO_ERROR) {
1783 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001784 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001785 }
1786
Eric Laurent6b446ce2019-12-13 10:56:31 -08001787 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001788
1789 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001791 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001792 }
1793
Eric Laurent6b446ce2019-12-13 10:56:31 -08001794 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001795 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001796 mEnabled = false;
1797 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001798 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001799}
1800
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001801Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001802{
1803 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001804 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001805 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001807 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001810 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001811 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001812
1813 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001814 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 }
1816 mEnabled = false;
1817
Eric Laurent6c796322019-04-09 14:13:17 -07001818 effect->updatePolicyState();
1819
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001820 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001821 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001822 }
1823
Eric Laurent6b446ce2019-12-13 10:56:31 -08001824 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001825 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001826}
1827
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001828Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001829{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001830 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001832 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001833}
1834
1835void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1836{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001837 AutoMutex _l(mLock);
1838 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1839 if (mDisconnected) {
1840 if (unpinIfLast) {
1841 android_errorWriteLog(0x534e4554, "32707507");
1842 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 return;
1844 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001846 {
Eric Laurent41709552019-12-16 19:34:05 -08001847 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001848 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001849 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001850 ALOGW("%s Effect handle %p disconnected after thread destruction",
1851 __func__, this);
1852 }
1853 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001854 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001856
Eric Laurentca7cc822012-11-19 14:55:58 -08001857 if (mClient != 0) {
1858 if (mCblk != NULL) {
1859 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1860 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1861 }
1862 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001863 // Client destructor must run with AudioFlinger client mutex locked
1864 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 mClient.clear();
1866 }
1867}
1868
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001869Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1870 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1871 return Status::ok();
1872}
1873
1874Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1875 const std::vector<uint8_t>& cmdData,
1876 int32_t maxResponseSize,
1877 std::vector<uint8_t>* response,
1878 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001879{
1880 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001881 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001882
Eric Laurentc7ab3092017-06-15 18:43:46 -07001883 // reject commands reserved for internal use by audio framework if coming from outside
1884 // of audioserver
1885 switch(cmdCode) {
1886 case EFFECT_CMD_ENABLE:
1887 case EFFECT_CMD_DISABLE:
1888 case EFFECT_CMD_SET_PARAM:
1889 case EFFECT_CMD_SET_PARAM_DEFERRED:
1890 case EFFECT_CMD_SET_PARAM_COMMIT:
1891 case EFFECT_CMD_GET_PARAM:
1892 break;
1893 default:
1894 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1895 break;
1896 }
1897 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001898 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001899 }
1900
Eric Laurent1ffc5852016-12-15 14:46:09 -08001901 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001902 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001903 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001904 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001905 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 writeToBuffer(NO_ERROR, response);
1907 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001908 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001910 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001911 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001912 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001913 writeToBuffer(NO_ERROR, response);
1914 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001915 }
1916
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001918 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001919 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001920 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001921 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001922 // only get parameter command is permitted for applications not controlling the effect
1923 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001924 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001925 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001926
1927 // handle commands that are not forwarded transparently to effect engine
1928 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001929 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001930 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001931 }
1932
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001933 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001934 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001935 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001936 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001937 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001938
Eric Laurentca7cc822012-11-19 14:55:58 -08001939 // No need to trylock() here as this function is executed in the binder thread serving a
1940 // particular client process: no risk to block the whole media server process or mixer
1941 // threads if we are stuck here
1942 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001943 // keep local copy of index in case of client corruption b/32220769
1944 const uint32_t clientIndex = mCblk->clientIndex;
1945 const uint32_t serverIndex = mCblk->serverIndex;
1946 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1947 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 mCblk->serverIndex = 0;
1949 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001950 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001951 }
1952 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001953 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001954 for (uint32_t index = serverIndex; index < clientIndex;) {
1955 int *p = (int *)(mBuffer + index);
1956 const int size = *p++;
1957 if (size < 0
1958 || size > EFFECT_PARAM_BUFFER_SIZE
1959 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001960 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001961 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 break;
1963 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001964
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001965 std::copy(reinterpret_cast<const uint8_t*>(p),
1966 reinterpret_cast<const uint8_t*>(p) + size,
1967 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001968
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001969 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001970 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001971 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001972 sizeof(int),
1973 &replyBuffer);
1974 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001975
1976 // verify shared memory: server index shouldn't change; client index can't go back.
1977 if (serverIndex != mCblk->serverIndex
1978 || clientIndex > mCblk->clientIndex) {
1979 android_errorWriteLog(0x534e4554, "32220769");
1980 status = BAD_VALUE;
1981 break;
1982 }
1983
Eric Laurentca7cc822012-11-19 14:55:58 -08001984 // stop at first error encountered
1985 if (ret != NO_ERROR) {
1986 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001987 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001988 break;
1989 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001990 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001991 break;
1992 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001993 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001994 }
1995 mCblk->serverIndex = 0;
1996 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001997 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001998 }
1999
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002000 status_t status = effect->command(cmdCode,
2001 cmdData,
2002 maxResponseSize,
2003 response);
2004 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002005}
2006
2007void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2008{
2009 ALOGV("setControl %p control %d", this, hasControl);
2010
2011 mHasControl = hasControl;
2012 mEnabled = enabled;
2013
2014 if (signal && mEffectClient != 0) {
2015 mEffectClient->controlStatusChanged(hasControl);
2016 }
2017}
2018
2019void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002020 const std::vector<uint8_t>& cmdData,
2021 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002022{
2023 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002024 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002025 }
2026}
2027
2028
2029
2030void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2031{
2032 if (mEffectClient != 0) {
2033 mEffectClient->enableStatusChanged(enabled);
2034 }
2035}
2036
Eric Laurentde8caf42021-08-11 17:19:25 +02002037void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2038{
2039 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2040 mEffectClient->framesProcessed(frames);
2041 }
2042}
2043
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002044void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002045{
2046 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2047
Marco Nelissenb2208842014-02-07 14:00:50 -08002048 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002049 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002050 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002051 mHasControl ? "yes" : "no",
2052 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002053 mCblk ? mCblk->clientIndex : 0,
2054 mCblk ? mCblk->serverIndex : 0
2055 );
2056
2057 if (locked) {
2058 mCblk->lock.unlock();
2059 }
2060}
2061
2062#undef LOG_TAG
2063#define LOG_TAG "AudioFlinger::EffectChain"
2064
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002065AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2066 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002067 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002068 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002069 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002070 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002071{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002072 sp<ThreadBase> p = thread.promote();
2073 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002074 return;
2075 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002076 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002077 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2078 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002079}
2080
2081AudioFlinger::EffectChain::~EffectChain()
2082{
Eric Laurentca7cc822012-11-19 14:55:58 -08002083}
2084
2085// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2086sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2087 effect_descriptor_t *descriptor)
2088{
2089 size_t size = mEffects.size();
2090
2091 for (size_t i = 0; i < size; i++) {
2092 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2093 return mEffects[i];
2094 }
2095 }
2096 return 0;
2097}
2098
2099// getEffectFromId_l() must be called with ThreadBase::mLock held
2100sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2101{
2102 size_t size = mEffects.size();
2103
2104 for (size_t i = 0; i < size; i++) {
2105 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2106 if (id == 0 || mEffects[i]->id() == id) {
2107 return mEffects[i];
2108 }
2109 }
2110 return 0;
2111}
2112
2113// getEffectFromType_l() must be called with ThreadBase::mLock held
2114sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2115 const effect_uuid_t *type)
2116{
2117 size_t size = mEffects.size();
2118
2119 for (size_t i = 0; i < size; i++) {
2120 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2121 return mEffects[i];
2122 }
2123 }
2124 return 0;
2125}
2126
Eric Laurent6c796322019-04-09 14:13:17 -07002127std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2128{
2129 std::vector<int> ids;
2130 Mutex::Autolock _l(mLock);
2131 for (size_t i = 0; i < mEffects.size(); i++) {
2132 ids.push_back(mEffects[i]->id());
2133 }
2134 return ids;
2135}
2136
Eric Laurentca7cc822012-11-19 14:55:58 -08002137void AudioFlinger::EffectChain::clearInputBuffer()
2138{
2139 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002140 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002141}
2142
2143// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002144void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002145{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002146 if (mInBuffer == NULL) {
2147 return;
2148 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002149 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2150 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002151
Eric Laurent6b446ce2019-12-13 10:56:31 -08002152 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002153 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002154}
2155
2156// Must be called with EffectChain::mLock locked
2157void AudioFlinger::EffectChain::process_l()
2158{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002159 // never process effects when:
2160 // - on an OFFLOAD thread
2161 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002162 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002163 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002164 bool tracksOnSession = (trackCnt() != 0);
2165
2166 if (!tracksOnSession && mTailBufferCount == 0) {
2167 doProcess = false;
2168 }
2169
2170 if (activeTrackCnt() == 0) {
2171 // if no track is active and the effect tail has not been rendered,
2172 // the input buffer must be cleared here as the mixer process will not do it
2173 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002174 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002175 if (mTailBufferCount > 0) {
2176 mTailBufferCount--;
2177 }
2178 }
2179 }
2180 }
2181
2182 size_t size = mEffects.size();
2183 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002184 // Only the input and output buffers of the chain can be external,
2185 // and 'update' / 'commit' do nothing for allocated buffers, thus
2186 // it's not needed to consider any other buffers here.
2187 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002188 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2189 mOutBuffer->update();
2190 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002191 for (size_t i = 0; i < size; i++) {
2192 mEffects[i]->process();
2193 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002194 mInBuffer->commit();
2195 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2196 mOutBuffer->commit();
2197 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002198 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002199 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002200 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002201 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2202 }
2203 if (doResetVolume) {
2204 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002205 }
2206}
2207
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002208// createEffect_l() must be called with ThreadBase::mLock held
2209status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002210 effect_descriptor_t *desc,
2211 int id,
2212 audio_session_t sessionId,
2213 bool pinned)
2214{
2215 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002216 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002217 status_t lStatus = effect->status();
2218 if (lStatus == NO_ERROR) {
2219 lStatus = addEffect_ll(effect);
2220 }
2221 if (lStatus != NO_ERROR) {
2222 effect.clear();
2223 }
2224 return lStatus;
2225}
2226
2227// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002228status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2229{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002230 Mutex::Autolock _l(mLock);
2231 return addEffect_ll(effect);
2232}
2233// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2234status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2235{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002236 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002237
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002238 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002239 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2240 // Auxiliary effects are inserted at the beginning of mEffects vector as
2241 // they are processed first and accumulated in chain input buffer
2242 mEffects.insertAt(effect, 0);
2243
2244 // the input buffer for auxiliary effect contains mono samples in
2245 // 32 bit format. This is to avoid saturation in AudoMixer
2246 // accumulation stage. Saturation is done in EffectModule::process() before
2247 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002248 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002249 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002250#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002251 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002252 numSamples * sizeof(float), &halBuffer);
2253#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002254 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002255 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002256#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002257 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002258
2259 effect->configure();
2260
Mikhail Naganov022b9952017-01-04 16:36:51 -08002261 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002262 // auxiliary effects output samples to chain input buffer for further processing
2263 // by insert effects
2264 effect->setOutBuffer(mInBuffer);
2265 } else {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002266 ssize_t idx_insert = getInsertIndex(desc);
2267 if (idx_insert < 0) {
2268 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002269 }
2270
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002271 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002272 mEffects.insertAt(effect, idx_insert);
2273
2274 effect->configure();
2275
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002276 // - By default:
2277 // All effects read samples from chain input buffer.
2278 // The last effect in the chain, writes samples to chain output buffer,
2279 // otherwise to chain input buffer
2280 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2281 // The spatializer effect (first effect) reads samples from the input buffer
2282 // and writes samples to the output buffer.
2283 // All other effects read and writes samples to the output buffer
2284 if (mEffectCallback->isSpatializer()
2285 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002286 effect->setOutBuffer(mOutBuffer);
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002287 if (idx_insert == 0) {
2288 if (previousSize != 0) {
2289 mEffects[1]->configure();
2290 mEffects[1]->setInBuffer(mOutBuffer);
2291 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2292 }
2293 effect->setInBuffer(mInBuffer);
2294 } else {
2295 effect->setInBuffer(mOutBuffer);
2296 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002297 } else {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002298 effect->setInBuffer(mInBuffer);
2299 if (idx_insert == previousSize) {
2300 if (idx_insert != 0) {
2301 mEffects[idx_insert-1]->configure();
2302 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2303 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2304 }
2305 effect->setOutBuffer(mOutBuffer);
2306 } else {
2307 effect->setOutBuffer(mInBuffer);
2308 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002310 ALOGV("%s effect %p, added in chain %p at rank %zu",
2311 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 }
2313 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002314
Eric Laurentca7cc822012-11-19 14:55:58 -08002315 return NO_ERROR;
2316}
2317
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002318ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2319 // Insert effects are inserted at the end of mEffects vector as they are processed
2320 // after track and auxiliary effects.
2321 // Insert effect order as a function of indicated preference:
2322 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2323 // another effect is present
2324 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2325 // last effect claiming first position
2326 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2327 // first effect claiming last position
2328 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2329 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2330 // already present
2331 // Spatializer or Downmixer effects are inserted in first position because
2332 // they adapt the channel count for all other effects in the chain
2333 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2334 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2335 return 0;
2336 }
2337
2338 size_t size = mEffects.size();
2339 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2340 ssize_t idx_insert;
2341 ssize_t idx_insert_first = -1;
2342 ssize_t idx_insert_last = -1;
2343
2344 idx_insert = size;
2345 for (size_t i = 0; i < size; i++) {
2346 effect_descriptor_t d = mEffects[i]->desc();
2347 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2348 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2349 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2350 // check invalid effect chaining combinations
2351 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2352 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2353 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2354 __func__, desc.name, d.name);
2355 return -1;
2356 }
2357 // remember position of first insert effect and by default
2358 // select this as insert position for new effect
2359 if (idx_insert == size) {
2360 idx_insert = i;
2361 }
2362 // remember position of last insert effect claiming
2363 // first position
2364 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2365 idx_insert_first = i;
2366 }
2367 // remember position of first insert effect claiming
2368 // last position
2369 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2370 idx_insert_last == -1) {
2371 idx_insert_last = i;
2372 }
2373 }
2374 }
2375
2376 // modify idx_insert from first position if needed
2377 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2378 if (idx_insert_last != -1) {
2379 idx_insert = idx_insert_last;
2380 } else {
2381 idx_insert = size;
2382 }
2383 } else {
2384 if (idx_insert_first != -1) {
2385 idx_insert = idx_insert_first + 1;
2386 }
2387 }
2388 return idx_insert;
2389}
2390
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002391// removeEffect_l() must be called with ThreadBase::mLock held
2392size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2393 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002394{
2395 Mutex::Autolock _l(mLock);
2396 size_t size = mEffects.size();
2397 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2398
2399 for (size_t i = 0; i < size; i++) {
2400 if (effect == mEffects[i]) {
2401 // calling stop here will remove pre-processing effect from the audio HAL.
2402 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2403 // the middle of a read from audio HAL
2404 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2405 mEffects[i]->state() == EffectModule::STOPPING) {
2406 mEffects[i]->stop();
2407 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002408 if (release) {
2409 mEffects[i]->release_l();
2410 }
2411
Mikhail Naganov022b9952017-01-04 16:36:51 -08002412 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002413 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002414 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002415 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 }
2417 }
2418 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002419
2420 // make sure the input buffer configuration for the new first effect in the chain
2421 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2422 if (i == 0 && size > 1) {
2423 mEffects[0]->configure();
2424 mEffects[0]->setInBuffer(mInBuffer);
2425 }
2426
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002427 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002428 this, i);
2429 break;
2430 }
2431 }
2432
2433 return mEffects.size();
2434}
2435
jiabin8f278ee2019-11-11 12:16:27 -08002436// setDevices_l() must be called with ThreadBase::mLock held
2437void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002438{
2439 size_t size = mEffects.size();
2440 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002441 mEffects[i]->setDevices(devices);
2442 }
2443}
2444
2445// setInputDevice_l() must be called with ThreadBase::mLock held
2446void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2447{
2448 size_t size = mEffects.size();
2449 for (size_t i = 0; i < size; i++) {
2450 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002451 }
2452}
2453
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002454// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002455void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2456{
2457 size_t size = mEffects.size();
2458 for (size_t i = 0; i < size; i++) {
2459 mEffects[i]->setMode(mode);
2460 }
2461}
2462
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002463// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002464void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2465{
2466 size_t size = mEffects.size();
2467 for (size_t i = 0; i < size; i++) {
2468 mEffects[i]->setAudioSource(source);
2469 }
2470}
2471
Zhou Songd505c642020-02-20 16:35:37 +08002472bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2473 for (const auto &effect : mEffects) {
2474 if (effect->isVolumeControlEnabled()) return true;
2475 }
2476 return false;
2477}
2478
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002479// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002480bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002481{
2482 uint32_t newLeft = *left;
2483 uint32_t newRight = *right;
2484 bool hasControl = false;
2485 int ctrlIdx = -1;
2486 size_t size = mEffects.size();
2487
2488 // first update volume controller
2489 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002490 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002491 ctrlIdx = i - 1;
2492 hasControl = true;
2493 break;
2494 }
2495 }
2496
Eric Laurentfa1e1232016-08-02 19:01:49 -07002497 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002498 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002499 if (hasControl) {
2500 *left = mNewLeftVolume;
2501 *right = mNewRightVolume;
2502 }
2503 return hasControl;
2504 }
2505
2506 mVolumeCtrlIdx = ctrlIdx;
2507 mLeftVolume = newLeft;
2508 mRightVolume = newRight;
2509
2510 // second get volume update from volume controller
2511 if (ctrlIdx >= 0) {
2512 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2513 mNewLeftVolume = newLeft;
2514 mNewRightVolume = newRight;
2515 }
2516 // then indicate volume to all other effects in chain.
2517 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002518 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002519 uint32_t lVol = newLeft;
2520 uint32_t rVol = newRight;
2521
2522 for (size_t i = 0; i < size; i++) {
2523 if ((int)i == ctrlIdx) {
2524 continue;
2525 }
2526 // this also works for ctrlIdx == -1 when there is no volume controller
2527 if ((int)i > ctrlIdx) {
2528 lVol = *left;
2529 rVol = *right;
2530 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002531 // Pass requested volume directly if this is volume monitor module
2532 if (mEffects[i]->isVolumeMonitor()) {
2533 mEffects[i]->setVolume(left, right, false);
2534 } else {
2535 mEffects[i]->setVolume(&lVol, &rVol, false);
2536 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002537 }
2538 *left = newLeft;
2539 *right = newRight;
2540
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002541 setVolumeForOutput_l(*left, *right);
2542
Eric Laurentca7cc822012-11-19 14:55:58 -08002543 return hasControl;
2544}
2545
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002546// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002547void AudioFlinger::EffectChain::resetVolume_l()
2548{
Eric Laurente7449bf2016-08-03 18:44:07 -07002549 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2550 uint32_t left = mLeftVolume;
2551 uint32_t right = mRightVolume;
2552 (void)setVolume_l(&left, &right, true);
2553 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002554}
2555
jiabineb3bda02020-06-30 14:07:03 -07002556// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2557bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2558{
2559 for (size_t i = 0; i < mEffects.size(); ++i) {
2560 if (mEffects[i]->isHapticGenerator()) {
2561 return true;
2562 }
2563 }
2564 return false;
2565}
2566
jiabine70bc7f2020-06-30 22:07:55 -07002567void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2568{
2569 Mutex::Autolock _l(mLock);
2570 for (size_t i = 0; i < mEffects.size(); ++i) {
2571 mEffects[i]->setHapticIntensity(id, intensity);
2572 }
2573}
2574
Eric Laurent1b928682014-10-02 19:41:47 -07002575void AudioFlinger::EffectChain::syncHalEffectsState()
2576{
2577 Mutex::Autolock _l(mLock);
2578 for (size_t i = 0; i < mEffects.size(); i++) {
2579 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2580 mEffects[i]->state() == EffectModule::STOPPING) {
2581 mEffects[i]->addEffectToHal_l();
2582 }
2583 }
2584}
2585
Eric Laurentca7cc822012-11-19 14:55:58 -08002586void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2587{
Eric Laurentca7cc822012-11-19 14:55:58 -08002588 String8 result;
2589
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002590 const size_t numEffects = mEffects.size();
2591 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002592
Marco Nelissenb2208842014-02-07 14:00:50 -08002593 if (numEffects) {
2594 bool locked = AudioFlinger::dumpTryLock(mLock);
2595 // failed to lock - AudioFlinger is probably deadlocked
2596 if (!locked) {
2597 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002598 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002599
Andy Hungbded9c82017-11-30 18:47:35 -08002600 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2601 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2602 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2603 (int)inBufferStr.size(), "In buffer ",
2604 (int)outBufferStr.size(), "Out buffer ");
2605 result.appendFormat("\t%s %s %d\n",
2606 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002607 write(fd, result.string(), result.size());
2608
2609 for (size_t i = 0; i < numEffects; ++i) {
2610 sp<EffectModule> effect = mEffects[i];
2611 if (effect != 0) {
2612 effect->dump(fd, args);
2613 }
2614 }
2615
2616 if (locked) {
2617 mLock.unlock();
2618 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002619 } else {
2620 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002621 }
2622}
2623
2624// must be called with ThreadBase::mLock held
2625void AudioFlinger::EffectChain::setEffectSuspended_l(
2626 const effect_uuid_t *type, bool suspend)
2627{
2628 sp<SuspendedEffectDesc> desc;
2629 // use effect type UUID timelow as key as there is no real risk of identical
2630 // timeLow fields among effect type UUIDs.
2631 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2632 if (suspend) {
2633 if (index >= 0) {
2634 desc = mSuspendedEffects.valueAt(index);
2635 } else {
2636 desc = new SuspendedEffectDesc();
2637 desc->mType = *type;
2638 mSuspendedEffects.add(type->timeLow, desc);
2639 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2640 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002641
Eric Laurentca7cc822012-11-19 14:55:58 -08002642 if (desc->mRefCount++ == 0) {
2643 sp<EffectModule> effect = getEffectIfEnabled(type);
2644 if (effect != 0) {
2645 desc->mEffect = effect;
2646 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002647 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002648 }
2649 }
2650 } else {
2651 if (index < 0) {
2652 return;
2653 }
2654 desc = mSuspendedEffects.valueAt(index);
2655 if (desc->mRefCount <= 0) {
2656 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002657 desc->mRefCount = 0;
2658 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002659 }
2660 if (--desc->mRefCount == 0) {
2661 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2662 if (desc->mEffect != 0) {
2663 sp<EffectModule> effect = desc->mEffect.promote();
2664 if (effect != 0) {
2665 effect->setSuspended(false);
2666 effect->lock();
2667 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002668 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002669 effect->setEnabled_l(handle->enabled());
2670 }
2671 effect->unlock();
2672 }
2673 desc->mEffect.clear();
2674 }
2675 mSuspendedEffects.removeItemsAt(index);
2676 }
2677 }
2678}
2679
2680// must be called with ThreadBase::mLock held
2681void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2682{
2683 sp<SuspendedEffectDesc> desc;
2684
2685 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2686 if (suspend) {
2687 if (index >= 0) {
2688 desc = mSuspendedEffects.valueAt(index);
2689 } else {
2690 desc = new SuspendedEffectDesc();
2691 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2692 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2693 }
2694 if (desc->mRefCount++ == 0) {
2695 Vector< sp<EffectModule> > effects;
2696 getSuspendEligibleEffects(effects);
2697 for (size_t i = 0; i < effects.size(); i++) {
2698 setEffectSuspended_l(&effects[i]->desc().type, true);
2699 }
2700 }
2701 } else {
2702 if (index < 0) {
2703 return;
2704 }
2705 desc = mSuspendedEffects.valueAt(index);
2706 if (desc->mRefCount <= 0) {
2707 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2708 desc->mRefCount = 1;
2709 }
2710 if (--desc->mRefCount == 0) {
2711 Vector<const effect_uuid_t *> types;
2712 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2713 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2714 continue;
2715 }
2716 types.add(&mSuspendedEffects.valueAt(i)->mType);
2717 }
2718 for (size_t i = 0; i < types.size(); i++) {
2719 setEffectSuspended_l(types[i], false);
2720 }
2721 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2722 mSuspendedEffects.keyAt(index));
2723 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2724 }
2725 }
2726}
2727
2728
2729// The volume effect is used for automated tests only
2730#ifndef OPENSL_ES_H_
2731static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2732 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2733const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2734#endif //OPENSL_ES_H_
2735
Eric Laurentd8365c52017-07-16 15:27:05 -07002736/* static */
2737bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2738{
2739 // Only NS and AEC are suspended when BtNRec is off
2740 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2741 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2742 return true;
2743 }
2744 return false;
2745}
2746
Eric Laurentca7cc822012-11-19 14:55:58 -08002747bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2748{
2749 // auxiliary effects and visualizer are never suspended on output mix
2750 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2751 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2752 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002753 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2754 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002755 return false;
2756 }
2757 return true;
2758}
2759
2760void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2761 Vector< sp<AudioFlinger::EffectModule> > &effects)
2762{
2763 effects.clear();
2764 for (size_t i = 0; i < mEffects.size(); i++) {
2765 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2766 effects.add(mEffects[i]);
2767 }
2768 }
2769}
2770
2771sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2772 const effect_uuid_t *type)
2773{
2774 sp<EffectModule> effect = getEffectFromType_l(type);
2775 return effect != 0 && effect->isEnabled() ? effect : 0;
2776}
2777
2778void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2779 bool enabled)
2780{
2781 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2782 if (enabled) {
2783 if (index < 0) {
2784 // if the effect is not suspend check if all effects are suspended
2785 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2786 if (index < 0) {
2787 return;
2788 }
2789 if (!isEffectEligibleForSuspend(effect->desc())) {
2790 return;
2791 }
2792 setEffectSuspended_l(&effect->desc().type, enabled);
2793 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2794 if (index < 0) {
2795 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2796 return;
2797 }
2798 }
2799 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2800 effect->desc().type.timeLow);
2801 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002802 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002803 if (desc->mEffect == 0) {
2804 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002805 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002806 effect->setSuspended(true);
2807 }
2808 } else {
2809 if (index < 0) {
2810 return;
2811 }
2812 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2813 effect->desc().type.timeLow);
2814 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2815 desc->mEffect.clear();
2816 effect->setSuspended(false);
2817 }
2818}
2819
Eric Laurent5baf2af2013-09-12 17:37:00 -07002820bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002821{
2822 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002823 return isNonOffloadableEnabled_l();
2824}
2825
2826bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2827{
Eric Laurent813e2a72013-08-31 12:59:48 -07002828 size_t size = mEffects.size();
2829 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002830 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002831 return true;
2832 }
2833 }
2834 return false;
2835}
2836
Eric Laurentaaa44472014-09-12 17:41:50 -07002837void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2838{
2839 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002840 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002841}
2842
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002843void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2844{
2845 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2846 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2847 }
2848 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2849 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2850 }
2851}
2852
2853void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2854{
2855 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2856 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2857 }
2858 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2859 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2860 }
2861}
2862
2863bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002864{
2865 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002866 for (const auto &effect : mEffects) {
2867 if (effect->isProcessImplemented()) {
2868 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002869 }
2870 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002871 // Allow effects without processing.
2872 return true;
2873}
2874
2875bool AudioFlinger::EffectChain::isFastCompatible() const
2876{
2877 Mutex::Autolock _l(mLock);
2878 for (const auto &effect : mEffects) {
2879 if (effect->isProcessImplemented()
2880 && effect->isImplementationSoftware()) {
2881 return false;
2882 }
2883 }
2884 // Allow effects without processing or hw accelerated effects.
2885 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002886}
2887
2888// isCompatibleWithThread_l() must be called with thread->mLock held
2889bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2890{
2891 Mutex::Autolock _l(mLock);
2892 for (size_t i = 0; i < mEffects.size(); i++) {
2893 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2894 return false;
2895 }
2896 }
2897 return true;
2898}
2899
Eric Laurent6b446ce2019-12-13 10:56:31 -08002900// EffectCallbackInterface implementation
2901status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2902 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2903 sp<EffectHalInterface> *effect) {
2904 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002905 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002906 if (effectsFactory != 0) {
2907 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2908 }
2909 return status;
2910}
2911
2912bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002913 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002914 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002915 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002916}
2917
2918status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2919 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002920 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002921}
2922
2923status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2924 sp<EffectHalInterface> effect) {
2925 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002926 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002927 if (t == nullptr) {
2928 return result;
2929 }
2930 sp <StreamHalInterface> st = t->stream();
2931 if (st == nullptr) {
2932 return result;
2933 }
2934 result = st->addEffect(effect);
2935 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2936 return result;
2937}
2938
2939status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2940 sp<EffectHalInterface> effect) {
2941 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002942 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002943 if (t == nullptr) {
2944 return result;
2945 }
2946 sp <StreamHalInterface> st = t->stream();
2947 if (st == nullptr) {
2948 return result;
2949 }
2950 result = st->removeEffect(effect);
2951 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2952 return result;
2953}
2954
2955audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002956 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002957 if (t == nullptr) {
2958 return AUDIO_IO_HANDLE_NONE;
2959 }
2960 return t->id();
2961}
2962
2963bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002964 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002965 if (t == nullptr) {
2966 return true;
2967 }
2968 return t->isOutput();
2969}
2970
2971bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002972 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002973}
2974
2975bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002976 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002977}
2978
2979bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002980 switch (mThreadType) {
2981 case ThreadBase::OFFLOAD:
2982 case ThreadBase::MMAP_PLAYBACK:
2983 case ThreadBase::MMAP_CAPTURE:
2984 return true;
2985 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08002986 return false;
2987 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002988}
2989
2990bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
2991 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002992}
2993
2994uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002995 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002996 if (t == nullptr) {
2997 return 0;
2998 }
2999 return t->sampleRate();
3000}
3001
Eric Laurentf1f22e72021-07-13 14:04:14 +02003002audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3003 sp<ThreadBase> t = thread().promote();
3004 if (t == nullptr) {
3005 return AUDIO_CHANNEL_NONE;
3006 }
3007 sp<EffectChain> c = chain().promote();
3008 if (c == nullptr) {
3009 return AUDIO_CHANNEL_NONE;
3010 }
3011
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003012 if (mThreadType == ThreadBase::SPATIALIZER) {
3013 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3014 if (c->isFirstEffect(id)) {
3015 return t->mixerChannelMask();
3016 } else {
3017 return t->channelMask();
3018 }
3019 } else if (!audio_is_global_session(c->sessionId())) {
3020 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3021 return t->mixerChannelMask();
3022 } else {
3023 return t->channelMask();
3024 }
3025 } else {
3026 return t->channelMask();
3027 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003028 } else {
3029 return t->channelMask();
3030 }
3031}
3032
3033uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003034 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003035}
3036
3037audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003038 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003039 if (t == nullptr) {
3040 return AUDIO_CHANNEL_NONE;
3041 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003042 sp<EffectChain> c = chain().promote();
3043 if (c == nullptr) {
3044 return AUDIO_CHANNEL_NONE;
3045 }
3046
3047 if (mThreadType == ThreadBase::SPATIALIZER) {
3048 if (!audio_is_global_session(c->sessionId())) {
3049 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3050 return t->mixerChannelMask();
3051 } else {
3052 return t->channelMask();
3053 }
3054 } else {
3055 return t->channelMask();
3056 }
3057 } else {
3058 return t->channelMask();
3059 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003060}
3061
Eric Laurentf1f22e72021-07-13 14:04:14 +02003062uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003063 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003064}
3065
jiabineb3bda02020-06-30 14:07:03 -07003066audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003067 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003068 if (t == nullptr) {
3069 return AUDIO_CHANNEL_NONE;
3070 }
3071 return t->hapticChannelMask();
3072}
3073
Eric Laurent6b446ce2019-12-13 10:56:31 -08003074size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003075 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003076 if (t == nullptr) {
3077 return 0;
3078 }
3079 return t->frameCount();
3080}
3081
3082uint32_t AudioFlinger::EffectChain::EffectCallback::latency() 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 0;
3086 }
3087 return t->latency_l();
3088}
3089
3090void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003091 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003092 if (t == nullptr) {
3093 return;
3094 }
3095 t->setVolumeForOutput_l(left, right);
3096}
3097
3098void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003099 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003100 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003101 if (t == nullptr) {
3102 return;
3103 }
3104 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3105
Andy Hung328d6772021-01-12 12:32:21 -08003106 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003107 if (c == nullptr) {
3108 return;
3109 }
Eric Laurent41709552019-12-16 19:34:05 -08003110 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3111 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003112}
3113
Eric Laurent41709552019-12-16 19:34:05 -08003114void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003115 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003116 if (t == nullptr) {
3117 return;
3118 }
Eric Laurent41709552019-12-16 19:34:05 -08003119 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3120 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003121}
3122
Eric Laurent41709552019-12-16 19:34:05 -08003123void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003124 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3125
Andy Hung328d6772021-01-12 12:32:21 -08003126 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003127 if (t == nullptr) {
3128 return;
3129 }
3130 t->onEffectDisable();
3131}
3132
3133bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3134 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003135 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003136 if (t == nullptr) {
3137 return false;
3138 }
3139 t->disconnectEffectHandle(handle, unpinIfLast);
3140 return true;
3141}
3142
3143void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003144 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003145 if (c == nullptr) {
3146 return;
3147 }
3148 c->resetVolume_l();
3149
3150}
3151
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003152product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003153 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003154 if (c == nullptr) {
3155 return PRODUCT_STRATEGY_NONE;
3156 }
3157 return c->strategy();
3158}
3159
3160int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003161 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003162 if (c == nullptr) {
3163 return 0;
3164 }
3165 return c->activeTrackCnt();
3166}
3167
Eric Laurentb82e6b72019-11-22 17:25:04 -08003168
3169#undef LOG_TAG
3170#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3171
3172status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3173{
3174 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3175 Mutex::Autolock _l(mProxyLock);
3176 if (status == NO_ERROR) {
3177 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003178 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003179 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003180 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003181 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003182 bs = handle.second->disable(&status);
3183 }
3184 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003185 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003186 }
3187 }
3188 }
3189 ALOGV("%s enable %d status %d", __func__, enabled, status);
3190 return status;
3191}
3192
3193status_t AudioFlinger::DeviceEffectProxy::init(
3194 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3195//For all audio patches
3196//If src or sink device match
3197//If the effect is HW accelerated
3198// if no corresponding effect module
3199// Create EffectModule: mHalEffect
3200//Create and attach EffectHandle
3201//If the effect is not HW accelerated and the patch sink or src is a mixer port
3202// Create Effect on patch input or output thread on session -1
3203//Add EffectHandle to EffectHandle map of Effect Proxy:
3204 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3205 status_t status = NO_ERROR;
3206 for (auto &patch : patches) {
3207 status = onCreatePatch(patch.first, patch.second);
3208 ALOGV("%s onCreatePatch status %d", __func__, status);
3209 if (status == BAD_VALUE) {
3210 return status;
3211 }
3212 }
3213 return status;
3214}
3215
3216status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3217 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3218 status_t status = NAME_NOT_FOUND;
3219 sp<EffectHandle> handle;
3220 // only consider source[0] as this is the only "true" source of a patch
3221 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3222 ALOGV("%s source checkPort status %d", __func__, status);
3223 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3224 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3225 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3226 }
3227 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3228 Mutex::Autolock _l(mProxyLock);
3229 mEffectHandles.emplace(patchHandle, handle);
3230 }
3231 ALOGW_IF(status == BAD_VALUE,
3232 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3233
3234 return status;
3235}
3236
3237status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3238 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3239
3240 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3241 __func__, port->type, port->ext.device.type,
3242 port->ext.device.address, port->id, patch.isSoftware());
3243 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003244 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003245 return NAME_NOT_FOUND;
3246 }
3247 status_t status = NAME_NOT_FOUND;
3248
3249 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3250 Mutex::Autolock _l(mProxyLock);
3251 mDevicePort = *port;
3252 mHalEffect = new EffectModule(mMyCallback,
3253 const_cast<effect_descriptor_t *>(&mDescriptor),
3254 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3255 false /* pinned */, port->id);
3256 if (audio_is_input_device(mDevice.mType)) {
3257 mHalEffect->setInputDevice(mDevice);
3258 } else {
3259 mHalEffect->setDevices({mDevice});
3260 }
Eric Laurentde8caf42021-08-11 17:19:25 +02003261 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3262 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003263 status = (*handle)->initCheck();
3264 if (status == OK) {
3265 status = mHalEffect->addHandle((*handle).get());
3266 } else {
3267 mHalEffect.clear();
3268 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3269 }
3270 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3271 sp <ThreadBase> thread;
3272 if (audio_port_config_has_input_direction(port)) {
3273 if (patch.isSoftware()) {
3274 thread = patch.mRecord.thread();
3275 } else {
3276 thread = patch.thread().promote();
3277 }
3278 } else {
3279 if (patch.isSoftware()) {
3280 thread = patch.mPlayback.thread();
3281 } else {
3282 thread = patch.thread().promote();
3283 }
3284 }
3285 int enabled;
3286 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3287 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003288 &enabled, &status, false, false /*probe*/,
3289 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003290 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3291 } else {
3292 status = BAD_VALUE;
3293 }
3294 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003295 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003296 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003297 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003298 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003299 bs = (*handle)->disable(&status);
3300 }
3301 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003302 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003303 }
3304 }
3305 return status;
3306}
3307
3308void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3309 Mutex::Autolock _l(mProxyLock);
3310 mEffectHandles.erase(patchHandle);
3311}
3312
3313
3314size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3315{
3316 Mutex::Autolock _l(mProxyLock);
3317 if (effect == mHalEffect) {
3318 mHalEffect.clear();
3319 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3320 }
3321 return mHalEffect == nullptr ? 0 : 1;
3322}
3323
3324status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3325 sp<EffectHalInterface> effect) {
3326 if (mHalEffect == nullptr) {
3327 return NO_INIT;
3328 }
3329 return mManagerCallback->addEffectToHal(
3330 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3331}
3332
3333status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3334 sp<EffectHalInterface> effect) {
3335 if (mHalEffect == nullptr) {
3336 return NO_INIT;
3337 }
3338 return mManagerCallback->removeEffectFromHal(
3339 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3340}
3341
3342bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3343 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3344 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3345 }
3346 return true;
3347}
3348
3349uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3350 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3351 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3352 return mDevicePort.sample_rate;
3353 }
3354 return DEFAULT_OUTPUT_SAMPLE_RATE;
3355}
3356
3357audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3358 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3359 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3360 return mDevicePort.channel_mask;
3361 }
3362 return AUDIO_CHANNEL_OUT_STEREO;
3363}
3364
3365uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3366 if (isOutput()) {
3367 return audio_channel_count_from_out_mask(channelMask());
3368 }
3369 return audio_channel_count_from_in_mask(channelMask());
3370}
3371
3372void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3373 const Vector<String16> args;
3374 EffectBase::dump(fd, args);
3375
3376 const bool locked = dumpTryLock(mProxyLock);
3377 if (!locked) {
3378 String8 result("DeviceEffectProxy may be deadlocked\n");
3379 write(fd, result.string(), result.size());
3380 }
3381
3382 String8 outStr;
3383 if (mHalEffect != nullptr) {
3384 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3385 } else {
3386 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3387 }
3388 write(fd, outStr.string(), outStr.size());
3389 outStr.clear();
3390
3391 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3392 write(fd, outStr.string(), outStr.size());
3393 outStr.clear();
3394
3395 for (const auto& iter : mEffectHandles) {
3396 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3397 write(fd, outStr.string(), outStr.size());
3398 outStr.clear();
3399 sp<EffectBase> effect = iter.second->effect().promote();
3400 if (effect != nullptr) {
3401 effect->dump(fd, args);
3402 }
3403 }
3404
3405 if (locked) {
3406 mLock.unlock();
3407 }
3408}
3409
3410#undef LOG_TAG
3411#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3412
3413int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3414 return mManagerCallback->newEffectId();
3415}
3416
3417
3418bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3419 EffectHandle *handle, bool unpinIfLast) {
3420 sp<EffectBase> effectBase = handle->effect().promote();
3421 if (effectBase == nullptr) {
3422 return false;
3423 }
3424
3425 sp<EffectModule> effect = effectBase->asEffectModule();
3426 if (effect == nullptr) {
3427 return false;
3428 }
3429
3430 // restore suspended effects if the disconnected handle was enabled and the last one.
3431 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3432 if (remove) {
3433 sp<DeviceEffectProxy> proxy = mProxy.promote();
3434 if (proxy != nullptr) {
3435 proxy->removeEffect(effect);
3436 }
3437 if (handle->enabled()) {
3438 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3439 }
3440 }
3441 return true;
3442}
3443
3444status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3445 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3446 sp<EffectHalInterface> *effect) {
3447 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3448}
3449
3450status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3451 sp<EffectHalInterface> effect) {
3452 sp<DeviceEffectProxy> proxy = mProxy.promote();
3453 if (proxy == nullptr) {
3454 return NO_INIT;
3455 }
3456 return proxy->addEffectToHal(effect);
3457}
3458
3459status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3460 sp<EffectHalInterface> effect) {
3461 sp<DeviceEffectProxy> proxy = mProxy.promote();
3462 if (proxy == nullptr) {
3463 return NO_INIT;
3464 }
3465 return proxy->addEffectToHal(effect);
3466}
3467
3468bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3469 sp<DeviceEffectProxy> proxy = mProxy.promote();
3470 if (proxy == nullptr) {
3471 return true;
3472 }
3473 return proxy->isOutput();
3474}
3475
3476uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3477 sp<DeviceEffectProxy> proxy = mProxy.promote();
3478 if (proxy == nullptr) {
3479 return DEFAULT_OUTPUT_SAMPLE_RATE;
3480 }
3481 return proxy->sampleRate();
3482}
3483
Eric Laurentf1f22e72021-07-13 14:04:14 +02003484audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3485 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003486 sp<DeviceEffectProxy> proxy = mProxy.promote();
3487 if (proxy == nullptr) {
3488 return AUDIO_CHANNEL_OUT_STEREO;
3489 }
3490 return proxy->channelMask();
3491}
3492
Eric Laurentf1f22e72021-07-13 14:04:14 +02003493uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3494 sp<DeviceEffectProxy> proxy = mProxy.promote();
3495 if (proxy == nullptr) {
3496 return 2;
3497 }
3498 return proxy->channelCount();
3499}
3500
3501audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3502 sp<DeviceEffectProxy> proxy = mProxy.promote();
3503 if (proxy == nullptr) {
3504 return AUDIO_CHANNEL_OUT_STEREO;
3505 }
3506 return proxy->channelMask();
3507}
3508
3509uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003510 sp<DeviceEffectProxy> proxy = mProxy.promote();
3511 if (proxy == nullptr) {
3512 return 2;
3513 }
3514 return proxy->channelCount();
3515}
3516
Glenn Kasten63238ef2015-03-02 15:50:29 -08003517} // namespace android