blob: 031e0cfdeaa442e99d97722697460dff79cb1d90 [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
Andy Hungbded9c82017-11-30 18:47:35 -08001588static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1589 std::stringstream ss;
1590
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001591 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001592 return "nullptr"; // make different than below
1593 } else if (buffer->externalData() != nullptr) {
1594 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1595 << " -> "
1596 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1597 } else {
1598 ss << buffer->audioBuffer()->raw;
1599 }
1600 return ss.str();
1601}
Marco Nelissenb2208842014-02-07 14:00:50 -08001602
Eric Laurent41709552019-12-16 19:34:05 -08001603void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001604{
Eric Laurent41709552019-12-16 19:34:05 -08001605 EffectBase::dump(fd, args);
1606
Eric Laurentca7cc822012-11-19 14:55:58 -08001607 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001608 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001609
Eric Laurent41709552019-12-16 19:34:05 -08001610 result.append("\t\tStatus Engine:\n");
1611 result.appendFormat("\t\t%03d %p\n",
1612 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001613
1614 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001615
1616 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001617 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1618 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1619 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 mConfig.inputCfg.buffer.frameCount,
1621 mConfig.inputCfg.samplingRate,
1622 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001623 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001624 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001625
1626 result.append("\t\t- Output configuration:\n");
1627 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001628 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001629 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001630 mConfig.outputCfg.buffer.frameCount,
1631 mConfig.outputCfg.samplingRate,
1632 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001633 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001634 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001635
rago94a1ee82017-07-21 15:11:02 -07001636#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001637
Andy Hungbded9c82017-11-30 18:47:35 -08001638 result.appendFormat("\t\t- HAL buffers:\n"
1639 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1640 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1641 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1642 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1643 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001644#endif
1645
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 write(fd, result.string(), result.length());
1647
Mikhail Naganov4d547672019-02-22 14:19:19 -08001648 if (mEffectInterface != 0) {
1649 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1650 (void)mEffectInterface->dump(fd);
1651 }
1652
Eric Laurentca7cc822012-11-19 14:55:58 -08001653 if (locked) {
1654 mLock.unlock();
1655 }
1656}
1657
1658// ----------------------------------------------------------------------------
1659// EffectHandle implementation
1660// ----------------------------------------------------------------------------
1661
1662#undef LOG_TAG
1663#define LOG_TAG "AudioFlinger::EffectHandle"
1664
Eric Laurent41709552019-12-16 19:34:05 -08001665AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001666 const sp<AudioFlinger::Client>& client,
1667 const sp<media::IEffectClient>& effectClient,
1668 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001669 : BnEffect(),
1670 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001671 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001672{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001673 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001674
1675 if (client == 0) {
1676 return;
1677 }
1678 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1679 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001680 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001681 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001682 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001683 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001684 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001685 return;
1686 }
Glenn Kastene75da402013-11-20 13:54:52 -08001687 new(mCblk) effect_param_cblk_t();
1688 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001689}
1690
1691AudioFlinger::EffectHandle::~EffectHandle()
1692{
1693 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001694 disconnect(false);
1695}
1696
Glenn Kastene75da402013-11-20 13:54:52 -08001697status_t AudioFlinger::EffectHandle::initCheck()
1698{
1699 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1700}
1701
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001702#define RETURN(code) \
1703 *_aidl_return = (code); \
1704 return Status::ok();
1705
1706Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001707{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001708 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001709 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001710 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001711 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001712 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001713 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001715 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001716 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001717
1718 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001719 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 }
1721
1722 mEnabled = true;
1723
Eric Laurent6c796322019-04-09 14:13:17 -07001724 status_t status = effect->updatePolicyState();
1725 if (status != NO_ERROR) {
1726 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001727 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001728 }
1729
Eric Laurent6b446ce2019-12-13 10:56:31 -08001730 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001731
1732 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001733 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001734 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 }
1736
Eric Laurent6b446ce2019-12-13 10:56:31 -08001737 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001738 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 mEnabled = false;
1740 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001741 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001742}
1743
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001744Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001745{
1746 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001747 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001748 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001749 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001750 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001751 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001753 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001754 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001755
1756 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001757 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 }
1759 mEnabled = false;
1760
Eric Laurent6c796322019-04-09 14:13:17 -07001761 effect->updatePolicyState();
1762
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001763 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001764 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001765 }
1766
Eric Laurent6b446ce2019-12-13 10:56:31 -08001767 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001768 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001769}
1770
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001771Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001772{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001773 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001774 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001775 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001776}
1777
1778void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1779{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001780 AutoMutex _l(mLock);
1781 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1782 if (mDisconnected) {
1783 if (unpinIfLast) {
1784 android_errorWriteLog(0x534e4554, "32707507");
1785 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001786 return;
1787 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001788 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001789 {
Eric Laurent41709552019-12-16 19:34:05 -08001790 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001791 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001792 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001793 ALOGW("%s Effect handle %p disconnected after thread destruction",
1794 __func__, this);
1795 }
1796 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001797 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001798 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001799
Eric Laurentca7cc822012-11-19 14:55:58 -08001800 if (mClient != 0) {
1801 if (mCblk != NULL) {
1802 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1803 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1804 }
1805 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001806 // Client destructor must run with AudioFlinger client mutex locked
1807 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 mClient.clear();
1809 }
1810}
1811
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001812Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1813 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1814 return Status::ok();
1815}
1816
1817Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1818 const std::vector<uint8_t>& cmdData,
1819 int32_t maxResponseSize,
1820 std::vector<uint8_t>* response,
1821 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001822{
1823 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001824 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001825
Eric Laurentc7ab3092017-06-15 18:43:46 -07001826 // reject commands reserved for internal use by audio framework if coming from outside
1827 // of audioserver
1828 switch(cmdCode) {
1829 case EFFECT_CMD_ENABLE:
1830 case EFFECT_CMD_DISABLE:
1831 case EFFECT_CMD_SET_PARAM:
1832 case EFFECT_CMD_SET_PARAM_DEFERRED:
1833 case EFFECT_CMD_SET_PARAM_COMMIT:
1834 case EFFECT_CMD_GET_PARAM:
1835 break;
1836 default:
1837 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1838 break;
1839 }
1840 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001842 }
1843
Eric Laurent1ffc5852016-12-15 14:46:09 -08001844 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001845 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001846 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001847 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001848 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 writeToBuffer(NO_ERROR, response);
1850 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001851 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001852 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001853 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001854 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001855 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 writeToBuffer(NO_ERROR, response);
1857 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001858 }
1859
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001860 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001861 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001862 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001863 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001864 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 // only get parameter command is permitted for applications not controlling the effect
1866 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001869
1870 // handle commands that are not forwarded transparently to effect engine
1871 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001872 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001874 }
1875
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001876 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001877 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001878 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001879 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001880 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001881
Eric Laurentca7cc822012-11-19 14:55:58 -08001882 // No need to trylock() here as this function is executed in the binder thread serving a
1883 // particular client process: no risk to block the whole media server process or mixer
1884 // threads if we are stuck here
1885 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001886 // keep local copy of index in case of client corruption b/32220769
1887 const uint32_t clientIndex = mCblk->clientIndex;
1888 const uint32_t serverIndex = mCblk->serverIndex;
1889 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1890 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001891 mCblk->serverIndex = 0;
1892 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001893 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001894 }
1895 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001896 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001897 for (uint32_t index = serverIndex; index < clientIndex;) {
1898 int *p = (int *)(mBuffer + index);
1899 const int size = *p++;
1900 if (size < 0
1901 || size > EFFECT_PARAM_BUFFER_SIZE
1902 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001903 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001904 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001905 break;
1906 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001907
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 std::copy(reinterpret_cast<const uint8_t*>(p),
1909 reinterpret_cast<const uint8_t*>(p) + size,
1910 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001911
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001913 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001914 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001915 sizeof(int),
1916 &replyBuffer);
1917 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001918
1919 // verify shared memory: server index shouldn't change; client index can't go back.
1920 if (serverIndex != mCblk->serverIndex
1921 || clientIndex > mCblk->clientIndex) {
1922 android_errorWriteLog(0x534e4554, "32220769");
1923 status = BAD_VALUE;
1924 break;
1925 }
1926
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 // stop at first error encountered
1928 if (ret != NO_ERROR) {
1929 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001930 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 break;
1932 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001933 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 break;
1935 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001936 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 }
1938 mCblk->serverIndex = 0;
1939 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001940 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 }
1942
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001943 status_t status = effect->command(cmdCode,
1944 cmdData,
1945 maxResponseSize,
1946 response);
1947 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001948}
1949
1950void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1951{
1952 ALOGV("setControl %p control %d", this, hasControl);
1953
1954 mHasControl = hasControl;
1955 mEnabled = enabled;
1956
1957 if (signal && mEffectClient != 0) {
1958 mEffectClient->controlStatusChanged(hasControl);
1959 }
1960}
1961
1962void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001963 const std::vector<uint8_t>& cmdData,
1964 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001965{
1966 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001968 }
1969}
1970
1971
1972
1973void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1974{
1975 if (mEffectClient != 0) {
1976 mEffectClient->enableStatusChanged(enabled);
1977 }
1978}
1979
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001980void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001981{
1982 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1983
Marco Nelissenb2208842014-02-07 14:00:50 -08001984 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001985 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001987 mHasControl ? "yes" : "no",
1988 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001989 mCblk ? mCblk->clientIndex : 0,
1990 mCblk ? mCblk->serverIndex : 0
1991 );
1992
1993 if (locked) {
1994 mCblk->lock.unlock();
1995 }
1996}
1997
1998#undef LOG_TAG
1999#define LOG_TAG "AudioFlinger::EffectChain"
2000
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002001AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2002 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002003 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002004 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002005 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002006 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002007{
2008 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002009 sp<ThreadBase> p = thread.promote();
2010 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002011 return;
2012 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002013 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2014 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002015}
2016
2017AudioFlinger::EffectChain::~EffectChain()
2018{
Eric Laurentca7cc822012-11-19 14:55:58 -08002019}
2020
2021// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2022sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2023 effect_descriptor_t *descriptor)
2024{
2025 size_t size = mEffects.size();
2026
2027 for (size_t i = 0; i < size; i++) {
2028 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2029 return mEffects[i];
2030 }
2031 }
2032 return 0;
2033}
2034
2035// getEffectFromId_l() must be called with ThreadBase::mLock held
2036sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2037{
2038 size_t size = mEffects.size();
2039
2040 for (size_t i = 0; i < size; i++) {
2041 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2042 if (id == 0 || mEffects[i]->id() == id) {
2043 return mEffects[i];
2044 }
2045 }
2046 return 0;
2047}
2048
2049// getEffectFromType_l() must be called with ThreadBase::mLock held
2050sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2051 const effect_uuid_t *type)
2052{
2053 size_t size = mEffects.size();
2054
2055 for (size_t i = 0; i < size; i++) {
2056 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2057 return mEffects[i];
2058 }
2059 }
2060 return 0;
2061}
2062
Eric Laurent6c796322019-04-09 14:13:17 -07002063std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2064{
2065 std::vector<int> ids;
2066 Mutex::Autolock _l(mLock);
2067 for (size_t i = 0; i < mEffects.size(); i++) {
2068 ids.push_back(mEffects[i]->id());
2069 }
2070 return ids;
2071}
2072
Eric Laurentca7cc822012-11-19 14:55:58 -08002073void AudioFlinger::EffectChain::clearInputBuffer()
2074{
2075 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002076 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002077}
2078
2079// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002080void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002081{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002082 if (mInBuffer == NULL) {
2083 return;
2084 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002085 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002086 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002087
Eric Laurent6b446ce2019-12-13 10:56:31 -08002088 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002089 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002090}
2091
2092// Must be called with EffectChain::mLock locked
2093void AudioFlinger::EffectChain::process_l()
2094{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002095 // never process effects when:
2096 // - on an OFFLOAD thread
2097 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002098 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002099 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002100 bool tracksOnSession = (trackCnt() != 0);
2101
2102 if (!tracksOnSession && mTailBufferCount == 0) {
2103 doProcess = false;
2104 }
2105
2106 if (activeTrackCnt() == 0) {
2107 // if no track is active and the effect tail has not been rendered,
2108 // the input buffer must be cleared here as the mixer process will not do it
2109 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002110 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002111 if (mTailBufferCount > 0) {
2112 mTailBufferCount--;
2113 }
2114 }
2115 }
2116 }
2117
2118 size_t size = mEffects.size();
2119 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002120 // Only the input and output buffers of the chain can be external,
2121 // and 'update' / 'commit' do nothing for allocated buffers, thus
2122 // it's not needed to consider any other buffers here.
2123 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002124 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2125 mOutBuffer->update();
2126 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002127 for (size_t i = 0; i < size; i++) {
2128 mEffects[i]->process();
2129 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002130 mInBuffer->commit();
2131 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2132 mOutBuffer->commit();
2133 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002134 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002135 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002136 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002137 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2138 }
2139 if (doResetVolume) {
2140 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002141 }
2142}
2143
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002144// createEffect_l() must be called with ThreadBase::mLock held
2145status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002146 effect_descriptor_t *desc,
2147 int id,
2148 audio_session_t sessionId,
2149 bool pinned)
2150{
2151 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002152 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002153 status_t lStatus = effect->status();
2154 if (lStatus == NO_ERROR) {
2155 lStatus = addEffect_ll(effect);
2156 }
2157 if (lStatus != NO_ERROR) {
2158 effect.clear();
2159 }
2160 return lStatus;
2161}
2162
2163// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002164status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2165{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002166 Mutex::Autolock _l(mLock);
2167 return addEffect_ll(effect);
2168}
2169// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2170status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2171{
Eric Laurentca7cc822012-11-19 14:55:58 -08002172 effect_descriptor_t desc = effect->desc();
2173 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2174
Eric Laurent6b446ce2019-12-13 10:56:31 -08002175 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002176
2177 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2178 // Auxiliary effects are inserted at the beginning of mEffects vector as
2179 // they are processed first and accumulated in chain input buffer
2180 mEffects.insertAt(effect, 0);
2181
2182 // the input buffer for auxiliary effect contains mono samples in
2183 // 32 bit format. This is to avoid saturation in AudoMixer
2184 // accumulation stage. Saturation is done in EffectModule::process() before
2185 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002186 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002187 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002188#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002189 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002190 numSamples * sizeof(float), &halBuffer);
2191#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002192 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002193 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002194#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002195 if (result != OK) return result;
2196 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002197 // auxiliary effects output samples to chain input buffer for further processing
2198 // by insert effects
2199 effect->setOutBuffer(mInBuffer);
2200 } else {
2201 // Insert effects are inserted at the end of mEffects vector as they are processed
2202 // after track and auxiliary effects.
2203 // Insert effect order as a function of indicated preference:
2204 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2205 // another effect is present
2206 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2207 // last effect claiming first position
2208 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2209 // first effect claiming last position
2210 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2211 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2212 // already present
2213
2214 size_t size = mEffects.size();
2215 size_t idx_insert = size;
2216 ssize_t idx_insert_first = -1;
2217 ssize_t idx_insert_last = -1;
2218
2219 for (size_t i = 0; i < size; i++) {
2220 effect_descriptor_t d = mEffects[i]->desc();
2221 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2222 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2223 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2224 // check invalid effect chaining combinations
2225 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2226 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2227 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2228 desc.name, d.name);
2229 return INVALID_OPERATION;
2230 }
2231 // remember position of first insert effect and by default
2232 // select this as insert position for new effect
2233 if (idx_insert == size) {
2234 idx_insert = i;
2235 }
2236 // remember position of last insert effect claiming
2237 // first position
2238 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2239 idx_insert_first = i;
2240 }
2241 // remember position of first insert effect claiming
2242 // last position
2243 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2244 idx_insert_last == -1) {
2245 idx_insert_last = i;
2246 }
2247 }
2248 }
2249
2250 // modify idx_insert from first position if needed
2251 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2252 if (idx_insert_last != -1) {
2253 idx_insert = idx_insert_last;
2254 } else {
2255 idx_insert = size;
2256 }
2257 } else {
2258 if (idx_insert_first != -1) {
2259 idx_insert = idx_insert_first + 1;
2260 }
2261 }
2262
2263 // always read samples from chain input buffer
2264 effect->setInBuffer(mInBuffer);
2265
2266 // if last effect in the chain, output samples to chain
2267 // output buffer, otherwise to chain input buffer
2268 if (idx_insert == size) {
2269 if (idx_insert != 0) {
2270 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2271 mEffects[idx_insert-1]->configure();
2272 }
2273 effect->setOutBuffer(mOutBuffer);
2274 } else {
2275 effect->setOutBuffer(mInBuffer);
2276 }
2277 mEffects.insertAt(effect, idx_insert);
2278
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002279 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002280 idx_insert);
2281 }
2282 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002283
Eric Laurentca7cc822012-11-19 14:55:58 -08002284 return NO_ERROR;
2285}
2286
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002287// removeEffect_l() must be called with ThreadBase::mLock held
2288size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2289 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002290{
2291 Mutex::Autolock _l(mLock);
2292 size_t size = mEffects.size();
2293 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2294
2295 for (size_t i = 0; i < size; i++) {
2296 if (effect == mEffects[i]) {
2297 // calling stop here will remove pre-processing effect from the audio HAL.
2298 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2299 // the middle of a read from audio HAL
2300 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2301 mEffects[i]->state() == EffectModule::STOPPING) {
2302 mEffects[i]->stop();
2303 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002304 if (release) {
2305 mEffects[i]->release_l();
2306 }
2307
Mikhail Naganov022b9952017-01-04 16:36:51 -08002308 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 if (i == size - 1 && i != 0) {
2310 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2311 mEffects[i - 1]->configure();
2312 }
2313 }
2314 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002315 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002316 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002317
Eric Laurentca7cc822012-11-19 14:55:58 -08002318 break;
2319 }
2320 }
2321
2322 return mEffects.size();
2323}
2324
jiabin8f278ee2019-11-11 12:16:27 -08002325// setDevices_l() must be called with ThreadBase::mLock held
2326void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002327{
2328 size_t size = mEffects.size();
2329 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002330 mEffects[i]->setDevices(devices);
2331 }
2332}
2333
2334// setInputDevice_l() must be called with ThreadBase::mLock held
2335void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2336{
2337 size_t size = mEffects.size();
2338 for (size_t i = 0; i < size; i++) {
2339 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002340 }
2341}
2342
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002343// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002344void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2345{
2346 size_t size = mEffects.size();
2347 for (size_t i = 0; i < size; i++) {
2348 mEffects[i]->setMode(mode);
2349 }
2350}
2351
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002352// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002353void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2354{
2355 size_t size = mEffects.size();
2356 for (size_t i = 0; i < size; i++) {
2357 mEffects[i]->setAudioSource(source);
2358 }
2359}
2360
Zhou Songd505c642020-02-20 16:35:37 +08002361bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2362 for (const auto &effect : mEffects) {
2363 if (effect->isVolumeControlEnabled()) return true;
2364 }
2365 return false;
2366}
2367
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002368// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002369bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002370{
2371 uint32_t newLeft = *left;
2372 uint32_t newRight = *right;
2373 bool hasControl = false;
2374 int ctrlIdx = -1;
2375 size_t size = mEffects.size();
2376
2377 // first update volume controller
2378 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002379 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002380 ctrlIdx = i - 1;
2381 hasControl = true;
2382 break;
2383 }
2384 }
2385
Eric Laurentfa1e1232016-08-02 19:01:49 -07002386 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002387 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002388 if (hasControl) {
2389 *left = mNewLeftVolume;
2390 *right = mNewRightVolume;
2391 }
2392 return hasControl;
2393 }
2394
2395 mVolumeCtrlIdx = ctrlIdx;
2396 mLeftVolume = newLeft;
2397 mRightVolume = newRight;
2398
2399 // second get volume update from volume controller
2400 if (ctrlIdx >= 0) {
2401 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2402 mNewLeftVolume = newLeft;
2403 mNewRightVolume = newRight;
2404 }
2405 // then indicate volume to all other effects in chain.
2406 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002407 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002408 uint32_t lVol = newLeft;
2409 uint32_t rVol = newRight;
2410
2411 for (size_t i = 0; i < size; i++) {
2412 if ((int)i == ctrlIdx) {
2413 continue;
2414 }
2415 // this also works for ctrlIdx == -1 when there is no volume controller
2416 if ((int)i > ctrlIdx) {
2417 lVol = *left;
2418 rVol = *right;
2419 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002420 // Pass requested volume directly if this is volume monitor module
2421 if (mEffects[i]->isVolumeMonitor()) {
2422 mEffects[i]->setVolume(left, right, false);
2423 } else {
2424 mEffects[i]->setVolume(&lVol, &rVol, false);
2425 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002426 }
2427 *left = newLeft;
2428 *right = newRight;
2429
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002430 setVolumeForOutput_l(*left, *right);
2431
Eric Laurentca7cc822012-11-19 14:55:58 -08002432 return hasControl;
2433}
2434
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002435// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002436void AudioFlinger::EffectChain::resetVolume_l()
2437{
Eric Laurente7449bf2016-08-03 18:44:07 -07002438 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2439 uint32_t left = mLeftVolume;
2440 uint32_t right = mRightVolume;
2441 (void)setVolume_l(&left, &right, true);
2442 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002443}
2444
jiabineb3bda02020-06-30 14:07:03 -07002445// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2446bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2447{
2448 for (size_t i = 0; i < mEffects.size(); ++i) {
2449 if (mEffects[i]->isHapticGenerator()) {
2450 return true;
2451 }
2452 }
2453 return false;
2454}
2455
jiabine70bc7f2020-06-30 22:07:55 -07002456void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2457{
2458 Mutex::Autolock _l(mLock);
2459 for (size_t i = 0; i < mEffects.size(); ++i) {
2460 mEffects[i]->setHapticIntensity(id, intensity);
2461 }
2462}
2463
Eric Laurent1b928682014-10-02 19:41:47 -07002464void AudioFlinger::EffectChain::syncHalEffectsState()
2465{
2466 Mutex::Autolock _l(mLock);
2467 for (size_t i = 0; i < mEffects.size(); i++) {
2468 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2469 mEffects[i]->state() == EffectModule::STOPPING) {
2470 mEffects[i]->addEffectToHal_l();
2471 }
2472 }
2473}
2474
Eric Laurentca7cc822012-11-19 14:55:58 -08002475void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2476{
Eric Laurentca7cc822012-11-19 14:55:58 -08002477 String8 result;
2478
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002479 const size_t numEffects = mEffects.size();
2480 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002481
Marco Nelissenb2208842014-02-07 14:00:50 -08002482 if (numEffects) {
2483 bool locked = AudioFlinger::dumpTryLock(mLock);
2484 // failed to lock - AudioFlinger is probably deadlocked
2485 if (!locked) {
2486 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002487 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002488
Andy Hungbded9c82017-11-30 18:47:35 -08002489 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2490 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2491 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2492 (int)inBufferStr.size(), "In buffer ",
2493 (int)outBufferStr.size(), "Out buffer ");
2494 result.appendFormat("\t%s %s %d\n",
2495 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002496 write(fd, result.string(), result.size());
2497
2498 for (size_t i = 0; i < numEffects; ++i) {
2499 sp<EffectModule> effect = mEffects[i];
2500 if (effect != 0) {
2501 effect->dump(fd, args);
2502 }
2503 }
2504
2505 if (locked) {
2506 mLock.unlock();
2507 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002508 } else {
2509 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002510 }
2511}
2512
2513// must be called with ThreadBase::mLock held
2514void AudioFlinger::EffectChain::setEffectSuspended_l(
2515 const effect_uuid_t *type, bool suspend)
2516{
2517 sp<SuspendedEffectDesc> desc;
2518 // use effect type UUID timelow as key as there is no real risk of identical
2519 // timeLow fields among effect type UUIDs.
2520 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2521 if (suspend) {
2522 if (index >= 0) {
2523 desc = mSuspendedEffects.valueAt(index);
2524 } else {
2525 desc = new SuspendedEffectDesc();
2526 desc->mType = *type;
2527 mSuspendedEffects.add(type->timeLow, desc);
2528 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2529 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002530
Eric Laurentca7cc822012-11-19 14:55:58 -08002531 if (desc->mRefCount++ == 0) {
2532 sp<EffectModule> effect = getEffectIfEnabled(type);
2533 if (effect != 0) {
2534 desc->mEffect = effect;
2535 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002536 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002537 }
2538 }
2539 } else {
2540 if (index < 0) {
2541 return;
2542 }
2543 desc = mSuspendedEffects.valueAt(index);
2544 if (desc->mRefCount <= 0) {
2545 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002546 desc->mRefCount = 0;
2547 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002548 }
2549 if (--desc->mRefCount == 0) {
2550 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2551 if (desc->mEffect != 0) {
2552 sp<EffectModule> effect = desc->mEffect.promote();
2553 if (effect != 0) {
2554 effect->setSuspended(false);
2555 effect->lock();
2556 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002557 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002558 effect->setEnabled_l(handle->enabled());
2559 }
2560 effect->unlock();
2561 }
2562 desc->mEffect.clear();
2563 }
2564 mSuspendedEffects.removeItemsAt(index);
2565 }
2566 }
2567}
2568
2569// must be called with ThreadBase::mLock held
2570void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2571{
2572 sp<SuspendedEffectDesc> desc;
2573
2574 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2575 if (suspend) {
2576 if (index >= 0) {
2577 desc = mSuspendedEffects.valueAt(index);
2578 } else {
2579 desc = new SuspendedEffectDesc();
2580 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2581 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2582 }
2583 if (desc->mRefCount++ == 0) {
2584 Vector< sp<EffectModule> > effects;
2585 getSuspendEligibleEffects(effects);
2586 for (size_t i = 0; i < effects.size(); i++) {
2587 setEffectSuspended_l(&effects[i]->desc().type, true);
2588 }
2589 }
2590 } else {
2591 if (index < 0) {
2592 return;
2593 }
2594 desc = mSuspendedEffects.valueAt(index);
2595 if (desc->mRefCount <= 0) {
2596 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2597 desc->mRefCount = 1;
2598 }
2599 if (--desc->mRefCount == 0) {
2600 Vector<const effect_uuid_t *> types;
2601 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2602 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2603 continue;
2604 }
2605 types.add(&mSuspendedEffects.valueAt(i)->mType);
2606 }
2607 for (size_t i = 0; i < types.size(); i++) {
2608 setEffectSuspended_l(types[i], false);
2609 }
2610 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2611 mSuspendedEffects.keyAt(index));
2612 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2613 }
2614 }
2615}
2616
2617
2618// The volume effect is used for automated tests only
2619#ifndef OPENSL_ES_H_
2620static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2621 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2622const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2623#endif //OPENSL_ES_H_
2624
Eric Laurentd8365c52017-07-16 15:27:05 -07002625/* static */
2626bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2627{
2628 // Only NS and AEC are suspended when BtNRec is off
2629 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2630 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2631 return true;
2632 }
2633 return false;
2634}
2635
Eric Laurentca7cc822012-11-19 14:55:58 -08002636bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2637{
2638 // auxiliary effects and visualizer are never suspended on output mix
2639 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2640 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2641 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002642 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2643 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002644 return false;
2645 }
2646 return true;
2647}
2648
2649void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2650 Vector< sp<AudioFlinger::EffectModule> > &effects)
2651{
2652 effects.clear();
2653 for (size_t i = 0; i < mEffects.size(); i++) {
2654 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2655 effects.add(mEffects[i]);
2656 }
2657 }
2658}
2659
2660sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2661 const effect_uuid_t *type)
2662{
2663 sp<EffectModule> effect = getEffectFromType_l(type);
2664 return effect != 0 && effect->isEnabled() ? effect : 0;
2665}
2666
2667void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2668 bool enabled)
2669{
2670 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2671 if (enabled) {
2672 if (index < 0) {
2673 // if the effect is not suspend check if all effects are suspended
2674 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2675 if (index < 0) {
2676 return;
2677 }
2678 if (!isEffectEligibleForSuspend(effect->desc())) {
2679 return;
2680 }
2681 setEffectSuspended_l(&effect->desc().type, enabled);
2682 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2683 if (index < 0) {
2684 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2685 return;
2686 }
2687 }
2688 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2689 effect->desc().type.timeLow);
2690 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002691 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002692 if (desc->mEffect == 0) {
2693 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002694 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002695 effect->setSuspended(true);
2696 }
2697 } else {
2698 if (index < 0) {
2699 return;
2700 }
2701 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2702 effect->desc().type.timeLow);
2703 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2704 desc->mEffect.clear();
2705 effect->setSuspended(false);
2706 }
2707}
2708
Eric Laurent5baf2af2013-09-12 17:37:00 -07002709bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002710{
2711 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002712 return isNonOffloadableEnabled_l();
2713}
2714
2715bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2716{
Eric Laurent813e2a72013-08-31 12:59:48 -07002717 size_t size = mEffects.size();
2718 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002719 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002720 return true;
2721 }
2722 }
2723 return false;
2724}
2725
Eric Laurentaaa44472014-09-12 17:41:50 -07002726void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2727{
2728 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002729 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002730}
2731
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002732void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2733{
2734 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2735 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2736 }
2737 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2738 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2739 }
2740}
2741
2742void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2743{
2744 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2745 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2746 }
2747 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2748 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2749 }
2750}
2751
2752bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002753{
2754 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002755 for (const auto &effect : mEffects) {
2756 if (effect->isProcessImplemented()) {
2757 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002758 }
2759 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002760 // Allow effects without processing.
2761 return true;
2762}
2763
2764bool AudioFlinger::EffectChain::isFastCompatible() const
2765{
2766 Mutex::Autolock _l(mLock);
2767 for (const auto &effect : mEffects) {
2768 if (effect->isProcessImplemented()
2769 && effect->isImplementationSoftware()) {
2770 return false;
2771 }
2772 }
2773 // Allow effects without processing or hw accelerated effects.
2774 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002775}
2776
2777// isCompatibleWithThread_l() must be called with thread->mLock held
2778bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2779{
2780 Mutex::Autolock _l(mLock);
2781 for (size_t i = 0; i < mEffects.size(); i++) {
2782 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2783 return false;
2784 }
2785 }
2786 return true;
2787}
2788
Eric Laurent6b446ce2019-12-13 10:56:31 -08002789// EffectCallbackInterface implementation
2790status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2791 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2792 sp<EffectHalInterface> *effect) {
2793 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002794 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002795 if (effectsFactory != 0) {
2796 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2797 }
2798 return status;
2799}
2800
2801bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002802 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002803 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002804 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002805}
2806
2807status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2808 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002809 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002810}
2811
2812status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2813 sp<EffectHalInterface> effect) {
2814 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002815 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002816 if (t == nullptr) {
2817 return result;
2818 }
2819 sp <StreamHalInterface> st = t->stream();
2820 if (st == nullptr) {
2821 return result;
2822 }
2823 result = st->addEffect(effect);
2824 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2825 return result;
2826}
2827
2828status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2829 sp<EffectHalInterface> effect) {
2830 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002831 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002832 if (t == nullptr) {
2833 return result;
2834 }
2835 sp <StreamHalInterface> st = t->stream();
2836 if (st == nullptr) {
2837 return result;
2838 }
2839 result = st->removeEffect(effect);
2840 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2841 return result;
2842}
2843
2844audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002845 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002846 if (t == nullptr) {
2847 return AUDIO_IO_HANDLE_NONE;
2848 }
2849 return t->id();
2850}
2851
2852bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002853 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002854 if (t == nullptr) {
2855 return true;
2856 }
2857 return t->isOutput();
2858}
2859
2860bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002861 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002862 if (t == nullptr) {
2863 return false;
2864 }
2865 return t->type() == ThreadBase::OFFLOAD;
2866}
2867
2868bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002869 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002870 if (t == nullptr) {
2871 return false;
2872 }
2873 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2874}
2875
2876bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002877 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002878 if (t == nullptr) {
2879 return false;
2880 }
Andy Hungea840382020-05-05 21:50:17 -07002881 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002882}
2883
2884uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002885 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002886 if (t == nullptr) {
2887 return 0;
2888 }
2889 return t->sampleRate();
2890}
2891
2892audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002893 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002894 if (t == nullptr) {
2895 return AUDIO_CHANNEL_NONE;
2896 }
2897 return t->channelMask();
2898}
2899
2900uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002901 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002902 if (t == nullptr) {
2903 return 0;
2904 }
2905 return t->channelCount();
2906}
2907
jiabineb3bda02020-06-30 14:07:03 -07002908audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002909 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07002910 if (t == nullptr) {
2911 return AUDIO_CHANNEL_NONE;
2912 }
2913 return t->hapticChannelMask();
2914}
2915
Eric Laurent6b446ce2019-12-13 10:56:31 -08002916size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002917 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002918 if (t == nullptr) {
2919 return 0;
2920 }
2921 return t->frameCount();
2922}
2923
2924uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08002925 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002926 if (t == nullptr) {
2927 return 0;
2928 }
2929 return t->latency_l();
2930}
2931
2932void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08002933 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002934 if (t == nullptr) {
2935 return;
2936 }
2937 t->setVolumeForOutput_l(left, right);
2938}
2939
2940void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002941 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08002942 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002943 if (t == nullptr) {
2944 return;
2945 }
2946 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2947
Andy Hung328d6772021-01-12 12:32:21 -08002948 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002949 if (c == nullptr) {
2950 return;
2951 }
Eric Laurent41709552019-12-16 19:34:05 -08002952 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2953 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002954}
2955
Eric Laurent41709552019-12-16 19:34:05 -08002956void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08002957 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002958 if (t == nullptr) {
2959 return;
2960 }
Eric Laurent41709552019-12-16 19:34:05 -08002961 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2962 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002963}
2964
Eric Laurent41709552019-12-16 19:34:05 -08002965void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002966 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2967
Andy Hung328d6772021-01-12 12:32:21 -08002968 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002969 if (t == nullptr) {
2970 return;
2971 }
2972 t->onEffectDisable();
2973}
2974
2975bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2976 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08002977 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002978 if (t == nullptr) {
2979 return false;
2980 }
2981 t->disconnectEffectHandle(handle, unpinIfLast);
2982 return true;
2983}
2984
2985void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08002986 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002987 if (c == nullptr) {
2988 return;
2989 }
2990 c->resetVolume_l();
2991
2992}
2993
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08002994product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08002995 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002996 if (c == nullptr) {
2997 return PRODUCT_STRATEGY_NONE;
2998 }
2999 return c->strategy();
3000}
3001
3002int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003003 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003004 if (c == nullptr) {
3005 return 0;
3006 }
3007 return c->activeTrackCnt();
3008}
3009
Eric Laurentb82e6b72019-11-22 17:25:04 -08003010
3011#undef LOG_TAG
3012#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3013
3014status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3015{
3016 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3017 Mutex::Autolock _l(mProxyLock);
3018 if (status == NO_ERROR) {
3019 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003020 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003021 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003022 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003023 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003024 bs = handle.second->disable(&status);
3025 }
3026 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003027 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003028 }
3029 }
3030 }
3031 ALOGV("%s enable %d status %d", __func__, enabled, status);
3032 return status;
3033}
3034
3035status_t AudioFlinger::DeviceEffectProxy::init(
3036 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3037//For all audio patches
3038//If src or sink device match
3039//If the effect is HW accelerated
3040// if no corresponding effect module
3041// Create EffectModule: mHalEffect
3042//Create and attach EffectHandle
3043//If the effect is not HW accelerated and the patch sink or src is a mixer port
3044// Create Effect on patch input or output thread on session -1
3045//Add EffectHandle to EffectHandle map of Effect Proxy:
3046 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3047 status_t status = NO_ERROR;
3048 for (auto &patch : patches) {
3049 status = onCreatePatch(patch.first, patch.second);
3050 ALOGV("%s onCreatePatch status %d", __func__, status);
3051 if (status == BAD_VALUE) {
3052 return status;
3053 }
3054 }
3055 return status;
3056}
3057
3058status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3059 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3060 status_t status = NAME_NOT_FOUND;
3061 sp<EffectHandle> handle;
3062 // only consider source[0] as this is the only "true" source of a patch
3063 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3064 ALOGV("%s source checkPort status %d", __func__, status);
3065 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3066 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3067 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3068 }
3069 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3070 Mutex::Autolock _l(mProxyLock);
3071 mEffectHandles.emplace(patchHandle, handle);
3072 }
3073 ALOGW_IF(status == BAD_VALUE,
3074 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3075
3076 return status;
3077}
3078
3079status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3080 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3081
3082 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3083 __func__, port->type, port->ext.device.type,
3084 port->ext.device.address, port->id, patch.isSoftware());
3085 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003086 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003087 return NAME_NOT_FOUND;
3088 }
3089 status_t status = NAME_NOT_FOUND;
3090
3091 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3092 Mutex::Autolock _l(mProxyLock);
3093 mDevicePort = *port;
3094 mHalEffect = new EffectModule(mMyCallback,
3095 const_cast<effect_descriptor_t *>(&mDescriptor),
3096 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3097 false /* pinned */, port->id);
3098 if (audio_is_input_device(mDevice.mType)) {
3099 mHalEffect->setInputDevice(mDevice);
3100 } else {
3101 mHalEffect->setDevices({mDevice});
3102 }
3103 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3104 status = (*handle)->initCheck();
3105 if (status == OK) {
3106 status = mHalEffect->addHandle((*handle).get());
3107 } else {
3108 mHalEffect.clear();
3109 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3110 }
3111 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3112 sp <ThreadBase> thread;
3113 if (audio_port_config_has_input_direction(port)) {
3114 if (patch.isSoftware()) {
3115 thread = patch.mRecord.thread();
3116 } else {
3117 thread = patch.thread().promote();
3118 }
3119 } else {
3120 if (patch.isSoftware()) {
3121 thread = patch.mPlayback.thread();
3122 } else {
3123 thread = patch.thread().promote();
3124 }
3125 }
3126 int enabled;
3127 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3128 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003129 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003130 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3131 } else {
3132 status = BAD_VALUE;
3133 }
3134 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003135 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003136 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003137 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003138 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003139 bs = (*handle)->disable(&status);
3140 }
3141 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003142 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003143 }
3144 }
3145 return status;
3146}
3147
3148void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3149 Mutex::Autolock _l(mProxyLock);
3150 mEffectHandles.erase(patchHandle);
3151}
3152
3153
3154size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3155{
3156 Mutex::Autolock _l(mProxyLock);
3157 if (effect == mHalEffect) {
3158 mHalEffect.clear();
3159 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3160 }
3161 return mHalEffect == nullptr ? 0 : 1;
3162}
3163
3164status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3165 sp<EffectHalInterface> effect) {
3166 if (mHalEffect == nullptr) {
3167 return NO_INIT;
3168 }
3169 return mManagerCallback->addEffectToHal(
3170 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3171}
3172
3173status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3174 sp<EffectHalInterface> effect) {
3175 if (mHalEffect == nullptr) {
3176 return NO_INIT;
3177 }
3178 return mManagerCallback->removeEffectFromHal(
3179 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3180}
3181
3182bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3183 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3184 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3185 }
3186 return true;
3187}
3188
3189uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3190 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3191 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3192 return mDevicePort.sample_rate;
3193 }
3194 return DEFAULT_OUTPUT_SAMPLE_RATE;
3195}
3196
3197audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3198 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3199 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3200 return mDevicePort.channel_mask;
3201 }
3202 return AUDIO_CHANNEL_OUT_STEREO;
3203}
3204
3205uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3206 if (isOutput()) {
3207 return audio_channel_count_from_out_mask(channelMask());
3208 }
3209 return audio_channel_count_from_in_mask(channelMask());
3210}
3211
3212void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3213 const Vector<String16> args;
3214 EffectBase::dump(fd, args);
3215
3216 const bool locked = dumpTryLock(mProxyLock);
3217 if (!locked) {
3218 String8 result("DeviceEffectProxy may be deadlocked\n");
3219 write(fd, result.string(), result.size());
3220 }
3221
3222 String8 outStr;
3223 if (mHalEffect != nullptr) {
3224 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3225 } else {
3226 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3227 }
3228 write(fd, outStr.string(), outStr.size());
3229 outStr.clear();
3230
3231 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3232 write(fd, outStr.string(), outStr.size());
3233 outStr.clear();
3234
3235 for (const auto& iter : mEffectHandles) {
3236 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3237 write(fd, outStr.string(), outStr.size());
3238 outStr.clear();
3239 sp<EffectBase> effect = iter.second->effect().promote();
3240 if (effect != nullptr) {
3241 effect->dump(fd, args);
3242 }
3243 }
3244
3245 if (locked) {
3246 mLock.unlock();
3247 }
3248}
3249
3250#undef LOG_TAG
3251#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3252
3253int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3254 return mManagerCallback->newEffectId();
3255}
3256
3257
3258bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3259 EffectHandle *handle, bool unpinIfLast) {
3260 sp<EffectBase> effectBase = handle->effect().promote();
3261 if (effectBase == nullptr) {
3262 return false;
3263 }
3264
3265 sp<EffectModule> effect = effectBase->asEffectModule();
3266 if (effect == nullptr) {
3267 return false;
3268 }
3269
3270 // restore suspended effects if the disconnected handle was enabled and the last one.
3271 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3272 if (remove) {
3273 sp<DeviceEffectProxy> proxy = mProxy.promote();
3274 if (proxy != nullptr) {
3275 proxy->removeEffect(effect);
3276 }
3277 if (handle->enabled()) {
3278 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3279 }
3280 }
3281 return true;
3282}
3283
3284status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3285 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3286 sp<EffectHalInterface> *effect) {
3287 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3288}
3289
3290status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3291 sp<EffectHalInterface> effect) {
3292 sp<DeviceEffectProxy> proxy = mProxy.promote();
3293 if (proxy == nullptr) {
3294 return NO_INIT;
3295 }
3296 return proxy->addEffectToHal(effect);
3297}
3298
3299status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3300 sp<EffectHalInterface> effect) {
3301 sp<DeviceEffectProxy> proxy = mProxy.promote();
3302 if (proxy == nullptr) {
3303 return NO_INIT;
3304 }
3305 return proxy->addEffectToHal(effect);
3306}
3307
3308bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3309 sp<DeviceEffectProxy> proxy = mProxy.promote();
3310 if (proxy == nullptr) {
3311 return true;
3312 }
3313 return proxy->isOutput();
3314}
3315
3316uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3317 sp<DeviceEffectProxy> proxy = mProxy.promote();
3318 if (proxy == nullptr) {
3319 return DEFAULT_OUTPUT_SAMPLE_RATE;
3320 }
3321 return proxy->sampleRate();
3322}
3323
3324audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3325 sp<DeviceEffectProxy> proxy = mProxy.promote();
3326 if (proxy == nullptr) {
3327 return AUDIO_CHANNEL_OUT_STEREO;
3328 }
3329 return proxy->channelMask();
3330}
3331
3332uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3333 sp<DeviceEffectProxy> proxy = mProxy.promote();
3334 if (proxy == nullptr) {
3335 return 2;
3336 }
3337 return proxy->channelCount();
3338}
3339
Glenn Kasten63238ef2015-03-02 15:50:29 -08003340} // namespace android