blob: 82b9c9662a7e1bb2e4c8422f3d8c8496b99a848d [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>
jiabin8f278ee2019-11-11 12:16:27 -080032#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070033#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioDeviceTypeAddr.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070035#include <media/audiohal/EffectHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070037#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080038
39#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080040
41// ----------------------------------------------------------------------------
42
43// Note: the following macro is used for extremely verbose logging message. In
44// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
45// 0; but one side effect of this is to turn all LOGV's as well. Some messages
46// are so verbose that we want to suppress them even when we have ALOG_ASSERT
47// turned on. Do not uncomment the #def below unless you really know what you
48// are doing and want to see all of the extremely verbose messages.
49//#define VERY_VERY_VERBOSE_LOGGING
50#ifdef VERY_VERY_VERBOSE_LOGGING
51#define ALOGVV ALOGV
52#else
53#define ALOGVV(a...) do { } while(0)
54#endif
55
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090056#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
57
Eric Laurentca7cc822012-11-19 14:55:58 -080058namespace android {
59
60// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080061// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080062// ----------------------------------------------------------------------------
63
64#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080065#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080066
Eric Laurent41709552019-12-16 19:34:05 -080067AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080068 effect_descriptor_t *desc,
69 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080070 audio_session_t sessionId,
71 bool pinned)
72 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080073 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080074 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080075{
Eric Laurentca7cc822012-11-19 14:55:58 -080076}
77
Eric Laurent41709552019-12-16 19:34:05 -080078// must be called with EffectModule::mLock held
79status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -080080{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080081
Eric Laurent41709552019-12-16 19:34:05 -080082 ALOGV("setEnabled %p enabled %d", this, enabled);
83
84 if (enabled != isEnabled()) {
85 switch (mState) {
86 // going from disabled to enabled
87 case IDLE:
88 mState = STARTING;
89 break;
90 case STOPPED:
91 mState = RESTART;
92 break;
93 case STOPPING:
94 mState = ACTIVE;
95 break;
96
97 // going from enabled to disabled
98 case RESTART:
99 mState = STOPPED;
100 break;
101 case STARTING:
102 mState = IDLE;
103 break;
104 case ACTIVE:
105 mState = STOPPING;
106 break;
107 case DESTROYED:
108 return NO_ERROR; // simply ignore as we are being destroyed
109 }
110 for (size_t i = 1; i < mHandles.size(); i++) {
111 EffectHandle *h = mHandles[i];
112 if (h != NULL && !h->disconnected()) {
113 h->setEnabled(enabled);
114 }
115 }
116 }
117 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800118}
119
Eric Laurent41709552019-12-16 19:34:05 -0800120status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
121{
122 status_t status;
123 {
124 Mutex::Autolock _l(mLock);
125 status = setEnabled_l(enabled);
126 }
127 if (fromHandle) {
128 if (enabled) {
129 if (status != NO_ERROR) {
130 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
131 } else {
132 mCallback->onEffectEnable(this);
133 }
134 } else {
135 mCallback->onEffectDisable(this);
136 }
137 }
138 return status;
139}
140
141bool AudioFlinger::EffectBase::isEnabled() const
142{
143 switch (mState) {
144 case RESTART:
145 case STARTING:
146 case ACTIVE:
147 return true;
148 case IDLE:
149 case STOPPING:
150 case STOPPED:
151 case DESTROYED:
152 default:
153 return false;
154 }
155}
156
157void AudioFlinger::EffectBase::setSuspended(bool suspended)
158{
159 Mutex::Autolock _l(mLock);
160 mSuspended = suspended;
161}
162
163bool AudioFlinger::EffectBase::suspended() const
164{
165 Mutex::Autolock _l(mLock);
166 return mSuspended;
167}
168
169status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 status_t status;
172
173 Mutex::Autolock _l(mLock);
174 int priority = handle->priority();
175 size_t size = mHandles.size();
176 EffectHandle *controlHandle = NULL;
177 size_t i;
178 for (i = 0; i < size; i++) {
179 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800181 continue;
182 }
183 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700184 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800185 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700186 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 if (h->priority() <= priority) {
188 break;
189 }
190 }
191 // if inserted in first place, move effect control from previous owner to this handle
192 if (i == 0) {
193 bool enabled = false;
194 if (controlHandle != NULL) {
195 enabled = controlHandle->enabled();
196 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
197 }
198 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
199 status = NO_ERROR;
200 } else {
201 status = ALREADY_EXISTS;
202 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700203 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800204 mHandles.insertAt(handle, i);
205 return status;
206}
207
Eric Laurent41709552019-12-16 19:34:05 -0800208status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700209{
210 status_t status = NO_ERROR;
211 bool doRegister = false;
212 bool registered = false;
213 bool doEnable = false;
214 bool enabled = false;
215 audio_io_handle_t io;
216 uint32_t strategy;
217
218 {
219 Mutex::Autolock _l(mLock);
220 // register effect when first handle is attached and unregister when last handle is removed
221 if (mPolicyRegistered != mHandles.size() > 0) {
222 doRegister = true;
223 mPolicyRegistered = mHandles.size() > 0;
224 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800225 io = mCallback->io();
226 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700227 }
228 }
229 // enable effect when registered according to enable state requested by controlling handle
230 if (mHandles.size() > 0) {
231 EffectHandle *handle = controlHandle_l();
232 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
233 doEnable = true;
234 mPolicyEnabled = handle->enabled();
235 }
236 }
237 registered = mPolicyRegistered;
238 enabled = mPolicyEnabled;
239 mPolicyLock.lock();
240 }
241 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
242 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
243 if (doRegister) {
244 if (registered) {
245 status = AudioSystem::registerEffect(
246 &mDescriptor,
247 io,
248 strategy,
249 mSessionId,
250 mId);
251 } else {
252 status = AudioSystem::unregisterEffect(mId);
253 }
254 }
255 if (registered && doEnable) {
256 status = AudioSystem::setEffectEnabled(mId, enabled);
257 }
258 mPolicyLock.unlock();
259
260 return status;
261}
262
263
Eric Laurent41709552019-12-16 19:34:05 -0800264ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800265{
266 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800267 return removeHandle_l(handle);
268}
269
Eric Laurent41709552019-12-16 19:34:05 -0800270ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800271{
Eric Laurentca7cc822012-11-19 14:55:58 -0800272 size_t size = mHandles.size();
273 size_t i;
274 for (i = 0; i < size; i++) {
275 if (mHandles[i] == handle) {
276 break;
277 }
278 }
279 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800280 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
281 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800282 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800283 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800284
285 mHandles.removeAt(i);
286 // if removed from first place, move effect control from this handle to next in line
287 if (i == 0) {
288 EffectHandle *h = controlHandle_l();
289 if (h != NULL) {
290 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
291 }
292 }
293
Eric Laurentca7cc822012-11-19 14:55:58 -0800294 if (mHandles.size() == 0 && !mPinned) {
295 mState = DESTROYED;
296 }
297
298 return mHandles.size();
299}
300
301// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800302AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800303{
304 // the first valid handle in the list has control over the module
305 for (size_t i = 0; i < mHandles.size(); i++) {
306 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800308 return h;
309 }
310 }
311
312 return NULL;
313}
314
Eric Laurentf10c7092016-12-06 17:09:56 -0800315// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800316ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800317{
318 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800319 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
320 return mHandles.size();
321 }
322
Eric Laurentf10c7092016-12-06 17:09:56 -0800323 Mutex::Autolock _l(mLock);
324 ssize_t numHandles = removeHandle_l(handle);
325 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800326 mLock.unlock();
327 mCallback->updateOrphanEffectChains(this);
328 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800329 }
330 return numHandles;
331}
332
Eric Laurent41709552019-12-16 19:34:05 -0800333bool AudioFlinger::EffectBase::purgeHandles()
334{
335 bool enabled = false;
336 Mutex::Autolock _l(mLock);
337 EffectHandle *handle = controlHandle_l();
338 if (handle != NULL) {
339 enabled = handle->enabled();
340 }
341 mHandles.clear();
342 return enabled;
343}
344
345void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
346 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
347}
348
349static String8 effectFlagsToString(uint32_t flags) {
350 String8 s;
351
352 s.append("conn. mode: ");
353 switch (flags & EFFECT_FLAG_TYPE_MASK) {
354 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
355 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
356 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
357 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
358 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
359 default: s.append("unknown/reserved"); break;
360 }
361 s.append(", ");
362
363 s.append("insert pref: ");
364 switch (flags & EFFECT_FLAG_INSERT_MASK) {
365 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
366 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
367 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
368 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
369 default: s.append("unknown/reserved"); break;
370 }
371 s.append(", ");
372
373 s.append("volume mgmt: ");
374 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
375 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
376 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
377 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
378 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
379 default: s.append("unknown/reserved"); break;
380 }
381 s.append(", ");
382
383 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
384 if (devind) {
385 s.append("device indication: ");
386 switch (devind) {
387 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
388 default: s.append("unknown/reserved"); break;
389 }
390 s.append(", ");
391 }
392
393 s.append("input mode: ");
394 switch (flags & EFFECT_FLAG_INPUT_MASK) {
395 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
396 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
397 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
398 default: s.append("not set"); break;
399 }
400 s.append(", ");
401
402 s.append("output mode: ");
403 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
404 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
405 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
406 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
407 default: s.append("not set"); break;
408 }
409 s.append(", ");
410
411 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
412 if (accel) {
413 s.append("hardware acceleration: ");
414 switch (accel) {
415 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
416 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
417 default: s.append("unknown/reserved"); break;
418 }
419 s.append(", ");
420 }
421
422 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
423 if (modeind) {
424 s.append("mode indication: ");
425 switch (modeind) {
426 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
427 default: s.append("unknown/reserved"); break;
428 }
429 s.append(", ");
430 }
431
432 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
433 if (srcind) {
434 s.append("source indication: ");
435 switch (srcind) {
436 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
437 default: s.append("unknown/reserved"); break;
438 }
439 s.append(", ");
440 }
441
442 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
443 s.append("offloadable, ");
444 }
445
446 int len = s.length();
447 if (s.length() > 2) {
448 (void) s.lockBuffer(len);
449 s.unlockBuffer(len - 2);
450 }
451 return s;
452}
453
454void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
455{
456 String8 result;
457
458 result.appendFormat("\tEffect ID %d:\n", mId);
459
460 bool locked = AudioFlinger::dumpTryLock(mLock);
461 // failed to lock - AudioFlinger is probably deadlocked
462 if (!locked) {
463 result.append("\t\tCould not lock Fx mutex:\n");
464 }
465
466 result.append("\t\tSession State Registered Enabled Suspended:\n");
467 result.appendFormat("\t\t%05d %03d %s %s %s\n",
468 mSessionId, mState, mPolicyRegistered ? "y" : "n",
469 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
470
471 result.append("\t\tDescriptor:\n");
472 char uuidStr[64];
473 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
474 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
475 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
476 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
477 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
478 mDescriptor.apiVersion,
479 mDescriptor.flags,
480 effectFlagsToString(mDescriptor.flags).string());
481 result.appendFormat("\t\t- name: %s\n",
482 mDescriptor.name);
483
484 result.appendFormat("\t\t- implementor: %s\n",
485 mDescriptor.implementor);
486
487 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
488 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
489 char buffer[256];
490 for (size_t i = 0; i < mHandles.size(); ++i) {
491 EffectHandle *handle = mHandles[i];
492 if (handle != NULL && !handle->disconnected()) {
493 handle->dumpToBuffer(buffer, sizeof(buffer));
494 result.append(buffer);
495 }
496 }
497 if (locked) {
498 mLock.unlock();
499 }
500
501 write(fd, result.string(), result.length());
502}
503
504// ----------------------------------------------------------------------------
505// EffectModule implementation
506// ----------------------------------------------------------------------------
507
508#undef LOG_TAG
509#define LOG_TAG "AudioFlinger::EffectModule"
510
511AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
512 effect_descriptor_t *desc,
513 int id,
514 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800515 bool pinned,
516 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800517 : EffectBase(callback, desc, id, sessionId, pinned),
518 // clear mConfig to ensure consistent initial value of buffer framecount
519 // in case buffers are associated by setInBuffer() or setOutBuffer()
520 // prior to configure().
521 mConfig{{}, {}},
522 mStatus(NO_INIT),
523 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
524 mDisableWaitCnt(0), // set by process() and updateState()
525 mOffloaded(false)
526#ifdef FLOAT_EFFECT_CHAIN
527 , mSupportsFloat(false)
528#endif
529{
530 ALOGV("Constructor %p pinned %d", this, pinned);
531 int lStatus;
532
533 // create effect engine from effect factory
534 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800535 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800536 if (mStatus != NO_ERROR) {
537 return;
538 }
539 lStatus = init();
540 if (lStatus < 0) {
541 mStatus = lStatus;
542 goto Error;
543 }
544
545 setOffloaded(callback->isOffload(), callback->io());
546 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
547
548 return;
549Error:
550 mEffectInterface.clear();
551 ALOGV("Constructor Error %d", mStatus);
552}
553
554AudioFlinger::EffectModule::~EffectModule()
555{
556 ALOGV("Destructor %p", this);
557 if (mEffectInterface != 0) {
558 char uuidStr[64];
559 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
560 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
561 this, uuidStr);
562 release_l();
563 }
564
565}
566
567ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
568{
569 ssize_t status = EffectBase::removeHandle_l(handle);
570
571 // Prevent calls to process() and other functions on effect interface from now on.
572 // The effect engine will be released by the destructor when the last strong reference on
573 // this object is released which can happen after next process is called.
574 if (status == 0 && !mPinned) {
575 mEffectInterface->close();
576 }
577
578 return status;
579}
580
Eric Laurentfa1e1232016-08-02 19:01:49 -0700581bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 Mutex::Autolock _l(mLock);
583
Eric Laurentfa1e1232016-08-02 19:01:49 -0700584 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800585 switch (mState) {
586 case RESTART:
587 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700588 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800589
590 case STARTING:
591 // clear auxiliary effect input buffer for next accumulation
592 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
593 memset(mConfig.inputCfg.buffer.raw,
594 0,
595 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
596 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700597 if (start_l() == NO_ERROR) {
598 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700599 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700600 } else {
601 mState = IDLE;
602 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 break;
604 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900605 // volume control for offload and direct threads must take effect immediately.
606 if (stop_l() == NO_ERROR
607 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700608 mDisableWaitCnt = mMaxDisableWaitCnt;
609 } else {
610 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
611 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800612 mState = STOPPED;
613 break;
614 case STOPPED:
615 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
616 // turn off sequence.
617 if (--mDisableWaitCnt == 0) {
618 reset_l();
619 mState = IDLE;
620 }
621 break;
622 default: //IDLE , ACTIVE, DESTROYED
623 break;
624 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700625
626 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800627}
628
629void AudioFlinger::EffectModule::process()
630{
631 Mutex::Autolock _l(mLock);
632
Mikhail Naganov022b9952017-01-04 16:36:51 -0800633 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800634 return;
635 }
636
rago94a1ee82017-07-21 15:11:02 -0700637 const uint32_t inChannelCount =
638 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
639 const uint32_t outChannelCount =
640 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
641 const bool auxType =
642 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
643
Andy Hungfa69ca32017-11-30 10:07:53 -0800644 // safeInputOutputSampleCount is 0 if the channel count between input and output
645 // buffers do not match. This prevents automatic accumulation or copying between the
646 // input and output effect buffers without an intermediary effect process.
647 // TODO: consider implementing channel conversion.
648 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700649 mInChannelCountRequested != mOutChannelCountRequested ? 0
650 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800651 mConfig.inputCfg.buffer.frameCount,
652 mConfig.outputCfg.buffer.frameCount);
653 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
654#ifdef FLOAT_EFFECT_CHAIN
655 accumulate_float(
656 mConfig.outputCfg.buffer.f32,
657 mConfig.inputCfg.buffer.f32,
658 safeInputOutputSampleCount);
659#else
660 accumulate_i16(
661 mConfig.outputCfg.buffer.s16,
662 mConfig.inputCfg.buffer.s16,
663 safeInputOutputSampleCount);
664#endif
665 };
666 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
667#ifdef FLOAT_EFFECT_CHAIN
668 memcpy(
669 mConfig.outputCfg.buffer.f32,
670 mConfig.inputCfg.buffer.f32,
671 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
672
673#else
674 memcpy(
675 mConfig.outputCfg.buffer.s16,
676 mConfig.inputCfg.buffer.s16,
677 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
678#endif
679 };
680
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700682 int ret;
683 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700684 if (auxType) {
685 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800686 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700687#ifdef FLOAT_EFFECT_CHAIN
688 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800689#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700690 // Do in-place float conversion for auxiliary effect input buffer.
691 static_assert(sizeof(float) <= sizeof(int32_t),
692 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
693
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 memcpy_to_float_from_q4_27(
695 mConfig.inputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.s32,
697 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800698#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800699 } else
Andy Hung116a4982017-11-30 10:15:08 -0800700#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800701 {
Andy Hung116a4982017-11-30 10:15:08 -0800702#ifdef FLOAT_AUX
703 memcpy_to_i16_from_float(
704 mConfig.inputCfg.buffer.s16,
705 mConfig.inputCfg.buffer.f32,
706 mConfig.inputCfg.buffer.frameCount);
707#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800708 memcpy_to_i16_from_q4_27(
709 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700710 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800711 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800712#endif
rago94a1ee82017-07-21 15:11:02 -0700713 }
rago94a1ee82017-07-21 15:11:02 -0700714 }
715#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800716 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
717 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
718
719 if (!auxType && mInChannelCountRequested != inChannelCount) {
720 adjust_channels(
721 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
722 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
723 sizeof(float),
724 sizeof(float)
725 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
726 inBuffer = mInConversionBuffer;
727 }
728 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
729 && mOutChannelCountRequested != outChannelCount) {
730 adjust_selected_channels(
731 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
732 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
733 sizeof(float),
734 sizeof(float)
735 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
736 outBuffer = mOutConversionBuffer;
737 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800738 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
739 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800740 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800741 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
742 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700743 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 memcpy_to_i16_from_float(
745 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800746 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800748 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700749 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800751 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800752 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
753 goto data_bypass;
754 }
755 memcpy_to_i16_from_float(
756 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800757 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800759 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700760 }
761 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800762#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800763 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800764#ifdef FLOAT_EFFECT_CHAIN
765 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800766 sp<EffectBufferHalInterface> target =
767 mOutChannelCountRequested != outChannelCount
768 ? mOutConversionBuffer : mOutBuffer;
769
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 mOutConversionBuffer->audioBuffer()->s16,
773 outChannelCount * mConfig.outputCfg.buffer.frameCount);
774 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800775 if (mOutChannelCountRequested != outChannelCount) {
776 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
777 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
778 sizeof(float),
779 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
780 }
rago94a1ee82017-07-21 15:11:02 -0700781#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700782 } else {
rago94a1ee82017-07-21 15:11:02 -0700783#ifdef FLOAT_EFFECT_CHAIN
784 data_bypass:
785#endif
786 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700788 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700790 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700792 }
793 }
794 ret = -ENODATA;
795 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800796
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 // force transition to IDLE state when engine is ready
798 if (mState == STOPPED && ret == -ENODATA) {
799 mDisableWaitCnt = 1;
800 }
801
802 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700803 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800804#ifdef FLOAT_AUX
805 const size_t size =
806 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
807#else
rago94a1ee82017-07-21 15:11:02 -0700808 const size_t size =
809 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800810#endif
rago94a1ee82017-07-21 15:11:02 -0700811 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800812 }
813 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700814 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
816 // If an insert effect is idle and input buffer is different from output buffer,
817 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800818 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700819 // similar handling with data_bypass above.
820 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
821 accumulateInputToOutput();
822 } else { // EFFECT_BUFFER_ACCESS_WRITE
823 copyInputToOutput();
824 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800825 }
826 }
827}
828
829void AudioFlinger::EffectModule::reset_l()
830{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700831 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800832 return;
833 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700834 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800835}
836
837status_t AudioFlinger::EffectModule::configure()
838{
rago94a1ee82017-07-21 15:11:02 -0700839 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841 uint32_t size;
842 audio_channel_mask_t channelMask;
843
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700844 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700845 status = NO_INIT;
846 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848
Eric Laurentca7cc822012-11-19 14:55:58 -0800849 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 // TODO: handle configuration of input (record) SW effects above the HAL,
851 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
852 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800853 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700855 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800856
857 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800858 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
859 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
860 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
861 mConfig.inputCfg.channels);
862 }
863#ifndef MULTICHANNEL_EFFECT_CHAIN
864 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
865 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
866 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
867 mConfig.outputCfg.channels);
868 }
869#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800871#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700872 // TODO: Update this logic when multichannel effects are implemented.
873 // For offloaded tracks consider mono output as stereo for proper effect initialization
874 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
875 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
876 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
877 ALOGV("Overriding effect input and output as STEREO");
878 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800879#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800881 mInChannelCountRequested =
882 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
883 mOutChannelCountRequested =
884 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700885
rago94a1ee82017-07-21 15:11:02 -0700886 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
887 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900888
889 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100890 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900891 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
892 ALOGV("Overriding effect input as 48kHz");
893 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800894 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900895 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800896 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
897 mConfig.inputCfg.bufferProvider.cookie = NULL;
898 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
899 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
900 mConfig.outputCfg.bufferProvider.cookie = NULL;
901 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
902 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
903 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
904 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800905 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800906 // always overwrites output buffer: input buffer == output buffer
907 // - in other sessions:
908 // last effect in the chain accumulates in output buffer: input buffer != output buffer
909 // other effect: overwrites output buffer: input buffer == output buffer
910 // Auxiliary effect:
911 // accumulates in output buffer: input buffer != output buffer
912 // Therefore: accumulate <=> input buffer != output buffer
913 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
914 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
915 } else {
916 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
917 }
918 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
919 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800920 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
922
Eric Laurent6b446ce2019-12-13 10:56:31 -0800923 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800924 this, mCallback->chain().promote().get(),
925 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800926
927 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700928 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700929 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800930 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700931 &mConfig,
932 &size,
933 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700934 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800935 status = cmdStatus;
936 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800937
938#ifdef MULTICHANNEL_EFFECT_CHAIN
939 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800940 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800941 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
942 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
943 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700944 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
945 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800946 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
947 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
948 }
949 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
950 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
951 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
952 }
953 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700954 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800955 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700956 &mConfig,
957 &size,
958 &cmdStatus);
959 if (status == NO_ERROR) {
960 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800961 }
962 }
963#endif
964
965#ifdef FLOAT_EFFECT_CHAIN
966 if (status == NO_ERROR) {
967 mSupportsFloat = true;
968 }
969
970 if (status != NO_ERROR) {
971 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
972 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
973 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
974 size = sizeof(int);
975 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
976 sizeof(mConfig),
977 &mConfig,
978 &size,
979 &cmdStatus);
980 if (status == NO_ERROR) {
981 status = cmdStatus;
982 }
983 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700984 mSupportsFloat = false;
985 ALOGVV("config worked with 16 bit");
986 } else {
987 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800988 }
rago94a1ee82017-07-21 15:11:02 -0700989 }
990#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800991
rago94a1ee82017-07-21 15:11:02 -0700992 if (status == NO_ERROR) {
993 // Establish Buffer strategy
994 setInBuffer(mInBuffer);
995 setOutBuffer(mOutBuffer);
996
997 // Update visualizer latency
998 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
999 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1000 effect_param_t *p = (effect_param_t *)buf32;
1001
1002 p->psize = sizeof(uint32_t);
1003 p->vsize = sizeof(uint32_t);
1004 size = sizeof(int);
1005 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1006
Eric Laurent6b446ce2019-12-13 10:56:31 -08001007 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001008
1009 *((int32_t *)p->data + 1)= latency;
1010 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1011 sizeof(effect_param_t) + 8,
1012 &buf32,
1013 &size,
1014 &cmdStatus);
1015 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001016 }
1017
Andy Hung05083ac2017-12-14 15:00:28 -08001018 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1019 mMaxDisableWaitCnt = (uint32_t)std::max(
1020 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1021 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1022 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001023
Eric Laurentd0ebb532013-04-02 16:41:41 -07001024exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001025 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001026 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001027 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001028 return status;
1029}
1030
1031status_t AudioFlinger::EffectModule::init()
1032{
1033 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001034 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001035 return NO_INIT;
1036 }
1037 status_t cmdStatus;
1038 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001039 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1040 0,
1041 NULL,
1042 &size,
1043 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001044 if (status == 0) {
1045 status = cmdStatus;
1046 }
1047 return status;
1048}
1049
Eric Laurent1b928682014-10-02 19:41:47 -07001050void AudioFlinger::EffectModule::addEffectToHal_l()
1051{
1052 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1053 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001054 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001055 }
1056}
1057
Eric Laurentfa1e1232016-08-02 19:01:49 -07001058// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001059status_t AudioFlinger::EffectModule::start()
1060{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001061 status_t status;
1062 {
1063 Mutex::Autolock _l(mLock);
1064 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001065 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001066 if (status == NO_ERROR) {
1067 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001068 }
1069 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001070}
1071
1072status_t AudioFlinger::EffectModule::start_l()
1073{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001074 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001075 return NO_INIT;
1076 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001077 if (mStatus != NO_ERROR) {
1078 return mStatus;
1079 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001080 status_t cmdStatus;
1081 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001082 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1083 0,
1084 NULL,
1085 &size,
1086 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001087 if (status == 0) {
1088 status = cmdStatus;
1089 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001090 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001091 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001092 }
1093 return status;
1094}
1095
1096status_t AudioFlinger::EffectModule::stop()
1097{
1098 Mutex::Autolock _l(mLock);
1099 return stop_l();
1100}
1101
1102status_t AudioFlinger::EffectModule::stop_l()
1103{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001104 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 return NO_INIT;
1106 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001107 if (mStatus != NO_ERROR) {
1108 return mStatus;
1109 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001110 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001111 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001112
1113 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001114 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1115 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1116 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001117 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001118 mSetVolumeReentrantTid = INVALID_PID;
1119 }
1120
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001121 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1122 0,
1123 NULL,
1124 &size,
1125 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001126 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 status = cmdStatus;
1128 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001129 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001130 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001131 }
1132 return status;
1133}
1134
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001135// must be called with EffectChain::mLock held
1136void AudioFlinger::EffectModule::release_l()
1137{
1138 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001139 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001140 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001141 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001142 mEffectInterface.clear();
1143 }
1144}
1145
Eric Laurent6b446ce2019-12-13 10:56:31 -08001146status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001147{
1148 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1149 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001150 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001151 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001152 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001153}
1154
Andy Hunge4a1d912016-08-17 14:11:13 -07001155// round up delta valid if value and divisor are positive.
1156template <typename T>
1157static T roundUpDelta(const T &value, const T &divisor) {
1158 T remainder = value % divisor;
1159 return remainder == 0 ? 0 : divisor - remainder;
1160}
1161
Eric Laurentca7cc822012-11-19 14:55:58 -08001162status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1163 uint32_t cmdSize,
1164 void *pCmdData,
1165 uint32_t *replySize,
1166 void *pReplyData)
1167{
1168 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001169 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001170
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001171 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001172 return NO_INIT;
1173 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001174 if (mStatus != NO_ERROR) {
1175 return mStatus;
1176 }
Andy Hung110bc952016-06-20 15:22:52 -07001177 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001178 (sizeof(effect_param_t) > cmdSize ||
1179 ((effect_param_t *)pCmdData)->psize > cmdSize
1180 - sizeof(effect_param_t))) {
1181 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001182 android_errorWriteLog(0x534e4554, "33003822");
1183 return -EINVAL;
1184 }
1185 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1186 (*replySize < sizeof(effect_param_t) ||
1187 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1188 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001189 return -EINVAL;
1190 }
ragoe2759072016-11-22 18:02:48 -08001191 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1192 (sizeof(effect_param_t) > *replySize
1193 || ((effect_param_t *)pCmdData)->psize > *replySize
1194 - sizeof(effect_param_t)
1195 || ((effect_param_t *)pCmdData)->vsize > *replySize
1196 - sizeof(effect_param_t)
1197 - ((effect_param_t *)pCmdData)->psize
1198 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1199 *replySize
1200 - sizeof(effect_param_t)
1201 - ((effect_param_t *)pCmdData)->psize
1202 - ((effect_param_t *)pCmdData)->vsize)) {
1203 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1204 android_errorWriteLog(0x534e4554, "32705438");
1205 return -EINVAL;
1206 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001207 if ((cmdCode == EFFECT_CMD_SET_PARAM
1208 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1209 (sizeof(effect_param_t) > cmdSize
1210 || ((effect_param_t *)pCmdData)->psize > cmdSize
1211 - sizeof(effect_param_t)
1212 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1213 - sizeof(effect_param_t)
1214 - ((effect_param_t *)pCmdData)->psize
1215 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1216 cmdSize
1217 - sizeof(effect_param_t)
1218 - ((effect_param_t *)pCmdData)->psize
1219 - ((effect_param_t *)pCmdData)->vsize)) {
1220 android_errorWriteLog(0x534e4554, "30204301");
1221 return -EINVAL;
1222 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001223 status_t status = mEffectInterface->command(cmdCode,
1224 cmdSize,
1225 pCmdData,
1226 replySize,
1227 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001228 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1229 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1230 for (size_t i = 1; i < mHandles.size(); i++) {
1231 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001232 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001233 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1234 }
1235 }
1236 }
1237 return status;
1238}
1239
Eric Laurentca7cc822012-11-19 14:55:58 -08001240bool AudioFlinger::EffectModule::isProcessEnabled() const
1241{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001242 if (mStatus != NO_ERROR) {
1243 return false;
1244 }
1245
Eric Laurentca7cc822012-11-19 14:55:58 -08001246 switch (mState) {
1247 case RESTART:
1248 case ACTIVE:
1249 case STOPPING:
1250 case STOPPED:
1251 return true;
1252 case IDLE:
1253 case STARTING:
1254 case DESTROYED:
1255 default:
1256 return false;
1257 }
1258}
1259
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001260bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1261{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001262 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001263}
1264
1265bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1266{
1267 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1268}
1269
Mikhail Naganov022b9952017-01-04 16:36:51 -08001270void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001271 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001272
1273 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001274 if (buffer != 0) {
1275 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1276 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1277 } else {
1278 mConfig.inputCfg.buffer.raw = NULL;
1279 }
1280 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001281 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001282
1283#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001284 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001285 // Theoretically insert effects can also do in-place conversions (destroying
1286 // the original buffer) when the output buffer is identical to the input buffer,
1287 // but we don't optimize for it here.
1288 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001289 const uint32_t inChannelCount =
1290 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1291 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001292 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001293 // we need to translate - create hidl shared buffer and intercept
1294 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001295 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1296 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1297 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001298
1299 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1300 __func__, inChannels, inFrameCount, size);
1301
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001302 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001303 || size > mInConversionBuffer->getSize())) {
1304 mInConversionBuffer.clear();
1305 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001306 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001307 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001308 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001309 mInConversionBuffer->setFrameCount(inFrameCount);
1310 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001311 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001312 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001313 }
1314 }
1315#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001316}
1317
1318void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001319 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001320
1321 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001322 if (buffer != 0) {
1323 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1324 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1325 } else {
1326 mConfig.outputCfg.buffer.raw = NULL;
1327 }
1328 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001329 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001330
1331#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001332 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001333 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001334 const uint32_t outChannelCount =
1335 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1336 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001337 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001338 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001339 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1340 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1341 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001342
1343 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1344 __func__, outChannels, outFrameCount, size);
1345
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001346 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001347 || size > mOutConversionBuffer->getSize())) {
1348 mOutConversionBuffer.clear();
1349 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001350 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001351 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001352 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001353 mOutConversionBuffer->setFrameCount(outFrameCount);
1354 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001355 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001356 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001357 }
1358 }
1359#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001360}
1361
Eric Laurentca7cc822012-11-19 14:55:58 -08001362status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1363{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001364 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001365 if (mStatus != NO_ERROR) {
1366 return mStatus;
1367 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001368 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001369 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1370 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1371 if (isProcessEnabled() &&
1372 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001373 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1374 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001375 uint32_t volume[2];
1376 uint32_t *pVolume = NULL;
1377 uint32_t size = sizeof(volume);
1378 volume[0] = *left;
1379 volume[1] = *right;
1380 if (controller) {
1381 pVolume = volume;
1382 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001383 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1384 size,
1385 volume,
1386 &size,
1387 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001388 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1389 *left = volume[0];
1390 *right = volume[1];
1391 }
1392 }
1393 return status;
1394}
1395
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001396void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1397{
Zhou Songd505c642020-02-20 16:35:37 +08001398 // for offload or direct thread, if the effect chain has non-offloadable
1399 // effect and any effect module within the chain has volume control, then
1400 // volume control is delegated to effect, otherwise, set volume to hal.
1401 if (mEffectCallback->isOffloadOrDirect() &&
1402 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001403 float vol_l = (float)left / (1 << 24);
1404 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001405 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001406 }
1407}
1408
jiabin8f278ee2019-11-11 12:16:27 -08001409status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1410 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001411{
jiabin8f278ee2019-11-11 12:16:27 -08001412 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1413 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001414 return NO_ERROR;
1415 }
1416
1417 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001418 if (mStatus != NO_ERROR) {
1419 return mStatus;
1420 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001422 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001423 status_t cmdStatus;
1424 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001425 // FIXME: use audio device types and addresses when the hal interface is ready.
1426 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001427 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001428 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001429 &size,
1430 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001431 }
1432 return status;
1433}
1434
jiabin8f278ee2019-11-11 12:16:27 -08001435status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1436{
1437 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1438}
1439
1440status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1441{
1442 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1443}
1444
Eric Laurentca7cc822012-11-19 14:55:58 -08001445status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1446{
1447 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001448 if (mStatus != NO_ERROR) {
1449 return mStatus;
1450 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001451 status_t status = NO_ERROR;
1452 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1453 status_t cmdStatus;
1454 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001455 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1456 sizeof(audio_mode_t),
1457 &mode,
1458 &size,
1459 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001460 if (status == NO_ERROR) {
1461 status = cmdStatus;
1462 }
1463 }
1464 return status;
1465}
1466
1467status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1468{
1469 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001470 if (mStatus != NO_ERROR) {
1471 return mStatus;
1472 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 status_t status = NO_ERROR;
1474 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1475 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001476 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1477 sizeof(audio_source_t),
1478 &source,
1479 &size,
1480 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001481 }
1482 return status;
1483}
1484
Eric Laurent5baf2af2013-09-12 17:37:00 -07001485status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1486{
1487 Mutex::Autolock _l(mLock);
1488 if (mStatus != NO_ERROR) {
1489 return mStatus;
1490 }
1491 status_t status = NO_ERROR;
1492 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1493 status_t cmdStatus;
1494 uint32_t size = sizeof(status_t);
1495 effect_offload_param_t cmd;
1496
1497 cmd.isOffload = offloaded;
1498 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001499 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1500 sizeof(effect_offload_param_t),
1501 &cmd,
1502 &size,
1503 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001504 if (status == NO_ERROR) {
1505 status = cmdStatus;
1506 }
1507 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1508 } else {
1509 if (offloaded) {
1510 status = INVALID_OPERATION;
1511 }
1512 mOffloaded = false;
1513 }
1514 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1515 return status;
1516}
1517
1518bool AudioFlinger::EffectModule::isOffloaded() const
1519{
1520 Mutex::Autolock _l(mLock);
1521 return mOffloaded;
1522}
1523
Andy Hungbded9c82017-11-30 18:47:35 -08001524static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1525 std::stringstream ss;
1526
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001527 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001528 return "nullptr"; // make different than below
1529 } else if (buffer->externalData() != nullptr) {
1530 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1531 << " -> "
1532 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1533 } else {
1534 ss << buffer->audioBuffer()->raw;
1535 }
1536 return ss.str();
1537}
Marco Nelissenb2208842014-02-07 14:00:50 -08001538
Eric Laurent41709552019-12-16 19:34:05 -08001539void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001540{
Eric Laurent41709552019-12-16 19:34:05 -08001541 EffectBase::dump(fd, args);
1542
Eric Laurentca7cc822012-11-19 14:55:58 -08001543 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001544 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001545
Eric Laurent41709552019-12-16 19:34:05 -08001546 result.append("\t\tStatus Engine:\n");
1547 result.appendFormat("\t\t%03d %p\n",
1548 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001549
1550 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001551
1552 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001553 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1554 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1555 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001556 mConfig.inputCfg.buffer.frameCount,
1557 mConfig.inputCfg.samplingRate,
1558 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001559 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001560 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001561
1562 result.append("\t\t- Output configuration:\n");
1563 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001564 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001565 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001566 mConfig.outputCfg.buffer.frameCount,
1567 mConfig.outputCfg.samplingRate,
1568 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001569 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001570 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001571
rago94a1ee82017-07-21 15:11:02 -07001572#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001573
Andy Hungbded9c82017-11-30 18:47:35 -08001574 result.appendFormat("\t\t- HAL buffers:\n"
1575 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1576 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1577 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1578 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1579 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001580#endif
1581
Eric Laurentca7cc822012-11-19 14:55:58 -08001582 write(fd, result.string(), result.length());
1583
Mikhail Naganov4d547672019-02-22 14:19:19 -08001584 if (mEffectInterface != 0) {
1585 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1586 (void)mEffectInterface->dump(fd);
1587 }
1588
Eric Laurentca7cc822012-11-19 14:55:58 -08001589 if (locked) {
1590 mLock.unlock();
1591 }
1592}
1593
1594// ----------------------------------------------------------------------------
1595// EffectHandle implementation
1596// ----------------------------------------------------------------------------
1597
1598#undef LOG_TAG
1599#define LOG_TAG "AudioFlinger::EffectHandle"
1600
Eric Laurent41709552019-12-16 19:34:05 -08001601AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 const sp<AudioFlinger::Client>& client,
1603 const sp<IEffectClient>& effectClient,
1604 int32_t priority)
1605 : BnEffect(),
1606 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001607 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001608{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001609 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001610
1611 if (client == 0) {
1612 return;
1613 }
1614 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1615 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001616 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001617 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001618 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001619 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001620 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001621 return;
1622 }
Glenn Kastene75da402013-11-20 13:54:52 -08001623 new(mCblk) effect_param_cblk_t();
1624 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001625}
1626
1627AudioFlinger::EffectHandle::~EffectHandle()
1628{
1629 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001630 disconnect(false);
1631}
1632
Glenn Kastene75da402013-11-20 13:54:52 -08001633status_t AudioFlinger::EffectHandle::initCheck()
1634{
1635 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1636}
1637
Eric Laurentca7cc822012-11-19 14:55:58 -08001638status_t AudioFlinger::EffectHandle::enable()
1639{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001640 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001641 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001642 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001643 if (effect == 0 || mDisconnected) {
1644 return DEAD_OBJECT;
1645 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 if (!mHasControl) {
1647 return INVALID_OPERATION;
1648 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001649
1650 if (mEnabled) {
1651 return NO_ERROR;
1652 }
1653
1654 mEnabled = true;
1655
Eric Laurent6c796322019-04-09 14:13:17 -07001656 status_t status = effect->updatePolicyState();
1657 if (status != NO_ERROR) {
1658 mEnabled = false;
1659 return status;
1660 }
1661
Eric Laurent6b446ce2019-12-13 10:56:31 -08001662 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001663
1664 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001665 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 return NO_ERROR;
1667 }
1668
Eric Laurent6b446ce2019-12-13 10:56:31 -08001669 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001670 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001671 mEnabled = false;
1672 }
1673 return status;
1674}
1675
1676status_t AudioFlinger::EffectHandle::disable()
1677{
1678 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001679 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001680 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001681 if (effect == 0 || mDisconnected) {
1682 return DEAD_OBJECT;
1683 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001684 if (!mHasControl) {
1685 return INVALID_OPERATION;
1686 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001687
1688 if (!mEnabled) {
1689 return NO_ERROR;
1690 }
1691 mEnabled = false;
1692
Eric Laurent6c796322019-04-09 14:13:17 -07001693 effect->updatePolicyState();
1694
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001695 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 return NO_ERROR;
1697 }
1698
Eric Laurent6b446ce2019-12-13 10:56:31 -08001699 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001700 return status;
1701}
1702
1703void AudioFlinger::EffectHandle::disconnect()
1704{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001705 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001706 disconnect(true);
1707}
1708
1709void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1710{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001711 AutoMutex _l(mLock);
1712 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1713 if (mDisconnected) {
1714 if (unpinIfLast) {
1715 android_errorWriteLog(0x534e4554, "32707507");
1716 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 return;
1718 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001719 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001720 {
Eric Laurent41709552019-12-16 19:34:05 -08001721 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001722 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001723 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001724 ALOGW("%s Effect handle %p disconnected after thread destruction",
1725 __func__, this);
1726 }
1727 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001728 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001729 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001730
Eric Laurentca7cc822012-11-19 14:55:58 -08001731 if (mClient != 0) {
1732 if (mCblk != NULL) {
1733 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1734 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1735 }
1736 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001737 // Client destructor must run with AudioFlinger client mutex locked
1738 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 mClient.clear();
1740 }
1741}
1742
1743status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1744 uint32_t cmdSize,
1745 void *pCmdData,
1746 uint32_t *replySize,
1747 void *pReplyData)
1748{
1749 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001750 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001751
Eric Laurentc7ab3092017-06-15 18:43:46 -07001752 // reject commands reserved for internal use by audio framework if coming from outside
1753 // of audioserver
1754 switch(cmdCode) {
1755 case EFFECT_CMD_ENABLE:
1756 case EFFECT_CMD_DISABLE:
1757 case EFFECT_CMD_SET_PARAM:
1758 case EFFECT_CMD_SET_PARAM_DEFERRED:
1759 case EFFECT_CMD_SET_PARAM_COMMIT:
1760 case EFFECT_CMD_GET_PARAM:
1761 break;
1762 default:
1763 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1764 break;
1765 }
1766 android_errorWriteLog(0x534e4554, "62019992");
1767 return BAD_VALUE;
1768 }
1769
Eric Laurent1ffc5852016-12-15 14:46:09 -08001770 if (cmdCode == EFFECT_CMD_ENABLE) {
1771 if (*replySize < sizeof(int)) {
1772 android_errorWriteLog(0x534e4554, "32095713");
1773 return BAD_VALUE;
1774 }
1775 *(int *)pReplyData = NO_ERROR;
1776 *replySize = sizeof(int);
1777 return enable();
1778 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1779 if (*replySize < sizeof(int)) {
1780 android_errorWriteLog(0x534e4554, "32095713");
1781 return BAD_VALUE;
1782 }
1783 *(int *)pReplyData = NO_ERROR;
1784 *replySize = sizeof(int);
1785 return disable();
1786 }
1787
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001788 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001789 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790 if (effect == 0 || mDisconnected) {
1791 return DEAD_OBJECT;
1792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 // only get parameter command is permitted for applications not controlling the effect
1794 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1795 return INVALID_OPERATION;
1796 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001797
1798 // handle commands that are not forwarded transparently to effect engine
1799 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001800 if (mClient == 0) {
1801 return INVALID_OPERATION;
1802 }
1803
Eric Laurent1ffc5852016-12-15 14:46:09 -08001804 if (*replySize < sizeof(int)) {
1805 android_errorWriteLog(0x534e4554, "32095713");
1806 return BAD_VALUE;
1807 }
1808 *(int *)pReplyData = NO_ERROR;
1809 *replySize = sizeof(int);
1810
Eric Laurentca7cc822012-11-19 14:55:58 -08001811 // No need to trylock() here as this function is executed in the binder thread serving a
1812 // particular client process: no risk to block the whole media server process or mixer
1813 // threads if we are stuck here
1814 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001815 // keep local copy of index in case of client corruption b/32220769
1816 const uint32_t clientIndex = mCblk->clientIndex;
1817 const uint32_t serverIndex = mCblk->serverIndex;
1818 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1819 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 mCblk->serverIndex = 0;
1821 mCblk->clientIndex = 0;
1822 return BAD_VALUE;
1823 }
1824 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001825 effect_param_t *param = NULL;
1826 for (uint32_t index = serverIndex; index < clientIndex;) {
1827 int *p = (int *)(mBuffer + index);
1828 const int size = *p++;
1829 if (size < 0
1830 || size > EFFECT_PARAM_BUFFER_SIZE
1831 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001832 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001833 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001834 break;
1835 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001836
1837 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001838 auto *newParam = (effect_param_t *)realloc(param, size);
1839 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001840 ALOGW("command(): out of memory");
1841 status = NO_MEMORY;
1842 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 }
George Burgess IV80a22162020-01-05 20:06:15 -08001844 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001845 memcpy(param, p, size);
1846
1847 int reply = 0;
1848 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001849 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001850 size,
1851 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001852 &rsize,
1853 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001854
1855 // verify shared memory: server index shouldn't change; client index can't go back.
1856 if (serverIndex != mCblk->serverIndex
1857 || clientIndex > mCblk->clientIndex) {
1858 android_errorWriteLog(0x534e4554, "32220769");
1859 status = BAD_VALUE;
1860 break;
1861 }
1862
Eric Laurentca7cc822012-11-19 14:55:58 -08001863 // stop at first error encountered
1864 if (ret != NO_ERROR) {
1865 status = ret;
1866 *(int *)pReplyData = reply;
1867 break;
1868 } else if (reply != NO_ERROR) {
1869 *(int *)pReplyData = reply;
1870 break;
1871 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001872 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001874 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 mCblk->serverIndex = 0;
1876 mCblk->clientIndex = 0;
1877 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001878 }
1879
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001880 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001881}
1882
1883void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1884{
1885 ALOGV("setControl %p control %d", this, hasControl);
1886
1887 mHasControl = hasControl;
1888 mEnabled = enabled;
1889
1890 if (signal && mEffectClient != 0) {
1891 mEffectClient->controlStatusChanged(hasControl);
1892 }
1893}
1894
1895void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1896 uint32_t cmdSize,
1897 void *pCmdData,
1898 uint32_t replySize,
1899 void *pReplyData)
1900{
1901 if (mEffectClient != 0) {
1902 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1903 }
1904}
1905
1906
1907
1908void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1909{
1910 if (mEffectClient != 0) {
1911 mEffectClient->enableStatusChanged(enabled);
1912 }
1913}
1914
1915status_t AudioFlinger::EffectHandle::onTransact(
1916 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1917{
1918 return BnEffect::onTransact(code, data, reply, flags);
1919}
1920
1921
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001922void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001923{
1924 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1925
Marco Nelissenb2208842014-02-07 14:00:50 -08001926 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001927 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001928 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001929 mHasControl ? "yes" : "no",
1930 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 mCblk ? mCblk->clientIndex : 0,
1932 mCblk ? mCblk->serverIndex : 0
1933 );
1934
1935 if (locked) {
1936 mCblk->lock.unlock();
1937 }
1938}
1939
1940#undef LOG_TAG
1941#define LOG_TAG "AudioFlinger::EffectChain"
1942
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001943AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1944 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001945 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001946 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001947 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001948 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08001949{
1950 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001951 sp<ThreadBase> p = thread.promote();
1952 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001953 return;
1954 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001955 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
1956 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08001957}
1958
1959AudioFlinger::EffectChain::~EffectChain()
1960{
Eric Laurentca7cc822012-11-19 14:55:58 -08001961}
1962
1963// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1964sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1965 effect_descriptor_t *descriptor)
1966{
1967 size_t size = mEffects.size();
1968
1969 for (size_t i = 0; i < size; i++) {
1970 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1971 return mEffects[i];
1972 }
1973 }
1974 return 0;
1975}
1976
1977// getEffectFromId_l() must be called with ThreadBase::mLock held
1978sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1979{
1980 size_t size = mEffects.size();
1981
1982 for (size_t i = 0; i < size; i++) {
1983 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1984 if (id == 0 || mEffects[i]->id() == id) {
1985 return mEffects[i];
1986 }
1987 }
1988 return 0;
1989}
1990
1991// getEffectFromType_l() must be called with ThreadBase::mLock held
1992sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1993 const effect_uuid_t *type)
1994{
1995 size_t size = mEffects.size();
1996
1997 for (size_t i = 0; i < size; i++) {
1998 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1999 return mEffects[i];
2000 }
2001 }
2002 return 0;
2003}
2004
Eric Laurent6c796322019-04-09 14:13:17 -07002005std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2006{
2007 std::vector<int> ids;
2008 Mutex::Autolock _l(mLock);
2009 for (size_t i = 0; i < mEffects.size(); i++) {
2010 ids.push_back(mEffects[i]->id());
2011 }
2012 return ids;
2013}
2014
Eric Laurentca7cc822012-11-19 14:55:58 -08002015void AudioFlinger::EffectChain::clearInputBuffer()
2016{
2017 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002018 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002019}
2020
2021// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002022void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002023{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002024 if (mInBuffer == NULL) {
2025 return;
2026 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002027 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002028 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002029
Eric Laurent6b446ce2019-12-13 10:56:31 -08002030 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002031 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002032}
2033
2034// Must be called with EffectChain::mLock locked
2035void AudioFlinger::EffectChain::process_l()
2036{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002037 // never process effects when:
2038 // - on an OFFLOAD thread
2039 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002040 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002041 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002042 bool tracksOnSession = (trackCnt() != 0);
2043
2044 if (!tracksOnSession && mTailBufferCount == 0) {
2045 doProcess = false;
2046 }
2047
2048 if (activeTrackCnt() == 0) {
2049 // if no track is active and the effect tail has not been rendered,
2050 // the input buffer must be cleared here as the mixer process will not do it
2051 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002052 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002053 if (mTailBufferCount > 0) {
2054 mTailBufferCount--;
2055 }
2056 }
2057 }
2058 }
2059
2060 size_t size = mEffects.size();
2061 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002062 // Only the input and output buffers of the chain can be external,
2063 // and 'update' / 'commit' do nothing for allocated buffers, thus
2064 // it's not needed to consider any other buffers here.
2065 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002066 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2067 mOutBuffer->update();
2068 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002069 for (size_t i = 0; i < size; i++) {
2070 mEffects[i]->process();
2071 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002072 mInBuffer->commit();
2073 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2074 mOutBuffer->commit();
2075 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002076 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002077 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002078 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002079 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2080 }
2081 if (doResetVolume) {
2082 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002083 }
2084}
2085
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002086// createEffect_l() must be called with ThreadBase::mLock held
2087status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002088 effect_descriptor_t *desc,
2089 int id,
2090 audio_session_t sessionId,
2091 bool pinned)
2092{
2093 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002094 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002095 status_t lStatus = effect->status();
2096 if (lStatus == NO_ERROR) {
2097 lStatus = addEffect_ll(effect);
2098 }
2099 if (lStatus != NO_ERROR) {
2100 effect.clear();
2101 }
2102 return lStatus;
2103}
2104
2105// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002106status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2107{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002108 Mutex::Autolock _l(mLock);
2109 return addEffect_ll(effect);
2110}
2111// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2112status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2113{
Eric Laurentca7cc822012-11-19 14:55:58 -08002114 effect_descriptor_t desc = effect->desc();
2115 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2116
Eric Laurent6b446ce2019-12-13 10:56:31 -08002117 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002118
2119 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2120 // Auxiliary effects are inserted at the beginning of mEffects vector as
2121 // they are processed first and accumulated in chain input buffer
2122 mEffects.insertAt(effect, 0);
2123
2124 // the input buffer for auxiliary effect contains mono samples in
2125 // 32 bit format. This is to avoid saturation in AudoMixer
2126 // accumulation stage. Saturation is done in EffectModule::process() before
2127 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002128 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002129 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002130#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002131 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002132 numSamples * sizeof(float), &halBuffer);
2133#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002134 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002135 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002136#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002137 if (result != OK) return result;
2138 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002139 // auxiliary effects output samples to chain input buffer for further processing
2140 // by insert effects
2141 effect->setOutBuffer(mInBuffer);
2142 } else {
2143 // Insert effects are inserted at the end of mEffects vector as they are processed
2144 // after track and auxiliary effects.
2145 // Insert effect order as a function of indicated preference:
2146 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2147 // another effect is present
2148 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2149 // last effect claiming first position
2150 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2151 // first effect claiming last position
2152 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2153 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2154 // already present
2155
2156 size_t size = mEffects.size();
2157 size_t idx_insert = size;
2158 ssize_t idx_insert_first = -1;
2159 ssize_t idx_insert_last = -1;
2160
2161 for (size_t i = 0; i < size; i++) {
2162 effect_descriptor_t d = mEffects[i]->desc();
2163 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2164 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2165 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2166 // check invalid effect chaining combinations
2167 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2168 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2169 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2170 desc.name, d.name);
2171 return INVALID_OPERATION;
2172 }
2173 // remember position of first insert effect and by default
2174 // select this as insert position for new effect
2175 if (idx_insert == size) {
2176 idx_insert = i;
2177 }
2178 // remember position of last insert effect claiming
2179 // first position
2180 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2181 idx_insert_first = i;
2182 }
2183 // remember position of first insert effect claiming
2184 // last position
2185 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2186 idx_insert_last == -1) {
2187 idx_insert_last = i;
2188 }
2189 }
2190 }
2191
2192 // modify idx_insert from first position if needed
2193 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2194 if (idx_insert_last != -1) {
2195 idx_insert = idx_insert_last;
2196 } else {
2197 idx_insert = size;
2198 }
2199 } else {
2200 if (idx_insert_first != -1) {
2201 idx_insert = idx_insert_first + 1;
2202 }
2203 }
2204
2205 // always read samples from chain input buffer
2206 effect->setInBuffer(mInBuffer);
2207
2208 // if last effect in the chain, output samples to chain
2209 // output buffer, otherwise to chain input buffer
2210 if (idx_insert == size) {
2211 if (idx_insert != 0) {
2212 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2213 mEffects[idx_insert-1]->configure();
2214 }
2215 effect->setOutBuffer(mOutBuffer);
2216 } else {
2217 effect->setOutBuffer(mInBuffer);
2218 }
2219 mEffects.insertAt(effect, idx_insert);
2220
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002221 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002222 idx_insert);
2223 }
2224 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002225
Eric Laurentca7cc822012-11-19 14:55:58 -08002226 return NO_ERROR;
2227}
2228
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002229// removeEffect_l() must be called with ThreadBase::mLock held
2230size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2231 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002232{
2233 Mutex::Autolock _l(mLock);
2234 size_t size = mEffects.size();
2235 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2236
2237 for (size_t i = 0; i < size; i++) {
2238 if (effect == mEffects[i]) {
2239 // calling stop here will remove pre-processing effect from the audio HAL.
2240 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2241 // the middle of a read from audio HAL
2242 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2243 mEffects[i]->state() == EffectModule::STOPPING) {
2244 mEffects[i]->stop();
2245 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002246 if (release) {
2247 mEffects[i]->release_l();
2248 }
2249
Mikhail Naganov022b9952017-01-04 16:36:51 -08002250 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002251 if (i == size - 1 && i != 0) {
2252 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2253 mEffects[i - 1]->configure();
2254 }
2255 }
2256 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002257 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002258 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002259
Eric Laurentca7cc822012-11-19 14:55:58 -08002260 break;
2261 }
2262 }
2263
2264 return mEffects.size();
2265}
2266
jiabin8f278ee2019-11-11 12:16:27 -08002267// setDevices_l() must be called with ThreadBase::mLock held
2268void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002269{
2270 size_t size = mEffects.size();
2271 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002272 mEffects[i]->setDevices(devices);
2273 }
2274}
2275
2276// setInputDevice_l() must be called with ThreadBase::mLock held
2277void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2278{
2279 size_t size = mEffects.size();
2280 for (size_t i = 0; i < size; i++) {
2281 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002282 }
2283}
2284
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002285// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002286void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2287{
2288 size_t size = mEffects.size();
2289 for (size_t i = 0; i < size; i++) {
2290 mEffects[i]->setMode(mode);
2291 }
2292}
2293
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002294// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002295void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2296{
2297 size_t size = mEffects.size();
2298 for (size_t i = 0; i < size; i++) {
2299 mEffects[i]->setAudioSource(source);
2300 }
2301}
2302
Zhou Songd505c642020-02-20 16:35:37 +08002303bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2304 for (const auto &effect : mEffects) {
2305 if (effect->isVolumeControlEnabled()) return true;
2306 }
2307 return false;
2308}
2309
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002310// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002311bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002312{
2313 uint32_t newLeft = *left;
2314 uint32_t newRight = *right;
2315 bool hasControl = false;
2316 int ctrlIdx = -1;
2317 size_t size = mEffects.size();
2318
2319 // first update volume controller
2320 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002321 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002322 ctrlIdx = i - 1;
2323 hasControl = true;
2324 break;
2325 }
2326 }
2327
Eric Laurentfa1e1232016-08-02 19:01:49 -07002328 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002329 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002330 if (hasControl) {
2331 *left = mNewLeftVolume;
2332 *right = mNewRightVolume;
2333 }
2334 return hasControl;
2335 }
2336
2337 mVolumeCtrlIdx = ctrlIdx;
2338 mLeftVolume = newLeft;
2339 mRightVolume = newRight;
2340
2341 // second get volume update from volume controller
2342 if (ctrlIdx >= 0) {
2343 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2344 mNewLeftVolume = newLeft;
2345 mNewRightVolume = newRight;
2346 }
2347 // then indicate volume to all other effects in chain.
2348 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002349 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002350 uint32_t lVol = newLeft;
2351 uint32_t rVol = newRight;
2352
2353 for (size_t i = 0; i < size; i++) {
2354 if ((int)i == ctrlIdx) {
2355 continue;
2356 }
2357 // this also works for ctrlIdx == -1 when there is no volume controller
2358 if ((int)i > ctrlIdx) {
2359 lVol = *left;
2360 rVol = *right;
2361 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002362 // Pass requested volume directly if this is volume monitor module
2363 if (mEffects[i]->isVolumeMonitor()) {
2364 mEffects[i]->setVolume(left, right, false);
2365 } else {
2366 mEffects[i]->setVolume(&lVol, &rVol, false);
2367 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002368 }
2369 *left = newLeft;
2370 *right = newRight;
2371
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002372 setVolumeForOutput_l(*left, *right);
2373
Eric Laurentca7cc822012-11-19 14:55:58 -08002374 return hasControl;
2375}
2376
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002377// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002378void AudioFlinger::EffectChain::resetVolume_l()
2379{
Eric Laurente7449bf2016-08-03 18:44:07 -07002380 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2381 uint32_t left = mLeftVolume;
2382 uint32_t right = mRightVolume;
2383 (void)setVolume_l(&left, &right, true);
2384 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002385}
2386
Eric Laurent1b928682014-10-02 19:41:47 -07002387void AudioFlinger::EffectChain::syncHalEffectsState()
2388{
2389 Mutex::Autolock _l(mLock);
2390 for (size_t i = 0; i < mEffects.size(); i++) {
2391 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2392 mEffects[i]->state() == EffectModule::STOPPING) {
2393 mEffects[i]->addEffectToHal_l();
2394 }
2395 }
2396}
2397
Eric Laurentca7cc822012-11-19 14:55:58 -08002398void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2399{
Eric Laurentca7cc822012-11-19 14:55:58 -08002400 String8 result;
2401
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002402 const size_t numEffects = mEffects.size();
2403 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002404
Marco Nelissenb2208842014-02-07 14:00:50 -08002405 if (numEffects) {
2406 bool locked = AudioFlinger::dumpTryLock(mLock);
2407 // failed to lock - AudioFlinger is probably deadlocked
2408 if (!locked) {
2409 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002410 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002411
Andy Hungbded9c82017-11-30 18:47:35 -08002412 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2413 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2414 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2415 (int)inBufferStr.size(), "In buffer ",
2416 (int)outBufferStr.size(), "Out buffer ");
2417 result.appendFormat("\t%s %s %d\n",
2418 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002419 write(fd, result.string(), result.size());
2420
2421 for (size_t i = 0; i < numEffects; ++i) {
2422 sp<EffectModule> effect = mEffects[i];
2423 if (effect != 0) {
2424 effect->dump(fd, args);
2425 }
2426 }
2427
2428 if (locked) {
2429 mLock.unlock();
2430 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002431 } else {
2432 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002433 }
2434}
2435
2436// must be called with ThreadBase::mLock held
2437void AudioFlinger::EffectChain::setEffectSuspended_l(
2438 const effect_uuid_t *type, bool suspend)
2439{
2440 sp<SuspendedEffectDesc> desc;
2441 // use effect type UUID timelow as key as there is no real risk of identical
2442 // timeLow fields among effect type UUIDs.
2443 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2444 if (suspend) {
2445 if (index >= 0) {
2446 desc = mSuspendedEffects.valueAt(index);
2447 } else {
2448 desc = new SuspendedEffectDesc();
2449 desc->mType = *type;
2450 mSuspendedEffects.add(type->timeLow, desc);
2451 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2452 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002453
Eric Laurentca7cc822012-11-19 14:55:58 -08002454 if (desc->mRefCount++ == 0) {
2455 sp<EffectModule> effect = getEffectIfEnabled(type);
2456 if (effect != 0) {
2457 desc->mEffect = effect;
2458 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002459 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002460 }
2461 }
2462 } else {
2463 if (index < 0) {
2464 return;
2465 }
2466 desc = mSuspendedEffects.valueAt(index);
2467 if (desc->mRefCount <= 0) {
2468 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002469 desc->mRefCount = 0;
2470 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002471 }
2472 if (--desc->mRefCount == 0) {
2473 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2474 if (desc->mEffect != 0) {
2475 sp<EffectModule> effect = desc->mEffect.promote();
2476 if (effect != 0) {
2477 effect->setSuspended(false);
2478 effect->lock();
2479 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002480 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002481 effect->setEnabled_l(handle->enabled());
2482 }
2483 effect->unlock();
2484 }
2485 desc->mEffect.clear();
2486 }
2487 mSuspendedEffects.removeItemsAt(index);
2488 }
2489 }
2490}
2491
2492// must be called with ThreadBase::mLock held
2493void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2494{
2495 sp<SuspendedEffectDesc> desc;
2496
2497 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2498 if (suspend) {
2499 if (index >= 0) {
2500 desc = mSuspendedEffects.valueAt(index);
2501 } else {
2502 desc = new SuspendedEffectDesc();
2503 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2504 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2505 }
2506 if (desc->mRefCount++ == 0) {
2507 Vector< sp<EffectModule> > effects;
2508 getSuspendEligibleEffects(effects);
2509 for (size_t i = 0; i < effects.size(); i++) {
2510 setEffectSuspended_l(&effects[i]->desc().type, true);
2511 }
2512 }
2513 } else {
2514 if (index < 0) {
2515 return;
2516 }
2517 desc = mSuspendedEffects.valueAt(index);
2518 if (desc->mRefCount <= 0) {
2519 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2520 desc->mRefCount = 1;
2521 }
2522 if (--desc->mRefCount == 0) {
2523 Vector<const effect_uuid_t *> types;
2524 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2525 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2526 continue;
2527 }
2528 types.add(&mSuspendedEffects.valueAt(i)->mType);
2529 }
2530 for (size_t i = 0; i < types.size(); i++) {
2531 setEffectSuspended_l(types[i], false);
2532 }
2533 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2534 mSuspendedEffects.keyAt(index));
2535 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2536 }
2537 }
2538}
2539
2540
2541// The volume effect is used for automated tests only
2542#ifndef OPENSL_ES_H_
2543static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2544 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2545const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2546#endif //OPENSL_ES_H_
2547
Eric Laurentd8365c52017-07-16 15:27:05 -07002548/* static */
2549bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2550{
2551 // Only NS and AEC are suspended when BtNRec is off
2552 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2553 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2554 return true;
2555 }
2556 return false;
2557}
2558
Eric Laurentca7cc822012-11-19 14:55:58 -08002559bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2560{
2561 // auxiliary effects and visualizer are never suspended on output mix
2562 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2563 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2564 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002565 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2566 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002567 return false;
2568 }
2569 return true;
2570}
2571
2572void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2573 Vector< sp<AudioFlinger::EffectModule> > &effects)
2574{
2575 effects.clear();
2576 for (size_t i = 0; i < mEffects.size(); i++) {
2577 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2578 effects.add(mEffects[i]);
2579 }
2580 }
2581}
2582
2583sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2584 const effect_uuid_t *type)
2585{
2586 sp<EffectModule> effect = getEffectFromType_l(type);
2587 return effect != 0 && effect->isEnabled() ? effect : 0;
2588}
2589
2590void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2591 bool enabled)
2592{
2593 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2594 if (enabled) {
2595 if (index < 0) {
2596 // if the effect is not suspend check if all effects are suspended
2597 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2598 if (index < 0) {
2599 return;
2600 }
2601 if (!isEffectEligibleForSuspend(effect->desc())) {
2602 return;
2603 }
2604 setEffectSuspended_l(&effect->desc().type, enabled);
2605 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2606 if (index < 0) {
2607 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2608 return;
2609 }
2610 }
2611 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2612 effect->desc().type.timeLow);
2613 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002614 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002615 if (desc->mEffect == 0) {
2616 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002617 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002618 effect->setSuspended(true);
2619 }
2620 } else {
2621 if (index < 0) {
2622 return;
2623 }
2624 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2625 effect->desc().type.timeLow);
2626 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2627 desc->mEffect.clear();
2628 effect->setSuspended(false);
2629 }
2630}
2631
Eric Laurent5baf2af2013-09-12 17:37:00 -07002632bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002633{
2634 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002635 return isNonOffloadableEnabled_l();
2636}
2637
2638bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2639{
Eric Laurent813e2a72013-08-31 12:59:48 -07002640 size_t size = mEffects.size();
2641 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002642 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002643 return true;
2644 }
2645 }
2646 return false;
2647}
2648
Eric Laurentaaa44472014-09-12 17:41:50 -07002649void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2650{
2651 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002652 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002653}
2654
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002655void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2656{
2657 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2658 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2659 }
2660 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2661 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2662 }
2663}
2664
2665void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2666{
2667 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2668 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2669 }
2670 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2671 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2672 }
2673}
2674
2675bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002676{
2677 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002678 for (const auto &effect : mEffects) {
2679 if (effect->isProcessImplemented()) {
2680 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002681 }
2682 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002683 // Allow effects without processing.
2684 return true;
2685}
2686
2687bool AudioFlinger::EffectChain::isFastCompatible() const
2688{
2689 Mutex::Autolock _l(mLock);
2690 for (const auto &effect : mEffects) {
2691 if (effect->isProcessImplemented()
2692 && effect->isImplementationSoftware()) {
2693 return false;
2694 }
2695 }
2696 // Allow effects without processing or hw accelerated effects.
2697 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002698}
2699
2700// isCompatibleWithThread_l() must be called with thread->mLock held
2701bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2702{
2703 Mutex::Autolock _l(mLock);
2704 for (size_t i = 0; i < mEffects.size(); i++) {
2705 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2706 return false;
2707 }
2708 }
2709 return true;
2710}
2711
Eric Laurent6b446ce2019-12-13 10:56:31 -08002712// EffectCallbackInterface implementation
2713status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2714 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2715 sp<EffectHalInterface> *effect) {
2716 status_t status = NO_INIT;
2717 sp<AudioFlinger> af = mAudioFlinger.promote();
2718 if (af == nullptr) {
2719 return status;
2720 }
2721 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2722 if (effectsFactory != 0) {
2723 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2724 }
2725 return status;
2726}
2727
2728bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002729 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002730 sp<AudioFlinger> af = mAudioFlinger.promote();
2731 if (af == nullptr) {
2732 return false;
2733 }
Eric Laurent41709552019-12-16 19:34:05 -08002734 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2735 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002736}
2737
2738status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2739 size_t size, sp<EffectBufferHalInterface>* buffer) {
2740 sp<AudioFlinger> af = mAudioFlinger.promote();
2741 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2742 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2743}
2744
2745status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2746 sp<EffectHalInterface> effect) {
2747 status_t result = NO_INIT;
2748 sp<ThreadBase> t = mThread.promote();
2749 if (t == nullptr) {
2750 return result;
2751 }
2752 sp <StreamHalInterface> st = t->stream();
2753 if (st == nullptr) {
2754 return result;
2755 }
2756 result = st->addEffect(effect);
2757 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2758 return result;
2759}
2760
2761status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2762 sp<EffectHalInterface> effect) {
2763 status_t result = NO_INIT;
2764 sp<ThreadBase> t = mThread.promote();
2765 if (t == nullptr) {
2766 return result;
2767 }
2768 sp <StreamHalInterface> st = t->stream();
2769 if (st == nullptr) {
2770 return result;
2771 }
2772 result = st->removeEffect(effect);
2773 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2774 return result;
2775}
2776
2777audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2778 sp<ThreadBase> t = mThread.promote();
2779 if (t == nullptr) {
2780 return AUDIO_IO_HANDLE_NONE;
2781 }
2782 return t->id();
2783}
2784
2785bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2786 sp<ThreadBase> t = mThread.promote();
2787 if (t == nullptr) {
2788 return true;
2789 }
2790 return t->isOutput();
2791}
2792
2793bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2794 sp<ThreadBase> t = mThread.promote();
2795 if (t == nullptr) {
2796 return false;
2797 }
2798 return t->type() == ThreadBase::OFFLOAD;
2799}
2800
2801bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2802 sp<ThreadBase> t = mThread.promote();
2803 if (t == nullptr) {
2804 return false;
2805 }
2806 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2807}
2808
2809bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2810 sp<ThreadBase> t = mThread.promote();
2811 if (t == nullptr) {
2812 return false;
2813 }
2814 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2815}
2816
2817uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2818 sp<ThreadBase> t = mThread.promote();
2819 if (t == nullptr) {
2820 return 0;
2821 }
2822 return t->sampleRate();
2823}
2824
2825audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2826 sp<ThreadBase> t = mThread.promote();
2827 if (t == nullptr) {
2828 return AUDIO_CHANNEL_NONE;
2829 }
2830 return t->channelMask();
2831}
2832
2833uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2834 sp<ThreadBase> t = mThread.promote();
2835 if (t == nullptr) {
2836 return 0;
2837 }
2838 return t->channelCount();
2839}
2840
2841size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2842 sp<ThreadBase> t = mThread.promote();
2843 if (t == nullptr) {
2844 return 0;
2845 }
2846 return t->frameCount();
2847}
2848
2849uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2850 sp<ThreadBase> t = mThread.promote();
2851 if (t == nullptr) {
2852 return 0;
2853 }
2854 return t->latency_l();
2855}
2856
2857void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2858 sp<ThreadBase> t = mThread.promote();
2859 if (t == nullptr) {
2860 return;
2861 }
2862 t->setVolumeForOutput_l(left, right);
2863}
2864
2865void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002866 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002867 sp<ThreadBase> t = mThread.promote();
2868 if (t == nullptr) {
2869 return;
2870 }
2871 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2872
2873 sp<EffectChain> c = mChain.promote();
2874 if (c == nullptr) {
2875 return;
2876 }
Eric Laurent41709552019-12-16 19:34:05 -08002877 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2878 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002879}
2880
Eric Laurent41709552019-12-16 19:34:05 -08002881void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002882 sp<ThreadBase> t = mThread.promote();
2883 if (t == nullptr) {
2884 return;
2885 }
Eric Laurent41709552019-12-16 19:34:05 -08002886 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2887 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002888}
2889
Eric Laurent41709552019-12-16 19:34:05 -08002890void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002891 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2892
2893 sp<ThreadBase> t = mThread.promote();
2894 if (t == nullptr) {
2895 return;
2896 }
2897 t->onEffectDisable();
2898}
2899
2900bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2901 bool unpinIfLast) {
2902 sp<ThreadBase> t = mThread.promote();
2903 if (t == nullptr) {
2904 return false;
2905 }
2906 t->disconnectEffectHandle(handle, unpinIfLast);
2907 return true;
2908}
2909
2910void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2911 sp<EffectChain> c = mChain.promote();
2912 if (c == nullptr) {
2913 return;
2914 }
2915 c->resetVolume_l();
2916
2917}
2918
2919uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2920 sp<EffectChain> c = mChain.promote();
2921 if (c == nullptr) {
2922 return PRODUCT_STRATEGY_NONE;
2923 }
2924 return c->strategy();
2925}
2926
2927int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2928 sp<EffectChain> c = mChain.promote();
2929 if (c == nullptr) {
2930 return 0;
2931 }
2932 return c->activeTrackCnt();
2933}
2934
Eric Laurentb82e6b72019-11-22 17:25:04 -08002935
2936#undef LOG_TAG
2937#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2938
2939status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2940{
2941 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2942 Mutex::Autolock _l(mProxyLock);
2943 if (status == NO_ERROR) {
2944 for (auto& handle : mEffectHandles) {
2945 if (enabled) {
2946 status = handle.second->enable();
2947 } else {
2948 status = handle.second->disable();
2949 }
2950 }
2951 }
2952 ALOGV("%s enable %d status %d", __func__, enabled, status);
2953 return status;
2954}
2955
2956status_t AudioFlinger::DeviceEffectProxy::init(
2957 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2958//For all audio patches
2959//If src or sink device match
2960//If the effect is HW accelerated
2961// if no corresponding effect module
2962// Create EffectModule: mHalEffect
2963//Create and attach EffectHandle
2964//If the effect is not HW accelerated and the patch sink or src is a mixer port
2965// Create Effect on patch input or output thread on session -1
2966//Add EffectHandle to EffectHandle map of Effect Proxy:
2967 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2968 status_t status = NO_ERROR;
2969 for (auto &patch : patches) {
2970 status = onCreatePatch(patch.first, patch.second);
2971 ALOGV("%s onCreatePatch status %d", __func__, status);
2972 if (status == BAD_VALUE) {
2973 return status;
2974 }
2975 }
2976 return status;
2977}
2978
2979status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2980 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2981 status_t status = NAME_NOT_FOUND;
2982 sp<EffectHandle> handle;
2983 // only consider source[0] as this is the only "true" source of a patch
2984 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2985 ALOGV("%s source checkPort status %d", __func__, status);
2986 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2987 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2988 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2989 }
2990 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2991 Mutex::Autolock _l(mProxyLock);
2992 mEffectHandles.emplace(patchHandle, handle);
2993 }
2994 ALOGW_IF(status == BAD_VALUE,
2995 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2996
2997 return status;
2998}
2999
3000status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3001 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3002
3003 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3004 __func__, port->type, port->ext.device.type,
3005 port->ext.device.address, port->id, patch.isSoftware());
3006 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
3007 || port->ext.device.address != mDevice.mAddress) {
3008 return NAME_NOT_FOUND;
3009 }
3010 status_t status = NAME_NOT_FOUND;
3011
3012 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3013 Mutex::Autolock _l(mProxyLock);
3014 mDevicePort = *port;
3015 mHalEffect = new EffectModule(mMyCallback,
3016 const_cast<effect_descriptor_t *>(&mDescriptor),
3017 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3018 false /* pinned */, port->id);
3019 if (audio_is_input_device(mDevice.mType)) {
3020 mHalEffect->setInputDevice(mDevice);
3021 } else {
3022 mHalEffect->setDevices({mDevice});
3023 }
3024 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3025 status = (*handle)->initCheck();
3026 if (status == OK) {
3027 status = mHalEffect->addHandle((*handle).get());
3028 } else {
3029 mHalEffect.clear();
3030 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3031 }
3032 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3033 sp <ThreadBase> thread;
3034 if (audio_port_config_has_input_direction(port)) {
3035 if (patch.isSoftware()) {
3036 thread = patch.mRecord.thread();
3037 } else {
3038 thread = patch.thread().promote();
3039 }
3040 } else {
3041 if (patch.isSoftware()) {
3042 thread = patch.mPlayback.thread();
3043 } else {
3044 thread = patch.thread().promote();
3045 }
3046 }
3047 int enabled;
3048 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3049 const_cast<effect_descriptor_t *>(&mDescriptor),
3050 &enabled, &status, false);
3051 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3052 } else {
3053 status = BAD_VALUE;
3054 }
3055 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3056 if (isEnabled()) {
3057 (*handle)->enable();
3058 } else {
3059 (*handle)->disable();
3060 }
3061 }
3062 return status;
3063}
3064
3065void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3066 Mutex::Autolock _l(mProxyLock);
3067 mEffectHandles.erase(patchHandle);
3068}
3069
3070
3071size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3072{
3073 Mutex::Autolock _l(mProxyLock);
3074 if (effect == mHalEffect) {
3075 mHalEffect.clear();
3076 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3077 }
3078 return mHalEffect == nullptr ? 0 : 1;
3079}
3080
3081status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3082 sp<EffectHalInterface> effect) {
3083 if (mHalEffect == nullptr) {
3084 return NO_INIT;
3085 }
3086 return mManagerCallback->addEffectToHal(
3087 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3088}
3089
3090status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3091 sp<EffectHalInterface> effect) {
3092 if (mHalEffect == nullptr) {
3093 return NO_INIT;
3094 }
3095 return mManagerCallback->removeEffectFromHal(
3096 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3097}
3098
3099bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3100 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3101 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3102 }
3103 return true;
3104}
3105
3106uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3107 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3108 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3109 return mDevicePort.sample_rate;
3110 }
3111 return DEFAULT_OUTPUT_SAMPLE_RATE;
3112}
3113
3114audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3115 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3116 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3117 return mDevicePort.channel_mask;
3118 }
3119 return AUDIO_CHANNEL_OUT_STEREO;
3120}
3121
3122uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3123 if (isOutput()) {
3124 return audio_channel_count_from_out_mask(channelMask());
3125 }
3126 return audio_channel_count_from_in_mask(channelMask());
3127}
3128
3129void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3130 const Vector<String16> args;
3131 EffectBase::dump(fd, args);
3132
3133 const bool locked = dumpTryLock(mProxyLock);
3134 if (!locked) {
3135 String8 result("DeviceEffectProxy may be deadlocked\n");
3136 write(fd, result.string(), result.size());
3137 }
3138
3139 String8 outStr;
3140 if (mHalEffect != nullptr) {
3141 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3142 } else {
3143 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3144 }
3145 write(fd, outStr.string(), outStr.size());
3146 outStr.clear();
3147
3148 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3149 write(fd, outStr.string(), outStr.size());
3150 outStr.clear();
3151
3152 for (const auto& iter : mEffectHandles) {
3153 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3154 write(fd, outStr.string(), outStr.size());
3155 outStr.clear();
3156 sp<EffectBase> effect = iter.second->effect().promote();
3157 if (effect != nullptr) {
3158 effect->dump(fd, args);
3159 }
3160 }
3161
3162 if (locked) {
3163 mLock.unlock();
3164 }
3165}
3166
3167#undef LOG_TAG
3168#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3169
3170int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3171 return mManagerCallback->newEffectId();
3172}
3173
3174
3175bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3176 EffectHandle *handle, bool unpinIfLast) {
3177 sp<EffectBase> effectBase = handle->effect().promote();
3178 if (effectBase == nullptr) {
3179 return false;
3180 }
3181
3182 sp<EffectModule> effect = effectBase->asEffectModule();
3183 if (effect == nullptr) {
3184 return false;
3185 }
3186
3187 // restore suspended effects if the disconnected handle was enabled and the last one.
3188 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3189 if (remove) {
3190 sp<DeviceEffectProxy> proxy = mProxy.promote();
3191 if (proxy != nullptr) {
3192 proxy->removeEffect(effect);
3193 }
3194 if (handle->enabled()) {
3195 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3196 }
3197 }
3198 return true;
3199}
3200
3201status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3202 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3203 sp<EffectHalInterface> *effect) {
3204 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3205}
3206
3207status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3208 sp<EffectHalInterface> effect) {
3209 sp<DeviceEffectProxy> proxy = mProxy.promote();
3210 if (proxy == nullptr) {
3211 return NO_INIT;
3212 }
3213 return proxy->addEffectToHal(effect);
3214}
3215
3216status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3217 sp<EffectHalInterface> effect) {
3218 sp<DeviceEffectProxy> proxy = mProxy.promote();
3219 if (proxy == nullptr) {
3220 return NO_INIT;
3221 }
3222 return proxy->addEffectToHal(effect);
3223}
3224
3225bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3226 sp<DeviceEffectProxy> proxy = mProxy.promote();
3227 if (proxy == nullptr) {
3228 return true;
3229 }
3230 return proxy->isOutput();
3231}
3232
3233uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3234 sp<DeviceEffectProxy> proxy = mProxy.promote();
3235 if (proxy == nullptr) {
3236 return DEFAULT_OUTPUT_SAMPLE_RATE;
3237 }
3238 return proxy->sampleRate();
3239}
3240
3241audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3242 sp<DeviceEffectProxy> proxy = mProxy.promote();
3243 if (proxy == nullptr) {
3244 return AUDIO_CHANNEL_OUT_STEREO;
3245 }
3246 return proxy->channelMask();
3247}
3248
3249uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3250 sp<DeviceEffectProxy> proxy = mProxy.promote();
3251 if (proxy == nullptr) {
3252 return 2;
3253 }
3254 return proxy->channelCount();
3255}
3256
Glenn Kasten63238ef2015-03-02 15:50:29 -08003257} // namespace android