blob: b267d8824b1aed5e4274da001c3225f9aebf6133 [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
Andy Hung1131b6e2020-12-08 20:47:45 -080063using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070064using binder::Status;
65
66namespace {
67
68// Append a POD value into a vector of bytes.
69template<typename T>
70void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
71 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
72 buffer->insert(buffer->end(), ar, ar + sizeof(T));
73}
74
75// Write a POD value into a vector of bytes (clears the previous buffer
76// content).
77template<typename T>
78void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
79 buffer->clear();
80 appendToBuffer(value, buffer);
81}
82
83} // namespace
84
Eric Laurentca7cc822012-11-19 14:55:58 -080085// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080086// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080087// ----------------------------------------------------------------------------
88
89#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080090#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080091
Eric Laurent41709552019-12-16 19:34:05 -080092AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080093 effect_descriptor_t *desc,
94 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080095 audio_session_t sessionId,
96 bool pinned)
97 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080098 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080099 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800100{
Eric Laurentca7cc822012-11-19 14:55:58 -0800101}
102
Eric Laurent41709552019-12-16 19:34:05 -0800103// must be called with EffectModule::mLock held
104status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800105{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800106
Eric Laurent41709552019-12-16 19:34:05 -0800107 ALOGV("setEnabled %p enabled %d", this, enabled);
108
109 if (enabled != isEnabled()) {
110 switch (mState) {
111 // going from disabled to enabled
112 case IDLE:
113 mState = STARTING;
114 break;
115 case STOPPED:
116 mState = RESTART;
117 break;
118 case STOPPING:
119 mState = ACTIVE;
120 break;
121
122 // going from enabled to disabled
123 case RESTART:
124 mState = STOPPED;
125 break;
126 case STARTING:
127 mState = IDLE;
128 break;
129 case ACTIVE:
130 mState = STOPPING;
131 break;
132 case DESTROYED:
133 return NO_ERROR; // simply ignore as we are being destroyed
134 }
135 for (size_t i = 1; i < mHandles.size(); i++) {
136 EffectHandle *h = mHandles[i];
137 if (h != NULL && !h->disconnected()) {
138 h->setEnabled(enabled);
139 }
140 }
141 }
142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800143}
144
Eric Laurent41709552019-12-16 19:34:05 -0800145status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
146{
147 status_t status;
148 {
149 Mutex::Autolock _l(mLock);
150 status = setEnabled_l(enabled);
151 }
152 if (fromHandle) {
153 if (enabled) {
154 if (status != NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -0700155 getCallback()->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
Eric Laurent41709552019-12-16 19:34:05 -0800156 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700157 getCallback()->onEffectEnable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800158 }
159 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700160 getCallback()->onEffectDisable(this);
Eric Laurent41709552019-12-16 19:34:05 -0800161 }
162 }
163 return status;
164}
165
166bool AudioFlinger::EffectBase::isEnabled() const
167{
168 switch (mState) {
169 case RESTART:
170 case STARTING:
171 case ACTIVE:
172 return true;
173 case IDLE:
174 case STOPPING:
175 case STOPPED:
176 case DESTROYED:
177 default:
178 return false;
179 }
180}
181
182void AudioFlinger::EffectBase::setSuspended(bool suspended)
183{
184 Mutex::Autolock _l(mLock);
185 mSuspended = suspended;
186}
187
188bool AudioFlinger::EffectBase::suspended() const
189{
190 Mutex::Autolock _l(mLock);
191 return mSuspended;
192}
193
194status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800195{
196 status_t status;
197
198 Mutex::Autolock _l(mLock);
199 int priority = handle->priority();
200 size_t size = mHandles.size();
201 EffectHandle *controlHandle = NULL;
202 size_t i;
203 for (i = 0; i < size; i++) {
204 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800205 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800206 continue;
207 }
208 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 if (h->priority() <= priority) {
213 break;
214 }
215 }
216 // if inserted in first place, move effect control from previous owner to this handle
217 if (i == 0) {
218 bool enabled = false;
219 if (controlHandle != NULL) {
220 enabled = controlHandle->enabled();
221 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
222 }
223 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
224 status = NO_ERROR;
225 } else {
226 status = ALREADY_EXISTS;
227 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700228 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800229 mHandles.insertAt(handle, i);
230 return status;
231}
232
Eric Laurent41709552019-12-16 19:34:05 -0800233status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700234{
235 status_t status = NO_ERROR;
236 bool doRegister = false;
237 bool registered = false;
238 bool doEnable = false;
239 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700240 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800241 product_strategy_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700242
243 {
244 Mutex::Autolock _l(mLock);
245 // register effect when first handle is attached and unregister when last handle is removed
246 if (mPolicyRegistered != mHandles.size() > 0) {
247 doRegister = true;
248 mPolicyRegistered = mHandles.size() > 0;
249 if (mPolicyRegistered) {
Andy Hungfda44002021-06-03 17:23:16 -0700250 const auto callback = getCallback();
251 io = callback->io();
252 strategy = callback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700253 }
254 }
255 // enable effect when registered according to enable state requested by controlling handle
256 if (mHandles.size() > 0) {
257 EffectHandle *handle = controlHandle_l();
258 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
259 doEnable = true;
260 mPolicyEnabled = handle->enabled();
261 }
262 }
263 registered = mPolicyRegistered;
264 enabled = mPolicyEnabled;
Eric Laurentb9d06642021-03-18 15:52:11 +0100265 // The simultaneous release of two EffectHandles with the same EffectModule
266 // may cause us to call this method at the same time.
267 // This may deadlock under some circumstances (b/180941720). Avoid this.
268 if (!doRegister && !(registered && doEnable)) {
269 return NO_ERROR;
270 }
Eric Laurent6c796322019-04-09 14:13:17 -0700271 mPolicyLock.lock();
272 }
273 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
274 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
275 if (doRegister) {
276 if (registered) {
277 status = AudioSystem::registerEffect(
278 &mDescriptor,
279 io,
280 strategy,
281 mSessionId,
282 mId);
283 } else {
284 status = AudioSystem::unregisterEffect(mId);
285 }
286 }
287 if (registered && doEnable) {
288 status = AudioSystem::setEffectEnabled(mId, enabled);
289 }
290 mPolicyLock.unlock();
291
292 return status;
293}
294
295
Eric Laurent41709552019-12-16 19:34:05 -0800296ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800297{
298 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800299 return removeHandle_l(handle);
300}
301
Eric Laurent41709552019-12-16 19:34:05 -0800302ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800303{
Eric Laurentca7cc822012-11-19 14:55:58 -0800304 size_t size = mHandles.size();
305 size_t i;
306 for (i = 0; i < size; i++) {
307 if (mHandles[i] == handle) {
308 break;
309 }
310 }
311 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800312 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
313 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800314 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800315 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800316
317 mHandles.removeAt(i);
318 // if removed from first place, move effect control from this handle to next in line
319 if (i == 0) {
320 EffectHandle *h = controlHandle_l();
321 if (h != NULL) {
322 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
323 }
324 }
325
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530326 // Prevent calls to process() and other functions on effect interface from now on.
327 // The effect engine will be released by the destructor when the last strong reference on
328 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800329 if (mHandles.size() == 0 && !mPinned) {
330 mState = DESTROYED;
331 }
332
333 return mHandles.size();
334}
335
336// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800337AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800338{
339 // the first valid handle in the list has control over the module
340 for (size_t i = 0; i < mHandles.size(); i++) {
341 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800342 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800343 return h;
344 }
345 }
346
347 return NULL;
348}
349
Eric Laurentf10c7092016-12-06 17:09:56 -0800350// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800351ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800352{
Andy Hungfda44002021-06-03 17:23:16 -0700353 const auto callback = getCallback();
Eric Laurentf10c7092016-12-06 17:09:56 -0800354 ALOGV("disconnect() %p handle %p", this, handle);
Andy Hungfda44002021-06-03 17:23:16 -0700355 if (callback->disconnectEffectHandle(handle, unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800356 return mHandles.size();
357 }
358
Eric Laurentf10c7092016-12-06 17:09:56 -0800359 Mutex::Autolock _l(mLock);
360 ssize_t numHandles = removeHandle_l(handle);
361 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800362 mLock.unlock();
Andy Hungfda44002021-06-03 17:23:16 -0700363 callback->updateOrphanEffectChains(this);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800364 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800365 }
366 return numHandles;
367}
368
Eric Laurent41709552019-12-16 19:34:05 -0800369bool AudioFlinger::EffectBase::purgeHandles()
370{
371 bool enabled = false;
372 Mutex::Autolock _l(mLock);
373 EffectHandle *handle = controlHandle_l();
374 if (handle != NULL) {
375 enabled = handle->enabled();
376 }
377 mHandles.clear();
378 return enabled;
379}
380
381void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
Andy Hungfda44002021-06-03 17:23:16 -0700382 getCallback()->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
Eric Laurent41709552019-12-16 19:34:05 -0800383}
384
385static String8 effectFlagsToString(uint32_t flags) {
386 String8 s;
387
388 s.append("conn. mode: ");
389 switch (flags & EFFECT_FLAG_TYPE_MASK) {
390 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
391 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
392 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
393 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
394 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
395 default: s.append("unknown/reserved"); break;
396 }
397 s.append(", ");
398
399 s.append("insert pref: ");
400 switch (flags & EFFECT_FLAG_INSERT_MASK) {
401 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
402 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
403 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
404 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
405 default: s.append("unknown/reserved"); break;
406 }
407 s.append(", ");
408
409 s.append("volume mgmt: ");
410 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
411 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
412 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
413 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
414 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
415 default: s.append("unknown/reserved"); break;
416 }
417 s.append(", ");
418
419 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
420 if (devind) {
421 s.append("device indication: ");
422 switch (devind) {
423 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
424 default: s.append("unknown/reserved"); break;
425 }
426 s.append(", ");
427 }
428
429 s.append("input mode: ");
430 switch (flags & EFFECT_FLAG_INPUT_MASK) {
431 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
432 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
433 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
434 default: s.append("not set"); break;
435 }
436 s.append(", ");
437
438 s.append("output mode: ");
439 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
440 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
441 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
442 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
443 default: s.append("not set"); break;
444 }
445 s.append(", ");
446
447 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
448 if (accel) {
449 s.append("hardware acceleration: ");
450 switch (accel) {
451 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
452 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
453 default: s.append("unknown/reserved"); break;
454 }
455 s.append(", ");
456 }
457
458 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
459 if (modeind) {
460 s.append("mode indication: ");
461 switch (modeind) {
462 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
463 default: s.append("unknown/reserved"); break;
464 }
465 s.append(", ");
466 }
467
468 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
469 if (srcind) {
470 s.append("source indication: ");
471 switch (srcind) {
472 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
473 default: s.append("unknown/reserved"); break;
474 }
475 s.append(", ");
476 }
477
478 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
479 s.append("offloadable, ");
480 }
481
482 int len = s.length();
483 if (s.length() > 2) {
484 (void) s.lockBuffer(len);
485 s.unlockBuffer(len - 2);
486 }
487 return s;
488}
489
490void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
491{
492 String8 result;
493
494 result.appendFormat("\tEffect ID %d:\n", mId);
495
496 bool locked = AudioFlinger::dumpTryLock(mLock);
497 // failed to lock - AudioFlinger is probably deadlocked
498 if (!locked) {
499 result.append("\t\tCould not lock Fx mutex:\n");
500 }
501
502 result.append("\t\tSession State Registered Enabled Suspended:\n");
503 result.appendFormat("\t\t%05d %03d %s %s %s\n",
504 mSessionId, mState, mPolicyRegistered ? "y" : "n",
505 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
506
507 result.append("\t\tDescriptor:\n");
508 char uuidStr[64];
509 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
510 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
511 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
512 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
513 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
514 mDescriptor.apiVersion,
515 mDescriptor.flags,
516 effectFlagsToString(mDescriptor.flags).string());
517 result.appendFormat("\t\t- name: %s\n",
518 mDescriptor.name);
519
520 result.appendFormat("\t\t- implementor: %s\n",
521 mDescriptor.implementor);
522
523 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
524 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
525 char buffer[256];
526 for (size_t i = 0; i < mHandles.size(); ++i) {
527 EffectHandle *handle = mHandles[i];
528 if (handle != NULL && !handle->disconnected()) {
529 handle->dumpToBuffer(buffer, sizeof(buffer));
530 result.append(buffer);
531 }
532 }
533 if (locked) {
534 mLock.unlock();
535 }
536
537 write(fd, result.string(), result.length());
538}
539
540// ----------------------------------------------------------------------------
541// EffectModule implementation
542// ----------------------------------------------------------------------------
543
544#undef LOG_TAG
545#define LOG_TAG "AudioFlinger::EffectModule"
546
547AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
548 effect_descriptor_t *desc,
549 int id,
550 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800551 bool pinned,
552 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800553 : EffectBase(callback, desc, id, sessionId, pinned),
554 // clear mConfig to ensure consistent initial value of buffer framecount
555 // in case buffers are associated by setInBuffer() or setOutBuffer()
556 // prior to configure().
557 mConfig{{}, {}},
558 mStatus(NO_INIT),
559 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
560 mDisableWaitCnt(0), // set by process() and updateState()
David Li6c8ac4b2021-06-22 22:17:52 +0800561 mOffloaded(false),
562 mAddedToHal(false)
Eric Laurent41709552019-12-16 19:34:05 -0800563#ifdef FLOAT_EFFECT_CHAIN
564 , mSupportsFloat(false)
565#endif
566{
567 ALOGV("Constructor %p pinned %d", this, pinned);
568 int lStatus;
569
570 // create effect engine from effect factory
571 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800572 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800573 if (mStatus != NO_ERROR) {
574 return;
575 }
576 lStatus = init();
577 if (lStatus < 0) {
578 mStatus = lStatus;
579 goto Error;
580 }
581
582 setOffloaded(callback->isOffload(), callback->io());
583 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
584
585 return;
586Error:
587 mEffectInterface.clear();
588 ALOGV("Constructor Error %d", mStatus);
589}
590
591AudioFlinger::EffectModule::~EffectModule()
592{
593 ALOGV("Destructor %p", this);
594 if (mEffectInterface != 0) {
595 char uuidStr[64];
596 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
597 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
598 this, uuidStr);
599 release_l();
600 }
601
602}
603
Eric Laurentfa1e1232016-08-02 19:01:49 -0700604bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800605 Mutex::Autolock _l(mLock);
606
Eric Laurentfa1e1232016-08-02 19:01:49 -0700607 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800608 switch (mState) {
609 case RESTART:
610 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700611 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800612
613 case STARTING:
614 // clear auxiliary effect input buffer for next accumulation
615 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
616 memset(mConfig.inputCfg.buffer.raw,
617 0,
618 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
619 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700620 if (start_l() == NO_ERROR) {
621 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700622 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700623 } else {
624 mState = IDLE;
625 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800626 break;
627 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900628 // volume control for offload and direct threads must take effect immediately.
629 if (stop_l() == NO_ERROR
630 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700631 mDisableWaitCnt = mMaxDisableWaitCnt;
632 } else {
633 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
634 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800635 mState = STOPPED;
636 break;
637 case STOPPED:
638 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
639 // turn off sequence.
640 if (--mDisableWaitCnt == 0) {
641 reset_l();
642 mState = IDLE;
643 }
644 break;
645 default: //IDLE , ACTIVE, DESTROYED
646 break;
647 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700648
649 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800650}
651
652void AudioFlinger::EffectModule::process()
653{
654 Mutex::Autolock _l(mLock);
655
Mikhail Naganov022b9952017-01-04 16:36:51 -0800656 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800657 return;
658 }
659
rago94a1ee82017-07-21 15:11:02 -0700660 const uint32_t inChannelCount =
661 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
662 const uint32_t outChannelCount =
663 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
664 const bool auxType =
665 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
666
Andy Hungfa69ca32017-11-30 10:07:53 -0800667 // safeInputOutputSampleCount is 0 if the channel count between input and output
668 // buffers do not match. This prevents automatic accumulation or copying between the
669 // input and output effect buffers without an intermediary effect process.
670 // TODO: consider implementing channel conversion.
671 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700672 mInChannelCountRequested != mOutChannelCountRequested ? 0
673 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800674 mConfig.inputCfg.buffer.frameCount,
675 mConfig.outputCfg.buffer.frameCount);
676 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
677#ifdef FLOAT_EFFECT_CHAIN
678 accumulate_float(
679 mConfig.outputCfg.buffer.f32,
680 mConfig.inputCfg.buffer.f32,
681 safeInputOutputSampleCount);
682#else
683 accumulate_i16(
684 mConfig.outputCfg.buffer.s16,
685 mConfig.inputCfg.buffer.s16,
686 safeInputOutputSampleCount);
687#endif
688 };
689 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
690#ifdef FLOAT_EFFECT_CHAIN
691 memcpy(
692 mConfig.outputCfg.buffer.f32,
693 mConfig.inputCfg.buffer.f32,
694 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
695
696#else
697 memcpy(
698 mConfig.outputCfg.buffer.s16,
699 mConfig.inputCfg.buffer.s16,
700 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
701#endif
702 };
703
Eric Laurentca7cc822012-11-19 14:55:58 -0800704 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700705 int ret;
706 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700707 if (auxType) {
708 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800709 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700710#ifdef FLOAT_EFFECT_CHAIN
711 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800712#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700713 // Do in-place float conversion for auxiliary effect input buffer.
714 static_assert(sizeof(float) <= sizeof(int32_t),
715 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
716
Andy Hungfa69ca32017-11-30 10:07:53 -0800717 memcpy_to_float_from_q4_27(
718 mConfig.inputCfg.buffer.f32,
719 mConfig.inputCfg.buffer.s32,
720 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800721#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800722 } else
Andy Hung116a4982017-11-30 10:15:08 -0800723#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800724 {
Andy Hung116a4982017-11-30 10:15:08 -0800725#ifdef FLOAT_AUX
726 memcpy_to_i16_from_float(
727 mConfig.inputCfg.buffer.s16,
728 mConfig.inputCfg.buffer.f32,
729 mConfig.inputCfg.buffer.frameCount);
730#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800731 memcpy_to_i16_from_q4_27(
732 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700733 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800734 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800735#endif
rago94a1ee82017-07-21 15:11:02 -0700736 }
rago94a1ee82017-07-21 15:11:02 -0700737 }
738#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800739 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
740 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
741
742 if (!auxType && mInChannelCountRequested != inChannelCount) {
743 adjust_channels(
744 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
745 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
746 sizeof(float),
747 sizeof(float)
748 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
749 inBuffer = mInConversionBuffer;
750 }
751 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
752 && mOutChannelCountRequested != outChannelCount) {
753 adjust_selected_channels(
754 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
755 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
756 sizeof(float),
757 sizeof(float)
758 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
759 outBuffer = mOutConversionBuffer;
760 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800761 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
762 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800763 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800764 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
765 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700766 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800767 memcpy_to_i16_from_float(
768 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800769 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700772 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800773 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800774 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800775 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
776 goto data_bypass;
777 }
778 memcpy_to_i16_from_float(
779 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800780 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800781 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800782 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700783 }
784 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800785#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800786 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800787#ifdef FLOAT_EFFECT_CHAIN
788 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 sp<EffectBufferHalInterface> target =
790 mOutChannelCountRequested != outChannelCount
791 ? mOutConversionBuffer : mOutBuffer;
792
Andy Hungfa69ca32017-11-30 10:07:53 -0800793 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800794 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800795 mOutConversionBuffer->audioBuffer()->s16,
796 outChannelCount * mConfig.outputCfg.buffer.frameCount);
797 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800798 if (mOutChannelCountRequested != outChannelCount) {
799 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
800 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
801 sizeof(float),
802 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
803 }
rago94a1ee82017-07-21 15:11:02 -0700804#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700805 } else {
rago94a1ee82017-07-21 15:11:02 -0700806#ifdef FLOAT_EFFECT_CHAIN
807 data_bypass:
808#endif
809 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800810 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700811 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800812 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700813 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800814 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700815 }
816 }
817 ret = -ENODATA;
818 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800819
Eric Laurentca7cc822012-11-19 14:55:58 -0800820 // force transition to IDLE state when engine is ready
821 if (mState == STOPPED && ret == -ENODATA) {
822 mDisableWaitCnt = 1;
823 }
824
825 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700826 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800827#ifdef FLOAT_AUX
828 const size_t size =
829 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
830#else
rago94a1ee82017-07-21 15:11:02 -0700831 const size_t size =
832 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800833#endif
rago94a1ee82017-07-21 15:11:02 -0700834 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800835 }
836 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700837 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800838 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
839 // If an insert effect is idle and input buffer is different from output buffer,
840 // accumulate input onto output
Andy Hungfda44002021-06-03 17:23:16 -0700841 if (getCallback()->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700842 // similar handling with data_bypass above.
843 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
844 accumulateInputToOutput();
845 } else { // EFFECT_BUFFER_ACCESS_WRITE
846 copyInputToOutput();
847 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800848 }
849 }
850}
851
852void AudioFlinger::EffectModule::reset_l()
853{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700854 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800855 return;
856 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700857 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800858}
859
860status_t AudioFlinger::EffectModule::configure()
861{
rago94a1ee82017-07-21 15:11:02 -0700862 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700863 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700864 uint32_t size;
865 audio_channel_mask_t channelMask;
Andy Hungfda44002021-06-03 17:23:16 -0700866 sp<EffectCallbackInterface> callback;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700867
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700868 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700869 status = NO_INIT;
870 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800871 }
872
Eric Laurentca7cc822012-11-19 14:55:58 -0800873 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800874 // TODO: handle configuration of input (record) SW effects above the HAL,
875 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
876 // in which case input channel masks should be used here.
Andy Hungfda44002021-06-03 17:23:16 -0700877 callback = getCallback();
878 channelMask = callback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800879 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700880 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800881
882 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800883 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
884 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
885 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
886 mConfig.inputCfg.channels);
887 }
888#ifndef MULTICHANNEL_EFFECT_CHAIN
889 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
890 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
891 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
892 mConfig.outputCfg.channels);
893 }
894#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800895 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800896#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700897 // TODO: Update this logic when multichannel effects are implemented.
898 // For offloaded tracks consider mono output as stereo for proper effect initialization
899 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
900 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
901 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
902 ALOGV("Overriding effect input and output as STEREO");
903 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800904#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800905 }
jiabineb3bda02020-06-30 14:07:03 -0700906 if (isHapticGenerator()) {
Andy Hungfda44002021-06-03 17:23:16 -0700907 audio_channel_mask_t hapticChannelMask = callback->hapticChannelMask();
jiabineb3bda02020-06-30 14:07:03 -0700908 mConfig.inputCfg.channels |= hapticChannelMask;
909 mConfig.outputCfg.channels |= hapticChannelMask;
910 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800911 mInChannelCountRequested =
912 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
913 mOutChannelCountRequested =
914 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700915
rago94a1ee82017-07-21 15:11:02 -0700916 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
917 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900918
919 // Don't use sample rate for thread if effect isn't offloadable.
Andy Hungfda44002021-06-03 17:23:16 -0700920 if (callback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900921 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
922 ALOGV("Overriding effect input as 48kHz");
923 } else {
Andy Hungfda44002021-06-03 17:23:16 -0700924 mConfig.inputCfg.samplingRate = callback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900925 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800926 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
927 mConfig.inputCfg.bufferProvider.cookie = NULL;
928 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
929 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
930 mConfig.outputCfg.bufferProvider.cookie = NULL;
931 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
932 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
933 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
934 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800935 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800936 // always overwrites output buffer: input buffer == output buffer
937 // - in other sessions:
938 // last effect in the chain accumulates in output buffer: input buffer != output buffer
939 // other effect: overwrites output buffer: input buffer == output buffer
940 // Auxiliary effect:
941 // accumulates in output buffer: input buffer != output buffer
942 // Therefore: accumulate <=> input buffer != output buffer
943 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
944 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
945 } else {
946 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
947 }
948 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
949 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Andy Hungfda44002021-06-03 17:23:16 -0700950 mConfig.inputCfg.buffer.frameCount = callback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800951 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
952
Eric Laurent6b446ce2019-12-13 10:56:31 -0800953 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Andy Hungfda44002021-06-03 17:23:16 -0700954 this, callback->chain().promote().get(),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800955 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800956
957 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700958 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700959 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800960 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700961 &mConfig,
962 &size,
963 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700964 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800965 status = cmdStatus;
966 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800967
968#ifdef MULTICHANNEL_EFFECT_CHAIN
969 if (status != NO_ERROR &&
Andy Hungfda44002021-06-03 17:23:16 -0700970 callback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800971 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
972 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
973 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700974 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
975 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800976 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
977 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
978 }
979 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
980 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
981 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
982 }
983 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700984 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800985 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700986 &mConfig,
987 &size,
988 &cmdStatus);
989 if (status == NO_ERROR) {
990 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800991 }
992 }
993#endif
994
995#ifdef FLOAT_EFFECT_CHAIN
996 if (status == NO_ERROR) {
997 mSupportsFloat = true;
998 }
999
1000 if (status != NO_ERROR) {
1001 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1002 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1003 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1004 size = sizeof(int);
1005 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1006 sizeof(mConfig),
1007 &mConfig,
1008 &size,
1009 &cmdStatus);
1010 if (status == NO_ERROR) {
1011 status = cmdStatus;
1012 }
1013 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001014 mSupportsFloat = false;
1015 ALOGVV("config worked with 16 bit");
1016 } else {
1017 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001018 }
rago94a1ee82017-07-21 15:11:02 -07001019 }
1020#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001021
rago94a1ee82017-07-21 15:11:02 -07001022 if (status == NO_ERROR) {
1023 // Establish Buffer strategy
1024 setInBuffer(mInBuffer);
1025 setOutBuffer(mOutBuffer);
1026
1027 // Update visualizer latency
1028 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1029 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1030 effect_param_t *p = (effect_param_t *)buf32;
1031
1032 p->psize = sizeof(uint32_t);
1033 p->vsize = sizeof(uint32_t);
1034 size = sizeof(int);
1035 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1036
Andy Hungfda44002021-06-03 17:23:16 -07001037 uint32_t latency = callback->latency();
rago94a1ee82017-07-21 15:11:02 -07001038
1039 *((int32_t *)p->data + 1)= latency;
1040 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1041 sizeof(effect_param_t) + 8,
1042 &buf32,
1043 &size,
1044 &cmdStatus);
1045 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001046 }
1047
Andy Hung05083ac2017-12-14 15:00:28 -08001048 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1049 mMaxDisableWaitCnt = (uint32_t)std::max(
1050 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1051 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1052 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001053
Eric Laurentd0ebb532013-04-02 16:41:41 -07001054exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001055 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001056 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001057 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001058 return status;
1059}
1060
1061status_t AudioFlinger::EffectModule::init()
1062{
1063 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001064 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001065 return NO_INIT;
1066 }
1067 status_t cmdStatus;
1068 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001069 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1070 0,
1071 NULL,
1072 &size,
1073 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001074 if (status == 0) {
1075 status = cmdStatus;
1076 }
1077 return status;
1078}
1079
Eric Laurent1b928682014-10-02 19:41:47 -07001080void AudioFlinger::EffectModule::addEffectToHal_l()
1081{
1082 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1083 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001084 if (mAddedToHal) {
1085 return;
1086 }
1087
Andy Hungfda44002021-06-03 17:23:16 -07001088 (void)getCallback()->addEffectToHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001089 mAddedToHal = true;
Eric Laurent1b928682014-10-02 19:41:47 -07001090 }
1091}
1092
Eric Laurentfa1e1232016-08-02 19:01:49 -07001093// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001094status_t AudioFlinger::EffectModule::start()
1095{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001096 status_t status;
1097 {
1098 Mutex::Autolock _l(mLock);
1099 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001100 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001101 if (status == NO_ERROR) {
Andy Hungfda44002021-06-03 17:23:16 -07001102 getCallback()->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001103 }
1104 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001105}
1106
1107status_t AudioFlinger::EffectModule::start_l()
1108{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001109 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001110 return NO_INIT;
1111 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001112 if (mStatus != NO_ERROR) {
1113 return mStatus;
1114 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001115 status_t cmdStatus;
1116 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001117 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1118 0,
1119 NULL,
1120 &size,
1121 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001122 if (status == 0) {
1123 status = cmdStatus;
1124 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001125 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001126 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001127 }
1128 return status;
1129}
1130
1131status_t AudioFlinger::EffectModule::stop()
1132{
1133 Mutex::Autolock _l(mLock);
1134 return stop_l();
1135}
1136
1137status_t AudioFlinger::EffectModule::stop_l()
1138{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001139 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 return NO_INIT;
1141 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001142 if (mStatus != NO_ERROR) {
1143 return mStatus;
1144 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001145 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001147
1148 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001149 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1150 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1151 mSetVolumeReentrantTid = gettid();
Andy Hungfda44002021-06-03 17:23:16 -07001152 getCallback()->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001153 mSetVolumeReentrantTid = INVALID_PID;
1154 }
1155
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001156 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1157 0,
1158 NULL,
1159 &size,
1160 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001161 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001162 status = cmdStatus;
1163 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001164 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001165 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001166 }
1167 return status;
1168}
1169
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001170// must be called with EffectChain::mLock held
1171void AudioFlinger::EffectModule::release_l()
1172{
1173 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001174 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001175 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001176 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001177 mEffectInterface.clear();
1178 }
1179}
1180
Eric Laurent6b446ce2019-12-13 10:56:31 -08001181status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001182{
1183 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1184 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
David Li6c8ac4b2021-06-22 22:17:52 +08001185 if (!mAddedToHal) {
1186 return NO_ERROR;
1187 }
1188
Andy Hungfda44002021-06-03 17:23:16 -07001189 getCallback()->removeEffectFromHal(mEffectInterface);
David Li6c8ac4b2021-06-22 22:17:52 +08001190 mAddedToHal = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08001191 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001192 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001193}
1194
Andy Hunge4a1d912016-08-17 14:11:13 -07001195// round up delta valid if value and divisor are positive.
1196template <typename T>
1197static T roundUpDelta(const T &value, const T &divisor) {
1198 T remainder = value % divisor;
1199 return remainder == 0 ? 0 : divisor - remainder;
1200}
1201
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001202status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1203 const std::vector<uint8_t>& cmdData,
1204 int32_t maxReplySize,
1205 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001206{
1207 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001208 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001209
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001210 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001211 return NO_INIT;
1212 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001213 if (mStatus != NO_ERROR) {
1214 return mStatus;
1215 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001216 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1217 return -EINVAL;
1218 }
1219 size_t cmdSize = cmdData.size();
1220 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1221 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1222 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001223 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001224 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001225 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001226 android_errorWriteLog(0x534e4554, "33003822");
1227 return -EINVAL;
1228 }
1229 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001230 (maxReplySize < sizeof(effect_param_t) ||
1231 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001232 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001233 return -EINVAL;
1234 }
ragoe2759072016-11-22 18:02:48 -08001235 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001236 (sizeof(effect_param_t) > maxReplySize
1237 || param->psize > maxReplySize - sizeof(effect_param_t)
1238 || param->vsize > maxReplySize - sizeof(effect_param_t)
1239 - param->psize
1240 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1241 maxReplySize
1242 - sizeof(effect_param_t)
1243 - param->psize
1244 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001245 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1246 android_errorWriteLog(0x534e4554, "32705438");
1247 return -EINVAL;
1248 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001249 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001250 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1251 && // DEFERRED not generally used
1252 (param == nullptr
1253 || param->psize > cmdSize - sizeof(effect_param_t)
1254 || param->vsize > cmdSize - sizeof(effect_param_t)
1255 - param->psize
1256 || roundUpDelta(param->psize,
1257 (uint32_t) sizeof(int)) >
1258 cmdSize
1259 - sizeof(effect_param_t)
1260 - param->psize
1261 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001262 android_errorWriteLog(0x534e4554, "30204301");
1263 return -EINVAL;
1264 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001265 uint32_t replySize = maxReplySize;
1266 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001267 status_t status = mEffectInterface->command(cmdCode,
1268 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001269 const_cast<uint8_t*>(cmdData.data()),
1270 &replySize,
1271 reply->data());
1272 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001273 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001274 for (size_t i = 1; i < mHandles.size(); i++) {
1275 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001276 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001277 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001278 }
1279 }
1280 }
1281 return status;
1282}
1283
Eric Laurentca7cc822012-11-19 14:55:58 -08001284bool AudioFlinger::EffectModule::isProcessEnabled() const
1285{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001286 if (mStatus != NO_ERROR) {
1287 return false;
1288 }
1289
Eric Laurentca7cc822012-11-19 14:55:58 -08001290 switch (mState) {
1291 case RESTART:
1292 case ACTIVE:
1293 case STOPPING:
1294 case STOPPED:
1295 return true;
1296 case IDLE:
1297 case STARTING:
1298 case DESTROYED:
1299 default:
1300 return false;
1301 }
1302}
1303
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001304bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1305{
Andy Hungfda44002021-06-03 17:23:16 -07001306 return getCallback()->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001307}
1308
1309bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1310{
1311 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1312}
1313
Mikhail Naganov022b9952017-01-04 16:36:51 -08001314void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001315 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001316
1317 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001318 if (buffer != 0) {
1319 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1320 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1321 } else {
1322 mConfig.inputCfg.buffer.raw = NULL;
1323 }
1324 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001325 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001326
1327#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001328 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001329 // Theoretically insert effects can also do in-place conversions (destroying
1330 // the original buffer) when the output buffer is identical to the input buffer,
1331 // but we don't optimize for it here.
1332 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001333 const uint32_t inChannelCount =
1334 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1335 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001336 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001337 // we need to translate - create hidl shared buffer and intercept
1338 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001339 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1340 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1341 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001342
1343 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1344 __func__, inChannels, inFrameCount, size);
1345
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001346 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001347 || size > mInConversionBuffer->getSize())) {
1348 mInConversionBuffer.clear();
1349 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001350 (void)getCallback()->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001351 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001352 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001353 mInConversionBuffer->setFrameCount(inFrameCount);
1354 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001355 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001356 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001357 }
1358 }
1359#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001360}
1361
1362void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001363 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001364
1365 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001366 if (buffer != 0) {
1367 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1368 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1369 } else {
1370 mConfig.outputCfg.buffer.raw = NULL;
1371 }
1372 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001373 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001374
1375#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001376 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001377 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001378 const uint32_t outChannelCount =
1379 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1380 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001381 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001382 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001383 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1384 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1385 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001386
1387 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1388 __func__, outChannels, outFrameCount, size);
1389
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001390 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001391 || size > mOutConversionBuffer->getSize())) {
1392 mOutConversionBuffer.clear();
1393 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Andy Hungfda44002021-06-03 17:23:16 -07001394 (void)getCallback()->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001395 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001396 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001397 mOutConversionBuffer->setFrameCount(outFrameCount);
1398 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001399 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001400 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001401 }
1402 }
1403#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001404}
1405
Eric Laurentca7cc822012-11-19 14:55:58 -08001406status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1407{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001408 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001409 if (mStatus != NO_ERROR) {
1410 return mStatus;
1411 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001412 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001413 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1414 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1415 if (isProcessEnabled() &&
1416 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001417 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1418 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001419 uint32_t volume[2];
1420 uint32_t *pVolume = NULL;
1421 uint32_t size = sizeof(volume);
1422 volume[0] = *left;
1423 volume[1] = *right;
1424 if (controller) {
1425 pVolume = volume;
1426 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001427 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1428 size,
1429 volume,
1430 &size,
1431 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001432 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1433 *left = volume[0];
1434 *right = volume[1];
1435 }
1436 }
1437 return status;
1438}
1439
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001440void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1441{
Zhou Songd505c642020-02-20 16:35:37 +08001442 // for offload or direct thread, if the effect chain has non-offloadable
1443 // effect and any effect module within the chain has volume control, then
1444 // volume control is delegated to effect, otherwise, set volume to hal.
1445 if (mEffectCallback->isOffloadOrDirect() &&
1446 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001447 float vol_l = (float)left / (1 << 24);
1448 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001449 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001450 }
1451}
1452
jiabin8f278ee2019-11-11 12:16:27 -08001453status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1454 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001455{
jiabin8f278ee2019-11-11 12:16:27 -08001456 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1457 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001458 return NO_ERROR;
1459 }
1460
1461 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001462 if (mStatus != NO_ERROR) {
1463 return mStatus;
1464 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001465 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001466 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001467 status_t cmdStatus;
1468 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001469 // FIXME: use audio device types and addresses when the hal interface is ready.
1470 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001471 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001472 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001473 &size,
1474 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001475 }
1476 return status;
1477}
1478
jiabin8f278ee2019-11-11 12:16:27 -08001479status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1480{
1481 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1482}
1483
1484status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1485{
1486 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1487}
1488
Eric Laurentca7cc822012-11-19 14:55:58 -08001489status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
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_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1497 status_t cmdStatus;
1498 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001499 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1500 sizeof(audio_mode_t),
1501 &mode,
1502 &size,
1503 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 if (status == NO_ERROR) {
1505 status = cmdStatus;
1506 }
1507 }
1508 return status;
1509}
1510
1511status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1512{
1513 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001514 if (mStatus != NO_ERROR) {
1515 return mStatus;
1516 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001517 status_t status = NO_ERROR;
1518 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1519 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001520 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1521 sizeof(audio_source_t),
1522 &source,
1523 &size,
1524 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001525 }
1526 return status;
1527}
1528
Eric Laurent5baf2af2013-09-12 17:37:00 -07001529status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1530{
1531 Mutex::Autolock _l(mLock);
1532 if (mStatus != NO_ERROR) {
1533 return mStatus;
1534 }
1535 status_t status = NO_ERROR;
1536 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1537 status_t cmdStatus;
1538 uint32_t size = sizeof(status_t);
1539 effect_offload_param_t cmd;
1540
1541 cmd.isOffload = offloaded;
1542 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001543 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1544 sizeof(effect_offload_param_t),
1545 &cmd,
1546 &size,
1547 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001548 if (status == NO_ERROR) {
1549 status = cmdStatus;
1550 }
1551 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1552 } else {
1553 if (offloaded) {
1554 status = INVALID_OPERATION;
1555 }
1556 mOffloaded = false;
1557 }
1558 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1559 return status;
1560}
1561
1562bool AudioFlinger::EffectModule::isOffloaded() const
1563{
1564 Mutex::Autolock _l(mLock);
1565 return mOffloaded;
1566}
1567
jiabineb3bda02020-06-30 14:07:03 -07001568/*static*/
1569bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1570 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1571}
1572
1573bool AudioFlinger::EffectModule::isHapticGenerator() const {
1574 return isHapticGenerator(&mDescriptor.type);
1575}
1576
jiabine70bc7f2020-06-30 22:07:55 -07001577status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1578{
1579 if (mStatus != NO_ERROR) {
1580 return mStatus;
1581 }
1582 if (!isHapticGenerator()) {
1583 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1584 return INVALID_OPERATION;
1585 }
1586
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001587 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1588 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001589 param->psize = sizeof(int32_t);
1590 param->vsize = sizeof(int32_t) * 2;
1591 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1592 *((int32_t*)param->data + 1) = id;
1593 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001594 std::vector<uint8_t> response;
1595 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001596 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001597 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1598 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001599 }
1600 return status;
1601}
1602
jiabin1319f5a2021-03-30 22:21:24 +00001603status_t AudioFlinger::EffectModule::setVibratorInfo(const media::AudioVibratorInfo* vibratorInfo)
1604{
1605 if (mStatus != NO_ERROR) {
1606 return mStatus;
1607 }
1608 if (!isHapticGenerator()) {
1609 ALOGW("Should not set vibrator info for effects that are not HapticGenerator");
1610 return INVALID_OPERATION;
1611 }
1612
1613 std::vector<uint8_t> request(
1614 sizeof(effect_param_t) + sizeof(int32_t) + 2 * sizeof(float));
1615 effect_param_t *param = (effect_param_t*) request.data();
1616 param->psize = sizeof(int32_t);
1617 param->vsize = 2 * sizeof(float);
1618 *(int32_t*)param->data = HG_PARAM_VIBRATOR_INFO;
1619 float* vibratorInfoPtr = reinterpret_cast<float*>(param->data + sizeof(int32_t));
1620 vibratorInfoPtr[0] = vibratorInfo->resonantFrequency;
1621 vibratorInfoPtr[1] = vibratorInfo->qFactor;
1622 std::vector<uint8_t> response;
1623 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
1624 if (status == NO_ERROR) {
1625 LOG_ALWAYS_FATAL_IF(response.size() != sizeof(status_t));
1626 status = *reinterpret_cast<const status_t*>(response.data());
1627 }
1628 return status;
1629}
1630
Andy Hungbded9c82017-11-30 18:47:35 -08001631static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1632 std::stringstream ss;
1633
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001634 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001635 return "nullptr"; // make different than below
1636 } else if (buffer->externalData() != nullptr) {
1637 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1638 << " -> "
1639 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1640 } else {
1641 ss << buffer->audioBuffer()->raw;
1642 }
1643 return ss.str();
1644}
Marco Nelissenb2208842014-02-07 14:00:50 -08001645
Eric Laurent41709552019-12-16 19:34:05 -08001646void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001647{
Eric Laurent41709552019-12-16 19:34:05 -08001648 EffectBase::dump(fd, args);
1649
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001651 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001652
Eric Laurent41709552019-12-16 19:34:05 -08001653 result.append("\t\tStatus Engine:\n");
1654 result.appendFormat("\t\t%03d %p\n",
1655 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001656
1657 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001658
1659 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001660 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1661 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1662 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 mConfig.inputCfg.buffer.frameCount,
1664 mConfig.inputCfg.samplingRate,
1665 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001666 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001667 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001668
1669 result.append("\t\t- Output configuration:\n");
1670 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001671 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001672 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001673 mConfig.outputCfg.buffer.frameCount,
1674 mConfig.outputCfg.samplingRate,
1675 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001676 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001677 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001678
rago94a1ee82017-07-21 15:11:02 -07001679#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001680
Andy Hungbded9c82017-11-30 18:47:35 -08001681 result.appendFormat("\t\t- HAL buffers:\n"
1682 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1683 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1684 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1685 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1686 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001687#endif
1688
Eric Laurentca7cc822012-11-19 14:55:58 -08001689 write(fd, result.string(), result.length());
1690
Mikhail Naganov4d547672019-02-22 14:19:19 -08001691 if (mEffectInterface != 0) {
1692 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1693 (void)mEffectInterface->dump(fd);
1694 }
1695
Eric Laurentca7cc822012-11-19 14:55:58 -08001696 if (locked) {
1697 mLock.unlock();
1698 }
1699}
1700
1701// ----------------------------------------------------------------------------
1702// EffectHandle implementation
1703// ----------------------------------------------------------------------------
1704
1705#undef LOG_TAG
1706#define LOG_TAG "AudioFlinger::EffectHandle"
1707
Eric Laurent41709552019-12-16 19:34:05 -08001708AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001709 const sp<AudioFlinger::Client>& client,
1710 const sp<media::IEffectClient>& effectClient,
1711 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001712 : BnEffect(),
1713 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001714 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001715{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001716 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001717
1718 if (client == 0) {
1719 return;
1720 }
1721 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1722 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001723 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001724 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001725 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001726 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001727 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 return;
1729 }
Glenn Kastene75da402013-11-20 13:54:52 -08001730 new(mCblk) effect_param_cblk_t();
1731 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001732}
1733
1734AudioFlinger::EffectHandle::~EffectHandle()
1735{
1736 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001737 disconnect(false);
1738}
1739
Glenn Kastene75da402013-11-20 13:54:52 -08001740status_t AudioFlinger::EffectHandle::initCheck()
1741{
1742 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1743}
1744
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001745#define RETURN(code) \
1746 *_aidl_return = (code); \
1747 return Status::ok();
1748
1749Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001750{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001751 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001753 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001754 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001755 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001756 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001757 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001758 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001760
1761 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001762 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001763 }
1764
1765 mEnabled = true;
1766
Eric Laurent6c796322019-04-09 14:13:17 -07001767 status_t status = effect->updatePolicyState();
1768 if (status != NO_ERROR) {
1769 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001770 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001771 }
1772
Eric Laurent6b446ce2019-12-13 10:56:31 -08001773 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001774
1775 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001776 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001777 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001778 }
1779
Eric Laurent6b446ce2019-12-13 10:56:31 -08001780 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001781 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001782 mEnabled = false;
1783 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001784 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001785}
1786
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001787Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001788{
1789 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001790 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001791 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001793 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001794 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001795 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001796 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001797 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001798
1799 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001800 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001801 }
1802 mEnabled = false;
1803
Eric Laurent6c796322019-04-09 14:13:17 -07001804 effect->updatePolicyState();
1805
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001806 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001807 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 }
1809
Eric Laurent6b446ce2019-12-13 10:56:31 -08001810 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001811 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001812}
1813
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001814Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001815{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001816 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001818 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001819}
1820
1821void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1822{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001823 AutoMutex _l(mLock);
1824 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1825 if (mDisconnected) {
1826 if (unpinIfLast) {
1827 android_errorWriteLog(0x534e4554, "32707507");
1828 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 return;
1830 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001831 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001832 {
Eric Laurent41709552019-12-16 19:34:05 -08001833 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001834 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001835 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001836 ALOGW("%s Effect handle %p disconnected after thread destruction",
1837 __func__, this);
1838 }
1839 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001840 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001841 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001842
Eric Laurentca7cc822012-11-19 14:55:58 -08001843 if (mClient != 0) {
1844 if (mCblk != NULL) {
1845 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1846 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1847 }
1848 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001849 // Client destructor must run with AudioFlinger client mutex locked
1850 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001851 mClient.clear();
1852 }
1853}
1854
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001855Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1856 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1857 return Status::ok();
1858}
1859
1860Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1861 const std::vector<uint8_t>& cmdData,
1862 int32_t maxResponseSize,
1863 std::vector<uint8_t>* response,
1864 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001865{
1866 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001867 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001868
Eric Laurentc7ab3092017-06-15 18:43:46 -07001869 // reject commands reserved for internal use by audio framework if coming from outside
1870 // of audioserver
1871 switch(cmdCode) {
1872 case EFFECT_CMD_ENABLE:
1873 case EFFECT_CMD_DISABLE:
1874 case EFFECT_CMD_SET_PARAM:
1875 case EFFECT_CMD_SET_PARAM_DEFERRED:
1876 case EFFECT_CMD_SET_PARAM_COMMIT:
1877 case EFFECT_CMD_GET_PARAM:
1878 break;
1879 default:
1880 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1881 break;
1882 }
1883 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001884 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001885 }
1886
Eric Laurent1ffc5852016-12-15 14:46:09 -08001887 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001888 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001889 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001891 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001892 writeToBuffer(NO_ERROR, response);
1893 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001894 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001895 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001896 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001898 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001899 writeToBuffer(NO_ERROR, response);
1900 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001901 }
1902
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001903 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001904 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001905 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001907 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001908 // only get parameter command is permitted for applications not controlling the effect
1909 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001910 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001911 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001912
1913 // handle commands that are not forwarded transparently to effect engine
1914 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001915 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001916 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001917 }
1918
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001919 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001920 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001921 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001922 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001923 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001924
Eric Laurentca7cc822012-11-19 14:55:58 -08001925 // No need to trylock() here as this function is executed in the binder thread serving a
1926 // particular client process: no risk to block the whole media server process or mixer
1927 // threads if we are stuck here
1928 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001929 // keep local copy of index in case of client corruption b/32220769
1930 const uint32_t clientIndex = mCblk->clientIndex;
1931 const uint32_t serverIndex = mCblk->serverIndex;
1932 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1933 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001934 mCblk->serverIndex = 0;
1935 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001936 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001937 }
1938 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001939 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001940 for (uint32_t index = serverIndex; index < clientIndex;) {
1941 int *p = (int *)(mBuffer + index);
1942 const int size = *p++;
1943 if (size < 0
1944 || size > EFFECT_PARAM_BUFFER_SIZE
1945 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001946 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001947 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 break;
1949 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001950
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001951 std::copy(reinterpret_cast<const uint8_t*>(p),
1952 reinterpret_cast<const uint8_t*>(p) + size,
1953 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001954
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001955 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001956 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001957 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001958 sizeof(int),
1959 &replyBuffer);
1960 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001961
1962 // verify shared memory: server index shouldn't change; client index can't go back.
1963 if (serverIndex != mCblk->serverIndex
1964 || clientIndex > mCblk->clientIndex) {
1965 android_errorWriteLog(0x534e4554, "32220769");
1966 status = BAD_VALUE;
1967 break;
1968 }
1969
Eric Laurentca7cc822012-11-19 14:55:58 -08001970 // stop at first error encountered
1971 if (ret != NO_ERROR) {
1972 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001973 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001974 break;
1975 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001976 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001977 break;
1978 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001979 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001980 }
1981 mCblk->serverIndex = 0;
1982 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001983 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001984 }
1985
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001986 status_t status = effect->command(cmdCode,
1987 cmdData,
1988 maxResponseSize,
1989 response);
1990 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001991}
1992
1993void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1994{
1995 ALOGV("setControl %p control %d", this, hasControl);
1996
1997 mHasControl = hasControl;
1998 mEnabled = enabled;
1999
2000 if (signal && mEffectClient != 0) {
2001 mEffectClient->controlStatusChanged(hasControl);
2002 }
2003}
2004
2005void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002006 const std::vector<uint8_t>& cmdData,
2007 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08002008{
2009 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07002010 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08002011 }
2012}
2013
2014
2015
2016void AudioFlinger::EffectHandle::setEnabled(bool enabled)
2017{
2018 if (mEffectClient != 0) {
2019 mEffectClient->enableStatusChanged(enabled);
2020 }
2021}
2022
Glenn Kasten01d3acb2014-02-06 08:24:07 -08002023void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08002024{
2025 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
2026
Marco Nelissenb2208842014-02-07 14:00:50 -08002027 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07002028 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002029 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08002030 mHasControl ? "yes" : "no",
2031 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08002032 mCblk ? mCblk->clientIndex : 0,
2033 mCblk ? mCblk->serverIndex : 0
2034 );
2035
2036 if (locked) {
2037 mCblk->lock.unlock();
2038 }
2039}
2040
2041#undef LOG_TAG
2042#define LOG_TAG "AudioFlinger::EffectChain"
2043
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002044AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2045 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002046 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002047 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002048 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002049 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002050{
2051 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002052 sp<ThreadBase> p = thread.promote();
2053 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002054 return;
2055 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002056 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2057 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002058}
2059
2060AudioFlinger::EffectChain::~EffectChain()
2061{
Eric Laurentca7cc822012-11-19 14:55:58 -08002062}
2063
2064// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2065sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2066 effect_descriptor_t *descriptor)
2067{
2068 size_t size = mEffects.size();
2069
2070 for (size_t i = 0; i < size; i++) {
2071 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2072 return mEffects[i];
2073 }
2074 }
2075 return 0;
2076}
2077
2078// getEffectFromId_l() must be called with ThreadBase::mLock held
2079sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2080{
2081 size_t size = mEffects.size();
2082
2083 for (size_t i = 0; i < size; i++) {
2084 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2085 if (id == 0 || mEffects[i]->id() == id) {
2086 return mEffects[i];
2087 }
2088 }
2089 return 0;
2090}
2091
2092// getEffectFromType_l() must be called with ThreadBase::mLock held
2093sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2094 const effect_uuid_t *type)
2095{
2096 size_t size = mEffects.size();
2097
2098 for (size_t i = 0; i < size; i++) {
2099 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2100 return mEffects[i];
2101 }
2102 }
2103 return 0;
2104}
2105
Eric Laurent6c796322019-04-09 14:13:17 -07002106std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2107{
2108 std::vector<int> ids;
2109 Mutex::Autolock _l(mLock);
2110 for (size_t i = 0; i < mEffects.size(); i++) {
2111 ids.push_back(mEffects[i]->id());
2112 }
2113 return ids;
2114}
2115
Eric Laurentca7cc822012-11-19 14:55:58 -08002116void AudioFlinger::EffectChain::clearInputBuffer()
2117{
2118 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002119 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002120}
2121
2122// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002123void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002124{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002125 if (mInBuffer == NULL) {
2126 return;
2127 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002128 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002129 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002130
Eric Laurent6b446ce2019-12-13 10:56:31 -08002131 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002132 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002133}
2134
2135// Must be called with EffectChain::mLock locked
2136void AudioFlinger::EffectChain::process_l()
2137{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002138 // never process effects when:
2139 // - on an OFFLOAD thread
2140 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002141 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002142 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002143 bool tracksOnSession = (trackCnt() != 0);
2144
2145 if (!tracksOnSession && mTailBufferCount == 0) {
2146 doProcess = false;
2147 }
2148
2149 if (activeTrackCnt() == 0) {
2150 // if no track is active and the effect tail has not been rendered,
2151 // the input buffer must be cleared here as the mixer process will not do it
2152 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002153 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002154 if (mTailBufferCount > 0) {
2155 mTailBufferCount--;
2156 }
2157 }
2158 }
2159 }
2160
2161 size_t size = mEffects.size();
2162 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002163 // Only the input and output buffers of the chain can be external,
2164 // and 'update' / 'commit' do nothing for allocated buffers, thus
2165 // it's not needed to consider any other buffers here.
2166 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002167 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2168 mOutBuffer->update();
2169 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002170 for (size_t i = 0; i < size; i++) {
2171 mEffects[i]->process();
2172 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002173 mInBuffer->commit();
2174 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2175 mOutBuffer->commit();
2176 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002177 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002178 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002179 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002180 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2181 }
2182 if (doResetVolume) {
2183 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002184 }
2185}
2186
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002187// createEffect_l() must be called with ThreadBase::mLock held
2188status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002189 effect_descriptor_t *desc,
2190 int id,
2191 audio_session_t sessionId,
2192 bool pinned)
2193{
2194 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002195 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002196 status_t lStatus = effect->status();
2197 if (lStatus == NO_ERROR) {
2198 lStatus = addEffect_ll(effect);
2199 }
2200 if (lStatus != NO_ERROR) {
2201 effect.clear();
2202 }
2203 return lStatus;
2204}
2205
2206// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002207status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2208{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002209 Mutex::Autolock _l(mLock);
2210 return addEffect_ll(effect);
2211}
2212// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2213status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2214{
Eric Laurentca7cc822012-11-19 14:55:58 -08002215 effect_descriptor_t desc = effect->desc();
2216 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2217
Eric Laurent6b446ce2019-12-13 10:56:31 -08002218 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002219
2220 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2221 // Auxiliary effects are inserted at the beginning of mEffects vector as
2222 // they are processed first and accumulated in chain input buffer
2223 mEffects.insertAt(effect, 0);
2224
2225 // the input buffer for auxiliary effect contains mono samples in
2226 // 32 bit format. This is to avoid saturation in AudoMixer
2227 // accumulation stage. Saturation is done in EffectModule::process() before
2228 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002229 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002230 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002231#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002232 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002233 numSamples * sizeof(float), &halBuffer);
2234#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002235 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002236 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002237#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002238 if (result != OK) return result;
2239 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002240 // auxiliary effects output samples to chain input buffer for further processing
2241 // by insert effects
2242 effect->setOutBuffer(mInBuffer);
2243 } else {
2244 // Insert effects are inserted at the end of mEffects vector as they are processed
2245 // after track and auxiliary effects.
2246 // Insert effect order as a function of indicated preference:
2247 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2248 // another effect is present
2249 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2250 // last effect claiming first position
2251 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2252 // first effect claiming last position
2253 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2254 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2255 // already present
2256
2257 size_t size = mEffects.size();
2258 size_t idx_insert = size;
2259 ssize_t idx_insert_first = -1;
2260 ssize_t idx_insert_last = -1;
2261
2262 for (size_t i = 0; i < size; i++) {
2263 effect_descriptor_t d = mEffects[i]->desc();
2264 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2265 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2266 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2267 // check invalid effect chaining combinations
2268 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2269 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2270 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2271 desc.name, d.name);
2272 return INVALID_OPERATION;
2273 }
2274 // remember position of first insert effect and by default
2275 // select this as insert position for new effect
2276 if (idx_insert == size) {
2277 idx_insert = i;
2278 }
2279 // remember position of last insert effect claiming
2280 // first position
2281 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2282 idx_insert_first = i;
2283 }
2284 // remember position of first insert effect claiming
2285 // last position
2286 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2287 idx_insert_last == -1) {
2288 idx_insert_last = i;
2289 }
2290 }
2291 }
2292
2293 // modify idx_insert from first position if needed
2294 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2295 if (idx_insert_last != -1) {
2296 idx_insert = idx_insert_last;
2297 } else {
2298 idx_insert = size;
2299 }
2300 } else {
2301 if (idx_insert_first != -1) {
2302 idx_insert = idx_insert_first + 1;
2303 }
2304 }
2305
2306 // always read samples from chain input buffer
2307 effect->setInBuffer(mInBuffer);
2308
2309 // if last effect in the chain, output samples to chain
2310 // output buffer, otherwise to chain input buffer
2311 if (idx_insert == size) {
2312 if (idx_insert != 0) {
2313 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2314 mEffects[idx_insert-1]->configure();
2315 }
2316 effect->setOutBuffer(mOutBuffer);
2317 } else {
2318 effect->setOutBuffer(mInBuffer);
2319 }
2320 mEffects.insertAt(effect, idx_insert);
2321
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002322 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002323 idx_insert);
2324 }
2325 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002326
Eric Laurentca7cc822012-11-19 14:55:58 -08002327 return NO_ERROR;
2328}
2329
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002330// removeEffect_l() must be called with ThreadBase::mLock held
2331size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2332 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002333{
2334 Mutex::Autolock _l(mLock);
2335 size_t size = mEffects.size();
2336 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2337
2338 for (size_t i = 0; i < size; i++) {
2339 if (effect == mEffects[i]) {
2340 // calling stop here will remove pre-processing effect from the audio HAL.
2341 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2342 // the middle of a read from audio HAL
2343 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2344 mEffects[i]->state() == EffectModule::STOPPING) {
2345 mEffects[i]->stop();
2346 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002347 if (release) {
2348 mEffects[i]->release_l();
2349 }
2350
Mikhail Naganov022b9952017-01-04 16:36:51 -08002351 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002352 if (i == size - 1 && i != 0) {
2353 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2354 mEffects[i - 1]->configure();
2355 }
2356 }
2357 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002358 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002359 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002360
Eric Laurentca7cc822012-11-19 14:55:58 -08002361 break;
2362 }
2363 }
2364
2365 return mEffects.size();
2366}
2367
jiabin8f278ee2019-11-11 12:16:27 -08002368// setDevices_l() must be called with ThreadBase::mLock held
2369void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002370{
2371 size_t size = mEffects.size();
2372 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002373 mEffects[i]->setDevices(devices);
2374 }
2375}
2376
2377// setInputDevice_l() must be called with ThreadBase::mLock held
2378void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2379{
2380 size_t size = mEffects.size();
2381 for (size_t i = 0; i < size; i++) {
2382 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002383 }
2384}
2385
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002386// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002387void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2388{
2389 size_t size = mEffects.size();
2390 for (size_t i = 0; i < size; i++) {
2391 mEffects[i]->setMode(mode);
2392 }
2393}
2394
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002395// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002396void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2397{
2398 size_t size = mEffects.size();
2399 for (size_t i = 0; i < size; i++) {
2400 mEffects[i]->setAudioSource(source);
2401 }
2402}
2403
Zhou Songd505c642020-02-20 16:35:37 +08002404bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2405 for (const auto &effect : mEffects) {
2406 if (effect->isVolumeControlEnabled()) return true;
2407 }
2408 return false;
2409}
2410
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002411// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002412bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002413{
2414 uint32_t newLeft = *left;
2415 uint32_t newRight = *right;
2416 bool hasControl = false;
2417 int ctrlIdx = -1;
2418 size_t size = mEffects.size();
2419
2420 // first update volume controller
2421 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002422 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002423 ctrlIdx = i - 1;
2424 hasControl = true;
2425 break;
2426 }
2427 }
2428
Eric Laurentfa1e1232016-08-02 19:01:49 -07002429 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002430 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002431 if (hasControl) {
2432 *left = mNewLeftVolume;
2433 *right = mNewRightVolume;
2434 }
2435 return hasControl;
2436 }
2437
2438 mVolumeCtrlIdx = ctrlIdx;
2439 mLeftVolume = newLeft;
2440 mRightVolume = newRight;
2441
2442 // second get volume update from volume controller
2443 if (ctrlIdx >= 0) {
2444 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2445 mNewLeftVolume = newLeft;
2446 mNewRightVolume = newRight;
2447 }
2448 // then indicate volume to all other effects in chain.
2449 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002450 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002451 uint32_t lVol = newLeft;
2452 uint32_t rVol = newRight;
2453
2454 for (size_t i = 0; i < size; i++) {
2455 if ((int)i == ctrlIdx) {
2456 continue;
2457 }
2458 // this also works for ctrlIdx == -1 when there is no volume controller
2459 if ((int)i > ctrlIdx) {
2460 lVol = *left;
2461 rVol = *right;
2462 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002463 // Pass requested volume directly if this is volume monitor module
2464 if (mEffects[i]->isVolumeMonitor()) {
2465 mEffects[i]->setVolume(left, right, false);
2466 } else {
2467 mEffects[i]->setVolume(&lVol, &rVol, false);
2468 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002469 }
2470 *left = newLeft;
2471 *right = newRight;
2472
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002473 setVolumeForOutput_l(*left, *right);
2474
Eric Laurentca7cc822012-11-19 14:55:58 -08002475 return hasControl;
2476}
2477
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002478// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002479void AudioFlinger::EffectChain::resetVolume_l()
2480{
Eric Laurente7449bf2016-08-03 18:44:07 -07002481 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2482 uint32_t left = mLeftVolume;
2483 uint32_t right = mRightVolume;
2484 (void)setVolume_l(&left, &right, true);
2485 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002486}
2487
jiabineb3bda02020-06-30 14:07:03 -07002488// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2489bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2490{
2491 for (size_t i = 0; i < mEffects.size(); ++i) {
2492 if (mEffects[i]->isHapticGenerator()) {
2493 return true;
2494 }
2495 }
2496 return false;
2497}
2498
jiabine70bc7f2020-06-30 22:07:55 -07002499void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2500{
2501 Mutex::Autolock _l(mLock);
2502 for (size_t i = 0; i < mEffects.size(); ++i) {
2503 mEffects[i]->setHapticIntensity(id, intensity);
2504 }
2505}
2506
Eric Laurent1b928682014-10-02 19:41:47 -07002507void AudioFlinger::EffectChain::syncHalEffectsState()
2508{
2509 Mutex::Autolock _l(mLock);
2510 for (size_t i = 0; i < mEffects.size(); i++) {
2511 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2512 mEffects[i]->state() == EffectModule::STOPPING) {
2513 mEffects[i]->addEffectToHal_l();
2514 }
2515 }
2516}
2517
Eric Laurentca7cc822012-11-19 14:55:58 -08002518void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2519{
Eric Laurentca7cc822012-11-19 14:55:58 -08002520 String8 result;
2521
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002522 const size_t numEffects = mEffects.size();
2523 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002524
Marco Nelissenb2208842014-02-07 14:00:50 -08002525 if (numEffects) {
2526 bool locked = AudioFlinger::dumpTryLock(mLock);
2527 // failed to lock - AudioFlinger is probably deadlocked
2528 if (!locked) {
2529 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002530 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002531
Andy Hungbded9c82017-11-30 18:47:35 -08002532 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2533 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2534 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2535 (int)inBufferStr.size(), "In buffer ",
2536 (int)outBufferStr.size(), "Out buffer ");
2537 result.appendFormat("\t%s %s %d\n",
2538 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002539 write(fd, result.string(), result.size());
2540
2541 for (size_t i = 0; i < numEffects; ++i) {
2542 sp<EffectModule> effect = mEffects[i];
2543 if (effect != 0) {
2544 effect->dump(fd, args);
2545 }
2546 }
2547
2548 if (locked) {
2549 mLock.unlock();
2550 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002551 } else {
2552 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002553 }
2554}
2555
2556// must be called with ThreadBase::mLock held
2557void AudioFlinger::EffectChain::setEffectSuspended_l(
2558 const effect_uuid_t *type, bool suspend)
2559{
2560 sp<SuspendedEffectDesc> desc;
2561 // use effect type UUID timelow as key as there is no real risk of identical
2562 // timeLow fields among effect type UUIDs.
2563 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2564 if (suspend) {
2565 if (index >= 0) {
2566 desc = mSuspendedEffects.valueAt(index);
2567 } else {
2568 desc = new SuspendedEffectDesc();
2569 desc->mType = *type;
2570 mSuspendedEffects.add(type->timeLow, desc);
2571 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2572 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002573
Eric Laurentca7cc822012-11-19 14:55:58 -08002574 if (desc->mRefCount++ == 0) {
2575 sp<EffectModule> effect = getEffectIfEnabled(type);
2576 if (effect != 0) {
2577 desc->mEffect = effect;
2578 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002579 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002580 }
2581 }
2582 } else {
2583 if (index < 0) {
2584 return;
2585 }
2586 desc = mSuspendedEffects.valueAt(index);
2587 if (desc->mRefCount <= 0) {
2588 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002589 desc->mRefCount = 0;
2590 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002591 }
2592 if (--desc->mRefCount == 0) {
2593 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2594 if (desc->mEffect != 0) {
2595 sp<EffectModule> effect = desc->mEffect.promote();
2596 if (effect != 0) {
2597 effect->setSuspended(false);
2598 effect->lock();
2599 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002600 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002601 effect->setEnabled_l(handle->enabled());
2602 }
2603 effect->unlock();
2604 }
2605 desc->mEffect.clear();
2606 }
2607 mSuspendedEffects.removeItemsAt(index);
2608 }
2609 }
2610}
2611
2612// must be called with ThreadBase::mLock held
2613void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2614{
2615 sp<SuspendedEffectDesc> desc;
2616
2617 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2618 if (suspend) {
2619 if (index >= 0) {
2620 desc = mSuspendedEffects.valueAt(index);
2621 } else {
2622 desc = new SuspendedEffectDesc();
2623 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2624 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2625 }
2626 if (desc->mRefCount++ == 0) {
2627 Vector< sp<EffectModule> > effects;
2628 getSuspendEligibleEffects(effects);
2629 for (size_t i = 0; i < effects.size(); i++) {
2630 setEffectSuspended_l(&effects[i]->desc().type, true);
2631 }
2632 }
2633 } else {
2634 if (index < 0) {
2635 return;
2636 }
2637 desc = mSuspendedEffects.valueAt(index);
2638 if (desc->mRefCount <= 0) {
2639 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2640 desc->mRefCount = 1;
2641 }
2642 if (--desc->mRefCount == 0) {
2643 Vector<const effect_uuid_t *> types;
2644 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2645 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2646 continue;
2647 }
2648 types.add(&mSuspendedEffects.valueAt(i)->mType);
2649 }
2650 for (size_t i = 0; i < types.size(); i++) {
2651 setEffectSuspended_l(types[i], false);
2652 }
2653 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2654 mSuspendedEffects.keyAt(index));
2655 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2656 }
2657 }
2658}
2659
2660
2661// The volume effect is used for automated tests only
2662#ifndef OPENSL_ES_H_
2663static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2664 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2665const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2666#endif //OPENSL_ES_H_
2667
Eric Laurentd8365c52017-07-16 15:27:05 -07002668/* static */
2669bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2670{
2671 // Only NS and AEC are suspended when BtNRec is off
2672 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2673 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2674 return true;
2675 }
2676 return false;
2677}
2678
Eric Laurentca7cc822012-11-19 14:55:58 -08002679bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2680{
2681 // auxiliary effects and visualizer are never suspended on output mix
2682 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2683 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2684 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002685 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2686 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002687 return false;
2688 }
2689 return true;
2690}
2691
2692void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2693 Vector< sp<AudioFlinger::EffectModule> > &effects)
2694{
2695 effects.clear();
2696 for (size_t i = 0; i < mEffects.size(); i++) {
2697 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2698 effects.add(mEffects[i]);
2699 }
2700 }
2701}
2702
2703sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2704 const effect_uuid_t *type)
2705{
2706 sp<EffectModule> effect = getEffectFromType_l(type);
2707 return effect != 0 && effect->isEnabled() ? effect : 0;
2708}
2709
2710void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2711 bool enabled)
2712{
2713 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2714 if (enabled) {
2715 if (index < 0) {
2716 // if the effect is not suspend check if all effects are suspended
2717 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2718 if (index < 0) {
2719 return;
2720 }
2721 if (!isEffectEligibleForSuspend(effect->desc())) {
2722 return;
2723 }
2724 setEffectSuspended_l(&effect->desc().type, enabled);
2725 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2726 if (index < 0) {
2727 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2728 return;
2729 }
2730 }
2731 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2732 effect->desc().type.timeLow);
2733 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002734 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002735 if (desc->mEffect == 0) {
2736 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002737 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002738 effect->setSuspended(true);
2739 }
2740 } else {
2741 if (index < 0) {
2742 return;
2743 }
2744 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2745 effect->desc().type.timeLow);
2746 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2747 desc->mEffect.clear();
2748 effect->setSuspended(false);
2749 }
2750}
2751
Eric Laurent5baf2af2013-09-12 17:37:00 -07002752bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002753{
2754 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002755 return isNonOffloadableEnabled_l();
2756}
2757
2758bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2759{
Eric Laurent813e2a72013-08-31 12:59:48 -07002760 size_t size = mEffects.size();
2761 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002762 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002763 return true;
2764 }
2765 }
2766 return false;
2767}
2768
Eric Laurentaaa44472014-09-12 17:41:50 -07002769void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2770{
2771 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002772 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002773}
2774
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002775void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2776{
2777 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2778 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2779 }
2780 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2781 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2782 }
2783}
2784
2785void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2786{
2787 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2788 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2789 }
2790 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2791 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2792 }
2793}
2794
2795bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002796{
2797 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002798 for (const auto &effect : mEffects) {
2799 if (effect->isProcessImplemented()) {
2800 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002801 }
2802 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002803 // Allow effects without processing.
2804 return true;
2805}
2806
2807bool AudioFlinger::EffectChain::isFastCompatible() const
2808{
2809 Mutex::Autolock _l(mLock);
2810 for (const auto &effect : mEffects) {
2811 if (effect->isProcessImplemented()
2812 && effect->isImplementationSoftware()) {
2813 return false;
2814 }
2815 }
2816 // Allow effects without processing or hw accelerated effects.
2817 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002818}
2819
2820// isCompatibleWithThread_l() must be called with thread->mLock held
2821bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2822{
2823 Mutex::Autolock _l(mLock);
2824 for (size_t i = 0; i < mEffects.size(); i++) {
2825 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2826 return false;
2827 }
2828 }
2829 return true;
2830}
2831
Eric Laurent6b446ce2019-12-13 10:56:31 -08002832// EffectCallbackInterface implementation
2833status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2834 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2835 sp<EffectHalInterface> *effect) {
2836 status_t status = NO_INIT;
Andy Hung6626a012021-01-12 13:38:00 -08002837 sp<EffectsFactoryHalInterface> effectsFactory = mAudioFlinger.getEffectsFactory();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002838 if (effectsFactory != 0) {
2839 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2840 }
2841 return status;
2842}
2843
2844bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002845 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent41709552019-12-16 19:34:05 -08002846 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
Andy Hung6626a012021-01-12 13:38:00 -08002847 return mAudioFlinger.updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002848}
2849
2850status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2851 size_t size, sp<EffectBufferHalInterface>* buffer) {
Andy Hung6626a012021-01-12 13:38:00 -08002852 return mAudioFlinger.mEffectsFactoryHal->allocateBuffer(size, buffer);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002853}
2854
2855status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2856 sp<EffectHalInterface> effect) {
2857 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002858 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002859 if (t == nullptr) {
2860 return result;
2861 }
2862 sp <StreamHalInterface> st = t->stream();
2863 if (st == nullptr) {
2864 return result;
2865 }
2866 result = st->addEffect(effect);
2867 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2868 return result;
2869}
2870
2871status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2872 sp<EffectHalInterface> effect) {
2873 status_t result = NO_INIT;
Andy Hung328d6772021-01-12 12:32:21 -08002874 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002875 if (t == nullptr) {
2876 return result;
2877 }
2878 sp <StreamHalInterface> st = t->stream();
2879 if (st == nullptr) {
2880 return result;
2881 }
2882 result = st->removeEffect(effect);
2883 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2884 return result;
2885}
2886
2887audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
Andy Hung328d6772021-01-12 12:32:21 -08002888 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002889 if (t == nullptr) {
2890 return AUDIO_IO_HANDLE_NONE;
2891 }
2892 return t->id();
2893}
2894
2895bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
Andy Hung328d6772021-01-12 12:32:21 -08002896 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002897 if (t == nullptr) {
2898 return true;
2899 }
2900 return t->isOutput();
2901}
2902
2903bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
Andy Hung328d6772021-01-12 12:32:21 -08002904 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002905 if (t == nullptr) {
2906 return false;
2907 }
2908 return t->type() == ThreadBase::OFFLOAD;
2909}
2910
2911bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
Andy Hung328d6772021-01-12 12:32:21 -08002912 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002913 if (t == nullptr) {
2914 return false;
2915 }
2916 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2917}
2918
2919bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
Andy Hung328d6772021-01-12 12:32:21 -08002920 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002921 if (t == nullptr) {
2922 return false;
2923 }
Andy Hungea840382020-05-05 21:50:17 -07002924 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002925}
2926
2927uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
Andy Hung328d6772021-01-12 12:32:21 -08002928 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002929 if (t == nullptr) {
2930 return 0;
2931 }
2932 return t->sampleRate();
2933}
2934
2935audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002936 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002937 if (t == nullptr) {
2938 return AUDIO_CHANNEL_NONE;
2939 }
2940 return t->channelMask();
2941}
2942
2943uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002944 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002945 if (t == nullptr) {
2946 return 0;
2947 }
2948 return t->channelCount();
2949}
2950
jiabineb3bda02020-06-30 14:07:03 -07002951audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
Andy Hung328d6772021-01-12 12:32:21 -08002952 sp<ThreadBase> t = thread().promote();
jiabineb3bda02020-06-30 14:07:03 -07002953 if (t == nullptr) {
2954 return AUDIO_CHANNEL_NONE;
2955 }
2956 return t->hapticChannelMask();
2957}
2958
Eric Laurent6b446ce2019-12-13 10:56:31 -08002959size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
Andy Hung328d6772021-01-12 12:32:21 -08002960 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002961 if (t == nullptr) {
2962 return 0;
2963 }
2964 return t->frameCount();
2965}
2966
2967uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
Andy Hung328d6772021-01-12 12:32:21 -08002968 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002969 if (t == nullptr) {
2970 return 0;
2971 }
2972 return t->latency_l();
2973}
2974
2975void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
Andy Hung328d6772021-01-12 12:32:21 -08002976 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002977 if (t == nullptr) {
2978 return;
2979 }
2980 t->setVolumeForOutput_l(left, right);
2981}
2982
2983void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002984 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Andy Hung328d6772021-01-12 12:32:21 -08002985 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002986 if (t == nullptr) {
2987 return;
2988 }
2989 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2990
Andy Hung328d6772021-01-12 12:32:21 -08002991 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002992 if (c == nullptr) {
2993 return;
2994 }
Eric Laurent41709552019-12-16 19:34:05 -08002995 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2996 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002997}
2998
Eric Laurent41709552019-12-16 19:34:05 -08002999void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Andy Hung328d6772021-01-12 12:32:21 -08003000 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003001 if (t == nullptr) {
3002 return;
3003 }
Eric Laurent41709552019-12-16 19:34:05 -08003004 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
3005 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08003006}
3007
Eric Laurent41709552019-12-16 19:34:05 -08003008void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08003009 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
3010
Andy Hung328d6772021-01-12 12:32:21 -08003011 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003012 if (t == nullptr) {
3013 return;
3014 }
3015 t->onEffectDisable();
3016}
3017
3018bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
3019 bool unpinIfLast) {
Andy Hung328d6772021-01-12 12:32:21 -08003020 sp<ThreadBase> t = thread().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003021 if (t == nullptr) {
3022 return false;
3023 }
3024 t->disconnectEffectHandle(handle, unpinIfLast);
3025 return true;
3026}
3027
3028void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
Andy Hung328d6772021-01-12 12:32:21 -08003029 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003030 if (c == nullptr) {
3031 return;
3032 }
3033 c->resetVolume_l();
3034
3035}
3036
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -08003037product_strategy_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
Andy Hung328d6772021-01-12 12:32:21 -08003038 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003039 if (c == nullptr) {
3040 return PRODUCT_STRATEGY_NONE;
3041 }
3042 return c->strategy();
3043}
3044
3045int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
Andy Hung328d6772021-01-12 12:32:21 -08003046 sp<EffectChain> c = chain().promote();
Eric Laurent6b446ce2019-12-13 10:56:31 -08003047 if (c == nullptr) {
3048 return 0;
3049 }
3050 return c->activeTrackCnt();
3051}
3052
Eric Laurentb82e6b72019-11-22 17:25:04 -08003053
3054#undef LOG_TAG
3055#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3056
3057status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3058{
3059 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3060 Mutex::Autolock _l(mProxyLock);
3061 if (status == NO_ERROR) {
3062 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003063 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003064 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003065 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003066 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003067 bs = handle.second->disable(&status);
3068 }
3069 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003070 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003071 }
3072 }
3073 }
3074 ALOGV("%s enable %d status %d", __func__, enabled, status);
3075 return status;
3076}
3077
3078status_t AudioFlinger::DeviceEffectProxy::init(
3079 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3080//For all audio patches
3081//If src or sink device match
3082//If the effect is HW accelerated
3083// if no corresponding effect module
3084// Create EffectModule: mHalEffect
3085//Create and attach EffectHandle
3086//If the effect is not HW accelerated and the patch sink or src is a mixer port
3087// Create Effect on patch input or output thread on session -1
3088//Add EffectHandle to EffectHandle map of Effect Proxy:
3089 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3090 status_t status = NO_ERROR;
3091 for (auto &patch : patches) {
3092 status = onCreatePatch(patch.first, patch.second);
3093 ALOGV("%s onCreatePatch status %d", __func__, status);
3094 if (status == BAD_VALUE) {
3095 return status;
3096 }
3097 }
3098 return status;
3099}
3100
3101status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3102 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3103 status_t status = NAME_NOT_FOUND;
3104 sp<EffectHandle> handle;
3105 // only consider source[0] as this is the only "true" source of a patch
3106 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3107 ALOGV("%s source checkPort status %d", __func__, status);
3108 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3109 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3110 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3111 }
3112 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3113 Mutex::Autolock _l(mProxyLock);
3114 mEffectHandles.emplace(patchHandle, handle);
3115 }
3116 ALOGW_IF(status == BAD_VALUE,
3117 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3118
3119 return status;
3120}
3121
3122status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3123 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3124
3125 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3126 __func__, port->type, port->ext.device.type,
3127 port->ext.device.address, port->id, patch.isSoftware());
3128 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003129 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003130 return NAME_NOT_FOUND;
3131 }
3132 status_t status = NAME_NOT_FOUND;
3133
3134 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3135 Mutex::Autolock _l(mProxyLock);
3136 mDevicePort = *port;
3137 mHalEffect = new EffectModule(mMyCallback,
3138 const_cast<effect_descriptor_t *>(&mDescriptor),
3139 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3140 false /* pinned */, port->id);
3141 if (audio_is_input_device(mDevice.mType)) {
3142 mHalEffect->setInputDevice(mDevice);
3143 } else {
3144 mHalEffect->setDevices({mDevice});
3145 }
3146 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3147 status = (*handle)->initCheck();
3148 if (status == OK) {
3149 status = mHalEffect->addHandle((*handle).get());
3150 } else {
3151 mHalEffect.clear();
3152 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3153 }
3154 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3155 sp <ThreadBase> thread;
3156 if (audio_port_config_has_input_direction(port)) {
3157 if (patch.isSoftware()) {
3158 thread = patch.mRecord.thread();
3159 } else {
3160 thread = patch.thread().promote();
3161 }
3162 } else {
3163 if (patch.isSoftware()) {
3164 thread = patch.mPlayback.thread();
3165 } else {
3166 thread = patch.thread().promote();
3167 }
3168 }
3169 int enabled;
3170 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3171 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003172 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003173 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3174 } else {
3175 status = BAD_VALUE;
3176 }
3177 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003178 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003179 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003180 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003181 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003182 bs = (*handle)->disable(&status);
3183 }
3184 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003185 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003186 }
3187 }
3188 return status;
3189}
3190
3191void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3192 Mutex::Autolock _l(mProxyLock);
3193 mEffectHandles.erase(patchHandle);
3194}
3195
3196
3197size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3198{
3199 Mutex::Autolock _l(mProxyLock);
3200 if (effect == mHalEffect) {
3201 mHalEffect.clear();
3202 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3203 }
3204 return mHalEffect == nullptr ? 0 : 1;
3205}
3206
3207status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3208 sp<EffectHalInterface> effect) {
3209 if (mHalEffect == nullptr) {
3210 return NO_INIT;
3211 }
3212 return mManagerCallback->addEffectToHal(
3213 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3214}
3215
3216status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3217 sp<EffectHalInterface> effect) {
3218 if (mHalEffect == nullptr) {
3219 return NO_INIT;
3220 }
3221 return mManagerCallback->removeEffectFromHal(
3222 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3223}
3224
3225bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3226 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3227 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3228 }
3229 return true;
3230}
3231
3232uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3233 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3234 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3235 return mDevicePort.sample_rate;
3236 }
3237 return DEFAULT_OUTPUT_SAMPLE_RATE;
3238}
3239
3240audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3241 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3242 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3243 return mDevicePort.channel_mask;
3244 }
3245 return AUDIO_CHANNEL_OUT_STEREO;
3246}
3247
3248uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3249 if (isOutput()) {
3250 return audio_channel_count_from_out_mask(channelMask());
3251 }
3252 return audio_channel_count_from_in_mask(channelMask());
3253}
3254
3255void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3256 const Vector<String16> args;
3257 EffectBase::dump(fd, args);
3258
3259 const bool locked = dumpTryLock(mProxyLock);
3260 if (!locked) {
3261 String8 result("DeviceEffectProxy may be deadlocked\n");
3262 write(fd, result.string(), result.size());
3263 }
3264
3265 String8 outStr;
3266 if (mHalEffect != nullptr) {
3267 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3268 } else {
3269 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3270 }
3271 write(fd, outStr.string(), outStr.size());
3272 outStr.clear();
3273
3274 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3275 write(fd, outStr.string(), outStr.size());
3276 outStr.clear();
3277
3278 for (const auto& iter : mEffectHandles) {
3279 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3280 write(fd, outStr.string(), outStr.size());
3281 outStr.clear();
3282 sp<EffectBase> effect = iter.second->effect().promote();
3283 if (effect != nullptr) {
3284 effect->dump(fd, args);
3285 }
3286 }
3287
3288 if (locked) {
3289 mLock.unlock();
3290 }
3291}
3292
3293#undef LOG_TAG
3294#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3295
3296int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3297 return mManagerCallback->newEffectId();
3298}
3299
3300
3301bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3302 EffectHandle *handle, bool unpinIfLast) {
3303 sp<EffectBase> effectBase = handle->effect().promote();
3304 if (effectBase == nullptr) {
3305 return false;
3306 }
3307
3308 sp<EffectModule> effect = effectBase->asEffectModule();
3309 if (effect == nullptr) {
3310 return false;
3311 }
3312
3313 // restore suspended effects if the disconnected handle was enabled and the last one.
3314 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3315 if (remove) {
3316 sp<DeviceEffectProxy> proxy = mProxy.promote();
3317 if (proxy != nullptr) {
3318 proxy->removeEffect(effect);
3319 }
3320 if (handle->enabled()) {
3321 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3322 }
3323 }
3324 return true;
3325}
3326
3327status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3328 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3329 sp<EffectHalInterface> *effect) {
3330 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3331}
3332
3333status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3334 sp<EffectHalInterface> effect) {
3335 sp<DeviceEffectProxy> proxy = mProxy.promote();
3336 if (proxy == nullptr) {
3337 return NO_INIT;
3338 }
3339 return proxy->addEffectToHal(effect);
3340}
3341
3342status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3343 sp<EffectHalInterface> effect) {
3344 sp<DeviceEffectProxy> proxy = mProxy.promote();
3345 if (proxy == nullptr) {
3346 return NO_INIT;
3347 }
3348 return proxy->addEffectToHal(effect);
3349}
3350
3351bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3352 sp<DeviceEffectProxy> proxy = mProxy.promote();
3353 if (proxy == nullptr) {
3354 return true;
3355 }
3356 return proxy->isOutput();
3357}
3358
3359uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3360 sp<DeviceEffectProxy> proxy = mProxy.promote();
3361 if (proxy == nullptr) {
3362 return DEFAULT_OUTPUT_SAMPLE_RATE;
3363 }
3364 return proxy->sampleRate();
3365}
3366
3367audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3368 sp<DeviceEffectProxy> proxy = mProxy.promote();
3369 if (proxy == nullptr) {
3370 return AUDIO_CHANNEL_OUT_STEREO;
3371 }
3372 return proxy->channelMask();
3373}
3374
3375uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3376 sp<DeviceEffectProxy> proxy = mProxy.promote();
3377 if (proxy == nullptr) {
3378 return 2;
3379 }
3380 return proxy->channelCount();
3381}
3382
Glenn Kasten63238ef2015-03-02 15:50:29 -08003383} // namespace android