blob: e972abce890c9935219d7bb954377dce2cda4d90 [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
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530294 // Prevent calls to process() and other functions on effect interface from now on.
295 // The effect engine will be released by the destructor when the last strong reference on
296 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800297 if (mHandles.size() == 0 && !mPinned) {
298 mState = DESTROYED;
299 }
300
301 return mHandles.size();
302}
303
304// must be called with EffectModule::mLock held
Eric Laurente0b9a362019-12-16 19:34:05 -0800305AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800306{
307 // the first valid handle in the list has control over the module
308 for (size_t i = 0; i < mHandles.size(); i++) {
309 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800310 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800311 return h;
312 }
313 }
314
315 return NULL;
316}
317
Eric Laurentf10c7092016-12-06 17:09:56 -0800318// unsafe method called when the effect parent thread has been destroyed
Eric Laurente0b9a362019-12-16 19:34:05 -0800319ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800320{
321 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent5d885392019-12-13 10:56:31 -0800322 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
323 return mHandles.size();
324 }
325
Eric Laurentf10c7092016-12-06 17:09:56 -0800326 Mutex::Autolock _l(mLock);
327 ssize_t numHandles = removeHandle_l(handle);
328 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent5d885392019-12-13 10:56:31 -0800329 mLock.unlock();
330 mCallback->updateOrphanEffectChains(this);
331 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800332 }
333 return numHandles;
334}
335
Eric Laurente0b9a362019-12-16 19:34:05 -0800336bool AudioFlinger::EffectBase::purgeHandles()
337{
338 bool enabled = false;
339 Mutex::Autolock _l(mLock);
340 EffectHandle *handle = controlHandle_l();
341 if (handle != NULL) {
342 enabled = handle->enabled();
343 }
344 mHandles.clear();
345 return enabled;
346}
347
348void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
349 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
350}
351
352static String8 effectFlagsToString(uint32_t flags) {
353 String8 s;
354
355 s.append("conn. mode: ");
356 switch (flags & EFFECT_FLAG_TYPE_MASK) {
357 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
358 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
359 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
360 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
361 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
362 default: s.append("unknown/reserved"); break;
363 }
364 s.append(", ");
365
366 s.append("insert pref: ");
367 switch (flags & EFFECT_FLAG_INSERT_MASK) {
368 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
369 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
370 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
371 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
372 default: s.append("unknown/reserved"); break;
373 }
374 s.append(", ");
375
376 s.append("volume mgmt: ");
377 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
378 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
379 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
380 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
381 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
382 default: s.append("unknown/reserved"); break;
383 }
384 s.append(", ");
385
386 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
387 if (devind) {
388 s.append("device indication: ");
389 switch (devind) {
390 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
391 default: s.append("unknown/reserved"); break;
392 }
393 s.append(", ");
394 }
395
396 s.append("input mode: ");
397 switch (flags & EFFECT_FLAG_INPUT_MASK) {
398 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
399 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
400 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
401 default: s.append("not set"); break;
402 }
403 s.append(", ");
404
405 s.append("output mode: ");
406 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
407 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
408 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
409 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
410 default: s.append("not set"); break;
411 }
412 s.append(", ");
413
414 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
415 if (accel) {
416 s.append("hardware acceleration: ");
417 switch (accel) {
418 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
419 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
420 default: s.append("unknown/reserved"); break;
421 }
422 s.append(", ");
423 }
424
425 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
426 if (modeind) {
427 s.append("mode indication: ");
428 switch (modeind) {
429 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
430 default: s.append("unknown/reserved"); break;
431 }
432 s.append(", ");
433 }
434
435 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
436 if (srcind) {
437 s.append("source indication: ");
438 switch (srcind) {
439 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
440 default: s.append("unknown/reserved"); break;
441 }
442 s.append(", ");
443 }
444
445 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
446 s.append("offloadable, ");
447 }
448
449 int len = s.length();
450 if (s.length() > 2) {
451 (void) s.lockBuffer(len);
452 s.unlockBuffer(len - 2);
453 }
454 return s;
455}
456
457void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
458{
459 String8 result;
460
461 result.appendFormat("\tEffect ID %d:\n", mId);
462
463 bool locked = AudioFlinger::dumpTryLock(mLock);
464 // failed to lock - AudioFlinger is probably deadlocked
465 if (!locked) {
466 result.append("\t\tCould not lock Fx mutex:\n");
467 }
468
469 result.append("\t\tSession State Registered Enabled Suspended:\n");
470 result.appendFormat("\t\t%05d %03d %s %s %s\n",
471 mSessionId, mState, mPolicyRegistered ? "y" : "n",
472 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
473
474 result.append("\t\tDescriptor:\n");
475 char uuidStr[64];
476 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
477 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
478 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
479 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
480 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
481 mDescriptor.apiVersion,
482 mDescriptor.flags,
483 effectFlagsToString(mDescriptor.flags).string());
484 result.appendFormat("\t\t- name: %s\n",
485 mDescriptor.name);
486
487 result.appendFormat("\t\t- implementor: %s\n",
488 mDescriptor.implementor);
489
490 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
491 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
492 char buffer[256];
493 for (size_t i = 0; i < mHandles.size(); ++i) {
494 EffectHandle *handle = mHandles[i];
495 if (handle != NULL && !handle->disconnected()) {
496 handle->dumpToBuffer(buffer, sizeof(buffer));
497 result.append(buffer);
498 }
499 }
500 if (locked) {
501 mLock.unlock();
502 }
503
504 write(fd, result.string(), result.length());
505}
506
507// ----------------------------------------------------------------------------
508// EffectModule implementation
509// ----------------------------------------------------------------------------
510
511#undef LOG_TAG
512#define LOG_TAG "AudioFlinger::EffectModule"
513
514AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
515 effect_descriptor_t *desc,
516 int id,
517 audio_session_t sessionId,
Eric Laurent9b2064c2019-11-22 17:25:04 -0800518 bool pinned,
519 audio_port_handle_t deviceId)
Eric Laurente0b9a362019-12-16 19:34:05 -0800520 : EffectBase(callback, desc, id, sessionId, pinned),
521 // clear mConfig to ensure consistent initial value of buffer framecount
522 // in case buffers are associated by setInBuffer() or setOutBuffer()
523 // prior to configure().
524 mConfig{{}, {}},
525 mStatus(NO_INIT),
526 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
527 mDisableWaitCnt(0), // set by process() and updateState()
528 mOffloaded(false)
529#ifdef FLOAT_EFFECT_CHAIN
530 , mSupportsFloat(false)
531#endif
532{
533 ALOGV("Constructor %p pinned %d", this, pinned);
534 int lStatus;
535
536 // create effect engine from effect factory
537 mStatus = callback->createEffectHal(
Eric Laurent9b2064c2019-11-22 17:25:04 -0800538 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurente0b9a362019-12-16 19:34:05 -0800539 if (mStatus != NO_ERROR) {
540 return;
541 }
542 lStatus = init();
543 if (lStatus < 0) {
544 mStatus = lStatus;
545 goto Error;
546 }
547
548 setOffloaded(callback->isOffload(), callback->io());
549 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
550
551 return;
552Error:
553 mEffectInterface.clear();
554 ALOGV("Constructor Error %d", mStatus);
555}
556
557AudioFlinger::EffectModule::~EffectModule()
558{
559 ALOGV("Destructor %p", this);
560 if (mEffectInterface != 0) {
561 char uuidStr[64];
562 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
563 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
564 this, uuidStr);
565 release_l();
566 }
567
568}
569
Eric Laurentfa1e1232016-08-02 19:01:49 -0700570bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800571 Mutex::Autolock _l(mLock);
572
Eric Laurentfa1e1232016-08-02 19:01:49 -0700573 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800574 switch (mState) {
575 case RESTART:
576 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700577 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800578
579 case STARTING:
580 // clear auxiliary effect input buffer for next accumulation
581 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
582 memset(mConfig.inputCfg.buffer.raw,
583 0,
584 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
585 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700586 if (start_l() == NO_ERROR) {
587 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700588 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700589 } else {
590 mState = IDLE;
591 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800592 break;
593 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900594 // volume control for offload and direct threads must take effect immediately.
595 if (stop_l() == NO_ERROR
596 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700597 mDisableWaitCnt = mMaxDisableWaitCnt;
598 } else {
599 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
600 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800601 mState = STOPPED;
602 break;
603 case STOPPED:
604 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
605 // turn off sequence.
606 if (--mDisableWaitCnt == 0) {
607 reset_l();
608 mState = IDLE;
609 }
610 break;
611 default: //IDLE , ACTIVE, DESTROYED
612 break;
613 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700614
615 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800616}
617
618void AudioFlinger::EffectModule::process()
619{
620 Mutex::Autolock _l(mLock);
621
Mikhail Naganov022b9952017-01-04 16:36:51 -0800622 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800623 return;
624 }
625
rago94a1ee82017-07-21 15:11:02 -0700626 const uint32_t inChannelCount =
627 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
628 const uint32_t outChannelCount =
629 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
630 const bool auxType =
631 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
632
Andy Hungfa69ca32017-11-30 10:07:53 -0800633 // safeInputOutputSampleCount is 0 if the channel count between input and output
634 // buffers do not match. This prevents automatic accumulation or copying between the
635 // input and output effect buffers without an intermediary effect process.
636 // TODO: consider implementing channel conversion.
637 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700638 mInChannelCountRequested != mOutChannelCountRequested ? 0
639 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800640 mConfig.inputCfg.buffer.frameCount,
641 mConfig.outputCfg.buffer.frameCount);
642 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
643#ifdef FLOAT_EFFECT_CHAIN
644 accumulate_float(
645 mConfig.outputCfg.buffer.f32,
646 mConfig.inputCfg.buffer.f32,
647 safeInputOutputSampleCount);
648#else
649 accumulate_i16(
650 mConfig.outputCfg.buffer.s16,
651 mConfig.inputCfg.buffer.s16,
652 safeInputOutputSampleCount);
653#endif
654 };
655 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
656#ifdef FLOAT_EFFECT_CHAIN
657 memcpy(
658 mConfig.outputCfg.buffer.f32,
659 mConfig.inputCfg.buffer.f32,
660 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
661
662#else
663 memcpy(
664 mConfig.outputCfg.buffer.s16,
665 mConfig.inputCfg.buffer.s16,
666 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
667#endif
668 };
669
Eric Laurentca7cc822012-11-19 14:55:58 -0800670 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700671 int ret;
672 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700673 if (auxType) {
674 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800675 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700676#ifdef FLOAT_EFFECT_CHAIN
677 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800678#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700679 // Do in-place float conversion for auxiliary effect input buffer.
680 static_assert(sizeof(float) <= sizeof(int32_t),
681 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
682
Andy Hungfa69ca32017-11-30 10:07:53 -0800683 memcpy_to_float_from_q4_27(
684 mConfig.inputCfg.buffer.f32,
685 mConfig.inputCfg.buffer.s32,
686 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800687#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800688 } else
Andy Hung116a4982017-11-30 10:15:08 -0800689#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800690 {
Andy Hung116a4982017-11-30 10:15:08 -0800691#ifdef FLOAT_AUX
692 memcpy_to_i16_from_float(
693 mConfig.inputCfg.buffer.s16,
694 mConfig.inputCfg.buffer.f32,
695 mConfig.inputCfg.buffer.frameCount);
696#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800697 memcpy_to_i16_from_q4_27(
698 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700699 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800700 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800701#endif
rago94a1ee82017-07-21 15:11:02 -0700702 }
rago94a1ee82017-07-21 15:11:02 -0700703 }
704#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800705 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
706 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
707
708 if (!auxType && mInChannelCountRequested != inChannelCount) {
709 adjust_channels(
710 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
711 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
712 sizeof(float),
713 sizeof(float)
714 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
715 inBuffer = mInConversionBuffer;
716 }
717 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
718 && mOutChannelCountRequested != outChannelCount) {
719 adjust_selected_channels(
720 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
721 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
722 sizeof(float),
723 sizeof(float)
724 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
725 outBuffer = mOutConversionBuffer;
726 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800727 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
728 if (!auxType) {
729 if (mInConversionBuffer.get() == nullptr) {
730 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
731 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700732 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800733 memcpy_to_i16_from_float(
734 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800735 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800736 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800737 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700738 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800739 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
740 if (mOutConversionBuffer.get() == nullptr) {
741 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
742 goto data_bypass;
743 }
744 memcpy_to_i16_from_float(
745 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800746 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800748 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700749 }
750 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800751#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800752 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800753#ifdef FLOAT_EFFECT_CHAIN
754 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800755 sp<EffectBufferHalInterface> target =
756 mOutChannelCountRequested != outChannelCount
757 ? mOutConversionBuffer : mOutBuffer;
758
Andy Hungfa69ca32017-11-30 10:07:53 -0800759 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800760 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800761 mOutConversionBuffer->audioBuffer()->s16,
762 outChannelCount * mConfig.outputCfg.buffer.frameCount);
763 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800764 if (mOutChannelCountRequested != outChannelCount) {
765 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
766 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
767 sizeof(float),
768 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
769 }
rago94a1ee82017-07-21 15:11:02 -0700770#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700771 } else {
rago94a1ee82017-07-21 15:11:02 -0700772#ifdef FLOAT_EFFECT_CHAIN
773 data_bypass:
774#endif
775 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700777 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800778 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700779 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800780 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700781 }
782 }
783 ret = -ENODATA;
784 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800785
Eric Laurentca7cc822012-11-19 14:55:58 -0800786 // force transition to IDLE state when engine is ready
787 if (mState == STOPPED && ret == -ENODATA) {
788 mDisableWaitCnt = 1;
789 }
790
791 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700792 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800793#ifdef FLOAT_AUX
794 const size_t size =
795 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
796#else
rago94a1ee82017-07-21 15:11:02 -0700797 const size_t size =
798 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800799#endif
rago94a1ee82017-07-21 15:11:02 -0700800 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800801 }
802 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700803 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800804 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
805 // If an insert effect is idle and input buffer is different from output buffer,
806 // accumulate input onto output
Eric Laurent5d885392019-12-13 10:56:31 -0800807 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700808 // similar handling with data_bypass above.
809 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
810 accumulateInputToOutput();
811 } else { // EFFECT_BUFFER_ACCESS_WRITE
812 copyInputToOutput();
813 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800814 }
815 }
816}
817
818void AudioFlinger::EffectModule::reset_l()
819{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700820 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800821 return;
822 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700823 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800824}
825
826status_t AudioFlinger::EffectModule::configure()
827{
rago94a1ee82017-07-21 15:11:02 -0700828 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700829 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700830 uint32_t size;
831 audio_channel_mask_t channelMask;
832
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700833 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700834 status = NO_INIT;
835 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800836 }
837
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800839 // TODO: handle configuration of input (record) SW effects above the HAL,
840 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
841 // in which case input channel masks should be used here.
Eric Laurent5d885392019-12-13 10:56:31 -0800842 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800843 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700844 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800845
846 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800847 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
848 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
849 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
850 mConfig.inputCfg.channels);
851 }
852#ifndef MULTICHANNEL_EFFECT_CHAIN
853 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
854 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
855 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
856 mConfig.outputCfg.channels);
857 }
858#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800859 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800860#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700861 // TODO: Update this logic when multichannel effects are implemented.
862 // For offloaded tracks consider mono output as stereo for proper effect initialization
863 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
864 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
865 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
866 ALOGV("Overriding effect input and output as STEREO");
867 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800868#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800869 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800870 mInChannelCountRequested =
871 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
872 mOutChannelCountRequested =
873 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700874
rago94a1ee82017-07-21 15:11:02 -0700875 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
876 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900877
878 // Don't use sample rate for thread if effect isn't offloadable.
Eric Laurent5d885392019-12-13 10:56:31 -0800879 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900880 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
881 ALOGV("Overriding effect input as 48kHz");
882 } else {
Eric Laurent5d885392019-12-13 10:56:31 -0800883 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900884 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800885 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
886 mConfig.inputCfg.bufferProvider.cookie = NULL;
887 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
888 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
889 mConfig.outputCfg.bufferProvider.cookie = NULL;
890 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
891 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
892 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
893 // Insert effect:
Eric Laurenta20c4e92019-11-12 15:55:51 -0800894 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 // always overwrites output buffer: input buffer == output buffer
896 // - in other sessions:
897 // last effect in the chain accumulates in output buffer: input buffer != output buffer
898 // other effect: overwrites output buffer: input buffer == output buffer
899 // Auxiliary effect:
900 // accumulates in output buffer: input buffer != output buffer
901 // Therefore: accumulate <=> input buffer != output buffer
902 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
903 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
904 } else {
905 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
906 }
907 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
908 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent5d885392019-12-13 10:56:31 -0800909 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800910 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
911
Eric Laurent5d885392019-12-13 10:56:31 -0800912 ALOGV("configure() %p chain %p buffer %p framecount %zu",
913 this, mCallback->chain().promote() != nullptr ? mCallback->chain().promote().get() :
914 nullptr,
915 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800916
917 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700918 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700919 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800920 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700921 &mConfig,
922 &size,
923 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700924 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 status = cmdStatus;
926 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800927
928#ifdef MULTICHANNEL_EFFECT_CHAIN
929 if (status != NO_ERROR &&
Eric Laurent5d885392019-12-13 10:56:31 -0800930 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
932 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
933 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700934 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
935 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800936 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
937 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
938 }
939 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
940 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
941 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
942 }
943 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700944 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800945 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700946 &mConfig,
947 &size,
948 &cmdStatus);
949 if (status == NO_ERROR) {
950 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800951 }
952 }
953#endif
954
955#ifdef FLOAT_EFFECT_CHAIN
956 if (status == NO_ERROR) {
957 mSupportsFloat = true;
958 }
959
960 if (status != NO_ERROR) {
961 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
962 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
963 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
964 size = sizeof(int);
965 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
966 sizeof(mConfig),
967 &mConfig,
968 &size,
969 &cmdStatus);
970 if (status == NO_ERROR) {
971 status = cmdStatus;
972 }
973 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700974 mSupportsFloat = false;
975 ALOGVV("config worked with 16 bit");
976 } else {
977 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800978 }
rago94a1ee82017-07-21 15:11:02 -0700979 }
980#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800981
rago94a1ee82017-07-21 15:11:02 -0700982 if (status == NO_ERROR) {
983 // Establish Buffer strategy
984 setInBuffer(mInBuffer);
985 setOutBuffer(mOutBuffer);
986
987 // Update visualizer latency
988 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
989 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
990 effect_param_t *p = (effect_param_t *)buf32;
991
992 p->psize = sizeof(uint32_t);
993 p->vsize = sizeof(uint32_t);
994 size = sizeof(int);
995 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
996
Eric Laurent5d885392019-12-13 10:56:31 -0800997 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -0700998
999 *((int32_t *)p->data + 1)= latency;
1000 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1001 sizeof(effect_param_t) + 8,
1002 &buf32,
1003 &size,
1004 &cmdStatus);
1005 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001006 }
1007
Andy Hung05083ac2017-12-14 15:00:28 -08001008 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1009 mMaxDisableWaitCnt = (uint32_t)std::max(
1010 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1011 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1012 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001013
Eric Laurentd0ebb532013-04-02 16:41:41 -07001014exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001015 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001016 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001017 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 return status;
1019}
1020
1021status_t AudioFlinger::EffectModule::init()
1022{
1023 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001024 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001025 return NO_INIT;
1026 }
1027 status_t cmdStatus;
1028 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001029 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1030 0,
1031 NULL,
1032 &size,
1033 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 if (status == 0) {
1035 status = cmdStatus;
1036 }
1037 return status;
1038}
1039
Eric Laurent1b928682014-10-02 19:41:47 -07001040void AudioFlinger::EffectModule::addEffectToHal_l()
1041{
1042 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1043 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -08001044 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001045 }
1046}
1047
Eric Laurentfa1e1232016-08-02 19:01:49 -07001048// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001049status_t AudioFlinger::EffectModule::start()
1050{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001051 status_t status;
1052 {
1053 Mutex::Autolock _l(mLock);
1054 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001055 }
Eric Laurent5d885392019-12-13 10:56:31 -08001056 if (status == NO_ERROR) {
1057 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001058 }
1059 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001060}
1061
1062status_t AudioFlinger::EffectModule::start_l()
1063{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001064 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 return NO_INIT;
1066 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001067 if (mStatus != NO_ERROR) {
1068 return mStatus;
1069 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001070 status_t cmdStatus;
1071 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001072 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1073 0,
1074 NULL,
1075 &size,
1076 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001077 if (status == 0) {
1078 status = cmdStatus;
1079 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001080 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001081 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001082 }
1083 return status;
1084}
1085
1086status_t AudioFlinger::EffectModule::stop()
1087{
1088 Mutex::Autolock _l(mLock);
1089 return stop_l();
1090}
1091
1092status_t AudioFlinger::EffectModule::stop_l()
1093{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001094 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001095 return NO_INIT;
1096 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001097 if (mStatus != NO_ERROR) {
1098 return mStatus;
1099 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001100 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001101 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001102
1103 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001104 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1105 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1106 mSetVolumeReentrantTid = gettid();
Eric Laurent5d885392019-12-13 10:56:31 -08001107 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001108 mSetVolumeReentrantTid = INVALID_PID;
1109 }
1110
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001111 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1112 0,
1113 NULL,
1114 &size,
1115 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001116 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001117 status = cmdStatus;
1118 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001119 if (status == NO_ERROR) {
Eric Laurent5d885392019-12-13 10:56:31 -08001120 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001121 }
1122 return status;
1123}
1124
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001125// must be called with EffectChain::mLock held
1126void AudioFlinger::EffectModule::release_l()
1127{
1128 if (mEffectInterface != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001129 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001130 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001131 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001132 mEffectInterface.clear();
1133 }
1134}
1135
Eric Laurent5d885392019-12-13 10:56:31 -08001136status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001137{
1138 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1139 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent5d885392019-12-13 10:56:31 -08001140 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001141 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001143}
1144
Andy Hunge4a1d912016-08-17 14:11:13 -07001145// round up delta valid if value and divisor are positive.
1146template <typename T>
1147static T roundUpDelta(const T &value, const T &divisor) {
1148 T remainder = value % divisor;
1149 return remainder == 0 ? 0 : divisor - remainder;
1150}
1151
Eric Laurentca7cc822012-11-19 14:55:58 -08001152status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1153 uint32_t cmdSize,
1154 void *pCmdData,
1155 uint32_t *replySize,
1156 void *pReplyData)
1157{
1158 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001159 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001160
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001161 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 return NO_INIT;
1163 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001164 if (mStatus != NO_ERROR) {
1165 return mStatus;
1166 }
Andy Hung110bc952016-06-20 15:22:52 -07001167 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001168 (sizeof(effect_param_t) > cmdSize ||
1169 ((effect_param_t *)pCmdData)->psize > cmdSize
1170 - sizeof(effect_param_t))) {
1171 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001172 android_errorWriteLog(0x534e4554, "33003822");
1173 return -EINVAL;
1174 }
1175 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1176 (*replySize < sizeof(effect_param_t) ||
1177 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1178 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001179 return -EINVAL;
1180 }
ragoe2759072016-11-22 18:02:48 -08001181 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1182 (sizeof(effect_param_t) > *replySize
1183 || ((effect_param_t *)pCmdData)->psize > *replySize
1184 - sizeof(effect_param_t)
1185 || ((effect_param_t *)pCmdData)->vsize > *replySize
1186 - sizeof(effect_param_t)
1187 - ((effect_param_t *)pCmdData)->psize
1188 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1189 *replySize
1190 - sizeof(effect_param_t)
1191 - ((effect_param_t *)pCmdData)->psize
1192 - ((effect_param_t *)pCmdData)->vsize)) {
1193 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1194 android_errorWriteLog(0x534e4554, "32705438");
1195 return -EINVAL;
1196 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001197 if ((cmdCode == EFFECT_CMD_SET_PARAM
1198 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1199 (sizeof(effect_param_t) > cmdSize
1200 || ((effect_param_t *)pCmdData)->psize > cmdSize
1201 - sizeof(effect_param_t)
1202 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1203 - sizeof(effect_param_t)
1204 - ((effect_param_t *)pCmdData)->psize
1205 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1206 cmdSize
1207 - sizeof(effect_param_t)
1208 - ((effect_param_t *)pCmdData)->psize
1209 - ((effect_param_t *)pCmdData)->vsize)) {
1210 android_errorWriteLog(0x534e4554, "30204301");
1211 return -EINVAL;
1212 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001213 status_t status = mEffectInterface->command(cmdCode,
1214 cmdSize,
1215 pCmdData,
1216 replySize,
1217 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001218 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1219 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1220 for (size_t i = 1; i < mHandles.size(); i++) {
1221 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001222 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001223 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1224 }
1225 }
1226 }
1227 return status;
1228}
1229
Eric Laurentca7cc822012-11-19 14:55:58 -08001230bool AudioFlinger::EffectModule::isProcessEnabled() const
1231{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001232 if (mStatus != NO_ERROR) {
1233 return false;
1234 }
1235
Eric Laurentca7cc822012-11-19 14:55:58 -08001236 switch (mState) {
1237 case RESTART:
1238 case ACTIVE:
1239 case STOPPING:
1240 case STOPPED:
1241 return true;
1242 case IDLE:
1243 case STARTING:
1244 case DESTROYED:
1245 default:
1246 return false;
1247 }
1248}
1249
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001250bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1251{
Eric Laurent5d885392019-12-13 10:56:31 -08001252 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001253}
1254
1255bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1256{
1257 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1258}
1259
Mikhail Naganov022b9952017-01-04 16:36:51 -08001260void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001261 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001262
1263 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001264 if (buffer != 0) {
1265 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1266 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1267 } else {
1268 mConfig.inputCfg.buffer.raw = NULL;
1269 }
1270 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001271 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001272
1273#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001274 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001275 // Theoretically insert effects can also do in-place conversions (destroying
1276 // the original buffer) when the output buffer is identical to the input buffer,
1277 // but we don't optimize for it here.
1278 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001279 const uint32_t inChannelCount =
1280 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1281 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1282 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001283 // we need to translate - create hidl shared buffer and intercept
1284 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001285 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1286 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1287 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001288
1289 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1290 __func__, inChannels, inFrameCount, size);
1291
Andy Hungbded9c82017-11-30 18:47:35 -08001292 if (size > 0 && (mInConversionBuffer.get() == nullptr
1293 || size > mInConversionBuffer->getSize())) {
1294 mInConversionBuffer.clear();
1295 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001296 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001297 }
Andy Hungbded9c82017-11-30 18:47:35 -08001298 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001299 mInConversionBuffer->setFrameCount(inFrameCount);
1300 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001301 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001302 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001303 }
1304 }
1305#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001306}
1307
1308void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001309 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001310
1311 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001312 if (buffer != 0) {
1313 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1314 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1315 } else {
1316 mConfig.outputCfg.buffer.raw = NULL;
1317 }
1318 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001319 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001320
1321#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001322 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001323 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001324 const uint32_t outChannelCount =
1325 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1326 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1327 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001328 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001329 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1330 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1331 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001332
1333 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1334 __func__, outChannels, outFrameCount, size);
1335
Andy Hungbded9c82017-11-30 18:47:35 -08001336 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1337 || size > mOutConversionBuffer->getSize())) {
1338 mOutConversionBuffer.clear();
1339 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent5d885392019-12-13 10:56:31 -08001340 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001341 }
Andy Hungbded9c82017-11-30 18:47:35 -08001342 if (mOutConversionBuffer.get() != nullptr) {
1343 mOutConversionBuffer->setFrameCount(outFrameCount);
1344 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001345 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001346 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001347 }
1348 }
1349#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001350}
1351
Eric Laurentca7cc822012-11-19 14:55:58 -08001352status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1353{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001354 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001355 if (mStatus != NO_ERROR) {
1356 return mStatus;
1357 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001358 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001359 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1360 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1361 if (isProcessEnabled() &&
1362 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001363 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1364 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001365 uint32_t volume[2];
1366 uint32_t *pVolume = NULL;
1367 uint32_t size = sizeof(volume);
1368 volume[0] = *left;
1369 volume[1] = *right;
1370 if (controller) {
1371 pVolume = volume;
1372 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001373 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1374 size,
1375 volume,
1376 &size,
1377 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001378 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1379 *left = volume[0];
1380 *right = volume[1];
1381 }
1382 }
1383 return status;
1384}
1385
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001386void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1387{
Eric Laurent5d885392019-12-13 10:56:31 -08001388 if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001389 float vol_l = (float)left / (1 << 24);
1390 float vol_r = (float)right / (1 << 24);
Eric Laurent5d885392019-12-13 10:56:31 -08001391 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001392 }
1393}
1394
jiabinb8269fd2019-11-11 12:16:27 -08001395status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1396 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001397{
jiabinb8269fd2019-11-11 12:16:27 -08001398 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1399 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001400 return NO_ERROR;
1401 }
1402
1403 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001404 if (mStatus != NO_ERROR) {
1405 return mStatus;
1406 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001407 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001408 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001409 status_t cmdStatus;
1410 uint32_t size = sizeof(status_t);
jiabinb8269fd2019-11-11 12:16:27 -08001411 // FIXME: use audio device types and addresses when the hal interface is ready.
1412 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001413 sizeof(uint32_t),
jiabinb8269fd2019-11-11 12:16:27 -08001414 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001415 &size,
1416 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001417 }
1418 return status;
1419}
1420
jiabinb8269fd2019-11-11 12:16:27 -08001421status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1422{
1423 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1424}
1425
1426status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1427{
1428 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1429}
1430
Eric Laurentca7cc822012-11-19 14:55:58 -08001431status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1432{
1433 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001434 if (mStatus != NO_ERROR) {
1435 return mStatus;
1436 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001437 status_t status = NO_ERROR;
1438 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1439 status_t cmdStatus;
1440 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001441 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1442 sizeof(audio_mode_t),
1443 &mode,
1444 &size,
1445 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001446 if (status == NO_ERROR) {
1447 status = cmdStatus;
1448 }
1449 }
1450 return status;
1451}
1452
1453status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1454{
1455 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001456 if (mStatus != NO_ERROR) {
1457 return mStatus;
1458 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001459 status_t status = NO_ERROR;
1460 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1461 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001462 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1463 sizeof(audio_source_t),
1464 &source,
1465 &size,
1466 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 }
1468 return status;
1469}
1470
Eric Laurent5baf2af2013-09-12 17:37:00 -07001471status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1472{
1473 Mutex::Autolock _l(mLock);
1474 if (mStatus != NO_ERROR) {
1475 return mStatus;
1476 }
1477 status_t status = NO_ERROR;
1478 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1479 status_t cmdStatus;
1480 uint32_t size = sizeof(status_t);
1481 effect_offload_param_t cmd;
1482
1483 cmd.isOffload = offloaded;
1484 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001485 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1486 sizeof(effect_offload_param_t),
1487 &cmd,
1488 &size,
1489 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001490 if (status == NO_ERROR) {
1491 status = cmdStatus;
1492 }
1493 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1494 } else {
1495 if (offloaded) {
1496 status = INVALID_OPERATION;
1497 }
1498 mOffloaded = false;
1499 }
1500 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1501 return status;
1502}
1503
1504bool AudioFlinger::EffectModule::isOffloaded() const
1505{
1506 Mutex::Autolock _l(mLock);
1507 return mOffloaded;
1508}
1509
Andy Hungbded9c82017-11-30 18:47:35 -08001510static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1511 std::stringstream ss;
1512
1513 if (buffer.get() == nullptr) {
1514 return "nullptr"; // make different than below
1515 } else if (buffer->externalData() != nullptr) {
1516 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1517 << " -> "
1518 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1519 } else {
1520 ss << buffer->audioBuffer()->raw;
1521 }
1522 return ss.str();
1523}
Marco Nelissenb2208842014-02-07 14:00:50 -08001524
Eric Laurente0b9a362019-12-16 19:34:05 -08001525void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001526{
Eric Laurente0b9a362019-12-16 19:34:05 -08001527 EffectBase::dump(fd, args);
1528
Eric Laurentca7cc822012-11-19 14:55:58 -08001529 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001530 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001531
Eric Laurente0b9a362019-12-16 19:34:05 -08001532 result.append("\t\tStatus Engine:\n");
1533 result.appendFormat("\t\t%03d %p\n",
1534 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001535
1536 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001537
1538 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001539 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1540 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1541 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001542 mConfig.inputCfg.buffer.frameCount,
1543 mConfig.inputCfg.samplingRate,
1544 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001545 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001546 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001547
1548 result.append("\t\t- Output configuration:\n");
1549 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001550 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001551 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001552 mConfig.outputCfg.buffer.frameCount,
1553 mConfig.outputCfg.samplingRate,
1554 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001555 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001556 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001557
rago94a1ee82017-07-21 15:11:02 -07001558#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001559
Andy Hungbded9c82017-11-30 18:47:35 -08001560 result.appendFormat("\t\t- HAL buffers:\n"
1561 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1562 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1563 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1564 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1565 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001566#endif
1567
Eric Laurentca7cc822012-11-19 14:55:58 -08001568 write(fd, result.string(), result.length());
1569
Mikhail Naganov4d547672019-02-22 14:19:19 -08001570 if (mEffectInterface != 0) {
1571 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1572 (void)mEffectInterface->dump(fd);
1573 }
1574
Eric Laurentca7cc822012-11-19 14:55:58 -08001575 if (locked) {
1576 mLock.unlock();
1577 }
1578}
1579
1580// ----------------------------------------------------------------------------
1581// EffectHandle implementation
1582// ----------------------------------------------------------------------------
1583
1584#undef LOG_TAG
1585#define LOG_TAG "AudioFlinger::EffectHandle"
1586
Eric Laurente0b9a362019-12-16 19:34:05 -08001587AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001588 const sp<AudioFlinger::Client>& client,
1589 const sp<IEffectClient>& effectClient,
1590 int32_t priority)
1591 : BnEffect(),
1592 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001593 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001594{
Eric Laurent9b2064c2019-11-22 17:25:04 -08001595 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001596
1597 if (client == 0) {
1598 return;
1599 }
1600 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1601 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001602 if (mCblkMemory == 0 ||
1603 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001604 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001605 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001606 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001607 return;
1608 }
Glenn Kastene75da402013-11-20 13:54:52 -08001609 new(mCblk) effect_param_cblk_t();
1610 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001611}
1612
1613AudioFlinger::EffectHandle::~EffectHandle()
1614{
1615 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001616 disconnect(false);
1617}
1618
Glenn Kastene75da402013-11-20 13:54:52 -08001619status_t AudioFlinger::EffectHandle::initCheck()
1620{
1621 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1622}
1623
Eric Laurentca7cc822012-11-19 14:55:58 -08001624status_t AudioFlinger::EffectHandle::enable()
1625{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001626 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 ALOGV("enable %p", this);
Eric Laurente0b9a362019-12-16 19:34:05 -08001628 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001629 if (effect == 0 || mDisconnected) {
1630 return DEAD_OBJECT;
1631 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001632 if (!mHasControl) {
1633 return INVALID_OPERATION;
1634 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001635
1636 if (mEnabled) {
1637 return NO_ERROR;
1638 }
1639
1640 mEnabled = true;
1641
Eric Laurent6c796322019-04-09 14:13:17 -07001642 status_t status = effect->updatePolicyState();
1643 if (status != NO_ERROR) {
1644 mEnabled = false;
1645 return status;
1646 }
1647
Eric Laurent5d885392019-12-13 10:56:31 -08001648 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001649
1650 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001651 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001652 return NO_ERROR;
1653 }
1654
Eric Laurent5d885392019-12-13 10:56:31 -08001655 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001656 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001657 mEnabled = false;
1658 }
1659 return status;
1660}
1661
1662status_t AudioFlinger::EffectHandle::disable()
1663{
1664 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001665 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001666 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001667 if (effect == 0 || mDisconnected) {
1668 return DEAD_OBJECT;
1669 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001670 if (!mHasControl) {
1671 return INVALID_OPERATION;
1672 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001673
1674 if (!mEnabled) {
1675 return NO_ERROR;
1676 }
1677 mEnabled = false;
1678
Eric Laurent6c796322019-04-09 14:13:17 -07001679 effect->updatePolicyState();
1680
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001681 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001682 return NO_ERROR;
1683 }
1684
Eric Laurent5d885392019-12-13 10:56:31 -08001685 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001686 return status;
1687}
1688
1689void AudioFlinger::EffectHandle::disconnect()
1690{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001691 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001692 disconnect(true);
1693}
1694
1695void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1696{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001697 AutoMutex _l(mLock);
1698 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1699 if (mDisconnected) {
1700 if (unpinIfLast) {
1701 android_errorWriteLog(0x534e4554, "32707507");
1702 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 return;
1704 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001705 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001706 {
Eric Laurente0b9a362019-12-16 19:34:05 -08001707 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001708 if (effect != 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08001709 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001710 ALOGW("%s Effect handle %p disconnected after thread destruction",
1711 __func__, this);
1712 }
1713 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001714 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001716
Eric Laurentca7cc822012-11-19 14:55:58 -08001717 if (mClient != 0) {
1718 if (mCblk != NULL) {
1719 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1720 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1721 }
1722 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001723 // Client destructor must run with AudioFlinger client mutex locked
1724 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001725 mClient.clear();
1726 }
1727}
1728
1729status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1730 uint32_t cmdSize,
1731 void *pCmdData,
1732 uint32_t *replySize,
1733 void *pReplyData)
1734{
1735 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001736 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001737
Eric Laurentc7ab3092017-06-15 18:43:46 -07001738 // reject commands reserved for internal use by audio framework if coming from outside
1739 // of audioserver
1740 switch(cmdCode) {
1741 case EFFECT_CMD_ENABLE:
1742 case EFFECT_CMD_DISABLE:
1743 case EFFECT_CMD_SET_PARAM:
1744 case EFFECT_CMD_SET_PARAM_DEFERRED:
1745 case EFFECT_CMD_SET_PARAM_COMMIT:
1746 case EFFECT_CMD_GET_PARAM:
1747 break;
1748 default:
1749 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1750 break;
1751 }
1752 android_errorWriteLog(0x534e4554, "62019992");
1753 return BAD_VALUE;
1754 }
1755
Eric Laurent1ffc5852016-12-15 14:46:09 -08001756 if (cmdCode == EFFECT_CMD_ENABLE) {
1757 if (*replySize < sizeof(int)) {
1758 android_errorWriteLog(0x534e4554, "32095713");
1759 return BAD_VALUE;
1760 }
1761 *(int *)pReplyData = NO_ERROR;
1762 *replySize = sizeof(int);
1763 return enable();
1764 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1765 if (*replySize < sizeof(int)) {
1766 android_errorWriteLog(0x534e4554, "32095713");
1767 return BAD_VALUE;
1768 }
1769 *(int *)pReplyData = NO_ERROR;
1770 *replySize = sizeof(int);
1771 return disable();
1772 }
1773
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 AutoMutex _l(mLock);
Eric Laurente0b9a362019-12-16 19:34:05 -08001775 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001776 if (effect == 0 || mDisconnected) {
1777 return DEAD_OBJECT;
1778 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 // only get parameter command is permitted for applications not controlling the effect
1780 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1781 return INVALID_OPERATION;
1782 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001783
1784 // handle commands that are not forwarded transparently to effect engine
1785 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurent9b2064c2019-11-22 17:25:04 -08001786 if (mClient == 0) {
1787 return INVALID_OPERATION;
1788 }
1789
Eric Laurent1ffc5852016-12-15 14:46:09 -08001790 if (*replySize < sizeof(int)) {
1791 android_errorWriteLog(0x534e4554, "32095713");
1792 return BAD_VALUE;
1793 }
1794 *(int *)pReplyData = NO_ERROR;
1795 *replySize = sizeof(int);
1796
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 // No need to trylock() here as this function is executed in the binder thread serving a
1798 // particular client process: no risk to block the whole media server process or mixer
1799 // threads if we are stuck here
1800 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001801 // keep local copy of index in case of client corruption b/32220769
1802 const uint32_t clientIndex = mCblk->clientIndex;
1803 const uint32_t serverIndex = mCblk->serverIndex;
1804 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1805 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001806 mCblk->serverIndex = 0;
1807 mCblk->clientIndex = 0;
1808 return BAD_VALUE;
1809 }
1810 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001811 effect_param_t *param = NULL;
1812 for (uint32_t index = serverIndex; index < clientIndex;) {
1813 int *p = (int *)(mBuffer + index);
1814 const int size = *p++;
1815 if (size < 0
1816 || size > EFFECT_PARAM_BUFFER_SIZE
1817 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001818 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001819 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001820 break;
1821 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001822
1823 // copy to local memory in case of client corruption b/32220769
George Burgess IV80a22162020-01-05 20:06:15 -08001824 auto *newParam = (effect_param_t *)realloc(param, size);
1825 if (newParam == NULL) {
Andy Hunga447a0f2016-11-15 17:19:58 -08001826 ALOGW("command(): out of memory");
1827 status = NO_MEMORY;
1828 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 }
George Burgess IV80a22162020-01-05 20:06:15 -08001830 param = newParam;
Andy Hunga447a0f2016-11-15 17:19:58 -08001831 memcpy(param, p, size);
1832
1833 int reply = 0;
1834 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001835 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001836 size,
1837 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001838 &rsize,
1839 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001840
1841 // verify shared memory: server index shouldn't change; client index can't go back.
1842 if (serverIndex != mCblk->serverIndex
1843 || clientIndex > mCblk->clientIndex) {
1844 android_errorWriteLog(0x534e4554, "32220769");
1845 status = BAD_VALUE;
1846 break;
1847 }
1848
Eric Laurentca7cc822012-11-19 14:55:58 -08001849 // stop at first error encountered
1850 if (ret != NO_ERROR) {
1851 status = ret;
1852 *(int *)pReplyData = reply;
1853 break;
1854 } else if (reply != NO_ERROR) {
1855 *(int *)pReplyData = reply;
1856 break;
1857 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001858 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001860 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 mCblk->serverIndex = 0;
1862 mCblk->clientIndex = 0;
1863 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001864 }
1865
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001866 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001867}
1868
1869void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1870{
1871 ALOGV("setControl %p control %d", this, hasControl);
1872
1873 mHasControl = hasControl;
1874 mEnabled = enabled;
1875
1876 if (signal && mEffectClient != 0) {
1877 mEffectClient->controlStatusChanged(hasControl);
1878 }
1879}
1880
1881void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1882 uint32_t cmdSize,
1883 void *pCmdData,
1884 uint32_t replySize,
1885 void *pReplyData)
1886{
1887 if (mEffectClient != 0) {
1888 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1889 }
1890}
1891
1892
1893
1894void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1895{
1896 if (mEffectClient != 0) {
1897 mEffectClient->enableStatusChanged(enabled);
1898 }
1899}
1900
1901status_t AudioFlinger::EffectHandle::onTransact(
1902 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1903{
1904 return BnEffect::onTransact(code, data, reply, flags);
1905}
1906
1907
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001908void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001909{
1910 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1911
Marco Nelissenb2208842014-02-07 14:00:50 -08001912 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001913 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001914 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001915 mHasControl ? "yes" : "no",
1916 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001917 mCblk ? mCblk->clientIndex : 0,
1918 mCblk ? mCblk->serverIndex : 0
1919 );
1920
1921 if (locked) {
1922 mCblk->lock.unlock();
1923 }
1924}
1925
1926#undef LOG_TAG
1927#define LOG_TAG "AudioFlinger::EffectChain"
1928
1929AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001930 audio_session_t sessionId)
Eric Laurent5d885392019-12-13 10:56:31 -08001931 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001932 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent5d885392019-12-13 10:56:31 -08001933 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
1934 mEffectCallback(new EffectCallback(this, thread, thread->mAudioFlinger.get()))
Eric Laurentca7cc822012-11-19 14:55:58 -08001935{
1936 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent5d885392019-12-13 10:56:31 -08001937 if (thread == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001938 return;
1939 }
1940 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1941 thread->frameCount();
1942}
1943
1944AudioFlinger::EffectChain::~EffectChain()
1945{
Eric Laurentca7cc822012-11-19 14:55:58 -08001946}
1947
1948// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1949sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1950 effect_descriptor_t *descriptor)
1951{
1952 size_t size = mEffects.size();
1953
1954 for (size_t i = 0; i < size; i++) {
1955 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1956 return mEffects[i];
1957 }
1958 }
1959 return 0;
1960}
1961
1962// getEffectFromId_l() must be called with ThreadBase::mLock held
1963sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1964{
1965 size_t size = mEffects.size();
1966
1967 for (size_t i = 0; i < size; i++) {
1968 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1969 if (id == 0 || mEffects[i]->id() == id) {
1970 return mEffects[i];
1971 }
1972 }
1973 return 0;
1974}
1975
1976// getEffectFromType_l() must be called with ThreadBase::mLock held
1977sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1978 const effect_uuid_t *type)
1979{
1980 size_t size = mEffects.size();
1981
1982 for (size_t i = 0; i < size; i++) {
1983 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1984 return mEffects[i];
1985 }
1986 }
1987 return 0;
1988}
1989
Eric Laurent6c796322019-04-09 14:13:17 -07001990std::vector<int> AudioFlinger::EffectChain::getEffectIds()
1991{
1992 std::vector<int> ids;
1993 Mutex::Autolock _l(mLock);
1994 for (size_t i = 0; i < mEffects.size(); i++) {
1995 ids.push_back(mEffects[i]->id());
1996 }
1997 return ids;
1998}
1999
Eric Laurentca7cc822012-11-19 14:55:58 -08002000void AudioFlinger::EffectChain::clearInputBuffer()
2001{
2002 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002003 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002004}
2005
2006// Must be called with EffectChain::mLock locked
Eric Laurent5d885392019-12-13 10:56:31 -08002007void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002008{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002009 if (mInBuffer == NULL) {
2010 return;
2011 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002012 const size_t frameSize =
Eric Laurent5d885392019-12-13 10:56:31 -08002013 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002014
Eric Laurent5d885392019-12-13 10:56:31 -08002015 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002016 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002017}
2018
2019// Must be called with EffectChain::mLock locked
2020void AudioFlinger::EffectChain::process_l()
2021{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002022 // never process effects when:
2023 // - on an OFFLOAD thread
2024 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent5d885392019-12-13 10:56:31 -08002025 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurenta20c4e92019-11-12 15:55:51 -08002026 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002027 bool tracksOnSession = (trackCnt() != 0);
2028
2029 if (!tracksOnSession && mTailBufferCount == 0) {
2030 doProcess = false;
2031 }
2032
2033 if (activeTrackCnt() == 0) {
2034 // if no track is active and the effect tail has not been rendered,
2035 // the input buffer must be cleared here as the mixer process will not do it
2036 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent5d885392019-12-13 10:56:31 -08002037 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002038 if (mTailBufferCount > 0) {
2039 mTailBufferCount--;
2040 }
2041 }
2042 }
2043 }
2044
2045 size_t size = mEffects.size();
2046 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002047 // Only the input and output buffers of the chain can be external,
2048 // and 'update' / 'commit' do nothing for allocated buffers, thus
2049 // it's not needed to consider any other buffers here.
2050 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002051 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2052 mOutBuffer->update();
2053 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 for (size_t i = 0; i < size; i++) {
2055 mEffects[i]->process();
2056 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002057 mInBuffer->commit();
2058 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2059 mOutBuffer->commit();
2060 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002061 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002062 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002063 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002064 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2065 }
2066 if (doResetVolume) {
2067 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002068 }
2069}
2070
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002071// createEffect_l() must be called with ThreadBase::mLock held
2072status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002073 effect_descriptor_t *desc,
2074 int id,
2075 audio_session_t sessionId,
2076 bool pinned)
2077{
2078 Mutex::Autolock _l(mLock);
Eric Laurent9b2064c2019-11-22 17:25:04 -08002079 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002080 status_t lStatus = effect->status();
2081 if (lStatus == NO_ERROR) {
2082 lStatus = addEffect_ll(effect);
2083 }
2084 if (lStatus != NO_ERROR) {
2085 effect.clear();
2086 }
2087 return lStatus;
2088}
2089
2090// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002091status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2092{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002093 Mutex::Autolock _l(mLock);
2094 return addEffect_ll(effect);
2095}
2096// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2097status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2098{
Eric Laurentca7cc822012-11-19 14:55:58 -08002099 effect_descriptor_t desc = effect->desc();
2100 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2101
Eric Laurent5d885392019-12-13 10:56:31 -08002102 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002103
2104 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2105 // Auxiliary effects are inserted at the beginning of mEffects vector as
2106 // they are processed first and accumulated in chain input buffer
2107 mEffects.insertAt(effect, 0);
2108
2109 // the input buffer for auxiliary effect contains mono samples in
2110 // 32 bit format. This is to avoid saturation in AudoMixer
2111 // accumulation stage. Saturation is done in EffectModule::process() before
2112 // calling the process in effect engine
Eric Laurent5d885392019-12-13 10:56:31 -08002113 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002114 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002115#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent5d885392019-12-13 10:56:31 -08002116 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002117 numSamples * sizeof(float), &halBuffer);
2118#else
Eric Laurent5d885392019-12-13 10:56:31 -08002119 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002120 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002121#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002122 if (result != OK) return result;
2123 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002124 // auxiliary effects output samples to chain input buffer for further processing
2125 // by insert effects
2126 effect->setOutBuffer(mInBuffer);
2127 } else {
2128 // Insert effects are inserted at the end of mEffects vector as they are processed
2129 // after track and auxiliary effects.
2130 // Insert effect order as a function of indicated preference:
2131 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2132 // another effect is present
2133 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2134 // last effect claiming first position
2135 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2136 // first effect claiming last position
2137 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2138 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2139 // already present
2140
2141 size_t size = mEffects.size();
2142 size_t idx_insert = size;
2143 ssize_t idx_insert_first = -1;
2144 ssize_t idx_insert_last = -1;
2145
2146 for (size_t i = 0; i < size; i++) {
2147 effect_descriptor_t d = mEffects[i]->desc();
2148 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2149 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2150 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2151 // check invalid effect chaining combinations
2152 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2153 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2154 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2155 desc.name, d.name);
2156 return INVALID_OPERATION;
2157 }
2158 // remember position of first insert effect and by default
2159 // select this as insert position for new effect
2160 if (idx_insert == size) {
2161 idx_insert = i;
2162 }
2163 // remember position of last insert effect claiming
2164 // first position
2165 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2166 idx_insert_first = i;
2167 }
2168 // remember position of first insert effect claiming
2169 // last position
2170 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2171 idx_insert_last == -1) {
2172 idx_insert_last = i;
2173 }
2174 }
2175 }
2176
2177 // modify idx_insert from first position if needed
2178 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2179 if (idx_insert_last != -1) {
2180 idx_insert = idx_insert_last;
2181 } else {
2182 idx_insert = size;
2183 }
2184 } else {
2185 if (idx_insert_first != -1) {
2186 idx_insert = idx_insert_first + 1;
2187 }
2188 }
2189
2190 // always read samples from chain input buffer
2191 effect->setInBuffer(mInBuffer);
2192
2193 // if last effect in the chain, output samples to chain
2194 // output buffer, otherwise to chain input buffer
2195 if (idx_insert == size) {
2196 if (idx_insert != 0) {
2197 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2198 mEffects[idx_insert-1]->configure();
2199 }
2200 effect->setOutBuffer(mOutBuffer);
2201 } else {
2202 effect->setOutBuffer(mInBuffer);
2203 }
2204 mEffects.insertAt(effect, idx_insert);
2205
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002206 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002207 idx_insert);
2208 }
2209 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002210
Eric Laurentca7cc822012-11-19 14:55:58 -08002211 return NO_ERROR;
2212}
2213
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002214// removeEffect_l() must be called with ThreadBase::mLock held
2215size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2216 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002217{
2218 Mutex::Autolock _l(mLock);
2219 size_t size = mEffects.size();
2220 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2221
2222 for (size_t i = 0; i < size; i++) {
2223 if (effect == mEffects[i]) {
2224 // calling stop here will remove pre-processing effect from the audio HAL.
2225 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2226 // the middle of a read from audio HAL
2227 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2228 mEffects[i]->state() == EffectModule::STOPPING) {
2229 mEffects[i]->stop();
2230 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002231 if (release) {
2232 mEffects[i]->release_l();
2233 }
2234
Mikhail Naganov022b9952017-01-04 16:36:51 -08002235 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002236 if (i == size - 1 && i != 0) {
2237 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2238 mEffects[i - 1]->configure();
2239 }
2240 }
2241 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002242 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002243 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002244
Eric Laurentca7cc822012-11-19 14:55:58 -08002245 break;
2246 }
2247 }
2248
2249 return mEffects.size();
2250}
2251
jiabinb8269fd2019-11-11 12:16:27 -08002252// setDevices_l() must be called with ThreadBase::mLock held
2253void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002254{
2255 size_t size = mEffects.size();
2256 for (size_t i = 0; i < size; i++) {
jiabinb8269fd2019-11-11 12:16:27 -08002257 mEffects[i]->setDevices(devices);
2258 }
2259}
2260
2261// setInputDevice_l() must be called with ThreadBase::mLock held
2262void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2263{
2264 size_t size = mEffects.size();
2265 for (size_t i = 0; i < size; i++) {
2266 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002267 }
2268}
2269
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002270// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002271void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2272{
2273 size_t size = mEffects.size();
2274 for (size_t i = 0; i < size; i++) {
2275 mEffects[i]->setMode(mode);
2276 }
2277}
2278
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002279// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002280void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2281{
2282 size_t size = mEffects.size();
2283 for (size_t i = 0; i < size; i++) {
2284 mEffects[i]->setAudioSource(source);
2285 }
2286}
2287
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002288// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002289bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002290{
2291 uint32_t newLeft = *left;
2292 uint32_t newRight = *right;
2293 bool hasControl = false;
2294 int ctrlIdx = -1;
2295 size_t size = mEffects.size();
2296
2297 // first update volume controller
2298 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002299 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002300 ctrlIdx = i - 1;
2301 hasControl = true;
2302 break;
2303 }
2304 }
2305
Eric Laurentfa1e1232016-08-02 19:01:49 -07002306 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002307 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002308 if (hasControl) {
2309 *left = mNewLeftVolume;
2310 *right = mNewRightVolume;
2311 }
2312 return hasControl;
2313 }
2314
2315 mVolumeCtrlIdx = ctrlIdx;
2316 mLeftVolume = newLeft;
2317 mRightVolume = newRight;
2318
2319 // second get volume update from volume controller
2320 if (ctrlIdx >= 0) {
2321 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2322 mNewLeftVolume = newLeft;
2323 mNewRightVolume = newRight;
2324 }
2325 // then indicate volume to all other effects in chain.
2326 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002327 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002328 uint32_t lVol = newLeft;
2329 uint32_t rVol = newRight;
2330
2331 for (size_t i = 0; i < size; i++) {
2332 if ((int)i == ctrlIdx) {
2333 continue;
2334 }
2335 // this also works for ctrlIdx == -1 when there is no volume controller
2336 if ((int)i > ctrlIdx) {
2337 lVol = *left;
2338 rVol = *right;
2339 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002340 // Pass requested volume directly if this is volume monitor module
2341 if (mEffects[i]->isVolumeMonitor()) {
2342 mEffects[i]->setVolume(left, right, false);
2343 } else {
2344 mEffects[i]->setVolume(&lVol, &rVol, false);
2345 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002346 }
2347 *left = newLeft;
2348 *right = newRight;
2349
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002350 setVolumeForOutput_l(*left, *right);
2351
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 return hasControl;
2353}
2354
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002355// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002356void AudioFlinger::EffectChain::resetVolume_l()
2357{
Eric Laurente7449bf2016-08-03 18:44:07 -07002358 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2359 uint32_t left = mLeftVolume;
2360 uint32_t right = mRightVolume;
2361 (void)setVolume_l(&left, &right, true);
2362 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002363}
2364
Eric Laurent1b928682014-10-02 19:41:47 -07002365void AudioFlinger::EffectChain::syncHalEffectsState()
2366{
2367 Mutex::Autolock _l(mLock);
2368 for (size_t i = 0; i < mEffects.size(); i++) {
2369 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2370 mEffects[i]->state() == EffectModule::STOPPING) {
2371 mEffects[i]->addEffectToHal_l();
2372 }
2373 }
2374}
2375
Eric Laurentca7cc822012-11-19 14:55:58 -08002376void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2377{
Eric Laurentca7cc822012-11-19 14:55:58 -08002378 String8 result;
2379
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002380 const size_t numEffects = mEffects.size();
2381 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002382
Marco Nelissenb2208842014-02-07 14:00:50 -08002383 if (numEffects) {
2384 bool locked = AudioFlinger::dumpTryLock(mLock);
2385 // failed to lock - AudioFlinger is probably deadlocked
2386 if (!locked) {
2387 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002388 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002389
Andy Hungbded9c82017-11-30 18:47:35 -08002390 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2391 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2392 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2393 (int)inBufferStr.size(), "In buffer ",
2394 (int)outBufferStr.size(), "Out buffer ");
2395 result.appendFormat("\t%s %s %d\n",
2396 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002397 write(fd, result.string(), result.size());
2398
2399 for (size_t i = 0; i < numEffects; ++i) {
2400 sp<EffectModule> effect = mEffects[i];
2401 if (effect != 0) {
2402 effect->dump(fd, args);
2403 }
2404 }
2405
2406 if (locked) {
2407 mLock.unlock();
2408 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002409 } else {
2410 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002411 }
2412}
2413
2414// must be called with ThreadBase::mLock held
2415void AudioFlinger::EffectChain::setEffectSuspended_l(
2416 const effect_uuid_t *type, bool suspend)
2417{
2418 sp<SuspendedEffectDesc> desc;
2419 // use effect type UUID timelow as key as there is no real risk of identical
2420 // timeLow fields among effect type UUIDs.
2421 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2422 if (suspend) {
2423 if (index >= 0) {
2424 desc = mSuspendedEffects.valueAt(index);
2425 } else {
2426 desc = new SuspendedEffectDesc();
2427 desc->mType = *type;
2428 mSuspendedEffects.add(type->timeLow, desc);
2429 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2430 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002431
Eric Laurentca7cc822012-11-19 14:55:58 -08002432 if (desc->mRefCount++ == 0) {
2433 sp<EffectModule> effect = getEffectIfEnabled(type);
2434 if (effect != 0) {
2435 desc->mEffect = effect;
2436 effect->setSuspended(true);
Eric Laurent5d885392019-12-13 10:56:31 -08002437 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002438 }
2439 }
2440 } else {
2441 if (index < 0) {
2442 return;
2443 }
2444 desc = mSuspendedEffects.valueAt(index);
2445 if (desc->mRefCount <= 0) {
2446 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002447 desc->mRefCount = 0;
2448 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002449 }
2450 if (--desc->mRefCount == 0) {
2451 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2452 if (desc->mEffect != 0) {
2453 sp<EffectModule> effect = desc->mEffect.promote();
2454 if (effect != 0) {
2455 effect->setSuspended(false);
2456 effect->lock();
2457 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002458 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002459 effect->setEnabled_l(handle->enabled());
2460 }
2461 effect->unlock();
2462 }
2463 desc->mEffect.clear();
2464 }
2465 mSuspendedEffects.removeItemsAt(index);
2466 }
2467 }
2468}
2469
2470// must be called with ThreadBase::mLock held
2471void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2472{
2473 sp<SuspendedEffectDesc> desc;
2474
2475 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2476 if (suspend) {
2477 if (index >= 0) {
2478 desc = mSuspendedEffects.valueAt(index);
2479 } else {
2480 desc = new SuspendedEffectDesc();
2481 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2482 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2483 }
2484 if (desc->mRefCount++ == 0) {
2485 Vector< sp<EffectModule> > effects;
2486 getSuspendEligibleEffects(effects);
2487 for (size_t i = 0; i < effects.size(); i++) {
2488 setEffectSuspended_l(&effects[i]->desc().type, true);
2489 }
2490 }
2491 } else {
2492 if (index < 0) {
2493 return;
2494 }
2495 desc = mSuspendedEffects.valueAt(index);
2496 if (desc->mRefCount <= 0) {
2497 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2498 desc->mRefCount = 1;
2499 }
2500 if (--desc->mRefCount == 0) {
2501 Vector<const effect_uuid_t *> types;
2502 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2503 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2504 continue;
2505 }
2506 types.add(&mSuspendedEffects.valueAt(i)->mType);
2507 }
2508 for (size_t i = 0; i < types.size(); i++) {
2509 setEffectSuspended_l(types[i], false);
2510 }
2511 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2512 mSuspendedEffects.keyAt(index));
2513 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2514 }
2515 }
2516}
2517
2518
2519// The volume effect is used for automated tests only
2520#ifndef OPENSL_ES_H_
2521static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2522 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2523const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2524#endif //OPENSL_ES_H_
2525
Eric Laurentd8365c52017-07-16 15:27:05 -07002526/* static */
2527bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2528{
2529 // Only NS and AEC are suspended when BtNRec is off
2530 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2531 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2532 return true;
2533 }
2534 return false;
2535}
2536
Eric Laurentca7cc822012-11-19 14:55:58 -08002537bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2538{
2539 // auxiliary effects and visualizer are never suspended on output mix
2540 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2541 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2542 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002543 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2544 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002545 return false;
2546 }
2547 return true;
2548}
2549
2550void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2551 Vector< sp<AudioFlinger::EffectModule> > &effects)
2552{
2553 effects.clear();
2554 for (size_t i = 0; i < mEffects.size(); i++) {
2555 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2556 effects.add(mEffects[i]);
2557 }
2558 }
2559}
2560
2561sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2562 const effect_uuid_t *type)
2563{
2564 sp<EffectModule> effect = getEffectFromType_l(type);
2565 return effect != 0 && effect->isEnabled() ? effect : 0;
2566}
2567
2568void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2569 bool enabled)
2570{
2571 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2572 if (enabled) {
2573 if (index < 0) {
2574 // if the effect is not suspend check if all effects are suspended
2575 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2576 if (index < 0) {
2577 return;
2578 }
2579 if (!isEffectEligibleForSuspend(effect->desc())) {
2580 return;
2581 }
2582 setEffectSuspended_l(&effect->desc().type, enabled);
2583 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2584 if (index < 0) {
2585 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2586 return;
2587 }
2588 }
2589 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2590 effect->desc().type.timeLow);
2591 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002592 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002593 if (desc->mEffect == 0) {
2594 desc->mEffect = effect;
Eric Laurent5d885392019-12-13 10:56:31 -08002595 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002596 effect->setSuspended(true);
2597 }
2598 } else {
2599 if (index < 0) {
2600 return;
2601 }
2602 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2603 effect->desc().type.timeLow);
2604 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2605 desc->mEffect.clear();
2606 effect->setSuspended(false);
2607 }
2608}
2609
Eric Laurent5baf2af2013-09-12 17:37:00 -07002610bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002611{
2612 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002613 return isNonOffloadableEnabled_l();
2614}
2615
2616bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2617{
Eric Laurent813e2a72013-08-31 12:59:48 -07002618 size_t size = mEffects.size();
2619 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002620 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002621 return true;
2622 }
2623 }
2624 return false;
2625}
2626
Eric Laurentaaa44472014-09-12 17:41:50 -07002627void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2628{
2629 Mutex::Autolock _l(mLock);
Eric Laurent5d885392019-12-13 10:56:31 -08002630 mEffectCallback->setThread(thread.get());
Eric Laurentaaa44472014-09-12 17:41:50 -07002631}
2632
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002633void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2634{
2635 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2636 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2637 }
2638 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2639 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2640 }
2641}
2642
2643void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2644{
2645 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2646 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2647 }
2648 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2649 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2650 }
2651}
2652
2653bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002654{
2655 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002656 for (const auto &effect : mEffects) {
2657 if (effect->isProcessImplemented()) {
2658 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002659 }
2660 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002661 // Allow effects without processing.
2662 return true;
2663}
2664
2665bool AudioFlinger::EffectChain::isFastCompatible() const
2666{
2667 Mutex::Autolock _l(mLock);
2668 for (const auto &effect : mEffects) {
2669 if (effect->isProcessImplemented()
2670 && effect->isImplementationSoftware()) {
2671 return false;
2672 }
2673 }
2674 // Allow effects without processing or hw accelerated effects.
2675 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002676}
2677
2678// isCompatibleWithThread_l() must be called with thread->mLock held
2679bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2680{
2681 Mutex::Autolock _l(mLock);
2682 for (size_t i = 0; i < mEffects.size(); i++) {
2683 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2684 return false;
2685 }
2686 }
2687 return true;
2688}
2689
Eric Laurent5d885392019-12-13 10:56:31 -08002690// EffectCallbackInterface implementation
2691status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2692 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2693 sp<EffectHalInterface> *effect) {
2694 status_t status = NO_INIT;
2695 sp<AudioFlinger> af = mAudioFlinger.promote();
2696 if (af == nullptr) {
2697 return status;
2698 }
2699 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2700 if (effectsFactory != 0) {
2701 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2702 }
2703 return status;
2704}
2705
2706bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurente0b9a362019-12-16 19:34:05 -08002707 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002708 sp<AudioFlinger> af = mAudioFlinger.promote();
2709 if (af == nullptr) {
2710 return false;
2711 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002712 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2713 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002714}
2715
2716status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2717 size_t size, sp<EffectBufferHalInterface>* buffer) {
2718 sp<AudioFlinger> af = mAudioFlinger.promote();
2719 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2720 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2721}
2722
2723status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2724 sp<EffectHalInterface> effect) {
2725 status_t result = NO_INIT;
2726 sp<ThreadBase> t = mThread.promote();
2727 if (t == nullptr) {
2728 return result;
2729 }
2730 sp <StreamHalInterface> st = t->stream();
2731 if (st == nullptr) {
2732 return result;
2733 }
2734 result = st->addEffect(effect);
2735 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2736 return result;
2737}
2738
2739status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2740 sp<EffectHalInterface> effect) {
2741 status_t result = NO_INIT;
2742 sp<ThreadBase> t = mThread.promote();
2743 if (t == nullptr) {
2744 return result;
2745 }
2746 sp <StreamHalInterface> st = t->stream();
2747 if (st == nullptr) {
2748 return result;
2749 }
2750 result = st->removeEffect(effect);
2751 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2752 return result;
2753}
2754
2755audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2756 sp<ThreadBase> t = mThread.promote();
2757 if (t == nullptr) {
2758 return AUDIO_IO_HANDLE_NONE;
2759 }
2760 return t->id();
2761}
2762
2763bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2764 sp<ThreadBase> t = mThread.promote();
2765 if (t == nullptr) {
2766 return true;
2767 }
2768 return t->isOutput();
2769}
2770
2771bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2772 sp<ThreadBase> t = mThread.promote();
2773 if (t == nullptr) {
2774 return false;
2775 }
2776 return t->type() == ThreadBase::OFFLOAD;
2777}
2778
2779bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2780 sp<ThreadBase> t = mThread.promote();
2781 if (t == nullptr) {
2782 return false;
2783 }
2784 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2785}
2786
2787bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2788 sp<ThreadBase> t = mThread.promote();
2789 if (t == nullptr) {
2790 return false;
2791 }
2792 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2793}
2794
2795uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2796 sp<ThreadBase> t = mThread.promote();
2797 if (t == nullptr) {
2798 return 0;
2799 }
2800 return t->sampleRate();
2801}
2802
2803audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2804 sp<ThreadBase> t = mThread.promote();
2805 if (t == nullptr) {
2806 return AUDIO_CHANNEL_NONE;
2807 }
2808 return t->channelMask();
2809}
2810
2811uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2812 sp<ThreadBase> t = mThread.promote();
2813 if (t == nullptr) {
2814 return 0;
2815 }
2816 return t->channelCount();
2817}
2818
2819size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2820 sp<ThreadBase> t = mThread.promote();
2821 if (t == nullptr) {
2822 return 0;
2823 }
2824 return t->frameCount();
2825}
2826
2827uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2828 sp<ThreadBase> t = mThread.promote();
2829 if (t == nullptr) {
2830 return 0;
2831 }
2832 return t->latency_l();
2833}
2834
2835void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2836 sp<ThreadBase> t = mThread.promote();
2837 if (t == nullptr) {
2838 return;
2839 }
2840 t->setVolumeForOutput_l(left, right);
2841}
2842
2843void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurente0b9a362019-12-16 19:34:05 -08002844 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent5d885392019-12-13 10:56:31 -08002845 sp<ThreadBase> t = mThread.promote();
2846 if (t == nullptr) {
2847 return;
2848 }
2849 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2850
2851 sp<EffectChain> c = mChain.promote();
2852 if (c == nullptr) {
2853 return;
2854 }
Eric Laurente0b9a362019-12-16 19:34:05 -08002855 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2856 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent5d885392019-12-13 10:56:31 -08002857}
2858
Eric Laurente0b9a362019-12-16 19:34:05 -08002859void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002860 sp<ThreadBase> t = mThread.promote();
2861 if (t == 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 t->onEffectEnable(effect->asEffectModule());
Eric Laurent5d885392019-12-13 10:56:31 -08002866}
2867
Eric Laurente0b9a362019-12-16 19:34:05 -08002868void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent5d885392019-12-13 10:56:31 -08002869 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2870
2871 sp<ThreadBase> t = mThread.promote();
2872 if (t == nullptr) {
2873 return;
2874 }
2875 t->onEffectDisable();
2876}
2877
2878bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2879 bool unpinIfLast) {
2880 sp<ThreadBase> t = mThread.promote();
2881 if (t == nullptr) {
2882 return false;
2883 }
2884 t->disconnectEffectHandle(handle, unpinIfLast);
2885 return true;
2886}
2887
2888void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2889 sp<EffectChain> c = mChain.promote();
2890 if (c == nullptr) {
2891 return;
2892 }
2893 c->resetVolume_l();
2894
2895}
2896
2897uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2898 sp<EffectChain> c = mChain.promote();
2899 if (c == nullptr) {
2900 return PRODUCT_STRATEGY_NONE;
2901 }
2902 return c->strategy();
2903}
2904
2905int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2906 sp<EffectChain> c = mChain.promote();
2907 if (c == nullptr) {
2908 return 0;
2909 }
2910 return c->activeTrackCnt();
2911}
2912
Eric Laurent9b2064c2019-11-22 17:25:04 -08002913
2914#undef LOG_TAG
2915#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2916
2917status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2918{
2919 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2920 Mutex::Autolock _l(mProxyLock);
2921 if (status == NO_ERROR) {
2922 for (auto& handle : mEffectHandles) {
2923 if (enabled) {
2924 status = handle.second->enable();
2925 } else {
2926 status = handle.second->disable();
2927 }
2928 }
2929 }
2930 ALOGV("%s enable %d status %d", __func__, enabled, status);
2931 return status;
2932}
2933
2934status_t AudioFlinger::DeviceEffectProxy::init(
2935 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2936//For all audio patches
2937//If src or sink device match
2938//If the effect is HW accelerated
2939// if no corresponding effect module
2940// Create EffectModule: mHalEffect
2941//Create and attach EffectHandle
2942//If the effect is not HW accelerated and the patch sink or src is a mixer port
2943// Create Effect on patch input or output thread on session -1
2944//Add EffectHandle to EffectHandle map of Effect Proxy:
2945 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2946 status_t status = NO_ERROR;
2947 for (auto &patch : patches) {
2948 status = onCreatePatch(patch.first, patch.second);
2949 ALOGV("%s onCreatePatch status %d", __func__, status);
2950 if (status == BAD_VALUE) {
2951 return status;
2952 }
2953 }
2954 return status;
2955}
2956
2957status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2958 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2959 status_t status = NAME_NOT_FOUND;
2960 sp<EffectHandle> handle;
2961 // only consider source[0] as this is the only "true" source of a patch
2962 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2963 ALOGV("%s source checkPort status %d", __func__, status);
2964 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2965 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2966 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2967 }
2968 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2969 Mutex::Autolock _l(mProxyLock);
2970 mEffectHandles.emplace(patchHandle, handle);
2971 }
2972 ALOGW_IF(status == BAD_VALUE,
2973 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2974
2975 return status;
2976}
2977
2978status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
2979 const struct audio_port_config *port, sp <EffectHandle> *handle) {
2980
2981 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
2982 __func__, port->type, port->ext.device.type,
2983 port->ext.device.address, port->id, patch.isSoftware());
2984 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
2985 || port->ext.device.address != mDevice.mAddress) {
2986 return NAME_NOT_FOUND;
2987 }
2988 status_t status = NAME_NOT_FOUND;
2989
2990 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
2991 Mutex::Autolock _l(mProxyLock);
2992 mDevicePort = *port;
2993 mHalEffect = new EffectModule(mMyCallback,
2994 const_cast<effect_descriptor_t *>(&mDescriptor),
2995 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
2996 false /* pinned */, port->id);
2997 if (audio_is_input_device(mDevice.mType)) {
2998 mHalEffect->setInputDevice(mDevice);
2999 } else {
3000 mHalEffect->setDevices({mDevice});
3001 }
3002 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3003 status = (*handle)->initCheck();
3004 if (status == OK) {
3005 status = mHalEffect->addHandle((*handle).get());
3006 } else {
3007 mHalEffect.clear();
3008 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3009 }
3010 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3011 sp <ThreadBase> thread;
3012 if (audio_port_config_has_input_direction(port)) {
3013 if (patch.isSoftware()) {
3014 thread = patch.mRecord.thread();
3015 } else {
3016 thread = patch.thread().promote();
3017 }
3018 } else {
3019 if (patch.isSoftware()) {
3020 thread = patch.mPlayback.thread();
3021 } else {
3022 thread = patch.thread().promote();
3023 }
3024 }
3025 int enabled;
3026 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3027 const_cast<effect_descriptor_t *>(&mDescriptor),
3028 &enabled, &status, false);
3029 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3030 } else {
3031 status = BAD_VALUE;
3032 }
3033 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3034 if (isEnabled()) {
3035 (*handle)->enable();
3036 } else {
3037 (*handle)->disable();
3038 }
3039 }
3040 return status;
3041}
3042
3043void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3044 Mutex::Autolock _l(mProxyLock);
3045 mEffectHandles.erase(patchHandle);
3046}
3047
3048
3049size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3050{
3051 Mutex::Autolock _l(mProxyLock);
3052 if (effect == mHalEffect) {
3053 mHalEffect.clear();
3054 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3055 }
3056 return mHalEffect == nullptr ? 0 : 1;
3057}
3058
3059status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3060 sp<EffectHalInterface> effect) {
3061 if (mHalEffect == nullptr) {
3062 return NO_INIT;
3063 }
3064 return mManagerCallback->addEffectToHal(
3065 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3066}
3067
3068status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3069 sp<EffectHalInterface> effect) {
3070 if (mHalEffect == nullptr) {
3071 return NO_INIT;
3072 }
3073 return mManagerCallback->removeEffectFromHal(
3074 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3075}
3076
3077bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3078 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3079 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3080 }
3081 return true;
3082}
3083
3084uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3085 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3086 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3087 return mDevicePort.sample_rate;
3088 }
3089 return DEFAULT_OUTPUT_SAMPLE_RATE;
3090}
3091
3092audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3093 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3094 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3095 return mDevicePort.channel_mask;
3096 }
3097 return AUDIO_CHANNEL_OUT_STEREO;
3098}
3099
3100uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3101 if (isOutput()) {
3102 return audio_channel_count_from_out_mask(channelMask());
3103 }
3104 return audio_channel_count_from_in_mask(channelMask());
3105}
3106
3107void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3108 const Vector<String16> args;
3109 EffectBase::dump(fd, args);
3110
3111 const bool locked = dumpTryLock(mProxyLock);
3112 if (!locked) {
3113 String8 result("DeviceEffectProxy may be deadlocked\n");
3114 write(fd, result.string(), result.size());
3115 }
3116
3117 String8 outStr;
3118 if (mHalEffect != nullptr) {
3119 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3120 } else {
3121 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3122 }
3123 write(fd, outStr.string(), outStr.size());
3124 outStr.clear();
3125
3126 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3127 write(fd, outStr.string(), outStr.size());
3128 outStr.clear();
3129
3130 for (const auto& iter : mEffectHandles) {
3131 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3132 write(fd, outStr.string(), outStr.size());
3133 outStr.clear();
3134 sp<EffectBase> effect = iter.second->effect().promote();
3135 if (effect != nullptr) {
3136 effect->dump(fd, args);
3137 }
3138 }
3139
3140 if (locked) {
3141 mLock.unlock();
3142 }
3143}
3144
3145#undef LOG_TAG
3146#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3147
3148int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3149 return mManagerCallback->newEffectId();
3150}
3151
3152
3153bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3154 EffectHandle *handle, bool unpinIfLast) {
3155 sp<EffectBase> effectBase = handle->effect().promote();
3156 if (effectBase == nullptr) {
3157 return false;
3158 }
3159
3160 sp<EffectModule> effect = effectBase->asEffectModule();
3161 if (effect == nullptr) {
3162 return false;
3163 }
3164
3165 // restore suspended effects if the disconnected handle was enabled and the last one.
3166 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3167 if (remove) {
3168 sp<DeviceEffectProxy> proxy = mProxy.promote();
3169 if (proxy != nullptr) {
3170 proxy->removeEffect(effect);
3171 }
3172 if (handle->enabled()) {
3173 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3174 }
3175 }
3176 return true;
3177}
3178
3179status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3180 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3181 sp<EffectHalInterface> *effect) {
3182 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3183}
3184
3185status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3186 sp<EffectHalInterface> effect) {
3187 sp<DeviceEffectProxy> proxy = mProxy.promote();
3188 if (proxy == nullptr) {
3189 return NO_INIT;
3190 }
3191 return proxy->addEffectToHal(effect);
3192}
3193
3194status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3195 sp<EffectHalInterface> effect) {
3196 sp<DeviceEffectProxy> proxy = mProxy.promote();
3197 if (proxy == nullptr) {
3198 return NO_INIT;
3199 }
3200 return proxy->addEffectToHal(effect);
3201}
3202
3203bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3204 sp<DeviceEffectProxy> proxy = mProxy.promote();
3205 if (proxy == nullptr) {
3206 return true;
3207 }
3208 return proxy->isOutput();
3209}
3210
3211uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3212 sp<DeviceEffectProxy> proxy = mProxy.promote();
3213 if (proxy == nullptr) {
3214 return DEFAULT_OUTPUT_SAMPLE_RATE;
3215 }
3216 return proxy->sampleRate();
3217}
3218
3219audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3220 sp<DeviceEffectProxy> proxy = mProxy.promote();
3221 if (proxy == nullptr) {
3222 return AUDIO_CHANNEL_OUT_STEREO;
3223 }
3224 return proxy->channelMask();
3225}
3226
3227uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3228 sp<DeviceEffectProxy> proxy = mProxy.promote();
3229 if (proxy == nullptr) {
3230 return 2;
3231 }
3232 return proxy->channelCount();
3233}
3234
Glenn Kasten63238ef2015-03-02 15:50:29 -08003235} // namespace android