blob: 865922fe0a325fe4e57dea56fb995d0cafddf1c4 [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 }
jiabin229f94d2022-08-23 16:37:30 -07001057
1058 if (isVolumeControl()) {
1059 // Force initializing the volume as 0 for volume control effect for safer ramping
1060 uint32_t left = 0;
1061 uint32_t right = 0;
1062 setVolumeInternal(&left, &right, true /*controller*/);
1063 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 }
1065
Andy Hung05083ac2017-12-14 15:00:28 -08001066 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1067 mMaxDisableWaitCnt = (uint32_t)std::max(
1068 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1069 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1070 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001071
Eric Laurentd0ebb532013-04-02 16:41:41 -07001072exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001073 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001074 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001075 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 return status;
1077}
1078
1079status_t AudioFlinger::EffectModule::init()
1080{
1081 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001082 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001083 return NO_INIT;
1084 }
1085 status_t cmdStatus;
1086 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001087 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1088 0,
1089 NULL,
1090 &size,
1091 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 if (status == 0) {
1093 status = cmdStatus;
1094 }
1095 return status;
1096}
1097
Eric Laurent1b928682014-10-02 19:41:47 -07001098void AudioFlinger::EffectModule::addEffectToHal_l()
1099{
1100 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1101 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001102 if (mAddedToHal) {
1103 return;
1104 }
1105
Andy Hungfda44002021-06-03 17:23:16 -07001106 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001107 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001108 }
1109}
1110
Eric Laurentfa1e1232016-08-02 19:01:49 -07001111// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001112status_t AudioFlinger::EffectModule::start()
1113{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001114 status_t status;
1115 {
1116 Mutex::Autolock _l(mLock);
1117 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001118 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001119 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001120 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001121 }
1122 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001123}
1124
1125status_t AudioFlinger::EffectModule::start_l()
1126{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001127 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 return NO_INIT;
1129 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001130 if (mStatus != NO_ERROR) {
1131 return mStatus;
1132 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 status_t cmdStatus;
1134 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001135 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1136 0,
1137 NULL,
1138 &size,
1139 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 if (status == 0) {
1141 status = cmdStatus;
1142 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001143 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001144 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 }
1146 return status;
1147}
1148
1149status_t AudioFlinger::EffectModule::stop()
1150{
1151 Mutex::Autolock _l(mLock);
1152 return stop_l();
1153}
1154
1155status_t AudioFlinger::EffectModule::stop_l()
1156{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001157 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001158 return NO_INIT;
1159 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001160 if (mStatus != NO_ERROR) {
1161 return mStatus;
1162 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001163 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001164 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001165
1166 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001167 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1168 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1169 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001170 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001171 mSetVolumeReentrantTid = INVALID_PID;
1172 }
1173
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001174 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1175 0,
1176 NULL,
1177 &size,
1178 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001179 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 status = cmdStatus;
1181 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001182 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001183 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001184 }
1185 return status;
1186}
1187
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001188// must be called with EffectChain::mLock held
1189void AudioFlinger::EffectModule::release_l()
1190{
1191 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001192 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001193 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001194 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001195 mEffectInterface.clear();
1196 }
1197}
1198
Eric Laurent6b446ce2019-12-13 10:56:31 -08001199status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001200{
1201 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1202 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001203 if (!mAddedToHal) {
1204 return NO_ERROR;
1205 }
1206
Andy Hungfda44002021-06-03 17:23:16 -07001207 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001208 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001209 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001210 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001211}
1212
Andy Hunge4a1d912016-08-17 14:11:13 -07001213// round up delta valid if value and divisor are positive.
1214template <typename T>
1215static T roundUpDelta(const T &value, const T &divisor) {
1216 T remainder = value % divisor;
1217 return remainder == 0 ? 0 : divisor - remainder;
1218}
1219
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001220status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1221 const std::vector<uint8_t>& cmdData,
1222 int32_t maxReplySize,
1223 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001224{
1225 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001226 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001227
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001228 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001229 return NO_INIT;
1230 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001231 if (mStatus != NO_ERROR) {
1232 return mStatus;
1233 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001234 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1235 return -EINVAL;
1236 }
1237 size_t cmdSize = cmdData.size();
1238 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1239 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1240 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001241 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001242 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001243 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001244 android_errorWriteLog(0x534e4554, "33003822");
1245 return -EINVAL;
1246 }
1247 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001248 (maxReplySize < sizeof(effect_param_t) ||
1249 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001250 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001251 return -EINVAL;
1252 }
ragoe2759072016-11-22 18:02:48 -08001253 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 (sizeof(effect_param_t) > maxReplySize
1255 || param->psize > maxReplySize - sizeof(effect_param_t)
1256 || param->vsize > maxReplySize - sizeof(effect_param_t)
1257 - param->psize
1258 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1259 maxReplySize
1260 - sizeof(effect_param_t)
1261 - param->psize
1262 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001263 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1264 android_errorWriteLog(0x534e4554, "32705438");
1265 return -EINVAL;
1266 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001267 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001268 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1269 && // DEFERRED not generally used
1270 (param == nullptr
1271 || param->psize > cmdSize - sizeof(effect_param_t)
1272 || param->vsize > cmdSize - sizeof(effect_param_t)
1273 - param->psize
1274 || roundUpDelta(param->psize,
1275 (uint32_t) sizeof(int)) >
1276 cmdSize
1277 - sizeof(effect_param_t)
1278 - param->psize
1279 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001280 android_errorWriteLog(0x534e4554, "30204301");
1281 return -EINVAL;
1282 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001283 uint32_t replySize = maxReplySize;
1284 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001285 status_t status = mEffectInterface->command(cmdCode,
1286 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001287 const_cast<uint8_t*>(cmdData.data()),
1288 &replySize,
1289 reply->data());
1290 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001292 for (size_t i = 1; i < mHandles.size(); i++) {
1293 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001294 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001295 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 }
1297 }
1298 }
1299 return status;
1300}
1301
Eric Laurentca7cc822012-11-19 14:55:58 -08001302bool AudioFlinger::EffectModule::isProcessEnabled() const
1303{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001304 if (mStatus != NO_ERROR) {
1305 return false;
1306 }
1307
Eric Laurentca7cc822012-11-19 14:55:58 -08001308 switch (mState) {
1309 case RESTART:
1310 case ACTIVE:
1311 case STOPPING:
1312 case STOPPED:
1313 return true;
1314 case IDLE:
1315 case STARTING:
1316 case DESTROYED:
1317 default:
1318 return false;
1319 }
1320}
1321
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001322bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1323{
Andy Hungfda44002021-06-03 17:23:16 -07001324 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001325}
1326
1327bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1328{
1329 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1330}
1331
Mikhail Naganov022b9952017-01-04 16:36:51 -08001332void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001333 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001334
1335 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001336 if (buffer != 0) {
1337 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1338 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1339 } else {
1340 mConfig.inputCfg.buffer.raw = NULL;
1341 }
1342 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001343 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001344
1345#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001346 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001347 // Theoretically insert effects can also do in-place conversions (destroying
1348 // the original buffer) when the output buffer is identical to the input buffer,
1349 // but we don't optimize for it here.
1350 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001351 const uint32_t inChannelCount =
1352 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1353 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001354 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001355 // we need to translate - create hidl shared buffer and intercept
1356 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001357 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1358 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1359 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001360
1361 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1362 __func__, inChannels, inFrameCount, size);
1363
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001364 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001365 || size > mInConversionBuffer->getSize())) {
1366 mInConversionBuffer.clear();
1367 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001368 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001369 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001370 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001371 mInConversionBuffer->setFrameCount(inFrameCount);
1372 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001373 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001374 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001375 }
1376 }
1377#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001378}
1379
1380void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001381 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001382
1383 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001384 if (buffer != 0) {
1385 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1386 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1387 } else {
1388 mConfig.outputCfg.buffer.raw = NULL;
1389 }
1390 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001391 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001392
1393#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001394 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001395 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001396 const uint32_t outChannelCount =
1397 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1398 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001399 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001400 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001401 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1402 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1403 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001404
1405 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1406 __func__, outChannels, outFrameCount, size);
1407
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001408 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001409 || size > mOutConversionBuffer->getSize())) {
1410 mOutConversionBuffer.clear();
1411 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001412 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001413 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001414 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001415 mOutConversionBuffer->setFrameCount(outFrameCount);
1416 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001417 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001418 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001419 }
1420 }
1421#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001422}
1423
Eric Laurentca7cc822012-11-19 14:55:58 -08001424status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1425{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001426 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001427 if (mStatus != NO_ERROR) {
1428 return mStatus;
1429 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001430 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1432 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1433 if (isProcessEnabled() &&
1434 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001435 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1436 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin229f94d2022-08-23 16:37:30 -07001437 status = setVolumeInternal(left, right, controller);
1438 }
1439 return status;
1440}
1441
1442status_t AudioFlinger::EffectModule::setVolumeInternal(
1443 uint32_t *left, uint32_t *right, bool controller) {
1444 uint32_t volume[2] = {*left, *right};
1445 uint32_t *pVolume = controller ? volume : nullptr;
1446 uint32_t size = sizeof(volume);
1447 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1448 size,
1449 volume,
1450 &size,
1451 pVolume);
1452 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1453 *left = volume[0];
1454 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001455 }
1456 return status;
1457}
1458
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001459void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1460{
Zhou Songd505c642020-02-20 16:35:37 +08001461 // for offload or direct thread, if the effect chain has non-offloadable
1462 // effect and any effect module within the chain has volume control, then
1463 // volume control is delegated to effect, otherwise, set volume to hal.
1464 if (mEffectCallback->isOffloadOrDirect() &&
1465 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001466 float vol_l = (float)left / (1 << 24);
1467 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001468 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001469 }
1470}
1471
jiabin8f278ee2019-11-11 12:16:27 -08001472status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1473 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001474{
jiabin8f278ee2019-11-11 12:16:27 -08001475 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1476 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 return NO_ERROR;
1478 }
1479
1480 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001481 if (mStatus != NO_ERROR) {
1482 return mStatus;
1483 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001485 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001486 status_t cmdStatus;
1487 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001488 // FIXME: use audio device types and addresses when the hal interface is ready.
1489 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001490 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001491 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001492 &size,
1493 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001494 }
1495 return status;
1496}
1497
jiabin8f278ee2019-11-11 12:16:27 -08001498status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1499{
1500 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1501}
1502
1503status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1504{
1505 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1506}
1507
Eric Laurentca7cc822012-11-19 14:55:58 -08001508status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1509{
1510 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001511 if (mStatus != NO_ERROR) {
1512 return mStatus;
1513 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001514 status_t status = NO_ERROR;
1515 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1516 status_t cmdStatus;
1517 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001518 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1519 sizeof(audio_mode_t),
1520 &mode,
1521 &size,
1522 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001523 if (status == NO_ERROR) {
1524 status = cmdStatus;
1525 }
1526 }
1527 return status;
1528}
1529
1530status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1531{
1532 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001533 if (mStatus != NO_ERROR) {
1534 return mStatus;
1535 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001536 status_t status = NO_ERROR;
1537 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1538 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001539 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1540 sizeof(audio_source_t),
1541 &source,
1542 &size,
1543 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001544 }
1545 return status;
1546}
1547
Eric Laurent5baf2af2013-09-12 17:37:00 -07001548status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1549{
1550 Mutex::Autolock _l(mLock);
1551 if (mStatus != NO_ERROR) {
1552 return mStatus;
1553 }
1554 status_t status = NO_ERROR;
1555 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1556 status_t cmdStatus;
1557 uint32_t size = sizeof(status_t);
1558 effect_offload_param_t cmd;
1559
1560 cmd.isOffload = offloaded;
1561 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001562 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1563 sizeof(effect_offload_param_t),
1564 &cmd,
1565 &size,
1566 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001567 if (status == NO_ERROR) {
1568 status = cmdStatus;
1569 }
1570 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1571 } else {
1572 if (offloaded) {
1573 status = INVALID_OPERATION;
1574 }
1575 mOffloaded = false;
1576 }
1577 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1578 return status;
1579}
1580
1581bool AudioFlinger::EffectModule::isOffloaded() const
1582{
1583 Mutex::Autolock _l(mLock);
1584 return mOffloaded;
1585}
1586
jiabineb3bda02020-06-30 14:07:03 -07001587/*static*/
1588bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1589 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1590}
1591
1592bool AudioFlinger::EffectModule::isHapticGenerator() const {
1593 return isHapticGenerator(&mDescriptor.type);
1594}
1595
jiabine70bc7f2020-06-30 22:07:55 -07001596status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1597{
1598 if (mStatus != NO_ERROR) {
1599 return mStatus;
1600 }
1601 if (!isHapticGenerator()) {
1602 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1603 return INVALID_OPERATION;
1604 }
1605
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001606 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1607 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001608 param->psize = sizeof(int32_t);
1609 param->vsize = sizeof(int32_t) * 2;
1610 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1611 *((int32_t*)param->data + 1) = id;
1612 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001613 std::vector<uint8_t> response;
1614 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001615 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001616 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1617 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001618 }
1619 return status;
1620}
1621
Lais Andradebc3f37a2021-07-02 00:13:19 +01001622status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001623{
1624 if (mStatus != NO_ERROR) {
1625 return mStatus;
1626 }
1627 if (!isHapticGenerator()) {
1628 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1629 return INVALID_OPERATION;
1630 }
1631
Lais Andradebc3f37a2021-07-02 00:13:19 +01001632 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001633 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001634 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001635 effect_param_t *param = (effect_param_t*) request.data();
1636 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001637 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001638 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1639 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001640 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1641 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1642 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001643 std::vector<uint8_t> response;
1644 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1645 if (status == NO_ERROR) {
1646 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1647 status = *reinterpret_cast<const status_t*>(response.data());
1648 }
1649 return status;
1650}
1651
Andy Hungbded9c82017-11-30 18:47:35 -08001652static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1653 std::stringstream ss;
1654
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001655 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001656 return "nullptr"; // make different than below
1657 } else if (buffer->externalData() != nullptr) {
1658 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1659 << " -> "
1660 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1661 } else {
1662 ss << buffer->audioBuffer()->raw;
1663 }
1664 return ss.str();
1665}
Marco Nelissenb2208842014-02-07 14:00:50 -08001666
Eric Laurent41709552019-12-16 19:34:05 -08001667void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001668{
Eric Laurent41709552019-12-16 19:34:05 -08001669 EffectBase::dump(fd, args);
1670
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001672 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001673
Eric Laurent41709552019-12-16 19:34:05 -08001674 result.append("\t\tStatus Engine:\n");
1675 result.appendFormat("\t\t%03d %p\n",
1676 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001677
1678 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001679
1680 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001681 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1682 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1683 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 mConfig.inputCfg.buffer.frameCount,
1685 mConfig.inputCfg.samplingRate,
1686 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001687 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001688 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001689
1690 result.append("\t\t- Output configuration:\n");
1691 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001692 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001693 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001694 mConfig.outputCfg.buffer.frameCount,
1695 mConfig.outputCfg.samplingRate,
1696 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001697 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001698 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001699
rago94a1ee82017-07-21 15:11:02 -07001700#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001701
Andy Hungbded9c82017-11-30 18:47:35 -08001702 result.appendFormat("\t\t- HAL buffers:\n"
1703 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1704 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1705 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1706 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1707 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001708#endif
1709
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 write(fd, result.string(), result.length());
1711
Mikhail Naganov4d547672019-02-22 14:19:19 -08001712 if (mEffectInterface != 0) {
1713 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1714 (void)mEffectInterface->dump(fd);
1715 }
1716
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 if (locked) {
1718 mLock.unlock();
1719 }
1720}
1721
1722// ----------------------------------------------------------------------------
1723// EffectHandle implementation
1724// ----------------------------------------------------------------------------
1725
1726#undef LOG_TAG
1727#define LOG_TAG "AudioFlinger::EffectHandle"
1728
Eric Laurent41709552019-12-16 19:34:05 -08001729AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001730 const sp<AudioFlinger::Client>& client,
1731 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001732 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 : BnEffect(),
1734 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001735 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1736 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001737{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001738 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001739
1740 if (client == 0) {
1741 return;
1742 }
1743 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1744 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001745 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001746 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001747 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001749 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001750 return;
1751 }
Glenn Kastene75da402013-11-20 13:54:52 -08001752 new(mCblk) effect_param_cblk_t();
1753 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001754}
1755
1756AudioFlinger::EffectHandle::~EffectHandle()
1757{
1758 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 disconnect(false);
1760}
1761
Glenn Kastene75da402013-11-20 13:54:52 -08001762status_t AudioFlinger::EffectHandle::initCheck()
1763{
1764 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1765}
1766
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001767#define RETURN(code) \
1768 *_aidl_return = (code); \
1769 return Status::ok();
1770
1771Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001772{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001773 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001774 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001775 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001776 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001777 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001778 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001780 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001781 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001782
1783 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001784 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001785 }
1786
1787 mEnabled = true;
1788
Eric Laurent6c796322019-04-09 14:13:17 -07001789 status_t status = effect->updatePolicyState();
1790 if (status != NO_ERROR) {
1791 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001792 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001793 }
1794
Eric Laurent6b446ce2019-12-13 10:56:31 -08001795 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001796
1797 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001798 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001799 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001800 }
1801
Eric Laurent6b446ce2019-12-13 10:56:31 -08001802 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001803 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001804 mEnabled = false;
1805 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001806 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001807}
1808
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001809Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001810{
1811 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001812 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001813 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001814 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001815 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001816 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001818 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001820
1821 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001822 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001823 }
1824 mEnabled = false;
1825
Eric Laurent6c796322019-04-09 14:13:17 -07001826 effect->updatePolicyState();
1827
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001828 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001829 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001830 }
1831
Eric Laurent6b446ce2019-12-13 10:56:31 -08001832 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001833 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001834}
1835
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001836Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001837{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001838 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001839 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001840 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001841}
1842
1843void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1844{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 AutoMutex _l(mLock);
1846 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1847 if (mDisconnected) {
1848 if (unpinIfLast) {
1849 android_errorWriteLog(0x534e4554, "32707507");
1850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 return;
1852 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 {
Eric Laurent41709552019-12-16 19:34:05 -08001855 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001856 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001857 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001858 ALOGW("%s Effect handle %p disconnected after thread destruction",
1859 __func__, this);
1860 }
1861 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001862 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001863 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001864
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 if (mClient != 0) {
1866 if (mCblk != NULL) {
1867 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1868 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1869 }
1870 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001871 // Client destructor must run with AudioFlinger client mutex locked
1872 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 mClient.clear();
1874 }
1875}
1876
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001877Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1878 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1879 return Status::ok();
1880}
1881
1882Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1883 const std::vector<uint8_t>& cmdData,
1884 int32_t maxResponseSize,
1885 std::vector<uint8_t>* response,
1886 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001887{
1888 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001889 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001890
Eric Laurentc7ab3092017-06-15 18:43:46 -07001891 // reject commands reserved for internal use by audio framework if coming from outside
1892 // of audioserver
1893 switch(cmdCode) {
1894 case EFFECT_CMD_ENABLE:
1895 case EFFECT_CMD_DISABLE:
1896 case EFFECT_CMD_SET_PARAM:
1897 case EFFECT_CMD_SET_PARAM_DEFERRED:
1898 case EFFECT_CMD_SET_PARAM_COMMIT:
1899 case EFFECT_CMD_GET_PARAM:
1900 break;
1901 default:
1902 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1903 break;
1904 }
1905 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001907 }
1908
Eric Laurent1ffc5852016-12-15 14:46:09 -08001909 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001911 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001913 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001914 writeToBuffer(NO_ERROR, response);
1915 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001916 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001917 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001918 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001919 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001920 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 writeToBuffer(NO_ERROR, response);
1922 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001923 }
1924
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001925 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001926 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001927 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001928 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001929 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001930 // only get parameter command is permitted for applications not controlling the effect
1931 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001932 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001934
1935 // handle commands that are not forwarded transparently to effect engine
1936 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001937 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001938 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001939 }
1940
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001941 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001942 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001943 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001944 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001945 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001946
Eric Laurentca7cc822012-11-19 14:55:58 -08001947 // No need to trylock() here as this function is executed in the binder thread serving a
1948 // particular client process: no risk to block the whole media server process or mixer
1949 // threads if we are stuck here
1950 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001951 // keep local copy of index in case of client corruption b/32220769
1952 const uint32_t clientIndex = mCblk->clientIndex;
1953 const uint32_t serverIndex = mCblk->serverIndex;
1954 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1955 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001956 mCblk->serverIndex = 0;
1957 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001959 }
1960 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001961 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001962 for (uint32_t index = serverIndex; index < clientIndex;) {
1963 int *p = (int *)(mBuffer + index);
1964 const int size = *p++;
1965 if (size < 0
1966 || size > EFFECT_PARAM_BUFFER_SIZE
1967 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001969 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001970 break;
1971 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001972
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001973 std::copy(reinterpret_cast<const uint8_t*>(p),
1974 reinterpret_cast<const uint8_t*>(p) + size,
1975 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001976
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001977 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001978 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001979 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001980 sizeof(int),
1981 &replyBuffer);
1982 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001983
1984 // verify shared memory: server index shouldn't change; client index can't go back.
1985 if (serverIndex != mCblk->serverIndex
1986 || clientIndex > mCblk->clientIndex) {
1987 android_errorWriteLog(0x534e4554, "32220769");
1988 status = BAD_VALUE;
1989 break;
1990 }
1991
Eric Laurentca7cc822012-11-19 14:55:58 -08001992 // stop at first error encountered
1993 if (ret != NO_ERROR) {
1994 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001996 break;
1997 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001998 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001999 break;
2000 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002001 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002002 }
2003 mCblk->serverIndex = 0;
2004 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002005 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002006 }
2007
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 status_t status = effect->command(cmdCode,
2009 cmdData,
2010 maxResponseSize,
2011 response);
2012 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002013}
2014
2015void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2016{
2017 ALOGV("setControl %p control %d", this, hasControl);
2018
2019 mHasControl = hasControl;
2020 mEnabled = enabled;
2021
2022 if (signal && mEffectClient != 0) {
2023 mEffectClient->controlStatusChanged(hasControl);
2024 }
2025}
2026
2027void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002028 const std::vector<uint8_t>& cmdData,
2029 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002030{
2031 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002032 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002033 }
2034}
2035
2036
2037
2038void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2039{
2040 if (mEffectClient != 0) {
2041 mEffectClient->enableStatusChanged(enabled);
2042 }
2043}
2044
Eric Laurentde8caf42021-08-11 17:19:25 +02002045void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2046{
2047 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2048 mEffectClient->framesProcessed(frames);
2049 }
2050}
2051
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002052void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002053{
2054 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2055
Marco Nelissenb2208842014-02-07 14:00:50 -08002056 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002057 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002058 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002059 mHasControl ? "yes" : "no",
2060 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 mCblk ? mCblk->clientIndex : 0,
2062 mCblk ? mCblk->serverIndex : 0
2063 );
2064
2065 if (locked) {
2066 mCblk->lock.unlock();
2067 }
2068}
2069
2070#undef LOG_TAG
2071#define LOG_TAG "AudioFlinger::EffectChain"
2072
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002073AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2074 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002075 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002076 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002077 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002078 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002079{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002080 sp<ThreadBase> p = thread.promote();
2081 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002082 return;
2083 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002084 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002085 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2086 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002087}
2088
2089AudioFlinger::EffectChain::~EffectChain()
2090{
Eric Laurentca7cc822012-11-19 14:55:58 -08002091}
2092
2093// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2094sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2095 effect_descriptor_t *descriptor)
2096{
2097 size_t size = mEffects.size();
2098
2099 for (size_t i = 0; i < size; i++) {
2100 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2101 return mEffects[i];
2102 }
2103 }
2104 return 0;
2105}
2106
2107// getEffectFromId_l() must be called with ThreadBase::mLock held
2108sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2109{
2110 size_t size = mEffects.size();
2111
2112 for (size_t i = 0; i < size; i++) {
2113 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2114 if (id == 0 || mEffects[i]->id() == id) {
2115 return mEffects[i];
2116 }
2117 }
2118 return 0;
2119}
2120
2121// getEffectFromType_l() must be called with ThreadBase::mLock held
2122sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2123 const effect_uuid_t *type)
2124{
2125 size_t size = mEffects.size();
2126
2127 for (size_t i = 0; i < size; i++) {
2128 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2129 return mEffects[i];
2130 }
2131 }
2132 return 0;
2133}
2134
Eric Laurent6c796322019-04-09 14:13:17 -07002135std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2136{
2137 std::vector<int> ids;
2138 Mutex::Autolock _l(mLock);
2139 for (size_t i = 0; i < mEffects.size(); i++) {
2140 ids.push_back(mEffects[i]->id());
2141 }
2142 return ids;
2143}
2144
Eric Laurentca7cc822012-11-19 14:55:58 -08002145void AudioFlinger::EffectChain::clearInputBuffer()
2146{
2147 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002148 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002149}
2150
2151// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002152void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002153{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002154 if (mInBuffer == NULL) {
2155 return;
2156 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002157 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2158 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002159
Eric Laurent6b446ce2019-12-13 10:56:31 -08002160 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002161 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002162}
2163
2164// Must be called with EffectChain::mLock locked
2165void AudioFlinger::EffectChain::process_l()
2166{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002167 // never process effects when:
2168 // - on an OFFLOAD thread
2169 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002170 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002171 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002172 bool tracksOnSession = (trackCnt() != 0);
2173
2174 if (!tracksOnSession && mTailBufferCount == 0) {
2175 doProcess = false;
2176 }
2177
2178 if (activeTrackCnt() == 0) {
2179 // if no track is active and the effect tail has not been rendered,
2180 // the input buffer must be cleared here as the mixer process will not do it
2181 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002182 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002183 if (mTailBufferCount > 0) {
2184 mTailBufferCount--;
2185 }
2186 }
2187 }
2188 }
2189
2190 size_t size = mEffects.size();
2191 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002192 // Only the input and output buffers of the chain can be external,
2193 // and 'update' / 'commit' do nothing for allocated buffers, thus
2194 // it's not needed to consider any other buffers here.
2195 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002196 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2197 mOutBuffer->update();
2198 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002199 for (size_t i = 0; i < size; i++) {
2200 mEffects[i]->process();
2201 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002202 mInBuffer->commit();
2203 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2204 mOutBuffer->commit();
2205 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002206 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002207 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002208 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002209 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2210 }
2211 if (doResetVolume) {
2212 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002213 }
2214}
2215
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002216// createEffect_l() must be called with ThreadBase::mLock held
2217status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002218 effect_descriptor_t *desc,
2219 int id,
2220 audio_session_t sessionId,
2221 bool pinned)
2222{
2223 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002224 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002225 status_t lStatus = effect->status();
2226 if (lStatus == NO_ERROR) {
2227 lStatus = addEffect_ll(effect);
2228 }
2229 if (lStatus != NO_ERROR) {
2230 effect.clear();
2231 }
2232 return lStatus;
2233}
2234
2235// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002236status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2237{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002238 Mutex::Autolock _l(mLock);
2239 return addEffect_ll(effect);
2240}
2241// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2242status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2243{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002244 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002245
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002246 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002247 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2248 // Auxiliary effects are inserted at the beginning of mEffects vector as
2249 // they are processed first and accumulated in chain input buffer
2250 mEffects.insertAt(effect, 0);
2251
2252 // the input buffer for auxiliary effect contains mono samples in
2253 // 32 bit format. This is to avoid saturation in AudoMixer
2254 // accumulation stage. Saturation is done in EffectModule::process() before
2255 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002256 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002257 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002258#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002259 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002260 numSamples * sizeof(float), &halBuffer);
2261#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002262 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002263 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002264#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002265 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002266
2267 effect->configure();
2268
Mikhail Naganov022b9952017-01-04 16:36:51 -08002269 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002270 // auxiliary effects output samples to chain input buffer for further processing
2271 // by insert effects
2272 effect->setOutBuffer(mInBuffer);
2273 } else {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002274 ssize_t idx_insert = getInsertIndex(desc);
2275 if (idx_insert < 0) {
2276 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002277 }
2278
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002279 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002280 mEffects.insertAt(effect, idx_insert);
2281
2282 effect->configure();
2283
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002284 // - By default:
2285 // All effects read samples from chain input buffer.
2286 // The last effect in the chain, writes samples to chain output buffer,
2287 // otherwise to chain input buffer
2288 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2289 // The spatializer effect (first effect) reads samples from the input buffer
2290 // and writes samples to the output buffer.
2291 // All other effects read and writes samples to the output buffer
2292 if (mEffectCallback->isSpatializer()
2293 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002294 effect->setOutBuffer(mOutBuffer);
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002295 if (idx_insert == 0) {
2296 if (previousSize != 0) {
2297 mEffects[1]->configure();
2298 mEffects[1]->setInBuffer(mOutBuffer);
2299 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2300 }
2301 effect->setInBuffer(mInBuffer);
2302 } else {
2303 effect->setInBuffer(mOutBuffer);
2304 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002305 } else {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002306 effect->setInBuffer(mInBuffer);
2307 if (idx_insert == previousSize) {
2308 if (idx_insert != 0) {
2309 mEffects[idx_insert-1]->configure();
2310 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2311 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2312 }
2313 effect->setOutBuffer(mOutBuffer);
2314 } else {
2315 effect->setOutBuffer(mInBuffer);
2316 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002317 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002318 ALOGV("%s effect %p, added in chain %p at rank %zu",
2319 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002320 }
2321 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002322
Eric Laurentca7cc822012-11-19 14:55:58 -08002323 return NO_ERROR;
2324}
2325
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002326ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2327 // Insert effects are inserted at the end of mEffects vector as they are processed
2328 // after track and auxiliary effects.
2329 // Insert effect order as a function of indicated preference:
2330 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2331 // another effect is present
2332 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2333 // last effect claiming first position
2334 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2335 // first effect claiming last position
2336 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2337 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2338 // already present
2339 // Spatializer or Downmixer effects are inserted in first position because
2340 // they adapt the channel count for all other effects in the chain
2341 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2342 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2343 return 0;
2344 }
2345
2346 size_t size = mEffects.size();
2347 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2348 ssize_t idx_insert;
2349 ssize_t idx_insert_first = -1;
2350 ssize_t idx_insert_last = -1;
2351
2352 idx_insert = size;
2353 for (size_t i = 0; i < size; i++) {
2354 effect_descriptor_t d = mEffects[i]->desc();
2355 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2356 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2357 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2358 // check invalid effect chaining combinations
2359 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2360 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2361 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2362 __func__, desc.name, d.name);
2363 return -1;
2364 }
2365 // remember position of first insert effect and by default
2366 // select this as insert position for new effect
2367 if (idx_insert == size) {
2368 idx_insert = i;
2369 }
2370 // remember position of last insert effect claiming
2371 // first position
2372 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2373 idx_insert_first = i;
2374 }
2375 // remember position of first insert effect claiming
2376 // last position
2377 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2378 idx_insert_last == -1) {
2379 idx_insert_last = i;
2380 }
2381 }
2382 }
2383
2384 // modify idx_insert from first position if needed
2385 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2386 if (idx_insert_last != -1) {
2387 idx_insert = idx_insert_last;
2388 } else {
2389 idx_insert = size;
2390 }
2391 } else {
2392 if (idx_insert_first != -1) {
2393 idx_insert = idx_insert_first + 1;
2394 }
2395 }
2396 return idx_insert;
2397}
2398
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002399// removeEffect_l() must be called with ThreadBase::mLock held
2400size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2401 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002402{
2403 Mutex::Autolock _l(mLock);
2404 size_t size = mEffects.size();
2405 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2406
2407 for (size_t i = 0; i < size; i++) {
2408 if (effect == mEffects[i]) {
2409 // calling stop here will remove pre-processing effect from the audio HAL.
2410 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2411 // the middle of a read from audio HAL
2412 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2413 mEffects[i]->state() == EffectModule::STOPPING) {
2414 mEffects[i]->stop();
2415 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002416 if (release) {
2417 mEffects[i]->release_l();
2418 }
2419
Mikhail Naganov022b9952017-01-04 16:36:51 -08002420 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002421 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002422 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002423 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002424 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002425 }
2426 }
2427 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002428
2429 // make sure the input buffer configuration for the new first effect in the chain
2430 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2431 if (i == 0 && size > 1) {
2432 mEffects[0]->configure();
2433 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002434 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002435 }
2436
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002437 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002438 this, i);
2439 break;
2440 }
2441 }
2442
2443 return mEffects.size();
2444}
2445
jiabin8f278ee2019-11-11 12:16:27 -08002446// setDevices_l() must be called with ThreadBase::mLock held
2447void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002448{
2449 size_t size = mEffects.size();
2450 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002451 mEffects[i]->setDevices(devices);
2452 }
2453}
2454
2455// setInputDevice_l() must be called with ThreadBase::mLock held
2456void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2457{
2458 size_t size = mEffects.size();
2459 for (size_t i = 0; i < size; i++) {
2460 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002461 }
2462}
2463
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002464// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002465void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2466{
2467 size_t size = mEffects.size();
2468 for (size_t i = 0; i < size; i++) {
2469 mEffects[i]->setMode(mode);
2470 }
2471}
2472
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002473// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002474void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2475{
2476 size_t size = mEffects.size();
2477 for (size_t i = 0; i < size; i++) {
2478 mEffects[i]->setAudioSource(source);
2479 }
2480}
2481
Zhou Songd505c642020-02-20 16:35:37 +08002482bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2483 for (const auto &effect : mEffects) {
2484 if (effect->isVolumeControlEnabled()) return true;
2485 }
2486 return false;
2487}
2488
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002489// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002490bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002491{
2492 uint32_t newLeft = *left;
2493 uint32_t newRight = *right;
2494 bool hasControl = false;
2495 int ctrlIdx = -1;
2496 size_t size = mEffects.size();
2497
2498 // first update volume controller
2499 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002500 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002501 ctrlIdx = i - 1;
2502 hasControl = true;
2503 break;
2504 }
2505 }
2506
Eric Laurentfa1e1232016-08-02 19:01:49 -07002507 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002508 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002509 if (hasControl) {
2510 *left = mNewLeftVolume;
2511 *right = mNewRightVolume;
2512 }
2513 return hasControl;
2514 }
2515
2516 mVolumeCtrlIdx = ctrlIdx;
2517 mLeftVolume = newLeft;
2518 mRightVolume = newRight;
2519
2520 // second get volume update from volume controller
2521 if (ctrlIdx >= 0) {
2522 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2523 mNewLeftVolume = newLeft;
2524 mNewRightVolume = newRight;
2525 }
2526 // then indicate volume to all other effects in chain.
2527 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002528 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002529 uint32_t lVol = newLeft;
2530 uint32_t rVol = newRight;
2531
2532 for (size_t i = 0; i < size; i++) {
2533 if ((int)i == ctrlIdx) {
2534 continue;
2535 }
2536 // this also works for ctrlIdx == -1 when there is no volume controller
2537 if ((int)i > ctrlIdx) {
2538 lVol = *left;
2539 rVol = *right;
2540 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002541 // Pass requested volume directly if this is volume monitor module
2542 if (mEffects[i]->isVolumeMonitor()) {
2543 mEffects[i]->setVolume(left, right, false);
2544 } else {
2545 mEffects[i]->setVolume(&lVol, &rVol, false);
2546 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002547 }
2548 *left = newLeft;
2549 *right = newRight;
2550
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002551 setVolumeForOutput_l(*left, *right);
2552
Eric Laurentca7cc822012-11-19 14:55:58 -08002553 return hasControl;
2554}
2555
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002556// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002557void AudioFlinger::EffectChain::resetVolume_l()
2558{
Eric Laurente7449bf2016-08-03 18:44:07 -07002559 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2560 uint32_t left = mLeftVolume;
2561 uint32_t right = mRightVolume;
2562 (void)setVolume_l(&left, &right, true);
2563 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002564}
2565
jiabineb3bda02020-06-30 14:07:03 -07002566// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2567bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2568{
2569 for (size_t i = 0; i < mEffects.size(); ++i) {
2570 if (mEffects[i]->isHapticGenerator()) {
2571 return true;
2572 }
2573 }
2574 return false;
2575}
2576
jiabine70bc7f2020-06-30 22:07:55 -07002577void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2578{
2579 Mutex::Autolock _l(mLock);
2580 for (size_t i = 0; i < mEffects.size(); ++i) {
2581 mEffects[i]->setHapticIntensity(id, intensity);
2582 }
2583}
2584
Eric Laurent1b928682014-10-02 19:41:47 -07002585void AudioFlinger::EffectChain::syncHalEffectsState()
2586{
2587 Mutex::Autolock _l(mLock);
2588 for (size_t i = 0; i < mEffects.size(); i++) {
2589 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2590 mEffects[i]->state() == EffectModule::STOPPING) {
2591 mEffects[i]->addEffectToHal_l();
2592 }
2593 }
2594}
2595
Eric Laurentca7cc822012-11-19 14:55:58 -08002596void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2597{
Eric Laurentca7cc822012-11-19 14:55:58 -08002598 String8 result;
2599
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002600 const size_t numEffects = mEffects.size();
2601 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002602
Marco Nelissenb2208842014-02-07 14:00:50 -08002603 if (numEffects) {
2604 bool locked = AudioFlinger::dumpTryLock(mLock);
2605 // failed to lock - AudioFlinger is probably deadlocked
2606 if (!locked) {
2607 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002608 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002609
Andy Hungbded9c82017-11-30 18:47:35 -08002610 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2611 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2612 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2613 (int)inBufferStr.size(), "In buffer ",
2614 (int)outBufferStr.size(), "Out buffer ");
2615 result.appendFormat("\t%s %s %d\n",
2616 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002617 write(fd, result.string(), result.size());
2618
2619 for (size_t i = 0; i < numEffects; ++i) {
2620 sp<EffectModule> effect = mEffects[i];
2621 if (effect != 0) {
2622 effect->dump(fd, args);
2623 }
2624 }
2625
2626 if (locked) {
2627 mLock.unlock();
2628 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002629 } else {
2630 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002631 }
2632}
2633
2634// must be called with ThreadBase::mLock held
2635void AudioFlinger::EffectChain::setEffectSuspended_l(
2636 const effect_uuid_t *type, bool suspend)
2637{
2638 sp<SuspendedEffectDesc> desc;
2639 // use effect type UUID timelow as key as there is no real risk of identical
2640 // timeLow fields among effect type UUIDs.
2641 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2642 if (suspend) {
2643 if (index >= 0) {
2644 desc = mSuspendedEffects.valueAt(index);
2645 } else {
2646 desc = new SuspendedEffectDesc();
2647 desc->mType = *type;
2648 mSuspendedEffects.add(type->timeLow, desc);
2649 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2650 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002651
Eric Laurentca7cc822012-11-19 14:55:58 -08002652 if (desc->mRefCount++ == 0) {
2653 sp<EffectModule> effect = getEffectIfEnabled(type);
2654 if (effect != 0) {
2655 desc->mEffect = effect;
2656 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002657 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002658 }
2659 }
2660 } else {
2661 if (index < 0) {
2662 return;
2663 }
2664 desc = mSuspendedEffects.valueAt(index);
2665 if (desc->mRefCount <= 0) {
2666 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002667 desc->mRefCount = 0;
2668 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002669 }
2670 if (--desc->mRefCount == 0) {
2671 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2672 if (desc->mEffect != 0) {
2673 sp<EffectModule> effect = desc->mEffect.promote();
2674 if (effect != 0) {
2675 effect->setSuspended(false);
2676 effect->lock();
2677 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002678 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002679 effect->setEnabled_l(handle->enabled());
2680 }
2681 effect->unlock();
2682 }
2683 desc->mEffect.clear();
2684 }
2685 mSuspendedEffects.removeItemsAt(index);
2686 }
2687 }
2688}
2689
2690// must be called with ThreadBase::mLock held
2691void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2692{
2693 sp<SuspendedEffectDesc> desc;
2694
2695 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2696 if (suspend) {
2697 if (index >= 0) {
2698 desc = mSuspendedEffects.valueAt(index);
2699 } else {
2700 desc = new SuspendedEffectDesc();
2701 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2702 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2703 }
2704 if (desc->mRefCount++ == 0) {
2705 Vector< sp<EffectModule> > effects;
2706 getSuspendEligibleEffects(effects);
2707 for (size_t i = 0; i < effects.size(); i++) {
2708 setEffectSuspended_l(&effects[i]->desc().type, true);
2709 }
2710 }
2711 } else {
2712 if (index < 0) {
2713 return;
2714 }
2715 desc = mSuspendedEffects.valueAt(index);
2716 if (desc->mRefCount <= 0) {
2717 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2718 desc->mRefCount = 1;
2719 }
2720 if (--desc->mRefCount == 0) {
2721 Vector<const effect_uuid_t *> types;
2722 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2723 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2724 continue;
2725 }
2726 types.add(&mSuspendedEffects.valueAt(i)->mType);
2727 }
2728 for (size_t i = 0; i < types.size(); i++) {
2729 setEffectSuspended_l(types[i], false);
2730 }
2731 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2732 mSuspendedEffects.keyAt(index));
2733 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2734 }
2735 }
2736}
2737
2738
2739// The volume effect is used for automated tests only
2740#ifndef OPENSL_ES_H_
2741static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2742 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2743const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2744#endif //OPENSL_ES_H_
2745
Eric Laurentd8365c52017-07-16 15:27:05 -07002746/* static */
2747bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2748{
2749 // Only NS and AEC are suspended when BtNRec is off
2750 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2751 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2752 return true;
2753 }
2754 return false;
2755}
2756
Eric Laurentca7cc822012-11-19 14:55:58 -08002757bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2758{
2759 // auxiliary effects and visualizer are never suspended on output mix
2760 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2761 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2762 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002763 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2764 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002765 return false;
2766 }
2767 return true;
2768}
2769
2770void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2771 Vector< sp<AudioFlinger::EffectModule> > &effects)
2772{
2773 effects.clear();
2774 for (size_t i = 0; i < mEffects.size(); i++) {
2775 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2776 effects.add(mEffects[i]);
2777 }
2778 }
2779}
2780
2781sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2782 const effect_uuid_t *type)
2783{
2784 sp<EffectModule> effect = getEffectFromType_l(type);
2785 return effect != 0 && effect->isEnabled() ? effect : 0;
2786}
2787
2788void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2789 bool enabled)
2790{
2791 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2792 if (enabled) {
2793 if (index < 0) {
2794 // if the effect is not suspend check if all effects are suspended
2795 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2796 if (index < 0) {
2797 return;
2798 }
2799 if (!isEffectEligibleForSuspend(effect->desc())) {
2800 return;
2801 }
2802 setEffectSuspended_l(&effect->desc().type, enabled);
2803 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2804 if (index < 0) {
2805 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2806 return;
2807 }
2808 }
2809 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2810 effect->desc().type.timeLow);
2811 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002812 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002813 if (desc->mEffect == 0) {
2814 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002815 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002816 effect->setSuspended(true);
2817 }
2818 } else {
2819 if (index < 0) {
2820 return;
2821 }
2822 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2823 effect->desc().type.timeLow);
2824 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2825 desc->mEffect.clear();
2826 effect->setSuspended(false);
2827 }
2828}
2829
Eric Laurent5baf2af2013-09-12 17:37:00 -07002830bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002831{
2832 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002833 return isNonOffloadableEnabled_l();
2834}
2835
2836bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2837{
Eric Laurent813e2a72013-08-31 12:59:48 -07002838 size_t size = mEffects.size();
2839 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002840 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002841 return true;
2842 }
2843 }
2844 return false;
2845}
2846
Eric Laurentaaa44472014-09-12 17:41:50 -07002847void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2848{
2849 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002850 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002851}
2852
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002853void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2854{
2855 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2856 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2857 }
2858 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2859 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2860 }
2861}
2862
2863void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2864{
2865 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2866 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2867 }
2868 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2869 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2870 }
2871}
2872
2873bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002874{
2875 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002876 for (const auto &effect : mEffects) {
2877 if (effect->isProcessImplemented()) {
2878 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002879 }
2880 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002881 // Allow effects without processing.
2882 return true;
2883}
2884
2885bool AudioFlinger::EffectChain::isFastCompatible() const
2886{
2887 Mutex::Autolock _l(mLock);
2888 for (const auto &effect : mEffects) {
2889 if (effect->isProcessImplemented()
2890 && effect->isImplementationSoftware()) {
2891 return false;
2892 }
2893 }
2894 // Allow effects without processing or hw accelerated effects.
2895 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002896}
2897
2898// isCompatibleWithThread_l() must be called with thread->mLock held
2899bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2900{
2901 Mutex::Autolock _l(mLock);
2902 for (size_t i = 0; i < mEffects.size(); i++) {
2903 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2904 return false;
2905 }
2906 }
2907 return true;
2908}
2909
Eric Laurent6b446ce2019-12-13 10:56:31 -08002910// EffectCallbackInterface implementation
2911status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2912 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2913 sp<EffectHalInterface> *effect) {
2914 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002915 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002916 if (effectsFactory != 0) {
2917 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2918 }
2919 return status;
2920}
2921
2922bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002923 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002924 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002925 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002926}
2927
2928status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2929 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002930 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002931}
2932
2933status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2934 sp<EffectHalInterface> effect) {
2935 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002936 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002937 if (t == nullptr) {
2938 return result;
2939 }
2940 sp <StreamHalInterface> st = t->stream();
2941 if (st == nullptr) {
2942 return result;
2943 }
2944 result = st->addEffect(effect);
2945 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2946 return result;
2947}
2948
2949status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2950 sp<EffectHalInterface> effect) {
2951 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002952 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002953 if (t == nullptr) {
2954 return result;
2955 }
2956 sp <StreamHalInterface> st = t->stream();
2957 if (st == nullptr) {
2958 return result;
2959 }
2960 result = st->removeEffect(effect);
2961 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2962 return result;
2963}
2964
2965audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002966 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002967 if (t == nullptr) {
2968 return AUDIO_IO_HANDLE_NONE;
2969 }
2970 return t->id();
2971}
2972
2973bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002974 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002975 if (t == nullptr) {
2976 return true;
2977 }
2978 return t->isOutput();
2979}
2980
2981bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002982 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002983}
2984
2985bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002986 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002987}
2988
2989bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002990 switch (mThreadType) {
2991 case ThreadBase::OFFLOAD:
2992 case ThreadBase::MMAP_PLAYBACK:
2993 case ThreadBase::MMAP_CAPTURE:
2994 return true;
2995 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08002996 return false;
2997 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02002998}
2999
3000bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3001 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003002}
3003
3004uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003005 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003006 if (t == nullptr) {
3007 return 0;
3008 }
3009 return t->sampleRate();
3010}
3011
Eric Laurentf1f22e72021-07-13 14:04:14 +02003012audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3013 sp<ThreadBase> t = thread().promote();
3014 if (t == nullptr) {
3015 return AUDIO_CHANNEL_NONE;
3016 }
3017 sp<EffectChain> c = chain().promote();
3018 if (c == nullptr) {
3019 return AUDIO_CHANNEL_NONE;
3020 }
3021
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003022 if (mThreadType == ThreadBase::SPATIALIZER) {
3023 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3024 if (c->isFirstEffect(id)) {
3025 return t->mixerChannelMask();
3026 } else {
3027 return t->channelMask();
3028 }
3029 } else if (!audio_is_global_session(c->sessionId())) {
3030 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3031 return t->mixerChannelMask();
3032 } else {
3033 return t->channelMask();
3034 }
3035 } else {
3036 return t->channelMask();
3037 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003038 } else {
3039 return t->channelMask();
3040 }
3041}
3042
3043uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003044 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003045}
3046
3047audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003048 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (t == nullptr) {
3050 return AUDIO_CHANNEL_NONE;
3051 }
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003052 sp<EffectChain> c = chain().promote();
3053 if (c == nullptr) {
3054 return AUDIO_CHANNEL_NONE;
3055 }
3056
3057 if (mThreadType == ThreadBase::SPATIALIZER) {
3058 if (!audio_is_global_session(c->sessionId())) {
3059 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3060 return t->mixerChannelMask();
3061 } else {
3062 return t->channelMask();
3063 }
3064 } else {
3065 return t->channelMask();
3066 }
3067 } else {
3068 return t->channelMask();
3069 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003070}
3071
Eric Laurentf1f22e72021-07-13 14:04:14 +02003072uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurent0dccd2e2021-10-26 17:40:18 +02003073 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003074}
3075
jiabineb3bda02020-06-30 14:07:03 -07003076audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003077 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003078 if (t == nullptr) {
3079 return AUDIO_CHANNEL_NONE;
3080 }
3081 return t->hapticChannelMask();
3082}
3083
Eric Laurent6b446ce2019-12-13 10:56:31 -08003084size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003085 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003086 if (t == nullptr) {
3087 return 0;
3088 }
3089 return t->frameCount();
3090}
3091
3092uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003093 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003094 if (t == nullptr) {
3095 return 0;
3096 }
3097 return t->latency_l();
3098}
3099
3100void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003101 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003102 if (t == nullptr) {
3103 return;
3104 }
3105 t->setVolumeForOutput_l(left, right);
3106}
3107
3108void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003109 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003110 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003111 if (t == nullptr) {
3112 return;
3113 }
3114 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3115
Andy Hung328d6772021-01-12 12:32:21 -08003116 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003117 if (c == nullptr) {
3118 return;
3119 }
Eric Laurent41709552019-12-16 19:34:05 -08003120 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3121 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003122}
3123
Eric Laurent41709552019-12-16 19:34:05 -08003124void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003125 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003126 if (t == nullptr) {
3127 return;
3128 }
Eric Laurent41709552019-12-16 19:34:05 -08003129 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3130 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003131}
3132
Eric Laurent41709552019-12-16 19:34:05 -08003133void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003134 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3135
Andy Hung328d6772021-01-12 12:32:21 -08003136 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003137 if (t == nullptr) {
3138 return;
3139 }
3140 t->onEffectDisable();
3141}
3142
3143bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3144 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003145 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003146 if (t == nullptr) {
3147 return false;
3148 }
3149 t->disconnectEffectHandle(handle, unpinIfLast);
3150 return true;
3151}
3152
3153void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003154 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003155 if (c == nullptr) {
3156 return;
3157 }
3158 c->resetVolume_l();
3159
3160}
3161
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003162product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003163 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003164 if (c == nullptr) {
3165 return PRODUCT_STRATEGY_NONE;
3166 }
3167 return c->strategy();
3168}
3169
3170int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003171 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003172 if (c == nullptr) {
3173 return 0;
3174 }
3175 return c->activeTrackCnt();
3176}
3177
Eric Laurentb82e6b72019-11-22 17:25:04 -08003178
3179#undef LOG_TAG
3180#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3181
3182status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3183{
3184 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3185 Mutex::Autolock _l(mProxyLock);
3186 if (status == NO_ERROR) {
3187 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003188 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003189 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003190 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003191 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003192 bs = handle.second->disable(&status);
3193 }
3194 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003195 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003196 }
3197 }
3198 }
3199 ALOGV("%s enable %d status %d", __func__, enabled, status);
3200 return status;
3201}
3202
3203status_t AudioFlinger::DeviceEffectProxy::init(
3204 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3205//For all audio patches
3206//If src or sink device match
3207//If the effect is HW accelerated
3208// if no corresponding effect module
3209// Create EffectModule: mHalEffect
3210//Create and attach EffectHandle
3211//If the effect is not HW accelerated and the patch sink or src is a mixer port
3212// Create Effect on patch input or output thread on session -1
3213//Add EffectHandle to EffectHandle map of Effect Proxy:
3214 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3215 status_t status = NO_ERROR;
3216 for (auto &patch : patches) {
3217 status = onCreatePatch(patch.first, patch.second);
3218 ALOGV("%s onCreatePatch status %d", __func__, status);
3219 if (status == BAD_VALUE) {
3220 return status;
3221 }
3222 }
3223 return status;
3224}
3225
3226status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3227 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3228 status_t status = NAME_NOT_FOUND;
3229 sp<EffectHandle> handle;
3230 // only consider source[0] as this is the only "true" source of a patch
3231 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3232 ALOGV("%s source checkPort status %d", __func__, status);
3233 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3234 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3235 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3236 }
3237 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3238 Mutex::Autolock _l(mProxyLock);
3239 mEffectHandles.emplace(patchHandle, handle);
3240 }
3241 ALOGW_IF(status == BAD_VALUE,
3242 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3243
3244 return status;
3245}
3246
3247status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3248 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3249
3250 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3251 __func__, port->type, port->ext.device.type,
3252 port->ext.device.address, port->id, patch.isSoftware());
3253 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003254 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003255 return NAME_NOT_FOUND;
3256 }
3257 status_t status = NAME_NOT_FOUND;
3258
3259 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3260 Mutex::Autolock _l(mProxyLock);
3261 mDevicePort = *port;
3262 mHalEffect = new EffectModule(mMyCallback,
3263 const_cast<effect_descriptor_t *>(&mDescriptor),
3264 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3265 false /* pinned */, port->id);
3266 if (audio_is_input_device(mDevice.mType)) {
3267 mHalEffect->setInputDevice(mDevice);
3268 } else {
3269 mHalEffect->setDevices({mDevice});
3270 }
Eric Laurentde8caf42021-08-11 17:19:25 +02003271 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3272 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003273 status = (*handle)->initCheck();
3274 if (status == OK) {
3275 status = mHalEffect->addHandle((*handle).get());
3276 } else {
3277 mHalEffect.clear();
3278 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3279 }
3280 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3281 sp <ThreadBase> thread;
3282 if (audio_port_config_has_input_direction(port)) {
3283 if (patch.isSoftware()) {
3284 thread = patch.mRecord.thread();
3285 } else {
3286 thread = patch.thread().promote();
3287 }
3288 } else {
3289 if (patch.isSoftware()) {
3290 thread = patch.mPlayback.thread();
3291 } else {
3292 thread = patch.thread().promote();
3293 }
3294 }
3295 int enabled;
3296 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3297 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003298 &enabled, &status, false, false /*probe*/,
3299 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003300 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3301 } else {
3302 status = BAD_VALUE;
3303 }
3304 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003305 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003306 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003307 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003308 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003309 bs = (*handle)->disable(&status);
3310 }
3311 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003312 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003313 }
3314 }
3315 return status;
3316}
3317
3318void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3319 Mutex::Autolock _l(mProxyLock);
3320 mEffectHandles.erase(patchHandle);
3321}
3322
3323
3324size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3325{
3326 Mutex::Autolock _l(mProxyLock);
3327 if (effect == mHalEffect) {
3328 mHalEffect.clear();
3329 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3330 }
3331 return mHalEffect == nullptr ? 0 : 1;
3332}
3333
3334status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3335 sp<EffectHalInterface> effect) {
3336 if (mHalEffect == nullptr) {
3337 return NO_INIT;
3338 }
3339 return mManagerCallback->addEffectToHal(
3340 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3341}
3342
3343status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3344 sp<EffectHalInterface> effect) {
3345 if (mHalEffect == nullptr) {
3346 return NO_INIT;
3347 }
3348 return mManagerCallback->removeEffectFromHal(
3349 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3350}
3351
3352bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3353 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3354 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3355 }
3356 return true;
3357}
3358
3359uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3360 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3361 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3362 return mDevicePort.sample_rate;
3363 }
3364 return DEFAULT_OUTPUT_SAMPLE_RATE;
3365}
3366
3367audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3368 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3369 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3370 return mDevicePort.channel_mask;
3371 }
3372 return AUDIO_CHANNEL_OUT_STEREO;
3373}
3374
3375uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3376 if (isOutput()) {
3377 return audio_channel_count_from_out_mask(channelMask());
3378 }
3379 return audio_channel_count_from_in_mask(channelMask());
3380}
3381
3382void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3383 const Vector<String16> args;
3384 EffectBase::dump(fd, args);
3385
3386 const bool locked = dumpTryLock(mProxyLock);
3387 if (!locked) {
3388 String8 result("DeviceEffectProxy may be deadlocked\n");
3389 write(fd, result.string(), result.size());
3390 }
3391
3392 String8 outStr;
3393 if (mHalEffect != nullptr) {
3394 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3395 } else {
3396 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3397 }
3398 write(fd, outStr.string(), outStr.size());
3399 outStr.clear();
3400
3401 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3402 write(fd, outStr.string(), outStr.size());
3403 outStr.clear();
3404
3405 for (const auto& iter : mEffectHandles) {
3406 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3407 write(fd, outStr.string(), outStr.size());
3408 outStr.clear();
3409 sp<EffectBase> effect = iter.second->effect().promote();
3410 if (effect != nullptr) {
3411 effect->dump(fd, args);
3412 }
3413 }
3414
3415 if (locked) {
3416 mLock.unlock();
3417 }
3418}
3419
3420#undef LOG_TAG
3421#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3422
3423int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3424 return mManagerCallback->newEffectId();
3425}
3426
3427
3428bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3429 EffectHandle *handle, bool unpinIfLast) {
3430 sp<EffectBase> effectBase = handle->effect().promote();
3431 if (effectBase == nullptr) {
3432 return false;
3433 }
3434
3435 sp<EffectModule> effect = effectBase->asEffectModule();
3436 if (effect == nullptr) {
3437 return false;
3438 }
3439
3440 // restore suspended effects if the disconnected handle was enabled and the last one.
3441 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3442 if (remove) {
3443 sp<DeviceEffectProxy> proxy = mProxy.promote();
3444 if (proxy != nullptr) {
3445 proxy->removeEffect(effect);
3446 }
3447 if (handle->enabled()) {
3448 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3449 }
3450 }
3451 return true;
3452}
3453
3454status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3455 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3456 sp<EffectHalInterface> *effect) {
3457 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3458}
3459
3460status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3461 sp<EffectHalInterface> effect) {
3462 sp<DeviceEffectProxy> proxy = mProxy.promote();
3463 if (proxy == nullptr) {
3464 return NO_INIT;
3465 }
3466 return proxy->addEffectToHal(effect);
3467}
3468
3469status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3470 sp<EffectHalInterface> effect) {
3471 sp<DeviceEffectProxy> proxy = mProxy.promote();
3472 if (proxy == nullptr) {
3473 return NO_INIT;
3474 }
3475 return proxy->addEffectToHal(effect);
3476}
3477
3478bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3479 sp<DeviceEffectProxy> proxy = mProxy.promote();
3480 if (proxy == nullptr) {
3481 return true;
3482 }
3483 return proxy->isOutput();
3484}
3485
3486uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3487 sp<DeviceEffectProxy> proxy = mProxy.promote();
3488 if (proxy == nullptr) {
3489 return DEFAULT_OUTPUT_SAMPLE_RATE;
3490 }
3491 return proxy->sampleRate();
3492}
3493
Eric Laurentf1f22e72021-07-13 14:04:14 +02003494audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3495 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003496 sp<DeviceEffectProxy> proxy = mProxy.promote();
3497 if (proxy == nullptr) {
3498 return AUDIO_CHANNEL_OUT_STEREO;
3499 }
3500 return proxy->channelMask();
3501}
3502
Eric Laurentf1f22e72021-07-13 14:04:14 +02003503uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3504 sp<DeviceEffectProxy> proxy = mProxy.promote();
3505 if (proxy == nullptr) {
3506 return 2;
3507 }
3508 return proxy->channelCount();
3509}
3510
3511audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3512 sp<DeviceEffectProxy> proxy = mProxy.promote();
3513 if (proxy == nullptr) {
3514 return AUDIO_CHANNEL_OUT_STEREO;
3515 }
3516 return proxy->channelMask();
3517}
3518
3519uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003520 sp<DeviceEffectProxy> proxy = mProxy.promote();
3521 if (proxy == nullptr) {
3522 return 2;
3523 }
3524 return proxy->channelCount();
3525}
3526
Glenn Kasten63238ef2015-03-02 15:50:29 -08003527} // namespace android