blob: 39feec40cb5307e10c88bb41c92134c0ec8f8344 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020027#include <system/audio_effects/effect_downmix.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070028#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070029#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070030#include <system/audio_effects/effect_ns.h>
Eric Laurentb62d0362021-10-26 17:40:18 +020031#include <system/audio_effects/effect_spatializer.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070032#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080033#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080034#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070035#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070037#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080038#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070039#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070040#include <media/audiohal/EffectHalInterface.h>
41#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungc747c532022-03-07 21:41:14 -080042#include <mediautils/MethodStatistics.h>
Andy Hungab7ef302018-05-15 19:35:29 -070043#include <mediautils/ServiceUtilities.h>
Andy Hunga2a1ac32022-03-18 16:12:11 -070044#include <mediautils/TimeCheck.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080045
46#include "AudioFlinger.h"
Andy Hungba8e52b2023-05-11 14:33:03 -070047#include "EffectConfiguration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080048
49// ----------------------------------------------------------------------------
50
51// Note: the following macro is used for extremely verbose logging message. In
52// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
53// 0; but one side effect of this is to turn all LOGV's as well. Some messages
54// are so verbose that we want to suppress them even when we have ALOG_ASSERT
55// turned on. Do not uncomment the #def below unless you really know what you
56// are doing and want to see all of the extremely verbose messages.
57//#define VERY_VERY_VERBOSE_LOGGING
58#ifdef VERY_VERY_VERBOSE_LOGGING
59#define ALOGVV ALOGV
60#else
61#define ALOGVV(a...) do { } while(0)
62#endif
63
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090064#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
65
Eric Laurentca7cc822012-11-19 14:55:58 -080066namespace android {
67
Andy Hung1131b6e2020-12-08 20:47:45 -080068using aidl_utils::statusTFromBinderStatus;
Andy Hungba8e52b2023-05-11 14:33:03 -070069using audioflinger::EffectConfiguration;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070070using binder::Status;
71
72namespace {
73
74// Append a POD value into a vector of bytes.
75template<typename T>
76void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
77 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
78 buffer->insert(buffer->end(), ar, ar + sizeof(T));
79}
80
81// Write a POD value into a vector of bytes (clears the previous buffer
82// content).
83template<typename T>
84void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
85 buffer->clear();
86 appendToBuffer(value, buffer);
87}
88
89} // namespace
90
Eric Laurentca7cc822012-11-19 14:55:58 -080091// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080092// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080093// ----------------------------------------------------------------------------
94
95#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080096#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080097
Eric Laurent41709552019-12-16 19:34:05 -080098AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080099 effect_descriptor_t *desc,
100 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800101 audio_session_t sessionId,
102 bool pinned)
103 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -0800104 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -0800105 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800106{
Eric Laurentca7cc822012-11-19 14:55:58 -0800107}
108
Eric Laurent41709552019-12-16 19:34:05 -0800109// must be called with EffectModule::mLock held
110status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800111{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800112
Eric Laurent41709552019-12-16 19:34:05 -0800113 ALOGV("setEnabled %p enabled %d", this, enabled);
114
115 if (enabled != isEnabled()) {
116 switch (mState) {
117 // going from disabled to enabled
118 case IDLE:
119 mState = STARTING;
120 break;
121 case STOPPED:
122 mState = RESTART;
123 break;
124 case STOPPING:
125 mState = ACTIVE;
126 break;
127
128 // going from enabled to disabled
129 case RESTART:
130 mState = STOPPED;
131 break;
132 case STARTING:
133 mState = IDLE;
134 break;
135 case ACTIVE:
136 mState = STOPPING;
137 break;
138 case DESTROYED:
139 return NO_ERROR; // simply ignore as we are being destroyed
140 }
141 for (size_t i = 1; i < mHandles.size(); i++) {
142 EffectHandle *h = mHandles[i];
143 if (h != NULL && !h->disconnected()) {
144 h->setEnabled(enabled);
145 }
146 }
147 }
148 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800149}
150
Eric Laurent41709552019-12-16 19:34:05 -0800151status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
152{
153 status_t status;
154 {
155 Mutex::Autolock _l(mLock);
156 status = setEnabled_l(enabled);
157 }
158 if (fromHandle) {
159 if (enabled) {
160 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700161 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800162 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700163 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800164 }
165 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700166 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800167 }
168 }
169 return status;
170}
171
172bool AudioFlinger::EffectBase::isEnabled() const
173{
174 switch (mState) {
175 case RESTART:
176 case STARTING:
177 case ACTIVE:
178 return true;
179 case IDLE:
180 case STOPPING:
181 case STOPPED:
182 case DESTROYED:
183 default:
184 return false;
185 }
186}
187
188void AudioFlinger::EffectBase::setSuspended(bool suspended)
189{
190 Mutex::Autolock _l(mLock);
191 mSuspended = suspended;
192}
193
194bool AudioFlinger::EffectBase::suspended() const
195{
196 Mutex::Autolock _l(mLock);
197 return mSuspended;
198}
199
200status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800201{
202 status_t status;
203
204 Mutex::Autolock _l(mLock);
205 int priority = handle->priority();
206 size_t size = mHandles.size();
207 EffectHandle *controlHandle = NULL;
208 size_t i;
209 for (i = 0; i < size; i++) {
210 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800211 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 continue;
213 }
214 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700215 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800216 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700217 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800218 if (h->priority() <= priority) {
219 break;
220 }
221 }
222 // if inserted in first place, move effect control from previous owner to this handle
223 if (i == 0) {
224 bool enabled = false;
225 if (controlHandle != NULL) {
226 enabled = controlHandle->enabled();
227 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
228 }
229 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
230 status = NO_ERROR;
231 } else {
232 status = ALREADY_EXISTS;
233 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700234 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800235 mHandles.insertAt(handle, i);
236 return status;
237}
238
Eric Laurent41709552019-12-16 19:34:05 -0800239status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700240{
241 status_t status = NO_ERROR;
242 bool doRegister = false;
243 bool registered = false;
244 bool doEnable = false;
245 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700246 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800247 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700248
249 {
250 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200251
252 if ((isInternal_l() && !mPolicyRegistered)
253 || !getCallback()->isAudioPolicyReady()) {
254 return NO_ERROR;
255 }
256
Eric Laurent6c796322019-04-09 14:13:17 -0700257 // register effect when first handle is attached and unregister when last handle is removed
258 if (mPolicyRegistered != mHandles.size() > 0) {
259 doRegister = true;
260 mPolicyRegistered = mHandles.size() > 0;
261 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700262 const auto callback = getCallback();
263 io = callback->io();
264 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700265 }
266 }
267 // enable effect when registered according to enable state requested by controlling handle
268 if (mHandles.size() > 0) {
269 EffectHandle *handle = controlHandle_l();
270 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
271 doEnable = true;
272 mPolicyEnabled = handle->enabled();
273 }
274 }
275 registered = mPolicyRegistered;
276 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100277 // The simultaneous release of two EffectHandles with the same EffectModule
278 // may cause us to call this method at the same time.
279 // This may deadlock under some circumstances (b/180941720). Avoid this.
280 if (!doRegister && !(registered && doEnable)) {
281 return NO_ERROR;
282 }
Eric Laurent6c796322019-04-09 14:13:17 -0700283 }
fengjnlan46407562022-09-07 16:20:01 +0800284 mPolicyLock.lock();
Eric Laurent6c796322019-04-09 14:13:17 -0700285 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
286 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
287 if (doRegister) {
288 if (registered) {
289 status = AudioSystem::registerEffect(
290 &mDescriptor,
291 io,
292 strategy,
293 mSessionId,
294 mId);
295 } else {
296 status = AudioSystem::unregisterEffect(mId);
297 }
298 }
299 if (registered && doEnable) {
300 status = AudioSystem::setEffectEnabled(mId, enabled);
301 }
302 mPolicyLock.unlock();
303
304 return status;
305}
306
307
Eric Laurent41709552019-12-16 19:34:05 -0800308ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800309{
310 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800311 return removeHandle_l(handle);
312}
313
Eric Laurent41709552019-12-16 19:34:05 -0800314ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800315{
Eric Laurentca7cc822012-11-19 14:55:58 -0800316 size_t size = mHandles.size();
317 size_t i;
318 for (i = 0; i < size; i++) {
319 if (mHandles[i] == handle) {
320 break;
321 }
322 }
323 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800324 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
325 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800326 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800327 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800328
329 mHandles.removeAt(i);
330 // if removed from first place, move effect control from this handle to next in line
331 if (i == 0) {
332 EffectHandle *h = controlHandle_l();
333 if (h != NULL) {
334 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
335 }
336 }
337
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530338 // Prevent calls to process() and other functions on effect interface from now on.
339 // The effect engine will be released by the destructor when the last strong reference on
340 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800341 if (mHandles.size() == 0 && !mPinned) {
342 mState = DESTROYED;
343 }
344
345 return mHandles.size();
346}
347
348// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800349AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800350{
351 // the first valid handle in the list has control over the module
352 for (size_t i = 0; i < mHandles.size(); i++) {
353 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800354 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800355 return h;
356 }
357 }
358
359 return NULL;
360}
361
Eric Laurentf10c7092016-12-06 17:09:56 -0800362// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800363ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800364{
Andy Hungfda44002021-06-03 17:23:16 -0700365 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800366 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700367 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800368 return mHandles.size();
369 }
370
Eric Laurentf10c7092016-12-06 17:09:56 -0800371 Mutex::Autolock _l(mLock);
372 ssize_t numHandles = removeHandle_l(handle);
373 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800374 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700375 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800376 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800377 }
378 return numHandles;
379}
380
Eric Laurent41709552019-12-16 19:34:05 -0800381bool AudioFlinger::EffectBase::purgeHandles()
382{
383 bool enabled = false;
384 Mutex::Autolock _l(mLock);
385 EffectHandle *handle = controlHandle_l();
386 if (handle != NULL) {
387 enabled = handle->enabled();
388 }
389 mHandles.clear();
390 return enabled;
391}
392
393void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700394 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800395}
396
397static String8 effectFlagsToString(uint32_t flags) {
398 String8 s;
399
400 s.append("conn. mode: ");
401 switch (flags & EFFECT_FLAG_TYPE_MASK) {
402 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
403 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
404 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
405 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
406 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
407 default: s.append("unknown/reserved"); break;
408 }
409 s.append(", ");
410
411 s.append("insert pref: ");
412 switch (flags & EFFECT_FLAG_INSERT_MASK) {
413 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
414 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
415 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
416 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
417 default: s.append("unknown/reserved"); break;
418 }
419 s.append(", ");
420
421 s.append("volume mgmt: ");
422 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
423 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
424 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
425 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
426 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
427 default: s.append("unknown/reserved"); break;
428 }
429 s.append(", ");
430
431 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
432 if (devind) {
433 s.append("device indication: ");
434 switch (devind) {
435 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
436 default: s.append("unknown/reserved"); break;
437 }
438 s.append(", ");
439 }
440
441 s.append("input mode: ");
442 switch (flags & EFFECT_FLAG_INPUT_MASK) {
443 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
444 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
445 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
446 default: s.append("not set"); break;
447 }
448 s.append(", ");
449
450 s.append("output mode: ");
451 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
452 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
453 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
454 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
455 default: s.append("not set"); break;
456 }
457 s.append(", ");
458
459 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
460 if (accel) {
461 s.append("hardware acceleration: ");
462 switch (accel) {
463 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
464 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
465 default: s.append("unknown/reserved"); break;
466 }
467 s.append(", ");
468 }
469
470 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
471 if (modeind) {
472 s.append("mode indication: ");
473 switch (modeind) {
474 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
475 default: s.append("unknown/reserved"); break;
476 }
477 s.append(", ");
478 }
479
480 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
481 if (srcind) {
482 s.append("source indication: ");
483 switch (srcind) {
484 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
485 default: s.append("unknown/reserved"); break;
486 }
487 s.append(", ");
488 }
489
490 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
491 s.append("offloadable, ");
492 }
493
494 int len = s.length();
495 if (s.length() > 2) {
496 (void) s.lockBuffer(len);
497 s.unlockBuffer(len - 2);
498 }
499 return s;
500}
501
502void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
Andy Hung920f6572022-10-06 12:09:49 -0700503NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurent41709552019-12-16 19:34:05 -0800504{
505 String8 result;
506
507 result.appendFormat("\tEffect ID %d:\n", mId);
508
509 bool locked = AudioFlinger::dumpTryLock(mLock);
510 // failed to lock - AudioFlinger is probably deadlocked
511 if (!locked) {
512 result.append("\t\tCould not lock Fx mutex:\n");
513 }
514
515 result.append("\t\tSession State Registered Enabled Suspended:\n");
516 result.appendFormat("\t\t%05d %03d %s %s %s\n",
517 mSessionId, mState, mPolicyRegistered ? "y" : "n",
518 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
519
520 result.append("\t\tDescriptor:\n");
521 char uuidStr[64];
522 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
523 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
524 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
525 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
526 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
527 mDescriptor.apiVersion,
528 mDescriptor.flags,
529 effectFlagsToString(mDescriptor.flags).string());
530 result.appendFormat("\t\t- name: %s\n",
531 mDescriptor.name);
532
533 result.appendFormat("\t\t- implementor: %s\n",
534 mDescriptor.implementor);
535
536 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
537 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
538 char buffer[256];
539 for (size_t i = 0; i < mHandles.size(); ++i) {
540 EffectHandle *handle = mHandles[i];
541 if (handle != NULL && !handle->disconnected()) {
542 handle->dumpToBuffer(buffer, sizeof(buffer));
543 result.append(buffer);
544 }
545 }
546 if (locked) {
547 mLock.unlock();
548 }
549
550 write(fd, result.string(), result.length());
551}
552
553// ----------------------------------------------------------------------------
554// EffectModule implementation
555// ----------------------------------------------------------------------------
556
557#undef LOG_TAG
558#define LOG_TAG "AudioFlinger::EffectModule"
559
560AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
561 effect_descriptor_t *desc,
562 int id,
563 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800564 bool pinned,
565 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800566 : EffectBase(callback, desc, id, sessionId, pinned),
567 // clear mConfig to ensure consistent initial value of buffer framecount
568 // in case buffers are associated by setInBuffer() or setOutBuffer()
569 // prior to configure().
570 mConfig{{}, {}},
571 mStatus(NO_INIT),
572 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
573 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800574 mOffloaded(false),
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000575 mIsOutput(false)
Eric Laurent41709552019-12-16 19:34:05 -0800576#ifdef FLOAT_EFFECT_CHAIN
577 , mSupportsFloat(false)
578#endif
579{
580 ALOGV("Constructor %p pinned %d", this, pinned);
581 int lStatus;
582
583 // create effect engine from effect factory
584 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800585 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800586 if (mStatus != NO_ERROR) {
587 return;
588 }
589 lStatus = init();
590 if (lStatus < 0) {
591 mStatus = lStatus;
592 goto Error;
593 }
594
595 setOffloaded(callback->isOffload(), callback->io());
596 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
597
598 return;
599Error:
600 mEffectInterface.clear();
601 ALOGV("Constructor Error %d", mStatus);
602}
603
604AudioFlinger::EffectModule::~EffectModule()
605{
606 ALOGV("Destructor %p", this);
607 if (mEffectInterface != 0) {
608 char uuidStr[64];
609 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
610 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
611 this, uuidStr);
612 release_l();
613 }
614
615}
616
Eric Laurentfa1e1232016-08-02 19:01:49 -0700617bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800618 Mutex::Autolock _l(mLock);
619
Eric Laurentfa1e1232016-08-02 19:01:49 -0700620 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800621 switch (mState) {
622 case RESTART:
623 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700624 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800625
626 case STARTING:
627 // clear auxiliary effect input buffer for next accumulation
628 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
629 memset(mConfig.inputCfg.buffer.raw,
630 0,
631 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
632 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700633 if (start_l() == NO_ERROR) {
634 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700635 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700636 } else {
637 mState = IDLE;
638 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800639 break;
640 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900641 // volume control for offload and direct threads must take effect immediately.
642 if (stop_l() == NO_ERROR
643 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700644 mDisableWaitCnt = mMaxDisableWaitCnt;
645 } else {
646 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
647 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800648 mState = STOPPED;
649 break;
650 case STOPPED:
651 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
652 // turn off sequence.
653 if (--mDisableWaitCnt == 0) {
654 reset_l();
655 mState = IDLE;
656 }
657 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200658 case ACTIVE:
659 for (size_t i = 0; i < mHandles.size(); i++) {
660 if (!mHandles[i]->disconnected()) {
661 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
662 }
663 }
664 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800665 default: //IDLE , ACTIVE, DESTROYED
666 break;
667 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700668
669 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800670}
671
672void AudioFlinger::EffectModule::process()
673{
674 Mutex::Autolock _l(mLock);
675
Mikhail Naganov022b9952017-01-04 16:36:51 -0800676 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800677 return;
678 }
679
rago94a1ee82017-07-21 15:11:02 -0700680 const uint32_t inChannelCount =
681 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
682 const uint32_t outChannelCount =
683 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
684 const bool auxType =
685 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
686
Andy Hungfa69ca32017-11-30 10:07:53 -0800687 // safeInputOutputSampleCount is 0 if the channel count between input and output
688 // buffers do not match. This prevents automatic accumulation or copying between the
689 // input and output effect buffers without an intermediary effect process.
690 // TODO: consider implementing channel conversion.
691 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700692 mInChannelCountRequested != mOutChannelCountRequested ? 0
693 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 mConfig.inputCfg.buffer.frameCount,
695 mConfig.outputCfg.buffer.frameCount);
696 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
697#ifdef FLOAT_EFFECT_CHAIN
698 accumulate_float(
699 mConfig.outputCfg.buffer.f32,
700 mConfig.inputCfg.buffer.f32,
701 safeInputOutputSampleCount);
702#else
703 accumulate_i16(
704 mConfig.outputCfg.buffer.s16,
705 mConfig.inputCfg.buffer.s16,
706 safeInputOutputSampleCount);
707#endif
708 };
709 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
710#ifdef FLOAT_EFFECT_CHAIN
711 memcpy(
712 mConfig.outputCfg.buffer.f32,
713 mConfig.inputCfg.buffer.f32,
714 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
715
716#else
717 memcpy(
718 mConfig.outputCfg.buffer.s16,
719 mConfig.inputCfg.buffer.s16,
720 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
721#endif
722 };
723
Eric Laurentca7cc822012-11-19 14:55:58 -0800724 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700725 int ret;
726 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700727 if (auxType) {
728 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800729 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700730#ifdef FLOAT_EFFECT_CHAIN
731 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800732#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700733 // Do in-place float conversion for auxiliary effect input buffer.
734 static_assert(sizeof(float) <= sizeof(int32_t),
735 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
736
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 memcpy_to_float_from_q4_27(
738 mConfig.inputCfg.buffer.f32,
739 mConfig.inputCfg.buffer.s32,
740 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800741#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 } else
Andy Hung116a4982017-11-30 10:15:08 -0800743#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 {
Andy Hung116a4982017-11-30 10:15:08 -0800745#ifdef FLOAT_AUX
746 memcpy_to_i16_from_float(
747 mConfig.inputCfg.buffer.s16,
748 mConfig.inputCfg.buffer.f32,
749 mConfig.inputCfg.buffer.frameCount);
750#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 memcpy_to_i16_from_q4_27(
752 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700753 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800754 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800755#endif
rago94a1ee82017-07-21 15:11:02 -0700756 }
rago94a1ee82017-07-21 15:11:02 -0700757 }
758#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800759 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
760 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
761
762 if (!auxType && mInChannelCountRequested != inChannelCount) {
763 adjust_channels(
764 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
765 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
766 sizeof(float),
767 sizeof(float)
768 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
769 inBuffer = mInConversionBuffer;
770 }
771 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
772 && mOutChannelCountRequested != outChannelCount) {
773 adjust_selected_channels(
774 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
775 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
776 sizeof(float),
777 sizeof(float)
778 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
779 outBuffer = mOutConversionBuffer;
780 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
782 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800783 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800784 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
785 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700786 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 memcpy_to_i16_from_float(
788 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800791 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700792 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800794 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800795 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
796 goto data_bypass;
797 }
798 memcpy_to_i16_from_float(
799 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800800 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800801 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800802 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700803 }
804 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800805#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800806 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800807#ifdef FLOAT_EFFECT_CHAIN
808 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800809 sp<EffectBufferHalInterface> target =
810 mOutChannelCountRequested != outChannelCount
811 ? mOutConversionBuffer : mOutBuffer;
812
Andy Hungfa69ca32017-11-30 10:07:53 -0800813 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800814 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800815 mOutConversionBuffer->audioBuffer()->s16,
816 outChannelCount * mConfig.outputCfg.buffer.frameCount);
817 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800818 if (mOutChannelCountRequested != outChannelCount) {
819 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
820 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
821 sizeof(float),
822 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
823 }
rago94a1ee82017-07-21 15:11:02 -0700824#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700825 } else {
rago94a1ee82017-07-21 15:11:02 -0700826#ifdef FLOAT_EFFECT_CHAIN
827 data_bypass:
828#endif
829 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800830 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700831 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800832 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700833 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800834 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700835 }
836 }
837 ret = -ENODATA;
838 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800839
Eric Laurentca7cc822012-11-19 14:55:58 -0800840 // force transition to IDLE state when engine is ready
841 if (mState == STOPPED && ret == -ENODATA) {
842 mDisableWaitCnt = 1;
843 }
844
845 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700846 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800847#ifdef FLOAT_AUX
848 const size_t size =
849 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
850#else
rago94a1ee82017-07-21 15:11:02 -0700851 const size_t size =
852 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800853#endif
rago94a1ee82017-07-21 15:11:02 -0700854 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800855 }
856 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700857 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800858 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
859 // If an insert effect is idle and input buffer is different from output buffer,
860 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700861 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700862 // similar handling with data_bypass above.
863 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
864 accumulateInputToOutput();
865 } else { // EFFECT_BUFFER_ACCESS_WRITE
866 copyInputToOutput();
867 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800868 }
869 }
870}
871
872void AudioFlinger::EffectModule::reset_l()
873{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700874 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800875 return;
876 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700877 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800878}
879
880status_t AudioFlinger::EffectModule::configure()
881{
rago94a1ee82017-07-21 15:11:02 -0700882 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700883 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700884 uint32_t size;
885 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700886 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700887
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700888 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700889 status = NO_INIT;
890 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800891 }
892
Eric Laurentca7cc822012-11-19 14:55:58 -0800893 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800894 // TODO: handle configuration of input (record) SW effects above the HAL,
895 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
896 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700897 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200898 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800899 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200900 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800901
902 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800903 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
904 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
905 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
906 mConfig.inputCfg.channels);
907 }
908#ifndef MULTICHANNEL_EFFECT_CHAIN
909 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
910 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
911 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
912 mConfig.outputCfg.channels);
913 }
914#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800915 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800916#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700917 // TODO: Update this logic when multichannel effects are implemented.
918 // For offloaded tracks consider mono output as stereo for proper effect initialization
919 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
920 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
921 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
922 ALOGV("Overriding effect input and output as STEREO");
923 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800924#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 }
jiabineb3bda02020-06-30 14:07:03 -0700926 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700927 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700928 mConfig.inputCfg.channels |= hapticChannelMask;
929 mConfig.outputCfg.channels |= hapticChannelMask;
930 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 mInChannelCountRequested =
932 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
933 mOutChannelCountRequested =
934 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700935
rago94a1ee82017-07-21 15:11:02 -0700936 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
937 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900938
939 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700940 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900941 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
942 ALOGV("Overriding effect input as 48kHz");
943 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700944 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900945 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800946 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
947 mConfig.inputCfg.bufferProvider.cookie = NULL;
948 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
949 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
950 mConfig.outputCfg.bufferProvider.cookie = NULL;
951 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
952 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
953 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
954 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800955 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800956 // always overwrites output buffer: input buffer == output buffer
957 // - in other sessions:
958 // last effect in the chain accumulates in output buffer: input buffer != output buffer
959 // other effect: overwrites output buffer: input buffer == output buffer
960 // Auxiliary effect:
961 // accumulates in output buffer: input buffer != output buffer
962 // Therefore: accumulate <=> input buffer != output buffer
Andy Hung799c8d02021-10-28 17:05:40 -0700963 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
965 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700966 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800967 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000968 mIsOutput = callback->isOutput();
Eric Laurentca7cc822012-11-19 14:55:58 -0800969
Eric Laurent6b446ce2019-12-13 10:56:31 -0800970 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700971 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800972 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800973
974 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700975 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700976 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800977 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700978 &mConfig,
979 &size,
980 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700981 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800982 status = cmdStatus;
983 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800984
985#ifdef MULTICHANNEL_EFFECT_CHAIN
986 if (status != NO_ERROR &&
Andy Hungba8e52b2023-05-11 14:33:03 -0700987 EffectConfiguration::isHidl() && // only HIDL effects support channel conversion
Mikhail Naganov8d7da002022-04-19 21:21:23 +0000988 mIsOutput &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800989 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
990 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
991 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700992 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
993 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800994 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
995 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
996 }
997 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
998 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
999 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
1000 }
1001 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -07001002 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -08001003 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -07001004 &mConfig,
1005 &size,
1006 &cmdStatus);
1007 if (status == NO_ERROR) {
1008 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001009 }
1010 }
1011#endif
1012
1013#ifdef FLOAT_EFFECT_CHAIN
1014 if (status == NO_ERROR) {
1015 mSupportsFloat = true;
1016 }
1017
Andy Hungba8e52b2023-05-11 14:33:03 -07001018 // only HIDL effects support integer conversion.
1019 if (status != NO_ERROR && EffectConfiguration::isHidl()) {
Andy Hung9aad48c2017-11-29 10:29:19 -08001020 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1021 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1022 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1023 size = sizeof(int);
1024 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1025 sizeof(mConfig),
1026 &mConfig,
1027 &size,
1028 &cmdStatus);
1029 if (status == NO_ERROR) {
1030 status = cmdStatus;
1031 }
1032 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001033 mSupportsFloat = false;
1034 ALOGVV("config worked with 16 bit");
1035 } else {
1036 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001037 }
rago94a1ee82017-07-21 15:11:02 -07001038 }
1039#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001040
rago94a1ee82017-07-21 15:11:02 -07001041 if (status == NO_ERROR) {
1042 // Establish Buffer strategy
1043 setInBuffer(mInBuffer);
1044 setOutBuffer(mOutBuffer);
1045
1046 // Update visualizer latency
1047 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1048 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1049 effect_param_t *p = (effect_param_t *)buf32;
1050
1051 p->psize = sizeof(uint32_t);
1052 p->vsize = sizeof(uint32_t);
1053 size = sizeof(int);
1054 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1055
Andy Hungfda44002021-06-03 17:23:16 -07001056 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001057
1058 *((int32_t *)p->data + 1)= latency;
1059 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1060 sizeof(effect_param_t) + 8,
1061 &buf32,
1062 &size,
1063 &cmdStatus);
1064 }
jiabin4e246532022-08-23 16:37:30 -07001065
1066 if (isVolumeControl()) {
1067 // Force initializing the volume as 0 for volume control effect for safer ramping
1068 uint32_t left = 0;
1069 uint32_t right = 0;
1070 setVolumeInternal(&left, &right, true /*controller*/);
1071 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001072 }
1073
Andy Hung05083ac2017-12-14 15:00:28 -08001074 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1075 mMaxDisableWaitCnt = (uint32_t)std::max(
1076 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1077 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1078 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001079
Eric Laurentd0ebb532013-04-02 16:41:41 -07001080exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001081 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001082 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001083 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001084 return status;
1085}
1086
1087status_t AudioFlinger::EffectModule::init()
1088{
1089 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001090 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001091 return NO_INIT;
1092 }
1093 status_t cmdStatus;
1094 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001095 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1096 0,
1097 NULL,
1098 &size,
1099 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 if (status == 0) {
1101 status = cmdStatus;
1102 }
1103 return status;
1104}
1105
Eric Laurent1b928682014-10-02 19:41:47 -07001106void AudioFlinger::EffectModule::addEffectToHal_l()
1107{
1108 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1109 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001110 if (mCurrentHalStream == getCallback()->io()) {
David Li6c8ac4b2021-06-22 22:17:52 +08001111 return;
1112 }
1113
Andy Hungfda44002021-06-03 17:23:16 -07001114 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001115 mCurrentHalStream = getCallback()->io();
Eric Laurent1b928682014-10-02 19:41:47 -07001116 }
1117}
1118
Eric Laurentfa1e1232016-08-02 19:01:49 -07001119// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001120status_t AudioFlinger::EffectModule::start()
1121{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001122 status_t status;
1123 {
1124 Mutex::Autolock _l(mLock);
1125 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001126 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001127 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001128 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001129 }
1130 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001131}
1132
1133status_t AudioFlinger::EffectModule::start_l()
1134{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001135 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 return NO_INIT;
1137 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001138 if (mStatus != NO_ERROR) {
1139 return mStatus;
1140 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001141 status_t cmdStatus;
1142 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001143 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1144 0,
1145 NULL,
1146 &size,
1147 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001148 if (status == 0) {
1149 status = cmdStatus;
1150 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001151 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001152 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001153 }
1154 return status;
1155}
1156
1157status_t AudioFlinger::EffectModule::stop()
1158{
1159 Mutex::Autolock _l(mLock);
1160 return stop_l();
1161}
1162
1163status_t AudioFlinger::EffectModule::stop_l()
1164{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001165 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001166 return NO_INIT;
1167 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001168 if (mStatus != NO_ERROR) {
1169 return mStatus;
1170 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001171 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001173
1174 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001175 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1176 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1177 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001178 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001179 mSetVolumeReentrantTid = INVALID_PID;
1180 }
1181
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001182 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1183 0,
1184 NULL,
1185 &size,
1186 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001187 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001188 status = cmdStatus;
1189 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001190 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001191 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001192 }
1193 return status;
1194}
1195
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001196// must be called with EffectChain::mLock held
1197void AudioFlinger::EffectModule::release_l()
1198{
1199 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001200 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001201 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001202 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001203 mEffectInterface.clear();
1204 }
1205}
1206
Eric Laurent6b446ce2019-12-13 10:56:31 -08001207status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001208{
1209 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1210 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent9f57aa82023-03-17 19:28:37 +01001211 if (mCurrentHalStream != getCallback()->io()) {
1212 return (mCurrentHalStream == AUDIO_IO_HANDLE_NONE) ? NO_ERROR : INVALID_OPERATION;
David Li6c8ac4b2021-06-22 22:17:52 +08001213 }
Andy Hungfda44002021-06-03 17:23:16 -07001214 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurent9f57aa82023-03-17 19:28:37 +01001215 mCurrentHalStream = AUDIO_IO_HANDLE_NONE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001216 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001217 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001218}
1219
Andy Hunge4a1d912016-08-17 14:11:13 -07001220// round up delta valid if value and divisor are positive.
1221template <typename T>
1222static T roundUpDelta(const T &value, const T &divisor) {
1223 T remainder = value % divisor;
1224 return remainder == 0 ? 0 : divisor - remainder;
1225}
1226
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001227status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1228 const std::vector<uint8_t>& cmdData,
1229 int32_t maxReplySize,
1230 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001231{
1232 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001233 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001234
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001235 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001236 return NO_INIT;
1237 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001238 if (mStatus != NO_ERROR) {
1239 return mStatus;
1240 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001241 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1242 return -EINVAL;
1243 }
1244 size_t cmdSize = cmdData.size();
1245 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1246 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1247 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001248 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001249 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001250 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001251 android_errorWriteLog(0x534e4554, "33003822");
1252 return -EINVAL;
1253 }
1254 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001255 (maxReplySize < static_cast<signed>(sizeof(effect_param_t)) ||
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001256 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001257 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001258 return -EINVAL;
1259 }
ragoe2759072016-11-22 18:02:48 -08001260 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung920f6572022-10-06 12:09:49 -07001261 (static_cast<signed>(sizeof(effect_param_t)) > maxReplySize
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001262 || param->psize > maxReplySize - sizeof(effect_param_t)
1263 || param->vsize > maxReplySize - sizeof(effect_param_t)
1264 - param->psize
1265 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1266 maxReplySize
1267 - sizeof(effect_param_t)
1268 - param->psize
1269 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001270 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1271 android_errorWriteLog(0x534e4554, "32705438");
1272 return -EINVAL;
1273 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001274 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001275 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1276 && // DEFERRED not generally used
1277 (param == nullptr
1278 || param->psize > cmdSize - sizeof(effect_param_t)
1279 || param->vsize > cmdSize - sizeof(effect_param_t)
1280 - param->psize
1281 || roundUpDelta(param->psize,
1282 (uint32_t) sizeof(int)) >
1283 cmdSize
1284 - sizeof(effect_param_t)
1285 - param->psize
1286 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001287 android_errorWriteLog(0x534e4554, "30204301");
1288 return -EINVAL;
1289 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001290 uint32_t replySize = maxReplySize;
1291 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001292 status_t status = mEffectInterface->command(cmdCode,
1293 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001294 const_cast<uint8_t*>(cmdData.data()),
1295 &replySize,
1296 reply->data());
1297 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001298 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 for (size_t i = 1; i < mHandles.size(); i++) {
1300 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001301 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001302 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001303 }
1304 }
1305 }
1306 return status;
1307}
1308
Eric Laurentca7cc822012-11-19 14:55:58 -08001309bool AudioFlinger::EffectModule::isProcessEnabled() const
1310{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001311 if (mStatus != NO_ERROR) {
1312 return false;
1313 }
1314
Eric Laurentca7cc822012-11-19 14:55:58 -08001315 switch (mState) {
1316 case RESTART:
1317 case ACTIVE:
1318 case STOPPING:
1319 case STOPPED:
1320 return true;
1321 case IDLE:
1322 case STARTING:
1323 case DESTROYED:
1324 default:
1325 return false;
1326 }
1327}
1328
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001329bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1330{
Andy Hungfda44002021-06-03 17:23:16 -07001331 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001332}
1333
1334bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1335{
1336 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1337}
1338
Mikhail Naganov022b9952017-01-04 16:36:51 -08001339void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001340 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001341
1342 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001343 if (buffer != 0) {
1344 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1345 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1346 } else {
1347 mConfig.inputCfg.buffer.raw = NULL;
1348 }
1349 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001350 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001351
1352#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001353 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001354 // Theoretically insert effects can also do in-place conversions (destroying
1355 // the original buffer) when the output buffer is identical to the input buffer,
1356 // but we don't optimize for it here.
1357 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001358 const uint32_t inChannelCount =
1359 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1360 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001361 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001362 // we need to translate - create hidl shared buffer and intercept
1363 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001364 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1365 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1366 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001367
1368 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1369 __func__, inChannels, inFrameCount, size);
1370
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001371 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001372 || size > mInConversionBuffer->getSize())) {
1373 mInConversionBuffer.clear();
1374 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001375 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001376 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001377 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001378 mInConversionBuffer->setFrameCount(inFrameCount);
1379 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001380 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001381 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001382 }
1383 }
1384#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001385}
1386
1387void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001388 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001389
1390 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001391 if (buffer != 0) {
1392 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1393 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1394 } else {
1395 mConfig.outputCfg.buffer.raw = NULL;
1396 }
1397 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001398 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001399
1400#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001401 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001402 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001403 const uint32_t outChannelCount =
1404 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1405 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001406 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001407 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001408 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1409 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1410 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001411
1412 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1413 __func__, outChannels, outFrameCount, size);
1414
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001415 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001416 || size > mOutConversionBuffer->getSize())) {
1417 mOutConversionBuffer.clear();
1418 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001419 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001420 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001421 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001422 mOutConversionBuffer->setFrameCount(outFrameCount);
1423 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001424 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001425 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001426 }
1427 }
1428#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001429}
1430
Eric Laurentca7cc822012-11-19 14:55:58 -08001431status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1432{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001433 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001434 if (mStatus != NO_ERROR) {
1435 return mStatus;
1436 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001437 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001438 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1439 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1440 if (isProcessEnabled() &&
1441 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001442 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1443 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
jiabin4e246532022-08-23 16:37:30 -07001444 status = setVolumeInternal(left, right, controller);
1445 }
1446 return status;
1447}
1448
1449status_t AudioFlinger::EffectModule::setVolumeInternal(
1450 uint32_t *left, uint32_t *right, bool controller) {
1451 uint32_t volume[2] = {*left, *right};
1452 uint32_t *pVolume = controller ? volume : nullptr;
1453 uint32_t size = sizeof(volume);
1454 status_t status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1455 size,
1456 volume,
1457 &size,
1458 pVolume);
1459 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1460 *left = volume[0];
1461 *right = volume[1];
Eric Laurentca7cc822012-11-19 14:55:58 -08001462 }
1463 return status;
1464}
1465
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001466void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1467{
Zhou Songd505c642020-02-20 16:35:37 +08001468 // for offload or direct thread, if the effect chain has non-offloadable
1469 // effect and any effect module within the chain has volume control, then
1470 // volume control is delegated to effect, otherwise, set volume to hal.
1471 if (mEffectCallback->isOffloadOrDirect() &&
1472 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001473 float vol_l = (float)left / (1 << 24);
1474 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001475 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001476 }
1477}
1478
jiabin8f278ee2019-11-11 12:16:27 -08001479status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1480 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001481{
jiabin8f278ee2019-11-11 12:16:27 -08001482 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1483 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 return NO_ERROR;
1485 }
1486
1487 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001488 if (mStatus != NO_ERROR) {
1489 return mStatus;
1490 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001491 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001492 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 status_t cmdStatus;
1494 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001495 // FIXME: use audio device types and addresses when the hal interface is ready.
1496 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001497 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001498 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001499 &size,
1500 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 }
1502 return status;
1503}
1504
jiabin8f278ee2019-11-11 12:16:27 -08001505status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1506{
1507 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1508}
1509
1510status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1511{
1512 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1513}
1514
Eric Laurentca7cc822012-11-19 14:55:58 -08001515status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1516{
1517 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001518 if (mStatus != NO_ERROR) {
1519 return mStatus;
1520 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001521 status_t status = NO_ERROR;
1522 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1523 status_t cmdStatus;
1524 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001525 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1526 sizeof(audio_mode_t),
1527 &mode,
1528 &size,
1529 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 if (status == NO_ERROR) {
1531 status = cmdStatus;
1532 }
1533 }
1534 return status;
1535}
1536
1537status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1538{
1539 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001540 if (mStatus != NO_ERROR) {
1541 return mStatus;
1542 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001543 status_t status = NO_ERROR;
1544 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1545 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001546 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1547 sizeof(audio_source_t),
1548 &source,
1549 &size,
1550 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001551 }
1552 return status;
1553}
1554
Eric Laurent5baf2af2013-09-12 17:37:00 -07001555status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1556{
1557 Mutex::Autolock _l(mLock);
1558 if (mStatus != NO_ERROR) {
1559 return mStatus;
1560 }
1561 status_t status = NO_ERROR;
1562 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1563 status_t cmdStatus;
1564 uint32_t size = sizeof(status_t);
1565 effect_offload_param_t cmd;
1566
1567 cmd.isOffload = offloaded;
1568 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001569 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1570 sizeof(effect_offload_param_t),
1571 &cmd,
1572 &size,
1573 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001574 if (status == NO_ERROR) {
1575 status = cmdStatus;
1576 }
1577 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1578 } else {
1579 if (offloaded) {
1580 status = INVALID_OPERATION;
1581 }
1582 mOffloaded = false;
1583 }
1584 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1585 return status;
1586}
1587
1588bool AudioFlinger::EffectModule::isOffloaded() const
1589{
1590 Mutex::Autolock _l(mLock);
1591 return mOffloaded;
1592}
1593
jiabineb3bda02020-06-30 14:07:03 -07001594/*static*/
1595bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1596 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1597}
1598
1599bool AudioFlinger::EffectModule::isHapticGenerator() const {
1600 return isHapticGenerator(&mDescriptor.type);
1601}
1602
Simon Bowden62823412022-10-17 14:52:26 +00001603status_t AudioFlinger::EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07001604{
1605 if (mStatus != NO_ERROR) {
1606 return mStatus;
1607 }
1608 if (!isHapticGenerator()) {
1609 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1610 return INVALID_OPERATION;
1611 }
1612
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001613 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1614 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001615 param->psize = sizeof(int32_t);
1616 param->vsize = sizeof(int32_t) * 2;
1617 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1618 *((int32_t*)param->data + 1) = id;
Simon Bowden62823412022-10-17 14:52:26 +00001619 *((int32_t*)param->data + 2) = static_cast<int32_t>(intensity);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001620 std::vector<uint8_t> response;
1621 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001622 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001623 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1624 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001625 }
1626 return status;
1627}
1628
Lais Andradebc3f37a2021-07-02 00:13:19 +01001629status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001630{
1631 if (mStatus != NO_ERROR) {
1632 return mStatus;
1633 }
1634 if (!isHapticGenerator()) {
1635 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1636 return INVALID_OPERATION;
1637 }
1638
Lais Andradebc3f37a2021-07-02 00:13:19 +01001639 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001640 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001641 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001642 effect_param_t *param = (effect_param_t*) request.data();
1643 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001644 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001645 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1646 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001647 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1648 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1649 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001650 std::vector<uint8_t> response;
1651 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1652 if (status == NO_ERROR) {
1653 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1654 status = *reinterpret_cast<const status_t*>(response.data());
1655 }
1656 return status;
1657}
1658
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001659status_t AudioFlinger::EffectModule::getConfigs(
1660 audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
1661 Mutex::Autolock _l(mLock);
1662 if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
1663 return NO_INIT;
1664 }
1665 inputCfg->sample_rate = mConfig.inputCfg.samplingRate;
1666 inputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.inputCfg.channels);
1667 inputCfg->format = static_cast<audio_format_t>(mConfig.inputCfg.format);
1668 outputCfg->sample_rate = mConfig.outputCfg.samplingRate;
1669 outputCfg->channel_mask = static_cast<audio_channel_mask_t>(mConfig.outputCfg.channels);
1670 outputCfg->format = static_cast<audio_format_t>(mConfig.outputCfg.format);
1671 *isOutput = mIsOutput;
1672 return NO_ERROR;
1673}
1674
Andy Hungbded9c82017-11-30 18:47:35 -08001675static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1676 std::stringstream ss;
1677
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001678 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001679 return "nullptr"; // make different than below
1680 } else if (buffer->externalData() != nullptr) {
1681 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1682 << " -> "
1683 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1684 } else {
1685 ss << buffer->audioBuffer()->raw;
1686 }
1687 return ss.str();
1688}
Marco Nelissenb2208842014-02-07 14:00:50 -08001689
Eric Laurent41709552019-12-16 19:34:05 -08001690void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07001691NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08001692{
Eric Laurent41709552019-12-16 19:34:05 -08001693 EffectBase::dump(fd, args);
1694
Eric Laurentca7cc822012-11-19 14:55:58 -08001695 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001697
Eric Laurent41709552019-12-16 19:34:05 -08001698 result.append("\t\tStatus Engine:\n");
1699 result.appendFormat("\t\t%03d %p\n",
1700 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001701
1702 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001703
1704 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001705 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1706 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1707 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 mConfig.inputCfg.buffer.frameCount,
1709 mConfig.inputCfg.samplingRate,
1710 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001711 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001712 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001713
1714 result.append("\t\t- Output configuration:\n");
1715 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001716 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001717 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001718 mConfig.outputCfg.buffer.frameCount,
1719 mConfig.outputCfg.samplingRate,
1720 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001721 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001722 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001723
rago94a1ee82017-07-21 15:11:02 -07001724#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001725
Andy Hungbded9c82017-11-30 18:47:35 -08001726 result.appendFormat("\t\t- HAL buffers:\n"
1727 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1728 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1729 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1730 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1731 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001732#endif
1733
Eric Laurentca7cc822012-11-19 14:55:58 -08001734 write(fd, result.string(), result.length());
1735
Mikhail Naganov4d547672019-02-22 14:19:19 -08001736 if (mEffectInterface != 0) {
1737 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1738 (void)mEffectInterface->dump(fd);
1739 }
1740
Eric Laurentca7cc822012-11-19 14:55:58 -08001741 if (locked) {
1742 mLock.unlock();
1743 }
1744}
1745
1746// ----------------------------------------------------------------------------
1747// EffectHandle implementation
1748// ----------------------------------------------------------------------------
1749
1750#undef LOG_TAG
1751#define LOG_TAG "AudioFlinger::EffectHandle"
1752
Eric Laurent41709552019-12-16 19:34:05 -08001753AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001754 const sp<AudioFlinger::Client>& client,
1755 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001756 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001757 : BnEffect(),
1758 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001759 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1760 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001761{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001762 ALOGV("constructor %p client %p", this, client.get());
Andy Hung225aef62022-12-06 16:33:20 -08001763 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurentca7cc822012-11-19 14:55:58 -08001764
1765 if (client == 0) {
1766 return;
1767 }
1768 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
Atneya3c61d882021-09-20 14:52:15 -04001769 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{
1770 {static_cast<size_t>(EFFECT_PARAM_BUFFER_SIZE + bufOffset)},
1771 std::string("Effect ID: ")
1772 .append(std::to_string(effect->id()))
1773 .append(" Session ID: ")
1774 .append(std::to_string(static_cast<int>(effect->sessionId())))
1775 .append(" \n")
1776 });
Glenn Kastene75da402013-11-20 13:54:52 -08001777 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001778 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001779 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001781 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 return;
1783 }
Glenn Kastene75da402013-11-20 13:54:52 -08001784 new(mCblk) effect_param_cblk_t();
1785 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001786}
1787
1788AudioFlinger::EffectHandle::~EffectHandle()
1789{
1790 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001791 disconnect(false);
1792}
1793
Andy Hungc747c532022-03-07 21:41:14 -08001794// Creates an association between Binder code to name for IEffect.
1795#define IEFFECT_BINDER_METHOD_MACRO_LIST \
1796BINDER_METHOD_ENTRY(enable) \
1797BINDER_METHOD_ENTRY(disable) \
1798BINDER_METHOD_ENTRY(command) \
1799BINDER_METHOD_ENTRY(disconnect) \
1800BINDER_METHOD_ENTRY(getCblk) \
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001801BINDER_METHOD_ENTRY(getConfig) \
Andy Hungc747c532022-03-07 21:41:14 -08001802
1803// singleton for Binder Method Statistics for IEffect
1804mediautils::MethodStatistics<int>& getIEffectStatistics() {
1805 using Code = int;
1806
1807#pragma push_macro("BINDER_METHOD_ENTRY")
1808#undef BINDER_METHOD_ENTRY
1809#define BINDER_METHOD_ENTRY(ENTRY) \
1810 {(Code)media::BnEffect::TRANSACTION_##ENTRY, #ENTRY},
1811
1812 static mediautils::MethodStatistics<Code> methodStatistics{
1813 IEFFECT_BINDER_METHOD_MACRO_LIST
1814 METHOD_STATISTICS_BINDER_CODE_NAMES(Code)
1815 };
1816#pragma pop_macro("BINDER_METHOD_ENTRY")
1817
1818 return methodStatistics;
1819}
1820
1821status_t AudioFlinger::EffectHandle::onTransact(
1822 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
Andy Hunga2a1ac32022-03-18 16:12:11 -07001823 const std::string methodName = getIEffectStatistics().getMethodForCode(code);
1824 mediautils::TimeCheck check(
1825 std::string("IEffect::").append(methodName),
1826 [code](bool timeout, float elapsedMs) {
1827 if (timeout) {
1828 ; // we don't timeout right now on the effect interface.
1829 } else {
1830 getIEffectStatistics().event(code, elapsedMs);
1831 }
Andy Hungf8ab0932022-06-13 19:49:43 -07001832 }, {} /* timeoutDuration */, {} /* secondChanceDuration */, false /* crashOnTimeout */);
Andy Hungc747c532022-03-07 21:41:14 -08001833 return BnEffect::onTransact(code, data, reply, flags);
1834}
1835
Glenn Kastene75da402013-11-20 13:54:52 -08001836status_t AudioFlinger::EffectHandle::initCheck()
1837{
1838 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1839}
1840
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841#define RETURN(code) \
1842 *_aidl_return = (code); \
1843 return Status::ok();
1844
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001845#define VALUE_OR_RETURN_STATUS_AS_OUT(exp) \
1846 ({ \
1847 auto _tmp = (exp); \
1848 if (!_tmp.ok()) { RETURN(_tmp.error()); } \
1849 std::move(_tmp.value()); \
1850 })
1851
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001852Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001853{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001856 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001858 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001859 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001860 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001861 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001862 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001863
1864 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001865 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001866 }
1867
1868 mEnabled = true;
1869
Eric Laurent6c796322019-04-09 14:13:17 -07001870 status_t status = effect->updatePolicyState();
1871 if (status != NO_ERROR) {
1872 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001874 }
1875
Eric Laurent6b446ce2019-12-13 10:56:31 -08001876 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001877
1878 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001879 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001880 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001881 }
1882
Eric Laurent6b446ce2019-12-13 10:56:31 -08001883 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001885 mEnabled = false;
1886 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001887 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001888}
1889
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001891{
1892 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001893 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001894 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001895 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001896 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001897 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001898 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001901
1902 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001903 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001904 }
1905 mEnabled = false;
1906
Eric Laurent6c796322019-04-09 14:13:17 -07001907 effect->updatePolicyState();
1908
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001909 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001911 }
1912
Eric Laurent6b446ce2019-12-13 10:56:31 -08001913 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001914 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001915}
1916
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001917Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001918{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001919 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001920 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001922}
1923
1924void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1925{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001926 AutoMutex _l(mLock);
1927 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1928 if (mDisconnected) {
1929 if (unpinIfLast) {
1930 android_errorWriteLog(0x534e4554, "32707507");
1931 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001932 return;
1933 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001934 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001935 {
Eric Laurent41709552019-12-16 19:34:05 -08001936 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001937 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001938 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001939 ALOGW("%s Effect handle %p disconnected after thread destruction",
1940 __func__, this);
1941 }
1942 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001943 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001944 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001945
Eric Laurentca7cc822012-11-19 14:55:58 -08001946 if (mClient != 0) {
1947 if (mCblk != NULL) {
1948 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1949 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1950 }
1951 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001952 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung920f6572022-10-06 12:09:49 -07001953 Mutex::Autolock _l2(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001954 mClient.clear();
1955 }
1956}
1957
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1959 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1960 return Status::ok();
1961}
1962
Mikhail Naganov8d7da002022-04-19 21:21:23 +00001963Status AudioFlinger::EffectHandle::getConfig(
1964 media::EffectConfig* _config, int32_t* _aidl_return) {
1965 AutoMutex _l(mLock);
1966 sp<EffectBase> effect = mEffect.promote();
1967 if (effect == nullptr || mDisconnected) {
1968 RETURN(DEAD_OBJECT);
1969 }
1970 sp<EffectModule> effectModule = effect->asEffectModule();
1971 if (effectModule == nullptr) {
1972 RETURN(INVALID_OPERATION);
1973 }
1974 audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1975 audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
1976 bool isOutput;
1977 status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
1978 if (status == NO_ERROR) {
1979 constexpr bool isInput = false; // effects always use 'OUT' channel masks.
1980 _config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1981 legacy2aidl_audio_config_base_t_AudioConfigBase(inputCfg, isInput));
1982 _config->outputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
1983 legacy2aidl_audio_config_base_t_AudioConfigBase(outputCfg, isInput));
1984 _config->isOnInputStream = !isOutput;
1985 }
1986 RETURN(status);
1987}
1988
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001989Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1990 const std::vector<uint8_t>& cmdData,
1991 int32_t maxResponseSize,
1992 std::vector<uint8_t>* response,
1993 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001994{
1995 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001996 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001997
Eric Laurentc7ab3092017-06-15 18:43:46 -07001998 // reject commands reserved for internal use by audio framework if coming from outside
1999 // of audioserver
2000 switch(cmdCode) {
2001 case EFFECT_CMD_ENABLE:
2002 case EFFECT_CMD_DISABLE:
2003 case EFFECT_CMD_SET_PARAM:
2004 case EFFECT_CMD_SET_PARAM_DEFERRED:
2005 case EFFECT_CMD_SET_PARAM_COMMIT:
2006 case EFFECT_CMD_GET_PARAM:
2007 break;
2008 default:
2009 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
2010 break;
2011 }
2012 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002013 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07002014 }
2015
Eric Laurent1ffc5852016-12-15 14:46:09 -08002016 if (cmdCode == EFFECT_CMD_ENABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002017 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002018 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002019 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002020 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002021 writeToBuffer(NO_ERROR, response);
2022 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002023 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Andy Hung920f6572022-10-06 12:09:49 -07002024 if (maxResponseSize < static_cast<signed>(sizeof(int))) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002025 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002026 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002027 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002028 writeToBuffer(NO_ERROR, response);
2029 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002030 }
2031
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002032 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08002033 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002034 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002035 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002036 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 // only get parameter command is permitted for applications not controlling the effect
2038 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002039 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08002040 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002041
2042 // handle commands that are not forwarded transparently to effect engine
2043 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08002044 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002045 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002046 }
2047
Andy Hung920f6572022-10-06 12:09:49 -07002048 if (maxResponseSize < (signed)sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08002049 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002050 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002051 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002052 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08002053
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 // No need to trylock() here as this function is executed in the binder thread serving a
2055 // particular client process: no risk to block the whole media server process or mixer
2056 // threads if we are stuck here
Andy Hung920f6572022-10-06 12:09:49 -07002057 Mutex::Autolock _l2(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08002058 // keep local copy of index in case of client corruption b/32220769
2059 const uint32_t clientIndex = mCblk->clientIndex;
2060 const uint32_t serverIndex = mCblk->serverIndex;
2061 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
2062 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 mCblk->serverIndex = 0;
2064 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002065 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 }
2067 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002068 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08002069 for (uint32_t index = serverIndex; index < clientIndex;) {
2070 int *p = (int *)(mBuffer + index);
2071 const int size = *p++;
2072 if (size < 0
2073 || size > EFFECT_PARAM_BUFFER_SIZE
2074 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002075 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08002076 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08002077 break;
2078 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002079
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002080 std::copy(reinterpret_cast<const uint8_t*>(p),
2081 reinterpret_cast<const uint8_t*>(p) + size,
2082 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08002083
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002084 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002085 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08002086 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002087 sizeof(int),
2088 &replyBuffer);
2089 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08002090
2091 // verify shared memory: server index shouldn't change; client index can't go back.
2092 if (serverIndex != mCblk->serverIndex
2093 || clientIndex > mCblk->clientIndex) {
2094 android_errorWriteLog(0x534e4554, "32220769");
2095 status = BAD_VALUE;
2096 break;
2097 }
2098
Eric Laurentca7cc822012-11-19 14:55:58 -08002099 // stop at first error encountered
2100 if (ret != NO_ERROR) {
2101 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002102 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002103 break;
2104 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002105 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08002106 break;
2107 }
Andy Hunga447a0f2016-11-15 17:19:58 -08002108 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08002109 }
2110 mCblk->serverIndex = 0;
2111 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002112 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002113 }
2114
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002115 status_t status = effect->command(cmdCode,
2116 cmdData,
2117 maxResponseSize,
2118 response);
2119 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002120}
2121
2122void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2123{
2124 ALOGV("setControl %p control %d", this, hasControl);
2125
2126 mHasControl = hasControl;
2127 mEnabled = enabled;
2128
2129 if (signal && mEffectClient != 0) {
2130 mEffectClient->controlStatusChanged(hasControl);
2131 }
2132}
2133
2134void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002135 const std::vector<uint8_t>& cmdData,
2136 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002137{
2138 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002139 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002140 }
2141}
2142
2143
2144
2145void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2146{
2147 if (mEffectClient != 0) {
2148 mEffectClient->enableStatusChanged(enabled);
2149 }
2150}
2151
Eric Laurentde8caf42021-08-11 17:19:25 +02002152void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2153{
2154 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2155 mEffectClient->framesProcessed(frames);
2156 }
2157}
2158
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002159void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Andy Hung920f6572022-10-06 12:09:49 -07002160NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002161{
2162 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2163
Marco Nelissenb2208842014-02-07 14:00:50 -08002164 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002165 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002166 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002167 mHasControl ? "yes" : "no",
2168 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002169 mCblk ? mCblk->clientIndex : 0,
2170 mCblk ? mCblk->serverIndex : 0
2171 );
2172
2173 if (locked) {
2174 mCblk->lock.unlock();
2175 }
2176}
2177
2178#undef LOG_TAG
2179#define LOG_TAG "AudioFlinger::EffectChain"
2180
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002181AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2182 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002184 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002185 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002186 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002187{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002188 sp<ThreadBase> p = thread.promote();
2189 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002190 return;
2191 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002192 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002193 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2194 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002195}
2196
2197AudioFlinger::EffectChain::~EffectChain()
2198{
Eric Laurentca7cc822012-11-19 14:55:58 -08002199}
2200
2201// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2202sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2203 effect_descriptor_t *descriptor)
2204{
2205 size_t size = mEffects.size();
2206
2207 for (size_t i = 0; i < size; i++) {
2208 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2209 return mEffects[i];
2210 }
2211 }
2212 return 0;
2213}
2214
2215// getEffectFromId_l() must be called with ThreadBase::mLock held
2216sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2217{
2218 size_t size = mEffects.size();
2219
2220 for (size_t i = 0; i < size; i++) {
2221 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2222 if (id == 0 || mEffects[i]->id() == id) {
2223 return mEffects[i];
2224 }
2225 }
2226 return 0;
2227}
2228
2229// getEffectFromType_l() must be called with ThreadBase::mLock held
2230sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2231 const effect_uuid_t *type)
2232{
2233 size_t size = mEffects.size();
2234
2235 for (size_t i = 0; i < size; i++) {
2236 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2237 return mEffects[i];
2238 }
2239 }
2240 return 0;
2241}
2242
Eric Laurent6c796322019-04-09 14:13:17 -07002243std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2244{
2245 std::vector<int> ids;
2246 Mutex::Autolock _l(mLock);
2247 for (size_t i = 0; i < mEffects.size(); i++) {
2248 ids.push_back(mEffects[i]->id());
2249 }
2250 return ids;
2251}
2252
Eric Laurentca7cc822012-11-19 14:55:58 -08002253void AudioFlinger::EffectChain::clearInputBuffer()
2254{
2255 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002256 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002257}
2258
2259// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002260void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002261{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002262 if (mInBuffer == NULL) {
2263 return;
2264 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002265 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2266 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002267
Eric Laurent6b446ce2019-12-13 10:56:31 -08002268 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002269 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002270}
2271
2272// Must be called with EffectChain::mLock locked
2273void AudioFlinger::EffectChain::process_l()
2274{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002275 // never process effects when:
2276 // - on an OFFLOAD thread
2277 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002278 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002279 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002280 bool tracksOnSession = (trackCnt() != 0);
2281
2282 if (!tracksOnSession && mTailBufferCount == 0) {
2283 doProcess = false;
2284 }
2285
2286 if (activeTrackCnt() == 0) {
2287 // if no track is active and the effect tail has not been rendered,
2288 // the input buffer must be cleared here as the mixer process will not do it
2289 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002290 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002291 if (mTailBufferCount > 0) {
2292 mTailBufferCount--;
2293 }
2294 }
2295 }
2296 }
2297
2298 size_t size = mEffects.size();
2299 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002300 // Only the input and output buffers of the chain can be external,
2301 // and 'update' / 'commit' do nothing for allocated buffers, thus
2302 // it's not needed to consider any other buffers here.
2303 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002304 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2305 mOutBuffer->update();
2306 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002307 for (size_t i = 0; i < size; i++) {
2308 mEffects[i]->process();
2309 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002310 mInBuffer->commit();
2311 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2312 mOutBuffer->commit();
2313 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002314 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002315 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002316 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002317 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2318 }
2319 if (doResetVolume) {
2320 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002321 }
2322}
2323
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002324// createEffect_l() must be called with ThreadBase::mLock held
2325status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002326 effect_descriptor_t *desc,
2327 int id,
2328 audio_session_t sessionId,
2329 bool pinned)
2330{
2331 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002332 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002333 status_t lStatus = effect->status();
2334 if (lStatus == NO_ERROR) {
2335 lStatus = addEffect_ll(effect);
2336 }
2337 if (lStatus != NO_ERROR) {
2338 effect.clear();
2339 }
2340 return lStatus;
2341}
2342
2343// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002344status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2345{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002346 Mutex::Autolock _l(mLock);
2347 return addEffect_ll(effect);
2348}
2349// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2350status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2351{
Eric Laurent6b446ce2019-12-13 10:56:31 -08002352 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002353
Eric Laurentb62d0362021-10-26 17:40:18 +02002354 effect_descriptor_t desc = effect->desc();
Eric Laurentca7cc822012-11-19 14:55:58 -08002355 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2356 // Auxiliary effects are inserted at the beginning of mEffects vector as
2357 // they are processed first and accumulated in chain input buffer
2358 mEffects.insertAt(effect, 0);
2359
2360 // the input buffer for auxiliary effect contains mono samples in
2361 // 32 bit format. This is to avoid saturation in AudoMixer
2362 // accumulation stage. Saturation is done in EffectModule::process() before
2363 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002364 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002365 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002366#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002367 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002368 numSamples * sizeof(float), &halBuffer);
2369#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002370 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002371 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002372#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002373 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002374
2375 effect->configure();
2376
Mikhail Naganov022b9952017-01-04 16:36:51 -08002377 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002378 // auxiliary effects output samples to chain input buffer for further processing
2379 // by insert effects
2380 effect->setOutBuffer(mInBuffer);
2381 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002382 ssize_t idx_insert = getInsertIndex(desc);
2383 if (idx_insert < 0) {
2384 return INVALID_OPERATION;
Eric Laurentca7cc822012-11-19 14:55:58 -08002385 }
2386
Eric Laurentb62d0362021-10-26 17:40:18 +02002387 size_t previousSize = mEffects.size();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002388 mEffects.insertAt(effect, idx_insert);
2389
2390 effect->configure();
2391
Eric Laurentb62d0362021-10-26 17:40:18 +02002392 // - By default:
2393 // All effects read samples from chain input buffer.
2394 // The last effect in the chain, writes samples to chain output buffer,
2395 // otherwise to chain input buffer
2396 // - In the OUTPUT_STAGE chain of a spatializer mixer thread:
2397 // The spatializer effect (first effect) reads samples from the input buffer
2398 // and writes samples to the output buffer.
2399 // All other effects read and writes samples to the output buffer
2400 if (mEffectCallback->isSpatializer()
2401 && mSessionId == AUDIO_SESSION_OUTPUT_STAGE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002402 effect->setOutBuffer(mOutBuffer);
Eric Laurentb62d0362021-10-26 17:40:18 +02002403 if (idx_insert == 0) {
2404 if (previousSize != 0) {
2405 mEffects[1]->configure();
2406 mEffects[1]->setInBuffer(mOutBuffer);
2407 mEffects[1]->updateAccessMode(); // reconfig if neeeded.
2408 }
2409 effect->setInBuffer(mInBuffer);
2410 } else {
2411 effect->setInBuffer(mOutBuffer);
2412 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002413 } else {
Eric Laurentb62d0362021-10-26 17:40:18 +02002414 effect->setInBuffer(mInBuffer);
Andy Hung920f6572022-10-06 12:09:49 -07002415 if (idx_insert == static_cast<ssize_t>(previousSize)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002416 if (idx_insert != 0) {
2417 mEffects[idx_insert-1]->configure();
2418 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2419 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
2420 }
2421 effect->setOutBuffer(mOutBuffer);
2422 } else {
2423 effect->setOutBuffer(mInBuffer);
2424 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002425 }
Eric Laurentb62d0362021-10-26 17:40:18 +02002426 ALOGV("%s effect %p, added in chain %p at rank %zu",
2427 __func__, effect.get(), this, idx_insert);
Eric Laurentca7cc822012-11-19 14:55:58 -08002428 }
2429 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002430
Eric Laurentca7cc822012-11-19 14:55:58 -08002431 return NO_ERROR;
2432}
2433
Eric Laurentb62d0362021-10-26 17:40:18 +02002434ssize_t AudioFlinger::EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
2435 // Insert effects are inserted at the end of mEffects vector as they are processed
2436 // after track and auxiliary effects.
2437 // Insert effect order as a function of indicated preference:
2438 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2439 // another effect is present
2440 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2441 // last effect claiming first position
2442 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2443 // first effect claiming last position
2444 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2445 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2446 // already present
2447 // Spatializer or Downmixer effects are inserted in first position because
2448 // they adapt the channel count for all other effects in the chain
2449 if ((memcmp(&desc.type, FX_IID_SPATIALIZER, sizeof(effect_uuid_t)) == 0)
2450 || (memcmp(&desc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0)) {
2451 return 0;
2452 }
2453
2454 size_t size = mEffects.size();
2455 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2456 ssize_t idx_insert;
2457 ssize_t idx_insert_first = -1;
2458 ssize_t idx_insert_last = -1;
2459
2460 idx_insert = size;
2461 for (size_t i = 0; i < size; i++) {
2462 effect_descriptor_t d = mEffects[i]->desc();
2463 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2464 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2465 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2466 // check invalid effect chaining combinations
2467 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2468 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2469 ALOGW("%s could not insert effect %s: exclusive conflict with %s",
2470 __func__, desc.name, d.name);
2471 return -1;
2472 }
2473 // remember position of first insert effect and by default
2474 // select this as insert position for new effect
Andy Hung920f6572022-10-06 12:09:49 -07002475 if (idx_insert == static_cast<ssize_t>(size)) {
Eric Laurentb62d0362021-10-26 17:40:18 +02002476 idx_insert = i;
2477 }
2478 // remember position of last insert effect claiming
2479 // first position
2480 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2481 idx_insert_first = i;
2482 }
2483 // remember position of first insert effect claiming
2484 // last position
2485 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2486 idx_insert_last == -1) {
2487 idx_insert_last = i;
2488 }
2489 }
2490 }
2491
2492 // modify idx_insert from first position if needed
2493 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2494 if (idx_insert_last != -1) {
2495 idx_insert = idx_insert_last;
2496 } else {
2497 idx_insert = size;
2498 }
2499 } else {
2500 if (idx_insert_first != -1) {
2501 idx_insert = idx_insert_first + 1;
2502 }
2503 }
2504 return idx_insert;
2505}
2506
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002507// removeEffect_l() must be called with ThreadBase::mLock held
2508size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2509 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002510{
2511 Mutex::Autolock _l(mLock);
2512 size_t size = mEffects.size();
2513 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2514
2515 for (size_t i = 0; i < size; i++) {
2516 if (effect == mEffects[i]) {
2517 // calling stop here will remove pre-processing effect from the audio HAL.
2518 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2519 // the middle of a read from audio HAL
2520 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2521 mEffects[i]->state() == EffectModule::STOPPING) {
2522 mEffects[i]->stop();
2523 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002524 if (release) {
2525 mEffects[i]->release_l();
2526 }
2527
Mikhail Naganov022b9952017-01-04 16:36:51 -08002528 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002529 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002530 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002531 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002532 mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002533 }
2534 }
2535 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002536
2537 // make sure the input buffer configuration for the new first effect in the chain
2538 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2539 if (i == 0 && size > 1) {
2540 mEffects[0]->configure();
2541 mEffects[0]->setInBuffer(mInBuffer);
Eric Laurent6bb7dbe2021-12-23 15:39:36 +01002542 mEffects[0]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentf1f22e72021-07-13 14:04:14 +02002543 }
2544
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002545 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002546 this, i);
2547 break;
2548 }
2549 }
2550
2551 return mEffects.size();
2552}
2553
jiabin8f278ee2019-11-11 12:16:27 -08002554// setDevices_l() must be called with ThreadBase::mLock held
2555void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002556{
2557 size_t size = mEffects.size();
2558 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002559 mEffects[i]->setDevices(devices);
2560 }
2561}
2562
2563// setInputDevice_l() must be called with ThreadBase::mLock held
2564void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2565{
2566 size_t size = mEffects.size();
2567 for (size_t i = 0; i < size; i++) {
2568 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002569 }
2570}
2571
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002572// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002573void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2574{
2575 size_t size = mEffects.size();
2576 for (size_t i = 0; i < size; i++) {
2577 mEffects[i]->setMode(mode);
2578 }
2579}
2580
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002581// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002582void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2583{
2584 size_t size = mEffects.size();
2585 for (size_t i = 0; i < size; i++) {
2586 mEffects[i]->setAudioSource(source);
2587 }
2588}
2589
Zhou Songd505c642020-02-20 16:35:37 +08002590bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2591 for (const auto &effect : mEffects) {
2592 if (effect->isVolumeControlEnabled()) return true;
2593 }
2594 return false;
2595}
2596
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002597// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002598bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002599{
2600 uint32_t newLeft = *left;
2601 uint32_t newRight = *right;
2602 bool hasControl = false;
2603 int ctrlIdx = -1;
2604 size_t size = mEffects.size();
2605
2606 // first update volume controller
2607 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002608 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002609 ctrlIdx = i - 1;
2610 hasControl = true;
2611 break;
2612 }
2613 }
2614
Eric Laurentfa1e1232016-08-02 19:01:49 -07002615 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002616 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002617 if (hasControl) {
2618 *left = mNewLeftVolume;
2619 *right = mNewRightVolume;
2620 }
2621 return hasControl;
2622 }
2623
2624 mVolumeCtrlIdx = ctrlIdx;
2625 mLeftVolume = newLeft;
2626 mRightVolume = newRight;
2627
2628 // second get volume update from volume controller
2629 if (ctrlIdx >= 0) {
2630 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2631 mNewLeftVolume = newLeft;
2632 mNewRightVolume = newRight;
2633 }
2634 // then indicate volume to all other effects in chain.
2635 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002636 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002637 uint32_t lVol = newLeft;
2638 uint32_t rVol = newRight;
2639
2640 for (size_t i = 0; i < size; i++) {
2641 if ((int)i == ctrlIdx) {
2642 continue;
2643 }
2644 // this also works for ctrlIdx == -1 when there is no volume controller
2645 if ((int)i > ctrlIdx) {
2646 lVol = *left;
2647 rVol = *right;
2648 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002649 // Pass requested volume directly if this is volume monitor module
2650 if (mEffects[i]->isVolumeMonitor()) {
2651 mEffects[i]->setVolume(left, right, false);
2652 } else {
2653 mEffects[i]->setVolume(&lVol, &rVol, false);
2654 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002655 }
2656 *left = newLeft;
2657 *right = newRight;
2658
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002659 setVolumeForOutput_l(*left, *right);
2660
Eric Laurentca7cc822012-11-19 14:55:58 -08002661 return hasControl;
2662}
2663
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002664// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002665void AudioFlinger::EffectChain::resetVolume_l()
2666{
Eric Laurente7449bf2016-08-03 18:44:07 -07002667 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2668 uint32_t left = mLeftVolume;
2669 uint32_t right = mRightVolume;
2670 (void)setVolume_l(&left, &right, true);
2671 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002672}
2673
jiabineb3bda02020-06-30 14:07:03 -07002674// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2675bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2676{
2677 for (size_t i = 0; i < mEffects.size(); ++i) {
2678 if (mEffects[i]->isHapticGenerator()) {
2679 return true;
2680 }
2681 }
2682 return false;
2683}
2684
Simon Bowden62823412022-10-17 14:52:26 +00002685void AudioFlinger::EffectChain::setHapticIntensity_l(int id, os::HapticScale intensity)
jiabine70bc7f2020-06-30 22:07:55 -07002686{
2687 Mutex::Autolock _l(mLock);
2688 for (size_t i = 0; i < mEffects.size(); ++i) {
2689 mEffects[i]->setHapticIntensity(id, intensity);
2690 }
2691}
2692
Eric Laurent1b928682014-10-02 19:41:47 -07002693void AudioFlinger::EffectChain::syncHalEffectsState()
2694{
2695 Mutex::Autolock _l(mLock);
2696 for (size_t i = 0; i < mEffects.size(); i++) {
2697 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2698 mEffects[i]->state() == EffectModule::STOPPING) {
2699 mEffects[i]->addEffectToHal_l();
2700 }
2701 }
2702}
2703
Eric Laurentca7cc822012-11-19 14:55:58 -08002704void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
Andy Hung920f6572022-10-06 12:09:49 -07002705NO_THREAD_SAFETY_ANALYSIS // conditional try lock
Eric Laurentca7cc822012-11-19 14:55:58 -08002706{
Eric Laurentca7cc822012-11-19 14:55:58 -08002707 String8 result;
2708
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002709 const size_t numEffects = mEffects.size();
2710 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002711
Marco Nelissenb2208842014-02-07 14:00:50 -08002712 if (numEffects) {
2713 bool locked = AudioFlinger::dumpTryLock(mLock);
2714 // failed to lock - AudioFlinger is probably deadlocked
2715 if (!locked) {
2716 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002717 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002718
Andy Hungbded9c82017-11-30 18:47:35 -08002719 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2720 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2721 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2722 (int)inBufferStr.size(), "In buffer ",
2723 (int)outBufferStr.size(), "Out buffer ");
2724 result.appendFormat("\t%s %s %d\n",
2725 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002726 write(fd, result.string(), result.size());
2727
2728 for (size_t i = 0; i < numEffects; ++i) {
2729 sp<EffectModule> effect = mEffects[i];
2730 if (effect != 0) {
2731 effect->dump(fd, args);
2732 }
2733 }
2734
2735 if (locked) {
2736 mLock.unlock();
2737 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002738 } else {
2739 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002740 }
2741}
2742
2743// must be called with ThreadBase::mLock held
2744void AudioFlinger::EffectChain::setEffectSuspended_l(
2745 const effect_uuid_t *type, bool suspend)
2746{
2747 sp<SuspendedEffectDesc> desc;
2748 // use effect type UUID timelow as key as there is no real risk of identical
2749 // timeLow fields among effect type UUIDs.
2750 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2751 if (suspend) {
2752 if (index >= 0) {
2753 desc = mSuspendedEffects.valueAt(index);
2754 } else {
2755 desc = new SuspendedEffectDesc();
2756 desc->mType = *type;
2757 mSuspendedEffects.add(type->timeLow, desc);
2758 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2759 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002760
Eric Laurentca7cc822012-11-19 14:55:58 -08002761 if (desc->mRefCount++ == 0) {
2762 sp<EffectModule> effect = getEffectIfEnabled(type);
2763 if (effect != 0) {
2764 desc->mEffect = effect;
2765 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002766 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002767 }
2768 }
2769 } else {
2770 if (index < 0) {
2771 return;
2772 }
2773 desc = mSuspendedEffects.valueAt(index);
2774 if (desc->mRefCount <= 0) {
2775 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002776 desc->mRefCount = 0;
2777 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002778 }
2779 if (--desc->mRefCount == 0) {
2780 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2781 if (desc->mEffect != 0) {
2782 sp<EffectModule> effect = desc->mEffect.promote();
2783 if (effect != 0) {
2784 effect->setSuspended(false);
2785 effect->lock();
2786 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002787 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002788 effect->setEnabled_l(handle->enabled());
2789 }
2790 effect->unlock();
2791 }
2792 desc->mEffect.clear();
2793 }
2794 mSuspendedEffects.removeItemsAt(index);
2795 }
2796 }
2797}
2798
2799// must be called with ThreadBase::mLock held
2800void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2801{
2802 sp<SuspendedEffectDesc> desc;
2803
2804 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2805 if (suspend) {
2806 if (index >= 0) {
2807 desc = mSuspendedEffects.valueAt(index);
2808 } else {
2809 desc = new SuspendedEffectDesc();
2810 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2811 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2812 }
2813 if (desc->mRefCount++ == 0) {
2814 Vector< sp<EffectModule> > effects;
2815 getSuspendEligibleEffects(effects);
2816 for (size_t i = 0; i < effects.size(); i++) {
2817 setEffectSuspended_l(&effects[i]->desc().type, true);
2818 }
2819 }
2820 } else {
2821 if (index < 0) {
2822 return;
2823 }
2824 desc = mSuspendedEffects.valueAt(index);
2825 if (desc->mRefCount <= 0) {
2826 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2827 desc->mRefCount = 1;
2828 }
2829 if (--desc->mRefCount == 0) {
2830 Vector<const effect_uuid_t *> types;
2831 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2832 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2833 continue;
2834 }
2835 types.add(&mSuspendedEffects.valueAt(i)->mType);
2836 }
2837 for (size_t i = 0; i < types.size(); i++) {
2838 setEffectSuspended_l(types[i], false);
2839 }
2840 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2841 mSuspendedEffects.keyAt(index));
2842 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2843 }
2844 }
2845}
2846
2847
2848// The volume effect is used for automated tests only
2849#ifndef OPENSL_ES_H_
2850static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2851 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2852const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2853#endif //OPENSL_ES_H_
2854
Eric Laurentd8365c52017-07-16 15:27:05 -07002855/* static */
2856bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2857{
2858 // Only NS and AEC are suspended when BtNRec is off
2859 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2860 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2861 return true;
2862 }
2863 return false;
2864}
2865
Eric Laurentca7cc822012-11-19 14:55:58 -08002866bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2867{
2868 // auxiliary effects and visualizer are never suspended on output mix
2869 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2870 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2871 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002872 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2873 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002874 return false;
2875 }
2876 return true;
2877}
2878
2879void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2880 Vector< sp<AudioFlinger::EffectModule> > &effects)
2881{
2882 effects.clear();
2883 for (size_t i = 0; i < mEffects.size(); i++) {
2884 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2885 effects.add(mEffects[i]);
2886 }
2887 }
2888}
2889
2890sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2891 const effect_uuid_t *type)
2892{
2893 sp<EffectModule> effect = getEffectFromType_l(type);
2894 return effect != 0 && effect->isEnabled() ? effect : 0;
2895}
2896
2897void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2898 bool enabled)
2899{
2900 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2901 if (enabled) {
2902 if (index < 0) {
2903 // if the effect is not suspend check if all effects are suspended
2904 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2905 if (index < 0) {
2906 return;
2907 }
2908 if (!isEffectEligibleForSuspend(effect->desc())) {
2909 return;
2910 }
2911 setEffectSuspended_l(&effect->desc().type, enabled);
2912 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2913 if (index < 0) {
2914 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2915 return;
2916 }
2917 }
2918 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2919 effect->desc().type.timeLow);
2920 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002921 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002922 if (desc->mEffect == 0) {
2923 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002924 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002925 effect->setSuspended(true);
2926 }
2927 } else {
2928 if (index < 0) {
2929 return;
2930 }
2931 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2932 effect->desc().type.timeLow);
2933 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2934 desc->mEffect.clear();
2935 effect->setSuspended(false);
2936 }
2937}
2938
Eric Laurent5baf2af2013-09-12 17:37:00 -07002939bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002940{
2941 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002942 return isNonOffloadableEnabled_l();
2943}
2944
2945bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2946{
Eric Laurent813e2a72013-08-31 12:59:48 -07002947 size_t size = mEffects.size();
2948 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002949 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002950 return true;
2951 }
2952 }
2953 return false;
2954}
2955
Eric Laurentaaa44472014-09-12 17:41:50 -07002956void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2957{
2958 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002959 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002960}
2961
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002962void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2963{
2964 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2965 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2966 }
2967 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2968 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2969 }
jiabinc658e452022-10-21 20:52:21 +00002970 if ((*flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != 0 && !isBitPerfectCompatible()) {
2971 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_BIT_PERFECT);
2972 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002973}
2974
2975void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2976{
2977 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2978 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2979 }
2980 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2981 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2982 }
2983}
2984
2985bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002986{
2987 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002988 for (const auto &effect : mEffects) {
2989 if (effect->isProcessImplemented()) {
2990 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002991 }
2992 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002993 // Allow effects without processing.
2994 return true;
2995}
2996
2997bool AudioFlinger::EffectChain::isFastCompatible() const
2998{
2999 Mutex::Autolock _l(mLock);
3000 for (const auto &effect : mEffects) {
3001 if (effect->isProcessImplemented()
3002 && effect->isImplementationSoftware()) {
3003 return false;
3004 }
3005 }
3006 // Allow effects without processing or hw accelerated effects.
3007 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07003008}
3009
jiabinc658e452022-10-21 20:52:21 +00003010bool AudioFlinger::EffectChain::isBitPerfectCompatible() const {
3011 Mutex::Autolock _l(mLock);
3012 for (const auto &effect : mEffects) {
3013 if (effect->isProcessImplemented()
3014 && effect->isImplementationSoftware()) {
3015 return false;
3016 }
3017 }
3018 // Allow effects without processing or hw accelerated effects.
3019 return true;
3020}
3021
Eric Laurent4c415062016-06-17 16:14:16 -07003022// isCompatibleWithThread_l() must be called with thread->mLock held
3023bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
3024{
3025 Mutex::Autolock _l(mLock);
3026 for (size_t i = 0; i < mEffects.size(); i++) {
3027 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
3028 return false;
3029 }
3030 }
3031 return true;
3032}
3033
Eric Laurent6b446ce2019-12-13 10:56:31 -08003034// EffectCallbackInterface implementation
3035status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
3036 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3037 sp<EffectHalInterface> *effect) {
3038 status_t status = NO_INIT;
Andy Hungba8e52b2023-05-11 14:33:03 -07003039 const sp<EffectsFactoryHalInterface> effectsFactory =
3040 EffectConfiguration::getEffectsFactoryHal();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003041 if (effectsFactory != 0) {
3042 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
3043 }
3044 return status;
3045}
3046
3047bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08003048 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08003049 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08003050 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003051}
3052
3053status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
3054 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08003055 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003056}
3057
3058status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003059 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003060 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003061 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062 if (t == nullptr) {
3063 return result;
3064 }
3065 sp <StreamHalInterface> st = t->stream();
3066 if (st == nullptr) {
3067 return result;
3068 }
3069 result = st->addEffect(effect);
3070 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
3071 return result;
3072}
3073
3074status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003075 const sp<EffectHalInterface>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003076 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08003077 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003078 if (t == nullptr) {
3079 return result;
3080 }
3081 sp <StreamHalInterface> st = t->stream();
3082 if (st == nullptr) {
3083 return result;
3084 }
3085 result = st->removeEffect(effect);
3086 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
3087 return result;
3088}
3089
3090audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08003091 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003092 if (t == nullptr) {
3093 return AUDIO_IO_HANDLE_NONE;
3094 }
3095 return t->id();
3096}
3097
3098bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08003099 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003100 if (t == nullptr) {
3101 return true;
3102 }
3103 return t->isOutput();
3104}
3105
3106bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003107 return mThreadType == ThreadBase::OFFLOAD;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003108}
3109
3110bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003111 return mThreadType == ThreadBase::OFFLOAD || mThreadType == ThreadBase::DIRECT;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003112}
3113
3114bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003115 switch (mThreadType) {
3116 case ThreadBase::OFFLOAD:
3117 case ThreadBase::MMAP_PLAYBACK:
3118 case ThreadBase::MMAP_CAPTURE:
3119 return true;
3120 default:
Eric Laurent6b446ce2019-12-13 10:56:31 -08003121 return false;
3122 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003123}
3124
3125bool AudioFlinger::EffectChain::EffectCallback::isSpatializer() const {
3126 return mThreadType == ThreadBase::SPATIALIZER;
Eric Laurent6b446ce2019-12-13 10:56:31 -08003127}
3128
3129uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08003130 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003131 if (t == nullptr) {
3132 return 0;
3133 }
3134 return t->sampleRate();
3135}
3136
Eric Laurentf1f22e72021-07-13 14:04:14 +02003137audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
3138 sp<ThreadBase> t = thread().promote();
3139 if (t == nullptr) {
3140 return AUDIO_CHANNEL_NONE;
3141 }
3142 sp<EffectChain> c = chain().promote();
3143 if (c == nullptr) {
3144 return AUDIO_CHANNEL_NONE;
3145 }
3146
Eric Laurentb62d0362021-10-26 17:40:18 +02003147 if (mThreadType == ThreadBase::SPATIALIZER) {
3148 if (c->sessionId() == AUDIO_SESSION_OUTPUT_STAGE) {
3149 if (c->isFirstEffect(id)) {
3150 return t->mixerChannelMask();
3151 } else {
3152 return t->channelMask();
3153 }
3154 } else if (!audio_is_global_session(c->sessionId())) {
3155 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3156 return t->mixerChannelMask();
3157 } else {
3158 return t->channelMask();
3159 }
3160 } else {
3161 return t->channelMask();
3162 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02003163 } else {
3164 return t->channelMask();
3165 }
3166}
3167
3168uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003169 return audio_channel_count_from_out_mask(inChannelMask(id));
Eric Laurentf1f22e72021-07-13 14:04:14 +02003170}
3171
3172audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003173 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003174 if (t == nullptr) {
3175 return AUDIO_CHANNEL_NONE;
3176 }
Eric Laurentb62d0362021-10-26 17:40:18 +02003177 sp<EffectChain> c = chain().promote();
3178 if (c == nullptr) {
3179 return AUDIO_CHANNEL_NONE;
3180 }
3181
3182 if (mThreadType == ThreadBase::SPATIALIZER) {
3183 if (!audio_is_global_session(c->sessionId())) {
3184 if ((t->hasAudioSession_l(c->sessionId()) & ThreadBase::SPATIALIZED_SESSION) != 0) {
3185 return t->mixerChannelMask();
3186 } else {
3187 return t->channelMask();
3188 }
3189 } else {
3190 return t->channelMask();
3191 }
3192 } else {
3193 return t->channelMask();
3194 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08003195}
3196
Eric Laurentf1f22e72021-07-13 14:04:14 +02003197uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Eric Laurentb62d0362021-10-26 17:40:18 +02003198 return audio_channel_count_from_out_mask(outChannelMask());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003199}
3200
jiabineb3bda02020-06-30 14:07:03 -07003201audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003202 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003203 if (t == nullptr) {
3204 return AUDIO_CHANNEL_NONE;
3205 }
3206 return t->hapticChannelMask();
3207}
3208
Eric Laurent6b446ce2019-12-13 10:56:31 -08003209size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003210 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003211 if (t == nullptr) {
3212 return 0;
3213 }
3214 return t->frameCount();
3215}
3216
Andy Hung920f6572022-10-06 12:09:49 -07003217uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const
3218NO_THREAD_SAFETY_ANALYSIS // latency_l() access
3219{
Andy Hung328d6772021-01-12 12:32:21 -08003220 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003221 if (t == nullptr) {
3222 return 0;
3223 }
Andy Hung920f6572022-10-06 12:09:49 -07003224 // TODO(b/275956781) - this requires the thread lock.
Eric Laurent6b446ce2019-12-13 10:56:31 -08003225 return t->latency_l();
3226}
3227
Andy Hung920f6572022-10-06 12:09:49 -07003228void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const
3229NO_THREAD_SAFETY_ANALYSIS // setVolumeForOutput_l() access
3230{
Andy Hung328d6772021-01-12 12:32:21 -08003231 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003232 if (t == nullptr) {
3233 return;
3234 }
3235 t->setVolumeForOutput_l(left, right);
3236}
3237
3238void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003239 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003240 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003241 if (t == nullptr) {
3242 return;
3243 }
3244 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3245
Andy Hung328d6772021-01-12 12:32:21 -08003246 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003247 if (c == nullptr) {
3248 return;
3249 }
Eric Laurent41709552019-12-16 19:34:05 -08003250 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3251 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003252}
3253
Eric Laurent41709552019-12-16 19:34:05 -08003254void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003255 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003256 if (t == nullptr) {
3257 return;
3258 }
Eric Laurent41709552019-12-16 19:34:05 -08003259 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3260 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003261}
3262
Eric Laurent41709552019-12-16 19:34:05 -08003263void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003264 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3265
Andy Hung328d6772021-01-12 12:32:21 -08003266 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003267 if (t == nullptr) {
3268 return;
3269 }
3270 t->onEffectDisable();
3271}
3272
3273bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3274 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003275 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003276 if (t == nullptr) {
3277 return false;
3278 }
3279 t->disconnectEffectHandle(handle, unpinIfLast);
3280 return true;
3281}
3282
3283void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003284 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003285 if (c == nullptr) {
3286 return;
3287 }
3288 c->resetVolume_l();
3289
3290}
3291
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003292product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003293 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003294 if (c == nullptr) {
3295 return PRODUCT_STRATEGY_NONE;
3296 }
3297 return c->strategy();
3298}
3299
3300int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003301 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003302 if (c == nullptr) {
3303 return 0;
3304 }
3305 return c->activeTrackCnt();
3306}
3307
Eric Laurentb82e6b72019-11-22 17:25:04 -08003308
3309#undef LOG_TAG
3310#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3311
3312status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3313{
3314 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3315 Mutex::Autolock _l(mProxyLock);
3316 if (status == NO_ERROR) {
3317 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003318 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003319 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003320 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003321 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003322 bs = handle.second->disable(&status);
3323 }
3324 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003325 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003326 }
3327 }
3328 }
3329 ALOGV("%s enable %d status %d", __func__, enabled, status);
3330 return status;
3331}
3332
3333status_t AudioFlinger::DeviceEffectProxy::init(
3334 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3335//For all audio patches
3336//If src or sink device match
3337//If the effect is HW accelerated
3338// if no corresponding effect module
3339// Create EffectModule: mHalEffect
3340//Create and attach EffectHandle
3341//If the effect is not HW accelerated and the patch sink or src is a mixer port
3342// Create Effect on patch input or output thread on session -1
3343//Add EffectHandle to EffectHandle map of Effect Proxy:
3344 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3345 status_t status = NO_ERROR;
3346 for (auto &patch : patches) {
3347 status = onCreatePatch(patch.first, patch.second);
3348 ALOGV("%s onCreatePatch status %d", __func__, status);
3349 if (status == BAD_VALUE) {
3350 return status;
3351 }
3352 }
3353 return status;
3354}
3355
3356status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3357 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3358 status_t status = NAME_NOT_FOUND;
3359 sp<EffectHandle> handle;
3360 // only consider source[0] as this is the only "true" source of a patch
3361 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3362 ALOGV("%s source checkPort status %d", __func__, status);
3363 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3364 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3365 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3366 }
3367 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3368 Mutex::Autolock _l(mProxyLock);
3369 mEffectHandles.emplace(patchHandle, handle);
3370 }
3371 ALOGW_IF(status == BAD_VALUE,
3372 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3373
3374 return status;
3375}
3376
3377status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3378 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3379
3380 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3381 __func__, port->type, port->ext.device.type,
3382 port->ext.device.address, port->id, patch.isSoftware());
3383 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003384 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003385 return NAME_NOT_FOUND;
3386 }
3387 status_t status = NAME_NOT_FOUND;
3388
3389 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3390 Mutex::Autolock _l(mProxyLock);
3391 mDevicePort = *port;
3392 mHalEffect = new EffectModule(mMyCallback,
3393 const_cast<effect_descriptor_t *>(&mDescriptor),
3394 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3395 false /* pinned */, port->id);
3396 if (audio_is_input_device(mDevice.mType)) {
3397 mHalEffect->setInputDevice(mDevice);
3398 } else {
3399 mHalEffect->setDevices({mDevice});
3400 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003401 mHalEffect->configure();
3402
Eric Laurentde8caf42021-08-11 17:19:25 +02003403 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3404 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003405 status = (*handle)->initCheck();
3406 if (status == OK) {
3407 status = mHalEffect->addHandle((*handle).get());
3408 } else {
3409 mHalEffect.clear();
3410 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3411 }
3412 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3413 sp <ThreadBase> thread;
3414 if (audio_port_config_has_input_direction(port)) {
Shunkai Yao39a6ded2023-04-27 18:09:08 +00003415 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
3416 ALOGI("%s don't create postprocessing effect on record thread", __func__);
3417 return NAME_NOT_FOUND;
3418 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003419 if (patch.isSoftware()) {
3420 thread = patch.mRecord.thread();
3421 } else {
3422 thread = patch.thread().promote();
3423 }
3424 } else {
Shunkai Yao39a6ded2023-04-27 18:09:08 +00003425 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
3426 ALOGI("%s don't create preprocessing effect on playback thread", __func__);
3427 return NAME_NOT_FOUND;
3428 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003429 if (patch.isSoftware()) {
3430 thread = patch.mPlayback.thread();
3431 } else {
3432 thread = patch.thread().promote();
3433 }
3434 }
3435 int enabled;
3436 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3437 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003438 &enabled, &status, false, false /*probe*/,
3439 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003440 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3441 } else {
3442 status = BAD_VALUE;
3443 }
3444 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003445 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003446 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003447 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003448 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003449 bs = (*handle)->disable(&status);
3450 }
3451 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003452 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003453 }
3454 }
3455 return status;
3456}
3457
3458void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003459 sp<EffectHandle> effect;
3460 {
3461 Mutex::Autolock _l(mProxyLock);
3462 if (mEffectHandles.find(patchHandle) != mEffectHandles.end()) {
3463 effect = mEffectHandles.at(patchHandle);
3464 mEffectHandles.erase(patchHandle);
3465 }
3466 }
Eric Laurentb82e6b72019-11-22 17:25:04 -08003467}
3468
3469
3470size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3471{
3472 Mutex::Autolock _l(mProxyLock);
3473 if (effect == mHalEffect) {
Eric Laurent76c89f32021-12-03 17:13:23 +01003474 mHalEffect->release_l();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003475 mHalEffect.clear();
3476 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3477 }
3478 return mHalEffect == nullptr ? 0 : 1;
3479}
3480
3481status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003482 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003483 if (mHalEffect == nullptr) {
3484 return NO_INIT;
3485 }
3486 return mManagerCallback->addEffectToHal(
3487 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3488}
3489
3490status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003491 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003492 if (mHalEffect == nullptr) {
3493 return NO_INIT;
3494 }
3495 return mManagerCallback->removeEffectFromHal(
3496 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3497}
3498
3499bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3500 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3501 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3502 }
3503 return true;
3504}
3505
3506uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3507 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3508 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3509 return mDevicePort.sample_rate;
3510 }
3511 return DEFAULT_OUTPUT_SAMPLE_RATE;
3512}
3513
3514audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3515 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3516 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3517 return mDevicePort.channel_mask;
3518 }
3519 return AUDIO_CHANNEL_OUT_STEREO;
3520}
3521
3522uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3523 if (isOutput()) {
3524 return audio_channel_count_from_out_mask(channelMask());
3525 }
3526 return audio_channel_count_from_in_mask(channelMask());
3527}
3528
Andy Hung920f6572022-10-06 12:09:49 -07003529void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces)
3530NO_THREAD_SAFETY_ANALYSIS // conditional try lock
3531{
Eric Laurentb82e6b72019-11-22 17:25:04 -08003532 const Vector<String16> args;
3533 EffectBase::dump(fd, args);
3534
3535 const bool locked = dumpTryLock(mProxyLock);
3536 if (!locked) {
3537 String8 result("DeviceEffectProxy may be deadlocked\n");
3538 write(fd, result.string(), result.size());
3539 }
3540
3541 String8 outStr;
3542 if (mHalEffect != nullptr) {
3543 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3544 } else {
3545 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3546 }
3547 write(fd, outStr.string(), outStr.size());
3548 outStr.clear();
3549
3550 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3551 write(fd, outStr.string(), outStr.size());
3552 outStr.clear();
3553
3554 for (const auto& iter : mEffectHandles) {
3555 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3556 write(fd, outStr.string(), outStr.size());
3557 outStr.clear();
3558 sp<EffectBase> effect = iter.second->effect().promote();
3559 if (effect != nullptr) {
3560 effect->dump(fd, args);
3561 }
3562 }
3563
3564 if (locked) {
3565 mLock.unlock();
3566 }
3567}
3568
3569#undef LOG_TAG
3570#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3571
3572int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3573 return mManagerCallback->newEffectId();
3574}
3575
3576
3577bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3578 EffectHandle *handle, bool unpinIfLast) {
3579 sp<EffectBase> effectBase = handle->effect().promote();
3580 if (effectBase == nullptr) {
3581 return false;
3582 }
3583
3584 sp<EffectModule> effect = effectBase->asEffectModule();
3585 if (effect == nullptr) {
3586 return false;
3587 }
3588
3589 // restore suspended effects if the disconnected handle was enabled and the last one.
3590 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3591 if (remove) {
3592 sp<DeviceEffectProxy> proxy = mProxy.promote();
3593 if (proxy != nullptr) {
3594 proxy->removeEffect(effect);
3595 }
3596 if (handle->enabled()) {
3597 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3598 }
3599 }
3600 return true;
3601}
3602
3603status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3604 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3605 sp<EffectHalInterface> *effect) {
3606 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3607}
3608
3609status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
Andy Hung920f6572022-10-06 12:09:49 -07003610 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003611 sp<DeviceEffectProxy> proxy = mProxy.promote();
3612 if (proxy == nullptr) {
3613 return NO_INIT;
3614 }
3615 return proxy->addEffectToHal(effect);
3616}
3617
3618status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
Andy Hung920f6572022-10-06 12:09:49 -07003619 const sp<EffectHalInterface>& effect) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003620 sp<DeviceEffectProxy> proxy = mProxy.promote();
3621 if (proxy == nullptr) {
3622 return NO_INIT;
3623 }
Eric Laurent76c89f32021-12-03 17:13:23 +01003624 return proxy->removeEffectFromHal(effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003625}
3626
3627bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3628 sp<DeviceEffectProxy> proxy = mProxy.promote();
3629 if (proxy == nullptr) {
3630 return true;
3631 }
3632 return proxy->isOutput();
3633}
3634
3635uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3636 sp<DeviceEffectProxy> proxy = mProxy.promote();
3637 if (proxy == nullptr) {
3638 return DEFAULT_OUTPUT_SAMPLE_RATE;
3639 }
3640 return proxy->sampleRate();
3641}
3642
Eric Laurentf1f22e72021-07-13 14:04:14 +02003643audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3644 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003645 sp<DeviceEffectProxy> proxy = mProxy.promote();
3646 if (proxy == nullptr) {
3647 return AUDIO_CHANNEL_OUT_STEREO;
3648 }
3649 return proxy->channelMask();
3650}
3651
Eric Laurentf1f22e72021-07-13 14:04:14 +02003652uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3653 sp<DeviceEffectProxy> proxy = mProxy.promote();
3654 if (proxy == nullptr) {
3655 return 2;
3656 }
3657 return proxy->channelCount();
3658}
3659
3660audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3661 sp<DeviceEffectProxy> proxy = mProxy.promote();
3662 if (proxy == nullptr) {
3663 return AUDIO_CHANNEL_OUT_STEREO;
3664 }
3665 return proxy->channelMask();
3666}
3667
3668uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003669 sp<DeviceEffectProxy> proxy = mProxy.promote();
3670 if (proxy == nullptr) {
3671 return 2;
3672 }
3673 return proxy->channelCount();
3674}
3675
Eric Laurent76c89f32021-12-03 17:13:23 +01003676void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectEnable(
3677 const sp<EffectBase>& effectBase) {
3678 sp<EffectModule> effect = effectBase->asEffectModule();
3679 if (effect == nullptr) {
3680 return;
3681 }
3682 effect->start();
3683}
3684
3685void AudioFlinger::DeviceEffectProxy::ProxyCallback::onEffectDisable(
3686 const sp<EffectBase>& effectBase) {
3687 sp<EffectModule> effect = effectBase->asEffectModule();
3688 if (effect == nullptr) {
3689 return;
3690 }
3691 effect->stop();
3692}
3693
Glenn Kasten63238ef2015-03-02 15:50:29 -08003694} // namespace android