blob: 529b87c3e52722e5ce9e0c98af1150bea70bb586 [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>
jiabinb8269fd2019-11-11 12:16:27 -080033#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070034#include <media/AudioEffect.h>
jiabinb8269fd2019-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 Laurente0b9a362019-12-16 19:34:05 -080062// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080063// ----------------------------------------------------------------------------
64
65#undef LOG_TAG
Eric Laurente0b9a362019-12-16 19:34:05 -080066#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080067
Eric Laurente0b9a362019-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 Laurent5d885392019-12-13 10:56:31 -080074 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurent5d885392019-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 Laurente0b9a362019-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 Laurente0b9a362019-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
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530295 // Prevent calls to process() and other functions on effect interface from now on.
296 // The effect engine will be released by the destructor when the last strong reference on
297 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800298 if (mHandles.size() == 0 && !mPinned) {
299 mState = DESTROYED;
300 }
301
302 return mHandles.size();
303}
304
305// must be called with EffectModule::mLock held
Eric Laurente0b9a362019-12-16 19:34:05 -0800306AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800307{
308 // the first valid handle in the list has control over the module
309 for (size_t i = 0; i < mHandles.size(); i++) {
310 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800311 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800312 return h;
313 }
314 }
315
316 return NULL;
317}
318
Eric Laurentf10c7092016-12-06 17:09:56 -0800319// unsafe method called when the effect parent thread has been destroyed
Eric Laurente0b9a362019-12-16 19:34:05 -0800320ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800321{
322 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent5d885392019-12-13 10:56:31 -0800323 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
324 return mHandles.size();
325 }
326
Eric Laurentf10c7092016-12-06 17:09:56 -0800327 Mutex::Autolock _l(mLock);
328 ssize_t numHandles = removeHandle_l(handle);
329 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent5d885392019-12-13 10:56:31 -0800330 mLock.unlock();
331 mCallback->updateOrphanEffectChains(this);
332 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800333 }
334 return numHandles;
335}
336
Eric Laurente0b9a362019-12-16 19:34:05 -0800337bool AudioFlinger::EffectBase::purgeHandles()
338{
339 bool enabled = false;
340 Mutex::Autolock _l(mLock);
341 EffectHandle *handle = controlHandle_l();
342 if (handle != NULL) {
343 enabled = handle->enabled();
344 }
345 mHandles.clear();
346 return enabled;
347}
348
349void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
350 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
351}
352
353static String8 effectFlagsToString(uint32_t flags) {
354 String8 s;
355
356 s.append("conn. mode: ");
357 switch (flags & EFFECT_FLAG_TYPE_MASK) {
358 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
359 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
360 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
361 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
362 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
363 default: s.append("unknown/reserved"); break;
364 }
365 s.append(", ");
366
367 s.append("insert pref: ");
368 switch (flags & EFFECT_FLAG_INSERT_MASK) {
369 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
370 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
371 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
372 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
373 default: s.append("unknown/reserved"); break;
374 }
375 s.append(", ");
376
377 s.append("volume mgmt: ");
378 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
379 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
380 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
381 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
382 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
383 default: s.append("unknown/reserved"); break;
384 }
385 s.append(", ");
386
387 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
388 if (devind) {
389 s.append("device indication: ");
390 switch (devind) {
391 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
392 default: s.append("unknown/reserved"); break;
393 }
394 s.append(", ");
395 }
396
397 s.append("input mode: ");
398 switch (flags & EFFECT_FLAG_INPUT_MASK) {
399 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
400 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
401 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
402 default: s.append("not set"); break;
403 }
404 s.append(", ");
405
406 s.append("output mode: ");
407 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
408 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
409 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
410 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
411 default: s.append("not set"); break;
412 }
413 s.append(", ");
414
415 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
416 if (accel) {
417 s.append("hardware acceleration: ");
418 switch (accel) {
419 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
420 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
421 default: s.append("unknown/reserved"); break;
422 }
423 s.append(", ");
424 }
425
426 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
427 if (modeind) {
428 s.append("mode indication: ");
429 switch (modeind) {
430 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
431 default: s.append("unknown/reserved"); break;
432 }
433 s.append(", ");
434 }
435
436 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
437 if (srcind) {
438 s.append("source indication: ");
439 switch (srcind) {
440 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
441 default: s.append("unknown/reserved"); break;
442 }
443 s.append(", ");
444 }
445
446 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
447 s.append("offloadable, ");
448 }
449
450 int len = s.length();
451 if (s.length() > 2) {
452 (void) s.lockBuffer(len);
453 s.unlockBuffer(len - 2);
454 }
455 return s;
456}
457
458void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
459{
460 String8 result;
461
462 result.appendFormat("\tEffect ID %d:\n", mId);
463
464 bool locked = AudioFlinger::dumpTryLock(mLock);
465 // failed to lock - AudioFlinger is probably deadlocked
466 if (!locked) {
467 result.append("\t\tCould not lock Fx mutex:\n");
468 }
469
470 result.append("\t\tSession State Registered Enabled Suspended:\n");
471 result.appendFormat("\t\t%05d %03d %s %s %s\n",
472 mSessionId, mState, mPolicyRegistered ? "y" : "n",
473 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
474
475 result.append("\t\tDescriptor:\n");
476 char uuidStr[64];
477 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
478 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
479 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
480 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
481 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
482 mDescriptor.apiVersion,
483 mDescriptor.flags,
484 effectFlagsToString(mDescriptor.flags).string());
485 result.appendFormat("\t\t- name: %s\n",
486 mDescriptor.name);
487
488 result.appendFormat("\t\t- implementor: %s\n",
489 mDescriptor.implementor);
490
491 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
492 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
493 char buffer[256];
494 for (size_t i = 0; i < mHandles.size(); ++i) {
495 EffectHandle *handle = mHandles[i];
496 if (handle != NULL && !handle->disconnected()) {
497 handle->dumpToBuffer(buffer, sizeof(buffer));
498 result.append(buffer);
499 }
500 }
501 if (locked) {
502 mLock.unlock();
503 }
504
505 write(fd, result.string(), result.length());
506}
507
508// ----------------------------------------------------------------------------
509// EffectModule implementation
510// ----------------------------------------------------------------------------
511
512#undef LOG_TAG
513#define LOG_TAG "AudioFlinger::EffectModule"
514
515AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
516 effect_descriptor_t *desc,
517 int id,
518 audio_session_t sessionId,
Eric Laurent9b2064c2019-11-22 17:25:04 -0800519 bool pinned,
520 audio_port_handle_t deviceId)
Eric Laurente0b9a362019-12-16 19:34:05 -0800521 : EffectBase(callback, desc, id, sessionId, pinned),
522 // clear mConfig to ensure consistent initial value of buffer framecount
523 // in case buffers are associated by setInBuffer() or setOutBuffer()
524 // prior to configure().
525 mConfig{{}, {}},
526 mStatus(NO_INIT),
527 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
528 mDisableWaitCnt(0), // set by process() and updateState()
529 mOffloaded(false)
530#ifdef FLOAT_EFFECT_CHAIN
531 , mSupportsFloat(false)
532#endif
533{
534 ALOGV("Constructor %p pinned %d", this, pinned);
535 int lStatus;
536
537 // create effect engine from effect factory
538 mStatus = callback->createEffectHal(
Eric Laurent9b2064c2019-11-22 17:25:04 -0800539 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurente0b9a362019-12-16 19:34:05 -0800540 if (mStatus != NO_ERROR) {
541 return;
542 }
543 lStatus = init();
544 if (lStatus < 0) {
545 mStatus = lStatus;
546 goto Error;
547 }
548
549 setOffloaded(callback->isOffload(), callback->io());
550 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
551
552 return;
553Error:
554 mEffectInterface.clear();
555 ALOGV("Constructor Error %d", mStatus);
556}
557
558AudioFlinger::EffectModule::~EffectModule()
559{
560 ALOGV("Destructor %p", this);
561 if (mEffectInterface != 0) {
562 char uuidStr[64];
563 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
564 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
565 this, uuidStr);
566 release_l();
567 }
568
569}
570
Eric Laurentfa1e1232016-08-02 19:01:49 -0700571bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800572 Mutex::Autolock _l(mLock);
573
Eric Laurentfa1e1232016-08-02 19:01:49 -0700574 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800575 switch (mState) {
576 case RESTART:
577 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700578 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800579
580 case STARTING:
581 // clear auxiliary effect input buffer for next accumulation
582 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
583 memset(mConfig.inputCfg.buffer.raw,
584 0,
585 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
586 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700587 if (start_l() == NO_ERROR) {
588 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700589 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700590 } else {
591 mState = IDLE;
592 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800593 break;
594 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900595 // volume control for offload and direct threads must take effect immediately.
596 if (stop_l() == NO_ERROR
597 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700598 mDisableWaitCnt = mMaxDisableWaitCnt;
599 } else {
600 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
601 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800602 mState = STOPPED;
603 break;
604 case STOPPED:
605 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
606 // turn off sequence.
607 if (--mDisableWaitCnt == 0) {
608 reset_l();
609 mState = IDLE;
610 }
611 break;
612 default: //IDLE , ACTIVE, DESTROYED
613 break;
614 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700615
616 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800617}
618
619void AudioFlinger::EffectModule::process()
620{
621 Mutex::Autolock _l(mLock);
622
Mikhail Naganov022b9952017-01-04 16:36:51 -0800623 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800624 return;
625 }
626
rago94a1ee82017-07-21 15:11:02 -0700627 const uint32_t inChannelCount =
628 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
629 const uint32_t outChannelCount =
630 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
631 const bool auxType =
632 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
633
Andy Hungfa69ca32017-11-30 10:07:53 -0800634 // safeInputOutputSampleCount is 0 if the channel count between input and output
635 // buffers do not match. This prevents automatic accumulation or copying between the
636 // input and output effect buffers without an intermediary effect process.
637 // TODO: consider implementing channel conversion.
638 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700639 mInChannelCountRequested != mOutChannelCountRequested ? 0
640 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800641 mConfig.inputCfg.buffer.frameCount,
642 mConfig.outputCfg.buffer.frameCount);
643 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
644#ifdef FLOAT_EFFECT_CHAIN
645 accumulate_float(
646 mConfig.outputCfg.buffer.f32,
647 mConfig.inputCfg.buffer.f32,
648 safeInputOutputSampleCount);
649#else
650 accumulate_i16(
651 mConfig.outputCfg.buffer.s16,
652 mConfig.inputCfg.buffer.s16,
653 safeInputOutputSampleCount);
654#endif
655 };
656 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
657#ifdef FLOAT_EFFECT_CHAIN
658 memcpy(
659 mConfig.outputCfg.buffer.f32,
660 mConfig.inputCfg.buffer.f32,
661 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
662
663#else
664 memcpy(
665 mConfig.outputCfg.buffer.s16,
666 mConfig.inputCfg.buffer.s16,
667 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
668#endif
669 };
670
Eric Laurentca7cc822012-11-19 14:55:58 -0800671 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700672 int ret;
673 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700674 if (auxType) {
675 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800676 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700677#ifdef FLOAT_EFFECT_CHAIN
678 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800679#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700680 // Do in-place float conversion for auxiliary effect input buffer.
681 static_assert(sizeof(float) <= sizeof(int32_t),
682 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
683
Andy Hungfa69ca32017-11-30 10:07:53 -0800684 memcpy_to_float_from_q4_27(
685 mConfig.inputCfg.buffer.f32,
686 mConfig.inputCfg.buffer.s32,
687 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800688#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800689 } else
Andy Hung116a4982017-11-30 10:15:08 -0800690#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800691 {
Andy Hung116a4982017-11-30 10:15:08 -0800692#ifdef FLOAT_AUX
693 memcpy_to_i16_from_float(
694 mConfig.inputCfg.buffer.s16,
695 mConfig.inputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.frameCount);
697#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800698 memcpy_to_i16_from_q4_27(
699 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700700 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800701 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800702#endif
rago94a1ee82017-07-21 15:11:02 -0700703 }
rago94a1ee82017-07-21 15:11:02 -0700704 }
705#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800706 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
707 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
708
709 if (!auxType && mInChannelCountRequested != inChannelCount) {
710 adjust_channels(
711 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
712 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
713 sizeof(float),
714 sizeof(float)
715 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
716 inBuffer = mInConversionBuffer;
717 }
718 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
719 && mOutChannelCountRequested != outChannelCount) {
720 adjust_selected_channels(
721 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
722 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
723 sizeof(float),
724 sizeof(float)
725 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
726 outBuffer = mOutConversionBuffer;
727 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800728 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
729 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800730 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800731 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
732 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700733 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800734 memcpy_to_i16_from_float(
735 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800736 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800738 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700739 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800740 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800741 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800742 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
743 goto data_bypass;
744 }
745 memcpy_to_i16_from_float(
746 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800747 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800748 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800749 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700750 }
751 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800752#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800753 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800754#ifdef FLOAT_EFFECT_CHAIN
755 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800756 sp<EffectBufferHalInterface> target =
757 mOutChannelCountRequested != outChannelCount
758 ? mOutConversionBuffer : mOutBuffer;
759
Andy Hungfa69ca32017-11-30 10:07:53 -0800760 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800761 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800762 mOutConversionBuffer->audioBuffer()->s16,
763 outChannelCount * mConfig.outputCfg.buffer.frameCount);
764 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800765 if (mOutChannelCountRequested != outChannelCount) {
766 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
767 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
768 sizeof(float),
769 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
770 }
rago94a1ee82017-07-21 15:11:02 -0700771#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700772 } else {
rago94a1ee82017-07-21 15:11:02 -0700773#ifdef FLOAT_EFFECT_CHAIN
774 data_bypass:
775#endif
776 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800777 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700778 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800779 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700780 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700782 }
783 }
784 ret = -ENODATA;
785 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800786
Eric Laurentca7cc822012-11-19 14:55:58 -0800787 // force transition to IDLE state when engine is ready
788 if (mState == STOPPED && ret == -ENODATA) {
789 mDisableWaitCnt = 1;
790 }
791
792 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700793 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800794#ifdef FLOAT_AUX
795 const size_t size =
796 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
797#else
rago94a1ee82017-07-21 15:11:02 -0700798 const size_t size =
799 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800800#endif
rago94a1ee82017-07-21 15:11:02 -0700801 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800802 }
803 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700804 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800805 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
806 // If an insert effect is idle and input buffer is different from output buffer,
807 // accumulate input onto output
Eric Laurent5d885392019-12-13 10:56:31 -0800808 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700809 // similar handling with data_bypass above.
810 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
811 accumulateInputToOutput();
812 } else { // EFFECT_BUFFER_ACCESS_WRITE
813 copyInputToOutput();
814 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 }
816 }
817}
818
819void AudioFlinger::EffectModule::reset_l()
820{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700821 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800822 return;
823 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700824 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800825}
826
827status_t AudioFlinger::EffectModule::configure()
828{
rago94a1ee82017-07-21 15:11:02 -0700829 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700830 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700831 uint32_t size;
832 audio_channel_mask_t channelMask;
833
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700834 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700835 status = NO_INIT;
836 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800837 }
838
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800840 // TODO: handle configuration of input (record) SW effects above the HAL,
841 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
842 // in which case input channel masks should be used here.
Eric Laurent5d885392019-12-13 10:56:31 -0800843 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800844 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700845 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800846
847 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800848 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
849 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
850 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
851 mConfig.inputCfg.channels);
852 }
853#ifndef MULTICHANNEL_EFFECT_CHAIN
854 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
855 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
856 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
857 mConfig.outputCfg.channels);
858 }
859#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800860 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800861#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700862 // TODO: Update this logic when multichannel effects are implemented.
863 // For offloaded tracks consider mono output as stereo for proper effect initialization
864 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
865 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
866 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
867 ALOGV("Overriding effect input and output as STEREO");
868 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800869#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800871 mInChannelCountRequested =
872 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
873 mOutChannelCountRequested =
874 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700875
rago94a1ee82017-07-21 15:11:02 -0700876 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
877 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900878
879 // Don't use sample rate for thread if effect isn't offloadable.
Eric Laurent5d885392019-12-13 10:56:31 -0800880 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900881 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
882 ALOGV("Overriding effect input as 48kHz");
883 } else {
Eric Laurent5d885392019-12-13 10:56:31 -0800884 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900885 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800886 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
887 mConfig.inputCfg.bufferProvider.cookie = NULL;
888 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
889 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
890 mConfig.outputCfg.bufferProvider.cookie = NULL;
891 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
892 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
893 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
894 // Insert effect:
Eric Laurenta20c4e92019-11-12 15:55:51 -0800895 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800896 // always overwrites output buffer: input buffer == output buffer
897 // - in other sessions:
898 // last effect in the chain accumulates in output buffer: input buffer != output buffer
899 // other effect: overwrites output buffer: input buffer == output buffer
900 // Auxiliary effect:
901 // accumulates in output buffer: input buffer != output buffer
902 // Therefore: accumulate <=> input buffer != output buffer
903 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
904 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
905 } else {
906 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
907 }
908 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
909 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent5d885392019-12-13 10:56:31 -0800910 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800911 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
912
Eric Laurent5d885392019-12-13 10:56:31 -0800913 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800914 this, mCallback->chain().promote().get(),
915 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800916
917 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700918 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700919 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800920 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700921 &mConfig,
922 &size,
923 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700924 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 status = cmdStatus;
926 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800927
928#ifdef MULTICHANNEL_EFFECT_CHAIN
929 if (status != NO_ERROR &&
Eric Laurent5d885392019-12-13 10:56:31 -0800930 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
932 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
933 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700934 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
935 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800936 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
937 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
938 }
939 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
940 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
941 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
942 }
943 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700944 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800945 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700946 &mConfig,
947 &size,
948 &cmdStatus);
949 if (status == NO_ERROR) {
950 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800951 }
952 }
953#endif
954
955#ifdef FLOAT_EFFECT_CHAIN
956 if (status == NO_ERROR) {
957 mSupportsFloat = true;
958 }
959
960 if (status != NO_ERROR) {
961 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
962 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
963 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
964 size = sizeof(int);
965 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
966 sizeof(mConfig),
967 &mConfig,
968 &size,
969 &cmdStatus);
970 if (status == NO_ERROR) {
971 status = cmdStatus;
972 }
973 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700974 mSupportsFloat = false;
975 ALOGVV("config worked with 16 bit");
976 } else {
977 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800978 }
rago94a1ee82017-07-21 15:11:02 -0700979 }
980#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800981
rago94a1ee82017-07-21 15:11:02 -0700982 if (status == NO_ERROR) {
983 // Establish Buffer strategy
984 setInBuffer(mInBuffer);
985 setOutBuffer(mOutBuffer);
986
987 // Update visualizer latency
988 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
989 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
990 effect_param_t *p = (effect_param_t *)buf32;
991
992 p->psize = sizeof(uint32_t);
993 p->vsize = sizeof(uint32_t);
994 size = sizeof(int);
995 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
996
Eric Laurent5d885392019-12-13 10:56:31 -0800997 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -0700998
999 *((int32_t *)p->data + 1)= latency;
1000 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1001 sizeof(effect_param_t) + 8,
1002 &buf32,
1003 &size,
1004 &cmdStatus);
1005 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001006 }
1007
Andy Hung05083ac2017-12-14 15:00:28 -08001008 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1009 mMaxDisableWaitCnt = (uint32_t)std::max(
1010 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1011 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1012 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001013
Eric Laurentd0ebb532013-04-02 16:41:41 -07001014exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001015 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001016 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001017 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 return status;
1019}
1020
1021status_t AudioFlinger::EffectModule::init()
1022{
1023 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001024 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001025 return NO_INIT;
1026 }
1027 status_t cmdStatus;
1028 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001029 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1030 0,
1031 NULL,
1032 &size,
1033 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 if (status == 0) {
1035 status = cmdStatus;
1036 }
1037 return status;
1038}
1039
Eric Laurent1b928682014-10-02 19:41:47 -07001040void AudioFlinger::EffectModule::addEffectToHal_l()
1041{
1042 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1043 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -08001044 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001045 }
1046}
1047
Eric Laurentfa1e1232016-08-02 19:01:49 -07001048// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001049status_t AudioFlinger::EffectModule::start()
1050{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001051 status_t status;
1052 {
1053 Mutex::Autolock _l(mLock);
1054 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001055 }
Eric Laurent5d885392019-12-13 10:56:31 -08001056 if (status == NO_ERROR) {
1057 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001058 }
1059 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001060}
1061
1062status_t AudioFlinger::EffectModule::start_l()
1063{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001064 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 return NO_INIT;
1066 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001067 if (mStatus != NO_ERROR) {
1068 return mStatus;
1069 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 status_t cmdStatus;
1071 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001072 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1073 0,
1074 NULL,
1075 &size,
1076 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 if (status == 0) {
1078 status = cmdStatus;
1079 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001080 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001081 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001082 }
1083 return status;
1084}
1085
1086status_t AudioFlinger::EffectModule::stop()
1087{
1088 Mutex::Autolock _l(mLock);
1089 return stop_l();
1090}
1091
1092status_t AudioFlinger::EffectModule::stop_l()
1093{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001094 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 return NO_INIT;
1096 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001097 if (mStatus != NO_ERROR) {
1098 return mStatus;
1099 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001100 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001101 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001102
1103 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001104 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1105 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1106 mSetVolumeReentrantTid = gettid();
Eric Laurent5d885392019-12-13 10:56:31 -08001107 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001108 mSetVolumeReentrantTid = INVALID_PID;
1109 }
1110
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001111 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1112 0,
1113 NULL,
1114 &size,
1115 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001116 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001117 status = cmdStatus;
1118 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001119 if (status == NO_ERROR) {
Eric Laurent5d885392019-12-13 10:56:31 -08001120 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001121 }
1122 return status;
1123}
1124
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001125// must be called with EffectChain::mLock held
1126void AudioFlinger::EffectModule::release_l()
1127{
1128 if (mEffectInterface != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001129 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001130 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001131 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001132 mEffectInterface.clear();
1133 }
1134}
1135
Eric Laurent5d885392019-12-13 10:56:31 -08001136status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001137{
1138 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1139 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -08001140 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001141 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001143}
1144
Andy Hunge4a1d912016-08-17 14:11:13 -07001145// round up delta valid if value and divisor are positive.
1146template <typename T>
1147static T roundUpDelta(const T &value, const T &divisor) {
1148 T remainder = value % divisor;
1149 return remainder == 0 ? 0 : divisor - remainder;
1150}
1151
Eric Laurentca7cc822012-11-19 14:55:58 -08001152status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1153 uint32_t cmdSize,
1154 void *pCmdData,
1155 uint32_t *replySize,
1156 void *pReplyData)
1157{
1158 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001159 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001160
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001161 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 return NO_INIT;
1163 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001164 if (mStatus != NO_ERROR) {
1165 return mStatus;
1166 }
Andy Hung110bc952016-06-20 15:22:52 -07001167 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001168 (sizeof(effect_param_t) > cmdSize ||
1169 ((effect_param_t *)pCmdData)->psize > cmdSize
1170 - sizeof(effect_param_t))) {
1171 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001172 android_errorWriteLog(0x534e4554, "33003822");
1173 return -EINVAL;
1174 }
1175 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1176 (*replySize < sizeof(effect_param_t) ||
1177 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1178 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001179 return -EINVAL;
1180 }
ragoe2759072016-11-22 18:02:48 -08001181 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1182 (sizeof(effect_param_t) > *replySize
1183 || ((effect_param_t *)pCmdData)->psize > *replySize
1184 - sizeof(effect_param_t)
1185 || ((effect_param_t *)pCmdData)->vsize > *replySize
1186 - sizeof(effect_param_t)
1187 - ((effect_param_t *)pCmdData)->psize
1188 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1189 *replySize
1190 - sizeof(effect_param_t)
1191 - ((effect_param_t *)pCmdData)->psize
1192 - ((effect_param_t *)pCmdData)->vsize)) {
1193 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1194 android_errorWriteLog(0x534e4554, "32705438");
1195 return -EINVAL;
1196 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001197 if ((cmdCode == EFFECT_CMD_SET_PARAM
1198 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1199 (sizeof(effect_param_t) > cmdSize
1200 || ((effect_param_t *)pCmdData)->psize > cmdSize
1201 - sizeof(effect_param_t)
1202 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1203 - sizeof(effect_param_t)
1204 - ((effect_param_t *)pCmdData)->psize
1205 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1206 cmdSize
1207 - sizeof(effect_param_t)
1208 - ((effect_param_t *)pCmdData)->psize
1209 - ((effect_param_t *)pCmdData)->vsize)) {
1210 android_errorWriteLog(0x534e4554, "30204301");
1211 return -EINVAL;
1212 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001213 status_t status = mEffectInterface->command(cmdCode,
1214 cmdSize,
1215 pCmdData,
1216 replySize,
1217 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001218 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1219 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1220 for (size_t i = 1; i < mHandles.size(); i++) {
1221 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001222 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1224 }
1225 }
1226 }
1227 return status;
1228}
1229
Eric Laurentca7cc822012-11-19 14:55:58 -08001230bool AudioFlinger::EffectModule::isProcessEnabled() const
1231{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001232 if (mStatus != NO_ERROR) {
1233 return false;
1234 }
1235
Eric Laurentca7cc822012-11-19 14:55:58 -08001236 switch (mState) {
1237 case RESTART:
1238 case ACTIVE:
1239 case STOPPING:
1240 case STOPPED:
1241 return true;
1242 case IDLE:
1243 case STARTING:
1244 case DESTROYED:
1245 default:
1246 return false;
1247 }
1248}
1249
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001250bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1251{
Eric Laurent5d885392019-12-13 10:56:31 -08001252 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001253}
1254
1255bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1256{
1257 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1258}
1259
Mikhail Naganov022b9952017-01-04 16:36:51 -08001260void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001261 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001262
1263 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001264 if (buffer != 0) {
1265 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1266 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1267 } else {
1268 mConfig.inputCfg.buffer.raw = NULL;
1269 }
1270 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001271 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001272
1273#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001274 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001275 // Theoretically insert effects can also do in-place conversions (destroying
1276 // the original buffer) when the output buffer is identical to the input buffer,
1277 // but we don't optimize for it here.
1278 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001279 const uint32_t inChannelCount =
1280 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1281 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001282 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001283 // we need to translate - create hidl shared buffer and intercept
1284 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001285 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1286 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1287 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001288
1289 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1290 __func__, inChannels, inFrameCount, size);
1291
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001292 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001293 || size > mInConversionBuffer->getSize())) {
1294 mInConversionBuffer.clear();
1295 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001296 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001297 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001298 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001299 mInConversionBuffer->setFrameCount(inFrameCount);
1300 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001301 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001302 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001303 }
1304 }
1305#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001306}
1307
1308void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001309 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001310
1311 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001312 if (buffer != 0) {
1313 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1314 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1315 } else {
1316 mConfig.outputCfg.buffer.raw = NULL;
1317 }
1318 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001319 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001320
1321#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001322 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001323 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001324 const uint32_t outChannelCount =
1325 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1326 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001327 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001328 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001329 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1330 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1331 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001332
1333 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1334 __func__, outChannels, outFrameCount, size);
1335
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001336 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001337 || size > mOutConversionBuffer->getSize())) {
1338 mOutConversionBuffer.clear();
1339 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001340 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001341 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001342 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001343 mOutConversionBuffer->setFrameCount(outFrameCount);
1344 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001345 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001346 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001347 }
1348 }
1349#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001350}
1351
Eric Laurentca7cc822012-11-19 14:55:58 -08001352status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1353{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001354 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001355 if (mStatus != NO_ERROR) {
1356 return mStatus;
1357 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001358 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001359 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1360 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1361 if (isProcessEnabled() &&
1362 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001363 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1364 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001365 uint32_t volume[2];
1366 uint32_t *pVolume = NULL;
1367 uint32_t size = sizeof(volume);
1368 volume[0] = *left;
1369 volume[1] = *right;
1370 if (controller) {
1371 pVolume = volume;
1372 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001373 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1374 size,
1375 volume,
1376 &size,
1377 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001378 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1379 *left = volume[0];
1380 *right = volume[1];
1381 }
1382 }
1383 return status;
1384}
1385
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001386void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1387{
Zhou Songd505c642020-02-20 16:35:37 +08001388 // for offload or direct thread, if the effect chain has non-offloadable
1389 // effect and any effect module within the chain has volume control, then
1390 // volume control is delegated to effect, otherwise, set volume to hal.
1391 if (mEffectCallback->isOffloadOrDirect() &&
1392 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001393 float vol_l = (float)left / (1 << 24);
1394 float vol_r = (float)right / (1 << 24);
Eric Laurent5d885392019-12-13 10:56:31 -08001395 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001396 }
1397}
1398
jiabinb8269fd2019-11-11 12:16:27 -08001399status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1400 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001401{
jiabinb8269fd2019-11-11 12:16:27 -08001402 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1403 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001404 return NO_ERROR;
1405 }
1406
1407 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001408 if (mStatus != NO_ERROR) {
1409 return mStatus;
1410 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001412 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 status_t cmdStatus;
1414 uint32_t size = sizeof(status_t);
jiabinb8269fd2019-11-11 12:16:27 -08001415 // FIXME: use audio device types and addresses when the hal interface is ready.
1416 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001417 sizeof(uint32_t),
jiabinb8269fd2019-11-11 12:16:27 -08001418 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001419 &size,
1420 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 }
1422 return status;
1423}
1424
jiabinb8269fd2019-11-11 12:16:27 -08001425status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1426{
1427 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1428}
1429
1430status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1431{
1432 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1433}
1434
Eric Laurentca7cc822012-11-19 14:55:58 -08001435status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1436{
1437 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001438 if (mStatus != NO_ERROR) {
1439 return mStatus;
1440 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001441 status_t status = NO_ERROR;
1442 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1443 status_t cmdStatus;
1444 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001445 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1446 sizeof(audio_mode_t),
1447 &mode,
1448 &size,
1449 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001450 if (status == NO_ERROR) {
1451 status = cmdStatus;
1452 }
1453 }
1454 return status;
1455}
1456
1457status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1458{
1459 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001460 if (mStatus != NO_ERROR) {
1461 return mStatus;
1462 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001463 status_t status = NO_ERROR;
1464 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1465 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001466 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1467 sizeof(audio_source_t),
1468 &source,
1469 &size,
1470 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001471 }
1472 return status;
1473}
1474
Eric Laurent5baf2af2013-09-12 17:37:00 -07001475status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1476{
1477 Mutex::Autolock _l(mLock);
1478 if (mStatus != NO_ERROR) {
1479 return mStatus;
1480 }
1481 status_t status = NO_ERROR;
1482 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1483 status_t cmdStatus;
1484 uint32_t size = sizeof(status_t);
1485 effect_offload_param_t cmd;
1486
1487 cmd.isOffload = offloaded;
1488 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001489 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1490 sizeof(effect_offload_param_t),
1491 &cmd,
1492 &size,
1493 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001494 if (status == NO_ERROR) {
1495 status = cmdStatus;
1496 }
1497 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1498 } else {
1499 if (offloaded) {
1500 status = INVALID_OPERATION;
1501 }
1502 mOffloaded = false;
1503 }
1504 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1505 return status;
1506}
1507
1508bool AudioFlinger::EffectModule::isOffloaded() const
1509{
1510 Mutex::Autolock _l(mLock);
1511 return mOffloaded;
1512}
1513
Andy Hungbded9c82017-11-30 18:47:35 -08001514static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1515 std::stringstream ss;
1516
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001517 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001518 return "nullptr"; // make different than below
1519 } else if (buffer->externalData() != nullptr) {
1520 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1521 << " -> "
1522 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1523 } else {
1524 ss << buffer->audioBuffer()->raw;
1525 }
1526 return ss.str();
1527}
Marco Nelissenb2208842014-02-07 14:00:50 -08001528
Eric Laurente0b9a362019-12-16 19:34:05 -08001529void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001530{
Eric Laurente0b9a362019-12-16 19:34:05 -08001531 EffectBase::dump(fd, args);
1532
Eric Laurentca7cc822012-11-19 14:55:58 -08001533 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001534 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001535
Eric Laurente0b9a362019-12-16 19:34:05 -08001536 result.append("\t\tStatus Engine:\n");
1537 result.appendFormat("\t\t%03d %p\n",
1538 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001539
1540 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001541
1542 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001543 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1544 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1545 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001546 mConfig.inputCfg.buffer.frameCount,
1547 mConfig.inputCfg.samplingRate,
1548 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001549 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001550 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001551
1552 result.append("\t\t- Output configuration:\n");
1553 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001554 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001555 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001556 mConfig.outputCfg.buffer.frameCount,
1557 mConfig.outputCfg.samplingRate,
1558 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001559 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001560 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001561
rago94a1ee82017-07-21 15:11:02 -07001562#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001563
Andy Hungbded9c82017-11-30 18:47:35 -08001564 result.appendFormat("\t\t- HAL buffers:\n"
1565 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1566 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1567 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1568 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1569 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001570#endif
1571
Eric Laurentca7cc822012-11-19 14:55:58 -08001572 write(fd, result.string(), result.length());
1573
Mikhail Naganov4d547672019-02-22 14:19:19 -08001574 if (mEffectInterface != 0) {
1575 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1576 (void)mEffectInterface->dump(fd);
1577 }
1578
Eric Laurentca7cc822012-11-19 14:55:58 -08001579 if (locked) {
1580 mLock.unlock();
1581 }
1582}
1583
1584// ----------------------------------------------------------------------------
1585// EffectHandle implementation
1586// ----------------------------------------------------------------------------
1587
1588#undef LOG_TAG
1589#define LOG_TAG "AudioFlinger::EffectHandle"
1590
Eric Laurente0b9a362019-12-16 19:34:05 -08001591AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001592 const sp<AudioFlinger::Client>& client,
1593 const sp<IEffectClient>& effectClient,
1594 int32_t priority)
1595 : BnEffect(),
1596 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001597 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001598{
Eric Laurent9b2064c2019-11-22 17:25:04 -08001599 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001600
1601 if (client == 0) {
1602 return;
1603 }
1604 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1605 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001606 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001607 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001608 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001609 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001610 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001611 return;
1612 }
Glenn Kastene75da402013-11-20 13:54:52 -08001613 new(mCblk) effect_param_cblk_t();
1614 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001615}
1616
1617AudioFlinger::EffectHandle::~EffectHandle()
1618{
1619 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001620 disconnect(false);
1621}
1622
Glenn Kastene75da402013-11-20 13:54:52 -08001623status_t AudioFlinger::EffectHandle::initCheck()
1624{
1625 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1626}
1627
Eric Laurentca7cc822012-11-19 14:55:58 -08001628status_t AudioFlinger::EffectHandle::enable()
1629{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001630 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001631 ALOGV("enable %p", this);
Eric Laurente0b9a362019-12-16 19:34:05 -08001632 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001633 if (effect == 0 || mDisconnected) {
1634 return DEAD_OBJECT;
1635 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001636 if (!mHasControl) {
1637 return INVALID_OPERATION;
1638 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001639
1640 if (mEnabled) {
1641 return NO_ERROR;
1642 }
1643
1644 mEnabled = true;
1645
Eric Laurent6c796322019-04-09 14:13:17 -07001646 status_t status = effect->updatePolicyState();
1647 if (status != NO_ERROR) {
1648 mEnabled = false;
1649 return status;
1650 }
1651
Eric Laurent5d885392019-12-13 10:56:31 -08001652 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001653
1654 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001655 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 return NO_ERROR;
1657 }
1658
Eric Laurent5d885392019-12-13 10:56:31 -08001659 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001661 mEnabled = false;
1662 }
1663 return status;
1664}
1665
1666status_t AudioFlinger::EffectHandle::disable()
1667{
1668 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001669 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001670 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001671 if (effect == 0 || mDisconnected) {
1672 return DEAD_OBJECT;
1673 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001674 if (!mHasControl) {
1675 return INVALID_OPERATION;
1676 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001677
1678 if (!mEnabled) {
1679 return NO_ERROR;
1680 }
1681 mEnabled = false;
1682
Eric Laurent6c796322019-04-09 14:13:17 -07001683 effect->updatePolicyState();
1684
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001685 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 return NO_ERROR;
1687 }
1688
Eric Laurent5d885392019-12-13 10:56:31 -08001689 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001690 return status;
1691}
1692
1693void AudioFlinger::EffectHandle::disconnect()
1694{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001695 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 disconnect(true);
1697}
1698
1699void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1700{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001701 AutoMutex _l(mLock);
1702 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1703 if (mDisconnected) {
1704 if (unpinIfLast) {
1705 android_errorWriteLog(0x534e4554, "32707507");
1706 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001707 return;
1708 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001709 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001710 {
Eric Laurente0b9a362019-12-16 19:34:05 -08001711 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001712 if (effect != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001713 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001714 ALOGW("%s Effect handle %p disconnected after thread destruction",
1715 __func__, this);
1716 }
1717 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001718 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001719 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001720
Eric Laurentca7cc822012-11-19 14:55:58 -08001721 if (mClient != 0) {
1722 if (mCblk != NULL) {
1723 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1724 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1725 }
1726 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001727 // Client destructor must run with AudioFlinger client mutex locked
1728 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001729 mClient.clear();
1730 }
1731}
1732
1733status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1734 uint32_t cmdSize,
1735 void *pCmdData,
1736 uint32_t *replySize,
1737 void *pReplyData)
1738{
1739 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001740 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001741
Eric Laurentc7ab3092017-06-15 18:43:46 -07001742 // reject commands reserved for internal use by audio framework if coming from outside
1743 // of audioserver
1744 switch(cmdCode) {
1745 case EFFECT_CMD_ENABLE:
1746 case EFFECT_CMD_DISABLE:
1747 case EFFECT_CMD_SET_PARAM:
1748 case EFFECT_CMD_SET_PARAM_DEFERRED:
1749 case EFFECT_CMD_SET_PARAM_COMMIT:
1750 case EFFECT_CMD_GET_PARAM:
1751 break;
1752 default:
1753 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1754 break;
1755 }
1756 android_errorWriteLog(0x534e4554, "62019992");
1757 return BAD_VALUE;
1758 }
1759
Eric Laurent1ffc5852016-12-15 14:46:09 -08001760 if (cmdCode == EFFECT_CMD_ENABLE) {
1761 if (*replySize < sizeof(int)) {
1762 android_errorWriteLog(0x534e4554, "32095713");
1763 return BAD_VALUE;
1764 }
1765 *(int *)pReplyData = NO_ERROR;
1766 *replySize = sizeof(int);
1767 return enable();
1768 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1769 if (*replySize < sizeof(int)) {
1770 android_errorWriteLog(0x534e4554, "32095713");
1771 return BAD_VALUE;
1772 }
1773 *(int *)pReplyData = NO_ERROR;
1774 *replySize = sizeof(int);
1775 return disable();
1776 }
1777
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001778 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001779 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001780 if (effect == 0 || mDisconnected) {
1781 return DEAD_OBJECT;
1782 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001783 // only get parameter command is permitted for applications not controlling the effect
1784 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1785 return INVALID_OPERATION;
1786 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001787
1788 // handle commands that are not forwarded transparently to effect engine
1789 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent9b2064c2019-11-22 17:25:04 -08001790 if (mClient == 0) {
1791 return INVALID_OPERATION;
1792 }
1793
Eric Laurent1ffc5852016-12-15 14:46:09 -08001794 if (*replySize < sizeof(int)) {
1795 android_errorWriteLog(0x534e4554, "32095713");
1796 return BAD_VALUE;
1797 }
1798 *(int *)pReplyData = NO_ERROR;
1799 *replySize = sizeof(int);
1800
Eric Laurentca7cc822012-11-19 14:55:58 -08001801 // No need to trylock() here as this function is executed in the binder thread serving a
1802 // particular client process: no risk to block the whole media server process or mixer
1803 // threads if we are stuck here
1804 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001805 // keep local copy of index in case of client corruption b/32220769
1806 const uint32_t clientIndex = mCblk->clientIndex;
1807 const uint32_t serverIndex = mCblk->serverIndex;
1808 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1809 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001810 mCblk->serverIndex = 0;
1811 mCblk->clientIndex = 0;
1812 return BAD_VALUE;
1813 }
1814 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001815 effect_param_t *param = NULL;
1816 for (uint32_t index = serverIndex; index < clientIndex;) {
1817 int *p = (int *)(mBuffer + index);
1818 const int size = *p++;
1819 if (size < 0
1820 || size > EFFECT_PARAM_BUFFER_SIZE
1821 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001822 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001823 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001824 break;
1825 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001826
1827 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001828 auto *newParam = (effect_param_t *)realloc(param, size);
1829 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001830 ALOGW("command(): out of memory");
1831 status = NO_MEMORY;
1832 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001833 }
George Burgess IV80a22162020-01-05 20:06:15 -08001834 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001835 memcpy(param, p, size);
1836
1837 int reply = 0;
1838 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001839 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001840 size,
1841 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001842 &rsize,
1843 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001844
1845 // verify shared memory: server index shouldn't change; client index can't go back.
1846 if (serverIndex != mCblk->serverIndex
1847 || clientIndex > mCblk->clientIndex) {
1848 android_errorWriteLog(0x534e4554, "32220769");
1849 status = BAD_VALUE;
1850 break;
1851 }
1852
Eric Laurentca7cc822012-11-19 14:55:58 -08001853 // stop at first error encountered
1854 if (ret != NO_ERROR) {
1855 status = ret;
1856 *(int *)pReplyData = reply;
1857 break;
1858 } else if (reply != NO_ERROR) {
1859 *(int *)pReplyData = reply;
1860 break;
1861 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001862 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001863 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001864 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001865 mCblk->serverIndex = 0;
1866 mCblk->clientIndex = 0;
1867 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 }
1869
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001870 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001871}
1872
1873void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1874{
1875 ALOGV("setControl %p control %d", this, hasControl);
1876
1877 mHasControl = hasControl;
1878 mEnabled = enabled;
1879
1880 if (signal && mEffectClient != 0) {
1881 mEffectClient->controlStatusChanged(hasControl);
1882 }
1883}
1884
1885void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1886 uint32_t cmdSize,
1887 void *pCmdData,
1888 uint32_t replySize,
1889 void *pReplyData)
1890{
1891 if (mEffectClient != 0) {
1892 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1893 }
1894}
1895
1896
1897
1898void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1899{
1900 if (mEffectClient != 0) {
1901 mEffectClient->enableStatusChanged(enabled);
1902 }
1903}
1904
1905status_t AudioFlinger::EffectHandle::onTransact(
1906 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1907{
1908 return BnEffect::onTransact(code, data, reply, flags);
1909}
1910
1911
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001912void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001913{
1914 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1915
Marco Nelissenb2208842014-02-07 14:00:50 -08001916 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001917 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001918 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001919 mHasControl ? "yes" : "no",
1920 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001921 mCblk ? mCblk->clientIndex : 0,
1922 mCblk ? mCblk->serverIndex : 0
1923 );
1924
1925 if (locked) {
1926 mCblk->lock.unlock();
1927 }
1928}
1929
1930#undef LOG_TAG
1931#define LOG_TAG "AudioFlinger::EffectChain"
1932
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001933AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1934 audio_session_t sessionId)
Eric Laurent5d885392019-12-13 10:56:31 -08001935 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001936 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent5d885392019-12-13 10:56:31 -08001937 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001938 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08001939{
1940 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001941 sp<ThreadBase> p = thread.promote();
1942 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001943 return;
1944 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001945 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
1946 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08001947}
1948
1949AudioFlinger::EffectChain::~EffectChain()
1950{
Eric Laurentca7cc822012-11-19 14:55:58 -08001951}
1952
1953// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1954sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1955 effect_descriptor_t *descriptor)
1956{
1957 size_t size = mEffects.size();
1958
1959 for (size_t i = 0; i < size; i++) {
1960 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1961 return mEffects[i];
1962 }
1963 }
1964 return 0;
1965}
1966
1967// getEffectFromId_l() must be called with ThreadBase::mLock held
1968sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1969{
1970 size_t size = mEffects.size();
1971
1972 for (size_t i = 0; i < size; i++) {
1973 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1974 if (id == 0 || mEffects[i]->id() == id) {
1975 return mEffects[i];
1976 }
1977 }
1978 return 0;
1979}
1980
1981// getEffectFromType_l() must be called with ThreadBase::mLock held
1982sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1983 const effect_uuid_t *type)
1984{
1985 size_t size = mEffects.size();
1986
1987 for (size_t i = 0; i < size; i++) {
1988 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1989 return mEffects[i];
1990 }
1991 }
1992 return 0;
1993}
1994
Eric Laurent6c796322019-04-09 14:13:17 -07001995std::vector<int> AudioFlinger::EffectChain::getEffectIds()
1996{
1997 std::vector<int> ids;
1998 Mutex::Autolock _l(mLock);
1999 for (size_t i = 0; i < mEffects.size(); i++) {
2000 ids.push_back(mEffects[i]->id());
2001 }
2002 return ids;
2003}
2004
Eric Laurentca7cc822012-11-19 14:55:58 -08002005void AudioFlinger::EffectChain::clearInputBuffer()
2006{
2007 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002008 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002009}
2010
2011// Must be called with EffectChain::mLock locked
Eric Laurent5d885392019-12-13 10:56:31 -08002012void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002013{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002014 if (mInBuffer == NULL) {
2015 return;
2016 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002017 const size_t frameSize =
Eric Laurent5d885392019-12-13 10:56:31 -08002018 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002019
Eric Laurent5d885392019-12-13 10:56:31 -08002020 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002021 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002022}
2023
2024// Must be called with EffectChain::mLock locked
2025void AudioFlinger::EffectChain::process_l()
2026{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002027 // never process effects when:
2028 // - on an OFFLOAD thread
2029 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent5d885392019-12-13 10:56:31 -08002030 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurenta20c4e92019-11-12 15:55:51 -08002031 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002032 bool tracksOnSession = (trackCnt() != 0);
2033
2034 if (!tracksOnSession && mTailBufferCount == 0) {
2035 doProcess = false;
2036 }
2037
2038 if (activeTrackCnt() == 0) {
2039 // if no track is active and the effect tail has not been rendered,
2040 // the input buffer must be cleared here as the mixer process will not do it
2041 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08002042 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002043 if (mTailBufferCount > 0) {
2044 mTailBufferCount--;
2045 }
2046 }
2047 }
2048 }
2049
2050 size_t size = mEffects.size();
2051 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002052 // Only the input and output buffers of the chain can be external,
2053 // and 'update' / 'commit' do nothing for allocated buffers, thus
2054 // it's not needed to consider any other buffers here.
2055 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002056 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2057 mOutBuffer->update();
2058 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002059 for (size_t i = 0; i < size; i++) {
2060 mEffects[i]->process();
2061 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002062 mInBuffer->commit();
2063 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2064 mOutBuffer->commit();
2065 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002066 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002067 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002068 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002069 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2070 }
2071 if (doResetVolume) {
2072 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 }
2074}
2075
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002076// createEffect_l() must be called with ThreadBase::mLock held
2077status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002078 effect_descriptor_t *desc,
2079 int id,
2080 audio_session_t sessionId,
2081 bool pinned)
2082{
2083 Mutex::Autolock _l(mLock);
Eric Laurent9b2064c2019-11-22 17:25:04 -08002084 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002085 status_t lStatus = effect->status();
2086 if (lStatus == NO_ERROR) {
2087 lStatus = addEffect_ll(effect);
2088 }
2089 if (lStatus != NO_ERROR) {
2090 effect.clear();
2091 }
2092 return lStatus;
2093}
2094
2095// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002096status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2097{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002098 Mutex::Autolock _l(mLock);
2099 return addEffect_ll(effect);
2100}
2101// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2102status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2103{
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 effect_descriptor_t desc = effect->desc();
2105 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2106
Eric Laurent5d885392019-12-13 10:56:31 -08002107 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002108
2109 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2110 // Auxiliary effects are inserted at the beginning of mEffects vector as
2111 // they are processed first and accumulated in chain input buffer
2112 mEffects.insertAt(effect, 0);
2113
2114 // the input buffer for auxiliary effect contains mono samples in
2115 // 32 bit format. This is to avoid saturation in AudoMixer
2116 // accumulation stage. Saturation is done in EffectModule::process() before
2117 // calling the process in effect engine
Eric Laurent5d885392019-12-13 10:56:31 -08002118 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002119 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002120#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent5d885392019-12-13 10:56:31 -08002121 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002122 numSamples * sizeof(float), &halBuffer);
2123#else
Eric Laurent5d885392019-12-13 10:56:31 -08002124 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002125 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002126#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002127 if (result != OK) return result;
2128 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002129 // auxiliary effects output samples to chain input buffer for further processing
2130 // by insert effects
2131 effect->setOutBuffer(mInBuffer);
2132 } else {
2133 // Insert effects are inserted at the end of mEffects vector as they are processed
2134 // after track and auxiliary effects.
2135 // Insert effect order as a function of indicated preference:
2136 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2137 // another effect is present
2138 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2139 // last effect claiming first position
2140 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2141 // first effect claiming last position
2142 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2143 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2144 // already present
2145
2146 size_t size = mEffects.size();
2147 size_t idx_insert = size;
2148 ssize_t idx_insert_first = -1;
2149 ssize_t idx_insert_last = -1;
2150
2151 for (size_t i = 0; i < size; i++) {
2152 effect_descriptor_t d = mEffects[i]->desc();
2153 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2154 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2155 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2156 // check invalid effect chaining combinations
2157 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2158 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2159 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2160 desc.name, d.name);
2161 return INVALID_OPERATION;
2162 }
2163 // remember position of first insert effect and by default
2164 // select this as insert position for new effect
2165 if (idx_insert == size) {
2166 idx_insert = i;
2167 }
2168 // remember position of last insert effect claiming
2169 // first position
2170 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2171 idx_insert_first = i;
2172 }
2173 // remember position of first insert effect claiming
2174 // last position
2175 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2176 idx_insert_last == -1) {
2177 idx_insert_last = i;
2178 }
2179 }
2180 }
2181
2182 // modify idx_insert from first position if needed
2183 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2184 if (idx_insert_last != -1) {
2185 idx_insert = idx_insert_last;
2186 } else {
2187 idx_insert = size;
2188 }
2189 } else {
2190 if (idx_insert_first != -1) {
2191 idx_insert = idx_insert_first + 1;
2192 }
2193 }
2194
2195 // always read samples from chain input buffer
2196 effect->setInBuffer(mInBuffer);
2197
2198 // if last effect in the chain, output samples to chain
2199 // output buffer, otherwise to chain input buffer
2200 if (idx_insert == size) {
2201 if (idx_insert != 0) {
2202 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2203 mEffects[idx_insert-1]->configure();
2204 }
2205 effect->setOutBuffer(mOutBuffer);
2206 } else {
2207 effect->setOutBuffer(mInBuffer);
2208 }
2209 mEffects.insertAt(effect, idx_insert);
2210
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002211 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002212 idx_insert);
2213 }
2214 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002215
Eric Laurentca7cc822012-11-19 14:55:58 -08002216 return NO_ERROR;
2217}
2218
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002219// removeEffect_l() must be called with ThreadBase::mLock held
2220size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2221 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002222{
2223 Mutex::Autolock _l(mLock);
2224 size_t size = mEffects.size();
2225 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2226
2227 for (size_t i = 0; i < size; i++) {
2228 if (effect == mEffects[i]) {
2229 // calling stop here will remove pre-processing effect from the audio HAL.
2230 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2231 // the middle of a read from audio HAL
2232 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2233 mEffects[i]->state() == EffectModule::STOPPING) {
2234 mEffects[i]->stop();
2235 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002236 if (release) {
2237 mEffects[i]->release_l();
2238 }
2239
Mikhail Naganov022b9952017-01-04 16:36:51 -08002240 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002241 if (i == size - 1 && i != 0) {
2242 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2243 mEffects[i - 1]->configure();
2244 }
2245 }
2246 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002247 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002248 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002249
Eric Laurentca7cc822012-11-19 14:55:58 -08002250 break;
2251 }
2252 }
2253
2254 return mEffects.size();
2255}
2256
jiabinb8269fd2019-11-11 12:16:27 -08002257// setDevices_l() must be called with ThreadBase::mLock held
2258void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002259{
2260 size_t size = mEffects.size();
2261 for (size_t i = 0; i < size; i++) {
jiabinb8269fd2019-11-11 12:16:27 -08002262 mEffects[i]->setDevices(devices);
2263 }
2264}
2265
2266// setInputDevice_l() must be called with ThreadBase::mLock held
2267void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2268{
2269 size_t size = mEffects.size();
2270 for (size_t i = 0; i < size; i++) {
2271 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002272 }
2273}
2274
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002275// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002276void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2277{
2278 size_t size = mEffects.size();
2279 for (size_t i = 0; i < size; i++) {
2280 mEffects[i]->setMode(mode);
2281 }
2282}
2283
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002284// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002285void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2286{
2287 size_t size = mEffects.size();
2288 for (size_t i = 0; i < size; i++) {
2289 mEffects[i]->setAudioSource(source);
2290 }
2291}
2292
Zhou Songd505c642020-02-20 16:35:37 +08002293bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2294 for (const auto &effect : mEffects) {
2295 if (effect->isVolumeControlEnabled()) return true;
2296 }
2297 return false;
2298}
2299
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002300// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002301bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002302{
2303 uint32_t newLeft = *left;
2304 uint32_t newRight = *right;
2305 bool hasControl = false;
2306 int ctrlIdx = -1;
2307 size_t size = mEffects.size();
2308
2309 // first update volume controller
2310 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002311 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 ctrlIdx = i - 1;
2313 hasControl = true;
2314 break;
2315 }
2316 }
2317
Eric Laurentfa1e1232016-08-02 19:01:49 -07002318 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002319 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002320 if (hasControl) {
2321 *left = mNewLeftVolume;
2322 *right = mNewRightVolume;
2323 }
2324 return hasControl;
2325 }
2326
2327 mVolumeCtrlIdx = ctrlIdx;
2328 mLeftVolume = newLeft;
2329 mRightVolume = newRight;
2330
2331 // second get volume update from volume controller
2332 if (ctrlIdx >= 0) {
2333 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2334 mNewLeftVolume = newLeft;
2335 mNewRightVolume = newRight;
2336 }
2337 // then indicate volume to all other effects in chain.
2338 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002339 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002340 uint32_t lVol = newLeft;
2341 uint32_t rVol = newRight;
2342
2343 for (size_t i = 0; i < size; i++) {
2344 if ((int)i == ctrlIdx) {
2345 continue;
2346 }
2347 // this also works for ctrlIdx == -1 when there is no volume controller
2348 if ((int)i > ctrlIdx) {
2349 lVol = *left;
2350 rVol = *right;
2351 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002352 // Pass requested volume directly if this is volume monitor module
2353 if (mEffects[i]->isVolumeMonitor()) {
2354 mEffects[i]->setVolume(left, right, false);
2355 } else {
2356 mEffects[i]->setVolume(&lVol, &rVol, false);
2357 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002358 }
2359 *left = newLeft;
2360 *right = newRight;
2361
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002362 setVolumeForOutput_l(*left, *right);
2363
Eric Laurentca7cc822012-11-19 14:55:58 -08002364 return hasControl;
2365}
2366
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002367// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002368void AudioFlinger::EffectChain::resetVolume_l()
2369{
Eric Laurente7449bf2016-08-03 18:44:07 -07002370 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2371 uint32_t left = mLeftVolume;
2372 uint32_t right = mRightVolume;
2373 (void)setVolume_l(&left, &right, true);
2374 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002375}
2376
Eric Laurent1b928682014-10-02 19:41:47 -07002377void AudioFlinger::EffectChain::syncHalEffectsState()
2378{
2379 Mutex::Autolock _l(mLock);
2380 for (size_t i = 0; i < mEffects.size(); i++) {
2381 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2382 mEffects[i]->state() == EffectModule::STOPPING) {
2383 mEffects[i]->addEffectToHal_l();
2384 }
2385 }
2386}
2387
Eric Laurentca7cc822012-11-19 14:55:58 -08002388void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2389{
Eric Laurentca7cc822012-11-19 14:55:58 -08002390 String8 result;
2391
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002392 const size_t numEffects = mEffects.size();
2393 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002394
Marco Nelissenb2208842014-02-07 14:00:50 -08002395 if (numEffects) {
2396 bool locked = AudioFlinger::dumpTryLock(mLock);
2397 // failed to lock - AudioFlinger is probably deadlocked
2398 if (!locked) {
2399 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002400 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002401
Andy Hungbded9c82017-11-30 18:47:35 -08002402 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2403 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2404 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2405 (int)inBufferStr.size(), "In buffer ",
2406 (int)outBufferStr.size(), "Out buffer ");
2407 result.appendFormat("\t%s %s %d\n",
2408 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002409 write(fd, result.string(), result.size());
2410
2411 for (size_t i = 0; i < numEffects; ++i) {
2412 sp<EffectModule> effect = mEffects[i];
2413 if (effect != 0) {
2414 effect->dump(fd, args);
2415 }
2416 }
2417
2418 if (locked) {
2419 mLock.unlock();
2420 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002421 } else {
2422 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002423 }
2424}
2425
2426// must be called with ThreadBase::mLock held
2427void AudioFlinger::EffectChain::setEffectSuspended_l(
2428 const effect_uuid_t *type, bool suspend)
2429{
2430 sp<SuspendedEffectDesc> desc;
2431 // use effect type UUID timelow as key as there is no real risk of identical
2432 // timeLow fields among effect type UUIDs.
2433 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2434 if (suspend) {
2435 if (index >= 0) {
2436 desc = mSuspendedEffects.valueAt(index);
2437 } else {
2438 desc = new SuspendedEffectDesc();
2439 desc->mType = *type;
2440 mSuspendedEffects.add(type->timeLow, desc);
2441 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2442 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002443
Eric Laurentca7cc822012-11-19 14:55:58 -08002444 if (desc->mRefCount++ == 0) {
2445 sp<EffectModule> effect = getEffectIfEnabled(type);
2446 if (effect != 0) {
2447 desc->mEffect = effect;
2448 effect->setSuspended(true);
Eric Laurent5d885392019-12-13 10:56:31 -08002449 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002450 }
2451 }
2452 } else {
2453 if (index < 0) {
2454 return;
2455 }
2456 desc = mSuspendedEffects.valueAt(index);
2457 if (desc->mRefCount <= 0) {
2458 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002459 desc->mRefCount = 0;
2460 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002461 }
2462 if (--desc->mRefCount == 0) {
2463 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2464 if (desc->mEffect != 0) {
2465 sp<EffectModule> effect = desc->mEffect.promote();
2466 if (effect != 0) {
2467 effect->setSuspended(false);
2468 effect->lock();
2469 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002470 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002471 effect->setEnabled_l(handle->enabled());
2472 }
2473 effect->unlock();
2474 }
2475 desc->mEffect.clear();
2476 }
2477 mSuspendedEffects.removeItemsAt(index);
2478 }
2479 }
2480}
2481
2482// must be called with ThreadBase::mLock held
2483void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2484{
2485 sp<SuspendedEffectDesc> desc;
2486
2487 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2488 if (suspend) {
2489 if (index >= 0) {
2490 desc = mSuspendedEffects.valueAt(index);
2491 } else {
2492 desc = new SuspendedEffectDesc();
2493 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2494 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2495 }
2496 if (desc->mRefCount++ == 0) {
2497 Vector< sp<EffectModule> > effects;
2498 getSuspendEligibleEffects(effects);
2499 for (size_t i = 0; i < effects.size(); i++) {
2500 setEffectSuspended_l(&effects[i]->desc().type, true);
2501 }
2502 }
2503 } else {
2504 if (index < 0) {
2505 return;
2506 }
2507 desc = mSuspendedEffects.valueAt(index);
2508 if (desc->mRefCount <= 0) {
2509 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2510 desc->mRefCount = 1;
2511 }
2512 if (--desc->mRefCount == 0) {
2513 Vector<const effect_uuid_t *> types;
2514 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2515 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2516 continue;
2517 }
2518 types.add(&mSuspendedEffects.valueAt(i)->mType);
2519 }
2520 for (size_t i = 0; i < types.size(); i++) {
2521 setEffectSuspended_l(types[i], false);
2522 }
2523 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2524 mSuspendedEffects.keyAt(index));
2525 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2526 }
2527 }
2528}
2529
2530
2531// The volume effect is used for automated tests only
2532#ifndef OPENSL_ES_H_
2533static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2534 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2535const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2536#endif //OPENSL_ES_H_
2537
Eric Laurentd8365c52017-07-16 15:27:05 -07002538/* static */
2539bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2540{
2541 // Only NS and AEC are suspended when BtNRec is off
2542 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2543 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2544 return true;
2545 }
2546 return false;
2547}
2548
Eric Laurentca7cc822012-11-19 14:55:58 -08002549bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2550{
2551 // auxiliary effects and visualizer are never suspended on output mix
2552 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2553 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2554 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002555 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2556 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002557 return false;
2558 }
2559 return true;
2560}
2561
2562void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2563 Vector< sp<AudioFlinger::EffectModule> > &effects)
2564{
2565 effects.clear();
2566 for (size_t i = 0; i < mEffects.size(); i++) {
2567 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2568 effects.add(mEffects[i]);
2569 }
2570 }
2571}
2572
2573sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2574 const effect_uuid_t *type)
2575{
2576 sp<EffectModule> effect = getEffectFromType_l(type);
2577 return effect != 0 && effect->isEnabled() ? effect : 0;
2578}
2579
2580void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2581 bool enabled)
2582{
2583 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2584 if (enabled) {
2585 if (index < 0) {
2586 // if the effect is not suspend check if all effects are suspended
2587 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2588 if (index < 0) {
2589 return;
2590 }
2591 if (!isEffectEligibleForSuspend(effect->desc())) {
2592 return;
2593 }
2594 setEffectSuspended_l(&effect->desc().type, enabled);
2595 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2596 if (index < 0) {
2597 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2598 return;
2599 }
2600 }
2601 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2602 effect->desc().type.timeLow);
2603 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002604 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002605 if (desc->mEffect == 0) {
2606 desc->mEffect = effect;
Eric Laurent5d885392019-12-13 10:56:31 -08002607 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002608 effect->setSuspended(true);
2609 }
2610 } else {
2611 if (index < 0) {
2612 return;
2613 }
2614 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2615 effect->desc().type.timeLow);
2616 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2617 desc->mEffect.clear();
2618 effect->setSuspended(false);
2619 }
2620}
2621
Eric Laurent5baf2af2013-09-12 17:37:00 -07002622bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002623{
2624 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002625 return isNonOffloadableEnabled_l();
2626}
2627
2628bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2629{
Eric Laurent813e2a72013-08-31 12:59:48 -07002630 size_t size = mEffects.size();
2631 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002632 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002633 return true;
2634 }
2635 }
2636 return false;
2637}
2638
Eric Laurentaaa44472014-09-12 17:41:50 -07002639void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2640{
2641 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002642 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002643}
2644
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002645void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2646{
2647 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2648 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2649 }
2650 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2651 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2652 }
2653}
2654
2655void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2656{
2657 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2658 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2659 }
2660 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2661 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2662 }
2663}
2664
2665bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002666{
2667 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002668 for (const auto &effect : mEffects) {
2669 if (effect->isProcessImplemented()) {
2670 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002671 }
2672 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002673 // Allow effects without processing.
2674 return true;
2675}
2676
2677bool AudioFlinger::EffectChain::isFastCompatible() const
2678{
2679 Mutex::Autolock _l(mLock);
2680 for (const auto &effect : mEffects) {
2681 if (effect->isProcessImplemented()
2682 && effect->isImplementationSoftware()) {
2683 return false;
2684 }
2685 }
2686 // Allow effects without processing or hw accelerated effects.
2687 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002688}
2689
2690// isCompatibleWithThread_l() must be called with thread->mLock held
2691bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2692{
2693 Mutex::Autolock _l(mLock);
2694 for (size_t i = 0; i < mEffects.size(); i++) {
2695 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2696 return false;
2697 }
2698 }
2699 return true;
2700}
2701
Eric Laurent5d885392019-12-13 10:56:31 -08002702// EffectCallbackInterface implementation
2703status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2704 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2705 sp<EffectHalInterface> *effect) {
2706 status_t status = NO_INIT;
2707 sp<AudioFlinger> af = mAudioFlinger.promote();
2708 if (af == nullptr) {
2709 return status;
2710 }
2711 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2712 if (effectsFactory != 0) {
2713 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2714 }
2715 return status;
2716}
2717
2718bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurente0b9a362019-12-16 19:34:05 -08002719 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002720 sp<AudioFlinger> af = mAudioFlinger.promote();
2721 if (af == nullptr) {
2722 return false;
2723 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002724 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2725 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002726}
2727
2728status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2729 size_t size, sp<EffectBufferHalInterface>* buffer) {
2730 sp<AudioFlinger> af = mAudioFlinger.promote();
2731 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2732 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2733}
2734
2735status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2736 sp<EffectHalInterface> effect) {
2737 status_t result = NO_INIT;
2738 sp<ThreadBase> t = mThread.promote();
2739 if (t == nullptr) {
2740 return result;
2741 }
2742 sp <StreamHalInterface> st = t->stream();
2743 if (st == nullptr) {
2744 return result;
2745 }
2746 result = st->addEffect(effect);
2747 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2748 return result;
2749}
2750
2751status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2752 sp<EffectHalInterface> effect) {
2753 status_t result = NO_INIT;
2754 sp<ThreadBase> t = mThread.promote();
2755 if (t == nullptr) {
2756 return result;
2757 }
2758 sp <StreamHalInterface> st = t->stream();
2759 if (st == nullptr) {
2760 return result;
2761 }
2762 result = st->removeEffect(effect);
2763 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2764 return result;
2765}
2766
2767audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2768 sp<ThreadBase> t = mThread.promote();
2769 if (t == nullptr) {
2770 return AUDIO_IO_HANDLE_NONE;
2771 }
2772 return t->id();
2773}
2774
2775bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2776 sp<ThreadBase> t = mThread.promote();
2777 if (t == nullptr) {
2778 return true;
2779 }
2780 return t->isOutput();
2781}
2782
2783bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2784 sp<ThreadBase> t = mThread.promote();
2785 if (t == nullptr) {
2786 return false;
2787 }
2788 return t->type() == ThreadBase::OFFLOAD;
2789}
2790
2791bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2792 sp<ThreadBase> t = mThread.promote();
2793 if (t == nullptr) {
2794 return false;
2795 }
2796 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2797}
2798
2799bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2800 sp<ThreadBase> t = mThread.promote();
2801 if (t == nullptr) {
2802 return false;
2803 }
Andy Hungea840382020-05-05 21:50:17 -07002804 return t->isOffloadOrMmap();
Eric Laurent5d885392019-12-13 10:56:31 -08002805}
2806
2807uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2808 sp<ThreadBase> t = mThread.promote();
2809 if (t == nullptr) {
2810 return 0;
2811 }
2812 return t->sampleRate();
2813}
2814
2815audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2816 sp<ThreadBase> t = mThread.promote();
2817 if (t == nullptr) {
2818 return AUDIO_CHANNEL_NONE;
2819 }
2820 return t->channelMask();
2821}
2822
2823uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2824 sp<ThreadBase> t = mThread.promote();
2825 if (t == nullptr) {
2826 return 0;
2827 }
2828 return t->channelCount();
2829}
2830
2831size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2832 sp<ThreadBase> t = mThread.promote();
2833 if (t == nullptr) {
2834 return 0;
2835 }
2836 return t->frameCount();
2837}
2838
2839uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2840 sp<ThreadBase> t = mThread.promote();
2841 if (t == nullptr) {
2842 return 0;
2843 }
2844 return t->latency_l();
2845}
2846
2847void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2848 sp<ThreadBase> t = mThread.promote();
2849 if (t == nullptr) {
2850 return;
2851 }
2852 t->setVolumeForOutput_l(left, right);
2853}
2854
2855void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurente0b9a362019-12-16 19:34:05 -08002856 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent5d885392019-12-13 10:56:31 -08002857 sp<ThreadBase> t = mThread.promote();
2858 if (t == nullptr) {
2859 return;
2860 }
2861 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2862
2863 sp<EffectChain> c = mChain.promote();
2864 if (c == nullptr) {
2865 return;
2866 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002867 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2868 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent5d885392019-12-13 10:56:31 -08002869}
2870
Eric Laurente0b9a362019-12-16 19:34:05 -08002871void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002872 sp<ThreadBase> t = mThread.promote();
2873 if (t == nullptr) {
2874 return;
2875 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002876 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2877 t->onEffectEnable(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002878}
2879
Eric Laurente0b9a362019-12-16 19:34:05 -08002880void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002881 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2882
2883 sp<ThreadBase> t = mThread.promote();
2884 if (t == nullptr) {
2885 return;
2886 }
2887 t->onEffectDisable();
2888}
2889
2890bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2891 bool unpinIfLast) {
2892 sp<ThreadBase> t = mThread.promote();
2893 if (t == nullptr) {
2894 return false;
2895 }
2896 t->disconnectEffectHandle(handle, unpinIfLast);
2897 return true;
2898}
2899
2900void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2901 sp<EffectChain> c = mChain.promote();
2902 if (c == nullptr) {
2903 return;
2904 }
2905 c->resetVolume_l();
2906
2907}
2908
2909uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2910 sp<EffectChain> c = mChain.promote();
2911 if (c == nullptr) {
2912 return PRODUCT_STRATEGY_NONE;
2913 }
2914 return c->strategy();
2915}
2916
2917int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2918 sp<EffectChain> c = mChain.promote();
2919 if (c == nullptr) {
2920 return 0;
2921 }
2922 return c->activeTrackCnt();
2923}
2924
Eric Laurent9b2064c2019-11-22 17:25:04 -08002925
2926#undef LOG_TAG
2927#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2928
2929status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2930{
2931 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2932 Mutex::Autolock _l(mProxyLock);
2933 if (status == NO_ERROR) {
2934 for (auto& handle : mEffectHandles) {
2935 if (enabled) {
2936 status = handle.second->enable();
2937 } else {
2938 status = handle.second->disable();
2939 }
2940 }
2941 }
2942 ALOGV("%s enable %d status %d", __func__, enabled, status);
2943 return status;
2944}
2945
2946status_t AudioFlinger::DeviceEffectProxy::init(
2947 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2948//For all audio patches
2949//If src or sink device match
2950//If the effect is HW accelerated
2951// if no corresponding effect module
2952// Create EffectModule: mHalEffect
2953//Create and attach EffectHandle
2954//If the effect is not HW accelerated and the patch sink or src is a mixer port
2955// Create Effect on patch input or output thread on session -1
2956//Add EffectHandle to EffectHandle map of Effect Proxy:
2957 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2958 status_t status = NO_ERROR;
2959 for (auto &patch : patches) {
2960 status = onCreatePatch(patch.first, patch.second);
2961 ALOGV("%s onCreatePatch status %d", __func__, status);
2962 if (status == BAD_VALUE) {
2963 return status;
2964 }
2965 }
2966 return status;
2967}
2968
2969status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2970 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2971 status_t status = NAME_NOT_FOUND;
2972 sp<EffectHandle> handle;
2973 // only consider source[0] as this is the only "true" source of a patch
2974 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2975 ALOGV("%s source checkPort status %d", __func__, status);
2976 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2977 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2978 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2979 }
2980 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2981 Mutex::Autolock _l(mProxyLock);
2982 mEffectHandles.emplace(patchHandle, handle);
2983 }
2984 ALOGW_IF(status == BAD_VALUE,
2985 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2986
2987 return status;
2988}
2989
2990status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
2991 const struct audio_port_config *port, sp <EffectHandle> *handle) {
2992
2993 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
2994 __func__, port->type, port->ext.device.type,
2995 port->ext.device.address, port->id, patch.isSoftware());
2996 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
2997 || port->ext.device.address != mDevice.mAddress) {
2998 return NAME_NOT_FOUND;
2999 }
3000 status_t status = NAME_NOT_FOUND;
3001
3002 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3003 Mutex::Autolock _l(mProxyLock);
3004 mDevicePort = *port;
3005 mHalEffect = new EffectModule(mMyCallback,
3006 const_cast<effect_descriptor_t *>(&mDescriptor),
3007 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3008 false /* pinned */, port->id);
3009 if (audio_is_input_device(mDevice.mType)) {
3010 mHalEffect->setInputDevice(mDevice);
3011 } else {
3012 mHalEffect->setDevices({mDevice});
3013 }
3014 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3015 status = (*handle)->initCheck();
3016 if (status == OK) {
3017 status = mHalEffect->addHandle((*handle).get());
3018 } else {
3019 mHalEffect.clear();
3020 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3021 }
3022 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3023 sp <ThreadBase> thread;
3024 if (audio_port_config_has_input_direction(port)) {
3025 if (patch.isSoftware()) {
3026 thread = patch.mRecord.thread();
3027 } else {
3028 thread = patch.thread().promote();
3029 }
3030 } else {
3031 if (patch.isSoftware()) {
3032 thread = patch.mPlayback.thread();
3033 } else {
3034 thread = patch.thread().promote();
3035 }
3036 }
3037 int enabled;
3038 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3039 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003040 &enabled, &status, false, false /*probe*/);
Eric Laurent9b2064c2019-11-22 17:25:04 -08003041 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3042 } else {
3043 status = BAD_VALUE;
3044 }
3045 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3046 if (isEnabled()) {
3047 (*handle)->enable();
3048 } else {
3049 (*handle)->disable();
3050 }
3051 }
3052 return status;
3053}
3054
3055void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3056 Mutex::Autolock _l(mProxyLock);
3057 mEffectHandles.erase(patchHandle);
3058}
3059
3060
3061size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3062{
3063 Mutex::Autolock _l(mProxyLock);
3064 if (effect == mHalEffect) {
3065 mHalEffect.clear();
3066 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3067 }
3068 return mHalEffect == nullptr ? 0 : 1;
3069}
3070
3071status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3072 sp<EffectHalInterface> effect) {
3073 if (mHalEffect == nullptr) {
3074 return NO_INIT;
3075 }
3076 return mManagerCallback->addEffectToHal(
3077 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3078}
3079
3080status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3081 sp<EffectHalInterface> effect) {
3082 if (mHalEffect == nullptr) {
3083 return NO_INIT;
3084 }
3085 return mManagerCallback->removeEffectFromHal(
3086 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3087}
3088
3089bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3090 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3091 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3092 }
3093 return true;
3094}
3095
3096uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3097 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3098 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3099 return mDevicePort.sample_rate;
3100 }
3101 return DEFAULT_OUTPUT_SAMPLE_RATE;
3102}
3103
3104audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3105 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3106 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3107 return mDevicePort.channel_mask;
3108 }
3109 return AUDIO_CHANNEL_OUT_STEREO;
3110}
3111
3112uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3113 if (isOutput()) {
3114 return audio_channel_count_from_out_mask(channelMask());
3115 }
3116 return audio_channel_count_from_in_mask(channelMask());
3117}
3118
3119void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3120 const Vector<String16> args;
3121 EffectBase::dump(fd, args);
3122
3123 const bool locked = dumpTryLock(mProxyLock);
3124 if (!locked) {
3125 String8 result("DeviceEffectProxy may be deadlocked\n");
3126 write(fd, result.string(), result.size());
3127 }
3128
3129 String8 outStr;
3130 if (mHalEffect != nullptr) {
3131 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3132 } else {
3133 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3134 }
3135 write(fd, outStr.string(), outStr.size());
3136 outStr.clear();
3137
3138 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3139 write(fd, outStr.string(), outStr.size());
3140 outStr.clear();
3141
3142 for (const auto& iter : mEffectHandles) {
3143 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3144 write(fd, outStr.string(), outStr.size());
3145 outStr.clear();
3146 sp<EffectBase> effect = iter.second->effect().promote();
3147 if (effect != nullptr) {
3148 effect->dump(fd, args);
3149 }
3150 }
3151
3152 if (locked) {
3153 mLock.unlock();
3154 }
3155}
3156
3157#undef LOG_TAG
3158#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3159
3160int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3161 return mManagerCallback->newEffectId();
3162}
3163
3164
3165bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3166 EffectHandle *handle, bool unpinIfLast) {
3167 sp<EffectBase> effectBase = handle->effect().promote();
3168 if (effectBase == nullptr) {
3169 return false;
3170 }
3171
3172 sp<EffectModule> effect = effectBase->asEffectModule();
3173 if (effect == nullptr) {
3174 return false;
3175 }
3176
3177 // restore suspended effects if the disconnected handle was enabled and the last one.
3178 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3179 if (remove) {
3180 sp<DeviceEffectProxy> proxy = mProxy.promote();
3181 if (proxy != nullptr) {
3182 proxy->removeEffect(effect);
3183 }
3184 if (handle->enabled()) {
3185 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3186 }
3187 }
3188 return true;
3189}
3190
3191status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3192 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3193 sp<EffectHalInterface> *effect) {
3194 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3195}
3196
3197status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3198 sp<EffectHalInterface> effect) {
3199 sp<DeviceEffectProxy> proxy = mProxy.promote();
3200 if (proxy == nullptr) {
3201 return NO_INIT;
3202 }
3203 return proxy->addEffectToHal(effect);
3204}
3205
3206status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3207 sp<EffectHalInterface> effect) {
3208 sp<DeviceEffectProxy> proxy = mProxy.promote();
3209 if (proxy == nullptr) {
3210 return NO_INIT;
3211 }
3212 return proxy->addEffectToHal(effect);
3213}
3214
3215bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3216 sp<DeviceEffectProxy> proxy = mProxy.promote();
3217 if (proxy == nullptr) {
3218 return true;
3219 }
3220 return proxy->isOutput();
3221}
3222
3223uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3224 sp<DeviceEffectProxy> proxy = mProxy.promote();
3225 if (proxy == nullptr) {
3226 return DEFAULT_OUTPUT_SAMPLE_RATE;
3227 }
3228 return proxy->sampleRate();
3229}
3230
3231audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3232 sp<DeviceEffectProxy> proxy = mProxy.promote();
3233 if (proxy == nullptr) {
3234 return AUDIO_CHANNEL_OUT_STEREO;
3235 }
3236 return proxy->channelMask();
3237}
3238
3239uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3240 sp<DeviceEffectProxy> proxy = mProxy.promote();
3241 if (proxy == nullptr) {
3242 return 2;
3243 }
3244 return proxy->channelCount();
3245}
3246
Glenn Kasten63238ef2015-03-02 15:50:29 -08003247} // namespace android