blob: ef2daa21df04fca514977dd5907d8a9c0567c084 [file] [log] [blame]
Eric Laurentca7cc822012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
rago94a1ee82017-07-21 15:11:02 -070022#include <algorithm>
23
Glenn Kasten153b9fe2013-07-15 11:23:36 -070024#include "Configuration.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080025#include <utils/Log.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070026#include <system/audio_effects/effect_aec.h>
Ricardo Garciac2a3a822019-07-17 14:29:12 -070027#include <system/audio_effects/effect_dynamicsprocessing.h>
Eric Laurentd8365c52017-07-16 15:27:05 -070028#include <system/audio_effects/effect_ns.h>
29#include <system/audio_effects/effect_visualizer.h>
Andy Hung9aad48c2017-11-29 10:29:19 -080030#include <audio_utils/channels.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080031#include <audio_utils/primitives.h>
jiabin8f278ee2019-11-11 12:16:27 -080032#include <media/AudioContainers.h>
Mikhail Naganov424c4f52017-07-19 17:54:29 -070033#include <media/AudioEffect.h>
jiabin8f278ee2019-11-11 12:16:27 -080034#include <media/AudioDeviceTypeAddr.h>
Mikhail Naganova0c91332016-09-19 10:01:12 -070035#include <media/audiohal/EffectHalInterface.h>
36#include <media/audiohal/EffectsFactoryHalInterface.h>
Andy Hungab7ef302018-05-15 19:35:29 -070037#include <mediautils/ServiceUtilities.h>
Eric Laurentca7cc822012-11-19 14:55:58 -080038
39#include "AudioFlinger.h"
Eric Laurentca7cc822012-11-19 14:55:58 -080040
41// ----------------------------------------------------------------------------
42
43// Note: the following macro is used for extremely verbose logging message. In
44// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
45// 0; but one side effect of this is to turn all LOGV's as well. Some messages
46// are so verbose that we want to suppress them even when we have ALOG_ASSERT
47// turned on. Do not uncomment the #def below unless you really know what you
48// are doing and want to see all of the extremely verbose messages.
49//#define VERY_VERY_VERBOSE_LOGGING
50#ifdef VERY_VERY_VERBOSE_LOGGING
51#define ALOGVV ALOGV
52#else
53#define ALOGVV(a...) do { } while(0)
54#endif
55
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +090056#define DEFAULT_OUTPUT_SAMPLE_RATE 48000
57
Eric Laurentca7cc822012-11-19 14:55:58 -080058namespace android {
59
60// ----------------------------------------------------------------------------
Eric Laurent41709552019-12-16 19:34:05 -080061// EffectBase implementation
Eric Laurentca7cc822012-11-19 14:55:58 -080062// ----------------------------------------------------------------------------
63
64#undef LOG_TAG
Eric Laurent41709552019-12-16 19:34:05 -080065#define LOG_TAG "AudioFlinger::EffectBase"
Eric Laurentca7cc822012-11-19 14:55:58 -080066
Eric Laurent41709552019-12-16 19:34:05 -080067AudioFlinger::EffectBase::EffectBase(const sp<AudioFlinger::EffectCallbackInterface>& callback,
Eric Laurentca7cc822012-11-19 14:55:58 -080068 effect_descriptor_t *desc,
69 int id,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080070 audio_session_t sessionId,
71 bool pinned)
72 : mPinned(pinned),
Eric Laurent6b446ce2019-12-13 10:56:31 -080073 mCallback(callback), mId(id), mSessionId(sessionId),
Eric Laurent41709552019-12-16 19:34:05 -080074 mDescriptor(*desc)
Eric Laurentca7cc822012-11-19 14:55:58 -080075{
Eric Laurentca7cc822012-11-19 14:55:58 -080076}
77
Eric Laurent41709552019-12-16 19:34:05 -080078// must be called with EffectModule::mLock held
79status_t AudioFlinger::EffectBase::setEnabled_l(bool enabled)
Eric Laurentca7cc822012-11-19 14:55:58 -080080{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -080081
Eric Laurent41709552019-12-16 19:34:05 -080082 ALOGV("setEnabled %p enabled %d", this, enabled);
83
84 if (enabled != isEnabled()) {
85 switch (mState) {
86 // going from disabled to enabled
87 case IDLE:
88 mState = STARTING;
89 break;
90 case STOPPED:
91 mState = RESTART;
92 break;
93 case STOPPING:
94 mState = ACTIVE;
95 break;
96
97 // going from enabled to disabled
98 case RESTART:
99 mState = STOPPED;
100 break;
101 case STARTING:
102 mState = IDLE;
103 break;
104 case ACTIVE:
105 mState = STOPPING;
106 break;
107 case DESTROYED:
108 return NO_ERROR; // simply ignore as we are being destroyed
109 }
110 for (size_t i = 1; i < mHandles.size(); i++) {
111 EffectHandle *h = mHandles[i];
112 if (h != NULL && !h->disconnected()) {
113 h->setEnabled(enabled);
114 }
115 }
116 }
117 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -0800118}
119
Eric Laurent41709552019-12-16 19:34:05 -0800120status_t AudioFlinger::EffectBase::setEnabled(bool enabled, bool fromHandle)
121{
122 status_t status;
123 {
124 Mutex::Autolock _l(mLock);
125 status = setEnabled_l(enabled);
126 }
127 if (fromHandle) {
128 if (enabled) {
129 if (status != NO_ERROR) {
130 mCallback->checkSuspendOnEffectEnabled(this, false, false /*threadLocked*/);
131 } else {
132 mCallback->onEffectEnable(this);
133 }
134 } else {
135 mCallback->onEffectDisable(this);
136 }
137 }
138 return status;
139}
140
141bool AudioFlinger::EffectBase::isEnabled() const
142{
143 switch (mState) {
144 case RESTART:
145 case STARTING:
146 case ACTIVE:
147 return true;
148 case IDLE:
149 case STOPPING:
150 case STOPPED:
151 case DESTROYED:
152 default:
153 return false;
154 }
155}
156
157void AudioFlinger::EffectBase::setSuspended(bool suspended)
158{
159 Mutex::Autolock _l(mLock);
160 mSuspended = suspended;
161}
162
163bool AudioFlinger::EffectBase::suspended() const
164{
165 Mutex::Autolock _l(mLock);
166 return mSuspended;
167}
168
169status_t AudioFlinger::EffectBase::addHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800170{
171 status_t status;
172
173 Mutex::Autolock _l(mLock);
174 int priority = handle->priority();
175 size_t size = mHandles.size();
176 EffectHandle *controlHandle = NULL;
177 size_t i;
178 for (i = 0; i < size; i++) {
179 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800180 if (h == NULL || h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800181 continue;
182 }
183 // first non destroyed handle is considered in control
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700184 if (controlHandle == NULL) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800185 controlHandle = h;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700186 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800187 if (h->priority() <= priority) {
188 break;
189 }
190 }
191 // if inserted in first place, move effect control from previous owner to this handle
192 if (i == 0) {
193 bool enabled = false;
194 if (controlHandle != NULL) {
195 enabled = controlHandle->enabled();
196 controlHandle->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
197 }
198 handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
199 status = NO_ERROR;
200 } else {
201 status = ALREADY_EXISTS;
202 }
Glenn Kastenc42e9b42016-03-21 11:35:03 -0700203 ALOGV("addHandle() %p added handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800204 mHandles.insertAt(handle, i);
205 return status;
206}
207
Eric Laurent41709552019-12-16 19:34:05 -0800208status_t AudioFlinger::EffectBase::updatePolicyState()
Eric Laurent6c796322019-04-09 14:13:17 -0700209{
210 status_t status = NO_ERROR;
211 bool doRegister = false;
212 bool registered = false;
213 bool doEnable = false;
214 bool enabled = false;
215 audio_io_handle_t io;
216 uint32_t strategy;
217
218 {
219 Mutex::Autolock _l(mLock);
220 // register effect when first handle is attached and unregister when last handle is removed
221 if (mPolicyRegistered != mHandles.size() > 0) {
222 doRegister = true;
223 mPolicyRegistered = mHandles.size() > 0;
224 if (mPolicyRegistered) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800225 io = mCallback->io();
226 strategy = mCallback->strategy();
Eric Laurent6c796322019-04-09 14:13:17 -0700227 }
228 }
229 // enable effect when registered according to enable state requested by controlling handle
230 if (mHandles.size() > 0) {
231 EffectHandle *handle = controlHandle_l();
232 if (handle != nullptr && mPolicyEnabled != handle->enabled()) {
233 doEnable = true;
234 mPolicyEnabled = handle->enabled();
235 }
236 }
237 registered = mPolicyRegistered;
238 enabled = mPolicyEnabled;
239 mPolicyLock.lock();
240 }
241 ALOGV("%s name %s id %d session %d doRegister %d registered %d doEnable %d enabled %d",
242 __func__, mDescriptor.name, mId, mSessionId, doRegister, registered, doEnable, enabled);
243 if (doRegister) {
244 if (registered) {
245 status = AudioSystem::registerEffect(
246 &mDescriptor,
247 io,
248 strategy,
249 mSessionId,
250 mId);
251 } else {
252 status = AudioSystem::unregisterEffect(mId);
253 }
254 }
255 if (registered && doEnable) {
256 status = AudioSystem::setEffectEnabled(mId, enabled);
257 }
258 mPolicyLock.unlock();
259
260 return status;
261}
262
263
Eric Laurent41709552019-12-16 19:34:05 -0800264ssize_t AudioFlinger::EffectBase::removeHandle(EffectHandle *handle)
Eric Laurentca7cc822012-11-19 14:55:58 -0800265{
266 Mutex::Autolock _l(mLock);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800267 return removeHandle_l(handle);
268}
269
Eric Laurent41709552019-12-16 19:34:05 -0800270ssize_t AudioFlinger::EffectBase::removeHandle_l(EffectHandle *handle)
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800271{
Eric Laurentca7cc822012-11-19 14:55:58 -0800272 size_t size = mHandles.size();
273 size_t i;
274 for (i = 0; i < size; i++) {
275 if (mHandles[i] == handle) {
276 break;
277 }
278 }
279 if (i == size) {
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800280 ALOGW("%s %p handle not found %p", __FUNCTION__, this, handle);
281 return BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -0800282 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800283 ALOGV("removeHandle_l() %p removed handle %p in position %zu", this, handle, i);
Eric Laurentca7cc822012-11-19 14:55:58 -0800284
285 mHandles.removeAt(i);
286 // if removed from first place, move effect control from this handle to next in line
287 if (i == 0) {
288 EffectHandle *h = controlHandle_l();
289 if (h != NULL) {
290 h->setControl(true /*hasControl*/, true /*signal*/ , handle->enabled() /*enabled*/);
291 }
292 }
293
Eric Laurentca7cc822012-11-19 14:55:58 -0800294 if (mHandles.size() == 0 && !mPinned) {
295 mState = DESTROYED;
296 }
297
298 return mHandles.size();
299}
300
301// must be called with EffectModule::mLock held
Eric Laurent41709552019-12-16 19:34:05 -0800302AudioFlinger::EffectHandle *AudioFlinger::EffectBase::controlHandle_l()
Eric Laurentca7cc822012-11-19 14:55:58 -0800303{
304 // the first valid handle in the list has control over the module
305 for (size_t i = 0; i < mHandles.size(); i++) {
306 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -0800307 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800308 return h;
309 }
310 }
311
312 return NULL;
313}
314
Eric Laurentf10c7092016-12-06 17:09:56 -0800315// unsafe method called when the effect parent thread has been destroyed
Eric Laurent41709552019-12-16 19:34:05 -0800316ssize_t AudioFlinger::EffectBase::disconnectHandle(EffectHandle *handle, bool unpinIfLast)
Eric Laurentf10c7092016-12-06 17:09:56 -0800317{
318 ALOGV("disconnect() %p handle %p", this, handle);
Eric Laurent6b446ce2019-12-13 10:56:31 -0800319 if (mCallback->disconnectEffectHandle(handle, unpinIfLast)) {
320 return mHandles.size();
321 }
322
Eric Laurentf10c7092016-12-06 17:09:56 -0800323 Mutex::Autolock _l(mLock);
324 ssize_t numHandles = removeHandle_l(handle);
325 if ((numHandles == 0) && (!mPinned || unpinIfLast)) {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800326 mLock.unlock();
327 mCallback->updateOrphanEffectChains(this);
328 mLock.lock();
Eric Laurentf10c7092016-12-06 17:09:56 -0800329 }
330 return numHandles;
331}
332
Eric Laurent41709552019-12-16 19:34:05 -0800333bool AudioFlinger::EffectBase::purgeHandles()
334{
335 bool enabled = false;
336 Mutex::Autolock _l(mLock);
337 EffectHandle *handle = controlHandle_l();
338 if (handle != NULL) {
339 enabled = handle->enabled();
340 }
341 mHandles.clear();
342 return enabled;
343}
344
345void AudioFlinger::EffectBase::checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) {
346 mCallback->checkSuspendOnEffectEnabled(this, enabled, threadLocked);
347}
348
349static String8 effectFlagsToString(uint32_t flags) {
350 String8 s;
351
352 s.append("conn. mode: ");
353 switch (flags & EFFECT_FLAG_TYPE_MASK) {
354 case EFFECT_FLAG_TYPE_INSERT: s.append("insert"); break;
355 case EFFECT_FLAG_TYPE_AUXILIARY: s.append("auxiliary"); break;
356 case EFFECT_FLAG_TYPE_REPLACE: s.append("replace"); break;
357 case EFFECT_FLAG_TYPE_PRE_PROC: s.append("preproc"); break;
358 case EFFECT_FLAG_TYPE_POST_PROC: s.append("postproc"); break;
359 default: s.append("unknown/reserved"); break;
360 }
361 s.append(", ");
362
363 s.append("insert pref: ");
364 switch (flags & EFFECT_FLAG_INSERT_MASK) {
365 case EFFECT_FLAG_INSERT_ANY: s.append("any"); break;
366 case EFFECT_FLAG_INSERT_FIRST: s.append("first"); break;
367 case EFFECT_FLAG_INSERT_LAST: s.append("last"); break;
368 case EFFECT_FLAG_INSERT_EXCLUSIVE: s.append("exclusive"); break;
369 default: s.append("unknown/reserved"); break;
370 }
371 s.append(", ");
372
373 s.append("volume mgmt: ");
374 switch (flags & EFFECT_FLAG_VOLUME_MASK) {
375 case EFFECT_FLAG_VOLUME_NONE: s.append("none"); break;
376 case EFFECT_FLAG_VOLUME_CTRL: s.append("implements control"); break;
377 case EFFECT_FLAG_VOLUME_IND: s.append("requires indication"); break;
378 case EFFECT_FLAG_VOLUME_MONITOR: s.append("monitors volume"); break;
379 default: s.append("unknown/reserved"); break;
380 }
381 s.append(", ");
382
383 uint32_t devind = flags & EFFECT_FLAG_DEVICE_MASK;
384 if (devind) {
385 s.append("device indication: ");
386 switch (devind) {
387 case EFFECT_FLAG_DEVICE_IND: s.append("requires updates"); break;
388 default: s.append("unknown/reserved"); break;
389 }
390 s.append(", ");
391 }
392
393 s.append("input mode: ");
394 switch (flags & EFFECT_FLAG_INPUT_MASK) {
395 case EFFECT_FLAG_INPUT_DIRECT: s.append("direct"); break;
396 case EFFECT_FLAG_INPUT_PROVIDER: s.append("provider"); break;
397 case EFFECT_FLAG_INPUT_BOTH: s.append("direct+provider"); break;
398 default: s.append("not set"); break;
399 }
400 s.append(", ");
401
402 s.append("output mode: ");
403 switch (flags & EFFECT_FLAG_OUTPUT_MASK) {
404 case EFFECT_FLAG_OUTPUT_DIRECT: s.append("direct"); break;
405 case EFFECT_FLAG_OUTPUT_PROVIDER: s.append("provider"); break;
406 case EFFECT_FLAG_OUTPUT_BOTH: s.append("direct+provider"); break;
407 default: s.append("not set"); break;
408 }
409 s.append(", ");
410
411 uint32_t accel = flags & EFFECT_FLAG_HW_ACC_MASK;
412 if (accel) {
413 s.append("hardware acceleration: ");
414 switch (accel) {
415 case EFFECT_FLAG_HW_ACC_SIMPLE: s.append("non-tunneled"); break;
416 case EFFECT_FLAG_HW_ACC_TUNNEL: s.append("tunneled"); break;
417 default: s.append("unknown/reserved"); break;
418 }
419 s.append(", ");
420 }
421
422 uint32_t modeind = flags & EFFECT_FLAG_AUDIO_MODE_MASK;
423 if (modeind) {
424 s.append("mode indication: ");
425 switch (modeind) {
426 case EFFECT_FLAG_AUDIO_MODE_IND: s.append("required"); break;
427 default: s.append("unknown/reserved"); break;
428 }
429 s.append(", ");
430 }
431
432 uint32_t srcind = flags & EFFECT_FLAG_AUDIO_SOURCE_MASK;
433 if (srcind) {
434 s.append("source indication: ");
435 switch (srcind) {
436 case EFFECT_FLAG_AUDIO_SOURCE_IND: s.append("required"); break;
437 default: s.append("unknown/reserved"); break;
438 }
439 s.append(", ");
440 }
441
442 if (flags & EFFECT_FLAG_OFFLOAD_MASK) {
443 s.append("offloadable, ");
444 }
445
446 int len = s.length();
447 if (s.length() > 2) {
448 (void) s.lockBuffer(len);
449 s.unlockBuffer(len - 2);
450 }
451 return s;
452}
453
454void AudioFlinger::EffectBase::dump(int fd, const Vector<String16>& args __unused)
455{
456 String8 result;
457
458 result.appendFormat("\tEffect ID %d:\n", mId);
459
460 bool locked = AudioFlinger::dumpTryLock(mLock);
461 // failed to lock - AudioFlinger is probably deadlocked
462 if (!locked) {
463 result.append("\t\tCould not lock Fx mutex:\n");
464 }
465
466 result.append("\t\tSession State Registered Enabled Suspended:\n");
467 result.appendFormat("\t\t%05d %03d %s %s %s\n",
468 mSessionId, mState, mPolicyRegistered ? "y" : "n",
469 mPolicyEnabled ? "y" : "n", mSuspended ? "y" : "n");
470
471 result.append("\t\tDescriptor:\n");
472 char uuidStr[64];
473 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
474 result.appendFormat("\t\t- UUID: %s\n", uuidStr);
475 AudioEffect::guidToString(&mDescriptor.type, uuidStr, sizeof(uuidStr));
476 result.appendFormat("\t\t- TYPE: %s\n", uuidStr);
477 result.appendFormat("\t\t- apiVersion: %08X\n\t\t- flags: %08X (%s)\n",
478 mDescriptor.apiVersion,
479 mDescriptor.flags,
480 effectFlagsToString(mDescriptor.flags).string());
481 result.appendFormat("\t\t- name: %s\n",
482 mDescriptor.name);
483
484 result.appendFormat("\t\t- implementor: %s\n",
485 mDescriptor.implementor);
486
487 result.appendFormat("\t\t%zu Clients:\n", mHandles.size());
488 result.append("\t\t\t Pid Priority Ctrl Locked client server\n");
489 char buffer[256];
490 for (size_t i = 0; i < mHandles.size(); ++i) {
491 EffectHandle *handle = mHandles[i];
492 if (handle != NULL && !handle->disconnected()) {
493 handle->dumpToBuffer(buffer, sizeof(buffer));
494 result.append(buffer);
495 }
496 }
497 if (locked) {
498 mLock.unlock();
499 }
500
501 write(fd, result.string(), result.length());
502}
503
504// ----------------------------------------------------------------------------
505// EffectModule implementation
506// ----------------------------------------------------------------------------
507
508#undef LOG_TAG
509#define LOG_TAG "AudioFlinger::EffectModule"
510
511AudioFlinger::EffectModule::EffectModule(const sp<AudioFlinger::EffectCallbackInterface>& callback,
512 effect_descriptor_t *desc,
513 int id,
514 audio_session_t sessionId,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800515 bool pinned,
516 audio_port_handle_t deviceId)
Eric Laurent41709552019-12-16 19:34:05 -0800517 : EffectBase(callback, desc, id, sessionId, pinned),
518 // clear mConfig to ensure consistent initial value of buffer framecount
519 // in case buffers are associated by setInBuffer() or setOutBuffer()
520 // prior to configure().
521 mConfig{{}, {}},
522 mStatus(NO_INIT),
523 mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
524 mDisableWaitCnt(0), // set by process() and updateState()
525 mOffloaded(false)
526#ifdef FLOAT_EFFECT_CHAIN
527 , mSupportsFloat(false)
528#endif
529{
530 ALOGV("Constructor %p pinned %d", this, pinned);
531 int lStatus;
532
533 // create effect engine from effect factory
534 mStatus = callback->createEffectHal(
Eric Laurentb82e6b72019-11-22 17:25:04 -0800535 &desc->uuid, sessionId, deviceId, &mEffectInterface);
Eric Laurent41709552019-12-16 19:34:05 -0800536 if (mStatus != NO_ERROR) {
537 return;
538 }
539 lStatus = init();
540 if (lStatus < 0) {
541 mStatus = lStatus;
542 goto Error;
543 }
544
545 setOffloaded(callback->isOffload(), callback->io());
546 ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
547
548 return;
549Error:
550 mEffectInterface.clear();
551 ALOGV("Constructor Error %d", mStatus);
552}
553
554AudioFlinger::EffectModule::~EffectModule()
555{
556 ALOGV("Destructor %p", this);
557 if (mEffectInterface != 0) {
558 char uuidStr[64];
559 AudioEffect::guidToString(&mDescriptor.uuid, uuidStr, sizeof(uuidStr));
560 ALOGW("EffectModule %p destructor called with unreleased interface, effect %s",
561 this, uuidStr);
562 release_l();
563 }
564
565}
566
567ssize_t AudioFlinger::EffectModule::removeHandle_l(EffectHandle *handle)
568{
569 ssize_t status = EffectBase::removeHandle_l(handle);
570
571 // Prevent calls to process() and other functions on effect interface from now on.
572 // The effect engine will be released by the destructor when the last strong reference on
573 // this object is released which can happen after next process is called.
574 if (status == 0 && !mPinned) {
575 mEffectInterface->close();
576 }
577
578 return status;
579}
580
Eric Laurentfa1e1232016-08-02 19:01:49 -0700581bool AudioFlinger::EffectModule::updateState() {
Eric Laurentca7cc822012-11-19 14:55:58 -0800582 Mutex::Autolock _l(mLock);
583
Eric Laurentfa1e1232016-08-02 19:01:49 -0700584 bool started = false;
Eric Laurentca7cc822012-11-19 14:55:58 -0800585 switch (mState) {
586 case RESTART:
587 reset_l();
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700588 FALLTHROUGH_INTENDED;
Eric Laurentca7cc822012-11-19 14:55:58 -0800589
590 case STARTING:
591 // clear auxiliary effect input buffer for next accumulation
592 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
593 memset(mConfig.inputCfg.buffer.raw,
594 0,
595 mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
596 }
Eric Laurentd0ebb532013-04-02 16:41:41 -0700597 if (start_l() == NO_ERROR) {
598 mState = ACTIVE;
Eric Laurentfa1e1232016-08-02 19:01:49 -0700599 started = true;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700600 } else {
601 mState = IDLE;
602 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800603 break;
604 case STOPPING:
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +0900605 // volume control for offload and direct threads must take effect immediately.
606 if (stop_l() == NO_ERROR
607 && !(isVolumeControl() && isOffloadedOrDirect())) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700608 mDisableWaitCnt = mMaxDisableWaitCnt;
609 } else {
610 mDisableWaitCnt = 1; // will cause immediate transition to IDLE
611 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800612 mState = STOPPED;
613 break;
614 case STOPPED:
615 // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
616 // turn off sequence.
617 if (--mDisableWaitCnt == 0) {
618 reset_l();
619 mState = IDLE;
620 }
621 break;
622 default: //IDLE , ACTIVE, DESTROYED
623 break;
624 }
Eric Laurentfa1e1232016-08-02 19:01:49 -0700625
626 return started;
Eric Laurentca7cc822012-11-19 14:55:58 -0800627}
628
629void AudioFlinger::EffectModule::process()
630{
631 Mutex::Autolock _l(mLock);
632
Mikhail Naganov022b9952017-01-04 16:36:51 -0800633 if (mState == DESTROYED || mEffectInterface == 0 || mInBuffer == 0 || mOutBuffer == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800634 return;
635 }
636
rago94a1ee82017-07-21 15:11:02 -0700637 const uint32_t inChannelCount =
638 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
639 const uint32_t outChannelCount =
640 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
641 const bool auxType =
642 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
643
Andy Hungfa69ca32017-11-30 10:07:53 -0800644 // safeInputOutputSampleCount is 0 if the channel count between input and output
645 // buffers do not match. This prevents automatic accumulation or copying between the
646 // input and output effect buffers without an intermediary effect process.
647 // TODO: consider implementing channel conversion.
648 const size_t safeInputOutputSampleCount =
Andy Hungdd2e7a82018-10-31 14:19:13 -0700649 mInChannelCountRequested != mOutChannelCountRequested ? 0
650 : mOutChannelCountRequested * std::min(
Andy Hungfa69ca32017-11-30 10:07:53 -0800651 mConfig.inputCfg.buffer.frameCount,
652 mConfig.outputCfg.buffer.frameCount);
653 const auto accumulateInputToOutput = [this, safeInputOutputSampleCount]() {
654#ifdef FLOAT_EFFECT_CHAIN
655 accumulate_float(
656 mConfig.outputCfg.buffer.f32,
657 mConfig.inputCfg.buffer.f32,
658 safeInputOutputSampleCount);
659#else
660 accumulate_i16(
661 mConfig.outputCfg.buffer.s16,
662 mConfig.inputCfg.buffer.s16,
663 safeInputOutputSampleCount);
664#endif
665 };
666 const auto copyInputToOutput = [this, safeInputOutputSampleCount]() {
667#ifdef FLOAT_EFFECT_CHAIN
668 memcpy(
669 mConfig.outputCfg.buffer.f32,
670 mConfig.inputCfg.buffer.f32,
671 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.f32));
672
673#else
674 memcpy(
675 mConfig.outputCfg.buffer.s16,
676 mConfig.inputCfg.buffer.s16,
677 safeInputOutputSampleCount * sizeof(*mConfig.outputCfg.buffer.s16));
678#endif
679 };
680
Eric Laurentca7cc822012-11-19 14:55:58 -0800681 if (isProcessEnabled()) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700682 int ret;
683 if (isProcessImplemented()) {
rago94a1ee82017-07-21 15:11:02 -0700684 if (auxType) {
685 // We overwrite the aux input buffer here and clear after processing.
Andy Hung9aad48c2017-11-29 10:29:19 -0800686 // aux input is always mono.
rago94a1ee82017-07-21 15:11:02 -0700687#ifdef FLOAT_EFFECT_CHAIN
688 if (mSupportsFloat) {
Andy Hung116a4982017-11-30 10:15:08 -0800689#ifndef FLOAT_AUX
rago94a1ee82017-07-21 15:11:02 -0700690 // Do in-place float conversion for auxiliary effect input buffer.
691 static_assert(sizeof(float) <= sizeof(int32_t),
692 "in-place conversion requires sizeof(float) <= sizeof(int32_t)");
693
Andy Hungfa69ca32017-11-30 10:07:53 -0800694 memcpy_to_float_from_q4_27(
695 mConfig.inputCfg.buffer.f32,
696 mConfig.inputCfg.buffer.s32,
697 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800698#endif // !FLOAT_AUX
Andy Hungfa69ca32017-11-30 10:07:53 -0800699 } else
Andy Hung116a4982017-11-30 10:15:08 -0800700#endif // FLOAT_EFFECT_CHAIN
Andy Hungfa69ca32017-11-30 10:07:53 -0800701 {
Andy Hung116a4982017-11-30 10:15:08 -0800702#ifdef FLOAT_AUX
703 memcpy_to_i16_from_float(
704 mConfig.inputCfg.buffer.s16,
705 mConfig.inputCfg.buffer.f32,
706 mConfig.inputCfg.buffer.frameCount);
707#else
Andy Hungfa69ca32017-11-30 10:07:53 -0800708 memcpy_to_i16_from_q4_27(
709 mConfig.inputCfg.buffer.s16,
rago94a1ee82017-07-21 15:11:02 -0700710 mConfig.inputCfg.buffer.s32,
Andy Hung5effdf62017-11-27 13:51:40 -0800711 mConfig.inputCfg.buffer.frameCount);
Andy Hung116a4982017-11-30 10:15:08 -0800712#endif
rago94a1ee82017-07-21 15:11:02 -0700713 }
rago94a1ee82017-07-21 15:11:02 -0700714 }
715#ifdef FLOAT_EFFECT_CHAIN
Andy Hung9aad48c2017-11-29 10:29:19 -0800716 sp<EffectBufferHalInterface> inBuffer = mInBuffer;
717 sp<EffectBufferHalInterface> outBuffer = mOutBuffer;
718
719 if (!auxType && mInChannelCountRequested != inChannelCount) {
720 adjust_channels(
721 inBuffer->audioBuffer()->f32, mInChannelCountRequested,
722 mInConversionBuffer->audioBuffer()->f32, inChannelCount,
723 sizeof(float),
724 sizeof(float)
725 * mInChannelCountRequested * mConfig.inputCfg.buffer.frameCount);
726 inBuffer = mInConversionBuffer;
727 }
728 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE
729 && mOutChannelCountRequested != outChannelCount) {
730 adjust_selected_channels(
731 outBuffer->audioBuffer()->f32, mOutChannelCountRequested,
732 mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
733 sizeof(float),
734 sizeof(float)
735 * mOutChannelCountRequested * mConfig.outputCfg.buffer.frameCount);
736 outBuffer = mOutConversionBuffer;
737 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800738 if (!mSupportsFloat) { // convert input to int16_t as effect doesn't support float.
739 if (!auxType) {
740 if (mInConversionBuffer.get() == nullptr) {
741 ALOGW("%s: mInConversionBuffer is null, bypassing", __func__);
742 goto data_bypass;
rago94a1ee82017-07-21 15:11:02 -0700743 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800744 memcpy_to_i16_from_float(
745 mInConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800746 inBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800747 inChannelCount * mConfig.inputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800748 inBuffer = mInConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700749 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800750 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
751 if (mOutConversionBuffer.get() == nullptr) {
752 ALOGW("%s: mOutConversionBuffer is null, bypassing", __func__);
753 goto data_bypass;
754 }
755 memcpy_to_i16_from_float(
756 mOutConversionBuffer->audioBuffer()->s16,
Andy Hung9aad48c2017-11-29 10:29:19 -0800757 outBuffer->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800758 outChannelCount * mConfig.outputCfg.buffer.frameCount);
Andy Hung9aad48c2017-11-29 10:29:19 -0800759 outBuffer = mOutConversionBuffer;
rago94a1ee82017-07-21 15:11:02 -0700760 }
761 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800762#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -0800763 ret = mEffectInterface->process();
Andy Hungfa69ca32017-11-30 10:07:53 -0800764#ifdef FLOAT_EFFECT_CHAIN
765 if (!mSupportsFloat) { // convert output int16_t back to float.
Andy Hung9aad48c2017-11-29 10:29:19 -0800766 sp<EffectBufferHalInterface> target =
767 mOutChannelCountRequested != outChannelCount
768 ? mOutConversionBuffer : mOutBuffer;
769
Andy Hungfa69ca32017-11-30 10:07:53 -0800770 memcpy_to_float_from_i16(
Andy Hung9aad48c2017-11-29 10:29:19 -0800771 target->audioBuffer()->f32,
Andy Hungfa69ca32017-11-30 10:07:53 -0800772 mOutConversionBuffer->audioBuffer()->s16,
773 outChannelCount * mConfig.outputCfg.buffer.frameCount);
774 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800775 if (mOutChannelCountRequested != outChannelCount) {
776 adjust_selected_channels(mOutConversionBuffer->audioBuffer()->f32, outChannelCount,
777 mOutBuffer->audioBuffer()->f32, mOutChannelCountRequested,
778 sizeof(float),
779 sizeof(float) * outChannelCount * mConfig.outputCfg.buffer.frameCount);
780 }
rago94a1ee82017-07-21 15:11:02 -0700781#endif
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700782 } else {
rago94a1ee82017-07-21 15:11:02 -0700783#ifdef FLOAT_EFFECT_CHAIN
784 data_bypass:
785#endif
786 if (!auxType /* aux effects do not require data bypass */
Andy Hungfa69ca32017-11-30 10:07:53 -0800787 && mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700788 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
Andy Hungfa69ca32017-11-30 10:07:53 -0800789 accumulateInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700790 } else {
Andy Hungfa69ca32017-11-30 10:07:53 -0800791 copyInputToOutput();
Eric Laurent6dd0fd92016-09-15 12:44:53 -0700792 }
793 }
794 ret = -ENODATA;
795 }
Andy Hungfa69ca32017-11-30 10:07:53 -0800796
Eric Laurentca7cc822012-11-19 14:55:58 -0800797 // force transition to IDLE state when engine is ready
798 if (mState == STOPPED && ret == -ENODATA) {
799 mDisableWaitCnt = 1;
800 }
801
802 // clear auxiliary effect input buffer for next accumulation
rago94a1ee82017-07-21 15:11:02 -0700803 if (auxType) {
Andy Hung116a4982017-11-30 10:15:08 -0800804#ifdef FLOAT_AUX
805 const size_t size =
806 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(float);
807#else
rago94a1ee82017-07-21 15:11:02 -0700808 const size_t size =
809 mConfig.inputCfg.buffer.frameCount * inChannelCount * sizeof(int32_t);
Andy Hung116a4982017-11-30 10:15:08 -0800810#endif
rago94a1ee82017-07-21 15:11:02 -0700811 memset(mConfig.inputCfg.buffer.raw, 0, size);
Eric Laurentca7cc822012-11-19 14:55:58 -0800812 }
813 } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
rago94a1ee82017-07-21 15:11:02 -0700814 // mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw
Eric Laurentca7cc822012-11-19 14:55:58 -0800815 mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
816 // If an insert effect is idle and input buffer is different from output buffer,
817 // accumulate input onto output
Eric Laurent6b446ce2019-12-13 10:56:31 -0800818 if (mCallback->activeTrackCnt() != 0) {
Andy Hunge8ac1b22018-10-31 14:22:35 -0700819 // similar handling with data_bypass above.
820 if (mConfig.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE) {
821 accumulateInputToOutput();
822 } else { // EFFECT_BUFFER_ACCESS_WRITE
823 copyInputToOutput();
824 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800825 }
826 }
827}
828
829void AudioFlinger::EffectModule::reset_l()
830{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700831 if (mStatus != NO_ERROR || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800832 return;
833 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700834 mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, 0, NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -0800835}
836
837status_t AudioFlinger::EffectModule::configure()
838{
rago94a1ee82017-07-21 15:11:02 -0700839 ALOGVV("configure() started");
Eric Laurentd0ebb532013-04-02 16:41:41 -0700840 status_t status;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700841 uint32_t size;
842 audio_channel_mask_t channelMask;
843
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700844 if (mEffectInterface == 0) {
Eric Laurentd0ebb532013-04-02 16:41:41 -0700845 status = NO_INIT;
846 goto exit;
Eric Laurentca7cc822012-11-19 14:55:58 -0800847 }
848
Eric Laurentca7cc822012-11-19 14:55:58 -0800849 // TODO: handle configuration of effects replacing track process
Andy Hung9aad48c2017-11-29 10:29:19 -0800850 // TODO: handle configuration of input (record) SW effects above the HAL,
851 // similar to output EFFECT_FLAG_TYPE_INSERT/REPLACE,
852 // in which case input channel masks should be used here.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800853 channelMask = mCallback->channelMask();
Andy Hung9aad48c2017-11-29 10:29:19 -0800854 mConfig.inputCfg.channels = channelMask;
Ricardo Garciad11da702015-05-28 12:14:12 -0700855 mConfig.outputCfg.channels = channelMask;
Eric Laurentca7cc822012-11-19 14:55:58 -0800856
857 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800858 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_MONO) {
859 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
860 ALOGV("Overriding auxiliary effect input channels %#x as MONO",
861 mConfig.inputCfg.channels);
862 }
863#ifndef MULTICHANNEL_EFFECT_CHAIN
864 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
865 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
866 ALOGV("Overriding auxiliary effect output channels %#x as STEREO",
867 mConfig.outputCfg.channels);
868 }
869#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800870 } else {
Andy Hung9aad48c2017-11-29 10:29:19 -0800871#ifndef MULTICHANNEL_EFFECT_CHAIN
Ricardo Garciad11da702015-05-28 12:14:12 -0700872 // TODO: Update this logic when multichannel effects are implemented.
873 // For offloaded tracks consider mono output as stereo for proper effect initialization
874 if (channelMask == AUDIO_CHANNEL_OUT_MONO) {
875 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
876 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
877 ALOGV("Overriding effect input and output as STEREO");
878 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800879#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800880 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800881 mInChannelCountRequested =
882 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
883 mOutChannelCountRequested =
884 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
Ricardo Garciad11da702015-05-28 12:14:12 -0700885
rago94a1ee82017-07-21 15:11:02 -0700886 mConfig.inputCfg.format = EFFECT_BUFFER_FORMAT;
887 mConfig.outputCfg.format = EFFECT_BUFFER_FORMAT;
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900888
889 // Don't use sample rate for thread if effect isn't offloadable.
Eric Laurent6b446ce2019-12-13 10:56:31 -0800890 if (mCallback->isOffload() && !isOffloaded()) {
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900891 mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
892 ALOGV("Overriding effect input as 48kHz");
893 } else {
Eric Laurent6b446ce2019-12-13 10:56:31 -0800894 mConfig.inputCfg.samplingRate = mCallback->sampleRate();
Yuuki Yokoyamae17f8312017-05-26 19:06:33 +0900895 }
Eric Laurentca7cc822012-11-19 14:55:58 -0800896 mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
897 mConfig.inputCfg.bufferProvider.cookie = NULL;
898 mConfig.inputCfg.bufferProvider.getBuffer = NULL;
899 mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
900 mConfig.outputCfg.bufferProvider.cookie = NULL;
901 mConfig.outputCfg.bufferProvider.getBuffer = NULL;
902 mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
903 mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
904 // Insert effect:
Eric Laurent3f75a5b2019-11-12 15:55:51 -0800905 // - in global sessions (e.g AUDIO_SESSION_OUTPUT_MIX),
Eric Laurentca7cc822012-11-19 14:55:58 -0800906 // always overwrites output buffer: input buffer == output buffer
907 // - in other sessions:
908 // last effect in the chain accumulates in output buffer: input buffer != output buffer
909 // other effect: overwrites output buffer: input buffer == output buffer
910 // Auxiliary effect:
911 // accumulates in output buffer: input buffer != output buffer
912 // Therefore: accumulate <=> input buffer != output buffer
913 if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
914 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
915 } else {
916 mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
917 }
918 mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
919 mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
Eric Laurent6b446ce2019-12-13 10:56:31 -0800920 mConfig.inputCfg.buffer.frameCount = mCallback->frameCount();
Eric Laurentca7cc822012-11-19 14:55:58 -0800921 mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
922
Eric Laurent6b446ce2019-12-13 10:56:31 -0800923 ALOGV("configure() %p chain %p buffer %p framecount %zu",
924 this, mCallback->chain().promote() != nullptr ? mCallback->chain().promote().get() :
925 nullptr,
926 mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
Eric Laurentca7cc822012-11-19 14:55:58 -0800927
928 status_t cmdStatus;
Eric Laurentd0ebb532013-04-02 16:41:41 -0700929 size = sizeof(int);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700930 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800931 sizeof(mConfig),
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -0700932 &mConfig,
933 &size,
934 &cmdStatus);
rago94a1ee82017-07-21 15:11:02 -0700935 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -0800936 status = cmdStatus;
937 }
Andy Hung9aad48c2017-11-29 10:29:19 -0800938
939#ifdef MULTICHANNEL_EFFECT_CHAIN
940 if (status != NO_ERROR &&
Eric Laurent6b446ce2019-12-13 10:56:31 -0800941 mCallback->isOutput() &&
Andy Hung9aad48c2017-11-29 10:29:19 -0800942 (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
943 || mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO)) {
944 // Older effects may require exact STEREO position mask.
Andy Hung01b32722018-05-18 13:52:02 -0700945 if (mConfig.inputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
946 && (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
Andy Hung9aad48c2017-11-29 10:29:19 -0800947 ALOGV("Overriding effect input channels %#x as STEREO", mConfig.inputCfg.channels);
948 mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
949 }
950 if (mConfig.outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO) {
951 ALOGV("Overriding effect output channels %#x as STEREO", mConfig.outputCfg.channels);
952 mConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
953 }
954 size = sizeof(int);
rago94a1ee82017-07-21 15:11:02 -0700955 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
Andy Hung9aad48c2017-11-29 10:29:19 -0800956 sizeof(mConfig),
rago94a1ee82017-07-21 15:11:02 -0700957 &mConfig,
958 &size,
959 &cmdStatus);
960 if (status == NO_ERROR) {
961 status = cmdStatus;
Andy Hung9aad48c2017-11-29 10:29:19 -0800962 }
963 }
964#endif
965
966#ifdef FLOAT_EFFECT_CHAIN
967 if (status == NO_ERROR) {
968 mSupportsFloat = true;
969 }
970
971 if (status != NO_ERROR) {
972 ALOGV("EFFECT_CMD_SET_CONFIG failed with float format, retry with int16_t.");
973 mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
974 mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
975 size = sizeof(int);
976 status = mEffectInterface->command(EFFECT_CMD_SET_CONFIG,
977 sizeof(mConfig),
978 &mConfig,
979 &size,
980 &cmdStatus);
981 if (status == NO_ERROR) {
982 status = cmdStatus;
983 }
984 if (status == NO_ERROR) {
rago94a1ee82017-07-21 15:11:02 -0700985 mSupportsFloat = false;
986 ALOGVV("config worked with 16 bit");
987 } else {
988 ALOGE("%s failed %d with int16_t (as well as float)", __func__, status);
Eric Laurentca7cc822012-11-19 14:55:58 -0800989 }
rago94a1ee82017-07-21 15:11:02 -0700990 }
991#endif
Eric Laurentca7cc822012-11-19 14:55:58 -0800992
rago94a1ee82017-07-21 15:11:02 -0700993 if (status == NO_ERROR) {
994 // Establish Buffer strategy
995 setInBuffer(mInBuffer);
996 setOutBuffer(mOutBuffer);
997
998 // Update visualizer latency
999 if (memcmp(&mDescriptor.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) {
1000 uint32_t buf32[sizeof(effect_param_t) / sizeof(uint32_t) + 2];
1001 effect_param_t *p = (effect_param_t *)buf32;
1002
1003 p->psize = sizeof(uint32_t);
1004 p->vsize = sizeof(uint32_t);
1005 size = sizeof(int);
1006 *(int32_t *)p->data = VISUALIZER_PARAM_LATENCY;
1007
Eric Laurent6b446ce2019-12-13 10:56:31 -08001008 uint32_t latency = mCallback->latency();
rago94a1ee82017-07-21 15:11:02 -07001009
1010 *((int32_t *)p->data + 1)= latency;
1011 mEffectInterface->command(EFFECT_CMD_SET_PARAM,
1012 sizeof(effect_param_t) + 8,
1013 &buf32,
1014 &size,
1015 &cmdStatus);
1016 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001017 }
1018
Andy Hung05083ac2017-12-14 15:00:28 -08001019 // mConfig.outputCfg.buffer.frameCount cannot be zero.
1020 mMaxDisableWaitCnt = (uint32_t)std::max(
1021 (uint64_t)1, // mMaxDisableWaitCnt must be greater than zero.
1022 (uint64_t)MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate
1023 / ((uint64_t)1000 * mConfig.outputCfg.buffer.frameCount));
Eric Laurentca7cc822012-11-19 14:55:58 -08001024
Eric Laurentd0ebb532013-04-02 16:41:41 -07001025exit:
Andy Hung6f88dc42017-12-13 16:19:39 -08001026 // TODO: consider clearing mConfig on error.
Eric Laurentd0ebb532013-04-02 16:41:41 -07001027 mStatus = status;
rago94a1ee82017-07-21 15:11:02 -07001028 ALOGVV("configure ended");
Eric Laurentca7cc822012-11-19 14:55:58 -08001029 return status;
1030}
1031
1032status_t AudioFlinger::EffectModule::init()
1033{
1034 Mutex::Autolock _l(mLock);
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001035 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001036 return NO_INIT;
1037 }
1038 status_t cmdStatus;
1039 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001040 status_t status = mEffectInterface->command(EFFECT_CMD_INIT,
1041 0,
1042 NULL,
1043 &size,
1044 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001045 if (status == 0) {
1046 status = cmdStatus;
1047 }
1048 return status;
1049}
1050
Eric Laurent1b928682014-10-02 19:41:47 -07001051void AudioFlinger::EffectModule::addEffectToHal_l()
1052{
1053 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1054 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001055 (void)mCallback->addEffectToHal(mEffectInterface);
Eric Laurent1b928682014-10-02 19:41:47 -07001056 }
1057}
1058
Eric Laurentfa1e1232016-08-02 19:01:49 -07001059// start() must be called with PlaybackThread::mLock or EffectChain::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08001060status_t AudioFlinger::EffectModule::start()
1061{
Eric Laurentfa1e1232016-08-02 19:01:49 -07001062 status_t status;
1063 {
1064 Mutex::Autolock _l(mLock);
1065 status = start_l();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001066 }
Eric Laurent6b446ce2019-12-13 10:56:31 -08001067 if (status == NO_ERROR) {
1068 mCallback->resetVolume();
Eric Laurentfa1e1232016-08-02 19:01:49 -07001069 }
1070 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001071}
1072
1073status_t AudioFlinger::EffectModule::start_l()
1074{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001075 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001076 return NO_INIT;
1077 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001078 if (mStatus != NO_ERROR) {
1079 return mStatus;
1080 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001081 status_t cmdStatus;
1082 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001083 status_t status = mEffectInterface->command(EFFECT_CMD_ENABLE,
1084 0,
1085 NULL,
1086 &size,
1087 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001088 if (status == 0) {
1089 status = cmdStatus;
1090 }
Eric Laurentcb4b6e92014-10-01 14:26:10 -07001091 if (status == 0) {
Eric Laurent1b928682014-10-02 19:41:47 -07001092 addEffectToHal_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08001093 }
1094 return status;
1095}
1096
1097status_t AudioFlinger::EffectModule::stop()
1098{
1099 Mutex::Autolock _l(mLock);
1100 return stop_l();
1101}
1102
1103status_t AudioFlinger::EffectModule::stop_l()
1104{
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001105 if (mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001106 return NO_INIT;
1107 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001108 if (mStatus != NO_ERROR) {
1109 return mStatus;
1110 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001111 status_t cmdStatus = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001112 uint32_t size = sizeof(status_t);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001113
1114 if (isVolumeControl() && isOffloadedOrDirect()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001115 // We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
1116 // resetVolume_l --> setVolume_l --> EffectModule::setVolume
1117 mSetVolumeReentrantTid = gettid();
Eric Laurent6b446ce2019-12-13 10:56:31 -08001118 mCallback->resetVolume();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001119 mSetVolumeReentrantTid = INVALID_PID;
1120 }
1121
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001122 status_t status = mEffectInterface->command(EFFECT_CMD_DISABLE,
1123 0,
1124 NULL,
1125 &size,
1126 &cmdStatus);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001127 if (status == NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001128 status = cmdStatus;
1129 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001130 if (status == NO_ERROR) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001131 status = removeEffectFromHal_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001132 }
1133 return status;
1134}
1135
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001136// must be called with EffectChain::mLock held
1137void AudioFlinger::EffectModule::release_l()
1138{
1139 if (mEffectInterface != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001140 removeEffectFromHal_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001141 // release effect engine
Mikhail Naganov022b9952017-01-04 16:36:51 -08001142 mEffectInterface->close();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001143 mEffectInterface.clear();
1144 }
1145}
1146
Eric Laurent6b446ce2019-12-13 10:56:31 -08001147status_t AudioFlinger::EffectModule::removeEffectFromHal_l()
Eric Laurentbfb1b832013-01-07 09:53:42 -08001148{
1149 if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
1150 (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001151 mCallback->removeEffectFromHal(mEffectInterface);
Eric Laurentca7cc822012-11-19 14:55:58 -08001152 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001153 return NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001154}
1155
Andy Hunge4a1d912016-08-17 14:11:13 -07001156// round up delta valid if value and divisor are positive.
1157template <typename T>
1158static T roundUpDelta(const T &value, const T &divisor) {
1159 T remainder = value % divisor;
1160 return remainder == 0 ? 0 : divisor - remainder;
1161}
1162
Eric Laurentca7cc822012-11-19 14:55:58 -08001163status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
1164 uint32_t cmdSize,
1165 void *pCmdData,
1166 uint32_t *replySize,
1167 void *pReplyData)
1168{
1169 Mutex::Autolock _l(mLock);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001170 ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001171
Mikhail Naganov1dc98672016-08-18 17:50:29 -07001172 if (mState == DESTROYED || mEffectInterface == 0) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001173 return NO_INIT;
1174 }
Eric Laurentd0ebb532013-04-02 16:41:41 -07001175 if (mStatus != NO_ERROR) {
1176 return mStatus;
1177 }
Andy Hung110bc952016-06-20 15:22:52 -07001178 if (cmdCode == EFFECT_CMD_GET_PARAM &&
Andy Hung6660f122016-11-04 19:40:53 -07001179 (sizeof(effect_param_t) > cmdSize ||
1180 ((effect_param_t *)pCmdData)->psize > cmdSize
1181 - sizeof(effect_param_t))) {
1182 android_errorWriteLog(0x534e4554, "32438594");
Andy Hungb3456642016-11-28 13:50:21 -08001183 android_errorWriteLog(0x534e4554, "33003822");
1184 return -EINVAL;
1185 }
1186 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1187 (*replySize < sizeof(effect_param_t) ||
1188 ((effect_param_t *)pCmdData)->psize > *replySize - sizeof(effect_param_t))) {
1189 android_errorWriteLog(0x534e4554, "29251553");
Andy Hung6660f122016-11-04 19:40:53 -07001190 return -EINVAL;
1191 }
ragoe2759072016-11-22 18:02:48 -08001192 if (cmdCode == EFFECT_CMD_GET_PARAM &&
1193 (sizeof(effect_param_t) > *replySize
1194 || ((effect_param_t *)pCmdData)->psize > *replySize
1195 - sizeof(effect_param_t)
1196 || ((effect_param_t *)pCmdData)->vsize > *replySize
1197 - sizeof(effect_param_t)
1198 - ((effect_param_t *)pCmdData)->psize
1199 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1200 *replySize
1201 - sizeof(effect_param_t)
1202 - ((effect_param_t *)pCmdData)->psize
1203 - ((effect_param_t *)pCmdData)->vsize)) {
1204 ALOGV("\tLVM_ERROR : EFFECT_CMD_GET_PARAM: reply size inconsistent");
1205 android_errorWriteLog(0x534e4554, "32705438");
1206 return -EINVAL;
1207 }
Andy Hunge4a1d912016-08-17 14:11:13 -07001208 if ((cmdCode == EFFECT_CMD_SET_PARAM
1209 || cmdCode == EFFECT_CMD_SET_PARAM_DEFERRED) && // DEFERRED not generally used
1210 (sizeof(effect_param_t) > cmdSize
1211 || ((effect_param_t *)pCmdData)->psize > cmdSize
1212 - sizeof(effect_param_t)
1213 || ((effect_param_t *)pCmdData)->vsize > cmdSize
1214 - sizeof(effect_param_t)
1215 - ((effect_param_t *)pCmdData)->psize
1216 || roundUpDelta(((effect_param_t *)pCmdData)->psize, (uint32_t)sizeof(int)) >
1217 cmdSize
1218 - sizeof(effect_param_t)
1219 - ((effect_param_t *)pCmdData)->psize
1220 - ((effect_param_t *)pCmdData)->vsize)) {
1221 android_errorWriteLog(0x534e4554, "30204301");
1222 return -EINVAL;
1223 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001224 status_t status = mEffectInterface->command(cmdCode,
1225 cmdSize,
1226 pCmdData,
1227 replySize,
1228 pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001229 if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
1230 uint32_t size = (replySize == NULL) ? 0 : *replySize;
1231 for (size_t i = 1; i < mHandles.size(); i++) {
1232 EffectHandle *h = mHandles[i];
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001233 if (h != NULL && !h->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001234 h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
1235 }
1236 }
1237 }
1238 return status;
1239}
1240
Eric Laurentca7cc822012-11-19 14:55:58 -08001241bool AudioFlinger::EffectModule::isProcessEnabled() const
1242{
Eric Laurentd0ebb532013-04-02 16:41:41 -07001243 if (mStatus != NO_ERROR) {
1244 return false;
1245 }
1246
Eric Laurentca7cc822012-11-19 14:55:58 -08001247 switch (mState) {
1248 case RESTART:
1249 case ACTIVE:
1250 case STOPPING:
1251 case STOPPED:
1252 return true;
1253 case IDLE:
1254 case STARTING:
1255 case DESTROYED:
1256 default:
1257 return false;
1258 }
1259}
1260
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001261bool AudioFlinger::EffectModule::isOffloadedOrDirect() const
1262{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001263 return mCallback->isOffloadOrDirect();
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001264}
1265
1266bool AudioFlinger::EffectModule::isVolumeControlEnabled() const
1267{
1268 return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
1269}
1270
Mikhail Naganov022b9952017-01-04 16:36:51 -08001271void AudioFlinger::EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001272 ALOGVV("setInBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001273
1274 // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001275 if (buffer != 0) {
1276 mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
1277 buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
1278 } else {
1279 mConfig.inputCfg.buffer.raw = NULL;
1280 }
1281 mInBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001282 mEffectInterface->setInBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001283
1284#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001285 // aux effects do in place conversion to float - we don't allocate mInConversionBuffer.
rago94a1ee82017-07-21 15:11:02 -07001286 // Theoretically insert effects can also do in-place conversions (destroying
1287 // the original buffer) when the output buffer is identical to the input buffer,
1288 // but we don't optimize for it here.
1289 const bool auxType = (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY;
Andy Hung9aad48c2017-11-29 10:29:19 -08001290 const uint32_t inChannelCount =
1291 audio_channel_count_from_out_mask(mConfig.inputCfg.channels);
1292 const bool formatMismatch = !mSupportsFloat || mInChannelCountRequested != inChannelCount;
1293 if (!auxType && formatMismatch && mInBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001294 // we need to translate - create hidl shared buffer and intercept
1295 const size_t inFrameCount = mConfig.inputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001296 // Use FCC_2 in case mInChannelCountRequested is mono and the effect is stereo.
1297 const uint32_t inChannels = std::max((uint32_t)FCC_2, mInChannelCountRequested);
1298 const size_t size = inChannels * inFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001299
1300 ALOGV("%s: setInBuffer updating for inChannels:%d inFrameCount:%zu total size:%zu",
1301 __func__, inChannels, inFrameCount, size);
1302
Andy Hungbded9c82017-11-30 18:47:35 -08001303 if (size > 0 && (mInConversionBuffer.get() == nullptr
1304 || size > mInConversionBuffer->getSize())) {
1305 mInConversionBuffer.clear();
1306 ALOGV("%s: allocating mInConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001307 (void)mCallback->allocateHalBuffer(size, &mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001308 }
Andy Hungbded9c82017-11-30 18:47:35 -08001309 if (mInConversionBuffer.get() != nullptr) {
Andy Hungbded9c82017-11-30 18:47:35 -08001310 mInConversionBuffer->setFrameCount(inFrameCount);
1311 mEffectInterface->setInBuffer(mInConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001312 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001313 ALOGE("%s cannot create mInConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001314 }
1315 }
1316#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001317}
1318
1319void AudioFlinger::EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
rago94a1ee82017-07-21 15:11:02 -07001320 ALOGVV("setOutBuffer %p",(&buffer));
Andy Hung6f88dc42017-12-13 16:19:39 -08001321
1322 // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
Mikhail Naganov022b9952017-01-04 16:36:51 -08001323 if (buffer != 0) {
1324 mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
1325 buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
1326 } else {
1327 mConfig.outputCfg.buffer.raw = NULL;
1328 }
1329 mOutBuffer = buffer;
Andy Hungc15aaee2017-11-27 17:02:40 -08001330 mEffectInterface->setOutBuffer(buffer);
rago94a1ee82017-07-21 15:11:02 -07001331
1332#ifdef FLOAT_EFFECT_CHAIN
Andy Hungbded9c82017-11-30 18:47:35 -08001333 // Note: Any effect that does not accumulate does not need mOutConversionBuffer and
rago94a1ee82017-07-21 15:11:02 -07001334 // can do in-place conversion from int16_t to float. We don't optimize here.
Andy Hung9aad48c2017-11-29 10:29:19 -08001335 const uint32_t outChannelCount =
1336 audio_channel_count_from_out_mask(mConfig.outputCfg.channels);
1337 const bool formatMismatch = !mSupportsFloat || mOutChannelCountRequested != outChannelCount;
1338 if (formatMismatch && mOutBuffer.get() != nullptr) {
rago94a1ee82017-07-21 15:11:02 -07001339 const size_t outFrameCount = mConfig.outputCfg.buffer.frameCount;
Andy Hung9aad48c2017-11-29 10:29:19 -08001340 // Use FCC_2 in case mOutChannelCountRequested is mono and the effect is stereo.
1341 const uint32_t outChannels = std::max((uint32_t)FCC_2, mOutChannelCountRequested);
1342 const size_t size = outChannels * outFrameCount * std::max(sizeof(int16_t), sizeof(float));
rago94a1ee82017-07-21 15:11:02 -07001343
1344 ALOGV("%s: setOutBuffer updating for outChannels:%d outFrameCount:%zu total size:%zu",
1345 __func__, outChannels, outFrameCount, size);
1346
Andy Hungbded9c82017-11-30 18:47:35 -08001347 if (size > 0 && (mOutConversionBuffer.get() == nullptr
1348 || size > mOutConversionBuffer->getSize())) {
1349 mOutConversionBuffer.clear();
1350 ALOGV("%s: allocating mOutConversionBuffer %zu", __func__, size);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001351 (void)mCallback->allocateHalBuffer(size, &mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001352 }
Andy Hungbded9c82017-11-30 18:47:35 -08001353 if (mOutConversionBuffer.get() != nullptr) {
1354 mOutConversionBuffer->setFrameCount(outFrameCount);
1355 mEffectInterface->setOutBuffer(mOutConversionBuffer);
rago94a1ee82017-07-21 15:11:02 -07001356 } else if (size > 0) {
Andy Hungbded9c82017-11-30 18:47:35 -08001357 ALOGE("%s cannot create mOutConversionBuffer", __func__);
rago94a1ee82017-07-21 15:11:02 -07001358 }
1359 }
1360#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08001361}
1362
Eric Laurentca7cc822012-11-19 14:55:58 -08001363status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
1364{
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001365 AutoLockReentrant _l(mLock, mSetVolumeReentrantTid);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001366 if (mStatus != NO_ERROR) {
1367 return mStatus;
1368 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001369 status_t status = NO_ERROR;
Eric Laurentca7cc822012-11-19 14:55:58 -08001370 // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
1371 // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
1372 if (isProcessEnabled() &&
1373 ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
Jasmine Cha934ecfb2019-01-23 18:19:14 +08001374 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND ||
1375 (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_MONITOR)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001376 uint32_t volume[2];
1377 uint32_t *pVolume = NULL;
1378 uint32_t size = sizeof(volume);
1379 volume[0] = *left;
1380 volume[1] = *right;
1381 if (controller) {
1382 pVolume = volume;
1383 }
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001384 status = mEffectInterface->command(EFFECT_CMD_SET_VOLUME,
1385 size,
1386 volume,
1387 &size,
1388 pVolume);
Eric Laurentca7cc822012-11-19 14:55:58 -08001389 if (controller && status == NO_ERROR && size == sizeof(volume)) {
1390 *left = volume[0];
1391 *right = volume[1];
1392 }
1393 }
1394 return status;
1395}
1396
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001397void AudioFlinger::EffectChain::setVolumeForOutput_l(uint32_t left, uint32_t right)
1398{
Eric Laurent6b446ce2019-12-13 10:56:31 -08001399 if (mEffectCallback->isOffloadOrDirect() && !isNonOffloadableEnabled_l()) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001400 float vol_l = (float)left / (1 << 24);
1401 float vol_r = (float)right / (1 << 24);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001402 mEffectCallback->setVolumeForOutput(vol_l, vol_r);
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09001403 }
1404}
1405
jiabin8f278ee2019-11-11 12:16:27 -08001406status_t AudioFlinger::EffectModule::sendSetAudioDevicesCommand(
1407 const AudioDeviceTypeAddrVector &devices, uint32_t cmdCode)
Eric Laurentca7cc822012-11-19 14:55:58 -08001408{
jiabin8f278ee2019-11-11 12:16:27 -08001409 audio_devices_t deviceType = deviceTypesToBitMask(getAudioDeviceTypes(devices));
1410 if (deviceType == AUDIO_DEVICE_NONE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001411 return NO_ERROR;
1412 }
1413
1414 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001415 if (mStatus != NO_ERROR) {
1416 return mStatus;
1417 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001418 status_t status = NO_ERROR;
Eric Laurent7e1139c2013-06-06 18:29:01 -07001419 if ((mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001420 status_t cmdStatus;
1421 uint32_t size = sizeof(status_t);
jiabin8f278ee2019-11-11 12:16:27 -08001422 // FIXME: use audio device types and addresses when the hal interface is ready.
1423 status = mEffectInterface->command(cmdCode,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001424 sizeof(uint32_t),
jiabin8f278ee2019-11-11 12:16:27 -08001425 &deviceType,
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001426 &size,
1427 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001428 }
1429 return status;
1430}
1431
jiabin8f278ee2019-11-11 12:16:27 -08001432status_t AudioFlinger::EffectModule::setDevices(const AudioDeviceTypeAddrVector &devices)
1433{
1434 return sendSetAudioDevicesCommand(devices, EFFECT_CMD_SET_DEVICE);
1435}
1436
1437status_t AudioFlinger::EffectModule::setInputDevice(const AudioDeviceTypeAddr &device)
1438{
1439 return sendSetAudioDevicesCommand({device}, EFFECT_CMD_SET_INPUT_DEVICE);
1440}
1441
Eric Laurentca7cc822012-11-19 14:55:58 -08001442status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
1443{
1444 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001445 if (mStatus != NO_ERROR) {
1446 return mStatus;
1447 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001448 status_t status = NO_ERROR;
1449 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
1450 status_t cmdStatus;
1451 uint32_t size = sizeof(status_t);
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001452 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_MODE,
1453 sizeof(audio_mode_t),
1454 &mode,
1455 &size,
1456 &cmdStatus);
Eric Laurentca7cc822012-11-19 14:55:58 -08001457 if (status == NO_ERROR) {
1458 status = cmdStatus;
1459 }
1460 }
1461 return status;
1462}
1463
1464status_t AudioFlinger::EffectModule::setAudioSource(audio_source_t source)
1465{
1466 Mutex::Autolock _l(mLock);
Eric Laurentd0ebb532013-04-02 16:41:41 -07001467 if (mStatus != NO_ERROR) {
1468 return mStatus;
1469 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001470 status_t status = NO_ERROR;
1471 if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_SOURCE_MASK) == EFFECT_FLAG_AUDIO_SOURCE_IND) {
1472 uint32_t size = 0;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001473 status = mEffectInterface->command(EFFECT_CMD_SET_AUDIO_SOURCE,
1474 sizeof(audio_source_t),
1475 &source,
1476 &size,
1477 NULL);
Eric Laurentca7cc822012-11-19 14:55:58 -08001478 }
1479 return status;
1480}
1481
Eric Laurent5baf2af2013-09-12 17:37:00 -07001482status_t AudioFlinger::EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
1483{
1484 Mutex::Autolock _l(mLock);
1485 if (mStatus != NO_ERROR) {
1486 return mStatus;
1487 }
1488 status_t status = NO_ERROR;
1489 if ((mDescriptor.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) != 0) {
1490 status_t cmdStatus;
1491 uint32_t size = sizeof(status_t);
1492 effect_offload_param_t cmd;
1493
1494 cmd.isOffload = offloaded;
1495 cmd.ioHandle = io;
Mikhail Naganov4a3d5c22016-08-15 13:47:42 -07001496 status = mEffectInterface->command(EFFECT_CMD_OFFLOAD,
1497 sizeof(effect_offload_param_t),
1498 &cmd,
1499 &size,
1500 &cmdStatus);
Eric Laurent5baf2af2013-09-12 17:37:00 -07001501 if (status == NO_ERROR) {
1502 status = cmdStatus;
1503 }
1504 mOffloaded = (status == NO_ERROR) ? offloaded : false;
1505 } else {
1506 if (offloaded) {
1507 status = INVALID_OPERATION;
1508 }
1509 mOffloaded = false;
1510 }
1511 ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
1512 return status;
1513}
1514
1515bool AudioFlinger::EffectModule::isOffloaded() const
1516{
1517 Mutex::Autolock _l(mLock);
1518 return mOffloaded;
1519}
1520
Andy Hungbded9c82017-11-30 18:47:35 -08001521static std::string dumpInOutBuffer(bool isInput, const sp<EffectBufferHalInterface> &buffer) {
1522 std::stringstream ss;
1523
1524 if (buffer.get() == nullptr) {
1525 return "nullptr"; // make different than below
1526 } else if (buffer->externalData() != nullptr) {
1527 ss << (isInput ? buffer->externalData() : buffer->audioBuffer()->raw)
1528 << " -> "
1529 << (isInput ? buffer->audioBuffer()->raw : buffer->externalData());
1530 } else {
1531 ss << buffer->audioBuffer()->raw;
1532 }
1533 return ss.str();
1534}
Marco Nelissenb2208842014-02-07 14:00:50 -08001535
Eric Laurent41709552019-12-16 19:34:05 -08001536void AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
Eric Laurentca7cc822012-11-19 14:55:58 -08001537{
Eric Laurent41709552019-12-16 19:34:05 -08001538 EffectBase::dump(fd, args);
1539
Eric Laurentca7cc822012-11-19 14:55:58 -08001540 String8 result;
Eric Laurentca7cc822012-11-19 14:55:58 -08001541 bool locked = AudioFlinger::dumpTryLock(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001542
Eric Laurent41709552019-12-16 19:34:05 -08001543 result.append("\t\tStatus Engine:\n");
1544 result.appendFormat("\t\t%03d %p\n",
1545 mStatus, mEffectInterface.get());
Andy Hung9718d662017-12-22 17:57:39 -08001546
1547 result.appendFormat("\t\t- data: %s\n", mSupportsFloat ? "float" : "int16");
Eric Laurentca7cc822012-11-19 14:55:58 -08001548
1549 result.append("\t\t- Input configuration:\n");
Andy Hung9718d662017-12-22 17:57:39 -08001550 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
1551 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
1552 mConfig.inputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001553 mConfig.inputCfg.buffer.frameCount,
1554 mConfig.inputCfg.samplingRate,
1555 mConfig.inputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001556 mConfig.inputCfg.format,
Andy Hung9718d662017-12-22 17:57:39 -08001557 formatToString((audio_format_t)mConfig.inputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001558
1559 result.append("\t\t- Output configuration:\n");
1560 result.append("\t\t\tBuffer Frames Smp rate Channels Format\n");
Andy Hung9718d662017-12-22 17:57:39 -08001561 result.appendFormat("\t\t\t%p %05zu %05d %08x %6d (%s)\n",
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001562 mConfig.outputCfg.buffer.raw,
Eric Laurentca7cc822012-11-19 14:55:58 -08001563 mConfig.outputCfg.buffer.frameCount,
1564 mConfig.outputCfg.samplingRate,
1565 mConfig.outputCfg.channels,
Marco Nelissenb2208842014-02-07 14:00:50 -08001566 mConfig.outputCfg.format,
Mikhail Naganov913d06c2016-11-01 12:49:22 -07001567 formatToString((audio_format_t)mConfig.outputCfg.format).c_str());
Eric Laurentca7cc822012-11-19 14:55:58 -08001568
rago94a1ee82017-07-21 15:11:02 -07001569#ifdef FLOAT_EFFECT_CHAIN
rago94a1ee82017-07-21 15:11:02 -07001570
Andy Hungbded9c82017-11-30 18:47:35 -08001571 result.appendFormat("\t\t- HAL buffers:\n"
1572 "\t\t\tIn(%s) InConversion(%s) Out(%s) OutConversion(%s)\n",
1573 dumpInOutBuffer(true /* isInput */, mInBuffer).c_str(),
1574 dumpInOutBuffer(true /* isInput */, mInConversionBuffer).c_str(),
1575 dumpInOutBuffer(false /* isInput */, mOutBuffer).c_str(),
1576 dumpInOutBuffer(false /* isInput */, mOutConversionBuffer).c_str());
rago94a1ee82017-07-21 15:11:02 -07001577#endif
1578
Eric Laurentca7cc822012-11-19 14:55:58 -08001579 write(fd, result.string(), result.length());
1580
Mikhail Naganov4d547672019-02-22 14:19:19 -08001581 if (mEffectInterface != 0) {
1582 dprintf(fd, "\tEffect ID %d HAL dump:\n", mId);
1583 (void)mEffectInterface->dump(fd);
1584 }
1585
Eric Laurentca7cc822012-11-19 14:55:58 -08001586 if (locked) {
1587 mLock.unlock();
1588 }
1589}
1590
1591// ----------------------------------------------------------------------------
1592// EffectHandle implementation
1593// ----------------------------------------------------------------------------
1594
1595#undef LOG_TAG
1596#define LOG_TAG "AudioFlinger::EffectHandle"
1597
Eric Laurent41709552019-12-16 19:34:05 -08001598AudioFlinger::EffectHandle::EffectHandle(const sp<EffectBase>& effect,
Eric Laurentca7cc822012-11-19 14:55:58 -08001599 const sp<AudioFlinger::Client>& client,
1600 const sp<IEffectClient>& effectClient,
1601 int32_t priority)
1602 : BnEffect(),
1603 mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001604 mPriority(priority), mHasControl(false), mEnabled(false), mDisconnected(false)
Eric Laurentca7cc822012-11-19 14:55:58 -08001605{
Eric Laurentb82e6b72019-11-22 17:25:04 -08001606 ALOGV("constructor %p client %p", this, client.get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001607
1608 if (client == 0) {
1609 return;
1610 }
1611 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
1612 mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
Glenn Kastene75da402013-11-20 13:54:52 -08001613 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001614 (mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001615 ALOGE("not enough memory for Effect size=%zu", EFFECT_PARAM_BUFFER_SIZE +
Eric Laurentca7cc822012-11-19 14:55:58 -08001616 sizeof(effect_param_cblk_t));
Glenn Kastene75da402013-11-20 13:54:52 -08001617 mCblkMemory.clear();
Eric Laurentca7cc822012-11-19 14:55:58 -08001618 return;
1619 }
Glenn Kastene75da402013-11-20 13:54:52 -08001620 new(mCblk) effect_param_cblk_t();
1621 mBuffer = (uint8_t *)mCblk + bufOffset;
Eric Laurentca7cc822012-11-19 14:55:58 -08001622}
1623
1624AudioFlinger::EffectHandle::~EffectHandle()
1625{
1626 ALOGV("Destructor %p", this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001627 disconnect(false);
1628}
1629
Glenn Kastene75da402013-11-20 13:54:52 -08001630status_t AudioFlinger::EffectHandle::initCheck()
1631{
1632 return mClient == 0 || mCblkMemory != 0 ? OK : NO_MEMORY;
1633}
1634
Eric Laurentca7cc822012-11-19 14:55:58 -08001635status_t AudioFlinger::EffectHandle::enable()
1636{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001637 AutoMutex _l(mLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001638 ALOGV("enable %p", this);
Eric Laurent41709552019-12-16 19:34:05 -08001639 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001640 if (effect == 0 || mDisconnected) {
1641 return DEAD_OBJECT;
1642 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001643 if (!mHasControl) {
1644 return INVALID_OPERATION;
1645 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001646
1647 if (mEnabled) {
1648 return NO_ERROR;
1649 }
1650
1651 mEnabled = true;
1652
Eric Laurent6c796322019-04-09 14:13:17 -07001653 status_t status = effect->updatePolicyState();
1654 if (status != NO_ERROR) {
1655 mEnabled = false;
1656 return status;
1657 }
1658
Eric Laurent6b446ce2019-12-13 10:56:31 -08001659 effect->checkSuspendOnEffectEnabled(true, false /*threadLocked*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001660
1661 // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001662 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001663 return NO_ERROR;
1664 }
1665
Eric Laurent6b446ce2019-12-13 10:56:31 -08001666 status = effect->setEnabled(true, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001667 if (status != NO_ERROR) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001668 mEnabled = false;
1669 }
1670 return status;
1671}
1672
1673status_t AudioFlinger::EffectHandle::disable()
1674{
1675 ALOGV("disable %p", this);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001676 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001677 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001678 if (effect == 0 || mDisconnected) {
1679 return DEAD_OBJECT;
1680 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001681 if (!mHasControl) {
1682 return INVALID_OPERATION;
1683 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001684
1685 if (!mEnabled) {
1686 return NO_ERROR;
1687 }
1688 mEnabled = false;
1689
Eric Laurent6c796322019-04-09 14:13:17 -07001690 effect->updatePolicyState();
1691
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001692 if (effect->suspended()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001693 return NO_ERROR;
1694 }
1695
Eric Laurent6b446ce2019-12-13 10:56:31 -08001696 status_t status = effect->setEnabled(false, true /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08001697 return status;
1698}
1699
1700void AudioFlinger::EffectHandle::disconnect()
1701{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001702 ALOGV("%s %p", __FUNCTION__, this);
Eric Laurentca7cc822012-11-19 14:55:58 -08001703 disconnect(true);
1704}
1705
1706void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
1707{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001708 AutoMutex _l(mLock);
1709 ALOGV("disconnect(%s) %p", unpinIfLast ? "true" : "false", this);
1710 if (mDisconnected) {
1711 if (unpinIfLast) {
1712 android_errorWriteLog(0x534e4554, "32707507");
1713 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001714 return;
1715 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001716 mDisconnected = true;
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001717 {
Eric Laurent41709552019-12-16 19:34:05 -08001718 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001719 if (effect != 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08001720 if (effect->disconnectHandle(this, unpinIfLast) > 0) {
Eric Laurent6c796322019-04-09 14:13:17 -07001721 ALOGW("%s Effect handle %p disconnected after thread destruction",
1722 __func__, this);
1723 }
1724 effect->updatePolicyState();
Eric Laurentf10c7092016-12-06 17:09:56 -08001725 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001726 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001727
Eric Laurentca7cc822012-11-19 14:55:58 -08001728 if (mClient != 0) {
1729 if (mCblk != NULL) {
1730 // unlike ~TrackBase(), mCblk is never a local new, so don't delete
1731 mCblk->~effect_param_cblk_t(); // destroy our shared-structure.
1732 }
1733 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
Eric Laurent021cf962014-05-13 10:18:14 -07001734 // Client destructor must run with AudioFlinger client mutex locked
1735 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurentca7cc822012-11-19 14:55:58 -08001736 mClient.clear();
1737 }
1738}
1739
1740status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
1741 uint32_t cmdSize,
1742 void *pCmdData,
1743 uint32_t *replySize,
1744 void *pReplyData)
1745{
1746 ALOGVV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001747 cmdCode, mHasControl, mEffect.unsafe_get());
Eric Laurentca7cc822012-11-19 14:55:58 -08001748
Eric Laurentc7ab3092017-06-15 18:43:46 -07001749 // reject commands reserved for internal use by audio framework if coming from outside
1750 // of audioserver
1751 switch(cmdCode) {
1752 case EFFECT_CMD_ENABLE:
1753 case EFFECT_CMD_DISABLE:
1754 case EFFECT_CMD_SET_PARAM:
1755 case EFFECT_CMD_SET_PARAM_DEFERRED:
1756 case EFFECT_CMD_SET_PARAM_COMMIT:
1757 case EFFECT_CMD_GET_PARAM:
1758 break;
1759 default:
1760 if (cmdCode >= EFFECT_CMD_FIRST_PROPRIETARY) {
1761 break;
1762 }
1763 android_errorWriteLog(0x534e4554, "62019992");
1764 return BAD_VALUE;
1765 }
1766
Eric Laurent1ffc5852016-12-15 14:46:09 -08001767 if (cmdCode == EFFECT_CMD_ENABLE) {
1768 if (*replySize < sizeof(int)) {
1769 android_errorWriteLog(0x534e4554, "32095713");
1770 return BAD_VALUE;
1771 }
1772 *(int *)pReplyData = NO_ERROR;
1773 *replySize = sizeof(int);
1774 return enable();
1775 } else if (cmdCode == EFFECT_CMD_DISABLE) {
1776 if (*replySize < sizeof(int)) {
1777 android_errorWriteLog(0x534e4554, "32095713");
1778 return BAD_VALUE;
1779 }
1780 *(int *)pReplyData = NO_ERROR;
1781 *replySize = sizeof(int);
1782 return disable();
1783 }
1784
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001785 AutoMutex _l(mLock);
Eric Laurent41709552019-12-16 19:34:05 -08001786 sp<EffectBase> effect = mEffect.promote();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001787 if (effect == 0 || mDisconnected) {
1788 return DEAD_OBJECT;
1789 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001790 // only get parameter command is permitted for applications not controlling the effect
1791 if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
1792 return INVALID_OPERATION;
1793 }
Eric Laurentca7cc822012-11-19 14:55:58 -08001794
1795 // handle commands that are not forwarded transparently to effect engine
1796 if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
Eric Laurentb82e6b72019-11-22 17:25:04 -08001797 if (mClient == 0) {
1798 return INVALID_OPERATION;
1799 }
1800
Eric Laurent1ffc5852016-12-15 14:46:09 -08001801 if (*replySize < sizeof(int)) {
1802 android_errorWriteLog(0x534e4554, "32095713");
1803 return BAD_VALUE;
1804 }
1805 *(int *)pReplyData = NO_ERROR;
1806 *replySize = sizeof(int);
1807
Eric Laurentca7cc822012-11-19 14:55:58 -08001808 // No need to trylock() here as this function is executed in the binder thread serving a
1809 // particular client process: no risk to block the whole media server process or mixer
1810 // threads if we are stuck here
1811 Mutex::Autolock _l(mCblk->lock);
Andy Hunga447a0f2016-11-15 17:19:58 -08001812 // keep local copy of index in case of client corruption b/32220769
1813 const uint32_t clientIndex = mCblk->clientIndex;
1814 const uint32_t serverIndex = mCblk->serverIndex;
1815 if (clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
1816 serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001817 mCblk->serverIndex = 0;
1818 mCblk->clientIndex = 0;
1819 return BAD_VALUE;
1820 }
1821 status_t status = NO_ERROR;
Andy Hunga447a0f2016-11-15 17:19:58 -08001822 effect_param_t *param = NULL;
1823 for (uint32_t index = serverIndex; index < clientIndex;) {
1824 int *p = (int *)(mBuffer + index);
1825 const int size = *p++;
1826 if (size < 0
1827 || size > EFFECT_PARAM_BUFFER_SIZE
1828 || ((uint8_t *)p + size) > mBuffer + clientIndex) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001829 ALOGW("command(): invalid parameter block size");
Andy Hunga447a0f2016-11-15 17:19:58 -08001830 status = BAD_VALUE;
Eric Laurentca7cc822012-11-19 14:55:58 -08001831 break;
1832 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001833
1834 // copy to local memory in case of client corruption b/32220769
1835 param = (effect_param_t *)realloc(param, size);
1836 if (param == NULL) {
1837 ALOGW("command(): out of memory");
1838 status = NO_MEMORY;
1839 break;
Eric Laurentca7cc822012-11-19 14:55:58 -08001840 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001841 memcpy(param, p, size);
1842
1843 int reply = 0;
1844 uint32_t rsize = sizeof(reply);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001845 status_t ret = effect->command(EFFECT_CMD_SET_PARAM,
Andy Hunga447a0f2016-11-15 17:19:58 -08001846 size,
1847 param,
Eric Laurentca7cc822012-11-19 14:55:58 -08001848 &rsize,
1849 &reply);
Andy Hunga447a0f2016-11-15 17:19:58 -08001850
1851 // verify shared memory: server index shouldn't change; client index can't go back.
1852 if (serverIndex != mCblk->serverIndex
1853 || clientIndex > mCblk->clientIndex) {
1854 android_errorWriteLog(0x534e4554, "32220769");
1855 status = BAD_VALUE;
1856 break;
1857 }
1858
Eric Laurentca7cc822012-11-19 14:55:58 -08001859 // stop at first error encountered
1860 if (ret != NO_ERROR) {
1861 status = ret;
1862 *(int *)pReplyData = reply;
1863 break;
1864 } else if (reply != NO_ERROR) {
1865 *(int *)pReplyData = reply;
1866 break;
1867 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001868 index += size;
Eric Laurentca7cc822012-11-19 14:55:58 -08001869 }
Andy Hunga447a0f2016-11-15 17:19:58 -08001870 free(param);
Eric Laurentca7cc822012-11-19 14:55:58 -08001871 mCblk->serverIndex = 0;
1872 mCblk->clientIndex = 0;
1873 return status;
Eric Laurentca7cc822012-11-19 14:55:58 -08001874 }
1875
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08001876 return effect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
Eric Laurentca7cc822012-11-19 14:55:58 -08001877}
1878
1879void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
1880{
1881 ALOGV("setControl %p control %d", this, hasControl);
1882
1883 mHasControl = hasControl;
1884 mEnabled = enabled;
1885
1886 if (signal && mEffectClient != 0) {
1887 mEffectClient->controlStatusChanged(hasControl);
1888 }
1889}
1890
1891void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
1892 uint32_t cmdSize,
1893 void *pCmdData,
1894 uint32_t replySize,
1895 void *pReplyData)
1896{
1897 if (mEffectClient != 0) {
1898 mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
1899 }
1900}
1901
1902
1903
1904void AudioFlinger::EffectHandle::setEnabled(bool enabled)
1905{
1906 if (mEffectClient != 0) {
1907 mEffectClient->enableStatusChanged(enabled);
1908 }
1909}
1910
1911status_t AudioFlinger::EffectHandle::onTransact(
1912 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1913{
1914 return BnEffect::onTransact(code, data, reply, flags);
1915}
1916
1917
Glenn Kasten01d3acb2014-02-06 08:24:07 -08001918void AudioFlinger::EffectHandle::dumpToBuffer(char* buffer, size_t size)
Eric Laurentca7cc822012-11-19 14:55:58 -08001919{
1920 bool locked = mCblk != NULL && AudioFlinger::dumpTryLock(mCblk->lock);
1921
Marco Nelissenb2208842014-02-07 14:00:50 -08001922 snprintf(buffer, size, "\t\t\t%5d %5d %3s %3s %5u %5u\n",
Andy Hung4ef19fa2018-05-15 19:35:29 -07001923 (mClient == 0) ? getpid() : mClient->pid(),
Eric Laurentca7cc822012-11-19 14:55:58 -08001924 mPriority,
Marco Nelissenb2208842014-02-07 14:00:50 -08001925 mHasControl ? "yes" : "no",
1926 locked ? "yes" : "no",
Eric Laurentca7cc822012-11-19 14:55:58 -08001927 mCblk ? mCblk->clientIndex : 0,
1928 mCblk ? mCblk->serverIndex : 0
1929 );
1930
1931 if (locked) {
1932 mCblk->lock.unlock();
1933 }
1934}
1935
1936#undef LOG_TAG
1937#define LOG_TAG "AudioFlinger::EffectChain"
1938
1939AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
Glenn Kastend848eb42016-03-08 13:42:11 -08001940 audio_session_t sessionId)
Eric Laurent6b446ce2019-12-13 10:56:31 -08001941 : mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
Mikhail Naganov022b9952017-01-04 16:36:51 -08001942 mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
Eric Laurent6b446ce2019-12-13 10:56:31 -08001943 mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX),
1944 mEffectCallback(new EffectCallback(this, thread, thread->mAudioFlinger.get()))
Eric Laurentca7cc822012-11-19 14:55:58 -08001945{
1946 mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
Eric Laurent6b446ce2019-12-13 10:56:31 -08001947 if (thread == nullptr) {
Eric Laurentca7cc822012-11-19 14:55:58 -08001948 return;
1949 }
1950 mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
1951 thread->frameCount();
1952}
1953
1954AudioFlinger::EffectChain::~EffectChain()
1955{
Eric Laurentca7cc822012-11-19 14:55:58 -08001956}
1957
1958// getEffectFromDesc_l() must be called with ThreadBase::mLock held
1959sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(
1960 effect_descriptor_t *descriptor)
1961{
1962 size_t size = mEffects.size();
1963
1964 for (size_t i = 0; i < size; i++) {
1965 if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
1966 return mEffects[i];
1967 }
1968 }
1969 return 0;
1970}
1971
1972// getEffectFromId_l() must be called with ThreadBase::mLock held
1973sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
1974{
1975 size_t size = mEffects.size();
1976
1977 for (size_t i = 0; i < size; i++) {
1978 // by convention, return first effect if id provided is 0 (0 is never a valid id)
1979 if (id == 0 || mEffects[i]->id() == id) {
1980 return mEffects[i];
1981 }
1982 }
1983 return 0;
1984}
1985
1986// getEffectFromType_l() must be called with ThreadBase::mLock held
1987sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
1988 const effect_uuid_t *type)
1989{
1990 size_t size = mEffects.size();
1991
1992 for (size_t i = 0; i < size; i++) {
1993 if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
1994 return mEffects[i];
1995 }
1996 }
1997 return 0;
1998}
1999
Eric Laurent6c796322019-04-09 14:13:17 -07002000std::vector<int> AudioFlinger::EffectChain::getEffectIds()
2001{
2002 std::vector<int> ids;
2003 Mutex::Autolock _l(mLock);
2004 for (size_t i = 0; i < mEffects.size(); i++) {
2005 ids.push_back(mEffects[i]->id());
2006 }
2007 return ids;
2008}
2009
Eric Laurentca7cc822012-11-19 14:55:58 -08002010void AudioFlinger::EffectChain::clearInputBuffer()
2011{
2012 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002013 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002014}
2015
2016// Must be called with EffectChain::mLock locked
Eric Laurent6b446ce2019-12-13 10:56:31 -08002017void AudioFlinger::EffectChain::clearInputBuffer_l()
Eric Laurentca7cc822012-11-19 14:55:58 -08002018{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002019 if (mInBuffer == NULL) {
2020 return;
2021 }
Ricardo Garcia726b6a72014-08-11 12:04:54 -07002022 const size_t frameSize =
Eric Laurent6b446ce2019-12-13 10:56:31 -08002023 audio_bytes_per_sample(EFFECT_BUFFER_FORMAT) * mEffectCallback->channelCount();
rago94a1ee82017-07-21 15:11:02 -07002024
Eric Laurent6b446ce2019-12-13 10:56:31 -08002025 memset(mInBuffer->audioBuffer()->raw, 0, mEffectCallback->frameCount() * frameSize);
Mikhail Naganov022b9952017-01-04 16:36:51 -08002026 mInBuffer->commit();
Eric Laurentca7cc822012-11-19 14:55:58 -08002027}
2028
2029// Must be called with EffectChain::mLock locked
2030void AudioFlinger::EffectChain::process_l()
2031{
Jean-Michel Trivifed62922013-09-25 18:50:33 -07002032 // never process effects when:
2033 // - on an OFFLOAD thread
2034 // - no more tracks are on the session and the effect tail has been rendered
Eric Laurent6b446ce2019-12-13 10:56:31 -08002035 bool doProcess = !mEffectCallback->isOffloadOrMmap();
Eric Laurent3f75a5b2019-11-12 15:55:51 -08002036 if (!audio_is_global_session(mSessionId)) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002037 bool tracksOnSession = (trackCnt() != 0);
2038
2039 if (!tracksOnSession && mTailBufferCount == 0) {
2040 doProcess = false;
2041 }
2042
2043 if (activeTrackCnt() == 0) {
2044 // if no track is active and the effect tail has not been rendered,
2045 // the input buffer must be cleared here as the mixer process will not do it
2046 if (tracksOnSession || mTailBufferCount > 0) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002047 clearInputBuffer_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002048 if (mTailBufferCount > 0) {
2049 mTailBufferCount--;
2050 }
2051 }
2052 }
2053 }
2054
2055 size_t size = mEffects.size();
2056 if (doProcess) {
Mikhail Naganov022b9952017-01-04 16:36:51 -08002057 // Only the input and output buffers of the chain can be external,
2058 // and 'update' / 'commit' do nothing for allocated buffers, thus
2059 // it's not needed to consider any other buffers here.
2060 mInBuffer->update();
Mikhail Naganov06888802017-01-19 12:47:55 -08002061 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2062 mOutBuffer->update();
2063 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002064 for (size_t i = 0; i < size; i++) {
2065 mEffects[i]->process();
2066 }
Mikhail Naganov06888802017-01-19 12:47:55 -08002067 mInBuffer->commit();
2068 if (mInBuffer->audioBuffer()->raw != mOutBuffer->audioBuffer()->raw) {
2069 mOutBuffer->commit();
2070 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002071 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002072 bool doResetVolume = false;
Eric Laurentca7cc822012-11-19 14:55:58 -08002073 for (size_t i = 0; i < size; i++) {
Eric Laurentfa1e1232016-08-02 19:01:49 -07002074 doResetVolume = mEffects[i]->updateState() || doResetVolume;
2075 }
2076 if (doResetVolume) {
2077 resetVolume_l();
Eric Laurentca7cc822012-11-19 14:55:58 -08002078 }
2079}
2080
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002081// createEffect_l() must be called with ThreadBase::mLock held
2082status_t AudioFlinger::EffectChain::createEffect_l(sp<EffectModule>& effect,
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002083 effect_descriptor_t *desc,
2084 int id,
2085 audio_session_t sessionId,
2086 bool pinned)
2087{
2088 Mutex::Autolock _l(mLock);
Eric Laurentb82e6b72019-11-22 17:25:04 -08002089 effect = new EffectModule(mEffectCallback, desc, id, sessionId, pinned, AUDIO_PORT_HANDLE_NONE);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002090 status_t lStatus = effect->status();
2091 if (lStatus == NO_ERROR) {
2092 lStatus = addEffect_ll(effect);
2093 }
2094 if (lStatus != NO_ERROR) {
2095 effect.clear();
2096 }
2097 return lStatus;
2098}
2099
2100// addEffect_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002101status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
2102{
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002103 Mutex::Autolock _l(mLock);
2104 return addEffect_ll(effect);
2105}
2106// addEffect_l() must be called with ThreadBase::mLock and EffectChain::mLock held
2107status_t AudioFlinger::EffectChain::addEffect_ll(const sp<EffectModule>& effect)
2108{
Eric Laurentca7cc822012-11-19 14:55:58 -08002109 effect_descriptor_t desc = effect->desc();
2110 uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
2111
Eric Laurent6b446ce2019-12-13 10:56:31 -08002112 effect->setCallback(mEffectCallback);
Eric Laurentca7cc822012-11-19 14:55:58 -08002113
2114 if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
2115 // Auxiliary effects are inserted at the beginning of mEffects vector as
2116 // they are processed first and accumulated in chain input buffer
2117 mEffects.insertAt(effect, 0);
2118
2119 // the input buffer for auxiliary effect contains mono samples in
2120 // 32 bit format. This is to avoid saturation in AudoMixer
2121 // accumulation stage. Saturation is done in EffectModule::process() before
2122 // calling the process in effect engine
Eric Laurent6b446ce2019-12-13 10:56:31 -08002123 size_t numSamples = mEffectCallback->frameCount();
Mikhail Naganov022b9952017-01-04 16:36:51 -08002124 sp<EffectBufferHalInterface> halBuffer;
rago94a1ee82017-07-21 15:11:02 -07002125#ifdef FLOAT_EFFECT_CHAIN
Eric Laurent6b446ce2019-12-13 10:56:31 -08002126 status_t result = mEffectCallback->allocateHalBuffer(
rago94a1ee82017-07-21 15:11:02 -07002127 numSamples * sizeof(float), &halBuffer);
2128#else
Eric Laurent6b446ce2019-12-13 10:56:31 -08002129 status_t result = mEffectCallback->allocateHalBuffer(
Mikhail Naganov022b9952017-01-04 16:36:51 -08002130 numSamples * sizeof(int32_t), &halBuffer);
rago94a1ee82017-07-21 15:11:02 -07002131#endif
Mikhail Naganov022b9952017-01-04 16:36:51 -08002132 if (result != OK) return result;
2133 effect->setInBuffer(halBuffer);
Eric Laurentca7cc822012-11-19 14:55:58 -08002134 // auxiliary effects output samples to chain input buffer for further processing
2135 // by insert effects
2136 effect->setOutBuffer(mInBuffer);
2137 } else {
2138 // Insert effects are inserted at the end of mEffects vector as they are processed
2139 // after track and auxiliary effects.
2140 // Insert effect order as a function of indicated preference:
2141 // if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
2142 // another effect is present
2143 // else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
2144 // last effect claiming first position
2145 // else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
2146 // first effect claiming last position
2147 // else if EFFECT_FLAG_INSERT_ANY insert after first or before last
2148 // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
2149 // already present
2150
2151 size_t size = mEffects.size();
2152 size_t idx_insert = size;
2153 ssize_t idx_insert_first = -1;
2154 ssize_t idx_insert_last = -1;
2155
2156 for (size_t i = 0; i < size; i++) {
2157 effect_descriptor_t d = mEffects[i]->desc();
2158 uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
2159 uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
2160 if (iMode == EFFECT_FLAG_TYPE_INSERT) {
2161 // check invalid effect chaining combinations
2162 if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
2163 iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
2164 ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s",
2165 desc.name, d.name);
2166 return INVALID_OPERATION;
2167 }
2168 // remember position of first insert effect and by default
2169 // select this as insert position for new effect
2170 if (idx_insert == size) {
2171 idx_insert = i;
2172 }
2173 // remember position of last insert effect claiming
2174 // first position
2175 if (iPref == EFFECT_FLAG_INSERT_FIRST) {
2176 idx_insert_first = i;
2177 }
2178 // remember position of first insert effect claiming
2179 // last position
2180 if (iPref == EFFECT_FLAG_INSERT_LAST &&
2181 idx_insert_last == -1) {
2182 idx_insert_last = i;
2183 }
2184 }
2185 }
2186
2187 // modify idx_insert from first position if needed
2188 if (insertPref == EFFECT_FLAG_INSERT_LAST) {
2189 if (idx_insert_last != -1) {
2190 idx_insert = idx_insert_last;
2191 } else {
2192 idx_insert = size;
2193 }
2194 } else {
2195 if (idx_insert_first != -1) {
2196 idx_insert = idx_insert_first + 1;
2197 }
2198 }
2199
2200 // always read samples from chain input buffer
2201 effect->setInBuffer(mInBuffer);
2202
2203 // if last effect in the chain, output samples to chain
2204 // output buffer, otherwise to chain input buffer
2205 if (idx_insert == size) {
2206 if (idx_insert != 0) {
2207 mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
2208 mEffects[idx_insert-1]->configure();
2209 }
2210 effect->setOutBuffer(mOutBuffer);
2211 } else {
2212 effect->setOutBuffer(mInBuffer);
2213 }
2214 mEffects.insertAt(effect, idx_insert);
2215
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002216 ALOGV("addEffect_l() effect %p, added in chain %p at rank %zu", effect.get(), this,
Eric Laurentca7cc822012-11-19 14:55:58 -08002217 idx_insert);
2218 }
2219 effect->configure();
Eric Laurentd8365c52017-07-16 15:27:05 -07002220
Eric Laurentca7cc822012-11-19 14:55:58 -08002221 return NO_ERROR;
2222}
2223
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002224// removeEffect_l() must be called with ThreadBase::mLock held
2225size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect,
2226 bool release)
Eric Laurentca7cc822012-11-19 14:55:58 -08002227{
2228 Mutex::Autolock _l(mLock);
2229 size_t size = mEffects.size();
2230 uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
2231
2232 for (size_t i = 0; i < size; i++) {
2233 if (effect == mEffects[i]) {
2234 // calling stop here will remove pre-processing effect from the audio HAL.
2235 // This is safe as we hold the EffectChain mutex which guarantees that we are not in
2236 // the middle of a read from audio HAL
2237 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2238 mEffects[i]->state() == EffectModule::STOPPING) {
2239 mEffects[i]->stop();
2240 }
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002241 if (release) {
2242 mEffects[i]->release_l();
2243 }
2244
Mikhail Naganov022b9952017-01-04 16:36:51 -08002245 if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002246 if (i == size - 1 && i != 0) {
2247 mEffects[i - 1]->setOutBuffer(mOutBuffer);
2248 mEffects[i - 1]->configure();
2249 }
2250 }
2251 mEffects.removeAt(i);
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002252 ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
Eric Laurentca7cc822012-11-19 14:55:58 -08002253 this, i);
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002254
Eric Laurentca7cc822012-11-19 14:55:58 -08002255 break;
2256 }
2257 }
2258
2259 return mEffects.size();
2260}
2261
jiabin8f278ee2019-11-11 12:16:27 -08002262// setDevices_l() must be called with ThreadBase::mLock held
2263void AudioFlinger::EffectChain::setDevices_l(const AudioDeviceTypeAddrVector &devices)
Eric Laurentca7cc822012-11-19 14:55:58 -08002264{
2265 size_t size = mEffects.size();
2266 for (size_t i = 0; i < size; i++) {
jiabin8f278ee2019-11-11 12:16:27 -08002267 mEffects[i]->setDevices(devices);
2268 }
2269}
2270
2271// setInputDevice_l() must be called with ThreadBase::mLock held
2272void AudioFlinger::EffectChain::setInputDevice_l(const AudioDeviceTypeAddr &device)
2273{
2274 size_t size = mEffects.size();
2275 for (size_t i = 0; i < size; i++) {
2276 mEffects[i]->setInputDevice(device);
Eric Laurentca7cc822012-11-19 14:55:58 -08002277 }
2278}
2279
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002280// setMode_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002281void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
2282{
2283 size_t size = mEffects.size();
2284 for (size_t i = 0; i < size; i++) {
2285 mEffects[i]->setMode(mode);
2286 }
2287}
2288
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002289// setAudioSource_l() must be called with ThreadBase::mLock held
Eric Laurentca7cc822012-11-19 14:55:58 -08002290void AudioFlinger::EffectChain::setAudioSource_l(audio_source_t source)
2291{
2292 size_t size = mEffects.size();
2293 for (size_t i = 0; i < size; i++) {
2294 mEffects[i]->setAudioSource(source);
2295 }
2296}
2297
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002298// setVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002299bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right, bool force)
Eric Laurentca7cc822012-11-19 14:55:58 -08002300{
2301 uint32_t newLeft = *left;
2302 uint32_t newRight = *right;
2303 bool hasControl = false;
2304 int ctrlIdx = -1;
2305 size_t size = mEffects.size();
2306
2307 // first update volume controller
2308 for (size_t i = size; i > 0; i--) {
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002309 if (mEffects[i - 1]->isVolumeControlEnabled()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002310 ctrlIdx = i - 1;
2311 hasControl = true;
2312 break;
2313 }
2314 }
2315
Eric Laurentfa1e1232016-08-02 19:01:49 -07002316 if (!force && ctrlIdx == mVolumeCtrlIdx &&
Eric Laurentcb4b6e92014-10-01 14:26:10 -07002317 *left == mLeftVolume && *right == mRightVolume) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002318 if (hasControl) {
2319 *left = mNewLeftVolume;
2320 *right = mNewRightVolume;
2321 }
2322 return hasControl;
2323 }
2324
2325 mVolumeCtrlIdx = ctrlIdx;
2326 mLeftVolume = newLeft;
2327 mRightVolume = newRight;
2328
2329 // second get volume update from volume controller
2330 if (ctrlIdx >= 0) {
2331 mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
2332 mNewLeftVolume = newLeft;
2333 mNewRightVolume = newRight;
2334 }
2335 // then indicate volume to all other effects in chain.
2336 // Pass altered volume to effects before volume controller
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002337 // and requested volume to effects after controller or with volume monitor flag
Eric Laurentca7cc822012-11-19 14:55:58 -08002338 uint32_t lVol = newLeft;
2339 uint32_t rVol = newRight;
2340
2341 for (size_t i = 0; i < size; i++) {
2342 if ((int)i == ctrlIdx) {
2343 continue;
2344 }
2345 // this also works for ctrlIdx == -1 when there is no volume controller
2346 if ((int)i > ctrlIdx) {
2347 lVol = *left;
2348 rVol = *right;
2349 }
Jasmine Cha934ecfb2019-01-23 18:19:14 +08002350 // Pass requested volume directly if this is volume monitor module
2351 if (mEffects[i]->isVolumeMonitor()) {
2352 mEffects[i]->setVolume(left, right, false);
2353 } else {
2354 mEffects[i]->setVolume(&lVol, &rVol, false);
2355 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002356 }
2357 *left = newLeft;
2358 *right = newRight;
2359
Tomoharu Kasahara1990bd42014-12-12 14:04:11 +09002360 setVolumeForOutput_l(*left, *right);
2361
Eric Laurentca7cc822012-11-19 14:55:58 -08002362 return hasControl;
2363}
2364
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002365// resetVolume_l() must be called with ThreadBase::mLock or EffectChain::mLock held
Eric Laurentfa1e1232016-08-02 19:01:49 -07002366void AudioFlinger::EffectChain::resetVolume_l()
2367{
Eric Laurente7449bf2016-08-03 18:44:07 -07002368 if ((mLeftVolume != UINT_MAX) && (mRightVolume != UINT_MAX)) {
2369 uint32_t left = mLeftVolume;
2370 uint32_t right = mRightVolume;
2371 (void)setVolume_l(&left, &right, true);
2372 }
Eric Laurentfa1e1232016-08-02 19:01:49 -07002373}
2374
Eric Laurent1b928682014-10-02 19:41:47 -07002375void AudioFlinger::EffectChain::syncHalEffectsState()
2376{
2377 Mutex::Autolock _l(mLock);
2378 for (size_t i = 0; i < mEffects.size(); i++) {
2379 if (mEffects[i]->state() == EffectModule::ACTIVE ||
2380 mEffects[i]->state() == EffectModule::STOPPING) {
2381 mEffects[i]->addEffectToHal_l();
2382 }
2383 }
2384}
2385
Eric Laurentca7cc822012-11-19 14:55:58 -08002386void AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
2387{
Eric Laurentca7cc822012-11-19 14:55:58 -08002388 String8 result;
2389
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002390 const size_t numEffects = mEffects.size();
2391 result.appendFormat(" %zu effects for session %d\n", numEffects, mSessionId);
Eric Laurentca7cc822012-11-19 14:55:58 -08002392
Marco Nelissenb2208842014-02-07 14:00:50 -08002393 if (numEffects) {
2394 bool locked = AudioFlinger::dumpTryLock(mLock);
2395 // failed to lock - AudioFlinger is probably deadlocked
2396 if (!locked) {
2397 result.append("\tCould not lock mutex:\n");
Eric Laurentca7cc822012-11-19 14:55:58 -08002398 }
Eric Laurentca7cc822012-11-19 14:55:58 -08002399
Andy Hungbded9c82017-11-30 18:47:35 -08002400 const std::string inBufferStr = dumpInOutBuffer(true /* isInput */, mInBuffer);
2401 const std::string outBufferStr = dumpInOutBuffer(false /* isInput */, mOutBuffer);
2402 result.appendFormat("\t%-*s%-*s Active tracks:\n",
2403 (int)inBufferStr.size(), "In buffer ",
2404 (int)outBufferStr.size(), "Out buffer ");
2405 result.appendFormat("\t%s %s %d\n",
2406 inBufferStr.c_str(), outBufferStr.c_str(), mActiveTrackCnt);
Marco Nelissenb2208842014-02-07 14:00:50 -08002407 write(fd, result.string(), result.size());
2408
2409 for (size_t i = 0; i < numEffects; ++i) {
2410 sp<EffectModule> effect = mEffects[i];
2411 if (effect != 0) {
2412 effect->dump(fd, args);
2413 }
2414 }
2415
2416 if (locked) {
2417 mLock.unlock();
2418 }
Mikhail Naganov19740ca2019-03-28 12:25:01 -07002419 } else {
2420 write(fd, result.string(), result.size());
Eric Laurentca7cc822012-11-19 14:55:58 -08002421 }
2422}
2423
2424// must be called with ThreadBase::mLock held
2425void AudioFlinger::EffectChain::setEffectSuspended_l(
2426 const effect_uuid_t *type, bool suspend)
2427{
2428 sp<SuspendedEffectDesc> desc;
2429 // use effect type UUID timelow as key as there is no real risk of identical
2430 // timeLow fields among effect type UUIDs.
2431 ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
2432 if (suspend) {
2433 if (index >= 0) {
2434 desc = mSuspendedEffects.valueAt(index);
2435 } else {
2436 desc = new SuspendedEffectDesc();
2437 desc->mType = *type;
2438 mSuspendedEffects.add(type->timeLow, desc);
2439 ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
2440 }
Eric Laurentd8365c52017-07-16 15:27:05 -07002441
Eric Laurentca7cc822012-11-19 14:55:58 -08002442 if (desc->mRefCount++ == 0) {
2443 sp<EffectModule> effect = getEffectIfEnabled(type);
2444 if (effect != 0) {
2445 desc->mEffect = effect;
2446 effect->setSuspended(true);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002447 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002448 }
2449 }
2450 } else {
2451 if (index < 0) {
2452 return;
2453 }
2454 desc = mSuspendedEffects.valueAt(index);
2455 if (desc->mRefCount <= 0) {
2456 ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
Eric Laurentd8365c52017-07-16 15:27:05 -07002457 desc->mRefCount = 0;
2458 return;
Eric Laurentca7cc822012-11-19 14:55:58 -08002459 }
2460 if (--desc->mRefCount == 0) {
2461 ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
2462 if (desc->mEffect != 0) {
2463 sp<EffectModule> effect = desc->mEffect.promote();
2464 if (effect != 0) {
2465 effect->setSuspended(false);
2466 effect->lock();
2467 EffectHandle *handle = effect->controlHandle_l();
Eric Laurent0d5a2ed2016-12-01 15:28:29 -08002468 if (handle != NULL && !handle->disconnected()) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002469 effect->setEnabled_l(handle->enabled());
2470 }
2471 effect->unlock();
2472 }
2473 desc->mEffect.clear();
2474 }
2475 mSuspendedEffects.removeItemsAt(index);
2476 }
2477 }
2478}
2479
2480// must be called with ThreadBase::mLock held
2481void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
2482{
2483 sp<SuspendedEffectDesc> desc;
2484
2485 ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2486 if (suspend) {
2487 if (index >= 0) {
2488 desc = mSuspendedEffects.valueAt(index);
2489 } else {
2490 desc = new SuspendedEffectDesc();
2491 mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
2492 ALOGV("setEffectSuspendedAll_l() add entry for 0");
2493 }
2494 if (desc->mRefCount++ == 0) {
2495 Vector< sp<EffectModule> > effects;
2496 getSuspendEligibleEffects(effects);
2497 for (size_t i = 0; i < effects.size(); i++) {
2498 setEffectSuspended_l(&effects[i]->desc().type, true);
2499 }
2500 }
2501 } else {
2502 if (index < 0) {
2503 return;
2504 }
2505 desc = mSuspendedEffects.valueAt(index);
2506 if (desc->mRefCount <= 0) {
2507 ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
2508 desc->mRefCount = 1;
2509 }
2510 if (--desc->mRefCount == 0) {
2511 Vector<const effect_uuid_t *> types;
2512 for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
2513 if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
2514 continue;
2515 }
2516 types.add(&mSuspendedEffects.valueAt(i)->mType);
2517 }
2518 for (size_t i = 0; i < types.size(); i++) {
2519 setEffectSuspended_l(types[i], false);
2520 }
2521 ALOGV("setEffectSuspendedAll_l() remove entry for %08x",
2522 mSuspendedEffects.keyAt(index));
2523 mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
2524 }
2525 }
2526}
2527
2528
2529// The volume effect is used for automated tests only
2530#ifndef OPENSL_ES_H_
2531static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
2532 { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
2533const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
2534#endif //OPENSL_ES_H_
2535
Eric Laurentd8365c52017-07-16 15:27:05 -07002536/* static */
2537bool AudioFlinger::EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
2538{
2539 // Only NS and AEC are suspended when BtNRec is off
2540 if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
2541 (memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2542 return true;
2543 }
2544 return false;
2545}
2546
Eric Laurentca7cc822012-11-19 14:55:58 -08002547bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
2548{
2549 // auxiliary effects and visualizer are never suspended on output mix
2550 if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
2551 (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
2552 (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
Ricardo Garciac2a3a822019-07-17 14:29:12 -07002553 (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0) ||
2554 (memcmp(&desc.type, SL_IID_DYNAMICSPROCESSING, sizeof(effect_uuid_t)) == 0))) {
Eric Laurentca7cc822012-11-19 14:55:58 -08002555 return false;
2556 }
2557 return true;
2558}
2559
2560void AudioFlinger::EffectChain::getSuspendEligibleEffects(
2561 Vector< sp<AudioFlinger::EffectModule> > &effects)
2562{
2563 effects.clear();
2564 for (size_t i = 0; i < mEffects.size(); i++) {
2565 if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
2566 effects.add(mEffects[i]);
2567 }
2568 }
2569}
2570
2571sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
2572 const effect_uuid_t *type)
2573{
2574 sp<EffectModule> effect = getEffectFromType_l(type);
2575 return effect != 0 && effect->isEnabled() ? effect : 0;
2576}
2577
2578void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
2579 bool enabled)
2580{
2581 ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2582 if (enabled) {
2583 if (index < 0) {
2584 // if the effect is not suspend check if all effects are suspended
2585 index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
2586 if (index < 0) {
2587 return;
2588 }
2589 if (!isEffectEligibleForSuspend(effect->desc())) {
2590 return;
2591 }
2592 setEffectSuspended_l(&effect->desc().type, enabled);
2593 index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
2594 if (index < 0) {
2595 ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
2596 return;
2597 }
2598 }
2599 ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
2600 effect->desc().type.timeLow);
2601 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
Eric Laurentd8365c52017-07-16 15:27:05 -07002602 // if effect is requested to suspended but was not yet enabled, suspend it now.
Eric Laurentca7cc822012-11-19 14:55:58 -08002603 if (desc->mEffect == 0) {
2604 desc->mEffect = effect;
Eric Laurent6b446ce2019-12-13 10:56:31 -08002605 effect->setEnabled(false, false /*fromHandle*/);
Eric Laurentca7cc822012-11-19 14:55:58 -08002606 effect->setSuspended(true);
2607 }
2608 } else {
2609 if (index < 0) {
2610 return;
2611 }
2612 ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
2613 effect->desc().type.timeLow);
2614 sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
2615 desc->mEffect.clear();
2616 effect->setSuspended(false);
2617 }
2618}
2619
Eric Laurent5baf2af2013-09-12 17:37:00 -07002620bool AudioFlinger::EffectChain::isNonOffloadableEnabled()
Eric Laurent813e2a72013-08-31 12:59:48 -07002621{
2622 Mutex::Autolock _l(mLock);
Shingo Kitajima1f8df9a2018-05-29 11:35:06 +09002623 return isNonOffloadableEnabled_l();
2624}
2625
2626bool AudioFlinger::EffectChain::isNonOffloadableEnabled_l()
2627{
Eric Laurent813e2a72013-08-31 12:59:48 -07002628 size_t size = mEffects.size();
2629 for (size_t i = 0; i < size; i++) {
Eric Laurent5baf2af2013-09-12 17:37:00 -07002630 if (mEffects[i]->isEnabled() && !mEffects[i]->isOffloadable()) {
Eric Laurent813e2a72013-08-31 12:59:48 -07002631 return true;
2632 }
2633 }
2634 return false;
2635}
2636
Eric Laurentaaa44472014-09-12 17:41:50 -07002637void AudioFlinger::EffectChain::setThread(const sp<ThreadBase>& thread)
2638{
2639 Mutex::Autolock _l(mLock);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002640 mEffectCallback->setThread(thread.get());
Eric Laurentaaa44472014-09-12 17:41:50 -07002641}
2642
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002643void AudioFlinger::EffectChain::checkOutputFlagCompatibility(audio_output_flags_t *flags) const
2644{
2645 if ((*flags & AUDIO_OUTPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2646 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_RAW);
2647 }
2648 if ((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2649 *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
2650 }
2651}
2652
2653void AudioFlinger::EffectChain::checkInputFlagCompatibility(audio_input_flags_t *flags) const
2654{
2655 if ((*flags & AUDIO_INPUT_FLAG_RAW) != 0 && !isRawCompatible()) {
2656 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_RAW);
2657 }
2658 if ((*flags & AUDIO_INPUT_FLAG_FAST) != 0 && !isFastCompatible()) {
2659 *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
2660 }
2661}
2662
2663bool AudioFlinger::EffectChain::isRawCompatible() const
Eric Laurent4c415062016-06-17 16:14:16 -07002664{
2665 Mutex::Autolock _l(mLock);
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002666 for (const auto &effect : mEffects) {
2667 if (effect->isProcessImplemented()) {
2668 return false;
Eric Laurent4c415062016-06-17 16:14:16 -07002669 }
2670 }
Andy Hungd3bb0ad2016-10-11 17:16:43 -07002671 // Allow effects without processing.
2672 return true;
2673}
2674
2675bool AudioFlinger::EffectChain::isFastCompatible() const
2676{
2677 Mutex::Autolock _l(mLock);
2678 for (const auto &effect : mEffects) {
2679 if (effect->isProcessImplemented()
2680 && effect->isImplementationSoftware()) {
2681 return false;
2682 }
2683 }
2684 // Allow effects without processing or hw accelerated effects.
2685 return true;
Eric Laurent4c415062016-06-17 16:14:16 -07002686}
2687
2688// isCompatibleWithThread_l() must be called with thread->mLock held
2689bool AudioFlinger::EffectChain::isCompatibleWithThread_l(const sp<ThreadBase>& thread) const
2690{
2691 Mutex::Autolock _l(mLock);
2692 for (size_t i = 0; i < mEffects.size(); i++) {
2693 if (thread->checkEffectCompatibility_l(&(mEffects[i]->desc()), mSessionId) != NO_ERROR) {
2694 return false;
2695 }
2696 }
2697 return true;
2698}
2699
Eric Laurent6b446ce2019-12-13 10:56:31 -08002700// EffectCallbackInterface implementation
2701status_t AudioFlinger::EffectChain::EffectCallback::createEffectHal(
2702 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
2703 sp<EffectHalInterface> *effect) {
2704 status_t status = NO_INIT;
2705 sp<AudioFlinger> af = mAudioFlinger.promote();
2706 if (af == nullptr) {
2707 return status;
2708 }
2709 sp<EffectsFactoryHalInterface> effectsFactory = af->getEffectsFactory();
2710 if (effectsFactory != 0) {
2711 status = effectsFactory->createEffect(pEffectUuid, sessionId, io(), deviceId, effect);
2712 }
2713 return status;
2714}
2715
2716bool AudioFlinger::EffectChain::EffectCallback::updateOrphanEffectChains(
Eric Laurent41709552019-12-16 19:34:05 -08002717 const sp<AudioFlinger::EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002718 sp<AudioFlinger> af = mAudioFlinger.promote();
2719 if (af == nullptr) {
2720 return false;
2721 }
Eric Laurent41709552019-12-16 19:34:05 -08002722 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2723 return af->updateOrphanEffectChains(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002724}
2725
2726status_t AudioFlinger::EffectChain::EffectCallback::allocateHalBuffer(
2727 size_t size, sp<EffectBufferHalInterface>* buffer) {
2728 sp<AudioFlinger> af = mAudioFlinger.promote();
2729 LOG_ALWAYS_FATAL_IF(af == nullptr, "allocateHalBuffer() could not retrieved audio flinger");
2730 return af->mEffectsFactoryHal->allocateBuffer(size, buffer);
2731}
2732
2733status_t AudioFlinger::EffectChain::EffectCallback::addEffectToHal(
2734 sp<EffectHalInterface> effect) {
2735 status_t result = NO_INIT;
2736 sp<ThreadBase> t = mThread.promote();
2737 if (t == nullptr) {
2738 return result;
2739 }
2740 sp <StreamHalInterface> st = t->stream();
2741 if (st == nullptr) {
2742 return result;
2743 }
2744 result = st->addEffect(effect);
2745 ALOGE_IF(result != OK, "Error when adding effect: %d", result);
2746 return result;
2747}
2748
2749status_t AudioFlinger::EffectChain::EffectCallback::removeEffectFromHal(
2750 sp<EffectHalInterface> effect) {
2751 status_t result = NO_INIT;
2752 sp<ThreadBase> t = mThread.promote();
2753 if (t == nullptr) {
2754 return result;
2755 }
2756 sp <StreamHalInterface> st = t->stream();
2757 if (st == nullptr) {
2758 return result;
2759 }
2760 result = st->removeEffect(effect);
2761 ALOGE_IF(result != OK, "Error when removing effect: %d", result);
2762 return result;
2763}
2764
2765audio_io_handle_t AudioFlinger::EffectChain::EffectCallback::io() const {
2766 sp<ThreadBase> t = mThread.promote();
2767 if (t == nullptr) {
2768 return AUDIO_IO_HANDLE_NONE;
2769 }
2770 return t->id();
2771}
2772
2773bool AudioFlinger::EffectChain::EffectCallback::isOutput() const {
2774 sp<ThreadBase> t = mThread.promote();
2775 if (t == nullptr) {
2776 return true;
2777 }
2778 return t->isOutput();
2779}
2780
2781bool AudioFlinger::EffectChain::EffectCallback::isOffload() const {
2782 sp<ThreadBase> t = mThread.promote();
2783 if (t == nullptr) {
2784 return false;
2785 }
2786 return t->type() == ThreadBase::OFFLOAD;
2787}
2788
2789bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrDirect() const {
2790 sp<ThreadBase> t = mThread.promote();
2791 if (t == nullptr) {
2792 return false;
2793 }
2794 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::DIRECT;
2795}
2796
2797bool AudioFlinger::EffectChain::EffectCallback::isOffloadOrMmap() const {
2798 sp<ThreadBase> t = mThread.promote();
2799 if (t == nullptr) {
2800 return false;
2801 }
2802 return t->type() == ThreadBase::OFFLOAD || t->type() == ThreadBase::MMAP;
2803}
2804
2805uint32_t AudioFlinger::EffectChain::EffectCallback::sampleRate() const {
2806 sp<ThreadBase> t = mThread.promote();
2807 if (t == nullptr) {
2808 return 0;
2809 }
2810 return t->sampleRate();
2811}
2812
2813audio_channel_mask_t AudioFlinger::EffectChain::EffectCallback::channelMask() const {
2814 sp<ThreadBase> t = mThread.promote();
2815 if (t == nullptr) {
2816 return AUDIO_CHANNEL_NONE;
2817 }
2818 return t->channelMask();
2819}
2820
2821uint32_t AudioFlinger::EffectChain::EffectCallback::channelCount() const {
2822 sp<ThreadBase> t = mThread.promote();
2823 if (t == nullptr) {
2824 return 0;
2825 }
2826 return t->channelCount();
2827}
2828
2829size_t AudioFlinger::EffectChain::EffectCallback::frameCount() const {
2830 sp<ThreadBase> t = mThread.promote();
2831 if (t == nullptr) {
2832 return 0;
2833 }
2834 return t->frameCount();
2835}
2836
2837uint32_t AudioFlinger::EffectChain::EffectCallback::latency() const {
2838 sp<ThreadBase> t = mThread.promote();
2839 if (t == nullptr) {
2840 return 0;
2841 }
2842 return t->latency_l();
2843}
2844
2845void AudioFlinger::EffectChain::EffectCallback::setVolumeForOutput(float left, float right) const {
2846 sp<ThreadBase> t = mThread.promote();
2847 if (t == nullptr) {
2848 return;
2849 }
2850 t->setVolumeForOutput_l(left, right);
2851}
2852
2853void AudioFlinger::EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
Eric Laurent41709552019-12-16 19:34:05 -08002854 const sp<EffectBase>& effect, bool enabled, bool threadLocked) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002855 sp<ThreadBase> t = mThread.promote();
2856 if (t == nullptr) {
2857 return;
2858 }
2859 t->checkSuspendOnEffectEnabled(enabled, effect->sessionId(), threadLocked);
2860
2861 sp<EffectChain> c = mChain.promote();
2862 if (c == nullptr) {
2863 return;
2864 }
Eric Laurent41709552019-12-16 19:34:05 -08002865 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2866 c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
Eric Laurent6b446ce2019-12-13 10:56:31 -08002867}
2868
Eric Laurent41709552019-12-16 19:34:05 -08002869void AudioFlinger::EffectChain::EffectCallback::onEffectEnable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002870 sp<ThreadBase> t = mThread.promote();
2871 if (t == nullptr) {
2872 return;
2873 }
Eric Laurent41709552019-12-16 19:34:05 -08002874 // in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
2875 t->onEffectEnable(effect->asEffectModule());
Eric Laurent6b446ce2019-12-13 10:56:31 -08002876}
2877
Eric Laurent41709552019-12-16 19:34:05 -08002878void AudioFlinger::EffectChain::EffectCallback::onEffectDisable(const sp<EffectBase>& effect) {
Eric Laurent6b446ce2019-12-13 10:56:31 -08002879 checkSuspendOnEffectEnabled(effect, false, false /*threadLocked*/);
2880
2881 sp<ThreadBase> t = mThread.promote();
2882 if (t == nullptr) {
2883 return;
2884 }
2885 t->onEffectDisable();
2886}
2887
2888bool AudioFlinger::EffectChain::EffectCallback::disconnectEffectHandle(EffectHandle *handle,
2889 bool unpinIfLast) {
2890 sp<ThreadBase> t = mThread.promote();
2891 if (t == nullptr) {
2892 return false;
2893 }
2894 t->disconnectEffectHandle(handle, unpinIfLast);
2895 return true;
2896}
2897
2898void AudioFlinger::EffectChain::EffectCallback::resetVolume() {
2899 sp<EffectChain> c = mChain.promote();
2900 if (c == nullptr) {
2901 return;
2902 }
2903 c->resetVolume_l();
2904
2905}
2906
2907uint32_t AudioFlinger::EffectChain::EffectCallback::strategy() const {
2908 sp<EffectChain> c = mChain.promote();
2909 if (c == nullptr) {
2910 return PRODUCT_STRATEGY_NONE;
2911 }
2912 return c->strategy();
2913}
2914
2915int32_t AudioFlinger::EffectChain::EffectCallback::activeTrackCnt() const {
2916 sp<EffectChain> c = mChain.promote();
2917 if (c == nullptr) {
2918 return 0;
2919 }
2920 return c->activeTrackCnt();
2921}
2922
Eric Laurentb82e6b72019-11-22 17:25:04 -08002923
2924#undef LOG_TAG
2925#define LOG_TAG "AudioFlinger::DeviceEffectProxy"
2926
2927status_t AudioFlinger::DeviceEffectProxy::setEnabled(bool enabled, bool fromHandle)
2928{
2929 status_t status = EffectBase::setEnabled(enabled, fromHandle);
2930 Mutex::Autolock _l(mProxyLock);
2931 if (status == NO_ERROR) {
2932 for (auto& handle : mEffectHandles) {
2933 if (enabled) {
2934 status = handle.second->enable();
2935 } else {
2936 status = handle.second->disable();
2937 }
2938 }
2939 }
2940 ALOGV("%s enable %d status %d", __func__, enabled, status);
2941 return status;
2942}
2943
2944status_t AudioFlinger::DeviceEffectProxy::init(
2945 const std::map <audio_patch_handle_t, PatchPanel::Patch>& patches) {
2946//For all audio patches
2947//If src or sink device match
2948//If the effect is HW accelerated
2949// if no corresponding effect module
2950// Create EffectModule: mHalEffect
2951//Create and attach EffectHandle
2952//If the effect is not HW accelerated and the patch sink or src is a mixer port
2953// Create Effect on patch input or output thread on session -1
2954//Add EffectHandle to EffectHandle map of Effect Proxy:
2955 ALOGV("%s device type %d address %s", __func__, mDevice.mType, mDevice.getAddress());
2956 status_t status = NO_ERROR;
2957 for (auto &patch : patches) {
2958 status = onCreatePatch(patch.first, patch.second);
2959 ALOGV("%s onCreatePatch status %d", __func__, status);
2960 if (status == BAD_VALUE) {
2961 return status;
2962 }
2963 }
2964 return status;
2965}
2966
2967status_t AudioFlinger::DeviceEffectProxy::onCreatePatch(
2968 audio_patch_handle_t patchHandle, const AudioFlinger::PatchPanel::Patch& patch) {
2969 status_t status = NAME_NOT_FOUND;
2970 sp<EffectHandle> handle;
2971 // only consider source[0] as this is the only "true" source of a patch
2972 status = checkPort(patch, &patch.mAudioPatch.sources[0], &handle);
2973 ALOGV("%s source checkPort status %d", __func__, status);
2974 for (uint32_t i = 0; i < patch.mAudioPatch.num_sinks && status == NAME_NOT_FOUND; i++) {
2975 status = checkPort(patch, &patch.mAudioPatch.sinks[i], &handle);
2976 ALOGV("%s sink %d checkPort status %d", __func__, i, status);
2977 }
2978 if (status == NO_ERROR || status == ALREADY_EXISTS) {
2979 Mutex::Autolock _l(mProxyLock);
2980 mEffectHandles.emplace(patchHandle, handle);
2981 }
2982 ALOGW_IF(status == BAD_VALUE,
2983 "%s cannot attach effect %s on patch %d", __func__, mDescriptor.name, patchHandle);
2984
2985 return status;
2986}
2987
2988status_t AudioFlinger::DeviceEffectProxy::checkPort(const PatchPanel::Patch& patch,
2989 const struct audio_port_config *port, sp <EffectHandle> *handle) {
2990
2991 ALOGV("%s type %d device type %d address %s device ID %d patch.isSoftware() %d",
2992 __func__, port->type, port->ext.device.type,
2993 port->ext.device.address, port->id, patch.isSoftware());
2994 if (port->type != AUDIO_PORT_TYPE_DEVICE || port->ext.device.type != mDevice.mType
2995 || port->ext.device.address != mDevice.mAddress) {
2996 return NAME_NOT_FOUND;
2997 }
2998 status_t status = NAME_NOT_FOUND;
2999
3000 if (mDescriptor.flags & EFFECT_FLAG_HW_ACC_TUNNEL) {
3001 Mutex::Autolock _l(mProxyLock);
3002 mDevicePort = *port;
3003 mHalEffect = new EffectModule(mMyCallback,
3004 const_cast<effect_descriptor_t *>(&mDescriptor),
3005 mMyCallback->newEffectId(), AUDIO_SESSION_DEVICE,
3006 false /* pinned */, port->id);
3007 if (audio_is_input_device(mDevice.mType)) {
3008 mHalEffect->setInputDevice(mDevice);
3009 } else {
3010 mHalEffect->setDevices({mDevice});
3011 }
3012 *handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/);
3013 status = (*handle)->initCheck();
3014 if (status == OK) {
3015 status = mHalEffect->addHandle((*handle).get());
3016 } else {
3017 mHalEffect.clear();
3018 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3019 }
3020 } else if (patch.isSoftware() || patch.thread().promote() != nullptr) {
3021 sp <ThreadBase> thread;
3022 if (audio_port_config_has_input_direction(port)) {
3023 if (patch.isSoftware()) {
3024 thread = patch.mRecord.thread();
3025 } else {
3026 thread = patch.thread().promote();
3027 }
3028 } else {
3029 if (patch.isSoftware()) {
3030 thread = patch.mPlayback.thread();
3031 } else {
3032 thread = patch.thread().promote();
3033 }
3034 }
3035 int enabled;
3036 *handle = thread->createEffect_l(nullptr, nullptr, 0, AUDIO_SESSION_DEVICE,
3037 const_cast<effect_descriptor_t *>(&mDescriptor),
3038 &enabled, &status, false);
3039 ALOGV("%s thread->createEffect_l status %d", __func__, status);
3040 } else {
3041 status = BAD_VALUE;
3042 }
3043 if (status == NO_ERROR || status == ALREADY_EXISTS) {
3044 if (isEnabled()) {
3045 (*handle)->enable();
3046 } else {
3047 (*handle)->disable();
3048 }
3049 }
3050 return status;
3051}
3052
3053void AudioFlinger::DeviceEffectProxy::onReleasePatch(audio_patch_handle_t patchHandle) {
3054 Mutex::Autolock _l(mProxyLock);
3055 mEffectHandles.erase(patchHandle);
3056}
3057
3058
3059size_t AudioFlinger::DeviceEffectProxy::removeEffect(const sp<EffectModule>& effect)
3060{
3061 Mutex::Autolock _l(mProxyLock);
3062 if (effect == mHalEffect) {
3063 mHalEffect.clear();
3064 mDevicePort.id = AUDIO_PORT_HANDLE_NONE;
3065 }
3066 return mHalEffect == nullptr ? 0 : 1;
3067}
3068
3069status_t AudioFlinger::DeviceEffectProxy::addEffectToHal(
3070 sp<EffectHalInterface> effect) {
3071 if (mHalEffect == nullptr) {
3072 return NO_INIT;
3073 }
3074 return mManagerCallback->addEffectToHal(
3075 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3076}
3077
3078status_t AudioFlinger::DeviceEffectProxy::removeEffectFromHal(
3079 sp<EffectHalInterface> effect) {
3080 if (mHalEffect == nullptr) {
3081 return NO_INIT;
3082 }
3083 return mManagerCallback->removeEffectFromHal(
3084 mDevicePort.id, mDevicePort.ext.device.hw_module, effect);
3085}
3086
3087bool AudioFlinger::DeviceEffectProxy::isOutput() const {
3088 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE) {
3089 return mDevicePort.role == AUDIO_PORT_ROLE_SINK;
3090 }
3091 return true;
3092}
3093
3094uint32_t AudioFlinger::DeviceEffectProxy::sampleRate() const {
3095 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3096 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) != 0) {
3097 return mDevicePort.sample_rate;
3098 }
3099 return DEFAULT_OUTPUT_SAMPLE_RATE;
3100}
3101
3102audio_channel_mask_t AudioFlinger::DeviceEffectProxy::channelMask() const {
3103 if (mDevicePort.id != AUDIO_PORT_HANDLE_NONE &&
3104 (mDevicePort.config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) != 0) {
3105 return mDevicePort.channel_mask;
3106 }
3107 return AUDIO_CHANNEL_OUT_STEREO;
3108}
3109
3110uint32_t AudioFlinger::DeviceEffectProxy::channelCount() const {
3111 if (isOutput()) {
3112 return audio_channel_count_from_out_mask(channelMask());
3113 }
3114 return audio_channel_count_from_in_mask(channelMask());
3115}
3116
3117void AudioFlinger::DeviceEffectProxy::dump(int fd, int spaces) {
3118 const Vector<String16> args;
3119 EffectBase::dump(fd, args);
3120
3121 const bool locked = dumpTryLock(mProxyLock);
3122 if (!locked) {
3123 String8 result("DeviceEffectProxy may be deadlocked\n");
3124 write(fd, result.string(), result.size());
3125 }
3126
3127 String8 outStr;
3128 if (mHalEffect != nullptr) {
3129 outStr.appendFormat("%*sHAL Effect Id: %d\n", spaces, "", mHalEffect->id());
3130 } else {
3131 outStr.appendFormat("%*sNO HAL Effect\n", spaces, "");
3132 }
3133 write(fd, outStr.string(), outStr.size());
3134 outStr.clear();
3135
3136 outStr.appendFormat("%*sSub Effects:\n", spaces, "");
3137 write(fd, outStr.string(), outStr.size());
3138 outStr.clear();
3139
3140 for (const auto& iter : mEffectHandles) {
3141 outStr.appendFormat("%*sEffect for patch handle %d:\n", spaces + 2, "", iter.first);
3142 write(fd, outStr.string(), outStr.size());
3143 outStr.clear();
3144 sp<EffectBase> effect = iter.second->effect().promote();
3145 if (effect != nullptr) {
3146 effect->dump(fd, args);
3147 }
3148 }
3149
3150 if (locked) {
3151 mLock.unlock();
3152 }
3153}
3154
3155#undef LOG_TAG
3156#define LOG_TAG "AudioFlinger::DeviceEffectProxy::ProxyCallback"
3157
3158int AudioFlinger::DeviceEffectProxy::ProxyCallback::newEffectId() {
3159 return mManagerCallback->newEffectId();
3160}
3161
3162
3163bool AudioFlinger::DeviceEffectProxy::ProxyCallback::disconnectEffectHandle(
3164 EffectHandle *handle, bool unpinIfLast) {
3165 sp<EffectBase> effectBase = handle->effect().promote();
3166 if (effectBase == nullptr) {
3167 return false;
3168 }
3169
3170 sp<EffectModule> effect = effectBase->asEffectModule();
3171 if (effect == nullptr) {
3172 return false;
3173 }
3174
3175 // restore suspended effects if the disconnected handle was enabled and the last one.
3176 bool remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
3177 if (remove) {
3178 sp<DeviceEffectProxy> proxy = mProxy.promote();
3179 if (proxy != nullptr) {
3180 proxy->removeEffect(effect);
3181 }
3182 if (handle->enabled()) {
3183 effectBase->checkSuspendOnEffectEnabled(false, false /*threadLocked*/);
3184 }
3185 }
3186 return true;
3187}
3188
3189status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::createEffectHal(
3190 const effect_uuid_t *pEffectUuid, int32_t sessionId, int32_t deviceId,
3191 sp<EffectHalInterface> *effect) {
3192 return mManagerCallback->createEffectHal(pEffectUuid, sessionId, deviceId, effect);
3193}
3194
3195status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::addEffectToHal(
3196 sp<EffectHalInterface> effect) {
3197 sp<DeviceEffectProxy> proxy = mProxy.promote();
3198 if (proxy == nullptr) {
3199 return NO_INIT;
3200 }
3201 return proxy->addEffectToHal(effect);
3202}
3203
3204status_t AudioFlinger::DeviceEffectProxy::ProxyCallback::removeEffectFromHal(
3205 sp<EffectHalInterface> effect) {
3206 sp<DeviceEffectProxy> proxy = mProxy.promote();
3207 if (proxy == nullptr) {
3208 return NO_INIT;
3209 }
3210 return proxy->addEffectToHal(effect);
3211}
3212
3213bool AudioFlinger::DeviceEffectProxy::ProxyCallback::isOutput() const {
3214 sp<DeviceEffectProxy> proxy = mProxy.promote();
3215 if (proxy == nullptr) {
3216 return true;
3217 }
3218 return proxy->isOutput();
3219}
3220
3221uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::sampleRate() const {
3222 sp<DeviceEffectProxy> proxy = mProxy.promote();
3223 if (proxy == nullptr) {
3224 return DEFAULT_OUTPUT_SAMPLE_RATE;
3225 }
3226 return proxy->sampleRate();
3227}
3228
3229audio_channel_mask_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelMask() const {
3230 sp<DeviceEffectProxy> proxy = mProxy.promote();
3231 if (proxy == nullptr) {
3232 return AUDIO_CHANNEL_OUT_STEREO;
3233 }
3234 return proxy->channelMask();
3235}
3236
3237uint32_t AudioFlinger::DeviceEffectProxy::ProxyCallback::channelCount() const {
3238 sp<DeviceEffectProxy> proxy = mProxy.promote();
3239 if (proxy == nullptr) {
3240 return 2;
3241 }
3242 return proxy->channelCount();
3243}
3244
Glenn Kasten63238ef2015-03-02 15:50:29 -08003245} // namespace android