blob: 7f34c146f30e80d86495618a99c518428f8ed7a8 [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>
jiabinb8269fd2019-11-11 12:16:27 -080032#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070033#include <media/AudioEffect.h>
jiabinb8269fd2019-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 Laurente0b9a362019-12-16 19:34:05 -080061// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080062// ----------------------------------------------------------------------------
63
64#undef LOG_TAG
Eric Laurente0b9a362019-12-16 19:34:05 -080065#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080066
Eric Laurente0b9a362019-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 Laurent5d885392019-12-13 10:56:31 -080073 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurent5d885392019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurente0b9a362019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurente0b9a362019-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,
515 bool pinned)
516 : EffectBase(callback, desc, id, sessionId, pinned),
517 // clear mConfig to ensure consistent initial value of buffer framecount
518 // in case buffers are associated by setInBuffer() or setOutBuffer()
519 // prior to configure().
520 mConfig{{}, {}},
521 mStatus(NO_INIT),
522 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
523 mDisableWaitCnt(0), // set by process() and updateState()
524 mOffloaded(false)
525#ifdef FLOAT_EFFECT_CHAIN
526 , mSupportsFloat(false)
527#endif
528{
529 ALOGV("Constructor %p pinned %d", this, pinned);
530 int lStatus;
531
532 // create effect engine from effect factory
533 mStatus = callback->createEffectHal(
534 &desc->uuid, sessionId, AUDIO_PORT_HANDLE_NONE, &mEffectInterface);
535 if (mStatus != NO_ERROR) {
536 return;
537 }
538 lStatus = init();
539 if (lStatus < 0) {
540 mStatus = lStatus;
541 goto Error;
542 }
543
544 setOffloaded(callback->isOffload(), callback->io());
545 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
546
547 return;
548Error:
549 mEffectInterface.clear();
550 ALOGV("Constructor Error %d", mStatus);
551}
552
553AudioFlinger::EffectModule::~EffectModule()
554{
555 ALOGV("Destructor %p", this);
556 if (mEffectInterface != 0) {
557 char uuidStr[64];
558 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
559 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
560 this, uuidStr);
561 release_l();
562 }
563
564}
565
566ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
567{
568 ssize_t status = EffectBase::removeHandle_l(handle);
569
570 // Prevent calls to process() and other functions on effect interface from now on.
571 // The effect engine will be released by the destructor when the last strong reference on
572 // this object is released which can happen after next process is called.
573 if (status == 0 && !mPinned) {
574 mEffectInterface->close();
575 }
576
577 return status;
578}
579
Eric Laurentfa1e1232016-08-02 19:01:49 -0700580bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800581 Mutex::Autolock _l(mLock);
582
Eric Laurentfa1e1232016-08-02 19:01:49 -0700583 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800584 switch (mState) {
585 case RESTART:
586 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700587 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800588
589 case STARTING:
590 // clear auxiliary effect input buffer for next accumulation
591 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
592 memset(mConfig.inputCfg.buffer.raw,
593 0,
594 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
595 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700596 if (start_l() == NO_ERROR) {
597 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700598 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700599 } else {
600 mState = IDLE;
601 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800602 break;
603 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900604 // volume control for offload and direct threads must take effect immediately.
605 if (stop_l() == NO_ERROR
606 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700607 mDisableWaitCnt = mMaxDisableWaitCnt;
608 } else {
609 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
610 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800611 mState = STOPPED;
612 break;
613 case STOPPED:
614 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
615 // turn off sequence.
616 if (--mDisableWaitCnt == 0) {
617 reset_l();
618 mState = IDLE;
619 }
620 break;
621 default: //IDLE , ACTIVE, DESTROYED
622 break;
623 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700624
625 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800626}
627
628void AudioFlinger::EffectModule::process()
629{
630 Mutex::Autolock _l(mLock);
631
Mikhail Naganov022b9952017-01-04 16:36:51 -0800632 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800633 return;
634 }
635
rago94a1ee82017-07-21 15:11:02 -0700636 const uint32_t inChannelCount =
637 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
638 const uint32_t outChannelCount =
639 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
640 const bool auxType =
641 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
642
Andy Hungfa69ca32017-11-30 10:07:53 -0800643 // safeInputOutputSampleCount is 0 if the channel count between input and output
644 // buffers do not match. This prevents automatic accumulation or copying between the
645 // input and output effect buffers without an intermediary effect process.
646 // TODO: consider implementing channel conversion.
647 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700648 mInChannelCountRequested != mOutChannelCountRequested ? 0
649 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800650 mConfig.inputCfg.buffer.frameCount,
651 mConfig.outputCfg.buffer.frameCount);
652 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
653#ifdef FLOAT_EFFECT_CHAIN
654 accumulate_float(
655 mConfig.outputCfg.buffer.f32,
656 mConfig.inputCfg.buffer.f32,
657 safeInputOutputSampleCount);
658#else
659 accumulate_i16(
660 mConfig.outputCfg.buffer.s16,
661 mConfig.inputCfg.buffer.s16,
662 safeInputOutputSampleCount);
663#endif
664 };
665 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
666#ifdef FLOAT_EFFECT_CHAIN
667 memcpy(
668 mConfig.outputCfg.buffer.f32,
669 mConfig.inputCfg.buffer.f32,
670 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
671
672#else
673 memcpy(
674 mConfig.outputCfg.buffer.s16,
675 mConfig.inputCfg.buffer.s16,
676 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
677#endif
678 };
679
Eric Laurentca7cc822012-11-19 14:55:58 -0800680 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700681 int ret;
682 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700683 if (auxType) {
684 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800685 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700686#ifdef FLOAT_EFFECT_CHAIN
687 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800688#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700689 // Do in-place float conversion for auxiliary effect input buffer.
690 static_assert(sizeof(float) <= sizeof(int32_t),
691 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
692
Andy Hungfa69ca32017-11-30 10:07:53 -0800693 memcpy_to_float_from_q4_27(
694 mConfig.inputCfg.buffer.f32,
695 mConfig.inputCfg.buffer.s32,
696 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800697#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800698 } else
Andy Hung116a4982017-11-30 10:15:08 -0800699#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800700 {
Andy Hung116a4982017-11-30 10:15:08 -0800701#ifdef FLOAT_AUX
702 memcpy_to_i16_from_float(
703 mConfig.inputCfg.buffer.s16,
704 mConfig.inputCfg.buffer.f32,
705 mConfig.inputCfg.buffer.frameCount);
706#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800707 memcpy_to_i16_from_q4_27(
708 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700709 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800710 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800711#endif
rago94a1ee82017-07-21 15:11:02 -0700712 }
rago94a1ee82017-07-21 15:11:02 -0700713 }
714#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800715 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
716 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
717
718 if (!auxType && mInChannelCountRequested != inChannelCount) {
719 adjust_channels(
720 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
721 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
722 sizeof(float),
723 sizeof(float)
724 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
725 inBuffer = mInConversionBuffer;
726 }
727 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
728 && mOutChannelCountRequested != outChannelCount) {
729 adjust_selected_channels(
730 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
731 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
732 sizeof(float),
733 sizeof(float)
734 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
735 outBuffer = mOutConversionBuffer;
736 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800737 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
738 if (!auxType) {
739 if (mInConversionBuffer.get() == nullptr) {
740 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
741 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700742 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800743 memcpy_to_i16_from_float(
744 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800745 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800746 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800747 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700748 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800749 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
750 if (mOutConversionBuffer.get() == nullptr) {
751 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
752 goto data_bypass;
753 }
754 memcpy_to_i16_from_float(
755 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800756 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800757 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800758 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700759 }
760 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800761#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800762 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800763#ifdef FLOAT_EFFECT_CHAIN
764 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800765 sp<EffectBufferHalInterface> target =
766 mOutChannelCountRequested != outChannelCount
767 ? mOutConversionBuffer : mOutBuffer;
768
Andy Hungfa69ca32017-11-30 10:07:53 -0800769 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800770 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800771 mOutConversionBuffer->audioBuffer()->s16,
772 outChannelCount * mConfig.outputCfg.buffer.frameCount);
773 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800774 if (mOutChannelCountRequested != outChannelCount) {
775 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
776 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
777 sizeof(float),
778 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
779 }
rago94a1ee82017-07-21 15:11:02 -0700780#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700781 } else {
rago94a1ee82017-07-21 15:11:02 -0700782#ifdef FLOAT_EFFECT_CHAIN
783 data_bypass:
784#endif
785 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700787 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800788 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700789 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800790 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700791 }
792 }
793 ret = -ENODATA;
794 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800795
Eric Laurentca7cc822012-11-19 14:55:58 -0800796 // force transition to IDLE state when engine is ready
797 if (mState == STOPPED && ret == -ENODATA) {
798 mDisableWaitCnt = 1;
799 }
800
801 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700802 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800803#ifdef FLOAT_AUX
804 const size_t size =
805 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
806#else
rago94a1ee82017-07-21 15:11:02 -0700807 const size_t size =
808 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800809#endif
rago94a1ee82017-07-21 15:11:02 -0700810 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800811 }
812 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700813 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800814 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
815 // If an insert effect is idle and input buffer is different from output buffer,
816 // accumulate input onto output
Eric Laurent5d885392019-12-13 10:56:31 -0800817 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700818 // similar handling with data_bypass above.
819 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
820 accumulateInputToOutput();
821 } else { // EFFECT_BUFFER_ACCESS_WRITE
822 copyInputToOutput();
823 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800824 }
825 }
826}
827
828void AudioFlinger::EffectModule::reset_l()
829{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700830 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800831 return;
832 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700833 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800834}
835
836status_t AudioFlinger::EffectModule::configure()
837{
rago94a1ee82017-07-21 15:11:02 -0700838 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700839 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 uint32_t size;
841 audio_channel_mask_t channelMask;
842
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700843 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700844 status = NO_INIT;
845 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 }
847
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800849 // TODO: handle configuration of input (record) SW effects above the HAL,
850 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
851 // in which case input channel masks should be used here.
Eric Laurent5d885392019-12-13 10:56:31 -0800852 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800853 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700854 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800855
856 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800857 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
858 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
859 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
860 mConfig.inputCfg.channels);
861 }
862#ifndef MULTICHANNEL_EFFECT_CHAIN
863 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
864 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
865 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
866 mConfig.outputCfg.channels);
867 }
868#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800869 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800870#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700871 // TODO: Update this logic when multichannel effects are implemented.
872 // For offloaded tracks consider mono output as stereo for proper effect initialization
873 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
874 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
875 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
876 ALOGV("Overriding effect input and output as STEREO");
877 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800878#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800879 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800880 mInChannelCountRequested =
881 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
882 mOutChannelCountRequested =
883 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700884
rago94a1ee82017-07-21 15:11:02 -0700885 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
886 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900887
888 // Don't use sample rate for thread if effect isn't offloadable.
Eric Laurent5d885392019-12-13 10:56:31 -0800889 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900890 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
891 ALOGV("Overriding effect input as 48kHz");
892 } else {
Eric Laurent5d885392019-12-13 10:56:31 -0800893 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900894 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
896 mConfig.inputCfg.bufferProvider.cookie = NULL;
897 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
898 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
899 mConfig.outputCfg.bufferProvider.cookie = NULL;
900 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
901 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
902 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
903 // Insert effect:
Eric Laurenta20c4e92019-11-12 15:55:51 -0800904 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800905 // always overwrites output buffer: input buffer == output buffer
906 // - in other sessions:
907 // last effect in the chain accumulates in output buffer: input buffer != output buffer
908 // other effect: overwrites output buffer: input buffer == output buffer
909 // Auxiliary effect:
910 // accumulates in output buffer: input buffer != output buffer
911 // Therefore: accumulate <=> input buffer != output buffer
912 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
913 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
914 } else {
915 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
916 }
917 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
918 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent5d885392019-12-13 10:56:31 -0800919 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800920 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
921
Eric Laurent5d885392019-12-13 10:56:31 -0800922 ALOGV("configure() %p chain %p buffer %p framecount %zu",
923 this, mCallback->chain().promote() != nullptr ? mCallback->chain().promote().get() :
924 nullptr,
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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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 Laurent5d885392019-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;
1292 if (!auxType && formatMismatch && mInBuffer.get() != 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
Andy Hungbded9c82017-11-30 18:47:35 -08001302 if (size > 0 && (mInConversionBuffer.get() == nullptr
1303 || size > mInConversionBuffer->getSize())) {
1304 mInConversionBuffer.clear();
1305 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001306 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001307 }
Andy Hungbded9c82017-11-30 18:47:35 -08001308 if (mInConversionBuffer.get() != 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;
1337 if (formatMismatch && mOutBuffer.get() != 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
Andy Hungbded9c82017-11-30 18:47:35 -08001346 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1347 || size > mOutConversionBuffer->getSize())) {
1348 mOutConversionBuffer.clear();
1349 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001350 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001351 }
Andy Hungbded9c82017-11-30 18:47:35 -08001352 if (mOutConversionBuffer.get() != nullptr) {
1353 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{
Eric Laurent5d885392019-12-13 10:56:31 -08001398 if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001399 float vol_l = (float)left / (1 << 24);
1400 float vol_r = (float)right / (1 << 24);
Eric Laurent5d885392019-12-13 10:56:31 -08001401 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001402 }
1403}
1404
jiabinb8269fd2019-11-11 12:16:27 -08001405status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1406 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001407{
jiabinb8269fd2019-11-11 12:16:27 -08001408 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1409 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 return NO_ERROR;
1411 }
1412
1413 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001414 if (mStatus != NO_ERROR) {
1415 return mStatus;
1416 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001418 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 status_t cmdStatus;
1420 uint32_t size = sizeof(status_t);
jiabinb8269fd2019-11-11 12:16:27 -08001421 // FIXME: use audio device types and addresses when the hal interface is ready.
1422 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001423 sizeof(uint32_t),
jiabinb8269fd2019-11-11 12:16:27 -08001424 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001425 &size,
1426 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001427 }
1428 return status;
1429}
1430
jiabinb8269fd2019-11-11 12:16:27 -08001431status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1432{
1433 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1434}
1435
1436status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1437{
1438 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1439}
1440
Eric Laurentca7cc822012-11-19 14:55:58 -08001441status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1442{
1443 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001444 if (mStatus != NO_ERROR) {
1445 return mStatus;
1446 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 status_t status = NO_ERROR;
1448 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1449 status_t cmdStatus;
1450 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001451 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1452 sizeof(audio_mode_t),
1453 &mode,
1454 &size,
1455 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 if (status == NO_ERROR) {
1457 status = cmdStatus;
1458 }
1459 }
1460 return status;
1461}
1462
1463status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1464{
1465 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001466 if (mStatus != NO_ERROR) {
1467 return mStatus;
1468 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001469 status_t status = NO_ERROR;
1470 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1471 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001472 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1473 sizeof(audio_source_t),
1474 &source,
1475 &size,
1476 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001477 }
1478 return status;
1479}
1480
Eric Laurent5baf2af2013-09-12 17:37:00 -07001481status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1482{
1483 Mutex::Autolock _l(mLock);
1484 if (mStatus != NO_ERROR) {
1485 return mStatus;
1486 }
1487 status_t status = NO_ERROR;
1488 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1489 status_t cmdStatus;
1490 uint32_t size = sizeof(status_t);
1491 effect_offload_param_t cmd;
1492
1493 cmd.isOffload = offloaded;
1494 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001495 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1496 sizeof(effect_offload_param_t),
1497 &cmd,
1498 &size,
1499 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001500 if (status == NO_ERROR) {
1501 status = cmdStatus;
1502 }
1503 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1504 } else {
1505 if (offloaded) {
1506 status = INVALID_OPERATION;
1507 }
1508 mOffloaded = false;
1509 }
1510 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1511 return status;
1512}
1513
1514bool AudioFlinger::EffectModule::isOffloaded() const
1515{
1516 Mutex::Autolock _l(mLock);
1517 return mOffloaded;
1518}
1519
Andy Hungbded9c82017-11-30 18:47:35 -08001520static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1521 std::stringstream ss;
1522
1523 if (buffer.get() == nullptr) {
1524 return "nullptr"; // make different than below
1525 } else if (buffer->externalData() != nullptr) {
1526 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1527 << " -> "
1528 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1529 } else {
1530 ss << buffer->audioBuffer()->raw;
1531 }
1532 return ss.str();
1533}
Marco Nelissenb2208842014-02-07 14:00:50 -08001534
Eric Laurente0b9a362019-12-16 19:34:05 -08001535void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001536{
Eric Laurente0b9a362019-12-16 19:34:05 -08001537 EffectBase::dump(fd, args);
1538
Eric Laurentca7cc822012-11-19 14:55:58 -08001539 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001541
Eric Laurente0b9a362019-12-16 19:34:05 -08001542 result.append("\t\tStatus Engine:\n");
1543 result.appendFormat("\t\t%03d %p\n",
1544 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001545
1546 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001547
1548 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001549 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1550 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1551 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001552 mConfig.inputCfg.buffer.frameCount,
1553 mConfig.inputCfg.samplingRate,
1554 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001555 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001556 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001557
1558 result.append("\t\t- Output configuration:\n");
1559 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001560 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001561 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001562 mConfig.outputCfg.buffer.frameCount,
1563 mConfig.outputCfg.samplingRate,
1564 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001565 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001566 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001567
rago94a1ee82017-07-21 15:11:02 -07001568#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001569
Andy Hungbded9c82017-11-30 18:47:35 -08001570 result.appendFormat("\t\t- HAL buffers:\n"
1571 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1572 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1573 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1574 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1575 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001576#endif
1577
Eric Laurentca7cc822012-11-19 14:55:58 -08001578 write(fd, result.string(), result.length());
1579
Mikhail Naganov4d547672019-02-22 14:19:19 -08001580 if (mEffectInterface != 0) {
1581 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1582 (void)mEffectInterface->dump(fd);
1583 }
1584
Eric Laurentca7cc822012-11-19 14:55:58 -08001585 if (locked) {
1586 mLock.unlock();
1587 }
1588}
1589
1590// ----------------------------------------------------------------------------
1591// EffectHandle implementation
1592// ----------------------------------------------------------------------------
1593
1594#undef LOG_TAG
1595#define LOG_TAG "AudioFlinger::EffectHandle"
1596
Eric Laurente0b9a362019-12-16 19:34:05 -08001597AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001598 const sp<AudioFlinger::Client>& client,
1599 const sp<IEffectClient>& effectClient,
1600 int32_t priority)
1601 : BnEffect(),
1602 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001603 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001604{
1605 ALOGV("constructor %p", this);
1606
1607 if (client == 0) {
1608 return;
1609 }
1610 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1611 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001612 if (mCblkMemory == 0 ||
1613 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001614 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001615 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001616 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001617 return;
1618 }
Glenn Kastene75da402013-11-20 13:54:52 -08001619 new(mCblk) effect_param_cblk_t();
1620 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001621}
1622
1623AudioFlinger::EffectHandle::~EffectHandle()
1624{
1625 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001626 disconnect(false);
1627}
1628
Glenn Kastene75da402013-11-20 13:54:52 -08001629status_t AudioFlinger::EffectHandle::initCheck()
1630{
1631 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1632}
1633
Eric Laurentca7cc822012-11-19 14:55:58 -08001634status_t AudioFlinger::EffectHandle::enable()
1635{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001636 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001637 ALOGV("enable %p", this);
Eric Laurente0b9a362019-12-16 19:34:05 -08001638 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001639 if (effect == 0 || mDisconnected) {
1640 return DEAD_OBJECT;
1641 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001642 if (!mHasControl) {
1643 return INVALID_OPERATION;
1644 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001645
1646 if (mEnabled) {
1647 return NO_ERROR;
1648 }
1649
1650 mEnabled = true;
1651
Eric Laurent6c796322019-04-09 14:13:17 -07001652 status_t status = effect->updatePolicyState();
1653 if (status != NO_ERROR) {
1654 mEnabled = false;
1655 return status;
1656 }
1657
Eric Laurent5d885392019-12-13 10:56:31 -08001658 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001659
1660 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001661 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 return NO_ERROR;
1663 }
1664
Eric Laurent5d885392019-12-13 10:56:31 -08001665 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001666 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 mEnabled = false;
1668 }
1669 return status;
1670}
1671
1672status_t AudioFlinger::EffectHandle::disable()
1673{
1674 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001675 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001676 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001677 if (effect == 0 || mDisconnected) {
1678 return DEAD_OBJECT;
1679 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001680 if (!mHasControl) {
1681 return INVALID_OPERATION;
1682 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001683
1684 if (!mEnabled) {
1685 return NO_ERROR;
1686 }
1687 mEnabled = false;
1688
Eric Laurent6c796322019-04-09 14:13:17 -07001689 effect->updatePolicyState();
1690
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001691 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 return NO_ERROR;
1693 }
1694
Eric Laurent5d885392019-12-13 10:56:31 -08001695 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 return status;
1697}
1698
1699void AudioFlinger::EffectHandle::disconnect()
1700{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001701 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 disconnect(true);
1703}
1704
1705void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1706{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001707 AutoMutex _l(mLock);
1708 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1709 if (mDisconnected) {
1710 if (unpinIfLast) {
1711 android_errorWriteLog(0x534e4554, "32707507");
1712 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 return;
1714 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001716 {
Eric Laurente0b9a362019-12-16 19:34:05 -08001717 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001718 if (effect != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001719 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001720 ALOGW("%s Effect handle %p disconnected after thread destruction",
1721 __func__, this);
1722 }
1723 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001724 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001725 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001726
Eric Laurentca7cc822012-11-19 14:55:58 -08001727 if (mClient != 0) {
1728 if (mCblk != NULL) {
1729 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1730 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1731 }
1732 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001733 // Client destructor must run with AudioFlinger client mutex locked
1734 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001735 mClient.clear();
1736 }
1737}
1738
1739status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1740 uint32_t cmdSize,
1741 void *pCmdData,
1742 uint32_t *replySize,
1743 void *pReplyData)
1744{
1745 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001746 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001747
Eric Laurentc7ab3092017-06-15 18:43:46 -07001748 // reject commands reserved for internal use by audio framework if coming from outside
1749 // of audioserver
1750 switch(cmdCode) {
1751 case EFFECT_CMD_ENABLE:
1752 case EFFECT_CMD_DISABLE:
1753 case EFFECT_CMD_SET_PARAM:
1754 case EFFECT_CMD_SET_PARAM_DEFERRED:
1755 case EFFECT_CMD_SET_PARAM_COMMIT:
1756 case EFFECT_CMD_GET_PARAM:
1757 break;
1758 default:
1759 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1760 break;
1761 }
1762 android_errorWriteLog(0x534e4554, "62019992");
1763 return BAD_VALUE;
1764 }
1765
Eric Laurent1ffc5852016-12-15 14:46:09 -08001766 if (cmdCode == EFFECT_CMD_ENABLE) {
1767 if (*replySize < sizeof(int)) {
1768 android_errorWriteLog(0x534e4554, "32095713");
1769 return BAD_VALUE;
1770 }
1771 *(int *)pReplyData = NO_ERROR;
1772 *replySize = sizeof(int);
1773 return enable();
1774 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1775 if (*replySize < sizeof(int)) {
1776 android_errorWriteLog(0x534e4554, "32095713");
1777 return BAD_VALUE;
1778 }
1779 *(int *)pReplyData = NO_ERROR;
1780 *replySize = sizeof(int);
1781 return disable();
1782 }
1783
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001784 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001785 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001786 if (effect == 0 || mDisconnected) {
1787 return DEAD_OBJECT;
1788 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001789 // only get parameter command is permitted for applications not controlling the effect
1790 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1791 return INVALID_OPERATION;
1792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 if (mClient == 0) {
1794 return INVALID_OPERATION;
1795 }
1796
1797 // handle commands that are not forwarded transparently to effect engine
1798 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001799 if (*replySize < sizeof(int)) {
1800 android_errorWriteLog(0x534e4554, "32095713");
1801 return BAD_VALUE;
1802 }
1803 *(int *)pReplyData = NO_ERROR;
1804 *replySize = sizeof(int);
1805
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 // No need to trylock() here as this function is executed in the binder thread serving a
1807 // particular client process: no risk to block the whole media server process or mixer
1808 // threads if we are stuck here
1809 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001810 // keep local copy of index in case of client corruption b/32220769
1811 const uint32_t clientIndex = mCblk->clientIndex;
1812 const uint32_t serverIndex = mCblk->serverIndex;
1813 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1814 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001815 mCblk->serverIndex = 0;
1816 mCblk->clientIndex = 0;
1817 return BAD_VALUE;
1818 }
1819 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001820 effect_param_t *param = NULL;
1821 for (uint32_t index = serverIndex; index < clientIndex;) {
1822 int *p = (int *)(mBuffer + index);
1823 const int size = *p++;
1824 if (size < 0
1825 || size > EFFECT_PARAM_BUFFER_SIZE
1826 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001827 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001828 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 break;
1830 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001831
1832 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001833 auto *newParam = (effect_param_t *)realloc(param, size);
1834 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001835 ALOGW("command(): out of memory");
1836 status = NO_MEMORY;
1837 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001838 }
George Burgess IV80a22162020-01-05 20:06:15 -08001839 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001840 memcpy(param, p, size);
1841
1842 int reply = 0;
1843 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001844 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001845 size,
1846 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001847 &rsize,
1848 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001849
1850 // verify shared memory: server index shouldn't change; client index can't go back.
1851 if (serverIndex != mCblk->serverIndex
1852 || clientIndex > mCblk->clientIndex) {
1853 android_errorWriteLog(0x534e4554, "32220769");
1854 status = BAD_VALUE;
1855 break;
1856 }
1857
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 // stop at first error encountered
1859 if (ret != NO_ERROR) {
1860 status = ret;
1861 *(int *)pReplyData = reply;
1862 break;
1863 } else if (reply != NO_ERROR) {
1864 *(int *)pReplyData = reply;
1865 break;
1866 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001867 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001868 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001869 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001870 mCblk->serverIndex = 0;
1871 mCblk->clientIndex = 0;
1872 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001873 }
1874
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001875 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001876}
1877
1878void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1879{
1880 ALOGV("setControl %p control %d", this, hasControl);
1881
1882 mHasControl = hasControl;
1883 mEnabled = enabled;
1884
1885 if (signal && mEffectClient != 0) {
1886 mEffectClient->controlStatusChanged(hasControl);
1887 }
1888}
1889
1890void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1891 uint32_t cmdSize,
1892 void *pCmdData,
1893 uint32_t replySize,
1894 void *pReplyData)
1895{
1896 if (mEffectClient != 0) {
1897 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1898 }
1899}
1900
1901
1902
1903void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1904{
1905 if (mEffectClient != 0) {
1906 mEffectClient->enableStatusChanged(enabled);
1907 }
1908}
1909
1910status_t AudioFlinger::EffectHandle::onTransact(
1911 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1912{
1913 return BnEffect::onTransact(code, data, reply, flags);
1914}
1915
1916
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001917void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001918{
1919 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1920
Marco Nelissenb2208842014-02-07 14:00:50 -08001921 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001922 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001923 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001924 mHasControl ? "yes" : "no",
1925 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001926 mCblk ? mCblk->clientIndex : 0,
1927 mCblk ? mCblk->serverIndex : 0
1928 );
1929
1930 if (locked) {
1931 mCblk->lock.unlock();
1932 }
1933}
1934
1935#undef LOG_TAG
1936#define LOG_TAG "AudioFlinger::EffectChain"
1937
1938AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001939 audio_session_t sessionId)
Eric Laurent5d885392019-12-13 10:56:31 -08001940 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001941 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent5d885392019-12-13 10:56:31 -08001942 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
1943 mEffectCallback(new EffectCallback(this, thread, thread->mAudioFlinger.get()))
Eric Laurentca7cc822012-11-19 14:55:58 -08001944{
1945 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent5d885392019-12-13 10:56:31 -08001946 if (thread == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001947 return;
1948 }
1949 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1950 thread->frameCount();
1951}
1952
1953AudioFlinger::EffectChain::~EffectChain()
1954{
Eric Laurentca7cc822012-11-19 14:55:58 -08001955}
1956
1957// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1958sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1959 effect_descriptor_t *descriptor)
1960{
1961 size_t size = mEffects.size();
1962
1963 for (size_t i = 0; i < size; i++) {
1964 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1965 return mEffects[i];
1966 }
1967 }
1968 return 0;
1969}
1970
1971// getEffectFromId_l() must be called with ThreadBase::mLock held
1972sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1973{
1974 size_t size = mEffects.size();
1975
1976 for (size_t i = 0; i < size; i++) {
1977 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1978 if (id == 0 || mEffects[i]->id() == id) {
1979 return mEffects[i];
1980 }
1981 }
1982 return 0;
1983}
1984
1985// getEffectFromType_l() must be called with ThreadBase::mLock held
1986sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1987 const effect_uuid_t *type)
1988{
1989 size_t size = mEffects.size();
1990
1991 for (size_t i = 0; i < size; i++) {
1992 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1993 return mEffects[i];
1994 }
1995 }
1996 return 0;
1997}
1998
Eric Laurent6c796322019-04-09 14:13:17 -07001999std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2000{
2001 std::vector<int> ids;
2002 Mutex::Autolock _l(mLock);
2003 for (size_t i = 0; i < mEffects.size(); i++) {
2004 ids.push_back(mEffects[i]->id());
2005 }
2006 return ids;
2007}
2008
Eric Laurentca7cc822012-11-19 14:55:58 -08002009void AudioFlinger::EffectChain::clearInputBuffer()
2010{
2011 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002012 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002013}
2014
2015// Must be called with EffectChain::mLock locked
Eric Laurent5d885392019-12-13 10:56:31 -08002016void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002017{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002018 if (mInBuffer == NULL) {
2019 return;
2020 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002021 const size_t frameSize =
Eric Laurent5d885392019-12-13 10:56:31 -08002022 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002023
Eric Laurent5d885392019-12-13 10:56:31 -08002024 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002025 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002026}
2027
2028// Must be called with EffectChain::mLock locked
2029void AudioFlinger::EffectChain::process_l()
2030{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002031 // never process effects when:
2032 // - on an OFFLOAD thread
2033 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent5d885392019-12-13 10:56:31 -08002034 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurenta20c4e92019-11-12 15:55:51 -08002035 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002036 bool tracksOnSession = (trackCnt() != 0);
2037
2038 if (!tracksOnSession && mTailBufferCount == 0) {
2039 doProcess = false;
2040 }
2041
2042 if (activeTrackCnt() == 0) {
2043 // if no track is active and the effect tail has not been rendered,
2044 // the input buffer must be cleared here as the mixer process will not do it
2045 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08002046 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002047 if (mTailBufferCount > 0) {
2048 mTailBufferCount--;
2049 }
2050 }
2051 }
2052 }
2053
2054 size_t size = mEffects.size();
2055 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002056 // Only the input and output buffers of the chain can be external,
2057 // and 'update' / 'commit' do nothing for allocated buffers, thus
2058 // it's not needed to consider any other buffers here.
2059 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002060 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2061 mOutBuffer->update();
2062 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 for (size_t i = 0; i < size; i++) {
2064 mEffects[i]->process();
2065 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002066 mInBuffer->commit();
2067 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2068 mOutBuffer->commit();
2069 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002070 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002071 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002072 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002073 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2074 }
2075 if (doResetVolume) {
2076 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002077 }
2078}
2079
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002080// createEffect_l() must be called with ThreadBase::mLock held
2081status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002082 effect_descriptor_t *desc,
2083 int id,
2084 audio_session_t sessionId,
2085 bool pinned)
2086{
2087 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002088 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002089 status_t lStatus = effect->status();
2090 if (lStatus == NO_ERROR) {
2091 lStatus = addEffect_ll(effect);
2092 }
2093 if (lStatus != NO_ERROR) {
2094 effect.clear();
2095 }
2096 return lStatus;
2097}
2098
2099// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002100status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2101{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002102 Mutex::Autolock _l(mLock);
2103 return addEffect_ll(effect);
2104}
2105// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2106status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2107{
Eric Laurentca7cc822012-11-19 14:55:58 -08002108 effect_descriptor_t desc = effect->desc();
2109 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2110
Eric Laurent5d885392019-12-13 10:56:31 -08002111 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002112
2113 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2114 // Auxiliary effects are inserted at the beginning of mEffects vector as
2115 // they are processed first and accumulated in chain input buffer
2116 mEffects.insertAt(effect, 0);
2117
2118 // the input buffer for auxiliary effect contains mono samples in
2119 // 32 bit format. This is to avoid saturation in AudoMixer
2120 // accumulation stage. Saturation is done in EffectModule::process() before
2121 // calling the process in effect engine
Eric Laurent5d885392019-12-13 10:56:31 -08002122 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002123 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002124#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent5d885392019-12-13 10:56:31 -08002125 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002126 numSamples * sizeof(float), &halBuffer);
2127#else
Eric Laurent5d885392019-12-13 10:56:31 -08002128 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002129 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002130#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002131 if (result != OK) return result;
2132 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002133 // auxiliary effects output samples to chain input buffer for further processing
2134 // by insert effects
2135 effect->setOutBuffer(mInBuffer);
2136 } else {
2137 // Insert effects are inserted at the end of mEffects vector as they are processed
2138 // after track and auxiliary effects.
2139 // Insert effect order as a function of indicated preference:
2140 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2141 // another effect is present
2142 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2143 // last effect claiming first position
2144 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2145 // first effect claiming last position
2146 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2147 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2148 // already present
2149
2150 size_t size = mEffects.size();
2151 size_t idx_insert = size;
2152 ssize_t idx_insert_first = -1;
2153 ssize_t idx_insert_last = -1;
2154
2155 for (size_t i = 0; i < size; i++) {
2156 effect_descriptor_t d = mEffects[i]->desc();
2157 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2158 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2159 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2160 // check invalid effect chaining combinations
2161 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2162 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2163 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2164 desc.name, d.name);
2165 return INVALID_OPERATION;
2166 }
2167 // remember position of first insert effect and by default
2168 // select this as insert position for new effect
2169 if (idx_insert == size) {
2170 idx_insert = i;
2171 }
2172 // remember position of last insert effect claiming
2173 // first position
2174 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2175 idx_insert_first = i;
2176 }
2177 // remember position of first insert effect claiming
2178 // last position
2179 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2180 idx_insert_last == -1) {
2181 idx_insert_last = i;
2182 }
2183 }
2184 }
2185
2186 // modify idx_insert from first position if needed
2187 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2188 if (idx_insert_last != -1) {
2189 idx_insert = idx_insert_last;
2190 } else {
2191 idx_insert = size;
2192 }
2193 } else {
2194 if (idx_insert_first != -1) {
2195 idx_insert = idx_insert_first + 1;
2196 }
2197 }
2198
2199 // always read samples from chain input buffer
2200 effect->setInBuffer(mInBuffer);
2201
2202 // if last effect in the chain, output samples to chain
2203 // output buffer, otherwise to chain input buffer
2204 if (idx_insert == size) {
2205 if (idx_insert != 0) {
2206 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2207 mEffects[idx_insert-1]->configure();
2208 }
2209 effect->setOutBuffer(mOutBuffer);
2210 } else {
2211 effect->setOutBuffer(mInBuffer);
2212 }
2213 mEffects.insertAt(effect, idx_insert);
2214
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002215 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002216 idx_insert);
2217 }
2218 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002219
Eric Laurentca7cc822012-11-19 14:55:58 -08002220 return NO_ERROR;
2221}
2222
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002223// removeEffect_l() must be called with ThreadBase::mLock held
2224size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2225 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002226{
2227 Mutex::Autolock _l(mLock);
2228 size_t size = mEffects.size();
2229 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2230
2231 for (size_t i = 0; i < size; i++) {
2232 if (effect == mEffects[i]) {
2233 // calling stop here will remove pre-processing effect from the audio HAL.
2234 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2235 // the middle of a read from audio HAL
2236 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2237 mEffects[i]->state() == EffectModule::STOPPING) {
2238 mEffects[i]->stop();
2239 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002240 if (release) {
2241 mEffects[i]->release_l();
2242 }
2243
Mikhail Naganov022b9952017-01-04 16:36:51 -08002244 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002245 if (i == size - 1 && i != 0) {
2246 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2247 mEffects[i - 1]->configure();
2248 }
2249 }
2250 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002251 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002252 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002253
Eric Laurentca7cc822012-11-19 14:55:58 -08002254 break;
2255 }
2256 }
2257
2258 return mEffects.size();
2259}
2260
jiabinb8269fd2019-11-11 12:16:27 -08002261// setDevices_l() must be called with ThreadBase::mLock held
2262void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002263{
2264 size_t size = mEffects.size();
2265 for (size_t i = 0; i < size; i++) {
jiabinb8269fd2019-11-11 12:16:27 -08002266 mEffects[i]->setDevices(devices);
2267 }
2268}
2269
2270// setInputDevice_l() must be called with ThreadBase::mLock held
2271void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2272{
2273 size_t size = mEffects.size();
2274 for (size_t i = 0; i < size; i++) {
2275 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002276 }
2277}
2278
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002279// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002280void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2281{
2282 size_t size = mEffects.size();
2283 for (size_t i = 0; i < size; i++) {
2284 mEffects[i]->setMode(mode);
2285 }
2286}
2287
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002288// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002289void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2290{
2291 size_t size = mEffects.size();
2292 for (size_t i = 0; i < size; i++) {
2293 mEffects[i]->setAudioSource(source);
2294 }
2295}
2296
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002297// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002298bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002299{
2300 uint32_t newLeft = *left;
2301 uint32_t newRight = *right;
2302 bool hasControl = false;
2303 int ctrlIdx = -1;
2304 size_t size = mEffects.size();
2305
2306 // first update volume controller
2307 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002308 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 ctrlIdx = i - 1;
2310 hasControl = true;
2311 break;
2312 }
2313 }
2314
Eric Laurentfa1e1232016-08-02 19:01:49 -07002315 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002316 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002317 if (hasControl) {
2318 *left = mNewLeftVolume;
2319 *right = mNewRightVolume;
2320 }
2321 return hasControl;
2322 }
2323
2324 mVolumeCtrlIdx = ctrlIdx;
2325 mLeftVolume = newLeft;
2326 mRightVolume = newRight;
2327
2328 // second get volume update from volume controller
2329 if (ctrlIdx >= 0) {
2330 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2331 mNewLeftVolume = newLeft;
2332 mNewRightVolume = newRight;
2333 }
2334 // then indicate volume to all other effects in chain.
2335 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002336 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002337 uint32_t lVol = newLeft;
2338 uint32_t rVol = newRight;
2339
2340 for (size_t i = 0; i < size; i++) {
2341 if ((int)i == ctrlIdx) {
2342 continue;
2343 }
2344 // this also works for ctrlIdx == -1 when there is no volume controller
2345 if ((int)i > ctrlIdx) {
2346 lVol = *left;
2347 rVol = *right;
2348 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002349 // Pass requested volume directly if this is volume monitor module
2350 if (mEffects[i]->isVolumeMonitor()) {
2351 mEffects[i]->setVolume(left, right, false);
2352 } else {
2353 mEffects[i]->setVolume(&lVol, &rVol, false);
2354 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002355 }
2356 *left = newLeft;
2357 *right = newRight;
2358
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002359 setVolumeForOutput_l(*left, *right);
2360
Eric Laurentca7cc822012-11-19 14:55:58 -08002361 return hasControl;
2362}
2363
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002364// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002365void AudioFlinger::EffectChain::resetVolume_l()
2366{
Eric Laurente7449bf2016-08-03 18:44:07 -07002367 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2368 uint32_t left = mLeftVolume;
2369 uint32_t right = mRightVolume;
2370 (void)setVolume_l(&left, &right, true);
2371 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002372}
2373
Eric Laurent1b928682014-10-02 19:41:47 -07002374void AudioFlinger::EffectChain::syncHalEffectsState()
2375{
2376 Mutex::Autolock _l(mLock);
2377 for (size_t i = 0; i < mEffects.size(); i++) {
2378 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2379 mEffects[i]->state() == EffectModule::STOPPING) {
2380 mEffects[i]->addEffectToHal_l();
2381 }
2382 }
2383}
2384
Eric Laurentca7cc822012-11-19 14:55:58 -08002385void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2386{
Eric Laurentca7cc822012-11-19 14:55:58 -08002387 String8 result;
2388
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002389 const size_t numEffects = mEffects.size();
2390 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002391
Marco Nelissenb2208842014-02-07 14:00:50 -08002392 if (numEffects) {
2393 bool locked = AudioFlinger::dumpTryLock(mLock);
2394 // failed to lock - AudioFlinger is probably deadlocked
2395 if (!locked) {
2396 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002397 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002398
Andy Hungbded9c82017-11-30 18:47:35 -08002399 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2400 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2401 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2402 (int)inBufferStr.size(), "In buffer ",
2403 (int)outBufferStr.size(), "Out buffer ");
2404 result.appendFormat("\t%s %s %d\n",
2405 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002406 write(fd, result.string(), result.size());
2407
2408 for (size_t i = 0; i < numEffects; ++i) {
2409 sp<EffectModule> effect = mEffects[i];
2410 if (effect != 0) {
2411 effect->dump(fd, args);
2412 }
2413 }
2414
2415 if (locked) {
2416 mLock.unlock();
2417 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002418 } else {
2419 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002420 }
2421}
2422
2423// must be called with ThreadBase::mLock held
2424void AudioFlinger::EffectChain::setEffectSuspended_l(
2425 const effect_uuid_t *type, bool suspend)
2426{
2427 sp<SuspendedEffectDesc> desc;
2428 // use effect type UUID timelow as key as there is no real risk of identical
2429 // timeLow fields among effect type UUIDs.
2430 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2431 if (suspend) {
2432 if (index >= 0) {
2433 desc = mSuspendedEffects.valueAt(index);
2434 } else {
2435 desc = new SuspendedEffectDesc();
2436 desc->mType = *type;
2437 mSuspendedEffects.add(type->timeLow, desc);
2438 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2439 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002440
Eric Laurentca7cc822012-11-19 14:55:58 -08002441 if (desc->mRefCount++ == 0) {
2442 sp<EffectModule> effect = getEffectIfEnabled(type);
2443 if (effect != 0) {
2444 desc->mEffect = effect;
2445 effect->setSuspended(true);
Eric Laurent5d885392019-12-13 10:56:31 -08002446 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002447 }
2448 }
2449 } else {
2450 if (index < 0) {
2451 return;
2452 }
2453 desc = mSuspendedEffects.valueAt(index);
2454 if (desc->mRefCount <= 0) {
2455 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002456 desc->mRefCount = 0;
2457 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002458 }
2459 if (--desc->mRefCount == 0) {
2460 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2461 if (desc->mEffect != 0) {
2462 sp<EffectModule> effect = desc->mEffect.promote();
2463 if (effect != 0) {
2464 effect->setSuspended(false);
2465 effect->lock();
2466 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002467 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002468 effect->setEnabled_l(handle->enabled());
2469 }
2470 effect->unlock();
2471 }
2472 desc->mEffect.clear();
2473 }
2474 mSuspendedEffects.removeItemsAt(index);
2475 }
2476 }
2477}
2478
2479// must be called with ThreadBase::mLock held
2480void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2481{
2482 sp<SuspendedEffectDesc> desc;
2483
2484 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2485 if (suspend) {
2486 if (index >= 0) {
2487 desc = mSuspendedEffects.valueAt(index);
2488 } else {
2489 desc = new SuspendedEffectDesc();
2490 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2491 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2492 }
2493 if (desc->mRefCount++ == 0) {
2494 Vector< sp<EffectModule> > effects;
2495 getSuspendEligibleEffects(effects);
2496 for (size_t i = 0; i < effects.size(); i++) {
2497 setEffectSuspended_l(&effects[i]->desc().type, true);
2498 }
2499 }
2500 } else {
2501 if (index < 0) {
2502 return;
2503 }
2504 desc = mSuspendedEffects.valueAt(index);
2505 if (desc->mRefCount <= 0) {
2506 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2507 desc->mRefCount = 1;
2508 }
2509 if (--desc->mRefCount == 0) {
2510 Vector<const effect_uuid_t *> types;
2511 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2512 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2513 continue;
2514 }
2515 types.add(&mSuspendedEffects.valueAt(i)->mType);
2516 }
2517 for (size_t i = 0; i < types.size(); i++) {
2518 setEffectSuspended_l(types[i], false);
2519 }
2520 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2521 mSuspendedEffects.keyAt(index));
2522 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2523 }
2524 }
2525}
2526
2527
2528// The volume effect is used for automated tests only
2529#ifndef OPENSL_ES_H_
2530static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2531 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2532const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2533#endif //OPENSL_ES_H_
2534
Eric Laurentd8365c52017-07-16 15:27:05 -07002535/* static */
2536bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2537{
2538 // Only NS and AEC are suspended when BtNRec is off
2539 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2540 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2541 return true;
2542 }
2543 return false;
2544}
2545
Eric Laurentca7cc822012-11-19 14:55:58 -08002546bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2547{
2548 // auxiliary effects and visualizer are never suspended on output mix
2549 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2550 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2551 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002552 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2553 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002554 return false;
2555 }
2556 return true;
2557}
2558
2559void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2560 Vector< sp<AudioFlinger::EffectModule> > &effects)
2561{
2562 effects.clear();
2563 for (size_t i = 0; i < mEffects.size(); i++) {
2564 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2565 effects.add(mEffects[i]);
2566 }
2567 }
2568}
2569
2570sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2571 const effect_uuid_t *type)
2572{
2573 sp<EffectModule> effect = getEffectFromType_l(type);
2574 return effect != 0 && effect->isEnabled() ? effect : 0;
2575}
2576
2577void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2578 bool enabled)
2579{
2580 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2581 if (enabled) {
2582 if (index < 0) {
2583 // if the effect is not suspend check if all effects are suspended
2584 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2585 if (index < 0) {
2586 return;
2587 }
2588 if (!isEffectEligibleForSuspend(effect->desc())) {
2589 return;
2590 }
2591 setEffectSuspended_l(&effect->desc().type, enabled);
2592 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2593 if (index < 0) {
2594 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2595 return;
2596 }
2597 }
2598 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2599 effect->desc().type.timeLow);
2600 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002601 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002602 if (desc->mEffect == 0) {
2603 desc->mEffect = effect;
Eric Laurent5d885392019-12-13 10:56:31 -08002604 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002605 effect->setSuspended(true);
2606 }
2607 } else {
2608 if (index < 0) {
2609 return;
2610 }
2611 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2612 effect->desc().type.timeLow);
2613 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2614 desc->mEffect.clear();
2615 effect->setSuspended(false);
2616 }
2617}
2618
Eric Laurent5baf2af2013-09-12 17:37:00 -07002619bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002620{
2621 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002622 return isNonOffloadableEnabled_l();
2623}
2624
2625bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2626{
Eric Laurent813e2a72013-08-31 12:59:48 -07002627 size_t size = mEffects.size();
2628 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002629 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002630 return true;
2631 }
2632 }
2633 return false;
2634}
2635
Eric Laurentaaa44472014-09-12 17:41:50 -07002636void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2637{
2638 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002639 mEffectCallback->setThread(thread.get());
Eric Laurentaaa44472014-09-12 17:41:50 -07002640}
2641
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002642void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2643{
2644 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2645 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2646 }
2647 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2648 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2649 }
2650}
2651
2652void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2653{
2654 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2655 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2656 }
2657 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2658 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2659 }
2660}
2661
2662bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002663{
2664 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002665 for (const auto &effect : mEffects) {
2666 if (effect->isProcessImplemented()) {
2667 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002668 }
2669 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002670 // Allow effects without processing.
2671 return true;
2672}
2673
2674bool AudioFlinger::EffectChain::isFastCompatible() const
2675{
2676 Mutex::Autolock _l(mLock);
2677 for (const auto &effect : mEffects) {
2678 if (effect->isProcessImplemented()
2679 && effect->isImplementationSoftware()) {
2680 return false;
2681 }
2682 }
2683 // Allow effects without processing or hw accelerated effects.
2684 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002685}
2686
2687// isCompatibleWithThread_l() must be called with thread->mLock held
2688bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2689{
2690 Mutex::Autolock _l(mLock);
2691 for (size_t i = 0; i < mEffects.size(); i++) {
2692 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2693 return false;
2694 }
2695 }
2696 return true;
2697}
2698
Eric Laurent5d885392019-12-13 10:56:31 -08002699// EffectCallbackInterface implementation
2700status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2701 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2702 sp<EffectHalInterface> *effect) {
2703 status_t status = NO_INIT;
2704 sp<AudioFlinger> af = mAudioFlinger.promote();
2705 if (af == nullptr) {
2706 return status;
2707 }
2708 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2709 if (effectsFactory != 0) {
2710 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2711 }
2712 return status;
2713}
2714
2715bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurente0b9a362019-12-16 19:34:05 -08002716 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002717 sp<AudioFlinger> af = mAudioFlinger.promote();
2718 if (af == nullptr) {
2719 return false;
2720 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002721 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2722 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002723}
2724
2725status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2726 size_t size, sp<EffectBufferHalInterface>* buffer) {
2727 sp<AudioFlinger> af = mAudioFlinger.promote();
2728 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2729 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2730}
2731
2732status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2733 sp<EffectHalInterface> effect) {
2734 status_t result = NO_INIT;
2735 sp<ThreadBase> t = mThread.promote();
2736 if (t == nullptr) {
2737 return result;
2738 }
2739 sp <StreamHalInterface> st = t->stream();
2740 if (st == nullptr) {
2741 return result;
2742 }
2743 result = st->addEffect(effect);
2744 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2745 return result;
2746}
2747
2748status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2749 sp<EffectHalInterface> effect) {
2750 status_t result = NO_INIT;
2751 sp<ThreadBase> t = mThread.promote();
2752 if (t == nullptr) {
2753 return result;
2754 }
2755 sp <StreamHalInterface> st = t->stream();
2756 if (st == nullptr) {
2757 return result;
2758 }
2759 result = st->removeEffect(effect);
2760 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2761 return result;
2762}
2763
2764audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2765 sp<ThreadBase> t = mThread.promote();
2766 if (t == nullptr) {
2767 return AUDIO_IO_HANDLE_NONE;
2768 }
2769 return t->id();
2770}
2771
2772bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2773 sp<ThreadBase> t = mThread.promote();
2774 if (t == nullptr) {
2775 return true;
2776 }
2777 return t->isOutput();
2778}
2779
2780bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2781 sp<ThreadBase> t = mThread.promote();
2782 if (t == nullptr) {
2783 return false;
2784 }
2785 return t->type() == ThreadBase::OFFLOAD;
2786}
2787
2788bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2789 sp<ThreadBase> t = mThread.promote();
2790 if (t == nullptr) {
2791 return false;
2792 }
2793 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2794}
2795
2796bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2797 sp<ThreadBase> t = mThread.promote();
2798 if (t == nullptr) {
2799 return false;
2800 }
2801 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2802}
2803
2804uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2805 sp<ThreadBase> t = mThread.promote();
2806 if (t == nullptr) {
2807 return 0;
2808 }
2809 return t->sampleRate();
2810}
2811
2812audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2813 sp<ThreadBase> t = mThread.promote();
2814 if (t == nullptr) {
2815 return AUDIO_CHANNEL_NONE;
2816 }
2817 return t->channelMask();
2818}
2819
2820uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2821 sp<ThreadBase> t = mThread.promote();
2822 if (t == nullptr) {
2823 return 0;
2824 }
2825 return t->channelCount();
2826}
2827
2828size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2829 sp<ThreadBase> t = mThread.promote();
2830 if (t == nullptr) {
2831 return 0;
2832 }
2833 return t->frameCount();
2834}
2835
2836uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2837 sp<ThreadBase> t = mThread.promote();
2838 if (t == nullptr) {
2839 return 0;
2840 }
2841 return t->latency_l();
2842}
2843
2844void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2845 sp<ThreadBase> t = mThread.promote();
2846 if (t == nullptr) {
2847 return;
2848 }
2849 t->setVolumeForOutput_l(left, right);
2850}
2851
2852void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurente0b9a362019-12-16 19:34:05 -08002853 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent5d885392019-12-13 10:56:31 -08002854 sp<ThreadBase> t = mThread.promote();
2855 if (t == nullptr) {
2856 return;
2857 }
2858 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2859
2860 sp<EffectChain> c = mChain.promote();
2861 if (c == nullptr) {
2862 return;
2863 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002864 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2865 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent5d885392019-12-13 10:56:31 -08002866}
2867
Eric Laurente0b9a362019-12-16 19:34:05 -08002868void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002869 sp<ThreadBase> t = mThread.promote();
2870 if (t == nullptr) {
2871 return;
2872 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002873 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2874 t->onEffectEnable(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002875}
2876
Eric Laurente0b9a362019-12-16 19:34:05 -08002877void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002878 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2879
2880 sp<ThreadBase> t = mThread.promote();
2881 if (t == nullptr) {
2882 return;
2883 }
2884 t->onEffectDisable();
2885}
2886
2887bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2888 bool unpinIfLast) {
2889 sp<ThreadBase> t = mThread.promote();
2890 if (t == nullptr) {
2891 return false;
2892 }
2893 t->disconnectEffectHandle(handle, unpinIfLast);
2894 return true;
2895}
2896
2897void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2898 sp<EffectChain> c = mChain.promote();
2899 if (c == nullptr) {
2900 return;
2901 }
2902 c->resetVolume_l();
2903
2904}
2905
2906uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2907 sp<EffectChain> c = mChain.promote();
2908 if (c == nullptr) {
2909 return PRODUCT_STRATEGY_NONE;
2910 }
2911 return c->strategy();
2912}
2913
2914int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2915 sp<EffectChain> c = mChain.promote();
2916 if (c == nullptr) {
2917 return 0;
2918 }
2919 return c->activeTrackCnt();
2920}
2921
Glenn Kasten63238ef2015-03-02 15:50:29 -08002922} // namespace android