blob: 3ce44f9d897b8aad9de63bb34e2bd7827f527441 [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()
David Li6c8ac4b2021-06-22 22:17:52 +0800561 mOffloaded(false),
562 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800563#ifdef FLOAT_EFFECT_CHAIN
564 , mSupportsFloat(false)
565#endif
566{
567 ALOGV("Constructor %p pinned %d", this, pinned);
568 int lStatus;
569
570 // create effect engine from effect factory
571 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800572 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800573 if (mStatus != NO_ERROR) {
574 return;
575 }
576 lStatus = init();
577 if (lStatus < 0) {
578 mStatus = lStatus;
579 goto Error;
580 }
581
582 setOffloaded(callback->isOffload(), callback->io());
583 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
584
585 return;
586Error:
587 mEffectInterface.clear();
588 ALOGV("Constructor Error %d", mStatus);
589}
590
591AudioFlinger::EffectModule::~EffectModule()
592{
593 ALOGV("Destructor %p", this);
594 if (mEffectInterface != 0) {
595 char uuidStr[64];
596 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
597 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
598 this, uuidStr);
599 release_l();
600 }
601
602}
603
Eric Laurentfa1e1232016-08-02 19:01:49 -0700604bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 Mutex::Autolock _l(mLock);
606
Eric Laurentfa1e1232016-08-02 19:01:49 -0700607 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800608 switch (mState) {
609 case RESTART:
610 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700611 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800612
613 case STARTING:
614 // clear auxiliary effect input buffer for next accumulation
615 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
616 memset(mConfig.inputCfg.buffer.raw,
617 0,
618 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
619 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700620 if (start_l() == NO_ERROR) {
621 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700622 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700623 } else {
624 mState = IDLE;
625 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800626 break;
627 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900628 // volume control for offload and direct threads must take effect immediately.
629 if (stop_l() == NO_ERROR
630 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 mDisableWaitCnt = mMaxDisableWaitCnt;
632 } else {
633 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
634 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800635 mState = STOPPED;
636 break;
637 case STOPPED:
638 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
639 // turn off sequence.
640 if (--mDisableWaitCnt == 0) {
641 reset_l();
642 mState = IDLE;
643 }
644 break;
645 default: //IDLE , ACTIVE, DESTROYED
646 break;
647 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700648
649 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800650}
651
652void AudioFlinger::EffectModule::process()
653{
654 Mutex::Autolock _l(mLock);
655
Mikhail Naganov022b9952017-01-04 16:36:51 -0800656 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 return;
658 }
659
rago94a1ee82017-07-21 15:11:02 -0700660 const uint32_t inChannelCount =
661 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
662 const uint32_t outChannelCount =
663 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
664 const bool auxType =
665 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
666
Andy Hungfa69ca32017-11-30 10:07:53 -0800667 // safeInputOutputSampleCount is 0 if the channel count between input and output
668 // buffers do not match. This prevents automatic accumulation or copying between the
669 // input and output effect buffers without an intermediary effect process.
670 // TODO: consider implementing channel conversion.
671 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700672 mInChannelCountRequested != mOutChannelCountRequested ? 0
673 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800674 mConfig.inputCfg.buffer.frameCount,
675 mConfig.outputCfg.buffer.frameCount);
676 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
677#ifdef FLOAT_EFFECT_CHAIN
678 accumulate_float(
679 mConfig.outputCfg.buffer.f32,
680 mConfig.inputCfg.buffer.f32,
681 safeInputOutputSampleCount);
682#else
683 accumulate_i16(
684 mConfig.outputCfg.buffer.s16,
685 mConfig.inputCfg.buffer.s16,
686 safeInputOutputSampleCount);
687#endif
688 };
689 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
690#ifdef FLOAT_EFFECT_CHAIN
691 memcpy(
692 mConfig.outputCfg.buffer.f32,
693 mConfig.inputCfg.buffer.f32,
694 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
695
696#else
697 memcpy(
698 mConfig.outputCfg.buffer.s16,
699 mConfig.inputCfg.buffer.s16,
700 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
701#endif
702 };
703
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700705 int ret;
706 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700707 if (auxType) {
708 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800709 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700710#ifdef FLOAT_EFFECT_CHAIN
711 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800712#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700713 // Do in-place float conversion for auxiliary effect input buffer.
714 static_assert(sizeof(float) <= sizeof(int32_t),
715 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
716
Andy Hungfa69ca32017-11-30 10:07:53 -0800717 memcpy_to_float_from_q4_27(
718 mConfig.inputCfg.buffer.f32,
719 mConfig.inputCfg.buffer.s32,
720 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800721#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800722 } else
Andy Hung116a4982017-11-30 10:15:08 -0800723#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800724 {
Andy Hung116a4982017-11-30 10:15:08 -0800725#ifdef FLOAT_AUX
726 memcpy_to_i16_from_float(
727 mConfig.inputCfg.buffer.s16,
728 mConfig.inputCfg.buffer.f32,
729 mConfig.inputCfg.buffer.frameCount);
730#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800731 memcpy_to_i16_from_q4_27(
732 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700733 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800734 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800735#endif
rago94a1ee82017-07-21 15:11:02 -0700736 }
rago94a1ee82017-07-21 15:11:02 -0700737 }
738#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800739 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
740 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
741
742 if (!auxType && mInChannelCountRequested != inChannelCount) {
743 adjust_channels(
744 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
745 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
746 sizeof(float),
747 sizeof(float)
748 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
749 inBuffer = mInConversionBuffer;
750 }
751 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
752 && mOutChannelCountRequested != outChannelCount) {
753 adjust_selected_channels(
754 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
755 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
756 sizeof(float),
757 sizeof(float)
758 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
759 outBuffer = mOutConversionBuffer;
760 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800761 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
762 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800763 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800764 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
765 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700766 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 memcpy_to_i16_from_float(
768 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800769 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700772 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800773 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800774 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800775 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
776 goto data_bypass;
777 }
778 memcpy_to_i16_from_float(
779 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800780 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800782 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700783 }
784 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800785#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800786 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800787#ifdef FLOAT_EFFECT_CHAIN
788 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 sp<EffectBufferHalInterface> target =
790 mOutChannelCountRequested != outChannelCount
791 ? mOutConversionBuffer : mOutBuffer;
792
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800794 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800795 mOutConversionBuffer->audioBuffer()->s16,
796 outChannelCount * mConfig.outputCfg.buffer.frameCount);
797 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800798 if (mOutChannelCountRequested != outChannelCount) {
799 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
800 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
801 sizeof(float),
802 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
803 }
rago94a1ee82017-07-21 15:11:02 -0700804#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700805 } else {
rago94a1ee82017-07-21 15:11:02 -0700806#ifdef FLOAT_EFFECT_CHAIN
807 data_bypass:
808#endif
809 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800810 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700811 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800812 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700813 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800814 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700815 }
816 }
817 ret = -ENODATA;
818 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800819
Eric Laurentca7cc822012-11-19 14:55:58 -0800820 // force transition to IDLE state when engine is ready
821 if (mState == STOPPED && ret == -ENODATA) {
822 mDisableWaitCnt = 1;
823 }
824
825 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700826 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800827#ifdef FLOAT_AUX
828 const size_t size =
829 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
830#else
rago94a1ee82017-07-21 15:11:02 -0700831 const size_t size =
832 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800833#endif
rago94a1ee82017-07-21 15:11:02 -0700834 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800835 }
836 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700837 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
839 // If an insert effect is idle and input buffer is different from output buffer,
840 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700841 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700842 // similar handling with data_bypass above.
843 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
844 accumulateInputToOutput();
845 } else { // EFFECT_BUFFER_ACCESS_WRITE
846 copyInputToOutput();
847 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 }
849 }
850}
851
852void AudioFlinger::EffectModule::reset_l()
853{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700854 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800855 return;
856 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700857 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800858}
859
860status_t AudioFlinger::EffectModule::configure()
861{
rago94a1ee82017-07-21 15:11:02 -0700862 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700863 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700864 uint32_t size;
865 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700866 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700867
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700868 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700869 status = NO_INIT;
870 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800871 }
872
Eric Laurentca7cc822012-11-19 14:55:58 -0800873 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800874 // TODO: handle configuration of input (record) SW effects above the HAL,
875 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
876 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700877 callback = getCallback();
878 channelMask = callback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800879 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700880 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800881
882 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800883 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
884 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
885 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
886 mConfig.inputCfg.channels);
887 }
888#ifndef MULTICHANNEL_EFFECT_CHAIN
889 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
890 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
891 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
892 mConfig.outputCfg.channels);
893 }
894#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800896#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700897 // TODO: Update this logic when multichannel effects are implemented.
898 // For offloaded tracks consider mono output as stereo for proper effect initialization
899 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
900 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
901 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
902 ALOGV("Overriding effect input and output as STEREO");
903 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800904#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800905 }
jiabineb3bda02020-06-30 14:07:03 -0700906 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700907 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700908 mConfig.inputCfg.channels |= hapticChannelMask;
909 mConfig.outputCfg.channels |= hapticChannelMask;
910 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800911 mInChannelCountRequested =
912 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
913 mOutChannelCountRequested =
914 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700915
rago94a1ee82017-07-21 15:11:02 -0700916 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
917 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900918
919 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700920 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900921 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
922 ALOGV("Overriding effect input as 48kHz");
923 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700924 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900925 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800926 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
927 mConfig.inputCfg.bufferProvider.cookie = NULL;
928 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
929 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
930 mConfig.outputCfg.bufferProvider.cookie = NULL;
931 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
932 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
933 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
934 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800935 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800936 // always overwrites output buffer: input buffer == output buffer
937 // - in other sessions:
938 // last effect in the chain accumulates in output buffer: input buffer != output buffer
939 // other effect: overwrites output buffer: input buffer == output buffer
940 // Auxiliary effect:
941 // accumulates in output buffer: input buffer != output buffer
942 // Therefore: accumulate <=> input buffer != output buffer
943 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
944 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
945 } else {
946 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
947 }
948 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
949 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700950 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800951 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
952
Eric Laurent6b446ce2019-12-13 10:56:31 -0800953 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700954 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800955 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800956
957 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700958 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700959 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800960 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700961 &mConfig,
962 &size,
963 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700964 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800965 status = cmdStatus;
966 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800967
968#ifdef MULTICHANNEL_EFFECT_CHAIN
969 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700970 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800971 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
972 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
973 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700974 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
975 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800976 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
977 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
978 }
979 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
980 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
981 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
982 }
983 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700984 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800985 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700986 &mConfig,
987 &size,
988 &cmdStatus);
989 if (status == NO_ERROR) {
990 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800991 }
992 }
993#endif
994
995#ifdef FLOAT_EFFECT_CHAIN
996 if (status == NO_ERROR) {
997 mSupportsFloat = true;
998 }
999
1000 if (status != NO_ERROR) {
1001 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1002 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1003 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1004 size = sizeof(int);
1005 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1006 sizeof(mConfig),
1007 &mConfig,
1008 &size,
1009 &cmdStatus);
1010 if (status == NO_ERROR) {
1011 status = cmdStatus;
1012 }
1013 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001014 mSupportsFloat = false;
1015 ALOGVV("config worked with 16 bit");
1016 } else {
1017 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 }
rago94a1ee82017-07-21 15:11:02 -07001019 }
1020#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001021
rago94a1ee82017-07-21 15:11:02 -07001022 if (status == NO_ERROR) {
1023 // Establish Buffer strategy
1024 setInBuffer(mInBuffer);
1025 setOutBuffer(mOutBuffer);
1026
1027 // Update visualizer latency
1028 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1029 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1030 effect_param_t *p = (effect_param_t *)buf32;
1031
1032 p->psize = sizeof(uint32_t);
1033 p->vsize = sizeof(uint32_t);
1034 size = sizeof(int);
1035 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1036
Andy Hungfda44002021-06-03 17:23:16 -07001037 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001038
1039 *((int32_t *)p->data + 1)= latency;
1040 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1041 sizeof(effect_param_t) + 8,
1042 &buf32,
1043 &size,
1044 &cmdStatus);
1045 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 }
1047
Andy Hung05083ac2017-12-14 15:00:28 -08001048 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1049 mMaxDisableWaitCnt = (uint32_t)std::max(
1050 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1051 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1052 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001053
Eric Laurentd0ebb532013-04-02 16:41:41 -07001054exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001055 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001056 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001057 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001058 return status;
1059}
1060
1061status_t AudioFlinger::EffectModule::init()
1062{
1063 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001064 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 return NO_INIT;
1066 }
1067 status_t cmdStatus;
1068 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001069 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1070 0,
1071 NULL,
1072 &size,
1073 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 if (status == 0) {
1075 status = cmdStatus;
1076 }
1077 return status;
1078}
1079
Eric Laurent1b928682014-10-02 19:41:47 -07001080void AudioFlinger::EffectModule::addEffectToHal_l()
1081{
1082 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1083 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001084 if (mAddedToHal) {
1085 return;
1086 }
1087
Andy Hungfda44002021-06-03 17:23:16 -07001088 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001089 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001090 }
1091}
1092
Eric Laurentfa1e1232016-08-02 19:01:49 -07001093// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001094status_t AudioFlinger::EffectModule::start()
1095{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001096 status_t status;
1097 {
1098 Mutex::Autolock _l(mLock);
1099 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001100 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001101 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001102 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001103 }
1104 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001105}
1106
1107status_t AudioFlinger::EffectModule::start_l()
1108{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001109 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001110 return NO_INIT;
1111 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001112 if (mStatus != NO_ERROR) {
1113 return mStatus;
1114 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001115 status_t cmdStatus;
1116 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001117 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1118 0,
1119 NULL,
1120 &size,
1121 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001122 if (status == 0) {
1123 status = cmdStatus;
1124 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001125 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001126 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 }
1128 return status;
1129}
1130
1131status_t AudioFlinger::EffectModule::stop()
1132{
1133 Mutex::Autolock _l(mLock);
1134 return stop_l();
1135}
1136
1137status_t AudioFlinger::EffectModule::stop_l()
1138{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001139 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 return NO_INIT;
1141 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001142 if (mStatus != NO_ERROR) {
1143 return mStatus;
1144 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001145 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001147
1148 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001149 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1150 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1151 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001152 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001153 mSetVolumeReentrantTid = INVALID_PID;
1154 }
1155
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001156 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1157 0,
1158 NULL,
1159 &size,
1160 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001161 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 status = cmdStatus;
1163 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001164 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001165 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001166 }
1167 return status;
1168}
1169
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001170// must be called with EffectChain::mLock held
1171void AudioFlinger::EffectModule::release_l()
1172{
1173 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001174 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001175 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001176 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001177 mEffectInterface.clear();
1178 }
1179}
1180
Eric Laurent6b446ce2019-12-13 10:56:31 -08001181status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001182{
1183 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1184 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001185 if (!mAddedToHal) {
1186 return NO_ERROR;
1187 }
1188
Andy Hungfda44002021-06-03 17:23:16 -07001189 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001190 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001191 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001192 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001193}
1194
Andy Hunge4a1d912016-08-17 14:11:13 -07001195// round up delta valid if value and divisor are positive.
1196template <typename T>
1197static T roundUpDelta(const T &value, const T &divisor) {
1198 T remainder = value % divisor;
1199 return remainder == 0 ? 0 : divisor - remainder;
1200}
1201
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001202status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1203 const std::vector<uint8_t>& cmdData,
1204 int32_t maxReplySize,
1205 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001206{
1207 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001208 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001209
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001210 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001211 return NO_INIT;
1212 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001213 if (mStatus != NO_ERROR) {
1214 return mStatus;
1215 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001216 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1217 return -EINVAL;
1218 }
1219 size_t cmdSize = cmdData.size();
1220 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1221 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1222 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001223 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001224 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001225 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001226 android_errorWriteLog(0x534e4554, "33003822");
1227 return -EINVAL;
1228 }
1229 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001230 (maxReplySize < sizeof(effect_param_t) ||
1231 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001232 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001233 return -EINVAL;
1234 }
ragoe2759072016-11-22 18:02:48 -08001235 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001236 (sizeof(effect_param_t) > maxReplySize
1237 || param->psize > maxReplySize - sizeof(effect_param_t)
1238 || param->vsize > maxReplySize - sizeof(effect_param_t)
1239 - param->psize
1240 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1241 maxReplySize
1242 - sizeof(effect_param_t)
1243 - param->psize
1244 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001245 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1246 android_errorWriteLog(0x534e4554, "32705438");
1247 return -EINVAL;
1248 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001249 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001250 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1251 && // DEFERRED not generally used
1252 (param == nullptr
1253 || param->psize > cmdSize - sizeof(effect_param_t)
1254 || param->vsize > cmdSize - sizeof(effect_param_t)
1255 - param->psize
1256 || roundUpDelta(param->psize,
1257 (uint32_t) sizeof(int)) >
1258 cmdSize
1259 - sizeof(effect_param_t)
1260 - param->psize
1261 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001262 android_errorWriteLog(0x534e4554, "30204301");
1263 return -EINVAL;
1264 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001265 uint32_t replySize = maxReplySize;
1266 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001267 status_t status = mEffectInterface->command(cmdCode,
1268 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001269 const_cast<uint8_t*>(cmdData.data()),
1270 &replySize,
1271 reply->data());
1272 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001273 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001274 for (size_t i = 1; i < mHandles.size(); i++) {
1275 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001276 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001277 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001278 }
1279 }
1280 }
1281 return status;
1282}
1283
Eric Laurentca7cc822012-11-19 14:55:58 -08001284bool AudioFlinger::EffectModule::isProcessEnabled() const
1285{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001286 if (mStatus != NO_ERROR) {
1287 return false;
1288 }
1289
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 switch (mState) {
1291 case RESTART:
1292 case ACTIVE:
1293 case STOPPING:
1294 case STOPPED:
1295 return true;
1296 case IDLE:
1297 case STARTING:
1298 case DESTROYED:
1299 default:
1300 return false;
1301 }
1302}
1303
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001304bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1305{
Andy Hungfda44002021-06-03 17:23:16 -07001306 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001307}
1308
1309bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1310{
1311 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1312}
1313
Mikhail Naganov022b9952017-01-04 16:36:51 -08001314void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001315 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001316
1317 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001318 if (buffer != 0) {
1319 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1320 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1321 } else {
1322 mConfig.inputCfg.buffer.raw = NULL;
1323 }
1324 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001325 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001326
1327#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001328 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001329 // Theoretically insert effects can also do in-place conversions (destroying
1330 // the original buffer) when the output buffer is identical to the input buffer,
1331 // but we don't optimize for it here.
1332 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001333 const uint32_t inChannelCount =
1334 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1335 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001336 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001337 // we need to translate - create hidl shared buffer and intercept
1338 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001339 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1340 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1341 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001342
1343 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1344 __func__, inChannels, inFrameCount, size);
1345
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001346 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001347 || size > mInConversionBuffer->getSize())) {
1348 mInConversionBuffer.clear();
1349 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001350 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001351 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001352 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001353 mInConversionBuffer->setFrameCount(inFrameCount);
1354 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001355 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001356 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001357 }
1358 }
1359#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001360}
1361
1362void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001363 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001364
1365 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001366 if (buffer != 0) {
1367 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1368 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1369 } else {
1370 mConfig.outputCfg.buffer.raw = NULL;
1371 }
1372 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001373 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001374
1375#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001376 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001377 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001378 const uint32_t outChannelCount =
1379 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1380 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001381 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001382 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001383 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1384 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1385 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001386
1387 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1388 __func__, outChannels, outFrameCount, size);
1389
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001390 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001391 || size > mOutConversionBuffer->getSize())) {
1392 mOutConversionBuffer.clear();
1393 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001394 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001395 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001396 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001397 mOutConversionBuffer->setFrameCount(outFrameCount);
1398 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001399 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001400 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001401 }
1402 }
1403#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001404}
1405
Eric Laurentca7cc822012-11-19 14:55:58 -08001406status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1407{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001408 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001409 if (mStatus != NO_ERROR) {
1410 return mStatus;
1411 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1414 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1415 if (isProcessEnabled() &&
1416 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001417 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1418 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 uint32_t volume[2];
1420 uint32_t *pVolume = NULL;
1421 uint32_t size = sizeof(volume);
1422 volume[0] = *left;
1423 volume[1] = *right;
1424 if (controller) {
1425 pVolume = volume;
1426 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001427 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1428 size,
1429 volume,
1430 &size,
1431 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1433 *left = volume[0];
1434 *right = volume[1];
1435 }
1436 }
1437 return status;
1438}
1439
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001440void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1441{
Zhou Songd505c642020-02-20 16:35:37 +08001442 // for offload or direct thread, if the effect chain has non-offloadable
1443 // effect and any effect module within the chain has volume control, then
1444 // volume control is delegated to effect, otherwise, set volume to hal.
1445 if (mEffectCallback->isOffloadOrDirect() &&
1446 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001447 float vol_l = (float)left / (1 << 24);
1448 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001449 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001450 }
1451}
1452
jiabin8f278ee2019-11-11 12:16:27 -08001453status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1454 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001455{
jiabin8f278ee2019-11-11 12:16:27 -08001456 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1457 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001458 return NO_ERROR;
1459 }
1460
1461 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001462 if (mStatus != NO_ERROR) {
1463 return mStatus;
1464 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001465 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001466 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 status_t cmdStatus;
1468 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001469 // FIXME: use audio device types and addresses when the hal interface is ready.
1470 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001471 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001472 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001473 &size,
1474 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001475 }
1476 return status;
1477}
1478
jiabin8f278ee2019-11-11 12:16:27 -08001479status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1480{
1481 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1482}
1483
1484status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1485{
1486 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1487}
1488
Eric Laurentca7cc822012-11-19 14:55:58 -08001489status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1490{
1491 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001492 if (mStatus != NO_ERROR) {
1493 return mStatus;
1494 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001495 status_t status = NO_ERROR;
1496 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1497 status_t cmdStatus;
1498 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001499 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1500 sizeof(audio_mode_t),
1501 &mode,
1502 &size,
1503 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 if (status == NO_ERROR) {
1505 status = cmdStatus;
1506 }
1507 }
1508 return status;
1509}
1510
1511status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1512{
1513 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001514 if (mStatus != NO_ERROR) {
1515 return mStatus;
1516 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 status_t status = NO_ERROR;
1518 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1519 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001520 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1521 sizeof(audio_source_t),
1522 &source,
1523 &size,
1524 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001525 }
1526 return status;
1527}
1528
Eric Laurent5baf2af2013-09-12 17:37:00 -07001529status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1530{
1531 Mutex::Autolock _l(mLock);
1532 if (mStatus != NO_ERROR) {
1533 return mStatus;
1534 }
1535 status_t status = NO_ERROR;
1536 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1537 status_t cmdStatus;
1538 uint32_t size = sizeof(status_t);
1539 effect_offload_param_t cmd;
1540
1541 cmd.isOffload = offloaded;
1542 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001543 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1544 sizeof(effect_offload_param_t),
1545 &cmd,
1546 &size,
1547 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001548 if (status == NO_ERROR) {
1549 status = cmdStatus;
1550 }
1551 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1552 } else {
1553 if (offloaded) {
1554 status = INVALID_OPERATION;
1555 }
1556 mOffloaded = false;
1557 }
1558 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1559 return status;
1560}
1561
1562bool AudioFlinger::EffectModule::isOffloaded() const
1563{
1564 Mutex::Autolock _l(mLock);
1565 return mOffloaded;
1566}
1567
jiabineb3bda02020-06-30 14:07:03 -07001568/*static*/
1569bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1570 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1571}
1572
1573bool AudioFlinger::EffectModule::isHapticGenerator() const {
1574 return isHapticGenerator(&mDescriptor.type);
1575}
1576
jiabine70bc7f2020-06-30 22:07:55 -07001577status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1578{
1579 if (mStatus != NO_ERROR) {
1580 return mStatus;
1581 }
1582 if (!isHapticGenerator()) {
1583 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1584 return INVALID_OPERATION;
1585 }
1586
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001587 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1588 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001589 param->psize = sizeof(int32_t);
1590 param->vsize = sizeof(int32_t) * 2;
1591 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1592 *((int32_t*)param->data + 1) = id;
1593 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001594 std::vector<uint8_t> response;
1595 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001596 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001597 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1598 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001599 }
1600 return status;
1601}
1602
Lais Andradebc3f37a2021-07-02 00:13:19 +01001603status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001604{
1605 if (mStatus != NO_ERROR) {
1606 return mStatus;
1607 }
1608 if (!isHapticGenerator()) {
1609 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1610 return INVALID_OPERATION;
1611 }
1612
Lais Andradebc3f37a2021-07-02 00:13:19 +01001613 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001614 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001615 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001616 effect_param_t *param = (effect_param_t*) request.data();
1617 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001618 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001619 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1620 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001621 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1622 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1623 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001624 std::vector<uint8_t> response;
1625 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1626 if (status == NO_ERROR) {
1627 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1628 status = *reinterpret_cast<const status_t*>(response.data());
1629 }
1630 return status;
1631}
1632
Andy Hungbded9c82017-11-30 18:47:35 -08001633static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1634 std::stringstream ss;
1635
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001636 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001637 return "nullptr"; // make different than below
1638 } else if (buffer->externalData() != nullptr) {
1639 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1640 << " -> "
1641 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1642 } else {
1643 ss << buffer->audioBuffer()->raw;
1644 }
1645 return ss.str();
1646}
Marco Nelissenb2208842014-02-07 14:00:50 -08001647
Eric Laurent41709552019-12-16 19:34:05 -08001648void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001649{
Eric Laurent41709552019-12-16 19:34:05 -08001650 EffectBase::dump(fd, args);
1651
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001653 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001654
Eric Laurent41709552019-12-16 19:34:05 -08001655 result.append("\t\tStatus Engine:\n");
1656 result.appendFormat("\t\t%03d %p\n",
1657 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001658
1659 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001660
1661 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001662 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1663 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1664 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 mConfig.inputCfg.buffer.frameCount,
1666 mConfig.inputCfg.samplingRate,
1667 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001668 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001669 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001670
1671 result.append("\t\t- Output configuration:\n");
1672 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001673 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001674 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001675 mConfig.outputCfg.buffer.frameCount,
1676 mConfig.outputCfg.samplingRate,
1677 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001678 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001679 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001680
rago94a1ee82017-07-21 15:11:02 -07001681#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001682
Andy Hungbded9c82017-11-30 18:47:35 -08001683 result.appendFormat("\t\t- HAL buffers:\n"
1684 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1685 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1686 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1687 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1688 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001689#endif
1690
Eric Laurentca7cc822012-11-19 14:55:58 -08001691 write(fd, result.string(), result.length());
1692
Mikhail Naganov4d547672019-02-22 14:19:19 -08001693 if (mEffectInterface != 0) {
1694 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1695 (void)mEffectInterface->dump(fd);
1696 }
1697
Eric Laurentca7cc822012-11-19 14:55:58 -08001698 if (locked) {
1699 mLock.unlock();
1700 }
1701}
1702
1703// ----------------------------------------------------------------------------
1704// EffectHandle implementation
1705// ----------------------------------------------------------------------------
1706
1707#undef LOG_TAG
1708#define LOG_TAG "AudioFlinger::EffectHandle"
1709
Eric Laurent41709552019-12-16 19:34:05 -08001710AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001711 const sp<AudioFlinger::Client>& client,
1712 const sp<media::IEffectClient>& effectClient,
1713 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 : BnEffect(),
1715 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001716 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001717{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001718 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001719
1720 if (client == 0) {
1721 return;
1722 }
1723 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1724 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001725 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001726 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001727 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001729 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001730 return;
1731 }
Glenn Kastene75da402013-11-20 13:54:52 -08001732 new(mCblk) effect_param_cblk_t();
1733 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001734}
1735
1736AudioFlinger::EffectHandle::~EffectHandle()
1737{
1738 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 disconnect(false);
1740}
1741
Glenn Kastene75da402013-11-20 13:54:52 -08001742status_t AudioFlinger::EffectHandle::initCheck()
1743{
1744 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1745}
1746
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001747#define RETURN(code) \
1748 *_aidl_return = (code); \
1749 return Status::ok();
1750
1751Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001752{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001753 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001754 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001755 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001756 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001757 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001758 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001760 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001761 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001762
1763 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001764 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001765 }
1766
1767 mEnabled = true;
1768
Eric Laurent6c796322019-04-09 14:13:17 -07001769 status_t status = effect->updatePolicyState();
1770 if (status != NO_ERROR) {
1771 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001773 }
1774
Eric Laurent6b446ce2019-12-13 10:56:31 -08001775 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001776
1777 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001778 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001779 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 }
1781
Eric Laurent6b446ce2019-12-13 10:56:31 -08001782 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001783 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001784 mEnabled = false;
1785 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001786 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001787}
1788
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001789Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001790{
1791 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001793 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001794 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001795 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001796 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001798 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001799 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001800
1801 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001802 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001803 }
1804 mEnabled = false;
1805
Eric Laurent6c796322019-04-09 14:13:17 -07001806 effect->updatePolicyState();
1807
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001809 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001810 }
1811
Eric Laurent6b446ce2019-12-13 10:56:31 -08001812 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001813 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001814}
1815
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001816Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001817{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001818 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001819 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001820 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001821}
1822
1823void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1824{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001825 AutoMutex _l(mLock);
1826 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1827 if (mDisconnected) {
1828 if (unpinIfLast) {
1829 android_errorWriteLog(0x534e4554, "32707507");
1830 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 return;
1832 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001833 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001834 {
Eric Laurent41709552019-12-16 19:34:05 -08001835 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001836 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001837 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001838 ALOGW("%s Effect handle %p disconnected after thread destruction",
1839 __func__, this);
1840 }
1841 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001842 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001843 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001844
Eric Laurentca7cc822012-11-19 14:55:58 -08001845 if (mClient != 0) {
1846 if (mCblk != NULL) {
1847 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1848 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1849 }
1850 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001851 // Client destructor must run with AudioFlinger client mutex locked
1852 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 mClient.clear();
1854 }
1855}
1856
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001857Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1858 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1859 return Status::ok();
1860}
1861
1862Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1863 const std::vector<uint8_t>& cmdData,
1864 int32_t maxResponseSize,
1865 std::vector<uint8_t>* response,
1866 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001867{
1868 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001869 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001870
Eric Laurentc7ab3092017-06-15 18:43:46 -07001871 // reject commands reserved for internal use by audio framework if coming from outside
1872 // of audioserver
1873 switch(cmdCode) {
1874 case EFFECT_CMD_ENABLE:
1875 case EFFECT_CMD_DISABLE:
1876 case EFFECT_CMD_SET_PARAM:
1877 case EFFECT_CMD_SET_PARAM_DEFERRED:
1878 case EFFECT_CMD_SET_PARAM_COMMIT:
1879 case EFFECT_CMD_GET_PARAM:
1880 break;
1881 default:
1882 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1883 break;
1884 }
1885 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001886 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001887 }
1888
Eric Laurent1ffc5852016-12-15 14:46:09 -08001889 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001891 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001893 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001894 writeToBuffer(NO_ERROR, response);
1895 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001896 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001898 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001900 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 writeToBuffer(NO_ERROR, response);
1902 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001903 }
1904
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001905 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001906 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001907 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001909 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001910 // only get parameter command is permitted for applications not controlling the effect
1911 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001913 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001914
1915 // handle commands that are not forwarded transparently to effect engine
1916 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001917 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001918 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001919 }
1920
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001922 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001923 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001924 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001925 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001926
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 // No need to trylock() here as this function is executed in the binder thread serving a
1928 // particular client process: no risk to block the whole media server process or mixer
1929 // threads if we are stuck here
1930 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001931 // keep local copy of index in case of client corruption b/32220769
1932 const uint32_t clientIndex = mCblk->clientIndex;
1933 const uint32_t serverIndex = mCblk->serverIndex;
1934 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1935 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001936 mCblk->serverIndex = 0;
1937 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001938 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001939 }
1940 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001941 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001942 for (uint32_t index = serverIndex; index < clientIndex;) {
1943 int *p = (int *)(mBuffer + index);
1944 const int size = *p++;
1945 if (size < 0
1946 || size > EFFECT_PARAM_BUFFER_SIZE
1947 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001949 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001950 break;
1951 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001952
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001953 std::copy(reinterpret_cast<const uint8_t*>(p),
1954 reinterpret_cast<const uint8_t*>(p) + size,
1955 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001956
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001957 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001958 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001959 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001960 sizeof(int),
1961 &replyBuffer);
1962 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001963
1964 // verify shared memory: server index shouldn't change; client index can't go back.
1965 if (serverIndex != mCblk->serverIndex
1966 || clientIndex > mCblk->clientIndex) {
1967 android_errorWriteLog(0x534e4554, "32220769");
1968 status = BAD_VALUE;
1969 break;
1970 }
1971
Eric Laurentca7cc822012-11-19 14:55:58 -08001972 // stop at first error encountered
1973 if (ret != NO_ERROR) {
1974 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001975 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001976 break;
1977 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001978 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001979 break;
1980 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001981 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001982 }
1983 mCblk->serverIndex = 0;
1984 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001985 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 }
1987
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001988 status_t status = effect->command(cmdCode,
1989 cmdData,
1990 maxResponseSize,
1991 response);
1992 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001993}
1994
1995void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1996{
1997 ALOGV("setControl %p control %d", this, hasControl);
1998
1999 mHasControl = hasControl;
2000 mEnabled = enabled;
2001
2002 if (signal && mEffectClient != 0) {
2003 mEffectClient->controlStatusChanged(hasControl);
2004 }
2005}
2006
2007void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002008 const std::vector<uint8_t>& cmdData,
2009 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002010{
2011 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002012 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002013 }
2014}
2015
2016
2017
2018void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2019{
2020 if (mEffectClient != 0) {
2021 mEffectClient->enableStatusChanged(enabled);
2022 }
2023}
2024
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002025void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002026{
2027 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2028
Marco Nelissenb2208842014-02-07 14:00:50 -08002029 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002030 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002031 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002032 mHasControl ? "yes" : "no",
2033 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002034 mCblk ? mCblk->clientIndex : 0,
2035 mCblk ? mCblk->serverIndex : 0
2036 );
2037
2038 if (locked) {
2039 mCblk->lock.unlock();
2040 }
2041}
2042
2043#undef LOG_TAG
2044#define LOG_TAG "AudioFlinger::EffectChain"
2045
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002046AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2047 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002048 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002049 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002050 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002051 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002052{
2053 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002054 sp<ThreadBase> p = thread.promote();
2055 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002056 return;
2057 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002058 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2059 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002060}
2061
2062AudioFlinger::EffectChain::~EffectChain()
2063{
Eric Laurentca7cc822012-11-19 14:55:58 -08002064}
2065
2066// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2067sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2068 effect_descriptor_t *descriptor)
2069{
2070 size_t size = mEffects.size();
2071
2072 for (size_t i = 0; i < size; i++) {
2073 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2074 return mEffects[i];
2075 }
2076 }
2077 return 0;
2078}
2079
2080// getEffectFromId_l() must be called with ThreadBase::mLock held
2081sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2082{
2083 size_t size = mEffects.size();
2084
2085 for (size_t i = 0; i < size; i++) {
2086 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2087 if (id == 0 || mEffects[i]->id() == id) {
2088 return mEffects[i];
2089 }
2090 }
2091 return 0;
2092}
2093
2094// getEffectFromType_l() must be called with ThreadBase::mLock held
2095sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2096 const effect_uuid_t *type)
2097{
2098 size_t size = mEffects.size();
2099
2100 for (size_t i = 0; i < size; i++) {
2101 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2102 return mEffects[i];
2103 }
2104 }
2105 return 0;
2106}
2107
Eric Laurent6c796322019-04-09 14:13:17 -07002108std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2109{
2110 std::vector<int> ids;
2111 Mutex::Autolock _l(mLock);
2112 for (size_t i = 0; i < mEffects.size(); i++) {
2113 ids.push_back(mEffects[i]->id());
2114 }
2115 return ids;
2116}
2117
Eric Laurentca7cc822012-11-19 14:55:58 -08002118void AudioFlinger::EffectChain::clearInputBuffer()
2119{
2120 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002121 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002122}
2123
2124// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002125void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002126{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002127 if (mInBuffer == NULL) {
2128 return;
2129 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002130 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002131 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002132
Eric Laurent6b446ce2019-12-13 10:56:31 -08002133 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002134 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002135}
2136
2137// Must be called with EffectChain::mLock locked
2138void AudioFlinger::EffectChain::process_l()
2139{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002140 // never process effects when:
2141 // - on an OFFLOAD thread
2142 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002143 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002144 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002145 bool tracksOnSession = (trackCnt() != 0);
2146
2147 if (!tracksOnSession && mTailBufferCount == 0) {
2148 doProcess = false;
2149 }
2150
2151 if (activeTrackCnt() == 0) {
2152 // if no track is active and the effect tail has not been rendered,
2153 // the input buffer must be cleared here as the mixer process will not do it
2154 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002155 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002156 if (mTailBufferCount > 0) {
2157 mTailBufferCount--;
2158 }
2159 }
2160 }
2161 }
2162
2163 size_t size = mEffects.size();
2164 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002165 // Only the input and output buffers of the chain can be external,
2166 // and 'update' / 'commit' do nothing for allocated buffers, thus
2167 // it's not needed to consider any other buffers here.
2168 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002169 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2170 mOutBuffer->update();
2171 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002172 for (size_t i = 0; i < size; i++) {
2173 mEffects[i]->process();
2174 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002175 mInBuffer->commit();
2176 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2177 mOutBuffer->commit();
2178 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002179 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002180 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002181 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002182 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2183 }
2184 if (doResetVolume) {
2185 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002186 }
2187}
2188
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002189// createEffect_l() must be called with ThreadBase::mLock held
2190status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002191 effect_descriptor_t *desc,
2192 int id,
2193 audio_session_t sessionId,
2194 bool pinned)
2195{
2196 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002197 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002198 status_t lStatus = effect->status();
2199 if (lStatus == NO_ERROR) {
2200 lStatus = addEffect_ll(effect);
2201 }
2202 if (lStatus != NO_ERROR) {
2203 effect.clear();
2204 }
2205 return lStatus;
2206}
2207
2208// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002209status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2210{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002211 Mutex::Autolock _l(mLock);
2212 return addEffect_ll(effect);
2213}
2214// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2215status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2216{
Eric Laurentca7cc822012-11-19 14:55:58 -08002217 effect_descriptor_t desc = effect->desc();
2218 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2219
Eric Laurent6b446ce2019-12-13 10:56:31 -08002220 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002221
2222 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2223 // Auxiliary effects are inserted at the beginning of mEffects vector as
2224 // they are processed first and accumulated in chain input buffer
2225 mEffects.insertAt(effect, 0);
2226
2227 // the input buffer for auxiliary effect contains mono samples in
2228 // 32 bit format. This is to avoid saturation in AudoMixer
2229 // accumulation stage. Saturation is done in EffectModule::process() before
2230 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002231 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002232 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002233#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002234 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002235 numSamples * sizeof(float), &halBuffer);
2236#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002237 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002238 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002239#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002240 if (result != OK) return result;
2241 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002242 // auxiliary effects output samples to chain input buffer for further processing
2243 // by insert effects
2244 effect->setOutBuffer(mInBuffer);
2245 } else {
2246 // Insert effects are inserted at the end of mEffects vector as they are processed
2247 // after track and auxiliary effects.
2248 // Insert effect order as a function of indicated preference:
2249 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2250 // another effect is present
2251 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2252 // last effect claiming first position
2253 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2254 // first effect claiming last position
2255 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2256 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2257 // already present
2258
2259 size_t size = mEffects.size();
2260 size_t idx_insert = size;
2261 ssize_t idx_insert_first = -1;
2262 ssize_t idx_insert_last = -1;
2263
2264 for (size_t i = 0; i < size; i++) {
2265 effect_descriptor_t d = mEffects[i]->desc();
2266 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2267 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2268 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2269 // check invalid effect chaining combinations
2270 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2271 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2272 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2273 desc.name, d.name);
2274 return INVALID_OPERATION;
2275 }
2276 // remember position of first insert effect and by default
2277 // select this as insert position for new effect
2278 if (idx_insert == size) {
2279 idx_insert = i;
2280 }
2281 // remember position of last insert effect claiming
2282 // first position
2283 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2284 idx_insert_first = i;
2285 }
2286 // remember position of first insert effect claiming
2287 // last position
2288 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2289 idx_insert_last == -1) {
2290 idx_insert_last = i;
2291 }
2292 }
2293 }
2294
2295 // modify idx_insert from first position if needed
2296 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2297 if (idx_insert_last != -1) {
2298 idx_insert = idx_insert_last;
2299 } else {
2300 idx_insert = size;
2301 }
2302 } else {
2303 if (idx_insert_first != -1) {
2304 idx_insert = idx_insert_first + 1;
2305 }
2306 }
2307
2308 // always read samples from chain input buffer
2309 effect->setInBuffer(mInBuffer);
2310
2311 // if last effect in the chain, output samples to chain
2312 // output buffer, otherwise to chain input buffer
2313 if (idx_insert == size) {
2314 if (idx_insert != 0) {
2315 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2316 mEffects[idx_insert-1]->configure();
2317 }
2318 effect->setOutBuffer(mOutBuffer);
2319 } else {
2320 effect->setOutBuffer(mInBuffer);
2321 }
2322 mEffects.insertAt(effect, idx_insert);
2323
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002324 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002325 idx_insert);
2326 }
2327 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002328
Eric Laurentca7cc822012-11-19 14:55:58 -08002329 return NO_ERROR;
2330}
2331
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002332// removeEffect_l() must be called with ThreadBase::mLock held
2333size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2334 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002335{
2336 Mutex::Autolock _l(mLock);
2337 size_t size = mEffects.size();
2338 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2339
2340 for (size_t i = 0; i < size; i++) {
2341 if (effect == mEffects[i]) {
2342 // calling stop here will remove pre-processing effect from the audio HAL.
2343 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2344 // the middle of a read from audio HAL
2345 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2346 mEffects[i]->state() == EffectModule::STOPPING) {
2347 mEffects[i]->stop();
2348 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002349 if (release) {
2350 mEffects[i]->release_l();
2351 }
2352
Mikhail Naganov022b9952017-01-04 16:36:51 -08002353 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002354 if (i == size - 1 && i != 0) {
2355 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2356 mEffects[i - 1]->configure();
2357 }
2358 }
2359 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002360 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002361 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002362
Eric Laurentca7cc822012-11-19 14:55:58 -08002363 break;
2364 }
2365 }
2366
2367 return mEffects.size();
2368}
2369
jiabin8f278ee2019-11-11 12:16:27 -08002370// setDevices_l() must be called with ThreadBase::mLock held
2371void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002372{
2373 size_t size = mEffects.size();
2374 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002375 mEffects[i]->setDevices(devices);
2376 }
2377}
2378
2379// setInputDevice_l() must be called with ThreadBase::mLock held
2380void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2381{
2382 size_t size = mEffects.size();
2383 for (size_t i = 0; i < size; i++) {
2384 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002385 }
2386}
2387
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002388// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002389void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2390{
2391 size_t size = mEffects.size();
2392 for (size_t i = 0; i < size; i++) {
2393 mEffects[i]->setMode(mode);
2394 }
2395}
2396
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002397// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002398void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2399{
2400 size_t size = mEffects.size();
2401 for (size_t i = 0; i < size; i++) {
2402 mEffects[i]->setAudioSource(source);
2403 }
2404}
2405
Zhou Songd505c642020-02-20 16:35:37 +08002406bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2407 for (const auto &effect : mEffects) {
2408 if (effect->isVolumeControlEnabled()) return true;
2409 }
2410 return false;
2411}
2412
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002413// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002414bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002415{
2416 uint32_t newLeft = *left;
2417 uint32_t newRight = *right;
2418 bool hasControl = false;
2419 int ctrlIdx = -1;
2420 size_t size = mEffects.size();
2421
2422 // first update volume controller
2423 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002424 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002425 ctrlIdx = i - 1;
2426 hasControl = true;
2427 break;
2428 }
2429 }
2430
Eric Laurentfa1e1232016-08-02 19:01:49 -07002431 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002432 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002433 if (hasControl) {
2434 *left = mNewLeftVolume;
2435 *right = mNewRightVolume;
2436 }
2437 return hasControl;
2438 }
2439
2440 mVolumeCtrlIdx = ctrlIdx;
2441 mLeftVolume = newLeft;
2442 mRightVolume = newRight;
2443
2444 // second get volume update from volume controller
2445 if (ctrlIdx >= 0) {
2446 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2447 mNewLeftVolume = newLeft;
2448 mNewRightVolume = newRight;
2449 }
2450 // then indicate volume to all other effects in chain.
2451 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002452 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002453 uint32_t lVol = newLeft;
2454 uint32_t rVol = newRight;
2455
2456 for (size_t i = 0; i < size; i++) {
2457 if ((int)i == ctrlIdx) {
2458 continue;
2459 }
2460 // this also works for ctrlIdx == -1 when there is no volume controller
2461 if ((int)i > ctrlIdx) {
2462 lVol = *left;
2463 rVol = *right;
2464 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002465 // Pass requested volume directly if this is volume monitor module
2466 if (mEffects[i]->isVolumeMonitor()) {
2467 mEffects[i]->setVolume(left, right, false);
2468 } else {
2469 mEffects[i]->setVolume(&lVol, &rVol, false);
2470 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002471 }
2472 *left = newLeft;
2473 *right = newRight;
2474
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002475 setVolumeForOutput_l(*left, *right);
2476
Eric Laurentca7cc822012-11-19 14:55:58 -08002477 return hasControl;
2478}
2479
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002480// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002481void AudioFlinger::EffectChain::resetVolume_l()
2482{
Eric Laurente7449bf2016-08-03 18:44:07 -07002483 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2484 uint32_t left = mLeftVolume;
2485 uint32_t right = mRightVolume;
2486 (void)setVolume_l(&left, &right, true);
2487 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002488}
2489
jiabineb3bda02020-06-30 14:07:03 -07002490// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2491bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2492{
2493 for (size_t i = 0; i < mEffects.size(); ++i) {
2494 if (mEffects[i]->isHapticGenerator()) {
2495 return true;
2496 }
2497 }
2498 return false;
2499}
2500
jiabine70bc7f2020-06-30 22:07:55 -07002501void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2502{
2503 Mutex::Autolock _l(mLock);
2504 for (size_t i = 0; i < mEffects.size(); ++i) {
2505 mEffects[i]->setHapticIntensity(id, intensity);
2506 }
2507}
2508
Eric Laurent1b928682014-10-02 19:41:47 -07002509void AudioFlinger::EffectChain::syncHalEffectsState()
2510{
2511 Mutex::Autolock _l(mLock);
2512 for (size_t i = 0; i < mEffects.size(); i++) {
2513 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2514 mEffects[i]->state() == EffectModule::STOPPING) {
2515 mEffects[i]->addEffectToHal_l();
2516 }
2517 }
2518}
2519
Eric Laurentca7cc822012-11-19 14:55:58 -08002520void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2521{
Eric Laurentca7cc822012-11-19 14:55:58 -08002522 String8 result;
2523
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002524 const size_t numEffects = mEffects.size();
2525 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002526
Marco Nelissenb2208842014-02-07 14:00:50 -08002527 if (numEffects) {
2528 bool locked = AudioFlinger::dumpTryLock(mLock);
2529 // failed to lock - AudioFlinger is probably deadlocked
2530 if (!locked) {
2531 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002532 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002533
Andy Hungbded9c82017-11-30 18:47:35 -08002534 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2535 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2536 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2537 (int)inBufferStr.size(), "In buffer ",
2538 (int)outBufferStr.size(), "Out buffer ");
2539 result.appendFormat("\t%s %s %d\n",
2540 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002541 write(fd, result.string(), result.size());
2542
2543 for (size_t i = 0; i < numEffects; ++i) {
2544 sp<EffectModule> effect = mEffects[i];
2545 if (effect != 0) {
2546 effect->dump(fd, args);
2547 }
2548 }
2549
2550 if (locked) {
2551 mLock.unlock();
2552 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002553 } else {
2554 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002555 }
2556}
2557
2558// must be called with ThreadBase::mLock held
2559void AudioFlinger::EffectChain::setEffectSuspended_l(
2560 const effect_uuid_t *type, bool suspend)
2561{
2562 sp<SuspendedEffectDesc> desc;
2563 // use effect type UUID timelow as key as there is no real risk of identical
2564 // timeLow fields among effect type UUIDs.
2565 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2566 if (suspend) {
2567 if (index >= 0) {
2568 desc = mSuspendedEffects.valueAt(index);
2569 } else {
2570 desc = new SuspendedEffectDesc();
2571 desc->mType = *type;
2572 mSuspendedEffects.add(type->timeLow, desc);
2573 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2574 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002575
Eric Laurentca7cc822012-11-19 14:55:58 -08002576 if (desc->mRefCount++ == 0) {
2577 sp<EffectModule> effect = getEffectIfEnabled(type);
2578 if (effect != 0) {
2579 desc->mEffect = effect;
2580 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002581 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002582 }
2583 }
2584 } else {
2585 if (index < 0) {
2586 return;
2587 }
2588 desc = mSuspendedEffects.valueAt(index);
2589 if (desc->mRefCount <= 0) {
2590 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002591 desc->mRefCount = 0;
2592 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002593 }
2594 if (--desc->mRefCount == 0) {
2595 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2596 if (desc->mEffect != 0) {
2597 sp<EffectModule> effect = desc->mEffect.promote();
2598 if (effect != 0) {
2599 effect->setSuspended(false);
2600 effect->lock();
2601 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002602 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002603 effect->setEnabled_l(handle->enabled());
2604 }
2605 effect->unlock();
2606 }
2607 desc->mEffect.clear();
2608 }
2609 mSuspendedEffects.removeItemsAt(index);
2610 }
2611 }
2612}
2613
2614// must be called with ThreadBase::mLock held
2615void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2616{
2617 sp<SuspendedEffectDesc> desc;
2618
2619 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2620 if (suspend) {
2621 if (index >= 0) {
2622 desc = mSuspendedEffects.valueAt(index);
2623 } else {
2624 desc = new SuspendedEffectDesc();
2625 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2626 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2627 }
2628 if (desc->mRefCount++ == 0) {
2629 Vector< sp<EffectModule> > effects;
2630 getSuspendEligibleEffects(effects);
2631 for (size_t i = 0; i < effects.size(); i++) {
2632 setEffectSuspended_l(&effects[i]->desc().type, true);
2633 }
2634 }
2635 } else {
2636 if (index < 0) {
2637 return;
2638 }
2639 desc = mSuspendedEffects.valueAt(index);
2640 if (desc->mRefCount <= 0) {
2641 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2642 desc->mRefCount = 1;
2643 }
2644 if (--desc->mRefCount == 0) {
2645 Vector<const effect_uuid_t *> types;
2646 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2647 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2648 continue;
2649 }
2650 types.add(&mSuspendedEffects.valueAt(i)->mType);
2651 }
2652 for (size_t i = 0; i < types.size(); i++) {
2653 setEffectSuspended_l(types[i], false);
2654 }
2655 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2656 mSuspendedEffects.keyAt(index));
2657 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2658 }
2659 }
2660}
2661
2662
2663// The volume effect is used for automated tests only
2664#ifndef OPENSL_ES_H_
2665static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2666 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2667const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2668#endif //OPENSL_ES_H_
2669
Eric Laurentd8365c52017-07-16 15:27:05 -07002670/* static */
2671bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2672{
2673 // Only NS and AEC are suspended when BtNRec is off
2674 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2675 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2676 return true;
2677 }
2678 return false;
2679}
2680
Eric Laurentca7cc822012-11-19 14:55:58 -08002681bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2682{
2683 // auxiliary effects and visualizer are never suspended on output mix
2684 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2685 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2686 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002687 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2688 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002689 return false;
2690 }
2691 return true;
2692}
2693
2694void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2695 Vector< sp<AudioFlinger::EffectModule> > &effects)
2696{
2697 effects.clear();
2698 for (size_t i = 0; i < mEffects.size(); i++) {
2699 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2700 effects.add(mEffects[i]);
2701 }
2702 }
2703}
2704
2705sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2706 const effect_uuid_t *type)
2707{
2708 sp<EffectModule> effect = getEffectFromType_l(type);
2709 return effect != 0 && effect->isEnabled() ? effect : 0;
2710}
2711
2712void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2713 bool enabled)
2714{
2715 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2716 if (enabled) {
2717 if (index < 0) {
2718 // if the effect is not suspend check if all effects are suspended
2719 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2720 if (index < 0) {
2721 return;
2722 }
2723 if (!isEffectEligibleForSuspend(effect->desc())) {
2724 return;
2725 }
2726 setEffectSuspended_l(&effect->desc().type, enabled);
2727 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2728 if (index < 0) {
2729 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2730 return;
2731 }
2732 }
2733 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2734 effect->desc().type.timeLow);
2735 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002736 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002737 if (desc->mEffect == 0) {
2738 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002739 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002740 effect->setSuspended(true);
2741 }
2742 } else {
2743 if (index < 0) {
2744 return;
2745 }
2746 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2747 effect->desc().type.timeLow);
2748 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2749 desc->mEffect.clear();
2750 effect->setSuspended(false);
2751 }
2752}
2753
Eric Laurent5baf2af2013-09-12 17:37:00 -07002754bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002755{
2756 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002757 return isNonOffloadableEnabled_l();
2758}
2759
2760bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2761{
Eric Laurent813e2a72013-08-31 12:59:48 -07002762 size_t size = mEffects.size();
2763 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002764 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002765 return true;
2766 }
2767 }
2768 return false;
2769}
2770
Eric Laurentaaa44472014-09-12 17:41:50 -07002771void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2772{
2773 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002774 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002775}
2776
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002777void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2778{
2779 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2780 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2781 }
2782 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2783 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2784 }
2785}
2786
2787void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2788{
2789 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2790 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2791 }
2792 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2793 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2794 }
2795}
2796
2797bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002798{
2799 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002800 for (const auto &effect : mEffects) {
2801 if (effect->isProcessImplemented()) {
2802 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002803 }
2804 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002805 // Allow effects without processing.
2806 return true;
2807}
2808
2809bool AudioFlinger::EffectChain::isFastCompatible() const
2810{
2811 Mutex::Autolock _l(mLock);
2812 for (const auto &effect : mEffects) {
2813 if (effect->isProcessImplemented()
2814 && effect->isImplementationSoftware()) {
2815 return false;
2816 }
2817 }
2818 // Allow effects without processing or hw accelerated effects.
2819 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002820}
2821
2822// isCompatibleWithThread_l() must be called with thread->mLock held
2823bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2824{
2825 Mutex::Autolock _l(mLock);
2826 for (size_t i = 0; i < mEffects.size(); i++) {
2827 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2828 return false;
2829 }
2830 }
2831 return true;
2832}
2833
Eric Laurent6b446ce2019-12-13 10:56:31 -08002834// EffectCallbackInterface implementation
2835status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2836 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2837 sp<EffectHalInterface> *effect) {
2838 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002839 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002840 if (effectsFactory != 0) {
2841 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2842 }
2843 return status;
2844}
2845
2846bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002847 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002848 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002849 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002850}
2851
2852status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2853 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002854 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002855}
2856
2857status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2858 sp<EffectHalInterface> effect) {
2859 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002860 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002861 if (t == nullptr) {
2862 return result;
2863 }
2864 sp <StreamHalInterface> st = t->stream();
2865 if (st == nullptr) {
2866 return result;
2867 }
2868 result = st->addEffect(effect);
2869 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2870 return result;
2871}
2872
2873status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2874 sp<EffectHalInterface> effect) {
2875 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002876 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002877 if (t == nullptr) {
2878 return result;
2879 }
2880 sp <StreamHalInterface> st = t->stream();
2881 if (st == nullptr) {
2882 return result;
2883 }
2884 result = st->removeEffect(effect);
2885 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2886 return result;
2887}
2888
2889audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002890 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002891 if (t == nullptr) {
2892 return AUDIO_IO_HANDLE_NONE;
2893 }
2894 return t->id();
2895}
2896
2897bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002898 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002899 if (t == nullptr) {
2900 return true;
2901 }
2902 return t->isOutput();
2903}
2904
2905bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002906 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002907 if (t == nullptr) {
2908 return false;
2909 }
2910 return t->type() == ThreadBase::OFFLOAD;
2911}
2912
2913bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002914 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002915 if (t == nullptr) {
2916 return false;
2917 }
2918 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2919}
2920
2921bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002922 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002923 if (t == nullptr) {
2924 return false;
2925 }
Andy Hungea840382020-05-05 21:50:17 -07002926 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002927}
2928
2929uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002930 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002931 if (t == nullptr) {
2932 return 0;
2933 }
2934 return t->sampleRate();
2935}
2936
2937audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002938 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002939 if (t == nullptr) {
2940 return AUDIO_CHANNEL_NONE;
2941 }
2942 return t->channelMask();
2943}
2944
2945uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002946 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002947 if (t == nullptr) {
2948 return 0;
2949 }
2950 return t->channelCount();
2951}
2952
jiabineb3bda02020-06-30 14:07:03 -07002953audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002954 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07002955 if (t == nullptr) {
2956 return AUDIO_CHANNEL_NONE;
2957 }
2958 return t->hapticChannelMask();
2959}
2960
Eric Laurent6b446ce2019-12-13 10:56:31 -08002961size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002962 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002963 if (t == nullptr) {
2964 return 0;
2965 }
2966 return t->frameCount();
2967}
2968
2969uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08002970 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002971 if (t == nullptr) {
2972 return 0;
2973 }
2974 return t->latency_l();
2975}
2976
2977void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08002978 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002979 if (t == nullptr) {
2980 return;
2981 }
2982 t->setVolumeForOutput_l(left, right);
2983}
2984
2985void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002986 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08002987 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002988 if (t == nullptr) {
2989 return;
2990 }
2991 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2992
Andy Hung328d6772021-01-12 12:32:21 -08002993 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002994 if (c == nullptr) {
2995 return;
2996 }
Eric Laurent41709552019-12-16 19:34:05 -08002997 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2998 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002999}
3000
Eric Laurent41709552019-12-16 19:34:05 -08003001void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003002 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003003 if (t == nullptr) {
3004 return;
3005 }
Eric Laurent41709552019-12-16 19:34:05 -08003006 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3007 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003008}
3009
Eric Laurent41709552019-12-16 19:34:05 -08003010void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003011 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3012
Andy Hung328d6772021-01-12 12:32:21 -08003013 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003014 if (t == nullptr) {
3015 return;
3016 }
3017 t->onEffectDisable();
3018}
3019
3020bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3021 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003022 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003023 if (t == nullptr) {
3024 return false;
3025 }
3026 t->disconnectEffectHandle(handle, unpinIfLast);
3027 return true;
3028}
3029
3030void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003031 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003032 if (c == nullptr) {
3033 return;
3034 }
3035 c->resetVolume_l();
3036
3037}
3038
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003039product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003040 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003041 if (c == nullptr) {
3042 return PRODUCT_STRATEGY_NONE;
3043 }
3044 return c->strategy();
3045}
3046
3047int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003048 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (c == nullptr) {
3050 return 0;
3051 }
3052 return c->activeTrackCnt();
3053}
3054
Eric Laurentb82e6b72019-11-22 17:25:04 -08003055
3056#undef LOG_TAG
3057#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3058
3059status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3060{
3061 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3062 Mutex::Autolock _l(mProxyLock);
3063 if (status == NO_ERROR) {
3064 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003065 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003066 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003067 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003068 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003069 bs = handle.second->disable(&status);
3070 }
3071 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003072 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003073 }
3074 }
3075 }
3076 ALOGV("%s enable %d status %d", __func__, enabled, status);
3077 return status;
3078}
3079
3080status_t AudioFlinger::DeviceEffectProxy::init(
3081 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3082//For all audio patches
3083//If src or sink device match
3084//If the effect is HW accelerated
3085// if no corresponding effect module
3086// Create EffectModule: mHalEffect
3087//Create and attach EffectHandle
3088//If the effect is not HW accelerated and the patch sink or src is a mixer port
3089// Create Effect on patch input or output thread on session -1
3090//Add EffectHandle to EffectHandle map of Effect Proxy:
3091 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3092 status_t status = NO_ERROR;
3093 for (auto &patch : patches) {
3094 status = onCreatePatch(patch.first, patch.second);
3095 ALOGV("%s onCreatePatch status %d", __func__, status);
3096 if (status == BAD_VALUE) {
3097 return status;
3098 }
3099 }
3100 return status;
3101}
3102
3103status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3104 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3105 status_t status = NAME_NOT_FOUND;
3106 sp<EffectHandle> handle;
3107 // only consider source[0] as this is the only "true" source of a patch
3108 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3109 ALOGV("%s source checkPort status %d", __func__, status);
3110 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3111 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3112 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3113 }
3114 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3115 Mutex::Autolock _l(mProxyLock);
3116 mEffectHandles.emplace(patchHandle, handle);
3117 }
3118 ALOGW_IF(status == BAD_VALUE,
3119 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3120
3121 return status;
3122}
3123
3124status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3125 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3126
3127 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3128 __func__, port->type, port->ext.device.type,
3129 port->ext.device.address, port->id, patch.isSoftware());
3130 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003131 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003132 return NAME_NOT_FOUND;
3133 }
3134 status_t status = NAME_NOT_FOUND;
3135
3136 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3137 Mutex::Autolock _l(mProxyLock);
3138 mDevicePort = *port;
3139 mHalEffect = new EffectModule(mMyCallback,
3140 const_cast<effect_descriptor_t *>(&mDescriptor),
3141 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3142 false /* pinned */, port->id);
3143 if (audio_is_input_device(mDevice.mType)) {
3144 mHalEffect->setInputDevice(mDevice);
3145 } else {
3146 mHalEffect->setDevices({mDevice});
3147 }
3148 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3149 status = (*handle)->initCheck();
3150 if (status == OK) {
3151 status = mHalEffect->addHandle((*handle).get());
3152 } else {
3153 mHalEffect.clear();
3154 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3155 }
3156 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3157 sp <ThreadBase> thread;
3158 if (audio_port_config_has_input_direction(port)) {
3159 if (patch.isSoftware()) {
3160 thread = patch.mRecord.thread();
3161 } else {
3162 thread = patch.thread().promote();
3163 }
3164 } else {
3165 if (patch.isSoftware()) {
3166 thread = patch.mPlayback.thread();
3167 } else {
3168 thread = patch.thread().promote();
3169 }
3170 }
3171 int enabled;
3172 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3173 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003174 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003175 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3176 } else {
3177 status = BAD_VALUE;
3178 }
3179 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003180 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003181 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003182 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003183 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003184 bs = (*handle)->disable(&status);
3185 }
3186 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003187 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003188 }
3189 }
3190 return status;
3191}
3192
3193void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3194 Mutex::Autolock _l(mProxyLock);
3195 mEffectHandles.erase(patchHandle);
3196}
3197
3198
3199size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3200{
3201 Mutex::Autolock _l(mProxyLock);
3202 if (effect == mHalEffect) {
3203 mHalEffect.clear();
3204 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3205 }
3206 return mHalEffect == nullptr ? 0 : 1;
3207}
3208
3209status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3210 sp<EffectHalInterface> effect) {
3211 if (mHalEffect == nullptr) {
3212 return NO_INIT;
3213 }
3214 return mManagerCallback->addEffectToHal(
3215 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3216}
3217
3218status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3219 sp<EffectHalInterface> effect) {
3220 if (mHalEffect == nullptr) {
3221 return NO_INIT;
3222 }
3223 return mManagerCallback->removeEffectFromHal(
3224 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3225}
3226
3227bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3228 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3229 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3230 }
3231 return true;
3232}
3233
3234uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3235 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3236 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3237 return mDevicePort.sample_rate;
3238 }
3239 return DEFAULT_OUTPUT_SAMPLE_RATE;
3240}
3241
3242audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3243 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3244 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3245 return mDevicePort.channel_mask;
3246 }
3247 return AUDIO_CHANNEL_OUT_STEREO;
3248}
3249
3250uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3251 if (isOutput()) {
3252 return audio_channel_count_from_out_mask(channelMask());
3253 }
3254 return audio_channel_count_from_in_mask(channelMask());
3255}
3256
3257void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3258 const Vector<String16> args;
3259 EffectBase::dump(fd, args);
3260
3261 const bool locked = dumpTryLock(mProxyLock);
3262 if (!locked) {
3263 String8 result("DeviceEffectProxy may be deadlocked\n");
3264 write(fd, result.string(), result.size());
3265 }
3266
3267 String8 outStr;
3268 if (mHalEffect != nullptr) {
3269 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3270 } else {
3271 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3272 }
3273 write(fd, outStr.string(), outStr.size());
3274 outStr.clear();
3275
3276 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3277 write(fd, outStr.string(), outStr.size());
3278 outStr.clear();
3279
3280 for (const auto& iter : mEffectHandles) {
3281 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3282 write(fd, outStr.string(), outStr.size());
3283 outStr.clear();
3284 sp<EffectBase> effect = iter.second->effect().promote();
3285 if (effect != nullptr) {
3286 effect->dump(fd, args);
3287 }
3288 }
3289
3290 if (locked) {
3291 mLock.unlock();
3292 }
3293}
3294
3295#undef LOG_TAG
3296#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3297
3298int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3299 return mManagerCallback->newEffectId();
3300}
3301
3302
3303bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3304 EffectHandle *handle, bool unpinIfLast) {
3305 sp<EffectBase> effectBase = handle->effect().promote();
3306 if (effectBase == nullptr) {
3307 return false;
3308 }
3309
3310 sp<EffectModule> effect = effectBase->asEffectModule();
3311 if (effect == nullptr) {
3312 return false;
3313 }
3314
3315 // restore suspended effects if the disconnected handle was enabled and the last one.
3316 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3317 if (remove) {
3318 sp<DeviceEffectProxy> proxy = mProxy.promote();
3319 if (proxy != nullptr) {
3320 proxy->removeEffect(effect);
3321 }
3322 if (handle->enabled()) {
3323 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3324 }
3325 }
3326 return true;
3327}
3328
3329status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3330 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3331 sp<EffectHalInterface> *effect) {
3332 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3333}
3334
3335status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3336 sp<EffectHalInterface> effect) {
3337 sp<DeviceEffectProxy> proxy = mProxy.promote();
3338 if (proxy == nullptr) {
3339 return NO_INIT;
3340 }
3341 return proxy->addEffectToHal(effect);
3342}
3343
3344status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3345 sp<EffectHalInterface> effect) {
3346 sp<DeviceEffectProxy> proxy = mProxy.promote();
3347 if (proxy == nullptr) {
3348 return NO_INIT;
3349 }
3350 return proxy->addEffectToHal(effect);
3351}
3352
3353bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3354 sp<DeviceEffectProxy> proxy = mProxy.promote();
3355 if (proxy == nullptr) {
3356 return true;
3357 }
3358 return proxy->isOutput();
3359}
3360
3361uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3362 sp<DeviceEffectProxy> proxy = mProxy.promote();
3363 if (proxy == nullptr) {
3364 return DEFAULT_OUTPUT_SAMPLE_RATE;
3365 }
3366 return proxy->sampleRate();
3367}
3368
3369audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3370 sp<DeviceEffectProxy> proxy = mProxy.promote();
3371 if (proxy == nullptr) {
3372 return AUDIO_CHANNEL_OUT_STEREO;
3373 }
3374 return proxy->channelMask();
3375}
3376
3377uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3378 sp<DeviceEffectProxy> proxy = mProxy.promote();
3379 if (proxy == nullptr) {
3380 return 2;
3381 }
3382 return proxy->channelCount();
3383}
3384
Glenn Kasten63238ef2015-03-02 15:50:29 -08003385} // namespace android