blob: 3dfeb83a4c999b8da46ff6079da22fc442ff6d1a [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>
Eric Laurentd8365c52017-07-16 15:27:05 -070028#include <system/audio_effects/effect_ns.h>
29#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080030#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070032#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080033#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070034#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080035#include <media/AudioDeviceTypeAddr.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070036#include <media/audiohal/EffectHalInterface.h>
37#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070038#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080039
40#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42// ----------------------------------------------------------------------------
43
44// Note: the following macro is used for extremely verbose logging message. In
45// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
46// 0; but one side effect of this is to turn all LOGV's as well. Some messages
47// are so verbose that we want to suppress them even when we have ALOG_ASSERT
48// turned on. Do not uncomment the #def below unless you really know what you
49// are doing and want to see all of the extremely verbose messages.
50//#define VERY_VERY_VERBOSE_LOGGING
51#ifdef VERY_VERY_VERBOSE_LOGGING
52#define ALOGVV ALOGV
53#else
54#define ALOGVV(a...) do { } while(0)
55#endif
56
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090057#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
58
Eric Laurentca7cc822012-11-19 14:55:58 -080059namespace android {
60
61// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080062// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080063// ----------------------------------------------------------------------------
64
65#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080066#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080067
Eric Laurent41709552019-12-16 19:34:05 -080068AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080069 effect_descriptor_t *desc,
70 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080071 audio_session_t sessionId,
72 bool pinned)
73 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080074 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080075 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080076{
Eric Laurentca7cc822012-11-19 14:55:58 -080077}
78
Eric Laurent41709552019-12-16 19:34:05 -080079// must be called with EffectModule::mLock held
80status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -080081{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080082
Eric Laurent41709552019-12-16 19:34:05 -080083 ALOGV("setEnabled %p enabled %d", this, enabled);
84
85 if (enabled != isEnabled()) {
86 switch (mState) {
87 // going from disabled to enabled
88 case IDLE:
89 mState = STARTING;
90 break;
91 case STOPPED:
92 mState = RESTART;
93 break;
94 case STOPPING:
95 mState = ACTIVE;
96 break;
97
98 // going from enabled to disabled
99 case RESTART:
100 mState = STOPPED;
101 break;
102 case STARTING:
103 mState = IDLE;
104 break;
105 case ACTIVE:
106 mState = STOPPING;
107 break;
108 case DESTROYED:
109 return NO_ERROR; // simply ignore as we are being destroyed
110 }
111 for (size_t i = 1; i < mHandles.size(); i++) {
112 EffectHandle *h = mHandles[i];
113 if (h != NULL && !h->disconnected()) {
114 h->setEnabled(enabled);
115 }
116 }
117 }
118 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800119}
120
Eric Laurent41709552019-12-16 19:34:05 -0800121status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
122{
123 status_t status;
124 {
125 Mutex::Autolock _l(mLock);
126 status = setEnabled_l(enabled);
127 }
128 if (fromHandle) {
129 if (enabled) {
130 if (status != NO_ERROR) {
131 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
132 } else {
133 mCallback->onEffectEnable(this);
134 }
135 } else {
136 mCallback->onEffectDisable(this);
137 }
138 }
139 return status;
140}
141
142bool AudioFlinger::EffectBase::isEnabled() const
143{
144 switch (mState) {
145 case RESTART:
146 case STARTING:
147 case ACTIVE:
148 return true;
149 case IDLE:
150 case STOPPING:
151 case STOPPED:
152 case DESTROYED:
153 default:
154 return false;
155 }
156}
157
158void AudioFlinger::EffectBase::setSuspended(bool suspended)
159{
160 Mutex::Autolock _l(mLock);
161 mSuspended = suspended;
162}
163
164bool AudioFlinger::EffectBase::suspended() const
165{
166 Mutex::Autolock _l(mLock);
167 return mSuspended;
168}
169
170status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800171{
172 status_t status;
173
174 Mutex::Autolock _l(mLock);
175 int priority = handle->priority();
176 size_t size = mHandles.size();
177 EffectHandle *controlHandle = NULL;
178 size_t i;
179 for (i = 0; i < size; i++) {
180 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800181 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800182 continue;
183 }
184 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700185 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800186 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700187 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800188 if (h->priority() <= priority) {
189 break;
190 }
191 }
192 // if inserted in first place, move effect control from previous owner to this handle
193 if (i == 0) {
194 bool enabled = false;
195 if (controlHandle != NULL) {
196 enabled = controlHandle->enabled();
197 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
198 }
199 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
200 status = NO_ERROR;
201 } else {
202 status = ALREADY_EXISTS;
203 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700204 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 mHandles.insertAt(handle, i);
206 return status;
207}
208
Eric Laurent41709552019-12-16 19:34:05 -0800209status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700210{
211 status_t status = NO_ERROR;
212 bool doRegister = false;
213 bool registered = false;
214 bool doEnable = false;
215 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700216 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovf698ff22020-03-31 10:07:29 -0700217 uint32_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700218
219 {
220 Mutex::Autolock _l(mLock);
221 // register effect when first handle is attached and unregister when last handle is removed
222 if (mPolicyRegistered != mHandles.size() > 0) {
223 doRegister = true;
224 mPolicyRegistered = mHandles.size() > 0;
225 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800226 io = mCallback->io();
227 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700228 }
229 }
230 // enable effect when registered according to enable state requested by controlling handle
231 if (mHandles.size() > 0) {
232 EffectHandle *handle = controlHandle_l();
233 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
234 doEnable = true;
235 mPolicyEnabled = handle->enabled();
236 }
237 }
238 registered = mPolicyRegistered;
239 enabled = mPolicyEnabled;
240 mPolicyLock.lock();
241 }
242 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
243 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
244 if (doRegister) {
245 if (registered) {
246 status = AudioSystem::registerEffect(
247 &mDescriptor,
248 io,
249 strategy,
250 mSessionId,
251 mId);
252 } else {
253 status = AudioSystem::unregisterEffect(mId);
254 }
255 }
256 if (registered && doEnable) {
257 status = AudioSystem::setEffectEnabled(mId, enabled);
258 }
259 mPolicyLock.unlock();
260
261 return status;
262}
263
264
Eric Laurent41709552019-12-16 19:34:05 -0800265ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800266{
267 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800268 return removeHandle_l(handle);
269}
270
Eric Laurent41709552019-12-16 19:34:05 -0800271ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800272{
Eric Laurentca7cc822012-11-19 14:55:58 -0800273 size_t size = mHandles.size();
274 size_t i;
275 for (i = 0; i < size; i++) {
276 if (mHandles[i] == handle) {
277 break;
278 }
279 }
280 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800281 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
282 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800283 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800284 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800285
286 mHandles.removeAt(i);
287 // if removed from first place, move effect control from this handle to next in line
288 if (i == 0) {
289 EffectHandle *h = controlHandle_l();
290 if (h != NULL) {
291 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
292 }
293 }
294
Eric Laurentca7cc822012-11-19 14:55:58 -0800295 if (mHandles.size() == 0 && !mPinned) {
296 mState = DESTROYED;
297 }
298
299 return mHandles.size();
300}
301
302// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800303AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800304{
305 // the first valid handle in the list has control over the module
306 for (size_t i = 0; i < mHandles.size(); i++) {
307 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800308 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800309 return h;
310 }
311 }
312
313 return NULL;
314}
315
Eric Laurentf10c7092016-12-06 17:09:56 -0800316// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800317ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800318{
319 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800320 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
321 return mHandles.size();
322 }
323
Eric Laurentf10c7092016-12-06 17:09:56 -0800324 Mutex::Autolock _l(mLock);
325 ssize_t numHandles = removeHandle_l(handle);
326 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800327 mLock.unlock();
328 mCallback->updateOrphanEffectChains(this);
329 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800330 }
331 return numHandles;
332}
333
Eric Laurent41709552019-12-16 19:34:05 -0800334bool AudioFlinger::EffectBase::purgeHandles()
335{
336 bool enabled = false;
337 Mutex::Autolock _l(mLock);
338 EffectHandle *handle = controlHandle_l();
339 if (handle != NULL) {
340 enabled = handle->enabled();
341 }
342 mHandles.clear();
343 return enabled;
344}
345
346void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
347 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
348}
349
350static String8 effectFlagsToString(uint32_t flags) {
351 String8 s;
352
353 s.append("conn. mode: ");
354 switch (flags & EFFECT_FLAG_TYPE_MASK) {
355 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
356 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
357 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
358 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
359 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
360 default: s.append("unknown/reserved"); break;
361 }
362 s.append(", ");
363
364 s.append("insert pref: ");
365 switch (flags & EFFECT_FLAG_INSERT_MASK) {
366 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
367 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
368 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
369 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
370 default: s.append("unknown/reserved"); break;
371 }
372 s.append(", ");
373
374 s.append("volume mgmt: ");
375 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
376 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
377 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
378 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
379 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
380 default: s.append("unknown/reserved"); break;
381 }
382 s.append(", ");
383
384 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
385 if (devind) {
386 s.append("device indication: ");
387 switch (devind) {
388 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
389 default: s.append("unknown/reserved"); break;
390 }
391 s.append(", ");
392 }
393
394 s.append("input mode: ");
395 switch (flags & EFFECT_FLAG_INPUT_MASK) {
396 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
397 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
398 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
399 default: s.append("not set"); break;
400 }
401 s.append(", ");
402
403 s.append("output mode: ");
404 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
405 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
406 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
407 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
408 default: s.append("not set"); break;
409 }
410 s.append(", ");
411
412 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
413 if (accel) {
414 s.append("hardware acceleration: ");
415 switch (accel) {
416 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
417 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
418 default: s.append("unknown/reserved"); break;
419 }
420 s.append(", ");
421 }
422
423 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
424 if (modeind) {
425 s.append("mode indication: ");
426 switch (modeind) {
427 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
428 default: s.append("unknown/reserved"); break;
429 }
430 s.append(", ");
431 }
432
433 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
434 if (srcind) {
435 s.append("source indication: ");
436 switch (srcind) {
437 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
438 default: s.append("unknown/reserved"); break;
439 }
440 s.append(", ");
441 }
442
443 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
444 s.append("offloadable, ");
445 }
446
447 int len = s.length();
448 if (s.length() > 2) {
449 (void) s.lockBuffer(len);
450 s.unlockBuffer(len - 2);
451 }
452 return s;
453}
454
455void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
456{
457 String8 result;
458
459 result.appendFormat("\tEffect ID %d:\n", mId);
460
461 bool locked = AudioFlinger::dumpTryLock(mLock);
462 // failed to lock - AudioFlinger is probably deadlocked
463 if (!locked) {
464 result.append("\t\tCould not lock Fx mutex:\n");
465 }
466
467 result.append("\t\tSession State Registered Enabled Suspended:\n");
468 result.appendFormat("\t\t%05d %03d %s %s %s\n",
469 mSessionId, mState, mPolicyRegistered ? "y" : "n",
470 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
471
472 result.append("\t\tDescriptor:\n");
473 char uuidStr[64];
474 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
475 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
476 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
477 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
478 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
479 mDescriptor.apiVersion,
480 mDescriptor.flags,
481 effectFlagsToString(mDescriptor.flags).string());
482 result.appendFormat("\t\t- name: %s\n",
483 mDescriptor.name);
484
485 result.appendFormat("\t\t- implementor: %s\n",
486 mDescriptor.implementor);
487
488 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
489 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
490 char buffer[256];
491 for (size_t i = 0; i < mHandles.size(); ++i) {
492 EffectHandle *handle = mHandles[i];
493 if (handle != NULL && !handle->disconnected()) {
494 handle->dumpToBuffer(buffer, sizeof(buffer));
495 result.append(buffer);
496 }
497 }
498 if (locked) {
499 mLock.unlock();
500 }
501
502 write(fd, result.string(), result.length());
503}
504
505// ----------------------------------------------------------------------------
506// EffectModule implementation
507// ----------------------------------------------------------------------------
508
509#undef LOG_TAG
510#define LOG_TAG "AudioFlinger::EffectModule"
511
512AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
513 effect_descriptor_t *desc,
514 int id,
515 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800516 bool pinned,
517 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800518 : EffectBase(callback, desc, id, sessionId, pinned),
519 // clear mConfig to ensure consistent initial value of buffer framecount
520 // in case buffers are associated by setInBuffer() or setOutBuffer()
521 // prior to configure().
522 mConfig{{}, {}},
523 mStatus(NO_INIT),
524 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
525 mDisableWaitCnt(0), // set by process() and updateState()
526 mOffloaded(false)
527#ifdef FLOAT_EFFECT_CHAIN
528 , mSupportsFloat(false)
529#endif
530{
531 ALOGV("Constructor %p pinned %d", this, pinned);
532 int lStatus;
533
534 // create effect engine from effect factory
535 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800536 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800537 if (mStatus != NO_ERROR) {
538 return;
539 }
540 lStatus = init();
541 if (lStatus < 0) {
542 mStatus = lStatus;
543 goto Error;
544 }
545
546 setOffloaded(callback->isOffload(), callback->io());
547 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
548
549 return;
550Error:
551 mEffectInterface.clear();
552 ALOGV("Constructor Error %d", mStatus);
553}
554
555AudioFlinger::EffectModule::~EffectModule()
556{
557 ALOGV("Destructor %p", this);
558 if (mEffectInterface != 0) {
559 char uuidStr[64];
560 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
561 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
562 this, uuidStr);
563 release_l();
564 }
565
566}
567
568ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
569{
570 ssize_t status = EffectBase::removeHandle_l(handle);
571
572 // Prevent calls to process() and other functions on effect interface from now on.
573 // The effect engine will be released by the destructor when the last strong reference on
574 // this object is released which can happen after next process is called.
575 if (status == 0 && !mPinned) {
576 mEffectInterface->close();
577 }
578
579 return status;
580}
581
Eric Laurentfa1e1232016-08-02 19:01:49 -0700582bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800583 Mutex::Autolock _l(mLock);
584
Eric Laurentfa1e1232016-08-02 19:01:49 -0700585 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800586 switch (mState) {
587 case RESTART:
588 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700589 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800590
591 case STARTING:
592 // clear auxiliary effect input buffer for next accumulation
593 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
594 memset(mConfig.inputCfg.buffer.raw,
595 0,
596 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
597 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700598 if (start_l() == NO_ERROR) {
599 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700600 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700601 } else {
602 mState = IDLE;
603 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800604 break;
605 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900606 // volume control for offload and direct threads must take effect immediately.
607 if (stop_l() == NO_ERROR
608 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700609 mDisableWaitCnt = mMaxDisableWaitCnt;
610 } else {
611 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
612 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800613 mState = STOPPED;
614 break;
615 case STOPPED:
616 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
617 // turn off sequence.
618 if (--mDisableWaitCnt == 0) {
619 reset_l();
620 mState = IDLE;
621 }
622 break;
623 default: //IDLE , ACTIVE, DESTROYED
624 break;
625 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700626
627 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800628}
629
630void AudioFlinger::EffectModule::process()
631{
632 Mutex::Autolock _l(mLock);
633
Mikhail Naganov022b9952017-01-04 16:36:51 -0800634 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800635 return;
636 }
637
rago94a1ee82017-07-21 15:11:02 -0700638 const uint32_t inChannelCount =
639 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
640 const uint32_t outChannelCount =
641 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
642 const bool auxType =
643 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
644
Andy Hungfa69ca32017-11-30 10:07:53 -0800645 // safeInputOutputSampleCount is 0 if the channel count between input and output
646 // buffers do not match. This prevents automatic accumulation or copying between the
647 // input and output effect buffers without an intermediary effect process.
648 // TODO: consider implementing channel conversion.
649 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700650 mInChannelCountRequested != mOutChannelCountRequested ? 0
651 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800652 mConfig.inputCfg.buffer.frameCount,
653 mConfig.outputCfg.buffer.frameCount);
654 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
655#ifdef FLOAT_EFFECT_CHAIN
656 accumulate_float(
657 mConfig.outputCfg.buffer.f32,
658 mConfig.inputCfg.buffer.f32,
659 safeInputOutputSampleCount);
660#else
661 accumulate_i16(
662 mConfig.outputCfg.buffer.s16,
663 mConfig.inputCfg.buffer.s16,
664 safeInputOutputSampleCount);
665#endif
666 };
667 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
668#ifdef FLOAT_EFFECT_CHAIN
669 memcpy(
670 mConfig.outputCfg.buffer.f32,
671 mConfig.inputCfg.buffer.f32,
672 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
673
674#else
675 memcpy(
676 mConfig.outputCfg.buffer.s16,
677 mConfig.inputCfg.buffer.s16,
678 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
679#endif
680 };
681
Eric Laurentca7cc822012-11-19 14:55:58 -0800682 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700683 int ret;
684 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700685 if (auxType) {
686 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800687 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700688#ifdef FLOAT_EFFECT_CHAIN
689 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800690#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700691 // Do in-place float conversion for auxiliary effect input buffer.
692 static_assert(sizeof(float) <= sizeof(int32_t),
693 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
694
Andy Hungfa69ca32017-11-30 10:07:53 -0800695 memcpy_to_float_from_q4_27(
696 mConfig.inputCfg.buffer.f32,
697 mConfig.inputCfg.buffer.s32,
698 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800699#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800700 } else
Andy Hung116a4982017-11-30 10:15:08 -0800701#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800702 {
Andy Hung116a4982017-11-30 10:15:08 -0800703#ifdef FLOAT_AUX
704 memcpy_to_i16_from_float(
705 mConfig.inputCfg.buffer.s16,
706 mConfig.inputCfg.buffer.f32,
707 mConfig.inputCfg.buffer.frameCount);
708#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800709 memcpy_to_i16_from_q4_27(
710 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700711 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800712 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800713#endif
rago94a1ee82017-07-21 15:11:02 -0700714 }
rago94a1ee82017-07-21 15:11:02 -0700715 }
716#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800717 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
718 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
719
720 if (!auxType && mInChannelCountRequested != inChannelCount) {
721 adjust_channels(
722 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
723 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
724 sizeof(float),
725 sizeof(float)
726 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
727 inBuffer = mInConversionBuffer;
728 }
729 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
730 && mOutChannelCountRequested != outChannelCount) {
731 adjust_selected_channels(
732 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
733 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
734 sizeof(float),
735 sizeof(float)
736 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
737 outBuffer = mOutConversionBuffer;
738 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800739 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
740 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800741 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
743 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700744 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800745 memcpy_to_i16_from_float(
746 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800747 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800749 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700750 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800752 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800753 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
754 goto data_bypass;
755 }
756 memcpy_to_i16_from_float(
757 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800758 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800759 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800760 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700761 }
762 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800763#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800764 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800765#ifdef FLOAT_EFFECT_CHAIN
766 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800767 sp<EffectBufferHalInterface> target =
768 mOutChannelCountRequested != outChannelCount
769 ? mOutConversionBuffer : mOutBuffer;
770
Andy Hungfa69ca32017-11-30 10:07:53 -0800771 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800772 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800773 mOutConversionBuffer->audioBuffer()->s16,
774 outChannelCount * mConfig.outputCfg.buffer.frameCount);
775 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800776 if (mOutChannelCountRequested != outChannelCount) {
777 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
778 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
779 sizeof(float),
780 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
781 }
rago94a1ee82017-07-21 15:11:02 -0700782#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700783 } else {
rago94a1ee82017-07-21 15:11:02 -0700784#ifdef FLOAT_EFFECT_CHAIN
785 data_bypass:
786#endif
787 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700789 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800792 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700793 }
794 }
795 ret = -ENODATA;
796 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800797
Eric Laurentca7cc822012-11-19 14:55:58 -0800798 // force transition to IDLE state when engine is ready
799 if (mState == STOPPED && ret == -ENODATA) {
800 mDisableWaitCnt = 1;
801 }
802
803 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700804 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800805#ifdef FLOAT_AUX
806 const size_t size =
807 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
808#else
rago94a1ee82017-07-21 15:11:02 -0700809 const size_t size =
810 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800811#endif
rago94a1ee82017-07-21 15:11:02 -0700812 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800813 }
814 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700815 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800816 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
817 // If an insert effect is idle and input buffer is different from output buffer,
818 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800819 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700820 // similar handling with data_bypass above.
821 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
822 accumulateInputToOutput();
823 } else { // EFFECT_BUFFER_ACCESS_WRITE
824 copyInputToOutput();
825 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 }
827 }
828}
829
830void AudioFlinger::EffectModule::reset_l()
831{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700832 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800833 return;
834 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700835 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800836}
837
838status_t AudioFlinger::EffectModule::configure()
839{
rago94a1ee82017-07-21 15:11:02 -0700840 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700842 uint32_t size;
843 audio_channel_mask_t channelMask;
844
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700845 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700846 status = NO_INIT;
847 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 }
849
Eric Laurentca7cc822012-11-19 14:55:58 -0800850 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800851 // TODO: handle configuration of input (record) SW effects above the HAL,
852 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
853 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800854 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800855 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700856 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800857
858 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800859 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
860 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
861 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
862 mConfig.inputCfg.channels);
863 }
864#ifndef MULTICHANNEL_EFFECT_CHAIN
865 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
866 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
867 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
868 mConfig.outputCfg.channels);
869 }
870#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800871 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800872#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700873 // TODO: Update this logic when multichannel effects are implemented.
874 // For offloaded tracks consider mono output as stereo for proper effect initialization
875 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
876 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
877 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
878 ALOGV("Overriding effect input and output as STEREO");
879 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800880#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800881 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800882 mInChannelCountRequested =
883 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
884 mOutChannelCountRequested =
885 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700886
rago94a1ee82017-07-21 15:11:02 -0700887 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
888 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900889
890 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100891 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900892 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
893 ALOGV("Overriding effect input as 48kHz");
894 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800895 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900896 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800897 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
898 mConfig.inputCfg.bufferProvider.cookie = NULL;
899 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
900 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
901 mConfig.outputCfg.bufferProvider.cookie = NULL;
902 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
903 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
904 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
905 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800906 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800907 // always overwrites output buffer: input buffer == output buffer
908 // - in other sessions:
909 // last effect in the chain accumulates in output buffer: input buffer != output buffer
910 // other effect: overwrites output buffer: input buffer == output buffer
911 // Auxiliary effect:
912 // accumulates in output buffer: input buffer != output buffer
913 // Therefore: accumulate <=> input buffer != output buffer
914 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
915 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
916 } else {
917 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
918 }
919 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
920 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800921 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800922 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
923
Eric Laurent6b446ce2019-12-13 10:56:31 -0800924 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800925 this, mCallback->chain().promote().get(),
926 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800927
928 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700929 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700930 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700932 &mConfig,
933 &size,
934 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700935 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800936 status = cmdStatus;
937 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800938
939#ifdef MULTICHANNEL_EFFECT_CHAIN
940 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800941 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800942 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
943 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
944 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700945 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
946 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800947 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
948 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
949 }
950 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
951 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
952 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
953 }
954 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700955 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800956 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700957 &mConfig,
958 &size,
959 &cmdStatus);
960 if (status == NO_ERROR) {
961 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800962 }
963 }
964#endif
965
966#ifdef FLOAT_EFFECT_CHAIN
967 if (status == NO_ERROR) {
968 mSupportsFloat = true;
969 }
970
971 if (status != NO_ERROR) {
972 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
973 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
974 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
975 size = sizeof(int);
976 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
977 sizeof(mConfig),
978 &mConfig,
979 &size,
980 &cmdStatus);
981 if (status == NO_ERROR) {
982 status = cmdStatus;
983 }
984 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700985 mSupportsFloat = false;
986 ALOGVV("config worked with 16 bit");
987 } else {
988 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 }
rago94a1ee82017-07-21 15:11:02 -0700990 }
991#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800992
rago94a1ee82017-07-21 15:11:02 -0700993 if (status == NO_ERROR) {
994 // Establish Buffer strategy
995 setInBuffer(mInBuffer);
996 setOutBuffer(mOutBuffer);
997
998 // Update visualizer latency
999 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1000 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1001 effect_param_t *p = (effect_param_t *)buf32;
1002
1003 p->psize = sizeof(uint32_t);
1004 p->vsize = sizeof(uint32_t);
1005 size = sizeof(int);
1006 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1007
Eric Laurent6b446ce2019-12-13 10:56:31 -08001008 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001009
1010 *((int32_t *)p->data + 1)= latency;
1011 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1012 sizeof(effect_param_t) + 8,
1013 &buf32,
1014 &size,
1015 &cmdStatus);
1016 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 }
1018
Andy Hung05083ac2017-12-14 15:00:28 -08001019 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1020 mMaxDisableWaitCnt = (uint32_t)std::max(
1021 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1022 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1023 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001024
Eric Laurentd0ebb532013-04-02 16:41:41 -07001025exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001026 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001027 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001028 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001029 return status;
1030}
1031
1032status_t AudioFlinger::EffectModule::init()
1033{
1034 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001035 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 return NO_INIT;
1037 }
1038 status_t cmdStatus;
1039 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001040 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1041 0,
1042 NULL,
1043 &size,
1044 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 if (status == 0) {
1046 status = cmdStatus;
1047 }
1048 return status;
1049}
1050
Eric Laurent1b928682014-10-02 19:41:47 -07001051void AudioFlinger::EffectModule::addEffectToHal_l()
1052{
1053 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1054 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001055 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001056 }
1057}
1058
Eric Laurentfa1e1232016-08-02 19:01:49 -07001059// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001060status_t AudioFlinger::EffectModule::start()
1061{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001062 status_t status;
1063 {
1064 Mutex::Autolock _l(mLock);
1065 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001066 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001067 if (status == NO_ERROR) {
1068 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001069 }
1070 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001071}
1072
1073status_t AudioFlinger::EffectModule::start_l()
1074{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001075 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 return NO_INIT;
1077 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001078 if (mStatus != NO_ERROR) {
1079 return mStatus;
1080 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 status_t cmdStatus;
1082 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001083 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1084 0,
1085 NULL,
1086 &size,
1087 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001088 if (status == 0) {
1089 status = cmdStatus;
1090 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001091 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001092 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 }
1094 return status;
1095}
1096
1097status_t AudioFlinger::EffectModule::stop()
1098{
1099 Mutex::Autolock _l(mLock);
1100 return stop_l();
1101}
1102
1103status_t AudioFlinger::EffectModule::stop_l()
1104{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001105 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 return NO_INIT;
1107 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001108 if (mStatus != NO_ERROR) {
1109 return mStatus;
1110 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001111 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001113
1114 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001115 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1116 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1117 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001118 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001119 mSetVolumeReentrantTid = INVALID_PID;
1120 }
1121
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001122 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1123 0,
1124 NULL,
1125 &size,
1126 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001127 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 status = cmdStatus;
1129 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001130 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001131 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001132 }
1133 return status;
1134}
1135
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001136// must be called with EffectChain::mLock held
1137void AudioFlinger::EffectModule::release_l()
1138{
1139 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001140 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001141 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001142 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001143 mEffectInterface.clear();
1144 }
1145}
1146
Eric Laurent6b446ce2019-12-13 10:56:31 -08001147status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001148{
1149 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1150 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001151 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001153 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001154}
1155
Andy Hunge4a1d912016-08-17 14:11:13 -07001156// round up delta valid if value and divisor are positive.
1157template <typename T>
1158static T roundUpDelta(const T &value, const T &divisor) {
1159 T remainder = value % divisor;
1160 return remainder == 0 ? 0 : divisor - remainder;
1161}
1162
Eric Laurentca7cc822012-11-19 14:55:58 -08001163status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1164 uint32_t cmdSize,
1165 void *pCmdData,
1166 uint32_t *replySize,
1167 void *pReplyData)
1168{
1169 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001170 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001171
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001172 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001173 return NO_INIT;
1174 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001175 if (mStatus != NO_ERROR) {
1176 return mStatus;
1177 }
Andy Hung110bc952016-06-20 15:22:52 -07001178 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001179 (sizeof(effect_param_t) > cmdSize ||
1180 ((effect_param_t *)pCmdData)->psize > cmdSize
1181 - sizeof(effect_param_t))) {
1182 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001183 android_errorWriteLog(0x534e4554, "33003822");
1184 return -EINVAL;
1185 }
1186 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1187 (*replySize < sizeof(effect_param_t) ||
1188 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1189 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001190 return -EINVAL;
1191 }
ragoe2759072016-11-22 18:02:48 -08001192 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1193 (sizeof(effect_param_t) > *replySize
1194 || ((effect_param_t *)pCmdData)->psize > *replySize
1195 - sizeof(effect_param_t)
1196 || ((effect_param_t *)pCmdData)->vsize > *replySize
1197 - sizeof(effect_param_t)
1198 - ((effect_param_t *)pCmdData)->psize
1199 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1200 *replySize
1201 - sizeof(effect_param_t)
1202 - ((effect_param_t *)pCmdData)->psize
1203 - ((effect_param_t *)pCmdData)->vsize)) {
1204 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1205 android_errorWriteLog(0x534e4554, "32705438");
1206 return -EINVAL;
1207 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001208 if ((cmdCode == EFFECT_CMD_SET_PARAM
1209 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1210 (sizeof(effect_param_t) > cmdSize
1211 || ((effect_param_t *)pCmdData)->psize > cmdSize
1212 - sizeof(effect_param_t)
1213 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1214 - sizeof(effect_param_t)
1215 - ((effect_param_t *)pCmdData)->psize
1216 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1217 cmdSize
1218 - sizeof(effect_param_t)
1219 - ((effect_param_t *)pCmdData)->psize
1220 - ((effect_param_t *)pCmdData)->vsize)) {
1221 android_errorWriteLog(0x534e4554, "30204301");
1222 return -EINVAL;
1223 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001224 status_t status = mEffectInterface->command(cmdCode,
1225 cmdSize,
1226 pCmdData,
1227 replySize,
1228 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001229 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1230 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1231 for (size_t i = 1; i < mHandles.size(); i++) {
1232 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001233 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001234 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1235 }
1236 }
1237 }
1238 return status;
1239}
1240
Eric Laurentca7cc822012-11-19 14:55:58 -08001241bool AudioFlinger::EffectModule::isProcessEnabled() const
1242{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001243 if (mStatus != NO_ERROR) {
1244 return false;
1245 }
1246
Eric Laurentca7cc822012-11-19 14:55:58 -08001247 switch (mState) {
1248 case RESTART:
1249 case ACTIVE:
1250 case STOPPING:
1251 case STOPPED:
1252 return true;
1253 case IDLE:
1254 case STARTING:
1255 case DESTROYED:
1256 default:
1257 return false;
1258 }
1259}
1260
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001261bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1262{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001263 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001264}
1265
1266bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1267{
1268 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1269}
1270
Mikhail Naganov022b9952017-01-04 16:36:51 -08001271void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001272 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001273
1274 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001275 if (buffer != 0) {
1276 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1277 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1278 } else {
1279 mConfig.inputCfg.buffer.raw = NULL;
1280 }
1281 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001282 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001283
1284#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001285 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001286 // Theoretically insert effects can also do in-place conversions (destroying
1287 // the original buffer) when the output buffer is identical to the input buffer,
1288 // but we don't optimize for it here.
1289 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001290 const uint32_t inChannelCount =
1291 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1292 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001293 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001294 // we need to translate - create hidl shared buffer and intercept
1295 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001296 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1297 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1298 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001299
1300 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1301 __func__, inChannels, inFrameCount, size);
1302
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001303 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001304 || size > mInConversionBuffer->getSize())) {
1305 mInConversionBuffer.clear();
1306 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001307 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001308 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001309 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001310 mInConversionBuffer->setFrameCount(inFrameCount);
1311 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001312 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001313 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001314 }
1315 }
1316#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001317}
1318
1319void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001320 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001321
1322 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001323 if (buffer != 0) {
1324 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1325 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1326 } else {
1327 mConfig.outputCfg.buffer.raw = NULL;
1328 }
1329 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001330 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001331
1332#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001333 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001334 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001335 const uint32_t outChannelCount =
1336 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1337 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001338 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001339 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001340 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1341 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1342 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001343
1344 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1345 __func__, outChannels, outFrameCount, size);
1346
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001347 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001348 || size > mOutConversionBuffer->getSize())) {
1349 mOutConversionBuffer.clear();
1350 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001351 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001352 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001353 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001354 mOutConversionBuffer->setFrameCount(outFrameCount);
1355 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001356 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001357 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001358 }
1359 }
1360#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001361}
1362
Eric Laurentca7cc822012-11-19 14:55:58 -08001363status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1364{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001365 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001366 if (mStatus != NO_ERROR) {
1367 return mStatus;
1368 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001369 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001370 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1371 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1372 if (isProcessEnabled() &&
1373 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001374 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1375 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 uint32_t volume[2];
1377 uint32_t *pVolume = NULL;
1378 uint32_t size = sizeof(volume);
1379 volume[0] = *left;
1380 volume[1] = *right;
1381 if (controller) {
1382 pVolume = volume;
1383 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001384 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1385 size,
1386 volume,
1387 &size,
1388 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001389 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1390 *left = volume[0];
1391 *right = volume[1];
1392 }
1393 }
1394 return status;
1395}
1396
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001397void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1398{
Zhou Songd505c642020-02-20 16:35:37 +08001399 // for offload or direct thread, if the effect chain has non-offloadable
1400 // effect and any effect module within the chain has volume control, then
1401 // volume control is delegated to effect, otherwise, set volume to hal.
1402 if (mEffectCallback->isOffloadOrDirect() &&
1403 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001404 float vol_l = (float)left / (1 << 24);
1405 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001406 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001407 }
1408}
1409
jiabin8f278ee2019-11-11 12:16:27 -08001410status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1411 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001412{
jiabin8f278ee2019-11-11 12:16:27 -08001413 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1414 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001415 return NO_ERROR;
1416 }
1417
1418 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001419 if (mStatus != NO_ERROR) {
1420 return mStatus;
1421 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001422 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001423 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001424 status_t cmdStatus;
1425 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001426 // FIXME: use audio device types and addresses when the hal interface is ready.
1427 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001428 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001429 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001430 &size,
1431 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 }
1433 return status;
1434}
1435
jiabin8f278ee2019-11-11 12:16:27 -08001436status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1437{
1438 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1439}
1440
1441status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1442{
1443 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1444}
1445
Eric Laurentca7cc822012-11-19 14:55:58 -08001446status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1447{
1448 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001449 if (mStatus != NO_ERROR) {
1450 return mStatus;
1451 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001452 status_t status = NO_ERROR;
1453 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1454 status_t cmdStatus;
1455 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001456 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1457 sizeof(audio_mode_t),
1458 &mode,
1459 &size,
1460 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001461 if (status == NO_ERROR) {
1462 status = cmdStatus;
1463 }
1464 }
1465 return status;
1466}
1467
1468status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
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;
1475 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1476 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001477 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1478 sizeof(audio_source_t),
1479 &source,
1480 &size,
1481 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 }
1483 return status;
1484}
1485
Eric Laurent5baf2af2013-09-12 17:37:00 -07001486status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1487{
1488 Mutex::Autolock _l(mLock);
1489 if (mStatus != NO_ERROR) {
1490 return mStatus;
1491 }
1492 status_t status = NO_ERROR;
1493 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1494 status_t cmdStatus;
1495 uint32_t size = sizeof(status_t);
1496 effect_offload_param_t cmd;
1497
1498 cmd.isOffload = offloaded;
1499 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001500 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1501 sizeof(effect_offload_param_t),
1502 &cmd,
1503 &size,
1504 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001505 if (status == NO_ERROR) {
1506 status = cmdStatus;
1507 }
1508 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1509 } else {
1510 if (offloaded) {
1511 status = INVALID_OPERATION;
1512 }
1513 mOffloaded = false;
1514 }
1515 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1516 return status;
1517}
1518
1519bool AudioFlinger::EffectModule::isOffloaded() const
1520{
1521 Mutex::Autolock _l(mLock);
1522 return mOffloaded;
1523}
1524
Andy Hungbded9c82017-11-30 18:47:35 -08001525static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1526 std::stringstream ss;
1527
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001528 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001529 return "nullptr"; // make different than below
1530 } else if (buffer->externalData() != nullptr) {
1531 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1532 << " -> "
1533 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1534 } else {
1535 ss << buffer->audioBuffer()->raw;
1536 }
1537 return ss.str();
1538}
Marco Nelissenb2208842014-02-07 14:00:50 -08001539
Eric Laurent41709552019-12-16 19:34:05 -08001540void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001541{
Eric Laurent41709552019-12-16 19:34:05 -08001542 EffectBase::dump(fd, args);
1543
Eric Laurentca7cc822012-11-19 14:55:58 -08001544 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001545 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001546
Eric Laurent41709552019-12-16 19:34:05 -08001547 result.append("\t\tStatus Engine:\n");
1548 result.appendFormat("\t\t%03d %p\n",
1549 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001550
1551 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001552
1553 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001554 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1555 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1556 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001557 mConfig.inputCfg.buffer.frameCount,
1558 mConfig.inputCfg.samplingRate,
1559 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001560 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001561 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001562
1563 result.append("\t\t- Output configuration:\n");
1564 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001565 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001566 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001567 mConfig.outputCfg.buffer.frameCount,
1568 mConfig.outputCfg.samplingRate,
1569 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001570 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001571 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001572
rago94a1ee82017-07-21 15:11:02 -07001573#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001574
Andy Hungbded9c82017-11-30 18:47:35 -08001575 result.appendFormat("\t\t- HAL buffers:\n"
1576 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1577 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1578 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1579 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1580 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001581#endif
1582
Eric Laurentca7cc822012-11-19 14:55:58 -08001583 write(fd, result.string(), result.length());
1584
Mikhail Naganov4d547672019-02-22 14:19:19 -08001585 if (mEffectInterface != 0) {
1586 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1587 (void)mEffectInterface->dump(fd);
1588 }
1589
Eric Laurentca7cc822012-11-19 14:55:58 -08001590 if (locked) {
1591 mLock.unlock();
1592 }
1593}
1594
1595// ----------------------------------------------------------------------------
1596// EffectHandle implementation
1597// ----------------------------------------------------------------------------
1598
1599#undef LOG_TAG
1600#define LOG_TAG "AudioFlinger::EffectHandle"
1601
Eric Laurent41709552019-12-16 19:34:05 -08001602AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001603 const sp<AudioFlinger::Client>& client,
1604 const sp<IEffectClient>& effectClient,
1605 int32_t priority)
1606 : BnEffect(),
1607 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001608 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001609{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001610 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001611
1612 if (client == 0) {
1613 return;
1614 }
1615 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1616 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001617 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001618 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001619 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001621 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001622 return;
1623 }
Glenn Kastene75da402013-11-20 13:54:52 -08001624 new(mCblk) effect_param_cblk_t();
1625 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001626}
1627
1628AudioFlinger::EffectHandle::~EffectHandle()
1629{
1630 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 disconnect(false);
1632}
1633
Glenn Kastene75da402013-11-20 13:54:52 -08001634status_t AudioFlinger::EffectHandle::initCheck()
1635{
1636 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1637}
1638
Eric Laurentca7cc822012-11-19 14:55:58 -08001639status_t AudioFlinger::EffectHandle::enable()
1640{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001641 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001643 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001644 if (effect == 0 || mDisconnected) {
1645 return DEAD_OBJECT;
1646 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001647 if (!mHasControl) {
1648 return INVALID_OPERATION;
1649 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001650
1651 if (mEnabled) {
1652 return NO_ERROR;
1653 }
1654
1655 mEnabled = true;
1656
Eric Laurent6c796322019-04-09 14:13:17 -07001657 status_t status = effect->updatePolicyState();
1658 if (status != NO_ERROR) {
1659 mEnabled = false;
1660 return status;
1661 }
1662
Eric Laurent6b446ce2019-12-13 10:56:31 -08001663 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001664
1665 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001666 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 return NO_ERROR;
1668 }
1669
Eric Laurent6b446ce2019-12-13 10:56:31 -08001670 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001672 mEnabled = false;
1673 }
1674 return status;
1675}
1676
1677status_t AudioFlinger::EffectHandle::disable()
1678{
1679 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001680 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001681 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001682 if (effect == 0 || mDisconnected) {
1683 return DEAD_OBJECT;
1684 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001685 if (!mHasControl) {
1686 return INVALID_OPERATION;
1687 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001688
1689 if (!mEnabled) {
1690 return NO_ERROR;
1691 }
1692 mEnabled = false;
1693
Eric Laurent6c796322019-04-09 14:13:17 -07001694 effect->updatePolicyState();
1695
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001696 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 return NO_ERROR;
1698 }
1699
Eric Laurent6b446ce2019-12-13 10:56:31 -08001700 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001701 return status;
1702}
1703
1704void AudioFlinger::EffectHandle::disconnect()
1705{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001706 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001707 disconnect(true);
1708}
1709
1710void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1711{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001712 AutoMutex _l(mLock);
1713 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1714 if (mDisconnected) {
1715 if (unpinIfLast) {
1716 android_errorWriteLog(0x534e4554, "32707507");
1717 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001718 return;
1719 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001720 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001721 {
Eric Laurent41709552019-12-16 19:34:05 -08001722 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001723 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001724 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001725 ALOGW("%s Effect handle %p disconnected after thread destruction",
1726 __func__, this);
1727 }
1728 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001729 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001730 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001731
Eric Laurentca7cc822012-11-19 14:55:58 -08001732 if (mClient != 0) {
1733 if (mCblk != NULL) {
1734 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1735 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1736 }
1737 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001738 // Client destructor must run with AudioFlinger client mutex locked
1739 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001740 mClient.clear();
1741 }
1742}
1743
1744status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1745 uint32_t cmdSize,
1746 void *pCmdData,
1747 uint32_t *replySize,
1748 void *pReplyData)
1749{
1750 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001751 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001752
Eric Laurentc7ab3092017-06-15 18:43:46 -07001753 // reject commands reserved for internal use by audio framework if coming from outside
1754 // of audioserver
1755 switch(cmdCode) {
1756 case EFFECT_CMD_ENABLE:
1757 case EFFECT_CMD_DISABLE:
1758 case EFFECT_CMD_SET_PARAM:
1759 case EFFECT_CMD_SET_PARAM_DEFERRED:
1760 case EFFECT_CMD_SET_PARAM_COMMIT:
1761 case EFFECT_CMD_GET_PARAM:
1762 break;
1763 default:
1764 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1765 break;
1766 }
1767 android_errorWriteLog(0x534e4554, "62019992");
1768 return BAD_VALUE;
1769 }
1770
Eric Laurent1ffc5852016-12-15 14:46:09 -08001771 if (cmdCode == EFFECT_CMD_ENABLE) {
1772 if (*replySize < sizeof(int)) {
1773 android_errorWriteLog(0x534e4554, "32095713");
1774 return BAD_VALUE;
1775 }
1776 *(int *)pReplyData = NO_ERROR;
1777 *replySize = sizeof(int);
1778 return enable();
1779 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1780 if (*replySize < sizeof(int)) {
1781 android_errorWriteLog(0x534e4554, "32095713");
1782 return BAD_VALUE;
1783 }
1784 *(int *)pReplyData = NO_ERROR;
1785 *replySize = sizeof(int);
1786 return disable();
1787 }
1788
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001789 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001790 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001791 if (effect == 0 || mDisconnected) {
1792 return DEAD_OBJECT;
1793 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001794 // only get parameter command is permitted for applications not controlling the effect
1795 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1796 return INVALID_OPERATION;
1797 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001798
1799 // handle commands that are not forwarded transparently to effect engine
1800 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001801 if (mClient == 0) {
1802 return INVALID_OPERATION;
1803 }
1804
Eric Laurent1ffc5852016-12-15 14:46:09 -08001805 if (*replySize < sizeof(int)) {
1806 android_errorWriteLog(0x534e4554, "32095713");
1807 return BAD_VALUE;
1808 }
1809 *(int *)pReplyData = NO_ERROR;
1810 *replySize = sizeof(int);
1811
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 // No need to trylock() here as this function is executed in the binder thread serving a
1813 // particular client process: no risk to block the whole media server process or mixer
1814 // threads if we are stuck here
1815 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001816 // keep local copy of index in case of client corruption b/32220769
1817 const uint32_t clientIndex = mCblk->clientIndex;
1818 const uint32_t serverIndex = mCblk->serverIndex;
1819 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1820 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001821 mCblk->serverIndex = 0;
1822 mCblk->clientIndex = 0;
1823 return BAD_VALUE;
1824 }
1825 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001826 effect_param_t *param = NULL;
1827 for (uint32_t index = serverIndex; index < clientIndex;) {
1828 int *p = (int *)(mBuffer + index);
1829 const int size = *p++;
1830 if (size < 0
1831 || size > EFFECT_PARAM_BUFFER_SIZE
1832 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001833 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001834 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001835 break;
1836 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001837
1838 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001839 auto *newParam = (effect_param_t *)realloc(param, size);
1840 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001841 ALOGW("command(): out of memory");
1842 status = NO_MEMORY;
1843 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001844 }
George Burgess IV80a22162020-01-05 20:06:15 -08001845 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001846 memcpy(param, p, size);
1847
1848 int reply = 0;
1849 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001850 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001851 size,
1852 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 &rsize,
1854 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001855
1856 // verify shared memory: server index shouldn't change; client index can't go back.
1857 if (serverIndex != mCblk->serverIndex
1858 || clientIndex > mCblk->clientIndex) {
1859 android_errorWriteLog(0x534e4554, "32220769");
1860 status = BAD_VALUE;
1861 break;
1862 }
1863
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 // stop at first error encountered
1865 if (ret != NO_ERROR) {
1866 status = ret;
1867 *(int *)pReplyData = reply;
1868 break;
1869 } else if (reply != NO_ERROR) {
1870 *(int *)pReplyData = reply;
1871 break;
1872 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001873 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001875 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001876 mCblk->serverIndex = 0;
1877 mCblk->clientIndex = 0;
1878 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001879 }
1880
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001881 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001882}
1883
1884void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1885{
1886 ALOGV("setControl %p control %d", this, hasControl);
1887
1888 mHasControl = hasControl;
1889 mEnabled = enabled;
1890
1891 if (signal && mEffectClient != 0) {
1892 mEffectClient->controlStatusChanged(hasControl);
1893 }
1894}
1895
1896void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1897 uint32_t cmdSize,
1898 void *pCmdData,
1899 uint32_t replySize,
1900 void *pReplyData)
1901{
1902 if (mEffectClient != 0) {
1903 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1904 }
1905}
1906
1907
1908
1909void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1910{
1911 if (mEffectClient != 0) {
1912 mEffectClient->enableStatusChanged(enabled);
1913 }
1914}
1915
1916status_t AudioFlinger::EffectHandle::onTransact(
1917 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1918{
1919 return BnEffect::onTransact(code, data, reply, flags);
1920}
1921
1922
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001923void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001924{
1925 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1926
Marco Nelissenb2208842014-02-07 14:00:50 -08001927 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001928 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001929 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001930 mHasControl ? "yes" : "no",
1931 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001932 mCblk ? mCblk->clientIndex : 0,
1933 mCblk ? mCblk->serverIndex : 0
1934 );
1935
1936 if (locked) {
1937 mCblk->lock.unlock();
1938 }
1939}
1940
1941#undef LOG_TAG
1942#define LOG_TAG "AudioFlinger::EffectChain"
1943
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001944AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1945 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001946 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001947 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001948 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001949 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08001950{
1951 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001952 sp<ThreadBase> p = thread.promote();
1953 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001954 return;
1955 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001956 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
1957 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08001958}
1959
1960AudioFlinger::EffectChain::~EffectChain()
1961{
Eric Laurentca7cc822012-11-19 14:55:58 -08001962}
1963
1964// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1965sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1966 effect_descriptor_t *descriptor)
1967{
1968 size_t size = mEffects.size();
1969
1970 for (size_t i = 0; i < size; i++) {
1971 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1972 return mEffects[i];
1973 }
1974 }
1975 return 0;
1976}
1977
1978// getEffectFromId_l() must be called with ThreadBase::mLock held
1979sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1980{
1981 size_t size = mEffects.size();
1982
1983 for (size_t i = 0; i < size; i++) {
1984 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1985 if (id == 0 || mEffects[i]->id() == id) {
1986 return mEffects[i];
1987 }
1988 }
1989 return 0;
1990}
1991
1992// getEffectFromType_l() must be called with ThreadBase::mLock held
1993sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1994 const effect_uuid_t *type)
1995{
1996 size_t size = mEffects.size();
1997
1998 for (size_t i = 0; i < size; i++) {
1999 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2000 return mEffects[i];
2001 }
2002 }
2003 return 0;
2004}
2005
Eric Laurent6c796322019-04-09 14:13:17 -07002006std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2007{
2008 std::vector<int> ids;
2009 Mutex::Autolock _l(mLock);
2010 for (size_t i = 0; i < mEffects.size(); i++) {
2011 ids.push_back(mEffects[i]->id());
2012 }
2013 return ids;
2014}
2015
Eric Laurentca7cc822012-11-19 14:55:58 -08002016void AudioFlinger::EffectChain::clearInputBuffer()
2017{
2018 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002019 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002020}
2021
2022// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002023void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002024{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002025 if (mInBuffer == NULL) {
2026 return;
2027 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002028 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002029 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002030
Eric Laurent6b446ce2019-12-13 10:56:31 -08002031 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002032 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002033}
2034
2035// Must be called with EffectChain::mLock locked
2036void AudioFlinger::EffectChain::process_l()
2037{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002038 // never process effects when:
2039 // - on an OFFLOAD thread
2040 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002041 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002042 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 bool tracksOnSession = (trackCnt() != 0);
2044
2045 if (!tracksOnSession && mTailBufferCount == 0) {
2046 doProcess = false;
2047 }
2048
2049 if (activeTrackCnt() == 0) {
2050 // if no track is active and the effect tail has not been rendered,
2051 // the input buffer must be cleared here as the mixer process will not do it
2052 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002053 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 if (mTailBufferCount > 0) {
2055 mTailBufferCount--;
2056 }
2057 }
2058 }
2059 }
2060
2061 size_t size = mEffects.size();
2062 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002063 // Only the input and output buffers of the chain can be external,
2064 // and 'update' / 'commit' do nothing for allocated buffers, thus
2065 // it's not needed to consider any other buffers here.
2066 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002067 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2068 mOutBuffer->update();
2069 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002070 for (size_t i = 0; i < size; i++) {
2071 mEffects[i]->process();
2072 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002073 mInBuffer->commit();
2074 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2075 mOutBuffer->commit();
2076 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002077 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002078 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002079 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002080 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2081 }
2082 if (doResetVolume) {
2083 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002084 }
2085}
2086
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002087// createEffect_l() must be called with ThreadBase::mLock held
2088status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002089 effect_descriptor_t *desc,
2090 int id,
2091 audio_session_t sessionId,
2092 bool pinned)
2093{
2094 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002095 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002096 status_t lStatus = effect->status();
2097 if (lStatus == NO_ERROR) {
2098 lStatus = addEffect_ll(effect);
2099 }
2100 if (lStatus != NO_ERROR) {
2101 effect.clear();
2102 }
2103 return lStatus;
2104}
2105
2106// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002107status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2108{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002109 Mutex::Autolock _l(mLock);
2110 return addEffect_ll(effect);
2111}
2112// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2113status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2114{
Eric Laurentca7cc822012-11-19 14:55:58 -08002115 effect_descriptor_t desc = effect->desc();
2116 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2117
Eric Laurent6b446ce2019-12-13 10:56:31 -08002118 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002119
2120 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2121 // Auxiliary effects are inserted at the beginning of mEffects vector as
2122 // they are processed first and accumulated in chain input buffer
2123 mEffects.insertAt(effect, 0);
2124
2125 // the input buffer for auxiliary effect contains mono samples in
2126 // 32 bit format. This is to avoid saturation in AudoMixer
2127 // accumulation stage. Saturation is done in EffectModule::process() before
2128 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002129 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002130 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002131#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002132 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002133 numSamples * sizeof(float), &halBuffer);
2134#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002135 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002136 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002137#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002138 if (result != OK) return result;
2139 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002140 // auxiliary effects output samples to chain input buffer for further processing
2141 // by insert effects
2142 effect->setOutBuffer(mInBuffer);
2143 } else {
2144 // Insert effects are inserted at the end of mEffects vector as they are processed
2145 // after track and auxiliary effects.
2146 // Insert effect order as a function of indicated preference:
2147 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2148 // another effect is present
2149 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2150 // last effect claiming first position
2151 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2152 // first effect claiming last position
2153 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2154 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2155 // already present
2156
2157 size_t size = mEffects.size();
2158 size_t idx_insert = size;
2159 ssize_t idx_insert_first = -1;
2160 ssize_t idx_insert_last = -1;
2161
2162 for (size_t i = 0; i < size; i++) {
2163 effect_descriptor_t d = mEffects[i]->desc();
2164 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2165 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2166 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2167 // check invalid effect chaining combinations
2168 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2169 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2170 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2171 desc.name, d.name);
2172 return INVALID_OPERATION;
2173 }
2174 // remember position of first insert effect and by default
2175 // select this as insert position for new effect
2176 if (idx_insert == size) {
2177 idx_insert = i;
2178 }
2179 // remember position of last insert effect claiming
2180 // first position
2181 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2182 idx_insert_first = i;
2183 }
2184 // remember position of first insert effect claiming
2185 // last position
2186 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2187 idx_insert_last == -1) {
2188 idx_insert_last = i;
2189 }
2190 }
2191 }
2192
2193 // modify idx_insert from first position if needed
2194 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2195 if (idx_insert_last != -1) {
2196 idx_insert = idx_insert_last;
2197 } else {
2198 idx_insert = size;
2199 }
2200 } else {
2201 if (idx_insert_first != -1) {
2202 idx_insert = idx_insert_first + 1;
2203 }
2204 }
2205
2206 // always read samples from chain input buffer
2207 effect->setInBuffer(mInBuffer);
2208
2209 // if last effect in the chain, output samples to chain
2210 // output buffer, otherwise to chain input buffer
2211 if (idx_insert == size) {
2212 if (idx_insert != 0) {
2213 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2214 mEffects[idx_insert-1]->configure();
2215 }
2216 effect->setOutBuffer(mOutBuffer);
2217 } else {
2218 effect->setOutBuffer(mInBuffer);
2219 }
2220 mEffects.insertAt(effect, idx_insert);
2221
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002222 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002223 idx_insert);
2224 }
2225 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002226
Eric Laurentca7cc822012-11-19 14:55:58 -08002227 return NO_ERROR;
2228}
2229
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002230// removeEffect_l() must be called with ThreadBase::mLock held
2231size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2232 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002233{
2234 Mutex::Autolock _l(mLock);
2235 size_t size = mEffects.size();
2236 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2237
2238 for (size_t i = 0; i < size; i++) {
2239 if (effect == mEffects[i]) {
2240 // calling stop here will remove pre-processing effect from the audio HAL.
2241 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2242 // the middle of a read from audio HAL
2243 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2244 mEffects[i]->state() == EffectModule::STOPPING) {
2245 mEffects[i]->stop();
2246 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002247 if (release) {
2248 mEffects[i]->release_l();
2249 }
2250
Mikhail Naganov022b9952017-01-04 16:36:51 -08002251 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002252 if (i == size - 1 && i != 0) {
2253 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2254 mEffects[i - 1]->configure();
2255 }
2256 }
2257 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002258 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002259 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002260
Eric Laurentca7cc822012-11-19 14:55:58 -08002261 break;
2262 }
2263 }
2264
2265 return mEffects.size();
2266}
2267
jiabin8f278ee2019-11-11 12:16:27 -08002268// setDevices_l() must be called with ThreadBase::mLock held
2269void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002270{
2271 size_t size = mEffects.size();
2272 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002273 mEffects[i]->setDevices(devices);
2274 }
2275}
2276
2277// setInputDevice_l() must be called with ThreadBase::mLock held
2278void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2279{
2280 size_t size = mEffects.size();
2281 for (size_t i = 0; i < size; i++) {
2282 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002283 }
2284}
2285
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002286// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002287void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2288{
2289 size_t size = mEffects.size();
2290 for (size_t i = 0; i < size; i++) {
2291 mEffects[i]->setMode(mode);
2292 }
2293}
2294
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002295// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002296void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2297{
2298 size_t size = mEffects.size();
2299 for (size_t i = 0; i < size; i++) {
2300 mEffects[i]->setAudioSource(source);
2301 }
2302}
2303
Zhou Songd505c642020-02-20 16:35:37 +08002304bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2305 for (const auto &effect : mEffects) {
2306 if (effect->isVolumeControlEnabled()) return true;
2307 }
2308 return false;
2309}
2310
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002311// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002312bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002313{
2314 uint32_t newLeft = *left;
2315 uint32_t newRight = *right;
2316 bool hasControl = false;
2317 int ctrlIdx = -1;
2318 size_t size = mEffects.size();
2319
2320 // first update volume controller
2321 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002322 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002323 ctrlIdx = i - 1;
2324 hasControl = true;
2325 break;
2326 }
2327 }
2328
Eric Laurentfa1e1232016-08-02 19:01:49 -07002329 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002330 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002331 if (hasControl) {
2332 *left = mNewLeftVolume;
2333 *right = mNewRightVolume;
2334 }
2335 return hasControl;
2336 }
2337
2338 mVolumeCtrlIdx = ctrlIdx;
2339 mLeftVolume = newLeft;
2340 mRightVolume = newRight;
2341
2342 // second get volume update from volume controller
2343 if (ctrlIdx >= 0) {
2344 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2345 mNewLeftVolume = newLeft;
2346 mNewRightVolume = newRight;
2347 }
2348 // then indicate volume to all other effects in chain.
2349 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002350 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002351 uint32_t lVol = newLeft;
2352 uint32_t rVol = newRight;
2353
2354 for (size_t i = 0; i < size; i++) {
2355 if ((int)i == ctrlIdx) {
2356 continue;
2357 }
2358 // this also works for ctrlIdx == -1 when there is no volume controller
2359 if ((int)i > ctrlIdx) {
2360 lVol = *left;
2361 rVol = *right;
2362 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002363 // Pass requested volume directly if this is volume monitor module
2364 if (mEffects[i]->isVolumeMonitor()) {
2365 mEffects[i]->setVolume(left, right, false);
2366 } else {
2367 mEffects[i]->setVolume(&lVol, &rVol, false);
2368 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002369 }
2370 *left = newLeft;
2371 *right = newRight;
2372
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002373 setVolumeForOutput_l(*left, *right);
2374
Eric Laurentca7cc822012-11-19 14:55:58 -08002375 return hasControl;
2376}
2377
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002378// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002379void AudioFlinger::EffectChain::resetVolume_l()
2380{
Eric Laurente7449bf2016-08-03 18:44:07 -07002381 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2382 uint32_t left = mLeftVolume;
2383 uint32_t right = mRightVolume;
2384 (void)setVolume_l(&left, &right, true);
2385 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002386}
2387
Eric Laurent1b928682014-10-02 19:41:47 -07002388void AudioFlinger::EffectChain::syncHalEffectsState()
2389{
2390 Mutex::Autolock _l(mLock);
2391 for (size_t i = 0; i < mEffects.size(); i++) {
2392 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2393 mEffects[i]->state() == EffectModule::STOPPING) {
2394 mEffects[i]->addEffectToHal_l();
2395 }
2396 }
2397}
2398
Eric Laurentca7cc822012-11-19 14:55:58 -08002399void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2400{
Eric Laurentca7cc822012-11-19 14:55:58 -08002401 String8 result;
2402
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002403 const size_t numEffects = mEffects.size();
2404 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002405
Marco Nelissenb2208842014-02-07 14:00:50 -08002406 if (numEffects) {
2407 bool locked = AudioFlinger::dumpTryLock(mLock);
2408 // failed to lock - AudioFlinger is probably deadlocked
2409 if (!locked) {
2410 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002411 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002412
Andy Hungbded9c82017-11-30 18:47:35 -08002413 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2414 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2415 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2416 (int)inBufferStr.size(), "In buffer ",
2417 (int)outBufferStr.size(), "Out buffer ");
2418 result.appendFormat("\t%s %s %d\n",
2419 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002420 write(fd, result.string(), result.size());
2421
2422 for (size_t i = 0; i < numEffects; ++i) {
2423 sp<EffectModule> effect = mEffects[i];
2424 if (effect != 0) {
2425 effect->dump(fd, args);
2426 }
2427 }
2428
2429 if (locked) {
2430 mLock.unlock();
2431 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002432 } else {
2433 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002434 }
2435}
2436
2437// must be called with ThreadBase::mLock held
2438void AudioFlinger::EffectChain::setEffectSuspended_l(
2439 const effect_uuid_t *type, bool suspend)
2440{
2441 sp<SuspendedEffectDesc> desc;
2442 // use effect type UUID timelow as key as there is no real risk of identical
2443 // timeLow fields among effect type UUIDs.
2444 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2445 if (suspend) {
2446 if (index >= 0) {
2447 desc = mSuspendedEffects.valueAt(index);
2448 } else {
2449 desc = new SuspendedEffectDesc();
2450 desc->mType = *type;
2451 mSuspendedEffects.add(type->timeLow, desc);
2452 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2453 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002454
Eric Laurentca7cc822012-11-19 14:55:58 -08002455 if (desc->mRefCount++ == 0) {
2456 sp<EffectModule> effect = getEffectIfEnabled(type);
2457 if (effect != 0) {
2458 desc->mEffect = effect;
2459 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002460 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002461 }
2462 }
2463 } else {
2464 if (index < 0) {
2465 return;
2466 }
2467 desc = mSuspendedEffects.valueAt(index);
2468 if (desc->mRefCount <= 0) {
2469 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002470 desc->mRefCount = 0;
2471 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002472 }
2473 if (--desc->mRefCount == 0) {
2474 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2475 if (desc->mEffect != 0) {
2476 sp<EffectModule> effect = desc->mEffect.promote();
2477 if (effect != 0) {
2478 effect->setSuspended(false);
2479 effect->lock();
2480 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002481 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002482 effect->setEnabled_l(handle->enabled());
2483 }
2484 effect->unlock();
2485 }
2486 desc->mEffect.clear();
2487 }
2488 mSuspendedEffects.removeItemsAt(index);
2489 }
2490 }
2491}
2492
2493// must be called with ThreadBase::mLock held
2494void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2495{
2496 sp<SuspendedEffectDesc> desc;
2497
2498 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2499 if (suspend) {
2500 if (index >= 0) {
2501 desc = mSuspendedEffects.valueAt(index);
2502 } else {
2503 desc = new SuspendedEffectDesc();
2504 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2505 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2506 }
2507 if (desc->mRefCount++ == 0) {
2508 Vector< sp<EffectModule> > effects;
2509 getSuspendEligibleEffects(effects);
2510 for (size_t i = 0; i < effects.size(); i++) {
2511 setEffectSuspended_l(&effects[i]->desc().type, true);
2512 }
2513 }
2514 } else {
2515 if (index < 0) {
2516 return;
2517 }
2518 desc = mSuspendedEffects.valueAt(index);
2519 if (desc->mRefCount <= 0) {
2520 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2521 desc->mRefCount = 1;
2522 }
2523 if (--desc->mRefCount == 0) {
2524 Vector<const effect_uuid_t *> types;
2525 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2526 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2527 continue;
2528 }
2529 types.add(&mSuspendedEffects.valueAt(i)->mType);
2530 }
2531 for (size_t i = 0; i < types.size(); i++) {
2532 setEffectSuspended_l(types[i], false);
2533 }
2534 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2535 mSuspendedEffects.keyAt(index));
2536 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2537 }
2538 }
2539}
2540
2541
2542// The volume effect is used for automated tests only
2543#ifndef OPENSL_ES_H_
2544static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2545 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2546const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2547#endif //OPENSL_ES_H_
2548
Eric Laurentd8365c52017-07-16 15:27:05 -07002549/* static */
2550bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2551{
2552 // Only NS and AEC are suspended when BtNRec is off
2553 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2554 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2555 return true;
2556 }
2557 return false;
2558}
2559
Eric Laurentca7cc822012-11-19 14:55:58 -08002560bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2561{
2562 // auxiliary effects and visualizer are never suspended on output mix
2563 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2564 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2565 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002566 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2567 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002568 return false;
2569 }
2570 return true;
2571}
2572
2573void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2574 Vector< sp<AudioFlinger::EffectModule> > &effects)
2575{
2576 effects.clear();
2577 for (size_t i = 0; i < mEffects.size(); i++) {
2578 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2579 effects.add(mEffects[i]);
2580 }
2581 }
2582}
2583
2584sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2585 const effect_uuid_t *type)
2586{
2587 sp<EffectModule> effect = getEffectFromType_l(type);
2588 return effect != 0 && effect->isEnabled() ? effect : 0;
2589}
2590
2591void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2592 bool enabled)
2593{
2594 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2595 if (enabled) {
2596 if (index < 0) {
2597 // if the effect is not suspend check if all effects are suspended
2598 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2599 if (index < 0) {
2600 return;
2601 }
2602 if (!isEffectEligibleForSuspend(effect->desc())) {
2603 return;
2604 }
2605 setEffectSuspended_l(&effect->desc().type, enabled);
2606 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2607 if (index < 0) {
2608 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2609 return;
2610 }
2611 }
2612 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2613 effect->desc().type.timeLow);
2614 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002615 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002616 if (desc->mEffect == 0) {
2617 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002618 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002619 effect->setSuspended(true);
2620 }
2621 } else {
2622 if (index < 0) {
2623 return;
2624 }
2625 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2626 effect->desc().type.timeLow);
2627 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2628 desc->mEffect.clear();
2629 effect->setSuspended(false);
2630 }
2631}
2632
Eric Laurent5baf2af2013-09-12 17:37:00 -07002633bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002634{
2635 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002636 return isNonOffloadableEnabled_l();
2637}
2638
2639bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2640{
Eric Laurent813e2a72013-08-31 12:59:48 -07002641 size_t size = mEffects.size();
2642 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002643 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002644 return true;
2645 }
2646 }
2647 return false;
2648}
2649
Eric Laurentaaa44472014-09-12 17:41:50 -07002650void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2651{
2652 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002653 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002654}
2655
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002656void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2657{
2658 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2659 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2660 }
2661 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2662 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2663 }
2664}
2665
2666void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2667{
2668 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2669 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2670 }
2671 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2672 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2673 }
2674}
2675
2676bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002677{
2678 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002679 for (const auto &effect : mEffects) {
2680 if (effect->isProcessImplemented()) {
2681 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002682 }
2683 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002684 // Allow effects without processing.
2685 return true;
2686}
2687
2688bool AudioFlinger::EffectChain::isFastCompatible() const
2689{
2690 Mutex::Autolock _l(mLock);
2691 for (const auto &effect : mEffects) {
2692 if (effect->isProcessImplemented()
2693 && effect->isImplementationSoftware()) {
2694 return false;
2695 }
2696 }
2697 // Allow effects without processing or hw accelerated effects.
2698 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002699}
2700
2701// isCompatibleWithThread_l() must be called with thread->mLock held
2702bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2703{
2704 Mutex::Autolock _l(mLock);
2705 for (size_t i = 0; i < mEffects.size(); i++) {
2706 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2707 return false;
2708 }
2709 }
2710 return true;
2711}
2712
Eric Laurent6b446ce2019-12-13 10:56:31 -08002713// EffectCallbackInterface implementation
2714status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2715 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2716 sp<EffectHalInterface> *effect) {
2717 status_t status = NO_INIT;
2718 sp<AudioFlinger> af = mAudioFlinger.promote();
2719 if (af == nullptr) {
2720 return status;
2721 }
2722 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2723 if (effectsFactory != 0) {
2724 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2725 }
2726 return status;
2727}
2728
2729bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002730 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002731 sp<AudioFlinger> af = mAudioFlinger.promote();
2732 if (af == nullptr) {
2733 return false;
2734 }
Eric Laurent41709552019-12-16 19:34:05 -08002735 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2736 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002737}
2738
2739status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2740 size_t size, sp<EffectBufferHalInterface>* buffer) {
2741 sp<AudioFlinger> af = mAudioFlinger.promote();
2742 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2743 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2744}
2745
2746status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2747 sp<EffectHalInterface> effect) {
2748 status_t result = NO_INIT;
2749 sp<ThreadBase> t = mThread.promote();
2750 if (t == nullptr) {
2751 return result;
2752 }
2753 sp <StreamHalInterface> st = t->stream();
2754 if (st == nullptr) {
2755 return result;
2756 }
2757 result = st->addEffect(effect);
2758 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2759 return result;
2760}
2761
2762status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2763 sp<EffectHalInterface> effect) {
2764 status_t result = NO_INIT;
2765 sp<ThreadBase> t = mThread.promote();
2766 if (t == nullptr) {
2767 return result;
2768 }
2769 sp <StreamHalInterface> st = t->stream();
2770 if (st == nullptr) {
2771 return result;
2772 }
2773 result = st->removeEffect(effect);
2774 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2775 return result;
2776}
2777
2778audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2779 sp<ThreadBase> t = mThread.promote();
2780 if (t == nullptr) {
2781 return AUDIO_IO_HANDLE_NONE;
2782 }
2783 return t->id();
2784}
2785
2786bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2787 sp<ThreadBase> t = mThread.promote();
2788 if (t == nullptr) {
2789 return true;
2790 }
2791 return t->isOutput();
2792}
2793
2794bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2795 sp<ThreadBase> t = mThread.promote();
2796 if (t == nullptr) {
2797 return false;
2798 }
2799 return t->type() == ThreadBase::OFFLOAD;
2800}
2801
2802bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2803 sp<ThreadBase> t = mThread.promote();
2804 if (t == nullptr) {
2805 return false;
2806 }
2807 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2808}
2809
2810bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2811 sp<ThreadBase> t = mThread.promote();
2812 if (t == nullptr) {
2813 return false;
2814 }
Andy Hungea840382020-05-05 21:50:17 -07002815 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002816}
2817
2818uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2819 sp<ThreadBase> t = mThread.promote();
2820 if (t == nullptr) {
2821 return 0;
2822 }
2823 return t->sampleRate();
2824}
2825
2826audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2827 sp<ThreadBase> t = mThread.promote();
2828 if (t == nullptr) {
2829 return AUDIO_CHANNEL_NONE;
2830 }
2831 return t->channelMask();
2832}
2833
2834uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2835 sp<ThreadBase> t = mThread.promote();
2836 if (t == nullptr) {
2837 return 0;
2838 }
2839 return t->channelCount();
2840}
2841
2842size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2843 sp<ThreadBase> t = mThread.promote();
2844 if (t == nullptr) {
2845 return 0;
2846 }
2847 return t->frameCount();
2848}
2849
2850uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2851 sp<ThreadBase> t = mThread.promote();
2852 if (t == nullptr) {
2853 return 0;
2854 }
2855 return t->latency_l();
2856}
2857
2858void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2859 sp<ThreadBase> t = mThread.promote();
2860 if (t == nullptr) {
2861 return;
2862 }
2863 t->setVolumeForOutput_l(left, right);
2864}
2865
2866void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002867 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002868 sp<ThreadBase> t = mThread.promote();
2869 if (t == nullptr) {
2870 return;
2871 }
2872 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2873
2874 sp<EffectChain> c = mChain.promote();
2875 if (c == nullptr) {
2876 return;
2877 }
Eric Laurent41709552019-12-16 19:34:05 -08002878 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2879 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002880}
2881
Eric Laurent41709552019-12-16 19:34:05 -08002882void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002883 sp<ThreadBase> t = mThread.promote();
2884 if (t == nullptr) {
2885 return;
2886 }
Eric Laurent41709552019-12-16 19:34:05 -08002887 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2888 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002889}
2890
Eric Laurent41709552019-12-16 19:34:05 -08002891void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002892 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2893
2894 sp<ThreadBase> t = mThread.promote();
2895 if (t == nullptr) {
2896 return;
2897 }
2898 t->onEffectDisable();
2899}
2900
2901bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2902 bool unpinIfLast) {
2903 sp<ThreadBase> t = mThread.promote();
2904 if (t == nullptr) {
2905 return false;
2906 }
2907 t->disconnectEffectHandle(handle, unpinIfLast);
2908 return true;
2909}
2910
2911void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2912 sp<EffectChain> c = mChain.promote();
2913 if (c == nullptr) {
2914 return;
2915 }
2916 c->resetVolume_l();
2917
2918}
2919
2920uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2921 sp<EffectChain> c = mChain.promote();
2922 if (c == nullptr) {
2923 return PRODUCT_STRATEGY_NONE;
2924 }
2925 return c->strategy();
2926}
2927
2928int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2929 sp<EffectChain> c = mChain.promote();
2930 if (c == nullptr) {
2931 return 0;
2932 }
2933 return c->activeTrackCnt();
2934}
2935
Eric Laurentb82e6b72019-11-22 17:25:04 -08002936
2937#undef LOG_TAG
2938#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2939
2940status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2941{
2942 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2943 Mutex::Autolock _l(mProxyLock);
2944 if (status == NO_ERROR) {
2945 for (auto& handle : mEffectHandles) {
2946 if (enabled) {
2947 status = handle.second->enable();
2948 } else {
2949 status = handle.second->disable();
2950 }
2951 }
2952 }
2953 ALOGV("%s enable %d status %d", __func__, enabled, status);
2954 return status;
2955}
2956
2957status_t AudioFlinger::DeviceEffectProxy::init(
2958 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2959//For all audio patches
2960//If src or sink device match
2961//If the effect is HW accelerated
2962// if no corresponding effect module
2963// Create EffectModule: mHalEffect
2964//Create and attach EffectHandle
2965//If the effect is not HW accelerated and the patch sink or src is a mixer port
2966// Create Effect on patch input or output thread on session -1
2967//Add EffectHandle to EffectHandle map of Effect Proxy:
2968 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2969 status_t status = NO_ERROR;
2970 for (auto &patch : patches) {
2971 status = onCreatePatch(patch.first, patch.second);
2972 ALOGV("%s onCreatePatch status %d", __func__, status);
2973 if (status == BAD_VALUE) {
2974 return status;
2975 }
2976 }
2977 return status;
2978}
2979
2980status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2981 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2982 status_t status = NAME_NOT_FOUND;
2983 sp<EffectHandle> handle;
2984 // only consider source[0] as this is the only "true" source of a patch
2985 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2986 ALOGV("%s source checkPort status %d", __func__, status);
2987 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2988 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2989 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2990 }
2991 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2992 Mutex::Autolock _l(mProxyLock);
2993 mEffectHandles.emplace(patchHandle, handle);
2994 }
2995 ALOGW_IF(status == BAD_VALUE,
2996 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2997
2998 return status;
2999}
3000
3001status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3002 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3003
3004 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3005 __func__, port->type, port->ext.device.type,
3006 port->ext.device.address, port->id, patch.isSoftware());
3007 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
3008 || port->ext.device.address != mDevice.mAddress) {
3009 return NAME_NOT_FOUND;
3010 }
3011 status_t status = NAME_NOT_FOUND;
3012
3013 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3014 Mutex::Autolock _l(mProxyLock);
3015 mDevicePort = *port;
3016 mHalEffect = new EffectModule(mMyCallback,
3017 const_cast<effect_descriptor_t *>(&mDescriptor),
3018 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3019 false /* pinned */, port->id);
3020 if (audio_is_input_device(mDevice.mType)) {
3021 mHalEffect->setInputDevice(mDevice);
3022 } else {
3023 mHalEffect->setDevices({mDevice});
3024 }
3025 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3026 status = (*handle)->initCheck();
3027 if (status == OK) {
3028 status = mHalEffect->addHandle((*handle).get());
3029 } else {
3030 mHalEffect.clear();
3031 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3032 }
3033 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3034 sp <ThreadBase> thread;
3035 if (audio_port_config_has_input_direction(port)) {
3036 if (patch.isSoftware()) {
3037 thread = patch.mRecord.thread();
3038 } else {
3039 thread = patch.thread().promote();
3040 }
3041 } else {
3042 if (patch.isSoftware()) {
3043 thread = patch.mPlayback.thread();
3044 } else {
3045 thread = patch.thread().promote();
3046 }
3047 }
3048 int enabled;
3049 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3050 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003051 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003052 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3053 } else {
3054 status = BAD_VALUE;
3055 }
3056 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3057 if (isEnabled()) {
3058 (*handle)->enable();
3059 } else {
3060 (*handle)->disable();
3061 }
3062 }
3063 return status;
3064}
3065
3066void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3067 Mutex::Autolock _l(mProxyLock);
3068 mEffectHandles.erase(patchHandle);
3069}
3070
3071
3072size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3073{
3074 Mutex::Autolock _l(mProxyLock);
3075 if (effect == mHalEffect) {
3076 mHalEffect.clear();
3077 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3078 }
3079 return mHalEffect == nullptr ? 0 : 1;
3080}
3081
3082status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3083 sp<EffectHalInterface> effect) {
3084 if (mHalEffect == nullptr) {
3085 return NO_INIT;
3086 }
3087 return mManagerCallback->addEffectToHal(
3088 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3089}
3090
3091status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3092 sp<EffectHalInterface> effect) {
3093 if (mHalEffect == nullptr) {
3094 return NO_INIT;
3095 }
3096 return mManagerCallback->removeEffectFromHal(
3097 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3098}
3099
3100bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3101 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3102 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3103 }
3104 return true;
3105}
3106
3107uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3108 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3109 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3110 return mDevicePort.sample_rate;
3111 }
3112 return DEFAULT_OUTPUT_SAMPLE_RATE;
3113}
3114
3115audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3116 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3117 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3118 return mDevicePort.channel_mask;
3119 }
3120 return AUDIO_CHANNEL_OUT_STEREO;
3121}
3122
3123uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3124 if (isOutput()) {
3125 return audio_channel_count_from_out_mask(channelMask());
3126 }
3127 return audio_channel_count_from_in_mask(channelMask());
3128}
3129
3130void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3131 const Vector<String16> args;
3132 EffectBase::dump(fd, args);
3133
3134 const bool locked = dumpTryLock(mProxyLock);
3135 if (!locked) {
3136 String8 result("DeviceEffectProxy may be deadlocked\n");
3137 write(fd, result.string(), result.size());
3138 }
3139
3140 String8 outStr;
3141 if (mHalEffect != nullptr) {
3142 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3143 } else {
3144 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3145 }
3146 write(fd, outStr.string(), outStr.size());
3147 outStr.clear();
3148
3149 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3150 write(fd, outStr.string(), outStr.size());
3151 outStr.clear();
3152
3153 for (const auto& iter : mEffectHandles) {
3154 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3155 write(fd, outStr.string(), outStr.size());
3156 outStr.clear();
3157 sp<EffectBase> effect = iter.second->effect().promote();
3158 if (effect != nullptr) {
3159 effect->dump(fd, args);
3160 }
3161 }
3162
3163 if (locked) {
3164 mLock.unlock();
3165 }
3166}
3167
3168#undef LOG_TAG
3169#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3170
3171int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3172 return mManagerCallback->newEffectId();
3173}
3174
3175
3176bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3177 EffectHandle *handle, bool unpinIfLast) {
3178 sp<EffectBase> effectBase = handle->effect().promote();
3179 if (effectBase == nullptr) {
3180 return false;
3181 }
3182
3183 sp<EffectModule> effect = effectBase->asEffectModule();
3184 if (effect == nullptr) {
3185 return false;
3186 }
3187
3188 // restore suspended effects if the disconnected handle was enabled and the last one.
3189 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3190 if (remove) {
3191 sp<DeviceEffectProxy> proxy = mProxy.promote();
3192 if (proxy != nullptr) {
3193 proxy->removeEffect(effect);
3194 }
3195 if (handle->enabled()) {
3196 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3197 }
3198 }
3199 return true;
3200}
3201
3202status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3203 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3204 sp<EffectHalInterface> *effect) {
3205 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3206}
3207
3208status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3209 sp<EffectHalInterface> effect) {
3210 sp<DeviceEffectProxy> proxy = mProxy.promote();
3211 if (proxy == nullptr) {
3212 return NO_INIT;
3213 }
3214 return proxy->addEffectToHal(effect);
3215}
3216
3217status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3218 sp<EffectHalInterface> effect) {
3219 sp<DeviceEffectProxy> proxy = mProxy.promote();
3220 if (proxy == nullptr) {
3221 return NO_INIT;
3222 }
3223 return proxy->addEffectToHal(effect);
3224}
3225
3226bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3227 sp<DeviceEffectProxy> proxy = mProxy.promote();
3228 if (proxy == nullptr) {
3229 return true;
3230 }
3231 return proxy->isOutput();
3232}
3233
3234uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3235 sp<DeviceEffectProxy> proxy = mProxy.promote();
3236 if (proxy == nullptr) {
3237 return DEFAULT_OUTPUT_SAMPLE_RATE;
3238 }
3239 return proxy->sampleRate();
3240}
3241
3242audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3243 sp<DeviceEffectProxy> proxy = mProxy.promote();
3244 if (proxy == nullptr) {
3245 return AUDIO_CHANNEL_OUT_STEREO;
3246 }
3247 return proxy->channelMask();
3248}
3249
3250uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3251 sp<DeviceEffectProxy> proxy = mProxy.promote();
3252 if (proxy == nullptr) {
3253 return 2;
3254 }
3255 return proxy->channelCount();
3256}
3257
Glenn Kasten63238ef2015-03-02 15:50:29 -08003258} // namespace android