blob: bd661f9ac65ed452e3d7be89bb5fc98d2dc40ada [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>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070028#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070029#include <system/audio_effects/effect_ns.h>
30#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080031#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080032#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070033#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070035#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070037#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070038#include <media/audiohal/EffectHalInterface.h>
39#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44// ----------------------------------------------------------------------------
45
46// Note: the following macro is used for extremely verbose logging message. In
47// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
48// 0; but one side effect of this is to turn all LOGV's as well. Some messages
49// are so verbose that we want to suppress them even when we have ALOG_ASSERT
50// turned on. Do not uncomment the #def below unless you really know what you
51// are doing and want to see all of the extremely verbose messages.
52//#define VERY_VERY_VERBOSE_LOGGING
53#ifdef VERY_VERY_VERBOSE_LOGGING
54#define ALOGVV ALOGV
55#else
56#define ALOGVV(a...) do { } while(0)
57#endif
58
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090059#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
60
Eric Laurentca7cc822012-11-19 14:55:58 -080061namespace android {
62
Andy Hung1131b6e2020-12-08 20:47:45 -080063using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070064using binder::Status;
65
66namespace {
67
68// Append a POD value into a vector of bytes.
69template<typename T>
70void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
71 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
72 buffer->insert(buffer->end(), ar, ar + sizeof(T));
73}
74
75// Write a POD value into a vector of bytes (clears the previous buffer
76// content).
77template<typename T>
78void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
79 buffer->clear();
80 appendToBuffer(value, buffer);
81}
82
83} // namespace
84
Eric Laurentca7cc822012-11-19 14:55:58 -080085// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080086// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080087// ----------------------------------------------------------------------------
88
89#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080090#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080091
Eric Laurent41709552019-12-16 19:34:05 -080092AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080093 effect_descriptor_t *desc,
94 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080095 audio_session_t sessionId,
96 bool pinned)
97 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080098 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080099 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800100{
Eric Laurentca7cc822012-11-19 14:55:58 -0800101}
102
Eric Laurent41709552019-12-16 19:34:05 -0800103// must be called with EffectModule::mLock held
104status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800105{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800106
Eric Laurent41709552019-12-16 19:34:05 -0800107 ALOGV("setEnabled %p enabled %d", this, enabled);
108
109 if (enabled != isEnabled()) {
110 switch (mState) {
111 // going from disabled to enabled
112 case IDLE:
113 mState = STARTING;
114 break;
115 case STOPPED:
116 mState = RESTART;
117 break;
118 case STOPPING:
119 mState = ACTIVE;
120 break;
121
122 // going from enabled to disabled
123 case RESTART:
124 mState = STOPPED;
125 break;
126 case STARTING:
127 mState = IDLE;
128 break;
129 case ACTIVE:
130 mState = STOPPING;
131 break;
132 case DESTROYED:
133 return NO_ERROR; // simply ignore as we are being destroyed
134 }
135 for (size_t i = 1; i < mHandles.size(); i++) {
136 EffectHandle *h = mHandles[i];
137 if (h != NULL && !h->disconnected()) {
138 h->setEnabled(enabled);
139 }
140 }
141 }
142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800143}
144
Eric Laurent41709552019-12-16 19:34:05 -0800145status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
146{
147 status_t status;
148 {
149 Mutex::Autolock _l(mLock);
150 status = setEnabled_l(enabled);
151 }
152 if (fromHandle) {
153 if (enabled) {
154 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700155 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800156 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700157 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800158 }
159 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700160 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800161 }
162 }
163 return status;
164}
165
166bool AudioFlinger::EffectBase::isEnabled() const
167{
168 switch (mState) {
169 case RESTART:
170 case STARTING:
171 case ACTIVE:
172 return true;
173 case IDLE:
174 case STOPPING:
175 case STOPPED:
176 case DESTROYED:
177 default:
178 return false;
179 }
180}
181
182void AudioFlinger::EffectBase::setSuspended(bool suspended)
183{
184 Mutex::Autolock _l(mLock);
185 mSuspended = suspended;
186}
187
188bool AudioFlinger::EffectBase::suspended() const
189{
190 Mutex::Autolock _l(mLock);
191 return mSuspended;
192}
193
194status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800195{
196 status_t status;
197
198 Mutex::Autolock _l(mLock);
199 int priority = handle->priority();
200 size_t size = mHandles.size();
201 EffectHandle *controlHandle = NULL;
202 size_t i;
203 for (i = 0; i < size; i++) {
204 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800205 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800206 continue;
207 }
208 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 if (h->priority() <= priority) {
213 break;
214 }
215 }
216 // if inserted in first place, move effect control from previous owner to this handle
217 if (i == 0) {
218 bool enabled = false;
219 if (controlHandle != NULL) {
220 enabled = controlHandle->enabled();
221 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
222 }
223 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
224 status = NO_ERROR;
225 } else {
226 status = ALREADY_EXISTS;
227 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700228 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800229 mHandles.insertAt(handle, i);
230 return status;
231}
232
Eric Laurent41709552019-12-16 19:34:05 -0800233status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700234{
235 status_t status = NO_ERROR;
236 bool doRegister = false;
237 bool registered = false;
238 bool doEnable = false;
239 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700240 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700242
243 {
244 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200245
246 if ((isInternal_l() && !mPolicyRegistered)
247 || !getCallback()->isAudioPolicyReady()) {
248 return NO_ERROR;
249 }
250
Eric Laurent6c796322019-04-09 14:13:17 -0700251 // register effect when first handle is attached and unregister when last handle is removed
252 if (mPolicyRegistered != mHandles.size() > 0) {
253 doRegister = true;
254 mPolicyRegistered = mHandles.size() > 0;
255 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700256 const auto callback = getCallback();
257 io = callback->io();
258 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700259 }
260 }
261 // enable effect when registered according to enable state requested by controlling handle
262 if (mHandles.size() > 0) {
263 EffectHandle *handle = controlHandle_l();
264 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
265 doEnable = true;
266 mPolicyEnabled = handle->enabled();
267 }
268 }
269 registered = mPolicyRegistered;
270 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100271 // The simultaneous release of two EffectHandles with the same EffectModule
272 // may cause us to call this method at the same time.
273 // This may deadlock under some circumstances (b/180941720). Avoid this.
274 if (!doRegister && !(registered && doEnable)) {
275 return NO_ERROR;
276 }
Eric Laurent6c796322019-04-09 14:13:17 -0700277 mPolicyLock.lock();
278 }
279 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
280 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
281 if (doRegister) {
282 if (registered) {
283 status = AudioSystem::registerEffect(
284 &mDescriptor,
285 io,
286 strategy,
287 mSessionId,
288 mId);
289 } else {
290 status = AudioSystem::unregisterEffect(mId);
291 }
292 }
293 if (registered && doEnable) {
294 status = AudioSystem::setEffectEnabled(mId, enabled);
295 }
296 mPolicyLock.unlock();
297
298 return status;
299}
300
301
Eric Laurent41709552019-12-16 19:34:05 -0800302ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800303{
304 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800305 return removeHandle_l(handle);
306}
307
Eric Laurent41709552019-12-16 19:34:05 -0800308ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309{
Eric Laurentca7cc822012-11-19 14:55:58 -0800310 size_t size = mHandles.size();
311 size_t i;
312 for (i = 0; i < size; i++) {
313 if (mHandles[i] == handle) {
314 break;
315 }
316 }
317 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800318 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
319 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800320 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800321 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800322
323 mHandles.removeAt(i);
324 // if removed from first place, move effect control from this handle to next in line
325 if (i == 0) {
326 EffectHandle *h = controlHandle_l();
327 if (h != NULL) {
328 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
329 }
330 }
331
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530332 // Prevent calls to process() and other functions on effect interface from now on.
333 // The effect engine will be released by the destructor when the last strong reference on
334 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 if (mHandles.size() == 0 && !mPinned) {
336 mState = DESTROYED;
337 }
338
339 return mHandles.size();
340}
341
342// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800343AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800344{
345 // the first valid handle in the list has control over the module
346 for (size_t i = 0; i < mHandles.size(); i++) {
347 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800348 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800349 return h;
350 }
351 }
352
353 return NULL;
354}
355
Eric Laurentf10c7092016-12-06 17:09:56 -0800356// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800357ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800358{
Andy Hungfda44002021-06-03 17:23:16 -0700359 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800360 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700361 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800362 return mHandles.size();
363 }
364
Eric Laurentf10c7092016-12-06 17:09:56 -0800365 Mutex::Autolock _l(mLock);
366 ssize_t numHandles = removeHandle_l(handle);
367 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800368 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700369 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800370 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800371 }
372 return numHandles;
373}
374
Eric Laurent41709552019-12-16 19:34:05 -0800375bool AudioFlinger::EffectBase::purgeHandles()
376{
377 bool enabled = false;
378 Mutex::Autolock _l(mLock);
379 EffectHandle *handle = controlHandle_l();
380 if (handle != NULL) {
381 enabled = handle->enabled();
382 }
383 mHandles.clear();
384 return enabled;
385}
386
387void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700388 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800389}
390
391static String8 effectFlagsToString(uint32_t flags) {
392 String8 s;
393
394 s.append("conn. mode: ");
395 switch (flags & EFFECT_FLAG_TYPE_MASK) {
396 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
397 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
398 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
399 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
400 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
401 default: s.append("unknown/reserved"); break;
402 }
403 s.append(", ");
404
405 s.append("insert pref: ");
406 switch (flags & EFFECT_FLAG_INSERT_MASK) {
407 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
408 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
409 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
410 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
411 default: s.append("unknown/reserved"); break;
412 }
413 s.append(", ");
414
415 s.append("volume mgmt: ");
416 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
417 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
418 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
419 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
420 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
421 default: s.append("unknown/reserved"); break;
422 }
423 s.append(", ");
424
425 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
426 if (devind) {
427 s.append("device indication: ");
428 switch (devind) {
429 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
430 default: s.append("unknown/reserved"); break;
431 }
432 s.append(", ");
433 }
434
435 s.append("input mode: ");
436 switch (flags & EFFECT_FLAG_INPUT_MASK) {
437 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
438 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
439 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
440 default: s.append("not set"); break;
441 }
442 s.append(", ");
443
444 s.append("output mode: ");
445 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
446 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
447 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
448 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
449 default: s.append("not set"); break;
450 }
451 s.append(", ");
452
453 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
454 if (accel) {
455 s.append("hardware acceleration: ");
456 switch (accel) {
457 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
458 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
459 default: s.append("unknown/reserved"); break;
460 }
461 s.append(", ");
462 }
463
464 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
465 if (modeind) {
466 s.append("mode indication: ");
467 switch (modeind) {
468 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
469 default: s.append("unknown/reserved"); break;
470 }
471 s.append(", ");
472 }
473
474 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
475 if (srcind) {
476 s.append("source indication: ");
477 switch (srcind) {
478 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
479 default: s.append("unknown/reserved"); break;
480 }
481 s.append(", ");
482 }
483
484 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
485 s.append("offloadable, ");
486 }
487
488 int len = s.length();
489 if (s.length() > 2) {
490 (void) s.lockBuffer(len);
491 s.unlockBuffer(len - 2);
492 }
493 return s;
494}
495
496void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
497{
498 String8 result;
499
500 result.appendFormat("\tEffect ID %d:\n", mId);
501
502 bool locked = AudioFlinger::dumpTryLock(mLock);
503 // failed to lock - AudioFlinger is probably deadlocked
504 if (!locked) {
505 result.append("\t\tCould not lock Fx mutex:\n");
506 }
507
508 result.append("\t\tSession State Registered Enabled Suspended:\n");
509 result.appendFormat("\t\t%05d %03d %s %s %s\n",
510 mSessionId, mState, mPolicyRegistered ? "y" : "n",
511 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
512
513 result.append("\t\tDescriptor:\n");
514 char uuidStr[64];
515 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
516 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
517 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
518 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
519 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
520 mDescriptor.apiVersion,
521 mDescriptor.flags,
522 effectFlagsToString(mDescriptor.flags).string());
523 result.appendFormat("\t\t- name: %s\n",
524 mDescriptor.name);
525
526 result.appendFormat("\t\t- implementor: %s\n",
527 mDescriptor.implementor);
528
529 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
530 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
531 char buffer[256];
532 for (size_t i = 0; i < mHandles.size(); ++i) {
533 EffectHandle *handle = mHandles[i];
534 if (handle != NULL && !handle->disconnected()) {
535 handle->dumpToBuffer(buffer, sizeof(buffer));
536 result.append(buffer);
537 }
538 }
539 if (locked) {
540 mLock.unlock();
541 }
542
543 write(fd, result.string(), result.length());
544}
545
546// ----------------------------------------------------------------------------
547// EffectModule implementation
548// ----------------------------------------------------------------------------
549
550#undef LOG_TAG
551#define LOG_TAG "AudioFlinger::EffectModule"
552
553AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
554 effect_descriptor_t *desc,
555 int id,
556 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800557 bool pinned,
558 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800559 : EffectBase(callback, desc, id, sessionId, pinned),
560 // clear mConfig to ensure consistent initial value of buffer framecount
561 // in case buffers are associated by setInBuffer() or setOutBuffer()
562 // prior to configure().
563 mConfig{{}, {}},
564 mStatus(NO_INIT),
565 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
566 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800567 mOffloaded(false),
568 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800569#ifdef FLOAT_EFFECT_CHAIN
570 , mSupportsFloat(false)
571#endif
572{
573 ALOGV("Constructor %p pinned %d", this, pinned);
574 int lStatus;
575
576 // create effect engine from effect factory
577 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800578 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800579 if (mStatus != NO_ERROR) {
580 return;
581 }
582 lStatus = init();
583 if (lStatus < 0) {
584 mStatus = lStatus;
585 goto Error;
586 }
587
588 setOffloaded(callback->isOffload(), callback->io());
589 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
590
591 return;
592Error:
593 mEffectInterface.clear();
594 ALOGV("Constructor Error %d", mStatus);
595}
596
597AudioFlinger::EffectModule::~EffectModule()
598{
599 ALOGV("Destructor %p", this);
600 if (mEffectInterface != 0) {
601 char uuidStr[64];
602 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
603 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
604 this, uuidStr);
605 release_l();
606 }
607
608}
609
Eric Laurentfa1e1232016-08-02 19:01:49 -0700610bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800611 Mutex::Autolock _l(mLock);
612
Eric Laurentfa1e1232016-08-02 19:01:49 -0700613 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800614 switch (mState) {
615 case RESTART:
616 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700617 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800618
619 case STARTING:
620 // clear auxiliary effect input buffer for next accumulation
621 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
622 memset(mConfig.inputCfg.buffer.raw,
623 0,
624 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
625 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700626 if (start_l() == NO_ERROR) {
627 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700628 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700629 } else {
630 mState = IDLE;
631 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800632 break;
633 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900634 // volume control for offload and direct threads must take effect immediately.
635 if (stop_l() == NO_ERROR
636 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700637 mDisableWaitCnt = mMaxDisableWaitCnt;
638 } else {
639 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
640 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800641 mState = STOPPED;
642 break;
643 case STOPPED:
644 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
645 // turn off sequence.
646 if (--mDisableWaitCnt == 0) {
647 reset_l();
648 mState = IDLE;
649 }
650 break;
651 default: //IDLE , ACTIVE, DESTROYED
652 break;
653 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700654
655 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800656}
657
658void AudioFlinger::EffectModule::process()
659{
660 Mutex::Autolock _l(mLock);
661
Mikhail Naganov022b9952017-01-04 16:36:51 -0800662 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800663 return;
664 }
665
rago94a1ee82017-07-21 15:11:02 -0700666 const uint32_t inChannelCount =
667 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
668 const uint32_t outChannelCount =
669 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
670 const bool auxType =
671 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
672
Andy Hungfa69ca32017-11-30 10:07:53 -0800673 // safeInputOutputSampleCount is 0 if the channel count between input and output
674 // buffers do not match. This prevents automatic accumulation or copying between the
675 // input and output effect buffers without an intermediary effect process.
676 // TODO: consider implementing channel conversion.
677 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700678 mInChannelCountRequested != mOutChannelCountRequested ? 0
679 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800680 mConfig.inputCfg.buffer.frameCount,
681 mConfig.outputCfg.buffer.frameCount);
682 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
683#ifdef FLOAT_EFFECT_CHAIN
684 accumulate_float(
685 mConfig.outputCfg.buffer.f32,
686 mConfig.inputCfg.buffer.f32,
687 safeInputOutputSampleCount);
688#else
689 accumulate_i16(
690 mConfig.outputCfg.buffer.s16,
691 mConfig.inputCfg.buffer.s16,
692 safeInputOutputSampleCount);
693#endif
694 };
695 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
696#ifdef FLOAT_EFFECT_CHAIN
697 memcpy(
698 mConfig.outputCfg.buffer.f32,
699 mConfig.inputCfg.buffer.f32,
700 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
701
702#else
703 memcpy(
704 mConfig.outputCfg.buffer.s16,
705 mConfig.inputCfg.buffer.s16,
706 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
707#endif
708 };
709
Eric Laurentca7cc822012-11-19 14:55:58 -0800710 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700711 int ret;
712 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700713 if (auxType) {
714 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800715 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700716#ifdef FLOAT_EFFECT_CHAIN
717 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800718#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700719 // Do in-place float conversion for auxiliary effect input buffer.
720 static_assert(sizeof(float) <= sizeof(int32_t),
721 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
722
Andy Hungfa69ca32017-11-30 10:07:53 -0800723 memcpy_to_float_from_q4_27(
724 mConfig.inputCfg.buffer.f32,
725 mConfig.inputCfg.buffer.s32,
726 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800727#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800728 } else
Andy Hung116a4982017-11-30 10:15:08 -0800729#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800730 {
Andy Hung116a4982017-11-30 10:15:08 -0800731#ifdef FLOAT_AUX
732 memcpy_to_i16_from_float(
733 mConfig.inputCfg.buffer.s16,
734 mConfig.inputCfg.buffer.f32,
735 mConfig.inputCfg.buffer.frameCount);
736#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 memcpy_to_i16_from_q4_27(
738 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700739 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800740 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800741#endif
rago94a1ee82017-07-21 15:11:02 -0700742 }
rago94a1ee82017-07-21 15:11:02 -0700743 }
744#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800745 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
746 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
747
748 if (!auxType && mInChannelCountRequested != inChannelCount) {
749 adjust_channels(
750 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
751 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
752 sizeof(float),
753 sizeof(float)
754 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
755 inBuffer = mInConversionBuffer;
756 }
757 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
758 && mOutChannelCountRequested != outChannelCount) {
759 adjust_selected_channels(
760 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
761 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
762 sizeof(float),
763 sizeof(float)
764 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
765 outBuffer = mOutConversionBuffer;
766 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
768 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800769 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
771 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700772 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800773 memcpy_to_i16_from_float(
774 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800775 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800777 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700778 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800779 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800780 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
782 goto data_bypass;
783 }
784 memcpy_to_i16_from_float(
785 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800786 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700789 }
790 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800791#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800792 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800793#ifdef FLOAT_EFFECT_CHAIN
794 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800795 sp<EffectBufferHalInterface> target =
796 mOutChannelCountRequested != outChannelCount
797 ? mOutConversionBuffer : mOutBuffer;
798
Andy Hungfa69ca32017-11-30 10:07:53 -0800799 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800800 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800801 mOutConversionBuffer->audioBuffer()->s16,
802 outChannelCount * mConfig.outputCfg.buffer.frameCount);
803 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800804 if (mOutChannelCountRequested != outChannelCount) {
805 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
806 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
807 sizeof(float),
808 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
809 }
rago94a1ee82017-07-21 15:11:02 -0700810#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700811 } else {
rago94a1ee82017-07-21 15:11:02 -0700812#ifdef FLOAT_EFFECT_CHAIN
813 data_bypass:
814#endif
815 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800816 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700817 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800818 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700819 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800820 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700821 }
822 }
823 ret = -ENODATA;
824 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800825
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 // force transition to IDLE state when engine is ready
827 if (mState == STOPPED && ret == -ENODATA) {
828 mDisableWaitCnt = 1;
829 }
830
831 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700832 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800833#ifdef FLOAT_AUX
834 const size_t size =
835 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
836#else
rago94a1ee82017-07-21 15:11:02 -0700837 const size_t size =
838 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800839#endif
rago94a1ee82017-07-21 15:11:02 -0700840 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800841 }
842 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700843 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800844 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
845 // If an insert effect is idle and input buffer is different from output buffer,
846 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700847 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700848 // similar handling with data_bypass above.
849 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
850 accumulateInputToOutput();
851 } else { // EFFECT_BUFFER_ACCESS_WRITE
852 copyInputToOutput();
853 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 }
855 }
856}
857
858void AudioFlinger::EffectModule::reset_l()
859{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700860 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800861 return;
862 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700863 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800864}
865
866status_t AudioFlinger::EffectModule::configure()
867{
rago94a1ee82017-07-21 15:11:02 -0700868 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700869 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700870 uint32_t size;
871 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700872 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700873
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700874 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700875 status = NO_INIT;
876 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800877 }
878
Eric Laurentca7cc822012-11-19 14:55:58 -0800879 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800880 // TODO: handle configuration of input (record) SW effects above the HAL,
881 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
882 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700883 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200884 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800885 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200886 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800887
888 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800889 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
890 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
891 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
892 mConfig.inputCfg.channels);
893 }
894#ifndef MULTICHANNEL_EFFECT_CHAIN
895 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
896 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
897 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
898 mConfig.outputCfg.channels);
899 }
900#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800901 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800902#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700903 // TODO: Update this logic when multichannel effects are implemented.
904 // For offloaded tracks consider mono output as stereo for proper effect initialization
905 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
906 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
907 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
908 ALOGV("Overriding effect input and output as STEREO");
909 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800910#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800911 }
jiabineb3bda02020-06-30 14:07:03 -0700912 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700913 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700914 mConfig.inputCfg.channels |= hapticChannelMask;
915 mConfig.outputCfg.channels |= hapticChannelMask;
916 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800917 mInChannelCountRequested =
918 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
919 mOutChannelCountRequested =
920 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700921
rago94a1ee82017-07-21 15:11:02 -0700922 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
923 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900924
925 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700926 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900927 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
928 ALOGV("Overriding effect input as 48kHz");
929 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700930 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900931 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800932 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
933 mConfig.inputCfg.bufferProvider.cookie = NULL;
934 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
935 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
936 mConfig.outputCfg.bufferProvider.cookie = NULL;
937 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
938 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
939 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
940 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800941 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800942 // always overwrites output buffer: input buffer == output buffer
943 // - in other sessions:
944 // last effect in the chain accumulates in output buffer: input buffer != output buffer
945 // other effect: overwrites output buffer: input buffer == output buffer
946 // Auxiliary effect:
947 // accumulates in output buffer: input buffer != output buffer
948 // Therefore: accumulate <=> input buffer != output buffer
949 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
950 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
951 } else {
952 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
953 }
954 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
955 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700956 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800957 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
958
Eric Laurent6b446ce2019-12-13 10:56:31 -0800959 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700960 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800961 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800962
963 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700964 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700965 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800966 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700967 &mConfig,
968 &size,
969 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700970 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800971 status = cmdStatus;
972 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800973
974#ifdef MULTICHANNEL_EFFECT_CHAIN
975 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700976 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800977 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
978 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
979 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700980 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
981 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800982 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
983 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
984 }
985 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
986 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
987 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
988 }
989 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700990 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800991 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700992 &mConfig,
993 &size,
994 &cmdStatus);
995 if (status == NO_ERROR) {
996 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800997 }
998 }
999#endif
1000
1001#ifdef FLOAT_EFFECT_CHAIN
1002 if (status == NO_ERROR) {
1003 mSupportsFloat = true;
1004 }
1005
1006 if (status != NO_ERROR) {
1007 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1008 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1009 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1010 size = sizeof(int);
1011 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1012 sizeof(mConfig),
1013 &mConfig,
1014 &size,
1015 &cmdStatus);
1016 if (status == NO_ERROR) {
1017 status = cmdStatus;
1018 }
1019 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001020 mSupportsFloat = false;
1021 ALOGVV("config worked with 16 bit");
1022 } else {
1023 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001024 }
rago94a1ee82017-07-21 15:11:02 -07001025 }
1026#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001027
rago94a1ee82017-07-21 15:11:02 -07001028 if (status == NO_ERROR) {
1029 // Establish Buffer strategy
1030 setInBuffer(mInBuffer);
1031 setOutBuffer(mOutBuffer);
1032
1033 // Update visualizer latency
1034 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1035 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1036 effect_param_t *p = (effect_param_t *)buf32;
1037
1038 p->psize = sizeof(uint32_t);
1039 p->vsize = sizeof(uint32_t);
1040 size = sizeof(int);
1041 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1042
Andy Hungfda44002021-06-03 17:23:16 -07001043 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001044
1045 *((int32_t *)p->data + 1)= latency;
1046 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1047 sizeof(effect_param_t) + 8,
1048 &buf32,
1049 &size,
1050 &cmdStatus);
1051 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001052 }
1053
Andy Hung05083ac2017-12-14 15:00:28 -08001054 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1055 mMaxDisableWaitCnt = (uint32_t)std::max(
1056 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1057 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1058 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001059
Eric Laurentd0ebb532013-04-02 16:41:41 -07001060exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001061 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001062 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001063 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return status;
1065}
1066
1067status_t AudioFlinger::EffectModule::init()
1068{
1069 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001070 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001071 return NO_INIT;
1072 }
1073 status_t cmdStatus;
1074 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001075 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1076 0,
1077 NULL,
1078 &size,
1079 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 if (status == 0) {
1081 status = cmdStatus;
1082 }
1083 return status;
1084}
1085
Eric Laurent1b928682014-10-02 19:41:47 -07001086void AudioFlinger::EffectModule::addEffectToHal_l()
1087{
1088 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1089 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001090 if (mAddedToHal) {
1091 return;
1092 }
1093
Andy Hungfda44002021-06-03 17:23:16 -07001094 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001095 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001096 }
1097}
1098
Eric Laurentfa1e1232016-08-02 19:01:49 -07001099// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001100status_t AudioFlinger::EffectModule::start()
1101{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001102 status_t status;
1103 {
1104 Mutex::Autolock _l(mLock);
1105 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001106 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001107 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001108 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001109 }
1110 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001111}
1112
1113status_t AudioFlinger::EffectModule::start_l()
1114{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001115 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 return NO_INIT;
1117 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001118 if (mStatus != NO_ERROR) {
1119 return mStatus;
1120 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 status_t cmdStatus;
1122 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001123 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1124 0,
1125 NULL,
1126 &size,
1127 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 if (status == 0) {
1129 status = cmdStatus;
1130 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001131 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001132 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001133 }
1134 return status;
1135}
1136
1137status_t AudioFlinger::EffectModule::stop()
1138{
1139 Mutex::Autolock _l(mLock);
1140 return stop_l();
1141}
1142
1143status_t AudioFlinger::EffectModule::stop_l()
1144{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001145 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 return NO_INIT;
1147 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001148 if (mStatus != NO_ERROR) {
1149 return mStatus;
1150 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001151 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001153
1154 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001155 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1156 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1157 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001158 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001159 mSetVolumeReentrantTid = INVALID_PID;
1160 }
1161
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001162 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1163 0,
1164 NULL,
1165 &size,
1166 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001167 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001168 status = cmdStatus;
1169 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001170 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001171 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001172 }
1173 return status;
1174}
1175
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001176// must be called with EffectChain::mLock held
1177void AudioFlinger::EffectModule::release_l()
1178{
1179 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001180 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001181 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001182 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001183 mEffectInterface.clear();
1184 }
1185}
1186
Eric Laurent6b446ce2019-12-13 10:56:31 -08001187status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001188{
1189 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1190 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001191 if (!mAddedToHal) {
1192 return NO_ERROR;
1193 }
1194
Andy Hungfda44002021-06-03 17:23:16 -07001195 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001196 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001197 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001198 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001199}
1200
Andy Hunge4a1d912016-08-17 14:11:13 -07001201// round up delta valid if value and divisor are positive.
1202template <typename T>
1203static T roundUpDelta(const T &value, const T &divisor) {
1204 T remainder = value % divisor;
1205 return remainder == 0 ? 0 : divisor - remainder;
1206}
1207
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001208status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1209 const std::vector<uint8_t>& cmdData,
1210 int32_t maxReplySize,
1211 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001212{
1213 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001214 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001215
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001216 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001217 return NO_INIT;
1218 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001219 if (mStatus != NO_ERROR) {
1220 return mStatus;
1221 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001222 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1223 return -EINVAL;
1224 }
1225 size_t cmdSize = cmdData.size();
1226 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1227 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1228 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001229 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001230 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001231 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001232 android_errorWriteLog(0x534e4554, "33003822");
1233 return -EINVAL;
1234 }
1235 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001236 (maxReplySize < sizeof(effect_param_t) ||
1237 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001238 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001239 return -EINVAL;
1240 }
ragoe2759072016-11-22 18:02:48 -08001241 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001242 (sizeof(effect_param_t) > maxReplySize
1243 || param->psize > maxReplySize - sizeof(effect_param_t)
1244 || param->vsize > maxReplySize - sizeof(effect_param_t)
1245 - param->psize
1246 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1247 maxReplySize
1248 - sizeof(effect_param_t)
1249 - param->psize
1250 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001251 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1252 android_errorWriteLog(0x534e4554, "32705438");
1253 return -EINVAL;
1254 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001255 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001256 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1257 && // DEFERRED not generally used
1258 (param == nullptr
1259 || param->psize > cmdSize - sizeof(effect_param_t)
1260 || param->vsize > cmdSize - sizeof(effect_param_t)
1261 - param->psize
1262 || roundUpDelta(param->psize,
1263 (uint32_t) sizeof(int)) >
1264 cmdSize
1265 - sizeof(effect_param_t)
1266 - param->psize
1267 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001268 android_errorWriteLog(0x534e4554, "30204301");
1269 return -EINVAL;
1270 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001271 uint32_t replySize = maxReplySize;
1272 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001273 status_t status = mEffectInterface->command(cmdCode,
1274 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001275 const_cast<uint8_t*>(cmdData.data()),
1276 &replySize,
1277 reply->data());
1278 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001279 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001280 for (size_t i = 1; i < mHandles.size(); i++) {
1281 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001282 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001283 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001284 }
1285 }
1286 }
1287 return status;
1288}
1289
Eric Laurentca7cc822012-11-19 14:55:58 -08001290bool AudioFlinger::EffectModule::isProcessEnabled() const
1291{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001292 if (mStatus != NO_ERROR) {
1293 return false;
1294 }
1295
Eric Laurentca7cc822012-11-19 14:55:58 -08001296 switch (mState) {
1297 case RESTART:
1298 case ACTIVE:
1299 case STOPPING:
1300 case STOPPED:
1301 return true;
1302 case IDLE:
1303 case STARTING:
1304 case DESTROYED:
1305 default:
1306 return false;
1307 }
1308}
1309
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001310bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1311{
Andy Hungfda44002021-06-03 17:23:16 -07001312 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001313}
1314
1315bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1316{
1317 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1318}
1319
Mikhail Naganov022b9952017-01-04 16:36:51 -08001320void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001321 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001322
1323 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001324 if (buffer != 0) {
1325 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1326 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1327 } else {
1328 mConfig.inputCfg.buffer.raw = NULL;
1329 }
1330 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001331 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001332
1333#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001334 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001335 // Theoretically insert effects can also do in-place conversions (destroying
1336 // the original buffer) when the output buffer is identical to the input buffer,
1337 // but we don't optimize for it here.
1338 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001339 const uint32_t inChannelCount =
1340 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1341 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001342 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001343 // we need to translate - create hidl shared buffer and intercept
1344 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001345 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1346 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1347 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001348
1349 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1350 __func__, inChannels, inFrameCount, size);
1351
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001352 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001353 || size > mInConversionBuffer->getSize())) {
1354 mInConversionBuffer.clear();
1355 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001356 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001357 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001358 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001359 mInConversionBuffer->setFrameCount(inFrameCount);
1360 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001361 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001362 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001363 }
1364 }
1365#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001366}
1367
1368void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001369 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001370
1371 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001372 if (buffer != 0) {
1373 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1374 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1375 } else {
1376 mConfig.outputCfg.buffer.raw = NULL;
1377 }
1378 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001379 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001380
1381#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001382 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001383 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001384 const uint32_t outChannelCount =
1385 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1386 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001387 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001388 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001389 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1390 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1391 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001392
1393 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1394 __func__, outChannels, outFrameCount, size);
1395
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001396 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001397 || size > mOutConversionBuffer->getSize())) {
1398 mOutConversionBuffer.clear();
1399 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001400 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001401 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001402 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001403 mOutConversionBuffer->setFrameCount(outFrameCount);
1404 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001405 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001406 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001407 }
1408 }
1409#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001410}
1411
Eric Laurentca7cc822012-11-19 14:55:58 -08001412status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1413{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001414 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001415 if (mStatus != NO_ERROR) {
1416 return mStatus;
1417 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1420 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1421 if (isProcessEnabled() &&
1422 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001423 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1424 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 uint32_t volume[2];
1426 uint32_t *pVolume = NULL;
1427 uint32_t size = sizeof(volume);
1428 volume[0] = *left;
1429 volume[1] = *right;
1430 if (controller) {
1431 pVolume = volume;
1432 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001433 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1434 size,
1435 volume,
1436 &size,
1437 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001438 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1439 *left = volume[0];
1440 *right = volume[1];
1441 }
1442 }
1443 return status;
1444}
1445
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001446void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1447{
Zhou Songd505c642020-02-20 16:35:37 +08001448 // for offload or direct thread, if the effect chain has non-offloadable
1449 // effect and any effect module within the chain has volume control, then
1450 // volume control is delegated to effect, otherwise, set volume to hal.
1451 if (mEffectCallback->isOffloadOrDirect() &&
1452 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001453 float vol_l = (float)left / (1 << 24);
1454 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001455 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001456 }
1457}
1458
jiabin8f278ee2019-11-11 12:16:27 -08001459status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1460 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001461{
jiabin8f278ee2019-11-11 12:16:27 -08001462 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1463 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 return NO_ERROR;
1465 }
1466
1467 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001468 if (mStatus != NO_ERROR) {
1469 return mStatus;
1470 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001472 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 status_t cmdStatus;
1474 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001475 // FIXME: use audio device types and addresses when the hal interface is ready.
1476 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001477 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001478 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001479 &size,
1480 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 }
1482 return status;
1483}
1484
jiabin8f278ee2019-11-11 12:16:27 -08001485status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1486{
1487 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1488}
1489
1490status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1491{
1492 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1493}
1494
Eric Laurentca7cc822012-11-19 14:55:58 -08001495status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1496{
1497 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001498 if (mStatus != NO_ERROR) {
1499 return mStatus;
1500 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001501 status_t status = NO_ERROR;
1502 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1503 status_t cmdStatus;
1504 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001505 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1506 sizeof(audio_mode_t),
1507 &mode,
1508 &size,
1509 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 if (status == NO_ERROR) {
1511 status = cmdStatus;
1512 }
1513 }
1514 return status;
1515}
1516
1517status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1518{
1519 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001520 if (mStatus != NO_ERROR) {
1521 return mStatus;
1522 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001523 status_t status = NO_ERROR;
1524 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1525 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001526 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1527 sizeof(audio_source_t),
1528 &source,
1529 &size,
1530 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001531 }
1532 return status;
1533}
1534
Eric Laurent5baf2af2013-09-12 17:37:00 -07001535status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1536{
1537 Mutex::Autolock _l(mLock);
1538 if (mStatus != NO_ERROR) {
1539 return mStatus;
1540 }
1541 status_t status = NO_ERROR;
1542 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1543 status_t cmdStatus;
1544 uint32_t size = sizeof(status_t);
1545 effect_offload_param_t cmd;
1546
1547 cmd.isOffload = offloaded;
1548 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001549 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1550 sizeof(effect_offload_param_t),
1551 &cmd,
1552 &size,
1553 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001554 if (status == NO_ERROR) {
1555 status = cmdStatus;
1556 }
1557 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1558 } else {
1559 if (offloaded) {
1560 status = INVALID_OPERATION;
1561 }
1562 mOffloaded = false;
1563 }
1564 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1565 return status;
1566}
1567
1568bool AudioFlinger::EffectModule::isOffloaded() const
1569{
1570 Mutex::Autolock _l(mLock);
1571 return mOffloaded;
1572}
1573
jiabineb3bda02020-06-30 14:07:03 -07001574/*static*/
1575bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1576 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1577}
1578
1579bool AudioFlinger::EffectModule::isHapticGenerator() const {
1580 return isHapticGenerator(&mDescriptor.type);
1581}
1582
jiabine70bc7f2020-06-30 22:07:55 -07001583status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1584{
1585 if (mStatus != NO_ERROR) {
1586 return mStatus;
1587 }
1588 if (!isHapticGenerator()) {
1589 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1590 return INVALID_OPERATION;
1591 }
1592
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001593 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1594 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001595 param->psize = sizeof(int32_t);
1596 param->vsize = sizeof(int32_t) * 2;
1597 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1598 *((int32_t*)param->data + 1) = id;
1599 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001600 std::vector<uint8_t> response;
1601 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001602 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001603 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1604 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001605 }
1606 return status;
1607}
1608
Lais Andradebc3f37a2021-07-02 00:13:19 +01001609status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001610{
1611 if (mStatus != NO_ERROR) {
1612 return mStatus;
1613 }
1614 if (!isHapticGenerator()) {
1615 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1616 return INVALID_OPERATION;
1617 }
1618
Lais Andradebc3f37a2021-07-02 00:13:19 +01001619 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001620 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001621 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001622 effect_param_t *param = (effect_param_t*) request.data();
1623 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001624 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001625 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1626 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001627 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1628 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1629 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001630 std::vector<uint8_t> response;
1631 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1632 if (status == NO_ERROR) {
1633 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1634 status = *reinterpret_cast<const status_t*>(response.data());
1635 }
1636 return status;
1637}
1638
Andy Hungbded9c82017-11-30 18:47:35 -08001639static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1640 std::stringstream ss;
1641
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001642 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001643 return "nullptr"; // make different than below
1644 } else if (buffer->externalData() != nullptr) {
1645 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1646 << " -> "
1647 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1648 } else {
1649 ss << buffer->audioBuffer()->raw;
1650 }
1651 return ss.str();
1652}
Marco Nelissenb2208842014-02-07 14:00:50 -08001653
Eric Laurent41709552019-12-16 19:34:05 -08001654void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001655{
Eric Laurent41709552019-12-16 19:34:05 -08001656 EffectBase::dump(fd, args);
1657
Eric Laurentca7cc822012-11-19 14:55:58 -08001658 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001659 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660
Eric Laurent41709552019-12-16 19:34:05 -08001661 result.append("\t\tStatus Engine:\n");
1662 result.appendFormat("\t\t%03d %p\n",
1663 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001664
1665 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001666
1667 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001668 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1669 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1670 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 mConfig.inputCfg.buffer.frameCount,
1672 mConfig.inputCfg.samplingRate,
1673 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001674 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001675 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001676
1677 result.append("\t\t- Output configuration:\n");
1678 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001679 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001680 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001681 mConfig.outputCfg.buffer.frameCount,
1682 mConfig.outputCfg.samplingRate,
1683 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001684 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001685 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001686
rago94a1ee82017-07-21 15:11:02 -07001687#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001688
Andy Hungbded9c82017-11-30 18:47:35 -08001689 result.appendFormat("\t\t- HAL buffers:\n"
1690 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1691 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1692 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1693 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1694 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001695#endif
1696
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 write(fd, result.string(), result.length());
1698
Mikhail Naganov4d547672019-02-22 14:19:19 -08001699 if (mEffectInterface != 0) {
1700 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1701 (void)mEffectInterface->dump(fd);
1702 }
1703
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 if (locked) {
1705 mLock.unlock();
1706 }
1707}
1708
1709// ----------------------------------------------------------------------------
1710// EffectHandle implementation
1711// ----------------------------------------------------------------------------
1712
1713#undef LOG_TAG
1714#define LOG_TAG "AudioFlinger::EffectHandle"
1715
Eric Laurent41709552019-12-16 19:34:05 -08001716AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001717 const sp<AudioFlinger::Client>& client,
1718 const sp<media::IEffectClient>& effectClient,
1719 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 : BnEffect(),
1721 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001722 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001723{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001724 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001725
1726 if (client == 0) {
1727 return;
1728 }
1729 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1730 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001731 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001732 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001733 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001734 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001735 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001736 return;
1737 }
Glenn Kastene75da402013-11-20 13:54:52 -08001738 new(mCblk) effect_param_cblk_t();
1739 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001740}
1741
1742AudioFlinger::EffectHandle::~EffectHandle()
1743{
1744 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001745 disconnect(false);
1746}
1747
Glenn Kastene75da402013-11-20 13:54:52 -08001748status_t AudioFlinger::EffectHandle::initCheck()
1749{
1750 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1751}
1752
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001753#define RETURN(code) \
1754 *_aidl_return = (code); \
1755 return Status::ok();
1756
1757Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001758{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001759 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001760 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001761 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001762 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001763 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001764 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001765 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001766 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001768
1769 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001770 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 }
1772
1773 mEnabled = true;
1774
Eric Laurent6c796322019-04-09 14:13:17 -07001775 status_t status = effect->updatePolicyState();
1776 if (status != NO_ERROR) {
1777 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001778 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001779 }
1780
Eric Laurent6b446ce2019-12-13 10:56:31 -08001781 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001782
1783 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001784 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001785 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 }
1787
Eric Laurent6b446ce2019-12-13 10:56:31 -08001788 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 mEnabled = false;
1791 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001792 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001793}
1794
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001795Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001796{
1797 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001798 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001799 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001800 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001801 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001802 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001803 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001804 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001805 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001806
1807 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001808 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 }
1810 mEnabled = false;
1811
Eric Laurent6c796322019-04-09 14:13:17 -07001812 effect->updatePolicyState();
1813
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001814 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001815 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001816 }
1817
Eric Laurent6b446ce2019-12-13 10:56:31 -08001818 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001819 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001820}
1821
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001822Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001823{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001824 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001825 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001826 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001827}
1828
1829void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1830{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001831 AutoMutex _l(mLock);
1832 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1833 if (mDisconnected) {
1834 if (unpinIfLast) {
1835 android_errorWriteLog(0x534e4554, "32707507");
1836 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001837 return;
1838 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001839 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001840 {
Eric Laurent41709552019-12-16 19:34:05 -08001841 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001842 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001843 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001844 ALOGW("%s Effect handle %p disconnected after thread destruction",
1845 __func__, this);
1846 }
1847 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001848 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001849 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001850
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 if (mClient != 0) {
1852 if (mCblk != NULL) {
1853 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1854 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1855 }
1856 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001857 // Client destructor must run with AudioFlinger client mutex locked
1858 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 mClient.clear();
1860 }
1861}
1862
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001863Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1864 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1865 return Status::ok();
1866}
1867
1868Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1869 const std::vector<uint8_t>& cmdData,
1870 int32_t maxResponseSize,
1871 std::vector<uint8_t>* response,
1872 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001873{
1874 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001875 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001876
Eric Laurentc7ab3092017-06-15 18:43:46 -07001877 // reject commands reserved for internal use by audio framework if coming from outside
1878 // of audioserver
1879 switch(cmdCode) {
1880 case EFFECT_CMD_ENABLE:
1881 case EFFECT_CMD_DISABLE:
1882 case EFFECT_CMD_SET_PARAM:
1883 case EFFECT_CMD_SET_PARAM_DEFERRED:
1884 case EFFECT_CMD_SET_PARAM_COMMIT:
1885 case EFFECT_CMD_GET_PARAM:
1886 break;
1887 default:
1888 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1889 break;
1890 }
1891 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001893 }
1894
Eric Laurent1ffc5852016-12-15 14:46:09 -08001895 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001896 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001897 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001898 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001899 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001900 writeToBuffer(NO_ERROR, response);
1901 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001902 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001903 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001904 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001906 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001907 writeToBuffer(NO_ERROR, response);
1908 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001909 }
1910
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001911 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001912 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001913 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001914 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001915 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001916 // only get parameter command is permitted for applications not controlling the effect
1917 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001918 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001919 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001920
1921 // handle commands that are not forwarded transparently to effect engine
1922 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001923 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001924 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001925 }
1926
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001927 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001928 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001929 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001930 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001931 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001932
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 // No need to trylock() here as this function is executed in the binder thread serving a
1934 // particular client process: no risk to block the whole media server process or mixer
1935 // threads if we are stuck here
1936 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001937 // keep local copy of index in case of client corruption b/32220769
1938 const uint32_t clientIndex = mCblk->clientIndex;
1939 const uint32_t serverIndex = mCblk->serverIndex;
1940 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1941 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001942 mCblk->serverIndex = 0;
1943 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001944 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001945 }
1946 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001948 for (uint32_t index = serverIndex; index < clientIndex;) {
1949 int *p = (int *)(mBuffer + index);
1950 const int size = *p++;
1951 if (size < 0
1952 || size > EFFECT_PARAM_BUFFER_SIZE
1953 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001954 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001955 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001956 break;
1957 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001958
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001959 std::copy(reinterpret_cast<const uint8_t*>(p),
1960 reinterpret_cast<const uint8_t*>(p) + size,
1961 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001962
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001963 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001964 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001965 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001966 sizeof(int),
1967 &replyBuffer);
1968 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001969
1970 // verify shared memory: server index shouldn't change; client index can't go back.
1971 if (serverIndex != mCblk->serverIndex
1972 || clientIndex > mCblk->clientIndex) {
1973 android_errorWriteLog(0x534e4554, "32220769");
1974 status = BAD_VALUE;
1975 break;
1976 }
1977
Eric Laurentca7cc822012-11-19 14:55:58 -08001978 // stop at first error encountered
1979 if (ret != NO_ERROR) {
1980 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001981 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001982 break;
1983 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001984 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001985 break;
1986 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001987 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001988 }
1989 mCblk->serverIndex = 0;
1990 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001991 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001992 }
1993
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001994 status_t status = effect->command(cmdCode,
1995 cmdData,
1996 maxResponseSize,
1997 response);
1998 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001999}
2000
2001void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2002{
2003 ALOGV("setControl %p control %d", this, hasControl);
2004
2005 mHasControl = hasControl;
2006 mEnabled = enabled;
2007
2008 if (signal && mEffectClient != 0) {
2009 mEffectClient->controlStatusChanged(hasControl);
2010 }
2011}
2012
2013void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002014 const std::vector<uint8_t>& cmdData,
2015 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002016{
2017 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002018 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002019 }
2020}
2021
2022
2023
2024void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2025{
2026 if (mEffectClient != 0) {
2027 mEffectClient->enableStatusChanged(enabled);
2028 }
2029}
2030
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002031void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002032{
2033 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2034
Marco Nelissenb2208842014-02-07 14:00:50 -08002035 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002036 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002038 mHasControl ? "yes" : "no",
2039 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002040 mCblk ? mCblk->clientIndex : 0,
2041 mCblk ? mCblk->serverIndex : 0
2042 );
2043
2044 if (locked) {
2045 mCblk->lock.unlock();
2046 }
2047}
2048
2049#undef LOG_TAG
2050#define LOG_TAG "AudioFlinger::EffectChain"
2051
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002052AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2053 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002054 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002055 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002056 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002057 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002058{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002059 sp<ThreadBase> p = thread.promote();
2060 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 return;
2062 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002063 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002064 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2065 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002066}
2067
2068AudioFlinger::EffectChain::~EffectChain()
2069{
Eric Laurentca7cc822012-11-19 14:55:58 -08002070}
2071
2072// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2073sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2074 effect_descriptor_t *descriptor)
2075{
2076 size_t size = mEffects.size();
2077
2078 for (size_t i = 0; i < size; i++) {
2079 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2080 return mEffects[i];
2081 }
2082 }
2083 return 0;
2084}
2085
2086// getEffectFromId_l() must be called with ThreadBase::mLock held
2087sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2088{
2089 size_t size = mEffects.size();
2090
2091 for (size_t i = 0; i < size; i++) {
2092 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2093 if (id == 0 || mEffects[i]->id() == id) {
2094 return mEffects[i];
2095 }
2096 }
2097 return 0;
2098}
2099
2100// getEffectFromType_l() must be called with ThreadBase::mLock held
2101sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2102 const effect_uuid_t *type)
2103{
2104 size_t size = mEffects.size();
2105
2106 for (size_t i = 0; i < size; i++) {
2107 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2108 return mEffects[i];
2109 }
2110 }
2111 return 0;
2112}
2113
Eric Laurent6c796322019-04-09 14:13:17 -07002114std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2115{
2116 std::vector<int> ids;
2117 Mutex::Autolock _l(mLock);
2118 for (size_t i = 0; i < mEffects.size(); i++) {
2119 ids.push_back(mEffects[i]->id());
2120 }
2121 return ids;
2122}
2123
Eric Laurentca7cc822012-11-19 14:55:58 -08002124void AudioFlinger::EffectChain::clearInputBuffer()
2125{
2126 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002127 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002128}
2129
2130// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002131void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002132{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002133 if (mInBuffer == NULL) {
2134 return;
2135 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002136 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2137 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002138
Eric Laurent6b446ce2019-12-13 10:56:31 -08002139 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002140 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002141}
2142
2143// Must be called with EffectChain::mLock locked
2144void AudioFlinger::EffectChain::process_l()
2145{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002146 // never process effects when:
2147 // - on an OFFLOAD thread
2148 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002149 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002150 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002151 bool tracksOnSession = (trackCnt() != 0);
2152
2153 if (!tracksOnSession && mTailBufferCount == 0) {
2154 doProcess = false;
2155 }
2156
2157 if (activeTrackCnt() == 0) {
2158 // if no track is active and the effect tail has not been rendered,
2159 // the input buffer must be cleared here as the mixer process will not do it
2160 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002161 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002162 if (mTailBufferCount > 0) {
2163 mTailBufferCount--;
2164 }
2165 }
2166 }
2167 }
2168
2169 size_t size = mEffects.size();
2170 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002171 // Only the input and output buffers of the chain can be external,
2172 // and 'update' / 'commit' do nothing for allocated buffers, thus
2173 // it's not needed to consider any other buffers here.
2174 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002175 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2176 mOutBuffer->update();
2177 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002178 for (size_t i = 0; i < size; i++) {
2179 mEffects[i]->process();
2180 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002181 mInBuffer->commit();
2182 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2183 mOutBuffer->commit();
2184 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002185 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002186 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002187 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002188 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2189 }
2190 if (doResetVolume) {
2191 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002192 }
2193}
2194
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002195// createEffect_l() must be called with ThreadBase::mLock held
2196status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002197 effect_descriptor_t *desc,
2198 int id,
2199 audio_session_t sessionId,
2200 bool pinned)
2201{
2202 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002203 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002204 status_t lStatus = effect->status();
2205 if (lStatus == NO_ERROR) {
2206 lStatus = addEffect_ll(effect);
2207 }
2208 if (lStatus != NO_ERROR) {
2209 effect.clear();
2210 }
2211 return lStatus;
2212}
2213
2214// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002215status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2216{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002217 Mutex::Autolock _l(mLock);
2218 return addEffect_ll(effect);
2219}
2220// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2221status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2222{
Eric Laurentca7cc822012-11-19 14:55:58 -08002223 effect_descriptor_t desc = effect->desc();
2224 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2225
Eric Laurent6b446ce2019-12-13 10:56:31 -08002226 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002227
2228 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2229 // Auxiliary effects are inserted at the beginning of mEffects vector as
2230 // they are processed first and accumulated in chain input buffer
2231 mEffects.insertAt(effect, 0);
2232
2233 // the input buffer for auxiliary effect contains mono samples in
2234 // 32 bit format. This is to avoid saturation in AudoMixer
2235 // accumulation stage. Saturation is done in EffectModule::process() before
2236 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002237 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002238 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002239#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002240 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002241 numSamples * sizeof(float), &halBuffer);
2242#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002243 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002244 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002245#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002246 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002247
2248 effect->configure();
2249
Mikhail Naganov022b9952017-01-04 16:36:51 -08002250 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002251 // auxiliary effects output samples to chain input buffer for further processing
2252 // by insert effects
2253 effect->setOutBuffer(mInBuffer);
2254 } else {
2255 // Insert effects are inserted at the end of mEffects vector as they are processed
2256 // after track and auxiliary effects.
2257 // Insert effect order as a function of indicated preference:
2258 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2259 // another effect is present
2260 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2261 // last effect claiming first position
2262 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2263 // first effect claiming last position
2264 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2265 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2266 // already present
2267
2268 size_t size = mEffects.size();
2269 size_t idx_insert = size;
2270 ssize_t idx_insert_first = -1;
2271 ssize_t idx_insert_last = -1;
2272
2273 for (size_t i = 0; i < size; i++) {
2274 effect_descriptor_t d = mEffects[i]->desc();
2275 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2276 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2277 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2278 // check invalid effect chaining combinations
2279 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2280 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2281 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2282 desc.name, d.name);
2283 return INVALID_OPERATION;
2284 }
2285 // remember position of first insert effect and by default
2286 // select this as insert position for new effect
2287 if (idx_insert == size) {
2288 idx_insert = i;
2289 }
2290 // remember position of last insert effect claiming
2291 // first position
2292 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2293 idx_insert_first = i;
2294 }
2295 // remember position of first insert effect claiming
2296 // last position
2297 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2298 idx_insert_last == -1) {
2299 idx_insert_last = i;
2300 }
2301 }
2302 }
2303
2304 // modify idx_insert from first position if needed
2305 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2306 if (idx_insert_last != -1) {
2307 idx_insert = idx_insert_last;
2308 } else {
2309 idx_insert = size;
2310 }
2311 } else {
2312 if (idx_insert_first != -1) {
2313 idx_insert = idx_insert_first + 1;
2314 }
2315 }
2316
Eric Laurentf1f22e72021-07-13 14:04:14 +02002317 mEffects.insertAt(effect, idx_insert);
2318
2319 effect->configure();
2320
Eric Laurentca7cc822012-11-19 14:55:58 -08002321 // always read samples from chain input buffer
2322 effect->setInBuffer(mInBuffer);
2323
2324 // if last effect in the chain, output samples to chain
2325 // output buffer, otherwise to chain input buffer
2326 if (idx_insert == size) {
2327 if (idx_insert != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002328 mEffects[idx_insert-1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002329 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002330 }
2331 effect->setOutBuffer(mOutBuffer);
2332 } else {
2333 effect->setOutBuffer(mInBuffer);
2334 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002335
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002336 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002337 idx_insert);
2338 }
2339 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002340
Eric Laurentca7cc822012-11-19 14:55:58 -08002341 return NO_ERROR;
2342}
2343
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002344// removeEffect_l() must be called with ThreadBase::mLock held
2345size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2346 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002347{
2348 Mutex::Autolock _l(mLock);
2349 size_t size = mEffects.size();
2350 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2351
2352 for (size_t i = 0; i < size; i++) {
2353 if (effect == mEffects[i]) {
2354 // calling stop here will remove pre-processing effect from the audio HAL.
2355 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2356 // the middle of a read from audio HAL
2357 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2358 mEffects[i]->state() == EffectModule::STOPPING) {
2359 mEffects[i]->stop();
2360 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002361 if (release) {
2362 mEffects[i]->release_l();
2363 }
2364
Mikhail Naganov022b9952017-01-04 16:36:51 -08002365 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002366 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002367 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002368 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002369 }
2370 }
2371 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002372
2373 // make sure the input buffer configuration for the new first effect in the chain
2374 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2375 if (i == 0 && size > 1) {
2376 mEffects[0]->configure();
2377 mEffects[0]->setInBuffer(mInBuffer);
2378 }
2379
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002380 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 this, i);
2382 break;
2383 }
2384 }
2385
2386 return mEffects.size();
2387}
2388
jiabin8f278ee2019-11-11 12:16:27 -08002389// setDevices_l() must be called with ThreadBase::mLock held
2390void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002391{
2392 size_t size = mEffects.size();
2393 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002394 mEffects[i]->setDevices(devices);
2395 }
2396}
2397
2398// setInputDevice_l() must be called with ThreadBase::mLock held
2399void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2400{
2401 size_t size = mEffects.size();
2402 for (size_t i = 0; i < size; i++) {
2403 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002404 }
2405}
2406
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002407// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002408void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2409{
2410 size_t size = mEffects.size();
2411 for (size_t i = 0; i < size; i++) {
2412 mEffects[i]->setMode(mode);
2413 }
2414}
2415
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002416// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002417void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2418{
2419 size_t size = mEffects.size();
2420 for (size_t i = 0; i < size; i++) {
2421 mEffects[i]->setAudioSource(source);
2422 }
2423}
2424
Zhou Songd505c642020-02-20 16:35:37 +08002425bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2426 for (const auto &effect : mEffects) {
2427 if (effect->isVolumeControlEnabled()) return true;
2428 }
2429 return false;
2430}
2431
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002432// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002433bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002434{
2435 uint32_t newLeft = *left;
2436 uint32_t newRight = *right;
2437 bool hasControl = false;
2438 int ctrlIdx = -1;
2439 size_t size = mEffects.size();
2440
2441 // first update volume controller
2442 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002443 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002444 ctrlIdx = i - 1;
2445 hasControl = true;
2446 break;
2447 }
2448 }
2449
Eric Laurentfa1e1232016-08-02 19:01:49 -07002450 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002451 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002452 if (hasControl) {
2453 *left = mNewLeftVolume;
2454 *right = mNewRightVolume;
2455 }
2456 return hasControl;
2457 }
2458
2459 mVolumeCtrlIdx = ctrlIdx;
2460 mLeftVolume = newLeft;
2461 mRightVolume = newRight;
2462
2463 // second get volume update from volume controller
2464 if (ctrlIdx >= 0) {
2465 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2466 mNewLeftVolume = newLeft;
2467 mNewRightVolume = newRight;
2468 }
2469 // then indicate volume to all other effects in chain.
2470 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002471 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002472 uint32_t lVol = newLeft;
2473 uint32_t rVol = newRight;
2474
2475 for (size_t i = 0; i < size; i++) {
2476 if ((int)i == ctrlIdx) {
2477 continue;
2478 }
2479 // this also works for ctrlIdx == -1 when there is no volume controller
2480 if ((int)i > ctrlIdx) {
2481 lVol = *left;
2482 rVol = *right;
2483 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002484 // Pass requested volume directly if this is volume monitor module
2485 if (mEffects[i]->isVolumeMonitor()) {
2486 mEffects[i]->setVolume(left, right, false);
2487 } else {
2488 mEffects[i]->setVolume(&lVol, &rVol, false);
2489 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002490 }
2491 *left = newLeft;
2492 *right = newRight;
2493
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002494 setVolumeForOutput_l(*left, *right);
2495
Eric Laurentca7cc822012-11-19 14:55:58 -08002496 return hasControl;
2497}
2498
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002499// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002500void AudioFlinger::EffectChain::resetVolume_l()
2501{
Eric Laurente7449bf2016-08-03 18:44:07 -07002502 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2503 uint32_t left = mLeftVolume;
2504 uint32_t right = mRightVolume;
2505 (void)setVolume_l(&left, &right, true);
2506 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002507}
2508
jiabineb3bda02020-06-30 14:07:03 -07002509// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2510bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2511{
2512 for (size_t i = 0; i < mEffects.size(); ++i) {
2513 if (mEffects[i]->isHapticGenerator()) {
2514 return true;
2515 }
2516 }
2517 return false;
2518}
2519
jiabine70bc7f2020-06-30 22:07:55 -07002520void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2521{
2522 Mutex::Autolock _l(mLock);
2523 for (size_t i = 0; i < mEffects.size(); ++i) {
2524 mEffects[i]->setHapticIntensity(id, intensity);
2525 }
2526}
2527
Eric Laurent1b928682014-10-02 19:41:47 -07002528void AudioFlinger::EffectChain::syncHalEffectsState()
2529{
2530 Mutex::Autolock _l(mLock);
2531 for (size_t i = 0; i < mEffects.size(); i++) {
2532 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2533 mEffects[i]->state() == EffectModule::STOPPING) {
2534 mEffects[i]->addEffectToHal_l();
2535 }
2536 }
2537}
2538
Eric Laurentca7cc822012-11-19 14:55:58 -08002539void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2540{
Eric Laurentca7cc822012-11-19 14:55:58 -08002541 String8 result;
2542
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002543 const size_t numEffects = mEffects.size();
2544 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002545
Marco Nelissenb2208842014-02-07 14:00:50 -08002546 if (numEffects) {
2547 bool locked = AudioFlinger::dumpTryLock(mLock);
2548 // failed to lock - AudioFlinger is probably deadlocked
2549 if (!locked) {
2550 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002551 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002552
Andy Hungbded9c82017-11-30 18:47:35 -08002553 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2554 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2555 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2556 (int)inBufferStr.size(), "In buffer ",
2557 (int)outBufferStr.size(), "Out buffer ");
2558 result.appendFormat("\t%s %s %d\n",
2559 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002560 write(fd, result.string(), result.size());
2561
2562 for (size_t i = 0; i < numEffects; ++i) {
2563 sp<EffectModule> effect = mEffects[i];
2564 if (effect != 0) {
2565 effect->dump(fd, args);
2566 }
2567 }
2568
2569 if (locked) {
2570 mLock.unlock();
2571 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002572 } else {
2573 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002574 }
2575}
2576
2577// must be called with ThreadBase::mLock held
2578void AudioFlinger::EffectChain::setEffectSuspended_l(
2579 const effect_uuid_t *type, bool suspend)
2580{
2581 sp<SuspendedEffectDesc> desc;
2582 // use effect type UUID timelow as key as there is no real risk of identical
2583 // timeLow fields among effect type UUIDs.
2584 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2585 if (suspend) {
2586 if (index >= 0) {
2587 desc = mSuspendedEffects.valueAt(index);
2588 } else {
2589 desc = new SuspendedEffectDesc();
2590 desc->mType = *type;
2591 mSuspendedEffects.add(type->timeLow, desc);
2592 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2593 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002594
Eric Laurentca7cc822012-11-19 14:55:58 -08002595 if (desc->mRefCount++ == 0) {
2596 sp<EffectModule> effect = getEffectIfEnabled(type);
2597 if (effect != 0) {
2598 desc->mEffect = effect;
2599 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002600 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002601 }
2602 }
2603 } else {
2604 if (index < 0) {
2605 return;
2606 }
2607 desc = mSuspendedEffects.valueAt(index);
2608 if (desc->mRefCount <= 0) {
2609 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002610 desc->mRefCount = 0;
2611 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002612 }
2613 if (--desc->mRefCount == 0) {
2614 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2615 if (desc->mEffect != 0) {
2616 sp<EffectModule> effect = desc->mEffect.promote();
2617 if (effect != 0) {
2618 effect->setSuspended(false);
2619 effect->lock();
2620 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002621 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002622 effect->setEnabled_l(handle->enabled());
2623 }
2624 effect->unlock();
2625 }
2626 desc->mEffect.clear();
2627 }
2628 mSuspendedEffects.removeItemsAt(index);
2629 }
2630 }
2631}
2632
2633// must be called with ThreadBase::mLock held
2634void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2635{
2636 sp<SuspendedEffectDesc> desc;
2637
2638 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2639 if (suspend) {
2640 if (index >= 0) {
2641 desc = mSuspendedEffects.valueAt(index);
2642 } else {
2643 desc = new SuspendedEffectDesc();
2644 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2645 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2646 }
2647 if (desc->mRefCount++ == 0) {
2648 Vector< sp<EffectModule> > effects;
2649 getSuspendEligibleEffects(effects);
2650 for (size_t i = 0; i < effects.size(); i++) {
2651 setEffectSuspended_l(&effects[i]->desc().type, true);
2652 }
2653 }
2654 } else {
2655 if (index < 0) {
2656 return;
2657 }
2658 desc = mSuspendedEffects.valueAt(index);
2659 if (desc->mRefCount <= 0) {
2660 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2661 desc->mRefCount = 1;
2662 }
2663 if (--desc->mRefCount == 0) {
2664 Vector<const effect_uuid_t *> types;
2665 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2666 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2667 continue;
2668 }
2669 types.add(&mSuspendedEffects.valueAt(i)->mType);
2670 }
2671 for (size_t i = 0; i < types.size(); i++) {
2672 setEffectSuspended_l(types[i], false);
2673 }
2674 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2675 mSuspendedEffects.keyAt(index));
2676 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2677 }
2678 }
2679}
2680
2681
2682// The volume effect is used for automated tests only
2683#ifndef OPENSL_ES_H_
2684static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2685 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2686const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2687#endif //OPENSL_ES_H_
2688
Eric Laurentd8365c52017-07-16 15:27:05 -07002689/* static */
2690bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2691{
2692 // Only NS and AEC are suspended when BtNRec is off
2693 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2694 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2695 return true;
2696 }
2697 return false;
2698}
2699
Eric Laurentca7cc822012-11-19 14:55:58 -08002700bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2701{
2702 // auxiliary effects and visualizer are never suspended on output mix
2703 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2704 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2705 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002706 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2707 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002708 return false;
2709 }
2710 return true;
2711}
2712
2713void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2714 Vector< sp<AudioFlinger::EffectModule> > &effects)
2715{
2716 effects.clear();
2717 for (size_t i = 0; i < mEffects.size(); i++) {
2718 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2719 effects.add(mEffects[i]);
2720 }
2721 }
2722}
2723
2724sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2725 const effect_uuid_t *type)
2726{
2727 sp<EffectModule> effect = getEffectFromType_l(type);
2728 return effect != 0 && effect->isEnabled() ? effect : 0;
2729}
2730
2731void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2732 bool enabled)
2733{
2734 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2735 if (enabled) {
2736 if (index < 0) {
2737 // if the effect is not suspend check if all effects are suspended
2738 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2739 if (index < 0) {
2740 return;
2741 }
2742 if (!isEffectEligibleForSuspend(effect->desc())) {
2743 return;
2744 }
2745 setEffectSuspended_l(&effect->desc().type, enabled);
2746 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2747 if (index < 0) {
2748 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2749 return;
2750 }
2751 }
2752 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2753 effect->desc().type.timeLow);
2754 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002755 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002756 if (desc->mEffect == 0) {
2757 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002758 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002759 effect->setSuspended(true);
2760 }
2761 } else {
2762 if (index < 0) {
2763 return;
2764 }
2765 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2766 effect->desc().type.timeLow);
2767 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2768 desc->mEffect.clear();
2769 effect->setSuspended(false);
2770 }
2771}
2772
Eric Laurent5baf2af2013-09-12 17:37:00 -07002773bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002774{
2775 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002776 return isNonOffloadableEnabled_l();
2777}
2778
2779bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2780{
Eric Laurent813e2a72013-08-31 12:59:48 -07002781 size_t size = mEffects.size();
2782 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002783 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002784 return true;
2785 }
2786 }
2787 return false;
2788}
2789
Eric Laurentaaa44472014-09-12 17:41:50 -07002790void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2791{
2792 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002793 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002794}
2795
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002796void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2797{
2798 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2799 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2800 }
2801 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2802 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2803 }
2804}
2805
2806void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2807{
2808 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2809 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2810 }
2811 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2812 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2813 }
2814}
2815
2816bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002817{
2818 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002819 for (const auto &effect : mEffects) {
2820 if (effect->isProcessImplemented()) {
2821 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002822 }
2823 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002824 // Allow effects without processing.
2825 return true;
2826}
2827
2828bool AudioFlinger::EffectChain::isFastCompatible() const
2829{
2830 Mutex::Autolock _l(mLock);
2831 for (const auto &effect : mEffects) {
2832 if (effect->isProcessImplemented()
2833 && effect->isImplementationSoftware()) {
2834 return false;
2835 }
2836 }
2837 // Allow effects without processing or hw accelerated effects.
2838 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002839}
2840
2841// isCompatibleWithThread_l() must be called with thread->mLock held
2842bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2843{
2844 Mutex::Autolock _l(mLock);
2845 for (size_t i = 0; i < mEffects.size(); i++) {
2846 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2847 return false;
2848 }
2849 }
2850 return true;
2851}
2852
Eric Laurent6b446ce2019-12-13 10:56:31 -08002853// EffectCallbackInterface implementation
2854status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2855 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2856 sp<EffectHalInterface> *effect) {
2857 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002858 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002859 if (effectsFactory != 0) {
2860 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2861 }
2862 return status;
2863}
2864
2865bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002866 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002867 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002868 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002869}
2870
2871status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2872 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002873 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002874}
2875
2876status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2877 sp<EffectHalInterface> effect) {
2878 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002879 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002880 if (t == nullptr) {
2881 return result;
2882 }
2883 sp <StreamHalInterface> st = t->stream();
2884 if (st == nullptr) {
2885 return result;
2886 }
2887 result = st->addEffect(effect);
2888 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2889 return result;
2890}
2891
2892status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2893 sp<EffectHalInterface> effect) {
2894 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002895 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002896 if (t == nullptr) {
2897 return result;
2898 }
2899 sp <StreamHalInterface> st = t->stream();
2900 if (st == nullptr) {
2901 return result;
2902 }
2903 result = st->removeEffect(effect);
2904 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2905 return result;
2906}
2907
2908audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002909 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002910 if (t == nullptr) {
2911 return AUDIO_IO_HANDLE_NONE;
2912 }
2913 return t->id();
2914}
2915
2916bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002917 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002918 if (t == nullptr) {
2919 return true;
2920 }
2921 return t->isOutput();
2922}
2923
2924bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002925 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002926 if (t == nullptr) {
2927 return false;
2928 }
2929 return t->type() == ThreadBase::OFFLOAD;
2930}
2931
2932bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002933 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002934 if (t == nullptr) {
2935 return false;
2936 }
2937 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2938}
2939
2940bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002941 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002942 if (t == nullptr) {
2943 return false;
2944 }
Andy Hungea840382020-05-05 21:50:17 -07002945 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002946}
2947
2948uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002949 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002950 if (t == nullptr) {
2951 return 0;
2952 }
2953 return t->sampleRate();
2954}
2955
Eric Laurentf1f22e72021-07-13 14:04:14 +02002956audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
2957 sp<ThreadBase> t = thread().promote();
2958 if (t == nullptr) {
2959 return AUDIO_CHANNEL_NONE;
2960 }
2961 sp<EffectChain> c = chain().promote();
2962 if (c == nullptr) {
2963 return AUDIO_CHANNEL_NONE;
2964 }
2965
2966 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
2967 || c->isFirstEffect(id)) {
2968 return t->mixerChannelMask();
2969 } else {
2970 return t->channelMask();
2971 }
2972}
2973
2974uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
2975 sp<ThreadBase> t = thread().promote();
2976 if (t == nullptr) {
2977 return 0;
2978 }
2979 sp<EffectChain> c = chain().promote();
2980 if (c == nullptr) {
2981 return 0;
2982 }
2983
2984 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
2985 || c->isFirstEffect(id)) {
2986 return audio_channel_count_from_out_mask(t->mixerChannelMask());
2987 } else {
2988 return t->channelCount();
2989 }
2990}
2991
2992audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002993 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002994 if (t == nullptr) {
2995 return AUDIO_CHANNEL_NONE;
2996 }
2997 return t->channelMask();
2998}
2999
Eric Laurentf1f22e72021-07-13 14:04:14 +02003000uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003001 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003002 if (t == nullptr) {
3003 return 0;
3004 }
3005 return t->channelCount();
3006}
3007
jiabineb3bda02020-06-30 14:07:03 -07003008audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003009 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003010 if (t == nullptr) {
3011 return AUDIO_CHANNEL_NONE;
3012 }
3013 return t->hapticChannelMask();
3014}
3015
Eric Laurent6b446ce2019-12-13 10:56:31 -08003016size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003017 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003018 if (t == nullptr) {
3019 return 0;
3020 }
3021 return t->frameCount();
3022}
3023
3024uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003025 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003026 if (t == nullptr) {
3027 return 0;
3028 }
3029 return t->latency_l();
3030}
3031
3032void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003033 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003034 if (t == nullptr) {
3035 return;
3036 }
3037 t->setVolumeForOutput_l(left, right);
3038}
3039
3040void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003041 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003042 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003043 if (t == nullptr) {
3044 return;
3045 }
3046 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3047
Andy Hung328d6772021-01-12 12:32:21 -08003048 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (c == nullptr) {
3050 return;
3051 }
Eric Laurent41709552019-12-16 19:34:05 -08003052 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3053 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003054}
3055
Eric Laurent41709552019-12-16 19:34:05 -08003056void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003057 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003058 if (t == nullptr) {
3059 return;
3060 }
Eric Laurent41709552019-12-16 19:34:05 -08003061 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3062 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003063}
3064
Eric Laurent41709552019-12-16 19:34:05 -08003065void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003066 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3067
Andy Hung328d6772021-01-12 12:32:21 -08003068 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069 if (t == nullptr) {
3070 return;
3071 }
3072 t->onEffectDisable();
3073}
3074
3075bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3076 bool unpinIfLast) {
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 false;
3080 }
3081 t->disconnectEffectHandle(handle, unpinIfLast);
3082 return true;
3083}
3084
3085void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003086 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003087 if (c == nullptr) {
3088 return;
3089 }
3090 c->resetVolume_l();
3091
3092}
3093
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003094product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003095 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003096 if (c == nullptr) {
3097 return PRODUCT_STRATEGY_NONE;
3098 }
3099 return c->strategy();
3100}
3101
3102int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003103 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003104 if (c == nullptr) {
3105 return 0;
3106 }
3107 return c->activeTrackCnt();
3108}
3109
Eric Laurentb82e6b72019-11-22 17:25:04 -08003110
3111#undef LOG_TAG
3112#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3113
3114status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3115{
3116 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3117 Mutex::Autolock _l(mProxyLock);
3118 if (status == NO_ERROR) {
3119 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003120 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003121 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003122 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003123 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003124 bs = handle.second->disable(&status);
3125 }
3126 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003127 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003128 }
3129 }
3130 }
3131 ALOGV("%s enable %d status %d", __func__, enabled, status);
3132 return status;
3133}
3134
3135status_t AudioFlinger::DeviceEffectProxy::init(
3136 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3137//For all audio patches
3138//If src or sink device match
3139//If the effect is HW accelerated
3140// if no corresponding effect module
3141// Create EffectModule: mHalEffect
3142//Create and attach EffectHandle
3143//If the effect is not HW accelerated and the patch sink or src is a mixer port
3144// Create Effect on patch input or output thread on session -1
3145//Add EffectHandle to EffectHandle map of Effect Proxy:
3146 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3147 status_t status = NO_ERROR;
3148 for (auto &patch : patches) {
3149 status = onCreatePatch(patch.first, patch.second);
3150 ALOGV("%s onCreatePatch status %d", __func__, status);
3151 if (status == BAD_VALUE) {
3152 return status;
3153 }
3154 }
3155 return status;
3156}
3157
3158status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3159 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3160 status_t status = NAME_NOT_FOUND;
3161 sp<EffectHandle> handle;
3162 // only consider source[0] as this is the only "true" source of a patch
3163 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3164 ALOGV("%s source checkPort status %d", __func__, status);
3165 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3166 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3167 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3168 }
3169 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3170 Mutex::Autolock _l(mProxyLock);
3171 mEffectHandles.emplace(patchHandle, handle);
3172 }
3173 ALOGW_IF(status == BAD_VALUE,
3174 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3175
3176 return status;
3177}
3178
3179status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3180 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3181
3182 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3183 __func__, port->type, port->ext.device.type,
3184 port->ext.device.address, port->id, patch.isSoftware());
3185 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003186 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003187 return NAME_NOT_FOUND;
3188 }
3189 status_t status = NAME_NOT_FOUND;
3190
3191 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3192 Mutex::Autolock _l(mProxyLock);
3193 mDevicePort = *port;
3194 mHalEffect = new EffectModule(mMyCallback,
3195 const_cast<effect_descriptor_t *>(&mDescriptor),
3196 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3197 false /* pinned */, port->id);
3198 if (audio_is_input_device(mDevice.mType)) {
3199 mHalEffect->setInputDevice(mDevice);
3200 } else {
3201 mHalEffect->setDevices({mDevice});
3202 }
3203 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3204 status = (*handle)->initCheck();
3205 if (status == OK) {
3206 status = mHalEffect->addHandle((*handle).get());
3207 } else {
3208 mHalEffect.clear();
3209 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3210 }
3211 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3212 sp <ThreadBase> thread;
3213 if (audio_port_config_has_input_direction(port)) {
3214 if (patch.isSoftware()) {
3215 thread = patch.mRecord.thread();
3216 } else {
3217 thread = patch.thread().promote();
3218 }
3219 } else {
3220 if (patch.isSoftware()) {
3221 thread = patch.mPlayback.thread();
3222 } else {
3223 thread = patch.thread().promote();
3224 }
3225 }
3226 int enabled;
3227 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3228 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003229 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003230 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3231 } else {
3232 status = BAD_VALUE;
3233 }
3234 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003235 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003236 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003237 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003238 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003239 bs = (*handle)->disable(&status);
3240 }
3241 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003242 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003243 }
3244 }
3245 return status;
3246}
3247
3248void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3249 Mutex::Autolock _l(mProxyLock);
3250 mEffectHandles.erase(patchHandle);
3251}
3252
3253
3254size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3255{
3256 Mutex::Autolock _l(mProxyLock);
3257 if (effect == mHalEffect) {
3258 mHalEffect.clear();
3259 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3260 }
3261 return mHalEffect == nullptr ? 0 : 1;
3262}
3263
3264status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3265 sp<EffectHalInterface> effect) {
3266 if (mHalEffect == nullptr) {
3267 return NO_INIT;
3268 }
3269 return mManagerCallback->addEffectToHal(
3270 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3271}
3272
3273status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3274 sp<EffectHalInterface> effect) {
3275 if (mHalEffect == nullptr) {
3276 return NO_INIT;
3277 }
3278 return mManagerCallback->removeEffectFromHal(
3279 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3280}
3281
3282bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3283 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3284 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3285 }
3286 return true;
3287}
3288
3289uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3290 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3291 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3292 return mDevicePort.sample_rate;
3293 }
3294 return DEFAULT_OUTPUT_SAMPLE_RATE;
3295}
3296
3297audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3298 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3299 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3300 return mDevicePort.channel_mask;
3301 }
3302 return AUDIO_CHANNEL_OUT_STEREO;
3303}
3304
3305uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3306 if (isOutput()) {
3307 return audio_channel_count_from_out_mask(channelMask());
3308 }
3309 return audio_channel_count_from_in_mask(channelMask());
3310}
3311
3312void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3313 const Vector<String16> args;
3314 EffectBase::dump(fd, args);
3315
3316 const bool locked = dumpTryLock(mProxyLock);
3317 if (!locked) {
3318 String8 result("DeviceEffectProxy may be deadlocked\n");
3319 write(fd, result.string(), result.size());
3320 }
3321
3322 String8 outStr;
3323 if (mHalEffect != nullptr) {
3324 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3325 } else {
3326 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3327 }
3328 write(fd, outStr.string(), outStr.size());
3329 outStr.clear();
3330
3331 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3332 write(fd, outStr.string(), outStr.size());
3333 outStr.clear();
3334
3335 for (const auto& iter : mEffectHandles) {
3336 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3337 write(fd, outStr.string(), outStr.size());
3338 outStr.clear();
3339 sp<EffectBase> effect = iter.second->effect().promote();
3340 if (effect != nullptr) {
3341 effect->dump(fd, args);
3342 }
3343 }
3344
3345 if (locked) {
3346 mLock.unlock();
3347 }
3348}
3349
3350#undef LOG_TAG
3351#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3352
3353int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3354 return mManagerCallback->newEffectId();
3355}
3356
3357
3358bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3359 EffectHandle *handle, bool unpinIfLast) {
3360 sp<EffectBase> effectBase = handle->effect().promote();
3361 if (effectBase == nullptr) {
3362 return false;
3363 }
3364
3365 sp<EffectModule> effect = effectBase->asEffectModule();
3366 if (effect == nullptr) {
3367 return false;
3368 }
3369
3370 // restore suspended effects if the disconnected handle was enabled and the last one.
3371 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3372 if (remove) {
3373 sp<DeviceEffectProxy> proxy = mProxy.promote();
3374 if (proxy != nullptr) {
3375 proxy->removeEffect(effect);
3376 }
3377 if (handle->enabled()) {
3378 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3379 }
3380 }
3381 return true;
3382}
3383
3384status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3385 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3386 sp<EffectHalInterface> *effect) {
3387 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3388}
3389
3390status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3391 sp<EffectHalInterface> effect) {
3392 sp<DeviceEffectProxy> proxy = mProxy.promote();
3393 if (proxy == nullptr) {
3394 return NO_INIT;
3395 }
3396 return proxy->addEffectToHal(effect);
3397}
3398
3399status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3400 sp<EffectHalInterface> effect) {
3401 sp<DeviceEffectProxy> proxy = mProxy.promote();
3402 if (proxy == nullptr) {
3403 return NO_INIT;
3404 }
3405 return proxy->addEffectToHal(effect);
3406}
3407
3408bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3409 sp<DeviceEffectProxy> proxy = mProxy.promote();
3410 if (proxy == nullptr) {
3411 return true;
3412 }
3413 return proxy->isOutput();
3414}
3415
3416uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3417 sp<DeviceEffectProxy> proxy = mProxy.promote();
3418 if (proxy == nullptr) {
3419 return DEFAULT_OUTPUT_SAMPLE_RATE;
3420 }
3421 return proxy->sampleRate();
3422}
3423
Eric Laurentf1f22e72021-07-13 14:04:14 +02003424audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3425 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003426 sp<DeviceEffectProxy> proxy = mProxy.promote();
3427 if (proxy == nullptr) {
3428 return AUDIO_CHANNEL_OUT_STEREO;
3429 }
3430 return proxy->channelMask();
3431}
3432
Eric Laurentf1f22e72021-07-13 14:04:14 +02003433uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3434 sp<DeviceEffectProxy> proxy = mProxy.promote();
3435 if (proxy == nullptr) {
3436 return 2;
3437 }
3438 return proxy->channelCount();
3439}
3440
3441audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3442 sp<DeviceEffectProxy> proxy = mProxy.promote();
3443 if (proxy == nullptr) {
3444 return AUDIO_CHANNEL_OUT_STEREO;
3445 }
3446 return proxy->channelMask();
3447}
3448
3449uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003450 sp<DeviceEffectProxy> proxy = mProxy.promote();
3451 if (proxy == nullptr) {
3452 return 2;
3453 }
3454 return proxy->channelCount();
3455}
3456
Glenn Kasten63238ef2015-03-02 15:50:29 -08003457} // namespace android