blob: efd2dbd42b7662fad9e961a9facf2486a87f2f40 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080044
45#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080046
47// ----------------------------------------------------------------------------
48
49// Note: the following macro is used for extremely verbose logging message. In
50// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
51// 0; but one side effect of this is to turn all LOGV's as well. Some messages
52// are so verbose that we want to suppress them even when we have ALOG_ASSERT
53// turned on. Do not uncomment the #def below unless you really know what you
54// are doing and want to see all of the extremely verbose messages.
55//#define VERY_VERY_VERBOSE_LOGGING
56#ifdef VERY_VERY_VERBOSE_LOGGING
57#define ALOGVV ALOGV
58#else
59#define ALOGVV(a...) do { } while(0)
60#endif
61
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090062#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
63
Eric Laurentca7cc822012-11-19 14:55:58 -080064namespace android {
65
Andy Hung1131b6e2020-12-08 20:47:45 -080066using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070067using binder::Status;
68
69namespace {
70
71// Append a POD value into a vector of bytes.
72template<typename T>
73void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
74 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
75 buffer->insert(buffer->end(), ar, ar + sizeof(T));
76}
77
78// Write a POD value into a vector of bytes (clears the previous buffer
79// content).
80template<typename T>
81void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
82 buffer->clear();
83 appendToBuffer(value, buffer);
84}
85
86} // namespace
87
Eric Laurentca7cc822012-11-19 14:55:58 -080088// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080089// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080090// ----------------------------------------------------------------------------
91
92#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080093#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080094
Eric Laurent41709552019-12-16 19:34:05 -080095AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080096 effect_descriptor_t *desc,
97 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080098 audio_session_t sessionId,
99 bool pinned)
100 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800101 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800102 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800103{
Eric Laurentca7cc822012-11-19 14:55:58 -0800104}
105
Eric Laurent41709552019-12-16 19:34:05 -0800106// must be called with EffectModule::mLock held
107status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800108{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800109
Eric Laurent41709552019-12-16 19:34:05 -0800110 ALOGV("setEnabled %p enabled %d", this, enabled);
111
112 if (enabled != isEnabled()) {
113 switch (mState) {
114 // going from disabled to enabled
115 case IDLE:
116 mState = STARTING;
117 break;
118 case STOPPED:
119 mState = RESTART;
120 break;
121 case STOPPING:
122 mState = ACTIVE;
123 break;
124
125 // going from enabled to disabled
126 case RESTART:
127 mState = STOPPED;
128 break;
129 case STARTING:
130 mState = IDLE;
131 break;
132 case ACTIVE:
133 mState = STOPPING;
134 break;
135 case DESTROYED:
136 return NO_ERROR; // simply ignore as we are being destroyed
137 }
138 for (size_t i = 1; i < mHandles.size(); i++) {
139 EffectHandle *h = mHandles[i];
140 if (h != NULL && !h->disconnected()) {
141 h->setEnabled(enabled);
142 }
143 }
144 }
145 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800146}
147
Eric Laurent41709552019-12-16 19:34:05 -0800148status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
149{
150 status_t status;
151 {
152 Mutex::Autolock _l(mLock);
153 status = setEnabled_l(enabled);
154 }
155 if (fromHandle) {
156 if (enabled) {
157 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700158 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800159 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700160 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800161 }
162 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700163 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800164 }
165 }
166 return status;
167}
168
169bool AudioFlinger::EffectBase::isEnabled() const
170{
171 switch (mState) {
172 case RESTART:
173 case STARTING:
174 case ACTIVE:
175 return true;
176 case IDLE:
177 case STOPPING:
178 case STOPPED:
179 case DESTROYED:
180 default:
181 return false;
182 }
183}
184
185void AudioFlinger::EffectBase::setSuspended(bool suspended)
186{
187 Mutex::Autolock _l(mLock);
188 mSuspended = suspended;
189}
190
191bool AudioFlinger::EffectBase::suspended() const
192{
193 Mutex::Autolock _l(mLock);
194 return mSuspended;
195}
196
197status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800198{
199 status_t status;
200
201 Mutex::Autolock _l(mLock);
202 int priority = handle->priority();
203 size_t size = mHandles.size();
204 EffectHandle *controlHandle = NULL;
205 size_t i;
206 for (i = 0; i < size; i++) {
207 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800208 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800209 continue;
210 }
211 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700212 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800213 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700214 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800215 if (h->priority() <= priority) {
216 break;
217 }
218 }
219 // if inserted in first place, move effect control from previous owner to this handle
220 if (i == 0) {
221 bool enabled = false;
222 if (controlHandle != NULL) {
223 enabled = controlHandle->enabled();
224 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
225 }
226 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
227 status = NO_ERROR;
228 } else {
229 status = ALREADY_EXISTS;
230 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700231 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800232 mHandles.insertAt(handle, i);
233 return status;
234}
235
Eric Laurent41709552019-12-16 19:34:05 -0800236status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700237{
238 status_t status = NO_ERROR;
239 bool doRegister = false;
240 bool registered = false;
241 bool doEnable = false;
242 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700243 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800244 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700245
246 {
247 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200248
249 if ((isInternal_l() && !mPolicyRegistered)
250 || !getCallback()->isAudioPolicyReady()) {
251 return NO_ERROR;
252 }
253
Eric Laurent6c796322019-04-09 14:13:17 -0700254 // register effect when first handle is attached and unregister when last handle is removed
255 if (mPolicyRegistered != mHandles.size() > 0) {
256 doRegister = true;
257 mPolicyRegistered = mHandles.size() > 0;
258 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700259 const auto callback = getCallback();
260 io = callback->io();
261 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700262 }
263 }
264 // enable effect when registered according to enable state requested by controlling handle
265 if (mHandles.size() > 0) {
266 EffectHandle *handle = controlHandle_l();
267 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
268 doEnable = true;
269 mPolicyEnabled = handle->enabled();
270 }
271 }
272 registered = mPolicyRegistered;
273 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100274 // The simultaneous release of two EffectHandles with the same EffectModule
275 // may cause us to call this method at the same time.
276 // This may deadlock under some circumstances (b/180941720). Avoid this.
277 if (!doRegister && !(registered && doEnable)) {
278 return NO_ERROR;
279 }
Eric Laurent6c796322019-04-09 14:13:17 -0700280 mPolicyLock.lock();
281 }
282 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
283 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
284 if (doRegister) {
285 if (registered) {
286 status = AudioSystem::registerEffect(
287 &mDescriptor,
288 io,
289 strategy,
290 mSessionId,
291 mId);
292 } else {
293 status = AudioSystem::unregisterEffect(mId);
294 }
295 }
296 if (registered && doEnable) {
297 status = AudioSystem::setEffectEnabled(mId, enabled);
298 }
299 mPolicyLock.unlock();
300
301 return status;
302}
303
304
Eric Laurent41709552019-12-16 19:34:05 -0800305ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800306{
307 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800308 return removeHandle_l(handle);
309}
310
Eric Laurent41709552019-12-16 19:34:05 -0800311ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800312{
Eric Laurentca7cc822012-11-19 14:55:58 -0800313 size_t size = mHandles.size();
314 size_t i;
315 for (i = 0; i < size; i++) {
316 if (mHandles[i] == handle) {
317 break;
318 }
319 }
320 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800321 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
322 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800323 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800324 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800325
326 mHandles.removeAt(i);
327 // if removed from first place, move effect control from this handle to next in line
328 if (i == 0) {
329 EffectHandle *h = controlHandle_l();
330 if (h != NULL) {
331 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
332 }
333 }
334
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530335 // Prevent calls to process() and other functions on effect interface from now on.
336 // The effect engine will be released by the destructor when the last strong reference on
337 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800338 if (mHandles.size() == 0 && !mPinned) {
339 mState = DESTROYED;
340 }
341
342 return mHandles.size();
343}
344
345// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800346AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800347{
348 // the first valid handle in the list has control over the module
349 for (size_t i = 0; i < mHandles.size(); i++) {
350 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800351 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800352 return h;
353 }
354 }
355
356 return NULL;
357}
358
Eric Laurentf10c7092016-12-06 17:09:56 -0800359// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800360ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800361{
Andy Hungfda44002021-06-03 17:23:16 -0700362 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800363 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700364 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800365 return mHandles.size();
366 }
367
Eric Laurentf10c7092016-12-06 17:09:56 -0800368 Mutex::Autolock _l(mLock);
369 ssize_t numHandles = removeHandle_l(handle);
370 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800371 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700372 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800373 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800374 }
375 return numHandles;
376}
377
Eric Laurent41709552019-12-16 19:34:05 -0800378bool AudioFlinger::EffectBase::purgeHandles()
379{
380 bool enabled = false;
381 Mutex::Autolock _l(mLock);
382 EffectHandle *handle = controlHandle_l();
383 if (handle != NULL) {
384 enabled = handle->enabled();
385 }
386 mHandles.clear();
387 return enabled;
388}
389
390void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700391 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800392}
393
394static String8 effectFlagsToString(uint32_t flags) {
395 String8 s;
396
397 s.append("conn. mode: ");
398 switch (flags & EFFECT_FLAG_TYPE_MASK) {
399 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
400 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
401 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
402 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
403 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
404 default: s.append("unknown/reserved"); break;
405 }
406 s.append(", ");
407
408 s.append("insert pref: ");
409 switch (flags & EFFECT_FLAG_INSERT_MASK) {
410 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
411 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
412 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
413 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
414 default: s.append("unknown/reserved"); break;
415 }
416 s.append(", ");
417
418 s.append("volume mgmt: ");
419 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
420 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
421 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
422 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
423 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
424 default: s.append("unknown/reserved"); break;
425 }
426 s.append(", ");
427
428 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
429 if (devind) {
430 s.append("device indication: ");
431 switch (devind) {
432 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
433 default: s.append("unknown/reserved"); break;
434 }
435 s.append(", ");
436 }
437
438 s.append("input mode: ");
439 switch (flags & EFFECT_FLAG_INPUT_MASK) {
440 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
441 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
442 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
443 default: s.append("not set"); break;
444 }
445 s.append(", ");
446
447 s.append("output mode: ");
448 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
449 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
450 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
451 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
452 default: s.append("not set"); break;
453 }
454 s.append(", ");
455
456 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
457 if (accel) {
458 s.append("hardware acceleration: ");
459 switch (accel) {
460 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
461 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
462 default: s.append("unknown/reserved"); break;
463 }
464 s.append(", ");
465 }
466
467 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
468 if (modeind) {
469 s.append("mode indication: ");
470 switch (modeind) {
471 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
472 default: s.append("unknown/reserved"); break;
473 }
474 s.append(", ");
475 }
476
477 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
478 if (srcind) {
479 s.append("source indication: ");
480 switch (srcind) {
481 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
482 default: s.append("unknown/reserved"); break;
483 }
484 s.append(", ");
485 }
486
487 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
488 s.append("offloadable, ");
489 }
490
491 int len = s.length();
492 if (s.length() > 2) {
493 (void) s.lockBuffer(len);
494 s.unlockBuffer(len - 2);
495 }
496 return s;
497}
498
499void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
500{
501 String8 result;
502
503 result.appendFormat("\tEffect ID %d:\n", mId);
504
505 bool locked = AudioFlinger::dumpTryLock(mLock);
506 // failed to lock - AudioFlinger is probably deadlocked
507 if (!locked) {
508 result.append("\t\tCould not lock Fx mutex:\n");
509 }
510
511 result.append("\t\tSession State Registered Enabled Suspended:\n");
512 result.appendFormat("\t\t%05d %03d %s %s %s\n",
513 mSessionId, mState, mPolicyRegistered ? "y" : "n",
514 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
515
516 result.append("\t\tDescriptor:\n");
517 char uuidStr[64];
518 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
519 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
520 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
521 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
522 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
523 mDescriptor.apiVersion,
524 mDescriptor.flags,
525 effectFlagsToString(mDescriptor.flags).string());
526 result.appendFormat("\t\t- name: %s\n",
527 mDescriptor.name);
528
529 result.appendFormat("\t\t- implementor: %s\n",
530 mDescriptor.implementor);
531
532 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
533 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
534 char buffer[256];
535 for (size_t i = 0; i < mHandles.size(); ++i) {
536 EffectHandle *handle = mHandles[i];
537 if (handle != NULL && !handle->disconnected()) {
538 handle->dumpToBuffer(buffer, sizeof(buffer));
539 result.append(buffer);
540 }
541 }
542 if (locked) {
543 mLock.unlock();
544 }
545
546 write(fd, result.string(), result.length());
547}
548
549// ----------------------------------------------------------------------------
550// EffectModule implementation
551// ----------------------------------------------------------------------------
552
553#undef LOG_TAG
554#define LOG_TAG "AudioFlinger::EffectModule"
555
556AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
557 effect_descriptor_t *desc,
558 int id,
559 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800560 bool pinned,
561 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800562 : EffectBase(callback, desc, id, sessionId, pinned),
563 // clear mConfig to ensure consistent initial value of buffer framecount
564 // in case buffers are associated by setInBuffer() or setOutBuffer()
565 // prior to configure().
566 mConfig{{}, {}},
567 mStatus(NO_INIT),
568 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
569 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800570 mOffloaded(false),
571 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800572#ifdef FLOAT_EFFECT_CHAIN
573 , mSupportsFloat(false)
574#endif
575{
576 ALOGV("Constructor %p pinned %d", this, pinned);
577 int lStatus;
578
579 // create effect engine from effect factory
580 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800581 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800582 if (mStatus != NO_ERROR) {
583 return;
584 }
585 lStatus = init();
586 if (lStatus < 0) {
587 mStatus = lStatus;
588 goto Error;
589 }
590
591 setOffloaded(callback->isOffload(), callback->io());
592 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
593
594 return;
595Error:
596 mEffectInterface.clear();
597 ALOGV("Constructor Error %d", mStatus);
598}
599
600AudioFlinger::EffectModule::~EffectModule()
601{
602 ALOGV("Destructor %p", this);
603 if (mEffectInterface != 0) {
604 char uuidStr[64];
605 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
606 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
607 this, uuidStr);
608 release_l();
609 }
610
611}
612
Eric Laurentfa1e1232016-08-02 19:01:49 -0700613bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800614 Mutex::Autolock _l(mLock);
615
Eric Laurentfa1e1232016-08-02 19:01:49 -0700616 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 switch (mState) {
618 case RESTART:
619 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700620 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800621
622 case STARTING:
623 // clear auxiliary effect input buffer for next accumulation
624 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
625 memset(mConfig.inputCfg.buffer.raw,
626 0,
627 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
628 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700629 if (start_l() == NO_ERROR) {
630 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700631 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 } else {
633 mState = IDLE;
634 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800635 break;
636 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900637 // volume control for offload and direct threads must take effect immediately.
638 if (stop_l() == NO_ERROR
639 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700640 mDisableWaitCnt = mMaxDisableWaitCnt;
641 } else {
642 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
643 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800644 mState = STOPPED;
645 break;
646 case STOPPED:
647 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
648 // turn off sequence.
649 if (--mDisableWaitCnt == 0) {
650 reset_l();
651 mState = IDLE;
652 }
653 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200654 case ACTIVE:
655 for (size_t i = 0; i < mHandles.size(); i++) {
656 if (!mHandles[i]->disconnected()) {
657 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
658 }
659 }
660 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800661 default: //IDLE , ACTIVE, DESTROYED
662 break;
663 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700664
665 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800666}
667
668void AudioFlinger::EffectModule::process()
669{
670 Mutex::Autolock _l(mLock);
671
Mikhail Naganov022b9952017-01-04 16:36:51 -0800672 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800673 return;
674 }
675
rago94a1ee82017-07-21 15:11:02 -0700676 const uint32_t inChannelCount =
677 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
678 const uint32_t outChannelCount =
679 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
680 const bool auxType =
681 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
682
Andy Hungfa69ca32017-11-30 10:07:53 -0800683 // safeInputOutputSampleCount is 0 if the channel count between input and output
684 // buffers do not match. This prevents automatic accumulation or copying between the
685 // input and output effect buffers without an intermediary effect process.
686 // TODO: consider implementing channel conversion.
687 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700688 mInChannelCountRequested != mOutChannelCountRequested ? 0
689 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800690 mConfig.inputCfg.buffer.frameCount,
691 mConfig.outputCfg.buffer.frameCount);
692 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
693#ifdef FLOAT_EFFECT_CHAIN
694 accumulate_float(
695 mConfig.outputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.f32,
697 safeInputOutputSampleCount);
698#else
699 accumulate_i16(
700 mConfig.outputCfg.buffer.s16,
701 mConfig.inputCfg.buffer.s16,
702 safeInputOutputSampleCount);
703#endif
704 };
705 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
706#ifdef FLOAT_EFFECT_CHAIN
707 memcpy(
708 mConfig.outputCfg.buffer.f32,
709 mConfig.inputCfg.buffer.f32,
710 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
711
712#else
713 memcpy(
714 mConfig.outputCfg.buffer.s16,
715 mConfig.inputCfg.buffer.s16,
716 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
717#endif
718 };
719
Eric Laurentca7cc822012-11-19 14:55:58 -0800720 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700721 int ret;
722 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700723 if (auxType) {
724 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800725 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700726#ifdef FLOAT_EFFECT_CHAIN
727 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800728#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700729 // Do in-place float conversion for auxiliary effect input buffer.
730 static_assert(sizeof(float) <= sizeof(int32_t),
731 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
732
Andy Hungfa69ca32017-11-30 10:07:53 -0800733 memcpy_to_float_from_q4_27(
734 mConfig.inputCfg.buffer.f32,
735 mConfig.inputCfg.buffer.s32,
736 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800737#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800738 } else
Andy Hung116a4982017-11-30 10:15:08 -0800739#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800740 {
Andy Hung116a4982017-11-30 10:15:08 -0800741#ifdef FLOAT_AUX
742 memcpy_to_i16_from_float(
743 mConfig.inputCfg.buffer.s16,
744 mConfig.inputCfg.buffer.f32,
745 mConfig.inputCfg.buffer.frameCount);
746#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 memcpy_to_i16_from_q4_27(
748 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700749 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800750 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800751#endif
rago94a1ee82017-07-21 15:11:02 -0700752 }
rago94a1ee82017-07-21 15:11:02 -0700753 }
754#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800755 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
756 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
757
758 if (!auxType && mInChannelCountRequested != inChannelCount) {
759 adjust_channels(
760 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
761 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
762 sizeof(float),
763 sizeof(float)
764 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
765 inBuffer = mInConversionBuffer;
766 }
767 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
768 && mOutChannelCountRequested != outChannelCount) {
769 adjust_selected_channels(
770 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
771 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
772 sizeof(float),
773 sizeof(float)
774 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
775 outBuffer = mOutConversionBuffer;
776 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800777 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
778 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800779 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
781 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700782 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800783 memcpy_to_i16_from_float(
784 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800785 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800787 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700788 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800790 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
792 goto data_bypass;
793 }
794 memcpy_to_i16_from_float(
795 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800796 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800797 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800798 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700799 }
800 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800801#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800802 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800803#ifdef FLOAT_EFFECT_CHAIN
804 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800805 sp<EffectBufferHalInterface> target =
806 mOutChannelCountRequested != outChannelCount
807 ? mOutConversionBuffer : mOutBuffer;
808
Andy Hungfa69ca32017-11-30 10:07:53 -0800809 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800810 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 mOutConversionBuffer->audioBuffer()->s16,
812 outChannelCount * mConfig.outputCfg.buffer.frameCount);
813 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800814 if (mOutChannelCountRequested != outChannelCount) {
815 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
816 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
817 sizeof(float),
818 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
819 }
rago94a1ee82017-07-21 15:11:02 -0700820#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700821 } else {
rago94a1ee82017-07-21 15:11:02 -0700822#ifdef FLOAT_EFFECT_CHAIN
823 data_bypass:
824#endif
825 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800826 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700827 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800828 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700829 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800830 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700831 }
832 }
833 ret = -ENODATA;
834 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800835
Eric Laurentca7cc822012-11-19 14:55:58 -0800836 // force transition to IDLE state when engine is ready
837 if (mState == STOPPED && ret == -ENODATA) {
838 mDisableWaitCnt = 1;
839 }
840
841 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700842 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800843#ifdef FLOAT_AUX
844 const size_t size =
845 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
846#else
rago94a1ee82017-07-21 15:11:02 -0700847 const size_t size =
848 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800849#endif
rago94a1ee82017-07-21 15:11:02 -0700850 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800851 }
852 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700853 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
855 // If an insert effect is idle and input buffer is different from output buffer,
856 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700857 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700858 // similar handling with data_bypass above.
859 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
860 accumulateInputToOutput();
861 } else { // EFFECT_BUFFER_ACCESS_WRITE
862 copyInputToOutput();
863 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800864 }
865 }
866}
867
868void AudioFlinger::EffectModule::reset_l()
869{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700870 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800871 return;
872 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700873 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800874}
875
876status_t AudioFlinger::EffectModule::configure()
877{
rago94a1ee82017-07-21 15:11:02 -0700878 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700879 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700880 uint32_t size;
881 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700882 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700883
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700884 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700885 status = NO_INIT;
886 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800887 }
888
Eric Laurentca7cc822012-11-19 14:55:58 -0800889 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800890 // TODO: handle configuration of input (record) SW effects above the HAL,
891 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
892 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700893 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200894 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800895 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200896 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800897
898 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800899 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
900 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
901 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
902 mConfig.inputCfg.channels);
903 }
904#ifndef MULTICHANNEL_EFFECT_CHAIN
905 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
906 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
907 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
908 mConfig.outputCfg.channels);
909 }
910#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800911 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800912#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700913 // TODO: Update this logic when multichannel effects are implemented.
914 // For offloaded tracks consider mono output as stereo for proper effect initialization
915 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
916 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
917 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
918 ALOGV("Overriding effect input and output as STEREO");
919 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800920#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 }
jiabineb3bda02020-06-30 14:07:03 -0700922 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700923 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700924 mConfig.inputCfg.channels |= hapticChannelMask;
925 mConfig.outputCfg.channels |= hapticChannelMask;
926 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800927 mInChannelCountRequested =
928 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
929 mOutChannelCountRequested =
930 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700931
rago94a1ee82017-07-21 15:11:02 -0700932 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
933 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900934
935 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700936 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900937 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
938 ALOGV("Overriding effect input as 48kHz");
939 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700940 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900941 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800942 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
943 mConfig.inputCfg.bufferProvider.cookie = NULL;
944 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
945 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
946 mConfig.outputCfg.bufferProvider.cookie = NULL;
947 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
948 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
949 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
950 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800951 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800952 // always overwrites output buffer: input buffer == output buffer
953 // - in other sessions:
954 // last effect in the chain accumulates in output buffer: input buffer != output buffer
955 // other effect: overwrites output buffer: input buffer == output buffer
956 // Auxiliary effect:
957 // accumulates in output buffer: input buffer != output buffer
958 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700959 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800960 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
961 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700962 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800963 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
964
Eric Laurent6b446ce2019-12-13 10:56:31 -0800965 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700966 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800967 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800968
969 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700970 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700971 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800972 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700973 &mConfig,
974 &size,
975 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700976 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800977 status = cmdStatus;
978 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800979
980#ifdef MULTICHANNEL_EFFECT_CHAIN
981 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700982 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800983 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
984 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
985 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700986 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
987 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800988 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
989 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
990 }
991 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
992 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
993 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
994 }
995 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700996 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800997 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700998 &mConfig,
999 &size,
1000 &cmdStatus);
1001 if (status == NO_ERROR) {
1002 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001003 }
1004 }
1005#endif
1006
1007#ifdef FLOAT_EFFECT_CHAIN
1008 if (status == NO_ERROR) {
1009 mSupportsFloat = true;
1010 }
1011
1012 if (status != NO_ERROR) {
1013 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1014 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1015 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1016 size = sizeof(int);
1017 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1018 sizeof(mConfig),
1019 &mConfig,
1020 &size,
1021 &cmdStatus);
1022 if (status == NO_ERROR) {
1023 status = cmdStatus;
1024 }
1025 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001026 mSupportsFloat = false;
1027 ALOGVV("config worked with 16 bit");
1028 } else {
1029 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001030 }
rago94a1ee82017-07-21 15:11:02 -07001031 }
1032#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001033
rago94a1ee82017-07-21 15:11:02 -07001034 if (status == NO_ERROR) {
1035 // Establish Buffer strategy
1036 setInBuffer(mInBuffer);
1037 setOutBuffer(mOutBuffer);
1038
1039 // Update visualizer latency
1040 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1041 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1042 effect_param_t *p = (effect_param_t *)buf32;
1043
1044 p->psize = sizeof(uint32_t);
1045 p->vsize = sizeof(uint32_t);
1046 size = sizeof(int);
1047 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1048
Andy Hungfda44002021-06-03 17:23:16 -07001049 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001050
1051 *((int32_t *)p->data + 1)= latency;
1052 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1053 sizeof(effect_param_t) + 8,
1054 &buf32,
1055 &size,
1056 &cmdStatus);
1057 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001058 }
1059
Andy Hung05083ac2017-12-14 15:00:28 -08001060 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1061 mMaxDisableWaitCnt = (uint32_t)std::max(
1062 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1063 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1064 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001065
Eric Laurentd0ebb532013-04-02 16:41:41 -07001066exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001067 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001068 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001069 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 return status;
1071}
1072
1073status_t AudioFlinger::EffectModule::init()
1074{
1075 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001076 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 return NO_INIT;
1078 }
1079 status_t cmdStatus;
1080 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001081 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1082 0,
1083 NULL,
1084 &size,
1085 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001086 if (status == 0) {
1087 status = cmdStatus;
1088 }
1089 return status;
1090}
1091
Eric Laurent1b928682014-10-02 19:41:47 -07001092void AudioFlinger::EffectModule::addEffectToHal_l()
1093{
1094 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1095 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001096 if (mAddedToHal) {
1097 return;
1098 }
1099
Andy Hungfda44002021-06-03 17:23:16 -07001100 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001101 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001102 }
1103}
1104
Eric Laurentfa1e1232016-08-02 19:01:49 -07001105// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001106status_t AudioFlinger::EffectModule::start()
1107{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001108 status_t status;
1109 {
1110 Mutex::Autolock _l(mLock);
1111 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001112 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001113 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001114 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001115 }
1116 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001117}
1118
1119status_t AudioFlinger::EffectModule::start_l()
1120{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001121 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001122 return NO_INIT;
1123 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001124 if (mStatus != NO_ERROR) {
1125 return mStatus;
1126 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 status_t cmdStatus;
1128 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001129 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1130 0,
1131 NULL,
1132 &size,
1133 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 if (status == 0) {
1135 status = cmdStatus;
1136 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001137 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001138 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001139 }
1140 return status;
1141}
1142
1143status_t AudioFlinger::EffectModule::stop()
1144{
1145 Mutex::Autolock _l(mLock);
1146 return stop_l();
1147}
1148
1149status_t AudioFlinger::EffectModule::stop_l()
1150{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001151 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 return NO_INIT;
1153 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001154 if (mStatus != NO_ERROR) {
1155 return mStatus;
1156 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001157 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001158 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001159
1160 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001161 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1162 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1163 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001164 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001165 mSetVolumeReentrantTid = INVALID_PID;
1166 }
1167
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001168 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1169 0,
1170 NULL,
1171 &size,
1172 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001174 status = cmdStatus;
1175 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001177 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001178 }
1179 return status;
1180}
1181
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001182// must be called with EffectChain::mLock held
1183void AudioFlinger::EffectModule::release_l()
1184{
1185 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001186 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001187 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001188 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001189 mEffectInterface.clear();
1190 }
1191}
1192
Eric Laurent6b446ce2019-12-13 10:56:31 -08001193status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001194{
1195 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1196 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001197 if (!mAddedToHal) {
1198 return NO_ERROR;
1199 }
1200
Andy Hungfda44002021-06-03 17:23:16 -07001201 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001202 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001203 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001204 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001205}
1206
Andy Hunge4a1d912016-08-17 14:11:13 -07001207// round up delta valid if value and divisor are positive.
1208template <typename T>
1209static T roundUpDelta(const T &value, const T &divisor) {
1210 T remainder = value % divisor;
1211 return remainder == 0 ? 0 : divisor - remainder;
1212}
1213
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001214status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1215 const std::vector<uint8_t>& cmdData,
1216 int32_t maxReplySize,
1217 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001218{
1219 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001220 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001221
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001222 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 return NO_INIT;
1224 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001225 if (mStatus != NO_ERROR) {
1226 return mStatus;
1227 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001228 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1229 return -EINVAL;
1230 }
1231 size_t cmdSize = cmdData.size();
1232 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1233 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1234 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001235 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001236 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001237 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001238 android_errorWriteLog(0x534e4554, "33003822");
1239 return -EINVAL;
1240 }
1241 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001242 (maxReplySize < sizeof(effect_param_t) ||
1243 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001244 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001245 return -EINVAL;
1246 }
ragoe2759072016-11-22 18:02:48 -08001247 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001248 (sizeof(effect_param_t) > maxReplySize
1249 || param->psize > maxReplySize - sizeof(effect_param_t)
1250 || param->vsize > maxReplySize - sizeof(effect_param_t)
1251 - param->psize
1252 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1253 maxReplySize
1254 - sizeof(effect_param_t)
1255 - param->psize
1256 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001257 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1258 android_errorWriteLog(0x534e4554, "32705438");
1259 return -EINVAL;
1260 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001261 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001262 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1263 && // DEFERRED not generally used
1264 (param == nullptr
1265 || param->psize > cmdSize - sizeof(effect_param_t)
1266 || param->vsize > cmdSize - sizeof(effect_param_t)
1267 - param->psize
1268 || roundUpDelta(param->psize,
1269 (uint32_t) sizeof(int)) >
1270 cmdSize
1271 - sizeof(effect_param_t)
1272 - param->psize
1273 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001274 android_errorWriteLog(0x534e4554, "30204301");
1275 return -EINVAL;
1276 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001277 uint32_t replySize = maxReplySize;
1278 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001279 status_t status = mEffectInterface->command(cmdCode,
1280 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001281 const_cast<uint8_t*>(cmdData.data()),
1282 &replySize,
1283 reply->data());
1284 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001285 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001286 for (size_t i = 1; i < mHandles.size(); i++) {
1287 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001288 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001289 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 }
1291 }
1292 }
1293 return status;
1294}
1295
Eric Laurentca7cc822012-11-19 14:55:58 -08001296bool AudioFlinger::EffectModule::isProcessEnabled() const
1297{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001298 if (mStatus != NO_ERROR) {
1299 return false;
1300 }
1301
Eric Laurentca7cc822012-11-19 14:55:58 -08001302 switch (mState) {
1303 case RESTART:
1304 case ACTIVE:
1305 case STOPPING:
1306 case STOPPED:
1307 return true;
1308 case IDLE:
1309 case STARTING:
1310 case DESTROYED:
1311 default:
1312 return false;
1313 }
1314}
1315
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001316bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1317{
Andy Hungfda44002021-06-03 17:23:16 -07001318 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001319}
1320
1321bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1322{
1323 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1324}
1325
Mikhail Naganov022b9952017-01-04 16:36:51 -08001326void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001327 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001328
1329 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001330 if (buffer != 0) {
1331 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1332 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1333 } else {
1334 mConfig.inputCfg.buffer.raw = NULL;
1335 }
1336 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001337 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001338
1339#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001340 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001341 // Theoretically insert effects can also do in-place conversions (destroying
1342 // the original buffer) when the output buffer is identical to the input buffer,
1343 // but we don't optimize for it here.
1344 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001345 const uint32_t inChannelCount =
1346 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1347 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001348 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001349 // we need to translate - create hidl shared buffer and intercept
1350 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001351 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1352 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1353 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001354
1355 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1356 __func__, inChannels, inFrameCount, size);
1357
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001358 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001359 || size > mInConversionBuffer->getSize())) {
1360 mInConversionBuffer.clear();
1361 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001362 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001363 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001364 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001365 mInConversionBuffer->setFrameCount(inFrameCount);
1366 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001367 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001368 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001369 }
1370 }
1371#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001372}
1373
1374void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001375 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001376
1377 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001378 if (buffer != 0) {
1379 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1380 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1381 } else {
1382 mConfig.outputCfg.buffer.raw = NULL;
1383 }
1384 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001385 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001386
1387#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001388 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001389 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001390 const uint32_t outChannelCount =
1391 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1392 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001393 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001394 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001395 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1396 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1397 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001398
1399 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1400 __func__, outChannels, outFrameCount, size);
1401
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001402 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001403 || size > mOutConversionBuffer->getSize())) {
1404 mOutConversionBuffer.clear();
1405 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001406 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001407 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001408 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001409 mOutConversionBuffer->setFrameCount(outFrameCount);
1410 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001411 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001412 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001413 }
1414 }
1415#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001416}
1417
Eric Laurentca7cc822012-11-19 14:55:58 -08001418status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1419{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001420 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001421 if (mStatus != NO_ERROR) {
1422 return mStatus;
1423 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1426 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1427 if (isProcessEnabled() &&
1428 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001429 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1430 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 uint32_t volume[2];
1432 uint32_t *pVolume = NULL;
1433 uint32_t size = sizeof(volume);
1434 volume[0] = *left;
1435 volume[1] = *right;
1436 if (controller) {
1437 pVolume = volume;
1438 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001439 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1440 size,
1441 volume,
1442 &size,
1443 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1445 *left = volume[0];
1446 *right = volume[1];
1447 }
1448 }
1449 return status;
1450}
1451
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001452void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1453{
Zhou Songd505c642020-02-20 16:35:37 +08001454 // for offload or direct thread, if the effect chain has non-offloadable
1455 // effect and any effect module within the chain has volume control, then
1456 // volume control is delegated to effect, otherwise, set volume to hal.
1457 if (mEffectCallback->isOffloadOrDirect() &&
1458 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001459 float vol_l = (float)left / (1 << 24);
1460 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001461 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001462 }
1463}
1464
jiabin8f278ee2019-11-11 12:16:27 -08001465status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1466 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001467{
jiabin8f278ee2019-11-11 12:16:27 -08001468 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1469 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001470 return NO_ERROR;
1471 }
1472
1473 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001474 if (mStatus != NO_ERROR) {
1475 return mStatus;
1476 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001478 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001479 status_t cmdStatus;
1480 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001481 // FIXME: use audio device types and addresses when the hal interface is ready.
1482 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001483 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001484 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001485 &size,
1486 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001487 }
1488 return status;
1489}
1490
jiabin8f278ee2019-11-11 12:16:27 -08001491status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1492{
1493 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1494}
1495
1496status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1497{
1498 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1499}
1500
Eric Laurentca7cc822012-11-19 14:55:58 -08001501status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1502{
1503 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001504 if (mStatus != NO_ERROR) {
1505 return mStatus;
1506 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001507 status_t status = NO_ERROR;
1508 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1509 status_t cmdStatus;
1510 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001511 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1512 sizeof(audio_mode_t),
1513 &mode,
1514 &size,
1515 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001516 if (status == NO_ERROR) {
1517 status = cmdStatus;
1518 }
1519 }
1520 return status;
1521}
1522
1523status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1524{
1525 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001526 if (mStatus != NO_ERROR) {
1527 return mStatus;
1528 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 status_t status = NO_ERROR;
1530 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1531 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001532 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1533 sizeof(audio_source_t),
1534 &source,
1535 &size,
1536 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001537 }
1538 return status;
1539}
1540
Eric Laurent5baf2af2013-09-12 17:37:00 -07001541status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1542{
1543 Mutex::Autolock _l(mLock);
1544 if (mStatus != NO_ERROR) {
1545 return mStatus;
1546 }
1547 status_t status = NO_ERROR;
1548 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1549 status_t cmdStatus;
1550 uint32_t size = sizeof(status_t);
1551 effect_offload_param_t cmd;
1552
1553 cmd.isOffload = offloaded;
1554 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001555 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1556 sizeof(effect_offload_param_t),
1557 &cmd,
1558 &size,
1559 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001560 if (status == NO_ERROR) {
1561 status = cmdStatus;
1562 }
1563 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1564 } else {
1565 if (offloaded) {
1566 status = INVALID_OPERATION;
1567 }
1568 mOffloaded = false;
1569 }
1570 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1571 return status;
1572}
1573
1574bool AudioFlinger::EffectModule::isOffloaded() const
1575{
1576 Mutex::Autolock _l(mLock);
1577 return mOffloaded;
1578}
1579
jiabineb3bda02020-06-30 14:07:03 -07001580/*static*/
1581bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1582 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1583}
1584
1585bool AudioFlinger::EffectModule::isHapticGenerator() const {
1586 return isHapticGenerator(&mDescriptor.type);
1587}
1588
jiabine70bc7f2020-06-30 22:07:55 -07001589status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1590{
1591 if (mStatus != NO_ERROR) {
1592 return mStatus;
1593 }
1594 if (!isHapticGenerator()) {
1595 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1596 return INVALID_OPERATION;
1597 }
1598
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001599 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1600 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001601 param->psize = sizeof(int32_t);
1602 param->vsize = sizeof(int32_t) * 2;
1603 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1604 *((int32_t*)param->data + 1) = id;
1605 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001606 std::vector<uint8_t> response;
1607 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001608 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001609 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1610 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001611 }
1612 return status;
1613}
1614
Lais Andradebc3f37a2021-07-02 00:13:19 +01001615status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001616{
1617 if (mStatus != NO_ERROR) {
1618 return mStatus;
1619 }
1620 if (!isHapticGenerator()) {
1621 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1622 return INVALID_OPERATION;
1623 }
1624
Lais Andradebc3f37a2021-07-02 00:13:19 +01001625 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001626 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001627 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001628 effect_param_t *param = (effect_param_t*) request.data();
1629 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001630 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001631 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1632 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001633 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1634 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1635 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001636 std::vector<uint8_t> response;
1637 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1638 if (status == NO_ERROR) {
1639 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1640 status = *reinterpret_cast<const status_t*>(response.data());
1641 }
1642 return status;
1643}
1644
Andy Hungbded9c82017-11-30 18:47:35 -08001645static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1646 std::stringstream ss;
1647
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001648 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001649 return "nullptr"; // make different than below
1650 } else if (buffer->externalData() != nullptr) {
1651 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1652 << " -> "
1653 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1654 } else {
1655 ss << buffer->audioBuffer()->raw;
1656 }
1657 return ss.str();
1658}
Marco Nelissenb2208842014-02-07 14:00:50 -08001659
Eric Laurent41709552019-12-16 19:34:05 -08001660void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001661{
Eric Laurent41709552019-12-16 19:34:05 -08001662 EffectBase::dump(fd, args);
1663
Eric Laurentca7cc822012-11-19 14:55:58 -08001664 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001666
Eric Laurent41709552019-12-16 19:34:05 -08001667 result.append("\t\tStatus Engine:\n");
1668 result.appendFormat("\t\t%03d %p\n",
1669 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001670
1671 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001672
1673 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001674 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1675 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1676 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001677 mConfig.inputCfg.buffer.frameCount,
1678 mConfig.inputCfg.samplingRate,
1679 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001680 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001681 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001682
1683 result.append("\t\t- Output configuration:\n");
1684 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001685 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001686 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001687 mConfig.outputCfg.buffer.frameCount,
1688 mConfig.outputCfg.samplingRate,
1689 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001690 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001691 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001692
rago94a1ee82017-07-21 15:11:02 -07001693#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001694
Andy Hungbded9c82017-11-30 18:47:35 -08001695 result.appendFormat("\t\t- HAL buffers:\n"
1696 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1697 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1698 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1699 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1700 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001701#endif
1702
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 write(fd, result.string(), result.length());
1704
Mikhail Naganov4d547672019-02-22 14:19:19 -08001705 if (mEffectInterface != 0) {
1706 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1707 (void)mEffectInterface->dump(fd);
1708 }
1709
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 if (locked) {
1711 mLock.unlock();
1712 }
1713}
1714
1715// ----------------------------------------------------------------------------
1716// EffectHandle implementation
1717// ----------------------------------------------------------------------------
1718
1719#undef LOG_TAG
1720#define LOG_TAG "AudioFlinger::EffectHandle"
1721
Eric Laurent41709552019-12-16 19:34:05 -08001722AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001723 const sp<AudioFlinger::Client>& client,
1724 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001725 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 : BnEffect(),
1727 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001728 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1729 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001730{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001731 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001732
1733 if (client == 0) {
1734 return;
1735 }
1736 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1737 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001738 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001739 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001740 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001741 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001742 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001743 return;
1744 }
Glenn Kastene75da402013-11-20 13:54:52 -08001745 new(mCblk) effect_param_cblk_t();
1746 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001747}
1748
1749AudioFlinger::EffectHandle::~EffectHandle()
1750{
1751 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 disconnect(false);
1753}
1754
Andy Hungc747c532022-03-07 21:41:14 -08001755// Creates an association between Binder code to name for IEffect.
1756#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1757BINDER_METHOD_ENTRY(enable) \
1758BINDER_METHOD_ENTRY(disable) \
1759BINDER_METHOD_ENTRY(command) \
1760BINDER_METHOD_ENTRY(disconnect) \
1761BINDER_METHOD_ENTRY(getCblk) \
1762
1763// singleton for Binder Method Statistics for IEffect
1764mediautils::MethodStatistics<int>& getIEffectStatistics() {
1765 using Code = int;
1766
1767#pragma push_macro("BINDER_METHOD_ENTRY")
1768#undef BINDER_METHOD_ENTRY
1769#define BINDER_METHOD_ENTRY(ENTRY) \
1770 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1771
1772 static mediautils::MethodStatistics<Code> methodStatistics{
1773 IEFFECT_BINDER_METHOD_MACRO_LIST
1774 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1775 };
1776#pragma pop_macro("BINDER_METHOD_ENTRY")
1777
1778 return methodStatistics;
1779}
1780
1781status_t AudioFlinger::EffectHandle::onTransact(
1782 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
1783 std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
1784 mediametrics::Defer defer([startTime, code] {
1785 std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
1786 getIEffectStatistics().event(code,
1787 std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(
1788 endTime - startTime).count());
1789 });
1790 return BnEffect::onTransact(code, data, reply, flags);
1791}
1792
Glenn Kastene75da402013-11-20 13:54:52 -08001793status_t AudioFlinger::EffectHandle::initCheck()
1794{
1795 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1796}
1797
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001798#define RETURN(code) \
1799 *_aidl_return = (code); \
1800 return Status::ok();
1801
1802Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001803{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001804 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001805 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001806 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001807 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001808 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001809 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001810 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001811 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001813
1814 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001815 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 }
1817
1818 mEnabled = true;
1819
Eric Laurent6c796322019-04-09 14:13:17 -07001820 status_t status = effect->updatePolicyState();
1821 if (status != NO_ERROR) {
1822 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001823 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001824 }
1825
Eric Laurent6b446ce2019-12-13 10:56:31 -08001826 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001827
1828 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001829 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 }
1832
Eric Laurent6b446ce2019-12-13 10:56:31 -08001833 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001834 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 mEnabled = false;
1836 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001837 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001838}
1839
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001840Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001841{
1842 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001843 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001844 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001846 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001847 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001848 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001850 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001851
1852 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001854 }
1855 mEnabled = false;
1856
Eric Laurent6c796322019-04-09 14:13:17 -07001857 effect->updatePolicyState();
1858
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001859 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001860 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 }
1862
Eric Laurent6b446ce2019-12-13 10:56:31 -08001863 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001864 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865}
1866
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001868{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001869 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001872}
1873
1874void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1875{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001876 AutoMutex _l(mLock);
1877 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1878 if (mDisconnected) {
1879 if (unpinIfLast) {
1880 android_errorWriteLog(0x534e4554, "32707507");
1881 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001882 return;
1883 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001884 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001885 {
Eric Laurent41709552019-12-16 19:34:05 -08001886 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001887 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001888 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001889 ALOGW("%s Effect handle %p disconnected after thread destruction",
1890 __func__, this);
1891 }
1892 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001893 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001894 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001895
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 if (mClient != 0) {
1897 if (mCblk != NULL) {
1898 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1899 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1900 }
1901 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001902 // Client destructor must run with AudioFlinger client mutex locked
1903 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001904 mClient.clear();
1905 }
1906}
1907
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1909 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1910 return Status::ok();
1911}
1912
1913Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1914 const std::vector<uint8_t>& cmdData,
1915 int32_t maxResponseSize,
1916 std::vector<uint8_t>* response,
1917 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001918{
1919 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001920 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001921
Eric Laurentc7ab3092017-06-15 18:43:46 -07001922 // reject commands reserved for internal use by audio framework if coming from outside
1923 // of audioserver
1924 switch(cmdCode) {
1925 case EFFECT_CMD_ENABLE:
1926 case EFFECT_CMD_DISABLE:
1927 case EFFECT_CMD_SET_PARAM:
1928 case EFFECT_CMD_SET_PARAM_DEFERRED:
1929 case EFFECT_CMD_SET_PARAM_COMMIT:
1930 case EFFECT_CMD_GET_PARAM:
1931 break;
1932 default:
1933 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1934 break;
1935 }
1936 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001937 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001938 }
1939
Eric Laurent1ffc5852016-12-15 14:46:09 -08001940 if (cmdCode == EFFECT_CMD_ENABLE) {
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);
1946 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001947 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001948 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001949 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001950 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001951 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001952 writeToBuffer(NO_ERROR, response);
1953 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001954 }
1955
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001956 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001957 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001958 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001959 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001960 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001961 // only get parameter command is permitted for applications not controlling the effect
1962 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001963 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001964 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001965
1966 // handle commands that are not forwarded transparently to effect engine
1967 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001968 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001969 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001970 }
1971
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001972 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001973 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001974 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001975 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001976 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001977
Eric Laurentca7cc822012-11-19 14:55:58 -08001978 // No need to trylock() here as this function is executed in the binder thread serving a
1979 // particular client process: no risk to block the whole media server process or mixer
1980 // threads if we are stuck here
1981 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001982 // keep local copy of index in case of client corruption b/32220769
1983 const uint32_t clientIndex = mCblk->clientIndex;
1984 const uint32_t serverIndex = mCblk->serverIndex;
1985 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1986 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001987 mCblk->serverIndex = 0;
1988 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001989 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 }
1991 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001992 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001993 for (uint32_t index = serverIndex; index < clientIndex;) {
1994 int *p = (int *)(mBuffer + index);
1995 const int size = *p++;
1996 if (size < 0
1997 || size > EFFECT_PARAM_BUFFER_SIZE
1998 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001999 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002000 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002001 break;
2002 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002003
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002004 std::copy(reinterpret_cast<const uint8_t*>(p),
2005 reinterpret_cast<const uint8_t*>(p) + size,
2006 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002007
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002009 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002010 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002011 sizeof(int),
2012 &replyBuffer);
2013 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002014
2015 // verify shared memory: server index shouldn't change; client index can't go back.
2016 if (serverIndex != mCblk->serverIndex
2017 || clientIndex > mCblk->clientIndex) {
2018 android_errorWriteLog(0x534e4554, "32220769");
2019 status = BAD_VALUE;
2020 break;
2021 }
2022
Eric Laurentca7cc822012-11-19 14:55:58 -08002023 // stop at first error encountered
2024 if (ret != NO_ERROR) {
2025 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002026 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 break;
2028 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002029 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002030 break;
2031 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002032 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002033 }
2034 mCblk->serverIndex = 0;
2035 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002036 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 }
2038
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002039 status_t status = effect->command(cmdCode,
2040 cmdData,
2041 maxResponseSize,
2042 response);
2043 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002044}
2045
2046void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2047{
2048 ALOGV("setControl %p control %d", this, hasControl);
2049
2050 mHasControl = hasControl;
2051 mEnabled = enabled;
2052
2053 if (signal && mEffectClient != 0) {
2054 mEffectClient->controlStatusChanged(hasControl);
2055 }
2056}
2057
2058void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002059 const std::vector<uint8_t>& cmdData,
2060 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002061{
2062 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002063 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002064 }
2065}
2066
2067
2068
2069void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2070{
2071 if (mEffectClient != 0) {
2072 mEffectClient->enableStatusChanged(enabled);
2073 }
2074}
2075
Eric Laurentde8caf42021-08-11 17:19:25 +02002076void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2077{
2078 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2079 mEffectClient->framesProcessed(frames);
2080 }
2081}
2082
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002083void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002084{
2085 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2086
Marco Nelissenb2208842014-02-07 14:00:50 -08002087 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002088 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002089 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002090 mHasControl ? "yes" : "no",
2091 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002092 mCblk ? mCblk->clientIndex : 0,
2093 mCblk ? mCblk->serverIndex : 0
2094 );
2095
2096 if (locked) {
2097 mCblk->lock.unlock();
2098 }
2099}
2100
2101#undef LOG_TAG
2102#define LOG_TAG "AudioFlinger::EffectChain"
2103
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002104AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2105 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002106 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002107 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002108 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002109 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002110{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002111 sp<ThreadBase> p = thread.promote();
2112 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002113 return;
2114 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002115 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002116 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2117 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002118}
2119
2120AudioFlinger::EffectChain::~EffectChain()
2121{
Eric Laurentca7cc822012-11-19 14:55:58 -08002122}
2123
2124// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2125sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2126 effect_descriptor_t *descriptor)
2127{
2128 size_t size = mEffects.size();
2129
2130 for (size_t i = 0; i < size; i++) {
2131 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2132 return mEffects[i];
2133 }
2134 }
2135 return 0;
2136}
2137
2138// getEffectFromId_l() must be called with ThreadBase::mLock held
2139sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2140{
2141 size_t size = mEffects.size();
2142
2143 for (size_t i = 0; i < size; i++) {
2144 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2145 if (id == 0 || mEffects[i]->id() == id) {
2146 return mEffects[i];
2147 }
2148 }
2149 return 0;
2150}
2151
2152// getEffectFromType_l() must be called with ThreadBase::mLock held
2153sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2154 const effect_uuid_t *type)
2155{
2156 size_t size = mEffects.size();
2157
2158 for (size_t i = 0; i < size; i++) {
2159 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2160 return mEffects[i];
2161 }
2162 }
2163 return 0;
2164}
2165
Eric Laurent6c796322019-04-09 14:13:17 -07002166std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2167{
2168 std::vector<int> ids;
2169 Mutex::Autolock _l(mLock);
2170 for (size_t i = 0; i < mEffects.size(); i++) {
2171 ids.push_back(mEffects[i]->id());
2172 }
2173 return ids;
2174}
2175
Eric Laurentca7cc822012-11-19 14:55:58 -08002176void AudioFlinger::EffectChain::clearInputBuffer()
2177{
2178 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002179 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002180}
2181
2182// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002184{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002185 if (mInBuffer == NULL) {
2186 return;
2187 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002188 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2189 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002190
Eric Laurent6b446ce2019-12-13 10:56:31 -08002191 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002192 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002193}
2194
2195// Must be called with EffectChain::mLock locked
2196void AudioFlinger::EffectChain::process_l()
2197{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002198 // never process effects when:
2199 // - on an OFFLOAD thread
2200 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002201 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002202 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002203 bool tracksOnSession = (trackCnt() != 0);
2204
2205 if (!tracksOnSession && mTailBufferCount == 0) {
2206 doProcess = false;
2207 }
2208
2209 if (activeTrackCnt() == 0) {
2210 // if no track is active and the effect tail has not been rendered,
2211 // the input buffer must be cleared here as the mixer process will not do it
2212 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002213 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002214 if (mTailBufferCount > 0) {
2215 mTailBufferCount--;
2216 }
2217 }
2218 }
2219 }
2220
2221 size_t size = mEffects.size();
2222 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002223 // Only the input and output buffers of the chain can be external,
2224 // and 'update' / 'commit' do nothing for allocated buffers, thus
2225 // it's not needed to consider any other buffers here.
2226 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002227 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2228 mOutBuffer->update();
2229 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002230 for (size_t i = 0; i < size; i++) {
2231 mEffects[i]->process();
2232 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002233 mInBuffer->commit();
2234 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2235 mOutBuffer->commit();
2236 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002237 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002238 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002239 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002240 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2241 }
2242 if (doResetVolume) {
2243 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002244 }
2245}
2246
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002247// createEffect_l() must be called with ThreadBase::mLock held
2248status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002249 effect_descriptor_t *desc,
2250 int id,
2251 audio_session_t sessionId,
2252 bool pinned)
2253{
2254 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002255 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002256 status_t lStatus = effect->status();
2257 if (lStatus == NO_ERROR) {
2258 lStatus = addEffect_ll(effect);
2259 }
2260 if (lStatus != NO_ERROR) {
2261 effect.clear();
2262 }
2263 return lStatus;
2264}
2265
2266// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002267status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2268{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002269 Mutex::Autolock _l(mLock);
2270 return addEffect_ll(effect);
2271}
2272// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2273status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2274{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002275 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002276
Eric Laurentb62d0362021-10-26 17:40:18 +02002277 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002278 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2279 // Auxiliary effects are inserted at the beginning of mEffects vector as
2280 // they are processed first and accumulated in chain input buffer
2281 mEffects.insertAt(effect, 0);
2282
2283 // the input buffer for auxiliary effect contains mono samples in
2284 // 32 bit format. This is to avoid saturation in AudoMixer
2285 // accumulation stage. Saturation is done in EffectModule::process() before
2286 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002287 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002288 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002289#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002290 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002291 numSamples * sizeof(float), &halBuffer);
2292#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002293 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002294 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002295#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002296 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002297
2298 effect->configure();
2299
Mikhail Naganov022b9952017-01-04 16:36:51 -08002300 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002301 // auxiliary effects output samples to chain input buffer for further processing
2302 // by insert effects
2303 effect->setOutBuffer(mInBuffer);
2304 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002305 ssize_t idx_insert = getInsertIndex(desc);
2306 if (idx_insert < 0) {
2307 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 }
2309
Eric Laurentb62d0362021-10-26 17:40:18 +02002310 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002311 mEffects.insertAt(effect, idx_insert);
2312
2313 effect->configure();
2314
Eric Laurentb62d0362021-10-26 17:40:18 +02002315 // - By default:
2316 // All effects read samples from chain input buffer.
2317 // The last effect in the chain, writes samples to chain output buffer,
2318 // otherwise to chain input buffer
2319 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2320 // The spatializer effect (first effect) reads samples from the input buffer
2321 // and writes samples to the output buffer.
2322 // All other effects read and writes samples to the output buffer
2323 if (mEffectCallback->isSpatializer()
2324 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002325 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002326 if (idx_insert == 0) {
2327 if (previousSize != 0) {
2328 mEffects[1]->configure();
2329 mEffects[1]->setInBuffer(mOutBuffer);
2330 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2331 }
2332 effect->setInBuffer(mInBuffer);
2333 } else {
2334 effect->setInBuffer(mOutBuffer);
2335 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002336 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002337 effect->setInBuffer(mInBuffer);
2338 if (idx_insert == previousSize) {
2339 if (idx_insert != 0) {
2340 mEffects[idx_insert-1]->configure();
2341 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2342 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2343 }
2344 effect->setOutBuffer(mOutBuffer);
2345 } else {
2346 effect->setOutBuffer(mInBuffer);
2347 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002349 ALOGV("%s effect %p, added in chain %p at rank %zu",
2350 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002351 }
2352 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002353
Eric Laurentca7cc822012-11-19 14:55:58 -08002354 return NO_ERROR;
2355}
2356
Eric Laurentb62d0362021-10-26 17:40:18 +02002357ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2358 // Insert effects are inserted at the end of mEffects vector as they are processed
2359 // after track and auxiliary effects.
2360 // Insert effect order as a function of indicated preference:
2361 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2362 // another effect is present
2363 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2364 // last effect claiming first position
2365 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2366 // first effect claiming last position
2367 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2368 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2369 // already present
2370 // Spatializer or Downmixer effects are inserted in first position because
2371 // they adapt the channel count for all other effects in the chain
2372 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2373 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2374 return 0;
2375 }
2376
2377 size_t size = mEffects.size();
2378 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2379 ssize_t idx_insert;
2380 ssize_t idx_insert_first = -1;
2381 ssize_t idx_insert_last = -1;
2382
2383 idx_insert = size;
2384 for (size_t i = 0; i < size; i++) {
2385 effect_descriptor_t d = mEffects[i]->desc();
2386 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2387 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2388 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2389 // check invalid effect chaining combinations
2390 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2391 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2392 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2393 __func__, desc.name, d.name);
2394 return -1;
2395 }
2396 // remember position of first insert effect and by default
2397 // select this as insert position for new effect
2398 if (idx_insert == size) {
2399 idx_insert = i;
2400 }
2401 // remember position of last insert effect claiming
2402 // first position
2403 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2404 idx_insert_first = i;
2405 }
2406 // remember position of first insert effect claiming
2407 // last position
2408 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2409 idx_insert_last == -1) {
2410 idx_insert_last = i;
2411 }
2412 }
2413 }
2414
2415 // modify idx_insert from first position if needed
2416 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2417 if (idx_insert_last != -1) {
2418 idx_insert = idx_insert_last;
2419 } else {
2420 idx_insert = size;
2421 }
2422 } else {
2423 if (idx_insert_first != -1) {
2424 idx_insert = idx_insert_first + 1;
2425 }
2426 }
2427 return idx_insert;
2428}
2429
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002430// removeEffect_l() must be called with ThreadBase::mLock held
2431size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2432 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002433{
2434 Mutex::Autolock _l(mLock);
2435 size_t size = mEffects.size();
2436 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2437
2438 for (size_t i = 0; i < size; i++) {
2439 if (effect == mEffects[i]) {
2440 // calling stop here will remove pre-processing effect from the audio HAL.
2441 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2442 // the middle of a read from audio HAL
2443 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2444 mEffects[i]->state() == EffectModule::STOPPING) {
2445 mEffects[i]->stop();
2446 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002447 if (release) {
2448 mEffects[i]->release_l();
2449 }
2450
Mikhail Naganov022b9952017-01-04 16:36:51 -08002451 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002452 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002453 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002454 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002455 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002456 }
2457 }
2458 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002459
2460 // make sure the input buffer configuration for the new first effect in the chain
2461 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2462 if (i == 0 && size > 1) {
2463 mEffects[0]->configure();
2464 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002465 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002466 }
2467
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002468 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002469 this, i);
2470 break;
2471 }
2472 }
2473
2474 return mEffects.size();
2475}
2476
jiabin8f278ee2019-11-11 12:16:27 -08002477// setDevices_l() must be called with ThreadBase::mLock held
2478void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002479{
2480 size_t size = mEffects.size();
2481 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002482 mEffects[i]->setDevices(devices);
2483 }
2484}
2485
2486// setInputDevice_l() must be called with ThreadBase::mLock held
2487void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2488{
2489 size_t size = mEffects.size();
2490 for (size_t i = 0; i < size; i++) {
2491 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002492 }
2493}
2494
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002495// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002496void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2497{
2498 size_t size = mEffects.size();
2499 for (size_t i = 0; i < size; i++) {
2500 mEffects[i]->setMode(mode);
2501 }
2502}
2503
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002504// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002505void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2506{
2507 size_t size = mEffects.size();
2508 for (size_t i = 0; i < size; i++) {
2509 mEffects[i]->setAudioSource(source);
2510 }
2511}
2512
Zhou Songd505c642020-02-20 16:35:37 +08002513bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2514 for (const auto &effect : mEffects) {
2515 if (effect->isVolumeControlEnabled()) return true;
2516 }
2517 return false;
2518}
2519
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002520// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002521bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002522{
2523 uint32_t newLeft = *left;
2524 uint32_t newRight = *right;
2525 bool hasControl = false;
2526 int ctrlIdx = -1;
2527 size_t size = mEffects.size();
2528
2529 // first update volume controller
2530 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002531 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002532 ctrlIdx = i - 1;
2533 hasControl = true;
2534 break;
2535 }
2536 }
2537
Eric Laurentfa1e1232016-08-02 19:01:49 -07002538 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002539 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002540 if (hasControl) {
2541 *left = mNewLeftVolume;
2542 *right = mNewRightVolume;
2543 }
2544 return hasControl;
2545 }
2546
2547 mVolumeCtrlIdx = ctrlIdx;
2548 mLeftVolume = newLeft;
2549 mRightVolume = newRight;
2550
2551 // second get volume update from volume controller
2552 if (ctrlIdx >= 0) {
2553 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2554 mNewLeftVolume = newLeft;
2555 mNewRightVolume = newRight;
2556 }
2557 // then indicate volume to all other effects in chain.
2558 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002559 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002560 uint32_t lVol = newLeft;
2561 uint32_t rVol = newRight;
2562
2563 for (size_t i = 0; i < size; i++) {
2564 if ((int)i == ctrlIdx) {
2565 continue;
2566 }
2567 // this also works for ctrlIdx == -1 when there is no volume controller
2568 if ((int)i > ctrlIdx) {
2569 lVol = *left;
2570 rVol = *right;
2571 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002572 // Pass requested volume directly if this is volume monitor module
2573 if (mEffects[i]->isVolumeMonitor()) {
2574 mEffects[i]->setVolume(left, right, false);
2575 } else {
2576 mEffects[i]->setVolume(&lVol, &rVol, false);
2577 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002578 }
2579 *left = newLeft;
2580 *right = newRight;
2581
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002582 setVolumeForOutput_l(*left, *right);
2583
Eric Laurentca7cc822012-11-19 14:55:58 -08002584 return hasControl;
2585}
2586
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002587// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002588void AudioFlinger::EffectChain::resetVolume_l()
2589{
Eric Laurente7449bf2016-08-03 18:44:07 -07002590 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2591 uint32_t left = mLeftVolume;
2592 uint32_t right = mRightVolume;
2593 (void)setVolume_l(&left, &right, true);
2594 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002595}
2596
jiabineb3bda02020-06-30 14:07:03 -07002597// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2598bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2599{
2600 for (size_t i = 0; i < mEffects.size(); ++i) {
2601 if (mEffects[i]->isHapticGenerator()) {
2602 return true;
2603 }
2604 }
2605 return false;
2606}
2607
jiabine70bc7f2020-06-30 22:07:55 -07002608void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2609{
2610 Mutex::Autolock _l(mLock);
2611 for (size_t i = 0; i < mEffects.size(); ++i) {
2612 mEffects[i]->setHapticIntensity(id, intensity);
2613 }
2614}
2615
Eric Laurent1b928682014-10-02 19:41:47 -07002616void AudioFlinger::EffectChain::syncHalEffectsState()
2617{
2618 Mutex::Autolock _l(mLock);
2619 for (size_t i = 0; i < mEffects.size(); i++) {
2620 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2621 mEffects[i]->state() == EffectModule::STOPPING) {
2622 mEffects[i]->addEffectToHal_l();
2623 }
2624 }
2625}
2626
Eric Laurentca7cc822012-11-19 14:55:58 -08002627void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2628{
Eric Laurentca7cc822012-11-19 14:55:58 -08002629 String8 result;
2630
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002631 const size_t numEffects = mEffects.size();
2632 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002633
Marco Nelissenb2208842014-02-07 14:00:50 -08002634 if (numEffects) {
2635 bool locked = AudioFlinger::dumpTryLock(mLock);
2636 // failed to lock - AudioFlinger is probably deadlocked
2637 if (!locked) {
2638 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002639 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002640
Andy Hungbded9c82017-11-30 18:47:35 -08002641 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2642 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2643 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2644 (int)inBufferStr.size(), "In buffer ",
2645 (int)outBufferStr.size(), "Out buffer ");
2646 result.appendFormat("\t%s %s %d\n",
2647 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002648 write(fd, result.string(), result.size());
2649
2650 for (size_t i = 0; i < numEffects; ++i) {
2651 sp<EffectModule> effect = mEffects[i];
2652 if (effect != 0) {
2653 effect->dump(fd, args);
2654 }
2655 }
2656
2657 if (locked) {
2658 mLock.unlock();
2659 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002660 } else {
2661 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002662 }
2663}
2664
2665// must be called with ThreadBase::mLock held
2666void AudioFlinger::EffectChain::setEffectSuspended_l(
2667 const effect_uuid_t *type, bool suspend)
2668{
2669 sp<SuspendedEffectDesc> desc;
2670 // use effect type UUID timelow as key as there is no real risk of identical
2671 // timeLow fields among effect type UUIDs.
2672 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2673 if (suspend) {
2674 if (index >= 0) {
2675 desc = mSuspendedEffects.valueAt(index);
2676 } else {
2677 desc = new SuspendedEffectDesc();
2678 desc->mType = *type;
2679 mSuspendedEffects.add(type->timeLow, desc);
2680 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2681 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002682
Eric Laurentca7cc822012-11-19 14:55:58 -08002683 if (desc->mRefCount++ == 0) {
2684 sp<EffectModule> effect = getEffectIfEnabled(type);
2685 if (effect != 0) {
2686 desc->mEffect = effect;
2687 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002688 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002689 }
2690 }
2691 } else {
2692 if (index < 0) {
2693 return;
2694 }
2695 desc = mSuspendedEffects.valueAt(index);
2696 if (desc->mRefCount <= 0) {
2697 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002698 desc->mRefCount = 0;
2699 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002700 }
2701 if (--desc->mRefCount == 0) {
2702 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2703 if (desc->mEffect != 0) {
2704 sp<EffectModule> effect = desc->mEffect.promote();
2705 if (effect != 0) {
2706 effect->setSuspended(false);
2707 effect->lock();
2708 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002709 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002710 effect->setEnabled_l(handle->enabled());
2711 }
2712 effect->unlock();
2713 }
2714 desc->mEffect.clear();
2715 }
2716 mSuspendedEffects.removeItemsAt(index);
2717 }
2718 }
2719}
2720
2721// must be called with ThreadBase::mLock held
2722void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2723{
2724 sp<SuspendedEffectDesc> desc;
2725
2726 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2727 if (suspend) {
2728 if (index >= 0) {
2729 desc = mSuspendedEffects.valueAt(index);
2730 } else {
2731 desc = new SuspendedEffectDesc();
2732 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2733 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2734 }
2735 if (desc->mRefCount++ == 0) {
2736 Vector< sp<EffectModule> > effects;
2737 getSuspendEligibleEffects(effects);
2738 for (size_t i = 0; i < effects.size(); i++) {
2739 setEffectSuspended_l(&effects[i]->desc().type, true);
2740 }
2741 }
2742 } else {
2743 if (index < 0) {
2744 return;
2745 }
2746 desc = mSuspendedEffects.valueAt(index);
2747 if (desc->mRefCount <= 0) {
2748 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2749 desc->mRefCount = 1;
2750 }
2751 if (--desc->mRefCount == 0) {
2752 Vector<const effect_uuid_t *> types;
2753 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2754 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2755 continue;
2756 }
2757 types.add(&mSuspendedEffects.valueAt(i)->mType);
2758 }
2759 for (size_t i = 0; i < types.size(); i++) {
2760 setEffectSuspended_l(types[i], false);
2761 }
2762 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2763 mSuspendedEffects.keyAt(index));
2764 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2765 }
2766 }
2767}
2768
2769
2770// The volume effect is used for automated tests only
2771#ifndef OPENSL_ES_H_
2772static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2773 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2774const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2775#endif //OPENSL_ES_H_
2776
Eric Laurentd8365c52017-07-16 15:27:05 -07002777/* static */
2778bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2779{
2780 // Only NS and AEC are suspended when BtNRec is off
2781 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2782 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2783 return true;
2784 }
2785 return false;
2786}
2787
Eric Laurentca7cc822012-11-19 14:55:58 -08002788bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2789{
2790 // auxiliary effects and visualizer are never suspended on output mix
2791 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2792 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2793 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002794 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2795 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002796 return false;
2797 }
2798 return true;
2799}
2800
2801void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2802 Vector< sp<AudioFlinger::EffectModule> > &effects)
2803{
2804 effects.clear();
2805 for (size_t i = 0; i < mEffects.size(); i++) {
2806 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2807 effects.add(mEffects[i]);
2808 }
2809 }
2810}
2811
2812sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2813 const effect_uuid_t *type)
2814{
2815 sp<EffectModule> effect = getEffectFromType_l(type);
2816 return effect != 0 && effect->isEnabled() ? effect : 0;
2817}
2818
2819void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2820 bool enabled)
2821{
2822 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2823 if (enabled) {
2824 if (index < 0) {
2825 // if the effect is not suspend check if all effects are suspended
2826 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2827 if (index < 0) {
2828 return;
2829 }
2830 if (!isEffectEligibleForSuspend(effect->desc())) {
2831 return;
2832 }
2833 setEffectSuspended_l(&effect->desc().type, enabled);
2834 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2835 if (index < 0) {
2836 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2837 return;
2838 }
2839 }
2840 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2841 effect->desc().type.timeLow);
2842 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002843 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002844 if (desc->mEffect == 0) {
2845 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002846 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002847 effect->setSuspended(true);
2848 }
2849 } else {
2850 if (index < 0) {
2851 return;
2852 }
2853 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2854 effect->desc().type.timeLow);
2855 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2856 desc->mEffect.clear();
2857 effect->setSuspended(false);
2858 }
2859}
2860
Eric Laurent5baf2af2013-09-12 17:37:00 -07002861bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002862{
2863 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002864 return isNonOffloadableEnabled_l();
2865}
2866
2867bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2868{
Eric Laurent813e2a72013-08-31 12:59:48 -07002869 size_t size = mEffects.size();
2870 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002871 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002872 return true;
2873 }
2874 }
2875 return false;
2876}
2877
Eric Laurentaaa44472014-09-12 17:41:50 -07002878void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2879{
2880 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002881 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002882}
2883
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002884void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2885{
2886 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2887 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2888 }
2889 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2890 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2891 }
2892}
2893
2894void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2895{
2896 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2897 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2898 }
2899 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2900 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2901 }
2902}
2903
2904bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002905{
2906 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002907 for (const auto &effect : mEffects) {
2908 if (effect->isProcessImplemented()) {
2909 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002910 }
2911 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002912 // Allow effects without processing.
2913 return true;
2914}
2915
2916bool AudioFlinger::EffectChain::isFastCompatible() const
2917{
2918 Mutex::Autolock _l(mLock);
2919 for (const auto &effect : mEffects) {
2920 if (effect->isProcessImplemented()
2921 && effect->isImplementationSoftware()) {
2922 return false;
2923 }
2924 }
2925 // Allow effects without processing or hw accelerated effects.
2926 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002927}
2928
2929// isCompatibleWithThread_l() must be called with thread->mLock held
2930bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2931{
2932 Mutex::Autolock _l(mLock);
2933 for (size_t i = 0; i < mEffects.size(); i++) {
2934 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2935 return false;
2936 }
2937 }
2938 return true;
2939}
2940
Eric Laurent6b446ce2019-12-13 10:56:31 -08002941// EffectCallbackInterface implementation
2942status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2943 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2944 sp<EffectHalInterface> *effect) {
2945 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002946 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002947 if (effectsFactory != 0) {
2948 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2949 }
2950 return status;
2951}
2952
2953bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002954 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002955 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002956 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002957}
2958
2959status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2960 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002961 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002962}
2963
2964status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2965 sp<EffectHalInterface> effect) {
2966 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002967 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002968 if (t == nullptr) {
2969 return result;
2970 }
2971 sp <StreamHalInterface> st = t->stream();
2972 if (st == nullptr) {
2973 return result;
2974 }
2975 result = st->addEffect(effect);
2976 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2977 return result;
2978}
2979
2980status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2981 sp<EffectHalInterface> effect) {
2982 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002983 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002984 if (t == nullptr) {
2985 return result;
2986 }
2987 sp <StreamHalInterface> st = t->stream();
2988 if (st == nullptr) {
2989 return result;
2990 }
2991 result = st->removeEffect(effect);
2992 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2993 return result;
2994}
2995
2996audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002997 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002998 if (t == nullptr) {
2999 return AUDIO_IO_HANDLE_NONE;
3000 }
3001 return t->id();
3002}
3003
3004bool AudioFlinger::EffectChain::EffectCallback::isOutput() 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 true;
3008 }
3009 return t->isOutput();
3010}
3011
3012bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003013 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003014}
3015
3016bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003017 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018}
3019
3020bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003021 switch (mThreadType) {
3022 case ThreadBase::OFFLOAD:
3023 case ThreadBase::MMAP_PLAYBACK:
3024 case ThreadBase::MMAP_CAPTURE:
3025 return true;
3026 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003027 return false;
3028 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003029}
3030
3031bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3032 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003033}
3034
3035uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003036 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003037 if (t == nullptr) {
3038 return 0;
3039 }
3040 return t->sampleRate();
3041}
3042
Eric Laurentf1f22e72021-07-13 14:04:14 +02003043audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3044 sp<ThreadBase> t = thread().promote();
3045 if (t == nullptr) {
3046 return AUDIO_CHANNEL_NONE;
3047 }
3048 sp<EffectChain> c = chain().promote();
3049 if (c == nullptr) {
3050 return AUDIO_CHANNEL_NONE;
3051 }
3052
Eric Laurentb62d0362021-10-26 17:40:18 +02003053 if (mThreadType == ThreadBase::SPATIALIZER) {
3054 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3055 if (c->isFirstEffect(id)) {
3056 return t->mixerChannelMask();
3057 } else {
3058 return t->channelMask();
3059 }
3060 } else if (!audio_is_global_session(c->sessionId())) {
3061 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3062 return t->mixerChannelMask();
3063 } else {
3064 return t->channelMask();
3065 }
3066 } else {
3067 return t->channelMask();
3068 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003069 } else {
3070 return t->channelMask();
3071 }
3072}
3073
3074uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003075 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003076}
3077
3078audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003079 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003080 if (t == nullptr) {
3081 return AUDIO_CHANNEL_NONE;
3082 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003083 sp<EffectChain> c = chain().promote();
3084 if (c == nullptr) {
3085 return AUDIO_CHANNEL_NONE;
3086 }
3087
3088 if (mThreadType == ThreadBase::SPATIALIZER) {
3089 if (!audio_is_global_session(c->sessionId())) {
3090 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3091 return t->mixerChannelMask();
3092 } else {
3093 return t->channelMask();
3094 }
3095 } else {
3096 return t->channelMask();
3097 }
3098 } else {
3099 return t->channelMask();
3100 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003101}
3102
Eric Laurentf1f22e72021-07-13 14:04:14 +02003103uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003104 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003105}
3106
jiabineb3bda02020-06-30 14:07:03 -07003107audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003108 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003109 if (t == nullptr) {
3110 return AUDIO_CHANNEL_NONE;
3111 }
3112 return t->hapticChannelMask();
3113}
3114
Eric Laurent6b446ce2019-12-13 10:56:31 -08003115size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003116 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003117 if (t == nullptr) {
3118 return 0;
3119 }
3120 return t->frameCount();
3121}
3122
3123uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003124 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003125 if (t == nullptr) {
3126 return 0;
3127 }
3128 return t->latency_l();
3129}
3130
3131void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003132 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003133 if (t == nullptr) {
3134 return;
3135 }
3136 t->setVolumeForOutput_l(left, right);
3137}
3138
3139void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003140 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003141 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003142 if (t == nullptr) {
3143 return;
3144 }
3145 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3146
Andy Hung328d6772021-01-12 12:32:21 -08003147 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003148 if (c == nullptr) {
3149 return;
3150 }
Eric Laurent41709552019-12-16 19:34:05 -08003151 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3152 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003153}
3154
Eric Laurent41709552019-12-16 19:34:05 -08003155void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003156 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003157 if (t == nullptr) {
3158 return;
3159 }
Eric Laurent41709552019-12-16 19:34:05 -08003160 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3161 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003162}
3163
Eric Laurent41709552019-12-16 19:34:05 -08003164void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003165 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3166
Andy Hung328d6772021-01-12 12:32:21 -08003167 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003168 if (t == nullptr) {
3169 return;
3170 }
3171 t->onEffectDisable();
3172}
3173
3174bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3175 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003176 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003177 if (t == nullptr) {
3178 return false;
3179 }
3180 t->disconnectEffectHandle(handle, unpinIfLast);
3181 return true;
3182}
3183
3184void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003185 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003186 if (c == nullptr) {
3187 return;
3188 }
3189 c->resetVolume_l();
3190
3191}
3192
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003193product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003194 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003195 if (c == nullptr) {
3196 return PRODUCT_STRATEGY_NONE;
3197 }
3198 return c->strategy();
3199}
3200
3201int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003202 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003203 if (c == nullptr) {
3204 return 0;
3205 }
3206 return c->activeTrackCnt();
3207}
3208
Eric Laurentb82e6b72019-11-22 17:25:04 -08003209
3210#undef LOG_TAG
3211#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3212
3213status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3214{
3215 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3216 Mutex::Autolock _l(mProxyLock);
3217 if (status == NO_ERROR) {
3218 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003219 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003220 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003221 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003222 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003223 bs = handle.second->disable(&status);
3224 }
3225 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003226 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003227 }
3228 }
3229 }
3230 ALOGV("%s enable %d status %d", __func__, enabled, status);
3231 return status;
3232}
3233
3234status_t AudioFlinger::DeviceEffectProxy::init(
3235 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3236//For all audio patches
3237//If src or sink device match
3238//If the effect is HW accelerated
3239// if no corresponding effect module
3240// Create EffectModule: mHalEffect
3241//Create and attach EffectHandle
3242//If the effect is not HW accelerated and the patch sink or src is a mixer port
3243// Create Effect on patch input or output thread on session -1
3244//Add EffectHandle to EffectHandle map of Effect Proxy:
3245 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3246 status_t status = NO_ERROR;
3247 for (auto &patch : patches) {
3248 status = onCreatePatch(patch.first, patch.second);
3249 ALOGV("%s onCreatePatch status %d", __func__, status);
3250 if (status == BAD_VALUE) {
3251 return status;
3252 }
3253 }
3254 return status;
3255}
3256
3257status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3258 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3259 status_t status = NAME_NOT_FOUND;
3260 sp<EffectHandle> handle;
3261 // only consider source[0] as this is the only "true" source of a patch
3262 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3263 ALOGV("%s source checkPort status %d", __func__, status);
3264 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3265 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3266 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3267 }
3268 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3269 Mutex::Autolock _l(mProxyLock);
3270 mEffectHandles.emplace(patchHandle, handle);
3271 }
3272 ALOGW_IF(status == BAD_VALUE,
3273 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3274
3275 return status;
3276}
3277
3278status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3279 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3280
3281 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3282 __func__, port->type, port->ext.device.type,
3283 port->ext.device.address, port->id, patch.isSoftware());
3284 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003285 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003286 return NAME_NOT_FOUND;
3287 }
3288 status_t status = NAME_NOT_FOUND;
3289
3290 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3291 Mutex::Autolock _l(mProxyLock);
3292 mDevicePort = *port;
3293 mHalEffect = new EffectModule(mMyCallback,
3294 const_cast<effect_descriptor_t *>(&mDescriptor),
3295 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3296 false /* pinned */, port->id);
3297 if (audio_is_input_device(mDevice.mType)) {
3298 mHalEffect->setInputDevice(mDevice);
3299 } else {
3300 mHalEffect->setDevices({mDevice});
3301 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003302 mHalEffect->configure();
3303
Eric Laurentde8caf42021-08-11 17:19:25 +02003304 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3305 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003306 status = (*handle)->initCheck();
3307 if (status == OK) {
3308 status = mHalEffect->addHandle((*handle).get());
3309 } else {
3310 mHalEffect.clear();
3311 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3312 }
3313 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3314 sp <ThreadBase> thread;
3315 if (audio_port_config_has_input_direction(port)) {
3316 if (patch.isSoftware()) {
3317 thread = patch.mRecord.thread();
3318 } else {
3319 thread = patch.thread().promote();
3320 }
3321 } else {
3322 if (patch.isSoftware()) {
3323 thread = patch.mPlayback.thread();
3324 } else {
3325 thread = patch.thread().promote();
3326 }
3327 }
3328 int enabled;
3329 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3330 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003331 &enabled, &status, false, false /*probe*/,
3332 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003333 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3334 } else {
3335 status = BAD_VALUE;
3336 }
3337 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003338 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003339 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003340 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003341 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003342 bs = (*handle)->disable(&status);
3343 }
3344 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003345 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003346 }
3347 }
3348 return status;
3349}
3350
3351void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003352 sp<EffectHandle> effect;
3353 {
3354 Mutex::Autolock _l(mProxyLock);
3355 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3356 effect = mEffectHandles.at(patchHandle);
3357 mEffectHandles.erase(patchHandle);
3358 }
3359 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003360}
3361
3362
3363size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3364{
3365 Mutex::Autolock _l(mProxyLock);
3366 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003367 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003368 mHalEffect.clear();
3369 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3370 }
3371 return mHalEffect == nullptr ? 0 : 1;
3372}
3373
3374status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3375 sp<EffectHalInterface> effect) {
3376 if (mHalEffect == nullptr) {
3377 return NO_INIT;
3378 }
3379 return mManagerCallback->addEffectToHal(
3380 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3381}
3382
3383status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3384 sp<EffectHalInterface> effect) {
3385 if (mHalEffect == nullptr) {
3386 return NO_INIT;
3387 }
3388 return mManagerCallback->removeEffectFromHal(
3389 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3390}
3391
3392bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3393 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3394 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3395 }
3396 return true;
3397}
3398
3399uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3400 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3401 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3402 return mDevicePort.sample_rate;
3403 }
3404 return DEFAULT_OUTPUT_SAMPLE_RATE;
3405}
3406
3407audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3408 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3409 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3410 return mDevicePort.channel_mask;
3411 }
3412 return AUDIO_CHANNEL_OUT_STEREO;
3413}
3414
3415uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3416 if (isOutput()) {
3417 return audio_channel_count_from_out_mask(channelMask());
3418 }
3419 return audio_channel_count_from_in_mask(channelMask());
3420}
3421
3422void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3423 const Vector<String16> args;
3424 EffectBase::dump(fd, args);
3425
3426 const bool locked = dumpTryLock(mProxyLock);
3427 if (!locked) {
3428 String8 result("DeviceEffectProxy may be deadlocked\n");
3429 write(fd, result.string(), result.size());
3430 }
3431
3432 String8 outStr;
3433 if (mHalEffect != nullptr) {
3434 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3435 } else {
3436 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3437 }
3438 write(fd, outStr.string(), outStr.size());
3439 outStr.clear();
3440
3441 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3442 write(fd, outStr.string(), outStr.size());
3443 outStr.clear();
3444
3445 for (const auto& iter : mEffectHandles) {
3446 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3447 write(fd, outStr.string(), outStr.size());
3448 outStr.clear();
3449 sp<EffectBase> effect = iter.second->effect().promote();
3450 if (effect != nullptr) {
3451 effect->dump(fd, args);
3452 }
3453 }
3454
3455 if (locked) {
3456 mLock.unlock();
3457 }
3458}
3459
3460#undef LOG_TAG
3461#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3462
3463int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3464 return mManagerCallback->newEffectId();
3465}
3466
3467
3468bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3469 EffectHandle *handle, bool unpinIfLast) {
3470 sp<EffectBase> effectBase = handle->effect().promote();
3471 if (effectBase == nullptr) {
3472 return false;
3473 }
3474
3475 sp<EffectModule> effect = effectBase->asEffectModule();
3476 if (effect == nullptr) {
3477 return false;
3478 }
3479
3480 // restore suspended effects if the disconnected handle was enabled and the last one.
3481 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3482 if (remove) {
3483 sp<DeviceEffectProxy> proxy = mProxy.promote();
3484 if (proxy != nullptr) {
3485 proxy->removeEffect(effect);
3486 }
3487 if (handle->enabled()) {
3488 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3489 }
3490 }
3491 return true;
3492}
3493
3494status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3495 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3496 sp<EffectHalInterface> *effect) {
3497 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3498}
3499
3500status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3501 sp<EffectHalInterface> effect) {
3502 sp<DeviceEffectProxy> proxy = mProxy.promote();
3503 if (proxy == nullptr) {
3504 return NO_INIT;
3505 }
3506 return proxy->addEffectToHal(effect);
3507}
3508
3509status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3510 sp<EffectHalInterface> effect) {
3511 sp<DeviceEffectProxy> proxy = mProxy.promote();
3512 if (proxy == nullptr) {
3513 return NO_INIT;
3514 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003515 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003516}
3517
3518bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3519 sp<DeviceEffectProxy> proxy = mProxy.promote();
3520 if (proxy == nullptr) {
3521 return true;
3522 }
3523 return proxy->isOutput();
3524}
3525
3526uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3527 sp<DeviceEffectProxy> proxy = mProxy.promote();
3528 if (proxy == nullptr) {
3529 return DEFAULT_OUTPUT_SAMPLE_RATE;
3530 }
3531 return proxy->sampleRate();
3532}
3533
Eric Laurentf1f22e72021-07-13 14:04:14 +02003534audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3535 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003536 sp<DeviceEffectProxy> proxy = mProxy.promote();
3537 if (proxy == nullptr) {
3538 return AUDIO_CHANNEL_OUT_STEREO;
3539 }
3540 return proxy->channelMask();
3541}
3542
Eric Laurentf1f22e72021-07-13 14:04:14 +02003543uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3544 sp<DeviceEffectProxy> proxy = mProxy.promote();
3545 if (proxy == nullptr) {
3546 return 2;
3547 }
3548 return proxy->channelCount();
3549}
3550
3551audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3552 sp<DeviceEffectProxy> proxy = mProxy.promote();
3553 if (proxy == nullptr) {
3554 return AUDIO_CHANNEL_OUT_STEREO;
3555 }
3556 return proxy->channelMask();
3557}
3558
3559uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003560 sp<DeviceEffectProxy> proxy = mProxy.promote();
3561 if (proxy == nullptr) {
3562 return 2;
3563 }
3564 return proxy->channelCount();
3565}
3566
Eric Laurent76c89f32021-12-03 17:13:23 +01003567void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3568 const sp<EffectBase>& effectBase) {
3569 sp<EffectModule> effect = effectBase->asEffectModule();
3570 if (effect == nullptr) {
3571 return;
3572 }
3573 effect->start();
3574}
3575
3576void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3577 const sp<EffectBase>& effectBase) {
3578 sp<EffectModule> effect = effectBase->asEffectModule();
3579 if (effect == nullptr) {
3580 return;
3581 }
3582 effect->stop();
3583}
3584
Glenn Kasten63238ef2015-03-02 15:50:29 -08003585} // namespace android