blob: 3ab7737d07af35fecf16480ed32a1230676c23a6 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
jiabineb3bda02020-06-30 14:07:03 -070028#include <system/audio_effects/effect_hapticgenerator.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070029#include <system/audio_effects/effect_ns.h>
30#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080031#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080032#include <audio_utils/primitives.h>
Mikhail Naganovf698ff22020-03-31 10:07:29 -070033#include <media/AudioCommonTypes.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070035#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080036#include <media/AudioDeviceTypeAddr.h>
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070037#include <media/ShmemCompat.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070038#include <media/audiohal/EffectHalInterface.h>
39#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070040#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080041
42#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080043
44// ----------------------------------------------------------------------------
45
46// Note: the following macro is used for extremely verbose logging message. In
47// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
48// 0; but one side effect of this is to turn all LOGV's as well. Some messages
49// are so verbose that we want to suppress them even when we have ALOG_ASSERT
50// turned on. Do not uncomment the #def below unless you really know what you
51// are doing and want to see all of the extremely verbose messages.
52//#define VERY_VERY_VERBOSE_LOGGING
53#ifdef VERY_VERY_VERBOSE_LOGGING
54#define ALOGVV ALOGV
55#else
56#define ALOGVV(a...) do { } while(0)
57#endif
58
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090059#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
60
Eric Laurentca7cc822012-11-19 14:55:58 -080061namespace android {
62
Andy Hung1131b6e2020-12-08 20:47:45 -080063using aidl_utils::statusTFromBinderStatus;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070064using binder::Status;
65
66namespace {
67
68// Append a POD value into a vector of bytes.
69template<typename T>
70void appendToBuffer(const T& value, std::vector<uint8_t>* buffer) {
71 const uint8_t* ar(reinterpret_cast<const uint8_t*>(&value));
72 buffer->insert(buffer->end(), ar, ar + sizeof(T));
73}
74
75// Write a POD value into a vector of bytes (clears the previous buffer
76// content).
77template<typename T>
78void writeToBuffer(const T& value, std::vector<uint8_t>* buffer) {
79 buffer->clear();
80 appendToBuffer(value, buffer);
81}
82
83} // namespace
84
Eric Laurentca7cc822012-11-19 14:55:58 -080085// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080086// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080087// ----------------------------------------------------------------------------
88
89#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080090#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080091
Eric Laurent41709552019-12-16 19:34:05 -080092AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080093 effect_descriptor_t *desc,
94 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080095 audio_session_t sessionId,
96 bool pinned)
97 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080098 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080099 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -0800100{
Eric Laurentca7cc822012-11-19 14:55:58 -0800101}
102
Eric Laurent41709552019-12-16 19:34:05 -0800103// must be called with EffectModule::mLock held
104status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -0800105{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800106
Eric Laurent41709552019-12-16 19:34:05 -0800107 ALOGV("setEnabled %p enabled %d", this, enabled);
108
109 if (enabled != isEnabled()) {
110 switch (mState) {
111 // going from disabled to enabled
112 case IDLE:
113 mState = STARTING;
114 break;
115 case STOPPED:
116 mState = RESTART;
117 break;
118 case STOPPING:
119 mState = ACTIVE;
120 break;
121
122 // going from enabled to disabled
123 case RESTART:
124 mState = STOPPED;
125 break;
126 case STARTING:
127 mState = IDLE;
128 break;
129 case ACTIVE:
130 mState = STOPPING;
131 break;
132 case DESTROYED:
133 return NO_ERROR; // simply ignore as we are being destroyed
134 }
135 for (size_t i = 1; i < mHandles.size(); i++) {
136 EffectHandle *h = mHandles[i];
137 if (h != NULL && !h->disconnected()) {
138 h->setEnabled(enabled);
139 }
140 }
141 }
142 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800143}
144
Eric Laurent41709552019-12-16 19:34:05 -0800145status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
146{
147 status_t status;
148 {
149 Mutex::Autolock _l(mLock);
150 status = setEnabled_l(enabled);
151 }
152 if (fromHandle) {
153 if (enabled) {
154 if (status != NO_ERROR) {
155 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
156 } else {
157 mCallback->onEffectEnable(this);
158 }
159 } else {
160 mCallback->onEffectDisable(this);
161 }
162 }
163 return status;
164}
165
166bool AudioFlinger::EffectBase::isEnabled() const
167{
168 switch (mState) {
169 case RESTART:
170 case STARTING:
171 case ACTIVE:
172 return true;
173 case IDLE:
174 case STOPPING:
175 case STOPPED:
176 case DESTROYED:
177 default:
178 return false;
179 }
180}
181
182void AudioFlinger::EffectBase::setSuspended(bool suspended)
183{
184 Mutex::Autolock _l(mLock);
185 mSuspended = suspended;
186}
187
188bool AudioFlinger::EffectBase::suspended() const
189{
190 Mutex::Autolock _l(mLock);
191 return mSuspended;
192}
193
194status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800195{
196 status_t status;
197
198 Mutex::Autolock _l(mLock);
199 int priority = handle->priority();
200 size_t size = mHandles.size();
201 EffectHandle *controlHandle = NULL;
202 size_t i;
203 for (i = 0; i < size; i++) {
204 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800205 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800206 continue;
207 }
208 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700209 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800210 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700211 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800212 if (h->priority() <= priority) {
213 break;
214 }
215 }
216 // if inserted in first place, move effect control from previous owner to this handle
217 if (i == 0) {
218 bool enabled = false;
219 if (controlHandle != NULL) {
220 enabled = controlHandle->enabled();
221 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
222 }
223 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
224 status = NO_ERROR;
225 } else {
226 status = ALREADY_EXISTS;
227 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700228 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800229 mHandles.insertAt(handle, i);
230 return status;
231}
232
Eric Laurent41709552019-12-16 19:34:05 -0800233status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700234{
235 status_t status = NO_ERROR;
236 bool doRegister = false;
237 bool registered = false;
238 bool doEnable = false;
239 bool enabled = false;
Mikhail Naganov379d6872020-03-26 13:04:11 -0700240 audio_io_handle_t io = AUDIO_IO_HANDLE_NONE;
Mikhail Naganovf698ff22020-03-31 10:07:29 -0700241 uint32_t strategy = PRODUCT_STRATEGY_NONE;
Eric Laurent6c796322019-04-09 14:13:17 -0700242
243 {
244 Mutex::Autolock _l(mLock);
245 // register effect when first handle is attached and unregister when last handle is removed
246 if (mPolicyRegistered != mHandles.size() > 0) {
247 doRegister = true;
248 mPolicyRegistered = mHandles.size() > 0;
249 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800250 io = mCallback->io();
251 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700252 }
253 }
254 // enable effect when registered according to enable state requested by controlling handle
255 if (mHandles.size() > 0) {
256 EffectHandle *handle = controlHandle_l();
257 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
258 doEnable = true;
259 mPolicyEnabled = handle->enabled();
260 }
261 }
262 registered = mPolicyRegistered;
263 enabled = mPolicyEnabled;
264 mPolicyLock.lock();
265 }
266 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
267 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
268 if (doRegister) {
269 if (registered) {
270 status = AudioSystem::registerEffect(
271 &mDescriptor,
272 io,
273 strategy,
274 mSessionId,
275 mId);
276 } else {
277 status = AudioSystem::unregisterEffect(mId);
278 }
279 }
280 if (registered && doEnable) {
281 status = AudioSystem::setEffectEnabled(mId, enabled);
282 }
283 mPolicyLock.unlock();
284
285 return status;
286}
287
288
Eric Laurent41709552019-12-16 19:34:05 -0800289ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800290{
291 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800292 return removeHandle_l(handle);
293}
294
Eric Laurent41709552019-12-16 19:34:05 -0800295ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800296{
Eric Laurentca7cc822012-11-19 14:55:58 -0800297 size_t size = mHandles.size();
298 size_t i;
299 for (i = 0; i < size; i++) {
300 if (mHandles[i] == handle) {
301 break;
302 }
303 }
304 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800305 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
306 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800307 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800308 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800309
310 mHandles.removeAt(i);
311 // if removed from first place, move effect control from this handle to next in line
312 if (i == 0) {
313 EffectHandle *h = controlHandle_l();
314 if (h != NULL) {
315 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
316 }
317 }
318
Jaideep Sharmaed8688022020-08-07 14:09:16 +0530319 // Prevent calls to process() and other functions on effect interface from now on.
320 // The effect engine will be released by the destructor when the last strong reference on
321 // this object is released which can happen after next process is called.
Eric Laurentca7cc822012-11-19 14:55:58 -0800322 if (mHandles.size() == 0 && !mPinned) {
323 mState = DESTROYED;
324 }
325
326 return mHandles.size();
327}
328
329// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800330AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800331{
332 // the first valid handle in the list has control over the module
333 for (size_t i = 0; i < mHandles.size(); i++) {
334 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800335 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800336 return h;
337 }
338 }
339
340 return NULL;
341}
342
Eric Laurentf10c7092016-12-06 17:09:56 -0800343// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800344ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800345{
346 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800347 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
348 return mHandles.size();
349 }
350
Eric Laurentf10c7092016-12-06 17:09:56 -0800351 Mutex::Autolock _l(mLock);
352 ssize_t numHandles = removeHandle_l(handle);
353 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800354 mLock.unlock();
355 mCallback->updateOrphanEffectChains(this);
356 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800357 }
358 return numHandles;
359}
360
Eric Laurent41709552019-12-16 19:34:05 -0800361bool AudioFlinger::EffectBase::purgeHandles()
362{
363 bool enabled = false;
364 Mutex::Autolock _l(mLock);
365 EffectHandle *handle = controlHandle_l();
366 if (handle != NULL) {
367 enabled = handle->enabled();
368 }
369 mHandles.clear();
370 return enabled;
371}
372
373void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
374 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
375}
376
377static String8 effectFlagsToString(uint32_t flags) {
378 String8 s;
379
380 s.append("conn. mode: ");
381 switch (flags & EFFECT_FLAG_TYPE_MASK) {
382 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
383 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
384 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
385 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
386 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
387 default: s.append("unknown/reserved"); break;
388 }
389 s.append(", ");
390
391 s.append("insert pref: ");
392 switch (flags & EFFECT_FLAG_INSERT_MASK) {
393 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
394 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
395 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
396 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
397 default: s.append("unknown/reserved"); break;
398 }
399 s.append(", ");
400
401 s.append("volume mgmt: ");
402 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
403 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
404 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
405 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
406 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
407 default: s.append("unknown/reserved"); break;
408 }
409 s.append(", ");
410
411 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
412 if (devind) {
413 s.append("device indication: ");
414 switch (devind) {
415 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
416 default: s.append("unknown/reserved"); break;
417 }
418 s.append(", ");
419 }
420
421 s.append("input mode: ");
422 switch (flags & EFFECT_FLAG_INPUT_MASK) {
423 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
424 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
425 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
426 default: s.append("not set"); break;
427 }
428 s.append(", ");
429
430 s.append("output mode: ");
431 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
432 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
433 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
434 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
435 default: s.append("not set"); break;
436 }
437 s.append(", ");
438
439 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
440 if (accel) {
441 s.append("hardware acceleration: ");
442 switch (accel) {
443 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
444 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
445 default: s.append("unknown/reserved"); break;
446 }
447 s.append(", ");
448 }
449
450 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
451 if (modeind) {
452 s.append("mode indication: ");
453 switch (modeind) {
454 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
455 default: s.append("unknown/reserved"); break;
456 }
457 s.append(", ");
458 }
459
460 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
461 if (srcind) {
462 s.append("source indication: ");
463 switch (srcind) {
464 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
465 default: s.append("unknown/reserved"); break;
466 }
467 s.append(", ");
468 }
469
470 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
471 s.append("offloadable, ");
472 }
473
474 int len = s.length();
475 if (s.length() > 2) {
476 (void) s.lockBuffer(len);
477 s.unlockBuffer(len - 2);
478 }
479 return s;
480}
481
482void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
483{
484 String8 result;
485
486 result.appendFormat("\tEffect ID %d:\n", mId);
487
488 bool locked = AudioFlinger::dumpTryLock(mLock);
489 // failed to lock - AudioFlinger is probably deadlocked
490 if (!locked) {
491 result.append("\t\tCould not lock Fx mutex:\n");
492 }
493
494 result.append("\t\tSession State Registered Enabled Suspended:\n");
495 result.appendFormat("\t\t%05d %03d %s %s %s\n",
496 mSessionId, mState, mPolicyRegistered ? "y" : "n",
497 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
498
499 result.append("\t\tDescriptor:\n");
500 char uuidStr[64];
501 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
502 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
503 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
504 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
505 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
506 mDescriptor.apiVersion,
507 mDescriptor.flags,
508 effectFlagsToString(mDescriptor.flags).string());
509 result.appendFormat("\t\t- name: %s\n",
510 mDescriptor.name);
511
512 result.appendFormat("\t\t- implementor: %s\n",
513 mDescriptor.implementor);
514
515 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
516 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
517 char buffer[256];
518 for (size_t i = 0; i < mHandles.size(); ++i) {
519 EffectHandle *handle = mHandles[i];
520 if (handle != NULL && !handle->disconnected()) {
521 handle->dumpToBuffer(buffer, sizeof(buffer));
522 result.append(buffer);
523 }
524 }
525 if (locked) {
526 mLock.unlock();
527 }
528
529 write(fd, result.string(), result.length());
530}
531
532// ----------------------------------------------------------------------------
533// EffectModule implementation
534// ----------------------------------------------------------------------------
535
536#undef LOG_TAG
537#define LOG_TAG "AudioFlinger::EffectModule"
538
539AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
540 effect_descriptor_t *desc,
541 int id,
542 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800543 bool pinned,
544 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800545 : EffectBase(callback, desc, id, sessionId, pinned),
546 // clear mConfig to ensure consistent initial value of buffer framecount
547 // in case buffers are associated by setInBuffer() or setOutBuffer()
548 // prior to configure().
549 mConfig{{}, {}},
550 mStatus(NO_INIT),
551 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
552 mDisableWaitCnt(0), // set by process() and updateState()
553 mOffloaded(false)
554#ifdef FLOAT_EFFECT_CHAIN
555 , mSupportsFloat(false)
556#endif
557{
558 ALOGV("Constructor %p pinned %d", this, pinned);
559 int lStatus;
560
561 // create effect engine from effect factory
562 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800563 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800564 if (mStatus != NO_ERROR) {
565 return;
566 }
567 lStatus = init();
568 if (lStatus < 0) {
569 mStatus = lStatus;
570 goto Error;
571 }
572
573 setOffloaded(callback->isOffload(), callback->io());
574 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
575
576 return;
577Error:
578 mEffectInterface.clear();
579 ALOGV("Constructor Error %d", mStatus);
580}
581
582AudioFlinger::EffectModule::~EffectModule()
583{
584 ALOGV("Destructor %p", this);
585 if (mEffectInterface != 0) {
586 char uuidStr[64];
587 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
588 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
589 this, uuidStr);
590 release_l();
591 }
592
593}
594
Eric Laurentfa1e1232016-08-02 19:01:49 -0700595bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800596 Mutex::Autolock _l(mLock);
597
Eric Laurentfa1e1232016-08-02 19:01:49 -0700598 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800599 switch (mState) {
600 case RESTART:
601 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700602 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800603
604 case STARTING:
605 // clear auxiliary effect input buffer for next accumulation
606 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
607 memset(mConfig.inputCfg.buffer.raw,
608 0,
609 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
610 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700611 if (start_l() == NO_ERROR) {
612 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700613 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700614 } else {
615 mState = IDLE;
616 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800617 break;
618 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900619 // volume control for offload and direct threads must take effect immediately.
620 if (stop_l() == NO_ERROR
621 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700622 mDisableWaitCnt = mMaxDisableWaitCnt;
623 } else {
624 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
625 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800626 mState = STOPPED;
627 break;
628 case STOPPED:
629 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
630 // turn off sequence.
631 if (--mDisableWaitCnt == 0) {
632 reset_l();
633 mState = IDLE;
634 }
635 break;
636 default: //IDLE , ACTIVE, DESTROYED
637 break;
638 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700639
640 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800641}
642
643void AudioFlinger::EffectModule::process()
644{
645 Mutex::Autolock _l(mLock);
646
Mikhail Naganov022b9952017-01-04 16:36:51 -0800647 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800648 return;
649 }
650
rago94a1ee82017-07-21 15:11:02 -0700651 const uint32_t inChannelCount =
652 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
653 const uint32_t outChannelCount =
654 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
655 const bool auxType =
656 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
657
Andy Hungfa69ca32017-11-30 10:07:53 -0800658 // safeInputOutputSampleCount is 0 if the channel count between input and output
659 // buffers do not match. This prevents automatic accumulation or copying between the
660 // input and output effect buffers without an intermediary effect process.
661 // TODO: consider implementing channel conversion.
662 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700663 mInChannelCountRequested != mOutChannelCountRequested ? 0
664 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800665 mConfig.inputCfg.buffer.frameCount,
666 mConfig.outputCfg.buffer.frameCount);
667 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
668#ifdef FLOAT_EFFECT_CHAIN
669 accumulate_float(
670 mConfig.outputCfg.buffer.f32,
671 mConfig.inputCfg.buffer.f32,
672 safeInputOutputSampleCount);
673#else
674 accumulate_i16(
675 mConfig.outputCfg.buffer.s16,
676 mConfig.inputCfg.buffer.s16,
677 safeInputOutputSampleCount);
678#endif
679 };
680 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
681#ifdef FLOAT_EFFECT_CHAIN
682 memcpy(
683 mConfig.outputCfg.buffer.f32,
684 mConfig.inputCfg.buffer.f32,
685 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
686
687#else
688 memcpy(
689 mConfig.outputCfg.buffer.s16,
690 mConfig.inputCfg.buffer.s16,
691 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
692#endif
693 };
694
Eric Laurentca7cc822012-11-19 14:55:58 -0800695 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700696 int ret;
697 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700698 if (auxType) {
699 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800700 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700701#ifdef FLOAT_EFFECT_CHAIN
702 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800703#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700704 // Do in-place float conversion for auxiliary effect input buffer.
705 static_assert(sizeof(float) <= sizeof(int32_t),
706 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
707
Andy Hungfa69ca32017-11-30 10:07:53 -0800708 memcpy_to_float_from_q4_27(
709 mConfig.inputCfg.buffer.f32,
710 mConfig.inputCfg.buffer.s32,
711 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800712#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800713 } else
Andy Hung116a4982017-11-30 10:15:08 -0800714#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800715 {
Andy Hung116a4982017-11-30 10:15:08 -0800716#ifdef FLOAT_AUX
717 memcpy_to_i16_from_float(
718 mConfig.inputCfg.buffer.s16,
719 mConfig.inputCfg.buffer.f32,
720 mConfig.inputCfg.buffer.frameCount);
721#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800722 memcpy_to_i16_from_q4_27(
723 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700724 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800725 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800726#endif
rago94a1ee82017-07-21 15:11:02 -0700727 }
rago94a1ee82017-07-21 15:11:02 -0700728 }
729#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800730 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
731 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
732
733 if (!auxType && mInChannelCountRequested != inChannelCount) {
734 adjust_channels(
735 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
736 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
737 sizeof(float),
738 sizeof(float)
739 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
740 inBuffer = mInConversionBuffer;
741 }
742 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
743 && mOutChannelCountRequested != outChannelCount) {
744 adjust_selected_channels(
745 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
746 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
747 sizeof(float),
748 sizeof(float)
749 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
750 outBuffer = mOutConversionBuffer;
751 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800752 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
753 if (!auxType) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800754 if (mInConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800755 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
756 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700757 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 memcpy_to_i16_from_float(
759 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800760 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800761 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800762 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700763 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800764 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800765 if (mOutConversionBuffer == nullptr) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800766 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
767 goto data_bypass;
768 }
769 memcpy_to_i16_from_float(
770 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800773 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700774 }
775 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800776#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800777 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800778#ifdef FLOAT_EFFECT_CHAIN
779 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800780 sp<EffectBufferHalInterface> target =
781 mOutChannelCountRequested != outChannelCount
782 ? mOutConversionBuffer : mOutBuffer;
783
Andy Hungfa69ca32017-11-30 10:07:53 -0800784 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800785 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800786 mOutConversionBuffer->audioBuffer()->s16,
787 outChannelCount * mConfig.outputCfg.buffer.frameCount);
788 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800789 if (mOutChannelCountRequested != outChannelCount) {
790 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
791 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
792 sizeof(float),
793 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
794 }
rago94a1ee82017-07-21 15:11:02 -0700795#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700796 } else {
rago94a1ee82017-07-21 15:11:02 -0700797#ifdef FLOAT_EFFECT_CHAIN
798 data_bypass:
799#endif
800 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800801 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700802 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800803 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700804 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800805 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700806 }
807 }
808 ret = -ENODATA;
809 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800810
Eric Laurentca7cc822012-11-19 14:55:58 -0800811 // force transition to IDLE state when engine is ready
812 if (mState == STOPPED && ret == -ENODATA) {
813 mDisableWaitCnt = 1;
814 }
815
816 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700817 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800818#ifdef FLOAT_AUX
819 const size_t size =
820 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
821#else
rago94a1ee82017-07-21 15:11:02 -0700822 const size_t size =
823 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800824#endif
rago94a1ee82017-07-21 15:11:02 -0700825 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800826 }
827 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700828 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800829 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
830 // If an insert effect is idle and input buffer is different from output buffer,
831 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800832 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700833 // similar handling with data_bypass above.
834 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
835 accumulateInputToOutput();
836 } else { // EFFECT_BUFFER_ACCESS_WRITE
837 copyInputToOutput();
838 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800839 }
840 }
841}
842
843void AudioFlinger::EffectModule::reset_l()
844{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700845 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800846 return;
847 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700848 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800849}
850
851status_t AudioFlinger::EffectModule::configure()
852{
rago94a1ee82017-07-21 15:11:02 -0700853 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700854 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700855 uint32_t size;
856 audio_channel_mask_t channelMask;
857
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700858 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700859 status = NO_INIT;
860 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800861 }
862
Eric Laurentca7cc822012-11-19 14:55:58 -0800863 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800864 // TODO: handle configuration of input (record) SW effects above the HAL,
865 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
866 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800867 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800868 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700869 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800870
871 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800872 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
873 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
874 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
875 mConfig.inputCfg.channels);
876 }
877#ifndef MULTICHANNEL_EFFECT_CHAIN
878 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
879 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
880 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
881 mConfig.outputCfg.channels);
882 }
883#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800884 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800885#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700886 // TODO: Update this logic when multichannel effects are implemented.
887 // For offloaded tracks consider mono output as stereo for proper effect initialization
888 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
889 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
890 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
891 ALOGV("Overriding effect input and output as STEREO");
892 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800893#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800894 }
jiabineb3bda02020-06-30 14:07:03 -0700895 if (isHapticGenerator()) {
896 audio_channel_mask_t hapticChannelMask = mCallback->hapticChannelMask();
897 mConfig.inputCfg.channels |= hapticChannelMask;
898 mConfig.outputCfg.channels |= hapticChannelMask;
899 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800900 mInChannelCountRequested =
901 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
902 mOutChannelCountRequested =
903 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700904
rago94a1ee82017-07-21 15:11:02 -0700905 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
906 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900907
908 // Don't use sample rate for thread if effect isn't offloadable.
Daniel Bonnevier6bc62092019-12-06 09:14:56 +0100909 if (mCallback->isOffloadOrDirect() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900910 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
911 ALOGV("Overriding effect input as 48kHz");
912 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800913 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900914 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800915 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
916 mConfig.inputCfg.bufferProvider.cookie = NULL;
917 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
918 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
919 mConfig.outputCfg.bufferProvider.cookie = NULL;
920 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
921 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
922 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
923 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800924 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800925 // always overwrites output buffer: input buffer == output buffer
926 // - in other sessions:
927 // last effect in the chain accumulates in output buffer: input buffer != output buffer
928 // other effect: overwrites output buffer: input buffer == output buffer
929 // Auxiliary effect:
930 // accumulates in output buffer: input buffer != output buffer
931 // Therefore: accumulate <=> input buffer != output buffer
932 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
933 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
934 } else {
935 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
936 }
937 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
938 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800939 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800940 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
941
Eric Laurent6b446ce2019-12-13 10:56:31 -0800942 ALOGV("configure() %p chain %p buffer %p framecount %zu",
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -0800943 this, mCallback->chain().promote().get(),
944 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800945
946 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700947 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700948 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800949 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700950 &mConfig,
951 &size,
952 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700953 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800954 status = cmdStatus;
955 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800956
957#ifdef MULTICHANNEL_EFFECT_CHAIN
958 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800959 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800960 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
961 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
962 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700963 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
964 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800965 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
966 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
967 }
968 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
969 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
970 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
971 }
972 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700973 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800974 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700975 &mConfig,
976 &size,
977 &cmdStatus);
978 if (status == NO_ERROR) {
979 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800980 }
981 }
982#endif
983
984#ifdef FLOAT_EFFECT_CHAIN
985 if (status == NO_ERROR) {
986 mSupportsFloat = true;
987 }
988
989 if (status != NO_ERROR) {
990 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
991 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
992 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
993 size = sizeof(int);
994 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
995 sizeof(mConfig),
996 &mConfig,
997 &size,
998 &cmdStatus);
999 if (status == NO_ERROR) {
1000 status = cmdStatus;
1001 }
1002 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -07001003 mSupportsFloat = false;
1004 ALOGVV("config worked with 16 bit");
1005 } else {
1006 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001007 }
rago94a1ee82017-07-21 15:11:02 -07001008 }
1009#endif
Eric Laurentca7cc822012-11-19 14:55:58 -08001010
rago94a1ee82017-07-21 15:11:02 -07001011 if (status == NO_ERROR) {
1012 // Establish Buffer strategy
1013 setInBuffer(mInBuffer);
1014 setOutBuffer(mOutBuffer);
1015
1016 // Update visualizer latency
1017 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1018 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1019 effect_param_t *p = (effect_param_t *)buf32;
1020
1021 p->psize = sizeof(uint32_t);
1022 p->vsize = sizeof(uint32_t);
1023 size = sizeof(int);
1024 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1025
Eric Laurent6b446ce2019-12-13 10:56:31 -08001026 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001027
1028 *((int32_t *)p->data + 1)= latency;
1029 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1030 sizeof(effect_param_t) + 8,
1031 &buf32,
1032 &size,
1033 &cmdStatus);
1034 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001035 }
1036
Andy Hung05083ac2017-12-14 15:00:28 -08001037 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1038 mMaxDisableWaitCnt = (uint32_t)std::max(
1039 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1040 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1041 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001042
Eric Laurentd0ebb532013-04-02 16:41:41 -07001043exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001044 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001045 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001046 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001047 return status;
1048}
1049
1050status_t AudioFlinger::EffectModule::init()
1051{
1052 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001053 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001054 return NO_INIT;
1055 }
1056 status_t cmdStatus;
1057 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001058 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1059 0,
1060 NULL,
1061 &size,
1062 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001063 if (status == 0) {
1064 status = cmdStatus;
1065 }
1066 return status;
1067}
1068
Eric Laurent1b928682014-10-02 19:41:47 -07001069void AudioFlinger::EffectModule::addEffectToHal_l()
1070{
1071 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1072 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001073 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001074 }
1075}
1076
Eric Laurentfa1e1232016-08-02 19:01:49 -07001077// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001078status_t AudioFlinger::EffectModule::start()
1079{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001080 status_t status;
1081 {
1082 Mutex::Autolock _l(mLock);
1083 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001084 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001085 if (status == NO_ERROR) {
1086 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001087 }
1088 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001089}
1090
1091status_t AudioFlinger::EffectModule::start_l()
1092{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001093 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001094 return NO_INIT;
1095 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001096 if (mStatus != NO_ERROR) {
1097 return mStatus;
1098 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001099 status_t cmdStatus;
1100 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001101 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1102 0,
1103 NULL,
1104 &size,
1105 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 if (status == 0) {
1107 status = cmdStatus;
1108 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001109 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001110 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001111 }
1112 return status;
1113}
1114
1115status_t AudioFlinger::EffectModule::stop()
1116{
1117 Mutex::Autolock _l(mLock);
1118 return stop_l();
1119}
1120
1121status_t AudioFlinger::EffectModule::stop_l()
1122{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001123 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001124 return NO_INIT;
1125 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001126 if (mStatus != NO_ERROR) {
1127 return mStatus;
1128 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001129 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001130 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001131
1132 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001133 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1134 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1135 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001136 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001137 mSetVolumeReentrantTid = INVALID_PID;
1138 }
1139
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001140 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1141 0,
1142 NULL,
1143 &size,
1144 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001145 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001146 status = cmdStatus;
1147 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001148 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001149 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001150 }
1151 return status;
1152}
1153
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001154// must be called with EffectChain::mLock held
1155void AudioFlinger::EffectModule::release_l()
1156{
1157 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001158 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001159 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001160 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001161 mEffectInterface.clear();
1162 }
1163}
1164
Eric Laurent6b446ce2019-12-13 10:56:31 -08001165status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001166{
1167 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1168 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001169 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001170 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001171 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001172}
1173
Andy Hunge4a1d912016-08-17 14:11:13 -07001174// round up delta valid if value and divisor are positive.
1175template <typename T>
1176static T roundUpDelta(const T &value, const T &divisor) {
1177 T remainder = value % divisor;
1178 return remainder == 0 ? 0 : divisor - remainder;
1179}
1180
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001181status_t AudioFlinger::EffectModule::command(int32_t cmdCode,
1182 const std::vector<uint8_t>& cmdData,
1183 int32_t maxReplySize,
1184 std::vector<uint8_t>* reply)
Eric Laurentca7cc822012-11-19 14:55:58 -08001185{
1186 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001187 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001188
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001189 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001190 return NO_INIT;
1191 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001192 if (mStatus != NO_ERROR) {
1193 return mStatus;
1194 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001195 if (maxReplySize < 0 || maxReplySize > EFFECT_PARAM_SIZE_MAX) {
1196 return -EINVAL;
1197 }
1198 size_t cmdSize = cmdData.size();
1199 const effect_param_t* param = cmdSize >= sizeof(effect_param_t)
1200 ? reinterpret_cast<const effect_param_t*>(cmdData.data())
1201 : nullptr;
Andy Hung110bc952016-06-20 15:22:52 -07001202 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001203 (param == nullptr || param->psize > cmdSize - sizeof(effect_param_t))) {
Andy Hung6660f122016-11-04 19:40:53 -07001204 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001205 android_errorWriteLog(0x534e4554, "33003822");
1206 return -EINVAL;
1207 }
1208 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001209 (maxReplySize < sizeof(effect_param_t) ||
1210 param->psize > maxReplySize - sizeof(effect_param_t))) {
Andy Hungb3456642016-11-28 13:50:21 -08001211 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001212 return -EINVAL;
1213 }
ragoe2759072016-11-22 18:02:48 -08001214 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001215 (sizeof(effect_param_t) > maxReplySize
1216 || param->psize > maxReplySize - sizeof(effect_param_t)
1217 || param->vsize > maxReplySize - sizeof(effect_param_t)
1218 - param->psize
1219 || roundUpDelta(param->psize, (uint32_t) sizeof(int)) >
1220 maxReplySize
1221 - sizeof(effect_param_t)
1222 - param->psize
1223 - param->vsize)) {
ragoe2759072016-11-22 18:02:48 -08001224 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1225 android_errorWriteLog(0x534e4554, "32705438");
1226 return -EINVAL;
1227 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001228 if ((cmdCode == EFFECT_CMD_SET_PARAM
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001229 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED)
1230 && // DEFERRED not generally used
1231 (param == nullptr
1232 || param->psize > cmdSize - sizeof(effect_param_t)
1233 || param->vsize > cmdSize - sizeof(effect_param_t)
1234 - param->psize
1235 || roundUpDelta(param->psize,
1236 (uint32_t) sizeof(int)) >
1237 cmdSize
1238 - sizeof(effect_param_t)
1239 - param->psize
1240 - param->vsize)) {
Andy Hunge4a1d912016-08-17 14:11:13 -07001241 android_errorWriteLog(0x534e4554, "30204301");
1242 return -EINVAL;
1243 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001244 uint32_t replySize = maxReplySize;
1245 reply->resize(replySize);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001246 status_t status = mEffectInterface->command(cmdCode,
1247 cmdSize,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001248 const_cast<uint8_t*>(cmdData.data()),
1249 &replySize,
1250 reply->data());
1251 reply->resize(status == NO_ERROR ? replySize : 0);
Eric Laurentca7cc822012-11-19 14:55:58 -08001252 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001253 for (size_t i = 1; i < mHandles.size(); i++) {
1254 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001255 if (h != NULL && !h->disconnected()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001256 h->commandExecuted(cmdCode, cmdData, *reply);
Eric Laurentca7cc822012-11-19 14:55:58 -08001257 }
1258 }
1259 }
1260 return status;
1261}
1262
Eric Laurentca7cc822012-11-19 14:55:58 -08001263bool AudioFlinger::EffectModule::isProcessEnabled() const
1264{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001265 if (mStatus != NO_ERROR) {
1266 return false;
1267 }
1268
Eric Laurentca7cc822012-11-19 14:55:58 -08001269 switch (mState) {
1270 case RESTART:
1271 case ACTIVE:
1272 case STOPPING:
1273 case STOPPED:
1274 return true;
1275 case IDLE:
1276 case STARTING:
1277 case DESTROYED:
1278 default:
1279 return false;
1280 }
1281}
1282
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001283bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1284{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001285 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001286}
1287
1288bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1289{
1290 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1291}
1292
Mikhail Naganov022b9952017-01-04 16:36:51 -08001293void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001294 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001295
1296 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001297 if (buffer != 0) {
1298 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1299 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1300 } else {
1301 mConfig.inputCfg.buffer.raw = NULL;
1302 }
1303 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001304 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001305
1306#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001307 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001308 // Theoretically insert effects can also do in-place conversions (destroying
1309 // the original buffer) when the output buffer is identical to the input buffer,
1310 // but we don't optimize for it here.
1311 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001312 const uint32_t inChannelCount =
1313 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1314 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001315 if (!auxType && formatMismatch && mInBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001316 // we need to translate - create hidl shared buffer and intercept
1317 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001318 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1319 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1320 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001321
1322 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1323 __func__, inChannels, inFrameCount, size);
1324
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001325 if (size > 0 && (mInConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001326 || size > mInConversionBuffer->getSize())) {
1327 mInConversionBuffer.clear();
1328 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001329 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001330 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001331 if (mInConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001332 mInConversionBuffer->setFrameCount(inFrameCount);
1333 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001334 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001335 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001336 }
1337 }
1338#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001339}
1340
1341void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001342 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001343
1344 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001345 if (buffer != 0) {
1346 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1347 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1348 } else {
1349 mConfig.outputCfg.buffer.raw = NULL;
1350 }
1351 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001352 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001353
1354#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001355 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001356 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001357 const uint32_t outChannelCount =
1358 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1359 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001360 if (formatMismatch && mOutBuffer != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001361 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001362 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1363 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1364 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001365
1366 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1367 __func__, outChannels, outFrameCount, size);
1368
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001369 if (size > 0 && (mOutConversionBuffer == nullptr
Andy Hungbded9c82017-11-30 18:47:35 -08001370 || size > mOutConversionBuffer->getSize())) {
1371 mOutConversionBuffer.clear();
1372 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001373 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001374 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001375 if (mOutConversionBuffer != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001376 mOutConversionBuffer->setFrameCount(outFrameCount);
1377 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001378 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001379 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001380 }
1381 }
1382#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001383}
1384
Eric Laurentca7cc822012-11-19 14:55:58 -08001385status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1386{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001387 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001388 if (mStatus != NO_ERROR) {
1389 return mStatus;
1390 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001391 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001392 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1393 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1394 if (isProcessEnabled() &&
1395 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001396 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1397 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001398 uint32_t volume[2];
1399 uint32_t *pVolume = NULL;
1400 uint32_t size = sizeof(volume);
1401 volume[0] = *left;
1402 volume[1] = *right;
1403 if (controller) {
1404 pVolume = volume;
1405 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001406 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1407 size,
1408 volume,
1409 &size,
1410 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1412 *left = volume[0];
1413 *right = volume[1];
1414 }
1415 }
1416 return status;
1417}
1418
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001419void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1420{
Zhou Songd505c642020-02-20 16:35:37 +08001421 // for offload or direct thread, if the effect chain has non-offloadable
1422 // effect and any effect module within the chain has volume control, then
1423 // volume control is delegated to effect, otherwise, set volume to hal.
1424 if (mEffectCallback->isOffloadOrDirect() &&
1425 !(isNonOffloadableEnabled_l() && hasVolumeControlEnabled_l())) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001426 float vol_l = (float)left / (1 << 24);
1427 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001428 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001429 }
1430}
1431
jiabin8f278ee2019-11-11 12:16:27 -08001432status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1433 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001434{
jiabin8f278ee2019-11-11 12:16:27 -08001435 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1436 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001437 return NO_ERROR;
1438 }
1439
1440 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001441 if (mStatus != NO_ERROR) {
1442 return mStatus;
1443 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001444 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001445 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001446 status_t cmdStatus;
1447 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001448 // FIXME: use audio device types and addresses when the hal interface is ready.
1449 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001450 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001451 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001452 &size,
1453 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001454 }
1455 return status;
1456}
1457
jiabin8f278ee2019-11-11 12:16:27 -08001458status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1459{
1460 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1461}
1462
1463status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1464{
1465 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1466}
1467
Eric Laurentca7cc822012-11-19 14:55:58 -08001468status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1469{
1470 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001471 if (mStatus != NO_ERROR) {
1472 return mStatus;
1473 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001474 status_t status = NO_ERROR;
1475 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1476 status_t cmdStatus;
1477 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001478 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1479 sizeof(audio_mode_t),
1480 &mode,
1481 &size,
1482 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001483 if (status == NO_ERROR) {
1484 status = cmdStatus;
1485 }
1486 }
1487 return status;
1488}
1489
1490status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1491{
1492 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001493 if (mStatus != NO_ERROR) {
1494 return mStatus;
1495 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001496 status_t status = NO_ERROR;
1497 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1498 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001499 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1500 sizeof(audio_source_t),
1501 &source,
1502 &size,
1503 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001504 }
1505 return status;
1506}
1507
Eric Laurent5baf2af2013-09-12 17:37:00 -07001508status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1509{
1510 Mutex::Autolock _l(mLock);
1511 if (mStatus != NO_ERROR) {
1512 return mStatus;
1513 }
1514 status_t status = NO_ERROR;
1515 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1516 status_t cmdStatus;
1517 uint32_t size = sizeof(status_t);
1518 effect_offload_param_t cmd;
1519
1520 cmd.isOffload = offloaded;
1521 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001522 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1523 sizeof(effect_offload_param_t),
1524 &cmd,
1525 &size,
1526 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001527 if (status == NO_ERROR) {
1528 status = cmdStatus;
1529 }
1530 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1531 } else {
1532 if (offloaded) {
1533 status = INVALID_OPERATION;
1534 }
1535 mOffloaded = false;
1536 }
1537 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1538 return status;
1539}
1540
1541bool AudioFlinger::EffectModule::isOffloaded() const
1542{
1543 Mutex::Autolock _l(mLock);
1544 return mOffloaded;
1545}
1546
jiabineb3bda02020-06-30 14:07:03 -07001547/*static*/
1548bool AudioFlinger::EffectModule::isHapticGenerator(const effect_uuid_t *type) {
1549 return memcmp(type, FX_IID_HAPTICGENERATOR, sizeof(effect_uuid_t)) == 0;
1550}
1551
1552bool AudioFlinger::EffectModule::isHapticGenerator() const {
1553 return isHapticGenerator(&mDescriptor.type);
1554}
1555
jiabine70bc7f2020-06-30 22:07:55 -07001556status_t AudioFlinger::EffectModule::setHapticIntensity(int id, int intensity)
1557{
1558 if (mStatus != NO_ERROR) {
1559 return mStatus;
1560 }
1561 if (!isHapticGenerator()) {
1562 ALOGW("Should not set haptic intensity for effects that are not HapticGenerator");
1563 return INVALID_OPERATION;
1564 }
1565
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001566 std::vector<uint8_t> request(sizeof(effect_param_t) + 3 * sizeof(uint32_t));
1567 effect_param_t *param = (effect_param_t*) request.data();
jiabine70bc7f2020-06-30 22:07:55 -07001568 param->psize = sizeof(int32_t);
1569 param->vsize = sizeof(int32_t) * 2;
1570 *(int32_t*)param->data = HG_PARAM_HAPTIC_INTENSITY;
1571 *((int32_t*)param->data + 1) = id;
1572 *((int32_t*)param->data + 2) = intensity;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001573 std::vector<uint8_t> response;
1574 status_t status = command(EFFECT_CMD_SET_PARAM, request, sizeof(int32_t), &response);
jiabine70bc7f2020-06-30 22:07:55 -07001575 if (status == NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001576 LOG_ALWAYS_FATAL_IF(response.size() != 4);
1577 status = *reinterpret_cast<const status_t*>(response.data());
jiabine70bc7f2020-06-30 22:07:55 -07001578 }
1579 return status;
1580}
1581
Andy Hungbded9c82017-11-30 18:47:35 -08001582static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1583 std::stringstream ss;
1584
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001585 if (buffer == nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001586 return "nullptr"; // make different than below
1587 } else if (buffer->externalData() != nullptr) {
1588 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1589 << " -> "
1590 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1591 } else {
1592 ss << buffer->audioBuffer()->raw;
1593 }
1594 return ss.str();
1595}
Marco Nelissenb2208842014-02-07 14:00:50 -08001596
Eric Laurent41709552019-12-16 19:34:05 -08001597void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001598{
Eric Laurent41709552019-12-16 19:34:05 -08001599 EffectBase::dump(fd, args);
1600
Eric Laurentca7cc822012-11-19 14:55:58 -08001601 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001602 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001603
Eric Laurent41709552019-12-16 19:34:05 -08001604 result.append("\t\tStatus Engine:\n");
1605 result.appendFormat("\t\t%03d %p\n",
1606 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001607
1608 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001609
1610 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001611 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1612 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1613 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001614 mConfig.inputCfg.buffer.frameCount,
1615 mConfig.inputCfg.samplingRate,
1616 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001617 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001618 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001619
1620 result.append("\t\t- Output configuration:\n");
1621 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001622 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001623 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001624 mConfig.outputCfg.buffer.frameCount,
1625 mConfig.outputCfg.samplingRate,
1626 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001627 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001628 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001629
rago94a1ee82017-07-21 15:11:02 -07001630#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001631
Andy Hungbded9c82017-11-30 18:47:35 -08001632 result.appendFormat("\t\t- HAL buffers:\n"
1633 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1634 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1635 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1636 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1637 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001638#endif
1639
Eric Laurentca7cc822012-11-19 14:55:58 -08001640 write(fd, result.string(), result.length());
1641
Mikhail Naganov4d547672019-02-22 14:19:19 -08001642 if (mEffectInterface != 0) {
1643 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1644 (void)mEffectInterface->dump(fd);
1645 }
1646
Eric Laurentca7cc822012-11-19 14:55:58 -08001647 if (locked) {
1648 mLock.unlock();
1649 }
1650}
1651
1652// ----------------------------------------------------------------------------
1653// EffectHandle implementation
1654// ----------------------------------------------------------------------------
1655
1656#undef LOG_TAG
1657#define LOG_TAG "AudioFlinger::EffectHandle"
1658
Eric Laurent41709552019-12-16 19:34:05 -08001659AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001660 const sp<AudioFlinger::Client>& client,
1661 const sp<media::IEffectClient>& effectClient,
1662 int32_t priority)
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 : BnEffect(),
1664 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001665 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001666{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001667 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001668
1669 if (client == 0) {
1670 return;
1671 }
1672 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1673 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001674 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001675 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001676 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001677 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001678 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001679 return;
1680 }
Glenn Kastene75da402013-11-20 13:54:52 -08001681 new(mCblk) effect_param_cblk_t();
1682 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001683}
1684
1685AudioFlinger::EffectHandle::~EffectHandle()
1686{
1687 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001688 disconnect(false);
1689}
1690
Glenn Kastene75da402013-11-20 13:54:52 -08001691status_t AudioFlinger::EffectHandle::initCheck()
1692{
1693 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1694}
1695
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001696#define RETURN(code) \
1697 *_aidl_return = (code); \
1698 return Status::ok();
1699
1700Status AudioFlinger::EffectHandle::enable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001701{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001702 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001704 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001705 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001706 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001707 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001708 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001709 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001710 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001711
1712 if (mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001713 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 }
1715
1716 mEnabled = true;
1717
Eric Laurent6c796322019-04-09 14:13:17 -07001718 status_t status = effect->updatePolicyState();
1719 if (status != NO_ERROR) {
1720 mEnabled = false;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001721 RETURN(status);
Eric Laurent6c796322019-04-09 14:13:17 -07001722 }
1723
Eric Laurent6b446ce2019-12-13 10:56:31 -08001724 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001725
1726 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001727 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001728 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001729 }
1730
Eric Laurent6b446ce2019-12-13 10:56:31 -08001731 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001732 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001733 mEnabled = false;
1734 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001735 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001736}
1737
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001738Status AudioFlinger::EffectHandle::disable(int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001739{
1740 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001741 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001742 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001743 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001744 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001745 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001746 if (!mHasControl) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001747 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001748 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001749
1750 if (!mEnabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001751 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001752 }
1753 mEnabled = false;
1754
Eric Laurent6c796322019-04-09 14:13:17 -07001755 effect->updatePolicyState();
1756
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001757 if (effect->suspended()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001758 RETURN(NO_ERROR);
Eric Laurentca7cc822012-11-19 14:55:58 -08001759 }
1760
Eric Laurent6b446ce2019-12-13 10:56:31 -08001761 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001762 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001763}
1764
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001765Status AudioFlinger::EffectHandle::disconnect()
Eric Laurentca7cc822012-11-19 14:55:58 -08001766{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001767 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001768 disconnect(true);
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001769 return Status::ok();
Eric Laurentca7cc822012-11-19 14:55:58 -08001770}
1771
1772void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1773{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001774 AutoMutex _l(mLock);
1775 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1776 if (mDisconnected) {
1777 if (unpinIfLast) {
1778 android_errorWriteLog(0x534e4554, "32707507");
1779 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001780 return;
1781 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001782 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001783 {
Eric Laurent41709552019-12-16 19:34:05 -08001784 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001785 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001786 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001787 ALOGW("%s Effect handle %p disconnected after thread destruction",
1788 __func__, this);
1789 }
1790 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001791 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001792 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001793
Eric Laurentca7cc822012-11-19 14:55:58 -08001794 if (mClient != 0) {
1795 if (mCblk != NULL) {
1796 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1797 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1798 }
1799 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001800 // Client destructor must run with AudioFlinger client mutex locked
1801 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001802 mClient.clear();
1803 }
1804}
1805
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001806Status AudioFlinger::EffectHandle::getCblk(media::SharedFileRegion* _aidl_return) {
1807 LOG_ALWAYS_FATAL_IF(!convertIMemoryToSharedFileRegion(mCblkMemory, _aidl_return));
1808 return Status::ok();
1809}
1810
1811Status AudioFlinger::EffectHandle::command(int32_t cmdCode,
1812 const std::vector<uint8_t>& cmdData,
1813 int32_t maxResponseSize,
1814 std::vector<uint8_t>* response,
1815 int32_t* _aidl_return)
Eric Laurentca7cc822012-11-19 14:55:58 -08001816{
1817 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001818 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001819
Eric Laurentc7ab3092017-06-15 18:43:46 -07001820 // reject commands reserved for internal use by audio framework if coming from outside
1821 // of audioserver
1822 switch(cmdCode) {
1823 case EFFECT_CMD_ENABLE:
1824 case EFFECT_CMD_DISABLE:
1825 case EFFECT_CMD_SET_PARAM:
1826 case EFFECT_CMD_SET_PARAM_DEFERRED:
1827 case EFFECT_CMD_SET_PARAM_COMMIT:
1828 case EFFECT_CMD_GET_PARAM:
1829 break;
1830 default:
1831 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1832 break;
1833 }
1834 android_errorWriteLog(0x534e4554, "62019992");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001835 RETURN(BAD_VALUE);
Eric Laurentc7ab3092017-06-15 18:43:46 -07001836 }
1837
Eric Laurent1ffc5852016-12-15 14:46:09 -08001838 if (cmdCode == EFFECT_CMD_ENABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001839 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001840 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001841 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001842 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001843 writeToBuffer(NO_ERROR, response);
1844 return enable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001845 } else if (cmdCode == EFFECT_CMD_DISABLE) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001846 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001847 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001848 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001849 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001850 writeToBuffer(NO_ERROR, response);
1851 return disable(_aidl_return);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001852 }
1853
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001854 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001855 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001856 if (effect == 0 || mDisconnected) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001857 RETURN(DEAD_OBJECT);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001858 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 // only get parameter command is permitted for applications not controlling the effect
1860 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001861 RETURN(INVALID_OPERATION);
Eric Laurentca7cc822012-11-19 14:55:58 -08001862 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001863
1864 // handle commands that are not forwarded transparently to effect engine
1865 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001866 if (mClient == 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001867 RETURN(INVALID_OPERATION);
Eric Laurentb82e6b72019-11-22 17:25:04 -08001868 }
1869
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001870 if (maxResponseSize < sizeof(int)) {
Eric Laurent1ffc5852016-12-15 14:46:09 -08001871 android_errorWriteLog(0x534e4554, "32095713");
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001872 RETURN(BAD_VALUE);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001873 }
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001874 writeToBuffer(NO_ERROR, response);
Eric Laurent1ffc5852016-12-15 14:46:09 -08001875
Eric Laurentca7cc822012-11-19 14:55:58 -08001876 // No need to trylock() here as this function is executed in the binder thread serving a
1877 // particular client process: no risk to block the whole media server process or mixer
1878 // threads if we are stuck here
1879 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001880 // keep local copy of index in case of client corruption b/32220769
1881 const uint32_t clientIndex = mCblk->clientIndex;
1882 const uint32_t serverIndex = mCblk->serverIndex;
1883 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1884 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001885 mCblk->serverIndex = 0;
1886 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001887 RETURN(BAD_VALUE);
Eric Laurentca7cc822012-11-19 14:55:58 -08001888 }
1889 status_t status = NO_ERROR;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001890 std::vector<uint8_t> param;
Andy Hunga447a0f2016-11-15 17:19:58 -08001891 for (uint32_t index = serverIndex; index < clientIndex;) {
1892 int *p = (int *)(mBuffer + index);
1893 const int size = *p++;
1894 if (size < 0
1895 || size > EFFECT_PARAM_BUFFER_SIZE
1896 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001897 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001898 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001899 break;
1900 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001901
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001902 std::copy(reinterpret_cast<const uint8_t*>(p),
1903 reinterpret_cast<const uint8_t*>(p) + size,
1904 std::back_inserter(param));
Andy Hunga447a0f2016-11-15 17:19:58 -08001905
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001906 std::vector<uint8_t> replyBuffer;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001907 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001908 param,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001909 sizeof(int),
1910 &replyBuffer);
1911 int reply = *reinterpret_cast<const int*>(replyBuffer.data());
Andy Hunga447a0f2016-11-15 17:19:58 -08001912
1913 // verify shared memory: server index shouldn't change; client index can't go back.
1914 if (serverIndex != mCblk->serverIndex
1915 || clientIndex > mCblk->clientIndex) {
1916 android_errorWriteLog(0x534e4554, "32220769");
1917 status = BAD_VALUE;
1918 break;
1919 }
1920
Eric Laurentca7cc822012-11-19 14:55:58 -08001921 // stop at first error encountered
1922 if (ret != NO_ERROR) {
1923 status = ret;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001924 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001925 break;
1926 } else if (reply != NO_ERROR) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001927 writeToBuffer(reply, response);
Eric Laurentca7cc822012-11-19 14:55:58 -08001928 break;
1929 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001930 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001931 }
1932 mCblk->serverIndex = 0;
1933 mCblk->clientIndex = 0;
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001934 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001935 }
1936
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001937 status_t status = effect->command(cmdCode,
1938 cmdData,
1939 maxResponseSize,
1940 response);
1941 RETURN(status);
Eric Laurentca7cc822012-11-19 14:55:58 -08001942}
1943
1944void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1945{
1946 ALOGV("setControl %p control %d", this, hasControl);
1947
1948 mHasControl = hasControl;
1949 mEnabled = enabled;
1950
1951 if (signal && mEffectClient != 0) {
1952 mEffectClient->controlStatusChanged(hasControl);
1953 }
1954}
1955
1956void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001957 const std::vector<uint8_t>& cmdData,
1958 const std::vector<uint8_t>& replyData)
Eric Laurentca7cc822012-11-19 14:55:58 -08001959{
1960 if (mEffectClient != 0) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07001961 mEffectClient->commandExecuted(cmdCode, cmdData, replyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001962 }
1963}
1964
1965
1966
1967void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1968{
1969 if (mEffectClient != 0) {
1970 mEffectClient->enableStatusChanged(enabled);
1971 }
1972}
1973
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001974void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001975{
1976 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1977
Marco Nelissenb2208842014-02-07 14:00:50 -08001978 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001979 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001980 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001981 mHasControl ? "yes" : "no",
1982 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001983 mCblk ? mCblk->clientIndex : 0,
1984 mCblk ? mCblk->serverIndex : 0
1985 );
1986
1987 if (locked) {
1988 mCblk->lock.unlock();
1989 }
1990}
1991
1992#undef LOG_TAG
1993#define LOG_TAG "AudioFlinger::EffectChain"
1994
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08001995AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& thread,
1996 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001997 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001998 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001999 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002000 mEffectCallback(new EffectCallback(wp<EffectChain>(this), thread))
Eric Laurentca7cc822012-11-19 14:55:58 -08002001{
2002 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002003 sp<ThreadBase> p = thread.promote();
2004 if (p == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002005 return;
2006 }
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002007 mMaxTailBuffers = ((kProcessTailDurationMs * p->sampleRate()) / 1000) /
2008 p->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -08002009}
2010
2011AudioFlinger::EffectChain::~EffectChain()
2012{
Eric Laurentca7cc822012-11-19 14:55:58 -08002013}
2014
2015// getEffectFromDesc_l() must be called with ThreadBase::mLock held
2016sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
2017 effect_descriptor_t *descriptor)
2018{
2019 size_t size = mEffects.size();
2020
2021 for (size_t i = 0; i < size; i++) {
2022 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
2023 return mEffects[i];
2024 }
2025 }
2026 return 0;
2027}
2028
2029// getEffectFromId_l() must be called with ThreadBase::mLock held
2030sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
2031{
2032 size_t size = mEffects.size();
2033
2034 for (size_t i = 0; i < size; i++) {
2035 // by convention, return first effect if id provided is 0 (0 is never a valid id)
2036 if (id == 0 || mEffects[i]->id() == id) {
2037 return mEffects[i];
2038 }
2039 }
2040 return 0;
2041}
2042
2043// getEffectFromType_l() must be called with ThreadBase::mLock held
2044sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
2045 const effect_uuid_t *type)
2046{
2047 size_t size = mEffects.size();
2048
2049 for (size_t i = 0; i < size; i++) {
2050 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
2051 return mEffects[i];
2052 }
2053 }
2054 return 0;
2055}
2056
Eric Laurent6c796322019-04-09 14:13:17 -07002057std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2058{
2059 std::vector<int> ids;
2060 Mutex::Autolock _l(mLock);
2061 for (size_t i = 0; i < mEffects.size(); i++) {
2062 ids.push_back(mEffects[i]->id());
2063 }
2064 return ids;
2065}
2066
Eric Laurentca7cc822012-11-19 14:55:58 -08002067void AudioFlinger::EffectChain::clearInputBuffer()
2068{
2069 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002070 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002071}
2072
2073// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002074void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002075{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002076 if (mInBuffer == NULL) {
2077 return;
2078 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002079 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002080 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002081
Eric Laurent6b446ce2019-12-13 10:56:31 -08002082 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002083 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002084}
2085
2086// Must be called with EffectChain::mLock locked
2087void AudioFlinger::EffectChain::process_l()
2088{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002089 // never process effects when:
2090 // - on an OFFLOAD thread
2091 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002092 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002093 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002094 bool tracksOnSession = (trackCnt() != 0);
2095
2096 if (!tracksOnSession && mTailBufferCount == 0) {
2097 doProcess = false;
2098 }
2099
2100 if (activeTrackCnt() == 0) {
2101 // if no track is active and the effect tail has not been rendered,
2102 // the input buffer must be cleared here as the mixer process will not do it
2103 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002104 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002105 if (mTailBufferCount > 0) {
2106 mTailBufferCount--;
2107 }
2108 }
2109 }
2110 }
2111
2112 size_t size = mEffects.size();
2113 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002114 // Only the input and output buffers of the chain can be external,
2115 // and 'update' / 'commit' do nothing for allocated buffers, thus
2116 // it's not needed to consider any other buffers here.
2117 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002118 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2119 mOutBuffer->update();
2120 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002121 for (size_t i = 0; i < size; i++) {
2122 mEffects[i]->process();
2123 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002124 mInBuffer->commit();
2125 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2126 mOutBuffer->commit();
2127 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002128 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002129 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002130 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002131 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2132 }
2133 if (doResetVolume) {
2134 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002135 }
2136}
2137
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002138// createEffect_l() must be called with ThreadBase::mLock held
2139status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002140 effect_descriptor_t *desc,
2141 int id,
2142 audio_session_t sessionId,
2143 bool pinned)
2144{
2145 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002146 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002147 status_t lStatus = effect->status();
2148 if (lStatus == NO_ERROR) {
2149 lStatus = addEffect_ll(effect);
2150 }
2151 if (lStatus != NO_ERROR) {
2152 effect.clear();
2153 }
2154 return lStatus;
2155}
2156
2157// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002158status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2159{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002160 Mutex::Autolock _l(mLock);
2161 return addEffect_ll(effect);
2162}
2163// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2164status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2165{
Eric Laurentca7cc822012-11-19 14:55:58 -08002166 effect_descriptor_t desc = effect->desc();
2167 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2168
Eric Laurent6b446ce2019-12-13 10:56:31 -08002169 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002170
2171 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2172 // Auxiliary effects are inserted at the beginning of mEffects vector as
2173 // they are processed first and accumulated in chain input buffer
2174 mEffects.insertAt(effect, 0);
2175
2176 // the input buffer for auxiliary effect contains mono samples in
2177 // 32 bit format. This is to avoid saturation in AudoMixer
2178 // accumulation stage. Saturation is done in EffectModule::process() before
2179 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002180 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002181 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002182#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002183 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002184 numSamples * sizeof(float), &halBuffer);
2185#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002186 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002187 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002188#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002189 if (result != OK) return result;
2190 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002191 // auxiliary effects output samples to chain input buffer for further processing
2192 // by insert effects
2193 effect->setOutBuffer(mInBuffer);
2194 } else {
2195 // Insert effects are inserted at the end of mEffects vector as they are processed
2196 // after track and auxiliary effects.
2197 // Insert effect order as a function of indicated preference:
2198 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2199 // another effect is present
2200 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2201 // last effect claiming first position
2202 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2203 // first effect claiming last position
2204 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2205 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2206 // already present
2207
2208 size_t size = mEffects.size();
2209 size_t idx_insert = size;
2210 ssize_t idx_insert_first = -1;
2211 ssize_t idx_insert_last = -1;
2212
2213 for (size_t i = 0; i < size; i++) {
2214 effect_descriptor_t d = mEffects[i]->desc();
2215 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2216 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2217 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2218 // check invalid effect chaining combinations
2219 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2220 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2221 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2222 desc.name, d.name);
2223 return INVALID_OPERATION;
2224 }
2225 // remember position of first insert effect and by default
2226 // select this as insert position for new effect
2227 if (idx_insert == size) {
2228 idx_insert = i;
2229 }
2230 // remember position of last insert effect claiming
2231 // first position
2232 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2233 idx_insert_first = i;
2234 }
2235 // remember position of first insert effect claiming
2236 // last position
2237 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2238 idx_insert_last == -1) {
2239 idx_insert_last = i;
2240 }
2241 }
2242 }
2243
2244 // modify idx_insert from first position if needed
2245 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2246 if (idx_insert_last != -1) {
2247 idx_insert = idx_insert_last;
2248 } else {
2249 idx_insert = size;
2250 }
2251 } else {
2252 if (idx_insert_first != -1) {
2253 idx_insert = idx_insert_first + 1;
2254 }
2255 }
2256
2257 // always read samples from chain input buffer
2258 effect->setInBuffer(mInBuffer);
2259
2260 // if last effect in the chain, output samples to chain
2261 // output buffer, otherwise to chain input buffer
2262 if (idx_insert == size) {
2263 if (idx_insert != 0) {
2264 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2265 mEffects[idx_insert-1]->configure();
2266 }
2267 effect->setOutBuffer(mOutBuffer);
2268 } else {
2269 effect->setOutBuffer(mInBuffer);
2270 }
2271 mEffects.insertAt(effect, idx_insert);
2272
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002273 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002274 idx_insert);
2275 }
2276 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002277
Eric Laurentca7cc822012-11-19 14:55:58 -08002278 return NO_ERROR;
2279}
2280
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002281// removeEffect_l() must be called with ThreadBase::mLock held
2282size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2283 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002284{
2285 Mutex::Autolock _l(mLock);
2286 size_t size = mEffects.size();
2287 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2288
2289 for (size_t i = 0; i < size; i++) {
2290 if (effect == mEffects[i]) {
2291 // calling stop here will remove pre-processing effect from the audio HAL.
2292 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2293 // the middle of a read from audio HAL
2294 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2295 mEffects[i]->state() == EffectModule::STOPPING) {
2296 mEffects[i]->stop();
2297 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002298 if (release) {
2299 mEffects[i]->release_l();
2300 }
2301
Mikhail Naganov022b9952017-01-04 16:36:51 -08002302 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002303 if (i == size - 1 && i != 0) {
2304 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2305 mEffects[i - 1]->configure();
2306 }
2307 }
2308 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002309 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002310 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002311
Eric Laurentca7cc822012-11-19 14:55:58 -08002312 break;
2313 }
2314 }
2315
2316 return mEffects.size();
2317}
2318
jiabin8f278ee2019-11-11 12:16:27 -08002319// setDevices_l() must be called with ThreadBase::mLock held
2320void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002321{
2322 size_t size = mEffects.size();
2323 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002324 mEffects[i]->setDevices(devices);
2325 }
2326}
2327
2328// setInputDevice_l() must be called with ThreadBase::mLock held
2329void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2330{
2331 size_t size = mEffects.size();
2332 for (size_t i = 0; i < size; i++) {
2333 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002334 }
2335}
2336
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002337// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002338void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2339{
2340 size_t size = mEffects.size();
2341 for (size_t i = 0; i < size; i++) {
2342 mEffects[i]->setMode(mode);
2343 }
2344}
2345
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002346// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002347void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2348{
2349 size_t size = mEffects.size();
2350 for (size_t i = 0; i < size; i++) {
2351 mEffects[i]->setAudioSource(source);
2352 }
2353}
2354
Zhou Songd505c642020-02-20 16:35:37 +08002355bool AudioFlinger::EffectChain::hasVolumeControlEnabled_l() const {
2356 for (const auto &effect : mEffects) {
2357 if (effect->isVolumeControlEnabled()) return true;
2358 }
2359 return false;
2360}
2361
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002362// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002363bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002364{
2365 uint32_t newLeft = *left;
2366 uint32_t newRight = *right;
2367 bool hasControl = false;
2368 int ctrlIdx = -1;
2369 size_t size = mEffects.size();
2370
2371 // first update volume controller
2372 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002373 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002374 ctrlIdx = i - 1;
2375 hasControl = true;
2376 break;
2377 }
2378 }
2379
Eric Laurentfa1e1232016-08-02 19:01:49 -07002380 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002381 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002382 if (hasControl) {
2383 *left = mNewLeftVolume;
2384 *right = mNewRightVolume;
2385 }
2386 return hasControl;
2387 }
2388
2389 mVolumeCtrlIdx = ctrlIdx;
2390 mLeftVolume = newLeft;
2391 mRightVolume = newRight;
2392
2393 // second get volume update from volume controller
2394 if (ctrlIdx >= 0) {
2395 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2396 mNewLeftVolume = newLeft;
2397 mNewRightVolume = newRight;
2398 }
2399 // then indicate volume to all other effects in chain.
2400 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002401 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002402 uint32_t lVol = newLeft;
2403 uint32_t rVol = newRight;
2404
2405 for (size_t i = 0; i < size; i++) {
2406 if ((int)i == ctrlIdx) {
2407 continue;
2408 }
2409 // this also works for ctrlIdx == -1 when there is no volume controller
2410 if ((int)i > ctrlIdx) {
2411 lVol = *left;
2412 rVol = *right;
2413 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002414 // Pass requested volume directly if this is volume monitor module
2415 if (mEffects[i]->isVolumeMonitor()) {
2416 mEffects[i]->setVolume(left, right, false);
2417 } else {
2418 mEffects[i]->setVolume(&lVol, &rVol, false);
2419 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002420 }
2421 *left = newLeft;
2422 *right = newRight;
2423
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002424 setVolumeForOutput_l(*left, *right);
2425
Eric Laurentca7cc822012-11-19 14:55:58 -08002426 return hasControl;
2427}
2428
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002429// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002430void AudioFlinger::EffectChain::resetVolume_l()
2431{
Eric Laurente7449bf2016-08-03 18:44:07 -07002432 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2433 uint32_t left = mLeftVolume;
2434 uint32_t right = mRightVolume;
2435 (void)setVolume_l(&left, &right, true);
2436 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002437}
2438
jiabineb3bda02020-06-30 14:07:03 -07002439// containsHapticGeneratingEffect_l must be called with ThreadBase::mLock or EffectChain::mLock held
2440bool AudioFlinger::EffectChain::containsHapticGeneratingEffect_l()
2441{
2442 for (size_t i = 0; i < mEffects.size(); ++i) {
2443 if (mEffects[i]->isHapticGenerator()) {
2444 return true;
2445 }
2446 }
2447 return false;
2448}
2449
jiabine70bc7f2020-06-30 22:07:55 -07002450void AudioFlinger::EffectChain::setHapticIntensity_l(int id, int intensity)
2451{
2452 Mutex::Autolock _l(mLock);
2453 for (size_t i = 0; i < mEffects.size(); ++i) {
2454 mEffects[i]->setHapticIntensity(id, intensity);
2455 }
2456}
2457
Eric Laurent1b928682014-10-02 19:41:47 -07002458void AudioFlinger::EffectChain::syncHalEffectsState()
2459{
2460 Mutex::Autolock _l(mLock);
2461 for (size_t i = 0; i < mEffects.size(); i++) {
2462 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2463 mEffects[i]->state() == EffectModule::STOPPING) {
2464 mEffects[i]->addEffectToHal_l();
2465 }
2466 }
2467}
2468
Eric Laurentca7cc822012-11-19 14:55:58 -08002469void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2470{
Eric Laurentca7cc822012-11-19 14:55:58 -08002471 String8 result;
2472
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002473 const size_t numEffects = mEffects.size();
2474 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002475
Marco Nelissenb2208842014-02-07 14:00:50 -08002476 if (numEffects) {
2477 bool locked = AudioFlinger::dumpTryLock(mLock);
2478 // failed to lock - AudioFlinger is probably deadlocked
2479 if (!locked) {
2480 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002481 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002482
Andy Hungbded9c82017-11-30 18:47:35 -08002483 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2484 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2485 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2486 (int)inBufferStr.size(), "In buffer ",
2487 (int)outBufferStr.size(), "Out buffer ");
2488 result.appendFormat("\t%s %s %d\n",
2489 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002490 write(fd, result.string(), result.size());
2491
2492 for (size_t i = 0; i < numEffects; ++i) {
2493 sp<EffectModule> effect = mEffects[i];
2494 if (effect != 0) {
2495 effect->dump(fd, args);
2496 }
2497 }
2498
2499 if (locked) {
2500 mLock.unlock();
2501 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002502 } else {
2503 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002504 }
2505}
2506
2507// must be called with ThreadBase::mLock held
2508void AudioFlinger::EffectChain::setEffectSuspended_l(
2509 const effect_uuid_t *type, bool suspend)
2510{
2511 sp<SuspendedEffectDesc> desc;
2512 // use effect type UUID timelow as key as there is no real risk of identical
2513 // timeLow fields among effect type UUIDs.
2514 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2515 if (suspend) {
2516 if (index >= 0) {
2517 desc = mSuspendedEffects.valueAt(index);
2518 } else {
2519 desc = new SuspendedEffectDesc();
2520 desc->mType = *type;
2521 mSuspendedEffects.add(type->timeLow, desc);
2522 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2523 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002524
Eric Laurentca7cc822012-11-19 14:55:58 -08002525 if (desc->mRefCount++ == 0) {
2526 sp<EffectModule> effect = getEffectIfEnabled(type);
2527 if (effect != 0) {
2528 desc->mEffect = effect;
2529 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002530 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002531 }
2532 }
2533 } else {
2534 if (index < 0) {
2535 return;
2536 }
2537 desc = mSuspendedEffects.valueAt(index);
2538 if (desc->mRefCount <= 0) {
2539 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002540 desc->mRefCount = 0;
2541 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002542 }
2543 if (--desc->mRefCount == 0) {
2544 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2545 if (desc->mEffect != 0) {
2546 sp<EffectModule> effect = desc->mEffect.promote();
2547 if (effect != 0) {
2548 effect->setSuspended(false);
2549 effect->lock();
2550 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002551 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002552 effect->setEnabled_l(handle->enabled());
2553 }
2554 effect->unlock();
2555 }
2556 desc->mEffect.clear();
2557 }
2558 mSuspendedEffects.removeItemsAt(index);
2559 }
2560 }
2561}
2562
2563// must be called with ThreadBase::mLock held
2564void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2565{
2566 sp<SuspendedEffectDesc> desc;
2567
2568 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2569 if (suspend) {
2570 if (index >= 0) {
2571 desc = mSuspendedEffects.valueAt(index);
2572 } else {
2573 desc = new SuspendedEffectDesc();
2574 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2575 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2576 }
2577 if (desc->mRefCount++ == 0) {
2578 Vector< sp<EffectModule> > effects;
2579 getSuspendEligibleEffects(effects);
2580 for (size_t i = 0; i < effects.size(); i++) {
2581 setEffectSuspended_l(&effects[i]->desc().type, true);
2582 }
2583 }
2584 } else {
2585 if (index < 0) {
2586 return;
2587 }
2588 desc = mSuspendedEffects.valueAt(index);
2589 if (desc->mRefCount <= 0) {
2590 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2591 desc->mRefCount = 1;
2592 }
2593 if (--desc->mRefCount == 0) {
2594 Vector<const effect_uuid_t *> types;
2595 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2596 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2597 continue;
2598 }
2599 types.add(&mSuspendedEffects.valueAt(i)->mType);
2600 }
2601 for (size_t i = 0; i < types.size(); i++) {
2602 setEffectSuspended_l(types[i], false);
2603 }
2604 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2605 mSuspendedEffects.keyAt(index));
2606 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2607 }
2608 }
2609}
2610
2611
2612// The volume effect is used for automated tests only
2613#ifndef OPENSL_ES_H_
2614static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2615 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2616const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2617#endif //OPENSL_ES_H_
2618
Eric Laurentd8365c52017-07-16 15:27:05 -07002619/* static */
2620bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2621{
2622 // Only NS and AEC are suspended when BtNRec is off
2623 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2624 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2625 return true;
2626 }
2627 return false;
2628}
2629
Eric Laurentca7cc822012-11-19 14:55:58 -08002630bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2631{
2632 // auxiliary effects and visualizer are never suspended on output mix
2633 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2634 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2635 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002636 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2637 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002638 return false;
2639 }
2640 return true;
2641}
2642
2643void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2644 Vector< sp<AudioFlinger::EffectModule> > &effects)
2645{
2646 effects.clear();
2647 for (size_t i = 0; i < mEffects.size(); i++) {
2648 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2649 effects.add(mEffects[i]);
2650 }
2651 }
2652}
2653
2654sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2655 const effect_uuid_t *type)
2656{
2657 sp<EffectModule> effect = getEffectFromType_l(type);
2658 return effect != 0 && effect->isEnabled() ? effect : 0;
2659}
2660
2661void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2662 bool enabled)
2663{
2664 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2665 if (enabled) {
2666 if (index < 0) {
2667 // if the effect is not suspend check if all effects are suspended
2668 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2669 if (index < 0) {
2670 return;
2671 }
2672 if (!isEffectEligibleForSuspend(effect->desc())) {
2673 return;
2674 }
2675 setEffectSuspended_l(&effect->desc().type, enabled);
2676 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2677 if (index < 0) {
2678 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2679 return;
2680 }
2681 }
2682 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2683 effect->desc().type.timeLow);
2684 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002685 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002686 if (desc->mEffect == 0) {
2687 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002688 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002689 effect->setSuspended(true);
2690 }
2691 } else {
2692 if (index < 0) {
2693 return;
2694 }
2695 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2696 effect->desc().type.timeLow);
2697 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2698 desc->mEffect.clear();
2699 effect->setSuspended(false);
2700 }
2701}
2702
Eric Laurent5baf2af2013-09-12 17:37:00 -07002703bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002704{
2705 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002706 return isNonOffloadableEnabled_l();
2707}
2708
2709bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2710{
Eric Laurent813e2a72013-08-31 12:59:48 -07002711 size_t size = mEffects.size();
2712 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002713 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002714 return true;
2715 }
2716 }
2717 return false;
2718}
2719
Eric Laurentaaa44472014-09-12 17:41:50 -07002720void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2721{
2722 Mutex::Autolock _l(mLock);
Ytai Ben-Tsvi3de1bbf2020-01-21 16:41:17 -08002723 mEffectCallback->setThread(thread);
Eric Laurentaaa44472014-09-12 17:41:50 -07002724}
2725
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002726void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2727{
2728 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2729 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2730 }
2731 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2732 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2733 }
2734}
2735
2736void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2737{
2738 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2739 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2740 }
2741 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2742 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2743 }
2744}
2745
2746bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002747{
2748 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002749 for (const auto &effect : mEffects) {
2750 if (effect->isProcessImplemented()) {
2751 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002752 }
2753 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002754 // Allow effects without processing.
2755 return true;
2756}
2757
2758bool AudioFlinger::EffectChain::isFastCompatible() const
2759{
2760 Mutex::Autolock _l(mLock);
2761 for (const auto &effect : mEffects) {
2762 if (effect->isProcessImplemented()
2763 && effect->isImplementationSoftware()) {
2764 return false;
2765 }
2766 }
2767 // Allow effects without processing or hw accelerated effects.
2768 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002769}
2770
2771// isCompatibleWithThread_l() must be called with thread->mLock held
2772bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2773{
2774 Mutex::Autolock _l(mLock);
2775 for (size_t i = 0; i < mEffects.size(); i++) {
2776 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2777 return false;
2778 }
2779 }
2780 return true;
2781}
2782
Eric Laurent6b446ce2019-12-13 10:56:31 -08002783// EffectCallbackInterface implementation
2784status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2785 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2786 sp<EffectHalInterface> *effect) {
2787 status_t status = NO_INIT;
2788 sp<AudioFlinger> af = mAudioFlinger.promote();
2789 if (af == nullptr) {
2790 return status;
2791 }
2792 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2793 if (effectsFactory != 0) {
2794 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2795 }
2796 return status;
2797}
2798
2799bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002800 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002801 sp<AudioFlinger> af = mAudioFlinger.promote();
2802 if (af == nullptr) {
2803 return false;
2804 }
Eric Laurent41709552019-12-16 19:34:05 -08002805 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2806 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002807}
2808
2809status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2810 size_t size, sp<EffectBufferHalInterface>* buffer) {
2811 sp<AudioFlinger> af = mAudioFlinger.promote();
2812 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2813 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2814}
2815
2816status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2817 sp<EffectHalInterface> effect) {
2818 status_t result = NO_INIT;
2819 sp<ThreadBase> t = mThread.promote();
2820 if (t == nullptr) {
2821 return result;
2822 }
2823 sp <StreamHalInterface> st = t->stream();
2824 if (st == nullptr) {
2825 return result;
2826 }
2827 result = st->addEffect(effect);
2828 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2829 return result;
2830}
2831
2832status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2833 sp<EffectHalInterface> effect) {
2834 status_t result = NO_INIT;
2835 sp<ThreadBase> t = mThread.promote();
2836 if (t == nullptr) {
2837 return result;
2838 }
2839 sp <StreamHalInterface> st = t->stream();
2840 if (st == nullptr) {
2841 return result;
2842 }
2843 result = st->removeEffect(effect);
2844 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2845 return result;
2846}
2847
2848audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2849 sp<ThreadBase> t = mThread.promote();
2850 if (t == nullptr) {
2851 return AUDIO_IO_HANDLE_NONE;
2852 }
2853 return t->id();
2854}
2855
2856bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2857 sp<ThreadBase> t = mThread.promote();
2858 if (t == nullptr) {
2859 return true;
2860 }
2861 return t->isOutput();
2862}
2863
2864bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2865 sp<ThreadBase> t = mThread.promote();
2866 if (t == nullptr) {
2867 return false;
2868 }
2869 return t->type() == ThreadBase::OFFLOAD;
2870}
2871
2872bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2873 sp<ThreadBase> t = mThread.promote();
2874 if (t == nullptr) {
2875 return false;
2876 }
2877 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2878}
2879
2880bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2881 sp<ThreadBase> t = mThread.promote();
2882 if (t == nullptr) {
2883 return false;
2884 }
Andy Hungea840382020-05-05 21:50:17 -07002885 return t->isOffloadOrMmap();
Eric Laurent6b446ce2019-12-13 10:56:31 -08002886}
2887
2888uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2889 sp<ThreadBase> t = mThread.promote();
2890 if (t == nullptr) {
2891 return 0;
2892 }
2893 return t->sampleRate();
2894}
2895
2896audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2897 sp<ThreadBase> t = mThread.promote();
2898 if (t == nullptr) {
2899 return AUDIO_CHANNEL_NONE;
2900 }
2901 return t->channelMask();
2902}
2903
2904uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2905 sp<ThreadBase> t = mThread.promote();
2906 if (t == nullptr) {
2907 return 0;
2908 }
2909 return t->channelCount();
2910}
2911
jiabineb3bda02020-06-30 14:07:03 -07002912audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::hapticChannelMask() const {
2913 sp<ThreadBase> t = mThread.promote();
2914 if (t == nullptr) {
2915 return AUDIO_CHANNEL_NONE;
2916 }
2917 return t->hapticChannelMask();
2918}
2919
Eric Laurent6b446ce2019-12-13 10:56:31 -08002920size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2921 sp<ThreadBase> t = mThread.promote();
2922 if (t == nullptr) {
2923 return 0;
2924 }
2925 return t->frameCount();
2926}
2927
2928uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2929 sp<ThreadBase> t = mThread.promote();
2930 if (t == nullptr) {
2931 return 0;
2932 }
2933 return t->latency_l();
2934}
2935
2936void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2937 sp<ThreadBase> t = mThread.promote();
2938 if (t == nullptr) {
2939 return;
2940 }
2941 t->setVolumeForOutput_l(left, right);
2942}
2943
2944void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002945 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002946 sp<ThreadBase> t = mThread.promote();
2947 if (t == nullptr) {
2948 return;
2949 }
2950 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2951
2952 sp<EffectChain> c = mChain.promote();
2953 if (c == nullptr) {
2954 return;
2955 }
Eric Laurent41709552019-12-16 19:34:05 -08002956 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2957 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002958}
2959
Eric Laurent41709552019-12-16 19:34:05 -08002960void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002961 sp<ThreadBase> t = mThread.promote();
2962 if (t == nullptr) {
2963 return;
2964 }
Eric Laurent41709552019-12-16 19:34:05 -08002965 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2966 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002967}
2968
Eric Laurent41709552019-12-16 19:34:05 -08002969void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002970 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2971
2972 sp<ThreadBase> t = mThread.promote();
2973 if (t == nullptr) {
2974 return;
2975 }
2976 t->onEffectDisable();
2977}
2978
2979bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2980 bool unpinIfLast) {
2981 sp<ThreadBase> t = mThread.promote();
2982 if (t == nullptr) {
2983 return false;
2984 }
2985 t->disconnectEffectHandle(handle, unpinIfLast);
2986 return true;
2987}
2988
2989void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2990 sp<EffectChain> c = mChain.promote();
2991 if (c == nullptr) {
2992 return;
2993 }
2994 c->resetVolume_l();
2995
2996}
2997
2998uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2999 sp<EffectChain> c = mChain.promote();
3000 if (c == nullptr) {
3001 return PRODUCT_STRATEGY_NONE;
3002 }
3003 return c->strategy();
3004}
3005
3006int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
3007 sp<EffectChain> c = mChain.promote();
3008 if (c == nullptr) {
3009 return 0;
3010 }
3011 return c->activeTrackCnt();
3012}
3013
Eric Laurentb82e6b72019-11-22 17:25:04 -08003014
3015#undef LOG_TAG
3016#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
3017
3018status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
3019{
3020 status_t status = EffectBase::setEnabled(enabled, fromHandle);
3021 Mutex::Autolock _l(mProxyLock);
3022 if (status == NO_ERROR) {
3023 for (auto& handle : mEffectHandles) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003024 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003025 if (enabled) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003026 bs = handle.second->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003027 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003028 bs = handle.second->disable(&status);
3029 }
3030 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003031 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003032 }
3033 }
3034 }
3035 ALOGV("%s enable %d status %d", __func__, enabled, status);
3036 return status;
3037}
3038
3039status_t AudioFlinger::DeviceEffectProxy::init(
3040 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
3041//For all audio patches
3042//If src or sink device match
3043//If the effect is HW accelerated
3044// if no corresponding effect module
3045// Create EffectModule: mHalEffect
3046//Create and attach EffectHandle
3047//If the effect is not HW accelerated and the patch sink or src is a mixer port
3048// Create Effect on patch input or output thread on session -1
3049//Add EffectHandle to EffectHandle map of Effect Proxy:
3050 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
3051 status_t status = NO_ERROR;
3052 for (auto &patch : patches) {
3053 status = onCreatePatch(patch.first, patch.second);
3054 ALOGV("%s onCreatePatch status %d", __func__, status);
3055 if (status == BAD_VALUE) {
3056 return status;
3057 }
3058 }
3059 return status;
3060}
3061
3062status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
3063 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
3064 status_t status = NAME_NOT_FOUND;
3065 sp<EffectHandle> handle;
3066 // only consider source[0] as this is the only "true" source of a patch
3067 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
3068 ALOGV("%s source checkPort status %d", __func__, status);
3069 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
3070 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
3071 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
3072 }
3073 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3074 Mutex::Autolock _l(mProxyLock);
3075 mEffectHandles.emplace(patchHandle, handle);
3076 }
3077 ALOGW_IF(status == BAD_VALUE,
3078 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
3079
3080 return status;
3081}
3082
3083status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
3084 const struct audio_port_config *port, sp <EffectHandle> *handle) {
3085
3086 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
3087 __func__, port->type, port->ext.device.type,
3088 port->ext.device.address, port->id, patch.isSoftware());
3089 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
jiabin0a488932020-08-07 17:32:40 -07003090 || port->ext.device.address != mDevice.address()) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08003091 return NAME_NOT_FOUND;
3092 }
3093 status_t status = NAME_NOT_FOUND;
3094
3095 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3096 Mutex::Autolock _l(mProxyLock);
3097 mDevicePort = *port;
3098 mHalEffect = new EffectModule(mMyCallback,
3099 const_cast<effect_descriptor_t *>(&mDescriptor),
3100 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3101 false /* pinned */, port->id);
3102 if (audio_is_input_device(mDevice.mType)) {
3103 mHalEffect->setInputDevice(mDevice);
3104 } else {
3105 mHalEffect->setDevices({mDevice});
3106 }
3107 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3108 status = (*handle)->initCheck();
3109 if (status == OK) {
3110 status = mHalEffect->addHandle((*handle).get());
3111 } else {
3112 mHalEffect.clear();
3113 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3114 }
3115 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3116 sp <ThreadBase> thread;
3117 if (audio_port_config_has_input_direction(port)) {
3118 if (patch.isSoftware()) {
3119 thread = patch.mRecord.thread();
3120 } else {
3121 thread = patch.thread().promote();
3122 }
3123 } else {
3124 if (patch.isSoftware()) {
3125 thread = patch.mPlayback.thread();
3126 } else {
3127 thread = patch.thread().promote();
3128 }
3129 }
3130 int enabled;
3131 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3132 const_cast<effect_descriptor_t *>(&mDescriptor),
Eric Laurent2fe0acd2020-03-13 14:30:46 -07003133 &enabled, &status, false, false /*probe*/);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003134 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3135 } else {
3136 status = BAD_VALUE;
3137 }
3138 if (status == NO_ERROR || status == ALREADY_EXISTS) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003139 Status bs;
Eric Laurentb82e6b72019-11-22 17:25:04 -08003140 if (isEnabled()) {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003141 bs = (*handle)->enable(&status);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003142 } else {
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -07003143 bs = (*handle)->disable(&status);
3144 }
3145 if (!bs.isOk()) {
Andy Hung1131b6e2020-12-08 20:47:45 -08003146 status = statusTFromBinderStatus(bs);
Eric Laurentb82e6b72019-11-22 17:25:04 -08003147 }
3148 }
3149 return status;
3150}
3151
3152void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3153 Mutex::Autolock _l(mProxyLock);
3154 mEffectHandles.erase(patchHandle);
3155}
3156
3157
3158size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3159{
3160 Mutex::Autolock _l(mProxyLock);
3161 if (effect == mHalEffect) {
3162 mHalEffect.clear();
3163 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3164 }
3165 return mHalEffect == nullptr ? 0 : 1;
3166}
3167
3168status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3169 sp<EffectHalInterface> effect) {
3170 if (mHalEffect == nullptr) {
3171 return NO_INIT;
3172 }
3173 return mManagerCallback->addEffectToHal(
3174 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3175}
3176
3177status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3178 sp<EffectHalInterface> effect) {
3179 if (mHalEffect == nullptr) {
3180 return NO_INIT;
3181 }
3182 return mManagerCallback->removeEffectFromHal(
3183 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3184}
3185
3186bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3187 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3188 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3189 }
3190 return true;
3191}
3192
3193uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3194 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3195 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3196 return mDevicePort.sample_rate;
3197 }
3198 return DEFAULT_OUTPUT_SAMPLE_RATE;
3199}
3200
3201audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3202 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3203 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3204 return mDevicePort.channel_mask;
3205 }
3206 return AUDIO_CHANNEL_OUT_STEREO;
3207}
3208
3209uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3210 if (isOutput()) {
3211 return audio_channel_count_from_out_mask(channelMask());
3212 }
3213 return audio_channel_count_from_in_mask(channelMask());
3214}
3215
3216void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3217 const Vector<String16> args;
3218 EffectBase::dump(fd, args);
3219
3220 const bool locked = dumpTryLock(mProxyLock);
3221 if (!locked) {
3222 String8 result("DeviceEffectProxy may be deadlocked\n");
3223 write(fd, result.string(), result.size());
3224 }
3225
3226 String8 outStr;
3227 if (mHalEffect != nullptr) {
3228 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3229 } else {
3230 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3231 }
3232 write(fd, outStr.string(), outStr.size());
3233 outStr.clear();
3234
3235 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3236 write(fd, outStr.string(), outStr.size());
3237 outStr.clear();
3238
3239 for (const auto& iter : mEffectHandles) {
3240 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3241 write(fd, outStr.string(), outStr.size());
3242 outStr.clear();
3243 sp<EffectBase> effect = iter.second->effect().promote();
3244 if (effect != nullptr) {
3245 effect->dump(fd, args);
3246 }
3247 }
3248
3249 if (locked) {
3250 mLock.unlock();
3251 }
3252}
3253
3254#undef LOG_TAG
3255#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3256
3257int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3258 return mManagerCallback->newEffectId();
3259}
3260
3261
3262bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3263 EffectHandle *handle, bool unpinIfLast) {
3264 sp<EffectBase> effectBase = handle->effect().promote();
3265 if (effectBase == nullptr) {
3266 return false;
3267 }
3268
3269 sp<EffectModule> effect = effectBase->asEffectModule();
3270 if (effect == nullptr) {
3271 return false;
3272 }
3273
3274 // restore suspended effects if the disconnected handle was enabled and the last one.
3275 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3276 if (remove) {
3277 sp<DeviceEffectProxy> proxy = mProxy.promote();
3278 if (proxy != nullptr) {
3279 proxy->removeEffect(effect);
3280 }
3281 if (handle->enabled()) {
3282 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3283 }
3284 }
3285 return true;
3286}
3287
3288status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3289 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3290 sp<EffectHalInterface> *effect) {
3291 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3292}
3293
3294status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3295 sp<EffectHalInterface> effect) {
3296 sp<DeviceEffectProxy> proxy = mProxy.promote();
3297 if (proxy == nullptr) {
3298 return NO_INIT;
3299 }
3300 return proxy->addEffectToHal(effect);
3301}
3302
3303status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3304 sp<EffectHalInterface> effect) {
3305 sp<DeviceEffectProxy> proxy = mProxy.promote();
3306 if (proxy == nullptr) {
3307 return NO_INIT;
3308 }
3309 return proxy->addEffectToHal(effect);
3310}
3311
3312bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3313 sp<DeviceEffectProxy> proxy = mProxy.promote();
3314 if (proxy == nullptr) {
3315 return true;
3316 }
3317 return proxy->isOutput();
3318}
3319
3320uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3321 sp<DeviceEffectProxy> proxy = mProxy.promote();
3322 if (proxy == nullptr) {
3323 return DEFAULT_OUTPUT_SAMPLE_RATE;
3324 }
3325 return proxy->sampleRate();
3326}
3327
3328audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3329 sp<DeviceEffectProxy> proxy = mProxy.promote();
3330 if (proxy == nullptr) {
3331 return AUDIO_CHANNEL_OUT_STEREO;
3332 }
3333 return proxy->channelMask();
3334}
3335
3336uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3337 sp<DeviceEffectProxy> proxy = mProxy.promote();
3338 if (proxy == nullptr) {
3339 return 2;
3340 }
3341 return proxy->channelCount();
3342}
3343
Glenn Kasten63238ef2015-03-02 15:50:29 -08003344} // namespace android