blob: d09c9f432228621149e748febc64526bc6e06b16 [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
Andy Hung799c8d02021-10-28 17:05:40 -0700956 mConfig.outputCfg.accessMode = requiredEffectBufferAccessMode();
Eric Laurentca7cc822012-11-19 14:55:58 -0800957 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
958 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700959 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800960 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
961
Eric Laurent6b446ce2019-12-13 10:56:31 -0800962 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700963 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800964 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800965
966 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700967 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700968 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800969 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700970 &mConfig,
971 &size,
972 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700973 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800974 status = cmdStatus;
975 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800976
977#ifdef MULTICHANNEL_EFFECT_CHAIN
978 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700979 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800980 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
981 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
982 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700983 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
984 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800985 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
986 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
987 }
988 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
989 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
990 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
991 }
992 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700993 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800994 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700995 &mConfig,
996 &size,
997 &cmdStatus);
998 if (status == NO_ERROR) {
999 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -08001000 }
1001 }
1002#endif
1003
1004#ifdef FLOAT_EFFECT_CHAIN
1005 if (status == NO_ERROR) {
1006 mSupportsFloat = true;
1007 }
1008
1009 if (status != NO_ERROR) {
1010 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1011 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1012 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1013 size = sizeof(int);
1014 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1015 sizeof(mConfig),
1016 &mConfig,
1017 &size,
1018 &cmdStatus);
1019 if (status == NO_ERROR) {
1020 status = cmdStatus;
1021 }
1022 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001023 mSupportsFloat = false;
1024 ALOGVV("config worked with 16 bit");
1025 } else {
1026 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001027 }
rago94a1ee82017-07-21 15:11:02 -07001028 }
1029#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001030
rago94a1ee82017-07-21 15:11:02 -07001031 if (status == NO_ERROR) {
1032 // Establish Buffer strategy
1033 setInBuffer(mInBuffer);
1034 setOutBuffer(mOutBuffer);
1035
1036 // Update visualizer latency
1037 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1038 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1039 effect_param_t *p = (effect_param_t *)buf32;
1040
1041 p->psize = sizeof(uint32_t);
1042 p->vsize = sizeof(uint32_t);
1043 size = sizeof(int);
1044 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1045
Andy Hungfda44002021-06-03 17:23:16 -07001046 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001047
1048 *((int32_t *)p->data + 1)= latency;
1049 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1050 sizeof(effect_param_t) + 8,
1051 &buf32,
1052 &size,
1053 &cmdStatus);
1054 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001055 }
1056
Andy Hung05083ac2017-12-14 15:00:28 -08001057 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1058 mMaxDisableWaitCnt = (uint32_t)std::max(
1059 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1060 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1061 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001062
Eric Laurentd0ebb532013-04-02 16:41:41 -07001063exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001064 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001065 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001066 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001067 return status;
1068}
1069
1070status_t AudioFlinger::EffectModule::init()
1071{
1072 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001073 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 return NO_INIT;
1075 }
1076 status_t cmdStatus;
1077 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001078 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1079 0,
1080 NULL,
1081 &size,
1082 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001083 if (status == 0) {
1084 status = cmdStatus;
1085 }
1086 return status;
1087}
1088
Eric Laurent1b928682014-10-02 19:41:47 -07001089void AudioFlinger::EffectModule::addEffectToHal_l()
1090{
1091 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1092 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001093 if (mAddedToHal) {
1094 return;
1095 }
1096
Andy Hungfda44002021-06-03 17:23:16 -07001097 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001098 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001099 }
1100}
1101
Eric Laurentfa1e1232016-08-02 19:01:49 -07001102// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001103status_t AudioFlinger::EffectModule::start()
1104{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001105 status_t status;
1106 {
1107 Mutex::Autolock _l(mLock);
1108 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001109 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001110 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001111 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001112 }
1113 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001114}
1115
1116status_t AudioFlinger::EffectModule::start_l()
1117{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001118 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001119 return NO_INIT;
1120 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001121 if (mStatus != NO_ERROR) {
1122 return mStatus;
1123 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001124 status_t cmdStatus;
1125 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001126 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1127 0,
1128 NULL,
1129 &size,
1130 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001131 if (status == 0) {
1132 status = cmdStatus;
1133 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001134 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001135 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001136 }
1137 return status;
1138}
1139
1140status_t AudioFlinger::EffectModule::stop()
1141{
1142 Mutex::Autolock _l(mLock);
1143 return stop_l();
1144}
1145
1146status_t AudioFlinger::EffectModule::stop_l()
1147{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001148 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001149 return NO_INIT;
1150 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001151 if (mStatus != NO_ERROR) {
1152 return mStatus;
1153 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001154 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001155 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001156
1157 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001158 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1159 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1160 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001161 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001162 mSetVolumeReentrantTid = INVALID_PID;
1163 }
1164
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001165 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1166 0,
1167 NULL,
1168 &size,
1169 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001170 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001171 status = cmdStatus;
1172 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001173 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001174 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001175 }
1176 return status;
1177}
1178
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001179// must be called with EffectChain::mLock held
1180void AudioFlinger::EffectModule::release_l()
1181{
1182 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001183 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001184 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001185 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001186 mEffectInterface.clear();
1187 }
1188}
1189
Eric Laurent6b446ce2019-12-13 10:56:31 -08001190status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001191{
1192 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1193 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001194 if (!mAddedToHal) {
1195 return NO_ERROR;
1196 }
1197
Andy Hungfda44002021-06-03 17:23:16 -07001198 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001199 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001200 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001201 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001202}
1203
Andy Hunge4a1d912016-08-17 14:11:13 -07001204// round up delta valid if value and divisor are positive.
1205template <typename T>
1206static T roundUpDelta(const T &value, const T &divisor) {
1207 T remainder = value % divisor;
1208 return remainder == 0 ? 0 : divisor - remainder;
1209}
1210
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001211status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1212 const std::vector<uint8_t>& cmdData,
1213 int32_t maxReplySize,
1214 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001215{
1216 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001217 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001218
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001219 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001220 return NO_INIT;
1221 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001222 if (mStatus != NO_ERROR) {
1223 return mStatus;
1224 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001225 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1226 return -EINVAL;
1227 }
1228 size_t cmdSize = cmdData.size();
1229 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1230 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1231 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001232 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001233 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001234 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001235 android_errorWriteLog(0x534e4554, "33003822");
1236 return -EINVAL;
1237 }
1238 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 (maxReplySize < sizeof(effect_param_t) ||
1240 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001241 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001242 return -EINVAL;
1243 }
ragoe2759072016-11-22 18:02:48 -08001244 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001245 (sizeof(effect_param_t) > maxReplySize
1246 || param->psize > maxReplySize - sizeof(effect_param_t)
1247 || param->vsize > maxReplySize - sizeof(effect_param_t)
1248 - param->psize
1249 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1250 maxReplySize
1251 - sizeof(effect_param_t)
1252 - param->psize
1253 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001254 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1255 android_errorWriteLog(0x534e4554, "32705438");
1256 return -EINVAL;
1257 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001258 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001259 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1260 && // DEFERRED not generally used
1261 (param == nullptr
1262 || param->psize > cmdSize - sizeof(effect_param_t)
1263 || param->vsize > cmdSize - sizeof(effect_param_t)
1264 - param->psize
1265 || roundUpDelta(param->psize,
1266 (uint32_t) sizeof(int)) >
1267 cmdSize
1268 - sizeof(effect_param_t)
1269 - param->psize
1270 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001271 android_errorWriteLog(0x534e4554, "30204301");
1272 return -EINVAL;
1273 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001274 uint32_t replySize = maxReplySize;
1275 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001276 status_t status = mEffectInterface->command(cmdCode,
1277 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001278 const_cast<uint8_t*>(cmdData.data()),
1279 &replySize,
1280 reply->data());
1281 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001282 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001283 for (size_t i = 1; i < mHandles.size(); i++) {
1284 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001285 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001286 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001287 }
1288 }
1289 }
1290 return status;
1291}
1292
Eric Laurentca7cc822012-11-19 14:55:58 -08001293bool AudioFlinger::EffectModule::isProcessEnabled() const
1294{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001295 if (mStatus != NO_ERROR) {
1296 return false;
1297 }
1298
Eric Laurentca7cc822012-11-19 14:55:58 -08001299 switch (mState) {
1300 case RESTART:
1301 case ACTIVE:
1302 case STOPPING:
1303 case STOPPED:
1304 return true;
1305 case IDLE:
1306 case STARTING:
1307 case DESTROYED:
1308 default:
1309 return false;
1310 }
1311}
1312
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001313bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1314{
Andy Hungfda44002021-06-03 17:23:16 -07001315 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001316}
1317
1318bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1319{
1320 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1321}
1322
Mikhail Naganov022b9952017-01-04 16:36:51 -08001323void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001324 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001325
1326 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001327 if (buffer != 0) {
1328 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1329 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1330 } else {
1331 mConfig.inputCfg.buffer.raw = NULL;
1332 }
1333 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001334 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001335
1336#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001337 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001338 // Theoretically insert effects can also do in-place conversions (destroying
1339 // the original buffer) when the output buffer is identical to the input buffer,
1340 // but we don't optimize for it here.
1341 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001342 const uint32_t inChannelCount =
1343 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1344 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001345 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001346 // we need to translate - create hidl shared buffer and intercept
1347 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001348 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1349 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1350 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001351
1352 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1353 __func__, inChannels, inFrameCount, size);
1354
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001355 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001356 || size > mInConversionBuffer->getSize())) {
1357 mInConversionBuffer.clear();
1358 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001359 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001360 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001361 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001362 mInConversionBuffer->setFrameCount(inFrameCount);
1363 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001364 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001365 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001366 }
1367 }
1368#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001369}
1370
1371void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001372 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001373
1374 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001375 if (buffer != 0) {
1376 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1377 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1378 } else {
1379 mConfig.outputCfg.buffer.raw = NULL;
1380 }
1381 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001382 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001383
1384#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001385 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001386 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001387 const uint32_t outChannelCount =
1388 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1389 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001390 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001391 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001392 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1393 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1394 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001395
1396 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1397 __func__, outChannels, outFrameCount, size);
1398
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001399 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001400 || size > mOutConversionBuffer->getSize())) {
1401 mOutConversionBuffer.clear();
1402 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001403 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001404 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001405 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001406 mOutConversionBuffer->setFrameCount(outFrameCount);
1407 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001408 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001409 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001410 }
1411 }
1412#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001413}
1414
Eric Laurentca7cc822012-11-19 14:55:58 -08001415status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1416{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001417 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001418 if (mStatus != NO_ERROR) {
1419 return mStatus;
1420 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001422 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1423 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1424 if (isProcessEnabled() &&
1425 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001426 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1427 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 uint32_t volume[2];
1429 uint32_t *pVolume = NULL;
1430 uint32_t size = sizeof(volume);
1431 volume[0] = *left;
1432 volume[1] = *right;
1433 if (controller) {
1434 pVolume = volume;
1435 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001436 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1437 size,
1438 volume,
1439 &size,
1440 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001441 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1442 *left = volume[0];
1443 *right = volume[1];
1444 }
1445 }
1446 return status;
1447}
1448
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001449void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1450{
Zhou Songd505c642020-02-20 16:35:37 +08001451 // for offload or direct thread, if the effect chain has non-offloadable
1452 // effect and any effect module within the chain has volume control, then
1453 // volume control is delegated to effect, otherwise, set volume to hal.
1454 if (mEffectCallback->isOffloadOrDirect() &&
1455 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001456 float vol_l = (float)left / (1 << 24);
1457 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001458 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001459 }
1460}
1461
jiabin8f278ee2019-11-11 12:16:27 -08001462status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1463 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001464{
jiabin8f278ee2019-11-11 12:16:27 -08001465 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1466 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 return NO_ERROR;
1468 }
1469
1470 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001471 if (mStatus != NO_ERROR) {
1472 return mStatus;
1473 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001475 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001476 status_t cmdStatus;
1477 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001478 // FIXME: use audio device types and addresses when the hal interface is ready.
1479 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001480 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001481 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001482 &size,
1483 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 }
1485 return status;
1486}
1487
jiabin8f278ee2019-11-11 12:16:27 -08001488status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1489{
1490 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1491}
1492
1493status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1494{
1495 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1496}
1497
Eric Laurentca7cc822012-11-19 14:55:58 -08001498status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1499{
1500 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001501 if (mStatus != NO_ERROR) {
1502 return mStatus;
1503 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 status_t status = NO_ERROR;
1505 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1506 status_t cmdStatus;
1507 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001508 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1509 sizeof(audio_mode_t),
1510 &mode,
1511 &size,
1512 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001513 if (status == NO_ERROR) {
1514 status = cmdStatus;
1515 }
1516 }
1517 return status;
1518}
1519
1520status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1521{
1522 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001523 if (mStatus != NO_ERROR) {
1524 return mStatus;
1525 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001526 status_t status = NO_ERROR;
1527 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1528 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001529 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1530 sizeof(audio_source_t),
1531 &source,
1532 &size,
1533 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001534 }
1535 return status;
1536}
1537
Eric Laurent5baf2af2013-09-12 17:37:00 -07001538status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1539{
1540 Mutex::Autolock _l(mLock);
1541 if (mStatus != NO_ERROR) {
1542 return mStatus;
1543 }
1544 status_t status = NO_ERROR;
1545 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1546 status_t cmdStatus;
1547 uint32_t size = sizeof(status_t);
1548 effect_offload_param_t cmd;
1549
1550 cmd.isOffload = offloaded;
1551 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001552 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1553 sizeof(effect_offload_param_t),
1554 &cmd,
1555 &size,
1556 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001557 if (status == NO_ERROR) {
1558 status = cmdStatus;
1559 }
1560 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1561 } else {
1562 if (offloaded) {
1563 status = INVALID_OPERATION;
1564 }
1565 mOffloaded = false;
1566 }
1567 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1568 return status;
1569}
1570
1571bool AudioFlinger::EffectModule::isOffloaded() const
1572{
1573 Mutex::Autolock _l(mLock);
1574 return mOffloaded;
1575}
1576
jiabineb3bda02020-06-30 14:07:03 -07001577/*static*/
1578bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1579 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1580}
1581
1582bool AudioFlinger::EffectModule::isHapticGenerator() const {
1583 return isHapticGenerator(&mDescriptor.type);
1584}
1585
jiabine70bc7f2020-06-30 22:07:55 -07001586status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1587{
1588 if (mStatus != NO_ERROR) {
1589 return mStatus;
1590 }
1591 if (!isHapticGenerator()) {
1592 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1593 return INVALID_OPERATION;
1594 }
1595
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001596 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1597 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001598 param->psize = sizeof(int32_t);
1599 param->vsize = sizeof(int32_t) * 2;
1600 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1601 *((int32_t*)param->data + 1) = id;
1602 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001603 std::vector<uint8_t> response;
1604 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001605 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001606 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1607 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001608 }
1609 return status;
1610}
1611
Lais Andradebc3f37a2021-07-02 00:13:19 +01001612status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
jiabin1319f5a2021-03-30 22:21:24 +00001613{
1614 if (mStatus != NO_ERROR) {
1615 return mStatus;
1616 }
1617 if (!isHapticGenerator()) {
1618 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1619 return INVALID_OPERATION;
1620 }
1621
Lais Andradebc3f37a2021-07-02 00:13:19 +01001622 const size_t paramCount = 3;
jiabin1319f5a2021-03-30 22:21:24 +00001623 std::vector<uint8_t> request(
Lais Andradebc3f37a2021-07-02 00:13:19 +01001624 sizeof(effect_param_t) + sizeof(int32_t) + paramCount * sizeof(float));
jiabin1319f5a2021-03-30 22:21:24 +00001625 effect_param_t *param = (effect_param_t*) request.data();
1626 param->psize = sizeof(int32_t);
Lais Andradebc3f37a2021-07-02 00:13:19 +01001627 param->vsize = paramCount * sizeof(float);
jiabin1319f5a2021-03-30 22:21:24 +00001628 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1629 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
Lais Andradebc3f37a2021-07-02 00:13:19 +01001630 vibratorInfoPtr[0] = vibratorInfo.resonantFrequency;
1631 vibratorInfoPtr[1] = vibratorInfo.qFactor;
1632 vibratorInfoPtr[2] = vibratorInfo.maxAmplitude;
jiabin1319f5a2021-03-30 22:21:24 +00001633 std::vector<uint8_t> response;
1634 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1635 if (status == NO_ERROR) {
1636 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1637 status = *reinterpret_cast<const status_t*>(response.data());
1638 }
1639 return status;
1640}
1641
Andy Hungbded9c82017-11-30 18:47:35 -08001642static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1643 std::stringstream ss;
1644
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001645 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001646 return "nullptr"; // make different than below
1647 } else if (buffer->externalData() != nullptr) {
1648 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1649 << " -> "
1650 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1651 } else {
1652 ss << buffer->audioBuffer()->raw;
1653 }
1654 return ss.str();
1655}
Marco Nelissenb2208842014-02-07 14:00:50 -08001656
Eric Laurent41709552019-12-16 19:34:05 -08001657void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001658{
Eric Laurent41709552019-12-16 19:34:05 -08001659 EffectBase::dump(fd, args);
1660
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001663
Eric Laurent41709552019-12-16 19:34:05 -08001664 result.append("\t\tStatus Engine:\n");
1665 result.appendFormat("\t\t%03d %p\n",
1666 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001667
1668 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001669
1670 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001671 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1672 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1673 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001674 mConfig.inputCfg.buffer.frameCount,
1675 mConfig.inputCfg.samplingRate,
1676 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001677 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001678 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001679
1680 result.append("\t\t- Output configuration:\n");
1681 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001682 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001683 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 mConfig.outputCfg.buffer.frameCount,
1685 mConfig.outputCfg.samplingRate,
1686 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001687 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001688 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001689
rago94a1ee82017-07-21 15:11:02 -07001690#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001691
Andy Hungbded9c82017-11-30 18:47:35 -08001692 result.appendFormat("\t\t- HAL buffers:\n"
1693 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1694 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1695 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1696 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1697 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001698#endif
1699
Eric Laurentca7cc822012-11-19 14:55:58 -08001700 write(fd, result.string(), result.length());
1701
Mikhail Naganov4d547672019-02-22 14:19:19 -08001702 if (mEffectInterface != 0) {
1703 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1704 (void)mEffectInterface->dump(fd);
1705 }
1706
Eric Laurentca7cc822012-11-19 14:55:58 -08001707 if (locked) {
1708 mLock.unlock();
1709 }
1710}
1711
1712// ----------------------------------------------------------------------------
1713// EffectHandle implementation
1714// ----------------------------------------------------------------------------
1715
1716#undef LOG_TAG
1717#define LOG_TAG "AudioFlinger::EffectHandle"
1718
Eric Laurent41709552019-12-16 19:34:05 -08001719AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001720 const sp<AudioFlinger::Client>& client,
1721 const sp<media::IEffectClient>& effectClient,
Eric Laurentde8caf42021-08-11 17:19:25 +02001722 int32_t priority, bool notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001723 : BnEffect(),
1724 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurentde8caf42021-08-11 17:19:25 +02001725 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false),
1726 mNotifyFramesProcessed(notifyFramesProcessed)
Eric Laurentca7cc822012-11-19 14:55:58 -08001727{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001728 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001729
1730 if (client == 0) {
1731 return;
1732 }
1733 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1734 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001735 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001736 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001737 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001738 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001739 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001740 return;
1741 }
Glenn Kastene75da402013-11-20 13:54:52 -08001742 new(mCblk) effect_param_cblk_t();
1743 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001744}
1745
1746AudioFlinger::EffectHandle::~EffectHandle()
1747{
1748 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001749 disconnect(false);
1750}
1751
Glenn Kastene75da402013-11-20 13:54:52 -08001752status_t AudioFlinger::EffectHandle::initCheck()
1753{
1754 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1755}
1756
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001757#define RETURN(code) \
1758 *_aidl_return = (code); \
1759 return Status::ok();
1760
1761Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001762{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001763 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001764 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001765 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001766 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001767 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001768 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001770 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001771 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001772
1773 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001774 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001775 }
1776
1777 mEnabled = true;
1778
Eric Laurent6c796322019-04-09 14:13:17 -07001779 status_t status = effect->updatePolicyState();
1780 if (status != NO_ERROR) {
1781 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001782 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001783 }
1784
Eric Laurent6b446ce2019-12-13 10:56:31 -08001785 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001786
1787 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001788 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001789 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 }
1791
Eric Laurent6b446ce2019-12-13 10:56:31 -08001792 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001794 mEnabled = false;
1795 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001796 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797}
1798
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001799Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001800{
1801 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001802 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001803 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001804 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001805 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001807 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001808 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001809 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001810
1811 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001812 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001813 }
1814 mEnabled = false;
1815
Eric Laurent6c796322019-04-09 14:13:17 -07001816 effect->updatePolicyState();
1817
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001818 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001819 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 }
1821
Eric Laurent6b446ce2019-12-13 10:56:31 -08001822 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001823 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001824}
1825
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001826Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001827{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001828 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001830 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001831}
1832
1833void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1834{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001835 AutoMutex _l(mLock);
1836 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1837 if (mDisconnected) {
1838 if (unpinIfLast) {
1839 android_errorWriteLog(0x534e4554, "32707507");
1840 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001841 return;
1842 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001843 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001844 {
Eric Laurent41709552019-12-16 19:34:05 -08001845 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001846 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001847 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001848 ALOGW("%s Effect handle %p disconnected after thread destruction",
1849 __func__, this);
1850 }
1851 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001852 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001854
Eric Laurentca7cc822012-11-19 14:55:58 -08001855 if (mClient != 0) {
1856 if (mCblk != NULL) {
1857 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1858 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1859 }
1860 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001861 // Client destructor must run with AudioFlinger client mutex locked
1862 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001863 mClient.clear();
1864 }
1865}
1866
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1868 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1869 return Status::ok();
1870}
1871
1872Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1873 const std::vector<uint8_t>& cmdData,
1874 int32_t maxResponseSize,
1875 std::vector<uint8_t>* response,
1876 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001877{
1878 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001879 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001880
Eric Laurentc7ab3092017-06-15 18:43:46 -07001881 // reject commands reserved for internal use by audio framework if coming from outside
1882 // of audioserver
1883 switch(cmdCode) {
1884 case EFFECT_CMD_ENABLE:
1885 case EFFECT_CMD_DISABLE:
1886 case EFFECT_CMD_SET_PARAM:
1887 case EFFECT_CMD_SET_PARAM_DEFERRED:
1888 case EFFECT_CMD_SET_PARAM_COMMIT:
1889 case EFFECT_CMD_GET_PARAM:
1890 break;
1891 default:
1892 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1893 break;
1894 }
1895 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001896 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001897 }
1898
Eric Laurent1ffc5852016-12-15 14:46:09 -08001899 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001900 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001901 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001902 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001903 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001904 writeToBuffer(NO_ERROR, response);
1905 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001906 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001907 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001908 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001910 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001911 writeToBuffer(NO_ERROR, response);
1912 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001913 }
1914
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001915 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001916 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001918 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001919 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001920 // only get parameter command is permitted for applications not controlling the effect
1921 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001922 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001923 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001924
1925 // handle commands that are not forwarded transparently to effect engine
1926 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001927 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001928 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001929 }
1930
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001931 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001932 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001933 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001934 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001935 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001936
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 // No need to trylock() here as this function is executed in the binder thread serving a
1938 // particular client process: no risk to block the whole media server process or mixer
1939 // threads if we are stuck here
1940 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001941 // keep local copy of index in case of client corruption b/32220769
1942 const uint32_t clientIndex = mCblk->clientIndex;
1943 const uint32_t serverIndex = mCblk->serverIndex;
1944 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1945 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001946 mCblk->serverIndex = 0;
1947 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001948 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001949 }
1950 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001951 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001952 for (uint32_t index = serverIndex; index < clientIndex;) {
1953 int *p = (int *)(mBuffer + index);
1954 const int size = *p++;
1955 if (size < 0
1956 || size > EFFECT_PARAM_BUFFER_SIZE
1957 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001958 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001959 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001960 break;
1961 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001962
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001963 std::copy(reinterpret_cast<const uint8_t*>(p),
1964 reinterpret_cast<const uint8_t*>(p) + size,
1965 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001966
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001968 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001969 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001970 sizeof(int),
1971 &replyBuffer);
1972 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001973
1974 // verify shared memory: server index shouldn't change; client index can't go back.
1975 if (serverIndex != mCblk->serverIndex
1976 || clientIndex > mCblk->clientIndex) {
1977 android_errorWriteLog(0x534e4554, "32220769");
1978 status = BAD_VALUE;
1979 break;
1980 }
1981
Eric Laurentca7cc822012-11-19 14:55:58 -08001982 // stop at first error encountered
1983 if (ret != NO_ERROR) {
1984 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001985 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001986 break;
1987 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001988 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001989 break;
1990 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001991 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001992 }
1993 mCblk->serverIndex = 0;
1994 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001995 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001996 }
1997
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001998 status_t status = effect->command(cmdCode,
1999 cmdData,
2000 maxResponseSize,
2001 response);
2002 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08002003}
2004
2005void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
2006{
2007 ALOGV("setControl %p control %d", this, hasControl);
2008
2009 mHasControl = hasControl;
2010 mEnabled = enabled;
2011
2012 if (signal && mEffectClient != 0) {
2013 mEffectClient->controlStatusChanged(hasControl);
2014 }
2015}
2016
2017void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002018 const std::vector<uint8_t>& cmdData,
2019 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002020{
2021 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002022 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002023 }
2024}
2025
2026
2027
2028void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2029{
2030 if (mEffectClient != 0) {
2031 mEffectClient->enableStatusChanged(enabled);
2032 }
2033}
2034
Eric Laurentde8caf42021-08-11 17:19:25 +02002035void AudioFlinger::EffectHandle::framesProcessed(int32_t frames) const
2036{
2037 if (mEffectClient != 0 && mNotifyFramesProcessed) {
2038 mEffectClient->framesProcessed(frames);
2039 }
2040}
2041
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002042void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002043{
2044 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2045
Marco Nelissenb2208842014-02-07 14:00:50 -08002046 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002047 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002048 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002049 mHasControl ? "yes" : "no",
2050 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002051 mCblk ? mCblk->clientIndex : 0,
2052 mCblk ? mCblk->serverIndex : 0
2053 );
2054
2055 if (locked) {
2056 mCblk->lock.unlock();
2057 }
2058}
2059
2060#undef LOG_TAG
2061#define LOG_TAG "AudioFlinger::EffectChain"
2062
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002063AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2064 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002065 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002066 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002067 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002068 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002069{
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002070 sp<ThreadBase> p = thread.promote();
2071 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002072 return;
2073 }
Eric Laurentd66d7a12021-07-13 13:35:32 +02002074 mStrategy = p->getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002075 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2076 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002077}
2078
2079AudioFlinger::EffectChain::~EffectChain()
2080{
Eric Laurentca7cc822012-11-19 14:55:58 -08002081}
2082
2083// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2084sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2085 effect_descriptor_t *descriptor)
2086{
2087 size_t size = mEffects.size();
2088
2089 for (size_t i = 0; i < size; i++) {
2090 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2091 return mEffects[i];
2092 }
2093 }
2094 return 0;
2095}
2096
2097// getEffectFromId_l() must be called with ThreadBase::mLock held
2098sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2099{
2100 size_t size = mEffects.size();
2101
2102 for (size_t i = 0; i < size; i++) {
2103 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2104 if (id == 0 || mEffects[i]->id() == id) {
2105 return mEffects[i];
2106 }
2107 }
2108 return 0;
2109}
2110
2111// getEffectFromType_l() must be called with ThreadBase::mLock held
2112sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2113 const effect_uuid_t *type)
2114{
2115 size_t size = mEffects.size();
2116
2117 for (size_t i = 0; i < size; i++) {
2118 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2119 return mEffects[i];
2120 }
2121 }
2122 return 0;
2123}
2124
Eric Laurent6c796322019-04-09 14:13:17 -07002125std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2126{
2127 std::vector<int> ids;
2128 Mutex::Autolock _l(mLock);
2129 for (size_t i = 0; i < mEffects.size(); i++) {
2130 ids.push_back(mEffects[i]->id());
2131 }
2132 return ids;
2133}
2134
Eric Laurentca7cc822012-11-19 14:55:58 -08002135void AudioFlinger::EffectChain::clearInputBuffer()
2136{
2137 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002138 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002139}
2140
2141// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002142void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002143{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002144 if (mInBuffer == NULL) {
2145 return;
2146 }
Eric Laurentf1f22e72021-07-13 14:04:14 +02002147 const size_t frameSize = audio_bytes_per_sample(EFFECT_BUFFER_FORMAT)
2148 * mEffectCallback->inChannelCount(mEffects[0]->id());
rago94a1ee82017-07-21 15:11:02 -07002149
Eric Laurent6b446ce2019-12-13 10:56:31 -08002150 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002151 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002152}
2153
2154// Must be called with EffectChain::mLock locked
2155void AudioFlinger::EffectChain::process_l()
2156{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002157 // never process effects when:
2158 // - on an OFFLOAD thread
2159 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002160 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002161 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002162 bool tracksOnSession = (trackCnt() != 0);
2163
2164 if (!tracksOnSession && mTailBufferCount == 0) {
2165 doProcess = false;
2166 }
2167
2168 if (activeTrackCnt() == 0) {
2169 // if no track is active and the effect tail has not been rendered,
2170 // the input buffer must be cleared here as the mixer process will not do it
2171 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002172 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002173 if (mTailBufferCount > 0) {
2174 mTailBufferCount--;
2175 }
2176 }
2177 }
2178 }
2179
2180 size_t size = mEffects.size();
2181 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002182 // Only the input and output buffers of the chain can be external,
2183 // and 'update' / 'commit' do nothing for allocated buffers, thus
2184 // it's not needed to consider any other buffers here.
2185 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002186 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2187 mOutBuffer->update();
2188 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002189 for (size_t i = 0; i < size; i++) {
2190 mEffects[i]->process();
2191 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002192 mInBuffer->commit();
2193 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2194 mOutBuffer->commit();
2195 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002196 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002197 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002198 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002199 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2200 }
2201 if (doResetVolume) {
2202 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002203 }
2204}
2205
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002206// createEffect_l() must be called with ThreadBase::mLock held
2207status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002208 effect_descriptor_t *desc,
2209 int id,
2210 audio_session_t sessionId,
2211 bool pinned)
2212{
2213 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002214 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002215 status_t lStatus = effect->status();
2216 if (lStatus == NO_ERROR) {
2217 lStatus = addEffect_ll(effect);
2218 }
2219 if (lStatus != NO_ERROR) {
2220 effect.clear();
2221 }
2222 return lStatus;
2223}
2224
2225// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002226status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2227{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002228 Mutex::Autolock _l(mLock);
2229 return addEffect_ll(effect);
2230}
2231// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2232status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2233{
Eric Laurentca7cc822012-11-19 14:55:58 -08002234 effect_descriptor_t desc = effect->desc();
2235 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2236
Eric Laurent6b446ce2019-12-13 10:56:31 -08002237 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002238
2239 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2240 // Auxiliary effects are inserted at the beginning of mEffects vector as
2241 // they are processed first and accumulated in chain input buffer
2242 mEffects.insertAt(effect, 0);
2243
2244 // the input buffer for auxiliary effect contains mono samples in
2245 // 32 bit format. This is to avoid saturation in AudoMixer
2246 // accumulation stage. Saturation is done in EffectModule::process() before
2247 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002248 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002249 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002250#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002251 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002252 numSamples * sizeof(float), &halBuffer);
2253#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002254 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002255 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002256#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002257 if (result != OK) return result;
Eric Laurentf1f22e72021-07-13 14:04:14 +02002258
2259 effect->configure();
2260
Mikhail Naganov022b9952017-01-04 16:36:51 -08002261 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002262 // auxiliary effects output samples to chain input buffer for further processing
2263 // by insert effects
2264 effect->setOutBuffer(mInBuffer);
2265 } else {
2266 // Insert effects are inserted at the end of mEffects vector as they are processed
2267 // after track and auxiliary effects.
2268 // Insert effect order as a function of indicated preference:
2269 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2270 // another effect is present
2271 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2272 // last effect claiming first position
2273 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2274 // first effect claiming last position
2275 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2276 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2277 // already present
2278
2279 size_t size = mEffects.size();
2280 size_t idx_insert = size;
2281 ssize_t idx_insert_first = -1;
2282 ssize_t idx_insert_last = -1;
2283
2284 for (size_t i = 0; i < size; i++) {
2285 effect_descriptor_t d = mEffects[i]->desc();
2286 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2287 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2288 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2289 // check invalid effect chaining combinations
2290 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2291 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2292 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2293 desc.name, d.name);
2294 return INVALID_OPERATION;
2295 }
2296 // remember position of first insert effect and by default
2297 // select this as insert position for new effect
2298 if (idx_insert == size) {
2299 idx_insert = i;
2300 }
2301 // remember position of last insert effect claiming
2302 // first position
2303 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2304 idx_insert_first = i;
2305 }
2306 // remember position of first insert effect claiming
2307 // last position
2308 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2309 idx_insert_last == -1) {
2310 idx_insert_last = i;
2311 }
2312 }
2313 }
2314
2315 // modify idx_insert from first position if needed
2316 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2317 if (idx_insert_last != -1) {
2318 idx_insert = idx_insert_last;
2319 } else {
2320 idx_insert = size;
2321 }
2322 } else {
2323 if (idx_insert_first != -1) {
2324 idx_insert = idx_insert_first + 1;
2325 }
2326 }
2327
Eric Laurentf1f22e72021-07-13 14:04:14 +02002328 mEffects.insertAt(effect, idx_insert);
2329
2330 effect->configure();
2331
Eric Laurentca7cc822012-11-19 14:55:58 -08002332 // always read samples from chain input buffer
2333 effect->setInBuffer(mInBuffer);
2334
2335 // if last effect in the chain, output samples to chain
2336 // output buffer, otherwise to chain input buffer
2337 if (idx_insert == size) {
2338 if (idx_insert != 0) {
Andy Hung799c8d02021-10-28 17:05:40 -07002339 // update channel mask before setting output buffer.
2340 mEffects[idx_insert - 1]->configure();
2341 mEffects[idx_insert - 1]->setOutBuffer(mInBuffer); // set output buffer
2342 mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
Eric Laurentca7cc822012-11-19 14:55:58 -08002343 }
2344 effect->setOutBuffer(mOutBuffer);
2345 } else {
2346 effect->setOutBuffer(mInBuffer);
2347 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002348
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002349 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002350 idx_insert);
2351 }
2352 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002353
Eric Laurentca7cc822012-11-19 14:55:58 -08002354 return NO_ERROR;
2355}
2356
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002357// removeEffect_l() must be called with ThreadBase::mLock held
2358size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2359 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002360{
2361 Mutex::Autolock _l(mLock);
2362 size_t size = mEffects.size();
2363 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2364
2365 for (size_t i = 0; i < size; i++) {
2366 if (effect == mEffects[i]) {
2367 // calling stop here will remove pre-processing effect from the audio HAL.
2368 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2369 // the middle of a read from audio HAL
2370 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2371 mEffects[i]->state() == EffectModule::STOPPING) {
2372 mEffects[i]->stop();
2373 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002374 if (release) {
2375 mEffects[i]->release_l();
2376 }
2377
Mikhail Naganov022b9952017-01-04 16:36:51 -08002378 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002379 if (i == size - 1 && i != 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002380 mEffects[i - 1]->configure();
Eric Laurentf1f22e72021-07-13 14:04:14 +02002381 mEffects[i - 1]->setOutBuffer(mOutBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002382 }
2383 }
2384 mEffects.removeAt(i);
Eric Laurentf1f22e72021-07-13 14:04:14 +02002385
2386 // make sure the input buffer configuration for the new first effect in the chain
2387 // is updated if needed (can switch from HAL channel mask to mixer channel mask)
2388 if (i == 0 && size > 1) {
2389 mEffects[0]->configure();
2390 mEffects[0]->setInBuffer(mInBuffer);
2391 }
2392
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002393 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002394 this, i);
2395 break;
2396 }
2397 }
2398
2399 return mEffects.size();
2400}
2401
jiabin8f278ee2019-11-11 12:16:27 -08002402// setDevices_l() must be called with ThreadBase::mLock held
2403void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002404{
2405 size_t size = mEffects.size();
2406 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002407 mEffects[i]->setDevices(devices);
2408 }
2409}
2410
2411// setInputDevice_l() must be called with ThreadBase::mLock held
2412void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2413{
2414 size_t size = mEffects.size();
2415 for (size_t i = 0; i < size; i++) {
2416 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002417 }
2418}
2419
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002420// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002421void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2422{
2423 size_t size = mEffects.size();
2424 for (size_t i = 0; i < size; i++) {
2425 mEffects[i]->setMode(mode);
2426 }
2427}
2428
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002429// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002430void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2431{
2432 size_t size = mEffects.size();
2433 for (size_t i = 0; i < size; i++) {
2434 mEffects[i]->setAudioSource(source);
2435 }
2436}
2437
Zhou Songd505c642020-02-20 16:35:37 +08002438bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2439 for (const auto &effect : mEffects) {
2440 if (effect->isVolumeControlEnabled()) return true;
2441 }
2442 return false;
2443}
2444
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002445// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002446bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002447{
2448 uint32_t newLeft = *left;
2449 uint32_t newRight = *right;
2450 bool hasControl = false;
2451 int ctrlIdx = -1;
2452 size_t size = mEffects.size();
2453
2454 // first update volume controller
2455 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002456 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002457 ctrlIdx = i - 1;
2458 hasControl = true;
2459 break;
2460 }
2461 }
2462
Eric Laurentfa1e1232016-08-02 19:01:49 -07002463 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002464 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002465 if (hasControl) {
2466 *left = mNewLeftVolume;
2467 *right = mNewRightVolume;
2468 }
2469 return hasControl;
2470 }
2471
2472 mVolumeCtrlIdx = ctrlIdx;
2473 mLeftVolume = newLeft;
2474 mRightVolume = newRight;
2475
2476 // second get volume update from volume controller
2477 if (ctrlIdx >= 0) {
2478 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2479 mNewLeftVolume = newLeft;
2480 mNewRightVolume = newRight;
2481 }
2482 // then indicate volume to all other effects in chain.
2483 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002484 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002485 uint32_t lVol = newLeft;
2486 uint32_t rVol = newRight;
2487
2488 for (size_t i = 0; i < size; i++) {
2489 if ((int)i == ctrlIdx) {
2490 continue;
2491 }
2492 // this also works for ctrlIdx == -1 when there is no volume controller
2493 if ((int)i > ctrlIdx) {
2494 lVol = *left;
2495 rVol = *right;
2496 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002497 // Pass requested volume directly if this is volume monitor module
2498 if (mEffects[i]->isVolumeMonitor()) {
2499 mEffects[i]->setVolume(left, right, false);
2500 } else {
2501 mEffects[i]->setVolume(&lVol, &rVol, false);
2502 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002503 }
2504 *left = newLeft;
2505 *right = newRight;
2506
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002507 setVolumeForOutput_l(*left, *right);
2508
Eric Laurentca7cc822012-11-19 14:55:58 -08002509 return hasControl;
2510}
2511
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002512// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002513void AudioFlinger::EffectChain::resetVolume_l()
2514{
Eric Laurente7449bf2016-08-03 18:44:07 -07002515 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2516 uint32_t left = mLeftVolume;
2517 uint32_t right = mRightVolume;
2518 (void)setVolume_l(&left, &right, true);
2519 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002520}
2521
jiabineb3bda02020-06-30 14:07:03 -07002522// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2523bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2524{
2525 for (size_t i = 0; i < mEffects.size(); ++i) {
2526 if (mEffects[i]->isHapticGenerator()) {
2527 return true;
2528 }
2529 }
2530 return false;
2531}
2532
jiabine70bc7f2020-06-30 22:07:55 -07002533void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2534{
2535 Mutex::Autolock _l(mLock);
2536 for (size_t i = 0; i < mEffects.size(); ++i) {
2537 mEffects[i]->setHapticIntensity(id, intensity);
2538 }
2539}
2540
Eric Laurent1b928682014-10-02 19:41:47 -07002541void AudioFlinger::EffectChain::syncHalEffectsState()
2542{
2543 Mutex::Autolock _l(mLock);
2544 for (size_t i = 0; i < mEffects.size(); i++) {
2545 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2546 mEffects[i]->state() == EffectModule::STOPPING) {
2547 mEffects[i]->addEffectToHal_l();
2548 }
2549 }
2550}
2551
Eric Laurentca7cc822012-11-19 14:55:58 -08002552void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2553{
Eric Laurentca7cc822012-11-19 14:55:58 -08002554 String8 result;
2555
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002556 const size_t numEffects = mEffects.size();
2557 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002558
Marco Nelissenb2208842014-02-07 14:00:50 -08002559 if (numEffects) {
2560 bool locked = AudioFlinger::dumpTryLock(mLock);
2561 // failed to lock - AudioFlinger is probably deadlocked
2562 if (!locked) {
2563 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002564 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002565
Andy Hungbded9c82017-11-30 18:47:35 -08002566 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2567 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2568 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2569 (int)inBufferStr.size(), "In buffer ",
2570 (int)outBufferStr.size(), "Out buffer ");
2571 result.appendFormat("\t%s %s %d\n",
2572 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002573 write(fd, result.string(), result.size());
2574
2575 for (size_t i = 0; i < numEffects; ++i) {
2576 sp<EffectModule> effect = mEffects[i];
2577 if (effect != 0) {
2578 effect->dump(fd, args);
2579 }
2580 }
2581
2582 if (locked) {
2583 mLock.unlock();
2584 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002585 } else {
2586 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002587 }
2588}
2589
2590// must be called with ThreadBase::mLock held
2591void AudioFlinger::EffectChain::setEffectSuspended_l(
2592 const effect_uuid_t *type, bool suspend)
2593{
2594 sp<SuspendedEffectDesc> desc;
2595 // use effect type UUID timelow as key as there is no real risk of identical
2596 // timeLow fields among effect type UUIDs.
2597 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2598 if (suspend) {
2599 if (index >= 0) {
2600 desc = mSuspendedEffects.valueAt(index);
2601 } else {
2602 desc = new SuspendedEffectDesc();
2603 desc->mType = *type;
2604 mSuspendedEffects.add(type->timeLow, desc);
2605 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2606 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002607
Eric Laurentca7cc822012-11-19 14:55:58 -08002608 if (desc->mRefCount++ == 0) {
2609 sp<EffectModule> effect = getEffectIfEnabled(type);
2610 if (effect != 0) {
2611 desc->mEffect = effect;
2612 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002613 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002614 }
2615 }
2616 } else {
2617 if (index < 0) {
2618 return;
2619 }
2620 desc = mSuspendedEffects.valueAt(index);
2621 if (desc->mRefCount <= 0) {
2622 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002623 desc->mRefCount = 0;
2624 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002625 }
2626 if (--desc->mRefCount == 0) {
2627 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2628 if (desc->mEffect != 0) {
2629 sp<EffectModule> effect = desc->mEffect.promote();
2630 if (effect != 0) {
2631 effect->setSuspended(false);
2632 effect->lock();
2633 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002634 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002635 effect->setEnabled_l(handle->enabled());
2636 }
2637 effect->unlock();
2638 }
2639 desc->mEffect.clear();
2640 }
2641 mSuspendedEffects.removeItemsAt(index);
2642 }
2643 }
2644}
2645
2646// must be called with ThreadBase::mLock held
2647void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2648{
2649 sp<SuspendedEffectDesc> desc;
2650
2651 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2652 if (suspend) {
2653 if (index >= 0) {
2654 desc = mSuspendedEffects.valueAt(index);
2655 } else {
2656 desc = new SuspendedEffectDesc();
2657 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2658 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2659 }
2660 if (desc->mRefCount++ == 0) {
2661 Vector< sp<EffectModule> > effects;
2662 getSuspendEligibleEffects(effects);
2663 for (size_t i = 0; i < effects.size(); i++) {
2664 setEffectSuspended_l(&effects[i]->desc().type, true);
2665 }
2666 }
2667 } else {
2668 if (index < 0) {
2669 return;
2670 }
2671 desc = mSuspendedEffects.valueAt(index);
2672 if (desc->mRefCount <= 0) {
2673 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2674 desc->mRefCount = 1;
2675 }
2676 if (--desc->mRefCount == 0) {
2677 Vector<const effect_uuid_t *> types;
2678 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2679 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2680 continue;
2681 }
2682 types.add(&mSuspendedEffects.valueAt(i)->mType);
2683 }
2684 for (size_t i = 0; i < types.size(); i++) {
2685 setEffectSuspended_l(types[i], false);
2686 }
2687 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2688 mSuspendedEffects.keyAt(index));
2689 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2690 }
2691 }
2692}
2693
2694
2695// The volume effect is used for automated tests only
2696#ifndef OPENSL_ES_H_
2697static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2698 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2699const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2700#endif //OPENSL_ES_H_
2701
Eric Laurentd8365c52017-07-16 15:27:05 -07002702/* static */
2703bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2704{
2705 // Only NS and AEC are suspended when BtNRec is off
2706 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2707 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2708 return true;
2709 }
2710 return false;
2711}
2712
Eric Laurentca7cc822012-11-19 14:55:58 -08002713bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2714{
2715 // auxiliary effects and visualizer are never suspended on output mix
2716 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2717 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2718 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002719 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2720 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002721 return false;
2722 }
2723 return true;
2724}
2725
2726void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2727 Vector< sp<AudioFlinger::EffectModule> > &effects)
2728{
2729 effects.clear();
2730 for (size_t i = 0; i < mEffects.size(); i++) {
2731 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2732 effects.add(mEffects[i]);
2733 }
2734 }
2735}
2736
2737sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2738 const effect_uuid_t *type)
2739{
2740 sp<EffectModule> effect = getEffectFromType_l(type);
2741 return effect != 0 && effect->isEnabled() ? effect : 0;
2742}
2743
2744void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2745 bool enabled)
2746{
2747 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2748 if (enabled) {
2749 if (index < 0) {
2750 // if the effect is not suspend check if all effects are suspended
2751 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2752 if (index < 0) {
2753 return;
2754 }
2755 if (!isEffectEligibleForSuspend(effect->desc())) {
2756 return;
2757 }
2758 setEffectSuspended_l(&effect->desc().type, enabled);
2759 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2760 if (index < 0) {
2761 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2762 return;
2763 }
2764 }
2765 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2766 effect->desc().type.timeLow);
2767 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002768 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002769 if (desc->mEffect == 0) {
2770 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002771 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002772 effect->setSuspended(true);
2773 }
2774 } else {
2775 if (index < 0) {
2776 return;
2777 }
2778 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2779 effect->desc().type.timeLow);
2780 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2781 desc->mEffect.clear();
2782 effect->setSuspended(false);
2783 }
2784}
2785
Eric Laurent5baf2af2013-09-12 17:37:00 -07002786bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002787{
2788 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002789 return isNonOffloadableEnabled_l();
2790}
2791
2792bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2793{
Eric Laurent813e2a72013-08-31 12:59:48 -07002794 size_t size = mEffects.size();
2795 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002796 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002797 return true;
2798 }
2799 }
2800 return false;
2801}
2802
Eric Laurentaaa44472014-09-12 17:41:50 -07002803void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2804{
2805 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002806 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002807}
2808
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002809void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2810{
2811 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2812 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2813 }
2814 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2815 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2816 }
2817}
2818
2819void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2820{
2821 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2822 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2823 }
2824 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2825 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2826 }
2827}
2828
2829bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002830{
2831 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002832 for (const auto &effect : mEffects) {
2833 if (effect->isProcessImplemented()) {
2834 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002835 }
2836 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002837 // Allow effects without processing.
2838 return true;
2839}
2840
2841bool AudioFlinger::EffectChain::isFastCompatible() const
2842{
2843 Mutex::Autolock _l(mLock);
2844 for (const auto &effect : mEffects) {
2845 if (effect->isProcessImplemented()
2846 && effect->isImplementationSoftware()) {
2847 return false;
2848 }
2849 }
2850 // Allow effects without processing or hw accelerated effects.
2851 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002852}
2853
2854// isCompatibleWithThread_l() must be called with thread->mLock held
2855bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2856{
2857 Mutex::Autolock _l(mLock);
2858 for (size_t i = 0; i < mEffects.size(); i++) {
2859 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2860 return false;
2861 }
2862 }
2863 return true;
2864}
2865
Eric Laurent6b446ce2019-12-13 10:56:31 -08002866// EffectCallbackInterface implementation
2867status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2868 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2869 sp<EffectHalInterface> *effect) {
2870 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002871 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002872 if (effectsFactory != 0) {
2873 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2874 }
2875 return status;
2876}
2877
2878bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002879 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002880 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002881 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002882}
2883
2884status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2885 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002886 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002887}
2888
2889status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2890 sp<EffectHalInterface> effect) {
2891 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002892 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002893 if (t == nullptr) {
2894 return result;
2895 }
2896 sp <StreamHalInterface> st = t->stream();
2897 if (st == nullptr) {
2898 return result;
2899 }
2900 result = st->addEffect(effect);
2901 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2902 return result;
2903}
2904
2905status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2906 sp<EffectHalInterface> effect) {
2907 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002908 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002909 if (t == nullptr) {
2910 return result;
2911 }
2912 sp <StreamHalInterface> st = t->stream();
2913 if (st == nullptr) {
2914 return result;
2915 }
2916 result = st->removeEffect(effect);
2917 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2918 return result;
2919}
2920
2921audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002922 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002923 if (t == nullptr) {
2924 return AUDIO_IO_HANDLE_NONE;
2925 }
2926 return t->id();
2927}
2928
2929bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002930 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002931 if (t == nullptr) {
2932 return true;
2933 }
2934 return t->isOutput();
2935}
2936
2937bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002938 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002939 if (t == nullptr) {
2940 return false;
2941 }
2942 return t->type() == ThreadBase::OFFLOAD;
2943}
2944
2945bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002946 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002947 if (t == nullptr) {
2948 return false;
2949 }
2950 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2951}
2952
2953bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002954 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002955 if (t == nullptr) {
2956 return false;
2957 }
Andy Hungea840382020-05-05 21:50:17 -07002958 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002959}
2960
2961uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002962 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002963 if (t == nullptr) {
2964 return 0;
2965 }
2966 return t->sampleRate();
2967}
2968
Eric Laurentf1f22e72021-07-13 14:04:14 +02002969audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::inChannelMask(int id) const {
2970 sp<ThreadBase> t = thread().promote();
2971 if (t == nullptr) {
2972 return AUDIO_CHANNEL_NONE;
2973 }
2974 sp<EffectChain> c = chain().promote();
2975 if (c == nullptr) {
2976 return AUDIO_CHANNEL_NONE;
2977 }
2978
2979 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
2980 || c->isFirstEffect(id)) {
2981 return t->mixerChannelMask();
2982 } else {
2983 return t->channelMask();
2984 }
2985}
2986
2987uint32_t AudioFlinger::EffectChain::EffectCallback::inChannelCount(int id) const {
2988 sp<ThreadBase> t = thread().promote();
2989 if (t == nullptr) {
2990 return 0;
2991 }
2992 sp<EffectChain> c = chain().promote();
2993 if (c == nullptr) {
2994 return 0;
2995 }
2996
2997 if (c->sessionId() != AUDIO_SESSION_OUTPUT_STAGE
2998 || c->isFirstEffect(id)) {
2999 return audio_channel_count_from_out_mask(t->mixerChannelMask());
3000 } else {
3001 return t->channelCount();
3002 }
3003}
3004
3005audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::outChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003006 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003007 if (t == nullptr) {
3008 return AUDIO_CHANNEL_NONE;
3009 }
3010 return t->channelMask();
3011}
3012
Eric Laurentf1f22e72021-07-13 14:04:14 +02003013uint32_t AudioFlinger::EffectChain::EffectCallback::outChannelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003014 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003015 if (t == nullptr) {
3016 return 0;
3017 }
3018 return t->channelCount();
3019}
3020
jiabineb3bda02020-06-30 14:07:03 -07003021audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08003022 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07003023 if (t == nullptr) {
3024 return AUDIO_CHANNEL_NONE;
3025 }
3026 return t->hapticChannelMask();
3027}
3028
Eric Laurent6b446ce2019-12-13 10:56:31 -08003029size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08003030 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003031 if (t == nullptr) {
3032 return 0;
3033 }
3034 return t->frameCount();
3035}
3036
3037uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08003038 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003039 if (t == nullptr) {
3040 return 0;
3041 }
3042 return t->latency_l();
3043}
3044
3045void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08003046 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003047 if (t == nullptr) {
3048 return;
3049 }
3050 t->setVolumeForOutput_l(left, right);
3051}
3052
3053void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08003054 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08003055 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003056 if (t == nullptr) {
3057 return;
3058 }
3059 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
3060
Andy Hung328d6772021-01-12 12:32:21 -08003061 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003062 if (c == nullptr) {
3063 return;
3064 }
Eric Laurent41709552019-12-16 19:34:05 -08003065 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3066 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08003067}
3068
Eric Laurent41709552019-12-16 19:34:05 -08003069void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003070 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003071 if (t == nullptr) {
3072 return;
3073 }
Eric Laurent41709552019-12-16 19:34:05 -08003074 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3075 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003076}
3077
Eric Laurent41709552019-12-16 19:34:05 -08003078void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003079 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3080
Andy Hung328d6772021-01-12 12:32:21 -08003081 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003082 if (t == nullptr) {
3083 return;
3084 }
3085 t->onEffectDisable();
3086}
3087
3088bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3089 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003090 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003091 if (t == nullptr) {
3092 return false;
3093 }
3094 t->disconnectEffectHandle(handle, unpinIfLast);
3095 return true;
3096}
3097
3098void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003099 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003100 if (c == nullptr) {
3101 return;
3102 }
3103 c->resetVolume_l();
3104
3105}
3106
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003107product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003108 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003109 if (c == nullptr) {
3110 return PRODUCT_STRATEGY_NONE;
3111 }
3112 return c->strategy();
3113}
3114
3115int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003116 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003117 if (c == nullptr) {
3118 return 0;
3119 }
3120 return c->activeTrackCnt();
3121}
3122
Eric Laurentb82e6b72019-11-22 17:25:04 -08003123
3124#undef LOG_TAG
3125#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3126
3127status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3128{
3129 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3130 Mutex::Autolock _l(mProxyLock);
3131 if (status == NO_ERROR) {
3132 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003133 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003134 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003135 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003136 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003137 bs = handle.second->disable(&status);
3138 }
3139 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003140 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003141 }
3142 }
3143 }
3144 ALOGV("%s enable %d status %d", __func__, enabled, status);
3145 return status;
3146}
3147
3148status_t AudioFlinger::DeviceEffectProxy::init(
3149 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3150//For all audio patches
3151//If src or sink device match
3152//If the effect is HW accelerated
3153// if no corresponding effect module
3154// Create EffectModule: mHalEffect
3155//Create and attach EffectHandle
3156//If the effect is not HW accelerated and the patch sink or src is a mixer port
3157// Create Effect on patch input or output thread on session -1
3158//Add EffectHandle to EffectHandle map of Effect Proxy:
3159 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3160 status_t status = NO_ERROR;
3161 for (auto &patch : patches) {
3162 status = onCreatePatch(patch.first, patch.second);
3163 ALOGV("%s onCreatePatch status %d", __func__, status);
3164 if (status == BAD_VALUE) {
3165 return status;
3166 }
3167 }
3168 return status;
3169}
3170
3171status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3172 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3173 status_t status = NAME_NOT_FOUND;
3174 sp<EffectHandle> handle;
3175 // only consider source[0] as this is the only "true" source of a patch
3176 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3177 ALOGV("%s source checkPort status %d", __func__, status);
3178 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3179 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3180 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3181 }
3182 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3183 Mutex::Autolock _l(mProxyLock);
3184 mEffectHandles.emplace(patchHandle, handle);
3185 }
3186 ALOGW_IF(status == BAD_VALUE,
3187 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3188
3189 return status;
3190}
3191
3192status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3193 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3194
3195 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3196 __func__, port->type, port->ext.device.type,
3197 port->ext.device.address, port->id, patch.isSoftware());
3198 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003199 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003200 return NAME_NOT_FOUND;
3201 }
3202 status_t status = NAME_NOT_FOUND;
3203
3204 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3205 Mutex::Autolock _l(mProxyLock);
3206 mDevicePort = *port;
3207 mHalEffect = new EffectModule(mMyCallback,
3208 const_cast<effect_descriptor_t *>(&mDescriptor),
3209 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3210 false /* pinned */, port->id);
3211 if (audio_is_input_device(mDevice.mType)) {
3212 mHalEffect->setInputDevice(mDevice);
3213 } else {
3214 mHalEffect->setDevices({mDevice});
3215 }
Eric Laurentde8caf42021-08-11 17:19:25 +02003216 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
3217 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003218 status = (*handle)->initCheck();
3219 if (status == OK) {
3220 status = mHalEffect->addHandle((*handle).get());
3221 } else {
3222 mHalEffect.clear();
3223 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3224 }
3225 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3226 sp <ThreadBase> thread;
3227 if (audio_port_config_has_input_direction(port)) {
3228 if (patch.isSoftware()) {
3229 thread = patch.mRecord.thread();
3230 } else {
3231 thread = patch.thread().promote();
3232 }
3233 } else {
3234 if (patch.isSoftware()) {
3235 thread = patch.mPlayback.thread();
3236 } else {
3237 thread = patch.thread().promote();
3238 }
3239 }
3240 int enabled;
3241 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3242 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurentde8caf42021-08-11 17:19:25 +02003243 &enabled, &status, false, false /*probe*/,
3244 mNotifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003245 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3246 } else {
3247 status = BAD_VALUE;
3248 }
3249 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003250 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003251 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003252 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003253 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003254 bs = (*handle)->disable(&status);
3255 }
3256 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003257 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003258 }
3259 }
3260 return status;
3261}
3262
3263void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3264 Mutex::Autolock _l(mProxyLock);
3265 mEffectHandles.erase(patchHandle);
3266}
3267
3268
3269size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3270{
3271 Mutex::Autolock _l(mProxyLock);
3272 if (effect == mHalEffect) {
3273 mHalEffect.clear();
3274 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3275 }
3276 return mHalEffect == nullptr ? 0 : 1;
3277}
3278
3279status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3280 sp<EffectHalInterface> effect) {
3281 if (mHalEffect == nullptr) {
3282 return NO_INIT;
3283 }
3284 return mManagerCallback->addEffectToHal(
3285 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3286}
3287
3288status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3289 sp<EffectHalInterface> effect) {
3290 if (mHalEffect == nullptr) {
3291 return NO_INIT;
3292 }
3293 return mManagerCallback->removeEffectFromHal(
3294 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3295}
3296
3297bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3298 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3299 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3300 }
3301 return true;
3302}
3303
3304uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3305 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3306 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3307 return mDevicePort.sample_rate;
3308 }
3309 return DEFAULT_OUTPUT_SAMPLE_RATE;
3310}
3311
3312audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3313 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3314 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3315 return mDevicePort.channel_mask;
3316 }
3317 return AUDIO_CHANNEL_OUT_STEREO;
3318}
3319
3320uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3321 if (isOutput()) {
3322 return audio_channel_count_from_out_mask(channelMask());
3323 }
3324 return audio_channel_count_from_in_mask(channelMask());
3325}
3326
3327void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3328 const Vector<String16> args;
3329 EffectBase::dump(fd, args);
3330
3331 const bool locked = dumpTryLock(mProxyLock);
3332 if (!locked) {
3333 String8 result("DeviceEffectProxy may be deadlocked\n");
3334 write(fd, result.string(), result.size());
3335 }
3336
3337 String8 outStr;
3338 if (mHalEffect != nullptr) {
3339 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3340 } else {
3341 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3342 }
3343 write(fd, outStr.string(), outStr.size());
3344 outStr.clear();
3345
3346 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3347 write(fd, outStr.string(), outStr.size());
3348 outStr.clear();
3349
3350 for (const auto& iter : mEffectHandles) {
3351 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3352 write(fd, outStr.string(), outStr.size());
3353 outStr.clear();
3354 sp<EffectBase> effect = iter.second->effect().promote();
3355 if (effect != nullptr) {
3356 effect->dump(fd, args);
3357 }
3358 }
3359
3360 if (locked) {
3361 mLock.unlock();
3362 }
3363}
3364
3365#undef LOG_TAG
3366#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3367
3368int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3369 return mManagerCallback->newEffectId();
3370}
3371
3372
3373bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3374 EffectHandle *handle, bool unpinIfLast) {
3375 sp<EffectBase> effectBase = handle->effect().promote();
3376 if (effectBase == nullptr) {
3377 return false;
3378 }
3379
3380 sp<EffectModule> effect = effectBase->asEffectModule();
3381 if (effect == nullptr) {
3382 return false;
3383 }
3384
3385 // restore suspended effects if the disconnected handle was enabled and the last one.
3386 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3387 if (remove) {
3388 sp<DeviceEffectProxy> proxy = mProxy.promote();
3389 if (proxy != nullptr) {
3390 proxy->removeEffect(effect);
3391 }
3392 if (handle->enabled()) {
3393 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3394 }
3395 }
3396 return true;
3397}
3398
3399status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3400 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3401 sp<EffectHalInterface> *effect) {
3402 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3403}
3404
3405status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3406 sp<EffectHalInterface> effect) {
3407 sp<DeviceEffectProxy> proxy = mProxy.promote();
3408 if (proxy == nullptr) {
3409 return NO_INIT;
3410 }
3411 return proxy->addEffectToHal(effect);
3412}
3413
3414status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3415 sp<EffectHalInterface> effect) {
3416 sp<DeviceEffectProxy> proxy = mProxy.promote();
3417 if (proxy == nullptr) {
3418 return NO_INIT;
3419 }
3420 return proxy->addEffectToHal(effect);
3421}
3422
3423bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3424 sp<DeviceEffectProxy> proxy = mProxy.promote();
3425 if (proxy == nullptr) {
3426 return true;
3427 }
3428 return proxy->isOutput();
3429}
3430
3431uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3432 sp<DeviceEffectProxy> proxy = mProxy.promote();
3433 if (proxy == nullptr) {
3434 return DEFAULT_OUTPUT_SAMPLE_RATE;
3435 }
3436 return proxy->sampleRate();
3437}
3438
Eric Laurentf1f22e72021-07-13 14:04:14 +02003439audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelMask(
3440 int id __unused) const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003441 sp<DeviceEffectProxy> proxy = mProxy.promote();
3442 if (proxy == nullptr) {
3443 return AUDIO_CHANNEL_OUT_STEREO;
3444 }
3445 return proxy->channelMask();
3446}
3447
Eric Laurentf1f22e72021-07-13 14:04:14 +02003448uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::inChannelCount(int id __unused) const {
3449 sp<DeviceEffectProxy> proxy = mProxy.promote();
3450 if (proxy == nullptr) {
3451 return 2;
3452 }
3453 return proxy->channelCount();
3454}
3455
3456audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelMask() const {
3457 sp<DeviceEffectProxy> proxy = mProxy.promote();
3458 if (proxy == nullptr) {
3459 return AUDIO_CHANNEL_OUT_STEREO;
3460 }
3461 return proxy->channelMask();
3462}
3463
3464uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::outChannelCount() const {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003465 sp<DeviceEffectProxy> proxy = mProxy.promote();
3466 if (proxy == nullptr) {
3467 return 2;
3468 }
3469 return proxy->channelCount();
3470}
3471
Glenn Kasten63238ef2015-03-02 15:50:29 -08003472} // namespace android