blob: eaad6efff374c2398f9278a5efff58f84f8f0257 [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>
jiabineb3bda02020-06-30 14:07:03 -070028#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070029#include <system/audio_effects/effect_ns.h>
30#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080031#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080032#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070033#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070035#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070037#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070038#include <media/audiohal/EffectHalInterface.h>
39#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44// ----------------------------------------------------------------------------
45
46// Note: the following macro is used for extremely verbose logging message. In
47// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
48// 0; but one side effect of this is to turn all LOGV's as well. Some messages
49// are so verbose that we want to suppress them even when we have ALOG_ASSERT
50// turned on. Do not uncomment the #def below unless you really know what you
51// are doing and want to see all of the extremely verbose messages.
52//#define VERY_VERY_VERBOSE_LOGGING
53#ifdef VERY_VERY_VERBOSE_LOGGING
54#define ALOGVV ALOGV
55#else
56#define ALOGVV(a...) do { } while(0)
57#endif
58
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090059#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
60
Eric Laurentca7cc822012-11-19 14:55:58 -080061namespace android {
62
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070063using binder::Status;
64
65namespace {
66
67// Append a POD value into a vector of bytes.
68template<typename T>
69void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
70 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
71 buffer->insert(buffer->end(), ar, ar + sizeof(T));
72}
73
74// Write a POD value into a vector of bytes (clears the previous buffer
75// content).
76template<typename T>
77void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
78 buffer->clear();
79 appendToBuffer(value, buffer);
80}
81
82} // namespace
83
Eric Laurentca7cc822012-11-19 14:55:58 -080084// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080085// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080086// ----------------------------------------------------------------------------
87
88#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080089#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080090
Eric Laurent41709552019-12-16 19:34:05 -080091AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080092 effect_descriptor_t *desc,
93 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080094 audio_session_t sessionId,
95 bool pinned)
96 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080097 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080098 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080099{
Eric Laurentca7cc822012-11-19 14:55:58 -0800100}
101
Eric Laurent41709552019-12-16 19:34:05 -0800102// must be called with EffectModule::mLock held
103status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurent41709552019-12-16 19:34:05 -0800106 ALOGV("setEnabled %p enabled %d", this, enabled);
107
108 if (enabled != isEnabled()) {
109 switch (mState) {
110 // going from disabled to enabled
111 case IDLE:
112 mState = STARTING;
113 break;
114 case STOPPED:
115 mState = RESTART;
116 break;
117 case STOPPING:
118 mState = ACTIVE;
119 break;
120
121 // going from enabled to disabled
122 case RESTART:
123 mState = STOPPED;
124 break;
125 case STARTING:
126 mState = IDLE;
127 break;
128 case ACTIVE:
129 mState = STOPPING;
130 break;
131 case DESTROYED:
132 return NO_ERROR; // simply ignore as we are being destroyed
133 }
134 for (size_t i = 1; i < mHandles.size(); i++) {
135 EffectHandle *h = mHandles[i];
136 if (h != NULL && !h->disconnected()) {
137 h->setEnabled(enabled);
138 }
139 }
140 }
141 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800142}
143
Eric Laurent41709552019-12-16 19:34:05 -0800144status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
145{
146 status_t status;
147 {
148 Mutex::Autolock _l(mLock);
149 status = setEnabled_l(enabled);
150 }
151 if (fromHandle) {
152 if (enabled) {
153 if (status != NO_ERROR) {
154 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
155 } else {
156 mCallback->onEffectEnable(this);
157 }
158 } else {
159 mCallback->onEffectDisable(this);
160 }
161 }
162 return status;
163}
164
165bool AudioFlinger::EffectBase::isEnabled() const
166{
167 switch (mState) {
168 case RESTART:
169 case STARTING:
170 case ACTIVE:
171 return true;
172 case IDLE:
173 case STOPPING:
174 case STOPPED:
175 case DESTROYED:
176 default:
177 return false;
178 }
179}
180
181void AudioFlinger::EffectBase::setSuspended(bool suspended)
182{
183 Mutex::Autolock _l(mLock);
184 mSuspended = suspended;
185}
186
187bool AudioFlinger::EffectBase::suspended() const
188{
189 Mutex::Autolock _l(mLock);
190 return mSuspended;
191}
192
193status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800194{
195 status_t status;
196
197 Mutex::Autolock _l(mLock);
198 int priority = handle->priority();
199 size_t size = mHandles.size();
200 EffectHandle *controlHandle = NULL;
201 size_t i;
202 for (i = 0; i < size; i++) {
203 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800204 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 continue;
206 }
207 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800209 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700210 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800211 if (h->priority() <= priority) {
212 break;
213 }
214 }
215 // if inserted in first place, move effect control from previous owner to this handle
216 if (i == 0) {
217 bool enabled = false;
218 if (controlHandle != NULL) {
219 enabled = controlHandle->enabled();
220 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
221 }
222 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
223 status = NO_ERROR;
224 } else {
225 status = ALREADY_EXISTS;
226 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700227 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800228 mHandles.insertAt(handle, i);
229 return status;
230}
231
Eric Laurent41709552019-12-16 19:34:05 -0800232status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700233{
234 status_t status = NO_ERROR;
235 bool doRegister = false;
236 bool registered = false;
237 bool doEnable = false;
238 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700239 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovf698ff22020-03-31 10:07:29 -0700240 uint32_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700241
242 {
243 Mutex::Autolock _l(mLock);
244 // register effect when first handle is attached and unregister when last handle is removed
245 if (mPolicyRegistered != mHandles.size() > 0) {
246 doRegister = true;
247 mPolicyRegistered = mHandles.size() > 0;
248 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800249 io = mCallback->io();
250 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700251 }
252 }
253 // enable effect when registered according to enable state requested by controlling handle
254 if (mHandles.size() > 0) {
255 EffectHandle *handle = controlHandle_l();
256 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
257 doEnable = true;
258 mPolicyEnabled = handle->enabled();
259 }
260 }
261 registered = mPolicyRegistered;
262 enabled = mPolicyEnabled;
263 mPolicyLock.lock();
264 }
265 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
266 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
267 if (doRegister) {
268 if (registered) {
269 status = AudioSystem::registerEffect(
270 &mDescriptor,
271 io,
272 strategy,
273 mSessionId,
274 mId);
275 } else {
276 status = AudioSystem::unregisterEffect(mId);
277 }
278 }
279 if (registered && doEnable) {
280 status = AudioSystem::setEffectEnabled(mId, enabled);
281 }
282 mPolicyLock.unlock();
283
284 return status;
285}
286
287
Eric Laurent41709552019-12-16 19:34:05 -0800288ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800289{
290 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800291 return removeHandle_l(handle);
292}
293
Eric Laurent41709552019-12-16 19:34:05 -0800294ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800295{
Eric Laurentca7cc822012-11-19 14:55:58 -0800296 size_t size = mHandles.size();
297 size_t i;
298 for (i = 0; i < size; i++) {
299 if (mHandles[i] == handle) {
300 break;
301 }
302 }
303 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800304 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
305 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800306 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800308
309 mHandles.removeAt(i);
310 // if removed from first place, move effect control from this handle to next in line
311 if (i == 0) {
312 EffectHandle *h = controlHandle_l();
313 if (h != NULL) {
314 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
315 }
316 }
317
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530318 // Prevent calls to process() and other functions on effect interface from now on.
319 // The effect engine will be released by the destructor when the last strong reference on
320 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800321 if (mHandles.size() == 0 && !mPinned) {
322 mState = DESTROYED;
323 }
324
325 return mHandles.size();
326}
327
328// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800329AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800330{
331 // the first valid handle in the list has control over the module
332 for (size_t i = 0; i < mHandles.size(); i++) {
333 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800334 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800335 return h;
336 }
337 }
338
339 return NULL;
340}
341
Eric Laurentf10c7092016-12-06 17:09:56 -0800342// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800343ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800344{
345 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800346 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
347 return mHandles.size();
348 }
349
Eric Laurentf10c7092016-12-06 17:09:56 -0800350 Mutex::Autolock _l(mLock);
351 ssize_t numHandles = removeHandle_l(handle);
352 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800353 mLock.unlock();
354 mCallback->updateOrphanEffectChains(this);
355 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800356 }
357 return numHandles;
358}
359
Eric Laurent41709552019-12-16 19:34:05 -0800360bool AudioFlinger::EffectBase::purgeHandles()
361{
362 bool enabled = false;
363 Mutex::Autolock _l(mLock);
364 EffectHandle *handle = controlHandle_l();
365 if (handle != NULL) {
366 enabled = handle->enabled();
367 }
368 mHandles.clear();
369 return enabled;
370}
371
372void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
373 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
374}
375
376static String8 effectFlagsToString(uint32_t flags) {
377 String8 s;
378
379 s.append("conn. mode: ");
380 switch (flags & EFFECT_FLAG_TYPE_MASK) {
381 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
382 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
383 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
384 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
385 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
386 default: s.append("unknown/reserved"); break;
387 }
388 s.append(", ");
389
390 s.append("insert pref: ");
391 switch (flags & EFFECT_FLAG_INSERT_MASK) {
392 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
393 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
394 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
395 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
396 default: s.append("unknown/reserved"); break;
397 }
398 s.append(", ");
399
400 s.append("volume mgmt: ");
401 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
402 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
403 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
404 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
405 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
406 default: s.append("unknown/reserved"); break;
407 }
408 s.append(", ");
409
410 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
411 if (devind) {
412 s.append("device indication: ");
413 switch (devind) {
414 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418 }
419
420 s.append("input mode: ");
421 switch (flags & EFFECT_FLAG_INPUT_MASK) {
422 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
423 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
424 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
425 default: s.append("not set"); break;
426 }
427 s.append(", ");
428
429 s.append("output mode: ");
430 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
431 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
432 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
433 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
434 default: s.append("not set"); break;
435 }
436 s.append(", ");
437
438 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
439 if (accel) {
440 s.append("hardware acceleration: ");
441 switch (accel) {
442 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
443 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
444 default: s.append("unknown/reserved"); break;
445 }
446 s.append(", ");
447 }
448
449 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
450 if (modeind) {
451 s.append("mode indication: ");
452 switch (modeind) {
453 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
454 default: s.append("unknown/reserved"); break;
455 }
456 s.append(", ");
457 }
458
459 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
460 if (srcind) {
461 s.append("source indication: ");
462 switch (srcind) {
463 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
464 default: s.append("unknown/reserved"); break;
465 }
466 s.append(", ");
467 }
468
469 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
470 s.append("offloadable, ");
471 }
472
473 int len = s.length();
474 if (s.length() > 2) {
475 (void) s.lockBuffer(len);
476 s.unlockBuffer(len - 2);
477 }
478 return s;
479}
480
481void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
482{
483 String8 result;
484
485 result.appendFormat("\tEffect ID %d:\n", mId);
486
487 bool locked = AudioFlinger::dumpTryLock(mLock);
488 // failed to lock - AudioFlinger is probably deadlocked
489 if (!locked) {
490 result.append("\t\tCould not lock Fx mutex:\n");
491 }
492
493 result.append("\t\tSession State Registered Enabled Suspended:\n");
494 result.appendFormat("\t\t%05d %03d %s %s %s\n",
495 mSessionId, mState, mPolicyRegistered ? "y" : "n",
496 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
497
498 result.append("\t\tDescriptor:\n");
499 char uuidStr[64];
500 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
501 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
502 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
503 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
504 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
505 mDescriptor.apiVersion,
506 mDescriptor.flags,
507 effectFlagsToString(mDescriptor.flags).string());
508 result.appendFormat("\t\t- name: %s\n",
509 mDescriptor.name);
510
511 result.appendFormat("\t\t- implementor: %s\n",
512 mDescriptor.implementor);
513
514 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
515 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
516 char buffer[256];
517 for (size_t i = 0; i < mHandles.size(); ++i) {
518 EffectHandle *handle = mHandles[i];
519 if (handle != NULL && !handle->disconnected()) {
520 handle->dumpToBuffer(buffer, sizeof(buffer));
521 result.append(buffer);
522 }
523 }
524 if (locked) {
525 mLock.unlock();
526 }
527
528 write(fd, result.string(), result.length());
529}
530
531// ----------------------------------------------------------------------------
532// EffectModule implementation
533// ----------------------------------------------------------------------------
534
535#undef LOG_TAG
536#define LOG_TAG "AudioFlinger::EffectModule"
537
538AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
539 effect_descriptor_t *desc,
540 int id,
541 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800542 bool pinned,
543 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800544 : EffectBase(callback, desc, id, sessionId, pinned),
545 // clear mConfig to ensure consistent initial value of buffer framecount
546 // in case buffers are associated by setInBuffer() or setOutBuffer()
547 // prior to configure().
548 mConfig{{}, {}},
549 mStatus(NO_INIT),
550 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
551 mDisableWaitCnt(0), // set by process() and updateState()
552 mOffloaded(false)
553#ifdef FLOAT_EFFECT_CHAIN
554 , mSupportsFloat(false)
555#endif
556{
557 ALOGV("Constructor %p pinned %d", this, pinned);
558 int lStatus;
559
560 // create effect engine from effect factory
561 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800562 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800563 if (mStatus != NO_ERROR) {
564 return;
565 }
566 lStatus = init();
567 if (lStatus < 0) {
568 mStatus = lStatus;
569 goto Error;
570 }
571
572 setOffloaded(callback->isOffload(), callback->io());
573 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
574
575 return;
576Error:
577 mEffectInterface.clear();
578 ALOGV("Constructor Error %d", mStatus);
579}
580
581AudioFlinger::EffectModule::~EffectModule()
582{
583 ALOGV("Destructor %p", this);
584 if (mEffectInterface != 0) {
585 char uuidStr[64];
586 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
587 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
588 this, uuidStr);
589 release_l();
590 }
591
592}
593
Eric Laurentfa1e1232016-08-02 19:01:49 -0700594bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800595 Mutex::Autolock _l(mLock);
596
Eric Laurentfa1e1232016-08-02 19:01:49 -0700597 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800598 switch (mState) {
599 case RESTART:
600 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700601 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800602
603 case STARTING:
604 // clear auxiliary effect input buffer for next accumulation
605 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
606 memset(mConfig.inputCfg.buffer.raw,
607 0,
608 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
609 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700610 if (start_l() == NO_ERROR) {
611 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700612 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700613 } else {
614 mState = IDLE;
615 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800616 break;
617 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900618 // volume control for offload and direct threads must take effect immediately.
619 if (stop_l() == NO_ERROR
620 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700621 mDisableWaitCnt = mMaxDisableWaitCnt;
622 } else {
623 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
624 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800625 mState = STOPPED;
626 break;
627 case STOPPED:
628 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
629 // turn off sequence.
630 if (--mDisableWaitCnt == 0) {
631 reset_l();
632 mState = IDLE;
633 }
634 break;
635 default: //IDLE , ACTIVE, DESTROYED
636 break;
637 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700638
639 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800640}
641
642void AudioFlinger::EffectModule::process()
643{
644 Mutex::Autolock _l(mLock);
645
Mikhail Naganov022b9952017-01-04 16:36:51 -0800646 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800647 return;
648 }
649
rago94a1ee82017-07-21 15:11:02 -0700650 const uint32_t inChannelCount =
651 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
652 const uint32_t outChannelCount =
653 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
654 const bool auxType =
655 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
656
Andy Hungfa69ca32017-11-30 10:07:53 -0800657 // safeInputOutputSampleCount is 0 if the channel count between input and output
658 // buffers do not match. This prevents automatic accumulation or copying between the
659 // input and output effect buffers without an intermediary effect process.
660 // TODO: consider implementing channel conversion.
661 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700662 mInChannelCountRequested != mOutChannelCountRequested ? 0
663 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800664 mConfig.inputCfg.buffer.frameCount,
665 mConfig.outputCfg.buffer.frameCount);
666 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
667#ifdef FLOAT_EFFECT_CHAIN
668 accumulate_float(
669 mConfig.outputCfg.buffer.f32,
670 mConfig.inputCfg.buffer.f32,
671 safeInputOutputSampleCount);
672#else
673 accumulate_i16(
674 mConfig.outputCfg.buffer.s16,
675 mConfig.inputCfg.buffer.s16,
676 safeInputOutputSampleCount);
677#endif
678 };
679 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
680#ifdef FLOAT_EFFECT_CHAIN
681 memcpy(
682 mConfig.outputCfg.buffer.f32,
683 mConfig.inputCfg.buffer.f32,
684 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
685
686#else
687 memcpy(
688 mConfig.outputCfg.buffer.s16,
689 mConfig.inputCfg.buffer.s16,
690 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
691#endif
692 };
693
Eric Laurentca7cc822012-11-19 14:55:58 -0800694 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700695 int ret;
696 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700697 if (auxType) {
698 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800699 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700700#ifdef FLOAT_EFFECT_CHAIN
701 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800702#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700703 // Do in-place float conversion for auxiliary effect input buffer.
704 static_assert(sizeof(float) <= sizeof(int32_t),
705 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
706
Andy Hungfa69ca32017-11-30 10:07:53 -0800707 memcpy_to_float_from_q4_27(
708 mConfig.inputCfg.buffer.f32,
709 mConfig.inputCfg.buffer.s32,
710 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800711#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800712 } else
Andy Hung116a4982017-11-30 10:15:08 -0800713#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800714 {
Andy Hung116a4982017-11-30 10:15:08 -0800715#ifdef FLOAT_AUX
716 memcpy_to_i16_from_float(
717 mConfig.inputCfg.buffer.s16,
718 mConfig.inputCfg.buffer.f32,
719 mConfig.inputCfg.buffer.frameCount);
720#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800721 memcpy_to_i16_from_q4_27(
722 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700723 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800724 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800725#endif
rago94a1ee82017-07-21 15:11:02 -0700726 }
rago94a1ee82017-07-21 15:11:02 -0700727 }
728#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800729 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
730 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
731
732 if (!auxType && mInChannelCountRequested != inChannelCount) {
733 adjust_channels(
734 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
735 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
736 sizeof(float),
737 sizeof(float)
738 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
739 inBuffer = mInConversionBuffer;
740 }
741 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
742 && mOutChannelCountRequested != outChannelCount) {
743 adjust_selected_channels(
744 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
745 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
746 sizeof(float),
747 sizeof(float)
748 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
749 outBuffer = mOutConversionBuffer;
750 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800751 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
752 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800753 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800754 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
755 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700756 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800757 memcpy_to_i16_from_float(
758 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800759 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800760 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800761 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700762 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800763 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800764 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800765 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
766 goto data_bypass;
767 }
768 memcpy_to_i16_from_float(
769 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800770 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800771 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800772 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700773 }
774 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800775#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800776 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800777#ifdef FLOAT_EFFECT_CHAIN
778 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800779 sp<EffectBufferHalInterface> target =
780 mOutChannelCountRequested != outChannelCount
781 ? mOutConversionBuffer : mOutBuffer;
782
Andy Hungfa69ca32017-11-30 10:07:53 -0800783 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800784 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800785 mOutConversionBuffer->audioBuffer()->s16,
786 outChannelCount * mConfig.outputCfg.buffer.frameCount);
787 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800788 if (mOutChannelCountRequested != outChannelCount) {
789 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
790 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
791 sizeof(float),
792 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
793 }
rago94a1ee82017-07-21 15:11:02 -0700794#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700795 } else {
rago94a1ee82017-07-21 15:11:02 -0700796#ifdef FLOAT_EFFECT_CHAIN
797 data_bypass:
798#endif
799 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800800 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700801 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800802 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700803 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800804 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700805 }
806 }
807 ret = -ENODATA;
808 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800809
Eric Laurentca7cc822012-11-19 14:55:58 -0800810 // force transition to IDLE state when engine is ready
811 if (mState == STOPPED && ret == -ENODATA) {
812 mDisableWaitCnt = 1;
813 }
814
815 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700816 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800817#ifdef FLOAT_AUX
818 const size_t size =
819 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
820#else
rago94a1ee82017-07-21 15:11:02 -0700821 const size_t size =
822 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800823#endif
rago94a1ee82017-07-21 15:11:02 -0700824 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800825 }
826 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700827 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800828 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
829 // If an insert effect is idle and input buffer is different from output buffer,
830 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800831 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700832 // similar handling with data_bypass above.
833 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
834 accumulateInputToOutput();
835 } else { // EFFECT_BUFFER_ACCESS_WRITE
836 copyInputToOutput();
837 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 }
839 }
840}
841
842void AudioFlinger::EffectModule::reset_l()
843{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700844 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800845 return;
846 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700847 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800848}
849
850status_t AudioFlinger::EffectModule::configure()
851{
rago94a1ee82017-07-21 15:11:02 -0700852 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700853 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700854 uint32_t size;
855 audio_channel_mask_t channelMask;
856
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700857 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700858 status = NO_INIT;
859 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800860 }
861
Eric Laurentca7cc822012-11-19 14:55:58 -0800862 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800863 // TODO: handle configuration of input (record) SW effects above the HAL,
864 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
865 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800866 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800867 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700868 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800869
870 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800871 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
872 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
873 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
874 mConfig.inputCfg.channels);
875 }
876#ifndef MULTICHANNEL_EFFECT_CHAIN
877 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
878 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
879 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
880 mConfig.outputCfg.channels);
881 }
882#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800883 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800884#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700885 // TODO: Update this logic when multichannel effects are implemented.
886 // For offloaded tracks consider mono output as stereo for proper effect initialization
887 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
888 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
889 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
890 ALOGV("Overriding effect input and output as STEREO");
891 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800892#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800893 }
jiabineb3bda02020-06-30 14:07:03 -0700894 if (isHapticGenerator()) {
895 audio_channel_mask_t hapticChannelMask = mCallback->hapticChannelMask();
896 mConfig.inputCfg.channels |= hapticChannelMask;
897 mConfig.outputCfg.channels |= hapticChannelMask;
898 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800899 mInChannelCountRequested =
900 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
901 mOutChannelCountRequested =
902 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700903
rago94a1ee82017-07-21 15:11:02 -0700904 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
905 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900906
907 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100908 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900909 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
910 ALOGV("Overriding effect input as 48kHz");
911 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800912 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900913 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800914 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
915 mConfig.inputCfg.bufferProvider.cookie = NULL;
916 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
917 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
918 mConfig.outputCfg.bufferProvider.cookie = NULL;
919 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
920 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
921 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
922 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800923 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800924 // always overwrites output buffer: input buffer == output buffer
925 // - in other sessions:
926 // last effect in the chain accumulates in output buffer: input buffer != output buffer
927 // other effect: overwrites output buffer: input buffer == output buffer
928 // Auxiliary effect:
929 // accumulates in output buffer: input buffer != output buffer
930 // Therefore: accumulate <=> input buffer != output buffer
931 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
932 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
933 } else {
934 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
935 }
936 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
937 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800938 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800939 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
940
Eric Laurent6b446ce2019-12-13 10:56:31 -0800941 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800942 this, mCallback->chain().promote().get(),
943 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800944
945 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700946 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700947 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800948 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700949 &mConfig,
950 &size,
951 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700952 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800953 status = cmdStatus;
954 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800955
956#ifdef MULTICHANNEL_EFFECT_CHAIN
957 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800958 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800959 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
960 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
961 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700962 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
963 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800964 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
965 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
966 }
967 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
968 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
969 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
970 }
971 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700972 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800973 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700974 &mConfig,
975 &size,
976 &cmdStatus);
977 if (status == NO_ERROR) {
978 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800979 }
980 }
981#endif
982
983#ifdef FLOAT_EFFECT_CHAIN
984 if (status == NO_ERROR) {
985 mSupportsFloat = true;
986 }
987
988 if (status != NO_ERROR) {
989 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
990 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
991 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
992 size = sizeof(int);
993 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
994 sizeof(mConfig),
995 &mConfig,
996 &size,
997 &cmdStatus);
998 if (status == NO_ERROR) {
999 status = cmdStatus;
1000 }
1001 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001002 mSupportsFloat = false;
1003 ALOGVV("config worked with 16 bit");
1004 } else {
1005 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001006 }
rago94a1ee82017-07-21 15:11:02 -07001007 }
1008#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001009
rago94a1ee82017-07-21 15:11:02 -07001010 if (status == NO_ERROR) {
1011 // Establish Buffer strategy
1012 setInBuffer(mInBuffer);
1013 setOutBuffer(mOutBuffer);
1014
1015 // Update visualizer latency
1016 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1017 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1018 effect_param_t *p = (effect_param_t *)buf32;
1019
1020 p->psize = sizeof(uint32_t);
1021 p->vsize = sizeof(uint32_t);
1022 size = sizeof(int);
1023 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1024
Eric Laurent6b446ce2019-12-13 10:56:31 -08001025 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001026
1027 *((int32_t *)p->data + 1)= latency;
1028 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1029 sizeof(effect_param_t) + 8,
1030 &buf32,
1031 &size,
1032 &cmdStatus);
1033 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001034 }
1035
Andy Hung05083ac2017-12-14 15:00:28 -08001036 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1037 mMaxDisableWaitCnt = (uint32_t)std::max(
1038 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1039 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1040 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001041
Eric Laurentd0ebb532013-04-02 16:41:41 -07001042exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001043 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001044 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001045 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 return status;
1047}
1048
1049status_t AudioFlinger::EffectModule::init()
1050{
1051 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001052 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001053 return NO_INIT;
1054 }
1055 status_t cmdStatus;
1056 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001057 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1058 0,
1059 NULL,
1060 &size,
1061 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001062 if (status == 0) {
1063 status = cmdStatus;
1064 }
1065 return status;
1066}
1067
Eric Laurent1b928682014-10-02 19:41:47 -07001068void AudioFlinger::EffectModule::addEffectToHal_l()
1069{
1070 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1071 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001072 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001073 }
1074}
1075
Eric Laurentfa1e1232016-08-02 19:01:49 -07001076// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001077status_t AudioFlinger::EffectModule::start()
1078{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001079 status_t status;
1080 {
1081 Mutex::Autolock _l(mLock);
1082 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001083 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001084 if (status == NO_ERROR) {
1085 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001086 }
1087 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001088}
1089
1090status_t AudioFlinger::EffectModule::start_l()
1091{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001092 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 return NO_INIT;
1094 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001095 if (mStatus != NO_ERROR) {
1096 return mStatus;
1097 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001098 status_t cmdStatus;
1099 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001100 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1101 0,
1102 NULL,
1103 &size,
1104 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001105 if (status == 0) {
1106 status = cmdStatus;
1107 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001108 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001109 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001110 }
1111 return status;
1112}
1113
1114status_t AudioFlinger::EffectModule::stop()
1115{
1116 Mutex::Autolock _l(mLock);
1117 return stop_l();
1118}
1119
1120status_t AudioFlinger::EffectModule::stop_l()
1121{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001122 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001123 return NO_INIT;
1124 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001125 if (mStatus != NO_ERROR) {
1126 return mStatus;
1127 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001128 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001129 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001130
1131 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001132 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1133 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1134 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001135 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001136 mSetVolumeReentrantTid = INVALID_PID;
1137 }
1138
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001139 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1140 0,
1141 NULL,
1142 &size,
1143 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001144 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001145 status = cmdStatus;
1146 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001147 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001148 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001149 }
1150 return status;
1151}
1152
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001153// must be called with EffectChain::mLock held
1154void AudioFlinger::EffectModule::release_l()
1155{
1156 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001157 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001158 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001159 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001160 mEffectInterface.clear();
1161 }
1162}
1163
Eric Laurent6b446ce2019-12-13 10:56:31 -08001164status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001165{
1166 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1167 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001168 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001169 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001170 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001171}
1172
Andy Hunge4a1d912016-08-17 14:11:13 -07001173// round up delta valid if value and divisor are positive.
1174template <typename T>
1175static T roundUpDelta(const T &value, const T &divisor) {
1176 T remainder = value % divisor;
1177 return remainder == 0 ? 0 : divisor - remainder;
1178}
1179
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001180status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1181 const std::vector<uint8_t>& cmdData,
1182 int32_t maxReplySize,
1183 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001184{
1185 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001186 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001187
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001188 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001189 return NO_INIT;
1190 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001191 if (mStatus != NO_ERROR) {
1192 return mStatus;
1193 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001194 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1195 return -EINVAL;
1196 }
1197 size_t cmdSize = cmdData.size();
1198 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1199 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1200 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001201 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001202 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001203 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001204 android_errorWriteLog(0x534e4554, "33003822");
1205 return -EINVAL;
1206 }
1207 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001208 (maxReplySize < sizeof(effect_param_t) ||
1209 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001210 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001211 return -EINVAL;
1212 }
ragoe2759072016-11-22 18:02:48 -08001213 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001214 (sizeof(effect_param_t) > maxReplySize
1215 || param->psize > maxReplySize - sizeof(effect_param_t)
1216 || param->vsize > maxReplySize - sizeof(effect_param_t)
1217 - param->psize
1218 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1219 maxReplySize
1220 - sizeof(effect_param_t)
1221 - param->psize
1222 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001223 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1224 android_errorWriteLog(0x534e4554, "32705438");
1225 return -EINVAL;
1226 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001227 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001228 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1229 && // DEFERRED not generally used
1230 (param == nullptr
1231 || param->psize > cmdSize - sizeof(effect_param_t)
1232 || param->vsize > cmdSize - sizeof(effect_param_t)
1233 - param->psize
1234 || roundUpDelta(param->psize,
1235 (uint32_t) sizeof(int)) >
1236 cmdSize
1237 - sizeof(effect_param_t)
1238 - param->psize
1239 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001240 android_errorWriteLog(0x534e4554, "30204301");
1241 return -EINVAL;
1242 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001243 uint32_t replySize = maxReplySize;
1244 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001245 status_t status = mEffectInterface->command(cmdCode,
1246 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001247 const_cast<uint8_t*>(cmdData.data()),
1248 &replySize,
1249 reply->data());
1250 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001251 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001252 for (size_t i = 1; i < mHandles.size(); i++) {
1253 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001254 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001255 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001256 }
1257 }
1258 }
1259 return status;
1260}
1261
Eric Laurentca7cc822012-11-19 14:55:58 -08001262bool AudioFlinger::EffectModule::isProcessEnabled() const
1263{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001264 if (mStatus != NO_ERROR) {
1265 return false;
1266 }
1267
Eric Laurentca7cc822012-11-19 14:55:58 -08001268 switch (mState) {
1269 case RESTART:
1270 case ACTIVE:
1271 case STOPPING:
1272 case STOPPED:
1273 return true;
1274 case IDLE:
1275 case STARTING:
1276 case DESTROYED:
1277 default:
1278 return false;
1279 }
1280}
1281
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001282bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1283{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001284 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001285}
1286
1287bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1288{
1289 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1290}
1291
Mikhail Naganov022b9952017-01-04 16:36:51 -08001292void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001293 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001294
1295 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001296 if (buffer != 0) {
1297 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1298 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1299 } else {
1300 mConfig.inputCfg.buffer.raw = NULL;
1301 }
1302 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001303 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001304
1305#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001306 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001307 // Theoretically insert effects can also do in-place conversions (destroying
1308 // the original buffer) when the output buffer is identical to the input buffer,
1309 // but we don't optimize for it here.
1310 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001311 const uint32_t inChannelCount =
1312 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1313 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001314 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001315 // we need to translate - create hidl shared buffer and intercept
1316 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001317 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1318 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1319 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001320
1321 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1322 __func__, inChannels, inFrameCount, size);
1323
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001324 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001325 || size > mInConversionBuffer->getSize())) {
1326 mInConversionBuffer.clear();
1327 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001328 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001329 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001330 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001331 mInConversionBuffer->setFrameCount(inFrameCount);
1332 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001333 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001334 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001335 }
1336 }
1337#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001338}
1339
1340void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001341 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001342
1343 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001344 if (buffer != 0) {
1345 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1346 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1347 } else {
1348 mConfig.outputCfg.buffer.raw = NULL;
1349 }
1350 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001351 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001352
1353#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001354 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001355 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001356 const uint32_t outChannelCount =
1357 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1358 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001359 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001360 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001361 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1362 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1363 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001364
1365 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1366 __func__, outChannels, outFrameCount, size);
1367
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001368 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001369 || size > mOutConversionBuffer->getSize())) {
1370 mOutConversionBuffer.clear();
1371 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001372 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001373 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001374 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001375 mOutConversionBuffer->setFrameCount(outFrameCount);
1376 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001377 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001378 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001379 }
1380 }
1381#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001382}
1383
Eric Laurentca7cc822012-11-19 14:55:58 -08001384status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1385{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001386 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001387 if (mStatus != NO_ERROR) {
1388 return mStatus;
1389 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001390 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1392 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1393 if (isProcessEnabled() &&
1394 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001395 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1396 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001397 uint32_t volume[2];
1398 uint32_t *pVolume = NULL;
1399 uint32_t size = sizeof(volume);
1400 volume[0] = *left;
1401 volume[1] = *right;
1402 if (controller) {
1403 pVolume = volume;
1404 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001405 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1406 size,
1407 volume,
1408 &size,
1409 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001410 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1411 *left = volume[0];
1412 *right = volume[1];
1413 }
1414 }
1415 return status;
1416}
1417
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001418void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1419{
Zhou Songd505c642020-02-20 16:35:37 +08001420 // for offload or direct thread, if the effect chain has non-offloadable
1421 // effect and any effect module within the chain has volume control, then
1422 // volume control is delegated to effect, otherwise, set volume to hal.
1423 if (mEffectCallback->isOffloadOrDirect() &&
1424 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001425 float vol_l = (float)left / (1 << 24);
1426 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001427 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001428 }
1429}
1430
jiabin8f278ee2019-11-11 12:16:27 -08001431status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1432 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001433{
jiabin8f278ee2019-11-11 12:16:27 -08001434 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1435 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001436 return NO_ERROR;
1437 }
1438
1439 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001440 if (mStatus != NO_ERROR) {
1441 return mStatus;
1442 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001443 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001444 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001445 status_t cmdStatus;
1446 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001447 // FIXME: use audio device types and addresses when the hal interface is ready.
1448 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001449 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001450 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001451 &size,
1452 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001453 }
1454 return status;
1455}
1456
jiabin8f278ee2019-11-11 12:16:27 -08001457status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1458{
1459 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1460}
1461
1462status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1463{
1464 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1465}
1466
Eric Laurentca7cc822012-11-19 14:55:58 -08001467status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1468{
1469 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001470 if (mStatus != NO_ERROR) {
1471 return mStatus;
1472 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001473 status_t status = NO_ERROR;
1474 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1475 status_t cmdStatus;
1476 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001477 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1478 sizeof(audio_mode_t),
1479 &mode,
1480 &size,
1481 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001482 if (status == NO_ERROR) {
1483 status = cmdStatus;
1484 }
1485 }
1486 return status;
1487}
1488
1489status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1490{
1491 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001492 if (mStatus != NO_ERROR) {
1493 return mStatus;
1494 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001495 status_t status = NO_ERROR;
1496 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1497 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001498 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1499 sizeof(audio_source_t),
1500 &source,
1501 &size,
1502 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001503 }
1504 return status;
1505}
1506
Eric Laurent5baf2af2013-09-12 17:37:00 -07001507status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1508{
1509 Mutex::Autolock _l(mLock);
1510 if (mStatus != NO_ERROR) {
1511 return mStatus;
1512 }
1513 status_t status = NO_ERROR;
1514 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1515 status_t cmdStatus;
1516 uint32_t size = sizeof(status_t);
1517 effect_offload_param_t cmd;
1518
1519 cmd.isOffload = offloaded;
1520 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001521 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1522 sizeof(effect_offload_param_t),
1523 &cmd,
1524 &size,
1525 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001526 if (status == NO_ERROR) {
1527 status = cmdStatus;
1528 }
1529 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1530 } else {
1531 if (offloaded) {
1532 status = INVALID_OPERATION;
1533 }
1534 mOffloaded = false;
1535 }
1536 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1537 return status;
1538}
1539
1540bool AudioFlinger::EffectModule::isOffloaded() const
1541{
1542 Mutex::Autolock _l(mLock);
1543 return mOffloaded;
1544}
1545
jiabineb3bda02020-06-30 14:07:03 -07001546/*static*/
1547bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1548 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1549}
1550
1551bool AudioFlinger::EffectModule::isHapticGenerator() const {
1552 return isHapticGenerator(&mDescriptor.type);
1553}
1554
jiabine70bc7f2020-06-30 22:07:55 -07001555status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1556{
1557 if (mStatus != NO_ERROR) {
1558 return mStatus;
1559 }
1560 if (!isHapticGenerator()) {
1561 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1562 return INVALID_OPERATION;
1563 }
1564
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001565 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1566 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001567 param->psize = sizeof(int32_t);
1568 param->vsize = sizeof(int32_t) * 2;
1569 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1570 *((int32_t*)param->data + 1) = id;
1571 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001572 std::vector<uint8_t> response;
1573 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001574 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001575 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1576 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001577 }
1578 return status;
1579}
1580
Andy Hungbded9c82017-11-30 18:47:35 -08001581static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1582 std::stringstream ss;
1583
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001584 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001585 return "nullptr"; // make different than below
1586 } else if (buffer->externalData() != nullptr) {
1587 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1588 << " -> "
1589 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1590 } else {
1591 ss << buffer->audioBuffer()->raw;
1592 }
1593 return ss.str();
1594}
Marco Nelissenb2208842014-02-07 14:00:50 -08001595
Eric Laurent41709552019-12-16 19:34:05 -08001596void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001597{
Eric Laurent41709552019-12-16 19:34:05 -08001598 EffectBase::dump(fd, args);
1599
Eric Laurentca7cc822012-11-19 14:55:58 -08001600 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001601 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001602
Eric Laurent41709552019-12-16 19:34:05 -08001603 result.append("\t\tStatus Engine:\n");
1604 result.appendFormat("\t\t%03d %p\n",
1605 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001606
1607 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001608
1609 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001610 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1611 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1612 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001613 mConfig.inputCfg.buffer.frameCount,
1614 mConfig.inputCfg.samplingRate,
1615 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001616 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001617 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001618
1619 result.append("\t\t- Output configuration:\n");
1620 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001621 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001622 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001623 mConfig.outputCfg.buffer.frameCount,
1624 mConfig.outputCfg.samplingRate,
1625 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001626 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001627 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001628
rago94a1ee82017-07-21 15:11:02 -07001629#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001630
Andy Hungbded9c82017-11-30 18:47:35 -08001631 result.appendFormat("\t\t- HAL buffers:\n"
1632 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1633 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1634 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1635 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1636 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001637#endif
1638
Eric Laurentca7cc822012-11-19 14:55:58 -08001639 write(fd, result.string(), result.length());
1640
Mikhail Naganov4d547672019-02-22 14:19:19 -08001641 if (mEffectInterface != 0) {
1642 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1643 (void)mEffectInterface->dump(fd);
1644 }
1645
Eric Laurentca7cc822012-11-19 14:55:58 -08001646 if (locked) {
1647 mLock.unlock();
1648 }
1649}
1650
1651// ----------------------------------------------------------------------------
1652// EffectHandle implementation
1653// ----------------------------------------------------------------------------
1654
1655#undef LOG_TAG
1656#define LOG_TAG "AudioFlinger::EffectHandle"
1657
Eric Laurent41709552019-12-16 19:34:05 -08001658AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001659 const sp<AudioFlinger::Client>& client,
1660 const sp<media::IEffectClient>& effectClient,
1661 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001662 : BnEffect(),
1663 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001664 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001665{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001666 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001667
1668 if (client == 0) {
1669 return;
1670 }
1671 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1672 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001673 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001674 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001675 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001676 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001677 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001678 return;
1679 }
Glenn Kastene75da402013-11-20 13:54:52 -08001680 new(mCblk) effect_param_cblk_t();
1681 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001682}
1683
1684AudioFlinger::EffectHandle::~EffectHandle()
1685{
1686 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001687 disconnect(false);
1688}
1689
Glenn Kastene75da402013-11-20 13:54:52 -08001690status_t AudioFlinger::EffectHandle::initCheck()
1691{
1692 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1693}
1694
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001695#define RETURN(code) \
1696 *_aidl_return = (code); \
1697 return Status::ok();
1698
1699Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001700{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001701 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001702 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001703 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001704 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001705 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001706 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001707 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001708 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001709 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001710
1711 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001712 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 }
1714
1715 mEnabled = true;
1716
Eric Laurent6c796322019-04-09 14:13:17 -07001717 status_t status = effect->updatePolicyState();
1718 if (status != NO_ERROR) {
1719 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001720 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001721 }
1722
Eric Laurent6b446ce2019-12-13 10:56:31 -08001723 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001724
1725 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001726 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001727 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 }
1729
Eric Laurent6b446ce2019-12-13 10:56:31 -08001730 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001731 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001732 mEnabled = false;
1733 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001734 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001735}
1736
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001737Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001738{
1739 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001740 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001741 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001742 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001743 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001744 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001745 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001746 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001747 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001748
1749 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001750 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001751 }
1752 mEnabled = false;
1753
Eric Laurent6c796322019-04-09 14:13:17 -07001754 effect->updatePolicyState();
1755
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001756 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001757 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 }
1759
Eric Laurent6b446ce2019-12-13 10:56:31 -08001760 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001761 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001762}
1763
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001764Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001765{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001766 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001767 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001768 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001769}
1770
1771void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1772{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001773 AutoMutex _l(mLock);
1774 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1775 if (mDisconnected) {
1776 if (unpinIfLast) {
1777 android_errorWriteLog(0x534e4554, "32707507");
1778 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001779 return;
1780 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001781 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001782 {
Eric Laurent41709552019-12-16 19:34:05 -08001783 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001784 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001785 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001786 ALOGW("%s Effect handle %p disconnected after thread destruction",
1787 __func__, this);
1788 }
1789 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001790 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001791 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001792
Eric Laurentca7cc822012-11-19 14:55:58 -08001793 if (mClient != 0) {
1794 if (mCblk != NULL) {
1795 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1796 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1797 }
1798 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001799 // Client destructor must run with AudioFlinger client mutex locked
1800 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001801 mClient.clear();
1802 }
1803}
1804
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001805Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1806 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1807 return Status::ok();
1808}
1809
1810Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1811 const std::vector<uint8_t>& cmdData,
1812 int32_t maxResponseSize,
1813 std::vector<uint8_t>* response,
1814 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001815{
1816 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001817 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001818
Eric Laurentc7ab3092017-06-15 18:43:46 -07001819 // reject commands reserved for internal use by audio framework if coming from outside
1820 // of audioserver
1821 switch(cmdCode) {
1822 case EFFECT_CMD_ENABLE:
1823 case EFFECT_CMD_DISABLE:
1824 case EFFECT_CMD_SET_PARAM:
1825 case EFFECT_CMD_SET_PARAM_DEFERRED:
1826 case EFFECT_CMD_SET_PARAM_COMMIT:
1827 case EFFECT_CMD_GET_PARAM:
1828 break;
1829 default:
1830 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1831 break;
1832 }
1833 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001834 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001835 }
1836
Eric Laurent1ffc5852016-12-15 14:46:09 -08001837 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001838 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001839 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001840 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001841 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001842 writeToBuffer(NO_ERROR, response);
1843 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001844 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001845 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001846 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001847 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001848 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 writeToBuffer(NO_ERROR, response);
1850 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001851 }
1852
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001853 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001854 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001855 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001857 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001858 // only get parameter command is permitted for applications not controlling the effect
1859 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001860 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001861 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001862
1863 // handle commands that are not forwarded transparently to effect engine
1864 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001865 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001866 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001867 }
1868
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001869 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001870 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001872 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001873 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001874
Eric Laurentca7cc822012-11-19 14:55:58 -08001875 // No need to trylock() here as this function is executed in the binder thread serving a
1876 // particular client process: no risk to block the whole media server process or mixer
1877 // threads if we are stuck here
1878 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001879 // keep local copy of index in case of client corruption b/32220769
1880 const uint32_t clientIndex = mCblk->clientIndex;
1881 const uint32_t serverIndex = mCblk->serverIndex;
1882 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1883 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001884 mCblk->serverIndex = 0;
1885 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001886 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001887 }
1888 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001889 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001890 for (uint32_t index = serverIndex; index < clientIndex;) {
1891 int *p = (int *)(mBuffer + index);
1892 const int size = *p++;
1893 if (size < 0
1894 || size > EFFECT_PARAM_BUFFER_SIZE
1895 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001896 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001897 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001898 break;
1899 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001900
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001901 std::copy(reinterpret_cast<const uint8_t*>(p),
1902 reinterpret_cast<const uint8_t*>(p) + size,
1903 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001904
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001905 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001906 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001907 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001908 sizeof(int),
1909 &replyBuffer);
1910 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001911
1912 // verify shared memory: server index shouldn't change; client index can't go back.
1913 if (serverIndex != mCblk->serverIndex
1914 || clientIndex > mCblk->clientIndex) {
1915 android_errorWriteLog(0x534e4554, "32220769");
1916 status = BAD_VALUE;
1917 break;
1918 }
1919
Eric Laurentca7cc822012-11-19 14:55:58 -08001920 // stop at first error encountered
1921 if (ret != NO_ERROR) {
1922 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001923 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 break;
1925 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001926 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 break;
1928 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001929 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001930 }
1931 mCblk->serverIndex = 0;
1932 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001933 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 }
1935
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001936 status_t status = effect->command(cmdCode,
1937 cmdData,
1938 maxResponseSize,
1939 response);
1940 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001941}
1942
1943void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1944{
1945 ALOGV("setControl %p control %d", this, hasControl);
1946
1947 mHasControl = hasControl;
1948 mEnabled = enabled;
1949
1950 if (signal && mEffectClient != 0) {
1951 mEffectClient->controlStatusChanged(hasControl);
1952 }
1953}
1954
1955void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001956 const std::vector<uint8_t>& cmdData,
1957 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001958{
1959 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001960 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001961 }
1962}
1963
1964
1965
1966void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1967{
1968 if (mEffectClient != 0) {
1969 mEffectClient->enableStatusChanged(enabled);
1970 }
1971}
1972
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001973void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001974{
1975 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1976
Marco Nelissenb2208842014-02-07 14:00:50 -08001977 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001978 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001979 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001980 mHasControl ? "yes" : "no",
1981 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001982 mCblk ? mCblk->clientIndex : 0,
1983 mCblk ? mCblk->serverIndex : 0
1984 );
1985
1986 if (locked) {
1987 mCblk->lock.unlock();
1988 }
1989}
1990
1991#undef LOG_TAG
1992#define LOG_TAG "AudioFlinger::EffectChain"
1993
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001994AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1995 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001996 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001997 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001998 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001999 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002000{
2001 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002002 sp<ThreadBase> p = thread.promote();
2003 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002004 return;
2005 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002006 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2007 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002008}
2009
2010AudioFlinger::EffectChain::~EffectChain()
2011{
Eric Laurentca7cc822012-11-19 14:55:58 -08002012}
2013
2014// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2015sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2016 effect_descriptor_t *descriptor)
2017{
2018 size_t size = mEffects.size();
2019
2020 for (size_t i = 0; i < size; i++) {
2021 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2022 return mEffects[i];
2023 }
2024 }
2025 return 0;
2026}
2027
2028// getEffectFromId_l() must be called with ThreadBase::mLock held
2029sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2030{
2031 size_t size = mEffects.size();
2032
2033 for (size_t i = 0; i < size; i++) {
2034 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2035 if (id == 0 || mEffects[i]->id() == id) {
2036 return mEffects[i];
2037 }
2038 }
2039 return 0;
2040}
2041
2042// getEffectFromType_l() must be called with ThreadBase::mLock held
2043sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2044 const effect_uuid_t *type)
2045{
2046 size_t size = mEffects.size();
2047
2048 for (size_t i = 0; i < size; i++) {
2049 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2050 return mEffects[i];
2051 }
2052 }
2053 return 0;
2054}
2055
Eric Laurent6c796322019-04-09 14:13:17 -07002056std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2057{
2058 std::vector<int> ids;
2059 Mutex::Autolock _l(mLock);
2060 for (size_t i = 0; i < mEffects.size(); i++) {
2061 ids.push_back(mEffects[i]->id());
2062 }
2063 return ids;
2064}
2065
Eric Laurentca7cc822012-11-19 14:55:58 -08002066void AudioFlinger::EffectChain::clearInputBuffer()
2067{
2068 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002069 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002070}
2071
2072// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002073void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002074{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002075 if (mInBuffer == NULL) {
2076 return;
2077 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002078 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002079 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002080
Eric Laurent6b446ce2019-12-13 10:56:31 -08002081 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002082 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002083}
2084
2085// Must be called with EffectChain::mLock locked
2086void AudioFlinger::EffectChain::process_l()
2087{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002088 // never process effects when:
2089 // - on an OFFLOAD thread
2090 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002091 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002092 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002093 bool tracksOnSession = (trackCnt() != 0);
2094
2095 if (!tracksOnSession && mTailBufferCount == 0) {
2096 doProcess = false;
2097 }
2098
2099 if (activeTrackCnt() == 0) {
2100 // if no track is active and the effect tail has not been rendered,
2101 // the input buffer must be cleared here as the mixer process will not do it
2102 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002103 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 if (mTailBufferCount > 0) {
2105 mTailBufferCount--;
2106 }
2107 }
2108 }
2109 }
2110
2111 size_t size = mEffects.size();
2112 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002113 // Only the input and output buffers of the chain can be external,
2114 // and 'update' / 'commit' do nothing for allocated buffers, thus
2115 // it's not needed to consider any other buffers here.
2116 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002117 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2118 mOutBuffer->update();
2119 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002120 for (size_t i = 0; i < size; i++) {
2121 mEffects[i]->process();
2122 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002123 mInBuffer->commit();
2124 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2125 mOutBuffer->commit();
2126 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002127 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002128 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002129 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002130 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2131 }
2132 if (doResetVolume) {
2133 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002134 }
2135}
2136
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002137// createEffect_l() must be called with ThreadBase::mLock held
2138status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002139 effect_descriptor_t *desc,
2140 int id,
2141 audio_session_t sessionId,
2142 bool pinned)
2143{
2144 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002145 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002146 status_t lStatus = effect->status();
2147 if (lStatus == NO_ERROR) {
2148 lStatus = addEffect_ll(effect);
2149 }
2150 if (lStatus != NO_ERROR) {
2151 effect.clear();
2152 }
2153 return lStatus;
2154}
2155
2156// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002157status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2158{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002159 Mutex::Autolock _l(mLock);
2160 return addEffect_ll(effect);
2161}
2162// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2163status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2164{
Eric Laurentca7cc822012-11-19 14:55:58 -08002165 effect_descriptor_t desc = effect->desc();
2166 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2167
Eric Laurent6b446ce2019-12-13 10:56:31 -08002168 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002169
2170 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2171 // Auxiliary effects are inserted at the beginning of mEffects vector as
2172 // they are processed first and accumulated in chain input buffer
2173 mEffects.insertAt(effect, 0);
2174
2175 // the input buffer for auxiliary effect contains mono samples in
2176 // 32 bit format. This is to avoid saturation in AudoMixer
2177 // accumulation stage. Saturation is done in EffectModule::process() before
2178 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002179 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002180 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002181#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002182 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002183 numSamples * sizeof(float), &halBuffer);
2184#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002185 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002186 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002187#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002188 if (result != OK) return result;
2189 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002190 // auxiliary effects output samples to chain input buffer for further processing
2191 // by insert effects
2192 effect->setOutBuffer(mInBuffer);
2193 } else {
2194 // Insert effects are inserted at the end of mEffects vector as they are processed
2195 // after track and auxiliary effects.
2196 // Insert effect order as a function of indicated preference:
2197 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2198 // another effect is present
2199 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2200 // last effect claiming first position
2201 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2202 // first effect claiming last position
2203 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2204 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2205 // already present
2206
2207 size_t size = mEffects.size();
2208 size_t idx_insert = size;
2209 ssize_t idx_insert_first = -1;
2210 ssize_t idx_insert_last = -1;
2211
2212 for (size_t i = 0; i < size; i++) {
2213 effect_descriptor_t d = mEffects[i]->desc();
2214 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2215 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2216 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2217 // check invalid effect chaining combinations
2218 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2219 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2220 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2221 desc.name, d.name);
2222 return INVALID_OPERATION;
2223 }
2224 // remember position of first insert effect and by default
2225 // select this as insert position for new effect
2226 if (idx_insert == size) {
2227 idx_insert = i;
2228 }
2229 // remember position of last insert effect claiming
2230 // first position
2231 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2232 idx_insert_first = i;
2233 }
2234 // remember position of first insert effect claiming
2235 // last position
2236 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2237 idx_insert_last == -1) {
2238 idx_insert_last = i;
2239 }
2240 }
2241 }
2242
2243 // modify idx_insert from first position if needed
2244 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2245 if (idx_insert_last != -1) {
2246 idx_insert = idx_insert_last;
2247 } else {
2248 idx_insert = size;
2249 }
2250 } else {
2251 if (idx_insert_first != -1) {
2252 idx_insert = idx_insert_first + 1;
2253 }
2254 }
2255
2256 // always read samples from chain input buffer
2257 effect->setInBuffer(mInBuffer);
2258
2259 // if last effect in the chain, output samples to chain
2260 // output buffer, otherwise to chain input buffer
2261 if (idx_insert == size) {
2262 if (idx_insert != 0) {
2263 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2264 mEffects[idx_insert-1]->configure();
2265 }
2266 effect->setOutBuffer(mOutBuffer);
2267 } else {
2268 effect->setOutBuffer(mInBuffer);
2269 }
2270 mEffects.insertAt(effect, idx_insert);
2271
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002272 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002273 idx_insert);
2274 }
2275 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002276
Eric Laurentca7cc822012-11-19 14:55:58 -08002277 return NO_ERROR;
2278}
2279
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002280// removeEffect_l() must be called with ThreadBase::mLock held
2281size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2282 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002283{
2284 Mutex::Autolock _l(mLock);
2285 size_t size = mEffects.size();
2286 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2287
2288 for (size_t i = 0; i < size; i++) {
2289 if (effect == mEffects[i]) {
2290 // calling stop here will remove pre-processing effect from the audio HAL.
2291 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2292 // the middle of a read from audio HAL
2293 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2294 mEffects[i]->state() == EffectModule::STOPPING) {
2295 mEffects[i]->stop();
2296 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002297 if (release) {
2298 mEffects[i]->release_l();
2299 }
2300
Mikhail Naganov022b9952017-01-04 16:36:51 -08002301 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002302 if (i == size - 1 && i != 0) {
2303 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2304 mEffects[i - 1]->configure();
2305 }
2306 }
2307 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002308 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002309 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002310
Eric Laurentca7cc822012-11-19 14:55:58 -08002311 break;
2312 }
2313 }
2314
2315 return mEffects.size();
2316}
2317
jiabin8f278ee2019-11-11 12:16:27 -08002318// setDevices_l() must be called with ThreadBase::mLock held
2319void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002320{
2321 size_t size = mEffects.size();
2322 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002323 mEffects[i]->setDevices(devices);
2324 }
2325}
2326
2327// setInputDevice_l() must be called with ThreadBase::mLock held
2328void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2329{
2330 size_t size = mEffects.size();
2331 for (size_t i = 0; i < size; i++) {
2332 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002333 }
2334}
2335
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002336// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002337void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2338{
2339 size_t size = mEffects.size();
2340 for (size_t i = 0; i < size; i++) {
2341 mEffects[i]->setMode(mode);
2342 }
2343}
2344
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002345// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002346void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2347{
2348 size_t size = mEffects.size();
2349 for (size_t i = 0; i < size; i++) {
2350 mEffects[i]->setAudioSource(source);
2351 }
2352}
2353
Zhou Songd505c642020-02-20 16:35:37 +08002354bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2355 for (const auto &effect : mEffects) {
2356 if (effect->isVolumeControlEnabled()) return true;
2357 }
2358 return false;
2359}
2360
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002361// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002362bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002363{
2364 uint32_t newLeft = *left;
2365 uint32_t newRight = *right;
2366 bool hasControl = false;
2367 int ctrlIdx = -1;
2368 size_t size = mEffects.size();
2369
2370 // first update volume controller
2371 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002372 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002373 ctrlIdx = i - 1;
2374 hasControl = true;
2375 break;
2376 }
2377 }
2378
Eric Laurentfa1e1232016-08-02 19:01:49 -07002379 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002380 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002381 if (hasControl) {
2382 *left = mNewLeftVolume;
2383 *right = mNewRightVolume;
2384 }
2385 return hasControl;
2386 }
2387
2388 mVolumeCtrlIdx = ctrlIdx;
2389 mLeftVolume = newLeft;
2390 mRightVolume = newRight;
2391
2392 // second get volume update from volume controller
2393 if (ctrlIdx >= 0) {
2394 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2395 mNewLeftVolume = newLeft;
2396 mNewRightVolume = newRight;
2397 }
2398 // then indicate volume to all other effects in chain.
2399 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002400 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002401 uint32_t lVol = newLeft;
2402 uint32_t rVol = newRight;
2403
2404 for (size_t i = 0; i < size; i++) {
2405 if ((int)i == ctrlIdx) {
2406 continue;
2407 }
2408 // this also works for ctrlIdx == -1 when there is no volume controller
2409 if ((int)i > ctrlIdx) {
2410 lVol = *left;
2411 rVol = *right;
2412 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002413 // Pass requested volume directly if this is volume monitor module
2414 if (mEffects[i]->isVolumeMonitor()) {
2415 mEffects[i]->setVolume(left, right, false);
2416 } else {
2417 mEffects[i]->setVolume(&lVol, &rVol, false);
2418 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002419 }
2420 *left = newLeft;
2421 *right = newRight;
2422
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002423 setVolumeForOutput_l(*left, *right);
2424
Eric Laurentca7cc822012-11-19 14:55:58 -08002425 return hasControl;
2426}
2427
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002428// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002429void AudioFlinger::EffectChain::resetVolume_l()
2430{
Eric Laurente7449bf2016-08-03 18:44:07 -07002431 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2432 uint32_t left = mLeftVolume;
2433 uint32_t right = mRightVolume;
2434 (void)setVolume_l(&left, &right, true);
2435 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002436}
2437
jiabineb3bda02020-06-30 14:07:03 -07002438// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2439bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2440{
2441 for (size_t i = 0; i < mEffects.size(); ++i) {
2442 if (mEffects[i]->isHapticGenerator()) {
2443 return true;
2444 }
2445 }
2446 return false;
2447}
2448
jiabine70bc7f2020-06-30 22:07:55 -07002449void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2450{
2451 Mutex::Autolock _l(mLock);
2452 for (size_t i = 0; i < mEffects.size(); ++i) {
2453 mEffects[i]->setHapticIntensity(id, intensity);
2454 }
2455}
2456
Eric Laurent1b928682014-10-02 19:41:47 -07002457void AudioFlinger::EffectChain::syncHalEffectsState()
2458{
2459 Mutex::Autolock _l(mLock);
2460 for (size_t i = 0; i < mEffects.size(); i++) {
2461 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2462 mEffects[i]->state() == EffectModule::STOPPING) {
2463 mEffects[i]->addEffectToHal_l();
2464 }
2465 }
2466}
2467
Eric Laurentca7cc822012-11-19 14:55:58 -08002468void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2469{
Eric Laurentca7cc822012-11-19 14:55:58 -08002470 String8 result;
2471
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002472 const size_t numEffects = mEffects.size();
2473 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002474
Marco Nelissenb2208842014-02-07 14:00:50 -08002475 if (numEffects) {
2476 bool locked = AudioFlinger::dumpTryLock(mLock);
2477 // failed to lock - AudioFlinger is probably deadlocked
2478 if (!locked) {
2479 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002480 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002481
Andy Hungbded9c82017-11-30 18:47:35 -08002482 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2483 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2484 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2485 (int)inBufferStr.size(), "In buffer ",
2486 (int)outBufferStr.size(), "Out buffer ");
2487 result.appendFormat("\t%s %s %d\n",
2488 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002489 write(fd, result.string(), result.size());
2490
2491 for (size_t i = 0; i < numEffects; ++i) {
2492 sp<EffectModule> effect = mEffects[i];
2493 if (effect != 0) {
2494 effect->dump(fd, args);
2495 }
2496 }
2497
2498 if (locked) {
2499 mLock.unlock();
2500 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002501 } else {
2502 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002503 }
2504}
2505
2506// must be called with ThreadBase::mLock held
2507void AudioFlinger::EffectChain::setEffectSuspended_l(
2508 const effect_uuid_t *type, bool suspend)
2509{
2510 sp<SuspendedEffectDesc> desc;
2511 // use effect type UUID timelow as key as there is no real risk of identical
2512 // timeLow fields among effect type UUIDs.
2513 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2514 if (suspend) {
2515 if (index >= 0) {
2516 desc = mSuspendedEffects.valueAt(index);
2517 } else {
2518 desc = new SuspendedEffectDesc();
2519 desc->mType = *type;
2520 mSuspendedEffects.add(type->timeLow, desc);
2521 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2522 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002523
Eric Laurentca7cc822012-11-19 14:55:58 -08002524 if (desc->mRefCount++ == 0) {
2525 sp<EffectModule> effect = getEffectIfEnabled(type);
2526 if (effect != 0) {
2527 desc->mEffect = effect;
2528 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002529 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002530 }
2531 }
2532 } else {
2533 if (index < 0) {
2534 return;
2535 }
2536 desc = mSuspendedEffects.valueAt(index);
2537 if (desc->mRefCount <= 0) {
2538 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002539 desc->mRefCount = 0;
2540 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002541 }
2542 if (--desc->mRefCount == 0) {
2543 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2544 if (desc->mEffect != 0) {
2545 sp<EffectModule> effect = desc->mEffect.promote();
2546 if (effect != 0) {
2547 effect->setSuspended(false);
2548 effect->lock();
2549 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002550 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002551 effect->setEnabled_l(handle->enabled());
2552 }
2553 effect->unlock();
2554 }
2555 desc->mEffect.clear();
2556 }
2557 mSuspendedEffects.removeItemsAt(index);
2558 }
2559 }
2560}
2561
2562// must be called with ThreadBase::mLock held
2563void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2564{
2565 sp<SuspendedEffectDesc> desc;
2566
2567 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2568 if (suspend) {
2569 if (index >= 0) {
2570 desc = mSuspendedEffects.valueAt(index);
2571 } else {
2572 desc = new SuspendedEffectDesc();
2573 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2574 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2575 }
2576 if (desc->mRefCount++ == 0) {
2577 Vector< sp<EffectModule> > effects;
2578 getSuspendEligibleEffects(effects);
2579 for (size_t i = 0; i < effects.size(); i++) {
2580 setEffectSuspended_l(&effects[i]->desc().type, true);
2581 }
2582 }
2583 } else {
2584 if (index < 0) {
2585 return;
2586 }
2587 desc = mSuspendedEffects.valueAt(index);
2588 if (desc->mRefCount <= 0) {
2589 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2590 desc->mRefCount = 1;
2591 }
2592 if (--desc->mRefCount == 0) {
2593 Vector<const effect_uuid_t *> types;
2594 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2595 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2596 continue;
2597 }
2598 types.add(&mSuspendedEffects.valueAt(i)->mType);
2599 }
2600 for (size_t i = 0; i < types.size(); i++) {
2601 setEffectSuspended_l(types[i], false);
2602 }
2603 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2604 mSuspendedEffects.keyAt(index));
2605 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2606 }
2607 }
2608}
2609
2610
2611// The volume effect is used for automated tests only
2612#ifndef OPENSL_ES_H_
2613static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2614 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2615const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2616#endif //OPENSL_ES_H_
2617
Eric Laurentd8365c52017-07-16 15:27:05 -07002618/* static */
2619bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2620{
2621 // Only NS and AEC are suspended when BtNRec is off
2622 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2623 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2624 return true;
2625 }
2626 return false;
2627}
2628
Eric Laurentca7cc822012-11-19 14:55:58 -08002629bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2630{
2631 // auxiliary effects and visualizer are never suspended on output mix
2632 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2633 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2634 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002635 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2636 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002637 return false;
2638 }
2639 return true;
2640}
2641
2642void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2643 Vector< sp<AudioFlinger::EffectModule> > &effects)
2644{
2645 effects.clear();
2646 for (size_t i = 0; i < mEffects.size(); i++) {
2647 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2648 effects.add(mEffects[i]);
2649 }
2650 }
2651}
2652
2653sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2654 const effect_uuid_t *type)
2655{
2656 sp<EffectModule> effect = getEffectFromType_l(type);
2657 return effect != 0 && effect->isEnabled() ? effect : 0;
2658}
2659
2660void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2661 bool enabled)
2662{
2663 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2664 if (enabled) {
2665 if (index < 0) {
2666 // if the effect is not suspend check if all effects are suspended
2667 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2668 if (index < 0) {
2669 return;
2670 }
2671 if (!isEffectEligibleForSuspend(effect->desc())) {
2672 return;
2673 }
2674 setEffectSuspended_l(&effect->desc().type, enabled);
2675 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2676 if (index < 0) {
2677 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2678 return;
2679 }
2680 }
2681 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2682 effect->desc().type.timeLow);
2683 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002684 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002685 if (desc->mEffect == 0) {
2686 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002687 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002688 effect->setSuspended(true);
2689 }
2690 } else {
2691 if (index < 0) {
2692 return;
2693 }
2694 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2695 effect->desc().type.timeLow);
2696 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2697 desc->mEffect.clear();
2698 effect->setSuspended(false);
2699 }
2700}
2701
Eric Laurent5baf2af2013-09-12 17:37:00 -07002702bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002703{
2704 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002705 return isNonOffloadableEnabled_l();
2706}
2707
2708bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2709{
Eric Laurent813e2a72013-08-31 12:59:48 -07002710 size_t size = mEffects.size();
2711 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002712 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002713 return true;
2714 }
2715 }
2716 return false;
2717}
2718
Eric Laurentaaa44472014-09-12 17:41:50 -07002719void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2720{
2721 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002722 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002723}
2724
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002725void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2726{
2727 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2728 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2729 }
2730 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2731 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2732 }
2733}
2734
2735void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2736{
2737 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2738 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2739 }
2740 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2741 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2742 }
2743}
2744
2745bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002746{
2747 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002748 for (const auto &effect : mEffects) {
2749 if (effect->isProcessImplemented()) {
2750 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002751 }
2752 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002753 // Allow effects without processing.
2754 return true;
2755}
2756
2757bool AudioFlinger::EffectChain::isFastCompatible() const
2758{
2759 Mutex::Autolock _l(mLock);
2760 for (const auto &effect : mEffects) {
2761 if (effect->isProcessImplemented()
2762 && effect->isImplementationSoftware()) {
2763 return false;
2764 }
2765 }
2766 // Allow effects without processing or hw accelerated effects.
2767 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002768}
2769
2770// isCompatibleWithThread_l() must be called with thread->mLock held
2771bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2772{
2773 Mutex::Autolock _l(mLock);
2774 for (size_t i = 0; i < mEffects.size(); i++) {
2775 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2776 return false;
2777 }
2778 }
2779 return true;
2780}
2781
Eric Laurent6b446ce2019-12-13 10:56:31 -08002782// EffectCallbackInterface implementation
2783status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2784 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2785 sp<EffectHalInterface> *effect) {
2786 status_t status = NO_INIT;
2787 sp<AudioFlinger> af = mAudioFlinger.promote();
2788 if (af == nullptr) {
2789 return status;
2790 }
2791 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2792 if (effectsFactory != 0) {
2793 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2794 }
2795 return status;
2796}
2797
2798bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002799 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002800 sp<AudioFlinger> af = mAudioFlinger.promote();
2801 if (af == nullptr) {
2802 return false;
2803 }
Eric Laurent41709552019-12-16 19:34:05 -08002804 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2805 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002806}
2807
2808status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2809 size_t size, sp<EffectBufferHalInterface>* buffer) {
2810 sp<AudioFlinger> af = mAudioFlinger.promote();
2811 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2812 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2813}
2814
2815status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2816 sp<EffectHalInterface> effect) {
2817 status_t result = NO_INIT;
2818 sp<ThreadBase> t = mThread.promote();
2819 if (t == nullptr) {
2820 return result;
2821 }
2822 sp <StreamHalInterface> st = t->stream();
2823 if (st == nullptr) {
2824 return result;
2825 }
2826 result = st->addEffect(effect);
2827 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2828 return result;
2829}
2830
2831status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2832 sp<EffectHalInterface> effect) {
2833 status_t result = NO_INIT;
2834 sp<ThreadBase> t = mThread.promote();
2835 if (t == nullptr) {
2836 return result;
2837 }
2838 sp <StreamHalInterface> st = t->stream();
2839 if (st == nullptr) {
2840 return result;
2841 }
2842 result = st->removeEffect(effect);
2843 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2844 return result;
2845}
2846
2847audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2848 sp<ThreadBase> t = mThread.promote();
2849 if (t == nullptr) {
2850 return AUDIO_IO_HANDLE_NONE;
2851 }
2852 return t->id();
2853}
2854
2855bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2856 sp<ThreadBase> t = mThread.promote();
2857 if (t == nullptr) {
2858 return true;
2859 }
2860 return t->isOutput();
2861}
2862
2863bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2864 sp<ThreadBase> t = mThread.promote();
2865 if (t == nullptr) {
2866 return false;
2867 }
2868 return t->type() == ThreadBase::OFFLOAD;
2869}
2870
2871bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2872 sp<ThreadBase> t = mThread.promote();
2873 if (t == nullptr) {
2874 return false;
2875 }
2876 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2877}
2878
2879bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2880 sp<ThreadBase> t = mThread.promote();
2881 if (t == nullptr) {
2882 return false;
2883 }
Andy Hungea840382020-05-05 21:50:17 -07002884 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002885}
2886
2887uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2888 sp<ThreadBase> t = mThread.promote();
2889 if (t == nullptr) {
2890 return 0;
2891 }
2892 return t->sampleRate();
2893}
2894
2895audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2896 sp<ThreadBase> t = mThread.promote();
2897 if (t == nullptr) {
2898 return AUDIO_CHANNEL_NONE;
2899 }
2900 return t->channelMask();
2901}
2902
2903uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2904 sp<ThreadBase> t = mThread.promote();
2905 if (t == nullptr) {
2906 return 0;
2907 }
2908 return t->channelCount();
2909}
2910
jiabineb3bda02020-06-30 14:07:03 -07002911audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
2912 sp<ThreadBase> t = mThread.promote();
2913 if (t == nullptr) {
2914 return AUDIO_CHANNEL_NONE;
2915 }
2916 return t->hapticChannelMask();
2917}
2918
Eric Laurent6b446ce2019-12-13 10:56:31 -08002919size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2920 sp<ThreadBase> t = mThread.promote();
2921 if (t == nullptr) {
2922 return 0;
2923 }
2924 return t->frameCount();
2925}
2926
2927uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2928 sp<ThreadBase> t = mThread.promote();
2929 if (t == nullptr) {
2930 return 0;
2931 }
2932 return t->latency_l();
2933}
2934
2935void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2936 sp<ThreadBase> t = mThread.promote();
2937 if (t == nullptr) {
2938 return;
2939 }
2940 t->setVolumeForOutput_l(left, right);
2941}
2942
2943void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002944 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002945 sp<ThreadBase> t = mThread.promote();
2946 if (t == nullptr) {
2947 return;
2948 }
2949 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2950
2951 sp<EffectChain> c = mChain.promote();
2952 if (c == nullptr) {
2953 return;
2954 }
Eric Laurent41709552019-12-16 19:34:05 -08002955 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2956 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002957}
2958
Eric Laurent41709552019-12-16 19:34:05 -08002959void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002960 sp<ThreadBase> t = mThread.promote();
2961 if (t == nullptr) {
2962 return;
2963 }
Eric Laurent41709552019-12-16 19:34:05 -08002964 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2965 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002966}
2967
Eric Laurent41709552019-12-16 19:34:05 -08002968void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002969 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2970
2971 sp<ThreadBase> t = mThread.promote();
2972 if (t == nullptr) {
2973 return;
2974 }
2975 t->onEffectDisable();
2976}
2977
2978bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2979 bool unpinIfLast) {
2980 sp<ThreadBase> t = mThread.promote();
2981 if (t == nullptr) {
2982 return false;
2983 }
2984 t->disconnectEffectHandle(handle, unpinIfLast);
2985 return true;
2986}
2987
2988void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2989 sp<EffectChain> c = mChain.promote();
2990 if (c == nullptr) {
2991 return;
2992 }
2993 c->resetVolume_l();
2994
2995}
2996
2997uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2998 sp<EffectChain> c = mChain.promote();
2999 if (c == nullptr) {
3000 return PRODUCT_STRATEGY_NONE;
3001 }
3002 return c->strategy();
3003}
3004
3005int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
3006 sp<EffectChain> c = mChain.promote();
3007 if (c == nullptr) {
3008 return 0;
3009 }
3010 return c->activeTrackCnt();
3011}
3012
Eric Laurentb82e6b72019-11-22 17:25:04 -08003013
3014#undef LOG_TAG
3015#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3016
3017status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3018{
3019 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3020 Mutex::Autolock _l(mProxyLock);
3021 if (status == NO_ERROR) {
3022 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003023 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003024 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003025 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003026 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003027 bs = handle.second->disable(&status);
3028 }
3029 if (!bs.isOk()) {
3030 status = bs.transactionError();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003031 }
3032 }
3033 }
3034 ALOGV("%s enable %d status %d", __func__, enabled, status);
3035 return status;
3036}
3037
3038status_t AudioFlinger::DeviceEffectProxy::init(
3039 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3040//For all audio patches
3041//If src or sink device match
3042//If the effect is HW accelerated
3043// if no corresponding effect module
3044// Create EffectModule: mHalEffect
3045//Create and attach EffectHandle
3046//If the effect is not HW accelerated and the patch sink or src is a mixer port
3047// Create Effect on patch input or output thread on session -1
3048//Add EffectHandle to EffectHandle map of Effect Proxy:
3049 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3050 status_t status = NO_ERROR;
3051 for (auto &patch : patches) {
3052 status = onCreatePatch(patch.first, patch.second);
3053 ALOGV("%s onCreatePatch status %d", __func__, status);
3054 if (status == BAD_VALUE) {
3055 return status;
3056 }
3057 }
3058 return status;
3059}
3060
3061status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3062 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3063 status_t status = NAME_NOT_FOUND;
3064 sp<EffectHandle> handle;
3065 // only consider source[0] as this is the only "true" source of a patch
3066 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3067 ALOGV("%s source checkPort status %d", __func__, status);
3068 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3069 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3070 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3071 }
3072 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3073 Mutex::Autolock _l(mProxyLock);
3074 mEffectHandles.emplace(patchHandle, handle);
3075 }
3076 ALOGW_IF(status == BAD_VALUE,
3077 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3078
3079 return status;
3080}
3081
3082status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3083 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3084
3085 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3086 __func__, port->type, port->ext.device.type,
3087 port->ext.device.address, port->id, patch.isSoftware());
3088 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003089 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003090 return NAME_NOT_FOUND;
3091 }
3092 status_t status = NAME_NOT_FOUND;
3093
3094 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3095 Mutex::Autolock _l(mProxyLock);
3096 mDevicePort = *port;
3097 mHalEffect = new EffectModule(mMyCallback,
3098 const_cast<effect_descriptor_t *>(&mDescriptor),
3099 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3100 false /* pinned */, port->id);
3101 if (audio_is_input_device(mDevice.mType)) {
3102 mHalEffect->setInputDevice(mDevice);
3103 } else {
3104 mHalEffect->setDevices({mDevice});
3105 }
3106 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3107 status = (*handle)->initCheck();
3108 if (status == OK) {
3109 status = mHalEffect->addHandle((*handle).get());
3110 } else {
3111 mHalEffect.clear();
3112 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3113 }
3114 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3115 sp <ThreadBase> thread;
3116 if (audio_port_config_has_input_direction(port)) {
3117 if (patch.isSoftware()) {
3118 thread = patch.mRecord.thread();
3119 } else {
3120 thread = patch.thread().promote();
3121 }
3122 } else {
3123 if (patch.isSoftware()) {
3124 thread = patch.mPlayback.thread();
3125 } else {
3126 thread = patch.thread().promote();
3127 }
3128 }
3129 int enabled;
3130 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3131 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003132 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003133 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3134 } else {
3135 status = BAD_VALUE;
3136 }
3137 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003138 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003139 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003140 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003141 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003142 bs = (*handle)->disable(&status);
3143 }
3144 if (!bs.isOk()) {
3145 status = bs.transactionError();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003146 }
3147 }
3148 return status;
3149}
3150
3151void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3152 Mutex::Autolock _l(mProxyLock);
3153 mEffectHandles.erase(patchHandle);
3154}
3155
3156
3157size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3158{
3159 Mutex::Autolock _l(mProxyLock);
3160 if (effect == mHalEffect) {
3161 mHalEffect.clear();
3162 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3163 }
3164 return mHalEffect == nullptr ? 0 : 1;
3165}
3166
3167status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3168 sp<EffectHalInterface> effect) {
3169 if (mHalEffect == nullptr) {
3170 return NO_INIT;
3171 }
3172 return mManagerCallback->addEffectToHal(
3173 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3174}
3175
3176status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3177 sp<EffectHalInterface> effect) {
3178 if (mHalEffect == nullptr) {
3179 return NO_INIT;
3180 }
3181 return mManagerCallback->removeEffectFromHal(
3182 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3183}
3184
3185bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3186 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3187 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3188 }
3189 return true;
3190}
3191
3192uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3193 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3194 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3195 return mDevicePort.sample_rate;
3196 }
3197 return DEFAULT_OUTPUT_SAMPLE_RATE;
3198}
3199
3200audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3201 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3202 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3203 return mDevicePort.channel_mask;
3204 }
3205 return AUDIO_CHANNEL_OUT_STEREO;
3206}
3207
3208uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3209 if (isOutput()) {
3210 return audio_channel_count_from_out_mask(channelMask());
3211 }
3212 return audio_channel_count_from_in_mask(channelMask());
3213}
3214
3215void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3216 const Vector<String16> args;
3217 EffectBase::dump(fd, args);
3218
3219 const bool locked = dumpTryLock(mProxyLock);
3220 if (!locked) {
3221 String8 result("DeviceEffectProxy may be deadlocked\n");
3222 write(fd, result.string(), result.size());
3223 }
3224
3225 String8 outStr;
3226 if (mHalEffect != nullptr) {
3227 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3228 } else {
3229 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3230 }
3231 write(fd, outStr.string(), outStr.size());
3232 outStr.clear();
3233
3234 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3235 write(fd, outStr.string(), outStr.size());
3236 outStr.clear();
3237
3238 for (const auto& iter : mEffectHandles) {
3239 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3240 write(fd, outStr.string(), outStr.size());
3241 outStr.clear();
3242 sp<EffectBase> effect = iter.second->effect().promote();
3243 if (effect != nullptr) {
3244 effect->dump(fd, args);
3245 }
3246 }
3247
3248 if (locked) {
3249 mLock.unlock();
3250 }
3251}
3252
3253#undef LOG_TAG
3254#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3255
3256int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3257 return mManagerCallback->newEffectId();
3258}
3259
3260
3261bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3262 EffectHandle *handle, bool unpinIfLast) {
3263 sp<EffectBase> effectBase = handle->effect().promote();
3264 if (effectBase == nullptr) {
3265 return false;
3266 }
3267
3268 sp<EffectModule> effect = effectBase->asEffectModule();
3269 if (effect == nullptr) {
3270 return false;
3271 }
3272
3273 // restore suspended effects if the disconnected handle was enabled and the last one.
3274 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3275 if (remove) {
3276 sp<DeviceEffectProxy> proxy = mProxy.promote();
3277 if (proxy != nullptr) {
3278 proxy->removeEffect(effect);
3279 }
3280 if (handle->enabled()) {
3281 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3282 }
3283 }
3284 return true;
3285}
3286
3287status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3288 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3289 sp<EffectHalInterface> *effect) {
3290 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3291}
3292
3293status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3294 sp<EffectHalInterface> effect) {
3295 sp<DeviceEffectProxy> proxy = mProxy.promote();
3296 if (proxy == nullptr) {
3297 return NO_INIT;
3298 }
3299 return proxy->addEffectToHal(effect);
3300}
3301
3302status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3303 sp<EffectHalInterface> effect) {
3304 sp<DeviceEffectProxy> proxy = mProxy.promote();
3305 if (proxy == nullptr) {
3306 return NO_INIT;
3307 }
3308 return proxy->addEffectToHal(effect);
3309}
3310
3311bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3312 sp<DeviceEffectProxy> proxy = mProxy.promote();
3313 if (proxy == nullptr) {
3314 return true;
3315 }
3316 return proxy->isOutput();
3317}
3318
3319uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3320 sp<DeviceEffectProxy> proxy = mProxy.promote();
3321 if (proxy == nullptr) {
3322 return DEFAULT_OUTPUT_SAMPLE_RATE;
3323 }
3324 return proxy->sampleRate();
3325}
3326
3327audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3328 sp<DeviceEffectProxy> proxy = mProxy.promote();
3329 if (proxy == nullptr) {
3330 return AUDIO_CHANNEL_OUT_STEREO;
3331 }
3332 return proxy->channelMask();
3333}
3334
3335uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3336 sp<DeviceEffectProxy> proxy = mProxy.promote();
3337 if (proxy == nullptr) {
3338 return 2;
3339 }
3340 return proxy->channelCount();
3341}
3342
Glenn Kasten63238ef2015-03-02 15:50:29 -08003343} // namespace android