blob: 3b5d6af451a3ec751c727bf07368198fe627ac49 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070028#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070029#include <system/audio_effects/effect_ns.h>
30#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080031#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080032#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070033#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070035#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070037#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070038#include <media/audiohal/EffectHalInterface.h>
39#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44// ----------------------------------------------------------------------------
45
46// Note: the following macro is used for extremely verbose logging message. In
47// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
48// 0; but one side effect of this is to turn all LOGV's as well. Some messages
49// are so verbose that we want to suppress them even when we have ALOG_ASSERT
50// turned on. Do not uncomment the #def below unless you really know what you
51// are doing and want to see all of the extremely verbose messages.
52//#define VERY_VERY_VERBOSE_LOGGING
53#ifdef VERY_VERY_VERBOSE_LOGGING
54#define ALOGVV ALOGV
55#else
56#define ALOGVV(a...) do { } while(0)
57#endif
58
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090059#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
60
Eric Laurentca7cc822012-11-19 14:55:58 -080061namespace android {
62
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070063using binder::Status;
64
65namespace {
66
67// Append a POD value into a vector of bytes.
68template<typename T>
69void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
70 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
71 buffer->insert(buffer->end(), ar, ar + sizeof(T));
72}
73
74// Write a POD value into a vector of bytes (clears the previous buffer
75// content).
76template<typename T>
77void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
78 buffer->clear();
79 appendToBuffer(value, buffer);
80}
81
82} // namespace
83
Eric Laurentca7cc822012-11-19 14:55:58 -080084// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080085// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080086// ----------------------------------------------------------------------------
87
88#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080089#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080090
Eric Laurent41709552019-12-16 19:34:05 -080091AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080092 effect_descriptor_t *desc,
93 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080094 audio_session_t sessionId,
95 bool pinned)
96 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080097 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080098 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080099{
Eric Laurentca7cc822012-11-19 14:55:58 -0800100}
101
Eric Laurent41709552019-12-16 19:34:05 -0800102// must be called with EffectModule::mLock held
103status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800104{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800105
Eric Laurent41709552019-12-16 19:34:05 -0800106 ALOGV("setEnabled %p enabled %d", this, enabled);
107
108 if (enabled != isEnabled()) {
109 switch (mState) {
110 // going from disabled to enabled
111 case IDLE:
112 mState = STARTING;
113 break;
114 case STOPPED:
115 mState = RESTART;
116 break;
117 case STOPPING:
118 mState = ACTIVE;
119 break;
120
121 // going from enabled to disabled
122 case RESTART:
123 mState = STOPPED;
124 break;
125 case STARTING:
126 mState = IDLE;
127 break;
128 case ACTIVE:
129 mState = STOPPING;
130 break;
131 case DESTROYED:
132 return NO_ERROR; // simply ignore as we are being destroyed
133 }
134 for (size_t i = 1; i < mHandles.size(); i++) {
135 EffectHandle *h = mHandles[i];
136 if (h != NULL && !h->disconnected()) {
137 h->setEnabled(enabled);
138 }
139 }
140 }
141 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800142}
143
Eric Laurent41709552019-12-16 19:34:05 -0800144status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
145{
146 status_t status;
147 {
148 Mutex::Autolock _l(mLock);
149 status = setEnabled_l(enabled);
150 }
151 if (fromHandle) {
152 if (enabled) {
153 if (status != NO_ERROR) {
154 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
155 } else {
156 mCallback->onEffectEnable(this);
157 }
158 } else {
159 mCallback->onEffectDisable(this);
160 }
161 }
162 return status;
163}
164
165bool AudioFlinger::EffectBase::isEnabled() const
166{
167 switch (mState) {
168 case RESTART:
169 case STARTING:
170 case ACTIVE:
171 return true;
172 case IDLE:
173 case STOPPING:
174 case STOPPED:
175 case DESTROYED:
176 default:
177 return false;
178 }
179}
180
181void AudioFlinger::EffectBase::setSuspended(bool suspended)
182{
183 Mutex::Autolock _l(mLock);
184 mSuspended = suspended;
185}
186
187bool AudioFlinger::EffectBase::suspended() const
188{
189 Mutex::Autolock _l(mLock);
190 return mSuspended;
191}
192
193status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800194{
195 status_t status;
196
197 Mutex::Autolock _l(mLock);
198 int priority = handle->priority();
199 size_t size = mHandles.size();
200 EffectHandle *controlHandle = NULL;
201 size_t i;
202 for (i = 0; i < size; i++) {
203 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800204 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800205 continue;
206 }
207 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700208 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800209 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700210 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800211 if (h->priority() <= priority) {
212 break;
213 }
214 }
215 // if inserted in first place, move effect control from previous owner to this handle
216 if (i == 0) {
217 bool enabled = false;
218 if (controlHandle != NULL) {
219 enabled = controlHandle->enabled();
220 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
221 }
222 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
223 status = NO_ERROR;
224 } else {
225 status = ALREADY_EXISTS;
226 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700227 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800228 mHandles.insertAt(handle, i);
229 return status;
230}
231
Eric Laurent41709552019-12-16 19:34:05 -0800232status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700233{
234 status_t status = NO_ERROR;
235 bool doRegister = false;
236 bool registered = false;
237 bool doEnable = false;
238 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700239 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovf698ff22020-03-31 10:07:29 -0700240 uint32_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700241
242 {
243 Mutex::Autolock _l(mLock);
244 // register effect when first handle is attached and unregister when last handle is removed
245 if (mPolicyRegistered != mHandles.size() > 0) {
246 doRegister = true;
247 mPolicyRegistered = mHandles.size() > 0;
248 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800249 io = mCallback->io();
250 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700251 }
252 }
253 // enable effect when registered according to enable state requested by controlling handle
254 if (mHandles.size() > 0) {
255 EffectHandle *handle = controlHandle_l();
256 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
257 doEnable = true;
258 mPolicyEnabled = handle->enabled();
259 }
260 }
261 registered = mPolicyRegistered;
262 enabled = mPolicyEnabled;
263 mPolicyLock.lock();
264 }
265 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
266 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
267 if (doRegister) {
268 if (registered) {
269 status = AudioSystem::registerEffect(
270 &mDescriptor,
271 io,
272 strategy,
273 mSessionId,
274 mId);
275 } else {
276 status = AudioSystem::unregisterEffect(mId);
277 }
278 }
279 if (registered && doEnable) {
280 status = AudioSystem::setEffectEnabled(mId, enabled);
281 }
282 mPolicyLock.unlock();
283
284 return status;
285}
286
287
Eric Laurent41709552019-12-16 19:34:05 -0800288ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800289{
290 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800291 return removeHandle_l(handle);
292}
293
Eric Laurent41709552019-12-16 19:34:05 -0800294ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800295{
Eric Laurentca7cc822012-11-19 14:55:58 -0800296 size_t size = mHandles.size();
297 size_t i;
298 for (i = 0; i < size; i++) {
299 if (mHandles[i] == handle) {
300 break;
301 }
302 }
303 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800304 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
305 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800306 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800308
309 mHandles.removeAt(i);
310 // if removed from first place, move effect control from this handle to next in line
311 if (i == 0) {
312 EffectHandle *h = controlHandle_l();
313 if (h != NULL) {
314 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
315 }
316 }
317
Eric Laurentca7cc822012-11-19 14:55:58 -0800318 if (mHandles.size() == 0 && !mPinned) {
319 mState = DESTROYED;
320 }
321
322 return mHandles.size();
323}
324
325// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800326AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800327{
328 // the first valid handle in the list has control over the module
329 for (size_t i = 0; i < mHandles.size(); i++) {
330 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800331 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800332 return h;
333 }
334 }
335
336 return NULL;
337}
338
Eric Laurentf10c7092016-12-06 17:09:56 -0800339// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800340ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800341{
342 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800343 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
344 return mHandles.size();
345 }
346
Eric Laurentf10c7092016-12-06 17:09:56 -0800347 Mutex::Autolock _l(mLock);
348 ssize_t numHandles = removeHandle_l(handle);
349 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800350 mLock.unlock();
351 mCallback->updateOrphanEffectChains(this);
352 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800353 }
354 return numHandles;
355}
356
Eric Laurent41709552019-12-16 19:34:05 -0800357bool AudioFlinger::EffectBase::purgeHandles()
358{
359 bool enabled = false;
360 Mutex::Autolock _l(mLock);
361 EffectHandle *handle = controlHandle_l();
362 if (handle != NULL) {
363 enabled = handle->enabled();
364 }
365 mHandles.clear();
366 return enabled;
367}
368
369void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
370 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
371}
372
373static String8 effectFlagsToString(uint32_t flags) {
374 String8 s;
375
376 s.append("conn. mode: ");
377 switch (flags & EFFECT_FLAG_TYPE_MASK) {
378 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
379 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
380 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
381 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
382 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
383 default: s.append("unknown/reserved"); break;
384 }
385 s.append(", ");
386
387 s.append("insert pref: ");
388 switch (flags & EFFECT_FLAG_INSERT_MASK) {
389 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
390 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
391 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
392 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
393 default: s.append("unknown/reserved"); break;
394 }
395 s.append(", ");
396
397 s.append("volume mgmt: ");
398 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
399 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
400 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
401 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
402 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
403 default: s.append("unknown/reserved"); break;
404 }
405 s.append(", ");
406
407 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
408 if (devind) {
409 s.append("device indication: ");
410 switch (devind) {
411 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
412 default: s.append("unknown/reserved"); break;
413 }
414 s.append(", ");
415 }
416
417 s.append("input mode: ");
418 switch (flags & EFFECT_FLAG_INPUT_MASK) {
419 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
420 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
421 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
422 default: s.append("not set"); break;
423 }
424 s.append(", ");
425
426 s.append("output mode: ");
427 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
428 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
429 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
430 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
431 default: s.append("not set"); break;
432 }
433 s.append(", ");
434
435 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
436 if (accel) {
437 s.append("hardware acceleration: ");
438 switch (accel) {
439 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
440 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
441 default: s.append("unknown/reserved"); break;
442 }
443 s.append(", ");
444 }
445
446 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
447 if (modeind) {
448 s.append("mode indication: ");
449 switch (modeind) {
450 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
451 default: s.append("unknown/reserved"); break;
452 }
453 s.append(", ");
454 }
455
456 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
457 if (srcind) {
458 s.append("source indication: ");
459 switch (srcind) {
460 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
461 default: s.append("unknown/reserved"); break;
462 }
463 s.append(", ");
464 }
465
466 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
467 s.append("offloadable, ");
468 }
469
470 int len = s.length();
471 if (s.length() > 2) {
472 (void) s.lockBuffer(len);
473 s.unlockBuffer(len - 2);
474 }
475 return s;
476}
477
478void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
479{
480 String8 result;
481
482 result.appendFormat("\tEffect ID %d:\n", mId);
483
484 bool locked = AudioFlinger::dumpTryLock(mLock);
485 // failed to lock - AudioFlinger is probably deadlocked
486 if (!locked) {
487 result.append("\t\tCould not lock Fx mutex:\n");
488 }
489
490 result.append("\t\tSession State Registered Enabled Suspended:\n");
491 result.appendFormat("\t\t%05d %03d %s %s %s\n",
492 mSessionId, mState, mPolicyRegistered ? "y" : "n",
493 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
494
495 result.append("\t\tDescriptor:\n");
496 char uuidStr[64];
497 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
498 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
499 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
500 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
501 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
502 mDescriptor.apiVersion,
503 mDescriptor.flags,
504 effectFlagsToString(mDescriptor.flags).string());
505 result.appendFormat("\t\t- name: %s\n",
506 mDescriptor.name);
507
508 result.appendFormat("\t\t- implementor: %s\n",
509 mDescriptor.implementor);
510
511 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
512 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
513 char buffer[256];
514 for (size_t i = 0; i < mHandles.size(); ++i) {
515 EffectHandle *handle = mHandles[i];
516 if (handle != NULL && !handle->disconnected()) {
517 handle->dumpToBuffer(buffer, sizeof(buffer));
518 result.append(buffer);
519 }
520 }
521 if (locked) {
522 mLock.unlock();
523 }
524
525 write(fd, result.string(), result.length());
526}
527
528// ----------------------------------------------------------------------------
529// EffectModule implementation
530// ----------------------------------------------------------------------------
531
532#undef LOG_TAG
533#define LOG_TAG "AudioFlinger::EffectModule"
534
535AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
536 effect_descriptor_t *desc,
537 int id,
538 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800539 bool pinned,
540 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800541 : EffectBase(callback, desc, id, sessionId, pinned),
542 // clear mConfig to ensure consistent initial value of buffer framecount
543 // in case buffers are associated by setInBuffer() or setOutBuffer()
544 // prior to configure().
545 mConfig{{}, {}},
546 mStatus(NO_INIT),
547 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
548 mDisableWaitCnt(0), // set by process() and updateState()
549 mOffloaded(false)
550#ifdef FLOAT_EFFECT_CHAIN
551 , mSupportsFloat(false)
552#endif
553{
554 ALOGV("Constructor %p pinned %d", this, pinned);
555 int lStatus;
556
557 // create effect engine from effect factory
558 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800559 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800560 if (mStatus != NO_ERROR) {
561 return;
562 }
563 lStatus = init();
564 if (lStatus < 0) {
565 mStatus = lStatus;
566 goto Error;
567 }
568
569 setOffloaded(callback->isOffload(), callback->io());
570 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
571
572 return;
573Error:
574 mEffectInterface.clear();
575 ALOGV("Constructor Error %d", mStatus);
576}
577
578AudioFlinger::EffectModule::~EffectModule()
579{
580 ALOGV("Destructor %p", this);
581 if (mEffectInterface != 0) {
582 char uuidStr[64];
583 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
584 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
585 this, uuidStr);
586 release_l();
587 }
588
589}
590
591ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
592{
593 ssize_t status = EffectBase::removeHandle_l(handle);
594
595 // Prevent calls to process() and other functions on effect interface from now on.
596 // The effect engine will be released by the destructor when the last strong reference on
597 // this object is released which can happen after next process is called.
598 if (status == 0 && !mPinned) {
599 mEffectInterface->close();
600 }
601
602 return status;
603}
604
Eric Laurentfa1e1232016-08-02 19:01:49 -0700605bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800606 Mutex::Autolock _l(mLock);
607
Eric Laurentfa1e1232016-08-02 19:01:49 -0700608 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800609 switch (mState) {
610 case RESTART:
611 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700612 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800613
614 case STARTING:
615 // clear auxiliary effect input buffer for next accumulation
616 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
617 memset(mConfig.inputCfg.buffer.raw,
618 0,
619 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
620 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700621 if (start_l() == NO_ERROR) {
622 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700623 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700624 } else {
625 mState = IDLE;
626 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800627 break;
628 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900629 // volume control for offload and direct threads must take effect immediately.
630 if (stop_l() == NO_ERROR
631 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700632 mDisableWaitCnt = mMaxDisableWaitCnt;
633 } else {
634 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
635 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800636 mState = STOPPED;
637 break;
638 case STOPPED:
639 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
640 // turn off sequence.
641 if (--mDisableWaitCnt == 0) {
642 reset_l();
643 mState = IDLE;
644 }
645 break;
646 default: //IDLE , ACTIVE, DESTROYED
647 break;
648 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700649
650 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800651}
652
653void AudioFlinger::EffectModule::process()
654{
655 Mutex::Autolock _l(mLock);
656
Mikhail Naganov022b9952017-01-04 16:36:51 -0800657 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800658 return;
659 }
660
rago94a1ee82017-07-21 15:11:02 -0700661 const uint32_t inChannelCount =
662 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
663 const uint32_t outChannelCount =
664 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
665 const bool auxType =
666 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
667
Andy Hungfa69ca32017-11-30 10:07:53 -0800668 // safeInputOutputSampleCount is 0 if the channel count between input and output
669 // buffers do not match. This prevents automatic accumulation or copying between the
670 // input and output effect buffers without an intermediary effect process.
671 // TODO: consider implementing channel conversion.
672 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700673 mInChannelCountRequested != mOutChannelCountRequested ? 0
674 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800675 mConfig.inputCfg.buffer.frameCount,
676 mConfig.outputCfg.buffer.frameCount);
677 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
678#ifdef FLOAT_EFFECT_CHAIN
679 accumulate_float(
680 mConfig.outputCfg.buffer.f32,
681 mConfig.inputCfg.buffer.f32,
682 safeInputOutputSampleCount);
683#else
684 accumulate_i16(
685 mConfig.outputCfg.buffer.s16,
686 mConfig.inputCfg.buffer.s16,
687 safeInputOutputSampleCount);
688#endif
689 };
690 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
691#ifdef FLOAT_EFFECT_CHAIN
692 memcpy(
693 mConfig.outputCfg.buffer.f32,
694 mConfig.inputCfg.buffer.f32,
695 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
696
697#else
698 memcpy(
699 mConfig.outputCfg.buffer.s16,
700 mConfig.inputCfg.buffer.s16,
701 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
702#endif
703 };
704
Eric Laurentca7cc822012-11-19 14:55:58 -0800705 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700706 int ret;
707 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700708 if (auxType) {
709 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800710 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700711#ifdef FLOAT_EFFECT_CHAIN
712 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800713#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700714 // Do in-place float conversion for auxiliary effect input buffer.
715 static_assert(sizeof(float) <= sizeof(int32_t),
716 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
717
Andy Hungfa69ca32017-11-30 10:07:53 -0800718 memcpy_to_float_from_q4_27(
719 mConfig.inputCfg.buffer.f32,
720 mConfig.inputCfg.buffer.s32,
721 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800722#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800723 } else
Andy Hung116a4982017-11-30 10:15:08 -0800724#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800725 {
Andy Hung116a4982017-11-30 10:15:08 -0800726#ifdef FLOAT_AUX
727 memcpy_to_i16_from_float(
728 mConfig.inputCfg.buffer.s16,
729 mConfig.inputCfg.buffer.f32,
730 mConfig.inputCfg.buffer.frameCount);
731#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800732 memcpy_to_i16_from_q4_27(
733 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700734 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800735 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800736#endif
rago94a1ee82017-07-21 15:11:02 -0700737 }
rago94a1ee82017-07-21 15:11:02 -0700738 }
739#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800740 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
741 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
742
743 if (!auxType && mInChannelCountRequested != inChannelCount) {
744 adjust_channels(
745 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
746 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
747 sizeof(float),
748 sizeof(float)
749 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
750 inBuffer = mInConversionBuffer;
751 }
752 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
753 && mOutChannelCountRequested != outChannelCount) {
754 adjust_selected_channels(
755 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
756 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
757 sizeof(float),
758 sizeof(float)
759 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
760 outBuffer = mOutConversionBuffer;
761 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800762 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
763 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800764 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800765 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
766 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700767 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800768 memcpy_to_i16_from_float(
769 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800770 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800771 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800772 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700773 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800774 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800775 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800776 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
777 goto data_bypass;
778 }
779 memcpy_to_i16_from_float(
780 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800781 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800782 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800783 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700784 }
785 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800786#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800787 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800788#ifdef FLOAT_EFFECT_CHAIN
789 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800790 sp<EffectBufferHalInterface> target =
791 mOutChannelCountRequested != outChannelCount
792 ? mOutConversionBuffer : mOutBuffer;
793
Andy Hungfa69ca32017-11-30 10:07:53 -0800794 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800795 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800796 mOutConversionBuffer->audioBuffer()->s16,
797 outChannelCount * mConfig.outputCfg.buffer.frameCount);
798 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800799 if (mOutChannelCountRequested != outChannelCount) {
800 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
801 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
802 sizeof(float),
803 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
804 }
rago94a1ee82017-07-21 15:11:02 -0700805#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700806 } else {
rago94a1ee82017-07-21 15:11:02 -0700807#ifdef FLOAT_EFFECT_CHAIN
808 data_bypass:
809#endif
810 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800811 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700812 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800813 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700814 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800815 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700816 }
817 }
818 ret = -ENODATA;
819 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800820
Eric Laurentca7cc822012-11-19 14:55:58 -0800821 // force transition to IDLE state when engine is ready
822 if (mState == STOPPED && ret == -ENODATA) {
823 mDisableWaitCnt = 1;
824 }
825
826 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700827 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800828#ifdef FLOAT_AUX
829 const size_t size =
830 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
831#else
rago94a1ee82017-07-21 15:11:02 -0700832 const size_t size =
833 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800834#endif
rago94a1ee82017-07-21 15:11:02 -0700835 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800836 }
837 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700838 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
840 // If an insert effect is idle and input buffer is different from output buffer,
841 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800842 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700843 // similar handling with data_bypass above.
844 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
845 accumulateInputToOutput();
846 } else { // EFFECT_BUFFER_ACCESS_WRITE
847 copyInputToOutput();
848 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800849 }
850 }
851}
852
853void AudioFlinger::EffectModule::reset_l()
854{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700855 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800856 return;
857 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700858 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800859}
860
861status_t AudioFlinger::EffectModule::configure()
862{
rago94a1ee82017-07-21 15:11:02 -0700863 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700864 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700865 uint32_t size;
866 audio_channel_mask_t channelMask;
867
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.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800877 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800878 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700879 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800880
881 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800882 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
883 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
884 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
885 mConfig.inputCfg.channels);
886 }
887#ifndef MULTICHANNEL_EFFECT_CHAIN
888 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
889 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
890 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
891 mConfig.outputCfg.channels);
892 }
893#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800895#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700896 // TODO: Update this logic when multichannel effects are implemented.
897 // For offloaded tracks consider mono output as stereo for proper effect initialization
898 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
899 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
900 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
901 ALOGV("Overriding effect input and output as STEREO");
902 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800903#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800904 }
jiabineb3bda02020-06-30 14:07:03 -0700905 if (isHapticGenerator()) {
906 audio_channel_mask_t hapticChannelMask = mCallback->hapticChannelMask();
907 mConfig.inputCfg.channels |= hapticChannelMask;
908 mConfig.outputCfg.channels |= hapticChannelMask;
909 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800910 mInChannelCountRequested =
911 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
912 mOutChannelCountRequested =
913 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700914
rago94a1ee82017-07-21 15:11:02 -0700915 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
916 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900917
918 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100919 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900920 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
921 ALOGV("Overriding effect input as 48kHz");
922 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800923 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900924 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
926 mConfig.inputCfg.bufferProvider.cookie = NULL;
927 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
928 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
929 mConfig.outputCfg.bufferProvider.cookie = NULL;
930 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
931 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
932 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
933 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800934 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800935 // always overwrites output buffer: input buffer == output buffer
936 // - in other sessions:
937 // last effect in the chain accumulates in output buffer: input buffer != output buffer
938 // other effect: overwrites output buffer: input buffer == output buffer
939 // Auxiliary effect:
940 // accumulates in output buffer: input buffer != output buffer
941 // Therefore: accumulate <=> input buffer != output buffer
942 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
943 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
944 } else {
945 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
946 }
947 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
948 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800949 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800950 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
951
Eric Laurent6b446ce2019-12-13 10:56:31 -0800952 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800953 this, mCallback->chain().promote().get(),
954 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800955
956 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700957 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700958 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800959 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700960 &mConfig,
961 &size,
962 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700963 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800964 status = cmdStatus;
965 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800966
967#ifdef MULTICHANNEL_EFFECT_CHAIN
968 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800969 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800970 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
971 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
972 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700973 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
974 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800975 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
976 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
977 }
978 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
979 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
980 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
981 }
982 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700983 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800984 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700985 &mConfig,
986 &size,
987 &cmdStatus);
988 if (status == NO_ERROR) {
989 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800990 }
991 }
992#endif
993
994#ifdef FLOAT_EFFECT_CHAIN
995 if (status == NO_ERROR) {
996 mSupportsFloat = true;
997 }
998
999 if (status != NO_ERROR) {
1000 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
1001 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1002 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
1003 size = sizeof(int);
1004 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
1005 sizeof(mConfig),
1006 &mConfig,
1007 &size,
1008 &cmdStatus);
1009 if (status == NO_ERROR) {
1010 status = cmdStatus;
1011 }
1012 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001013 mSupportsFloat = false;
1014 ALOGVV("config worked with 16 bit");
1015 } else {
1016 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 }
rago94a1ee82017-07-21 15:11:02 -07001018 }
1019#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001020
rago94a1ee82017-07-21 15:11:02 -07001021 if (status == NO_ERROR) {
1022 // Establish Buffer strategy
1023 setInBuffer(mInBuffer);
1024 setOutBuffer(mOutBuffer);
1025
1026 // Update visualizer latency
1027 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1028 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1029 effect_param_t *p = (effect_param_t *)buf32;
1030
1031 p->psize = sizeof(uint32_t);
1032 p->vsize = sizeof(uint32_t);
1033 size = sizeof(int);
1034 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1035
Eric Laurent6b446ce2019-12-13 10:56:31 -08001036 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001037
1038 *((int32_t *)p->data + 1)= latency;
1039 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1040 sizeof(effect_param_t) + 8,
1041 &buf32,
1042 &size,
1043 &cmdStatus);
1044 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 }
1046
Andy Hung05083ac2017-12-14 15:00:28 -08001047 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1048 mMaxDisableWaitCnt = (uint32_t)std::max(
1049 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1050 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1051 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001052
Eric Laurentd0ebb532013-04-02 16:41:41 -07001053exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001054 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001055 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001056 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001057 return status;
1058}
1059
1060status_t AudioFlinger::EffectModule::init()
1061{
1062 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001063 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001064 return NO_INIT;
1065 }
1066 status_t cmdStatus;
1067 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001068 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1069 0,
1070 NULL,
1071 &size,
1072 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001073 if (status == 0) {
1074 status = cmdStatus;
1075 }
1076 return status;
1077}
1078
Eric Laurent1b928682014-10-02 19:41:47 -07001079void AudioFlinger::EffectModule::addEffectToHal_l()
1080{
1081 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1082 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001083 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001084 }
1085}
1086
Eric Laurentfa1e1232016-08-02 19:01:49 -07001087// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001088status_t AudioFlinger::EffectModule::start()
1089{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001090 status_t status;
1091 {
1092 Mutex::Autolock _l(mLock);
1093 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001094 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001095 if (status == NO_ERROR) {
1096 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001097 }
1098 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001099}
1100
1101status_t AudioFlinger::EffectModule::start_l()
1102{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001103 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001104 return NO_INIT;
1105 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001106 if (mStatus != NO_ERROR) {
1107 return mStatus;
1108 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001109 status_t cmdStatus;
1110 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001111 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1112 0,
1113 NULL,
1114 &size,
1115 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001116 if (status == 0) {
1117 status = cmdStatus;
1118 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001119 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001120 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001121 }
1122 return status;
1123}
1124
1125status_t AudioFlinger::EffectModule::stop()
1126{
1127 Mutex::Autolock _l(mLock);
1128 return stop_l();
1129}
1130
1131status_t AudioFlinger::EffectModule::stop_l()
1132{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001133 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001134 return NO_INIT;
1135 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001136 if (mStatus != NO_ERROR) {
1137 return mStatus;
1138 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001139 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001140 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001141
1142 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001143 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1144 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1145 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001146 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001147 mSetVolumeReentrantTid = INVALID_PID;
1148 }
1149
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001150 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1151 0,
1152 NULL,
1153 &size,
1154 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001155 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001156 status = cmdStatus;
1157 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001158 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001159 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001160 }
1161 return status;
1162}
1163
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001164// must be called with EffectChain::mLock held
1165void AudioFlinger::EffectModule::release_l()
1166{
1167 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001168 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001169 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001170 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001171 mEffectInterface.clear();
1172 }
1173}
1174
Eric Laurent6b446ce2019-12-13 10:56:31 -08001175status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001176{
1177 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1178 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001179 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001180 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001181 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001182}
1183
Andy Hunge4a1d912016-08-17 14:11:13 -07001184// round up delta valid if value and divisor are positive.
1185template <typename T>
1186static T roundUpDelta(const T &value, const T &divisor) {
1187 T remainder = value % divisor;
1188 return remainder == 0 ? 0 : divisor - remainder;
1189}
1190
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001191status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1192 const std::vector<uint8_t>& cmdData,
1193 int32_t maxReplySize,
1194 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001195{
1196 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001197 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001198
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001199 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001200 return NO_INIT;
1201 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001202 if (mStatus != NO_ERROR) {
1203 return mStatus;
1204 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001205 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1206 return -EINVAL;
1207 }
1208 size_t cmdSize = cmdData.size();
1209 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1210 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1211 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001212 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001213 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001214 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001215 android_errorWriteLog(0x534e4554, "33003822");
1216 return -EINVAL;
1217 }
1218 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001219 (maxReplySize < sizeof(effect_param_t) ||
1220 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001221 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001222 return -EINVAL;
1223 }
ragoe2759072016-11-22 18:02:48 -08001224 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001225 (sizeof(effect_param_t) > maxReplySize
1226 || param->psize > maxReplySize - sizeof(effect_param_t)
1227 || param->vsize > maxReplySize - sizeof(effect_param_t)
1228 - param->psize
1229 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1230 maxReplySize
1231 - sizeof(effect_param_t)
1232 - param->psize
1233 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001234 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1235 android_errorWriteLog(0x534e4554, "32705438");
1236 return -EINVAL;
1237 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001238 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001239 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1240 && // DEFERRED not generally used
1241 (param == nullptr
1242 || param->psize > cmdSize - sizeof(effect_param_t)
1243 || param->vsize > cmdSize - sizeof(effect_param_t)
1244 - param->psize
1245 || roundUpDelta(param->psize,
1246 (uint32_t) sizeof(int)) >
1247 cmdSize
1248 - sizeof(effect_param_t)
1249 - param->psize
1250 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001251 android_errorWriteLog(0x534e4554, "30204301");
1252 return -EINVAL;
1253 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001254 uint32_t replySize = maxReplySize;
1255 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001256 status_t status = mEffectInterface->command(cmdCode,
1257 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001258 const_cast<uint8_t*>(cmdData.data()),
1259 &replySize,
1260 reply->data());
1261 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001262 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001263 for (size_t i = 1; i < mHandles.size(); i++) {
1264 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001265 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001266 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001267 }
1268 }
1269 }
1270 return status;
1271}
1272
Eric Laurentca7cc822012-11-19 14:55:58 -08001273bool AudioFlinger::EffectModule::isProcessEnabled() const
1274{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001275 if (mStatus != NO_ERROR) {
1276 return false;
1277 }
1278
Eric Laurentca7cc822012-11-19 14:55:58 -08001279 switch (mState) {
1280 case RESTART:
1281 case ACTIVE:
1282 case STOPPING:
1283 case STOPPED:
1284 return true;
1285 case IDLE:
1286 case STARTING:
1287 case DESTROYED:
1288 default:
1289 return false;
1290 }
1291}
1292
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001293bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1294{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001295 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001296}
1297
1298bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1299{
1300 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1301}
1302
Mikhail Naganov022b9952017-01-04 16:36:51 -08001303void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001304 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001305
1306 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001307 if (buffer != 0) {
1308 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1309 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1310 } else {
1311 mConfig.inputCfg.buffer.raw = NULL;
1312 }
1313 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001314 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001315
1316#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001317 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001318 // Theoretically insert effects can also do in-place conversions (destroying
1319 // the original buffer) when the output buffer is identical to the input buffer,
1320 // but we don't optimize for it here.
1321 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001322 const uint32_t inChannelCount =
1323 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1324 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001325 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001326 // we need to translate - create hidl shared buffer and intercept
1327 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001328 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1329 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1330 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001331
1332 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1333 __func__, inChannels, inFrameCount, size);
1334
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001335 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001336 || size > mInConversionBuffer->getSize())) {
1337 mInConversionBuffer.clear();
1338 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001339 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001340 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001341 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001342 mInConversionBuffer->setFrameCount(inFrameCount);
1343 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001344 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001345 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001346 }
1347 }
1348#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001349}
1350
1351void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001352 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001353
1354 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001355 if (buffer != 0) {
1356 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1357 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1358 } else {
1359 mConfig.outputCfg.buffer.raw = NULL;
1360 }
1361 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001362 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001363
1364#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001365 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001366 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001367 const uint32_t outChannelCount =
1368 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1369 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001370 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001371 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001372 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1373 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1374 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001375
1376 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1377 __func__, outChannels, outFrameCount, size);
1378
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001379 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001380 || size > mOutConversionBuffer->getSize())) {
1381 mOutConversionBuffer.clear();
1382 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001383 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001384 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001385 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001386 mOutConversionBuffer->setFrameCount(outFrameCount);
1387 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001388 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001389 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001390 }
1391 }
1392#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001393}
1394
Eric Laurentca7cc822012-11-19 14:55:58 -08001395status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1396{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001397 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001398 if (mStatus != NO_ERROR) {
1399 return mStatus;
1400 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001401 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001402 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1403 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1404 if (isProcessEnabled() &&
1405 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001406 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1407 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001408 uint32_t volume[2];
1409 uint32_t *pVolume = NULL;
1410 uint32_t size = sizeof(volume);
1411 volume[0] = *left;
1412 volume[1] = *right;
1413 if (controller) {
1414 pVolume = volume;
1415 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001416 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1417 size,
1418 volume,
1419 &size,
1420 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001421 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1422 *left = volume[0];
1423 *right = volume[1];
1424 }
1425 }
1426 return status;
1427}
1428
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001429void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1430{
Zhou Songd505c642020-02-20 16:35:37 +08001431 // for offload or direct thread, if the effect chain has non-offloadable
1432 // effect and any effect module within the chain has volume control, then
1433 // volume control is delegated to effect, otherwise, set volume to hal.
1434 if (mEffectCallback->isOffloadOrDirect() &&
1435 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001436 float vol_l = (float)left / (1 << 24);
1437 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001438 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001439 }
1440}
1441
jiabin8f278ee2019-11-11 12:16:27 -08001442status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1443 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001444{
jiabin8f278ee2019-11-11 12:16:27 -08001445 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1446 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001447 return NO_ERROR;
1448 }
1449
1450 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001451 if (mStatus != NO_ERROR) {
1452 return mStatus;
1453 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001454 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001455 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001456 status_t cmdStatus;
1457 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001458 // FIXME: use audio device types and addresses when the hal interface is ready.
1459 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001460 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001461 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001462 &size,
1463 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001464 }
1465 return status;
1466}
1467
jiabin8f278ee2019-11-11 12:16:27 -08001468status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1469{
1470 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1471}
1472
1473status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1474{
1475 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1476}
1477
Eric Laurentca7cc822012-11-19 14:55:58 -08001478status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1479{
1480 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001481 if (mStatus != NO_ERROR) {
1482 return mStatus;
1483 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001484 status_t status = NO_ERROR;
1485 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1486 status_t cmdStatus;
1487 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001488 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1489 sizeof(audio_mode_t),
1490 &mode,
1491 &size,
1492 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001493 if (status == NO_ERROR) {
1494 status = cmdStatus;
1495 }
1496 }
1497 return status;
1498}
1499
1500status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1501{
1502 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001503 if (mStatus != NO_ERROR) {
1504 return mStatus;
1505 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001506 status_t status = NO_ERROR;
1507 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1508 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001509 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1510 sizeof(audio_source_t),
1511 &source,
1512 &size,
1513 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001514 }
1515 return status;
1516}
1517
Eric Laurent5baf2af2013-09-12 17:37:00 -07001518status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1519{
1520 Mutex::Autolock _l(mLock);
1521 if (mStatus != NO_ERROR) {
1522 return mStatus;
1523 }
1524 status_t status = NO_ERROR;
1525 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1526 status_t cmdStatus;
1527 uint32_t size = sizeof(status_t);
1528 effect_offload_param_t cmd;
1529
1530 cmd.isOffload = offloaded;
1531 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001532 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1533 sizeof(effect_offload_param_t),
1534 &cmd,
1535 &size,
1536 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001537 if (status == NO_ERROR) {
1538 status = cmdStatus;
1539 }
1540 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1541 } else {
1542 if (offloaded) {
1543 status = INVALID_OPERATION;
1544 }
1545 mOffloaded = false;
1546 }
1547 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1548 return status;
1549}
1550
1551bool AudioFlinger::EffectModule::isOffloaded() const
1552{
1553 Mutex::Autolock _l(mLock);
1554 return mOffloaded;
1555}
1556
jiabineb3bda02020-06-30 14:07:03 -07001557/*static*/
1558bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1559 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1560}
1561
1562bool AudioFlinger::EffectModule::isHapticGenerator() const {
1563 return isHapticGenerator(&mDescriptor.type);
1564}
1565
jiabine70bc7f2020-06-30 22:07:55 -07001566status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1567{
1568 if (mStatus != NO_ERROR) {
1569 return mStatus;
1570 }
1571 if (!isHapticGenerator()) {
1572 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1573 return INVALID_OPERATION;
1574 }
1575
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001576 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1577 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001578 param->psize = sizeof(int32_t);
1579 param->vsize = sizeof(int32_t) * 2;
1580 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1581 *((int32_t*)param->data + 1) = id;
1582 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001583 std::vector<uint8_t> response;
1584 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001585 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001586 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1587 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001588 }
1589 return status;
1590}
1591
Andy Hungbded9c82017-11-30 18:47:35 -08001592static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1593 std::stringstream ss;
1594
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001595 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001596 return "nullptr"; // make different than below
1597 } else if (buffer->externalData() != nullptr) {
1598 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1599 << " -> "
1600 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1601 } else {
1602 ss << buffer->audioBuffer()->raw;
1603 }
1604 return ss.str();
1605}
Marco Nelissenb2208842014-02-07 14:00:50 -08001606
Eric Laurent41709552019-12-16 19:34:05 -08001607void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001608{
Eric Laurent41709552019-12-16 19:34:05 -08001609 EffectBase::dump(fd, args);
1610
Eric Laurentca7cc822012-11-19 14:55:58 -08001611 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001612 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001613
Eric Laurent41709552019-12-16 19:34:05 -08001614 result.append("\t\tStatus Engine:\n");
1615 result.appendFormat("\t\t%03d %p\n",
1616 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001617
1618 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001619
1620 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001621 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1622 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1623 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 mConfig.inputCfg.buffer.frameCount,
1625 mConfig.inputCfg.samplingRate,
1626 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001627 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001628 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001629
1630 result.append("\t\t- Output configuration:\n");
1631 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001632 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001633 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001634 mConfig.outputCfg.buffer.frameCount,
1635 mConfig.outputCfg.samplingRate,
1636 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001637 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001638 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001639
rago94a1ee82017-07-21 15:11:02 -07001640#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001641
Andy Hungbded9c82017-11-30 18:47:35 -08001642 result.appendFormat("\t\t- HAL buffers:\n"
1643 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1644 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1645 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1646 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1647 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001648#endif
1649
Eric Laurentca7cc822012-11-19 14:55:58 -08001650 write(fd, result.string(), result.length());
1651
Mikhail Naganov4d547672019-02-22 14:19:19 -08001652 if (mEffectInterface != 0) {
1653 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1654 (void)mEffectInterface->dump(fd);
1655 }
1656
Eric Laurentca7cc822012-11-19 14:55:58 -08001657 if (locked) {
1658 mLock.unlock();
1659 }
1660}
1661
1662// ----------------------------------------------------------------------------
1663// EffectHandle implementation
1664// ----------------------------------------------------------------------------
1665
1666#undef LOG_TAG
1667#define LOG_TAG "AudioFlinger::EffectHandle"
1668
Eric Laurent41709552019-12-16 19:34:05 -08001669AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001670 const sp<AudioFlinger::Client>& client,
1671 const sp<media::IEffectClient>& effectClient,
1672 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001673 : BnEffect(),
1674 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001675 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001676{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001677 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001678
1679 if (client == 0) {
1680 return;
1681 }
1682 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1683 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001684 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001685 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001686 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001687 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001688 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001689 return;
1690 }
Glenn Kastene75da402013-11-20 13:54:52 -08001691 new(mCblk) effect_param_cblk_t();
1692 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001693}
1694
1695AudioFlinger::EffectHandle::~EffectHandle()
1696{
1697 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001698 disconnect(false);
1699}
1700
Glenn Kastene75da402013-11-20 13:54:52 -08001701status_t AudioFlinger::EffectHandle::initCheck()
1702{
1703 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1704}
1705
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001706#define RETURN(code) \
1707 *_aidl_return = (code); \
1708 return Status::ok();
1709
1710Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001711{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001712 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001713 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001714 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001715 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001716 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001717 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001718 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001719 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001720 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001721
1722 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001723 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001724 }
1725
1726 mEnabled = true;
1727
Eric Laurent6c796322019-04-09 14:13:17 -07001728 status_t status = effect->updatePolicyState();
1729 if (status != NO_ERROR) {
1730 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001731 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001732 }
1733
Eric Laurent6b446ce2019-12-13 10:56:31 -08001734 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001735
1736 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001737 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001738 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001739 }
1740
Eric Laurent6b446ce2019-12-13 10:56:31 -08001741 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001742 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001743 mEnabled = false;
1744 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001745 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001746}
1747
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001748Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001749{
1750 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001751 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001752 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001753 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001754 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001755 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001756 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001757 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001758 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001759
1760 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001761 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001762 }
1763 mEnabled = false;
1764
Eric Laurent6c796322019-04-09 14:13:17 -07001765 effect->updatePolicyState();
1766
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001767 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001768 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001769 }
1770
Eric Laurent6b446ce2019-12-13 10:56:31 -08001771 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001772 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001773}
1774
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001775Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001776{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001777 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001778 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001779 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001780}
1781
1782void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1783{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001784 AutoMutex _l(mLock);
1785 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1786 if (mDisconnected) {
1787 if (unpinIfLast) {
1788 android_errorWriteLog(0x534e4554, "32707507");
1789 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 return;
1791 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001793 {
Eric Laurent41709552019-12-16 19:34:05 -08001794 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001795 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001796 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001797 ALOGW("%s Effect handle %p disconnected after thread destruction",
1798 __func__, this);
1799 }
1800 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001801 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001802 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001803
Eric Laurentca7cc822012-11-19 14:55:58 -08001804 if (mClient != 0) {
1805 if (mCblk != NULL) {
1806 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1807 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1808 }
1809 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001810 // Client destructor must run with AudioFlinger client mutex locked
1811 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001812 mClient.clear();
1813 }
1814}
1815
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001816Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1817 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1818 return Status::ok();
1819}
1820
1821Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1822 const std::vector<uint8_t>& cmdData,
1823 int32_t maxResponseSize,
1824 std::vector<uint8_t>* response,
1825 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001826{
1827 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001828 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001829
Eric Laurentc7ab3092017-06-15 18:43:46 -07001830 // reject commands reserved for internal use by audio framework if coming from outside
1831 // of audioserver
1832 switch(cmdCode) {
1833 case EFFECT_CMD_ENABLE:
1834 case EFFECT_CMD_DISABLE:
1835 case EFFECT_CMD_SET_PARAM:
1836 case EFFECT_CMD_SET_PARAM_DEFERRED:
1837 case EFFECT_CMD_SET_PARAM_COMMIT:
1838 case EFFECT_CMD_GET_PARAM:
1839 break;
1840 default:
1841 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1842 break;
1843 }
1844 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001845 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001846 }
1847
Eric Laurent1ffc5852016-12-15 14:46:09 -08001848 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001849 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001850 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001851 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001852 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001853 writeToBuffer(NO_ERROR, response);
1854 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001855 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001856 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001857 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001858 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001859 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001860 writeToBuffer(NO_ERROR, response);
1861 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001862 }
1863
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001864 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001865 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001866 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001868 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 // only get parameter command is permitted for applications not controlling the effect
1870 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001871 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001872 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001873
1874 // handle commands that are not forwarded transparently to effect engine
1875 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001876 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001877 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001878 }
1879
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001880 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001881 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001882 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001883 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001884 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001885
Eric Laurentca7cc822012-11-19 14:55:58 -08001886 // No need to trylock() here as this function is executed in the binder thread serving a
1887 // particular client process: no risk to block the whole media server process or mixer
1888 // threads if we are stuck here
1889 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001890 // keep local copy of index in case of client corruption b/32220769
1891 const uint32_t clientIndex = mCblk->clientIndex;
1892 const uint32_t serverIndex = mCblk->serverIndex;
1893 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1894 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001895 mCblk->serverIndex = 0;
1896 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001897 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001898 }
1899 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001900 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001901 for (uint32_t index = serverIndex; index < clientIndex;) {
1902 int *p = (int *)(mBuffer + index);
1903 const int size = *p++;
1904 if (size < 0
1905 || size > EFFECT_PARAM_BUFFER_SIZE
1906 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001907 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001908 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001909 break;
1910 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001911
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001912 std::copy(reinterpret_cast<const uint8_t*>(p),
1913 reinterpret_cast<const uint8_t*>(p) + size,
1914 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001915
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001916 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001917 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001918 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001919 sizeof(int),
1920 &replyBuffer);
1921 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001922
1923 // verify shared memory: server index shouldn't change; client index can't go back.
1924 if (serverIndex != mCblk->serverIndex
1925 || clientIndex > mCblk->clientIndex) {
1926 android_errorWriteLog(0x534e4554, "32220769");
1927 status = BAD_VALUE;
1928 break;
1929 }
1930
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 // stop at first error encountered
1932 if (ret != NO_ERROR) {
1933 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001934 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001935 break;
1936 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001937 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001938 break;
1939 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001940 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001941 }
1942 mCblk->serverIndex = 0;
1943 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001944 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001945 }
1946
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001947 status_t status = effect->command(cmdCode,
1948 cmdData,
1949 maxResponseSize,
1950 response);
1951 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001952}
1953
1954void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1955{
1956 ALOGV("setControl %p control %d", this, hasControl);
1957
1958 mHasControl = hasControl;
1959 mEnabled = enabled;
1960
1961 if (signal && mEffectClient != 0) {
1962 mEffectClient->controlStatusChanged(hasControl);
1963 }
1964}
1965
1966void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001967 const std::vector<uint8_t>& cmdData,
1968 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001969{
1970 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001971 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001972 }
1973}
1974
1975
1976
1977void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1978{
1979 if (mEffectClient != 0) {
1980 mEffectClient->enableStatusChanged(enabled);
1981 }
1982}
1983
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001984void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001985{
1986 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1987
Marco Nelissenb2208842014-02-07 14:00:50 -08001988 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001989 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001990 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001991 mHasControl ? "yes" : "no",
1992 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001993 mCblk ? mCblk->clientIndex : 0,
1994 mCblk ? mCblk->serverIndex : 0
1995 );
1996
1997 if (locked) {
1998 mCblk->lock.unlock();
1999 }
2000}
2001
2002#undef LOG_TAG
2003#define LOG_TAG "AudioFlinger::EffectChain"
2004
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002005AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
2006 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08002007 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08002008 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08002009 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002010 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002011{
2012 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002013 sp<ThreadBase> p = thread.promote();
2014 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002015 return;
2016 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002017 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2018 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002019}
2020
2021AudioFlinger::EffectChain::~EffectChain()
2022{
Eric Laurentca7cc822012-11-19 14:55:58 -08002023}
2024
2025// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2026sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2027 effect_descriptor_t *descriptor)
2028{
2029 size_t size = mEffects.size();
2030
2031 for (size_t i = 0; i < size; i++) {
2032 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2033 return mEffects[i];
2034 }
2035 }
2036 return 0;
2037}
2038
2039// getEffectFromId_l() must be called with ThreadBase::mLock held
2040sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2041{
2042 size_t size = mEffects.size();
2043
2044 for (size_t i = 0; i < size; i++) {
2045 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2046 if (id == 0 || mEffects[i]->id() == id) {
2047 return mEffects[i];
2048 }
2049 }
2050 return 0;
2051}
2052
2053// getEffectFromType_l() must be called with ThreadBase::mLock held
2054sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2055 const effect_uuid_t *type)
2056{
2057 size_t size = mEffects.size();
2058
2059 for (size_t i = 0; i < size; i++) {
2060 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2061 return mEffects[i];
2062 }
2063 }
2064 return 0;
2065}
2066
Eric Laurent6c796322019-04-09 14:13:17 -07002067std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2068{
2069 std::vector<int> ids;
2070 Mutex::Autolock _l(mLock);
2071 for (size_t i = 0; i < mEffects.size(); i++) {
2072 ids.push_back(mEffects[i]->id());
2073 }
2074 return ids;
2075}
2076
Eric Laurentca7cc822012-11-19 14:55:58 -08002077void AudioFlinger::EffectChain::clearInputBuffer()
2078{
2079 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002080 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002081}
2082
2083// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002084void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002085{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002086 if (mInBuffer == NULL) {
2087 return;
2088 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002089 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002090 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002091
Eric Laurent6b446ce2019-12-13 10:56:31 -08002092 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002093 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002094}
2095
2096// Must be called with EffectChain::mLock locked
2097void AudioFlinger::EffectChain::process_l()
2098{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002099 // never process effects when:
2100 // - on an OFFLOAD thread
2101 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002102 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002103 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002104 bool tracksOnSession = (trackCnt() != 0);
2105
2106 if (!tracksOnSession && mTailBufferCount == 0) {
2107 doProcess = false;
2108 }
2109
2110 if (activeTrackCnt() == 0) {
2111 // if no track is active and the effect tail has not been rendered,
2112 // the input buffer must be cleared here as the mixer process will not do it
2113 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002114 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002115 if (mTailBufferCount > 0) {
2116 mTailBufferCount--;
2117 }
2118 }
2119 }
2120 }
2121
2122 size_t size = mEffects.size();
2123 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002124 // Only the input and output buffers of the chain can be external,
2125 // and 'update' / 'commit' do nothing for allocated buffers, thus
2126 // it's not needed to consider any other buffers here.
2127 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002128 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2129 mOutBuffer->update();
2130 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002131 for (size_t i = 0; i < size; i++) {
2132 mEffects[i]->process();
2133 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002134 mInBuffer->commit();
2135 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2136 mOutBuffer->commit();
2137 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002138 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002139 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002140 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002141 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2142 }
2143 if (doResetVolume) {
2144 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002145 }
2146}
2147
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002148// createEffect_l() must be called with ThreadBase::mLock held
2149status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002150 effect_descriptor_t *desc,
2151 int id,
2152 audio_session_t sessionId,
2153 bool pinned)
2154{
2155 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002156 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002157 status_t lStatus = effect->status();
2158 if (lStatus == NO_ERROR) {
2159 lStatus = addEffect_ll(effect);
2160 }
2161 if (lStatus != NO_ERROR) {
2162 effect.clear();
2163 }
2164 return lStatus;
2165}
2166
2167// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002168status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2169{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002170 Mutex::Autolock _l(mLock);
2171 return addEffect_ll(effect);
2172}
2173// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2174status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2175{
Eric Laurentca7cc822012-11-19 14:55:58 -08002176 effect_descriptor_t desc = effect->desc();
2177 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2178
Eric Laurent6b446ce2019-12-13 10:56:31 -08002179 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002180
2181 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2182 // Auxiliary effects are inserted at the beginning of mEffects vector as
2183 // they are processed first and accumulated in chain input buffer
2184 mEffects.insertAt(effect, 0);
2185
2186 // the input buffer for auxiliary effect contains mono samples in
2187 // 32 bit format. This is to avoid saturation in AudoMixer
2188 // accumulation stage. Saturation is done in EffectModule::process() before
2189 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002190 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002191 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002192#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002193 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002194 numSamples * sizeof(float), &halBuffer);
2195#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002196 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002197 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002198#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002199 if (result != OK) return result;
2200 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002201 // auxiliary effects output samples to chain input buffer for further processing
2202 // by insert effects
2203 effect->setOutBuffer(mInBuffer);
2204 } else {
2205 // Insert effects are inserted at the end of mEffects vector as they are processed
2206 // after track and auxiliary effects.
2207 // Insert effect order as a function of indicated preference:
2208 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2209 // another effect is present
2210 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2211 // last effect claiming first position
2212 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2213 // first effect claiming last position
2214 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2215 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2216 // already present
2217
2218 size_t size = mEffects.size();
2219 size_t idx_insert = size;
2220 ssize_t idx_insert_first = -1;
2221 ssize_t idx_insert_last = -1;
2222
2223 for (size_t i = 0; i < size; i++) {
2224 effect_descriptor_t d = mEffects[i]->desc();
2225 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2226 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2227 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2228 // check invalid effect chaining combinations
2229 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2230 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2231 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2232 desc.name, d.name);
2233 return INVALID_OPERATION;
2234 }
2235 // remember position of first insert effect and by default
2236 // select this as insert position for new effect
2237 if (idx_insert == size) {
2238 idx_insert = i;
2239 }
2240 // remember position of last insert effect claiming
2241 // first position
2242 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2243 idx_insert_first = i;
2244 }
2245 // remember position of first insert effect claiming
2246 // last position
2247 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2248 idx_insert_last == -1) {
2249 idx_insert_last = i;
2250 }
2251 }
2252 }
2253
2254 // modify idx_insert from first position if needed
2255 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2256 if (idx_insert_last != -1) {
2257 idx_insert = idx_insert_last;
2258 } else {
2259 idx_insert = size;
2260 }
2261 } else {
2262 if (idx_insert_first != -1) {
2263 idx_insert = idx_insert_first + 1;
2264 }
2265 }
2266
2267 // always read samples from chain input buffer
2268 effect->setInBuffer(mInBuffer);
2269
2270 // if last effect in the chain, output samples to chain
2271 // output buffer, otherwise to chain input buffer
2272 if (idx_insert == size) {
2273 if (idx_insert != 0) {
2274 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2275 mEffects[idx_insert-1]->configure();
2276 }
2277 effect->setOutBuffer(mOutBuffer);
2278 } else {
2279 effect->setOutBuffer(mInBuffer);
2280 }
2281 mEffects.insertAt(effect, idx_insert);
2282
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002283 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002284 idx_insert);
2285 }
2286 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002287
Eric Laurentca7cc822012-11-19 14:55:58 -08002288 return NO_ERROR;
2289}
2290
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002291// removeEffect_l() must be called with ThreadBase::mLock held
2292size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2293 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002294{
2295 Mutex::Autolock _l(mLock);
2296 size_t size = mEffects.size();
2297 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2298
2299 for (size_t i = 0; i < size; i++) {
2300 if (effect == mEffects[i]) {
2301 // calling stop here will remove pre-processing effect from the audio HAL.
2302 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2303 // the middle of a read from audio HAL
2304 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2305 mEffects[i]->state() == EffectModule::STOPPING) {
2306 mEffects[i]->stop();
2307 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002308 if (release) {
2309 mEffects[i]->release_l();
2310 }
2311
Mikhail Naganov022b9952017-01-04 16:36:51 -08002312 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002313 if (i == size - 1 && i != 0) {
2314 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2315 mEffects[i - 1]->configure();
2316 }
2317 }
2318 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002319 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002320 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002321
Eric Laurentca7cc822012-11-19 14:55:58 -08002322 break;
2323 }
2324 }
2325
2326 return mEffects.size();
2327}
2328
jiabin8f278ee2019-11-11 12:16:27 -08002329// setDevices_l() must be called with ThreadBase::mLock held
2330void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002331{
2332 size_t size = mEffects.size();
2333 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002334 mEffects[i]->setDevices(devices);
2335 }
2336}
2337
2338// setInputDevice_l() must be called with ThreadBase::mLock held
2339void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2340{
2341 size_t size = mEffects.size();
2342 for (size_t i = 0; i < size; i++) {
2343 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002344 }
2345}
2346
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002347// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002348void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2349{
2350 size_t size = mEffects.size();
2351 for (size_t i = 0; i < size; i++) {
2352 mEffects[i]->setMode(mode);
2353 }
2354}
2355
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002356// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002357void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2358{
2359 size_t size = mEffects.size();
2360 for (size_t i = 0; i < size; i++) {
2361 mEffects[i]->setAudioSource(source);
2362 }
2363}
2364
Zhou Songd505c642020-02-20 16:35:37 +08002365bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2366 for (const auto &effect : mEffects) {
2367 if (effect->isVolumeControlEnabled()) return true;
2368 }
2369 return false;
2370}
2371
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002372// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002373bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002374{
2375 uint32_t newLeft = *left;
2376 uint32_t newRight = *right;
2377 bool hasControl = false;
2378 int ctrlIdx = -1;
2379 size_t size = mEffects.size();
2380
2381 // first update volume controller
2382 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002383 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002384 ctrlIdx = i - 1;
2385 hasControl = true;
2386 break;
2387 }
2388 }
2389
Eric Laurentfa1e1232016-08-02 19:01:49 -07002390 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002391 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002392 if (hasControl) {
2393 *left = mNewLeftVolume;
2394 *right = mNewRightVolume;
2395 }
2396 return hasControl;
2397 }
2398
2399 mVolumeCtrlIdx = ctrlIdx;
2400 mLeftVolume = newLeft;
2401 mRightVolume = newRight;
2402
2403 // second get volume update from volume controller
2404 if (ctrlIdx >= 0) {
2405 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2406 mNewLeftVolume = newLeft;
2407 mNewRightVolume = newRight;
2408 }
2409 // then indicate volume to all other effects in chain.
2410 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002411 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002412 uint32_t lVol = newLeft;
2413 uint32_t rVol = newRight;
2414
2415 for (size_t i = 0; i < size; i++) {
2416 if ((int)i == ctrlIdx) {
2417 continue;
2418 }
2419 // this also works for ctrlIdx == -1 when there is no volume controller
2420 if ((int)i > ctrlIdx) {
2421 lVol = *left;
2422 rVol = *right;
2423 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002424 // Pass requested volume directly if this is volume monitor module
2425 if (mEffects[i]->isVolumeMonitor()) {
2426 mEffects[i]->setVolume(left, right, false);
2427 } else {
2428 mEffects[i]->setVolume(&lVol, &rVol, false);
2429 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002430 }
2431 *left = newLeft;
2432 *right = newRight;
2433
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002434 setVolumeForOutput_l(*left, *right);
2435
Eric Laurentca7cc822012-11-19 14:55:58 -08002436 return hasControl;
2437}
2438
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002439// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002440void AudioFlinger::EffectChain::resetVolume_l()
2441{
Eric Laurente7449bf2016-08-03 18:44:07 -07002442 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2443 uint32_t left = mLeftVolume;
2444 uint32_t right = mRightVolume;
2445 (void)setVolume_l(&left, &right, true);
2446 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002447}
2448
jiabineb3bda02020-06-30 14:07:03 -07002449// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2450bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2451{
2452 for (size_t i = 0; i < mEffects.size(); ++i) {
2453 if (mEffects[i]->isHapticGenerator()) {
2454 return true;
2455 }
2456 }
2457 return false;
2458}
2459
jiabine70bc7f2020-06-30 22:07:55 -07002460void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2461{
2462 Mutex::Autolock _l(mLock);
2463 for (size_t i = 0; i < mEffects.size(); ++i) {
2464 mEffects[i]->setHapticIntensity(id, intensity);
2465 }
2466}
2467
Eric Laurent1b928682014-10-02 19:41:47 -07002468void AudioFlinger::EffectChain::syncHalEffectsState()
2469{
2470 Mutex::Autolock _l(mLock);
2471 for (size_t i = 0; i < mEffects.size(); i++) {
2472 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2473 mEffects[i]->state() == EffectModule::STOPPING) {
2474 mEffects[i]->addEffectToHal_l();
2475 }
2476 }
2477}
2478
Eric Laurentca7cc822012-11-19 14:55:58 -08002479void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2480{
Eric Laurentca7cc822012-11-19 14:55:58 -08002481 String8 result;
2482
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002483 const size_t numEffects = mEffects.size();
2484 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002485
Marco Nelissenb2208842014-02-07 14:00:50 -08002486 if (numEffects) {
2487 bool locked = AudioFlinger::dumpTryLock(mLock);
2488 // failed to lock - AudioFlinger is probably deadlocked
2489 if (!locked) {
2490 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002491 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002492
Andy Hungbded9c82017-11-30 18:47:35 -08002493 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2494 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2495 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2496 (int)inBufferStr.size(), "In buffer ",
2497 (int)outBufferStr.size(), "Out buffer ");
2498 result.appendFormat("\t%s %s %d\n",
2499 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002500 write(fd, result.string(), result.size());
2501
2502 for (size_t i = 0; i < numEffects; ++i) {
2503 sp<EffectModule> effect = mEffects[i];
2504 if (effect != 0) {
2505 effect->dump(fd, args);
2506 }
2507 }
2508
2509 if (locked) {
2510 mLock.unlock();
2511 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002512 } else {
2513 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002514 }
2515}
2516
2517// must be called with ThreadBase::mLock held
2518void AudioFlinger::EffectChain::setEffectSuspended_l(
2519 const effect_uuid_t *type, bool suspend)
2520{
2521 sp<SuspendedEffectDesc> desc;
2522 // use effect type UUID timelow as key as there is no real risk of identical
2523 // timeLow fields among effect type UUIDs.
2524 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2525 if (suspend) {
2526 if (index >= 0) {
2527 desc = mSuspendedEffects.valueAt(index);
2528 } else {
2529 desc = new SuspendedEffectDesc();
2530 desc->mType = *type;
2531 mSuspendedEffects.add(type->timeLow, desc);
2532 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2533 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002534
Eric Laurentca7cc822012-11-19 14:55:58 -08002535 if (desc->mRefCount++ == 0) {
2536 sp<EffectModule> effect = getEffectIfEnabled(type);
2537 if (effect != 0) {
2538 desc->mEffect = effect;
2539 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002540 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002541 }
2542 }
2543 } else {
2544 if (index < 0) {
2545 return;
2546 }
2547 desc = mSuspendedEffects.valueAt(index);
2548 if (desc->mRefCount <= 0) {
2549 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002550 desc->mRefCount = 0;
2551 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002552 }
2553 if (--desc->mRefCount == 0) {
2554 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2555 if (desc->mEffect != 0) {
2556 sp<EffectModule> effect = desc->mEffect.promote();
2557 if (effect != 0) {
2558 effect->setSuspended(false);
2559 effect->lock();
2560 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002561 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002562 effect->setEnabled_l(handle->enabled());
2563 }
2564 effect->unlock();
2565 }
2566 desc->mEffect.clear();
2567 }
2568 mSuspendedEffects.removeItemsAt(index);
2569 }
2570 }
2571}
2572
2573// must be called with ThreadBase::mLock held
2574void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2575{
2576 sp<SuspendedEffectDesc> desc;
2577
2578 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2579 if (suspend) {
2580 if (index >= 0) {
2581 desc = mSuspendedEffects.valueAt(index);
2582 } else {
2583 desc = new SuspendedEffectDesc();
2584 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2585 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2586 }
2587 if (desc->mRefCount++ == 0) {
2588 Vector< sp<EffectModule> > effects;
2589 getSuspendEligibleEffects(effects);
2590 for (size_t i = 0; i < effects.size(); i++) {
2591 setEffectSuspended_l(&effects[i]->desc().type, true);
2592 }
2593 }
2594 } else {
2595 if (index < 0) {
2596 return;
2597 }
2598 desc = mSuspendedEffects.valueAt(index);
2599 if (desc->mRefCount <= 0) {
2600 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2601 desc->mRefCount = 1;
2602 }
2603 if (--desc->mRefCount == 0) {
2604 Vector<const effect_uuid_t *> types;
2605 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2606 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2607 continue;
2608 }
2609 types.add(&mSuspendedEffects.valueAt(i)->mType);
2610 }
2611 for (size_t i = 0; i < types.size(); i++) {
2612 setEffectSuspended_l(types[i], false);
2613 }
2614 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2615 mSuspendedEffects.keyAt(index));
2616 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2617 }
2618 }
2619}
2620
2621
2622// The volume effect is used for automated tests only
2623#ifndef OPENSL_ES_H_
2624static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2625 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2626const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2627#endif //OPENSL_ES_H_
2628
Eric Laurentd8365c52017-07-16 15:27:05 -07002629/* static */
2630bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2631{
2632 // Only NS and AEC are suspended when BtNRec is off
2633 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2634 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2635 return true;
2636 }
2637 return false;
2638}
2639
Eric Laurentca7cc822012-11-19 14:55:58 -08002640bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2641{
2642 // auxiliary effects and visualizer are never suspended on output mix
2643 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2644 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2645 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002646 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2647 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002648 return false;
2649 }
2650 return true;
2651}
2652
2653void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2654 Vector< sp<AudioFlinger::EffectModule> > &effects)
2655{
2656 effects.clear();
2657 for (size_t i = 0; i < mEffects.size(); i++) {
2658 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2659 effects.add(mEffects[i]);
2660 }
2661 }
2662}
2663
2664sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2665 const effect_uuid_t *type)
2666{
2667 sp<EffectModule> effect = getEffectFromType_l(type);
2668 return effect != 0 && effect->isEnabled() ? effect : 0;
2669}
2670
2671void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2672 bool enabled)
2673{
2674 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2675 if (enabled) {
2676 if (index < 0) {
2677 // if the effect is not suspend check if all effects are suspended
2678 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2679 if (index < 0) {
2680 return;
2681 }
2682 if (!isEffectEligibleForSuspend(effect->desc())) {
2683 return;
2684 }
2685 setEffectSuspended_l(&effect->desc().type, enabled);
2686 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2687 if (index < 0) {
2688 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2689 return;
2690 }
2691 }
2692 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2693 effect->desc().type.timeLow);
2694 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002695 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002696 if (desc->mEffect == 0) {
2697 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002698 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002699 effect->setSuspended(true);
2700 }
2701 } else {
2702 if (index < 0) {
2703 return;
2704 }
2705 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2706 effect->desc().type.timeLow);
2707 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2708 desc->mEffect.clear();
2709 effect->setSuspended(false);
2710 }
2711}
2712
Eric Laurent5baf2af2013-09-12 17:37:00 -07002713bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002714{
2715 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002716 return isNonOffloadableEnabled_l();
2717}
2718
2719bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2720{
Eric Laurent813e2a72013-08-31 12:59:48 -07002721 size_t size = mEffects.size();
2722 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002723 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002724 return true;
2725 }
2726 }
2727 return false;
2728}
2729
Eric Laurentaaa44472014-09-12 17:41:50 -07002730void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2731{
2732 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002733 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002734}
2735
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002736void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2737{
2738 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2739 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2740 }
2741 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2742 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2743 }
2744}
2745
2746void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2747{
2748 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2749 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2750 }
2751 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2752 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2753 }
2754}
2755
2756bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002757{
2758 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002759 for (const auto &effect : mEffects) {
2760 if (effect->isProcessImplemented()) {
2761 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002762 }
2763 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002764 // Allow effects without processing.
2765 return true;
2766}
2767
2768bool AudioFlinger::EffectChain::isFastCompatible() const
2769{
2770 Mutex::Autolock _l(mLock);
2771 for (const auto &effect : mEffects) {
2772 if (effect->isProcessImplemented()
2773 && effect->isImplementationSoftware()) {
2774 return false;
2775 }
2776 }
2777 // Allow effects without processing or hw accelerated effects.
2778 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002779}
2780
2781// isCompatibleWithThread_l() must be called with thread->mLock held
2782bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2783{
2784 Mutex::Autolock _l(mLock);
2785 for (size_t i = 0; i < mEffects.size(); i++) {
2786 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2787 return false;
2788 }
2789 }
2790 return true;
2791}
2792
Eric Laurent6b446ce2019-12-13 10:56:31 -08002793// EffectCallbackInterface implementation
2794status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2795 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2796 sp<EffectHalInterface> *effect) {
2797 status_t status = NO_INIT;
2798 sp<AudioFlinger> af = mAudioFlinger.promote();
2799 if (af == nullptr) {
2800 return status;
2801 }
2802 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2803 if (effectsFactory != 0) {
2804 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2805 }
2806 return status;
2807}
2808
2809bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002810 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002811 sp<AudioFlinger> af = mAudioFlinger.promote();
2812 if (af == nullptr) {
2813 return false;
2814 }
Eric Laurent41709552019-12-16 19:34:05 -08002815 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2816 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002817}
2818
2819status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2820 size_t size, sp<EffectBufferHalInterface>* buffer) {
2821 sp<AudioFlinger> af = mAudioFlinger.promote();
2822 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2823 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2824}
2825
2826status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2827 sp<EffectHalInterface> effect) {
2828 status_t result = NO_INIT;
2829 sp<ThreadBase> t = mThread.promote();
2830 if (t == nullptr) {
2831 return result;
2832 }
2833 sp <StreamHalInterface> st = t->stream();
2834 if (st == nullptr) {
2835 return result;
2836 }
2837 result = st->addEffect(effect);
2838 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2839 return result;
2840}
2841
2842status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2843 sp<EffectHalInterface> effect) {
2844 status_t result = NO_INIT;
2845 sp<ThreadBase> t = mThread.promote();
2846 if (t == nullptr) {
2847 return result;
2848 }
2849 sp <StreamHalInterface> st = t->stream();
2850 if (st == nullptr) {
2851 return result;
2852 }
2853 result = st->removeEffect(effect);
2854 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2855 return result;
2856}
2857
2858audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2859 sp<ThreadBase> t = mThread.promote();
2860 if (t == nullptr) {
2861 return AUDIO_IO_HANDLE_NONE;
2862 }
2863 return t->id();
2864}
2865
2866bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2867 sp<ThreadBase> t = mThread.promote();
2868 if (t == nullptr) {
2869 return true;
2870 }
2871 return t->isOutput();
2872}
2873
2874bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2875 sp<ThreadBase> t = mThread.promote();
2876 if (t == nullptr) {
2877 return false;
2878 }
2879 return t->type() == ThreadBase::OFFLOAD;
2880}
2881
2882bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2883 sp<ThreadBase> t = mThread.promote();
2884 if (t == nullptr) {
2885 return false;
2886 }
2887 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2888}
2889
2890bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2891 sp<ThreadBase> t = mThread.promote();
2892 if (t == nullptr) {
2893 return false;
2894 }
Andy Hungea840382020-05-05 21:50:17 -07002895 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002896}
2897
2898uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2899 sp<ThreadBase> t = mThread.promote();
2900 if (t == nullptr) {
2901 return 0;
2902 }
2903 return t->sampleRate();
2904}
2905
2906audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2907 sp<ThreadBase> t = mThread.promote();
2908 if (t == nullptr) {
2909 return AUDIO_CHANNEL_NONE;
2910 }
2911 return t->channelMask();
2912}
2913
2914uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2915 sp<ThreadBase> t = mThread.promote();
2916 if (t == nullptr) {
2917 return 0;
2918 }
2919 return t->channelCount();
2920}
2921
jiabineb3bda02020-06-30 14:07:03 -07002922audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
2923 sp<ThreadBase> t = mThread.promote();
2924 if (t == nullptr) {
2925 return AUDIO_CHANNEL_NONE;
2926 }
2927 return t->hapticChannelMask();
2928}
2929
Eric Laurent6b446ce2019-12-13 10:56:31 -08002930size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2931 sp<ThreadBase> t = mThread.promote();
2932 if (t == nullptr) {
2933 return 0;
2934 }
2935 return t->frameCount();
2936}
2937
2938uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2939 sp<ThreadBase> t = mThread.promote();
2940 if (t == nullptr) {
2941 return 0;
2942 }
2943 return t->latency_l();
2944}
2945
2946void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2947 sp<ThreadBase> t = mThread.promote();
2948 if (t == nullptr) {
2949 return;
2950 }
2951 t->setVolumeForOutput_l(left, right);
2952}
2953
2954void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002955 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002956 sp<ThreadBase> t = mThread.promote();
2957 if (t == nullptr) {
2958 return;
2959 }
2960 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2961
2962 sp<EffectChain> c = mChain.promote();
2963 if (c == nullptr) {
2964 return;
2965 }
Eric Laurent41709552019-12-16 19:34:05 -08002966 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2967 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002968}
2969
Eric Laurent41709552019-12-16 19:34:05 -08002970void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002971 sp<ThreadBase> t = mThread.promote();
2972 if (t == nullptr) {
2973 return;
2974 }
Eric Laurent41709552019-12-16 19:34:05 -08002975 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2976 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002977}
2978
Eric Laurent41709552019-12-16 19:34:05 -08002979void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002980 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2981
2982 sp<ThreadBase> t = mThread.promote();
2983 if (t == nullptr) {
2984 return;
2985 }
2986 t->onEffectDisable();
2987}
2988
2989bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2990 bool unpinIfLast) {
2991 sp<ThreadBase> t = mThread.promote();
2992 if (t == nullptr) {
2993 return false;
2994 }
2995 t->disconnectEffectHandle(handle, unpinIfLast);
2996 return true;
2997}
2998
2999void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
3000 sp<EffectChain> c = mChain.promote();
3001 if (c == nullptr) {
3002 return;
3003 }
3004 c->resetVolume_l();
3005
3006}
3007
3008uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
3009 sp<EffectChain> c = mChain.promote();
3010 if (c == nullptr) {
3011 return PRODUCT_STRATEGY_NONE;
3012 }
3013 return c->strategy();
3014}
3015
3016int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
3017 sp<EffectChain> c = mChain.promote();
3018 if (c == nullptr) {
3019 return 0;
3020 }
3021 return c->activeTrackCnt();
3022}
3023
Eric Laurentb82e6b72019-11-22 17:25:04 -08003024
3025#undef LOG_TAG
3026#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3027
3028status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3029{
3030 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3031 Mutex::Autolock _l(mProxyLock);
3032 if (status == NO_ERROR) {
3033 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003034 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003035 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003036 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003037 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003038 bs = handle.second->disable(&status);
3039 }
3040 if (!bs.isOk()) {
3041 status = bs.transactionError();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003042 }
3043 }
3044 }
3045 ALOGV("%s enable %d status %d", __func__, enabled, status);
3046 return status;
3047}
3048
3049status_t AudioFlinger::DeviceEffectProxy::init(
3050 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3051//For all audio patches
3052//If src or sink device match
3053//If the effect is HW accelerated
3054// if no corresponding effect module
3055// Create EffectModule: mHalEffect
3056//Create and attach EffectHandle
3057//If the effect is not HW accelerated and the patch sink or src is a mixer port
3058// Create Effect on patch input or output thread on session -1
3059//Add EffectHandle to EffectHandle map of Effect Proxy:
3060 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3061 status_t status = NO_ERROR;
3062 for (auto &patch : patches) {
3063 status = onCreatePatch(patch.first, patch.second);
3064 ALOGV("%s onCreatePatch status %d", __func__, status);
3065 if (status == BAD_VALUE) {
3066 return status;
3067 }
3068 }
3069 return status;
3070}
3071
3072status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3073 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3074 status_t status = NAME_NOT_FOUND;
3075 sp<EffectHandle> handle;
3076 // only consider source[0] as this is the only "true" source of a patch
3077 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3078 ALOGV("%s source checkPort status %d", __func__, status);
3079 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3080 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3081 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3082 }
3083 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3084 Mutex::Autolock _l(mProxyLock);
3085 mEffectHandles.emplace(patchHandle, handle);
3086 }
3087 ALOGW_IF(status == BAD_VALUE,
3088 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3089
3090 return status;
3091}
3092
3093status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3094 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3095
3096 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3097 __func__, port->type, port->ext.device.type,
3098 port->ext.device.address, port->id, patch.isSoftware());
3099 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003100 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003101 return NAME_NOT_FOUND;
3102 }
3103 status_t status = NAME_NOT_FOUND;
3104
3105 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3106 Mutex::Autolock _l(mProxyLock);
3107 mDevicePort = *port;
3108 mHalEffect = new EffectModule(mMyCallback,
3109 const_cast<effect_descriptor_t *>(&mDescriptor),
3110 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3111 false /* pinned */, port->id);
3112 if (audio_is_input_device(mDevice.mType)) {
3113 mHalEffect->setInputDevice(mDevice);
3114 } else {
3115 mHalEffect->setDevices({mDevice});
3116 }
3117 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3118 status = (*handle)->initCheck();
3119 if (status == OK) {
3120 status = mHalEffect->addHandle((*handle).get());
3121 } else {
3122 mHalEffect.clear();
3123 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3124 }
3125 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3126 sp <ThreadBase> thread;
3127 if (audio_port_config_has_input_direction(port)) {
3128 if (patch.isSoftware()) {
3129 thread = patch.mRecord.thread();
3130 } else {
3131 thread = patch.thread().promote();
3132 }
3133 } else {
3134 if (patch.isSoftware()) {
3135 thread = patch.mPlayback.thread();
3136 } else {
3137 thread = patch.thread().promote();
3138 }
3139 }
3140 int enabled;
3141 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3142 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003143 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003144 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3145 } else {
3146 status = BAD_VALUE;
3147 }
3148 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003149 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003150 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003151 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003152 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003153 bs = (*handle)->disable(&status);
3154 }
3155 if (!bs.isOk()) {
3156 status = bs.transactionError();
Eric Laurentb82e6b72019-11-22 17:25:04 -08003157 }
3158 }
3159 return status;
3160}
3161
3162void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3163 Mutex::Autolock _l(mProxyLock);
3164 mEffectHandles.erase(patchHandle);
3165}
3166
3167
3168size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3169{
3170 Mutex::Autolock _l(mProxyLock);
3171 if (effect == mHalEffect) {
3172 mHalEffect.clear();
3173 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3174 }
3175 return mHalEffect == nullptr ? 0 : 1;
3176}
3177
3178status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3179 sp<EffectHalInterface> effect) {
3180 if (mHalEffect == nullptr) {
3181 return NO_INIT;
3182 }
3183 return mManagerCallback->addEffectToHal(
3184 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3185}
3186
3187status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3188 sp<EffectHalInterface> effect) {
3189 if (mHalEffect == nullptr) {
3190 return NO_INIT;
3191 }
3192 return mManagerCallback->removeEffectFromHal(
3193 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3194}
3195
3196bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3197 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3198 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3199 }
3200 return true;
3201}
3202
3203uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3204 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3205 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3206 return mDevicePort.sample_rate;
3207 }
3208 return DEFAULT_OUTPUT_SAMPLE_RATE;
3209}
3210
3211audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3212 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3213 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3214 return mDevicePort.channel_mask;
3215 }
3216 return AUDIO_CHANNEL_OUT_STEREO;
3217}
3218
3219uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3220 if (isOutput()) {
3221 return audio_channel_count_from_out_mask(channelMask());
3222 }
3223 return audio_channel_count_from_in_mask(channelMask());
3224}
3225
3226void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3227 const Vector<String16> args;
3228 EffectBase::dump(fd, args);
3229
3230 const bool locked = dumpTryLock(mProxyLock);
3231 if (!locked) {
3232 String8 result("DeviceEffectProxy may be deadlocked\n");
3233 write(fd, result.string(), result.size());
3234 }
3235
3236 String8 outStr;
3237 if (mHalEffect != nullptr) {
3238 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3239 } else {
3240 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3241 }
3242 write(fd, outStr.string(), outStr.size());
3243 outStr.clear();
3244
3245 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3246 write(fd, outStr.string(), outStr.size());
3247 outStr.clear();
3248
3249 for (const auto& iter : mEffectHandles) {
3250 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3251 write(fd, outStr.string(), outStr.size());
3252 outStr.clear();
3253 sp<EffectBase> effect = iter.second->effect().promote();
3254 if (effect != nullptr) {
3255 effect->dump(fd, args);
3256 }
3257 }
3258
3259 if (locked) {
3260 mLock.unlock();
3261 }
3262}
3263
3264#undef LOG_TAG
3265#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3266
3267int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3268 return mManagerCallback->newEffectId();
3269}
3270
3271
3272bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3273 EffectHandle *handle, bool unpinIfLast) {
3274 sp<EffectBase> effectBase = handle->effect().promote();
3275 if (effectBase == nullptr) {
3276 return false;
3277 }
3278
3279 sp<EffectModule> effect = effectBase->asEffectModule();
3280 if (effect == nullptr) {
3281 return false;
3282 }
3283
3284 // restore suspended effects if the disconnected handle was enabled and the last one.
3285 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3286 if (remove) {
3287 sp<DeviceEffectProxy> proxy = mProxy.promote();
3288 if (proxy != nullptr) {
3289 proxy->removeEffect(effect);
3290 }
3291 if (handle->enabled()) {
3292 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3293 }
3294 }
3295 return true;
3296}
3297
3298status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3299 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3300 sp<EffectHalInterface> *effect) {
3301 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3302}
3303
3304status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3305 sp<EffectHalInterface> effect) {
3306 sp<DeviceEffectProxy> proxy = mProxy.promote();
3307 if (proxy == nullptr) {
3308 return NO_INIT;
3309 }
3310 return proxy->addEffectToHal(effect);
3311}
3312
3313status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3314 sp<EffectHalInterface> effect) {
3315 sp<DeviceEffectProxy> proxy = mProxy.promote();
3316 if (proxy == nullptr) {
3317 return NO_INIT;
3318 }
3319 return proxy->addEffectToHal(effect);
3320}
3321
3322bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3323 sp<DeviceEffectProxy> proxy = mProxy.promote();
3324 if (proxy == nullptr) {
3325 return true;
3326 }
3327 return proxy->isOutput();
3328}
3329
3330uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3331 sp<DeviceEffectProxy> proxy = mProxy.promote();
3332 if (proxy == nullptr) {
3333 return DEFAULT_OUTPUT_SAMPLE_RATE;
3334 }
3335 return proxy->sampleRate();
3336}
3337
3338audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3339 sp<DeviceEffectProxy> proxy = mProxy.promote();
3340 if (proxy == nullptr) {
3341 return AUDIO_CHANNEL_OUT_STEREO;
3342 }
3343 return proxy->channelMask();
3344}
3345
3346uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3347 sp<DeviceEffectProxy> proxy = mProxy.promote();
3348 if (proxy == nullptr) {
3349 return 2;
3350 }
3351 return proxy->channelCount();
3352}
3353
Glenn Kasten63238ef2015-03-02 15:50:29 -08003354} // namespace android