blob: d75b13bedf96c32def1c155eb876b8f968b6799b [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) {
155 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
156 } else {
157 mCallback->onEffectEnable(this);
158 }
159 } else {
160 mCallback->onEffectDisable(this);
161 }
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) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800250 io = mCallback->io();
251 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700252 }
253 }
254 // enable effect when registered according to enable state requested by controlling handle
255 if (mHandles.size() > 0) {
256 EffectHandle *handle = controlHandle_l();
257 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
258 doEnable = true;
259 mPolicyEnabled = handle->enabled();
260 }
261 }
262 registered = mPolicyRegistered;
263 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100264 // The simultaneous release of two EffectHandles with the same EffectModule
265 // may cause us to call this method at the same time.
266 // This may deadlock under some circumstances (b/180941720). Avoid this.
267 if (!doRegister && !(registered && doEnable)) {
268 return NO_ERROR;
269 }
Eric Laurent6c796322019-04-09 14:13:17 -0700270 mPolicyLock.lock();
271 }
272 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
273 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
274 if (doRegister) {
275 if (registered) {
276 status = AudioSystem::registerEffect(
277 &mDescriptor,
278 io,
279 strategy,
280 mSessionId,
281 mId);
282 } else {
283 status = AudioSystem::unregisterEffect(mId);
284 }
285 }
286 if (registered && doEnable) {
287 status = AudioSystem::setEffectEnabled(mId, enabled);
288 }
289 mPolicyLock.unlock();
290
291 return status;
292}
293
294
Eric Laurent41709552019-12-16 19:34:05 -0800295ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800296{
297 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800298 return removeHandle_l(handle);
299}
300
Eric Laurent41709552019-12-16 19:34:05 -0800301ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800302{
Eric Laurentca7cc822012-11-19 14:55:58 -0800303 size_t size = mHandles.size();
304 size_t i;
305 for (i = 0; i < size; i++) {
306 if (mHandles[i] == handle) {
307 break;
308 }
309 }
310 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800311 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
312 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800313 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800314 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800315
316 mHandles.removeAt(i);
317 // if removed from first place, move effect control from this handle to next in line
318 if (i == 0) {
319 EffectHandle *h = controlHandle_l();
320 if (h != NULL) {
321 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
322 }
323 }
324
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530325 // Prevent calls to process() and other functions on effect interface from now on.
326 // The effect engine will be released by the destructor when the last strong reference on
327 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800328 if (mHandles.size() == 0 && !mPinned) {
329 mState = DESTROYED;
330 }
331
332 return mHandles.size();
333}
334
335// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800336AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800337{
338 // the first valid handle in the list has control over the module
339 for (size_t i = 0; i < mHandles.size(); i++) {
340 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800341 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800342 return h;
343 }
344 }
345
346 return NULL;
347}
348
Eric Laurentf10c7092016-12-06 17:09:56 -0800349// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800350ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800351{
352 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800353 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
354 return mHandles.size();
355 }
356
Eric Laurentf10c7092016-12-06 17:09:56 -0800357 Mutex::Autolock _l(mLock);
358 ssize_t numHandles = removeHandle_l(handle);
359 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800360 mLock.unlock();
361 mCallback->updateOrphanEffectChains(this);
362 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800363 }
364 return numHandles;
365}
366
Eric Laurent41709552019-12-16 19:34:05 -0800367bool AudioFlinger::EffectBase::purgeHandles()
368{
369 bool enabled = false;
370 Mutex::Autolock _l(mLock);
371 EffectHandle *handle = controlHandle_l();
372 if (handle != NULL) {
373 enabled = handle->enabled();
374 }
375 mHandles.clear();
376 return enabled;
377}
378
379void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
380 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
381}
382
383static String8 effectFlagsToString(uint32_t flags) {
384 String8 s;
385
386 s.append("conn. mode: ");
387 switch (flags & EFFECT_FLAG_TYPE_MASK) {
388 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
389 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
390 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
391 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
392 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
393 default: s.append("unknown/reserved"); break;
394 }
395 s.append(", ");
396
397 s.append("insert pref: ");
398 switch (flags & EFFECT_FLAG_INSERT_MASK) {
399 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
400 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
401 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
402 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
403 default: s.append("unknown/reserved"); break;
404 }
405 s.append(", ");
406
407 s.append("volume mgmt: ");
408 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
409 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
410 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
411 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
412 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
413 default: s.append("unknown/reserved"); break;
414 }
415 s.append(", ");
416
417 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
418 if (devind) {
419 s.append("device indication: ");
420 switch (devind) {
421 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
422 default: s.append("unknown/reserved"); break;
423 }
424 s.append(", ");
425 }
426
427 s.append("input mode: ");
428 switch (flags & EFFECT_FLAG_INPUT_MASK) {
429 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
430 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
431 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
432 default: s.append("not set"); break;
433 }
434 s.append(", ");
435
436 s.append("output mode: ");
437 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
438 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
439 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
440 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
441 default: s.append("not set"); break;
442 }
443 s.append(", ");
444
445 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
446 if (accel) {
447 s.append("hardware acceleration: ");
448 switch (accel) {
449 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
450 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
451 default: s.append("unknown/reserved"); break;
452 }
453 s.append(", ");
454 }
455
456 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
457 if (modeind) {
458 s.append("mode indication: ");
459 switch (modeind) {
460 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
461 default: s.append("unknown/reserved"); break;
462 }
463 s.append(", ");
464 }
465
466 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
467 if (srcind) {
468 s.append("source indication: ");
469 switch (srcind) {
470 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
471 default: s.append("unknown/reserved"); break;
472 }
473 s.append(", ");
474 }
475
476 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
477 s.append("offloadable, ");
478 }
479
480 int len = s.length();
481 if (s.length() > 2) {
482 (void) s.lockBuffer(len);
483 s.unlockBuffer(len - 2);
484 }
485 return s;
486}
487
488void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
489{
490 String8 result;
491
492 result.appendFormat("\tEffect ID %d:\n", mId);
493
494 bool locked = AudioFlinger::dumpTryLock(mLock);
495 // failed to lock - AudioFlinger is probably deadlocked
496 if (!locked) {
497 result.append("\t\tCould not lock Fx mutex:\n");
498 }
499
500 result.append("\t\tSession State Registered Enabled Suspended:\n");
501 result.appendFormat("\t\t%05d %03d %s %s %s\n",
502 mSessionId, mState, mPolicyRegistered ? "y" : "n",
503 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
504
505 result.append("\t\tDescriptor:\n");
506 char uuidStr[64];
507 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
508 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
509 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
510 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
511 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
512 mDescriptor.apiVersion,
513 mDescriptor.flags,
514 effectFlagsToString(mDescriptor.flags).string());
515 result.appendFormat("\t\t- name: %s\n",
516 mDescriptor.name);
517
518 result.appendFormat("\t\t- implementor: %s\n",
519 mDescriptor.implementor);
520
521 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
522 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
523 char buffer[256];
524 for (size_t i = 0; i < mHandles.size(); ++i) {
525 EffectHandle *handle = mHandles[i];
526 if (handle != NULL && !handle->disconnected()) {
527 handle->dumpToBuffer(buffer, sizeof(buffer));
528 result.append(buffer);
529 }
530 }
531 if (locked) {
532 mLock.unlock();
533 }
534
535 write(fd, result.string(), result.length());
536}
537
538// ----------------------------------------------------------------------------
539// EffectModule implementation
540// ----------------------------------------------------------------------------
541
542#undef LOG_TAG
543#define LOG_TAG "AudioFlinger::EffectModule"
544
545AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
546 effect_descriptor_t *desc,
547 int id,
548 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800549 bool pinned,
550 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800551 : EffectBase(callback, desc, id, sessionId, pinned),
552 // clear mConfig to ensure consistent initial value of buffer framecount
553 // in case buffers are associated by setInBuffer() or setOutBuffer()
554 // prior to configure().
555 mConfig{{}, {}},
556 mStatus(NO_INIT),
557 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
558 mDisableWaitCnt(0), // set by process() and updateState()
559 mOffloaded(false)
560#ifdef FLOAT_EFFECT_CHAIN
561 , mSupportsFloat(false)
562#endif
563{
564 ALOGV("Constructor %p pinned %d", this, pinned);
565 int lStatus;
566
567 // create effect engine from effect factory
568 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800569 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800570 if (mStatus != NO_ERROR) {
571 return;
572 }
573 lStatus = init();
574 if (lStatus < 0) {
575 mStatus = lStatus;
576 goto Error;
577 }
578
579 setOffloaded(callback->isOffload(), callback->io());
580 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
581
582 return;
583Error:
584 mEffectInterface.clear();
585 ALOGV("Constructor Error %d", mStatus);
586}
587
588AudioFlinger::EffectModule::~EffectModule()
589{
590 ALOGV("Destructor %p", this);
591 if (mEffectInterface != 0) {
592 char uuidStr[64];
593 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
594 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
595 this, uuidStr);
596 release_l();
597 }
598
599}
600
Eric Laurentfa1e1232016-08-02 19:01:49 -0700601bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800602 Mutex::Autolock _l(mLock);
603
Eric Laurentfa1e1232016-08-02 19:01:49 -0700604 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 switch (mState) {
606 case RESTART:
607 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700608 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800609
610 case STARTING:
611 // clear auxiliary effect input buffer for next accumulation
612 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
613 memset(mConfig.inputCfg.buffer.raw,
614 0,
615 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
616 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700617 if (start_l() == NO_ERROR) {
618 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700619 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700620 } else {
621 mState = IDLE;
622 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800623 break;
624 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900625 // volume control for offload and direct threads must take effect immediately.
626 if (stop_l() == NO_ERROR
627 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700628 mDisableWaitCnt = mMaxDisableWaitCnt;
629 } else {
630 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
631 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800632 mState = STOPPED;
633 break;
634 case STOPPED:
635 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
636 // turn off sequence.
637 if (--mDisableWaitCnt == 0) {
638 reset_l();
639 mState = IDLE;
640 }
641 break;
642 default: //IDLE , ACTIVE, DESTROYED
643 break;
644 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700645
646 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800647}
648
649void AudioFlinger::EffectModule::process()
650{
651 Mutex::Autolock _l(mLock);
652
Mikhail Naganov022b9952017-01-04 16:36:51 -0800653 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800654 return;
655 }
656
rago94a1ee82017-07-21 15:11:02 -0700657 const uint32_t inChannelCount =
658 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
659 const uint32_t outChannelCount =
660 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
661 const bool auxType =
662 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
663
Andy Hungfa69ca32017-11-30 10:07:53 -0800664 // safeInputOutputSampleCount is 0 if the channel count between input and output
665 // buffers do not match. This prevents automatic accumulation or copying between the
666 // input and output effect buffers without an intermediary effect process.
667 // TODO: consider implementing channel conversion.
668 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700669 mInChannelCountRequested != mOutChannelCountRequested ? 0
670 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800671 mConfig.inputCfg.buffer.frameCount,
672 mConfig.outputCfg.buffer.frameCount);
673 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
674#ifdef FLOAT_EFFECT_CHAIN
675 accumulate_float(
676 mConfig.outputCfg.buffer.f32,
677 mConfig.inputCfg.buffer.f32,
678 safeInputOutputSampleCount);
679#else
680 accumulate_i16(
681 mConfig.outputCfg.buffer.s16,
682 mConfig.inputCfg.buffer.s16,
683 safeInputOutputSampleCount);
684#endif
685 };
686 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
687#ifdef FLOAT_EFFECT_CHAIN
688 memcpy(
689 mConfig.outputCfg.buffer.f32,
690 mConfig.inputCfg.buffer.f32,
691 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
692
693#else
694 memcpy(
695 mConfig.outputCfg.buffer.s16,
696 mConfig.inputCfg.buffer.s16,
697 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
698#endif
699 };
700
Eric Laurentca7cc822012-11-19 14:55:58 -0800701 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700702 int ret;
703 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700704 if (auxType) {
705 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800706 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700707#ifdef FLOAT_EFFECT_CHAIN
708 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800709#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700710 // Do in-place float conversion for auxiliary effect input buffer.
711 static_assert(sizeof(float) <= sizeof(int32_t),
712 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
713
Andy Hungfa69ca32017-11-30 10:07:53 -0800714 memcpy_to_float_from_q4_27(
715 mConfig.inputCfg.buffer.f32,
716 mConfig.inputCfg.buffer.s32,
717 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800718#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800719 } else
Andy Hung116a4982017-11-30 10:15:08 -0800720#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800721 {
Andy Hung116a4982017-11-30 10:15:08 -0800722#ifdef FLOAT_AUX
723 memcpy_to_i16_from_float(
724 mConfig.inputCfg.buffer.s16,
725 mConfig.inputCfg.buffer.f32,
726 mConfig.inputCfg.buffer.frameCount);
727#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800728 memcpy_to_i16_from_q4_27(
729 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700730 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800731 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800732#endif
rago94a1ee82017-07-21 15:11:02 -0700733 }
rago94a1ee82017-07-21 15:11:02 -0700734 }
735#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800736 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
737 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
738
739 if (!auxType && mInChannelCountRequested != inChannelCount) {
740 adjust_channels(
741 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
742 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
743 sizeof(float),
744 sizeof(float)
745 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
746 inBuffer = mInConversionBuffer;
747 }
748 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
749 && mOutChannelCountRequested != outChannelCount) {
750 adjust_selected_channels(
751 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
752 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
753 sizeof(float),
754 sizeof(float)
755 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
756 outBuffer = mOutConversionBuffer;
757 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
759 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800760 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800761 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
762 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700763 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800764 memcpy_to_i16_from_float(
765 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800766 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800768 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700769 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800771 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
773 goto data_bypass;
774 }
775 memcpy_to_i16_from_float(
776 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800777 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800778 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800779 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700780 }
781 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800782#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800783 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800784#ifdef FLOAT_EFFECT_CHAIN
785 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800786 sp<EffectBufferHalInterface> target =
787 mOutChannelCountRequested != outChannelCount
788 ? mOutConversionBuffer : mOutBuffer;
789
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800791 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 mOutConversionBuffer->audioBuffer()->s16,
793 outChannelCount * mConfig.outputCfg.buffer.frameCount);
794 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800795 if (mOutChannelCountRequested != outChannelCount) {
796 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
797 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
798 sizeof(float),
799 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
800 }
rago94a1ee82017-07-21 15:11:02 -0700801#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700802 } else {
rago94a1ee82017-07-21 15:11:02 -0700803#ifdef FLOAT_EFFECT_CHAIN
804 data_bypass:
805#endif
806 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800807 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700808 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800809 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700810 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700812 }
813 }
814 ret = -ENODATA;
815 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800816
Eric Laurentca7cc822012-11-19 14:55:58 -0800817 // force transition to IDLE state when engine is ready
818 if (mState == STOPPED && ret == -ENODATA) {
819 mDisableWaitCnt = 1;
820 }
821
822 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700823 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800824#ifdef FLOAT_AUX
825 const size_t size =
826 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
827#else
rago94a1ee82017-07-21 15:11:02 -0700828 const size_t size =
829 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800830#endif
rago94a1ee82017-07-21 15:11:02 -0700831 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800832 }
833 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700834 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800835 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
836 // If an insert effect is idle and input buffer is different from output buffer,
837 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800838 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700839 // similar handling with data_bypass above.
840 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
841 accumulateInputToOutput();
842 } else { // EFFECT_BUFFER_ACCESS_WRITE
843 copyInputToOutput();
844 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800845 }
846 }
847}
848
849void AudioFlinger::EffectModule::reset_l()
850{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700851 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800852 return;
853 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700854 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800855}
856
857status_t AudioFlinger::EffectModule::configure()
858{
rago94a1ee82017-07-21 15:11:02 -0700859 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700860 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700861 uint32_t size;
862 audio_channel_mask_t channelMask;
863
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700864 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700865 status = NO_INIT;
866 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800867 }
868
Eric Laurentca7cc822012-11-19 14:55:58 -0800869 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800870 // TODO: handle configuration of input (record) SW effects above the HAL,
871 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
872 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800873 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800874 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700875 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800876
877 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800878 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
879 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
880 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
881 mConfig.inputCfg.channels);
882 }
883#ifndef MULTICHANNEL_EFFECT_CHAIN
884 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
885 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
886 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
887 mConfig.outputCfg.channels);
888 }
889#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800890 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800891#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700892 // TODO: Update this logic when multichannel effects are implemented.
893 // For offloaded tracks consider mono output as stereo for proper effect initialization
894 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
895 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
896 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
897 ALOGV("Overriding effect input and output as STEREO");
898 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800899#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800900 }
jiabineb3bda02020-06-30 14:07:03 -0700901 if (isHapticGenerator()) {
902 audio_channel_mask_t hapticChannelMask = mCallback->hapticChannelMask();
903 mConfig.inputCfg.channels |= hapticChannelMask;
904 mConfig.outputCfg.channels |= hapticChannelMask;
905 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800906 mInChannelCountRequested =
907 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
908 mOutChannelCountRequested =
909 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700910
rago94a1ee82017-07-21 15:11:02 -0700911 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
912 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900913
914 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100915 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900916 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
917 ALOGV("Overriding effect input as 48kHz");
918 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800919 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900920 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
922 mConfig.inputCfg.bufferProvider.cookie = NULL;
923 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
924 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
925 mConfig.outputCfg.bufferProvider.cookie = NULL;
926 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
927 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
928 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
929 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800930 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800931 // always overwrites output buffer: input buffer == output buffer
932 // - in other sessions:
933 // last effect in the chain accumulates in output buffer: input buffer != output buffer
934 // other effect: overwrites output buffer: input buffer == output buffer
935 // Auxiliary effect:
936 // accumulates in output buffer: input buffer != output buffer
937 // Therefore: accumulate <=> input buffer != output buffer
938 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
939 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
940 } else {
941 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
942 }
943 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
944 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800945 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800946 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
947
Eric Laurent6b446ce2019-12-13 10:56:31 -0800948 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800949 this, mCallback->chain().promote().get(),
950 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800951
952 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700953 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700954 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800955 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700956 &mConfig,
957 &size,
958 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700959 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800960 status = cmdStatus;
961 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800962
963#ifdef MULTICHANNEL_EFFECT_CHAIN
964 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800965 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800966 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
967 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
968 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700969 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
970 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800971 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
972 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
973 }
974 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
975 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
976 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
977 }
978 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700979 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800980 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700981 &mConfig,
982 &size,
983 &cmdStatus);
984 if (status == NO_ERROR) {
985 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800986 }
987 }
988#endif
989
990#ifdef FLOAT_EFFECT_CHAIN
991 if (status == NO_ERROR) {
992 mSupportsFloat = true;
993 }
994
995 if (status != NO_ERROR) {
996 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
997 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
998 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
999 size = sizeof(int);
1000 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1001 sizeof(mConfig),
1002 &mConfig,
1003 &size,
1004 &cmdStatus);
1005 if (status == NO_ERROR) {
1006 status = cmdStatus;
1007 }
1008 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001009 mSupportsFloat = false;
1010 ALOGVV("config worked with 16 bit");
1011 } else {
1012 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001013 }
rago94a1ee82017-07-21 15:11:02 -07001014 }
1015#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001016
rago94a1ee82017-07-21 15:11:02 -07001017 if (status == NO_ERROR) {
1018 // Establish Buffer strategy
1019 setInBuffer(mInBuffer);
1020 setOutBuffer(mOutBuffer);
1021
1022 // Update visualizer latency
1023 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1024 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1025 effect_param_t *p = (effect_param_t *)buf32;
1026
1027 p->psize = sizeof(uint32_t);
1028 p->vsize = sizeof(uint32_t);
1029 size = sizeof(int);
1030 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1031
Eric Laurent6b446ce2019-12-13 10:56:31 -08001032 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001033
1034 *((int32_t *)p->data + 1)= latency;
1035 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1036 sizeof(effect_param_t) + 8,
1037 &buf32,
1038 &size,
1039 &cmdStatus);
1040 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001041 }
1042
Andy Hung05083ac2017-12-14 15:00:28 -08001043 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1044 mMaxDisableWaitCnt = (uint32_t)std::max(
1045 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1046 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1047 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001048
Eric Laurentd0ebb532013-04-02 16:41:41 -07001049exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001050 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001051 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001052 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001053 return status;
1054}
1055
1056status_t AudioFlinger::EffectModule::init()
1057{
1058 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001059 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001060 return NO_INIT;
1061 }
1062 status_t cmdStatus;
1063 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001064 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1065 0,
1066 NULL,
1067 &size,
1068 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001069 if (status == 0) {
1070 status = cmdStatus;
1071 }
1072 return status;
1073}
1074
Eric Laurent1b928682014-10-02 19:41:47 -07001075void AudioFlinger::EffectModule::addEffectToHal_l()
1076{
1077 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1078 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001079 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001080 }
1081}
1082
Eric Laurentfa1e1232016-08-02 19:01:49 -07001083// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001084status_t AudioFlinger::EffectModule::start()
1085{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001086 status_t status;
1087 {
1088 Mutex::Autolock _l(mLock);
1089 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001090 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001091 if (status == NO_ERROR) {
1092 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001093 }
1094 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001095}
1096
1097status_t AudioFlinger::EffectModule::start_l()
1098{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001099 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001100 return NO_INIT;
1101 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001102 if (mStatus != NO_ERROR) {
1103 return mStatus;
1104 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 status_t cmdStatus;
1106 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001107 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1108 0,
1109 NULL,
1110 &size,
1111 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 if (status == 0) {
1113 status = cmdStatus;
1114 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001115 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001116 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001117 }
1118 return status;
1119}
1120
1121status_t AudioFlinger::EffectModule::stop()
1122{
1123 Mutex::Autolock _l(mLock);
1124 return stop_l();
1125}
1126
1127status_t AudioFlinger::EffectModule::stop_l()
1128{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001129 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 return NO_INIT;
1131 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001132 if (mStatus != NO_ERROR) {
1133 return mStatus;
1134 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001135 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001137
1138 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001139 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1140 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1141 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001142 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001143 mSetVolumeReentrantTid = INVALID_PID;
1144 }
1145
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001146 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1147 0,
1148 NULL,
1149 &size,
1150 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001151 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 status = cmdStatus;
1153 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001154 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001155 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001156 }
1157 return status;
1158}
1159
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001160// must be called with EffectChain::mLock held
1161void AudioFlinger::EffectModule::release_l()
1162{
1163 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001164 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001165 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001166 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001167 mEffectInterface.clear();
1168 }
1169}
1170
Eric Laurent6b446ce2019-12-13 10:56:31 -08001171status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001172{
1173 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1174 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001175 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001176 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001177 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001178}
1179
Andy Hunge4a1d912016-08-17 14:11:13 -07001180// round up delta valid if value and divisor are positive.
1181template <typename T>
1182static T roundUpDelta(const T &value, const T &divisor) {
1183 T remainder = value % divisor;
1184 return remainder == 0 ? 0 : divisor - remainder;
1185}
1186
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001187status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1188 const std::vector<uint8_t>& cmdData,
1189 int32_t maxReplySize,
1190 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001191{
1192 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001193 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001194
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001195 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001196 return NO_INIT;
1197 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001198 if (mStatus != NO_ERROR) {
1199 return mStatus;
1200 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001201 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1202 return -EINVAL;
1203 }
1204 size_t cmdSize = cmdData.size();
1205 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1206 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1207 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001208 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001209 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001210 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001211 android_errorWriteLog(0x534e4554, "33003822");
1212 return -EINVAL;
1213 }
1214 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001215 (maxReplySize < sizeof(effect_param_t) ||
1216 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001217 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001218 return -EINVAL;
1219 }
ragoe2759072016-11-22 18:02:48 -08001220 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001221 (sizeof(effect_param_t) > maxReplySize
1222 || param->psize > maxReplySize - sizeof(effect_param_t)
1223 || param->vsize > maxReplySize - sizeof(effect_param_t)
1224 - param->psize
1225 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1226 maxReplySize
1227 - sizeof(effect_param_t)
1228 - param->psize
1229 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001230 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1231 android_errorWriteLog(0x534e4554, "32705438");
1232 return -EINVAL;
1233 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001234 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001235 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1236 && // DEFERRED not generally used
1237 (param == nullptr
1238 || param->psize > cmdSize - sizeof(effect_param_t)
1239 || param->vsize > cmdSize - sizeof(effect_param_t)
1240 - param->psize
1241 || roundUpDelta(param->psize,
1242 (uint32_t) sizeof(int)) >
1243 cmdSize
1244 - sizeof(effect_param_t)
1245 - param->psize
1246 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001247 android_errorWriteLog(0x534e4554, "30204301");
1248 return -EINVAL;
1249 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001250 uint32_t replySize = maxReplySize;
1251 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001252 status_t status = mEffectInterface->command(cmdCode,
1253 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 const_cast<uint8_t*>(cmdData.data()),
1255 &replySize,
1256 reply->data());
1257 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001258 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001259 for (size_t i = 1; i < mHandles.size(); i++) {
1260 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001261 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001262 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 }
1264 }
1265 }
1266 return status;
1267}
1268
Eric Laurentca7cc822012-11-19 14:55:58 -08001269bool AudioFlinger::EffectModule::isProcessEnabled() const
1270{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001271 if (mStatus != NO_ERROR) {
1272 return false;
1273 }
1274
Eric Laurentca7cc822012-11-19 14:55:58 -08001275 switch (mState) {
1276 case RESTART:
1277 case ACTIVE:
1278 case STOPPING:
1279 case STOPPED:
1280 return true;
1281 case IDLE:
1282 case STARTING:
1283 case DESTROYED:
1284 default:
1285 return false;
1286 }
1287}
1288
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001289bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1290{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001291 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001292}
1293
1294bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1295{
1296 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1297}
1298
Mikhail Naganov022b9952017-01-04 16:36:51 -08001299void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001300 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001301
1302 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001303 if (buffer != 0) {
1304 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1305 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1306 } else {
1307 mConfig.inputCfg.buffer.raw = NULL;
1308 }
1309 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001310 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001311
1312#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001313 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001314 // Theoretically insert effects can also do in-place conversions (destroying
1315 // the original buffer) when the output buffer is identical to the input buffer,
1316 // but we don't optimize for it here.
1317 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001318 const uint32_t inChannelCount =
1319 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1320 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001321 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001322 // we need to translate - create hidl shared buffer and intercept
1323 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001324 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1325 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1326 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001327
1328 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1329 __func__, inChannels, inFrameCount, size);
1330
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001331 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001332 || size > mInConversionBuffer->getSize())) {
1333 mInConversionBuffer.clear();
1334 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001335 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001336 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001337 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001338 mInConversionBuffer->setFrameCount(inFrameCount);
1339 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001340 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001341 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001342 }
1343 }
1344#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001345}
1346
1347void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001348 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001349
1350 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001351 if (buffer != 0) {
1352 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1353 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1354 } else {
1355 mConfig.outputCfg.buffer.raw = NULL;
1356 }
1357 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001358 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001359
1360#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001361 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001362 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001363 const uint32_t outChannelCount =
1364 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1365 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001366 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001367 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001368 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1369 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1370 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001371
1372 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1373 __func__, outChannels, outFrameCount, size);
1374
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001375 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001376 || size > mOutConversionBuffer->getSize())) {
1377 mOutConversionBuffer.clear();
1378 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001379 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001380 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001381 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001382 mOutConversionBuffer->setFrameCount(outFrameCount);
1383 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001384 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001385 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001386 }
1387 }
1388#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001389}
1390
Eric Laurentca7cc822012-11-19 14:55:58 -08001391status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1392{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001393 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001394 if (mStatus != NO_ERROR) {
1395 return mStatus;
1396 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1399 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1400 if (isProcessEnabled() &&
1401 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001402 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1403 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001404 uint32_t volume[2];
1405 uint32_t *pVolume = NULL;
1406 uint32_t size = sizeof(volume);
1407 volume[0] = *left;
1408 volume[1] = *right;
1409 if (controller) {
1410 pVolume = volume;
1411 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001412 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1413 size,
1414 volume,
1415 &size,
1416 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1418 *left = volume[0];
1419 *right = volume[1];
1420 }
1421 }
1422 return status;
1423}
1424
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001425void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1426{
Zhou Songd505c642020-02-20 16:35:37 +08001427 // for offload or direct thread, if the effect chain has non-offloadable
1428 // effect and any effect module within the chain has volume control, then
1429 // volume control is delegated to effect, otherwise, set volume to hal.
1430 if (mEffectCallback->isOffloadOrDirect() &&
1431 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001432 float vol_l = (float)left / (1 << 24);
1433 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001434 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001435 }
1436}
1437
jiabin8f278ee2019-11-11 12:16:27 -08001438status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1439 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001440{
jiabin8f278ee2019-11-11 12:16:27 -08001441 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1442 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 return NO_ERROR;
1444 }
1445
1446 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001447 if (mStatus != NO_ERROR) {
1448 return mStatus;
1449 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001450 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001451 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001452 status_t cmdStatus;
1453 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001454 // FIXME: use audio device types and addresses when the hal interface is ready.
1455 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001456 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001457 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001458 &size,
1459 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 }
1461 return status;
1462}
1463
jiabin8f278ee2019-11-11 12:16:27 -08001464status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1465{
1466 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1467}
1468
1469status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1470{
1471 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1472}
1473
Eric Laurentca7cc822012-11-19 14:55:58 -08001474status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1475{
1476 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001477 if (mStatus != NO_ERROR) {
1478 return mStatus;
1479 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 status_t status = NO_ERROR;
1481 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1482 status_t cmdStatus;
1483 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001484 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1485 sizeof(audio_mode_t),
1486 &mode,
1487 &size,
1488 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001489 if (status == NO_ERROR) {
1490 status = cmdStatus;
1491 }
1492 }
1493 return status;
1494}
1495
1496status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1497{
1498 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001499 if (mStatus != NO_ERROR) {
1500 return mStatus;
1501 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001502 status_t status = NO_ERROR;
1503 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1504 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001505 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1506 sizeof(audio_source_t),
1507 &source,
1508 &size,
1509 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001510 }
1511 return status;
1512}
1513
Eric Laurent5baf2af2013-09-12 17:37:00 -07001514status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1515{
1516 Mutex::Autolock _l(mLock);
1517 if (mStatus != NO_ERROR) {
1518 return mStatus;
1519 }
1520 status_t status = NO_ERROR;
1521 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1522 status_t cmdStatus;
1523 uint32_t size = sizeof(status_t);
1524 effect_offload_param_t cmd;
1525
1526 cmd.isOffload = offloaded;
1527 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001528 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1529 sizeof(effect_offload_param_t),
1530 &cmd,
1531 &size,
1532 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001533 if (status == NO_ERROR) {
1534 status = cmdStatus;
1535 }
1536 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1537 } else {
1538 if (offloaded) {
1539 status = INVALID_OPERATION;
1540 }
1541 mOffloaded = false;
1542 }
1543 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1544 return status;
1545}
1546
1547bool AudioFlinger::EffectModule::isOffloaded() const
1548{
1549 Mutex::Autolock _l(mLock);
1550 return mOffloaded;
1551}
1552
jiabineb3bda02020-06-30 14:07:03 -07001553/*static*/
1554bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1555 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1556}
1557
1558bool AudioFlinger::EffectModule::isHapticGenerator() const {
1559 return isHapticGenerator(&mDescriptor.type);
1560}
1561
jiabine70bc7f2020-06-30 22:07:55 -07001562status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1563{
1564 if (mStatus != NO_ERROR) {
1565 return mStatus;
1566 }
1567 if (!isHapticGenerator()) {
1568 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1569 return INVALID_OPERATION;
1570 }
1571
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001572 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1573 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001574 param->psize = sizeof(int32_t);
1575 param->vsize = sizeof(int32_t) * 2;
1576 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1577 *((int32_t*)param->data + 1) = id;
1578 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001579 std::vector<uint8_t> response;
1580 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001581 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001582 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1583 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001584 }
1585 return status;
1586}
1587
jiabin1319f5a2021-03-30 22:21:24 +00001588status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo* vibratorInfo)
1589{
1590 if (mStatus != NO_ERROR) {
1591 return mStatus;
1592 }
1593 if (!isHapticGenerator()) {
1594 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1595 return INVALID_OPERATION;
1596 }
1597
1598 std::vector<uint8_t> request(
1599 sizeof(effect_param_t) + sizeof(int32_t) + 2 * sizeof(float));
1600 effect_param_t *param = (effect_param_t*) request.data();
1601 param->psize = sizeof(int32_t);
1602 param->vsize = 2 * sizeof(float);
1603 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1604 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
1605 vibratorInfoPtr[0] = vibratorInfo->resonantFrequency;
1606 vibratorInfoPtr[1] = vibratorInfo->qFactor;
1607 std::vector<uint8_t> response;
1608 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1609 if (status == NO_ERROR) {
1610 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1611 status = *reinterpret_cast<const status_t*>(response.data());
1612 }
1613 return status;
1614}
1615
Andy Hungbded9c82017-11-30 18:47:35 -08001616static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1617 std::stringstream ss;
1618
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001619 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001620 return "nullptr"; // make different than below
1621 } else if (buffer->externalData() != nullptr) {
1622 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1623 << " -> "
1624 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1625 } else {
1626 ss << buffer->audioBuffer()->raw;
1627 }
1628 return ss.str();
1629}
Marco Nelissenb2208842014-02-07 14:00:50 -08001630
Eric Laurent41709552019-12-16 19:34:05 -08001631void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001632{
Eric Laurent41709552019-12-16 19:34:05 -08001633 EffectBase::dump(fd, args);
1634
Eric Laurentca7cc822012-11-19 14:55:58 -08001635 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001636 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001637
Eric Laurent41709552019-12-16 19:34:05 -08001638 result.append("\t\tStatus Engine:\n");
1639 result.appendFormat("\t\t%03d %p\n",
1640 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001641
1642 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001643
1644 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001645 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1646 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1647 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001648 mConfig.inputCfg.buffer.frameCount,
1649 mConfig.inputCfg.samplingRate,
1650 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001651 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001652 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001653
1654 result.append("\t\t- Output configuration:\n");
1655 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001656 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001657 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001658 mConfig.outputCfg.buffer.frameCount,
1659 mConfig.outputCfg.samplingRate,
1660 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001661 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001662 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001663
rago94a1ee82017-07-21 15:11:02 -07001664#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001665
Andy Hungbded9c82017-11-30 18:47:35 -08001666 result.appendFormat("\t\t- HAL buffers:\n"
1667 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1668 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1669 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1670 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1671 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001672#endif
1673
Eric Laurentca7cc822012-11-19 14:55:58 -08001674 write(fd, result.string(), result.length());
1675
Mikhail Naganov4d547672019-02-22 14:19:19 -08001676 if (mEffectInterface != 0) {
1677 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1678 (void)mEffectInterface->dump(fd);
1679 }
1680
Eric Laurentca7cc822012-11-19 14:55:58 -08001681 if (locked) {
1682 mLock.unlock();
1683 }
1684}
1685
1686// ----------------------------------------------------------------------------
1687// EffectHandle implementation
1688// ----------------------------------------------------------------------------
1689
1690#undef LOG_TAG
1691#define LOG_TAG "AudioFlinger::EffectHandle"
1692
Eric Laurent41709552019-12-16 19:34:05 -08001693AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001694 const sp<AudioFlinger::Client>& client,
1695 const sp<media::IEffectClient>& effectClient,
1696 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 : BnEffect(),
1698 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001699 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001700{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001701 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001702
1703 if (client == 0) {
1704 return;
1705 }
1706 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1707 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001708 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001709 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001710 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001712 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 return;
1714 }
Glenn Kastene75da402013-11-20 13:54:52 -08001715 new(mCblk) effect_param_cblk_t();
1716 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001717}
1718
1719AudioFlinger::EffectHandle::~EffectHandle()
1720{
1721 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001722 disconnect(false);
1723}
1724
Glenn Kastene75da402013-11-20 13:54:52 -08001725status_t AudioFlinger::EffectHandle::initCheck()
1726{
1727 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1728}
1729
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001730#define RETURN(code) \
1731 *_aidl_return = (code); \
1732 return Status::ok();
1733
1734Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001735{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001736 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001738 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001739 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001740 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001741 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001743 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001744 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001745
1746 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001747 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 }
1749
1750 mEnabled = true;
1751
Eric Laurent6c796322019-04-09 14:13:17 -07001752 status_t status = effect->updatePolicyState();
1753 if (status != NO_ERROR) {
1754 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001755 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001756 }
1757
Eric Laurent6b446ce2019-12-13 10:56:31 -08001758 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001759
1760 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001761 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001762 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001763 }
1764
Eric Laurent6b446ce2019-12-13 10:56:31 -08001765 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001766 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 mEnabled = false;
1768 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001769 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001770}
1771
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001773{
1774 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001775 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001776 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001777 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001778 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001779 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001781 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001783
1784 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001785 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 }
1787 mEnabled = false;
1788
Eric Laurent6c796322019-04-09 14:13:17 -07001789 effect->updatePolicyState();
1790
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001791 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001792 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 }
1794
Eric Laurent6b446ce2019-12-13 10:56:31 -08001795 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001796 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797}
1798
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001799Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001800{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001801 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001802 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001803 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001804}
1805
1806void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1807{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 AutoMutex _l(mLock);
1809 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1810 if (mDisconnected) {
1811 if (unpinIfLast) {
1812 android_errorWriteLog(0x534e4554, "32707507");
1813 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001814 return;
1815 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001816 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001817 {
Eric Laurent41709552019-12-16 19:34:05 -08001818 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001819 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001820 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001821 ALOGW("%s Effect handle %p disconnected after thread destruction",
1822 __func__, this);
1823 }
1824 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001825 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001826 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001827
Eric Laurentca7cc822012-11-19 14:55:58 -08001828 if (mClient != 0) {
1829 if (mCblk != NULL) {
1830 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1831 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1832 }
1833 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001834 // Client destructor must run with AudioFlinger client mutex locked
1835 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001836 mClient.clear();
1837 }
1838}
1839
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001840Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1841 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1842 return Status::ok();
1843}
1844
1845Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1846 const std::vector<uint8_t>& cmdData,
1847 int32_t maxResponseSize,
1848 std::vector<uint8_t>* response,
1849 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001850{
1851 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001852 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001853
Eric Laurentc7ab3092017-06-15 18:43:46 -07001854 // reject commands reserved for internal use by audio framework if coming from outside
1855 // of audioserver
1856 switch(cmdCode) {
1857 case EFFECT_CMD_ENABLE:
1858 case EFFECT_CMD_DISABLE:
1859 case EFFECT_CMD_SET_PARAM:
1860 case EFFECT_CMD_SET_PARAM_DEFERRED:
1861 case EFFECT_CMD_SET_PARAM_COMMIT:
1862 case EFFECT_CMD_GET_PARAM:
1863 break;
1864 default:
1865 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1866 break;
1867 }
1868 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001869 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001870 }
1871
Eric Laurent1ffc5852016-12-15 14:46:09 -08001872 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001874 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001875 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001876 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001877 writeToBuffer(NO_ERROR, response);
1878 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001879 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001880 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001881 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001882 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001883 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001884 writeToBuffer(NO_ERROR, response);
1885 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001886 }
1887
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001888 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001889 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001890 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001891 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001892 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001893 // only get parameter command is permitted for applications not controlling the effect
1894 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001895 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001897
1898 // handle commands that are not forwarded transparently to effect engine
1899 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001900 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001902 }
1903
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001904 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001905 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001907 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001909
Eric Laurentca7cc822012-11-19 14:55:58 -08001910 // No need to trylock() here as this function is executed in the binder thread serving a
1911 // particular client process: no risk to block the whole media server process or mixer
1912 // threads if we are stuck here
1913 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001914 // keep local copy of index in case of client corruption b/32220769
1915 const uint32_t clientIndex = mCblk->clientIndex;
1916 const uint32_t serverIndex = mCblk->serverIndex;
1917 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1918 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001919 mCblk->serverIndex = 0;
1920 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001922 }
1923 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001924 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001925 for (uint32_t index = serverIndex; index < clientIndex;) {
1926 int *p = (int *)(mBuffer + index);
1927 const int size = *p++;
1928 if (size < 0
1929 || size > EFFECT_PARAM_BUFFER_SIZE
1930 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001932 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001933 break;
1934 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001935
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001936 std::copy(reinterpret_cast<const uint8_t*>(p),
1937 reinterpret_cast<const uint8_t*>(p) + size,
1938 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001939
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001940 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001941 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001942 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001943 sizeof(int),
1944 &replyBuffer);
1945 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001946
1947 // verify shared memory: server index shouldn't change; client index can't go back.
1948 if (serverIndex != mCblk->serverIndex
1949 || clientIndex > mCblk->clientIndex) {
1950 android_errorWriteLog(0x534e4554, "32220769");
1951 status = BAD_VALUE;
1952 break;
1953 }
1954
Eric Laurentca7cc822012-11-19 14:55:58 -08001955 // stop at first error encountered
1956 if (ret != NO_ERROR) {
1957 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001959 break;
1960 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001961 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 break;
1963 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001964 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001965 }
1966 mCblk->serverIndex = 0;
1967 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001968 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001969 }
1970
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001971 status_t status = effect->command(cmdCode,
1972 cmdData,
1973 maxResponseSize,
1974 response);
1975 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001976}
1977
1978void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1979{
1980 ALOGV("setControl %p control %d", this, hasControl);
1981
1982 mHasControl = hasControl;
1983 mEnabled = enabled;
1984
1985 if (signal && mEffectClient != 0) {
1986 mEffectClient->controlStatusChanged(hasControl);
1987 }
1988}
1989
1990void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001991 const std::vector<uint8_t>& cmdData,
1992 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001993{
1994 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001996 }
1997}
1998
1999
2000
2001void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2002{
2003 if (mEffectClient != 0) {
2004 mEffectClient->enableStatusChanged(enabled);
2005 }
2006}
2007
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002008void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002009{
2010 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2011
Marco Nelissenb2208842014-02-07 14:00:50 -08002012 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002013 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002014 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002015 mHasControl ? "yes" : "no",
2016 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002017 mCblk ? mCblk->clientIndex : 0,
2018 mCblk ? mCblk->serverIndex : 0
2019 );
2020
2021 if (locked) {
2022 mCblk->lock.unlock();
2023 }
2024}
2025
2026#undef LOG_TAG
2027#define LOG_TAG "AudioFlinger::EffectChain"
2028
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002029AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2030 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002031 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002032 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002033 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002034 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002035{
2036 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002037 sp<ThreadBase> p = thread.promote();
2038 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002039 return;
2040 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002041 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2042 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002043}
2044
2045AudioFlinger::EffectChain::~EffectChain()
2046{
Eric Laurentca7cc822012-11-19 14:55:58 -08002047}
2048
2049// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2050sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2051 effect_descriptor_t *descriptor)
2052{
2053 size_t size = mEffects.size();
2054
2055 for (size_t i = 0; i < size; i++) {
2056 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2057 return mEffects[i];
2058 }
2059 }
2060 return 0;
2061}
2062
2063// getEffectFromId_l() must be called with ThreadBase::mLock held
2064sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2065{
2066 size_t size = mEffects.size();
2067
2068 for (size_t i = 0; i < size; i++) {
2069 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2070 if (id == 0 || mEffects[i]->id() == id) {
2071 return mEffects[i];
2072 }
2073 }
2074 return 0;
2075}
2076
2077// getEffectFromType_l() must be called with ThreadBase::mLock held
2078sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2079 const effect_uuid_t *type)
2080{
2081 size_t size = mEffects.size();
2082
2083 for (size_t i = 0; i < size; i++) {
2084 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2085 return mEffects[i];
2086 }
2087 }
2088 return 0;
2089}
2090
Eric Laurent6c796322019-04-09 14:13:17 -07002091std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2092{
2093 std::vector<int> ids;
2094 Mutex::Autolock _l(mLock);
2095 for (size_t i = 0; i < mEffects.size(); i++) {
2096 ids.push_back(mEffects[i]->id());
2097 }
2098 return ids;
2099}
2100
Eric Laurentca7cc822012-11-19 14:55:58 -08002101void AudioFlinger::EffectChain::clearInputBuffer()
2102{
2103 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002104 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002105}
2106
2107// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002108void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002109{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002110 if (mInBuffer == NULL) {
2111 return;
2112 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002113 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002114 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002115
Eric Laurent6b446ce2019-12-13 10:56:31 -08002116 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002117 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002118}
2119
2120// Must be called with EffectChain::mLock locked
2121void AudioFlinger::EffectChain::process_l()
2122{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002123 // never process effects when:
2124 // - on an OFFLOAD thread
2125 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002126 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002127 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002128 bool tracksOnSession = (trackCnt() != 0);
2129
2130 if (!tracksOnSession && mTailBufferCount == 0) {
2131 doProcess = false;
2132 }
2133
2134 if (activeTrackCnt() == 0) {
2135 // if no track is active and the effect tail has not been rendered,
2136 // the input buffer must be cleared here as the mixer process will not do it
2137 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002138 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002139 if (mTailBufferCount > 0) {
2140 mTailBufferCount--;
2141 }
2142 }
2143 }
2144 }
2145
2146 size_t size = mEffects.size();
2147 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002148 // Only the input and output buffers of the chain can be external,
2149 // and 'update' / 'commit' do nothing for allocated buffers, thus
2150 // it's not needed to consider any other buffers here.
2151 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002152 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2153 mOutBuffer->update();
2154 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002155 for (size_t i = 0; i < size; i++) {
2156 mEffects[i]->process();
2157 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002158 mInBuffer->commit();
2159 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2160 mOutBuffer->commit();
2161 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002162 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002163 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002164 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002165 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2166 }
2167 if (doResetVolume) {
2168 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002169 }
2170}
2171
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002172// createEffect_l() must be called with ThreadBase::mLock held
2173status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002174 effect_descriptor_t *desc,
2175 int id,
2176 audio_session_t sessionId,
2177 bool pinned)
2178{
2179 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002180 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002181 status_t lStatus = effect->status();
2182 if (lStatus == NO_ERROR) {
2183 lStatus = addEffect_ll(effect);
2184 }
2185 if (lStatus != NO_ERROR) {
2186 effect.clear();
2187 }
2188 return lStatus;
2189}
2190
2191// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002192status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2193{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002194 Mutex::Autolock _l(mLock);
2195 return addEffect_ll(effect);
2196}
2197// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2198status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2199{
Eric Laurentca7cc822012-11-19 14:55:58 -08002200 effect_descriptor_t desc = effect->desc();
2201 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2202
Eric Laurent6b446ce2019-12-13 10:56:31 -08002203 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002204
2205 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2206 // Auxiliary effects are inserted at the beginning of mEffects vector as
2207 // they are processed first and accumulated in chain input buffer
2208 mEffects.insertAt(effect, 0);
2209
2210 // the input buffer for auxiliary effect contains mono samples in
2211 // 32 bit format. This is to avoid saturation in AudoMixer
2212 // accumulation stage. Saturation is done in EffectModule::process() before
2213 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002214 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002215 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002216#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002217 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002218 numSamples * sizeof(float), &halBuffer);
2219#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002220 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002221 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002222#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002223 if (result != OK) return result;
2224 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002225 // auxiliary effects output samples to chain input buffer for further processing
2226 // by insert effects
2227 effect->setOutBuffer(mInBuffer);
2228 } else {
2229 // Insert effects are inserted at the end of mEffects vector as they are processed
2230 // after track and auxiliary effects.
2231 // Insert effect order as a function of indicated preference:
2232 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2233 // another effect is present
2234 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2235 // last effect claiming first position
2236 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2237 // first effect claiming last position
2238 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2239 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2240 // already present
2241
2242 size_t size = mEffects.size();
2243 size_t idx_insert = size;
2244 ssize_t idx_insert_first = -1;
2245 ssize_t idx_insert_last = -1;
2246
2247 for (size_t i = 0; i < size; i++) {
2248 effect_descriptor_t d = mEffects[i]->desc();
2249 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2250 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2251 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2252 // check invalid effect chaining combinations
2253 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2254 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2255 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2256 desc.name, d.name);
2257 return INVALID_OPERATION;
2258 }
2259 // remember position of first insert effect and by default
2260 // select this as insert position for new effect
2261 if (idx_insert == size) {
2262 idx_insert = i;
2263 }
2264 // remember position of last insert effect claiming
2265 // first position
2266 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2267 idx_insert_first = i;
2268 }
2269 // remember position of first insert effect claiming
2270 // last position
2271 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2272 idx_insert_last == -1) {
2273 idx_insert_last = i;
2274 }
2275 }
2276 }
2277
2278 // modify idx_insert from first position if needed
2279 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2280 if (idx_insert_last != -1) {
2281 idx_insert = idx_insert_last;
2282 } else {
2283 idx_insert = size;
2284 }
2285 } else {
2286 if (idx_insert_first != -1) {
2287 idx_insert = idx_insert_first + 1;
2288 }
2289 }
2290
2291 // always read samples from chain input buffer
2292 effect->setInBuffer(mInBuffer);
2293
2294 // if last effect in the chain, output samples to chain
2295 // output buffer, otherwise to chain input buffer
2296 if (idx_insert == size) {
2297 if (idx_insert != 0) {
2298 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2299 mEffects[idx_insert-1]->configure();
2300 }
2301 effect->setOutBuffer(mOutBuffer);
2302 } else {
2303 effect->setOutBuffer(mInBuffer);
2304 }
2305 mEffects.insertAt(effect, idx_insert);
2306
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002307 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 idx_insert);
2309 }
2310 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002311
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 return NO_ERROR;
2313}
2314
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002315// removeEffect_l() must be called with ThreadBase::mLock held
2316size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2317 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002318{
2319 Mutex::Autolock _l(mLock);
2320 size_t size = mEffects.size();
2321 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2322
2323 for (size_t i = 0; i < size; i++) {
2324 if (effect == mEffects[i]) {
2325 // calling stop here will remove pre-processing effect from the audio HAL.
2326 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2327 // the middle of a read from audio HAL
2328 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2329 mEffects[i]->state() == EffectModule::STOPPING) {
2330 mEffects[i]->stop();
2331 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002332 if (release) {
2333 mEffects[i]->release_l();
2334 }
2335
Mikhail Naganov022b9952017-01-04 16:36:51 -08002336 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002337 if (i == size - 1 && i != 0) {
2338 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2339 mEffects[i - 1]->configure();
2340 }
2341 }
2342 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002343 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002344 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002345
Eric Laurentca7cc822012-11-19 14:55:58 -08002346 break;
2347 }
2348 }
2349
2350 return mEffects.size();
2351}
2352
jiabin8f278ee2019-11-11 12:16:27 -08002353// setDevices_l() must be called with ThreadBase::mLock held
2354void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002355{
2356 size_t size = mEffects.size();
2357 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002358 mEffects[i]->setDevices(devices);
2359 }
2360}
2361
2362// setInputDevice_l() must be called with ThreadBase::mLock held
2363void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2364{
2365 size_t size = mEffects.size();
2366 for (size_t i = 0; i < size; i++) {
2367 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002368 }
2369}
2370
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002371// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002372void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2373{
2374 size_t size = mEffects.size();
2375 for (size_t i = 0; i < size; i++) {
2376 mEffects[i]->setMode(mode);
2377 }
2378}
2379
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002380// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002381void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2382{
2383 size_t size = mEffects.size();
2384 for (size_t i = 0; i < size; i++) {
2385 mEffects[i]->setAudioSource(source);
2386 }
2387}
2388
Zhou Songd505c642020-02-20 16:35:37 +08002389bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2390 for (const auto &effect : mEffects) {
2391 if (effect->isVolumeControlEnabled()) return true;
2392 }
2393 return false;
2394}
2395
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002396// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002397bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002398{
2399 uint32_t newLeft = *left;
2400 uint32_t newRight = *right;
2401 bool hasControl = false;
2402 int ctrlIdx = -1;
2403 size_t size = mEffects.size();
2404
2405 // first update volume controller
2406 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002407 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002408 ctrlIdx = i - 1;
2409 hasControl = true;
2410 break;
2411 }
2412 }
2413
Eric Laurentfa1e1232016-08-02 19:01:49 -07002414 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002415 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002416 if (hasControl) {
2417 *left = mNewLeftVolume;
2418 *right = mNewRightVolume;
2419 }
2420 return hasControl;
2421 }
2422
2423 mVolumeCtrlIdx = ctrlIdx;
2424 mLeftVolume = newLeft;
2425 mRightVolume = newRight;
2426
2427 // second get volume update from volume controller
2428 if (ctrlIdx >= 0) {
2429 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2430 mNewLeftVolume = newLeft;
2431 mNewRightVolume = newRight;
2432 }
2433 // then indicate volume to all other effects in chain.
2434 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002435 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002436 uint32_t lVol = newLeft;
2437 uint32_t rVol = newRight;
2438
2439 for (size_t i = 0; i < size; i++) {
2440 if ((int)i == ctrlIdx) {
2441 continue;
2442 }
2443 // this also works for ctrlIdx == -1 when there is no volume controller
2444 if ((int)i > ctrlIdx) {
2445 lVol = *left;
2446 rVol = *right;
2447 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002448 // Pass requested volume directly if this is volume monitor module
2449 if (mEffects[i]->isVolumeMonitor()) {
2450 mEffects[i]->setVolume(left, right, false);
2451 } else {
2452 mEffects[i]->setVolume(&lVol, &rVol, false);
2453 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002454 }
2455 *left = newLeft;
2456 *right = newRight;
2457
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002458 setVolumeForOutput_l(*left, *right);
2459
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 return hasControl;
2461}
2462
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002463// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002464void AudioFlinger::EffectChain::resetVolume_l()
2465{
Eric Laurente7449bf2016-08-03 18:44:07 -07002466 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2467 uint32_t left = mLeftVolume;
2468 uint32_t right = mRightVolume;
2469 (void)setVolume_l(&left, &right, true);
2470 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002471}
2472
jiabineb3bda02020-06-30 14:07:03 -07002473// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2474bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2475{
2476 for (size_t i = 0; i < mEffects.size(); ++i) {
2477 if (mEffects[i]->isHapticGenerator()) {
2478 return true;
2479 }
2480 }
2481 return false;
2482}
2483
jiabine70bc7f2020-06-30 22:07:55 -07002484void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2485{
2486 Mutex::Autolock _l(mLock);
2487 for (size_t i = 0; i < mEffects.size(); ++i) {
2488 mEffects[i]->setHapticIntensity(id, intensity);
2489 }
2490}
2491
Eric Laurent1b928682014-10-02 19:41:47 -07002492void AudioFlinger::EffectChain::syncHalEffectsState()
2493{
2494 Mutex::Autolock _l(mLock);
2495 for (size_t i = 0; i < mEffects.size(); i++) {
2496 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2497 mEffects[i]->state() == EffectModule::STOPPING) {
2498 mEffects[i]->addEffectToHal_l();
2499 }
2500 }
2501}
2502
Eric Laurentca7cc822012-11-19 14:55:58 -08002503void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2504{
Eric Laurentca7cc822012-11-19 14:55:58 -08002505 String8 result;
2506
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002507 const size_t numEffects = mEffects.size();
2508 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002509
Marco Nelissenb2208842014-02-07 14:00:50 -08002510 if (numEffects) {
2511 bool locked = AudioFlinger::dumpTryLock(mLock);
2512 // failed to lock - AudioFlinger is probably deadlocked
2513 if (!locked) {
2514 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002515 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002516
Andy Hungbded9c82017-11-30 18:47:35 -08002517 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2518 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2519 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2520 (int)inBufferStr.size(), "In buffer ",
2521 (int)outBufferStr.size(), "Out buffer ");
2522 result.appendFormat("\t%s %s %d\n",
2523 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002524 write(fd, result.string(), result.size());
2525
2526 for (size_t i = 0; i < numEffects; ++i) {
2527 sp<EffectModule> effect = mEffects[i];
2528 if (effect != 0) {
2529 effect->dump(fd, args);
2530 }
2531 }
2532
2533 if (locked) {
2534 mLock.unlock();
2535 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002536 } else {
2537 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002538 }
2539}
2540
2541// must be called with ThreadBase::mLock held
2542void AudioFlinger::EffectChain::setEffectSuspended_l(
2543 const effect_uuid_t *type, bool suspend)
2544{
2545 sp<SuspendedEffectDesc> desc;
2546 // use effect type UUID timelow as key as there is no real risk of identical
2547 // timeLow fields among effect type UUIDs.
2548 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2549 if (suspend) {
2550 if (index >= 0) {
2551 desc = mSuspendedEffects.valueAt(index);
2552 } else {
2553 desc = new SuspendedEffectDesc();
2554 desc->mType = *type;
2555 mSuspendedEffects.add(type->timeLow, desc);
2556 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2557 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002558
Eric Laurentca7cc822012-11-19 14:55:58 -08002559 if (desc->mRefCount++ == 0) {
2560 sp<EffectModule> effect = getEffectIfEnabled(type);
2561 if (effect != 0) {
2562 desc->mEffect = effect;
2563 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002564 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002565 }
2566 }
2567 } else {
2568 if (index < 0) {
2569 return;
2570 }
2571 desc = mSuspendedEffects.valueAt(index);
2572 if (desc->mRefCount <= 0) {
2573 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002574 desc->mRefCount = 0;
2575 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002576 }
2577 if (--desc->mRefCount == 0) {
2578 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2579 if (desc->mEffect != 0) {
2580 sp<EffectModule> effect = desc->mEffect.promote();
2581 if (effect != 0) {
2582 effect->setSuspended(false);
2583 effect->lock();
2584 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002585 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002586 effect->setEnabled_l(handle->enabled());
2587 }
2588 effect->unlock();
2589 }
2590 desc->mEffect.clear();
2591 }
2592 mSuspendedEffects.removeItemsAt(index);
2593 }
2594 }
2595}
2596
2597// must be called with ThreadBase::mLock held
2598void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2599{
2600 sp<SuspendedEffectDesc> desc;
2601
2602 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2603 if (suspend) {
2604 if (index >= 0) {
2605 desc = mSuspendedEffects.valueAt(index);
2606 } else {
2607 desc = new SuspendedEffectDesc();
2608 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2609 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2610 }
2611 if (desc->mRefCount++ == 0) {
2612 Vector< sp<EffectModule> > effects;
2613 getSuspendEligibleEffects(effects);
2614 for (size_t i = 0; i < effects.size(); i++) {
2615 setEffectSuspended_l(&effects[i]->desc().type, true);
2616 }
2617 }
2618 } else {
2619 if (index < 0) {
2620 return;
2621 }
2622 desc = mSuspendedEffects.valueAt(index);
2623 if (desc->mRefCount <= 0) {
2624 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2625 desc->mRefCount = 1;
2626 }
2627 if (--desc->mRefCount == 0) {
2628 Vector<const effect_uuid_t *> types;
2629 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2630 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2631 continue;
2632 }
2633 types.add(&mSuspendedEffects.valueAt(i)->mType);
2634 }
2635 for (size_t i = 0; i < types.size(); i++) {
2636 setEffectSuspended_l(types[i], false);
2637 }
2638 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2639 mSuspendedEffects.keyAt(index));
2640 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2641 }
2642 }
2643}
2644
2645
2646// The volume effect is used for automated tests only
2647#ifndef OPENSL_ES_H_
2648static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2649 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2650const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2651#endif //OPENSL_ES_H_
2652
Eric Laurentd8365c52017-07-16 15:27:05 -07002653/* static */
2654bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2655{
2656 // Only NS and AEC are suspended when BtNRec is off
2657 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2658 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2659 return true;
2660 }
2661 return false;
2662}
2663
Eric Laurentca7cc822012-11-19 14:55:58 -08002664bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2665{
2666 // auxiliary effects and visualizer are never suspended on output mix
2667 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2668 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2669 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002670 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2671 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002672 return false;
2673 }
2674 return true;
2675}
2676
2677void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2678 Vector< sp<AudioFlinger::EffectModule> > &effects)
2679{
2680 effects.clear();
2681 for (size_t i = 0; i < mEffects.size(); i++) {
2682 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2683 effects.add(mEffects[i]);
2684 }
2685 }
2686}
2687
2688sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2689 const effect_uuid_t *type)
2690{
2691 sp<EffectModule> effect = getEffectFromType_l(type);
2692 return effect != 0 && effect->isEnabled() ? effect : 0;
2693}
2694
2695void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2696 bool enabled)
2697{
2698 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2699 if (enabled) {
2700 if (index < 0) {
2701 // if the effect is not suspend check if all effects are suspended
2702 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2703 if (index < 0) {
2704 return;
2705 }
2706 if (!isEffectEligibleForSuspend(effect->desc())) {
2707 return;
2708 }
2709 setEffectSuspended_l(&effect->desc().type, enabled);
2710 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2711 if (index < 0) {
2712 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2713 return;
2714 }
2715 }
2716 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2717 effect->desc().type.timeLow);
2718 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002719 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002720 if (desc->mEffect == 0) {
2721 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002722 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002723 effect->setSuspended(true);
2724 }
2725 } else {
2726 if (index < 0) {
2727 return;
2728 }
2729 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2730 effect->desc().type.timeLow);
2731 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2732 desc->mEffect.clear();
2733 effect->setSuspended(false);
2734 }
2735}
2736
Eric Laurent5baf2af2013-09-12 17:37:00 -07002737bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002738{
2739 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002740 return isNonOffloadableEnabled_l();
2741}
2742
2743bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2744{
Eric Laurent813e2a72013-08-31 12:59:48 -07002745 size_t size = mEffects.size();
2746 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002747 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002748 return true;
2749 }
2750 }
2751 return false;
2752}
2753
Eric Laurentaaa44472014-09-12 17:41:50 -07002754void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2755{
2756 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002757 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002758}
2759
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002760void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2761{
2762 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2763 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2764 }
2765 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2766 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2767 }
2768}
2769
2770void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2771{
2772 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2773 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2774 }
2775 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2776 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2777 }
2778}
2779
2780bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002781{
2782 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002783 for (const auto &effect : mEffects) {
2784 if (effect->isProcessImplemented()) {
2785 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002786 }
2787 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002788 // Allow effects without processing.
2789 return true;
2790}
2791
2792bool AudioFlinger::EffectChain::isFastCompatible() const
2793{
2794 Mutex::Autolock _l(mLock);
2795 for (const auto &effect : mEffects) {
2796 if (effect->isProcessImplemented()
2797 && effect->isImplementationSoftware()) {
2798 return false;
2799 }
2800 }
2801 // Allow effects without processing or hw accelerated effects.
2802 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002803}
2804
2805// isCompatibleWithThread_l() must be called with thread->mLock held
2806bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2807{
2808 Mutex::Autolock _l(mLock);
2809 for (size_t i = 0; i < mEffects.size(); i++) {
2810 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2811 return false;
2812 }
2813 }
2814 return true;
2815}
2816
Eric Laurent6b446ce2019-12-13 10:56:31 -08002817// EffectCallbackInterface implementation
2818status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2819 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2820 sp<EffectHalInterface> *effect) {
2821 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002822 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002823 if (effectsFactory != 0) {
2824 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2825 }
2826 return status;
2827}
2828
2829bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002830 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002831 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002832 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002833}
2834
2835status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2836 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002837 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002838}
2839
2840status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2841 sp<EffectHalInterface> effect) {
2842 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002843 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002844 if (t == nullptr) {
2845 return result;
2846 }
2847 sp <StreamHalInterface> st = t->stream();
2848 if (st == nullptr) {
2849 return result;
2850 }
2851 result = st->addEffect(effect);
2852 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2853 return result;
2854}
2855
2856status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2857 sp<EffectHalInterface> effect) {
2858 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002859 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002860 if (t == nullptr) {
2861 return result;
2862 }
2863 sp <StreamHalInterface> st = t->stream();
2864 if (st == nullptr) {
2865 return result;
2866 }
2867 result = st->removeEffect(effect);
2868 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2869 return result;
2870}
2871
2872audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002873 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002874 if (t == nullptr) {
2875 return AUDIO_IO_HANDLE_NONE;
2876 }
2877 return t->id();
2878}
2879
2880bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002881 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002882 if (t == nullptr) {
2883 return true;
2884 }
2885 return t->isOutput();
2886}
2887
2888bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002889 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002890 if (t == nullptr) {
2891 return false;
2892 }
2893 return t->type() == ThreadBase::OFFLOAD;
2894}
2895
2896bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002897 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002898 if (t == nullptr) {
2899 return false;
2900 }
2901 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2902}
2903
2904bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002905 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002906 if (t == nullptr) {
2907 return false;
2908 }
Andy Hungea840382020-05-05 21:50:17 -07002909 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002910}
2911
2912uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002913 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002914 if (t == nullptr) {
2915 return 0;
2916 }
2917 return t->sampleRate();
2918}
2919
2920audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002921 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002922 if (t == nullptr) {
2923 return AUDIO_CHANNEL_NONE;
2924 }
2925 return t->channelMask();
2926}
2927
2928uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002929 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002930 if (t == nullptr) {
2931 return 0;
2932 }
2933 return t->channelCount();
2934}
2935
jiabineb3bda02020-06-30 14:07:03 -07002936audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002937 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07002938 if (t == nullptr) {
2939 return AUDIO_CHANNEL_NONE;
2940 }
2941 return t->hapticChannelMask();
2942}
2943
Eric Laurent6b446ce2019-12-13 10:56:31 -08002944size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002945 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002946 if (t == nullptr) {
2947 return 0;
2948 }
2949 return t->frameCount();
2950}
2951
2952uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08002953 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002954 if (t == nullptr) {
2955 return 0;
2956 }
2957 return t->latency_l();
2958}
2959
2960void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08002961 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002962 if (t == nullptr) {
2963 return;
2964 }
2965 t->setVolumeForOutput_l(left, right);
2966}
2967
2968void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002969 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
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;
2973 }
2974 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2975
Andy Hung328d6772021-01-12 12:32:21 -08002976 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002977 if (c == nullptr) {
2978 return;
2979 }
Eric Laurent41709552019-12-16 19:34:05 -08002980 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2981 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002982}
2983
Eric Laurent41709552019-12-16 19:34:05 -08002984void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08002985 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002986 if (t == nullptr) {
2987 return;
2988 }
Eric Laurent41709552019-12-16 19:34:05 -08002989 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2990 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002991}
2992
Eric Laurent41709552019-12-16 19:34:05 -08002993void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002994 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2995
Andy Hung328d6772021-01-12 12:32:21 -08002996 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997 if (t == nullptr) {
2998 return;
2999 }
3000 t->onEffectDisable();
3001}
3002
3003bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3004 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003005 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003006 if (t == nullptr) {
3007 return false;
3008 }
3009 t->disconnectEffectHandle(handle, unpinIfLast);
3010 return true;
3011}
3012
3013void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003014 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003015 if (c == nullptr) {
3016 return;
3017 }
3018 c->resetVolume_l();
3019
3020}
3021
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003022product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003023 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003024 if (c == nullptr) {
3025 return PRODUCT_STRATEGY_NONE;
3026 }
3027 return c->strategy();
3028}
3029
3030int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
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 0;
3034 }
3035 return c->activeTrackCnt();
3036}
3037
Eric Laurentb82e6b72019-11-22 17:25:04 -08003038
3039#undef LOG_TAG
3040#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3041
3042status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3043{
3044 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3045 Mutex::Autolock _l(mProxyLock);
3046 if (status == NO_ERROR) {
3047 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003048 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003049 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003050 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003051 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003052 bs = handle.second->disable(&status);
3053 }
3054 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003055 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003056 }
3057 }
3058 }
3059 ALOGV("%s enable %d status %d", __func__, enabled, status);
3060 return status;
3061}
3062
3063status_t AudioFlinger::DeviceEffectProxy::init(
3064 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3065//For all audio patches
3066//If src or sink device match
3067//If the effect is HW accelerated
3068// if no corresponding effect module
3069// Create EffectModule: mHalEffect
3070//Create and attach EffectHandle
3071//If the effect is not HW accelerated and the patch sink or src is a mixer port
3072// Create Effect on patch input or output thread on session -1
3073//Add EffectHandle to EffectHandle map of Effect Proxy:
3074 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3075 status_t status = NO_ERROR;
3076 for (auto &patch : patches) {
3077 status = onCreatePatch(patch.first, patch.second);
3078 ALOGV("%s onCreatePatch status %d", __func__, status);
3079 if (status == BAD_VALUE) {
3080 return status;
3081 }
3082 }
3083 return status;
3084}
3085
3086status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3087 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3088 status_t status = NAME_NOT_FOUND;
3089 sp<EffectHandle> handle;
3090 // only consider source[0] as this is the only "true" source of a patch
3091 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3092 ALOGV("%s source checkPort status %d", __func__, status);
3093 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3094 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3095 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3096 }
3097 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3098 Mutex::Autolock _l(mProxyLock);
3099 mEffectHandles.emplace(patchHandle, handle);
3100 }
3101 ALOGW_IF(status == BAD_VALUE,
3102 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3103
3104 return status;
3105}
3106
3107status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3108 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3109
3110 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3111 __func__, port->type, port->ext.device.type,
3112 port->ext.device.address, port->id, patch.isSoftware());
3113 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003114 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003115 return NAME_NOT_FOUND;
3116 }
3117 status_t status = NAME_NOT_FOUND;
3118
3119 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3120 Mutex::Autolock _l(mProxyLock);
3121 mDevicePort = *port;
3122 mHalEffect = new EffectModule(mMyCallback,
3123 const_cast<effect_descriptor_t *>(&mDescriptor),
3124 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3125 false /* pinned */, port->id);
3126 if (audio_is_input_device(mDevice.mType)) {
3127 mHalEffect->setInputDevice(mDevice);
3128 } else {
3129 mHalEffect->setDevices({mDevice});
3130 }
3131 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3132 status = (*handle)->initCheck();
3133 if (status == OK) {
3134 status = mHalEffect->addHandle((*handle).get());
3135 } else {
3136 mHalEffect.clear();
3137 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3138 }
3139 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3140 sp <ThreadBase> thread;
3141 if (audio_port_config_has_input_direction(port)) {
3142 if (patch.isSoftware()) {
3143 thread = patch.mRecord.thread();
3144 } else {
3145 thread = patch.thread().promote();
3146 }
3147 } else {
3148 if (patch.isSoftware()) {
3149 thread = patch.mPlayback.thread();
3150 } else {
3151 thread = patch.thread().promote();
3152 }
3153 }
3154 int enabled;
3155 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3156 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003157 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003158 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3159 } else {
3160 status = BAD_VALUE;
3161 }
3162 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003163 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003164 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003165 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003166 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003167 bs = (*handle)->disable(&status);
3168 }
3169 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003170 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003171 }
3172 }
3173 return status;
3174}
3175
3176void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3177 Mutex::Autolock _l(mProxyLock);
3178 mEffectHandles.erase(patchHandle);
3179}
3180
3181
3182size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3183{
3184 Mutex::Autolock _l(mProxyLock);
3185 if (effect == mHalEffect) {
3186 mHalEffect.clear();
3187 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3188 }
3189 return mHalEffect == nullptr ? 0 : 1;
3190}
3191
3192status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3193 sp<EffectHalInterface> effect) {
3194 if (mHalEffect == nullptr) {
3195 return NO_INIT;
3196 }
3197 return mManagerCallback->addEffectToHal(
3198 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3199}
3200
3201status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3202 sp<EffectHalInterface> effect) {
3203 if (mHalEffect == nullptr) {
3204 return NO_INIT;
3205 }
3206 return mManagerCallback->removeEffectFromHal(
3207 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3208}
3209
3210bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3211 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3212 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3213 }
3214 return true;
3215}
3216
3217uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3218 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3219 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3220 return mDevicePort.sample_rate;
3221 }
3222 return DEFAULT_OUTPUT_SAMPLE_RATE;
3223}
3224
3225audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3226 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3227 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3228 return mDevicePort.channel_mask;
3229 }
3230 return AUDIO_CHANNEL_OUT_STEREO;
3231}
3232
3233uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3234 if (isOutput()) {
3235 return audio_channel_count_from_out_mask(channelMask());
3236 }
3237 return audio_channel_count_from_in_mask(channelMask());
3238}
3239
3240void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3241 const Vector<String16> args;
3242 EffectBase::dump(fd, args);
3243
3244 const bool locked = dumpTryLock(mProxyLock);
3245 if (!locked) {
3246 String8 result("DeviceEffectProxy may be deadlocked\n");
3247 write(fd, result.string(), result.size());
3248 }
3249
3250 String8 outStr;
3251 if (mHalEffect != nullptr) {
3252 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3253 } else {
3254 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3255 }
3256 write(fd, outStr.string(), outStr.size());
3257 outStr.clear();
3258
3259 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3260 write(fd, outStr.string(), outStr.size());
3261 outStr.clear();
3262
3263 for (const auto& iter : mEffectHandles) {
3264 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3265 write(fd, outStr.string(), outStr.size());
3266 outStr.clear();
3267 sp<EffectBase> effect = iter.second->effect().promote();
3268 if (effect != nullptr) {
3269 effect->dump(fd, args);
3270 }
3271 }
3272
3273 if (locked) {
3274 mLock.unlock();
3275 }
3276}
3277
3278#undef LOG_TAG
3279#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3280
3281int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3282 return mManagerCallback->newEffectId();
3283}
3284
3285
3286bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3287 EffectHandle *handle, bool unpinIfLast) {
3288 sp<EffectBase> effectBase = handle->effect().promote();
3289 if (effectBase == nullptr) {
3290 return false;
3291 }
3292
3293 sp<EffectModule> effect = effectBase->asEffectModule();
3294 if (effect == nullptr) {
3295 return false;
3296 }
3297
3298 // restore suspended effects if the disconnected handle was enabled and the last one.
3299 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3300 if (remove) {
3301 sp<DeviceEffectProxy> proxy = mProxy.promote();
3302 if (proxy != nullptr) {
3303 proxy->removeEffect(effect);
3304 }
3305 if (handle->enabled()) {
3306 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3307 }
3308 }
3309 return true;
3310}
3311
3312status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3313 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3314 sp<EffectHalInterface> *effect) {
3315 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3316}
3317
3318status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3319 sp<EffectHalInterface> effect) {
3320 sp<DeviceEffectProxy> proxy = mProxy.promote();
3321 if (proxy == nullptr) {
3322 return NO_INIT;
3323 }
3324 return proxy->addEffectToHal(effect);
3325}
3326
3327status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3328 sp<EffectHalInterface> effect) {
3329 sp<DeviceEffectProxy> proxy = mProxy.promote();
3330 if (proxy == nullptr) {
3331 return NO_INIT;
3332 }
3333 return proxy->addEffectToHal(effect);
3334}
3335
3336bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3337 sp<DeviceEffectProxy> proxy = mProxy.promote();
3338 if (proxy == nullptr) {
3339 return true;
3340 }
3341 return proxy->isOutput();
3342}
3343
3344uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3345 sp<DeviceEffectProxy> proxy = mProxy.promote();
3346 if (proxy == nullptr) {
3347 return DEFAULT_OUTPUT_SAMPLE_RATE;
3348 }
3349 return proxy->sampleRate();
3350}
3351
3352audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3353 sp<DeviceEffectProxy> proxy = mProxy.promote();
3354 if (proxy == nullptr) {
3355 return AUDIO_CHANNEL_OUT_STEREO;
3356 }
3357 return proxy->channelMask();
3358}
3359
3360uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3361 sp<DeviceEffectProxy> proxy = mProxy.promote();
3362 if (proxy == nullptr) {
3363 return 2;
3364 }
3365 return proxy->channelCount();
3366}
3367
Glenn Kasten63238ef2015-03-02 15:50:29 -08003368} // namespace android