blob: b80943e0baad9e90d5445d151c585d5c03a3ce26 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070028#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070029#include <system/audio_effects/effect_ns.h>
30#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080031#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080032#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070033#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070035#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070037#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070038#include <media/audiohal/EffectHalInterface.h>
39#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44// ----------------------------------------------------------------------------
45
46// Note: the following macro is used for extremely verbose logging message. In
47// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
48// 0; but one side effect of this is to turn all LOGV's as well. Some messages
49// are so verbose that we want to suppress them even when we have ALOG_ASSERT
50// turned on. Do not uncomment the #def below unless you really know what you
51// are doing and want to see all of the extremely verbose messages.
52//#define VERY_VERY_VERBOSE_LOGGING
53#ifdef VERY_VERY_VERBOSE_LOGGING
54#define ALOGVV ALOGV
55#else
56#define ALOGVV(a...) do { } while(0)
57#endif
58
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090059#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
60
Eric Laurentca7cc822012-11-19 14:55:58 -080061namespace android {
62
Andy Hung1131b6e2020-12-08 20:47:45 -080063using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070064using binder::Status;
65
66namespace {
67
68// Append a POD value into a vector of bytes.
69template<typename T>
70void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
71 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
72 buffer->insert(buffer->end(), ar, ar + sizeof(T));
73}
74
75// Write a POD value into a vector of bytes (clears the previous buffer
76// content).
77template<typename T>
78void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
79 buffer->clear();
80 appendToBuffer(value, buffer);
81}
82
83} // namespace
84
Eric Laurentca7cc822012-11-19 14:55:58 -080085// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080086// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080087// ----------------------------------------------------------------------------
88
89#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080090#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080091
Eric Laurent41709552019-12-16 19:34:05 -080092AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080093 effect_descriptor_t *desc,
94 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080095 audio_session_t sessionId,
96 bool pinned)
97 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080098 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080099 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800100{
Eric Laurentca7cc822012-11-19 14:55:58 -0800101}
102
Eric Laurent41709552019-12-16 19:34:05 -0800103// must be called with EffectModule::mLock held
104status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800105{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800106
Eric Laurent41709552019-12-16 19:34:05 -0800107 ALOGV("setEnabled %p enabled %d", this, enabled);
108
109 if (enabled != isEnabled()) {
110 switch (mState) {
111 // going from disabled to enabled
112 case IDLE:
113 mState = STARTING;
114 break;
115 case STOPPED:
116 mState = RESTART;
117 break;
118 case STOPPING:
119 mState = ACTIVE;
120 break;
121
122 // going from enabled to disabled
123 case RESTART:
124 mState = STOPPED;
125 break;
126 case STARTING:
127 mState = IDLE;
128 break;
129 case ACTIVE:
130 mState = STOPPING;
131 break;
132 case DESTROYED:
133 return NO_ERROR; // simply ignore as we are being destroyed
134 }
135 for (size_t i = 1; i < mHandles.size(); i++) {
136 EffectHandle *h = mHandles[i];
137 if (h != NULL && !h->disconnected()) {
138 h->setEnabled(enabled);
139 }
140 }
141 }
142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800143}
144
Eric Laurent41709552019-12-16 19:34:05 -0800145status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
146{
147 status_t status;
148 {
149 Mutex::Autolock _l(mLock);
150 status = setEnabled_l(enabled);
151 }
152 if (fromHandle) {
153 if (enabled) {
154 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700155 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800156 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700157 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800158 }
159 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700160 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800161 }
162 }
163 return status;
164}
165
166bool AudioFlinger::EffectBase::isEnabled() const
167{
168 switch (mState) {
169 case RESTART:
170 case STARTING:
171 case ACTIVE:
172 return true;
173 case IDLE:
174 case STOPPING:
175 case STOPPED:
176 case DESTROYED:
177 default:
178 return false;
179 }
180}
181
182void AudioFlinger::EffectBase::setSuspended(bool suspended)
183{
184 Mutex::Autolock _l(mLock);
185 mSuspended = suspended;
186}
187
188bool AudioFlinger::EffectBase::suspended() const
189{
190 Mutex::Autolock _l(mLock);
191 return mSuspended;
192}
193
194status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800195{
196 status_t status;
197
198 Mutex::Autolock _l(mLock);
199 int priority = handle->priority();
200 size_t size = mHandles.size();
201 EffectHandle *controlHandle = NULL;
202 size_t i;
203 for (i = 0; i < size; i++) {
204 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800205 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800206 continue;
207 }
208 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 if (h->priority() <= priority) {
213 break;
214 }
215 }
216 // if inserted in first place, move effect control from previous owner to this handle
217 if (i == 0) {
218 bool enabled = false;
219 if (controlHandle != NULL) {
220 enabled = controlHandle->enabled();
221 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
222 }
223 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
224 status = NO_ERROR;
225 } else {
226 status = ALREADY_EXISTS;
227 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700228 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800229 mHandles.insertAt(handle, i);
230 return status;
231}
232
Eric Laurent41709552019-12-16 19:34:05 -0800233status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700234{
235 status_t status = NO_ERROR;
236 bool doRegister = false;
237 bool registered = false;
238 bool doEnable = false;
239 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700240 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700242
243 {
244 Mutex::Autolock _l(mLock);
Eric Laurentd66d7a12021-07-13 13:35:32 +0200245
246 if ((isInternal_l() && !mPolicyRegistered)
247 || !getCallback()->isAudioPolicyReady()) {
248 return NO_ERROR;
249 }
250
Eric Laurent6c796322019-04-09 14:13:17 -0700251 // register effect when first handle is attached and unregister when last handle is removed
252 if (mPolicyRegistered != mHandles.size() > 0) {
253 doRegister = true;
254 mPolicyRegistered = mHandles.size() > 0;
255 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700256 const auto callback = getCallback();
257 io = callback->io();
258 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700259 }
260 }
261 // enable effect when registered according to enable state requested by controlling handle
262 if (mHandles.size() > 0) {
263 EffectHandle *handle = controlHandle_l();
264 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
265 doEnable = true;
266 mPolicyEnabled = handle->enabled();
267 }
268 }
269 registered = mPolicyRegistered;
270 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100271 // The simultaneous release of two EffectHandles with the same EffectModule
272 // may cause us to call this method at the same time.
273 // This may deadlock under some circumstances (b/180941720). Avoid this.
274 if (!doRegister && !(registered && doEnable)) {
275 return NO_ERROR;
276 }
Eric Laurent6c796322019-04-09 14:13:17 -0700277 mPolicyLock.lock();
278 }
279 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
280 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
281 if (doRegister) {
282 if (registered) {
283 status = AudioSystem::registerEffect(
284 &mDescriptor,
285 io,
286 strategy,
287 mSessionId,
288 mId);
289 } else {
290 status = AudioSystem::unregisterEffect(mId);
291 }
292 }
293 if (registered && doEnable) {
294 status = AudioSystem::setEffectEnabled(mId, enabled);
295 }
296 mPolicyLock.unlock();
297
298 return status;
299}
300
301
Eric Laurent41709552019-12-16 19:34:05 -0800302ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800303{
304 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800305 return removeHandle_l(handle);
306}
307
Eric Laurent41709552019-12-16 19:34:05 -0800308ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800309{
Eric Laurentca7cc822012-11-19 14:55:58 -0800310 size_t size = mHandles.size();
311 size_t i;
312 for (i = 0; i < size; i++) {
313 if (mHandles[i] == handle) {
314 break;
315 }
316 }
317 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800318 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
319 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800320 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800321 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800322
323 mHandles.removeAt(i);
324 // if removed from first place, move effect control from this handle to next in line
325 if (i == 0) {
326 EffectHandle *h = controlHandle_l();
327 if (h != NULL) {
328 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
329 }
330 }
331
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530332 // Prevent calls to process() and other functions on effect interface from now on.
333 // The effect engine will be released by the destructor when the last strong reference on
334 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 if (mHandles.size() == 0 && !mPinned) {
336 mState = DESTROYED;
337 }
338
339 return mHandles.size();
340}
341
342// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800343AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800344{
345 // the first valid handle in the list has control over the module
346 for (size_t i = 0; i < mHandles.size(); i++) {
347 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800348 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800349 return h;
350 }
351 }
352
353 return NULL;
354}
355
Eric Laurentf10c7092016-12-06 17:09:56 -0800356// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800357ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800358{
Andy Hungfda44002021-06-03 17:23:16 -0700359 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800360 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700361 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800362 return mHandles.size();
363 }
364
Eric Laurentf10c7092016-12-06 17:09:56 -0800365 Mutex::Autolock _l(mLock);
366 ssize_t numHandles = removeHandle_l(handle);
367 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800368 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700369 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800370 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800371 }
372 return numHandles;
373}
374
Eric Laurent41709552019-12-16 19:34:05 -0800375bool AudioFlinger::EffectBase::purgeHandles()
376{
377 bool enabled = false;
378 Mutex::Autolock _l(mLock);
379 EffectHandle *handle = controlHandle_l();
380 if (handle != NULL) {
381 enabled = handle->enabled();
382 }
383 mHandles.clear();
384 return enabled;
385}
386
387void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700388 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800389}
390
391static String8 effectFlagsToString(uint32_t flags) {
392 String8 s;
393
394 s.append("conn. mode: ");
395 switch (flags & EFFECT_FLAG_TYPE_MASK) {
396 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
397 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
398 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
399 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
400 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
401 default: s.append("unknown/reserved"); break;
402 }
403 s.append(", ");
404
405 s.append("insert pref: ");
406 switch (flags & EFFECT_FLAG_INSERT_MASK) {
407 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
408 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
409 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
410 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
411 default: s.append("unknown/reserved"); break;
412 }
413 s.append(", ");
414
415 s.append("volume mgmt: ");
416 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
417 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
418 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
419 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
420 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
421 default: s.append("unknown/reserved"); break;
422 }
423 s.append(", ");
424
425 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
426 if (devind) {
427 s.append("device indication: ");
428 switch (devind) {
429 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
430 default: s.append("unknown/reserved"); break;
431 }
432 s.append(", ");
433 }
434
435 s.append("input mode: ");
436 switch (flags & EFFECT_FLAG_INPUT_MASK) {
437 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
438 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
439 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
440 default: s.append("not set"); break;
441 }
442 s.append(", ");
443
444 s.append("output mode: ");
445 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
446 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
447 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
448 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
449 default: s.append("not set"); break;
450 }
451 s.append(", ");
452
453 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
454 if (accel) {
455 s.append("hardware acceleration: ");
456 switch (accel) {
457 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
458 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
459 default: s.append("unknown/reserved"); break;
460 }
461 s.append(", ");
462 }
463
464 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
465 if (modeind) {
466 s.append("mode indication: ");
467 switch (modeind) {
468 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
469 default: s.append("unknown/reserved"); break;
470 }
471 s.append(", ");
472 }
473
474 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
475 if (srcind) {
476 s.append("source indication: ");
477 switch (srcind) {
478 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
479 default: s.append("unknown/reserved"); break;
480 }
481 s.append(", ");
482 }
483
484 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
485 s.append("offloadable, ");
486 }
487
488 int len = s.length();
489 if (s.length() > 2) {
490 (void) s.lockBuffer(len);
491 s.unlockBuffer(len - 2);
492 }
493 return s;
494}
495
496void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
497{
498 String8 result;
499
500 result.appendFormat("\tEffect ID %d:\n", mId);
501
502 bool locked = AudioFlinger::dumpTryLock(mLock);
503 // failed to lock - AudioFlinger is probably deadlocked
504 if (!locked) {
505 result.append("\t\tCould not lock Fx mutex:\n");
506 }
507
508 result.append("\t\tSession State Registered Enabled Suspended:\n");
509 result.appendFormat("\t\t%05d %03d %s %s %s\n",
510 mSessionId, mState, mPolicyRegistered ? "y" : "n",
511 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
512
513 result.append("\t\tDescriptor:\n");
514 char uuidStr[64];
515 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
516 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
517 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
518 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
519 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
520 mDescriptor.apiVersion,
521 mDescriptor.flags,
522 effectFlagsToString(mDescriptor.flags).string());
523 result.appendFormat("\t\t- name: %s\n",
524 mDescriptor.name);
525
526 result.appendFormat("\t\t- implementor: %s\n",
527 mDescriptor.implementor);
528
529 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
530 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
531 char buffer[256];
532 for (size_t i = 0; i < mHandles.size(); ++i) {
533 EffectHandle *handle = mHandles[i];
534 if (handle != NULL && !handle->disconnected()) {
535 handle->dumpToBuffer(buffer, sizeof(buffer));
536 result.append(buffer);
537 }
538 }
539 if (locked) {
540 mLock.unlock();
541 }
542
543 write(fd, result.string(), result.length());
544}
545
546// ----------------------------------------------------------------------------
547// EffectModule implementation
548// ----------------------------------------------------------------------------
549
550#undef LOG_TAG
551#define LOG_TAG "AudioFlinger::EffectModule"
552
553AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
554 effect_descriptor_t *desc,
555 int id,
556 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800557 bool pinned,
558 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800559 : EffectBase(callback, desc, id, sessionId, pinned),
560 // clear mConfig to ensure consistent initial value of buffer framecount
561 // in case buffers are associated by setInBuffer() or setOutBuffer()
562 // prior to configure().
563 mConfig{{}, {}},
564 mStatus(NO_INIT),
565 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
566 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800567 mOffloaded(false),
568 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800569#ifdef FLOAT_EFFECT_CHAIN
570 , mSupportsFloat(false)
571#endif
572{
573 ALOGV("Constructor %p pinned %d", this, pinned);
574 int lStatus;
575
576 // create effect engine from effect factory
577 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800578 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800579 if (mStatus != NO_ERROR) {
580 return;
581 }
582 lStatus = init();
583 if (lStatus < 0) {
584 mStatus = lStatus;
585 goto Error;
586 }
587
588 setOffloaded(callback->isOffload(), callback->io());
589 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
590
591 return;
592Error:
593 mEffectInterface.clear();
594 ALOGV("Constructor Error %d", mStatus);
595}
596
597AudioFlinger::EffectModule::~EffectModule()
598{
599 ALOGV("Destructor %p", this);
600 if (mEffectInterface != 0) {
601 char uuidStr[64];
602 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
603 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
604 this, uuidStr);
605 release_l();
606 }
607
608}
609
Eric Laurentfa1e1232016-08-02 19:01:49 -0700610bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800611 Mutex::Autolock _l(mLock);
612
Eric Laurentfa1e1232016-08-02 19:01:49 -0700613 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800614 switch (mState) {
615 case RESTART:
616 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700617 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800618
619 case STARTING:
620 // clear auxiliary effect input buffer for next accumulation
621 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
622 memset(mConfig.inputCfg.buffer.raw,
623 0,
624 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
625 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700626 if (start_l() == NO_ERROR) {
627 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700628 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700629 } else {
630 mState = IDLE;
631 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800632 break;
633 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900634 // volume control for offload and direct threads must take effect immediately.
635 if (stop_l() == NO_ERROR
636 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700637 mDisableWaitCnt = mMaxDisableWaitCnt;
638 } else {
639 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
640 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800641 mState = STOPPED;
642 break;
643 case STOPPED:
644 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
645 // turn off sequence.
646 if (--mDisableWaitCnt == 0) {
647 reset_l();
648 mState = IDLE;
649 }
650 break;
Eric Laurentde8caf42021-08-11 17:19:25 +0200651 case ACTIVE:
652 for (size_t i = 0; i < mHandles.size(); i++) {
653 if (!mHandles[i]->disconnected()) {
654 mHandles[i]->framesProcessed(mConfig.inputCfg.buffer.frameCount);
655 }
656 }
657 break;
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 default: //IDLE , ACTIVE, DESTROYED
659 break;
660 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700661
662 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800663}
664
665void AudioFlinger::EffectModule::process()
666{
667 Mutex::Autolock _l(mLock);
668
Mikhail Naganov022b9952017-01-04 16:36:51 -0800669 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800670 return;
671 }
672
rago94a1ee82017-07-21 15:11:02 -0700673 const uint32_t inChannelCount =
674 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
675 const uint32_t outChannelCount =
676 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
677 const bool auxType =
678 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
679
Andy Hungfa69ca32017-11-30 10:07:53 -0800680 // safeInputOutputSampleCount is 0 if the channel count between input and output
681 // buffers do not match. This prevents automatic accumulation or copying between the
682 // input and output effect buffers without an intermediary effect process.
683 // TODO: consider implementing channel conversion.
684 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700685 mInChannelCountRequested != mOutChannelCountRequested ? 0
686 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800687 mConfig.inputCfg.buffer.frameCount,
688 mConfig.outputCfg.buffer.frameCount);
689 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
690#ifdef FLOAT_EFFECT_CHAIN
691 accumulate_float(
692 mConfig.outputCfg.buffer.f32,
693 mConfig.inputCfg.buffer.f32,
694 safeInputOutputSampleCount);
695#else
696 accumulate_i16(
697 mConfig.outputCfg.buffer.s16,
698 mConfig.inputCfg.buffer.s16,
699 safeInputOutputSampleCount);
700#endif
701 };
702 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
703#ifdef FLOAT_EFFECT_CHAIN
704 memcpy(
705 mConfig.outputCfg.buffer.f32,
706 mConfig.inputCfg.buffer.f32,
707 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
708
709#else
710 memcpy(
711 mConfig.outputCfg.buffer.s16,
712 mConfig.inputCfg.buffer.s16,
713 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
714#endif
715 };
716
Eric Laurentca7cc822012-11-19 14:55:58 -0800717 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700718 int ret;
719 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700720 if (auxType) {
721 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800722 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700723#ifdef FLOAT_EFFECT_CHAIN
724 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800725#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700726 // Do in-place float conversion for auxiliary effect input buffer.
727 static_assert(sizeof(float) <= sizeof(int32_t),
728 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
729
Andy Hungfa69ca32017-11-30 10:07:53 -0800730 memcpy_to_float_from_q4_27(
731 mConfig.inputCfg.buffer.f32,
732 mConfig.inputCfg.buffer.s32,
733 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800734#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800735 } else
Andy Hung116a4982017-11-30 10:15:08 -0800736#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 {
Andy Hung116a4982017-11-30 10:15:08 -0800738#ifdef FLOAT_AUX
739 memcpy_to_i16_from_float(
740 mConfig.inputCfg.buffer.s16,
741 mConfig.inputCfg.buffer.f32,
742 mConfig.inputCfg.buffer.frameCount);
743#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 memcpy_to_i16_from_q4_27(
745 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700746 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800747 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800748#endif
rago94a1ee82017-07-21 15:11:02 -0700749 }
rago94a1ee82017-07-21 15:11:02 -0700750 }
751#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800752 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
753 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
754
755 if (!auxType && mInChannelCountRequested != inChannelCount) {
756 adjust_channels(
757 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
758 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
759 sizeof(float),
760 sizeof(float)
761 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
762 inBuffer = mInConversionBuffer;
763 }
764 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
765 && mOutChannelCountRequested != outChannelCount) {
766 adjust_selected_channels(
767 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
768 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
769 sizeof(float),
770 sizeof(float)
771 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
772 outBuffer = mOutConversionBuffer;
773 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
775 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800776 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800777 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
778 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700779 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 memcpy_to_i16_from_float(
781 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800782 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800783 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800784 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700785 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800787 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
789 goto data_bypass;
790 }
791 memcpy_to_i16_from_float(
792 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800793 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800795 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700796 }
797 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800798#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800799 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800800#ifdef FLOAT_EFFECT_CHAIN
801 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800802 sp<EffectBufferHalInterface> target =
803 mOutChannelCountRequested != outChannelCount
804 ? mOutConversionBuffer : mOutBuffer;
805
Andy Hungfa69ca32017-11-30 10:07:53 -0800806 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800807 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800808 mOutConversionBuffer->audioBuffer()->s16,
809 outChannelCount * mConfig.outputCfg.buffer.frameCount);
810 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800811 if (mOutChannelCountRequested != outChannelCount) {
812 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
813 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
814 sizeof(float),
815 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
816 }
rago94a1ee82017-07-21 15:11:02 -0700817#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700818 } else {
rago94a1ee82017-07-21 15:11:02 -0700819#ifdef FLOAT_EFFECT_CHAIN
820 data_bypass:
821#endif
822 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800823 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700824 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800825 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700826 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800827 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700828 }
829 }
830 ret = -ENODATA;
831 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800832
Eric Laurentca7cc822012-11-19 14:55:58 -0800833 // force transition to IDLE state when engine is ready
834 if (mState == STOPPED && ret == -ENODATA) {
835 mDisableWaitCnt = 1;
836 }
837
838 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700839 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800840#ifdef FLOAT_AUX
841 const size_t size =
842 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
843#else
rago94a1ee82017-07-21 15:11:02 -0700844 const size_t size =
845 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800846#endif
rago94a1ee82017-07-21 15:11:02 -0700847 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 }
849 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700850 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800851 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
852 // If an insert effect is idle and input buffer is different from output buffer,
853 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700854 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700855 // similar handling with data_bypass above.
856 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
857 accumulateInputToOutput();
858 } else { // EFFECT_BUFFER_ACCESS_WRITE
859 copyInputToOutput();
860 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800861 }
862 }
863}
864
865void AudioFlinger::EffectModule::reset_l()
866{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700867 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800868 return;
869 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700870 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800871}
872
873status_t AudioFlinger::EffectModule::configure()
874{
rago94a1ee82017-07-21 15:11:02 -0700875 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700876 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700877 uint32_t size;
878 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700879 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700880
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700881 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700882 status = NO_INIT;
883 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 }
885
Eric Laurentca7cc822012-11-19 14:55:58 -0800886 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800887 // TODO: handle configuration of input (record) SW effects above the HAL,
888 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
889 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700890 callback = getCallback();
Eric Laurentf1f22e72021-07-13 14:04:14 +0200891 channelMask = callback->inChannelMask(mId);
Andy Hung9aad48c2017-11-29 10:29:19 -0800892 mConfig.inputCfg.channels = channelMask;
Eric Laurentf1f22e72021-07-13 14:04:14 +0200893 mConfig.outputCfg.channels = callback->outChannelMask();
Eric Laurentca7cc822012-11-19 14:55:58 -0800894
895 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800896 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
897 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
898 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
899 mConfig.inputCfg.channels);
900 }
901#ifndef MULTICHANNEL_EFFECT_CHAIN
902 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
903 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
904 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
905 mConfig.outputCfg.channels);
906 }
907#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800908 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800909#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700910 // TODO: Update this logic when multichannel effects are implemented.
911 // For offloaded tracks consider mono output as stereo for proper effect initialization
912 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
913 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
914 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
915 ALOGV("Overriding effect input and output as STEREO");
916 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800917#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800918 }
jiabineb3bda02020-06-30 14:07:03 -0700919 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700920 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700921 mConfig.inputCfg.channels |= hapticChannelMask;
922 mConfig.outputCfg.channels |= hapticChannelMask;
923 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800924 mInChannelCountRequested =
925 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
926 mOutChannelCountRequested =
927 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700928
rago94a1ee82017-07-21 15:11:02 -0700929 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
930 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900931
932 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700933 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900934 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
935 ALOGV("Overriding effect input as 48kHz");
936 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700937 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900938 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800939 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
940 mConfig.inputCfg.bufferProvider.cookie = NULL;
941 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
942 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
943 mConfig.outputCfg.bufferProvider.cookie = NULL;
944 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
945 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
946 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
947 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800948 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800949 // always overwrites output buffer: input buffer == output buffer
950 // - in other sessions:
951 // last effect in the chain accumulates in output buffer: input buffer != output buffer
952 // other effect: overwrites output buffer: input buffer == output buffer
953 // Auxiliary effect:
954 // accumulates in output buffer: input buffer != output buffer
955 // Therefore: accumulate <=> input buffer != output buffer
956 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
957 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
958 } else {
959 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
960 }
961 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
962 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700963 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
965
Eric Laurent6b446ce2019-12-13 10:56:31 -0800966 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700967 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800968 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800969
970 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700971 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700972 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800973 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700974 &mConfig,
975 &size,
976 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700977 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800978 status = cmdStatus;
979 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800980
981#ifdef MULTICHANNEL_EFFECT_CHAIN
982 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700983 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800984 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
985 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
986 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700987 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
988 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800989 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
990 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
991 }
992 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
993 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
994 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
995 }
996 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700997 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800998 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700999 &mConfig,
1000 &size,
1001 &cmdStatus);
1002 if (status == NO_ERROR) {
1003 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001004 }
1005 }
1006#endif
1007
1008#ifdef FLOAT_EFFECT_CHAIN
1009 if (status == NO_ERROR) {
1010 mSupportsFloat = true;
1011 }
1012
1013 if (status != NO_ERROR) {
1014 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1015 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1016 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1017 size = sizeof(int);
1018 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1019 sizeof(mConfig),
1020 &mConfig,
1021 &size,
1022 &cmdStatus);
1023 if (status == NO_ERROR) {
1024 status = cmdStatus;
1025 }
1026 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001027 mSupportsFloat = false;
1028 ALOGVV("config worked with 16 bit");
1029 } else {
1030 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001031 }
rago94a1ee82017-07-21 15:11:02 -07001032 }
1033#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001034
rago94a1ee82017-07-21 15:11:02 -07001035 if (status == NO_ERROR) {
1036 // Establish Buffer strategy
1037 setInBuffer(mInBuffer);
1038 setOutBuffer(mOutBuffer);
1039
1040 // Update visualizer latency
1041 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1042 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1043 effect_param_t *p = (effect_param_t *)buf32;
1044
1045 p->psize = sizeof(uint32_t);
1046 p->vsize = sizeof(uint32_t);
1047 size = sizeof(int);
1048 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1049
Andy Hungfda44002021-06-03 17:23:16 -07001050 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001051
1052 *((int32_t *)p->data + 1)= latency;
1053 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1054 sizeof(effect_param_t) + 8,
1055 &buf32,
1056 &size,
1057 &cmdStatus);
1058 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001059 }
1060
Andy Hung05083ac2017-12-14 15:00:28 -08001061 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1062 mMaxDisableWaitCnt = (uint32_t)std::max(
1063 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1064 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1065 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001066
Eric Laurentd0ebb532013-04-02 16:41:41 -07001067exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001068 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001069 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001070 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001071 return status;
1072}
1073
1074status_t AudioFlinger::EffectModule::init()
1075{
1076 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001077 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001078 return NO_INIT;
1079 }
1080 status_t cmdStatus;
1081 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001082 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1083 0,
1084 NULL,
1085 &size,
1086 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 if (status == 0) {
1088 status = cmdStatus;
1089 }
1090 return status;
1091}
1092
Eric Laurent1b928682014-10-02 19:41:47 -07001093void AudioFlinger::EffectModule::addEffectToHal_l()
1094{
1095 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1096 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001097 if (mAddedToHal) {
1098 return;
1099 }
1100
Andy Hungfda44002021-06-03 17:23:16 -07001101 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001102 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001103 }
1104}
1105
Eric Laurentfa1e1232016-08-02 19:01:49 -07001106// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001107status_t AudioFlinger::EffectModule::start()
1108{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001109 status_t status;
1110 {
1111 Mutex::Autolock _l(mLock);
1112 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001113 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001114 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001115 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001116 }
1117 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001118}
1119
1120status_t AudioFlinger::EffectModule::start_l()
1121{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001122 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001123 return NO_INIT;
1124 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001125 if (mStatus != NO_ERROR) {
1126 return mStatus;
1127 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 status_t cmdStatus;
1129 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001130 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1131 0,
1132 NULL,
1133 &size,
1134 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001135 if (status == 0) {
1136 status = cmdStatus;
1137 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001138 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001139 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 }
1141 return status;
1142}
1143
1144status_t AudioFlinger::EffectModule::stop()
1145{
1146 Mutex::Autolock _l(mLock);
1147 return stop_l();
1148}
1149
1150status_t AudioFlinger::EffectModule::stop_l()
1151{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001152 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001153 return NO_INIT;
1154 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001155 if (mStatus != NO_ERROR) {
1156 return mStatus;
1157 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001158 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001159 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001160
1161 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001162 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1163 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1164 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001165 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001166 mSetVolumeReentrantTid = INVALID_PID;
1167 }
1168
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001169 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1170 0,
1171 NULL,
1172 &size,
1173 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001174 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001175 status = cmdStatus;
1176 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001177 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001178 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001179 }
1180 return status;
1181}
1182
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001183// must be called with EffectChain::mLock held
1184void AudioFlinger::EffectModule::release_l()
1185{
1186 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001187 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001188 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001189 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001190 mEffectInterface.clear();
1191 }
1192}
1193
Eric Laurent6b446ce2019-12-13 10:56:31 -08001194status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001195{
1196 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1197 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001198 if (!mAddedToHal) {
1199 return NO_ERROR;
1200 }
1201
Andy Hungfda44002021-06-03 17:23:16 -07001202 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001203 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001204 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001205 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001206}
1207
Andy Hunge4a1d912016-08-17 14:11:13 -07001208// round up delta valid if value and divisor are positive.
1209template <typename T>
1210static T roundUpDelta(const T &value, const T &divisor) {
1211 T remainder = value % divisor;
1212 return remainder == 0 ? 0 : divisor - remainder;
1213}
1214
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001215status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1216 const std::vector<uint8_t>& cmdData,
1217 int32_t maxReplySize,
1218 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001219{
1220 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001221 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001222
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001223 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001224 return NO_INIT;
1225 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001226 if (mStatus != NO_ERROR) {
1227 return mStatus;
1228 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001229 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1230 return -EINVAL;
1231 }
1232 size_t cmdSize = cmdData.size();
1233 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1234 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1235 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001236 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001237 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001238 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001239 android_errorWriteLog(0x534e4554, "33003822");
1240 return -EINVAL;
1241 }
1242 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001243 (maxReplySize < sizeof(effect_param_t) ||
1244 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001245 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001246 return -EINVAL;
1247 }
ragoe2759072016-11-22 18:02:48 -08001248 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001249 (sizeof(effect_param_t) > maxReplySize
1250 || param->psize > maxReplySize - sizeof(effect_param_t)
1251 || param->vsize > maxReplySize - sizeof(effect_param_t)
1252 - param->psize
1253 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1254 maxReplySize
1255 - sizeof(effect_param_t)
1256 - param->psize
1257 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001258 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1259 android_errorWriteLog(0x534e4554, "32705438");
1260 return -EINVAL;
1261 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001262 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001263 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1264 && // DEFERRED not generally used
1265 (param == nullptr
1266 || param->psize > cmdSize - sizeof(effect_param_t)
1267 || param->vsize > cmdSize - sizeof(effect_param_t)
1268 - param->psize
1269 || roundUpDelta(param->psize,
1270 (uint32_t) sizeof(int)) >
1271 cmdSize
1272 - sizeof(effect_param_t)
1273 - param->psize
1274 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001275 android_errorWriteLog(0x534e4554, "30204301");
1276 return -EINVAL;
1277 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001278 uint32_t replySize = maxReplySize;
1279 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001280 status_t status = mEffectInterface->command(cmdCode,
1281 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001282 const_cast<uint8_t*>(cmdData.data()),
1283 &replySize,
1284 reply->data());
1285 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001286 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001287 for (size_t i = 1; i < mHandles.size(); i++) {
1288 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001289 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001290 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001291 }
1292 }
1293 }
1294 return status;
1295}
1296
Eric Laurentca7cc822012-11-19 14:55:58 -08001297bool AudioFlinger::EffectModule::isProcessEnabled() const
1298{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001299 if (mStatus != NO_ERROR) {
1300 return false;
1301 }
1302
Eric Laurentca7cc822012-11-19 14:55:58 -08001303 switch (mState) {
1304 case RESTART:
1305 case ACTIVE:
1306 case STOPPING:
1307 case STOPPED:
1308 return true;
1309 case IDLE:
1310 case STARTING:
1311 case DESTROYED:
1312 default:
1313 return false;
1314 }
1315}
1316
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001317bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1318{
Andy Hungfda44002021-06-03 17:23:16 -07001319 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001320}
1321
1322bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1323{
1324 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1325}
1326
Mikhail Naganov022b9952017-01-04 16:36:51 -08001327void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001328 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001329
1330 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001331 if (buffer != 0) {
1332 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1333 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1334 } else {
1335 mConfig.inputCfg.buffer.raw = NULL;
1336 }
1337 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001338 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001339
1340#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001341 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001342 // Theoretically insert effects can also do in-place conversions (destroying
1343 // the original buffer) when the output buffer is identical to the input buffer,
1344 // but we don't optimize for it here.
1345 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001346 const uint32_t inChannelCount =
1347 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1348 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001349 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001350 // we need to translate - create hidl shared buffer and intercept
1351 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001352 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1353 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1354 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001355
1356 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1357 __func__, inChannels, inFrameCount, size);
1358
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001359 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001360 || size > mInConversionBuffer->getSize())) {
1361 mInConversionBuffer.clear();
1362 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001363 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001364 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001365 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001366 mInConversionBuffer->setFrameCount(inFrameCount);
1367 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001368 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001369 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001370 }
1371 }
1372#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001373}
1374
1375void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001376 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001377
1378 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001379 if (buffer != 0) {
1380 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1381 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1382 } else {
1383 mConfig.outputCfg.buffer.raw = NULL;
1384 }
1385 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001386 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001387
1388#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001389 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001390 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001391 const uint32_t outChannelCount =
1392 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1393 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001394 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001395 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001396 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1397 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1398 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001399
1400 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1401 __func__, outChannels, outFrameCount, size);
1402
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001403 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001404 || size > mOutConversionBuffer->getSize())) {
1405 mOutConversionBuffer.clear();
1406 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001407 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001408 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001409 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001410 mOutConversionBuffer->setFrameCount(outFrameCount);
1411 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001412 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001413 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001414 }
1415 }
1416#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001417}
1418
Eric Laurentca7cc822012-11-19 14:55:58 -08001419status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1420{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001421 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001422 if (mStatus != NO_ERROR) {
1423 return mStatus;
1424 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001425 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001426 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1427 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1428 if (isProcessEnabled() &&
1429 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001430 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1431 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 uint32_t volume[2];
1433 uint32_t *pVolume = NULL;
1434 uint32_t size = sizeof(volume);
1435 volume[0] = *left;
1436 volume[1] = *right;
1437 if (controller) {
1438 pVolume = volume;
1439 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001440 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1441 size,
1442 volume,
1443 &size,
1444 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1446 *left = volume[0];
1447 *right = volume[1];
1448 }
1449 }
1450 return status;
1451}
1452
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001453void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1454{
Zhou Songd505c642020-02-20 16:35:37 +08001455 // for offload or direct thread, if the effect chain has non-offloadable
1456 // effect and any effect module within the chain has volume control, then
1457 // volume control is delegated to effect, otherwise, set volume to hal.
1458 if (mEffectCallback->isOffloadOrDirect() &&
1459 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001460 float vol_l = (float)left / (1 << 24);
1461 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001462 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001463 }
1464}
1465
jiabin8f278ee2019-11-11 12:16:27 -08001466status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1467 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001468{
jiabin8f278ee2019-11-11 12:16:27 -08001469 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1470 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 return NO_ERROR;
1472 }
1473
1474 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001475 if (mStatus != NO_ERROR) {
1476 return mStatus;
1477 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001479 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001480 status_t cmdStatus;
1481 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001482 // FIXME: use audio device types and addresses when the hal interface is ready.
1483 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001484 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001485 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001486 &size,
1487 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001488 }
1489 return status;
1490}
1491
jiabin8f278ee2019-11-11 12:16:27 -08001492status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1493{
1494 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1495}
1496
1497status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1498{
1499 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1500}
1501
Eric Laurentca7cc822012-11-19 14:55:58 -08001502status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1503{
1504 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001505 if (mStatus != NO_ERROR) {
1506 return mStatus;
1507 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001508 status_t status = NO_ERROR;
1509 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1510 status_t cmdStatus;
1511 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001512 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1513 sizeof(audio_mode_t),
1514 &mode,
1515 &size,
1516 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 if (status == NO_ERROR) {
1518 status = cmdStatus;
1519 }
1520 }
1521 return status;
1522}
1523
1524status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1525{
1526 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001527 if (mStatus != NO_ERROR) {
1528 return mStatus;
1529 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 status_t status = NO_ERROR;
1531 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1532 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001533 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1534 sizeof(audio_source_t),
1535 &source,
1536 &size,
1537 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001538 }
1539 return status;
1540}
1541
Eric Laurent5baf2af2013-09-12 17:37:00 -07001542status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1543{
1544 Mutex::Autolock _l(mLock);
1545 if (mStatus != NO_ERROR) {
1546 return mStatus;
1547 }
1548 status_t status = NO_ERROR;
1549 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1550 status_t cmdStatus;
1551 uint32_t size = sizeof(status_t);
1552 effect_offload_param_t cmd;
1553
1554 cmd.isOffload = offloaded;
1555 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001556 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1557 sizeof(effect_offload_param_t),
1558 &cmd,
1559 &size,
1560 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001561 if (status == NO_ERROR) {
1562 status = cmdStatus;
1563 }
1564 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1565 } else {
1566 if (offloaded) {
1567 status = INVALID_OPERATION;
1568 }
1569 mOffloaded = false;
1570 }
1571 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1572 return status;
1573}
1574
1575bool AudioFlinger::EffectModule::isOffloaded() const
1576{
1577 Mutex::Autolock _l(mLock);
1578 return mOffloaded;
1579}
1580
jiabineb3bda02020-06-30 14:07:03 -07001581/*static*/
1582bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1583 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1584}
1585
1586bool AudioFlinger::EffectModule::isHapticGenerator() const {
1587 return isHapticGenerator(&mDescriptor.type);
1588}
1589
jiabine70bc7f2020-06-30 22:07:55 -07001590status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1591{
1592 if (mStatus != NO_ERROR) {
1593 return mStatus;
1594 }
1595 if (!isHapticGenerator()) {
1596 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1597 return INVALID_OPERATION;
1598 }
1599
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001600 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1601 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001602 param->psize = sizeof(int32_t);
1603 param->vsize = sizeof(int32_t) * 2;
1604 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1605 *((int32_t*)param->data + 1) = id;
1606 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001607 std::vector<uint8_t> response;
1608 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001609 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001610 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1611 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001612 }
1613 return status;
1614}
1615
Lais Andradebc3f37a2021-07-02 00:13:19 +01001616status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001617{
1618 if (mStatus != NO_ERROR) {
1619 return mStatus;
1620 }
1621 if (!isHapticGenerator()) {
1622 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1623 return INVALID_OPERATION;
1624 }
1625
Lais Andradebc3f37a2021-07-02 00:13:19 +01001626 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001627 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001628 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001629 effect_param_t *param = (effect_param_t*) request.data();
1630 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001631 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001632 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1633 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001634 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1635 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1636 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001637 std::vector<uint8_t> response;
1638 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1639 if (status == NO_ERROR) {
1640 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1641 status = *reinterpret_cast<const status_t*>(response.data());
1642 }
1643 return status;
1644}
1645
Andy Hungbded9c82017-11-30 18:47:35 -08001646static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1647 std::stringstream ss;
1648
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001649 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001650 return "nullptr"; // make different than below
1651 } else if (buffer->externalData() != nullptr) {
1652 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1653 << " -> "
1654 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1655 } else {
1656 ss << buffer->audioBuffer()->raw;
1657 }
1658 return ss.str();
1659}
Marco Nelissenb2208842014-02-07 14:00:50 -08001660
Eric Laurent41709552019-12-16 19:34:05 -08001661void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001662{
Eric Laurent41709552019-12-16 19:34:05 -08001663 EffectBase::dump(fd, args);
1664
Eric Laurentca7cc822012-11-19 14:55:58 -08001665 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001667
Eric Laurent41709552019-12-16 19:34:05 -08001668 result.append("\t\tStatus Engine:\n");
1669 result.appendFormat("\t\t%03d %p\n",
1670 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001671
1672 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001673
1674 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001675 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1676 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1677 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001678 mConfig.inputCfg.buffer.frameCount,
1679 mConfig.inputCfg.samplingRate,
1680 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001681 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001682 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001683
1684 result.append("\t\t- Output configuration:\n");
1685 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001686 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001687 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 mConfig.outputCfg.buffer.frameCount,
1689 mConfig.outputCfg.samplingRate,
1690 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001691 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001692 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001693
rago94a1ee82017-07-21 15:11:02 -07001694#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001695
Andy Hungbded9c82017-11-30 18:47:35 -08001696 result.appendFormat("\t\t- HAL buffers:\n"
1697 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1698 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1699 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1700 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1701 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001702#endif
1703
Eric Laurentca7cc822012-11-19 14:55:58 -08001704 write(fd, result.string(), result.length());
1705
Mikhail Naganov4d547672019-02-22 14:19:19 -08001706 if (mEffectInterface != 0) {
1707 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1708 (void)mEffectInterface->dump(fd);
1709 }
1710
Eric Laurentca7cc822012-11-19 14:55:58 -08001711 if (locked) {
1712 mLock.unlock();
1713 }
1714}
1715
1716// ----------------------------------------------------------------------------
1717// EffectHandle implementation
1718// ----------------------------------------------------------------------------
1719
1720#undef LOG_TAG
1721#define LOG_TAG "AudioFlinger::EffectHandle"
1722
Eric Laurent41709552019-12-16 19:34:05 -08001723AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001724 const sp<AudioFlinger::Client>& client,
1725 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001726 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001727 : BnEffect(),
1728 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001729 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1730 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001731{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001732 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001733
1734 if (client == 0) {
1735 return;
1736 }
1737 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1738 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001739 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001740 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001741 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001743 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001744 return;
1745 }
Glenn Kastene75da402013-11-20 13:54:52 -08001746 new(mCblk) effect_param_cblk_t();
1747 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001748}
1749
1750AudioFlinger::EffectHandle::~EffectHandle()
1751{
1752 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001753 disconnect(false);
1754}
1755
Glenn Kastene75da402013-11-20 13:54:52 -08001756status_t AudioFlinger::EffectHandle::initCheck()
1757{
1758 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1759}
1760
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001761#define RETURN(code) \
1762 *_aidl_return = (code); \
1763 return Status::ok();
1764
1765Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001766{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001767 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001769 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001770 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001771 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001772 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001773 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001774 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001776
1777 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001778 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 }
1780
1781 mEnabled = true;
1782
Eric Laurent6c796322019-04-09 14:13:17 -07001783 status_t status = effect->updatePolicyState();
1784 if (status != NO_ERROR) {
1785 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001786 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001787 }
1788
Eric Laurent6b446ce2019-12-13 10:56:31 -08001789 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001790
1791 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001793 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001794 }
1795
Eric Laurent6b446ce2019-12-13 10:56:31 -08001796 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001798 mEnabled = false;
1799 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001800 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001801}
1802
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001803Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001804{
1805 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001807 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001808 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001809 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001810 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001811 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001812 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001813 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001814
1815 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001816 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 }
1818 mEnabled = false;
1819
Eric Laurent6c796322019-04-09 14:13:17 -07001820 effect->updatePolicyState();
1821
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001822 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001823 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001824 }
1825
Eric Laurent6b446ce2019-12-13 10:56:31 -08001826 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001827 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001828}
1829
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001831{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001832 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001833 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001834 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001835}
1836
1837void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1838{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001839 AutoMutex _l(mLock);
1840 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1841 if (mDisconnected) {
1842 if (unpinIfLast) {
1843 android_errorWriteLog(0x534e4554, "32707507");
1844 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001845 return;
1846 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001847 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001848 {
Eric Laurent41709552019-12-16 19:34:05 -08001849 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001851 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001852 ALOGW("%s Effect handle %p disconnected after thread destruction",
1853 __func__, this);
1854 }
1855 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001856 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001858
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 if (mClient != 0) {
1860 if (mCblk != NULL) {
1861 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1862 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1863 }
1864 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001865 // Client destructor must run with AudioFlinger client mutex locked
1866 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001867 mClient.clear();
1868 }
1869}
1870
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1872 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1873 return Status::ok();
1874}
1875
1876Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1877 const std::vector<uint8_t>& cmdData,
1878 int32_t maxResponseSize,
1879 std::vector<uint8_t>* response,
1880 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001881{
1882 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001883 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001884
Eric Laurentc7ab3092017-06-15 18:43:46 -07001885 // reject commands reserved for internal use by audio framework if coming from outside
1886 // of audioserver
1887 switch(cmdCode) {
1888 case EFFECT_CMD_ENABLE:
1889 case EFFECT_CMD_DISABLE:
1890 case EFFECT_CMD_SET_PARAM:
1891 case EFFECT_CMD_SET_PARAM_DEFERRED:
1892 case EFFECT_CMD_SET_PARAM_COMMIT:
1893 case EFFECT_CMD_GET_PARAM:
1894 break;
1895 default:
1896 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1897 break;
1898 }
1899 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001900 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001901 }
1902
Eric Laurent1ffc5852016-12-15 14:46:09 -08001903 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001904 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001905 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001907 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 writeToBuffer(NO_ERROR, response);
1909 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001910 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001911 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001912 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001913 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001914 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001915 writeToBuffer(NO_ERROR, response);
1916 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001917 }
1918
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001919 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001920 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001921 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001922 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001923 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 // only get parameter command is permitted for applications not controlling the effect
1925 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001926 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001928
1929 // handle commands that are not forwarded transparently to effect engine
1930 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001931 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001932 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001933 }
1934
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001935 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001936 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001937 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001938 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001939 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001940
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 // No need to trylock() here as this function is executed in the binder thread serving a
1942 // particular client process: no risk to block the whole media server process or mixer
1943 // threads if we are stuck here
1944 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001945 // keep local copy of index in case of client corruption b/32220769
1946 const uint32_t clientIndex = mCblk->clientIndex;
1947 const uint32_t serverIndex = mCblk->serverIndex;
1948 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1949 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001950 mCblk->serverIndex = 0;
1951 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001952 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001953 }
1954 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001955 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001956 for (uint32_t index = serverIndex; index < clientIndex;) {
1957 int *p = (int *)(mBuffer + index);
1958 const int size = *p++;
1959 if (size < 0
1960 || size > EFFECT_PARAM_BUFFER_SIZE
1961 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001963 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001964 break;
1965 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001966
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 std::copy(reinterpret_cast<const uint8_t*>(p),
1968 reinterpret_cast<const uint8_t*>(p) + size,
1969 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001970
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001971 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001972 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001973 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001974 sizeof(int),
1975 &replyBuffer);
1976 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001977
1978 // verify shared memory: server index shouldn't change; client index can't go back.
1979 if (serverIndex != mCblk->serverIndex
1980 || clientIndex > mCblk->clientIndex) {
1981 android_errorWriteLog(0x534e4554, "32220769");
1982 status = BAD_VALUE;
1983 break;
1984 }
1985
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 // stop at first error encountered
1987 if (ret != NO_ERROR) {
1988 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001989 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 break;
1991 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001992 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001993 break;
1994 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001995 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001996 }
1997 mCblk->serverIndex = 0;
1998 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001999 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002000 }
2001
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002002 status_t status = effect->command(cmdCode,
2003 cmdData,
2004 maxResponseSize,
2005 response);
2006 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002007}
2008
2009void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2010{
2011 ALOGV("setControl %p control %d", this, hasControl);
2012
2013 mHasControl = hasControl;
2014 mEnabled = enabled;
2015
2016 if (signal && mEffectClient != 0) {
2017 mEffectClient->controlStatusChanged(hasControl);
2018 }
2019}
2020
2021void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002022 const std::vector<uint8_t>& cmdData,
2023 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002024{
2025 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002026 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 }
2028}
2029
2030
2031
2032void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2033{
2034 if (mEffectClient != 0) {
2035 mEffectClient->enableStatusChanged(enabled);
2036 }
2037}
2038
Eric Laurentde8caf42021-08-11 17:19:25 +02002039void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2040{
2041 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2042 mEffectClient->framesProcessed(frames);
2043 }
2044}
2045
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002046void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002047{
2048 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2049
Marco Nelissenb2208842014-02-07 14:00:50 -08002050 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002051 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002052 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002053 mHasControl ? "yes" : "no",
2054 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002055 mCblk ? mCblk->clientIndex : 0,
2056 mCblk ? mCblk->serverIndex : 0
2057 );
2058
2059 if (locked) {
2060 mCblk->lock.unlock();
2061 }
2062}
2063
2064#undef LOG_TAG
2065#define LOG_TAG "AudioFlinger::EffectChain"
2066
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002067AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2068 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002069 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002070 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002071 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002072 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002073{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002074 sp<ThreadBase> p = thread.promote();
2075 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002076 return;
2077 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002078 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002079 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2080 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002081}
2082
2083AudioFlinger::EffectChain::~EffectChain()
2084{
Eric Laurentca7cc822012-11-19 14:55:58 -08002085}
2086
2087// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2088sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2089 effect_descriptor_t *descriptor)
2090{
2091 size_t size = mEffects.size();
2092
2093 for (size_t i = 0; i < size; i++) {
2094 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2095 return mEffects[i];
2096 }
2097 }
2098 return 0;
2099}
2100
2101// getEffectFromId_l() must be called with ThreadBase::mLock held
2102sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2103{
2104 size_t size = mEffects.size();
2105
2106 for (size_t i = 0; i < size; i++) {
2107 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2108 if (id == 0 || mEffects[i]->id() == id) {
2109 return mEffects[i];
2110 }
2111 }
2112 return 0;
2113}
2114
2115// getEffectFromType_l() must be called with ThreadBase::mLock held
2116sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2117 const effect_uuid_t *type)
2118{
2119 size_t size = mEffects.size();
2120
2121 for (size_t i = 0; i < size; i++) {
2122 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2123 return mEffects[i];
2124 }
2125 }
2126 return 0;
2127}
2128
Eric Laurent6c796322019-04-09 14:13:17 -07002129std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2130{
2131 std::vector<int> ids;
2132 Mutex::Autolock _l(mLock);
2133 for (size_t i = 0; i < mEffects.size(); i++) {
2134 ids.push_back(mEffects[i]->id());
2135 }
2136 return ids;
2137}
2138
Eric Laurentca7cc822012-11-19 14:55:58 -08002139void AudioFlinger::EffectChain::clearInputBuffer()
2140{
2141 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002142 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002143}
2144
2145// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002146void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002147{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002148 if (mInBuffer == NULL) {
2149 return;
2150 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002151 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2152 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002153
Eric Laurent6b446ce2019-12-13 10:56:31 -08002154 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002155 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002156}
2157
2158// Must be called with EffectChain::mLock locked
2159void AudioFlinger::EffectChain::process_l()
2160{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002161 // never process effects when:
2162 // - on an OFFLOAD thread
2163 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002164 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002165 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002166 bool tracksOnSession = (trackCnt() != 0);
2167
2168 if (!tracksOnSession && mTailBufferCount == 0) {
2169 doProcess = false;
2170 }
2171
2172 if (activeTrackCnt() == 0) {
2173 // if no track is active and the effect tail has not been rendered,
2174 // the input buffer must be cleared here as the mixer process will not do it
2175 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002176 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002177 if (mTailBufferCount > 0) {
2178 mTailBufferCount--;
2179 }
2180 }
2181 }
2182 }
2183
2184 size_t size = mEffects.size();
2185 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002186 // Only the input and output buffers of the chain can be external,
2187 // and 'update' / 'commit' do nothing for allocated buffers, thus
2188 // it's not needed to consider any other buffers here.
2189 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002190 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2191 mOutBuffer->update();
2192 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002193 for (size_t i = 0; i < size; i++) {
2194 mEffects[i]->process();
2195 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002196 mInBuffer->commit();
2197 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2198 mOutBuffer->commit();
2199 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002200 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002201 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002202 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002203 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2204 }
2205 if (doResetVolume) {
2206 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002207 }
2208}
2209
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002210// createEffect_l() must be called with ThreadBase::mLock held
2211status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002212 effect_descriptor_t *desc,
2213 int id,
2214 audio_session_t sessionId,
2215 bool pinned)
2216{
2217 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002218 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002219 status_t lStatus = effect->status();
2220 if (lStatus == NO_ERROR) {
2221 lStatus = addEffect_ll(effect);
2222 }
2223 if (lStatus != NO_ERROR) {
2224 effect.clear();
2225 }
2226 return lStatus;
2227}
2228
2229// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002230status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2231{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002232 Mutex::Autolock _l(mLock);
2233 return addEffect_ll(effect);
2234}
2235// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2236status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2237{
Eric Laurentca7cc822012-11-19 14:55:58 -08002238 effect_descriptor_t desc = effect->desc();
2239 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2240
Eric Laurent6b446ce2019-12-13 10:56:31 -08002241 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002242
2243 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2244 // Auxiliary effects are inserted at the beginning of mEffects vector as
2245 // they are processed first and accumulated in chain input buffer
2246 mEffects.insertAt(effect, 0);
2247
2248 // the input buffer for auxiliary effect contains mono samples in
2249 // 32 bit format. This is to avoid saturation in AudoMixer
2250 // accumulation stage. Saturation is done in EffectModule::process() before
2251 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002252 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002253 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002254#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002255 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002256 numSamples * sizeof(float), &halBuffer);
2257#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002258 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002259 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002260#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002261 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002262
2263 effect->configure();
2264
Mikhail Naganov022b9952017-01-04 16:36:51 -08002265 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002266 // auxiliary effects output samples to chain input buffer for further processing
2267 // by insert effects
2268 effect->setOutBuffer(mInBuffer);
2269 } else {
2270 // Insert effects are inserted at the end of mEffects vector as they are processed
2271 // after track and auxiliary effects.
2272 // Insert effect order as a function of indicated preference:
2273 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2274 // another effect is present
2275 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2276 // last effect claiming first position
2277 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2278 // first effect claiming last position
2279 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2280 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2281 // already present
2282
2283 size_t size = mEffects.size();
2284 size_t idx_insert = size;
2285 ssize_t idx_insert_first = -1;
2286 ssize_t idx_insert_last = -1;
2287
2288 for (size_t i = 0; i < size; i++) {
2289 effect_descriptor_t d = mEffects[i]->desc();
2290 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2291 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2292 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2293 // check invalid effect chaining combinations
2294 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2295 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2296 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2297 desc.name, d.name);
2298 return INVALID_OPERATION;
2299 }
2300 // remember position of first insert effect and by default
2301 // select this as insert position for new effect
2302 if (idx_insert == size) {
2303 idx_insert = i;
2304 }
2305 // remember position of last insert effect claiming
2306 // first position
2307 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2308 idx_insert_first = i;
2309 }
2310 // remember position of first insert effect claiming
2311 // last position
2312 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2313 idx_insert_last == -1) {
2314 idx_insert_last = i;
2315 }
2316 }
2317 }
2318
2319 // modify idx_insert from first position if needed
2320 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2321 if (idx_insert_last != -1) {
2322 idx_insert = idx_insert_last;
2323 } else {
2324 idx_insert = size;
2325 }
2326 } else {
2327 if (idx_insert_first != -1) {
2328 idx_insert = idx_insert_first + 1;
2329 }
2330 }
2331
Eric Laurentf1f22e72021-07-13 14:04:14 +02002332 mEffects.insertAt(effect, idx_insert);
2333
2334 effect->configure();
2335
Eric Laurentca7cc822012-11-19 14:55:58 -08002336 // always read samples from chain input buffer
2337 effect->setInBuffer(mInBuffer);
2338
2339 // if last effect in the chain, output samples to chain
2340 // output buffer, otherwise to chain input buffer
2341 if (idx_insert == size) {
2342 if (idx_insert != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002343 mEffects[idx_insert-1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002344 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002345 }
2346 effect->setOutBuffer(mOutBuffer);
2347 } else {
2348 effect->setOutBuffer(mInBuffer);
2349 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002350
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002351 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 idx_insert);
2353 }
2354 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002355
Eric Laurentca7cc822012-11-19 14:55:58 -08002356 return NO_ERROR;
2357}
2358
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002359// removeEffect_l() must be called with ThreadBase::mLock held
2360size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2361 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002362{
2363 Mutex::Autolock _l(mLock);
2364 size_t size = mEffects.size();
2365 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2366
2367 for (size_t i = 0; i < size; i++) {
2368 if (effect == mEffects[i]) {
2369 // calling stop here will remove pre-processing effect from the audio HAL.
2370 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2371 // the middle of a read from audio HAL
2372 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2373 mEffects[i]->state() == EffectModule::STOPPING) {
2374 mEffects[i]->stop();
2375 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002376 if (release) {
2377 mEffects[i]->release_l();
2378 }
2379
Mikhail Naganov022b9952017-01-04 16:36:51 -08002380 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002382 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002383 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002384 }
2385 }
2386 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002387
2388 // make sure the input buffer configuration for the new first effect in the chain
2389 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2390 if (i == 0 && size > 1) {
2391 mEffects[0]->configure();
2392 mEffects[0]->setInBuffer(mInBuffer);
2393 }
2394
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002395 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002396 this, i);
2397 break;
2398 }
2399 }
2400
2401 return mEffects.size();
2402}
2403
jiabin8f278ee2019-11-11 12:16:27 -08002404// setDevices_l() must be called with ThreadBase::mLock held
2405void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002406{
2407 size_t size = mEffects.size();
2408 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002409 mEffects[i]->setDevices(devices);
2410 }
2411}
2412
2413// setInputDevice_l() must be called with ThreadBase::mLock held
2414void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2415{
2416 size_t size = mEffects.size();
2417 for (size_t i = 0; i < size; i++) {
2418 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 }
2420}
2421
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002422// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002423void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2424{
2425 size_t size = mEffects.size();
2426 for (size_t i = 0; i < size; i++) {
2427 mEffects[i]->setMode(mode);
2428 }
2429}
2430
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002431// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002432void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2433{
2434 size_t size = mEffects.size();
2435 for (size_t i = 0; i < size; i++) {
2436 mEffects[i]->setAudioSource(source);
2437 }
2438}
2439
Zhou Songd505c642020-02-20 16:35:37 +08002440bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2441 for (const auto &effect : mEffects) {
2442 if (effect->isVolumeControlEnabled()) return true;
2443 }
2444 return false;
2445}
2446
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002447// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002448bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002449{
2450 uint32_t newLeft = *left;
2451 uint32_t newRight = *right;
2452 bool hasControl = false;
2453 int ctrlIdx = -1;
2454 size_t size = mEffects.size();
2455
2456 // first update volume controller
2457 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002458 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002459 ctrlIdx = i - 1;
2460 hasControl = true;
2461 break;
2462 }
2463 }
2464
Eric Laurentfa1e1232016-08-02 19:01:49 -07002465 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002466 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002467 if (hasControl) {
2468 *left = mNewLeftVolume;
2469 *right = mNewRightVolume;
2470 }
2471 return hasControl;
2472 }
2473
2474 mVolumeCtrlIdx = ctrlIdx;
2475 mLeftVolume = newLeft;
2476 mRightVolume = newRight;
2477
2478 // second get volume update from volume controller
2479 if (ctrlIdx >= 0) {
2480 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2481 mNewLeftVolume = newLeft;
2482 mNewRightVolume = newRight;
2483 }
2484 // then indicate volume to all other effects in chain.
2485 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002486 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002487 uint32_t lVol = newLeft;
2488 uint32_t rVol = newRight;
2489
2490 for (size_t i = 0; i < size; i++) {
2491 if ((int)i == ctrlIdx) {
2492 continue;
2493 }
2494 // this also works for ctrlIdx == -1 when there is no volume controller
2495 if ((int)i > ctrlIdx) {
2496 lVol = *left;
2497 rVol = *right;
2498 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002499 // Pass requested volume directly if this is volume monitor module
2500 if (mEffects[i]->isVolumeMonitor()) {
2501 mEffects[i]->setVolume(left, right, false);
2502 } else {
2503 mEffects[i]->setVolume(&lVol, &rVol, false);
2504 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002505 }
2506 *left = newLeft;
2507 *right = newRight;
2508
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002509 setVolumeForOutput_l(*left, *right);
2510
Eric Laurentca7cc822012-11-19 14:55:58 -08002511 return hasControl;
2512}
2513
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002514// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002515void AudioFlinger::EffectChain::resetVolume_l()
2516{
Eric Laurente7449bf2016-08-03 18:44:07 -07002517 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2518 uint32_t left = mLeftVolume;
2519 uint32_t right = mRightVolume;
2520 (void)setVolume_l(&left, &right, true);
2521 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002522}
2523
jiabineb3bda02020-06-30 14:07:03 -07002524// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2525bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2526{
2527 for (size_t i = 0; i < mEffects.size(); ++i) {
2528 if (mEffects[i]->isHapticGenerator()) {
2529 return true;
2530 }
2531 }
2532 return false;
2533}
2534
jiabine70bc7f2020-06-30 22:07:55 -07002535void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2536{
2537 Mutex::Autolock _l(mLock);
2538 for (size_t i = 0; i < mEffects.size(); ++i) {
2539 mEffects[i]->setHapticIntensity(id, intensity);
2540 }
2541}
2542
Eric Laurent1b928682014-10-02 19:41:47 -07002543void AudioFlinger::EffectChain::syncHalEffectsState()
2544{
2545 Mutex::Autolock _l(mLock);
2546 for (size_t i = 0; i < mEffects.size(); i++) {
2547 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2548 mEffects[i]->state() == EffectModule::STOPPING) {
2549 mEffects[i]->addEffectToHal_l();
2550 }
2551 }
2552}
2553
Eric Laurentca7cc822012-11-19 14:55:58 -08002554void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2555{
Eric Laurentca7cc822012-11-19 14:55:58 -08002556 String8 result;
2557
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002558 const size_t numEffects = mEffects.size();
2559 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002560
Marco Nelissenb2208842014-02-07 14:00:50 -08002561 if (numEffects) {
2562 bool locked = AudioFlinger::dumpTryLock(mLock);
2563 // failed to lock - AudioFlinger is probably deadlocked
2564 if (!locked) {
2565 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002566 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002567
Andy Hungbded9c82017-11-30 18:47:35 -08002568 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2569 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2570 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2571 (int)inBufferStr.size(), "In buffer ",
2572 (int)outBufferStr.size(), "Out buffer ");
2573 result.appendFormat("\t%s %s %d\n",
2574 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002575 write(fd, result.string(), result.size());
2576
2577 for (size_t i = 0; i < numEffects; ++i) {
2578 sp<EffectModule> effect = mEffects[i];
2579 if (effect != 0) {
2580 effect->dump(fd, args);
2581 }
2582 }
2583
2584 if (locked) {
2585 mLock.unlock();
2586 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002587 } else {
2588 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002589 }
2590}
2591
2592// must be called with ThreadBase::mLock held
2593void AudioFlinger::EffectChain::setEffectSuspended_l(
2594 const effect_uuid_t *type, bool suspend)
2595{
2596 sp<SuspendedEffectDesc> desc;
2597 // use effect type UUID timelow as key as there is no real risk of identical
2598 // timeLow fields among effect type UUIDs.
2599 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2600 if (suspend) {
2601 if (index >= 0) {
2602 desc = mSuspendedEffects.valueAt(index);
2603 } else {
2604 desc = new SuspendedEffectDesc();
2605 desc->mType = *type;
2606 mSuspendedEffects.add(type->timeLow, desc);
2607 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2608 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002609
Eric Laurentca7cc822012-11-19 14:55:58 -08002610 if (desc->mRefCount++ == 0) {
2611 sp<EffectModule> effect = getEffectIfEnabled(type);
2612 if (effect != 0) {
2613 desc->mEffect = effect;
2614 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002615 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002616 }
2617 }
2618 } else {
2619 if (index < 0) {
2620 return;
2621 }
2622 desc = mSuspendedEffects.valueAt(index);
2623 if (desc->mRefCount <= 0) {
2624 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002625 desc->mRefCount = 0;
2626 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002627 }
2628 if (--desc->mRefCount == 0) {
2629 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2630 if (desc->mEffect != 0) {
2631 sp<EffectModule> effect = desc->mEffect.promote();
2632 if (effect != 0) {
2633 effect->setSuspended(false);
2634 effect->lock();
2635 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002636 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002637 effect->setEnabled_l(handle->enabled());
2638 }
2639 effect->unlock();
2640 }
2641 desc->mEffect.clear();
2642 }
2643 mSuspendedEffects.removeItemsAt(index);
2644 }
2645 }
2646}
2647
2648// must be called with ThreadBase::mLock held
2649void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2650{
2651 sp<SuspendedEffectDesc> desc;
2652
2653 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2654 if (suspend) {
2655 if (index >= 0) {
2656 desc = mSuspendedEffects.valueAt(index);
2657 } else {
2658 desc = new SuspendedEffectDesc();
2659 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2660 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2661 }
2662 if (desc->mRefCount++ == 0) {
2663 Vector< sp<EffectModule> > effects;
2664 getSuspendEligibleEffects(effects);
2665 for (size_t i = 0; i < effects.size(); i++) {
2666 setEffectSuspended_l(&effects[i]->desc().type, true);
2667 }
2668 }
2669 } else {
2670 if (index < 0) {
2671 return;
2672 }
2673 desc = mSuspendedEffects.valueAt(index);
2674 if (desc->mRefCount <= 0) {
2675 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2676 desc->mRefCount = 1;
2677 }
2678 if (--desc->mRefCount == 0) {
2679 Vector<const effect_uuid_t *> types;
2680 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2681 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2682 continue;
2683 }
2684 types.add(&mSuspendedEffects.valueAt(i)->mType);
2685 }
2686 for (size_t i = 0; i < types.size(); i++) {
2687 setEffectSuspended_l(types[i], false);
2688 }
2689 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2690 mSuspendedEffects.keyAt(index));
2691 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2692 }
2693 }
2694}
2695
2696
2697// The volume effect is used for automated tests only
2698#ifndef OPENSL_ES_H_
2699static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2700 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2701const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2702#endif //OPENSL_ES_H_
2703
Eric Laurentd8365c52017-07-16 15:27:05 -07002704/* static */
2705bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2706{
2707 // Only NS and AEC are suspended when BtNRec is off
2708 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2709 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2710 return true;
2711 }
2712 return false;
2713}
2714
Eric Laurentca7cc822012-11-19 14:55:58 -08002715bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2716{
2717 // auxiliary effects and visualizer are never suspended on output mix
2718 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2719 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2720 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002721 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2722 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002723 return false;
2724 }
2725 return true;
2726}
2727
2728void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2729 Vector< sp<AudioFlinger::EffectModule> > &effects)
2730{
2731 effects.clear();
2732 for (size_t i = 0; i < mEffects.size(); i++) {
2733 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2734 effects.add(mEffects[i]);
2735 }
2736 }
2737}
2738
2739sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2740 const effect_uuid_t *type)
2741{
2742 sp<EffectModule> effect = getEffectFromType_l(type);
2743 return effect != 0 && effect->isEnabled() ? effect : 0;
2744}
2745
2746void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2747 bool enabled)
2748{
2749 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2750 if (enabled) {
2751 if (index < 0) {
2752 // if the effect is not suspend check if all effects are suspended
2753 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2754 if (index < 0) {
2755 return;
2756 }
2757 if (!isEffectEligibleForSuspend(effect->desc())) {
2758 return;
2759 }
2760 setEffectSuspended_l(&effect->desc().type, enabled);
2761 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2762 if (index < 0) {
2763 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2764 return;
2765 }
2766 }
2767 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2768 effect->desc().type.timeLow);
2769 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002770 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002771 if (desc->mEffect == 0) {
2772 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002773 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002774 effect->setSuspended(true);
2775 }
2776 } else {
2777 if (index < 0) {
2778 return;
2779 }
2780 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2781 effect->desc().type.timeLow);
2782 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2783 desc->mEffect.clear();
2784 effect->setSuspended(false);
2785 }
2786}
2787
Eric Laurent5baf2af2013-09-12 17:37:00 -07002788bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002789{
2790 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002791 return isNonOffloadableEnabled_l();
2792}
2793
2794bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2795{
Eric Laurent813e2a72013-08-31 12:59:48 -07002796 size_t size = mEffects.size();
2797 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002798 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002799 return true;
2800 }
2801 }
2802 return false;
2803}
2804
Eric Laurentaaa44472014-09-12 17:41:50 -07002805void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2806{
2807 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002808 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002809}
2810
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002811void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2812{
2813 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2814 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2815 }
2816 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2817 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2818 }
2819}
2820
2821void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2822{
2823 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2824 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2825 }
2826 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2827 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2828 }
2829}
2830
2831bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002832{
2833 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002834 for (const auto &effect : mEffects) {
2835 if (effect->isProcessImplemented()) {
2836 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002837 }
2838 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002839 // Allow effects without processing.
2840 return true;
2841}
2842
2843bool AudioFlinger::EffectChain::isFastCompatible() const
2844{
2845 Mutex::Autolock _l(mLock);
2846 for (const auto &effect : mEffects) {
2847 if (effect->isProcessImplemented()
2848 && effect->isImplementationSoftware()) {
2849 return false;
2850 }
2851 }
2852 // Allow effects without processing or hw accelerated effects.
2853 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002854}
2855
2856// isCompatibleWithThread_l() must be called with thread->mLock held
2857bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2858{
2859 Mutex::Autolock _l(mLock);
2860 for (size_t i = 0; i < mEffects.size(); i++) {
2861 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2862 return false;
2863 }
2864 }
2865 return true;
2866}
2867
Eric Laurent6b446ce2019-12-13 10:56:31 -08002868// EffectCallbackInterface implementation
2869status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2870 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2871 sp<EffectHalInterface> *effect) {
2872 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002873 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002874 if (effectsFactory != 0) {
2875 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2876 }
2877 return status;
2878}
2879
2880bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002881 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002882 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002883 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002884}
2885
2886status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2887 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002888 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002889}
2890
2891status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2892 sp<EffectHalInterface> effect) {
2893 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002894 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002895 if (t == nullptr) {
2896 return result;
2897 }
2898 sp <StreamHalInterface> st = t->stream();
2899 if (st == nullptr) {
2900 return result;
2901 }
2902 result = st->addEffect(effect);
2903 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2904 return result;
2905}
2906
2907status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2908 sp<EffectHalInterface> effect) {
2909 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002910 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002911 if (t == nullptr) {
2912 return result;
2913 }
2914 sp <StreamHalInterface> st = t->stream();
2915 if (st == nullptr) {
2916 return result;
2917 }
2918 result = st->removeEffect(effect);
2919 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2920 return result;
2921}
2922
2923audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002924 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002925 if (t == nullptr) {
2926 return AUDIO_IO_HANDLE_NONE;
2927 }
2928 return t->id();
2929}
2930
2931bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002932 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002933 if (t == nullptr) {
2934 return true;
2935 }
2936 return t->isOutput();
2937}
2938
2939bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002940 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002941 if (t == nullptr) {
2942 return false;
2943 }
2944 return t->type() == ThreadBase::OFFLOAD;
2945}
2946
2947bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002948 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002949 if (t == nullptr) {
2950 return false;
2951 }
2952 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2953}
2954
2955bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002956 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002957 if (t == nullptr) {
2958 return false;
2959 }
Andy Hungea840382020-05-05 21:50:17 -07002960 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002961}
2962
2963uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002964 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002965 if (t == nullptr) {
2966 return 0;
2967 }
2968 return t->sampleRate();
2969}
2970
Eric Laurentf1f22e72021-07-13 14:04:14 +02002971audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
2972 sp<ThreadBase> t = thread().promote();
2973 if (t == nullptr) {
2974 return AUDIO_CHANNEL_NONE;
2975 }
2976 sp<EffectChain> c = chain().promote();
2977 if (c == nullptr) {
2978 return AUDIO_CHANNEL_NONE;
2979 }
2980
2981 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
2982 || c->isFirstEffect(id)) {
2983 return t->mixerChannelMask();
2984 } else {
2985 return t->channelMask();
2986 }
2987}
2988
2989uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
2990 sp<ThreadBase> t = thread().promote();
2991 if (t == nullptr) {
2992 return 0;
2993 }
2994 sp<EffectChain> c = chain().promote();
2995 if (c == nullptr) {
2996 return 0;
2997 }
2998
2999 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
3000 || c->isFirstEffect(id)) {
3001 return audio_channel_count_from_out_mask(t->mixerChannelMask());
3002 } else {
3003 return t->channelCount();
3004 }
3005}
3006
3007audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003008 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003009 if (t == nullptr) {
3010 return AUDIO_CHANNEL_NONE;
3011 }
3012 return t->channelMask();
3013}
3014
Eric Laurentf1f22e72021-07-13 14:04:14 +02003015uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003016 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003017 if (t == nullptr) {
3018 return 0;
3019 }
3020 return t->channelCount();
3021}
3022
jiabineb3bda02020-06-30 14:07:03 -07003023audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003024 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003025 if (t == nullptr) {
3026 return AUDIO_CHANNEL_NONE;
3027 }
3028 return t->hapticChannelMask();
3029}
3030
Eric Laurent6b446ce2019-12-13 10:56:31 -08003031size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003032 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003033 if (t == nullptr) {
3034 return 0;
3035 }
3036 return t->frameCount();
3037}
3038
3039uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003040 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003041 if (t == nullptr) {
3042 return 0;
3043 }
3044 return t->latency_l();
3045}
3046
3047void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003048 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003049 if (t == nullptr) {
3050 return;
3051 }
3052 t->setVolumeForOutput_l(left, right);
3053}
3054
3055void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003056 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003057 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003058 if (t == nullptr) {
3059 return;
3060 }
3061 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3062
Andy Hung328d6772021-01-12 12:32:21 -08003063 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003064 if (c == nullptr) {
3065 return;
3066 }
Eric Laurent41709552019-12-16 19:34:05 -08003067 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3068 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003069}
3070
Eric Laurent41709552019-12-16 19:34:05 -08003071void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003072 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003073 if (t == nullptr) {
3074 return;
3075 }
Eric Laurent41709552019-12-16 19:34:05 -08003076 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3077 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003078}
3079
Eric Laurent41709552019-12-16 19:34:05 -08003080void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003081 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3082
Andy Hung328d6772021-01-12 12:32:21 -08003083 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003084 if (t == nullptr) {
3085 return;
3086 }
3087 t->onEffectDisable();
3088}
3089
3090bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3091 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003092 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003093 if (t == nullptr) {
3094 return false;
3095 }
3096 t->disconnectEffectHandle(handle, unpinIfLast);
3097 return true;
3098}
3099
3100void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003101 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003102 if (c == nullptr) {
3103 return;
3104 }
3105 c->resetVolume_l();
3106
3107}
3108
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003109product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003110 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003111 if (c == nullptr) {
3112 return PRODUCT_STRATEGY_NONE;
3113 }
3114 return c->strategy();
3115}
3116
3117int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003118 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003119 if (c == nullptr) {
3120 return 0;
3121 }
3122 return c->activeTrackCnt();
3123}
3124
Eric Laurentb82e6b72019-11-22 17:25:04 -08003125
3126#undef LOG_TAG
3127#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3128
3129status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3130{
3131 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3132 Mutex::Autolock _l(mProxyLock);
3133 if (status == NO_ERROR) {
3134 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003135 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003136 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003137 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003138 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003139 bs = handle.second->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 }
3146 ALOGV("%s enable %d status %d", __func__, enabled, status);
3147 return status;
3148}
3149
3150status_t AudioFlinger::DeviceEffectProxy::init(
3151 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3152//For all audio patches
3153//If src or sink device match
3154//If the effect is HW accelerated
3155// if no corresponding effect module
3156// Create EffectModule: mHalEffect
3157//Create and attach EffectHandle
3158//If the effect is not HW accelerated and the patch sink or src is a mixer port
3159// Create Effect on patch input or output thread on session -1
3160//Add EffectHandle to EffectHandle map of Effect Proxy:
3161 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3162 status_t status = NO_ERROR;
3163 for (auto &patch : patches) {
3164 status = onCreatePatch(patch.first, patch.second);
3165 ALOGV("%s onCreatePatch status %d", __func__, status);
3166 if (status == BAD_VALUE) {
3167 return status;
3168 }
3169 }
3170 return status;
3171}
3172
3173status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3174 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3175 status_t status = NAME_NOT_FOUND;
3176 sp<EffectHandle> handle;
3177 // only consider source[0] as this is the only "true" source of a patch
3178 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3179 ALOGV("%s source checkPort status %d", __func__, status);
3180 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3181 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3182 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3183 }
3184 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3185 Mutex::Autolock _l(mProxyLock);
3186 mEffectHandles.emplace(patchHandle, handle);
3187 }
3188 ALOGW_IF(status == BAD_VALUE,
3189 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3190
3191 return status;
3192}
3193
3194status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3195 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3196
3197 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3198 __func__, port->type, port->ext.device.type,
3199 port->ext.device.address, port->id, patch.isSoftware());
3200 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003201 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003202 return NAME_NOT_FOUND;
3203 }
3204 status_t status = NAME_NOT_FOUND;
3205
3206 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3207 Mutex::Autolock _l(mProxyLock);
3208 mDevicePort = *port;
3209 mHalEffect = new EffectModule(mMyCallback,
3210 const_cast<effect_descriptor_t *>(&mDescriptor),
3211 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3212 false /* pinned */, port->id);
3213 if (audio_is_input_device(mDevice.mType)) {
3214 mHalEffect->setInputDevice(mDevice);
3215 } else {
3216 mHalEffect->setDevices({mDevice});
3217 }
Eric Laurentde8caf42021-08-11 17:19:25 +02003218 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3219 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003220 status = (*handle)->initCheck();
3221 if (status == OK) {
3222 status = mHalEffect->addHandle((*handle).get());
3223 } else {
3224 mHalEffect.clear();
3225 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3226 }
3227 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3228 sp <ThreadBase> thread;
3229 if (audio_port_config_has_input_direction(port)) {
3230 if (patch.isSoftware()) {
3231 thread = patch.mRecord.thread();
3232 } else {
3233 thread = patch.thread().promote();
3234 }
3235 } else {
3236 if (patch.isSoftware()) {
3237 thread = patch.mPlayback.thread();
3238 } else {
3239 thread = patch.thread().promote();
3240 }
3241 }
3242 int enabled;
3243 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3244 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003245 &enabled, &status, false, false /*probe*/,
3246 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003247 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3248 } else {
3249 status = BAD_VALUE;
3250 }
3251 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003252 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003253 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003254 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003255 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003256 bs = (*handle)->disable(&status);
3257 }
3258 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003259 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003260 }
3261 }
3262 return status;
3263}
3264
3265void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3266 Mutex::Autolock _l(mProxyLock);
3267 mEffectHandles.erase(patchHandle);
3268}
3269
3270
3271size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3272{
3273 Mutex::Autolock _l(mProxyLock);
3274 if (effect == mHalEffect) {
3275 mHalEffect.clear();
3276 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3277 }
3278 return mHalEffect == nullptr ? 0 : 1;
3279}
3280
3281status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3282 sp<EffectHalInterface> effect) {
3283 if (mHalEffect == nullptr) {
3284 return NO_INIT;
3285 }
3286 return mManagerCallback->addEffectToHal(
3287 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3288}
3289
3290status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3291 sp<EffectHalInterface> effect) {
3292 if (mHalEffect == nullptr) {
3293 return NO_INIT;
3294 }
3295 return mManagerCallback->removeEffectFromHal(
3296 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3297}
3298
3299bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3300 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3301 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3302 }
3303 return true;
3304}
3305
3306uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3307 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3308 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3309 return mDevicePort.sample_rate;
3310 }
3311 return DEFAULT_OUTPUT_SAMPLE_RATE;
3312}
3313
3314audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3315 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3316 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3317 return mDevicePort.channel_mask;
3318 }
3319 return AUDIO_CHANNEL_OUT_STEREO;
3320}
3321
3322uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3323 if (isOutput()) {
3324 return audio_channel_count_from_out_mask(channelMask());
3325 }
3326 return audio_channel_count_from_in_mask(channelMask());
3327}
3328
3329void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3330 const Vector<String16> args;
3331 EffectBase::dump(fd, args);
3332
3333 const bool locked = dumpTryLock(mProxyLock);
3334 if (!locked) {
3335 String8 result("DeviceEffectProxy may be deadlocked\n");
3336 write(fd, result.string(), result.size());
3337 }
3338
3339 String8 outStr;
3340 if (mHalEffect != nullptr) {
3341 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3342 } else {
3343 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3344 }
3345 write(fd, outStr.string(), outStr.size());
3346 outStr.clear();
3347
3348 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3349 write(fd, outStr.string(), outStr.size());
3350 outStr.clear();
3351
3352 for (const auto& iter : mEffectHandles) {
3353 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3354 write(fd, outStr.string(), outStr.size());
3355 outStr.clear();
3356 sp<EffectBase> effect = iter.second->effect().promote();
3357 if (effect != nullptr) {
3358 effect->dump(fd, args);
3359 }
3360 }
3361
3362 if (locked) {
3363 mLock.unlock();
3364 }
3365}
3366
3367#undef LOG_TAG
3368#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3369
3370int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3371 return mManagerCallback->newEffectId();
3372}
3373
3374
3375bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3376 EffectHandle *handle, bool unpinIfLast) {
3377 sp<EffectBase> effectBase = handle->effect().promote();
3378 if (effectBase == nullptr) {
3379 return false;
3380 }
3381
3382 sp<EffectModule> effect = effectBase->asEffectModule();
3383 if (effect == nullptr) {
3384 return false;
3385 }
3386
3387 // restore suspended effects if the disconnected handle was enabled and the last one.
3388 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3389 if (remove) {
3390 sp<DeviceEffectProxy> proxy = mProxy.promote();
3391 if (proxy != nullptr) {
3392 proxy->removeEffect(effect);
3393 }
3394 if (handle->enabled()) {
3395 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3396 }
3397 }
3398 return true;
3399}
3400
3401status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3402 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3403 sp<EffectHalInterface> *effect) {
3404 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3405}
3406
3407status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3408 sp<EffectHalInterface> effect) {
3409 sp<DeviceEffectProxy> proxy = mProxy.promote();
3410 if (proxy == nullptr) {
3411 return NO_INIT;
3412 }
3413 return proxy->addEffectToHal(effect);
3414}
3415
3416status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3417 sp<EffectHalInterface> effect) {
3418 sp<DeviceEffectProxy> proxy = mProxy.promote();
3419 if (proxy == nullptr) {
3420 return NO_INIT;
3421 }
3422 return proxy->addEffectToHal(effect);
3423}
3424
3425bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3426 sp<DeviceEffectProxy> proxy = mProxy.promote();
3427 if (proxy == nullptr) {
3428 return true;
3429 }
3430 return proxy->isOutput();
3431}
3432
3433uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3434 sp<DeviceEffectProxy> proxy = mProxy.promote();
3435 if (proxy == nullptr) {
3436 return DEFAULT_OUTPUT_SAMPLE_RATE;
3437 }
3438 return proxy->sampleRate();
3439}
3440
Eric Laurentf1f22e72021-07-13 14:04:14 +02003441audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3442 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003443 sp<DeviceEffectProxy> proxy = mProxy.promote();
3444 if (proxy == nullptr) {
3445 return AUDIO_CHANNEL_OUT_STEREO;
3446 }
3447 return proxy->channelMask();
3448}
3449
Eric Laurentf1f22e72021-07-13 14:04:14 +02003450uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3451 sp<DeviceEffectProxy> proxy = mProxy.promote();
3452 if (proxy == nullptr) {
3453 return 2;
3454 }
3455 return proxy->channelCount();
3456}
3457
3458audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3459 sp<DeviceEffectProxy> proxy = mProxy.promote();
3460 if (proxy == nullptr) {
3461 return AUDIO_CHANNEL_OUT_STEREO;
3462 }
3463 return proxy->channelMask();
3464}
3465
3466uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003467 sp<DeviceEffectProxy> proxy = mProxy.promote();
3468 if (proxy == nullptr) {
3469 return 2;
3470 }
3471 return proxy->channelCount();
3472}
3473
Glenn Kasten63238ef2015-03-02 15:50:29 -08003474} // namespace android