blob: d3492d9e32dc448d70732836dcaa6534c6900bd7 [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);
245 // register effect when first handle is attached and unregister when last handle is removed
246 if (mPolicyRegistered != mHandles.size() > 0) {
247 doRegister = true;
248 mPolicyRegistered = mHandles.size() > 0;
249 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700250 const auto callback = getCallback();
251 io = callback->io();
252 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700253 }
254 }
255 // enable effect when registered according to enable state requested by controlling handle
256 if (mHandles.size() > 0) {
257 EffectHandle *handle = controlHandle_l();
258 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
259 doEnable = true;
260 mPolicyEnabled = handle->enabled();
261 }
262 }
263 registered = mPolicyRegistered;
264 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100265 // The simultaneous release of two EffectHandles with the same EffectModule
266 // may cause us to call this method at the same time.
267 // This may deadlock under some circumstances (b/180941720). Avoid this.
268 if (!doRegister && !(registered && doEnable)) {
269 return NO_ERROR;
270 }
Eric Laurent6c796322019-04-09 14:13:17 -0700271 mPolicyLock.lock();
272 }
273 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
274 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
275 if (doRegister) {
276 if (registered) {
277 status = AudioSystem::registerEffect(
278 &mDescriptor,
279 io,
280 strategy,
281 mSessionId,
282 mId);
283 } else {
284 status = AudioSystem::unregisterEffect(mId);
285 }
286 }
287 if (registered && doEnable) {
288 status = AudioSystem::setEffectEnabled(mId, enabled);
289 }
290 mPolicyLock.unlock();
291
292 return status;
293}
294
295
Eric Laurent41709552019-12-16 19:34:05 -0800296ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800297{
298 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800299 return removeHandle_l(handle);
300}
301
Eric Laurent41709552019-12-16 19:34:05 -0800302ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800303{
Eric Laurentca7cc822012-11-19 14:55:58 -0800304 size_t size = mHandles.size();
305 size_t i;
306 for (i = 0; i < size; i++) {
307 if (mHandles[i] == handle) {
308 break;
309 }
310 }
311 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800312 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
313 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800315 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800316
317 mHandles.removeAt(i);
318 // if removed from first place, move effect control from this handle to next in line
319 if (i == 0) {
320 EffectHandle *h = controlHandle_l();
321 if (h != NULL) {
322 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
323 }
324 }
325
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530326 // Prevent calls to process() and other functions on effect interface from now on.
327 // The effect engine will be released by the destructor when the last strong reference on
328 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 if (mHandles.size() == 0 && !mPinned) {
330 mState = DESTROYED;
331 }
332
333 return mHandles.size();
334}
335
336// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800337AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800338{
339 // the first valid handle in the list has control over the module
340 for (size_t i = 0; i < mHandles.size(); i++) {
341 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800342 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800343 return h;
344 }
345 }
346
347 return NULL;
348}
349
Eric Laurentf10c7092016-12-06 17:09:56 -0800350// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800351ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800352{
Andy Hungfda44002021-06-03 17:23:16 -0700353 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800354 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700355 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800356 return mHandles.size();
357 }
358
Eric Laurentf10c7092016-12-06 17:09:56 -0800359 Mutex::Autolock _l(mLock);
360 ssize_t numHandles = removeHandle_l(handle);
361 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800362 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700363 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800364 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800365 }
366 return numHandles;
367}
368
Eric Laurent41709552019-12-16 19:34:05 -0800369bool AudioFlinger::EffectBase::purgeHandles()
370{
371 bool enabled = false;
372 Mutex::Autolock _l(mLock);
373 EffectHandle *handle = controlHandle_l();
374 if (handle != NULL) {
375 enabled = handle->enabled();
376 }
377 mHandles.clear();
378 return enabled;
379}
380
381void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700382 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800383}
384
385static String8 effectFlagsToString(uint32_t flags) {
386 String8 s;
387
388 s.append("conn. mode: ");
389 switch (flags & EFFECT_FLAG_TYPE_MASK) {
390 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
391 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
392 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
393 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
394 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
395 default: s.append("unknown/reserved"); break;
396 }
397 s.append(", ");
398
399 s.append("insert pref: ");
400 switch (flags & EFFECT_FLAG_INSERT_MASK) {
401 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
402 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
403 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
404 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("volume mgmt: ");
410 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
411 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
412 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
413 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
414 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
420 if (devind) {
421 s.append("device indication: ");
422 switch (devind) {
423 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
424 default: s.append("unknown/reserved"); break;
425 }
426 s.append(", ");
427 }
428
429 s.append("input mode: ");
430 switch (flags & EFFECT_FLAG_INPUT_MASK) {
431 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
432 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
433 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
434 default: s.append("not set"); break;
435 }
436 s.append(", ");
437
438 s.append("output mode: ");
439 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
440 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
441 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
442 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
443 default: s.append("not set"); break;
444 }
445 s.append(", ");
446
447 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
448 if (accel) {
449 s.append("hardware acceleration: ");
450 switch (accel) {
451 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
452 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
453 default: s.append("unknown/reserved"); break;
454 }
455 s.append(", ");
456 }
457
458 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
459 if (modeind) {
460 s.append("mode indication: ");
461 switch (modeind) {
462 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
469 if (srcind) {
470 s.append("source indication: ");
471 switch (srcind) {
472 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
479 s.append("offloadable, ");
480 }
481
482 int len = s.length();
483 if (s.length() > 2) {
484 (void) s.lockBuffer(len);
485 s.unlockBuffer(len - 2);
486 }
487 return s;
488}
489
490void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
491{
492 String8 result;
493
494 result.appendFormat("\tEffect ID %d:\n", mId);
495
496 bool locked = AudioFlinger::dumpTryLock(mLock);
497 // failed to lock - AudioFlinger is probably deadlocked
498 if (!locked) {
499 result.append("\t\tCould not lock Fx mutex:\n");
500 }
501
502 result.append("\t\tSession State Registered Enabled Suspended:\n");
503 result.appendFormat("\t\t%05d %03d %s %s %s\n",
504 mSessionId, mState, mPolicyRegistered ? "y" : "n",
505 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
506
507 result.append("\t\tDescriptor:\n");
508 char uuidStr[64];
509 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
510 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
511 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
512 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
513 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
514 mDescriptor.apiVersion,
515 mDescriptor.flags,
516 effectFlagsToString(mDescriptor.flags).string());
517 result.appendFormat("\t\t- name: %s\n",
518 mDescriptor.name);
519
520 result.appendFormat("\t\t- implementor: %s\n",
521 mDescriptor.implementor);
522
523 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
524 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
525 char buffer[256];
526 for (size_t i = 0; i < mHandles.size(); ++i) {
527 EffectHandle *handle = mHandles[i];
528 if (handle != NULL && !handle->disconnected()) {
529 handle->dumpToBuffer(buffer, sizeof(buffer));
530 result.append(buffer);
531 }
532 }
533 if (locked) {
534 mLock.unlock();
535 }
536
537 write(fd, result.string(), result.length());
538}
539
540// ----------------------------------------------------------------------------
541// EffectModule implementation
542// ----------------------------------------------------------------------------
543
544#undef LOG_TAG
545#define LOG_TAG "AudioFlinger::EffectModule"
546
547AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
548 effect_descriptor_t *desc,
549 int id,
550 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800551 bool pinned,
552 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800553 : EffectBase(callback, desc, id, sessionId, pinned),
554 // clear mConfig to ensure consistent initial value of buffer framecount
555 // in case buffers are associated by setInBuffer() or setOutBuffer()
556 // prior to configure().
557 mConfig{{}, {}},
558 mStatus(NO_INIT),
559 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
560 mDisableWaitCnt(0), // set by process() and updateState()
561 mOffloaded(false)
562#ifdef FLOAT_EFFECT_CHAIN
563 , mSupportsFloat(false)
564#endif
565{
566 ALOGV("Constructor %p pinned %d", this, pinned);
567 int lStatus;
568
569 // create effect engine from effect factory
570 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800571 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800572 if (mStatus != NO_ERROR) {
573 return;
574 }
575 lStatus = init();
576 if (lStatus < 0) {
577 mStatus = lStatus;
578 goto Error;
579 }
580
581 setOffloaded(callback->isOffload(), callback->io());
582 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
583
584 return;
585Error:
586 mEffectInterface.clear();
587 ALOGV("Constructor Error %d", mStatus);
588}
589
590AudioFlinger::EffectModule::~EffectModule()
591{
592 ALOGV("Destructor %p", this);
593 if (mEffectInterface != 0) {
594 char uuidStr[64];
595 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
596 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
597 this, uuidStr);
598 release_l();
599 }
600
601}
602
Eric Laurentfa1e1232016-08-02 19:01:49 -0700603bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800604 Mutex::Autolock _l(mLock);
605
Eric Laurentfa1e1232016-08-02 19:01:49 -0700606 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800607 switch (mState) {
608 case RESTART:
609 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700610 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800611
612 case STARTING:
613 // clear auxiliary effect input buffer for next accumulation
614 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
615 memset(mConfig.inputCfg.buffer.raw,
616 0,
617 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
618 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700619 if (start_l() == NO_ERROR) {
620 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700621 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700622 } else {
623 mState = IDLE;
624 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800625 break;
626 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900627 // volume control for offload and direct threads must take effect immediately.
628 if (stop_l() == NO_ERROR
629 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700630 mDisableWaitCnt = mMaxDisableWaitCnt;
631 } else {
632 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
633 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800634 mState = STOPPED;
635 break;
636 case STOPPED:
637 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
638 // turn off sequence.
639 if (--mDisableWaitCnt == 0) {
640 reset_l();
641 mState = IDLE;
642 }
643 break;
644 default: //IDLE , ACTIVE, DESTROYED
645 break;
646 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700647
648 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800649}
650
651void AudioFlinger::EffectModule::process()
652{
653 Mutex::Autolock _l(mLock);
654
Mikhail Naganov022b9952017-01-04 16:36:51 -0800655 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800656 return;
657 }
658
rago94a1ee82017-07-21 15:11:02 -0700659 const uint32_t inChannelCount =
660 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
661 const uint32_t outChannelCount =
662 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
663 const bool auxType =
664 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
665
Andy Hungfa69ca32017-11-30 10:07:53 -0800666 // safeInputOutputSampleCount is 0 if the channel count between input and output
667 // buffers do not match. This prevents automatic accumulation or copying between the
668 // input and output effect buffers without an intermediary effect process.
669 // TODO: consider implementing channel conversion.
670 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700671 mInChannelCountRequested != mOutChannelCountRequested ? 0
672 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800673 mConfig.inputCfg.buffer.frameCount,
674 mConfig.outputCfg.buffer.frameCount);
675 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
676#ifdef FLOAT_EFFECT_CHAIN
677 accumulate_float(
678 mConfig.outputCfg.buffer.f32,
679 mConfig.inputCfg.buffer.f32,
680 safeInputOutputSampleCount);
681#else
682 accumulate_i16(
683 mConfig.outputCfg.buffer.s16,
684 mConfig.inputCfg.buffer.s16,
685 safeInputOutputSampleCount);
686#endif
687 };
688 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
689#ifdef FLOAT_EFFECT_CHAIN
690 memcpy(
691 mConfig.outputCfg.buffer.f32,
692 mConfig.inputCfg.buffer.f32,
693 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
694
695#else
696 memcpy(
697 mConfig.outputCfg.buffer.s16,
698 mConfig.inputCfg.buffer.s16,
699 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
700#endif
701 };
702
Eric Laurentca7cc822012-11-19 14:55:58 -0800703 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700704 int ret;
705 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700706 if (auxType) {
707 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800708 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700709#ifdef FLOAT_EFFECT_CHAIN
710 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800711#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700712 // Do in-place float conversion for auxiliary effect input buffer.
713 static_assert(sizeof(float) <= sizeof(int32_t),
714 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
715
Andy Hungfa69ca32017-11-30 10:07:53 -0800716 memcpy_to_float_from_q4_27(
717 mConfig.inputCfg.buffer.f32,
718 mConfig.inputCfg.buffer.s32,
719 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800720#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800721 } else
Andy Hung116a4982017-11-30 10:15:08 -0800722#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800723 {
Andy Hung116a4982017-11-30 10:15:08 -0800724#ifdef FLOAT_AUX
725 memcpy_to_i16_from_float(
726 mConfig.inputCfg.buffer.s16,
727 mConfig.inputCfg.buffer.f32,
728 mConfig.inputCfg.buffer.frameCount);
729#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800730 memcpy_to_i16_from_q4_27(
731 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700732 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800733 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800734#endif
rago94a1ee82017-07-21 15:11:02 -0700735 }
rago94a1ee82017-07-21 15:11:02 -0700736 }
737#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800738 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
739 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
740
741 if (!auxType && mInChannelCountRequested != inChannelCount) {
742 adjust_channels(
743 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
744 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
745 sizeof(float),
746 sizeof(float)
747 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
748 inBuffer = mInConversionBuffer;
749 }
750 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
751 && mOutChannelCountRequested != outChannelCount) {
752 adjust_selected_channels(
753 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
754 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
755 sizeof(float),
756 sizeof(float)
757 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
758 outBuffer = mOutConversionBuffer;
759 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800760 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
761 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800762 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800763 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
764 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700765 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800766 memcpy_to_i16_from_float(
767 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800768 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800769 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800770 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700771 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800773 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
775 goto data_bypass;
776 }
777 memcpy_to_i16_from_float(
778 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800779 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800781 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700782 }
783 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800784#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800785 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800786#ifdef FLOAT_EFFECT_CHAIN
787 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 sp<EffectBufferHalInterface> target =
789 mOutChannelCountRequested != outChannelCount
790 ? mOutConversionBuffer : mOutBuffer;
791
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800793 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 mOutConversionBuffer->audioBuffer()->s16,
795 outChannelCount * mConfig.outputCfg.buffer.frameCount);
796 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800797 if (mOutChannelCountRequested != outChannelCount) {
798 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
799 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
800 sizeof(float),
801 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
802 }
rago94a1ee82017-07-21 15:11:02 -0700803#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700804 } else {
rago94a1ee82017-07-21 15:11:02 -0700805#ifdef FLOAT_EFFECT_CHAIN
806 data_bypass:
807#endif
808 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800809 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700810 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700812 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800813 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700814 }
815 }
816 ret = -ENODATA;
817 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800818
Eric Laurentca7cc822012-11-19 14:55:58 -0800819 // force transition to IDLE state when engine is ready
820 if (mState == STOPPED && ret == -ENODATA) {
821 mDisableWaitCnt = 1;
822 }
823
824 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700825 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800826#ifdef FLOAT_AUX
827 const size_t size =
828 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
829#else
rago94a1ee82017-07-21 15:11:02 -0700830 const size_t size =
831 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800832#endif
rago94a1ee82017-07-21 15:11:02 -0700833 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800834 }
835 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700836 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
838 // If an insert effect is idle and input buffer is different from output buffer,
839 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700840 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700841 // similar handling with data_bypass above.
842 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
843 accumulateInputToOutput();
844 } else { // EFFECT_BUFFER_ACCESS_WRITE
845 copyInputToOutput();
846 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848 }
849}
850
851void AudioFlinger::EffectModule::reset_l()
852{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700853 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800854 return;
855 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700856 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800857}
858
859status_t AudioFlinger::EffectModule::configure()
860{
rago94a1ee82017-07-21 15:11:02 -0700861 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700862 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700863 uint32_t size;
864 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700865 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700866
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700867 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700868 status = NO_INIT;
869 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 }
871
Eric Laurentca7cc822012-11-19 14:55:58 -0800872 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800873 // TODO: handle configuration of input (record) SW effects above the HAL,
874 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
875 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700876 callback = getCallback();
877 channelMask = callback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800878 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700879 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800880
881 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800882 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
883 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
884 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
885 mConfig.inputCfg.channels);
886 }
887#ifndef MULTICHANNEL_EFFECT_CHAIN
888 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
889 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
890 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
891 mConfig.outputCfg.channels);
892 }
893#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800895#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700896 // TODO: Update this logic when multichannel effects are implemented.
897 // For offloaded tracks consider mono output as stereo for proper effect initialization
898 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
899 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
900 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
901 ALOGV("Overriding effect input and output as STEREO");
902 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800903#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800904 }
jiabineb3bda02020-06-30 14:07:03 -0700905 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700906 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700907 mConfig.inputCfg.channels |= hapticChannelMask;
908 mConfig.outputCfg.channels |= hapticChannelMask;
909 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800910 mInChannelCountRequested =
911 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
912 mOutChannelCountRequested =
913 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700914
rago94a1ee82017-07-21 15:11:02 -0700915 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
916 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900917
918 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700919 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900920 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
921 ALOGV("Overriding effect input as 48kHz");
922 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700923 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900924 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
926 mConfig.inputCfg.bufferProvider.cookie = NULL;
927 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
928 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
929 mConfig.outputCfg.bufferProvider.cookie = NULL;
930 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
931 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
932 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
933 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800934 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800935 // always overwrites output buffer: input buffer == output buffer
936 // - in other sessions:
937 // last effect in the chain accumulates in output buffer: input buffer != output buffer
938 // other effect: overwrites output buffer: input buffer == output buffer
939 // Auxiliary effect:
940 // accumulates in output buffer: input buffer != output buffer
941 // Therefore: accumulate <=> input buffer != output buffer
942 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
943 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
944 } else {
945 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
946 }
947 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
948 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700949 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800950 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
951
Eric Laurent6b446ce2019-12-13 10:56:31 -0800952 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700953 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800954 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800955
956 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700957 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700958 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800959 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700960 &mConfig,
961 &size,
962 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700963 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 status = cmdStatus;
965 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800966
967#ifdef MULTICHANNEL_EFFECT_CHAIN
968 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700969 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800970 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
971 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
972 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700973 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
974 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800975 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
976 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
977 }
978 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
979 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
980 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
981 }
982 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700983 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800984 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700985 &mConfig,
986 &size,
987 &cmdStatus);
988 if (status == NO_ERROR) {
989 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800990 }
991 }
992#endif
993
994#ifdef FLOAT_EFFECT_CHAIN
995 if (status == NO_ERROR) {
996 mSupportsFloat = true;
997 }
998
999 if (status != NO_ERROR) {
1000 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1001 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1002 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1003 size = sizeof(int);
1004 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1005 sizeof(mConfig),
1006 &mConfig,
1007 &size,
1008 &cmdStatus);
1009 if (status == NO_ERROR) {
1010 status = cmdStatus;
1011 }
1012 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001013 mSupportsFloat = false;
1014 ALOGVV("config worked with 16 bit");
1015 } else {
1016 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 }
rago94a1ee82017-07-21 15:11:02 -07001018 }
1019#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001020
rago94a1ee82017-07-21 15:11:02 -07001021 if (status == NO_ERROR) {
1022 // Establish Buffer strategy
1023 setInBuffer(mInBuffer);
1024 setOutBuffer(mOutBuffer);
1025
1026 // Update visualizer latency
1027 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1028 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1029 effect_param_t *p = (effect_param_t *)buf32;
1030
1031 p->psize = sizeof(uint32_t);
1032 p->vsize = sizeof(uint32_t);
1033 size = sizeof(int);
1034 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1035
Andy Hungfda44002021-06-03 17:23:16 -07001036 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001037
1038 *((int32_t *)p->data + 1)= latency;
1039 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1040 sizeof(effect_param_t) + 8,
1041 &buf32,
1042 &size,
1043 &cmdStatus);
1044 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 }
1046
Andy Hung05083ac2017-12-14 15:00:28 -08001047 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1048 mMaxDisableWaitCnt = (uint32_t)std::max(
1049 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1050 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1051 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001052
Eric Laurentd0ebb532013-04-02 16:41:41 -07001053exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001054 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001055 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001056 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001057 return status;
1058}
1059
1060status_t AudioFlinger::EffectModule::init()
1061{
1062 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001063 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return NO_INIT;
1065 }
1066 status_t cmdStatus;
1067 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001068 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1069 0,
1070 NULL,
1071 &size,
1072 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001073 if (status == 0) {
1074 status = cmdStatus;
1075 }
1076 return status;
1077}
1078
Eric Laurent1b928682014-10-02 19:41:47 -07001079void AudioFlinger::EffectModule::addEffectToHal_l()
1080{
1081 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1082 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Andy Hungfda44002021-06-03 17:23:16 -07001083 (void)getCallback()->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001084 }
1085}
1086
Eric Laurentfa1e1232016-08-02 19:01:49 -07001087// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001088status_t AudioFlinger::EffectModule::start()
1089{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001090 status_t status;
1091 {
1092 Mutex::Autolock _l(mLock);
1093 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001094 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001095 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001096 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001097 }
1098 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001099}
1100
1101status_t AudioFlinger::EffectModule::start_l()
1102{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001103 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001104 return NO_INIT;
1105 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001106 if (mStatus != NO_ERROR) {
1107 return mStatus;
1108 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001109 status_t cmdStatus;
1110 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001111 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1112 0,
1113 NULL,
1114 &size,
1115 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 if (status == 0) {
1117 status = cmdStatus;
1118 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001119 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001120 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 }
1122 return status;
1123}
1124
1125status_t AudioFlinger::EffectModule::stop()
1126{
1127 Mutex::Autolock _l(mLock);
1128 return stop_l();
1129}
1130
1131status_t AudioFlinger::EffectModule::stop_l()
1132{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001133 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 return NO_INIT;
1135 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001136 if (mStatus != NO_ERROR) {
1137 return mStatus;
1138 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001139 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001141
1142 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001143 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1144 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1145 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001146 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001147 mSetVolumeReentrantTid = INVALID_PID;
1148 }
1149
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001150 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1151 0,
1152 NULL,
1153 &size,
1154 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001155 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001156 status = cmdStatus;
1157 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001158 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001159 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001160 }
1161 return status;
1162}
1163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001164// must be called with EffectChain::mLock held
1165void AudioFlinger::EffectModule::release_l()
1166{
1167 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001168 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001169 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001170 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001171 mEffectInterface.clear();
1172 }
1173}
1174
Eric Laurent6b446ce2019-12-13 10:56:31 -08001175status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176{
1177 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1178 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Andy Hungfda44002021-06-03 17:23:16 -07001179 getCallback()->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001181 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001182}
1183
Andy Hunge4a1d912016-08-17 14:11:13 -07001184// round up delta valid if value and divisor are positive.
1185template <typename T>
1186static T roundUpDelta(const T &value, const T &divisor) {
1187 T remainder = value % divisor;
1188 return remainder == 0 ? 0 : divisor - remainder;
1189}
1190
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001191status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1192 const std::vector<uint8_t>& cmdData,
1193 int32_t maxReplySize,
1194 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001195{
1196 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001197 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001198
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001199 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001200 return NO_INIT;
1201 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001202 if (mStatus != NO_ERROR) {
1203 return mStatus;
1204 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001205 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1206 return -EINVAL;
1207 }
1208 size_t cmdSize = cmdData.size();
1209 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1210 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1211 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001212 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001213 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001214 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001215 android_errorWriteLog(0x534e4554, "33003822");
1216 return -EINVAL;
1217 }
1218 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001219 (maxReplySize < sizeof(effect_param_t) ||
1220 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001221 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001222 return -EINVAL;
1223 }
ragoe2759072016-11-22 18:02:48 -08001224 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001225 (sizeof(effect_param_t) > maxReplySize
1226 || param->psize > maxReplySize - sizeof(effect_param_t)
1227 || param->vsize > maxReplySize - sizeof(effect_param_t)
1228 - param->psize
1229 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1230 maxReplySize
1231 - sizeof(effect_param_t)
1232 - param->psize
1233 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001234 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1235 android_errorWriteLog(0x534e4554, "32705438");
1236 return -EINVAL;
1237 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001238 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1240 && // DEFERRED not generally used
1241 (param == nullptr
1242 || param->psize > cmdSize - sizeof(effect_param_t)
1243 || param->vsize > cmdSize - sizeof(effect_param_t)
1244 - param->psize
1245 || roundUpDelta(param->psize,
1246 (uint32_t) sizeof(int)) >
1247 cmdSize
1248 - sizeof(effect_param_t)
1249 - param->psize
1250 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001251 android_errorWriteLog(0x534e4554, "30204301");
1252 return -EINVAL;
1253 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 uint32_t replySize = maxReplySize;
1255 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001256 status_t status = mEffectInterface->command(cmdCode,
1257 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001258 const_cast<uint8_t*>(cmdData.data()),
1259 &replySize,
1260 reply->data());
1261 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001262 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 for (size_t i = 1; i < mHandles.size(); i++) {
1264 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001265 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001266 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001267 }
1268 }
1269 }
1270 return status;
1271}
1272
Eric Laurentca7cc822012-11-19 14:55:58 -08001273bool AudioFlinger::EffectModule::isProcessEnabled() const
1274{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001275 if (mStatus != NO_ERROR) {
1276 return false;
1277 }
1278
Eric Laurentca7cc822012-11-19 14:55:58 -08001279 switch (mState) {
1280 case RESTART:
1281 case ACTIVE:
1282 case STOPPING:
1283 case STOPPED:
1284 return true;
1285 case IDLE:
1286 case STARTING:
1287 case DESTROYED:
1288 default:
1289 return false;
1290 }
1291}
1292
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001293bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1294{
Andy Hungfda44002021-06-03 17:23:16 -07001295 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001296}
1297
1298bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1299{
1300 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1301}
1302
Mikhail Naganov022b9952017-01-04 16:36:51 -08001303void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001304 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001305
1306 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001307 if (buffer != 0) {
1308 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1309 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1310 } else {
1311 mConfig.inputCfg.buffer.raw = NULL;
1312 }
1313 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001314 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001315
1316#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001317 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001318 // Theoretically insert effects can also do in-place conversions (destroying
1319 // the original buffer) when the output buffer is identical to the input buffer,
1320 // but we don't optimize for it here.
1321 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001322 const uint32_t inChannelCount =
1323 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1324 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001325 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001326 // we need to translate - create hidl shared buffer and intercept
1327 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001328 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1329 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1330 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001331
1332 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1333 __func__, inChannels, inFrameCount, size);
1334
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001335 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001336 || size > mInConversionBuffer->getSize())) {
1337 mInConversionBuffer.clear();
1338 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001339 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001340 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001341 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001342 mInConversionBuffer->setFrameCount(inFrameCount);
1343 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001344 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001345 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001346 }
1347 }
1348#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001349}
1350
1351void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001352 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001353
1354 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001355 if (buffer != 0) {
1356 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1357 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1358 } else {
1359 mConfig.outputCfg.buffer.raw = NULL;
1360 }
1361 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001362 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001363
1364#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001365 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001366 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001367 const uint32_t outChannelCount =
1368 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1369 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001370 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001371 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001372 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1373 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1374 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001375
1376 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1377 __func__, outChannels, outFrameCount, size);
1378
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001379 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001380 || size > mOutConversionBuffer->getSize())) {
1381 mOutConversionBuffer.clear();
1382 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001383 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001384 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001385 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001386 mOutConversionBuffer->setFrameCount(outFrameCount);
1387 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001388 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001389 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001390 }
1391 }
1392#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001393}
1394
Eric Laurentca7cc822012-11-19 14:55:58 -08001395status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1396{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001397 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001398 if (mStatus != NO_ERROR) {
1399 return mStatus;
1400 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001401 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1403 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1404 if (isProcessEnabled() &&
1405 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001406 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1407 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 uint32_t volume[2];
1409 uint32_t *pVolume = NULL;
1410 uint32_t size = sizeof(volume);
1411 volume[0] = *left;
1412 volume[1] = *right;
1413 if (controller) {
1414 pVolume = volume;
1415 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001416 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1417 size,
1418 volume,
1419 &size,
1420 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1422 *left = volume[0];
1423 *right = volume[1];
1424 }
1425 }
1426 return status;
1427}
1428
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001429void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1430{
Zhou Songd505c642020-02-20 16:35:37 +08001431 // for offload or direct thread, if the effect chain has non-offloadable
1432 // effect and any effect module within the chain has volume control, then
1433 // volume control is delegated to effect, otherwise, set volume to hal.
1434 if (mEffectCallback->isOffloadOrDirect() &&
1435 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001436 float vol_l = (float)left / (1 << 24);
1437 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001438 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001439 }
1440}
1441
jiabin8f278ee2019-11-11 12:16:27 -08001442status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1443 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001444{
jiabin8f278ee2019-11-11 12:16:27 -08001445 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1446 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 return NO_ERROR;
1448 }
1449
1450 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001451 if (mStatus != NO_ERROR) {
1452 return mStatus;
1453 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001454 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001455 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 status_t cmdStatus;
1457 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001458 // FIXME: use audio device types and addresses when the hal interface is ready.
1459 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001460 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001461 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001462 &size,
1463 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 }
1465 return status;
1466}
1467
jiabin8f278ee2019-11-11 12:16:27 -08001468status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1469{
1470 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1471}
1472
1473status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1474{
1475 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1476}
1477
Eric Laurentca7cc822012-11-19 14:55:58 -08001478status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1479{
1480 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001481 if (mStatus != NO_ERROR) {
1482 return mStatus;
1483 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 status_t status = NO_ERROR;
1485 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1486 status_t cmdStatus;
1487 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001488 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1489 sizeof(audio_mode_t),
1490 &mode,
1491 &size,
1492 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 if (status == NO_ERROR) {
1494 status = cmdStatus;
1495 }
1496 }
1497 return status;
1498}
1499
1500status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1501{
1502 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001503 if (mStatus != NO_ERROR) {
1504 return mStatus;
1505 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 status_t status = NO_ERROR;
1507 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1508 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001509 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1510 sizeof(audio_source_t),
1511 &source,
1512 &size,
1513 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001514 }
1515 return status;
1516}
1517
Eric Laurent5baf2af2013-09-12 17:37:00 -07001518status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1519{
1520 Mutex::Autolock _l(mLock);
1521 if (mStatus != NO_ERROR) {
1522 return mStatus;
1523 }
1524 status_t status = NO_ERROR;
1525 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1526 status_t cmdStatus;
1527 uint32_t size = sizeof(status_t);
1528 effect_offload_param_t cmd;
1529
1530 cmd.isOffload = offloaded;
1531 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001532 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1533 sizeof(effect_offload_param_t),
1534 &cmd,
1535 &size,
1536 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001537 if (status == NO_ERROR) {
1538 status = cmdStatus;
1539 }
1540 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1541 } else {
1542 if (offloaded) {
1543 status = INVALID_OPERATION;
1544 }
1545 mOffloaded = false;
1546 }
1547 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1548 return status;
1549}
1550
1551bool AudioFlinger::EffectModule::isOffloaded() const
1552{
1553 Mutex::Autolock _l(mLock);
1554 return mOffloaded;
1555}
1556
jiabineb3bda02020-06-30 14:07:03 -07001557/*static*/
1558bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1559 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1560}
1561
1562bool AudioFlinger::EffectModule::isHapticGenerator() const {
1563 return isHapticGenerator(&mDescriptor.type);
1564}
1565
jiabine70bc7f2020-06-30 22:07:55 -07001566status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1567{
1568 if (mStatus != NO_ERROR) {
1569 return mStatus;
1570 }
1571 if (!isHapticGenerator()) {
1572 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1573 return INVALID_OPERATION;
1574 }
1575
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001576 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1577 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001578 param->psize = sizeof(int32_t);
1579 param->vsize = sizeof(int32_t) * 2;
1580 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1581 *((int32_t*)param->data + 1) = id;
1582 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001583 std::vector<uint8_t> response;
1584 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001585 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001586 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1587 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001588 }
1589 return status;
1590}
1591
jiabin1319f5a2021-03-30 22:21:24 +00001592status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo* vibratorInfo)
1593{
1594 if (mStatus != NO_ERROR) {
1595 return mStatus;
1596 }
1597 if (!isHapticGenerator()) {
1598 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1599 return INVALID_OPERATION;
1600 }
1601
1602 std::vector<uint8_t> request(
1603 sizeof(effect_param_t) + sizeof(int32_t) + 2 * sizeof(float));
1604 effect_param_t *param = (effect_param_t*) request.data();
1605 param->psize = sizeof(int32_t);
1606 param->vsize = 2 * sizeof(float);
1607 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1608 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
1609 vibratorInfoPtr[0] = vibratorInfo->resonantFrequency;
1610 vibratorInfoPtr[1] = vibratorInfo->qFactor;
1611 std::vector<uint8_t> response;
1612 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1613 if (status == NO_ERROR) {
1614 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1615 status = *reinterpret_cast<const status_t*>(response.data());
1616 }
1617 return status;
1618}
1619
Andy Hungbded9c82017-11-30 18:47:35 -08001620static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1621 std::stringstream ss;
1622
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001623 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001624 return "nullptr"; // make different than below
1625 } else if (buffer->externalData() != nullptr) {
1626 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1627 << " -> "
1628 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1629 } else {
1630 ss << buffer->audioBuffer()->raw;
1631 }
1632 return ss.str();
1633}
Marco Nelissenb2208842014-02-07 14:00:50 -08001634
Eric Laurent41709552019-12-16 19:34:05 -08001635void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001636{
Eric Laurent41709552019-12-16 19:34:05 -08001637 EffectBase::dump(fd, args);
1638
Eric Laurentca7cc822012-11-19 14:55:58 -08001639 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001640 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001641
Eric Laurent41709552019-12-16 19:34:05 -08001642 result.append("\t\tStatus Engine:\n");
1643 result.appendFormat("\t\t%03d %p\n",
1644 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001645
1646 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001647
1648 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001649 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1650 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1651 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 mConfig.inputCfg.buffer.frameCount,
1653 mConfig.inputCfg.samplingRate,
1654 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001655 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001656 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001657
1658 result.append("\t\t- Output configuration:\n");
1659 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001660 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001661 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 mConfig.outputCfg.buffer.frameCount,
1663 mConfig.outputCfg.samplingRate,
1664 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001665 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001666 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001667
rago94a1ee82017-07-21 15:11:02 -07001668#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001669
Andy Hungbded9c82017-11-30 18:47:35 -08001670 result.appendFormat("\t\t- HAL buffers:\n"
1671 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1672 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1673 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1674 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1675 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001676#endif
1677
Eric Laurentca7cc822012-11-19 14:55:58 -08001678 write(fd, result.string(), result.length());
1679
Mikhail Naganov4d547672019-02-22 14:19:19 -08001680 if (mEffectInterface != 0) {
1681 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1682 (void)mEffectInterface->dump(fd);
1683 }
1684
Eric Laurentca7cc822012-11-19 14:55:58 -08001685 if (locked) {
1686 mLock.unlock();
1687 }
1688}
1689
1690// ----------------------------------------------------------------------------
1691// EffectHandle implementation
1692// ----------------------------------------------------------------------------
1693
1694#undef LOG_TAG
1695#define LOG_TAG "AudioFlinger::EffectHandle"
1696
Eric Laurent41709552019-12-16 19:34:05 -08001697AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001698 const sp<AudioFlinger::Client>& client,
1699 const sp<media::IEffectClient>& effectClient,
1700 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 : BnEffect(),
1702 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001703 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001704{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001705 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001706
1707 if (client == 0) {
1708 return;
1709 }
1710 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1711 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001712 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001713 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001714 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001715 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001716 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 return;
1718 }
Glenn Kastene75da402013-11-20 13:54:52 -08001719 new(mCblk) effect_param_cblk_t();
1720 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001721}
1722
1723AudioFlinger::EffectHandle::~EffectHandle()
1724{
1725 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 disconnect(false);
1727}
1728
Glenn Kastene75da402013-11-20 13:54:52 -08001729status_t AudioFlinger::EffectHandle::initCheck()
1730{
1731 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1732}
1733
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001734#define RETURN(code) \
1735 *_aidl_return = (code); \
1736 return Status::ok();
1737
1738Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001739{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001740 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001741 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001742 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001743 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001744 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001745 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001746 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001747 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001749
1750 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001751 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 }
1753
1754 mEnabled = true;
1755
Eric Laurent6c796322019-04-09 14:13:17 -07001756 status_t status = effect->updatePolicyState();
1757 if (status != NO_ERROR) {
1758 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001759 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001760 }
1761
Eric Laurent6b446ce2019-12-13 10:56:31 -08001762 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001763
1764 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001765 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001766 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 }
1768
Eric Laurent6b446ce2019-12-13 10:56:31 -08001769 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001770 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 mEnabled = false;
1772 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001773 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001774}
1775
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001776Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001777{
1778 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001779 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001780 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001781 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001782 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001783 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001784 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001785 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001787
1788 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001789 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 }
1791 mEnabled = false;
1792
Eric Laurent6c796322019-04-09 14:13:17 -07001793 effect->updatePolicyState();
1794
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001795 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001796 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 }
1798
Eric Laurent6b446ce2019-12-13 10:56:31 -08001799 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001800 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001801}
1802
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001803Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001804{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001805 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001807 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001808}
1809
1810void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1811{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001812 AutoMutex _l(mLock);
1813 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1814 if (mDisconnected) {
1815 if (unpinIfLast) {
1816 android_errorWriteLog(0x534e4554, "32707507");
1817 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 return;
1819 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001820 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001821 {
Eric Laurent41709552019-12-16 19:34:05 -08001822 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001823 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001824 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001825 ALOGW("%s Effect handle %p disconnected after thread destruction",
1826 __func__, this);
1827 }
1828 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001829 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001830 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001831
Eric Laurentca7cc822012-11-19 14:55:58 -08001832 if (mClient != 0) {
1833 if (mCblk != NULL) {
1834 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1835 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1836 }
1837 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001838 // Client destructor must run with AudioFlinger client mutex locked
1839 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 mClient.clear();
1841 }
1842}
1843
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001844Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1845 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1846 return Status::ok();
1847}
1848
1849Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1850 const std::vector<uint8_t>& cmdData,
1851 int32_t maxResponseSize,
1852 std::vector<uint8_t>* response,
1853 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001854{
1855 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001856 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001857
Eric Laurentc7ab3092017-06-15 18:43:46 -07001858 // reject commands reserved for internal use by audio framework if coming from outside
1859 // of audioserver
1860 switch(cmdCode) {
1861 case EFFECT_CMD_ENABLE:
1862 case EFFECT_CMD_DISABLE:
1863 case EFFECT_CMD_SET_PARAM:
1864 case EFFECT_CMD_SET_PARAM_DEFERRED:
1865 case EFFECT_CMD_SET_PARAM_COMMIT:
1866 case EFFECT_CMD_GET_PARAM:
1867 break;
1868 default:
1869 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1870 break;
1871 }
1872 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001874 }
1875
Eric Laurent1ffc5852016-12-15 14:46:09 -08001876 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001877 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001878 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001879 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001880 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001881 writeToBuffer(NO_ERROR, response);
1882 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001883 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001884 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001885 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001886 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001887 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001888 writeToBuffer(NO_ERROR, response);
1889 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001890 }
1891
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001892 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001893 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001894 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001895 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001896 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001897 // only get parameter command is permitted for applications not controlling the effect
1898 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001900 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001901
1902 // handle commands that are not forwarded transparently to effect engine
1903 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001904 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001906 }
1907
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001909 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001911 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001913
Eric Laurentca7cc822012-11-19 14:55:58 -08001914 // No need to trylock() here as this function is executed in the binder thread serving a
1915 // particular client process: no risk to block the whole media server process or mixer
1916 // threads if we are stuck here
1917 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001918 // keep local copy of index in case of client corruption b/32220769
1919 const uint32_t clientIndex = mCblk->clientIndex;
1920 const uint32_t serverIndex = mCblk->serverIndex;
1921 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1922 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001923 mCblk->serverIndex = 0;
1924 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001925 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001926 }
1927 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001928 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001929 for (uint32_t index = serverIndex; index < clientIndex;) {
1930 int *p = (int *)(mBuffer + index);
1931 const int size = *p++;
1932 if (size < 0
1933 || size > EFFECT_PARAM_BUFFER_SIZE
1934 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001935 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001936 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 break;
1938 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001939
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001940 std::copy(reinterpret_cast<const uint8_t*>(p),
1941 reinterpret_cast<const uint8_t*>(p) + size,
1942 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001943
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001944 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001945 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001946 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947 sizeof(int),
1948 &replyBuffer);
1949 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001950
1951 // verify shared memory: server index shouldn't change; client index can't go back.
1952 if (serverIndex != mCblk->serverIndex
1953 || clientIndex > mCblk->clientIndex) {
1954 android_errorWriteLog(0x534e4554, "32220769");
1955 status = BAD_VALUE;
1956 break;
1957 }
1958
Eric Laurentca7cc822012-11-19 14:55:58 -08001959 // stop at first error encountered
1960 if (ret != NO_ERROR) {
1961 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001962 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001963 break;
1964 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001965 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001966 break;
1967 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001968 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 }
1970 mCblk->serverIndex = 0;
1971 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001972 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001973 }
1974
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001975 status_t status = effect->command(cmdCode,
1976 cmdData,
1977 maxResponseSize,
1978 response);
1979 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001980}
1981
1982void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1983{
1984 ALOGV("setControl %p control %d", this, hasControl);
1985
1986 mHasControl = hasControl;
1987 mEnabled = enabled;
1988
1989 if (signal && mEffectClient != 0) {
1990 mEffectClient->controlStatusChanged(hasControl);
1991 }
1992}
1993
1994void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 const std::vector<uint8_t>& cmdData,
1996 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001997{
1998 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001999 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002000 }
2001}
2002
2003
2004
2005void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2006{
2007 if (mEffectClient != 0) {
2008 mEffectClient->enableStatusChanged(enabled);
2009 }
2010}
2011
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002012void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002013{
2014 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2015
Marco Nelissenb2208842014-02-07 14:00:50 -08002016 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002017 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002018 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002019 mHasControl ? "yes" : "no",
2020 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002021 mCblk ? mCblk->clientIndex : 0,
2022 mCblk ? mCblk->serverIndex : 0
2023 );
2024
2025 if (locked) {
2026 mCblk->lock.unlock();
2027 }
2028}
2029
2030#undef LOG_TAG
2031#define LOG_TAG "AudioFlinger::EffectChain"
2032
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002033AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2034 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002035 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002036 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002037 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002038 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002039{
2040 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002041 sp<ThreadBase> p = thread.promote();
2042 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 return;
2044 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002045 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2046 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002047}
2048
2049AudioFlinger::EffectChain::~EffectChain()
2050{
Eric Laurentca7cc822012-11-19 14:55:58 -08002051}
2052
2053// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2054sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2055 effect_descriptor_t *descriptor)
2056{
2057 size_t size = mEffects.size();
2058
2059 for (size_t i = 0; i < size; i++) {
2060 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2061 return mEffects[i];
2062 }
2063 }
2064 return 0;
2065}
2066
2067// getEffectFromId_l() must be called with ThreadBase::mLock held
2068sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2069{
2070 size_t size = mEffects.size();
2071
2072 for (size_t i = 0; i < size; i++) {
2073 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2074 if (id == 0 || mEffects[i]->id() == id) {
2075 return mEffects[i];
2076 }
2077 }
2078 return 0;
2079}
2080
2081// getEffectFromType_l() must be called with ThreadBase::mLock held
2082sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2083 const effect_uuid_t *type)
2084{
2085 size_t size = mEffects.size();
2086
2087 for (size_t i = 0; i < size; i++) {
2088 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2089 return mEffects[i];
2090 }
2091 }
2092 return 0;
2093}
2094
Eric Laurent6c796322019-04-09 14:13:17 -07002095std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2096{
2097 std::vector<int> ids;
2098 Mutex::Autolock _l(mLock);
2099 for (size_t i = 0; i < mEffects.size(); i++) {
2100 ids.push_back(mEffects[i]->id());
2101 }
2102 return ids;
2103}
2104
Eric Laurentca7cc822012-11-19 14:55:58 -08002105void AudioFlinger::EffectChain::clearInputBuffer()
2106{
2107 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002108 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002109}
2110
2111// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002112void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002113{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002114 if (mInBuffer == NULL) {
2115 return;
2116 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002117 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002118 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002119
Eric Laurent6b446ce2019-12-13 10:56:31 -08002120 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002121 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002122}
2123
2124// Must be called with EffectChain::mLock locked
2125void AudioFlinger::EffectChain::process_l()
2126{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002127 // never process effects when:
2128 // - on an OFFLOAD thread
2129 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002130 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002131 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002132 bool tracksOnSession = (trackCnt() != 0);
2133
2134 if (!tracksOnSession && mTailBufferCount == 0) {
2135 doProcess = false;
2136 }
2137
2138 if (activeTrackCnt() == 0) {
2139 // if no track is active and the effect tail has not been rendered,
2140 // the input buffer must be cleared here as the mixer process will not do it
2141 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002142 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002143 if (mTailBufferCount > 0) {
2144 mTailBufferCount--;
2145 }
2146 }
2147 }
2148 }
2149
2150 size_t size = mEffects.size();
2151 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002152 // Only the input and output buffers of the chain can be external,
2153 // and 'update' / 'commit' do nothing for allocated buffers, thus
2154 // it's not needed to consider any other buffers here.
2155 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002156 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2157 mOutBuffer->update();
2158 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002159 for (size_t i = 0; i < size; i++) {
2160 mEffects[i]->process();
2161 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002162 mInBuffer->commit();
2163 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2164 mOutBuffer->commit();
2165 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002166 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002167 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002168 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002169 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2170 }
2171 if (doResetVolume) {
2172 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002173 }
2174}
2175
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002176// createEffect_l() must be called with ThreadBase::mLock held
2177status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002178 effect_descriptor_t *desc,
2179 int id,
2180 audio_session_t sessionId,
2181 bool pinned)
2182{
2183 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002184 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002185 status_t lStatus = effect->status();
2186 if (lStatus == NO_ERROR) {
2187 lStatus = addEffect_ll(effect);
2188 }
2189 if (lStatus != NO_ERROR) {
2190 effect.clear();
2191 }
2192 return lStatus;
2193}
2194
2195// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002196status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2197{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002198 Mutex::Autolock _l(mLock);
2199 return addEffect_ll(effect);
2200}
2201// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2202status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2203{
Eric Laurentca7cc822012-11-19 14:55:58 -08002204 effect_descriptor_t desc = effect->desc();
2205 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2206
Eric Laurent6b446ce2019-12-13 10:56:31 -08002207 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002208
2209 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2210 // Auxiliary effects are inserted at the beginning of mEffects vector as
2211 // they are processed first and accumulated in chain input buffer
2212 mEffects.insertAt(effect, 0);
2213
2214 // the input buffer for auxiliary effect contains mono samples in
2215 // 32 bit format. This is to avoid saturation in AudoMixer
2216 // accumulation stage. Saturation is done in EffectModule::process() before
2217 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002218 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002219 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002220#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002221 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002222 numSamples * sizeof(float), &halBuffer);
2223#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002224 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002225 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002226#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002227 if (result != OK) return result;
2228 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002229 // auxiliary effects output samples to chain input buffer for further processing
2230 // by insert effects
2231 effect->setOutBuffer(mInBuffer);
2232 } else {
2233 // Insert effects are inserted at the end of mEffects vector as they are processed
2234 // after track and auxiliary effects.
2235 // Insert effect order as a function of indicated preference:
2236 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2237 // another effect is present
2238 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2239 // last effect claiming first position
2240 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2241 // first effect claiming last position
2242 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2243 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2244 // already present
2245
2246 size_t size = mEffects.size();
2247 size_t idx_insert = size;
2248 ssize_t idx_insert_first = -1;
2249 ssize_t idx_insert_last = -1;
2250
2251 for (size_t i = 0; i < size; i++) {
2252 effect_descriptor_t d = mEffects[i]->desc();
2253 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2254 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2255 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2256 // check invalid effect chaining combinations
2257 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2258 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2259 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2260 desc.name, d.name);
2261 return INVALID_OPERATION;
2262 }
2263 // remember position of first insert effect and by default
2264 // select this as insert position for new effect
2265 if (idx_insert == size) {
2266 idx_insert = i;
2267 }
2268 // remember position of last insert effect claiming
2269 // first position
2270 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2271 idx_insert_first = i;
2272 }
2273 // remember position of first insert effect claiming
2274 // last position
2275 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2276 idx_insert_last == -1) {
2277 idx_insert_last = i;
2278 }
2279 }
2280 }
2281
2282 // modify idx_insert from first position if needed
2283 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2284 if (idx_insert_last != -1) {
2285 idx_insert = idx_insert_last;
2286 } else {
2287 idx_insert = size;
2288 }
2289 } else {
2290 if (idx_insert_first != -1) {
2291 idx_insert = idx_insert_first + 1;
2292 }
2293 }
2294
2295 // always read samples from chain input buffer
2296 effect->setInBuffer(mInBuffer);
2297
2298 // if last effect in the chain, output samples to chain
2299 // output buffer, otherwise to chain input buffer
2300 if (idx_insert == size) {
2301 if (idx_insert != 0) {
2302 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2303 mEffects[idx_insert-1]->configure();
2304 }
2305 effect->setOutBuffer(mOutBuffer);
2306 } else {
2307 effect->setOutBuffer(mInBuffer);
2308 }
2309 mEffects.insertAt(effect, idx_insert);
2310
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002311 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 idx_insert);
2313 }
2314 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002315
Eric Laurentca7cc822012-11-19 14:55:58 -08002316 return NO_ERROR;
2317}
2318
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002319// removeEffect_l() must be called with ThreadBase::mLock held
2320size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2321 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002322{
2323 Mutex::Autolock _l(mLock);
2324 size_t size = mEffects.size();
2325 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2326
2327 for (size_t i = 0; i < size; i++) {
2328 if (effect == mEffects[i]) {
2329 // calling stop here will remove pre-processing effect from the audio HAL.
2330 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2331 // the middle of a read from audio HAL
2332 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2333 mEffects[i]->state() == EffectModule::STOPPING) {
2334 mEffects[i]->stop();
2335 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002336 if (release) {
2337 mEffects[i]->release_l();
2338 }
2339
Mikhail Naganov022b9952017-01-04 16:36:51 -08002340 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002341 if (i == size - 1 && i != 0) {
2342 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2343 mEffects[i - 1]->configure();
2344 }
2345 }
2346 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002347 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002348 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002349
Eric Laurentca7cc822012-11-19 14:55:58 -08002350 break;
2351 }
2352 }
2353
2354 return mEffects.size();
2355}
2356
jiabin8f278ee2019-11-11 12:16:27 -08002357// setDevices_l() must be called with ThreadBase::mLock held
2358void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002359{
2360 size_t size = mEffects.size();
2361 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002362 mEffects[i]->setDevices(devices);
2363 }
2364}
2365
2366// setInputDevice_l() must be called with ThreadBase::mLock held
2367void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2368{
2369 size_t size = mEffects.size();
2370 for (size_t i = 0; i < size; i++) {
2371 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002372 }
2373}
2374
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002375// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002376void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2377{
2378 size_t size = mEffects.size();
2379 for (size_t i = 0; i < size; i++) {
2380 mEffects[i]->setMode(mode);
2381 }
2382}
2383
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002384// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002385void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2386{
2387 size_t size = mEffects.size();
2388 for (size_t i = 0; i < size; i++) {
2389 mEffects[i]->setAudioSource(source);
2390 }
2391}
2392
Zhou Songd505c642020-02-20 16:35:37 +08002393bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2394 for (const auto &effect : mEffects) {
2395 if (effect->isVolumeControlEnabled()) return true;
2396 }
2397 return false;
2398}
2399
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002400// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002401bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002402{
2403 uint32_t newLeft = *left;
2404 uint32_t newRight = *right;
2405 bool hasControl = false;
2406 int ctrlIdx = -1;
2407 size_t size = mEffects.size();
2408
2409 // first update volume controller
2410 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002411 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002412 ctrlIdx = i - 1;
2413 hasControl = true;
2414 break;
2415 }
2416 }
2417
Eric Laurentfa1e1232016-08-02 19:01:49 -07002418 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002419 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002420 if (hasControl) {
2421 *left = mNewLeftVolume;
2422 *right = mNewRightVolume;
2423 }
2424 return hasControl;
2425 }
2426
2427 mVolumeCtrlIdx = ctrlIdx;
2428 mLeftVolume = newLeft;
2429 mRightVolume = newRight;
2430
2431 // second get volume update from volume controller
2432 if (ctrlIdx >= 0) {
2433 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2434 mNewLeftVolume = newLeft;
2435 mNewRightVolume = newRight;
2436 }
2437 // then indicate volume to all other effects in chain.
2438 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002439 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002440 uint32_t lVol = newLeft;
2441 uint32_t rVol = newRight;
2442
2443 for (size_t i = 0; i < size; i++) {
2444 if ((int)i == ctrlIdx) {
2445 continue;
2446 }
2447 // this also works for ctrlIdx == -1 when there is no volume controller
2448 if ((int)i > ctrlIdx) {
2449 lVol = *left;
2450 rVol = *right;
2451 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002452 // Pass requested volume directly if this is volume monitor module
2453 if (mEffects[i]->isVolumeMonitor()) {
2454 mEffects[i]->setVolume(left, right, false);
2455 } else {
2456 mEffects[i]->setVolume(&lVol, &rVol, false);
2457 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002458 }
2459 *left = newLeft;
2460 *right = newRight;
2461
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002462 setVolumeForOutput_l(*left, *right);
2463
Eric Laurentca7cc822012-11-19 14:55:58 -08002464 return hasControl;
2465}
2466
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002467// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002468void AudioFlinger::EffectChain::resetVolume_l()
2469{
Eric Laurente7449bf2016-08-03 18:44:07 -07002470 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2471 uint32_t left = mLeftVolume;
2472 uint32_t right = mRightVolume;
2473 (void)setVolume_l(&left, &right, true);
2474 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002475}
2476
jiabineb3bda02020-06-30 14:07:03 -07002477// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2478bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2479{
2480 for (size_t i = 0; i < mEffects.size(); ++i) {
2481 if (mEffects[i]->isHapticGenerator()) {
2482 return true;
2483 }
2484 }
2485 return false;
2486}
2487
jiabine70bc7f2020-06-30 22:07:55 -07002488void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2489{
2490 Mutex::Autolock _l(mLock);
2491 for (size_t i = 0; i < mEffects.size(); ++i) {
2492 mEffects[i]->setHapticIntensity(id, intensity);
2493 }
2494}
2495
Eric Laurent1b928682014-10-02 19:41:47 -07002496void AudioFlinger::EffectChain::syncHalEffectsState()
2497{
2498 Mutex::Autolock _l(mLock);
2499 for (size_t i = 0; i < mEffects.size(); i++) {
2500 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2501 mEffects[i]->state() == EffectModule::STOPPING) {
2502 mEffects[i]->addEffectToHal_l();
2503 }
2504 }
2505}
2506
Eric Laurentca7cc822012-11-19 14:55:58 -08002507void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2508{
Eric Laurentca7cc822012-11-19 14:55:58 -08002509 String8 result;
2510
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002511 const size_t numEffects = mEffects.size();
2512 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002513
Marco Nelissenb2208842014-02-07 14:00:50 -08002514 if (numEffects) {
2515 bool locked = AudioFlinger::dumpTryLock(mLock);
2516 // failed to lock - AudioFlinger is probably deadlocked
2517 if (!locked) {
2518 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002519 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002520
Andy Hungbded9c82017-11-30 18:47:35 -08002521 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2522 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2523 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2524 (int)inBufferStr.size(), "In buffer ",
2525 (int)outBufferStr.size(), "Out buffer ");
2526 result.appendFormat("\t%s %s %d\n",
2527 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002528 write(fd, result.string(), result.size());
2529
2530 for (size_t i = 0; i < numEffects; ++i) {
2531 sp<EffectModule> effect = mEffects[i];
2532 if (effect != 0) {
2533 effect->dump(fd, args);
2534 }
2535 }
2536
2537 if (locked) {
2538 mLock.unlock();
2539 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002540 } else {
2541 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002542 }
2543}
2544
2545// must be called with ThreadBase::mLock held
2546void AudioFlinger::EffectChain::setEffectSuspended_l(
2547 const effect_uuid_t *type, bool suspend)
2548{
2549 sp<SuspendedEffectDesc> desc;
2550 // use effect type UUID timelow as key as there is no real risk of identical
2551 // timeLow fields among effect type UUIDs.
2552 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2553 if (suspend) {
2554 if (index >= 0) {
2555 desc = mSuspendedEffects.valueAt(index);
2556 } else {
2557 desc = new SuspendedEffectDesc();
2558 desc->mType = *type;
2559 mSuspendedEffects.add(type->timeLow, desc);
2560 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2561 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002562
Eric Laurentca7cc822012-11-19 14:55:58 -08002563 if (desc->mRefCount++ == 0) {
2564 sp<EffectModule> effect = getEffectIfEnabled(type);
2565 if (effect != 0) {
2566 desc->mEffect = effect;
2567 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002568 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002569 }
2570 }
2571 } else {
2572 if (index < 0) {
2573 return;
2574 }
2575 desc = mSuspendedEffects.valueAt(index);
2576 if (desc->mRefCount <= 0) {
2577 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002578 desc->mRefCount = 0;
2579 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002580 }
2581 if (--desc->mRefCount == 0) {
2582 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2583 if (desc->mEffect != 0) {
2584 sp<EffectModule> effect = desc->mEffect.promote();
2585 if (effect != 0) {
2586 effect->setSuspended(false);
2587 effect->lock();
2588 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002589 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002590 effect->setEnabled_l(handle->enabled());
2591 }
2592 effect->unlock();
2593 }
2594 desc->mEffect.clear();
2595 }
2596 mSuspendedEffects.removeItemsAt(index);
2597 }
2598 }
2599}
2600
2601// must be called with ThreadBase::mLock held
2602void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2603{
2604 sp<SuspendedEffectDesc> desc;
2605
2606 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2607 if (suspend) {
2608 if (index >= 0) {
2609 desc = mSuspendedEffects.valueAt(index);
2610 } else {
2611 desc = new SuspendedEffectDesc();
2612 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2613 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2614 }
2615 if (desc->mRefCount++ == 0) {
2616 Vector< sp<EffectModule> > effects;
2617 getSuspendEligibleEffects(effects);
2618 for (size_t i = 0; i < effects.size(); i++) {
2619 setEffectSuspended_l(&effects[i]->desc().type, true);
2620 }
2621 }
2622 } else {
2623 if (index < 0) {
2624 return;
2625 }
2626 desc = mSuspendedEffects.valueAt(index);
2627 if (desc->mRefCount <= 0) {
2628 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2629 desc->mRefCount = 1;
2630 }
2631 if (--desc->mRefCount == 0) {
2632 Vector<const effect_uuid_t *> types;
2633 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2634 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2635 continue;
2636 }
2637 types.add(&mSuspendedEffects.valueAt(i)->mType);
2638 }
2639 for (size_t i = 0; i < types.size(); i++) {
2640 setEffectSuspended_l(types[i], false);
2641 }
2642 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2643 mSuspendedEffects.keyAt(index));
2644 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2645 }
2646 }
2647}
2648
2649
2650// The volume effect is used for automated tests only
2651#ifndef OPENSL_ES_H_
2652static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2653 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2654const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2655#endif //OPENSL_ES_H_
2656
Eric Laurentd8365c52017-07-16 15:27:05 -07002657/* static */
2658bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2659{
2660 // Only NS and AEC are suspended when BtNRec is off
2661 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2662 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2663 return true;
2664 }
2665 return false;
2666}
2667
Eric Laurentca7cc822012-11-19 14:55:58 -08002668bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2669{
2670 // auxiliary effects and visualizer are never suspended on output mix
2671 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2672 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2673 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002674 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2675 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002676 return false;
2677 }
2678 return true;
2679}
2680
2681void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2682 Vector< sp<AudioFlinger::EffectModule> > &effects)
2683{
2684 effects.clear();
2685 for (size_t i = 0; i < mEffects.size(); i++) {
2686 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2687 effects.add(mEffects[i]);
2688 }
2689 }
2690}
2691
2692sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2693 const effect_uuid_t *type)
2694{
2695 sp<EffectModule> effect = getEffectFromType_l(type);
2696 return effect != 0 && effect->isEnabled() ? effect : 0;
2697}
2698
2699void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2700 bool enabled)
2701{
2702 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2703 if (enabled) {
2704 if (index < 0) {
2705 // if the effect is not suspend check if all effects are suspended
2706 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2707 if (index < 0) {
2708 return;
2709 }
2710 if (!isEffectEligibleForSuspend(effect->desc())) {
2711 return;
2712 }
2713 setEffectSuspended_l(&effect->desc().type, enabled);
2714 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2715 if (index < 0) {
2716 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2717 return;
2718 }
2719 }
2720 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2721 effect->desc().type.timeLow);
2722 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002723 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002724 if (desc->mEffect == 0) {
2725 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002726 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002727 effect->setSuspended(true);
2728 }
2729 } else {
2730 if (index < 0) {
2731 return;
2732 }
2733 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2734 effect->desc().type.timeLow);
2735 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2736 desc->mEffect.clear();
2737 effect->setSuspended(false);
2738 }
2739}
2740
Eric Laurent5baf2af2013-09-12 17:37:00 -07002741bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002742{
2743 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002744 return isNonOffloadableEnabled_l();
2745}
2746
2747bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2748{
Eric Laurent813e2a72013-08-31 12:59:48 -07002749 size_t size = mEffects.size();
2750 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002751 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002752 return true;
2753 }
2754 }
2755 return false;
2756}
2757
Eric Laurentaaa44472014-09-12 17:41:50 -07002758void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2759{
2760 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002761 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002762}
2763
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002764void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2765{
2766 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2767 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2768 }
2769 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2770 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2771 }
2772}
2773
2774void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2775{
2776 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2777 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2778 }
2779 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2780 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2781 }
2782}
2783
2784bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002785{
2786 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002787 for (const auto &effect : mEffects) {
2788 if (effect->isProcessImplemented()) {
2789 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002790 }
2791 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002792 // Allow effects without processing.
2793 return true;
2794}
2795
2796bool AudioFlinger::EffectChain::isFastCompatible() const
2797{
2798 Mutex::Autolock _l(mLock);
2799 for (const auto &effect : mEffects) {
2800 if (effect->isProcessImplemented()
2801 && effect->isImplementationSoftware()) {
2802 return false;
2803 }
2804 }
2805 // Allow effects without processing or hw accelerated effects.
2806 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002807}
2808
2809// isCompatibleWithThread_l() must be called with thread->mLock held
2810bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2811{
2812 Mutex::Autolock _l(mLock);
2813 for (size_t i = 0; i < mEffects.size(); i++) {
2814 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2815 return false;
2816 }
2817 }
2818 return true;
2819}
2820
Eric Laurent6b446ce2019-12-13 10:56:31 -08002821// EffectCallbackInterface implementation
2822status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2823 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2824 sp<EffectHalInterface> *effect) {
2825 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002826 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002827 if (effectsFactory != 0) {
2828 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2829 }
2830 return status;
2831}
2832
2833bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002834 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002835 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002836 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002837}
2838
2839status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2840 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002841 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002842}
2843
2844status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2845 sp<EffectHalInterface> effect) {
2846 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002847 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002848 if (t == nullptr) {
2849 return result;
2850 }
2851 sp <StreamHalInterface> st = t->stream();
2852 if (st == nullptr) {
2853 return result;
2854 }
2855 result = st->addEffect(effect);
2856 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2857 return result;
2858}
2859
2860status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2861 sp<EffectHalInterface> effect) {
2862 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002863 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002864 if (t == nullptr) {
2865 return result;
2866 }
2867 sp <StreamHalInterface> st = t->stream();
2868 if (st == nullptr) {
2869 return result;
2870 }
2871 result = st->removeEffect(effect);
2872 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2873 return result;
2874}
2875
2876audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002877 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002878 if (t == nullptr) {
2879 return AUDIO_IO_HANDLE_NONE;
2880 }
2881 return t->id();
2882}
2883
2884bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002885 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002886 if (t == nullptr) {
2887 return true;
2888 }
2889 return t->isOutput();
2890}
2891
2892bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002893 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002894 if (t == nullptr) {
2895 return false;
2896 }
2897 return t->type() == ThreadBase::OFFLOAD;
2898}
2899
2900bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002901 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002902 if (t == nullptr) {
2903 return false;
2904 }
2905 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2906}
2907
2908bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() 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 false;
2912 }
Andy Hungea840382020-05-05 21:50:17 -07002913 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002914}
2915
2916uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() 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 0;
2920 }
2921 return t->sampleRate();
2922}
2923
2924audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() 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 AUDIO_CHANNEL_NONE;
2928 }
2929 return t->channelMask();
2930}
2931
2932uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() 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 0;
2936 }
2937 return t->channelCount();
2938}
2939
jiabineb3bda02020-06-30 14:07:03 -07002940audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002941 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07002942 if (t == nullptr) {
2943 return AUDIO_CHANNEL_NONE;
2944 }
2945 return t->hapticChannelMask();
2946}
2947
Eric Laurent6b446ce2019-12-13 10:56:31 -08002948size_t AudioFlinger::EffectChain::EffectCallback::frameCount() 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->frameCount();
2954}
2955
2956uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08002957 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002958 if (t == nullptr) {
2959 return 0;
2960 }
2961 return t->latency_l();
2962}
2963
2964void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08002965 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002966 if (t == nullptr) {
2967 return;
2968 }
2969 t->setVolumeForOutput_l(left, right);
2970}
2971
2972void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002973 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08002974 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002975 if (t == nullptr) {
2976 return;
2977 }
2978 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2979
Andy Hung328d6772021-01-12 12:32:21 -08002980 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002981 if (c == nullptr) {
2982 return;
2983 }
Eric Laurent41709552019-12-16 19:34:05 -08002984 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2985 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002986}
2987
Eric Laurent41709552019-12-16 19:34:05 -08002988void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08002989 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002990 if (t == nullptr) {
2991 return;
2992 }
Eric Laurent41709552019-12-16 19:34:05 -08002993 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2994 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002995}
2996
Eric Laurent41709552019-12-16 19:34:05 -08002997void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002998 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2999
Andy Hung328d6772021-01-12 12:32:21 -08003000 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003001 if (t == nullptr) {
3002 return;
3003 }
3004 t->onEffectDisable();
3005}
3006
3007bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3008 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003009 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003010 if (t == nullptr) {
3011 return false;
3012 }
3013 t->disconnectEffectHandle(handle, unpinIfLast);
3014 return true;
3015}
3016
3017void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003018 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003019 if (c == nullptr) {
3020 return;
3021 }
3022 c->resetVolume_l();
3023
3024}
3025
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003026product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003027 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003028 if (c == nullptr) {
3029 return PRODUCT_STRATEGY_NONE;
3030 }
3031 return c->strategy();
3032}
3033
3034int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003035 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003036 if (c == nullptr) {
3037 return 0;
3038 }
3039 return c->activeTrackCnt();
3040}
3041
Eric Laurentb82e6b72019-11-22 17:25:04 -08003042
3043#undef LOG_TAG
3044#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3045
3046status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3047{
3048 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3049 Mutex::Autolock _l(mProxyLock);
3050 if (status == NO_ERROR) {
3051 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003052 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003053 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003054 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003055 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003056 bs = handle.second->disable(&status);
3057 }
3058 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003059 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003060 }
3061 }
3062 }
3063 ALOGV("%s enable %d status %d", __func__, enabled, status);
3064 return status;
3065}
3066
3067status_t AudioFlinger::DeviceEffectProxy::init(
3068 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3069//For all audio patches
3070//If src or sink device match
3071//If the effect is HW accelerated
3072// if no corresponding effect module
3073// Create EffectModule: mHalEffect
3074//Create and attach EffectHandle
3075//If the effect is not HW accelerated and the patch sink or src is a mixer port
3076// Create Effect on patch input or output thread on session -1
3077//Add EffectHandle to EffectHandle map of Effect Proxy:
3078 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3079 status_t status = NO_ERROR;
3080 for (auto &patch : patches) {
3081 status = onCreatePatch(patch.first, patch.second);
3082 ALOGV("%s onCreatePatch status %d", __func__, status);
3083 if (status == BAD_VALUE) {
3084 return status;
3085 }
3086 }
3087 return status;
3088}
3089
3090status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3091 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3092 status_t status = NAME_NOT_FOUND;
3093 sp<EffectHandle> handle;
3094 // only consider source[0] as this is the only "true" source of a patch
3095 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3096 ALOGV("%s source checkPort status %d", __func__, status);
3097 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3098 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3099 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3100 }
3101 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3102 Mutex::Autolock _l(mProxyLock);
3103 mEffectHandles.emplace(patchHandle, handle);
3104 }
3105 ALOGW_IF(status == BAD_VALUE,
3106 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3107
3108 return status;
3109}
3110
3111status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3112 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3113
3114 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3115 __func__, port->type, port->ext.device.type,
3116 port->ext.device.address, port->id, patch.isSoftware());
3117 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003118 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003119 return NAME_NOT_FOUND;
3120 }
3121 status_t status = NAME_NOT_FOUND;
3122
3123 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3124 Mutex::Autolock _l(mProxyLock);
3125 mDevicePort = *port;
3126 mHalEffect = new EffectModule(mMyCallback,
3127 const_cast<effect_descriptor_t *>(&mDescriptor),
3128 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3129 false /* pinned */, port->id);
3130 if (audio_is_input_device(mDevice.mType)) {
3131 mHalEffect->setInputDevice(mDevice);
3132 } else {
3133 mHalEffect->setDevices({mDevice});
3134 }
3135 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3136 status = (*handle)->initCheck();
3137 if (status == OK) {
3138 status = mHalEffect->addHandle((*handle).get());
3139 } else {
3140 mHalEffect.clear();
3141 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3142 }
3143 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3144 sp <ThreadBase> thread;
3145 if (audio_port_config_has_input_direction(port)) {
3146 if (patch.isSoftware()) {
3147 thread = patch.mRecord.thread();
3148 } else {
3149 thread = patch.thread().promote();
3150 }
3151 } else {
3152 if (patch.isSoftware()) {
3153 thread = patch.mPlayback.thread();
3154 } else {
3155 thread = patch.thread().promote();
3156 }
3157 }
3158 int enabled;
3159 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3160 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003161 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003162 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3163 } else {
3164 status = BAD_VALUE;
3165 }
3166 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003167 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003168 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003169 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003170 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003171 bs = (*handle)->disable(&status);
3172 }
3173 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003174 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003175 }
3176 }
3177 return status;
3178}
3179
3180void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3181 Mutex::Autolock _l(mProxyLock);
3182 mEffectHandles.erase(patchHandle);
3183}
3184
3185
3186size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3187{
3188 Mutex::Autolock _l(mProxyLock);
3189 if (effect == mHalEffect) {
3190 mHalEffect.clear();
3191 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3192 }
3193 return mHalEffect == nullptr ? 0 : 1;
3194}
3195
3196status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3197 sp<EffectHalInterface> effect) {
3198 if (mHalEffect == nullptr) {
3199 return NO_INIT;
3200 }
3201 return mManagerCallback->addEffectToHal(
3202 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3203}
3204
3205status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3206 sp<EffectHalInterface> effect) {
3207 if (mHalEffect == nullptr) {
3208 return NO_INIT;
3209 }
3210 return mManagerCallback->removeEffectFromHal(
3211 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3212}
3213
3214bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3215 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3216 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3217 }
3218 return true;
3219}
3220
3221uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3222 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3223 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3224 return mDevicePort.sample_rate;
3225 }
3226 return DEFAULT_OUTPUT_SAMPLE_RATE;
3227}
3228
3229audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3230 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3231 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3232 return mDevicePort.channel_mask;
3233 }
3234 return AUDIO_CHANNEL_OUT_STEREO;
3235}
3236
3237uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3238 if (isOutput()) {
3239 return audio_channel_count_from_out_mask(channelMask());
3240 }
3241 return audio_channel_count_from_in_mask(channelMask());
3242}
3243
3244void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3245 const Vector<String16> args;
3246 EffectBase::dump(fd, args);
3247
3248 const bool locked = dumpTryLock(mProxyLock);
3249 if (!locked) {
3250 String8 result("DeviceEffectProxy may be deadlocked\n");
3251 write(fd, result.string(), result.size());
3252 }
3253
3254 String8 outStr;
3255 if (mHalEffect != nullptr) {
3256 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3257 } else {
3258 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3259 }
3260 write(fd, outStr.string(), outStr.size());
3261 outStr.clear();
3262
3263 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3264 write(fd, outStr.string(), outStr.size());
3265 outStr.clear();
3266
3267 for (const auto& iter : mEffectHandles) {
3268 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3269 write(fd, outStr.string(), outStr.size());
3270 outStr.clear();
3271 sp<EffectBase> effect = iter.second->effect().promote();
3272 if (effect != nullptr) {
3273 effect->dump(fd, args);
3274 }
3275 }
3276
3277 if (locked) {
3278 mLock.unlock();
3279 }
3280}
3281
3282#undef LOG_TAG
3283#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3284
3285int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3286 return mManagerCallback->newEffectId();
3287}
3288
3289
3290bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3291 EffectHandle *handle, bool unpinIfLast) {
3292 sp<EffectBase> effectBase = handle->effect().promote();
3293 if (effectBase == nullptr) {
3294 return false;
3295 }
3296
3297 sp<EffectModule> effect = effectBase->asEffectModule();
3298 if (effect == nullptr) {
3299 return false;
3300 }
3301
3302 // restore suspended effects if the disconnected handle was enabled and the last one.
3303 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3304 if (remove) {
3305 sp<DeviceEffectProxy> proxy = mProxy.promote();
3306 if (proxy != nullptr) {
3307 proxy->removeEffect(effect);
3308 }
3309 if (handle->enabled()) {
3310 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3311 }
3312 }
3313 return true;
3314}
3315
3316status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3317 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3318 sp<EffectHalInterface> *effect) {
3319 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3320}
3321
3322status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3323 sp<EffectHalInterface> effect) {
3324 sp<DeviceEffectProxy> proxy = mProxy.promote();
3325 if (proxy == nullptr) {
3326 return NO_INIT;
3327 }
3328 return proxy->addEffectToHal(effect);
3329}
3330
3331status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3332 sp<EffectHalInterface> effect) {
3333 sp<DeviceEffectProxy> proxy = mProxy.promote();
3334 if (proxy == nullptr) {
3335 return NO_INIT;
3336 }
3337 return proxy->addEffectToHal(effect);
3338}
3339
3340bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3341 sp<DeviceEffectProxy> proxy = mProxy.promote();
3342 if (proxy == nullptr) {
3343 return true;
3344 }
3345 return proxy->isOutput();
3346}
3347
3348uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3349 sp<DeviceEffectProxy> proxy = mProxy.promote();
3350 if (proxy == nullptr) {
3351 return DEFAULT_OUTPUT_SAMPLE_RATE;
3352 }
3353 return proxy->sampleRate();
3354}
3355
3356audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3357 sp<DeviceEffectProxy> proxy = mProxy.promote();
3358 if (proxy == nullptr) {
3359 return AUDIO_CHANNEL_OUT_STEREO;
3360 }
3361 return proxy->channelMask();
3362}
3363
3364uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3365 sp<DeviceEffectProxy> proxy = mProxy.promote();
3366 if (proxy == nullptr) {
3367 return 2;
3368 }
3369 return proxy->channelCount();
3370}
3371
Glenn Kasten63238ef2015-03-02 15:50:29 -08003372} // namespace android